blob: 164fb4ce4bc4848ed00c207a9e31ed091de3896b [file] [log] [blame]
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001//===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the MachO-specific dumper for llvm-objdump.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm-objdump.h"
Kevin Enderby98c9acc2014-09-16 18:00:57 +000015#include "llvm-c/Disassembler.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000016#include "llvm/ADT/STLExtras.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000017#include "llvm/ADT/StringExtras.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000018#include "llvm/ADT/Triple.h"
Kevin Enderby04bf6932014-10-28 23:39:46 +000019#include "llvm/Config/config.h"
Zachary Turner6489d7b2015-04-23 17:37:47 +000020#include "llvm/DebugInfo/DIContext.h"
21#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000022#include "llvm/MC/MCAsmInfo.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000023#include "llvm/MC/MCContext.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000024#include "llvm/MC/MCDisassembler.h"
25#include "llvm/MC/MCInst.h"
26#include "llvm/MC/MCInstPrinter.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000027#include "llvm/MC/MCInstrDesc.h"
28#include "llvm/MC/MCInstrInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000029#include "llvm/MC/MCRegisterInfo.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000030#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000031#include "llvm/Object/MachO.h"
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +000032#include "llvm/Object/MachOUniversal.h"
Rafael Espindola9b709252013-04-13 01:45:40 +000033#include "llvm/Support/Casting.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000034#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/Debug.h"
Tim Northover4bd286a2014-08-01 13:07:19 +000036#include "llvm/Support/Endian.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000037#include "llvm/Support/Format.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000038#include "llvm/Support/FormattedStream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000039#include "llvm/Support/GraphWriter.h"
Kevin Enderby9a509442015-01-27 21:28:24 +000040#include "llvm/Support/LEB128.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000041#include "llvm/Support/MachO.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000042#include "llvm/Support/MemoryBuffer.h"
43#include "llvm/Support/TargetRegistry.h"
44#include "llvm/Support/TargetSelect.h"
45#include "llvm/Support/raw_ostream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000046#include <algorithm>
47#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000048#include <system_error>
Kevin Enderby04bf6932014-10-28 23:39:46 +000049
50#if HAVE_CXXABI_H
51#include <cxxabi.h>
52#endif
53
Benjamin Kramer43a772e2011-09-19 17:56:04 +000054using namespace llvm;
55using namespace object;
56
57static cl::opt<bool>
Kevin Enderbyb28ed012014-10-29 21:28:24 +000058 UseDbg("g",
59 cl::desc("Print line information from debug info if available"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000060
Kevin Enderbyb28ed012014-10-29 21:28:24 +000061static cl::opt<std::string> DSYMFile("dsym",
62 cl::desc("Use .dSYM file for debug info"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000063
Kevin Enderbyb28ed012014-10-29 21:28:24 +000064static cl::opt<bool> FullLeadingAddr("full-leading-addr",
65 cl::desc("Print full leading address"));
Kevin Enderbybf246f52014-09-24 23:08:22 +000066
Kevin Enderbyab5e6c92015-03-17 21:07:39 +000067static cl::opt<bool> NoLeadingAddr("no-leading-addr",
68 cl::desc("Print no leading address"));
69
Kevin Enderby13023a12015-01-15 23:19:11 +000070cl::opt<bool> llvm::UniversalHeaders("universal-headers",
71 cl::desc("Print Mach-O universal headers "
72 "(requires -macho)"));
73
Kevin Enderby131d1772015-01-09 19:22:37 +000074cl::opt<bool>
Kevin Enderby13023a12015-01-15 23:19:11 +000075 llvm::ArchiveHeaders("archive-headers",
76 cl::desc("Print archive headers for Mach-O archives "
77 "(requires -macho)"));
Kevin Enderby131d1772015-01-09 19:22:37 +000078
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000079cl::opt<bool>
Kevin Enderby8972e482015-04-30 20:30:42 +000080 ArchiveMemberOffsets("archive-member-offsets",
81 cl::desc("Print the offset to each archive member for "
82 "Mach-O archives (requires -macho and "
83 "-archive-headers)"));
84
85cl::opt<bool>
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000086 llvm::IndirectSymbols("indirect-symbols",
87 cl::desc("Print indirect symbol table for Mach-O "
88 "objects (requires -macho)"));
89
Kevin Enderby69fe98d2015-01-23 18:52:17 +000090cl::opt<bool>
91 llvm::DataInCode("data-in-code",
92 cl::desc("Print the data in code table for Mach-O objects "
93 "(requires -macho)"));
94
Kevin Enderby9a509442015-01-27 21:28:24 +000095cl::opt<bool>
96 llvm::LinkOptHints("link-opt-hints",
97 cl::desc("Print the linker optimization hints for "
98 "Mach-O objects (requires -macho)"));
99
Kevin Enderbycd66be52015-03-11 22:06:32 +0000100cl::opt<bool>
101 llvm::InfoPlist("info-plist",
102 cl::desc("Print the info plist section as strings for "
103 "Mach-O objects (requires -macho)"));
104
Kevin Enderbyf0640752015-03-13 17:56:32 +0000105cl::opt<bool>
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000106 llvm::DylibsUsed("dylibs-used",
107 cl::desc("Print the shared libraries used for linked "
108 "Mach-O files (requires -macho)"));
109
110cl::opt<bool>
111 llvm::DylibId("dylib-id",
112 cl::desc("Print the shared library's id for the dylib Mach-O "
113 "file (requires -macho)"));
114
115cl::opt<bool>
Kevin Enderbyf0640752015-03-13 17:56:32 +0000116 llvm::NonVerbose("non-verbose",
117 cl::desc("Print the info for Mach-O objects in "
118 "non-verbose or numeric form (requires -macho)"));
119
Kevin Enderby0fc11822015-04-01 20:57:01 +0000120cl::opt<bool>
121 llvm::ObjcMetaData("objc-meta-data",
122 cl::desc("Print the Objective-C runtime meta data for "
123 "Mach-O files (requires -macho)"));
124
Kevin Enderby6a221752015-03-17 17:10:57 +0000125cl::opt<std::string> llvm::DisSymName(
126 "dis-symname",
127 cl::desc("disassemble just this symbol's instructions (requires -macho"));
128
Kevin Enderby8e29ec92015-03-17 22:26:11 +0000129static cl::opt<bool> NoSymbolicOperands(
130 "no-symbolic-operands",
131 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
132
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000133static cl::list<std::string>
134 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
135 cl::ZeroOrMore);
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000136
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000137bool ArchAll = false;
138
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000139static std::string ThumbTripleName;
140
141static const Target *GetTarget(const MachOObjectFile *MachOObj,
142 const char **McpuDefault,
143 const Target **ThumbTarget) {
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000144 // Figure out the target triple.
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000145 if (TripleName.empty()) {
146 llvm::Triple TT("unknown-unknown-unknown");
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000147 llvm::Triple ThumbTriple = Triple();
148 TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000149 TripleName = TT.str();
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000150 ThumbTripleName = ThumbTriple.str();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000151 }
152
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000153 // Get the target specific parser.
154 std::string Error;
155 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000156 if (TheTarget && ThumbTripleName.empty())
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000157 return TheTarget;
158
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000159 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
160 if (*ThumbTarget)
161 return TheTarget;
162
163 errs() << "llvm-objdump: error: unable to get target for '";
164 if (!TheTarget)
165 errs() << TripleName;
166 else
167 errs() << ThumbTripleName;
168 errs() << "', see --version and --triple.\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000169 return nullptr;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000170}
171
Owen Andersond9243c42011-10-17 21:37:35 +0000172struct SymbolSorter {
173 bool operator()(const SymbolRef &A, const SymbolRef &B) {
Rafael Espindoladea00162015-07-03 17:44:18 +0000174 uint64_t AAddr = (A.getType() != SymbolRef::ST_Function) ? 0 : A.getValue();
175 uint64_t BAddr = (B.getType() != SymbolRef::ST_Function) ? 0 : B.getValue();
Owen Andersond9243c42011-10-17 21:37:35 +0000176 return AAddr < BAddr;
177 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000178};
179
Kevin Enderby273ae012013-06-06 17:20:50 +0000180// Types for the storted data in code table that is built before disassembly
181// and the predicate function to sort them.
182typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
183typedef std::vector<DiceTableEntry> DiceTable;
184typedef DiceTable::iterator dice_table_iterator;
185
Kevin Enderby930fdc72014-11-06 19:00:13 +0000186// This is used to search for a data in code table entry for the PC being
187// disassembled. The j parameter has the PC in j.first. A single data in code
188// table entry can cover many bytes for each of its Kind's. So if the offset,
189// aka the i.first value, of the data in code table entry plus its Length
190// covers the PC being searched for this will return true. If not it will
191// return false.
David Majnemerea9b8ee2014-11-04 08:41:48 +0000192static bool compareDiceTableEntries(const DiceTableEntry &i,
193 const DiceTableEntry &j) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000194 uint16_t Length;
195 i.second.getLength(Length);
196
197 return j.first >= i.first && j.first < i.first + Length;
Kevin Enderby273ae012013-06-06 17:20:50 +0000198}
199
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000200static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
Kevin Enderby930fdc72014-11-06 19:00:13 +0000201 unsigned short Kind) {
202 uint32_t Value, Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000203
204 switch (Kind) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000205 default:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000206 case MachO::DICE_KIND_DATA:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000207 if (Length >= 4) {
208 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000209 dumpBytes(makeArrayRef(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000210 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000211 outs() << "\t.long " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000212 Size = 4;
213 } else if (Length >= 2) {
214 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000215 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000216 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000217 outs() << "\t.short " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000218 Size = 2;
219 } else {
220 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000221 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000222 Value = bytes[0];
223 outs() << "\t.byte " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000224 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000225 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000226 if (Kind == MachO::DICE_KIND_DATA)
227 outs() << "\t@ KIND_DATA\n";
228 else
229 outs() << "\t@ data in code kind = " << Kind << "\n";
Kevin Enderby273ae012013-06-06 17:20:50 +0000230 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000231 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000232 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000233 dumpBytes(makeArrayRef(bytes, 1), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000234 Value = bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000235 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
236 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000237 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000238 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000239 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000240 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000241 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000242 outs() << "\t.short " << format("%5u", Value & 0xffff)
243 << "\t@ KIND_JUMP_TABLE16\n";
244 Size = 2;
Kevin Enderby273ae012013-06-06 17:20:50 +0000245 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000246 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000247 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
248 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000249 dumpBytes(makeArrayRef(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000250 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000251 outs() << "\t.long " << Value;
252 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
253 outs() << "\t@ KIND_JUMP_TABLE32\n";
254 else
255 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
256 Size = 4;
Kevin Enderby273ae012013-06-06 17:20:50 +0000257 break;
258 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000259 return Size;
Kevin Enderby273ae012013-06-06 17:20:50 +0000260}
261
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000262static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000263 std::vector<SectionRef> &Sections,
264 std::vector<SymbolRef> &Symbols,
265 SmallVectorImpl<uint64_t> &FoundFns,
266 uint64_t &BaseSegmentAddress) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000267 for (const SymbolRef &Symbol : MachOObj->symbols()) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000268 ErrorOr<StringRef> SymName = Symbol.getName();
269 if (std::error_code EC = SymName.getError())
270 report_fatal_error(EC.message());
271 if (!SymName->startswith("ltmp"))
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000272 Symbols.push_back(Symbol);
273 }
Owen Andersond9243c42011-10-17 21:37:35 +0000274
Alexey Samsonov48803e52014-03-13 14:37:36 +0000275 for (const SectionRef &Section : MachOObj->sections()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000276 StringRef SectName;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000277 Section.getName(SectName);
278 Sections.push_back(Section);
Owen Andersond9243c42011-10-17 21:37:35 +0000279 }
280
Kevin Enderby273ae012013-06-06 17:20:50 +0000281 bool BaseSegmentAddressSet = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000282 for (const auto &Command : MachOObj->load_commands()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000283 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000284 // We found a function starts segment, parse the addresses for later
285 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000286 MachO::linkedit_data_command LLC =
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000287 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000288
Charles Davis8bdfafd2013-09-01 04:28:48 +0000289 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000290 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
291 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000292 StringRef SegName = SLC.segname;
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000293 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
Kevin Enderby273ae012013-06-06 17:20:50 +0000294 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000295 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000296 }
297 }
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000298 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000299}
300
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000301static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
302 uint32_t n, uint32_t count,
303 uint32_t stride, uint64_t addr) {
304 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
305 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
306 if (n > nindirectsyms)
307 outs() << " (entries start past the end of the indirect symbol "
308 "table) (reserved1 field greater than the table size)";
309 else if (n + count > nindirectsyms)
310 outs() << " (entries extends past the end of the indirect symbol "
311 "table)";
312 outs() << "\n";
313 uint32_t cputype = O->getHeader().cputype;
314 if (cputype & MachO::CPU_ARCH_ABI64)
315 outs() << "address index";
316 else
317 outs() << "address index";
318 if (verbose)
319 outs() << " name\n";
320 else
321 outs() << "\n";
322 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
323 if (cputype & MachO::CPU_ARCH_ABI64)
324 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
325 else
326 outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
327 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
328 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
329 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
330 outs() << "LOCAL\n";
331 continue;
332 }
333 if (indirect_symbol ==
334 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
335 outs() << "LOCAL ABSOLUTE\n";
336 continue;
337 }
338 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
339 outs() << "ABSOLUTE\n";
340 continue;
341 }
342 outs() << format("%5u ", indirect_symbol);
Kevin Enderbyf0640752015-03-13 17:56:32 +0000343 if (verbose) {
344 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
345 if (indirect_symbol < Symtab.nsyms) {
346 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
347 SymbolRef Symbol = *Sym;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000348 ErrorOr<StringRef> SymName = Symbol.getName();
349 if (std::error_code EC = SymName.getError())
350 report_fatal_error(EC.message());
351 outs() << *SymName;
Kevin Enderbyf0640752015-03-13 17:56:32 +0000352 } else {
353 outs() << "?";
354 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000355 }
356 outs() << "\n";
357 }
358}
359
360static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000361 for (const auto &Load : O->load_commands()) {
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000362 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
363 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
364 for (unsigned J = 0; J < Seg.nsects; ++J) {
365 MachO::section_64 Sec = O->getSection64(Load, J);
366 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
367 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
368 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
369 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
370 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
371 section_type == MachO::S_SYMBOL_STUBS) {
372 uint32_t stride;
373 if (section_type == MachO::S_SYMBOL_STUBS)
374 stride = Sec.reserved2;
375 else
376 stride = 8;
377 if (stride == 0) {
378 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
379 << Sec.sectname << ") "
380 << "(size of stubs in reserved2 field is zero)\n";
381 continue;
382 }
383 uint32_t count = Sec.size / stride;
384 outs() << "Indirect symbols for (" << Sec.segname << ","
385 << Sec.sectname << ") " << count << " entries";
386 uint32_t n = Sec.reserved1;
387 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
388 }
389 }
390 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
391 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
392 for (unsigned J = 0; J < Seg.nsects; ++J) {
393 MachO::section Sec = O->getSection(Load, J);
394 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
395 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
396 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
397 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
398 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
399 section_type == MachO::S_SYMBOL_STUBS) {
400 uint32_t stride;
401 if (section_type == MachO::S_SYMBOL_STUBS)
402 stride = Sec.reserved2;
403 else
404 stride = 4;
405 if (stride == 0) {
406 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
407 << Sec.sectname << ") "
408 << "(size of stubs in reserved2 field is zero)\n";
409 continue;
410 }
411 uint32_t count = Sec.size / stride;
412 outs() << "Indirect symbols for (" << Sec.segname << ","
413 << Sec.sectname << ") " << count << " entries";
414 uint32_t n = Sec.reserved1;
415 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
416 }
417 }
418 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000419 }
420}
421
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000422static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
423 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
424 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
425 outs() << "Data in code table (" << nentries << " entries)\n";
426 outs() << "offset length kind\n";
427 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
428 ++DI) {
429 uint32_t Offset;
430 DI->getOffset(Offset);
431 outs() << format("0x%08" PRIx32, Offset) << " ";
432 uint16_t Length;
433 DI->getLength(Length);
434 outs() << format("%6u", Length) << " ";
435 uint16_t Kind;
436 DI->getKind(Kind);
437 if (verbose) {
438 switch (Kind) {
439 case MachO::DICE_KIND_DATA:
440 outs() << "DATA";
441 break;
442 case MachO::DICE_KIND_JUMP_TABLE8:
443 outs() << "JUMP_TABLE8";
444 break;
445 case MachO::DICE_KIND_JUMP_TABLE16:
446 outs() << "JUMP_TABLE16";
447 break;
448 case MachO::DICE_KIND_JUMP_TABLE32:
449 outs() << "JUMP_TABLE32";
450 break;
451 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
452 outs() << "ABS_JUMP_TABLE32";
453 break;
454 default:
455 outs() << format("0x%04" PRIx32, Kind);
456 break;
457 }
458 } else
459 outs() << format("0x%04" PRIx32, Kind);
460 outs() << "\n";
461 }
462}
463
Kevin Enderby9a509442015-01-27 21:28:24 +0000464static void PrintLinkOptHints(MachOObjectFile *O) {
465 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
466 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
467 uint32_t nloh = LohLC.datasize;
468 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
469 for (uint32_t i = 0; i < nloh;) {
470 unsigned n;
471 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
472 i += n;
473 outs() << " identifier " << identifier << " ";
474 if (i >= nloh)
475 return;
476 switch (identifier) {
477 case 1:
478 outs() << "AdrpAdrp\n";
479 break;
480 case 2:
481 outs() << "AdrpLdr\n";
482 break;
483 case 3:
484 outs() << "AdrpAddLdr\n";
485 break;
486 case 4:
487 outs() << "AdrpLdrGotLdr\n";
488 break;
489 case 5:
490 outs() << "AdrpAddStr\n";
491 break;
492 case 6:
493 outs() << "AdrpLdrGotStr\n";
494 break;
495 case 7:
496 outs() << "AdrpAdd\n";
497 break;
498 case 8:
499 outs() << "AdrpLdrGot\n";
500 break;
501 default:
502 outs() << "Unknown identifier value\n";
503 break;
504 }
505 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
506 i += n;
507 outs() << " narguments " << narguments << "\n";
508 if (i >= nloh)
509 return;
510
511 for (uint32_t j = 0; j < narguments; j++) {
512 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
513 i += n;
514 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
515 if (i >= nloh)
516 return;
517 }
518 }
519}
520
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000521static void PrintDylibs(MachOObjectFile *O, bool JustId) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000522 unsigned Index = 0;
523 for (const auto &Load : O->load_commands()) {
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000524 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
525 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
526 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
527 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
528 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
529 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
530 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
531 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
532 if (dl.dylib.name < dl.cmdsize) {
533 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
534 if (JustId)
535 outs() << p << "\n";
536 else {
537 outs() << "\t" << p;
538 outs() << " (compatibility version "
539 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
540 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
541 << (dl.dylib.compatibility_version & 0xff) << ",";
542 outs() << " current version "
543 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
544 << ((dl.dylib.current_version >> 8) & 0xff) << "."
545 << (dl.dylib.current_version & 0xff) << ")\n";
546 }
547 } else {
548 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
549 if (Load.C.cmd == MachO::LC_ID_DYLIB)
550 outs() << "LC_ID_DYLIB ";
551 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
552 outs() << "LC_LOAD_DYLIB ";
553 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
554 outs() << "LC_LOAD_WEAK_DYLIB ";
555 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
556 outs() << "LC_LAZY_LOAD_DYLIB ";
557 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
558 outs() << "LC_REEXPORT_DYLIB ";
559 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
560 outs() << "LC_LOAD_UPWARD_DYLIB ";
561 else
562 outs() << "LC_??? ";
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000563 outs() << "command " << Index++ << "\n";
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000564 }
565 }
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000566 }
567}
568
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000569typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
570
571static void CreateSymbolAddressMap(MachOObjectFile *O,
572 SymbolAddressMap *AddrMap) {
573 // Create a map of symbol addresses to symbol names.
574 for (const SymbolRef &Symbol : O->symbols()) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000575 SymbolRef::Type ST = Symbol.getType();
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000576 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
577 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +0000578 uint64_t Address = Symbol.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000579 ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
580 if (std::error_code EC = SymNameOrErr.getError())
581 report_fatal_error(EC.message());
582 StringRef SymName = *SymNameOrErr;
Kevin Enderby846c0002015-04-16 17:19:59 +0000583 if (!SymName.startswith(".objc"))
584 (*AddrMap)[Address] = SymName;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000585 }
586 }
587}
588
589// GuessSymbolName is passed the address of what might be a symbol and a
590// pointer to the SymbolAddressMap. It returns the name of a symbol
591// with that address or nullptr if no symbol is found with that address.
592static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
593 const char *SymbolName = nullptr;
594 // A DenseMap can't lookup up some values.
595 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
596 StringRef name = AddrMap->lookup(value);
597 if (!name.empty())
598 SymbolName = name.data();
599 }
600 return SymbolName;
601}
602
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000603static void DumpCstringChar(const char c) {
604 char p[2];
605 p[0] = c;
606 p[1] = '\0';
607 outs().write_escaped(p);
608}
609
Kevin Enderby10ba0412015-02-04 21:38:42 +0000610static void DumpCstringSection(MachOObjectFile *O, const char *sect,
611 uint32_t sect_size, uint64_t sect_addr,
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000612 bool print_addresses) {
Kevin Enderby10ba0412015-02-04 21:38:42 +0000613 for (uint32_t i = 0; i < sect_size; i++) {
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000614 if (print_addresses) {
615 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000616 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000617 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000618 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000619 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000620 for (; i < sect_size && sect[i] != '\0'; i++)
621 DumpCstringChar(sect[i]);
Kevin Enderby10ba0412015-02-04 21:38:42 +0000622 if (i < sect_size && sect[i] == '\0')
623 outs() << "\n";
624 }
625}
626
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000627static void DumpLiteral4(uint32_t l, float f) {
628 outs() << format("0x%08" PRIx32, l);
629 if ((l & 0x7f800000) != 0x7f800000)
630 outs() << format(" (%.16e)\n", f);
631 else {
632 if (l == 0x7f800000)
633 outs() << " (+Infinity)\n";
634 else if (l == 0xff800000)
635 outs() << " (-Infinity)\n";
636 else if ((l & 0x00400000) == 0x00400000)
637 outs() << " (non-signaling Not-a-Number)\n";
638 else
639 outs() << " (signaling Not-a-Number)\n";
640 }
641}
642
643static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
644 uint32_t sect_size, uint64_t sect_addr,
645 bool print_addresses) {
646 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
647 if (print_addresses) {
648 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000649 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000650 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000651 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000652 }
653 float f;
654 memcpy(&f, sect + i, sizeof(float));
655 if (O->isLittleEndian() != sys::IsLittleEndianHost)
656 sys::swapByteOrder(f);
657 uint32_t l;
658 memcpy(&l, sect + i, sizeof(uint32_t));
659 if (O->isLittleEndian() != sys::IsLittleEndianHost)
660 sys::swapByteOrder(l);
661 DumpLiteral4(l, f);
662 }
663}
664
665static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
666 double d) {
667 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
668 uint32_t Hi, Lo;
669 if (O->isLittleEndian()) {
670 Hi = l1;
671 Lo = l0;
672 } else {
673 Hi = l0;
674 Lo = l1;
675 }
676 // Hi is the high word, so this is equivalent to if(isfinite(d))
677 if ((Hi & 0x7ff00000) != 0x7ff00000)
678 outs() << format(" (%.16e)\n", d);
679 else {
680 if (Hi == 0x7ff00000 && Lo == 0)
681 outs() << " (+Infinity)\n";
682 else if (Hi == 0xfff00000 && Lo == 0)
683 outs() << " (-Infinity)\n";
684 else if ((Hi & 0x00080000) == 0x00080000)
685 outs() << " (non-signaling Not-a-Number)\n";
686 else
687 outs() << " (signaling Not-a-Number)\n";
688 }
689}
690
691static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
692 uint32_t sect_size, uint64_t sect_addr,
693 bool print_addresses) {
694 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
695 if (print_addresses) {
696 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000697 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000698 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000699 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000700 }
701 double d;
702 memcpy(&d, sect + i, sizeof(double));
703 if (O->isLittleEndian() != sys::IsLittleEndianHost)
704 sys::swapByteOrder(d);
705 uint32_t l0, l1;
706 memcpy(&l0, sect + i, sizeof(uint32_t));
707 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
708 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
709 sys::swapByteOrder(l0);
710 sys::swapByteOrder(l1);
711 }
712 DumpLiteral8(O, l0, l1, d);
713 }
714}
715
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000716static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
717 outs() << format("0x%08" PRIx32, l0) << " ";
718 outs() << format("0x%08" PRIx32, l1) << " ";
719 outs() << format("0x%08" PRIx32, l2) << " ";
720 outs() << format("0x%08" PRIx32, l3) << "\n";
721}
722
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000723static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
724 uint32_t sect_size, uint64_t sect_addr,
725 bool print_addresses) {
726 for (uint32_t i = 0; i < sect_size; i += 16) {
727 if (print_addresses) {
728 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000729 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000730 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000731 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000732 }
733 uint32_t l0, l1, l2, l3;
734 memcpy(&l0, sect + i, sizeof(uint32_t));
735 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
736 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
737 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
738 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
739 sys::swapByteOrder(l0);
740 sys::swapByteOrder(l1);
741 sys::swapByteOrder(l2);
742 sys::swapByteOrder(l3);
743 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000744 DumpLiteral16(l0, l1, l2, l3);
745 }
746}
747
748static void DumpLiteralPointerSection(MachOObjectFile *O,
749 const SectionRef &Section,
750 const char *sect, uint32_t sect_size,
751 uint64_t sect_addr,
752 bool print_addresses) {
753 // Collect the literal sections in this Mach-O file.
754 std::vector<SectionRef> LiteralSections;
755 for (const SectionRef &Section : O->sections()) {
756 DataRefImpl Ref = Section.getRawDataRefImpl();
757 uint32_t section_type;
758 if (O->is64Bit()) {
759 const MachO::section_64 Sec = O->getSection64(Ref);
760 section_type = Sec.flags & MachO::SECTION_TYPE;
761 } else {
762 const MachO::section Sec = O->getSection(Ref);
763 section_type = Sec.flags & MachO::SECTION_TYPE;
764 }
765 if (section_type == MachO::S_CSTRING_LITERALS ||
766 section_type == MachO::S_4BYTE_LITERALS ||
767 section_type == MachO::S_8BYTE_LITERALS ||
768 section_type == MachO::S_16BYTE_LITERALS)
769 LiteralSections.push_back(Section);
770 }
771
772 // Set the size of the literal pointer.
773 uint32_t lp_size = O->is64Bit() ? 8 : 4;
774
Eric Christopher572e03a2015-06-19 01:53:21 +0000775 // Collect the external relocation symbols for the literal pointers.
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000776 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
777 for (const RelocationRef &Reloc : Section.relocations()) {
778 DataRefImpl Rel;
779 MachO::any_relocation_info RE;
780 bool isExtern = false;
781 Rel = Reloc.getRawDataRefImpl();
782 RE = O->getRelocation(Rel);
783 isExtern = O->getPlainRelocationExternal(RE);
784 if (isExtern) {
Rafael Espindola96d071c2015-06-29 23:29:12 +0000785 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000786 symbol_iterator RelocSym = Reloc.getSymbol();
787 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
788 }
789 }
790 array_pod_sort(Relocs.begin(), Relocs.end());
791
792 // Dump each literal pointer.
793 for (uint32_t i = 0; i < sect_size; i += lp_size) {
794 if (print_addresses) {
795 if (O->is64Bit())
796 outs() << format("%016" PRIx64, sect_addr + i) << " ";
797 else
798 outs() << format("%08" PRIx64, sect_addr + i) << " ";
799 }
800 uint64_t lp;
801 if (O->is64Bit()) {
802 memcpy(&lp, sect + i, sizeof(uint64_t));
803 if (O->isLittleEndian() != sys::IsLittleEndianHost)
804 sys::swapByteOrder(lp);
805 } else {
806 uint32_t li;
807 memcpy(&li, sect + i, sizeof(uint32_t));
808 if (O->isLittleEndian() != sys::IsLittleEndianHost)
809 sys::swapByteOrder(li);
810 lp = li;
811 }
812
813 // First look for an external relocation entry for this literal pointer.
David Blaikie33dd45d02015-03-23 18:39:02 +0000814 auto Reloc = std::find_if(
815 Relocs.begin(), Relocs.end(),
816 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
817 if (Reloc != Relocs.end()) {
818 symbol_iterator RelocSym = Reloc->second;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000819 ErrorOr<StringRef> SymName = RelocSym->getName();
820 if (std::error_code EC = SymName.getError())
821 report_fatal_error(EC.message());
822 outs() << "external relocation entry for symbol:" << *SymName << "\n";
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000823 continue;
David Blaikie33dd45d02015-03-23 18:39:02 +0000824 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000825
826 // For local references see what the section the literal pointer points to.
David Blaikie33dd45d02015-03-23 18:39:02 +0000827 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
828 [&](const SectionRef &R) {
829 return lp >= R.getAddress() &&
830 lp < R.getAddress() + R.getSize();
831 });
832 if (Sect == LiteralSections.end()) {
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000833 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
David Blaikie33dd45d02015-03-23 18:39:02 +0000834 continue;
835 }
836
837 uint64_t SectAddress = Sect->getAddress();
838 uint64_t SectSize = Sect->getSize();
839
840 StringRef SectName;
841 Sect->getName(SectName);
842 DataRefImpl Ref = Sect->getRawDataRefImpl();
843 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
844 outs() << SegmentName << ":" << SectName << ":";
845
846 uint32_t section_type;
847 if (O->is64Bit()) {
848 const MachO::section_64 Sec = O->getSection64(Ref);
849 section_type = Sec.flags & MachO::SECTION_TYPE;
850 } else {
851 const MachO::section Sec = O->getSection(Ref);
852 section_type = Sec.flags & MachO::SECTION_TYPE;
853 }
854
855 StringRef BytesStr;
856 Sect->getContents(BytesStr);
857 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
858
859 switch (section_type) {
860 case MachO::S_CSTRING_LITERALS:
861 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
862 i++) {
863 DumpCstringChar(Contents[i]);
864 }
865 outs() << "\n";
866 break;
867 case MachO::S_4BYTE_LITERALS:
868 float f;
869 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
870 uint32_t l;
871 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
872 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
873 sys::swapByteOrder(f);
874 sys::swapByteOrder(l);
875 }
876 DumpLiteral4(l, f);
877 break;
878 case MachO::S_8BYTE_LITERALS: {
879 double d;
880 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
881 uint32_t l0, l1;
882 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
883 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
884 sizeof(uint32_t));
885 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
886 sys::swapByteOrder(f);
887 sys::swapByteOrder(l0);
888 sys::swapByteOrder(l1);
889 }
890 DumpLiteral8(O, l0, l1, d);
891 break;
892 }
893 case MachO::S_16BYTE_LITERALS: {
894 uint32_t l0, l1, l2, l3;
895 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
896 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
897 sizeof(uint32_t));
898 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
899 sizeof(uint32_t));
900 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
901 sizeof(uint32_t));
902 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
903 sys::swapByteOrder(l0);
904 sys::swapByteOrder(l1);
905 sys::swapByteOrder(l2);
906 sys::swapByteOrder(l3);
907 }
908 DumpLiteral16(l0, l1, l2, l3);
909 break;
910 }
911 }
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000912 }
913}
914
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000915static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
916 uint32_t sect_size, uint64_t sect_addr,
917 SymbolAddressMap *AddrMap,
918 bool verbose) {
919 uint32_t stride;
920 if (O->is64Bit())
921 stride = sizeof(uint64_t);
922 else
923 stride = sizeof(uint32_t);
924 for (uint32_t i = 0; i < sect_size; i += stride) {
925 const char *SymbolName = nullptr;
926 if (O->is64Bit()) {
927 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
928 uint64_t pointer_value;
929 memcpy(&pointer_value, sect + i, stride);
930 if (O->isLittleEndian() != sys::IsLittleEndianHost)
931 sys::swapByteOrder(pointer_value);
932 outs() << format("0x%016" PRIx64, pointer_value);
933 if (verbose)
934 SymbolName = GuessSymbolName(pointer_value, AddrMap);
935 } else {
936 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
937 uint32_t pointer_value;
938 memcpy(&pointer_value, sect + i, stride);
939 if (O->isLittleEndian() != sys::IsLittleEndianHost)
940 sys::swapByteOrder(pointer_value);
941 outs() << format("0x%08" PRIx32, pointer_value);
942 if (verbose)
943 SymbolName = GuessSymbolName(pointer_value, AddrMap);
944 }
945 if (SymbolName)
946 outs() << " " << SymbolName;
947 outs() << "\n";
948 }
949}
950
951static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
952 uint32_t size, uint64_t addr) {
953 uint32_t cputype = O->getHeader().cputype;
954 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
955 uint32_t j;
956 for (uint32_t i = 0; i < size; i += j, addr += j) {
957 if (O->is64Bit())
958 outs() << format("%016" PRIx64, addr) << "\t";
959 else
Kevin Enderbyf0640752015-03-13 17:56:32 +0000960 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000961 for (j = 0; j < 16 && i + j < size; j++) {
962 uint8_t byte_word = *(sect + i + j);
963 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
964 }
965 outs() << "\n";
966 }
967 } else {
968 uint32_t j;
969 for (uint32_t i = 0; i < size; i += j, addr += j) {
970 if (O->is64Bit())
971 outs() << format("%016" PRIx64, addr) << "\t";
972 else
973 outs() << format("%08" PRIx64, sect) << "\t";
974 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
975 j += sizeof(int32_t)) {
976 if (i + j + sizeof(int32_t) < size) {
977 uint32_t long_word;
978 memcpy(&long_word, sect + i + j, sizeof(int32_t));
979 if (O->isLittleEndian() != sys::IsLittleEndianHost)
980 sys::swapByteOrder(long_word);
981 outs() << format("%08" PRIx32, long_word) << " ";
982 } else {
983 for (uint32_t k = 0; i + j + k < size; k++) {
984 uint8_t byte_word = *(sect + i + j);
985 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
986 }
987 }
988 }
989 outs() << "\n";
990 }
991 }
992}
993
Kevin Enderby95df54c2015-02-04 01:01:38 +0000994static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
995 StringRef DisSegName, StringRef DisSectName);
Kevin Enderby4ad9bde2015-04-16 22:33:20 +0000996static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
997 uint32_t size, uint32_t addr);
Kevin Enderby95df54c2015-02-04 01:01:38 +0000998
999static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1000 bool verbose) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001001 SymbolAddressMap AddrMap;
1002 if (verbose)
1003 CreateSymbolAddressMap(O, &AddrMap);
1004
Colin LeMahieufcc32762015-07-29 19:08:10 +00001005 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1006 StringRef DumpSection = FilterSections[i];
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001007 std::pair<StringRef, StringRef> DumpSegSectName;
1008 DumpSegSectName = DumpSection.split(',');
1009 StringRef DumpSegName, DumpSectName;
1010 if (DumpSegSectName.second.size()) {
1011 DumpSegName = DumpSegSectName.first;
1012 DumpSectName = DumpSegSectName.second;
1013 } else {
1014 DumpSegName = "";
1015 DumpSectName = DumpSegSectName.first;
1016 }
1017 for (const SectionRef &Section : O->sections()) {
1018 StringRef SectName;
1019 Section.getName(SectName);
1020 DataRefImpl Ref = Section.getRawDataRefImpl();
1021 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1022 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1023 (SectName == DumpSectName)) {
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001024
Kevin Enderby95df54c2015-02-04 01:01:38 +00001025 uint32_t section_flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001026 if (O->is64Bit()) {
1027 const MachO::section_64 Sec = O->getSection64(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001028 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001029
1030 } else {
1031 const MachO::section Sec = O->getSection(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001032 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001033 }
Kevin Enderby95df54c2015-02-04 01:01:38 +00001034 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001035
1036 StringRef BytesStr;
1037 Section.getContents(BytesStr);
1038 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1039 uint32_t sect_size = BytesStr.size();
1040 uint64_t sect_addr = Section.getAddress();
1041
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001042 outs() << "Contents of (" << SegName << "," << SectName
1043 << ") section\n";
1044
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001045 if (verbose) {
Kevin Enderby95df54c2015-02-04 01:01:38 +00001046 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1047 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1048 DisassembleMachO(Filename, O, SegName, SectName);
1049 continue;
1050 }
Kevin Enderbycd66be52015-03-11 22:06:32 +00001051 if (SegName == "__TEXT" && SectName == "__info_plist") {
1052 outs() << sect;
1053 continue;
1054 }
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001055 if (SegName == "__OBJC" && SectName == "__protocol") {
1056 DumpProtocolSection(O, sect, sect_size, sect_addr);
1057 continue;
1058 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001059 switch (section_type) {
1060 case MachO::S_REGULAR:
1061 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1062 break;
1063 case MachO::S_ZEROFILL:
1064 outs() << "zerofill section and has no contents in the file\n";
1065 break;
Kevin Enderby10ba0412015-02-04 21:38:42 +00001066 case MachO::S_CSTRING_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001067 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby10ba0412015-02-04 21:38:42 +00001068 break;
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001069 case MachO::S_4BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001070 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001071 break;
1072 case MachO::S_8BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001073 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001074 break;
1075 case MachO::S_16BYTE_LITERALS:
Kevin Enderby0fc11822015-04-01 20:57:01 +00001076 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1077 break;
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001078 case MachO::S_LITERAL_POINTERS:
1079 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001080 !NoLeadingAddr);
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001081 break;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001082 case MachO::S_MOD_INIT_FUNC_POINTERS:
1083 case MachO::S_MOD_TERM_FUNC_POINTERS:
1084 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1085 verbose);
1086 break;
1087 default:
1088 outs() << "Unknown section type ("
1089 << format("0x%08" PRIx32, section_type) << ")\n";
1090 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1091 break;
1092 }
1093 } else {
1094 if (section_type == MachO::S_ZEROFILL)
1095 outs() << "zerofill section and has no contents in the file\n";
1096 else
1097 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1098 }
1099 }
1100 }
1101 }
1102}
1103
Kevin Enderbycd66be52015-03-11 22:06:32 +00001104static void DumpInfoPlistSectionContents(StringRef Filename,
1105 MachOObjectFile *O) {
1106 for (const SectionRef &Section : O->sections()) {
1107 StringRef SectName;
1108 Section.getName(SectName);
1109 DataRefImpl Ref = Section.getRawDataRefImpl();
1110 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1111 if (SegName == "__TEXT" && SectName == "__info_plist") {
1112 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1113 StringRef BytesStr;
1114 Section.getContents(BytesStr);
1115 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1116 outs() << sect;
1117 return;
1118 }
1119 }
1120}
1121
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001122// checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1123// and if it is and there is a list of architecture flags is specified then
1124// check to make sure this Mach-O file is one of those architectures or all
1125// architectures were specified. If not then an error is generated and this
1126// routine returns false. Else it returns true.
1127static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1128 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1129 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1130 bool ArchFound = false;
1131 MachO::mach_header H;
1132 MachO::mach_header_64 H_64;
1133 Triple T;
1134 if (MachO->is64Bit()) {
1135 H_64 = MachO->MachOObjectFile::getHeader64();
1136 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1137 } else {
1138 H = MachO->MachOObjectFile::getHeader();
1139 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1140 }
1141 unsigned i;
1142 for (i = 0; i < ArchFlags.size(); ++i) {
1143 if (ArchFlags[i] == T.getArchName())
1144 ArchFound = true;
1145 break;
1146 }
1147 if (!ArchFound) {
1148 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1149 << "architecture: " + ArchFlags[i] + "\n";
1150 return false;
1151 }
1152 }
1153 return true;
1154}
1155
Kevin Enderby0fc11822015-04-01 20:57:01 +00001156static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1157
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001158// ProcessMachO() is passed a single opened Mach-O file, which may be an
1159// archive member and or in a slice of a universal file. It prints the
1160// the file name and header info and then processes it according to the
1161// command line options.
1162static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1163 StringRef ArchiveMemberName = StringRef(),
1164 StringRef ArchitectureName = StringRef()) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001165 // If we are doing some processing here on the Mach-O file print the header
1166 // info. And don't print it otherwise like in the case of printing the
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001167 // UniversalHeaders or ArchiveHeaders.
Kevin Enderby131d1772015-01-09 19:22:37 +00001168 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001169 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
Colin LeMahieufcc32762015-07-29 19:08:10 +00001170 DylibsUsed || DylibId || ObjcMetaData || (FilterSections.size() != 0)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001171 outs() << Filename;
1172 if (!ArchiveMemberName.empty())
1173 outs() << '(' << ArchiveMemberName << ')';
1174 if (!ArchitectureName.empty())
1175 outs() << " (architecture " << ArchitectureName << ")";
1176 outs() << ":\n";
1177 }
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001178
1179 if (Disassemble)
Kevin Enderby95df54c2015-02-04 01:01:38 +00001180 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001181 if (IndirectSymbols)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001182 PrintIndirectSymbols(MachOOF, !NonVerbose);
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001183 if (DataInCode)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001184 PrintDataInCodeTable(MachOOF, !NonVerbose);
Kevin Enderby9a509442015-01-27 21:28:24 +00001185 if (LinkOptHints)
1186 PrintLinkOptHints(MachOOF);
Kevin Enderby98da6132015-01-20 21:47:46 +00001187 if (Relocations)
1188 PrintRelocations(MachOOF);
1189 if (SectionHeaders)
1190 PrintSectionHeaders(MachOOF);
1191 if (SectionContents)
1192 PrintSectionContents(MachOOF);
Colin LeMahieufcc32762015-07-29 19:08:10 +00001193 if (FilterSections.size() != 0)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001194 DumpSectionContents(Filename, MachOOF, !NonVerbose);
Kevin Enderbycd66be52015-03-11 22:06:32 +00001195 if (InfoPlist)
1196 DumpInfoPlistSectionContents(Filename, MachOOF);
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001197 if (DylibsUsed)
1198 PrintDylibs(MachOOF, false);
1199 if (DylibId)
1200 PrintDylibs(MachOOF, true);
Kevin Enderby98da6132015-01-20 21:47:46 +00001201 if (SymbolTable)
1202 PrintSymbolTable(MachOOF);
1203 if (UnwindInfo)
1204 printMachOUnwindInfo(MachOOF);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001205 if (PrivateHeaders)
1206 printMachOFileHeader(MachOOF);
Kevin Enderby0fc11822015-04-01 20:57:01 +00001207 if (ObjcMetaData)
1208 printObjcMetaData(MachOOF, !NonVerbose);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001209 if (ExportsTrie)
1210 printExportsTrie(MachOOF);
1211 if (Rebase)
1212 printRebaseTable(MachOOF);
1213 if (Bind)
1214 printBindTable(MachOOF);
1215 if (LazyBind)
1216 printLazyBindTable(MachOOF);
1217 if (WeakBind)
1218 printWeakBindTable(MachOOF);
1219}
1220
Kevin Enderby131d1772015-01-09 19:22:37 +00001221// printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1222static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1223 outs() << " cputype (" << cputype << ")\n";
1224 outs() << " cpusubtype (" << cpusubtype << ")\n";
1225}
1226
1227// printCPUType() helps print_fat_headers by printing the cputype and
1228// pusubtype (symbolically for the one's it knows about).
1229static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1230 switch (cputype) {
1231 case MachO::CPU_TYPE_I386:
1232 switch (cpusubtype) {
1233 case MachO::CPU_SUBTYPE_I386_ALL:
1234 outs() << " cputype CPU_TYPE_I386\n";
1235 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1236 break;
1237 default:
1238 printUnknownCPUType(cputype, cpusubtype);
1239 break;
1240 }
1241 break;
1242 case MachO::CPU_TYPE_X86_64:
1243 switch (cpusubtype) {
1244 case MachO::CPU_SUBTYPE_X86_64_ALL:
1245 outs() << " cputype CPU_TYPE_X86_64\n";
1246 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1247 break;
1248 case MachO::CPU_SUBTYPE_X86_64_H:
1249 outs() << " cputype CPU_TYPE_X86_64\n";
1250 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1251 break;
1252 default:
1253 printUnknownCPUType(cputype, cpusubtype);
1254 break;
1255 }
1256 break;
1257 case MachO::CPU_TYPE_ARM:
1258 switch (cpusubtype) {
1259 case MachO::CPU_SUBTYPE_ARM_ALL:
1260 outs() << " cputype CPU_TYPE_ARM\n";
1261 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1262 break;
1263 case MachO::CPU_SUBTYPE_ARM_V4T:
1264 outs() << " cputype CPU_TYPE_ARM\n";
1265 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1266 break;
1267 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1268 outs() << " cputype CPU_TYPE_ARM\n";
1269 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1270 break;
1271 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1272 outs() << " cputype CPU_TYPE_ARM\n";
1273 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1274 break;
1275 case MachO::CPU_SUBTYPE_ARM_V6:
1276 outs() << " cputype CPU_TYPE_ARM\n";
1277 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1278 break;
1279 case MachO::CPU_SUBTYPE_ARM_V6M:
1280 outs() << " cputype CPU_TYPE_ARM\n";
1281 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1282 break;
1283 case MachO::CPU_SUBTYPE_ARM_V7:
1284 outs() << " cputype CPU_TYPE_ARM\n";
1285 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1286 break;
1287 case MachO::CPU_SUBTYPE_ARM_V7EM:
1288 outs() << " cputype CPU_TYPE_ARM\n";
1289 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1290 break;
1291 case MachO::CPU_SUBTYPE_ARM_V7K:
1292 outs() << " cputype CPU_TYPE_ARM\n";
1293 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1294 break;
1295 case MachO::CPU_SUBTYPE_ARM_V7M:
1296 outs() << " cputype CPU_TYPE_ARM\n";
1297 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1298 break;
1299 case MachO::CPU_SUBTYPE_ARM_V7S:
1300 outs() << " cputype CPU_TYPE_ARM\n";
1301 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1302 break;
1303 default:
1304 printUnknownCPUType(cputype, cpusubtype);
1305 break;
1306 }
1307 break;
1308 case MachO::CPU_TYPE_ARM64:
1309 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1310 case MachO::CPU_SUBTYPE_ARM64_ALL:
1311 outs() << " cputype CPU_TYPE_ARM64\n";
1312 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1313 break;
1314 default:
1315 printUnknownCPUType(cputype, cpusubtype);
1316 break;
1317 }
1318 break;
1319 default:
1320 printUnknownCPUType(cputype, cpusubtype);
1321 break;
1322 }
1323}
1324
1325static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1326 bool verbose) {
1327 outs() << "Fat headers\n";
1328 if (verbose)
1329 outs() << "fat_magic FAT_MAGIC\n";
1330 else
1331 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1332
1333 uint32_t nfat_arch = UB->getNumberOfObjects();
1334 StringRef Buf = UB->getData();
1335 uint64_t size = Buf.size();
1336 uint64_t big_size = sizeof(struct MachO::fat_header) +
1337 nfat_arch * sizeof(struct MachO::fat_arch);
1338 outs() << "nfat_arch " << UB->getNumberOfObjects();
1339 if (nfat_arch == 0)
1340 outs() << " (malformed, contains zero architecture types)\n";
1341 else if (big_size > size)
1342 outs() << " (malformed, architectures past end of file)\n";
1343 else
1344 outs() << "\n";
1345
1346 for (uint32_t i = 0; i < nfat_arch; ++i) {
1347 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1348 uint32_t cputype = OFA.getCPUType();
1349 uint32_t cpusubtype = OFA.getCPUSubType();
1350 outs() << "architecture ";
1351 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1352 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1353 uint32_t other_cputype = other_OFA.getCPUType();
1354 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
Kevin Enderby0512bd72015-01-09 21:55:03 +00001355 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
Kevin Enderby131d1772015-01-09 19:22:37 +00001356 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
Kevin Enderby0512bd72015-01-09 21:55:03 +00001357 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001358 outs() << "(illegal duplicate architecture) ";
1359 break;
Kevin Enderby0512bd72015-01-09 21:55:03 +00001360 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001361 }
1362 if (verbose) {
1363 outs() << OFA.getArchTypeName() << "\n";
1364 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1365 } else {
1366 outs() << i << "\n";
1367 outs() << " cputype " << cputype << "\n";
1368 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1369 << "\n";
1370 }
1371 if (verbose &&
1372 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1373 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1374 else
1375 outs() << " capabilities "
1376 << format("0x%" PRIx32,
1377 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1378 outs() << " offset " << OFA.getOffset();
1379 if (OFA.getOffset() > size)
1380 outs() << " (past end of file)";
1381 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1382 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1383 outs() << "\n";
1384 outs() << " size " << OFA.getSize();
1385 big_size = OFA.getOffset() + OFA.getSize();
1386 if (big_size > size)
1387 outs() << " (past end of file)";
1388 outs() << "\n";
1389 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1390 << ")\n";
1391 }
1392}
1393
Kevin Enderby13023a12015-01-15 23:19:11 +00001394static void printArchiveChild(Archive::Child &C, bool verbose,
1395 bool print_offset) {
1396 if (print_offset)
1397 outs() << C.getChildOffset() << "\t";
1398 sys::fs::perms Mode = C.getAccessMode();
1399 if (verbose) {
1400 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1401 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1402 outs() << "-";
Davide Italianobb9a6cc2015-09-07 20:47:03 +00001403 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
1404 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
1405 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
1406 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
1407 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
1408 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
1409 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
1410 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
1411 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
Kevin Enderby13023a12015-01-15 23:19:11 +00001412 } else {
1413 outs() << format("0%o ", Mode);
1414 }
1415
1416 unsigned UID = C.getUID();
1417 outs() << format("%3d/", UID);
1418 unsigned GID = C.getGID();
1419 outs() << format("%-3d ", GID);
Kevin Enderbyc12718932015-01-16 22:10:36 +00001420 uint64_t Size = C.getRawSize();
Kevin Enderby479ee612015-01-23 21:02:44 +00001421 outs() << format("%5" PRId64, Size) << " ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001422
1423 StringRef RawLastModified = C.getRawLastModified();
1424 if (verbose) {
1425 unsigned Seconds;
1426 if (RawLastModified.getAsInteger(10, Seconds))
1427 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1428 else {
1429 // Since cime(3) returns a 26 character string of the form:
1430 // "Sun Sep 16 01:03:52 1973\n\0"
1431 // just print 24 characters.
1432 time_t t = Seconds;
1433 outs() << format("%.24s ", ctime(&t));
1434 }
1435 } else {
1436 outs() << RawLastModified << " ";
1437 }
1438
1439 if (verbose) {
1440 ErrorOr<StringRef> NameOrErr = C.getName();
1441 if (NameOrErr.getError()) {
1442 StringRef RawName = C.getRawName();
1443 outs() << RawName << "\n";
1444 } else {
1445 StringRef Name = NameOrErr.get();
1446 outs() << Name << "\n";
1447 }
1448 } else {
1449 StringRef RawName = C.getRawName();
1450 outs() << RawName << "\n";
1451 }
1452}
1453
1454static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
1455 if (A->hasSymbolTable()) {
1456 Archive::child_iterator S = A->getSymbolTableChild();
1457 Archive::Child C = *S;
1458 printArchiveChild(C, verbose, print_offset);
1459 }
1460 for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E;
1461 ++I) {
1462 Archive::Child C = *I;
1463 printArchiveChild(C, verbose, print_offset);
1464 }
1465}
1466
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001467// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1468// -arch flags selecting just those slices as specified by them and also parses
1469// archive files. Then for each individual Mach-O file ProcessMachO() is
1470// called to process the file based on the command line options.
1471void llvm::ParseInputMachO(StringRef Filename) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001472 // Check for -arch all and verifiy the -arch flags are valid.
1473 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1474 if (ArchFlags[i] == "all") {
1475 ArchAll = true;
1476 } else {
1477 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1478 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1479 "'for the -arch option\n";
1480 return;
1481 }
1482 }
1483 }
1484
1485 // Attempt to open the binary.
1486 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1487 if (std::error_code EC = BinaryOrErr.getError()) {
1488 errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n";
Rafael Espindolade882cd2014-12-03 23:29:34 +00001489 return;
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001490 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001491 Binary &Bin = *BinaryOrErr.get().getBinary();
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001492
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001493 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1494 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001495 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001496 printArchiveHeaders(A, !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001497 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1498 I != E; ++I) {
1499 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary();
1500 if (ChildOrErr.getError())
1501 continue;
1502 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1503 if (!checkMachOAndArchFlags(O, Filename))
1504 return;
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001505 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001506 }
1507 }
1508 return;
1509 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001510 if (UniversalHeaders) {
1511 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
Kevin Enderbyf0640752015-03-13 17:56:32 +00001512 printMachOUniversalHeaders(UB, !NonVerbose);
Kevin Enderby131d1772015-01-09 19:22:37 +00001513 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001514 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1515 // If we have a list of architecture flags specified dump only those.
1516 if (!ArchAll && ArchFlags.size() != 0) {
1517 // Look for a slice in the universal binary that matches each ArchFlag.
1518 bool ArchFound;
1519 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1520 ArchFound = false;
1521 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1522 E = UB->end_objects();
1523 I != E; ++I) {
1524 if (ArchFlags[i] == I->getArchTypeName()) {
1525 ArchFound = true;
1526 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1527 I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001528 std::string ArchitectureName = "";
1529 if (ArchFlags.size() > 1)
1530 ArchitectureName = I->getArchTypeName();
1531 if (ObjOrErr) {
1532 ObjectFile &O = *ObjOrErr.get();
1533 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001534 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001535 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1536 I->getAsArchive()) {
1537 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001538 outs() << "Archive : " << Filename;
1539 if (!ArchitectureName.empty())
1540 outs() << " (architecture " << ArchitectureName << ")";
1541 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001542 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001543 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001544 for (Archive::child_iterator AI = A->child_begin(),
1545 AE = A->child_end();
1546 AI != AE; ++AI) {
1547 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1548 if (ChildOrErr.getError())
1549 continue;
1550 if (MachOObjectFile *O =
1551 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001552 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001553 }
1554 }
1555 }
1556 }
1557 if (!ArchFound) {
1558 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1559 << "architecture: " + ArchFlags[i] + "\n";
1560 return;
1561 }
1562 }
1563 return;
1564 }
1565 // No architecture flags were specified so if this contains a slice that
1566 // matches the host architecture dump only that.
1567 if (!ArchAll) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001568 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1569 E = UB->end_objects();
1570 I != E; ++I) {
Kevin Enderby0512bd72015-01-09 21:55:03 +00001571 if (MachOObjectFile::getHostArch().getArchName() ==
1572 I->getArchTypeName()) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001573 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001574 std::string ArchiveName;
1575 ArchiveName.clear();
1576 if (ObjOrErr) {
1577 ObjectFile &O = *ObjOrErr.get();
1578 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001579 ProcessMachO(Filename, MachOOF);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001580 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1581 I->getAsArchive()) {
1582 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001583 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001584 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001585 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001586 for (Archive::child_iterator AI = A->child_begin(),
1587 AE = A->child_end();
1588 AI != AE; ++AI) {
1589 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1590 if (ChildOrErr.getError())
1591 continue;
1592 if (MachOObjectFile *O =
1593 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001594 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001595 }
1596 }
1597 return;
1598 }
1599 }
1600 }
1601 // Either all architectures have been specified or none have been specified
1602 // and this does not contain the host architecture so dump all the slices.
1603 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1604 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1605 E = UB->end_objects();
1606 I != E; ++I) {
1607 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001608 std::string ArchitectureName = "";
1609 if (moreThanOneArch)
1610 ArchitectureName = I->getArchTypeName();
1611 if (ObjOrErr) {
1612 ObjectFile &Obj = *ObjOrErr.get();
1613 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001614 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001615 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1616 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001617 outs() << "Archive : " << Filename;
1618 if (!ArchitectureName.empty())
1619 outs() << " (architecture " << ArchitectureName << ")";
1620 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001621 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001622 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001623 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1624 AI != AE; ++AI) {
1625 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1626 if (ChildOrErr.getError())
1627 continue;
1628 if (MachOObjectFile *O =
1629 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1630 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001631 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1632 ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001633 }
1634 }
1635 }
1636 }
1637 return;
1638 }
1639 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1640 if (!checkMachOAndArchFlags(O, Filename))
1641 return;
1642 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001643 ProcessMachO(Filename, MachOOF);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001644 } else
1645 errs() << "llvm-objdump: '" << Filename << "': "
1646 << "Object is not a Mach-O file type.\n";
1647 } else
1648 errs() << "llvm-objdump: '" << Filename << "': "
1649 << "Unrecognized file type.\n";
Rafael Espindola9b709252013-04-13 01:45:40 +00001650}
1651
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001652typedef std::pair<uint64_t, const char *> BindInfoEntry;
1653typedef std::vector<BindInfoEntry> BindTable;
1654typedef BindTable::iterator bind_table_iterator;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001655
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001656// The block of info used by the Symbolizer call backs.
1657struct DisassembleInfo {
1658 bool verbose;
1659 MachOObjectFile *O;
1660 SectionRef S;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001661 SymbolAddressMap *AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001662 std::vector<SectionRef> *Sections;
1663 const char *class_name;
1664 const char *selector_name;
1665 char *method;
Kevin Enderby04bf6932014-10-28 23:39:46 +00001666 char *demangled_name;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001667 uint64_t adrp_addr;
1668 uint32_t adrp_inst;
Kevin Enderby078be602014-10-23 19:53:12 +00001669 BindTable *bindtable;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001670};
1671
1672// SymbolizerGetOpInfo() is the operand information call back function.
1673// This is called to get the symbolic information for operand(s) of an
1674// instruction when it is being done. This routine does this from
1675// the relocation information, symbol table, etc. That block of information
1676// is a pointer to the struct DisassembleInfo that was passed when the
1677// disassembler context was created and passed to back to here when
1678// called back by the disassembler for instruction operands that could have
1679// relocation information. The address of the instruction containing operand is
1680// at the Pc parameter. The immediate value the operand has is passed in
1681// op_info->Value and is at Offset past the start of the instruction and has a
1682// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1683// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1684// names and addends of the symbolic expression to add for the operand. The
1685// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1686// information is returned then this function returns 1 else it returns 0.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00001687static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1688 uint64_t Size, int TagType, void *TagBuf) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001689 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1690 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001691 uint64_t value = op_info->Value;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001692
1693 // Make sure all fields returned are zero if we don't set them.
1694 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1695 op_info->Value = value;
1696
1697 // If the TagType is not the value 1 which it code knows about or if no
1698 // verbose symbolic information is wanted then just return 0, indicating no
1699 // information is being returned.
David Blaikie33dd45d02015-03-23 18:39:02 +00001700 if (TagType != 1 || !info->verbose)
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001701 return 0;
1702
1703 unsigned int Arch = info->O->getArch();
1704 if (Arch == Triple::x86) {
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001705 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1706 return 0;
1707 // First search the section's relocation entries (if any) for an entry
1708 // for this section offset.
1709 uint32_t sect_addr = info->S.getAddress();
1710 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1711 bool reloc_found = false;
1712 DataRefImpl Rel;
1713 MachO::any_relocation_info RE;
1714 bool isExtern = false;
1715 SymbolRef Symbol;
1716 bool r_scattered = false;
1717 uint32_t r_value, pair_r_value, r_type;
1718 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001719 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001720 if (RelocOffset == sect_offset) {
1721 Rel = Reloc.getRawDataRefImpl();
1722 RE = info->O->getRelocation(Rel);
Kevin Enderby3eb73e12014-11-11 19:16:45 +00001723 r_type = info->O->getAnyRelocationType(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001724 r_scattered = info->O->isRelocationScattered(RE);
1725 if (r_scattered) {
1726 r_value = info->O->getScatteredRelocationValue(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001727 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1728 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1729 DataRefImpl RelNext = Rel;
1730 info->O->moveRelocationNext(RelNext);
1731 MachO::any_relocation_info RENext;
1732 RENext = info->O->getRelocation(RelNext);
1733 if (info->O->isRelocationScattered(RENext))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001734 pair_r_value = info->O->getScatteredRelocationValue(RENext);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001735 else
1736 return 0;
1737 }
1738 } else {
1739 isExtern = info->O->getPlainRelocationExternal(RE);
1740 if (isExtern) {
1741 symbol_iterator RelocSym = Reloc.getSymbol();
1742 Symbol = *RelocSym;
1743 }
1744 }
1745 reloc_found = true;
1746 break;
1747 }
1748 }
1749 if (reloc_found && isExtern) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001750 ErrorOr<StringRef> SymName = Symbol.getName();
1751 if (std::error_code EC = SymName.getError())
1752 report_fatal_error(EC.message());
1753 const char *name = SymName->data();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001754 op_info->AddSymbol.Present = 1;
1755 op_info->AddSymbol.Name = name;
1756 // For i386 extern relocation entries the value in the instruction is
1757 // the offset from the symbol, and value is already set in op_info->Value.
1758 return 1;
1759 }
1760 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1761 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001762 const char *add = GuessSymbolName(r_value, info->AddrMap);
1763 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001764 uint32_t offset = value - (r_value - pair_r_value);
1765 op_info->AddSymbol.Present = 1;
1766 if (add != nullptr)
1767 op_info->AddSymbol.Name = add;
1768 else
1769 op_info->AddSymbol.Value = r_value;
1770 op_info->SubtractSymbol.Present = 1;
1771 if (sub != nullptr)
1772 op_info->SubtractSymbol.Name = sub;
1773 else
1774 op_info->SubtractSymbol.Value = pair_r_value;
1775 op_info->Value = offset;
1776 return 1;
1777 }
1778 // TODO:
1779 // Second search the external relocation entries of a fully linked image
1780 // (if any) for an entry that matches this segment offset.
1781 // uint32_t seg_offset = (Pc + Offset);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001782 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001783 }
1784 if (Arch == Triple::x86_64) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001785 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1786 return 0;
1787 // First search the section's relocation entries (if any) for an entry
1788 // for this section offset.
Rafael Espindola80291272014-10-08 15:28:58 +00001789 uint64_t sect_addr = info->S.getAddress();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001790 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1791 bool reloc_found = false;
1792 DataRefImpl Rel;
1793 MachO::any_relocation_info RE;
1794 bool isExtern = false;
1795 SymbolRef Symbol;
1796 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001797 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001798 if (RelocOffset == sect_offset) {
1799 Rel = Reloc.getRawDataRefImpl();
1800 RE = info->O->getRelocation(Rel);
1801 // NOTE: Scattered relocations don't exist on x86_64.
1802 isExtern = info->O->getPlainRelocationExternal(RE);
1803 if (isExtern) {
1804 symbol_iterator RelocSym = Reloc.getSymbol();
1805 Symbol = *RelocSym;
1806 }
1807 reloc_found = true;
1808 break;
1809 }
1810 }
1811 if (reloc_found && isExtern) {
1812 // The Value passed in will be adjusted by the Pc if the instruction
1813 // adds the Pc. But for x86_64 external relocation entries the Value
1814 // is the offset from the external symbol.
1815 if (info->O->getAnyRelocationPCRel(RE))
1816 op_info->Value -= Pc + Offset + Size;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001817 ErrorOr<StringRef> SymName = Symbol.getName();
1818 if (std::error_code EC = SymName.getError())
1819 report_fatal_error(EC.message());
1820 const char *name = SymName->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001821 unsigned Type = info->O->getAnyRelocationType(RE);
1822 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1823 DataRefImpl RelNext = Rel;
1824 info->O->moveRelocationNext(RelNext);
1825 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1826 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1827 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1828 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1829 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1830 op_info->SubtractSymbol.Present = 1;
1831 op_info->SubtractSymbol.Name = name;
1832 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1833 Symbol = *RelocSymNext;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001834 ErrorOr<StringRef> SymNameNext = Symbol.getName();
1835 if (std::error_code EC = SymNameNext.getError())
1836 report_fatal_error(EC.message());
1837 name = SymNameNext->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001838 }
1839 }
1840 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1841 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1842 op_info->AddSymbol.Present = 1;
1843 op_info->AddSymbol.Name = name;
1844 return 1;
1845 }
1846 // TODO:
1847 // Second search the external relocation entries of a fully linked image
1848 // (if any) for an entry that matches this segment offset.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001849 // uint64_t seg_offset = (Pc + Offset);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001850 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001851 }
1852 if (Arch == Triple::arm) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001853 if (Offset != 0 || (Size != 4 && Size != 2))
1854 return 0;
1855 // First search the section's relocation entries (if any) for an entry
1856 // for this section offset.
1857 uint32_t sect_addr = info->S.getAddress();
1858 uint32_t sect_offset = (Pc + Offset) - sect_addr;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001859 DataRefImpl Rel;
1860 MachO::any_relocation_info RE;
1861 bool isExtern = false;
1862 SymbolRef Symbol;
1863 bool r_scattered = false;
1864 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
David Blaikie33dd45d02015-03-23 18:39:02 +00001865 auto Reloc =
1866 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1867 [&](const RelocationRef &Reloc) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001868 uint64_t RelocOffset = Reloc.getOffset();
David Blaikie33dd45d02015-03-23 18:39:02 +00001869 return RelocOffset == sect_offset;
1870 });
1871
1872 if (Reloc == info->S.relocations().end())
1873 return 0;
1874
1875 Rel = Reloc->getRawDataRefImpl();
1876 RE = info->O->getRelocation(Rel);
1877 r_length = info->O->getAnyRelocationLength(RE);
1878 r_scattered = info->O->isRelocationScattered(RE);
1879 if (r_scattered) {
1880 r_value = info->O->getScatteredRelocationValue(RE);
1881 r_type = info->O->getScatteredRelocationType(RE);
1882 } else {
1883 r_type = info->O->getAnyRelocationType(RE);
1884 isExtern = info->O->getPlainRelocationExternal(RE);
1885 if (isExtern) {
1886 symbol_iterator RelocSym = Reloc->getSymbol();
1887 Symbol = *RelocSym;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001888 }
1889 }
David Blaikie33dd45d02015-03-23 18:39:02 +00001890 if (r_type == MachO::ARM_RELOC_HALF ||
1891 r_type == MachO::ARM_RELOC_SECTDIFF ||
1892 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
1893 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1894 DataRefImpl RelNext = Rel;
1895 info->O->moveRelocationNext(RelNext);
1896 MachO::any_relocation_info RENext;
1897 RENext = info->O->getRelocation(RelNext);
1898 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
1899 if (info->O->isRelocationScattered(RENext))
1900 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1901 }
1902
1903 if (isExtern) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001904 ErrorOr<StringRef> SymName = Symbol.getName();
1905 if (std::error_code EC = SymName.getError())
1906 report_fatal_error(EC.message());
1907 const char *name = SymName->data();
Kevin Enderby930fdc72014-11-06 19:00:13 +00001908 op_info->AddSymbol.Present = 1;
1909 op_info->AddSymbol.Name = name;
Sylvestre Ledru648cced2015-02-05 17:00:23 +00001910 switch (r_type) {
1911 case MachO::ARM_RELOC_HALF:
1912 if ((r_length & 0x1) == 1) {
1913 op_info->Value = value << 16 | other_half;
1914 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1915 } else {
1916 op_info->Value = other_half << 16 | value;
1917 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Sylvestre Ledrufe0c7ad2015-02-05 16:35:44 +00001918 }
Sylvestre Ledru648cced2015-02-05 17:00:23 +00001919 break;
1920 default:
1921 break;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001922 }
1923 return 1;
1924 }
1925 // If we have a branch that is not an external relocation entry then
1926 // return 0 so the code in tryAddingSymbolicOperand() can use the
1927 // SymbolLookUp call back with the branch target address to look up the
1928 // symbol and possiblity add an annotation for a symbol stub.
David Blaikie33dd45d02015-03-23 18:39:02 +00001929 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
1930 r_type == MachO::ARM_THUMB_RELOC_BR22))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001931 return 0;
1932
1933 uint32_t offset = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001934 if (r_type == MachO::ARM_RELOC_HALF ||
1935 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1936 if ((r_length & 0x1) == 1)
1937 value = value << 16 | other_half;
1938 else
1939 value = other_half << 16 | value;
1940 }
1941 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
1942 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
1943 offset = value - r_value;
1944 value = r_value;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001945 }
1946
David Blaikie33dd45d02015-03-23 18:39:02 +00001947 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001948 if ((r_length & 0x1) == 1)
1949 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1950 else
1951 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001952 const char *add = GuessSymbolName(r_value, info->AddrMap);
1953 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00001954 int32_t offset = value - (r_value - pair_r_value);
1955 op_info->AddSymbol.Present = 1;
1956 if (add != nullptr)
1957 op_info->AddSymbol.Name = add;
1958 else
1959 op_info->AddSymbol.Value = r_value;
1960 op_info->SubtractSymbol.Present = 1;
1961 if (sub != nullptr)
1962 op_info->SubtractSymbol.Name = sub;
1963 else
1964 op_info->SubtractSymbol.Value = pair_r_value;
1965 op_info->Value = offset;
1966 return 1;
1967 }
1968
Kevin Enderby930fdc72014-11-06 19:00:13 +00001969 op_info->AddSymbol.Present = 1;
1970 op_info->Value = offset;
David Blaikie33dd45d02015-03-23 18:39:02 +00001971 if (r_type == MachO::ARM_RELOC_HALF) {
1972 if ((r_length & 0x1) == 1)
1973 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1974 else
1975 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001976 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001977 const char *add = GuessSymbolName(value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00001978 if (add != nullptr) {
1979 op_info->AddSymbol.Name = add;
1980 return 1;
1981 }
1982 op_info->AddSymbol.Value = value;
1983 return 1;
David Blaikie33dd45d02015-03-23 18:39:02 +00001984 }
1985 if (Arch == Triple::aarch64) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001986 if (Offset != 0 || Size != 4)
1987 return 0;
1988 // First search the section's relocation entries (if any) for an entry
1989 // for this section offset.
1990 uint64_t sect_addr = info->S.getAddress();
1991 uint64_t sect_offset = (Pc + Offset) - sect_addr;
David Blaikie33dd45d02015-03-23 18:39:02 +00001992 auto Reloc =
1993 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1994 [&](const RelocationRef &Reloc) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001995 uint64_t RelocOffset = Reloc.getOffset();
David Blaikie33dd45d02015-03-23 18:39:02 +00001996 return RelocOffset == sect_offset;
1997 });
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001998
David Blaikie33dd45d02015-03-23 18:39:02 +00001999 if (Reloc == info->S.relocations().end())
2000 return 0;
2001
2002 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2003 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2004 uint32_t r_type = info->O->getAnyRelocationType(RE);
2005 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2006 DataRefImpl RelNext = Rel;
2007 info->O->moveRelocationNext(RelNext);
2008 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2009 if (value == 0) {
2010 value = info->O->getPlainRelocationSymbolNum(RENext);
2011 op_info->Value = value;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002012 }
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002013 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002014 // NOTE: Scattered relocations don't exist on arm64.
2015 if (!info->O->getPlainRelocationExternal(RE))
2016 return 0;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002017 ErrorOr<StringRef> SymName = Reloc->getSymbol()->getName();
2018 if (std::error_code EC = SymName.getError())
2019 report_fatal_error(EC.message());
2020 const char *name = SymName->data();
David Blaikie33dd45d02015-03-23 18:39:02 +00002021 op_info->AddSymbol.Present = 1;
2022 op_info->AddSymbol.Name = name;
2023
2024 switch (r_type) {
2025 case MachO::ARM64_RELOC_PAGE21:
2026 /* @page */
2027 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2028 break;
2029 case MachO::ARM64_RELOC_PAGEOFF12:
2030 /* @pageoff */
2031 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2032 break;
2033 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2034 /* @gotpage */
2035 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2036 break;
2037 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2038 /* @gotpageoff */
2039 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2040 break;
2041 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2042 /* @tvlppage is not implemented in llvm-mc */
2043 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2044 break;
2045 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2046 /* @tvlppageoff is not implemented in llvm-mc */
2047 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2048 break;
2049 default:
2050 case MachO::ARM64_RELOC_BRANCH26:
2051 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2052 break;
2053 }
2054 return 1;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002055 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002056 return 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002057}
2058
Kevin Enderbybf246f52014-09-24 23:08:22 +00002059// GuessCstringPointer is passed the address of what might be a pointer to a
2060// literal string in a cstring section. If that address is in a cstring section
2061// it returns a pointer to that string. Else it returns nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002062static const char *GuessCstringPointer(uint64_t ReferenceValue,
2063 struct DisassembleInfo *info) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002064 for (const auto &Load : info->O->load_commands()) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00002065 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2066 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2067 for (unsigned J = 0; J < Seg.nsects; ++J) {
2068 MachO::section_64 Sec = info->O->getSection64(Load, J);
2069 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2070 if (section_type == MachO::S_CSTRING_LITERALS &&
2071 ReferenceValue >= Sec.addr &&
2072 ReferenceValue < Sec.addr + Sec.size) {
2073 uint64_t sect_offset = ReferenceValue - Sec.addr;
2074 uint64_t object_offset = Sec.offset + sect_offset;
2075 StringRef MachOContents = info->O->getData();
2076 uint64_t object_size = MachOContents.size();
2077 const char *object_addr = (const char *)MachOContents.data();
2078 if (object_offset < object_size) {
2079 const char *name = object_addr + object_offset;
2080 return name;
2081 } else {
2082 return nullptr;
2083 }
2084 }
2085 }
2086 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2087 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2088 for (unsigned J = 0; J < Seg.nsects; ++J) {
2089 MachO::section Sec = info->O->getSection(Load, J);
2090 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2091 if (section_type == MachO::S_CSTRING_LITERALS &&
2092 ReferenceValue >= Sec.addr &&
2093 ReferenceValue < Sec.addr + Sec.size) {
2094 uint64_t sect_offset = ReferenceValue - Sec.addr;
2095 uint64_t object_offset = Sec.offset + sect_offset;
2096 StringRef MachOContents = info->O->getData();
2097 uint64_t object_size = MachOContents.size();
2098 const char *object_addr = (const char *)MachOContents.data();
2099 if (object_offset < object_size) {
2100 const char *name = object_addr + object_offset;
2101 return name;
2102 } else {
2103 return nullptr;
2104 }
2105 }
2106 }
2107 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00002108 }
2109 return nullptr;
2110}
2111
Kevin Enderby85974882014-09-26 22:20:44 +00002112// GuessIndirectSymbol returns the name of the indirect symbol for the
2113// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2114// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2115// symbol name being referenced by the stub or pointer.
2116static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2117 struct DisassembleInfo *info) {
Kevin Enderby85974882014-09-26 22:20:44 +00002118 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2119 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002120 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby85974882014-09-26 22:20:44 +00002121 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2122 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2123 for (unsigned J = 0; J < Seg.nsects; ++J) {
2124 MachO::section_64 Sec = info->O->getSection64(Load, J);
2125 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2126 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2127 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2128 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2129 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2130 section_type == MachO::S_SYMBOL_STUBS) &&
2131 ReferenceValue >= Sec.addr &&
2132 ReferenceValue < Sec.addr + Sec.size) {
2133 uint32_t stride;
2134 if (section_type == MachO::S_SYMBOL_STUBS)
2135 stride = Sec.reserved2;
2136 else
2137 stride = 8;
2138 if (stride == 0)
2139 return nullptr;
2140 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2141 if (index < Dysymtab.nindirectsyms) {
2142 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002143 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002144 if (indirect_symbol < Symtab.nsyms) {
2145 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2146 SymbolRef Symbol = *Sym;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002147 ErrorOr<StringRef> SymName = Symbol.getName();
2148 if (std::error_code EC = SymName.getError())
2149 report_fatal_error(EC.message());
2150 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002151 return name;
2152 }
2153 }
2154 }
2155 }
2156 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2157 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2158 for (unsigned J = 0; J < Seg.nsects; ++J) {
2159 MachO::section Sec = info->O->getSection(Load, J);
2160 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2161 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2162 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2163 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2164 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2165 section_type == MachO::S_SYMBOL_STUBS) &&
2166 ReferenceValue >= Sec.addr &&
2167 ReferenceValue < Sec.addr + Sec.size) {
2168 uint32_t stride;
2169 if (section_type == MachO::S_SYMBOL_STUBS)
2170 stride = Sec.reserved2;
2171 else
2172 stride = 4;
2173 if (stride == 0)
2174 return nullptr;
2175 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2176 if (index < Dysymtab.nindirectsyms) {
2177 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002178 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002179 if (indirect_symbol < Symtab.nsyms) {
2180 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2181 SymbolRef Symbol = *Sym;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002182 ErrorOr<StringRef> SymName = Symbol.getName();
2183 if (std::error_code EC = SymName.getError())
2184 report_fatal_error(EC.message());
2185 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002186 return name;
2187 }
2188 }
2189 }
2190 }
2191 }
Kevin Enderby85974882014-09-26 22:20:44 +00002192 }
2193 return nullptr;
2194}
2195
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002196// method_reference() is called passing it the ReferenceName that might be
2197// a reference it to an Objective-C method call. If so then it allocates and
2198// assembles a method call string with the values last seen and saved in
2199// the DisassembleInfo's class_name and selector_name fields. This is saved
2200// into the method field of the info and any previous string is free'ed.
2201// Then the class_name field in the info is set to nullptr. The method call
2202// string is set into ReferenceName and ReferenceType is set to
2203// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2204// then both ReferenceType and ReferenceName are left unchanged.
2205static void method_reference(struct DisassembleInfo *info,
2206 uint64_t *ReferenceType,
2207 const char **ReferenceName) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002208 unsigned int Arch = info->O->getArch();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002209 if (*ReferenceName != nullptr) {
2210 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002211 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002212 if (info->method != nullptr)
2213 free(info->method);
2214 if (info->class_name != nullptr) {
2215 info->method = (char *)malloc(5 + strlen(info->class_name) +
2216 strlen(info->selector_name));
2217 if (info->method != nullptr) {
2218 strcpy(info->method, "+[");
2219 strcat(info->method, info->class_name);
2220 strcat(info->method, " ");
2221 strcat(info->method, info->selector_name);
2222 strcat(info->method, "]");
2223 *ReferenceName = info->method;
2224 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2225 }
2226 } else {
2227 info->method = (char *)malloc(9 + strlen(info->selector_name));
2228 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002229 if (Arch == Triple::x86_64)
2230 strcpy(info->method, "-[%rdi ");
2231 else if (Arch == Triple::aarch64)
2232 strcpy(info->method, "-[x0 ");
2233 else
2234 strcpy(info->method, "-[r? ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002235 strcat(info->method, info->selector_name);
2236 strcat(info->method, "]");
2237 *ReferenceName = info->method;
2238 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2239 }
2240 }
2241 info->class_name = nullptr;
2242 }
2243 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002244 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002245 if (info->method != nullptr)
2246 free(info->method);
2247 info->method = (char *)malloc(17 + strlen(info->selector_name));
2248 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002249 if (Arch == Triple::x86_64)
2250 strcpy(info->method, "-[[%rdi super] ");
2251 else if (Arch == Triple::aarch64)
2252 strcpy(info->method, "-[[x0 super] ");
2253 else
2254 strcpy(info->method, "-[[r? super] ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002255 strcat(info->method, info->selector_name);
2256 strcat(info->method, "]");
2257 *ReferenceName = info->method;
2258 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2259 }
2260 info->class_name = nullptr;
2261 }
2262 }
2263 }
2264}
2265
2266// GuessPointerPointer() is passed the address of what might be a pointer to
2267// a reference to an Objective-C class, selector, message ref or cfstring.
2268// If so the value of the pointer is returned and one of the booleans are set
2269// to true. If not zero is returned and all the booleans are set to false.
2270static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2271 struct DisassembleInfo *info,
2272 bool &classref, bool &selref, bool &msgref,
2273 bool &cfstring) {
2274 classref = false;
2275 selref = false;
2276 msgref = false;
2277 cfstring = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002278 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002279 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2280 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2281 for (unsigned J = 0; J < Seg.nsects; ++J) {
2282 MachO::section_64 Sec = info->O->getSection64(Load, J);
2283 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2284 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2285 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2286 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2287 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2288 ReferenceValue >= Sec.addr &&
2289 ReferenceValue < Sec.addr + Sec.size) {
2290 uint64_t sect_offset = ReferenceValue - Sec.addr;
2291 uint64_t object_offset = Sec.offset + sect_offset;
2292 StringRef MachOContents = info->O->getData();
2293 uint64_t object_size = MachOContents.size();
2294 const char *object_addr = (const char *)MachOContents.data();
2295 if (object_offset < object_size) {
2296 uint64_t pointer_value;
2297 memcpy(&pointer_value, object_addr + object_offset,
2298 sizeof(uint64_t));
2299 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2300 sys::swapByteOrder(pointer_value);
2301 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2302 selref = true;
2303 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2304 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2305 classref = true;
2306 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2307 ReferenceValue + 8 < Sec.addr + Sec.size) {
2308 msgref = true;
2309 memcpy(&pointer_value, object_addr + object_offset + 8,
2310 sizeof(uint64_t));
2311 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2312 sys::swapByteOrder(pointer_value);
2313 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2314 cfstring = true;
2315 return pointer_value;
2316 } else {
2317 return 0;
2318 }
2319 }
2320 }
2321 }
2322 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002323 }
2324 return 0;
2325}
2326
2327// get_pointer_64 returns a pointer to the bytes in the object file at the
2328// Address from a section in the Mach-O file. And indirectly returns the
2329// offset into the section, number of bytes left in the section past the offset
2330// and which section is was being referenced. If the Address is not in a
2331// section nullptr is returned.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002332static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2333 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002334 DisassembleInfo *info,
2335 bool objc_only = false) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002336 offset = 0;
2337 left = 0;
2338 S = SectionRef();
2339 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2340 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2341 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
Kevin Enderby846c0002015-04-16 17:19:59 +00002342 if (objc_only) {
2343 StringRef SectName;
2344 ((*(info->Sections))[SectIdx]).getName(SectName);
2345 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2346 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2347 if (SegName != "__OBJC" && SectName != "__cstring")
2348 continue;
2349 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002350 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2351 S = (*(info->Sections))[SectIdx];
2352 offset = Address - SectAddress;
2353 left = SectSize - offset;
2354 StringRef SectContents;
2355 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2356 return SectContents.data() + offset;
2357 }
2358 }
2359 return nullptr;
2360}
2361
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002362static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2363 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002364 DisassembleInfo *info,
2365 bool objc_only = false) {
2366 return get_pointer_64(Address, offset, left, S, info, objc_only);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002367}
2368
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002369// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2370// the symbol indirectly through n_value. Based on the relocation information
2371// for the specified section offset in the specified section reference.
Kevin Enderby0fc11822015-04-01 20:57:01 +00002372// If no relocation information is found and a non-zero ReferenceValue for the
2373// symbol is passed, look up that address in the info's AddrMap.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002374static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2375 DisassembleInfo *info, uint64_t &n_value,
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002376 uint64_t ReferenceValue = 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002377 n_value = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002378 if (!info->verbose)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002379 return nullptr;
2380
2381 // See if there is an external relocation entry at the sect_offset.
2382 bool reloc_found = false;
2383 DataRefImpl Rel;
2384 MachO::any_relocation_info RE;
2385 bool isExtern = false;
2386 SymbolRef Symbol;
2387 for (const RelocationRef &Reloc : S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002388 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002389 if (RelocOffset == sect_offset) {
2390 Rel = Reloc.getRawDataRefImpl();
2391 RE = info->O->getRelocation(Rel);
2392 if (info->O->isRelocationScattered(RE))
2393 continue;
2394 isExtern = info->O->getPlainRelocationExternal(RE);
2395 if (isExtern) {
2396 symbol_iterator RelocSym = Reloc.getSymbol();
2397 Symbol = *RelocSym;
2398 }
2399 reloc_found = true;
2400 break;
2401 }
2402 }
2403 // If there is an external relocation entry for a symbol in this section
2404 // at this section_offset then use that symbol's value for the n_value
2405 // and return its name.
2406 const char *SymbolName = nullptr;
2407 if (reloc_found && isExtern) {
Rafael Espindoladea00162015-07-03 17:44:18 +00002408 n_value = Symbol.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002409 ErrorOr<StringRef> NameOrError = Symbol.getName();
2410 if (std::error_code EC = NameOrError.getError())
2411 report_fatal_error(EC.message());
2412 StringRef Name = *NameOrError;
2413 if (!Name.empty()) {
2414 SymbolName = Name.data();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002415 return SymbolName;
2416 }
2417 }
2418
2419 // TODO: For fully linked images, look through the external relocation
2420 // entries off the dynamic symtab command. For these the r_offset is from the
2421 // start of the first writeable segment in the Mach-O file. So the offset
2422 // to this section from that segment is passed to this routine by the caller,
2423 // as the database_offset. Which is the difference of the section's starting
2424 // address and the first writable segment.
2425 //
2426 // NOTE: need add passing the database_offset to this routine.
2427
Kevin Enderby0fc11822015-04-01 20:57:01 +00002428 // We did not find an external relocation entry so look up the ReferenceValue
2429 // as an address of a symbol and if found return that symbol's name.
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002430 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002431
2432 return SymbolName;
2433}
2434
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002435static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2436 DisassembleInfo *info,
2437 uint32_t ReferenceValue) {
2438 uint64_t n_value64;
2439 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2440}
2441
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002442// These are structs in the Objective-C meta data and read to produce the
2443// comments for disassembly. While these are part of the ABI they are no
2444// public defintions. So the are here not in include/llvm/Support/MachO.h .
2445
2446// The cfstring object in a 64-bit Mach-O file.
2447struct cfstring64_t {
2448 uint64_t isa; // class64_t * (64-bit pointer)
2449 uint64_t flags; // flag bits
2450 uint64_t characters; // char * (64-bit pointer)
2451 uint64_t length; // number of non-NULL characters in above
2452};
2453
2454// The class object in a 64-bit Mach-O file.
2455struct class64_t {
2456 uint64_t isa; // class64_t * (64-bit pointer)
2457 uint64_t superclass; // class64_t * (64-bit pointer)
2458 uint64_t cache; // Cache (64-bit pointer)
2459 uint64_t vtable; // IMP * (64-bit pointer)
2460 uint64_t data; // class_ro64_t * (64-bit pointer)
2461};
2462
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002463struct class32_t {
2464 uint32_t isa; /* class32_t * (32-bit pointer) */
2465 uint32_t superclass; /* class32_t * (32-bit pointer) */
2466 uint32_t cache; /* Cache (32-bit pointer) */
2467 uint32_t vtable; /* IMP * (32-bit pointer) */
2468 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2469};
2470
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002471struct class_ro64_t {
2472 uint32_t flags;
2473 uint32_t instanceStart;
2474 uint32_t instanceSize;
2475 uint32_t reserved;
2476 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2477 uint64_t name; // const char * (64-bit pointer)
2478 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2479 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2480 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2481 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2482 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2483};
2484
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002485struct class_ro32_t {
2486 uint32_t flags;
2487 uint32_t instanceStart;
2488 uint32_t instanceSize;
2489 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2490 uint32_t name; /* const char * (32-bit pointer) */
2491 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2492 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2493 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2494 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2495 uint32_t baseProperties; /* const struct objc_property_list *
2496 (32-bit pointer) */
2497};
2498
2499/* Values for class_ro{64,32}_t->flags */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002500#define RO_META (1 << 0)
2501#define RO_ROOT (1 << 1)
2502#define RO_HAS_CXX_STRUCTORS (1 << 2)
2503
2504struct method_list64_t {
2505 uint32_t entsize;
2506 uint32_t count;
2507 /* struct method64_t first; These structures follow inline */
2508};
2509
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002510struct method_list32_t {
2511 uint32_t entsize;
2512 uint32_t count;
2513 /* struct method32_t first; These structures follow inline */
2514};
2515
Kevin Enderby0fc11822015-04-01 20:57:01 +00002516struct method64_t {
2517 uint64_t name; /* SEL (64-bit pointer) */
2518 uint64_t types; /* const char * (64-bit pointer) */
2519 uint64_t imp; /* IMP (64-bit pointer) */
2520};
2521
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002522struct method32_t {
2523 uint32_t name; /* SEL (32-bit pointer) */
2524 uint32_t types; /* const char * (32-bit pointer) */
2525 uint32_t imp; /* IMP (32-bit pointer) */
2526};
2527
Kevin Enderby0fc11822015-04-01 20:57:01 +00002528struct protocol_list64_t {
2529 uint64_t count; /* uintptr_t (a 64-bit value) */
2530 /* struct protocol64_t * list[0]; These pointers follow inline */
2531};
2532
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002533struct protocol_list32_t {
2534 uint32_t count; /* uintptr_t (a 32-bit value) */
2535 /* struct protocol32_t * list[0]; These pointers follow inline */
2536};
2537
Kevin Enderby0fc11822015-04-01 20:57:01 +00002538struct protocol64_t {
2539 uint64_t isa; /* id * (64-bit pointer) */
2540 uint64_t name; /* const char * (64-bit pointer) */
2541 uint64_t protocols; /* struct protocol_list64_t *
2542 (64-bit pointer) */
2543 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2544 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2545 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2546 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2547 uint64_t instanceProperties; /* struct objc_property_list *
2548 (64-bit pointer) */
2549};
2550
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002551struct protocol32_t {
2552 uint32_t isa; /* id * (32-bit pointer) */
2553 uint32_t name; /* const char * (32-bit pointer) */
2554 uint32_t protocols; /* struct protocol_list_t *
2555 (32-bit pointer) */
2556 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2557 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2558 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2559 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2560 uint32_t instanceProperties; /* struct objc_property_list *
2561 (32-bit pointer) */
2562};
2563
Kevin Enderby0fc11822015-04-01 20:57:01 +00002564struct ivar_list64_t {
2565 uint32_t entsize;
2566 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002567 /* struct ivar64_t first; These structures follow inline */
2568};
2569
2570struct ivar_list32_t {
2571 uint32_t entsize;
2572 uint32_t count;
2573 /* struct ivar32_t first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002574};
2575
2576struct ivar64_t {
2577 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2578 uint64_t name; /* const char * (64-bit pointer) */
2579 uint64_t type; /* const char * (64-bit pointer) */
2580 uint32_t alignment;
2581 uint32_t size;
2582};
2583
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002584struct ivar32_t {
2585 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2586 uint32_t name; /* const char * (32-bit pointer) */
2587 uint32_t type; /* const char * (32-bit pointer) */
2588 uint32_t alignment;
2589 uint32_t size;
2590};
2591
Kevin Enderby0fc11822015-04-01 20:57:01 +00002592struct objc_property_list64 {
2593 uint32_t entsize;
2594 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002595 /* struct objc_property64 first; These structures follow inline */
2596};
2597
2598struct objc_property_list32 {
2599 uint32_t entsize;
2600 uint32_t count;
2601 /* struct objc_property32 first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002602};
2603
2604struct objc_property64 {
2605 uint64_t name; /* const char * (64-bit pointer) */
2606 uint64_t attributes; /* const char * (64-bit pointer) */
2607};
2608
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002609struct objc_property32 {
2610 uint32_t name; /* const char * (32-bit pointer) */
2611 uint32_t attributes; /* const char * (32-bit pointer) */
2612};
2613
Kevin Enderby0fc11822015-04-01 20:57:01 +00002614struct category64_t {
2615 uint64_t name; /* const char * (64-bit pointer) */
2616 uint64_t cls; /* struct class_t * (64-bit pointer) */
2617 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2618 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2619 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2620 uint64_t instanceProperties; /* struct objc_property_list *
2621 (64-bit pointer) */
2622};
2623
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002624struct category32_t {
2625 uint32_t name; /* const char * (32-bit pointer) */
2626 uint32_t cls; /* struct class_t * (32-bit pointer) */
2627 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2628 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2629 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2630 uint32_t instanceProperties; /* struct objc_property_list *
2631 (32-bit pointer) */
2632};
2633
Kevin Enderby0fc11822015-04-01 20:57:01 +00002634struct objc_image_info64 {
2635 uint32_t version;
2636 uint32_t flags;
2637};
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002638struct objc_image_info32 {
2639 uint32_t version;
2640 uint32_t flags;
2641};
Kevin Enderby846c0002015-04-16 17:19:59 +00002642struct imageInfo_t {
2643 uint32_t version;
2644 uint32_t flags;
2645};
Kevin Enderby0fc11822015-04-01 20:57:01 +00002646/* masks for objc_image_info.flags */
2647#define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2648#define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2649
2650struct message_ref64 {
2651 uint64_t imp; /* IMP (64-bit pointer) */
2652 uint64_t sel; /* SEL (64-bit pointer) */
2653};
2654
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002655struct message_ref32 {
2656 uint32_t imp; /* IMP (32-bit pointer) */
2657 uint32_t sel; /* SEL (32-bit pointer) */
2658};
2659
Kevin Enderby846c0002015-04-16 17:19:59 +00002660// Objective-C 1 (32-bit only) meta data structs.
2661
2662struct objc_module_t {
2663 uint32_t version;
2664 uint32_t size;
2665 uint32_t name; /* char * (32-bit pointer) */
2666 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2667};
2668
2669struct objc_symtab_t {
2670 uint32_t sel_ref_cnt;
2671 uint32_t refs; /* SEL * (32-bit pointer) */
2672 uint16_t cls_def_cnt;
2673 uint16_t cat_def_cnt;
2674 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2675};
2676
2677struct objc_class_t {
2678 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2679 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2680 uint32_t name; /* const char * (32-bit pointer) */
2681 int32_t version;
2682 int32_t info;
2683 int32_t instance_size;
2684 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2685 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2686 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2687 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2688};
2689
2690#define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2691// class is not a metaclass
2692#define CLS_CLASS 0x1
2693// class is a metaclass
2694#define CLS_META 0x2
2695
2696struct objc_category_t {
2697 uint32_t category_name; /* char * (32-bit pointer) */
2698 uint32_t class_name; /* char * (32-bit pointer) */
2699 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2700 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2701 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2702};
2703
2704struct objc_ivar_t {
2705 uint32_t ivar_name; /* char * (32-bit pointer) */
2706 uint32_t ivar_type; /* char * (32-bit pointer) */
2707 int32_t ivar_offset;
2708};
2709
2710struct objc_ivar_list_t {
2711 int32_t ivar_count;
2712 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2713};
2714
2715struct objc_method_list_t {
2716 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2717 int32_t method_count;
2718 // struct objc_method_t method_list[1]; /* variable length structure */
2719};
2720
2721struct objc_method_t {
2722 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2723 uint32_t method_types; /* char * (32-bit pointer) */
2724 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2725 (32-bit pointer) */
2726};
2727
2728struct objc_protocol_list_t {
2729 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2730 int32_t count;
2731 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2732 // (32-bit pointer) */
2733};
2734
2735struct objc_protocol_t {
2736 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2737 uint32_t protocol_name; /* char * (32-bit pointer) */
2738 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2739 uint32_t instance_methods; /* struct objc_method_description_list *
2740 (32-bit pointer) */
2741 uint32_t class_methods; /* struct objc_method_description_list *
2742 (32-bit pointer) */
2743};
2744
2745struct objc_method_description_list_t {
2746 int32_t count;
2747 // struct objc_method_description_t list[1];
2748};
2749
2750struct objc_method_description_t {
2751 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2752 uint32_t types; /* char * (32-bit pointer) */
2753};
2754
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002755inline void swapStruct(struct cfstring64_t &cfs) {
2756 sys::swapByteOrder(cfs.isa);
2757 sys::swapByteOrder(cfs.flags);
2758 sys::swapByteOrder(cfs.characters);
2759 sys::swapByteOrder(cfs.length);
2760}
2761
2762inline void swapStruct(struct class64_t &c) {
2763 sys::swapByteOrder(c.isa);
2764 sys::swapByteOrder(c.superclass);
2765 sys::swapByteOrder(c.cache);
2766 sys::swapByteOrder(c.vtable);
2767 sys::swapByteOrder(c.data);
2768}
2769
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002770inline void swapStruct(struct class32_t &c) {
2771 sys::swapByteOrder(c.isa);
2772 sys::swapByteOrder(c.superclass);
2773 sys::swapByteOrder(c.cache);
2774 sys::swapByteOrder(c.vtable);
2775 sys::swapByteOrder(c.data);
2776}
2777
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002778inline void swapStruct(struct class_ro64_t &cro) {
2779 sys::swapByteOrder(cro.flags);
2780 sys::swapByteOrder(cro.instanceStart);
2781 sys::swapByteOrder(cro.instanceSize);
2782 sys::swapByteOrder(cro.reserved);
2783 sys::swapByteOrder(cro.ivarLayout);
2784 sys::swapByteOrder(cro.name);
2785 sys::swapByteOrder(cro.baseMethods);
2786 sys::swapByteOrder(cro.baseProtocols);
2787 sys::swapByteOrder(cro.ivars);
2788 sys::swapByteOrder(cro.weakIvarLayout);
2789 sys::swapByteOrder(cro.baseProperties);
2790}
2791
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002792inline void swapStruct(struct class_ro32_t &cro) {
2793 sys::swapByteOrder(cro.flags);
2794 sys::swapByteOrder(cro.instanceStart);
2795 sys::swapByteOrder(cro.instanceSize);
2796 sys::swapByteOrder(cro.ivarLayout);
2797 sys::swapByteOrder(cro.name);
2798 sys::swapByteOrder(cro.baseMethods);
2799 sys::swapByteOrder(cro.baseProtocols);
2800 sys::swapByteOrder(cro.ivars);
2801 sys::swapByteOrder(cro.weakIvarLayout);
2802 sys::swapByteOrder(cro.baseProperties);
2803}
2804
Kevin Enderby0fc11822015-04-01 20:57:01 +00002805inline void swapStruct(struct method_list64_t &ml) {
2806 sys::swapByteOrder(ml.entsize);
2807 sys::swapByteOrder(ml.count);
2808}
2809
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002810inline void swapStruct(struct method_list32_t &ml) {
2811 sys::swapByteOrder(ml.entsize);
2812 sys::swapByteOrder(ml.count);
2813}
2814
Kevin Enderby0fc11822015-04-01 20:57:01 +00002815inline void swapStruct(struct method64_t &m) {
2816 sys::swapByteOrder(m.name);
2817 sys::swapByteOrder(m.types);
2818 sys::swapByteOrder(m.imp);
2819}
2820
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002821inline void swapStruct(struct method32_t &m) {
2822 sys::swapByteOrder(m.name);
2823 sys::swapByteOrder(m.types);
2824 sys::swapByteOrder(m.imp);
2825}
2826
Kevin Enderby0fc11822015-04-01 20:57:01 +00002827inline void swapStruct(struct protocol_list64_t &pl) {
2828 sys::swapByteOrder(pl.count);
2829}
2830
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002831inline void swapStruct(struct protocol_list32_t &pl) {
2832 sys::swapByteOrder(pl.count);
2833}
2834
Kevin Enderby0fc11822015-04-01 20:57:01 +00002835inline void swapStruct(struct protocol64_t &p) {
2836 sys::swapByteOrder(p.isa);
2837 sys::swapByteOrder(p.name);
2838 sys::swapByteOrder(p.protocols);
2839 sys::swapByteOrder(p.instanceMethods);
2840 sys::swapByteOrder(p.classMethods);
2841 sys::swapByteOrder(p.optionalInstanceMethods);
2842 sys::swapByteOrder(p.optionalClassMethods);
2843 sys::swapByteOrder(p.instanceProperties);
2844}
2845
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002846inline void swapStruct(struct protocol32_t &p) {
2847 sys::swapByteOrder(p.isa);
2848 sys::swapByteOrder(p.name);
2849 sys::swapByteOrder(p.protocols);
2850 sys::swapByteOrder(p.instanceMethods);
2851 sys::swapByteOrder(p.classMethods);
2852 sys::swapByteOrder(p.optionalInstanceMethods);
2853 sys::swapByteOrder(p.optionalClassMethods);
2854 sys::swapByteOrder(p.instanceProperties);
2855}
2856
Kevin Enderby0fc11822015-04-01 20:57:01 +00002857inline void swapStruct(struct ivar_list64_t &il) {
2858 sys::swapByteOrder(il.entsize);
2859 sys::swapByteOrder(il.count);
2860}
2861
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002862inline void swapStruct(struct ivar_list32_t &il) {
2863 sys::swapByteOrder(il.entsize);
2864 sys::swapByteOrder(il.count);
2865}
2866
Kevin Enderby0fc11822015-04-01 20:57:01 +00002867inline void swapStruct(struct ivar64_t &i) {
2868 sys::swapByteOrder(i.offset);
2869 sys::swapByteOrder(i.name);
2870 sys::swapByteOrder(i.type);
2871 sys::swapByteOrder(i.alignment);
2872 sys::swapByteOrder(i.size);
2873}
2874
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002875inline void swapStruct(struct ivar32_t &i) {
2876 sys::swapByteOrder(i.offset);
2877 sys::swapByteOrder(i.name);
2878 sys::swapByteOrder(i.type);
2879 sys::swapByteOrder(i.alignment);
2880 sys::swapByteOrder(i.size);
2881}
2882
Kevin Enderby0fc11822015-04-01 20:57:01 +00002883inline void swapStruct(struct objc_property_list64 &pl) {
2884 sys::swapByteOrder(pl.entsize);
2885 sys::swapByteOrder(pl.count);
2886}
2887
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002888inline void swapStruct(struct objc_property_list32 &pl) {
2889 sys::swapByteOrder(pl.entsize);
2890 sys::swapByteOrder(pl.count);
2891}
2892
Kevin Enderby0fc11822015-04-01 20:57:01 +00002893inline void swapStruct(struct objc_property64 &op) {
2894 sys::swapByteOrder(op.name);
2895 sys::swapByteOrder(op.attributes);
2896}
2897
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002898inline void swapStruct(struct objc_property32 &op) {
2899 sys::swapByteOrder(op.name);
2900 sys::swapByteOrder(op.attributes);
2901}
2902
Kevin Enderby0fc11822015-04-01 20:57:01 +00002903inline void swapStruct(struct category64_t &c) {
2904 sys::swapByteOrder(c.name);
2905 sys::swapByteOrder(c.cls);
2906 sys::swapByteOrder(c.instanceMethods);
2907 sys::swapByteOrder(c.classMethods);
2908 sys::swapByteOrder(c.protocols);
2909 sys::swapByteOrder(c.instanceProperties);
2910}
2911
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002912inline void swapStruct(struct category32_t &c) {
2913 sys::swapByteOrder(c.name);
2914 sys::swapByteOrder(c.cls);
2915 sys::swapByteOrder(c.instanceMethods);
2916 sys::swapByteOrder(c.classMethods);
2917 sys::swapByteOrder(c.protocols);
2918 sys::swapByteOrder(c.instanceProperties);
2919}
2920
Kevin Enderby0fc11822015-04-01 20:57:01 +00002921inline void swapStruct(struct objc_image_info64 &o) {
2922 sys::swapByteOrder(o.version);
2923 sys::swapByteOrder(o.flags);
2924}
2925
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002926inline void swapStruct(struct objc_image_info32 &o) {
2927 sys::swapByteOrder(o.version);
2928 sys::swapByteOrder(o.flags);
2929}
2930
Kevin Enderby846c0002015-04-16 17:19:59 +00002931inline void swapStruct(struct imageInfo_t &o) {
2932 sys::swapByteOrder(o.version);
2933 sys::swapByteOrder(o.flags);
2934}
2935
Kevin Enderby0fc11822015-04-01 20:57:01 +00002936inline void swapStruct(struct message_ref64 &mr) {
2937 sys::swapByteOrder(mr.imp);
2938 sys::swapByteOrder(mr.sel);
2939}
2940
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002941inline void swapStruct(struct message_ref32 &mr) {
2942 sys::swapByteOrder(mr.imp);
2943 sys::swapByteOrder(mr.sel);
2944}
2945
Kevin Enderby846c0002015-04-16 17:19:59 +00002946inline void swapStruct(struct objc_module_t &module) {
2947 sys::swapByteOrder(module.version);
2948 sys::swapByteOrder(module.size);
2949 sys::swapByteOrder(module.name);
2950 sys::swapByteOrder(module.symtab);
Jingyue Wufedecc42015-04-16 18:43:44 +00002951}
Kevin Enderby846c0002015-04-16 17:19:59 +00002952
2953inline void swapStruct(struct objc_symtab_t &symtab) {
2954 sys::swapByteOrder(symtab.sel_ref_cnt);
2955 sys::swapByteOrder(symtab.refs);
2956 sys::swapByteOrder(symtab.cls_def_cnt);
2957 sys::swapByteOrder(symtab.cat_def_cnt);
Jingyue Wufedecc42015-04-16 18:43:44 +00002958}
Kevin Enderby846c0002015-04-16 17:19:59 +00002959
2960inline void swapStruct(struct objc_class_t &objc_class) {
2961 sys::swapByteOrder(objc_class.isa);
2962 sys::swapByteOrder(objc_class.super_class);
2963 sys::swapByteOrder(objc_class.name);
2964 sys::swapByteOrder(objc_class.version);
2965 sys::swapByteOrder(objc_class.info);
2966 sys::swapByteOrder(objc_class.instance_size);
2967 sys::swapByteOrder(objc_class.ivars);
2968 sys::swapByteOrder(objc_class.methodLists);
2969 sys::swapByteOrder(objc_class.cache);
2970 sys::swapByteOrder(objc_class.protocols);
Jingyue Wufedecc42015-04-16 18:43:44 +00002971}
Kevin Enderby846c0002015-04-16 17:19:59 +00002972
2973inline void swapStruct(struct objc_category_t &objc_category) {
2974 sys::swapByteOrder(objc_category.category_name);
2975 sys::swapByteOrder(objc_category.class_name);
2976 sys::swapByteOrder(objc_category.instance_methods);
2977 sys::swapByteOrder(objc_category.class_methods);
2978 sys::swapByteOrder(objc_category.protocols);
2979}
2980
2981inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
2982 sys::swapByteOrder(objc_ivar_list.ivar_count);
2983}
2984
2985inline void swapStruct(struct objc_ivar_t &objc_ivar) {
2986 sys::swapByteOrder(objc_ivar.ivar_name);
2987 sys::swapByteOrder(objc_ivar.ivar_type);
2988 sys::swapByteOrder(objc_ivar.ivar_offset);
Jingyue Wufedecc42015-04-16 18:43:44 +00002989}
Kevin Enderby846c0002015-04-16 17:19:59 +00002990
2991inline void swapStruct(struct objc_method_list_t &method_list) {
2992 sys::swapByteOrder(method_list.obsolete);
2993 sys::swapByteOrder(method_list.method_count);
2994}
2995
2996inline void swapStruct(struct objc_method_t &method) {
2997 sys::swapByteOrder(method.method_name);
2998 sys::swapByteOrder(method.method_types);
2999 sys::swapByteOrder(method.method_imp);
3000}
3001
3002inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3003 sys::swapByteOrder(protocol_list.next);
3004 sys::swapByteOrder(protocol_list.count);
3005}
3006
3007inline void swapStruct(struct objc_protocol_t &protocol) {
3008 sys::swapByteOrder(protocol.isa);
3009 sys::swapByteOrder(protocol.protocol_name);
3010 sys::swapByteOrder(protocol.protocol_list);
3011 sys::swapByteOrder(protocol.instance_methods);
3012 sys::swapByteOrder(protocol.class_methods);
3013}
3014
3015inline void swapStruct(struct objc_method_description_list_t &mdl) {
3016 sys::swapByteOrder(mdl.count);
3017}
3018
3019inline void swapStruct(struct objc_method_description_t &md) {
3020 sys::swapByteOrder(md.name);
3021 sys::swapByteOrder(md.types);
3022}
3023
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003024static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3025 struct DisassembleInfo *info);
3026
3027// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3028// to an Objective-C class and returns the class name. It is also passed the
3029// address of the pointer, so when the pointer is zero as it can be in an .o
3030// file, that is used to look for an external relocation entry with a symbol
3031// name.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003032static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3033 uint64_t ReferenceValue,
3034 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003035 const char *r;
3036 uint32_t offset, left;
3037 SectionRef S;
3038
3039 // The pointer_value can be 0 in an object file and have a relocation
3040 // entry for the class symbol at the ReferenceValue (the address of the
3041 // pointer).
3042 if (pointer_value == 0) {
3043 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3044 if (r == nullptr || left < sizeof(uint64_t))
3045 return nullptr;
3046 uint64_t n_value;
3047 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3048 if (symbol_name == nullptr)
3049 return nullptr;
Hans Wennborgdb53e302014-10-23 21:59:17 +00003050 const char *class_name = strrchr(symbol_name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003051 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3052 return class_name + 2;
3053 else
3054 return nullptr;
3055 }
3056
3057 // The case were the pointer_value is non-zero and points to a class defined
3058 // in this Mach-O file.
3059 r = get_pointer_64(pointer_value, offset, left, S, info);
3060 if (r == nullptr || left < sizeof(struct class64_t))
3061 return nullptr;
3062 struct class64_t c;
3063 memcpy(&c, r, sizeof(struct class64_t));
3064 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3065 swapStruct(c);
3066 if (c.data == 0)
3067 return nullptr;
3068 r = get_pointer_64(c.data, offset, left, S, info);
3069 if (r == nullptr || left < sizeof(struct class_ro64_t))
3070 return nullptr;
3071 struct class_ro64_t cro;
3072 memcpy(&cro, r, sizeof(struct class_ro64_t));
3073 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3074 swapStruct(cro);
3075 if (cro.name == 0)
3076 return nullptr;
3077 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3078 return name;
3079}
3080
3081// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3082// pointer to a cfstring and returns its name or nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003083static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3084 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003085 const char *r, *name;
3086 uint32_t offset, left;
3087 SectionRef S;
3088 struct cfstring64_t cfs;
3089 uint64_t cfs_characters;
3090
3091 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3092 if (r == nullptr || left < sizeof(struct cfstring64_t))
3093 return nullptr;
3094 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3095 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3096 swapStruct(cfs);
3097 if (cfs.characters == 0) {
3098 uint64_t n_value;
3099 const char *symbol_name = get_symbol_64(
3100 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3101 if (symbol_name == nullptr)
3102 return nullptr;
3103 cfs_characters = n_value;
3104 } else
3105 cfs_characters = cfs.characters;
3106 name = get_pointer_64(cfs_characters, offset, left, S, info);
3107
3108 return name;
3109}
3110
3111// get_objc2_64bit_selref() is used for disassembly and is passed a the address
3112// of a pointer to an Objective-C selector reference when the pointer value is
3113// zero as in a .o file and is likely to have a external relocation entry with
3114// who's symbol's n_value is the real pointer to the selector name. If that is
3115// the case the real pointer to the selector name is returned else 0 is
3116// returned
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003117static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3118 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003119 uint32_t offset, left;
3120 SectionRef S;
3121
3122 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3123 if (r == nullptr || left < sizeof(uint64_t))
3124 return 0;
3125 uint64_t n_value;
3126 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3127 if (symbol_name == nullptr)
3128 return 0;
3129 return n_value;
3130}
3131
Kevin Enderby0fc11822015-04-01 20:57:01 +00003132static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3133 const char *sectname) {
3134 for (const SectionRef &Section : O->sections()) {
3135 StringRef SectName;
3136 Section.getName(SectName);
3137 DataRefImpl Ref = Section.getRawDataRefImpl();
3138 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3139 if (SegName == segname && SectName == sectname)
3140 return Section;
3141 }
3142 return SectionRef();
3143}
3144
3145static void
3146walk_pointer_list_64(const char *listname, const SectionRef S,
3147 MachOObjectFile *O, struct DisassembleInfo *info,
3148 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3149 if (S == SectionRef())
3150 return;
3151
3152 StringRef SectName;
3153 S.getName(SectName);
3154 DataRefImpl Ref = S.getRawDataRefImpl();
3155 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3156 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3157
3158 StringRef BytesStr;
3159 S.getContents(BytesStr);
3160 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3161
3162 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3163 uint32_t left = S.getSize() - i;
3164 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3165 uint64_t p = 0;
3166 memcpy(&p, Contents + i, size);
3167 if (i + sizeof(uint64_t) > S.getSize())
3168 outs() << listname << " list pointer extends past end of (" << SegName
3169 << "," << SectName << ") section\n";
3170 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3171
3172 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3173 sys::swapByteOrder(p);
3174
3175 uint64_t n_value = 0;
3176 const char *name = get_symbol_64(i, S, info, n_value, p);
3177 if (name == nullptr)
3178 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3179
3180 if (n_value != 0) {
3181 outs() << format("0x%" PRIx64, n_value);
3182 if (p != 0)
3183 outs() << " + " << format("0x%" PRIx64, p);
3184 } else
3185 outs() << format("0x%" PRIx64, p);
3186 if (name != nullptr)
3187 outs() << " " << name;
3188 outs() << "\n";
3189
3190 p += n_value;
3191 if (func)
3192 func(p, info);
3193 }
3194}
3195
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003196static void
3197walk_pointer_list_32(const char *listname, const SectionRef S,
3198 MachOObjectFile *O, struct DisassembleInfo *info,
3199 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3200 if (S == SectionRef())
3201 return;
3202
3203 StringRef SectName;
3204 S.getName(SectName);
3205 DataRefImpl Ref = S.getRawDataRefImpl();
3206 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3207 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3208
3209 StringRef BytesStr;
3210 S.getContents(BytesStr);
3211 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3212
3213 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3214 uint32_t left = S.getSize() - i;
3215 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3216 uint32_t p = 0;
3217 memcpy(&p, Contents + i, size);
3218 if (i + sizeof(uint32_t) > S.getSize())
3219 outs() << listname << " list pointer extends past end of (" << SegName
3220 << "," << SectName << ") section\n";
Kevin Enderbycf261312015-04-06 22:33:43 +00003221 uint32_t Address = S.getAddress() + i;
3222 outs() << format("%08" PRIx32, Address) << " ";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003223
3224 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3225 sys::swapByteOrder(p);
3226 outs() << format("0x%" PRIx32, p);
3227
3228 const char *name = get_symbol_32(i, S, info, p);
3229 if (name != nullptr)
3230 outs() << " " << name;
3231 outs() << "\n";
3232
3233 if (func)
3234 func(p, info);
3235 }
3236}
3237
3238static void print_layout_map(const char *layout_map, uint32_t left) {
3239 outs() << " layout map: ";
3240 do {
3241 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3242 left--;
3243 layout_map++;
3244 } while (*layout_map != '\0' && left != 0);
3245 outs() << "\n";
3246}
3247
Kevin Enderby0fc11822015-04-01 20:57:01 +00003248static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3249 uint32_t offset, left;
3250 SectionRef S;
3251 const char *layout_map;
3252
3253 if (p == 0)
3254 return;
3255 layout_map = get_pointer_64(p, offset, left, S, info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003256 print_layout_map(layout_map, left);
3257}
3258
3259static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3260 uint32_t offset, left;
3261 SectionRef S;
3262 const char *layout_map;
3263
3264 if (p == 0)
3265 return;
3266 layout_map = get_pointer_32(p, offset, left, S, info);
3267 print_layout_map(layout_map, left);
Kevin Enderby0fc11822015-04-01 20:57:01 +00003268}
3269
3270static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3271 const char *indent) {
3272 struct method_list64_t ml;
3273 struct method64_t m;
3274 const char *r;
3275 uint32_t offset, xoffset, left, i;
3276 SectionRef S, xS;
3277 const char *name, *sym_name;
3278 uint64_t n_value;
3279
3280 r = get_pointer_64(p, offset, left, S, info);
3281 if (r == nullptr)
3282 return;
3283 memset(&ml, '\0', sizeof(struct method_list64_t));
3284 if (left < sizeof(struct method_list64_t)) {
3285 memcpy(&ml, r, left);
3286 outs() << " (method_list_t entends past the end of the section)\n";
3287 } else
3288 memcpy(&ml, r, sizeof(struct method_list64_t));
3289 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3290 swapStruct(ml);
3291 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3292 outs() << indent << "\t\t count " << ml.count << "\n";
3293
3294 p += sizeof(struct method_list64_t);
3295 offset += sizeof(struct method_list64_t);
3296 for (i = 0; i < ml.count; i++) {
3297 r = get_pointer_64(p, offset, left, S, info);
3298 if (r == nullptr)
3299 return;
3300 memset(&m, '\0', sizeof(struct method64_t));
3301 if (left < sizeof(struct method64_t)) {
3302 memcpy(&ml, r, left);
3303 outs() << indent << " (method_t entends past the end of the section)\n";
3304 } else
3305 memcpy(&m, r, sizeof(struct method64_t));
3306 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3307 swapStruct(m);
3308
3309 outs() << indent << "\t\t name ";
3310 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3311 info, n_value, m.name);
3312 if (n_value != 0) {
3313 if (info->verbose && sym_name != nullptr)
3314 outs() << sym_name;
3315 else
3316 outs() << format("0x%" PRIx64, n_value);
3317 if (m.name != 0)
3318 outs() << " + " << format("0x%" PRIx64, m.name);
3319 } else
3320 outs() << format("0x%" PRIx64, m.name);
3321 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3322 if (name != nullptr)
3323 outs() << format(" %.*s", left, name);
3324 outs() << "\n";
3325
3326 outs() << indent << "\t\t types ";
3327 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3328 info, n_value, m.types);
3329 if (n_value != 0) {
3330 if (info->verbose && sym_name != nullptr)
3331 outs() << sym_name;
3332 else
3333 outs() << format("0x%" PRIx64, n_value);
3334 if (m.types != 0)
3335 outs() << " + " << format("0x%" PRIx64, m.types);
3336 } else
3337 outs() << format("0x%" PRIx64, m.types);
3338 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3339 if (name != nullptr)
3340 outs() << format(" %.*s", left, name);
3341 outs() << "\n";
3342
3343 outs() << indent << "\t\t imp ";
3344 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3345 n_value, m.imp);
3346 if (info->verbose && name == nullptr) {
3347 if (n_value != 0) {
3348 outs() << format("0x%" PRIx64, n_value) << " ";
3349 if (m.imp != 0)
3350 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3351 } else
3352 outs() << format("0x%" PRIx64, m.imp) << " ";
3353 }
3354 if (name != nullptr)
3355 outs() << name;
3356 outs() << "\n";
3357
3358 p += sizeof(struct method64_t);
3359 offset += sizeof(struct method64_t);
3360 }
3361}
3362
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003363static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3364 const char *indent) {
3365 struct method_list32_t ml;
3366 struct method32_t m;
Kevin Enderby846c0002015-04-16 17:19:59 +00003367 const char *r, *name;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003368 uint32_t offset, xoffset, left, i;
3369 SectionRef S, xS;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003370
3371 r = get_pointer_32(p, offset, left, S, info);
3372 if (r == nullptr)
3373 return;
3374 memset(&ml, '\0', sizeof(struct method_list32_t));
3375 if (left < sizeof(struct method_list32_t)) {
3376 memcpy(&ml, r, left);
3377 outs() << " (method_list_t entends past the end of the section)\n";
3378 } else
3379 memcpy(&ml, r, sizeof(struct method_list32_t));
3380 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3381 swapStruct(ml);
3382 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3383 outs() << indent << "\t\t count " << ml.count << "\n";
3384
3385 p += sizeof(struct method_list32_t);
3386 offset += sizeof(struct method_list32_t);
3387 for (i = 0; i < ml.count; i++) {
3388 r = get_pointer_32(p, offset, left, S, info);
3389 if (r == nullptr)
3390 return;
3391 memset(&m, '\0', sizeof(struct method32_t));
3392 if (left < sizeof(struct method32_t)) {
3393 memcpy(&ml, r, left);
3394 outs() << indent << " (method_t entends past the end of the section)\n";
3395 } else
3396 memcpy(&m, r, sizeof(struct method32_t));
3397 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3398 swapStruct(m);
3399
3400 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3401 name = get_pointer_32(m.name, xoffset, left, xS, info);
3402 if (name != nullptr)
3403 outs() << format(" %.*s", left, name);
3404 outs() << "\n";
3405
3406 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3407 name = get_pointer_32(m.types, xoffset, left, xS, info);
3408 if (name != nullptr)
3409 outs() << format(" %.*s", left, name);
3410 outs() << "\n";
3411
3412 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3413 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3414 m.imp);
3415 if (name != nullptr)
3416 outs() << " " << name;
3417 outs() << "\n";
3418
3419 p += sizeof(struct method32_t);
3420 offset += sizeof(struct method32_t);
3421 }
3422}
3423
Kevin Enderby846c0002015-04-16 17:19:59 +00003424static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3425 uint32_t offset, left, xleft;
3426 SectionRef S;
3427 struct objc_method_list_t method_list;
3428 struct objc_method_t method;
3429 const char *r, *methods, *name, *SymbolName;
3430 int32_t i;
3431
3432 r = get_pointer_32(p, offset, left, S, info, true);
3433 if (r == nullptr)
3434 return true;
3435
3436 outs() << "\n";
3437 if (left > sizeof(struct objc_method_list_t)) {
3438 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3439 } else {
3440 outs() << "\t\t objc_method_list extends past end of the section\n";
3441 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3442 memcpy(&method_list, r, left);
3443 }
3444 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3445 swapStruct(method_list);
3446
3447 outs() << "\t\t obsolete "
3448 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3449 outs() << "\t\t method_count " << method_list.method_count << "\n";
3450
3451 methods = r + sizeof(struct objc_method_list_t);
3452 for (i = 0; i < method_list.method_count; i++) {
3453 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3454 outs() << "\t\t remaining method's extend past the of the section\n";
3455 break;
3456 }
3457 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3458 sizeof(struct objc_method_t));
3459 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3460 swapStruct(method);
3461
3462 outs() << "\t\t method_name "
3463 << format("0x%08" PRIx32, method.method_name);
3464 if (info->verbose) {
3465 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3466 if (name != nullptr)
3467 outs() << format(" %.*s", xleft, name);
3468 else
3469 outs() << " (not in an __OBJC section)";
3470 }
3471 outs() << "\n";
3472
3473 outs() << "\t\t method_types "
3474 << format("0x%08" PRIx32, method.method_types);
3475 if (info->verbose) {
3476 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3477 if (name != nullptr)
3478 outs() << format(" %.*s", xleft, name);
3479 else
3480 outs() << " (not in an __OBJC section)";
3481 }
3482 outs() << "\n";
3483
3484 outs() << "\t\t method_imp "
3485 << format("0x%08" PRIx32, method.method_imp) << " ";
3486 if (info->verbose) {
3487 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3488 if (SymbolName != nullptr)
3489 outs() << SymbolName;
3490 }
3491 outs() << "\n";
3492 }
3493 return false;
3494}
3495
Kevin Enderby0fc11822015-04-01 20:57:01 +00003496static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3497 struct protocol_list64_t pl;
3498 uint64_t q, n_value;
3499 struct protocol64_t pc;
3500 const char *r;
3501 uint32_t offset, xoffset, left, i;
3502 SectionRef S, xS;
3503 const char *name, *sym_name;
3504
3505 r = get_pointer_64(p, offset, left, S, info);
3506 if (r == nullptr)
3507 return;
3508 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3509 if (left < sizeof(struct protocol_list64_t)) {
3510 memcpy(&pl, r, left);
3511 outs() << " (protocol_list_t entends past the end of the section)\n";
3512 } else
3513 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3514 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3515 swapStruct(pl);
3516 outs() << " count " << pl.count << "\n";
3517
3518 p += sizeof(struct protocol_list64_t);
3519 offset += sizeof(struct protocol_list64_t);
3520 for (i = 0; i < pl.count; i++) {
3521 r = get_pointer_64(p, offset, left, S, info);
3522 if (r == nullptr)
3523 return;
3524 q = 0;
3525 if (left < sizeof(uint64_t)) {
3526 memcpy(&q, r, left);
3527 outs() << " (protocol_t * entends past the end of the section)\n";
3528 } else
3529 memcpy(&q, r, sizeof(uint64_t));
3530 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3531 sys::swapByteOrder(q);
3532
3533 outs() << "\t\t list[" << i << "] ";
3534 sym_name = get_symbol_64(offset, S, info, n_value, q);
3535 if (n_value != 0) {
3536 if (info->verbose && sym_name != nullptr)
3537 outs() << sym_name;
3538 else
3539 outs() << format("0x%" PRIx64, n_value);
3540 if (q != 0)
3541 outs() << " + " << format("0x%" PRIx64, q);
3542 } else
3543 outs() << format("0x%" PRIx64, q);
3544 outs() << " (struct protocol_t *)\n";
3545
3546 r = get_pointer_64(q + n_value, offset, left, S, info);
3547 if (r == nullptr)
3548 return;
3549 memset(&pc, '\0', sizeof(struct protocol64_t));
3550 if (left < sizeof(struct protocol64_t)) {
3551 memcpy(&pc, r, left);
3552 outs() << " (protocol_t entends past the end of the section)\n";
3553 } else
3554 memcpy(&pc, r, sizeof(struct protocol64_t));
3555 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3556 swapStruct(pc);
3557
3558 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3559
3560 outs() << "\t\t\t name ";
3561 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3562 info, n_value, pc.name);
3563 if (n_value != 0) {
3564 if (info->verbose && sym_name != nullptr)
3565 outs() << sym_name;
3566 else
3567 outs() << format("0x%" PRIx64, n_value);
3568 if (pc.name != 0)
3569 outs() << " + " << format("0x%" PRIx64, pc.name);
3570 } else
3571 outs() << format("0x%" PRIx64, pc.name);
3572 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3573 if (name != nullptr)
3574 outs() << format(" %.*s", left, name);
3575 outs() << "\n";
3576
3577 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3578
3579 outs() << "\t\t instanceMethods ";
3580 sym_name =
3581 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3582 S, info, n_value, pc.instanceMethods);
3583 if (n_value != 0) {
3584 if (info->verbose && sym_name != nullptr)
3585 outs() << sym_name;
3586 else
3587 outs() << format("0x%" PRIx64, n_value);
3588 if (pc.instanceMethods != 0)
3589 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3590 } else
3591 outs() << format("0x%" PRIx64, pc.instanceMethods);
3592 outs() << " (struct method_list_t *)\n";
3593 if (pc.instanceMethods + n_value != 0)
3594 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3595
3596 outs() << "\t\t classMethods ";
3597 sym_name =
3598 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3599 info, n_value, pc.classMethods);
3600 if (n_value != 0) {
3601 if (info->verbose && sym_name != nullptr)
3602 outs() << sym_name;
3603 else
3604 outs() << format("0x%" PRIx64, n_value);
3605 if (pc.classMethods != 0)
3606 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3607 } else
3608 outs() << format("0x%" PRIx64, pc.classMethods);
3609 outs() << " (struct method_list_t *)\n";
3610 if (pc.classMethods + n_value != 0)
3611 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3612
3613 outs() << "\t optionalInstanceMethods "
3614 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3615 outs() << "\t optionalClassMethods "
3616 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3617 outs() << "\t instanceProperties "
3618 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3619
3620 p += sizeof(uint64_t);
3621 offset += sizeof(uint64_t);
3622 }
3623}
3624
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003625static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3626 struct protocol_list32_t pl;
3627 uint32_t q;
3628 struct protocol32_t pc;
3629 const char *r;
3630 uint32_t offset, xoffset, left, i;
3631 SectionRef S, xS;
3632 const char *name;
3633
3634 r = get_pointer_32(p, offset, left, S, info);
3635 if (r == nullptr)
3636 return;
3637 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3638 if (left < sizeof(struct protocol_list32_t)) {
3639 memcpy(&pl, r, left);
3640 outs() << " (protocol_list_t entends past the end of the section)\n";
3641 } else
3642 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3643 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3644 swapStruct(pl);
3645 outs() << " count " << pl.count << "\n";
3646
3647 p += sizeof(struct protocol_list32_t);
3648 offset += sizeof(struct protocol_list32_t);
3649 for (i = 0; i < pl.count; i++) {
3650 r = get_pointer_32(p, offset, left, S, info);
3651 if (r == nullptr)
3652 return;
3653 q = 0;
3654 if (left < sizeof(uint32_t)) {
3655 memcpy(&q, r, left);
3656 outs() << " (protocol_t * entends past the end of the section)\n";
3657 } else
3658 memcpy(&q, r, sizeof(uint32_t));
3659 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3660 sys::swapByteOrder(q);
3661 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3662 << " (struct protocol_t *)\n";
3663 r = get_pointer_32(q, offset, left, S, info);
3664 if (r == nullptr)
3665 return;
3666 memset(&pc, '\0', sizeof(struct protocol32_t));
3667 if (left < sizeof(struct protocol32_t)) {
3668 memcpy(&pc, r, left);
3669 outs() << " (protocol_t entends past the end of the section)\n";
3670 } else
3671 memcpy(&pc, r, sizeof(struct protocol32_t));
3672 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3673 swapStruct(pc);
3674 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3675 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3676 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3677 if (name != nullptr)
3678 outs() << format(" %.*s", left, name);
3679 outs() << "\n";
3680 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3681 outs() << "\t\t instanceMethods "
3682 << format("0x%" PRIx32, pc.instanceMethods)
3683 << " (struct method_list_t *)\n";
3684 if (pc.instanceMethods != 0)
3685 print_method_list32_t(pc.instanceMethods, info, "\t");
3686 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3687 << " (struct method_list_t *)\n";
3688 if (pc.classMethods != 0)
3689 print_method_list32_t(pc.classMethods, info, "\t");
3690 outs() << "\t optionalInstanceMethods "
3691 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3692 outs() << "\t optionalClassMethods "
3693 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3694 outs() << "\t instanceProperties "
3695 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3696 p += sizeof(uint32_t);
3697 offset += sizeof(uint32_t);
3698 }
3699}
3700
Kevin Enderby846c0002015-04-16 17:19:59 +00003701static void print_indent(uint32_t indent) {
3702 for (uint32_t i = 0; i < indent;) {
3703 if (indent - i >= 8) {
3704 outs() << "\t";
3705 i += 8;
3706 } else {
3707 for (uint32_t j = i; j < indent; j++)
3708 outs() << " ";
3709 return;
3710 }
3711 }
3712}
3713
3714static bool print_method_description_list(uint32_t p, uint32_t indent,
3715 struct DisassembleInfo *info) {
3716 uint32_t offset, left, xleft;
3717 SectionRef S;
3718 struct objc_method_description_list_t mdl;
3719 struct objc_method_description_t md;
3720 const char *r, *list, *name;
3721 int32_t i;
3722
3723 r = get_pointer_32(p, offset, left, S, info, true);
3724 if (r == nullptr)
3725 return true;
3726
3727 outs() << "\n";
3728 if (left > sizeof(struct objc_method_description_list_t)) {
3729 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3730 } else {
3731 print_indent(indent);
3732 outs() << " objc_method_description_list extends past end of the section\n";
3733 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3734 memcpy(&mdl, r, left);
3735 }
3736 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3737 swapStruct(mdl);
3738
3739 print_indent(indent);
3740 outs() << " count " << mdl.count << "\n";
3741
3742 list = r + sizeof(struct objc_method_description_list_t);
3743 for (i = 0; i < mdl.count; i++) {
3744 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3745 print_indent(indent);
3746 outs() << " remaining list entries extend past the of the section\n";
3747 break;
3748 }
3749 print_indent(indent);
3750 outs() << " list[" << i << "]\n";
3751 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3752 sizeof(struct objc_method_description_t));
3753 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3754 swapStruct(md);
3755
3756 print_indent(indent);
3757 outs() << " name " << format("0x%08" PRIx32, md.name);
3758 if (info->verbose) {
3759 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3760 if (name != nullptr)
3761 outs() << format(" %.*s", xleft, name);
3762 else
3763 outs() << " (not in an __OBJC section)";
3764 }
3765 outs() << "\n";
3766
3767 print_indent(indent);
3768 outs() << " types " << format("0x%08" PRIx32, md.types);
3769 if (info->verbose) {
3770 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3771 if (name != nullptr)
3772 outs() << format(" %.*s", xleft, name);
3773 else
3774 outs() << " (not in an __OBJC section)";
3775 }
3776 outs() << "\n";
3777 }
3778 return false;
3779}
3780
3781static bool print_protocol_list(uint32_t p, uint32_t indent,
3782 struct DisassembleInfo *info);
3783
3784static bool print_protocol(uint32_t p, uint32_t indent,
3785 struct DisassembleInfo *info) {
3786 uint32_t offset, left;
3787 SectionRef S;
3788 struct objc_protocol_t protocol;
3789 const char *r, *name;
3790
3791 r = get_pointer_32(p, offset, left, S, info, true);
3792 if (r == nullptr)
3793 return true;
3794
3795 outs() << "\n";
3796 if (left >= sizeof(struct objc_protocol_t)) {
3797 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3798 } else {
3799 print_indent(indent);
3800 outs() << " Protocol extends past end of the section\n";
3801 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3802 memcpy(&protocol, r, left);
3803 }
3804 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3805 swapStruct(protocol);
3806
3807 print_indent(indent);
3808 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3809 << "\n";
3810
3811 print_indent(indent);
3812 outs() << " protocol_name "
3813 << format("0x%08" PRIx32, protocol.protocol_name);
3814 if (info->verbose) {
3815 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3816 if (name != nullptr)
3817 outs() << format(" %.*s", left, name);
3818 else
3819 outs() << " (not in an __OBJC section)";
3820 }
3821 outs() << "\n";
3822
3823 print_indent(indent);
3824 outs() << " protocol_list "
3825 << format("0x%08" PRIx32, protocol.protocol_list);
3826 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3827 outs() << " (not in an __OBJC section)\n";
3828
3829 print_indent(indent);
3830 outs() << " instance_methods "
3831 << format("0x%08" PRIx32, protocol.instance_methods);
3832 if (print_method_description_list(protocol.instance_methods, indent, info))
3833 outs() << " (not in an __OBJC section)\n";
3834
3835 print_indent(indent);
3836 outs() << " class_methods "
3837 << format("0x%08" PRIx32, protocol.class_methods);
3838 if (print_method_description_list(protocol.class_methods, indent, info))
3839 outs() << " (not in an __OBJC section)\n";
3840
3841 return false;
3842}
3843
3844static bool print_protocol_list(uint32_t p, uint32_t indent,
3845 struct DisassembleInfo *info) {
3846 uint32_t offset, left, l;
3847 SectionRef S;
3848 struct objc_protocol_list_t protocol_list;
3849 const char *r, *list;
3850 int32_t i;
3851
3852 r = get_pointer_32(p, offset, left, S, info, true);
3853 if (r == nullptr)
3854 return true;
3855
3856 outs() << "\n";
3857 if (left > sizeof(struct objc_protocol_list_t)) {
3858 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
3859 } else {
3860 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
3861 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
3862 memcpy(&protocol_list, r, left);
3863 }
3864 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3865 swapStruct(protocol_list);
3866
3867 print_indent(indent);
3868 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
3869 << "\n";
3870 print_indent(indent);
3871 outs() << " count " << protocol_list.count << "\n";
3872
3873 list = r + sizeof(struct objc_protocol_list_t);
3874 for (i = 0; i < protocol_list.count; i++) {
3875 if ((i + 1) * sizeof(uint32_t) > left) {
3876 outs() << "\t\t remaining list entries extend past the of the section\n";
3877 break;
3878 }
3879 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
3880 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3881 sys::swapByteOrder(l);
3882
3883 print_indent(indent);
3884 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
3885 if (print_protocol(l, indent, info))
3886 outs() << "(not in an __OBJC section)\n";
3887 }
3888 return false;
3889}
3890
Kevin Enderby0fc11822015-04-01 20:57:01 +00003891static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3892 struct ivar_list64_t il;
3893 struct ivar64_t i;
3894 const char *r;
3895 uint32_t offset, xoffset, left, j;
3896 SectionRef S, xS;
3897 const char *name, *sym_name, *ivar_offset_p;
3898 uint64_t ivar_offset, n_value;
3899
3900 r = get_pointer_64(p, offset, left, S, info);
3901 if (r == nullptr)
3902 return;
3903 memset(&il, '\0', sizeof(struct ivar_list64_t));
3904 if (left < sizeof(struct ivar_list64_t)) {
3905 memcpy(&il, r, left);
3906 outs() << " (ivar_list_t entends past the end of the section)\n";
3907 } else
3908 memcpy(&il, r, sizeof(struct ivar_list64_t));
3909 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3910 swapStruct(il);
3911 outs() << " entsize " << il.entsize << "\n";
3912 outs() << " count " << il.count << "\n";
3913
3914 p += sizeof(struct ivar_list64_t);
3915 offset += sizeof(struct ivar_list64_t);
3916 for (j = 0; j < il.count; j++) {
3917 r = get_pointer_64(p, offset, left, S, info);
3918 if (r == nullptr)
3919 return;
3920 memset(&i, '\0', sizeof(struct ivar64_t));
3921 if (left < sizeof(struct ivar64_t)) {
3922 memcpy(&i, r, left);
3923 outs() << " (ivar_t entends past the end of the section)\n";
3924 } else
3925 memcpy(&i, r, sizeof(struct ivar64_t));
3926 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3927 swapStruct(i);
3928
3929 outs() << "\t\t\t offset ";
3930 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
3931 info, n_value, i.offset);
3932 if (n_value != 0) {
3933 if (info->verbose && sym_name != nullptr)
3934 outs() << sym_name;
3935 else
3936 outs() << format("0x%" PRIx64, n_value);
3937 if (i.offset != 0)
3938 outs() << " + " << format("0x%" PRIx64, i.offset);
3939 } else
3940 outs() << format("0x%" PRIx64, i.offset);
3941 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
3942 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
3943 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
3944 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3945 sys::swapByteOrder(ivar_offset);
3946 outs() << " " << ivar_offset << "\n";
3947 } else
3948 outs() << "\n";
3949
3950 outs() << "\t\t\t name ";
3951 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
3952 n_value, i.name);
3953 if (n_value != 0) {
3954 if (info->verbose && sym_name != nullptr)
3955 outs() << sym_name;
3956 else
3957 outs() << format("0x%" PRIx64, n_value);
3958 if (i.name != 0)
3959 outs() << " + " << format("0x%" PRIx64, i.name);
3960 } else
3961 outs() << format("0x%" PRIx64, i.name);
3962 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
3963 if (name != nullptr)
3964 outs() << format(" %.*s", left, name);
3965 outs() << "\n";
3966
3967 outs() << "\t\t\t type ";
3968 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
3969 n_value, i.name);
3970 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
3971 if (n_value != 0) {
3972 if (info->verbose && sym_name != nullptr)
3973 outs() << sym_name;
3974 else
3975 outs() << format("0x%" PRIx64, n_value);
3976 if (i.type != 0)
3977 outs() << " + " << format("0x%" PRIx64, i.type);
3978 } else
3979 outs() << format("0x%" PRIx64, i.type);
3980 if (name != nullptr)
3981 outs() << format(" %.*s", left, name);
3982 outs() << "\n";
3983
3984 outs() << "\t\t\talignment " << i.alignment << "\n";
3985 outs() << "\t\t\t size " << i.size << "\n";
3986
3987 p += sizeof(struct ivar64_t);
3988 offset += sizeof(struct ivar64_t);
3989 }
3990}
3991
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003992static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
3993 struct ivar_list32_t il;
3994 struct ivar32_t i;
3995 const char *r;
3996 uint32_t offset, xoffset, left, j;
3997 SectionRef S, xS;
3998 const char *name, *ivar_offset_p;
3999 uint32_t ivar_offset;
4000
4001 r = get_pointer_32(p, offset, left, S, info);
4002 if (r == nullptr)
4003 return;
4004 memset(&il, '\0', sizeof(struct ivar_list32_t));
4005 if (left < sizeof(struct ivar_list32_t)) {
4006 memcpy(&il, r, left);
4007 outs() << " (ivar_list_t entends past the end of the section)\n";
4008 } else
4009 memcpy(&il, r, sizeof(struct ivar_list32_t));
4010 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4011 swapStruct(il);
4012 outs() << " entsize " << il.entsize << "\n";
4013 outs() << " count " << il.count << "\n";
4014
4015 p += sizeof(struct ivar_list32_t);
4016 offset += sizeof(struct ivar_list32_t);
4017 for (j = 0; j < il.count; j++) {
4018 r = get_pointer_32(p, offset, left, S, info);
4019 if (r == nullptr)
4020 return;
4021 memset(&i, '\0', sizeof(struct ivar32_t));
4022 if (left < sizeof(struct ivar32_t)) {
4023 memcpy(&i, r, left);
4024 outs() << " (ivar_t entends past the end of the section)\n";
4025 } else
4026 memcpy(&i, r, sizeof(struct ivar32_t));
4027 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4028 swapStruct(i);
4029
4030 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4031 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4032 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4033 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4034 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4035 sys::swapByteOrder(ivar_offset);
4036 outs() << " " << ivar_offset << "\n";
4037 } else
4038 outs() << "\n";
4039
4040 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4041 name = get_pointer_32(i.name, xoffset, left, xS, info);
4042 if (name != nullptr)
4043 outs() << format(" %.*s", left, name);
4044 outs() << "\n";
4045
4046 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4047 name = get_pointer_32(i.type, xoffset, left, xS, info);
4048 if (name != nullptr)
4049 outs() << format(" %.*s", left, name);
4050 outs() << "\n";
4051
4052 outs() << "\t\t\talignment " << i.alignment << "\n";
4053 outs() << "\t\t\t size " << i.size << "\n";
4054
4055 p += sizeof(struct ivar32_t);
4056 offset += sizeof(struct ivar32_t);
4057 }
4058}
4059
Kevin Enderby0fc11822015-04-01 20:57:01 +00004060static void print_objc_property_list64(uint64_t p,
4061 struct DisassembleInfo *info) {
4062 struct objc_property_list64 opl;
4063 struct objc_property64 op;
4064 const char *r;
4065 uint32_t offset, xoffset, left, j;
4066 SectionRef S, xS;
4067 const char *name, *sym_name;
4068 uint64_t n_value;
4069
4070 r = get_pointer_64(p, offset, left, S, info);
4071 if (r == nullptr)
4072 return;
4073 memset(&opl, '\0', sizeof(struct objc_property_list64));
4074 if (left < sizeof(struct objc_property_list64)) {
4075 memcpy(&opl, r, left);
4076 outs() << " (objc_property_list entends past the end of the section)\n";
4077 } else
4078 memcpy(&opl, r, sizeof(struct objc_property_list64));
4079 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4080 swapStruct(opl);
4081 outs() << " entsize " << opl.entsize << "\n";
4082 outs() << " count " << opl.count << "\n";
4083
4084 p += sizeof(struct objc_property_list64);
4085 offset += sizeof(struct objc_property_list64);
4086 for (j = 0; j < opl.count; j++) {
4087 r = get_pointer_64(p, offset, left, S, info);
4088 if (r == nullptr)
4089 return;
4090 memset(&op, '\0', sizeof(struct objc_property64));
4091 if (left < sizeof(struct objc_property64)) {
4092 memcpy(&op, r, left);
4093 outs() << " (objc_property entends past the end of the section)\n";
4094 } else
4095 memcpy(&op, r, sizeof(struct objc_property64));
4096 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4097 swapStruct(op);
4098
4099 outs() << "\t\t\t name ";
4100 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4101 info, n_value, op.name);
4102 if (n_value != 0) {
4103 if (info->verbose && sym_name != nullptr)
4104 outs() << sym_name;
4105 else
4106 outs() << format("0x%" PRIx64, n_value);
4107 if (op.name != 0)
4108 outs() << " + " << format("0x%" PRIx64, op.name);
4109 } else
4110 outs() << format("0x%" PRIx64, op.name);
4111 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4112 if (name != nullptr)
4113 outs() << format(" %.*s", left, name);
4114 outs() << "\n";
4115
4116 outs() << "\t\t\tattributes ";
4117 sym_name =
4118 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4119 info, n_value, op.attributes);
4120 if (n_value != 0) {
4121 if (info->verbose && sym_name != nullptr)
4122 outs() << sym_name;
4123 else
4124 outs() << format("0x%" PRIx64, n_value);
4125 if (op.attributes != 0)
4126 outs() << " + " << format("0x%" PRIx64, op.attributes);
4127 } else
4128 outs() << format("0x%" PRIx64, op.attributes);
4129 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4130 if (name != nullptr)
4131 outs() << format(" %.*s", left, name);
4132 outs() << "\n";
4133
4134 p += sizeof(struct objc_property64);
4135 offset += sizeof(struct objc_property64);
4136 }
4137}
4138
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004139static void print_objc_property_list32(uint32_t p,
4140 struct DisassembleInfo *info) {
4141 struct objc_property_list32 opl;
4142 struct objc_property32 op;
4143 const char *r;
4144 uint32_t offset, xoffset, left, j;
4145 SectionRef S, xS;
4146 const char *name;
4147
4148 r = get_pointer_32(p, offset, left, S, info);
4149 if (r == nullptr)
4150 return;
4151 memset(&opl, '\0', sizeof(struct objc_property_list32));
4152 if (left < sizeof(struct objc_property_list32)) {
4153 memcpy(&opl, r, left);
4154 outs() << " (objc_property_list entends past the end of the section)\n";
4155 } else
4156 memcpy(&opl, r, sizeof(struct objc_property_list32));
4157 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4158 swapStruct(opl);
4159 outs() << " entsize " << opl.entsize << "\n";
4160 outs() << " count " << opl.count << "\n";
4161
4162 p += sizeof(struct objc_property_list32);
4163 offset += sizeof(struct objc_property_list32);
4164 for (j = 0; j < opl.count; j++) {
4165 r = get_pointer_32(p, offset, left, S, info);
4166 if (r == nullptr)
4167 return;
4168 memset(&op, '\0', sizeof(struct objc_property32));
4169 if (left < sizeof(struct objc_property32)) {
4170 memcpy(&op, r, left);
4171 outs() << " (objc_property entends past the end of the section)\n";
4172 } else
4173 memcpy(&op, r, sizeof(struct objc_property32));
4174 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4175 swapStruct(op);
4176
4177 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4178 name = get_pointer_32(op.name, xoffset, left, xS, info);
4179 if (name != nullptr)
4180 outs() << format(" %.*s", left, name);
4181 outs() << "\n";
4182
4183 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4184 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4185 if (name != nullptr)
4186 outs() << format(" %.*s", left, name);
4187 outs() << "\n";
4188
4189 p += sizeof(struct objc_property32);
4190 offset += sizeof(struct objc_property32);
4191 }
4192}
4193
Kevin Enderby0fc11822015-04-01 20:57:01 +00004194static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4195 bool &is_meta_class) {
4196 struct class_ro64_t cro;
4197 const char *r;
4198 uint32_t offset, xoffset, left;
4199 SectionRef S, xS;
4200 const char *name, *sym_name;
4201 uint64_t n_value;
4202
4203 r = get_pointer_64(p, offset, left, S, info);
4204 if (r == nullptr || left < sizeof(struct class_ro64_t))
4205 return;
4206 memset(&cro, '\0', sizeof(struct class_ro64_t));
4207 if (left < sizeof(struct class_ro64_t)) {
4208 memcpy(&cro, r, left);
4209 outs() << " (class_ro_t entends past the end of the section)\n";
4210 } else
4211 memcpy(&cro, r, sizeof(struct class_ro64_t));
4212 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4213 swapStruct(cro);
4214 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4215 if (cro.flags & RO_META)
4216 outs() << " RO_META";
4217 if (cro.flags & RO_ROOT)
4218 outs() << " RO_ROOT";
4219 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4220 outs() << " RO_HAS_CXX_STRUCTORS";
4221 outs() << "\n";
4222 outs() << " instanceStart " << cro.instanceStart << "\n";
4223 outs() << " instanceSize " << cro.instanceSize << "\n";
4224 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4225 << "\n";
4226 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4227 << "\n";
4228 print_layout_map64(cro.ivarLayout, info);
4229
4230 outs() << " name ";
4231 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4232 info, n_value, cro.name);
4233 if (n_value != 0) {
4234 if (info->verbose && sym_name != nullptr)
4235 outs() << sym_name;
4236 else
4237 outs() << format("0x%" PRIx64, n_value);
4238 if (cro.name != 0)
4239 outs() << " + " << format("0x%" PRIx64, cro.name);
4240 } else
4241 outs() << format("0x%" PRIx64, cro.name);
4242 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4243 if (name != nullptr)
4244 outs() << format(" %.*s", left, name);
4245 outs() << "\n";
4246
4247 outs() << " baseMethods ";
4248 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4249 S, info, n_value, cro.baseMethods);
4250 if (n_value != 0) {
4251 if (info->verbose && sym_name != nullptr)
4252 outs() << sym_name;
4253 else
4254 outs() << format("0x%" PRIx64, n_value);
4255 if (cro.baseMethods != 0)
4256 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4257 } else
4258 outs() << format("0x%" PRIx64, cro.baseMethods);
4259 outs() << " (struct method_list_t *)\n";
4260 if (cro.baseMethods + n_value != 0)
4261 print_method_list64_t(cro.baseMethods + n_value, info, "");
4262
4263 outs() << " baseProtocols ";
4264 sym_name =
4265 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4266 info, n_value, cro.baseProtocols);
4267 if (n_value != 0) {
4268 if (info->verbose && sym_name != nullptr)
4269 outs() << sym_name;
4270 else
4271 outs() << format("0x%" PRIx64, n_value);
4272 if (cro.baseProtocols != 0)
4273 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4274 } else
4275 outs() << format("0x%" PRIx64, cro.baseProtocols);
4276 outs() << "\n";
4277 if (cro.baseProtocols + n_value != 0)
4278 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4279
4280 outs() << " ivars ";
4281 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004282 info, n_value, cro.ivars);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004283 if (n_value != 0) {
4284 if (info->verbose && sym_name != nullptr)
4285 outs() << sym_name;
4286 else
4287 outs() << format("0x%" PRIx64, n_value);
4288 if (cro.ivars != 0)
4289 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4290 } else
4291 outs() << format("0x%" PRIx64, cro.ivars);
4292 outs() << "\n";
4293 if (cro.ivars + n_value != 0)
4294 print_ivar_list64_t(cro.ivars + n_value, info);
4295
4296 outs() << " weakIvarLayout ";
4297 sym_name =
4298 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4299 info, n_value, cro.weakIvarLayout);
4300 if (n_value != 0) {
4301 if (info->verbose && sym_name != nullptr)
4302 outs() << sym_name;
4303 else
4304 outs() << format("0x%" PRIx64, n_value);
4305 if (cro.weakIvarLayout != 0)
4306 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4307 } else
4308 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4309 outs() << "\n";
4310 print_layout_map64(cro.weakIvarLayout + n_value, info);
4311
4312 outs() << " baseProperties ";
4313 sym_name =
4314 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4315 info, n_value, cro.baseProperties);
4316 if (n_value != 0) {
4317 if (info->verbose && sym_name != nullptr)
4318 outs() << sym_name;
4319 else
4320 outs() << format("0x%" PRIx64, n_value);
4321 if (cro.baseProperties != 0)
4322 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4323 } else
4324 outs() << format("0x%" PRIx64, cro.baseProperties);
4325 outs() << "\n";
4326 if (cro.baseProperties + n_value != 0)
4327 print_objc_property_list64(cro.baseProperties + n_value, info);
4328
4329 is_meta_class = (cro.flags & RO_META) ? true : false;
4330}
4331
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004332static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4333 bool &is_meta_class) {
4334 struct class_ro32_t cro;
4335 const char *r;
4336 uint32_t offset, xoffset, left;
4337 SectionRef S, xS;
4338 const char *name;
4339
4340 r = get_pointer_32(p, offset, left, S, info);
4341 if (r == nullptr)
4342 return;
4343 memset(&cro, '\0', sizeof(struct class_ro32_t));
4344 if (left < sizeof(struct class_ro32_t)) {
4345 memcpy(&cro, r, left);
4346 outs() << " (class_ro_t entends past the end of the section)\n";
4347 } else
4348 memcpy(&cro, r, sizeof(struct class_ro32_t));
4349 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4350 swapStruct(cro);
4351 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4352 if (cro.flags & RO_META)
4353 outs() << " RO_META";
4354 if (cro.flags & RO_ROOT)
4355 outs() << " RO_ROOT";
4356 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4357 outs() << " RO_HAS_CXX_STRUCTORS";
4358 outs() << "\n";
4359 outs() << " instanceStart " << cro.instanceStart << "\n";
4360 outs() << " instanceSize " << cro.instanceSize << "\n";
4361 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4362 << "\n";
4363 print_layout_map32(cro.ivarLayout, info);
4364
4365 outs() << " name " << format("0x%" PRIx32, cro.name);
4366 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4367 if (name != nullptr)
4368 outs() << format(" %.*s", left, name);
4369 outs() << "\n";
4370
4371 outs() << " baseMethods "
4372 << format("0x%" PRIx32, cro.baseMethods)
4373 << " (struct method_list_t *)\n";
4374 if (cro.baseMethods != 0)
4375 print_method_list32_t(cro.baseMethods, info, "");
4376
4377 outs() << " baseProtocols "
4378 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4379 if (cro.baseProtocols != 0)
4380 print_protocol_list32_t(cro.baseProtocols, info);
4381 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4382 << "\n";
4383 if (cro.ivars != 0)
4384 print_ivar_list32_t(cro.ivars, info);
4385 outs() << " weakIvarLayout "
4386 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4387 print_layout_map32(cro.weakIvarLayout, info);
4388 outs() << " baseProperties "
4389 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4390 if (cro.baseProperties != 0)
4391 print_objc_property_list32(cro.baseProperties, info);
4392 is_meta_class = (cro.flags & RO_META) ? true : false;
4393}
4394
Kevin Enderby0fc11822015-04-01 20:57:01 +00004395static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4396 struct class64_t c;
4397 const char *r;
4398 uint32_t offset, left;
4399 SectionRef S;
4400 const char *name;
4401 uint64_t isa_n_value, n_value;
4402
4403 r = get_pointer_64(p, offset, left, S, info);
4404 if (r == nullptr || left < sizeof(struct class64_t))
4405 return;
4406 memset(&c, '\0', sizeof(struct class64_t));
4407 if (left < sizeof(struct class64_t)) {
4408 memcpy(&c, r, left);
4409 outs() << " (class_t entends past the end of the section)\n";
4410 } else
4411 memcpy(&c, r, sizeof(struct class64_t));
4412 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4413 swapStruct(c);
4414
4415 outs() << " isa " << format("0x%" PRIx64, c.isa);
4416 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4417 isa_n_value, c.isa);
4418 if (name != nullptr)
4419 outs() << " " << name;
4420 outs() << "\n";
4421
4422 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4423 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4424 n_value, c.superclass);
4425 if (name != nullptr)
4426 outs() << " " << name;
4427 outs() << "\n";
4428
4429 outs() << " cache " << format("0x%" PRIx64, c.cache);
4430 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4431 n_value, c.cache);
4432 if (name != nullptr)
4433 outs() << " " << name;
4434 outs() << "\n";
4435
4436 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4437 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4438 n_value, c.vtable);
4439 if (name != nullptr)
4440 outs() << " " << name;
4441 outs() << "\n";
4442
4443 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4444 n_value, c.data);
4445 outs() << " data ";
4446 if (n_value != 0) {
4447 if (info->verbose && name != nullptr)
4448 outs() << name;
4449 else
4450 outs() << format("0x%" PRIx64, n_value);
4451 if (c.data != 0)
4452 outs() << " + " << format("0x%" PRIx64, c.data);
4453 } else
4454 outs() << format("0x%" PRIx64, c.data);
4455 outs() << " (struct class_ro_t *)";
4456
4457 // This is a Swift class if some of the low bits of the pointer are set.
4458 if ((c.data + n_value) & 0x7)
4459 outs() << " Swift class";
4460 outs() << "\n";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004461 bool is_meta_class;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004462 print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class);
4463
Hans Wennborgcc9deb42015-09-29 18:02:48 +00004464 if (!is_meta_class) {
Kevin Enderby0fc11822015-04-01 20:57:01 +00004465 outs() << "Meta Class\n";
4466 print_class64_t(c.isa + isa_n_value, info);
4467 }
4468}
4469
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004470static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4471 struct class32_t c;
4472 const char *r;
4473 uint32_t offset, left;
4474 SectionRef S;
4475 const char *name;
4476
4477 r = get_pointer_32(p, offset, left, S, info);
4478 if (r == nullptr)
4479 return;
4480 memset(&c, '\0', sizeof(struct class32_t));
4481 if (left < sizeof(struct class32_t)) {
4482 memcpy(&c, r, left);
4483 outs() << " (class_t entends past the end of the section)\n";
4484 } else
4485 memcpy(&c, r, sizeof(struct class32_t));
4486 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4487 swapStruct(c);
4488
4489 outs() << " isa " << format("0x%" PRIx32, c.isa);
4490 name =
4491 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4492 if (name != nullptr)
4493 outs() << " " << name;
4494 outs() << "\n";
4495
4496 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4497 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4498 c.superclass);
4499 if (name != nullptr)
4500 outs() << " " << name;
4501 outs() << "\n";
4502
4503 outs() << " cache " << format("0x%" PRIx32, c.cache);
4504 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4505 c.cache);
4506 if (name != nullptr)
4507 outs() << " " << name;
4508 outs() << "\n";
4509
4510 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4511 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4512 c.vtable);
4513 if (name != nullptr)
4514 outs() << " " << name;
4515 outs() << "\n";
4516
4517 name =
4518 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4519 outs() << " data " << format("0x%" PRIx32, c.data)
4520 << " (struct class_ro_t *)";
4521
4522 // This is a Swift class if some of the low bits of the pointer are set.
4523 if (c.data & 0x3)
4524 outs() << " Swift class";
4525 outs() << "\n";
4526 bool is_meta_class;
4527 print_class_ro32_t(c.data & ~0x3, info, is_meta_class);
4528
Hans Wennborgcc9deb42015-09-29 18:02:48 +00004529 if (!is_meta_class) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004530 outs() << "Meta Class\n";
4531 print_class32_t(c.isa, info);
4532 }
4533}
4534
Kevin Enderby846c0002015-04-16 17:19:59 +00004535static void print_objc_class_t(struct objc_class_t *objc_class,
4536 struct DisassembleInfo *info) {
4537 uint32_t offset, left, xleft;
4538 const char *name, *p, *ivar_list;
4539 SectionRef S;
4540 int32_t i;
4541 struct objc_ivar_list_t objc_ivar_list;
4542 struct objc_ivar_t ivar;
4543
4544 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4545 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4546 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4547 if (name != nullptr)
4548 outs() << format(" %.*s", left, name);
4549 else
4550 outs() << " (not in an __OBJC section)";
4551 }
4552 outs() << "\n";
4553
4554 outs() << "\t super_class "
4555 << format("0x%08" PRIx32, objc_class->super_class);
4556 if (info->verbose) {
4557 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4558 if (name != nullptr)
4559 outs() << format(" %.*s", left, name);
4560 else
4561 outs() << " (not in an __OBJC section)";
4562 }
4563 outs() << "\n";
4564
4565 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4566 if (info->verbose) {
4567 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4568 if (name != nullptr)
4569 outs() << format(" %.*s", left, name);
4570 else
4571 outs() << " (not in an __OBJC section)";
4572 }
4573 outs() << "\n";
4574
4575 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4576 << "\n";
4577
4578 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4579 if (info->verbose) {
4580 if (CLS_GETINFO(objc_class, CLS_CLASS))
4581 outs() << " CLS_CLASS";
4582 else if (CLS_GETINFO(objc_class, CLS_META))
4583 outs() << " CLS_META";
4584 }
4585 outs() << "\n";
4586
4587 outs() << "\t instance_size "
4588 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4589
4590 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4591 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4592 if (p != nullptr) {
4593 if (left > sizeof(struct objc_ivar_list_t)) {
4594 outs() << "\n";
4595 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4596 } else {
4597 outs() << " (entends past the end of the section)\n";
4598 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4599 memcpy(&objc_ivar_list, p, left);
4600 }
4601 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4602 swapStruct(objc_ivar_list);
4603 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4604 ivar_list = p + sizeof(struct objc_ivar_list_t);
4605 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4606 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4607 outs() << "\t\t remaining ivar's extend past the of the section\n";
4608 break;
4609 }
4610 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4611 sizeof(struct objc_ivar_t));
4612 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4613 swapStruct(ivar);
4614
4615 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4616 if (info->verbose) {
4617 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4618 if (name != nullptr)
4619 outs() << format(" %.*s", xleft, name);
4620 else
4621 outs() << " (not in an __OBJC section)";
4622 }
4623 outs() << "\n";
4624
4625 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4626 if (info->verbose) {
4627 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4628 if (name != nullptr)
4629 outs() << format(" %.*s", xleft, name);
4630 else
4631 outs() << " (not in an __OBJC section)";
4632 }
4633 outs() << "\n";
4634
4635 outs() << "\t\t ivar_offset "
4636 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4637 }
4638 } else {
4639 outs() << " (not in an __OBJC section)\n";
4640 }
4641
4642 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4643 if (print_method_list(objc_class->methodLists, info))
4644 outs() << " (not in an __OBJC section)\n";
4645
4646 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4647 << "\n";
4648
4649 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4650 if (print_protocol_list(objc_class->protocols, 16, info))
4651 outs() << " (not in an __OBJC section)\n";
4652}
4653
4654static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4655 struct DisassembleInfo *info) {
4656 uint32_t offset, left;
4657 const char *name;
4658 SectionRef S;
4659
4660 outs() << "\t category name "
4661 << format("0x%08" PRIx32, objc_category->category_name);
4662 if (info->verbose) {
4663 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4664 true);
4665 if (name != nullptr)
4666 outs() << format(" %.*s", left, name);
4667 else
4668 outs() << " (not in an __OBJC section)";
4669 }
4670 outs() << "\n";
4671
4672 outs() << "\t\t class name "
4673 << format("0x%08" PRIx32, objc_category->class_name);
4674 if (info->verbose) {
4675 name =
4676 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4677 if (name != nullptr)
4678 outs() << format(" %.*s", left, name);
4679 else
4680 outs() << " (not in an __OBJC section)";
4681 }
4682 outs() << "\n";
4683
4684 outs() << "\t instance methods "
4685 << format("0x%08" PRIx32, objc_category->instance_methods);
4686 if (print_method_list(objc_category->instance_methods, info))
4687 outs() << " (not in an __OBJC section)\n";
4688
4689 outs() << "\t class methods "
4690 << format("0x%08" PRIx32, objc_category->class_methods);
4691 if (print_method_list(objc_category->class_methods, info))
4692 outs() << " (not in an __OBJC section)\n";
4693}
4694
Kevin Enderby0fc11822015-04-01 20:57:01 +00004695static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4696 struct category64_t c;
4697 const char *r;
4698 uint32_t offset, xoffset, left;
4699 SectionRef S, xS;
4700 const char *name, *sym_name;
4701 uint64_t n_value;
4702
4703 r = get_pointer_64(p, offset, left, S, info);
4704 if (r == nullptr)
4705 return;
4706 memset(&c, '\0', sizeof(struct category64_t));
4707 if (left < sizeof(struct category64_t)) {
4708 memcpy(&c, r, left);
4709 outs() << " (category_t entends past the end of the section)\n";
4710 } else
4711 memcpy(&c, r, sizeof(struct category64_t));
4712 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4713 swapStruct(c);
4714
4715 outs() << " name ";
4716 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4717 info, n_value, c.name);
4718 if (n_value != 0) {
4719 if (info->verbose && sym_name != nullptr)
4720 outs() << sym_name;
4721 else
4722 outs() << format("0x%" PRIx64, n_value);
4723 if (c.name != 0)
4724 outs() << " + " << format("0x%" PRIx64, c.name);
4725 } else
4726 outs() << format("0x%" PRIx64, c.name);
4727 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4728 if (name != nullptr)
4729 outs() << format(" %.*s", left, name);
4730 outs() << "\n";
4731
4732 outs() << " cls ";
4733 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4734 n_value, c.cls);
4735 if (n_value != 0) {
4736 if (info->verbose && sym_name != nullptr)
4737 outs() << sym_name;
4738 else
4739 outs() << format("0x%" PRIx64, n_value);
4740 if (c.cls != 0)
4741 outs() << " + " << format("0x%" PRIx64, c.cls);
4742 } else
4743 outs() << format("0x%" PRIx64, c.cls);
4744 outs() << "\n";
4745 if (c.cls + n_value != 0)
4746 print_class64_t(c.cls + n_value, info);
4747
4748 outs() << " instanceMethods ";
4749 sym_name =
4750 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4751 info, n_value, c.instanceMethods);
4752 if (n_value != 0) {
4753 if (info->verbose && sym_name != nullptr)
4754 outs() << sym_name;
4755 else
4756 outs() << format("0x%" PRIx64, n_value);
4757 if (c.instanceMethods != 0)
4758 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4759 } else
4760 outs() << format("0x%" PRIx64, c.instanceMethods);
4761 outs() << "\n";
4762 if (c.instanceMethods + n_value != 0)
4763 print_method_list64_t(c.instanceMethods + n_value, info, "");
4764
4765 outs() << " classMethods ";
4766 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4767 S, info, n_value, c.classMethods);
4768 if (n_value != 0) {
4769 if (info->verbose && sym_name != nullptr)
4770 outs() << sym_name;
4771 else
4772 outs() << format("0x%" PRIx64, n_value);
4773 if (c.classMethods != 0)
4774 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4775 } else
4776 outs() << format("0x%" PRIx64, c.classMethods);
4777 outs() << "\n";
4778 if (c.classMethods + n_value != 0)
4779 print_method_list64_t(c.classMethods + n_value, info, "");
4780
4781 outs() << " protocols ";
4782 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4783 info, n_value, c.protocols);
4784 if (n_value != 0) {
4785 if (info->verbose && sym_name != nullptr)
4786 outs() << sym_name;
4787 else
4788 outs() << format("0x%" PRIx64, n_value);
4789 if (c.protocols != 0)
4790 outs() << " + " << format("0x%" PRIx64, c.protocols);
4791 } else
4792 outs() << format("0x%" PRIx64, c.protocols);
4793 outs() << "\n";
4794 if (c.protocols + n_value != 0)
4795 print_protocol_list64_t(c.protocols + n_value, info);
4796
4797 outs() << "instanceProperties ";
4798 sym_name =
4799 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4800 S, info, n_value, c.instanceProperties);
4801 if (n_value != 0) {
4802 if (info->verbose && sym_name != nullptr)
4803 outs() << sym_name;
4804 else
4805 outs() << format("0x%" PRIx64, n_value);
4806 if (c.instanceProperties != 0)
4807 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4808 } else
4809 outs() << format("0x%" PRIx64, c.instanceProperties);
4810 outs() << "\n";
4811 if (c.instanceProperties + n_value != 0)
4812 print_objc_property_list64(c.instanceProperties + n_value, info);
4813}
4814
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004815static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4816 struct category32_t c;
4817 const char *r;
4818 uint32_t offset, left;
4819 SectionRef S, xS;
4820 const char *name;
4821
4822 r = get_pointer_32(p, offset, left, S, info);
4823 if (r == nullptr)
4824 return;
4825 memset(&c, '\0', sizeof(struct category32_t));
4826 if (left < sizeof(struct category32_t)) {
4827 memcpy(&c, r, left);
4828 outs() << " (category_t entends past the end of the section)\n";
4829 } else
4830 memcpy(&c, r, sizeof(struct category32_t));
4831 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4832 swapStruct(c);
4833
4834 outs() << " name " << format("0x%" PRIx32, c.name);
4835 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4836 c.name);
Hans Wennborgcc9deb42015-09-29 18:02:48 +00004837 if (name)
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004838 outs() << " " << name;
4839 outs() << "\n";
4840
4841 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
4842 if (c.cls != 0)
4843 print_class32_t(c.cls, info);
4844 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4845 << "\n";
4846 if (c.instanceMethods != 0)
4847 print_method_list32_t(c.instanceMethods, info, "");
4848 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
4849 << "\n";
4850 if (c.classMethods != 0)
4851 print_method_list32_t(c.classMethods, info, "");
4852 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
4853 if (c.protocols != 0)
4854 print_protocol_list32_t(c.protocols, info);
4855 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
4856 << "\n";
4857 if (c.instanceProperties != 0)
4858 print_objc_property_list32(c.instanceProperties, info);
4859}
4860
Kevin Enderby0fc11822015-04-01 20:57:01 +00004861static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
4862 uint32_t i, left, offset, xoffset;
4863 uint64_t p, n_value;
4864 struct message_ref64 mr;
4865 const char *name, *sym_name;
4866 const char *r;
4867 SectionRef xS;
4868
4869 if (S == SectionRef())
4870 return;
4871
4872 StringRef SectName;
4873 S.getName(SectName);
4874 DataRefImpl Ref = S.getRawDataRefImpl();
4875 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4876 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4877 offset = 0;
4878 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4879 p = S.getAddress() + i;
4880 r = get_pointer_64(p, offset, left, S, info);
4881 if (r == nullptr)
4882 return;
4883 memset(&mr, '\0', sizeof(struct message_ref64));
4884 if (left < sizeof(struct message_ref64)) {
4885 memcpy(&mr, r, left);
4886 outs() << " (message_ref entends past the end of the section)\n";
4887 } else
4888 memcpy(&mr, r, sizeof(struct message_ref64));
4889 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4890 swapStruct(mr);
4891
4892 outs() << " imp ";
4893 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
4894 n_value, mr.imp);
4895 if (n_value != 0) {
4896 outs() << format("0x%" PRIx64, n_value) << " ";
4897 if (mr.imp != 0)
4898 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
4899 } else
4900 outs() << format("0x%" PRIx64, mr.imp) << " ";
4901 if (name != nullptr)
4902 outs() << " " << name;
4903 outs() << "\n";
4904
4905 outs() << " sel ";
4906 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
4907 info, n_value, mr.sel);
4908 if (n_value != 0) {
4909 if (info->verbose && sym_name != nullptr)
4910 outs() << sym_name;
4911 else
4912 outs() << format("0x%" PRIx64, n_value);
4913 if (mr.sel != 0)
4914 outs() << " + " << format("0x%" PRIx64, mr.sel);
4915 } else
4916 outs() << format("0x%" PRIx64, mr.sel);
4917 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
4918 if (name != nullptr)
4919 outs() << format(" %.*s", left, name);
4920 outs() << "\n";
4921
4922 offset += sizeof(struct message_ref64);
4923 }
4924}
4925
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004926static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
4927 uint32_t i, left, offset, xoffset, p;
4928 struct message_ref32 mr;
4929 const char *name, *r;
4930 SectionRef xS;
4931
4932 if (S == SectionRef())
4933 return;
4934
4935 StringRef SectName;
4936 S.getName(SectName);
4937 DataRefImpl Ref = S.getRawDataRefImpl();
4938 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4939 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4940 offset = 0;
4941 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4942 p = S.getAddress() + i;
4943 r = get_pointer_32(p, offset, left, S, info);
4944 if (r == nullptr)
4945 return;
4946 memset(&mr, '\0', sizeof(struct message_ref32));
4947 if (left < sizeof(struct message_ref32)) {
4948 memcpy(&mr, r, left);
4949 outs() << " (message_ref entends past the end of the section)\n";
4950 } else
4951 memcpy(&mr, r, sizeof(struct message_ref32));
4952 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4953 swapStruct(mr);
4954
4955 outs() << " imp " << format("0x%" PRIx32, mr.imp);
4956 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
4957 mr.imp);
4958 if (name != nullptr)
4959 outs() << " " << name;
4960 outs() << "\n";
4961
4962 outs() << " sel " << format("0x%" PRIx32, mr.sel);
4963 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
4964 if (name != nullptr)
4965 outs() << " " << name;
4966 outs() << "\n";
4967
4968 offset += sizeof(struct message_ref32);
4969 }
4970}
4971
Kevin Enderby0fc11822015-04-01 20:57:01 +00004972static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
4973 uint32_t left, offset, swift_version;
4974 uint64_t p;
4975 struct objc_image_info64 o;
4976 const char *r;
4977
4978 StringRef SectName;
4979 S.getName(SectName);
4980 DataRefImpl Ref = S.getRawDataRefImpl();
4981 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4982 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4983 p = S.getAddress();
4984 r = get_pointer_64(p, offset, left, S, info);
4985 if (r == nullptr)
4986 return;
4987 memset(&o, '\0', sizeof(struct objc_image_info64));
4988 if (left < sizeof(struct objc_image_info64)) {
4989 memcpy(&o, r, left);
4990 outs() << " (objc_image_info entends past the end of the section)\n";
4991 } else
4992 memcpy(&o, r, sizeof(struct objc_image_info64));
4993 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4994 swapStruct(o);
4995 outs() << " version " << o.version << "\n";
4996 outs() << " flags " << format("0x%" PRIx32, o.flags);
4997 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
4998 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
4999 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5000 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5001 swift_version = (o.flags >> 8) & 0xff;
5002 if (swift_version != 0) {
5003 if (swift_version == 1)
5004 outs() << " Swift 1.0";
5005 else if (swift_version == 2)
5006 outs() << " Swift 1.1";
5007 else
5008 outs() << " unknown future Swift version (" << swift_version << ")";
5009 }
5010 outs() << "\n";
5011}
5012
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005013static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5014 uint32_t left, offset, swift_version, p;
5015 struct objc_image_info32 o;
5016 const char *r;
5017
5018 StringRef SectName;
5019 S.getName(SectName);
5020 DataRefImpl Ref = S.getRawDataRefImpl();
5021 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5022 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5023 p = S.getAddress();
5024 r = get_pointer_32(p, offset, left, S, info);
5025 if (r == nullptr)
5026 return;
5027 memset(&o, '\0', sizeof(struct objc_image_info32));
5028 if (left < sizeof(struct objc_image_info32)) {
5029 memcpy(&o, r, left);
5030 outs() << " (objc_image_info entends past the end of the section)\n";
5031 } else
5032 memcpy(&o, r, sizeof(struct objc_image_info32));
5033 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5034 swapStruct(o);
5035 outs() << " version " << o.version << "\n";
5036 outs() << " flags " << format("0x%" PRIx32, o.flags);
5037 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5038 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5039 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5040 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5041 swift_version = (o.flags >> 8) & 0xff;
5042 if (swift_version != 0) {
5043 if (swift_version == 1)
5044 outs() << " Swift 1.0";
5045 else if (swift_version == 2)
5046 outs() << " Swift 1.1";
5047 else
5048 outs() << " unknown future Swift version (" << swift_version << ")";
5049 }
5050 outs() << "\n";
5051}
5052
Kevin Enderby846c0002015-04-16 17:19:59 +00005053static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5054 uint32_t left, offset, p;
5055 struct imageInfo_t o;
5056 const char *r;
5057
5058 StringRef SectName;
5059 S.getName(SectName);
5060 DataRefImpl Ref = S.getRawDataRefImpl();
5061 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5062 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5063 p = S.getAddress();
5064 r = get_pointer_32(p, offset, left, S, info);
5065 if (r == nullptr)
5066 return;
5067 memset(&o, '\0', sizeof(struct imageInfo_t));
5068 if (left < sizeof(struct imageInfo_t)) {
5069 memcpy(&o, r, left);
5070 outs() << " (imageInfo entends past the end of the section)\n";
5071 } else
5072 memcpy(&o, r, sizeof(struct imageInfo_t));
5073 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5074 swapStruct(o);
5075 outs() << " version " << o.version << "\n";
5076 outs() << " flags " << format("0x%" PRIx32, o.flags);
5077 if (o.flags & 0x1)
5078 outs() << " F&C";
5079 if (o.flags & 0x2)
5080 outs() << " GC";
5081 if (o.flags & 0x4)
5082 outs() << " GC-only";
5083 else
5084 outs() << " RR";
5085 outs() << "\n";
5086}
5087
Kevin Enderby0fc11822015-04-01 20:57:01 +00005088static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5089 SymbolAddressMap AddrMap;
5090 if (verbose)
5091 CreateSymbolAddressMap(O, &AddrMap);
5092
5093 std::vector<SectionRef> Sections;
5094 for (const SectionRef &Section : O->sections()) {
5095 StringRef SectName;
5096 Section.getName(SectName);
5097 Sections.push_back(Section);
5098 }
5099
5100 struct DisassembleInfo info;
5101 // Set up the block of info used by the Symbolizer call backs.
5102 info.verbose = verbose;
5103 info.O = O;
5104 info.AddrMap = &AddrMap;
5105 info.Sections = &Sections;
5106 info.class_name = nullptr;
5107 info.selector_name = nullptr;
5108 info.method = nullptr;
5109 info.demangled_name = nullptr;
5110 info.bindtable = nullptr;
5111 info.adrp_addr = 0;
5112 info.adrp_inst = 0;
5113
5114 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5115 if (CL != SectionRef()) {
5116 info.S = CL;
5117 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5118 } else {
5119 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5120 info.S = CL;
5121 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5122 }
5123
5124 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5125 if (CR != SectionRef()) {
5126 info.S = CR;
5127 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5128 } else {
5129 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5130 info.S = CR;
5131 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5132 }
5133
5134 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5135 if (SR != SectionRef()) {
5136 info.S = SR;
5137 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5138 } else {
5139 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5140 info.S = SR;
5141 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5142 }
5143
5144 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5145 if (CA != SectionRef()) {
5146 info.S = CA;
5147 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5148 } else {
5149 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5150 info.S = CA;
5151 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5152 }
5153
5154 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5155 if (PL != SectionRef()) {
5156 info.S = PL;
5157 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5158 } else {
5159 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5160 info.S = PL;
5161 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5162 }
5163
5164 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5165 if (MR != SectionRef()) {
5166 info.S = MR;
5167 print_message_refs64(MR, &info);
5168 } else {
5169 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5170 info.S = MR;
5171 print_message_refs64(MR, &info);
5172 }
5173
5174 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5175 if (II != SectionRef()) {
5176 info.S = II;
5177 print_image_info64(II, &info);
5178 } else {
5179 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5180 info.S = II;
5181 print_image_info64(II, &info);
5182 }
Kevin Enderby0bc6ed42015-04-01 21:50:45 +00005183
5184 if (info.bindtable != nullptr)
5185 delete info.bindtable;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005186}
5187
5188static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005189 SymbolAddressMap AddrMap;
5190 if (verbose)
5191 CreateSymbolAddressMap(O, &AddrMap);
5192
5193 std::vector<SectionRef> Sections;
5194 for (const SectionRef &Section : O->sections()) {
5195 StringRef SectName;
5196 Section.getName(SectName);
5197 Sections.push_back(Section);
5198 }
5199
5200 struct DisassembleInfo info;
5201 // Set up the block of info used by the Symbolizer call backs.
5202 info.verbose = verbose;
5203 info.O = O;
5204 info.AddrMap = &AddrMap;
5205 info.Sections = &Sections;
5206 info.class_name = nullptr;
5207 info.selector_name = nullptr;
5208 info.method = nullptr;
5209 info.demangled_name = nullptr;
5210 info.bindtable = nullptr;
5211 info.adrp_addr = 0;
5212 info.adrp_inst = 0;
5213
5214 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5215 if (CL != SectionRef()) {
5216 info.S = CL;
5217 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5218 } else {
5219 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5220 info.S = CL;
5221 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5222 }
5223
5224 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5225 if (CR != SectionRef()) {
5226 info.S = CR;
5227 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5228 } else {
5229 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5230 info.S = CR;
5231 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5232 }
5233
5234 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5235 if (SR != SectionRef()) {
5236 info.S = SR;
5237 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5238 } else {
5239 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5240 info.S = SR;
5241 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5242 }
5243
5244 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5245 if (CA != SectionRef()) {
5246 info.S = CA;
5247 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5248 } else {
5249 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5250 info.S = CA;
5251 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5252 }
5253
5254 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5255 if (PL != SectionRef()) {
5256 info.S = PL;
5257 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5258 } else {
5259 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5260 info.S = PL;
5261 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5262 }
5263
5264 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5265 if (MR != SectionRef()) {
5266 info.S = MR;
5267 print_message_refs32(MR, &info);
5268 } else {
5269 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5270 info.S = MR;
5271 print_message_refs32(MR, &info);
5272 }
5273
5274 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5275 if (II != SectionRef()) {
5276 info.S = II;
5277 print_image_info32(II, &info);
5278 } else {
5279 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5280 info.S = II;
5281 print_image_info32(II, &info);
5282 }
Kevin Enderby0fc11822015-04-01 20:57:01 +00005283}
5284
5285static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby846c0002015-04-16 17:19:59 +00005286 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5287 const char *r, *name, *defs;
5288 struct objc_module_t module;
5289 SectionRef S, xS;
5290 struct objc_symtab_t symtab;
5291 struct objc_class_t objc_class;
5292 struct objc_category_t objc_category;
5293
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005294 outs() << "Objective-C segment\n";
Kevin Enderby846c0002015-04-16 17:19:59 +00005295 S = get_section(O, "__OBJC", "__module_info");
5296 if (S == SectionRef())
5297 return false;
5298
5299 SymbolAddressMap AddrMap;
5300 if (verbose)
5301 CreateSymbolAddressMap(O, &AddrMap);
5302
5303 std::vector<SectionRef> Sections;
5304 for (const SectionRef &Section : O->sections()) {
5305 StringRef SectName;
5306 Section.getName(SectName);
5307 Sections.push_back(Section);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005308 }
Kevin Enderby846c0002015-04-16 17:19:59 +00005309
5310 struct DisassembleInfo info;
5311 // Set up the block of info used by the Symbolizer call backs.
5312 info.verbose = verbose;
5313 info.O = O;
5314 info.AddrMap = &AddrMap;
5315 info.Sections = &Sections;
5316 info.class_name = nullptr;
5317 info.selector_name = nullptr;
5318 info.method = nullptr;
5319 info.demangled_name = nullptr;
5320 info.bindtable = nullptr;
5321 info.adrp_addr = 0;
5322 info.adrp_inst = 0;
5323
5324 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5325 p = S.getAddress() + i;
5326 r = get_pointer_32(p, offset, left, S, &info, true);
5327 if (r == nullptr)
5328 return true;
5329 memset(&module, '\0', sizeof(struct objc_module_t));
5330 if (left < sizeof(struct objc_module_t)) {
5331 memcpy(&module, r, left);
5332 outs() << " (module extends past end of __module_info section)\n";
5333 } else
5334 memcpy(&module, r, sizeof(struct objc_module_t));
5335 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5336 swapStruct(module);
5337
5338 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5339 outs() << " version " << module.version << "\n";
5340 outs() << " size " << module.size << "\n";
5341 outs() << " name ";
5342 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5343 if (name != nullptr)
5344 outs() << format("%.*s", left, name);
5345 else
5346 outs() << format("0x%08" PRIx32, module.name)
5347 << "(not in an __OBJC section)";
5348 outs() << "\n";
5349
5350 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5351 if (module.symtab == 0 || r == nullptr) {
5352 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5353 << " (not in an __OBJC section)\n";
5354 continue;
5355 }
5356 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5357 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5358 defs_left = 0;
5359 defs = nullptr;
5360 if (left < sizeof(struct objc_symtab_t)) {
5361 memcpy(&symtab, r, left);
5362 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5363 } else {
5364 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5365 if (left > sizeof(struct objc_symtab_t)) {
5366 defs_left = left - sizeof(struct objc_symtab_t);
5367 defs = r + sizeof(struct objc_symtab_t);
5368 }
5369 }
5370 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5371 swapStruct(symtab);
5372
5373 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5374 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5375 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5376 if (r == nullptr)
5377 outs() << " (not in an __OBJC section)";
5378 outs() << "\n";
5379 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5380 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5381 if (symtab.cls_def_cnt > 0)
5382 outs() << "\tClass Definitions\n";
5383 for (j = 0; j < symtab.cls_def_cnt; j++) {
5384 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5385 outs() << "\t(remaining class defs entries entends past the end of the "
5386 << "section)\n";
5387 break;
5388 }
5389 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5390 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5391 sys::swapByteOrder(def);
5392
5393 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5394 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5395 if (r != nullptr) {
5396 if (left > sizeof(struct objc_class_t)) {
5397 outs() << "\n";
5398 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5399 } else {
5400 outs() << " (entends past the end of the section)\n";
5401 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5402 memcpy(&objc_class, r, left);
5403 }
5404 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5405 swapStruct(objc_class);
5406 print_objc_class_t(&objc_class, &info);
5407 } else {
5408 outs() << "(not in an __OBJC section)\n";
5409 }
5410
5411 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5412 outs() << "\tMeta Class";
5413 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5414 if (r != nullptr) {
5415 if (left > sizeof(struct objc_class_t)) {
5416 outs() << "\n";
5417 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5418 } else {
5419 outs() << " (entends past the end of the section)\n";
5420 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5421 memcpy(&objc_class, r, left);
5422 }
5423 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5424 swapStruct(objc_class);
5425 print_objc_class_t(&objc_class, &info);
5426 } else {
5427 outs() << "(not in an __OBJC section)\n";
5428 }
5429 }
5430 }
5431 if (symtab.cat_def_cnt > 0)
5432 outs() << "\tCategory Definitions\n";
5433 for (j = 0; j < symtab.cat_def_cnt; j++) {
5434 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5435 outs() << "\t(remaining category defs entries entends past the end of "
5436 << "the section)\n";
5437 break;
5438 }
5439 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5440 sizeof(uint32_t));
5441 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5442 sys::swapByteOrder(def);
5443
5444 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5445 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5446 << format("0x%08" PRIx32, def);
5447 if (r != nullptr) {
5448 if (left > sizeof(struct objc_category_t)) {
5449 outs() << "\n";
5450 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5451 } else {
5452 outs() << " (entends past the end of the section)\n";
5453 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5454 memcpy(&objc_category, r, left);
5455 }
5456 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5457 swapStruct(objc_category);
5458 print_objc_objc_category_t(&objc_category, &info);
5459 } else {
5460 outs() << "(not in an __OBJC section)\n";
5461 }
5462 }
5463 }
5464 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5465 if (II != SectionRef())
5466 print_image_info(II, &info);
5467
5468 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005469}
5470
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00005471static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5472 uint32_t size, uint32_t addr) {
5473 SymbolAddressMap AddrMap;
5474 CreateSymbolAddressMap(O, &AddrMap);
5475
5476 std::vector<SectionRef> Sections;
5477 for (const SectionRef &Section : O->sections()) {
5478 StringRef SectName;
5479 Section.getName(SectName);
5480 Sections.push_back(Section);
5481 }
5482
5483 struct DisassembleInfo info;
5484 // Set up the block of info used by the Symbolizer call backs.
5485 info.verbose = true;
5486 info.O = O;
5487 info.AddrMap = &AddrMap;
5488 info.Sections = &Sections;
5489 info.class_name = nullptr;
5490 info.selector_name = nullptr;
5491 info.method = nullptr;
5492 info.demangled_name = nullptr;
5493 info.bindtable = nullptr;
5494 info.adrp_addr = 0;
5495 info.adrp_inst = 0;
5496
5497 const char *p;
5498 struct objc_protocol_t protocol;
5499 uint32_t left, paddr;
5500 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5501 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5502 left = size - (p - sect);
5503 if (left < sizeof(struct objc_protocol_t)) {
5504 outs() << "Protocol extends past end of __protocol section\n";
5505 memcpy(&protocol, p, left);
5506 } else
5507 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5508 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5509 swapStruct(protocol);
5510 paddr = addr + (p - sect);
5511 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5512 if (print_protocol(paddr, 0, &info))
5513 outs() << "(not in an __OBJC section)\n";
5514 }
5515}
5516
Kevin Enderby0fc11822015-04-01 20:57:01 +00005517static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
5518 if (O->is64Bit())
5519 printObjc2_64bit_MetaData(O, verbose);
5520 else {
5521 MachO::mach_header H;
5522 H = O->getHeader();
5523 if (H.cputype == MachO::CPU_TYPE_ARM)
5524 printObjc2_32bit_MetaData(O, verbose);
5525 else {
5526 // This is the 32-bit non-arm cputype case. Which is normally
5527 // the first Objective-C ABI. But it may be the case of a
5528 // binary for the iOS simulator which is the second Objective-C
5529 // ABI. In that case printObjc1_32bit_MetaData() will determine that
5530 // and return false.
Hans Wennborgcc9deb42015-09-29 18:02:48 +00005531 if (!printObjc1_32bit_MetaData(O, verbose))
Kevin Enderby0fc11822015-04-01 20:57:01 +00005532 printObjc2_32bit_MetaData(O, verbose);
5533 }
5534 }
5535}
5536
Kevin Enderbybf246f52014-09-24 23:08:22 +00005537// GuessLiteralPointer returns a string which for the item in the Mach-O file
5538// for the address passed in as ReferenceValue for printing as a comment with
5539// the instruction and also returns the corresponding type of that item
5540// indirectly through ReferenceType.
5541//
5542// If ReferenceValue is an address of literal cstring then a pointer to the
5543// cstring is returned and ReferenceType is set to
5544// LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
5545//
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005546// If ReferenceValue is an address of an Objective-C CFString, Selector ref or
5547// Class ref that name is returned and the ReferenceType is set accordingly.
5548//
5549// Lastly, literals which are Symbol address in a literal pool are looked for
5550// and if found the symbol name is returned and ReferenceType is set to
5551// LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
5552//
5553// If there is no item in the Mach-O file for the address passed in as
5554// ReferenceValue nullptr is returned and ReferenceType is unchanged.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00005555static const char *GuessLiteralPointer(uint64_t ReferenceValue,
5556 uint64_t ReferencePC,
5557 uint64_t *ReferenceType,
5558 struct DisassembleInfo *info) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005559 // First see if there is an external relocation entry at the ReferencePC.
Rafael Espindola80291272014-10-08 15:28:58 +00005560 uint64_t sect_addr = info->S.getAddress();
Kevin Enderbybf246f52014-09-24 23:08:22 +00005561 uint64_t sect_offset = ReferencePC - sect_addr;
5562 bool reloc_found = false;
5563 DataRefImpl Rel;
5564 MachO::any_relocation_info RE;
5565 bool isExtern = false;
5566 SymbolRef Symbol;
5567 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00005568 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderbybf246f52014-09-24 23:08:22 +00005569 if (RelocOffset == sect_offset) {
5570 Rel = Reloc.getRawDataRefImpl();
5571 RE = info->O->getRelocation(Rel);
5572 if (info->O->isRelocationScattered(RE))
5573 continue;
5574 isExtern = info->O->getPlainRelocationExternal(RE);
5575 if (isExtern) {
5576 symbol_iterator RelocSym = Reloc.getSymbol();
5577 Symbol = *RelocSym;
5578 }
5579 reloc_found = true;
5580 break;
5581 }
5582 }
5583 // If there is an external relocation entry for a symbol in a section
5584 // then used that symbol's value for the value of the reference.
5585 if (reloc_found && isExtern) {
5586 if (info->O->getAnyRelocationPCRel(RE)) {
5587 unsigned Type = info->O->getAnyRelocationType(RE);
5588 if (Type == MachO::X86_64_RELOC_SIGNED) {
Rafael Espindoladea00162015-07-03 17:44:18 +00005589 ReferenceValue = Symbol.getValue();
Kevin Enderbybf246f52014-09-24 23:08:22 +00005590 }
5591 }
5592 }
5593
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005594 // Look for literals such as Objective-C CFStrings refs, Selector refs,
5595 // Message refs and Class refs.
5596 bool classref, selref, msgref, cfstring;
5597 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
5598 selref, msgref, cfstring);
David Blaikie33dd45d02015-03-23 18:39:02 +00005599 if (classref && pointer_value == 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005600 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
5601 // And the pointer_value in that section is typically zero as it will be
5602 // set by dyld as part of the "bind information".
5603 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
5604 if (name != nullptr) {
5605 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
Hans Wennborgdb53e302014-10-23 21:59:17 +00005606 const char *class_name = strrchr(name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005607 if (class_name != nullptr && class_name[1] == '_' &&
5608 class_name[2] != '\0') {
5609 info->class_name = class_name + 2;
5610 return name;
5611 }
5612 }
5613 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005614
David Blaikie33dd45d02015-03-23 18:39:02 +00005615 if (classref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005616 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5617 const char *name =
5618 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
5619 if (name != nullptr)
5620 info->class_name = name;
5621 else
5622 name = "bad class ref";
Kevin Enderbybf246f52014-09-24 23:08:22 +00005623 return name;
5624 }
5625
David Blaikie33dd45d02015-03-23 18:39:02 +00005626 if (cfstring) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005627 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
5628 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
5629 return name;
5630 }
5631
David Blaikie33dd45d02015-03-23 18:39:02 +00005632 if (selref && pointer_value == 0)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005633 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
5634
5635 if (pointer_value != 0)
5636 ReferenceValue = pointer_value;
5637
5638 const char *name = GuessCstringPointer(ReferenceValue, info);
5639 if (name) {
David Blaikie33dd45d02015-03-23 18:39:02 +00005640 if (pointer_value != 0 && selref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005641 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
5642 info->selector_name = name;
David Blaikie33dd45d02015-03-23 18:39:02 +00005643 } else if (pointer_value != 0 && msgref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005644 info->class_name = nullptr;
5645 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
5646 info->selector_name = name;
5647 } else
5648 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
5649 return name;
5650 }
5651
5652 // Lastly look for an indirect symbol with this ReferenceValue which is in
5653 // a literal pool. If found return that symbol name.
5654 name = GuessIndirectSymbol(ReferenceValue, info);
5655 if (name) {
5656 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
5657 return name;
5658 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005659
5660 return nullptr;
5661}
5662
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005663// SymbolizerSymbolLookUp is the symbol lookup function passed when creating
Kevin Enderbybf246f52014-09-24 23:08:22 +00005664// the Symbolizer. It looks up the ReferenceValue using the info passed via the
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005665// pointer to the struct DisassembleInfo that was passed when MCSymbolizer
5666// is created and returns the symbol name that matches the ReferenceValue or
5667// nullptr if none. The ReferenceType is passed in for the IN type of
5668// reference the instruction is making from the values in defined in the header
5669// "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
5670// Out type and the ReferenceName will also be set which is added as a comment
5671// to the disassembled instruction.
5672//
Kevin Enderby04bf6932014-10-28 23:39:46 +00005673#if HAVE_CXXABI_H
5674// If the symbol name is a C++ mangled name then the demangled name is
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005675// returned through ReferenceName and ReferenceType is set to
5676// LLVMDisassembler_ReferenceType_DeMangled_Name .
Kevin Enderby04bf6932014-10-28 23:39:46 +00005677#endif
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005678//
5679// When this is called to get a symbol name for a branch target then the
5680// ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
5681// SymbolValue will be looked for in the indirect symbol table to determine if
5682// it is an address for a symbol stub. If so then the symbol name for that
5683// stub is returned indirectly through ReferenceName and then ReferenceType is
5684// set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
5685//
Kevin Enderbybf246f52014-09-24 23:08:22 +00005686// When this is called with an value loaded via a PC relative load then
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005687// ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
5688// SymbolValue is checked to be an address of literal pointer, symbol pointer,
5689// or an Objective-C meta data reference. If so the output ReferenceType is
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005690// set to correspond to that as well as setting the ReferenceName.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00005691static const char *SymbolizerSymbolLookUp(void *DisInfo,
5692 uint64_t ReferenceValue,
5693 uint64_t *ReferenceType,
5694 uint64_t ReferencePC,
5695 const char **ReferenceName) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005696 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
Kevin Enderbybf246f52014-09-24 23:08:22 +00005697 // If no verbose symbolic information is wanted then just return nullptr.
David Blaikie33dd45d02015-03-23 18:39:02 +00005698 if (!info->verbose) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005699 *ReferenceName = nullptr;
5700 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005701 return nullptr;
5702 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005703
Kevin Enderbyf6d25852015-01-31 00:37:11 +00005704 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderbybf246f52014-09-24 23:08:22 +00005705
Kevin Enderby85974882014-09-26 22:20:44 +00005706 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
5707 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005708 if (*ReferenceName != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005709 method_reference(info, ReferenceType, ReferenceName);
5710 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
5711 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
5712 } else
Kevin Enderby04bf6932014-10-28 23:39:46 +00005713#if HAVE_CXXABI_H
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005714 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00005715 if (info->demangled_name != nullptr)
5716 free(info->demangled_name);
5717 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005718 info->demangled_name =
5719 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005720 if (info->demangled_name != nullptr) {
5721 *ReferenceName = info->demangled_name;
5722 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5723 } else
5724 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5725 } else
5726#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005727 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5728 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
5729 *ReferenceName =
5730 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
Kevin Enderby85974882014-09-26 22:20:44 +00005731 if (*ReferenceName)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005732 method_reference(info, ReferenceType, ReferenceName);
Kevin Enderby85974882014-09-26 22:20:44 +00005733 else
5734 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005735 // If this is arm64 and the reference is an adrp instruction save the
5736 // instruction, passed in ReferenceValue and the address of the instruction
5737 // for use later if we see and add immediate instruction.
5738 } else if (info->O->getArch() == Triple::aarch64 &&
5739 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
5740 info->adrp_inst = ReferenceValue;
5741 info->adrp_addr = ReferencePC;
5742 SymbolName = nullptr;
5743 *ReferenceName = nullptr;
5744 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5745 // If this is arm64 and reference is an add immediate instruction and we
5746 // have
5747 // seen an adrp instruction just before it and the adrp's Xd register
5748 // matches
5749 // this add's Xn register reconstruct the value being referenced and look to
5750 // see if it is a literal pointer. Note the add immediate instruction is
5751 // passed in ReferenceValue.
5752 } else if (info->O->getArch() == Triple::aarch64 &&
5753 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
5754 ReferencePC - 4 == info->adrp_addr &&
5755 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5756 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5757 uint32_t addxri_inst;
5758 uint64_t adrp_imm, addxri_imm;
5759
5760 adrp_imm =
5761 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5762 if (info->adrp_inst & 0x0200000)
5763 adrp_imm |= 0xfffffffffc000000LL;
5764
5765 addxri_inst = ReferenceValue;
5766 addxri_imm = (addxri_inst >> 10) & 0xfff;
5767 if (((addxri_inst >> 22) & 0x3) == 1)
5768 addxri_imm <<= 12;
5769
5770 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5771 (adrp_imm << 12) + addxri_imm;
5772
5773 *ReferenceName =
5774 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5775 if (*ReferenceName == nullptr)
5776 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5777 // If this is arm64 and the reference is a load register instruction and we
5778 // have seen an adrp instruction just before it and the adrp's Xd register
5779 // matches this add's Xn register reconstruct the value being referenced and
5780 // look to see if it is a literal pointer. Note the load register
5781 // instruction is passed in ReferenceValue.
5782 } else if (info->O->getArch() == Triple::aarch64 &&
5783 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
5784 ReferencePC - 4 == info->adrp_addr &&
5785 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5786 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5787 uint32_t ldrxui_inst;
5788 uint64_t adrp_imm, ldrxui_imm;
5789
5790 adrp_imm =
5791 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5792 if (info->adrp_inst & 0x0200000)
5793 adrp_imm |= 0xfffffffffc000000LL;
5794
5795 ldrxui_inst = ReferenceValue;
5796 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
5797
5798 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5799 (adrp_imm << 12) + (ldrxui_imm << 3);
5800
5801 *ReferenceName =
5802 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5803 if (*ReferenceName == nullptr)
5804 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5805 }
5806 // If this arm64 and is an load register (PC-relative) instruction the
5807 // ReferenceValue is the PC plus the immediate value.
5808 else if (info->O->getArch() == Triple::aarch64 &&
5809 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
5810 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
5811 *ReferenceName =
5812 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5813 if (*ReferenceName == nullptr)
5814 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby85974882014-09-26 22:20:44 +00005815 }
Kevin Enderby04bf6932014-10-28 23:39:46 +00005816#if HAVE_CXXABI_H
5817 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5818 if (info->demangled_name != nullptr)
5819 free(info->demangled_name);
5820 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005821 info->demangled_name =
5822 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005823 if (info->demangled_name != nullptr) {
5824 *ReferenceName = info->demangled_name;
5825 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5826 }
5827 }
5828#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005829 else {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005830 *ReferenceName = nullptr;
5831 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5832 }
5833
5834 return SymbolName;
5835}
5836
Kevin Enderbybf246f52014-09-24 23:08:22 +00005837/// \brief Emits the comments that are stored in the CommentStream.
5838/// Each comment in the CommentStream must end with a newline.
5839static void emitComments(raw_svector_ostream &CommentStream,
5840 SmallString<128> &CommentsToEmit,
5841 formatted_raw_ostream &FormattedOS,
5842 const MCAsmInfo &MAI) {
5843 // Flush the stream before taking its content.
Kevin Enderbybf246f52014-09-24 23:08:22 +00005844 StringRef Comments = CommentsToEmit.str();
5845 // Get the default information for printing a comment.
5846 const char *CommentBegin = MAI.getCommentString();
5847 unsigned CommentColumn = MAI.getCommentColumn();
5848 bool IsFirst = true;
5849 while (!Comments.empty()) {
5850 if (!IsFirst)
5851 FormattedOS << '\n';
5852 // Emit a line of comments.
5853 FormattedOS.PadToColumn(CommentColumn);
5854 size_t Position = Comments.find('\n');
5855 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
5856 // Move after the newline character.
5857 Comments = Comments.substr(Position + 1);
5858 IsFirst = false;
5859 }
5860 FormattedOS.flush();
5861
5862 // Tell the comment stream that the vector changed underneath it.
5863 CommentsToEmit.clear();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005864}
5865
Kevin Enderby95df54c2015-02-04 01:01:38 +00005866static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
5867 StringRef DisSegName, StringRef DisSectName) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005868 const char *McpuDefault = nullptr;
5869 const Target *ThumbTarget = nullptr;
5870 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005871 if (!TheTarget) {
5872 // GetTarget prints out stuff.
5873 return;
5874 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005875 if (MCPU.empty() && McpuDefault)
5876 MCPU = McpuDefault;
5877
Ahmed Charles56440fd2014-03-06 05:51:42 +00005878 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005879 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005880 if (ThumbTarget)
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005881 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005882
Kevin Enderbyc9595622014-08-06 23:24:41 +00005883 // Package up features to be passed to target/subtarget
5884 std::string FeaturesStr;
5885 if (MAttrs.size()) {
5886 SubtargetFeatures Features;
5887 for (unsigned i = 0; i != MAttrs.size(); ++i)
5888 Features.AddFeature(MAttrs[i]);
5889 FeaturesStr = Features.getString();
5890 }
5891
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005892 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00005893 std::unique_ptr<const MCRegisterInfo> MRI(
5894 TheTarget->createMCRegInfo(TripleName));
5895 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +00005896 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +00005897 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +00005898 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Craig Toppere6cb63e2014-04-25 04:24:47 +00005899 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005900 std::unique_ptr<MCDisassembler> DisAsm(
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005901 TheTarget->createMCDisassembler(*STI, Ctx));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005902 std::unique_ptr<MCSymbolizer> Symbolizer;
5903 struct DisassembleInfo SymbolizerInfo;
5904 std::unique_ptr<MCRelocationInfo> RelInfo(
5905 TheTarget->createMCRelocationInfo(TripleName, Ctx));
5906 if (RelInfo) {
5907 Symbolizer.reset(TheTarget->createMCSymbolizer(
5908 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00005909 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005910 DisAsm->setSymbolizer(std::move(Symbolizer));
5911 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005912 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +00005913 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00005914 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00005915 // Set the display preference for hex vs. decimal immediates.
5916 IP->setPrintImmHex(PrintImmHex);
5917 // Comment stream and backing vector.
5918 SmallString<128> CommentsToEmit;
5919 raw_svector_ostream CommentStream(CommentsToEmit);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00005920 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
5921 // if it is done then arm64 comments for string literals don't get printed
5922 // and some constant get printed instead and not setting it causes intel
5923 // (32-bit and 64-bit) comments printed with different spacing before the
5924 // comment causing different diffs with the 'C' disassembler library API.
5925 // IP->setCommentStream(CommentStream);
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005926
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005927 if (!AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +00005928 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005929 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005930 return;
5931 }
5932
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005933 // Set up thumb disassembler.
5934 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
5935 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
5936 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005937 std::unique_ptr<MCDisassembler> ThumbDisAsm;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005938 std::unique_ptr<MCInstPrinter> ThumbIP;
5939 std::unique_ptr<MCContext> ThumbCtx;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005940 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
5941 struct DisassembleInfo ThumbSymbolizerInfo;
5942 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005943 if (ThumbTarget) {
5944 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
5945 ThumbAsmInfo.reset(
5946 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
5947 ThumbSTI.reset(
5948 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
5949 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
5950 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
Kevin Enderby930fdc72014-11-06 19:00:13 +00005951 MCContext *PtrThumbCtx = ThumbCtx.get();
5952 ThumbRelInfo.reset(
5953 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
5954 if (ThumbRelInfo) {
5955 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
5956 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00005957 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
Kevin Enderby930fdc72014-11-06 19:00:13 +00005958 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
5959 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005960 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
5961 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00005962 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
5963 *ThumbInstrInfo, *ThumbMRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00005964 // Set the display preference for hex vs. decimal immediates.
5965 ThumbIP->setPrintImmHex(PrintImmHex);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005966 }
5967
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005968 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005969 errs() << "error: couldn't initialize disassembler for target "
5970 << ThumbTripleName << '\n';
5971 return;
5972 }
5973
Charles Davis8bdfafd2013-09-01 04:28:48 +00005974 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005975
Ahmed Bougachaaa790682013-05-24 01:07:04 +00005976 // FIXME: Using the -cfg command line option, this code used to be able to
5977 // annotate relocations with the referenced symbol's name, and if this was
5978 // inside a __[cf]string section, the data it points to. This is now replaced
5979 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +00005980 std::vector<SectionRef> Sections;
5981 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005982 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +00005983 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005984
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00005985 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
Kevin Enderby273ae012013-06-06 17:20:50 +00005986 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005987
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005988 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +00005989 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005990
Kevin Enderby273ae012013-06-06 17:20:50 +00005991 // Build a data in code table that is sorted on by the address of each entry.
5992 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +00005993 if (Header.filetype == MachO::MH_OBJECT)
Rafael Espindola80291272014-10-08 15:28:58 +00005994 BaseAddress = Sections[0].getAddress();
Kevin Enderby273ae012013-06-06 17:20:50 +00005995 else
5996 BaseAddress = BaseSegmentAddress;
5997 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +00005998 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +00005999 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006000 uint32_t Offset;
6001 DI->getOffset(Offset);
6002 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6003 }
6004 array_pod_sort(Dices.begin(), Dices.end());
6005
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006006#ifndef NDEBUG
6007 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6008#else
6009 raw_ostream &DebugOut = nulls();
6010#endif
6011
Ahmed Charles56440fd2014-03-06 05:51:42 +00006012 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +00006013 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +00006014 // Try to find debug info and set up the DIContext for it.
6015 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +00006016 // A separate DSym file path was specified, parse it as a macho file,
6017 // get the sections and supply it to the section name parsing machinery.
6018 if (!DSYMFile.empty()) {
Rafael Espindola48af1c22014-08-19 18:44:46 +00006019 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006020 MemoryBuffer::getFileOrSTDIN(DSYMFile);
Rafael Espindola48af1c22014-08-19 18:44:46 +00006021 if (std::error_code EC = BufOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006022 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
Benjamin Kramer699128e2011-09-21 01:13:19 +00006023 return;
6024 }
Rafael Espindola48af1c22014-08-19 18:44:46 +00006025 DbgObj =
6026 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6027 .get()
6028 .release();
Benjamin Kramer699128e2011-09-21 01:13:19 +00006029 }
6030
Eric Christopher7370b552012-11-12 21:40:38 +00006031 // Setup the DIContext
Zachary Turner6489d7b2015-04-23 17:37:47 +00006032 diContext.reset(new DWARFContextInMemory(*DbgObj));
Benjamin Kramer699128e2011-09-21 01:13:19 +00006033 }
6034
Colin LeMahieufcc32762015-07-29 19:08:10 +00006035 if (FilterSections.size() == 0)
Kevin Enderby95df54c2015-02-04 01:01:38 +00006036 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006037
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006038 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00006039 StringRef SectName;
Kevin Enderby95df54c2015-02-04 01:01:38 +00006040 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6041 continue;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006042
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006043 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006044
Rafael Espindolab0f76a42013-04-05 15:15:22 +00006045 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
Kevin Enderby95df54c2015-02-04 01:01:38 +00006046 if (SegmentName != DisSegName)
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006047 continue;
6048
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006049 StringRef BytesStr;
6050 Sections[SectIdx].getContents(BytesStr);
Aaron Ballman106fd7b2014-11-12 14:01:17 +00006051 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6052 BytesStr.size());
Rafael Espindola80291272014-10-08 15:28:58 +00006053 uint64_t SectAddress = Sections[SectIdx].getAddress();
Rafael Espindolabd604f22014-11-07 00:52:15 +00006054
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006055 bool symbolTableWorked = false;
6056
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006057 // Parse relocations.
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00006058 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
6059 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00006060 uint64_t RelocOffset = Reloc.getOffset();
Rafael Espindola80291272014-10-08 15:28:58 +00006061 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Owen Andersond9243c42011-10-17 21:37:35 +00006062 RelocOffset -= SectionAddress;
6063
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00006064 symbol_iterator RelocSym = Reloc.getSymbol();
Owen Andersond9243c42011-10-17 21:37:35 +00006065
Rafael Espindola806f0062013-06-05 01:33:53 +00006066 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006067 }
6068 array_pod_sort(Relocs.begin(), Relocs.end());
6069
Kevin Enderbybf246f52014-09-24 23:08:22 +00006070 // Create a map of symbol addresses to symbol names for use by
6071 // the SymbolizerSymbolLookUp() routine.
6072 SymbolAddressMap AddrMap;
Kevin Enderby6a221752015-03-17 17:10:57 +00006073 bool DisSymNameFound = false;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006074 for (const SymbolRef &Symbol : MachOOF->symbols()) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006075 SymbolRef::Type ST = Symbol.getType();
Kevin Enderbybf246f52014-09-24 23:08:22 +00006076 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6077 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +00006078 uint64_t Address = Symbol.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006079 ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
6080 if (std::error_code EC = SymNameOrErr.getError())
6081 report_fatal_error(EC.message());
6082 StringRef SymName = *SymNameOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006083 AddrMap[Address] = SymName;
Kevin Enderby6a221752015-03-17 17:10:57 +00006084 if (!DisSymName.empty() && DisSymName == SymName)
6085 DisSymNameFound = true;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006086 }
6087 }
David Blaikie33dd45d02015-03-23 18:39:02 +00006088 if (!DisSymName.empty() && !DisSymNameFound) {
Kevin Enderby6a221752015-03-17 17:10:57 +00006089 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6090 return;
6091 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006092 // Set up the block of info used by the Symbolizer call backs.
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006093 SymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006094 SymbolizerInfo.O = MachOOF;
6095 SymbolizerInfo.S = Sections[SectIdx];
Kevin Enderbybf246f52014-09-24 23:08:22 +00006096 SymbolizerInfo.AddrMap = &AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006097 SymbolizerInfo.Sections = &Sections;
6098 SymbolizerInfo.class_name = nullptr;
6099 SymbolizerInfo.selector_name = nullptr;
6100 SymbolizerInfo.method = nullptr;
Kevin Enderby04bf6932014-10-28 23:39:46 +00006101 SymbolizerInfo.demangled_name = nullptr;
Kevin Enderby078be602014-10-23 19:53:12 +00006102 SymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006103 SymbolizerInfo.adrp_addr = 0;
6104 SymbolizerInfo.adrp_inst = 0;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006105 // Same for the ThumbSymbolizer
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006106 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006107 ThumbSymbolizerInfo.O = MachOOF;
6108 ThumbSymbolizerInfo.S = Sections[SectIdx];
6109 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6110 ThumbSymbolizerInfo.Sections = &Sections;
6111 ThumbSymbolizerInfo.class_name = nullptr;
6112 ThumbSymbolizerInfo.selector_name = nullptr;
6113 ThumbSymbolizerInfo.method = nullptr;
6114 ThumbSymbolizerInfo.demangled_name = nullptr;
6115 ThumbSymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006116 ThumbSymbolizerInfo.adrp_addr = 0;
6117 ThumbSymbolizerInfo.adrp_inst = 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006118
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006119 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006120 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006121 ErrorOr<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
6122 if (std::error_code EC = SymNameOrErr.getError())
6123 report_fatal_error(EC.message());
6124 StringRef SymName = *SymNameOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006125
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006126 SymbolRef::Type ST = Symbols[SymIdx].getType();
Owen Andersond9243c42011-10-17 21:37:35 +00006127 if (ST != SymbolRef::ST_Function)
6128 continue;
6129
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006130 // Make sure the symbol is defined in this section.
Rafael Espindola80291272014-10-08 15:28:58 +00006131 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Owen Andersond9243c42011-10-17 21:37:35 +00006132 if (!containsSym)
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006133 continue;
6134
Kevin Enderby6a221752015-03-17 17:10:57 +00006135 // If we are only disassembling one symbol see if this is that symbol.
6136 if (!DisSymName.empty() && DisSymName != SymName)
6137 continue;
6138
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006139 // Start at the address of the symbol relative to the section's address.
Rafael Espindoladea00162015-07-03 17:44:18 +00006140 uint64_t Start = Symbols[SymIdx].getValue();
Rafael Espindola80291272014-10-08 15:28:58 +00006141 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006142 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006143
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006144 // Stop disassembling either at the beginning of the next symbol or at
6145 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +00006146 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +00006147 uint64_t NextSym = 0;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006148 uint64_t NextSymIdx = SymIdx + 1;
Owen Andersond9243c42011-10-17 21:37:35 +00006149 while (Symbols.size() > NextSymIdx) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006150 SymbolRef::Type NextSymType = Symbols[NextSymIdx].getType();
Owen Andersond9243c42011-10-17 21:37:35 +00006151 if (NextSymType == SymbolRef::ST_Function) {
Rafael Espindola80291272014-10-08 15:28:58 +00006152 containsNextSym =
6153 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Rafael Espindoladea00162015-07-03 17:44:18 +00006154 NextSym = Symbols[NextSymIdx].getValue();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006155 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006156 break;
6157 }
6158 ++NextSymIdx;
6159 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006160
Rafael Espindola80291272014-10-08 15:28:58 +00006161 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006162 uint64_t End = containsNextSym ? NextSym : SectSize;
Owen Andersond9243c42011-10-17 21:37:35 +00006163 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006164
6165 symbolTableWorked = true;
Rafael Espindolabd604f22014-11-07 00:52:15 +00006166
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006167 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6168 bool isThumb =
6169 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
6170
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006171 outs() << SymName << ":\n";
6172 DILineInfo lastLine;
6173 for (uint64_t Index = Start; Index < End; Index += Size) {
6174 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +00006175
Kevin Enderbybf246f52014-09-24 23:08:22 +00006176 uint64_t PC = SectAddress + Index;
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006177 if (!NoLeadingAddr) {
6178 if (FullLeadingAddr) {
6179 if (MachOOF->is64Bit())
6180 outs() << format("%016" PRIx64, PC);
6181 else
6182 outs() << format("%08" PRIx64, PC);
6183 } else {
6184 outs() << format("%8" PRIx64 ":", PC);
6185 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006186 }
6187 if (!NoShowRawInsn)
6188 outs() << "\t";
Kevin Enderby273ae012013-06-06 17:20:50 +00006189
6190 // Check the data in code table here to see if this is data not an
6191 // instruction to be disassembled.
6192 DiceTable Dice;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006193 Dice.push_back(std::make_pair(PC, DiceRef()));
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006194 dice_table_iterator DTI =
6195 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6196 compareDiceTableEntries);
6197 if (DTI != Dices.end()) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006198 uint16_t Length;
6199 DTI->second.getLength(Length);
Kevin Enderby273ae012013-06-06 17:20:50 +00006200 uint16_t Kind;
6201 DTI->second.getKind(Kind);
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00006202 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006203 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6204 (PC == (DTI->first + Length - 1)) && (Length & 1))
6205 Size++;
Kevin Enderby273ae012013-06-06 17:20:50 +00006206 continue;
6207 }
6208
Kevin Enderbybf246f52014-09-24 23:08:22 +00006209 SmallVector<char, 64> AnnotationsBytes;
6210 raw_svector_ostream Annotations(AnnotationsBytes);
6211
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006212 bool gotInst;
6213 if (isThumb)
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006214 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006215 PC, DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006216 else
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006217 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
Kevin Enderbybf246f52014-09-24 23:08:22 +00006218 DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006219 if (gotInst) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006220 if (!NoShowRawInsn) {
Craig Topper0013be12015-09-21 05:32:41 +00006221 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006222 }
6223 formatted_raw_ostream FormattedOS(outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006224 StringRef AnnotationsStr = Annotations.str();
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006225 if (isThumb)
Akira Hatanakab46d0232015-03-27 20:36:02 +00006226 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006227 else
Akira Hatanaka1d079942015-03-28 20:44:05 +00006228 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006229 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
Owen Andersond9243c42011-10-17 21:37:35 +00006230
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006231 // Print debug info.
6232 if (diContext) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006233 DILineInfo dli = diContext->getLineInfoForAddress(PC);
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006234 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +00006235 if (dli != lastLine && dli.Line != 0)
6236 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6237 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006238 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006239 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006240 outs() << "\n";
6241 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006242 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006243 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006244 outs() << format("\t.byte 0x%02x #bad opcode\n",
6245 *(Bytes.data() + Index) & 0xff);
6246 Size = 1; // skip exactly one illegible byte and move on.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006247 } else if (Arch == Triple::aarch64) {
6248 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6249 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6250 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6251 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6252 outs() << format("\t.long\t0x%08x\n", opcode);
6253 Size = 4;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006254 } else {
6255 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6256 if (Size == 0)
6257 Size = 1; // skip illegible bytes
6258 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006259 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006260 }
6261 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006262 if (!symbolTableWorked) {
Rafael Espindola80291272014-10-08 15:28:58 +00006263 // Reading the symbol table didn't work, disassemble the whole section.
6264 uint64_t SectAddress = Sections[SectIdx].getAddress();
6265 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbybadd1002012-05-18 00:13:56 +00006266 uint64_t InstSize;
6267 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +00006268 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +00006269
Kevin Enderbybf246f52014-09-24 23:08:22 +00006270 uint64_t PC = SectAddress + Index;
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006271 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6272 DebugOut, nulls())) {
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006273 if (!NoLeadingAddr) {
6274 if (FullLeadingAddr) {
6275 if (MachOOF->is64Bit())
6276 outs() << format("%016" PRIx64, PC);
6277 else
6278 outs() << format("%08" PRIx64, PC);
6279 } else {
6280 outs() << format("%8" PRIx64 ":", PC);
6281 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006282 }
6283 if (!NoShowRawInsn) {
6284 outs() << "\t";
Craig Topper0013be12015-09-21 05:32:41 +00006285 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006286 }
Akira Hatanaka1d079942015-03-28 20:44:05 +00006287 IP->printInst(&Inst, outs(), "", *STI);
Bill Wendling4e68e062012-07-19 00:17:40 +00006288 outs() << "\n";
6289 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006290 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006291 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006292 outs() << format("\t.byte 0x%02x #bad opcode\n",
6293 *(Bytes.data() + Index) & 0xff);
6294 InstSize = 1; // skip exactly one illegible byte and move on.
6295 } else {
6296 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6297 if (InstSize == 0)
6298 InstSize = 1; // skip illegible bytes
6299 }
Bill Wendling4e68e062012-07-19 00:17:40 +00006300 }
Kevin Enderbybadd1002012-05-18 00:13:56 +00006301 }
6302 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006303 // The TripleName's need to be reset if we are called again for a different
6304 // archtecture.
6305 TripleName = "";
6306 ThumbTripleName = "";
6307
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006308 if (SymbolizerInfo.method != nullptr)
6309 free(SymbolizerInfo.method);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006310 if (SymbolizerInfo.demangled_name != nullptr)
6311 free(SymbolizerInfo.demangled_name);
Kevin Enderby078be602014-10-23 19:53:12 +00006312 if (SymbolizerInfo.bindtable != nullptr)
6313 delete SymbolizerInfo.bindtable;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006314 if (ThumbSymbolizerInfo.method != nullptr)
6315 free(ThumbSymbolizerInfo.method);
6316 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6317 free(ThumbSymbolizerInfo.demangled_name);
6318 if (ThumbSymbolizerInfo.bindtable != nullptr)
6319 delete ThumbSymbolizerInfo.bindtable;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006320 }
6321}
Tim Northover4bd286a2014-08-01 13:07:19 +00006322
Tim Northover39c70bb2014-08-12 11:52:59 +00006323//===----------------------------------------------------------------------===//
6324// __compact_unwind section dumping
6325//===----------------------------------------------------------------------===//
6326
Tim Northover4bd286a2014-08-01 13:07:19 +00006327namespace {
Tim Northover39c70bb2014-08-12 11:52:59 +00006328
6329template <typename T> static uint64_t readNext(const char *&Buf) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006330 using llvm::support::little;
6331 using llvm::support::unaligned;
Tim Northover39c70bb2014-08-12 11:52:59 +00006332
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006333 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6334 Buf += sizeof(T);
6335 return Val;
6336}
Tim Northover39c70bb2014-08-12 11:52:59 +00006337
Tim Northover4bd286a2014-08-01 13:07:19 +00006338struct CompactUnwindEntry {
6339 uint32_t OffsetInSection;
6340
6341 uint64_t FunctionAddr;
6342 uint32_t Length;
6343 uint32_t CompactEncoding;
6344 uint64_t PersonalityAddr;
6345 uint64_t LSDAAddr;
6346
6347 RelocationRef FunctionReloc;
6348 RelocationRef PersonalityReloc;
6349 RelocationRef LSDAReloc;
6350
6351 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006352 : OffsetInSection(Offset) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006353 if (Is64)
6354 read<uint64_t>(Contents.data() + Offset);
6355 else
6356 read<uint32_t>(Contents.data() + Offset);
6357 }
6358
6359private:
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006360 template <typename UIntPtr> void read(const char *Buf) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006361 FunctionAddr = readNext<UIntPtr>(Buf);
6362 Length = readNext<uint32_t>(Buf);
6363 CompactEncoding = readNext<uint32_t>(Buf);
6364 PersonalityAddr = readNext<UIntPtr>(Buf);
6365 LSDAAddr = readNext<UIntPtr>(Buf);
6366 }
6367};
6368}
6369
6370/// Given a relocation from __compact_unwind, consisting of the RelocationRef
6371/// and data being relocated, determine the best base Name and Addend to use for
6372/// display purposes.
6373///
6374/// 1. An Extern relocation will directly reference a symbol (and the data is
6375/// then already an addend), so use that.
6376/// 2. Otherwise the data is an offset in the object file's layout; try to find
6377// a symbol before it in the same section, and use the offset from there.
6378/// 3. Finally, if all that fails, fall back to an offset from the start of the
6379/// referenced section.
6380static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6381 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006382 const RelocationRef &Reloc, uint64_t Addr,
Tim Northover4bd286a2014-08-01 13:07:19 +00006383 StringRef &Name, uint64_t &Addend) {
6384 if (Reloc.getSymbol() != Obj->symbol_end()) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006385 ErrorOr<StringRef> NameOrErr = Reloc.getSymbol()->getName();
6386 if (std::error_code EC = NameOrErr.getError())
6387 report_fatal_error(EC.message());
6388 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00006389 Addend = Addr;
6390 return;
6391 }
6392
6393 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
Keno Fischerc780e8e2015-05-21 21:24:32 +00006394 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
Tim Northover4bd286a2014-08-01 13:07:19 +00006395
Rafael Espindola80291272014-10-08 15:28:58 +00006396 uint64_t SectionAddr = RelocSection.getAddress();
Tim Northover4bd286a2014-08-01 13:07:19 +00006397
6398 auto Sym = Symbols.upper_bound(Addr);
6399 if (Sym == Symbols.begin()) {
6400 // The first symbol in the object is after this reference, the best we can
6401 // do is section-relative notation.
6402 RelocSection.getName(Name);
6403 Addend = Addr - SectionAddr;
6404 return;
6405 }
6406
6407 // Go back one so that SymbolAddress <= Addr.
6408 --Sym;
6409
Rafael Espindola8bab8892015-08-07 23:27:14 +00006410 section_iterator SymSection = *Sym->second.getSection();
Tim Northover4bd286a2014-08-01 13:07:19 +00006411 if (RelocSection == *SymSection) {
6412 // There's a valid symbol in the same section before this reference.
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006413 ErrorOr<StringRef> NameOrErr = Sym->second.getName();
6414 if (std::error_code EC = NameOrErr.getError())
6415 report_fatal_error(EC.message());
6416 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00006417 Addend = Addr - Sym->first;
6418 return;
6419 }
6420
6421 // There is a symbol before this reference, but it's in a different
6422 // section. Probably not helpful to mention it, so use the section name.
6423 RelocSection.getName(Name);
6424 Addend = Addr - SectionAddr;
6425}
6426
6427static void printUnwindRelocDest(const MachOObjectFile *Obj,
6428 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006429 const RelocationRef &Reloc, uint64_t Addr) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006430 StringRef Name;
6431 uint64_t Addend;
6432
Rafael Espindola854038e2015-06-26 14:51:16 +00006433 if (!Reloc.getObject())
Tim Northover0b0add52014-09-09 10:45:06 +00006434 return;
6435
Tim Northover4bd286a2014-08-01 13:07:19 +00006436 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6437
6438 outs() << Name;
6439 if (Addend)
Tim Northover63a25622014-08-11 09:14:06 +00006440 outs() << " + " << format("0x%" PRIx64, Addend);
Tim Northover4bd286a2014-08-01 13:07:19 +00006441}
6442
6443static void
6444printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6445 std::map<uint64_t, SymbolRef> &Symbols,
6446 const SectionRef &CompactUnwind) {
6447
6448 assert(Obj->isLittleEndian() &&
6449 "There should not be a big-endian .o with __compact_unwind");
6450
6451 bool Is64 = Obj->is64Bit();
6452 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6453 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6454
6455 StringRef Contents;
6456 CompactUnwind.getContents(Contents);
6457
6458 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6459
6460 // First populate the initial raw offsets, encodings and so on from the entry.
6461 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6462 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
6463 CompactUnwinds.push_back(Entry);
6464 }
6465
6466 // Next we need to look at the relocations to find out what objects are
6467 // actually being referred to.
6468 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00006469 uint64_t RelocAddress = Reloc.getOffset();
Tim Northover4bd286a2014-08-01 13:07:19 +00006470
6471 uint32_t EntryIdx = RelocAddress / EntrySize;
6472 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
6473 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
6474
6475 if (OffsetInEntry == 0)
6476 Entry.FunctionReloc = Reloc;
6477 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
6478 Entry.PersonalityReloc = Reloc;
6479 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
6480 Entry.LSDAReloc = Reloc;
6481 else
6482 llvm_unreachable("Unexpected relocation in __compact_unwind section");
6483 }
6484
6485 // Finally, we're ready to print the data we've gathered.
6486 outs() << "Contents of __compact_unwind section:\n";
6487 for (auto &Entry : CompactUnwinds) {
Tim Northover06af2602014-08-08 12:08:51 +00006488 outs() << " Entry at offset "
6489 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00006490
6491 // 1. Start of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006492 outs() << " start: " << format("0x%" PRIx64,
6493 Entry.FunctionAddr) << ' ';
6494 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
Tim Northover4bd286a2014-08-01 13:07:19 +00006495 outs() << '\n';
6496
6497 // 2. Length of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006498 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
6499 << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00006500 // 3. The 32-bit compact encoding.
6501 outs() << " compact encoding: "
Tim Northoverb911bf82014-08-08 12:00:09 +00006502 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00006503
6504 // 4. The personality function, if present.
Rafael Espindola854038e2015-06-26 14:51:16 +00006505 if (Entry.PersonalityReloc.getObject()) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006506 outs() << " personality function: "
Tim Northoverb911bf82014-08-08 12:00:09 +00006507 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00006508 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
6509 Entry.PersonalityAddr);
6510 outs() << '\n';
6511 }
6512
6513 // 5. This entry's language-specific data area.
Rafael Espindola854038e2015-06-26 14:51:16 +00006514 if (Entry.LSDAReloc.getObject()) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006515 outs() << " LSDA: " << format("0x%" PRIx64,
6516 Entry.LSDAAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00006517 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
6518 outs() << '\n';
6519 }
6520 }
6521}
6522
Tim Northover39c70bb2014-08-12 11:52:59 +00006523//===----------------------------------------------------------------------===//
6524// __unwind_info section dumping
6525//===----------------------------------------------------------------------===//
6526
6527static void printRegularSecondLevelUnwindPage(const char *PageStart) {
6528 const char *Pos = PageStart;
6529 uint32_t Kind = readNext<uint32_t>(Pos);
6530 (void)Kind;
6531 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
6532
6533 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6534 uint16_t NumEntries = readNext<uint16_t>(Pos);
6535
6536 Pos = PageStart + EntriesStart;
6537 for (unsigned i = 0; i < NumEntries; ++i) {
6538 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6539 uint32_t Encoding = readNext<uint32_t>(Pos);
6540
6541 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006542 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6543 << ", "
6544 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006545 }
6546}
6547
6548static void printCompressedSecondLevelUnwindPage(
6549 const char *PageStart, uint32_t FunctionBase,
6550 const SmallVectorImpl<uint32_t> &CommonEncodings) {
6551 const char *Pos = PageStart;
6552 uint32_t Kind = readNext<uint32_t>(Pos);
6553 (void)Kind;
6554 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
6555
6556 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6557 uint16_t NumEntries = readNext<uint16_t>(Pos);
6558
6559 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
6560 readNext<uint16_t>(Pos);
Aaron Ballman80930af2014-08-14 13:53:19 +00006561 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
6562 PageStart + EncodingsStart);
Tim Northover39c70bb2014-08-12 11:52:59 +00006563
6564 Pos = PageStart + EntriesStart;
6565 for (unsigned i = 0; i < NumEntries; ++i) {
6566 uint32_t Entry = readNext<uint32_t>(Pos);
6567 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
6568 uint32_t EncodingIdx = Entry >> 24;
6569
6570 uint32_t Encoding;
6571 if (EncodingIdx < CommonEncodings.size())
6572 Encoding = CommonEncodings[EncodingIdx];
6573 else
6574 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
6575
6576 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006577 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6578 << ", "
6579 << "encoding[" << EncodingIdx
6580 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006581 }
6582}
6583
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006584static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
6585 std::map<uint64_t, SymbolRef> &Symbols,
6586 const SectionRef &UnwindInfo) {
Tim Northover39c70bb2014-08-12 11:52:59 +00006587
6588 assert(Obj->isLittleEndian() &&
6589 "There should not be a big-endian .o with __unwind_info");
6590
6591 outs() << "Contents of __unwind_info section:\n";
6592
6593 StringRef Contents;
6594 UnwindInfo.getContents(Contents);
6595 const char *Pos = Contents.data();
6596
6597 //===----------------------------------
6598 // Section header
6599 //===----------------------------------
6600
6601 uint32_t Version = readNext<uint32_t>(Pos);
6602 outs() << " Version: "
6603 << format("0x%" PRIx32, Version) << '\n';
6604 assert(Version == 1 && "only understand version 1");
6605
6606 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
6607 outs() << " Common encodings array section offset: "
6608 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
6609 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
6610 outs() << " Number of common encodings in array: "
6611 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
6612
6613 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
6614 outs() << " Personality function array section offset: "
6615 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
6616 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
6617 outs() << " Number of personality functions in array: "
6618 << format("0x%" PRIx32, NumPersonalities) << '\n';
6619
6620 uint32_t IndicesStart = readNext<uint32_t>(Pos);
6621 outs() << " Index array section offset: "
6622 << format("0x%" PRIx32, IndicesStart) << '\n';
6623 uint32_t NumIndices = readNext<uint32_t>(Pos);
6624 outs() << " Number of indices in array: "
6625 << format("0x%" PRIx32, NumIndices) << '\n';
6626
6627 //===----------------------------------
6628 // A shared list of common encodings
6629 //===----------------------------------
6630
6631 // These occupy indices in the range [0, N] whenever an encoding is referenced
6632 // from a compressed 2nd level index table. In practice the linker only
6633 // creates ~128 of these, so that indices are available to embed encodings in
6634 // the 2nd level index.
6635
6636 SmallVector<uint32_t, 64> CommonEncodings;
6637 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
6638 Pos = Contents.data() + CommonEncodingsStart;
6639 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
6640 uint32_t Encoding = readNext<uint32_t>(Pos);
6641 CommonEncodings.push_back(Encoding);
6642
6643 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
6644 << '\n';
6645 }
6646
Tim Northover39c70bb2014-08-12 11:52:59 +00006647 //===----------------------------------
6648 // Personality functions used in this executable
6649 //===----------------------------------
6650
6651 // There should be only a handful of these (one per source language,
6652 // roughly). Particularly since they only get 2 bits in the compact encoding.
6653
6654 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
6655 Pos = Contents.data() + PersonalitiesStart;
6656 for (unsigned i = 0; i < NumPersonalities; ++i) {
6657 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
6658 outs() << " personality[" << i + 1
6659 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
6660 }
6661
6662 //===----------------------------------
6663 // The level 1 index entries
6664 //===----------------------------------
6665
6666 // These specify an approximate place to start searching for the more detailed
6667 // information, sorted by PC.
6668
6669 struct IndexEntry {
6670 uint32_t FunctionOffset;
6671 uint32_t SecondLevelPageStart;
6672 uint32_t LSDAStart;
6673 };
6674
6675 SmallVector<IndexEntry, 4> IndexEntries;
6676
6677 outs() << " Top level indices: (count = " << NumIndices << ")\n";
6678 Pos = Contents.data() + IndicesStart;
6679 for (unsigned i = 0; i < NumIndices; ++i) {
6680 IndexEntry Entry;
6681
6682 Entry.FunctionOffset = readNext<uint32_t>(Pos);
6683 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
6684 Entry.LSDAStart = readNext<uint32_t>(Pos);
6685 IndexEntries.push_back(Entry);
6686
6687 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006688 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
6689 << ", "
Tim Northover39c70bb2014-08-12 11:52:59 +00006690 << "2nd level page offset="
6691 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006692 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006693 }
6694
Tim Northover39c70bb2014-08-12 11:52:59 +00006695 //===----------------------------------
6696 // Next come the LSDA tables
6697 //===----------------------------------
6698
6699 // The LSDA layout is rather implicit: it's a contiguous array of entries from
6700 // the first top-level index's LSDAOffset to the last (sentinel).
6701
6702 outs() << " LSDA descriptors:\n";
6703 Pos = Contents.data() + IndexEntries[0].LSDAStart;
6704 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
6705 (2 * sizeof(uint32_t));
6706 for (int i = 0; i < NumLSDAs; ++i) {
6707 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6708 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
6709 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006710 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6711 << ", "
6712 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006713 }
6714
6715 //===----------------------------------
6716 // Finally, the 2nd level indices
6717 //===----------------------------------
6718
6719 // Generally these are 4K in size, and have 2 possible forms:
6720 // + Regular stores up to 511 entries with disparate encodings
6721 // + Compressed stores up to 1021 entries if few enough compact encoding
6722 // values are used.
6723 outs() << " Second level indices:\n";
6724 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
6725 // The final sentinel top-level index has no associated 2nd level page
6726 if (IndexEntries[i].SecondLevelPageStart == 0)
6727 break;
6728
6729 outs() << " Second level index[" << i << "]: "
6730 << "offset in section="
6731 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
6732 << ", "
6733 << "base function offset="
6734 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
6735
6736 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
Aaron Ballman80930af2014-08-14 13:53:19 +00006737 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00006738 if (Kind == 2)
6739 printRegularSecondLevelUnwindPage(Pos);
6740 else if (Kind == 3)
6741 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
6742 CommonEncodings);
6743 else
6744 llvm_unreachable("Do not know how to print this kind of 2nd level page");
Tim Northover39c70bb2014-08-12 11:52:59 +00006745 }
6746}
6747
Tim Northover4bd286a2014-08-01 13:07:19 +00006748void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
6749 std::map<uint64_t, SymbolRef> Symbols;
6750 for (const SymbolRef &SymRef : Obj->symbols()) {
6751 // Discard any undefined or absolute symbols. They're not going to take part
6752 // in the convenience lookup for unwind info and just take up resources.
Rafael Espindola8bab8892015-08-07 23:27:14 +00006753 section_iterator Section = *SymRef.getSection();
Tim Northover4bd286a2014-08-01 13:07:19 +00006754 if (Section == Obj->section_end())
6755 continue;
6756
Rafael Espindoladea00162015-07-03 17:44:18 +00006757 uint64_t Addr = SymRef.getValue();
Tim Northover4bd286a2014-08-01 13:07:19 +00006758 Symbols.insert(std::make_pair(Addr, SymRef));
6759 }
6760
6761 for (const SectionRef &Section : Obj->sections()) {
6762 StringRef SectName;
6763 Section.getName(SectName);
6764 if (SectName == "__compact_unwind")
6765 printMachOCompactUnwindSection(Obj, Symbols, Section);
6766 else if (SectName == "__unwind_info")
Tim Northover39c70bb2014-08-12 11:52:59 +00006767 printMachOUnwindInfoSection(Obj, Symbols, Section);
Tim Northover4bd286a2014-08-01 13:07:19 +00006768 else if (SectName == "__eh_frame")
6769 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00006770 }
6771}
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006772
6773static void PrintMachHeader(uint32_t magic, uint32_t cputype,
6774 uint32_t cpusubtype, uint32_t filetype,
6775 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
6776 bool verbose) {
6777 outs() << "Mach header\n";
6778 outs() << " magic cputype cpusubtype caps filetype ncmds "
6779 "sizeofcmds flags\n";
6780 if (verbose) {
6781 if (magic == MachO::MH_MAGIC)
6782 outs() << " MH_MAGIC";
6783 else if (magic == MachO::MH_MAGIC_64)
6784 outs() << "MH_MAGIC_64";
6785 else
6786 outs() << format(" 0x%08" PRIx32, magic);
6787 switch (cputype) {
6788 case MachO::CPU_TYPE_I386:
6789 outs() << " I386";
6790 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6791 case MachO::CPU_SUBTYPE_I386_ALL:
6792 outs() << " ALL";
6793 break;
6794 default:
6795 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6796 break;
6797 }
6798 break;
6799 case MachO::CPU_TYPE_X86_64:
6800 outs() << " X86_64";
Kevin Enderby131d1772015-01-09 19:22:37 +00006801 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6802 case MachO::CPU_SUBTYPE_X86_64_ALL:
6803 outs() << " ALL";
6804 break;
6805 case MachO::CPU_SUBTYPE_X86_64_H:
6806 outs() << " Haswell";
6807 break;
6808 default:
6809 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6810 break;
6811 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006812 break;
6813 case MachO::CPU_TYPE_ARM:
6814 outs() << " ARM";
6815 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6816 case MachO::CPU_SUBTYPE_ARM_ALL:
6817 outs() << " ALL";
6818 break;
6819 case MachO::CPU_SUBTYPE_ARM_V4T:
6820 outs() << " V4T";
6821 break;
6822 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6823 outs() << " V5TEJ";
6824 break;
6825 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6826 outs() << " XSCALE";
6827 break;
6828 case MachO::CPU_SUBTYPE_ARM_V6:
6829 outs() << " V6";
6830 break;
6831 case MachO::CPU_SUBTYPE_ARM_V6M:
6832 outs() << " V6M";
6833 break;
6834 case MachO::CPU_SUBTYPE_ARM_V7:
6835 outs() << " V7";
6836 break;
6837 case MachO::CPU_SUBTYPE_ARM_V7EM:
6838 outs() << " V7EM";
6839 break;
6840 case MachO::CPU_SUBTYPE_ARM_V7K:
6841 outs() << " V7K";
6842 break;
6843 case MachO::CPU_SUBTYPE_ARM_V7M:
6844 outs() << " V7M";
6845 break;
6846 case MachO::CPU_SUBTYPE_ARM_V7S:
6847 outs() << " V7S";
6848 break;
6849 default:
6850 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6851 break;
6852 }
6853 break;
6854 case MachO::CPU_TYPE_ARM64:
6855 outs() << " ARM64";
6856 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6857 case MachO::CPU_SUBTYPE_ARM64_ALL:
6858 outs() << " ALL";
6859 break;
6860 default:
6861 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6862 break;
6863 }
6864 break;
6865 case MachO::CPU_TYPE_POWERPC:
6866 outs() << " PPC";
6867 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6868 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6869 outs() << " ALL";
6870 break;
6871 default:
6872 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6873 break;
6874 }
6875 break;
6876 case MachO::CPU_TYPE_POWERPC64:
6877 outs() << " PPC64";
6878 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6879 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6880 outs() << " ALL";
6881 break;
6882 default:
6883 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6884 break;
6885 }
6886 break;
6887 }
6888 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00006889 outs() << " LIB64";
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006890 } else {
6891 outs() << format(" 0x%02" PRIx32,
6892 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6893 }
6894 switch (filetype) {
6895 case MachO::MH_OBJECT:
6896 outs() << " OBJECT";
6897 break;
6898 case MachO::MH_EXECUTE:
6899 outs() << " EXECUTE";
6900 break;
6901 case MachO::MH_FVMLIB:
6902 outs() << " FVMLIB";
6903 break;
6904 case MachO::MH_CORE:
6905 outs() << " CORE";
6906 break;
6907 case MachO::MH_PRELOAD:
6908 outs() << " PRELOAD";
6909 break;
6910 case MachO::MH_DYLIB:
6911 outs() << " DYLIB";
6912 break;
6913 case MachO::MH_DYLIB_STUB:
6914 outs() << " DYLIB_STUB";
6915 break;
6916 case MachO::MH_DYLINKER:
6917 outs() << " DYLINKER";
6918 break;
6919 case MachO::MH_BUNDLE:
6920 outs() << " BUNDLE";
6921 break;
6922 case MachO::MH_DSYM:
6923 outs() << " DSYM";
6924 break;
6925 case MachO::MH_KEXT_BUNDLE:
6926 outs() << " KEXTBUNDLE";
6927 break;
6928 default:
6929 outs() << format(" %10u", filetype);
6930 break;
6931 }
6932 outs() << format(" %5u", ncmds);
6933 outs() << format(" %10u", sizeofcmds);
6934 uint32_t f = flags;
6935 if (f & MachO::MH_NOUNDEFS) {
6936 outs() << " NOUNDEFS";
6937 f &= ~MachO::MH_NOUNDEFS;
6938 }
6939 if (f & MachO::MH_INCRLINK) {
6940 outs() << " INCRLINK";
6941 f &= ~MachO::MH_INCRLINK;
6942 }
6943 if (f & MachO::MH_DYLDLINK) {
6944 outs() << " DYLDLINK";
6945 f &= ~MachO::MH_DYLDLINK;
6946 }
6947 if (f & MachO::MH_BINDATLOAD) {
6948 outs() << " BINDATLOAD";
6949 f &= ~MachO::MH_BINDATLOAD;
6950 }
6951 if (f & MachO::MH_PREBOUND) {
6952 outs() << " PREBOUND";
6953 f &= ~MachO::MH_PREBOUND;
6954 }
6955 if (f & MachO::MH_SPLIT_SEGS) {
6956 outs() << " SPLIT_SEGS";
6957 f &= ~MachO::MH_SPLIT_SEGS;
6958 }
6959 if (f & MachO::MH_LAZY_INIT) {
6960 outs() << " LAZY_INIT";
6961 f &= ~MachO::MH_LAZY_INIT;
6962 }
6963 if (f & MachO::MH_TWOLEVEL) {
6964 outs() << " TWOLEVEL";
6965 f &= ~MachO::MH_TWOLEVEL;
6966 }
6967 if (f & MachO::MH_FORCE_FLAT) {
6968 outs() << " FORCE_FLAT";
6969 f &= ~MachO::MH_FORCE_FLAT;
6970 }
6971 if (f & MachO::MH_NOMULTIDEFS) {
6972 outs() << " NOMULTIDEFS";
6973 f &= ~MachO::MH_NOMULTIDEFS;
6974 }
6975 if (f & MachO::MH_NOFIXPREBINDING) {
6976 outs() << " NOFIXPREBINDING";
6977 f &= ~MachO::MH_NOFIXPREBINDING;
6978 }
6979 if (f & MachO::MH_PREBINDABLE) {
6980 outs() << " PREBINDABLE";
6981 f &= ~MachO::MH_PREBINDABLE;
6982 }
6983 if (f & MachO::MH_ALLMODSBOUND) {
6984 outs() << " ALLMODSBOUND";
6985 f &= ~MachO::MH_ALLMODSBOUND;
6986 }
6987 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
6988 outs() << " SUBSECTIONS_VIA_SYMBOLS";
6989 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
6990 }
6991 if (f & MachO::MH_CANONICAL) {
6992 outs() << " CANONICAL";
6993 f &= ~MachO::MH_CANONICAL;
6994 }
6995 if (f & MachO::MH_WEAK_DEFINES) {
6996 outs() << " WEAK_DEFINES";
6997 f &= ~MachO::MH_WEAK_DEFINES;
6998 }
6999 if (f & MachO::MH_BINDS_TO_WEAK) {
7000 outs() << " BINDS_TO_WEAK";
7001 f &= ~MachO::MH_BINDS_TO_WEAK;
7002 }
7003 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7004 outs() << " ALLOW_STACK_EXECUTION";
7005 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7006 }
7007 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7008 outs() << " DEAD_STRIPPABLE_DYLIB";
7009 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7010 }
7011 if (f & MachO::MH_PIE) {
7012 outs() << " PIE";
7013 f &= ~MachO::MH_PIE;
7014 }
7015 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7016 outs() << " NO_REEXPORTED_DYLIBS";
7017 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7018 }
7019 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7020 outs() << " MH_HAS_TLV_DESCRIPTORS";
7021 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7022 }
7023 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7024 outs() << " MH_NO_HEAP_EXECUTION";
7025 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7026 }
7027 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7028 outs() << " APP_EXTENSION_SAFE";
7029 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7030 }
7031 if (f != 0 || flags == 0)
7032 outs() << format(" 0x%08" PRIx32, f);
7033 } else {
7034 outs() << format(" 0x%08" PRIx32, magic);
7035 outs() << format(" %7d", cputype);
7036 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7037 outs() << format(" 0x%02" PRIx32,
7038 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7039 outs() << format(" %10u", filetype);
7040 outs() << format(" %5u", ncmds);
7041 outs() << format(" %10u", sizeofcmds);
7042 outs() << format(" 0x%08" PRIx32, flags);
7043 }
7044 outs() << "\n";
7045}
7046
Kevin Enderby956366c2014-08-29 22:30:52 +00007047static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7048 StringRef SegName, uint64_t vmaddr,
7049 uint64_t vmsize, uint64_t fileoff,
7050 uint64_t filesize, uint32_t maxprot,
7051 uint32_t initprot, uint32_t nsects,
7052 uint32_t flags, uint32_t object_size,
7053 bool verbose) {
7054 uint64_t expected_cmdsize;
7055 if (cmd == MachO::LC_SEGMENT) {
7056 outs() << " cmd LC_SEGMENT\n";
7057 expected_cmdsize = nsects;
7058 expected_cmdsize *= sizeof(struct MachO::section);
7059 expected_cmdsize += sizeof(struct MachO::segment_command);
7060 } else {
7061 outs() << " cmd LC_SEGMENT_64\n";
7062 expected_cmdsize = nsects;
7063 expected_cmdsize *= sizeof(struct MachO::section_64);
7064 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7065 }
7066 outs() << " cmdsize " << cmdsize;
7067 if (cmdsize != expected_cmdsize)
7068 outs() << " Inconsistent size\n";
7069 else
7070 outs() << "\n";
7071 outs() << " segname " << SegName << "\n";
7072 if (cmd == MachO::LC_SEGMENT_64) {
7073 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7074 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7075 } else {
Kevin Enderbyadb7c432014-12-16 18:58:11 +00007076 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7077 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007078 }
7079 outs() << " fileoff " << fileoff;
7080 if (fileoff > object_size)
7081 outs() << " (past end of file)\n";
7082 else
7083 outs() << "\n";
7084 outs() << " filesize " << filesize;
7085 if (fileoff + filesize > object_size)
7086 outs() << " (past end of file)\n";
7087 else
7088 outs() << "\n";
7089 if (verbose) {
7090 if ((maxprot &
7091 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7092 MachO::VM_PROT_EXECUTE)) != 0)
7093 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7094 else {
Davide Italiano37ff06a2015-09-02 16:53:25 +00007095 outs() << " maxprot ";
7096 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
7097 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7098 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00007099 }
7100 if ((initprot &
7101 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7102 MachO::VM_PROT_EXECUTE)) != 0)
7103 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7104 else {
Davide Italiano37ff06a2015-09-02 16:53:25 +00007105 outs() << " initprot ";
7106 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
7107 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7108 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00007109 }
7110 } else {
7111 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7112 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7113 }
7114 outs() << " nsects " << nsects << "\n";
7115 if (verbose) {
7116 outs() << " flags";
7117 if (flags == 0)
7118 outs() << " (none)\n";
7119 else {
7120 if (flags & MachO::SG_HIGHVM) {
7121 outs() << " HIGHVM";
7122 flags &= ~MachO::SG_HIGHVM;
7123 }
7124 if (flags & MachO::SG_FVMLIB) {
7125 outs() << " FVMLIB";
7126 flags &= ~MachO::SG_FVMLIB;
7127 }
7128 if (flags & MachO::SG_NORELOC) {
7129 outs() << " NORELOC";
7130 flags &= ~MachO::SG_NORELOC;
7131 }
7132 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7133 outs() << " PROTECTED_VERSION_1";
7134 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7135 }
7136 if (flags)
7137 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7138 else
7139 outs() << "\n";
7140 }
7141 } else {
7142 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7143 }
7144}
7145
7146static void PrintSection(const char *sectname, const char *segname,
7147 uint64_t addr, uint64_t size, uint32_t offset,
7148 uint32_t align, uint32_t reloff, uint32_t nreloc,
7149 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7150 uint32_t cmd, const char *sg_segname,
7151 uint32_t filetype, uint32_t object_size,
7152 bool verbose) {
7153 outs() << "Section\n";
7154 outs() << " sectname " << format("%.16s\n", sectname);
7155 outs() << " segname " << format("%.16s", segname);
7156 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7157 outs() << " (does not match segment)\n";
7158 else
7159 outs() << "\n";
7160 if (cmd == MachO::LC_SEGMENT_64) {
7161 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7162 outs() << " size " << format("0x%016" PRIx64, size);
7163 } else {
Kevin Enderby75594b62014-12-16 21:00:25 +00007164 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7165 outs() << " size " << format("0x%08" PRIx64, size);
Kevin Enderby956366c2014-08-29 22:30:52 +00007166 }
7167 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7168 outs() << " (past end of file)\n";
7169 else
7170 outs() << "\n";
7171 outs() << " offset " << offset;
7172 if (offset > object_size)
7173 outs() << " (past end of file)\n";
7174 else
7175 outs() << "\n";
7176 uint32_t align_shifted = 1 << align;
7177 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7178 outs() << " reloff " << reloff;
7179 if (reloff > object_size)
7180 outs() << " (past end of file)\n";
7181 else
7182 outs() << "\n";
7183 outs() << " nreloc " << nreloc;
7184 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7185 outs() << " (past end of file)\n";
7186 else
7187 outs() << "\n";
7188 uint32_t section_type = flags & MachO::SECTION_TYPE;
7189 if (verbose) {
7190 outs() << " type";
7191 if (section_type == MachO::S_REGULAR)
7192 outs() << " S_REGULAR\n";
7193 else if (section_type == MachO::S_ZEROFILL)
7194 outs() << " S_ZEROFILL\n";
7195 else if (section_type == MachO::S_CSTRING_LITERALS)
7196 outs() << " S_CSTRING_LITERALS\n";
7197 else if (section_type == MachO::S_4BYTE_LITERALS)
7198 outs() << " S_4BYTE_LITERALS\n";
7199 else if (section_type == MachO::S_8BYTE_LITERALS)
7200 outs() << " S_8BYTE_LITERALS\n";
7201 else if (section_type == MachO::S_16BYTE_LITERALS)
7202 outs() << " S_16BYTE_LITERALS\n";
7203 else if (section_type == MachO::S_LITERAL_POINTERS)
7204 outs() << " S_LITERAL_POINTERS\n";
7205 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7206 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7207 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7208 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7209 else if (section_type == MachO::S_SYMBOL_STUBS)
7210 outs() << " S_SYMBOL_STUBS\n";
7211 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7212 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7213 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7214 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7215 else if (section_type == MachO::S_COALESCED)
7216 outs() << " S_COALESCED\n";
7217 else if (section_type == MachO::S_INTERPOSING)
7218 outs() << " S_INTERPOSING\n";
7219 else if (section_type == MachO::S_DTRACE_DOF)
7220 outs() << " S_DTRACE_DOF\n";
7221 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7222 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7223 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7224 outs() << " S_THREAD_LOCAL_REGULAR\n";
7225 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7226 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7227 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7228 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7229 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7230 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7231 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7232 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7233 else
7234 outs() << format("0x%08" PRIx32, section_type) << "\n";
7235 outs() << "attributes";
7236 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7237 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7238 outs() << " PURE_INSTRUCTIONS";
7239 if (section_attributes & MachO::S_ATTR_NO_TOC)
7240 outs() << " NO_TOC";
7241 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7242 outs() << " STRIP_STATIC_SYMS";
7243 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7244 outs() << " NO_DEAD_STRIP";
7245 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7246 outs() << " LIVE_SUPPORT";
7247 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7248 outs() << " SELF_MODIFYING_CODE";
7249 if (section_attributes & MachO::S_ATTR_DEBUG)
7250 outs() << " DEBUG";
7251 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7252 outs() << " SOME_INSTRUCTIONS";
7253 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7254 outs() << " EXT_RELOC";
7255 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7256 outs() << " LOC_RELOC";
7257 if (section_attributes == 0)
7258 outs() << " (none)";
7259 outs() << "\n";
7260 } else
7261 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7262 outs() << " reserved1 " << reserved1;
7263 if (section_type == MachO::S_SYMBOL_STUBS ||
7264 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7265 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7266 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7267 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7268 outs() << " (index into indirect symbol table)\n";
7269 else
7270 outs() << "\n";
7271 outs() << " reserved2 " << reserved2;
7272 if (section_type == MachO::S_SYMBOL_STUBS)
7273 outs() << " (size of stubs)\n";
7274 else
7275 outs() << "\n";
7276}
7277
David Majnemer73cc6ff2014-11-13 19:48:56 +00007278static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
Kevin Enderby956366c2014-08-29 22:30:52 +00007279 uint32_t object_size) {
7280 outs() << " cmd LC_SYMTAB\n";
7281 outs() << " cmdsize " << st.cmdsize;
7282 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7283 outs() << " Incorrect size\n";
7284 else
7285 outs() << "\n";
7286 outs() << " symoff " << st.symoff;
7287 if (st.symoff > object_size)
7288 outs() << " (past end of file)\n";
7289 else
7290 outs() << "\n";
7291 outs() << " nsyms " << st.nsyms;
7292 uint64_t big_size;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007293 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007294 big_size = st.nsyms;
7295 big_size *= sizeof(struct MachO::nlist_64);
7296 big_size += st.symoff;
7297 if (big_size > object_size)
7298 outs() << " (past end of file)\n";
7299 else
7300 outs() << "\n";
7301 } else {
7302 big_size = st.nsyms;
7303 big_size *= sizeof(struct MachO::nlist);
7304 big_size += st.symoff;
7305 if (big_size > object_size)
7306 outs() << " (past end of file)\n";
7307 else
7308 outs() << "\n";
7309 }
7310 outs() << " stroff " << st.stroff;
7311 if (st.stroff > object_size)
7312 outs() << " (past end of file)\n";
7313 else
7314 outs() << "\n";
7315 outs() << " strsize " << st.strsize;
7316 big_size = st.stroff;
7317 big_size += st.strsize;
7318 if (big_size > object_size)
7319 outs() << " (past end of file)\n";
7320 else
7321 outs() << "\n";
7322}
7323
7324static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7325 uint32_t nsyms, uint32_t object_size,
David Majnemer73cc6ff2014-11-13 19:48:56 +00007326 bool Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007327 outs() << " cmd LC_DYSYMTAB\n";
7328 outs() << " cmdsize " << dyst.cmdsize;
7329 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7330 outs() << " Incorrect size\n";
7331 else
7332 outs() << "\n";
7333 outs() << " ilocalsym " << dyst.ilocalsym;
7334 if (dyst.ilocalsym > nsyms)
7335 outs() << " (greater than the number of symbols)\n";
7336 else
7337 outs() << "\n";
7338 outs() << " nlocalsym " << dyst.nlocalsym;
7339 uint64_t big_size;
7340 big_size = dyst.ilocalsym;
7341 big_size += dyst.nlocalsym;
7342 if (big_size > nsyms)
7343 outs() << " (past the end of the symbol table)\n";
7344 else
7345 outs() << "\n";
7346 outs() << " iextdefsym " << dyst.iextdefsym;
7347 if (dyst.iextdefsym > nsyms)
7348 outs() << " (greater than the number of symbols)\n";
7349 else
7350 outs() << "\n";
7351 outs() << " nextdefsym " << dyst.nextdefsym;
7352 big_size = dyst.iextdefsym;
7353 big_size += dyst.nextdefsym;
7354 if (big_size > nsyms)
7355 outs() << " (past the end of the symbol table)\n";
7356 else
7357 outs() << "\n";
7358 outs() << " iundefsym " << dyst.iundefsym;
7359 if (dyst.iundefsym > nsyms)
7360 outs() << " (greater than the number of symbols)\n";
7361 else
7362 outs() << "\n";
7363 outs() << " nundefsym " << dyst.nundefsym;
7364 big_size = dyst.iundefsym;
7365 big_size += dyst.nundefsym;
7366 if (big_size > nsyms)
7367 outs() << " (past the end of the symbol table)\n";
7368 else
7369 outs() << "\n";
7370 outs() << " tocoff " << dyst.tocoff;
7371 if (dyst.tocoff > object_size)
7372 outs() << " (past end of file)\n";
7373 else
7374 outs() << "\n";
7375 outs() << " ntoc " << dyst.ntoc;
7376 big_size = dyst.ntoc;
7377 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7378 big_size += dyst.tocoff;
7379 if (big_size > object_size)
7380 outs() << " (past end of file)\n";
7381 else
7382 outs() << "\n";
7383 outs() << " modtaboff " << dyst.modtaboff;
7384 if (dyst.modtaboff > object_size)
7385 outs() << " (past end of file)\n";
7386 else
7387 outs() << "\n";
7388 outs() << " nmodtab " << dyst.nmodtab;
7389 uint64_t modtabend;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007390 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007391 modtabend = dyst.nmodtab;
7392 modtabend *= sizeof(struct MachO::dylib_module_64);
7393 modtabend += dyst.modtaboff;
7394 } else {
7395 modtabend = dyst.nmodtab;
7396 modtabend *= sizeof(struct MachO::dylib_module);
7397 modtabend += dyst.modtaboff;
7398 }
7399 if (modtabend > object_size)
7400 outs() << " (past end of file)\n";
7401 else
7402 outs() << "\n";
7403 outs() << " extrefsymoff " << dyst.extrefsymoff;
7404 if (dyst.extrefsymoff > object_size)
7405 outs() << " (past end of file)\n";
7406 else
7407 outs() << "\n";
7408 outs() << " nextrefsyms " << dyst.nextrefsyms;
7409 big_size = dyst.nextrefsyms;
7410 big_size *= sizeof(struct MachO::dylib_reference);
7411 big_size += dyst.extrefsymoff;
7412 if (big_size > object_size)
7413 outs() << " (past end of file)\n";
7414 else
7415 outs() << "\n";
7416 outs() << " indirectsymoff " << dyst.indirectsymoff;
7417 if (dyst.indirectsymoff > object_size)
7418 outs() << " (past end of file)\n";
7419 else
7420 outs() << "\n";
7421 outs() << " nindirectsyms " << dyst.nindirectsyms;
7422 big_size = dyst.nindirectsyms;
7423 big_size *= sizeof(uint32_t);
7424 big_size += dyst.indirectsymoff;
7425 if (big_size > object_size)
7426 outs() << " (past end of file)\n";
7427 else
7428 outs() << "\n";
7429 outs() << " extreloff " << dyst.extreloff;
7430 if (dyst.extreloff > object_size)
7431 outs() << " (past end of file)\n";
7432 else
7433 outs() << "\n";
7434 outs() << " nextrel " << dyst.nextrel;
7435 big_size = dyst.nextrel;
7436 big_size *= sizeof(struct MachO::relocation_info);
7437 big_size += dyst.extreloff;
7438 if (big_size > object_size)
7439 outs() << " (past end of file)\n";
7440 else
7441 outs() << "\n";
7442 outs() << " locreloff " << dyst.locreloff;
7443 if (dyst.locreloff > object_size)
7444 outs() << " (past end of file)\n";
7445 else
7446 outs() << "\n";
7447 outs() << " nlocrel " << dyst.nlocrel;
7448 big_size = dyst.nlocrel;
7449 big_size *= sizeof(struct MachO::relocation_info);
7450 big_size += dyst.locreloff;
7451 if (big_size > object_size)
7452 outs() << " (past end of file)\n";
7453 else
7454 outs() << "\n";
7455}
7456
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007457static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
7458 uint32_t object_size) {
7459 if (dc.cmd == MachO::LC_DYLD_INFO)
7460 outs() << " cmd LC_DYLD_INFO\n";
7461 else
7462 outs() << " cmd LC_DYLD_INFO_ONLY\n";
7463 outs() << " cmdsize " << dc.cmdsize;
7464 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
7465 outs() << " Incorrect size\n";
7466 else
7467 outs() << "\n";
7468 outs() << " rebase_off " << dc.rebase_off;
7469 if (dc.rebase_off > object_size)
7470 outs() << " (past end of file)\n";
7471 else
7472 outs() << "\n";
7473 outs() << " rebase_size " << dc.rebase_size;
7474 uint64_t big_size;
7475 big_size = dc.rebase_off;
7476 big_size += dc.rebase_size;
7477 if (big_size > object_size)
7478 outs() << " (past end of file)\n";
7479 else
7480 outs() << "\n";
7481 outs() << " bind_off " << dc.bind_off;
7482 if (dc.bind_off > object_size)
7483 outs() << " (past end of file)\n";
7484 else
7485 outs() << "\n";
7486 outs() << " bind_size " << dc.bind_size;
7487 big_size = dc.bind_off;
7488 big_size += dc.bind_size;
7489 if (big_size > object_size)
7490 outs() << " (past end of file)\n";
7491 else
7492 outs() << "\n";
7493 outs() << " weak_bind_off " << dc.weak_bind_off;
7494 if (dc.weak_bind_off > object_size)
7495 outs() << " (past end of file)\n";
7496 else
7497 outs() << "\n";
7498 outs() << " weak_bind_size " << dc.weak_bind_size;
7499 big_size = dc.weak_bind_off;
7500 big_size += dc.weak_bind_size;
7501 if (big_size > object_size)
7502 outs() << " (past end of file)\n";
7503 else
7504 outs() << "\n";
7505 outs() << " lazy_bind_off " << dc.lazy_bind_off;
7506 if (dc.lazy_bind_off > object_size)
7507 outs() << " (past end of file)\n";
7508 else
7509 outs() << "\n";
7510 outs() << " lazy_bind_size " << dc.lazy_bind_size;
7511 big_size = dc.lazy_bind_off;
7512 big_size += dc.lazy_bind_size;
7513 if (big_size > object_size)
7514 outs() << " (past end of file)\n";
7515 else
7516 outs() << "\n";
7517 outs() << " export_off " << dc.export_off;
7518 if (dc.export_off > object_size)
7519 outs() << " (past end of file)\n";
7520 else
7521 outs() << "\n";
7522 outs() << " export_size " << dc.export_size;
7523 big_size = dc.export_off;
7524 big_size += dc.export_size;
7525 if (big_size > object_size)
7526 outs() << " (past end of file)\n";
7527 else
7528 outs() << "\n";
7529}
7530
7531static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
7532 const char *Ptr) {
7533 if (dyld.cmd == MachO::LC_ID_DYLINKER)
7534 outs() << " cmd LC_ID_DYLINKER\n";
7535 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
7536 outs() << " cmd LC_LOAD_DYLINKER\n";
7537 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
7538 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
7539 else
7540 outs() << " cmd ?(" << dyld.cmd << ")\n";
7541 outs() << " cmdsize " << dyld.cmdsize;
7542 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
7543 outs() << " Incorrect size\n";
7544 else
7545 outs() << "\n";
7546 if (dyld.name >= dyld.cmdsize)
7547 outs() << " name ?(bad offset " << dyld.name << ")\n";
7548 else {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007549 const char *P = (const char *)(Ptr) + dyld.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007550 outs() << " name " << P << " (offset " << dyld.name << ")\n";
7551 }
7552}
7553
7554static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
7555 outs() << " cmd LC_UUID\n";
7556 outs() << " cmdsize " << uuid.cmdsize;
7557 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
7558 outs() << " Incorrect size\n";
7559 else
7560 outs() << "\n";
7561 outs() << " uuid ";
7562 outs() << format("%02" PRIX32, uuid.uuid[0]);
7563 outs() << format("%02" PRIX32, uuid.uuid[1]);
7564 outs() << format("%02" PRIX32, uuid.uuid[2]);
7565 outs() << format("%02" PRIX32, uuid.uuid[3]);
7566 outs() << "-";
7567 outs() << format("%02" PRIX32, uuid.uuid[4]);
7568 outs() << format("%02" PRIX32, uuid.uuid[5]);
7569 outs() << "-";
7570 outs() << format("%02" PRIX32, uuid.uuid[6]);
7571 outs() << format("%02" PRIX32, uuid.uuid[7]);
7572 outs() << "-";
7573 outs() << format("%02" PRIX32, uuid.uuid[8]);
7574 outs() << format("%02" PRIX32, uuid.uuid[9]);
7575 outs() << "-";
7576 outs() << format("%02" PRIX32, uuid.uuid[10]);
7577 outs() << format("%02" PRIX32, uuid.uuid[11]);
7578 outs() << format("%02" PRIX32, uuid.uuid[12]);
7579 outs() << format("%02" PRIX32, uuid.uuid[13]);
7580 outs() << format("%02" PRIX32, uuid.uuid[14]);
7581 outs() << format("%02" PRIX32, uuid.uuid[15]);
7582 outs() << "\n";
7583}
7584
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007585static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00007586 outs() << " cmd LC_RPATH\n";
7587 outs() << " cmdsize " << rpath.cmdsize;
7588 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
7589 outs() << " Incorrect size\n";
7590 else
7591 outs() << "\n";
7592 if (rpath.path >= rpath.cmdsize)
7593 outs() << " path ?(bad offset " << rpath.path << ")\n";
7594 else {
7595 const char *P = (const char *)(Ptr) + rpath.path;
7596 outs() << " path " << P << " (offset " << rpath.path << ")\n";
7597 }
7598}
7599
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007600static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
7601 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
7602 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
7603 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
7604 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
7605 else
7606 outs() << " cmd " << vd.cmd << " (?)\n";
7607 outs() << " cmdsize " << vd.cmdsize;
7608 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
7609 outs() << " Incorrect size\n";
7610 else
7611 outs() << "\n";
Davide Italiano56baef32015-08-26 12:26:11 +00007612 outs() << " version "
7613 << MachOObjectFile::getVersionMinMajor(vd, false) << "."
7614 << MachOObjectFile::getVersionMinMinor(vd, false);
7615 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
7616 if (Update != 0)
7617 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007618 outs() << "\n";
7619 if (vd.sdk == 0)
Kevin Enderby57538292014-12-17 01:01:30 +00007620 outs() << " sdk n/a";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007621 else {
Davide Italiano56baef32015-08-26 12:26:11 +00007622 outs() << " sdk "
7623 << MachOObjectFile::getVersionMinMajor(vd, true) << "."
7624 << MachOObjectFile::getVersionMinMinor(vd, true);
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007625 }
Davide Italiano56baef32015-08-26 12:26:11 +00007626 Update = MachOObjectFile::getVersionMinUpdate(vd, true);
7627 if (Update != 0)
7628 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007629 outs() << "\n";
7630}
7631
7632static void PrintSourceVersionCommand(MachO::source_version_command sd) {
7633 outs() << " cmd LC_SOURCE_VERSION\n";
7634 outs() << " cmdsize " << sd.cmdsize;
7635 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
7636 outs() << " Incorrect size\n";
7637 else
7638 outs() << "\n";
7639 uint64_t a = (sd.version >> 40) & 0xffffff;
7640 uint64_t b = (sd.version >> 30) & 0x3ff;
7641 uint64_t c = (sd.version >> 20) & 0x3ff;
7642 uint64_t d = (sd.version >> 10) & 0x3ff;
7643 uint64_t e = sd.version & 0x3ff;
7644 outs() << " version " << a << "." << b;
7645 if (e != 0)
7646 outs() << "." << c << "." << d << "." << e;
7647 else if (d != 0)
7648 outs() << "." << c << "." << d;
7649 else if (c != 0)
7650 outs() << "." << c;
7651 outs() << "\n";
7652}
7653
7654static void PrintEntryPointCommand(MachO::entry_point_command ep) {
7655 outs() << " cmd LC_MAIN\n";
7656 outs() << " cmdsize " << ep.cmdsize;
7657 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
7658 outs() << " Incorrect size\n";
7659 else
7660 outs() << "\n";
7661 outs() << " entryoff " << ep.entryoff << "\n";
7662 outs() << " stacksize " << ep.stacksize << "\n";
7663}
7664
Kevin Enderby0804f4672014-12-16 23:25:52 +00007665static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
7666 uint32_t object_size) {
7667 outs() << " cmd LC_ENCRYPTION_INFO\n";
7668 outs() << " cmdsize " << ec.cmdsize;
7669 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
7670 outs() << " Incorrect size\n";
7671 else
7672 outs() << "\n";
7673 outs() << " cryptoff " << ec.cryptoff;
7674 if (ec.cryptoff > object_size)
7675 outs() << " (past end of file)\n";
7676 else
7677 outs() << "\n";
7678 outs() << " cryptsize " << ec.cryptsize;
7679 if (ec.cryptsize > object_size)
7680 outs() << " (past end of file)\n";
7681 else
7682 outs() << "\n";
7683 outs() << " cryptid " << ec.cryptid << "\n";
7684}
7685
Kevin Enderby57538292014-12-17 01:01:30 +00007686static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007687 uint32_t object_size) {
Kevin Enderby57538292014-12-17 01:01:30 +00007688 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
7689 outs() << " cmdsize " << ec.cmdsize;
7690 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
7691 outs() << " Incorrect size\n";
7692 else
7693 outs() << "\n";
7694 outs() << " cryptoff " << ec.cryptoff;
7695 if (ec.cryptoff > object_size)
7696 outs() << " (past end of file)\n";
7697 else
7698 outs() << "\n";
7699 outs() << " cryptsize " << ec.cryptsize;
7700 if (ec.cryptsize > object_size)
7701 outs() << " (past end of file)\n";
7702 else
7703 outs() << "\n";
7704 outs() << " cryptid " << ec.cryptid << "\n";
7705 outs() << " pad " << ec.pad << "\n";
7706}
7707
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007708static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
7709 const char *Ptr) {
7710 outs() << " cmd LC_LINKER_OPTION\n";
7711 outs() << " cmdsize " << lo.cmdsize;
7712 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
7713 outs() << " Incorrect size\n";
7714 else
7715 outs() << "\n";
7716 outs() << " count " << lo.count << "\n";
7717 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
7718 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
7719 uint32_t i = 0;
7720 while (left > 0) {
7721 while (*string == '\0' && left > 0) {
7722 string++;
7723 left--;
7724 }
7725 if (left > 0) {
7726 i++;
7727 outs() << " string #" << i << " " << format("%.*s\n", left, string);
David Majnemerd4449ed2014-12-20 08:24:43 +00007728 uint32_t NullPos = StringRef(string, left).find('\0');
7729 uint32_t len = std::min(NullPos, left) + 1;
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007730 string += len;
7731 left -= len;
7732 }
7733 }
7734 if (lo.count != i)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007735 outs() << " count " << lo.count << " does not match number of strings "
7736 << i << "\n";
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007737}
7738
Kevin Enderbyb4b79312014-12-18 19:24:35 +00007739static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
7740 const char *Ptr) {
7741 outs() << " cmd LC_SUB_FRAMEWORK\n";
7742 outs() << " cmdsize " << sub.cmdsize;
7743 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
7744 outs() << " Incorrect size\n";
7745 else
7746 outs() << "\n";
7747 if (sub.umbrella < sub.cmdsize) {
7748 const char *P = Ptr + sub.umbrella;
7749 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
7750 } else {
7751 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
7752 }
7753}
7754
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00007755static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
7756 const char *Ptr) {
7757 outs() << " cmd LC_SUB_UMBRELLA\n";
7758 outs() << " cmdsize " << sub.cmdsize;
7759 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
7760 outs() << " Incorrect size\n";
7761 else
7762 outs() << "\n";
7763 if (sub.sub_umbrella < sub.cmdsize) {
7764 const char *P = Ptr + sub.sub_umbrella;
7765 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
7766 } else {
7767 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
7768 }
7769}
7770
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007771static void PrintSubLibraryCommand(MachO::sub_library_command sub,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007772 const char *Ptr) {
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007773 outs() << " cmd LC_SUB_LIBRARY\n";
7774 outs() << " cmdsize " << sub.cmdsize;
7775 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
7776 outs() << " Incorrect size\n";
7777 else
7778 outs() << "\n";
7779 if (sub.sub_library < sub.cmdsize) {
7780 const char *P = Ptr + sub.sub_library;
7781 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7782 } else {
7783 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7784 }
7785}
7786
Kevin Enderby186eac32014-12-19 21:06:24 +00007787static void PrintSubClientCommand(MachO::sub_client_command sub,
7788 const char *Ptr) {
7789 outs() << " cmd LC_SUB_CLIENT\n";
7790 outs() << " cmdsize " << sub.cmdsize;
7791 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7792 outs() << " Incorrect size\n";
7793 else
7794 outs() << "\n";
7795 if (sub.client < sub.cmdsize) {
7796 const char *P = Ptr + sub.client;
7797 outs() << " client " << P << " (offset " << sub.client << ")\n";
7798 } else {
7799 outs() << " client ?(bad offset " << sub.client << ")\n";
7800 }
7801}
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007802
Kevin Enderby52e4ce42014-12-19 22:25:22 +00007803static void PrintRoutinesCommand(MachO::routines_command r) {
7804 outs() << " cmd LC_ROUTINES\n";
7805 outs() << " cmdsize " << r.cmdsize;
7806 if (r.cmdsize != sizeof(struct MachO::routines_command))
7807 outs() << " Incorrect size\n";
7808 else
7809 outs() << "\n";
7810 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7811 outs() << " init_module " << r.init_module << "\n";
7812 outs() << " reserved1 " << r.reserved1 << "\n";
7813 outs() << " reserved2 " << r.reserved2 << "\n";
7814 outs() << " reserved3 " << r.reserved3 << "\n";
7815 outs() << " reserved4 " << r.reserved4 << "\n";
7816 outs() << " reserved5 " << r.reserved5 << "\n";
7817 outs() << " reserved6 " << r.reserved6 << "\n";
7818}
7819
7820static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7821 outs() << " cmd LC_ROUTINES_64\n";
7822 outs() << " cmdsize " << r.cmdsize;
7823 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7824 outs() << " Incorrect size\n";
7825 else
7826 outs() << "\n";
7827 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
7828 outs() << " init_module " << r.init_module << "\n";
7829 outs() << " reserved1 " << r.reserved1 << "\n";
7830 outs() << " reserved2 " << r.reserved2 << "\n";
7831 outs() << " reserved3 " << r.reserved3 << "\n";
7832 outs() << " reserved4 " << r.reserved4 << "\n";
7833 outs() << " reserved5 " << r.reserved5 << "\n";
7834 outs() << " reserved6 " << r.reserved6 << "\n";
7835}
7836
Kevin Enderby48ef5342014-12-23 22:56:39 +00007837static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7838 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7839 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7840 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7841 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7842 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7843 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7844 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7845 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7846 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7847 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7848 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7849 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7850 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7851 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7852 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7853 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7854 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7855 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7856 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7857 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7858 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7859}
7860
Kevin Enderby227df342014-12-23 23:43:59 +00007861static void Print_mmst_reg(MachO::mmst_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007862 uint32_t f;
7863 outs() << "\t mmst_reg ";
7864 for (f = 0; f < 10; f++)
7865 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7866 outs() << "\n";
7867 outs() << "\t mmst_rsrv ";
7868 for (f = 0; f < 6; f++)
7869 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7870 outs() << "\n";
7871}
7872
Kevin Enderbyaefb0032014-12-24 00:16:51 +00007873static void Print_xmm_reg(MachO::xmm_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007874 uint32_t f;
7875 outs() << "\t xmm_reg ";
7876 for (f = 0; f < 16; f++)
7877 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7878 outs() << "\n";
7879}
7880
7881static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7882 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7883 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7884 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7885 outs() << " denorm " << fpu.fpu_fcw.denorm;
7886 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7887 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7888 outs() << " undfl " << fpu.fpu_fcw.undfl;
7889 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7890 outs() << "\t\t pc ";
7891 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7892 outs() << "FP_PREC_24B ";
7893 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
7894 outs() << "FP_PREC_53B ";
7895 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
7896 outs() << "FP_PREC_64B ";
7897 else
7898 outs() << fpu.fpu_fcw.pc << " ";
7899 outs() << "rc ";
7900 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
7901 outs() << "FP_RND_NEAR ";
7902 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
7903 outs() << "FP_RND_DOWN ";
7904 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
7905 outs() << "FP_RND_UP ";
7906 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007907 outs() << "FP_CHOP ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00007908 outs() << "\n";
7909 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
7910 outs() << " denorm " << fpu.fpu_fsw.denorm;
7911 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
7912 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
7913 outs() << " undfl " << fpu.fpu_fsw.undfl;
7914 outs() << " precis " << fpu.fpu_fsw.precis;
7915 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
7916 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
7917 outs() << " c0 " << fpu.fpu_fsw.c0;
7918 outs() << " c1 " << fpu.fpu_fsw.c1;
7919 outs() << " c2 " << fpu.fpu_fsw.c2;
7920 outs() << " tos " << fpu.fpu_fsw.tos;
7921 outs() << " c3 " << fpu.fpu_fsw.c3;
7922 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
7923 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
7924 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
7925 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
7926 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
7927 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
7928 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
7929 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
7930 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
7931 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
7932 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
7933 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
7934 outs() << "\n";
7935 outs() << "\t fpu_stmm0:\n";
7936 Print_mmst_reg(fpu.fpu_stmm0);
7937 outs() << "\t fpu_stmm1:\n";
7938 Print_mmst_reg(fpu.fpu_stmm1);
7939 outs() << "\t fpu_stmm2:\n";
7940 Print_mmst_reg(fpu.fpu_stmm2);
7941 outs() << "\t fpu_stmm3:\n";
7942 Print_mmst_reg(fpu.fpu_stmm3);
7943 outs() << "\t fpu_stmm4:\n";
7944 Print_mmst_reg(fpu.fpu_stmm4);
7945 outs() << "\t fpu_stmm5:\n";
7946 Print_mmst_reg(fpu.fpu_stmm5);
7947 outs() << "\t fpu_stmm6:\n";
7948 Print_mmst_reg(fpu.fpu_stmm6);
7949 outs() << "\t fpu_stmm7:\n";
7950 Print_mmst_reg(fpu.fpu_stmm7);
7951 outs() << "\t fpu_xmm0:\n";
7952 Print_xmm_reg(fpu.fpu_xmm0);
7953 outs() << "\t fpu_xmm1:\n";
7954 Print_xmm_reg(fpu.fpu_xmm1);
7955 outs() << "\t fpu_xmm2:\n";
7956 Print_xmm_reg(fpu.fpu_xmm2);
7957 outs() << "\t fpu_xmm3:\n";
7958 Print_xmm_reg(fpu.fpu_xmm3);
7959 outs() << "\t fpu_xmm4:\n";
7960 Print_xmm_reg(fpu.fpu_xmm4);
7961 outs() << "\t fpu_xmm5:\n";
7962 Print_xmm_reg(fpu.fpu_xmm5);
7963 outs() << "\t fpu_xmm6:\n";
7964 Print_xmm_reg(fpu.fpu_xmm6);
7965 outs() << "\t fpu_xmm7:\n";
7966 Print_xmm_reg(fpu.fpu_xmm7);
7967 outs() << "\t fpu_xmm8:\n";
7968 Print_xmm_reg(fpu.fpu_xmm8);
7969 outs() << "\t fpu_xmm9:\n";
7970 Print_xmm_reg(fpu.fpu_xmm9);
7971 outs() << "\t fpu_xmm10:\n";
7972 Print_xmm_reg(fpu.fpu_xmm10);
7973 outs() << "\t fpu_xmm11:\n";
7974 Print_xmm_reg(fpu.fpu_xmm11);
7975 outs() << "\t fpu_xmm12:\n";
7976 Print_xmm_reg(fpu.fpu_xmm12);
7977 outs() << "\t fpu_xmm13:\n";
7978 Print_xmm_reg(fpu.fpu_xmm13);
7979 outs() << "\t fpu_xmm14:\n";
7980 Print_xmm_reg(fpu.fpu_xmm14);
7981 outs() << "\t fpu_xmm15:\n";
7982 Print_xmm_reg(fpu.fpu_xmm15);
7983 outs() << "\t fpu_rsrv4:\n";
7984 for (uint32_t f = 0; f < 6; f++) {
7985 outs() << "\t ";
7986 for (uint32_t g = 0; g < 16; g++)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007987 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00007988 outs() << "\n";
7989 }
7990 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
7991 outs() << "\n";
7992}
7993
7994static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
7995 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
7996 outs() << " err " << format("0x%08" PRIx32, exc64.err);
7997 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
7998}
7999
8000static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8001 bool isLittleEndian, uint32_t cputype) {
8002 if (t.cmd == MachO::LC_THREAD)
8003 outs() << " cmd LC_THREAD\n";
8004 else if (t.cmd == MachO::LC_UNIXTHREAD)
8005 outs() << " cmd LC_UNIXTHREAD\n";
8006 else
8007 outs() << " cmd " << t.cmd << " (unknown)\n";
8008 outs() << " cmdsize " << t.cmdsize;
8009 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8010 outs() << " Incorrect size\n";
8011 else
8012 outs() << "\n";
8013
8014 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8015 const char *end = Ptr + t.cmdsize;
8016 uint32_t flavor, count, left;
8017 if (cputype == MachO::CPU_TYPE_X86_64) {
8018 while (begin < end) {
8019 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8020 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8021 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008022 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008023 flavor = 0;
8024 begin = end;
8025 }
8026 if (isLittleEndian != sys::IsLittleEndianHost)
8027 sys::swapByteOrder(flavor);
8028 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8029 memcpy((char *)&count, begin, sizeof(uint32_t));
8030 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008031 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008032 count = 0;
8033 begin = end;
8034 }
8035 if (isLittleEndian != sys::IsLittleEndianHost)
8036 sys::swapByteOrder(count);
8037 if (flavor == MachO::x86_THREAD_STATE64) {
8038 outs() << " flavor x86_THREAD_STATE64\n";
8039 if (count == MachO::x86_THREAD_STATE64_COUNT)
8040 outs() << " count x86_THREAD_STATE64_COUNT\n";
8041 else
8042 outs() << " count " << count
8043 << " (not x86_THREAD_STATE64_COUNT)\n";
8044 MachO::x86_thread_state64_t cpu64;
8045 left = end - begin;
8046 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8047 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8048 begin += sizeof(MachO::x86_thread_state64_t);
8049 } else {
8050 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8051 memcpy(&cpu64, begin, left);
8052 begin += left;
8053 }
8054 if (isLittleEndian != sys::IsLittleEndianHost)
8055 swapStruct(cpu64);
8056 Print_x86_thread_state64_t(cpu64);
8057 } else if (flavor == MachO::x86_THREAD_STATE) {
8058 outs() << " flavor x86_THREAD_STATE\n";
8059 if (count == MachO::x86_THREAD_STATE_COUNT)
8060 outs() << " count x86_THREAD_STATE_COUNT\n";
8061 else
8062 outs() << " count " << count
8063 << " (not x86_THREAD_STATE_COUNT)\n";
8064 struct MachO::x86_thread_state_t ts;
8065 left = end - begin;
8066 if (left >= sizeof(MachO::x86_thread_state_t)) {
8067 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8068 begin += sizeof(MachO::x86_thread_state_t);
8069 } else {
8070 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8071 memcpy(&ts, begin, left);
8072 begin += left;
8073 }
8074 if (isLittleEndian != sys::IsLittleEndianHost)
8075 swapStruct(ts);
8076 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8077 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8078 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8079 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8080 else
8081 outs() << "tsh.count " << ts.tsh.count
8082 << " (not x86_THREAD_STATE64_COUNT\n";
8083 Print_x86_thread_state64_t(ts.uts.ts64);
8084 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008085 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8086 << ts.tsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008087 }
8088 } else if (flavor == MachO::x86_FLOAT_STATE) {
8089 outs() << " flavor x86_FLOAT_STATE\n";
8090 if (count == MachO::x86_FLOAT_STATE_COUNT)
8091 outs() << " count x86_FLOAT_STATE_COUNT\n";
8092 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008093 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008094 struct MachO::x86_float_state_t fs;
8095 left = end - begin;
8096 if (left >= sizeof(MachO::x86_float_state_t)) {
8097 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8098 begin += sizeof(MachO::x86_float_state_t);
8099 } else {
8100 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8101 memcpy(&fs, begin, left);
8102 begin += left;
8103 }
8104 if (isLittleEndian != sys::IsLittleEndianHost)
8105 swapStruct(fs);
8106 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8107 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008108 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008109 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8110 else
8111 outs() << "fsh.count " << fs.fsh.count
8112 << " (not x86_FLOAT_STATE64_COUNT\n";
8113 Print_x86_float_state_t(fs.ufs.fs64);
8114 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008115 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8116 << fs.fsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008117 }
8118 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8119 outs() << " flavor x86_EXCEPTION_STATE\n";
8120 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8121 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8122 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008123 outs() << " count " << count
Kevin Enderby48ef5342014-12-23 22:56:39 +00008124 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8125 struct MachO::x86_exception_state_t es;
8126 left = end - begin;
8127 if (left >= sizeof(MachO::x86_exception_state_t)) {
8128 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8129 begin += sizeof(MachO::x86_exception_state_t);
8130 } else {
8131 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8132 memcpy(&es, begin, left);
8133 begin += left;
8134 }
8135 if (isLittleEndian != sys::IsLittleEndianHost)
8136 swapStruct(es);
8137 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8138 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008139 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008140 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8141 else
8142 outs() << "\t esh.count " << es.esh.count
8143 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8144 Print_x86_exception_state_t(es.ues.es64);
8145 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008146 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8147 << es.esh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008148 }
8149 } else {
8150 outs() << " flavor " << flavor << " (unknown)\n";
8151 outs() << " count " << count << "\n";
8152 outs() << " state (unknown)\n";
8153 begin += count * sizeof(uint32_t);
8154 }
8155 }
8156 } else {
8157 while (begin < end) {
8158 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8159 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8160 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008161 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008162 flavor = 0;
8163 begin = end;
8164 }
8165 if (isLittleEndian != sys::IsLittleEndianHost)
8166 sys::swapByteOrder(flavor);
8167 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8168 memcpy((char *)&count, begin, sizeof(uint32_t));
8169 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008170 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008171 count = 0;
8172 begin = end;
8173 }
8174 if (isLittleEndian != sys::IsLittleEndianHost)
8175 sys::swapByteOrder(count);
8176 outs() << " flavor " << flavor << "\n";
8177 outs() << " count " << count << "\n";
8178 outs() << " state (Unknown cputype/cpusubtype)\n";
8179 begin += count * sizeof(uint32_t);
8180 }
8181 }
8182}
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008183
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008184static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8185 if (dl.cmd == MachO::LC_ID_DYLIB)
8186 outs() << " cmd LC_ID_DYLIB\n";
8187 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8188 outs() << " cmd LC_LOAD_DYLIB\n";
8189 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8190 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8191 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8192 outs() << " cmd LC_REEXPORT_DYLIB\n";
8193 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8194 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8195 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8196 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8197 else
8198 outs() << " cmd " << dl.cmd << " (unknown)\n";
8199 outs() << " cmdsize " << dl.cmdsize;
8200 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8201 outs() << " Incorrect size\n";
8202 else
8203 outs() << "\n";
8204 if (dl.dylib.name < dl.cmdsize) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008205 const char *P = (const char *)(Ptr) + dl.dylib.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008206 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8207 } else {
8208 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8209 }
8210 outs() << " time stamp " << dl.dylib.timestamp << " ";
8211 time_t t = dl.dylib.timestamp;
8212 outs() << ctime(&t);
8213 outs() << " current version ";
8214 if (dl.dylib.current_version == 0xffffffff)
8215 outs() << "n/a\n";
8216 else
8217 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8218 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8219 << (dl.dylib.current_version & 0xff) << "\n";
8220 outs() << "compatibility version ";
8221 if (dl.dylib.compatibility_version == 0xffffffff)
8222 outs() << "n/a\n";
8223 else
8224 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8225 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8226 << (dl.dylib.compatibility_version & 0xff) << "\n";
8227}
8228
8229static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8230 uint32_t object_size) {
8231 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8232 outs() << " cmd LC_FUNCTION_STARTS\n";
8233 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8234 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8235 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8236 outs() << " cmd LC_FUNCTION_STARTS\n";
8237 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8238 outs() << " cmd LC_DATA_IN_CODE\n";
8239 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8240 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8241 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8242 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8243 else
8244 outs() << " cmd " << ld.cmd << " (?)\n";
8245 outs() << " cmdsize " << ld.cmdsize;
8246 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8247 outs() << " Incorrect size\n";
8248 else
8249 outs() << "\n";
8250 outs() << " dataoff " << ld.dataoff;
8251 if (ld.dataoff > object_size)
8252 outs() << " (past end of file)\n";
8253 else
8254 outs() << "\n";
8255 outs() << " datasize " << ld.datasize;
8256 uint64_t big_size = ld.dataoff;
8257 big_size += ld.datasize;
8258 if (big_size > object_size)
8259 outs() << " (past end of file)\n";
8260 else
8261 outs() << "\n";
8262}
8263
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008264static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8265 uint32_t cputype, bool verbose) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008266 StringRef Buf = Obj->getData();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008267 unsigned Index = 0;
8268 for (const auto &Command : Obj->load_commands()) {
8269 outs() << "Load command " << Index++ << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00008270 if (Command.C.cmd == MachO::LC_SEGMENT) {
8271 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8272 const char *sg_segname = SLC.segname;
8273 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8274 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8275 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8276 verbose);
8277 for (unsigned j = 0; j < SLC.nsects; j++) {
Kevin Enderbyc9713382014-12-16 01:14:45 +00008278 MachO::section S = Obj->getSection(Command, j);
Kevin Enderby956366c2014-08-29 22:30:52 +00008279 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8280 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8281 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8282 }
8283 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8284 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8285 const char *sg_segname = SLC_64.segname;
8286 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8287 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8288 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8289 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8290 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8291 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8292 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8293 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8294 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8295 sg_segname, filetype, Buf.size(), verbose);
8296 }
8297 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8298 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008299 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008300 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8301 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8302 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008303 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8304 Obj->is64Bit());
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008305 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8306 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8307 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8308 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8309 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8310 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8311 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8312 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8313 PrintDyldLoadCommand(Dyld, Command.Ptr);
8314 } else if (Command.C.cmd == MachO::LC_UUID) {
8315 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8316 PrintUuidLoadCommand(Uuid);
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008317 } else if (Command.C.cmd == MachO::LC_RPATH) {
8318 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8319 PrintRpathLoadCommand(Rpath, Command.Ptr);
Kevin Enderby1ff0ecc2014-12-16 21:48:27 +00008320 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
8321 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008322 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8323 PrintVersionMinLoadCommand(Vd);
8324 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8325 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8326 PrintSourceVersionCommand(Sd);
8327 } else if (Command.C.cmd == MachO::LC_MAIN) {
8328 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8329 PrintEntryPointCommand(Ep);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008330 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008331 MachO::encryption_info_command Ei =
8332 Obj->getEncryptionInfoCommand(Command);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008333 PrintEncryptionInfoCommand(Ei, Buf.size());
Kevin Enderby57538292014-12-17 01:01:30 +00008334 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008335 MachO::encryption_info_command_64 Ei =
8336 Obj->getEncryptionInfoCommand64(Command);
Kevin Enderby57538292014-12-17 01:01:30 +00008337 PrintEncryptionInfoCommand64(Ei, Buf.size());
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008338 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008339 MachO::linker_option_command Lo =
8340 Obj->getLinkerOptionLoadCommand(Command);
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008341 PrintLinkerOptionCommand(Lo, Command.Ptr);
Kevin Enderbyb4b79312014-12-18 19:24:35 +00008342 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8343 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8344 PrintSubFrameworkCommand(Sf, Command.Ptr);
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00008345 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8346 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8347 PrintSubUmbrellaCommand(Sf, Command.Ptr);
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008348 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8349 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8350 PrintSubLibraryCommand(Sl, Command.Ptr);
Kevin Enderby186eac32014-12-19 21:06:24 +00008351 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8352 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8353 PrintSubClientCommand(Sc, Command.Ptr);
Kevin Enderby52e4ce42014-12-19 22:25:22 +00008354 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8355 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8356 PrintRoutinesCommand(Rc);
8357 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8358 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8359 PrintRoutinesCommand64(Rc);
Kevin Enderby48ef5342014-12-23 22:56:39 +00008360 } else if (Command.C.cmd == MachO::LC_THREAD ||
8361 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8362 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8363 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
Nick Kledzik15558912014-10-16 18:58:20 +00008364 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8365 Command.C.cmd == MachO::LC_ID_DYLIB ||
8366 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8367 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8368 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8369 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008370 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8371 PrintDylibCommand(Dl, Command.Ptr);
8372 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8373 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8374 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8375 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8376 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8377 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8378 MachO::linkedit_data_command Ld =
8379 Obj->getLinkeditDataLoadCommand(Command);
8380 PrintLinkEditDataCommand(Ld, Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008381 } else {
8382 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8383 << ")\n";
8384 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8385 // TODO: get and print the raw bytes of the load command.
8386 }
8387 // TODO: print all the other kinds of load commands.
Kevin Enderby956366c2014-08-29 22:30:52 +00008388 }
8389}
8390
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008391static void getAndPrintMachHeader(const MachOObjectFile *Obj,
Kevin Enderby956366c2014-08-29 22:30:52 +00008392 uint32_t &filetype, uint32_t &cputype,
8393 bool verbose) {
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008394 if (Obj->is64Bit()) {
8395 MachO::mach_header_64 H_64;
8396 H_64 = Obj->getHeader64();
8397 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8398 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00008399 filetype = H_64.filetype;
8400 cputype = H_64.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008401 } else {
8402 MachO::mach_header H;
8403 H = Obj->getHeader();
8404 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8405 H.sizeofcmds, H.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00008406 filetype = H.filetype;
8407 cputype = H.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008408 }
8409}
8410
8411void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8412 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby956366c2014-08-29 22:30:52 +00008413 uint32_t filetype = 0;
8414 uint32_t cputype = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008415 getAndPrintMachHeader(file, filetype, cputype, !NonVerbose);
8416 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008417}
Nick Kledzikd04bc352014-08-30 00:20:14 +00008418
8419//===----------------------------------------------------------------------===//
8420// export trie dumping
8421//===----------------------------------------------------------------------===//
8422
8423void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008424 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
8425 uint64_t Flags = Entry.flags();
Nick Kledzikd04bc352014-08-30 00:20:14 +00008426 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
8427 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
8428 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8429 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
8430 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8431 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
8432 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
8433 if (ReExport)
8434 outs() << "[re-export] ";
8435 else
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008436 outs() << format("0x%08llX ",
8437 Entry.address()); // FIXME:add in base address
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008438 outs() << Entry.name();
Nick Kledzikd04bc352014-08-30 00:20:14 +00008439 if (WeakDef || ThreadLocal || Resolver || Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008440 bool NeedsComma = false;
Nick Kledzik1d1ac4b2014-09-03 01:12:52 +00008441 outs() << " [";
Nick Kledzikd04bc352014-08-30 00:20:14 +00008442 if (WeakDef) {
8443 outs() << "weak_def";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008444 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008445 }
8446 if (ThreadLocal) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008447 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008448 outs() << ", ";
8449 outs() << "per-thread";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008450 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008451 }
8452 if (Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008453 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008454 outs() << ", ";
8455 outs() << "absolute";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008456 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008457 }
8458 if (Resolver) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008459 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008460 outs() << ", ";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008461 outs() << format("resolver=0x%08llX", Entry.other());
8462 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008463 }
8464 outs() << "]";
8465 }
8466 if (ReExport) {
8467 StringRef DylibName = "unknown";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008468 int Ordinal = Entry.other() - 1;
8469 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
8470 if (Entry.otherName().empty())
Nick Kledzikd04bc352014-08-30 00:20:14 +00008471 outs() << " (from " << DylibName << ")";
8472 else
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008473 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
Nick Kledzikd04bc352014-08-30 00:20:14 +00008474 }
8475 outs() << "\n";
8476 }
8477}
Nick Kledzikac431442014-09-12 21:34:15 +00008478
Nick Kledzikac431442014-09-12 21:34:15 +00008479//===----------------------------------------------------------------------===//
8480// rebase table dumping
8481//===----------------------------------------------------------------------===//
8482
8483namespace {
8484class SegInfo {
8485public:
8486 SegInfo(const object::MachOObjectFile *Obj);
8487
8488 StringRef segmentName(uint32_t SegIndex);
8489 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
8490 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
8491
8492private:
8493 struct SectionInfo {
8494 uint64_t Address;
8495 uint64_t Size;
8496 StringRef SectionName;
8497 StringRef SegmentName;
8498 uint64_t OffsetInSegment;
8499 uint64_t SegmentStartAddress;
8500 uint32_t SegmentIndex;
8501 };
8502 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
8503 SmallVector<SectionInfo, 32> Sections;
8504};
8505}
8506
8507SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
8508 // Build table of sections so segIndex/offset pairs can be translated.
Nick Kledzik56ebef42014-09-16 01:41:51 +00008509 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
Nick Kledzikac431442014-09-12 21:34:15 +00008510 StringRef CurSegName;
8511 uint64_t CurSegAddress;
8512 for (const SectionRef &Section : Obj->sections()) {
8513 SectionInfo Info;
Davide Italianoccd53fe2015-08-05 07:18:31 +00008514 error(Section.getName(Info.SectionName));
Rafael Espindola80291272014-10-08 15:28:58 +00008515 Info.Address = Section.getAddress();
8516 Info.Size = Section.getSize();
Nick Kledzikac431442014-09-12 21:34:15 +00008517 Info.SegmentName =
8518 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
8519 if (!Info.SegmentName.equals(CurSegName)) {
8520 ++CurSegIndex;
8521 CurSegName = Info.SegmentName;
8522 CurSegAddress = Info.Address;
8523 }
8524 Info.SegmentIndex = CurSegIndex - 1;
8525 Info.OffsetInSegment = Info.Address - CurSegAddress;
8526 Info.SegmentStartAddress = CurSegAddress;
8527 Sections.push_back(Info);
8528 }
8529}
8530
8531StringRef SegInfo::segmentName(uint32_t SegIndex) {
8532 for (const SectionInfo &SI : Sections) {
8533 if (SI.SegmentIndex == SegIndex)
8534 return SI.SegmentName;
8535 }
8536 llvm_unreachable("invalid segIndex");
8537}
8538
8539const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
8540 uint64_t OffsetInSeg) {
8541 for (const SectionInfo &SI : Sections) {
8542 if (SI.SegmentIndex != SegIndex)
8543 continue;
8544 if (SI.OffsetInSegment > OffsetInSeg)
8545 continue;
8546 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8547 continue;
8548 return SI;
8549 }
8550 llvm_unreachable("segIndex and offset not in any section");
8551}
8552
8553StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
8554 return findSection(SegIndex, OffsetInSeg).SectionName;
8555}
8556
8557uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
8558 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
8559 return SI.SegmentStartAddress + OffsetInSeg;
8560}
8561
8562void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
8563 // Build table of sections so names can used in final output.
8564 SegInfo sectionTable(Obj);
8565
8566 outs() << "segment section address type\n";
8567 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
8568 uint32_t SegIndex = Entry.segmentIndex();
8569 uint64_t OffsetInSeg = Entry.segmentOffset();
8570 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8571 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8572 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8573
8574 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008575 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
8576 SegmentName.str().c_str(), SectionName.str().c_str(),
8577 Address, Entry.typeName().str().c_str());
Nick Kledzikac431442014-09-12 21:34:15 +00008578 }
8579}
Nick Kledzik56ebef42014-09-16 01:41:51 +00008580
8581static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
8582 StringRef DylibName;
8583 switch (Ordinal) {
8584 case MachO::BIND_SPECIAL_DYLIB_SELF:
8585 return "this-image";
8586 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
8587 return "main-executable";
8588 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
8589 return "flat-namespace";
8590 default:
Nick Kledzikabd29872014-09-16 22:03:13 +00008591 if (Ordinal > 0) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008592 std::error_code EC =
8593 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
Nick Kledzikabd29872014-09-16 22:03:13 +00008594 if (EC)
Nick Kledzik51d2c2b2014-10-14 23:29:38 +00008595 return "<<bad library ordinal>>";
Nick Kledzikabd29872014-09-16 22:03:13 +00008596 return DylibName;
8597 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00008598 }
Nick Kledzikabd29872014-09-16 22:03:13 +00008599 return "<<unknown special ordinal>>";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008600}
8601
8602//===----------------------------------------------------------------------===//
8603// bind table dumping
8604//===----------------------------------------------------------------------===//
8605
8606void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
8607 // Build table of sections so names can used in final output.
8608 SegInfo sectionTable(Obj);
8609
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008610 outs() << "segment section address type "
8611 "addend dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008612 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
8613 uint32_t SegIndex = Entry.segmentIndex();
8614 uint64_t OffsetInSeg = Entry.segmentOffset();
8615 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8616 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8617 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8618
8619 // Table lines look like:
8620 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008621 StringRef Attr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00008622 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008623 Attr = " (weak_import)";
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008624 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008625 << left_justify(SectionName, 18) << " "
8626 << format_hex(Address, 10, true) << " "
8627 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008628 << format_decimal(Entry.addend(), 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008629 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008630 << Entry.symbolName() << Attr << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008631 }
8632}
8633
8634//===----------------------------------------------------------------------===//
8635// lazy bind table dumping
8636//===----------------------------------------------------------------------===//
8637
8638void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
8639 // Build table of sections so names can used in final output.
8640 SegInfo sectionTable(Obj);
8641
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008642 outs() << "segment section address "
8643 "dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008644 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
8645 uint32_t SegIndex = Entry.segmentIndex();
8646 uint64_t OffsetInSeg = Entry.segmentOffset();
8647 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8648 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8649 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8650
8651 // Table lines look like:
8652 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008653 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008654 << left_justify(SectionName, 18) << " "
8655 << format_hex(Address, 10, true) << " "
8656 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Nick Kledzik56ebef42014-09-16 01:41:51 +00008657 << Entry.symbolName() << "\n";
8658 }
8659}
8660
Nick Kledzik56ebef42014-09-16 01:41:51 +00008661//===----------------------------------------------------------------------===//
8662// weak bind table dumping
8663//===----------------------------------------------------------------------===//
8664
8665void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
8666 // Build table of sections so names can used in final output.
8667 SegInfo sectionTable(Obj);
8668
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008669 outs() << "segment section address "
8670 "type addend symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008671 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
8672 // Strong symbols don't have a location to update.
8673 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008674 outs() << " strong "
Nick Kledzik56ebef42014-09-16 01:41:51 +00008675 << Entry.symbolName() << "\n";
8676 continue;
8677 }
8678 uint32_t SegIndex = Entry.segmentIndex();
8679 uint64_t OffsetInSeg = Entry.segmentOffset();
8680 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8681 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8682 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8683
8684 // Table lines look like:
8685 // __DATA __data 0x00001000 pointer 0 _foo
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008686 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008687 << left_justify(SectionName, 18) << " "
8688 << format_hex(Address, 10, true) << " "
8689 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008690 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
8691 << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008692 }
8693}
8694
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008695// get_dyld_bind_info_symbolname() is used for disassembly and passed an
8696// address, ReferenceValue, in the Mach-O file and looks in the dyld bind
8697// information for that address. If the address is found its binding symbol
8698// name is returned. If not nullptr is returned.
8699static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
8700 struct DisassembleInfo *info) {
Kevin Enderby078be602014-10-23 19:53:12 +00008701 if (info->bindtable == nullptr) {
8702 info->bindtable = new (BindTable);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008703 SegInfo sectionTable(info->O);
8704 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
8705 uint32_t SegIndex = Entry.segmentIndex();
8706 uint64_t OffsetInSeg = Entry.segmentOffset();
8707 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8708 const char *SymbolName = nullptr;
8709 StringRef name = Entry.symbolName();
8710 if (!name.empty())
8711 SymbolName = name.data();
Kevin Enderby078be602014-10-23 19:53:12 +00008712 info->bindtable->push_back(std::make_pair(Address, SymbolName));
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008713 }
8714 }
Kevin Enderby078be602014-10-23 19:53:12 +00008715 for (bind_table_iterator BI = info->bindtable->begin(),
8716 BE = info->bindtable->end();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008717 BI != BE; ++BI) {
8718 uint64_t Address = BI->first;
8719 if (ReferenceValue == Address) {
8720 const char *SymbolName = BI->second;
8721 return SymbolName;
8722 }
8723 }
8724 return nullptr;
8725}