blob: bc743bcd067c04f3e1211df0ce430bb258566856 [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 Enderbyf6d25852015-01-31 00:37:11 +0000100cl::list<std::string>
101 llvm::DumpSections("section",
102 cl::desc("Prints the specified segment,section for "
103 "Mach-O objects (requires -macho)"));
104
Adrian Prantlc2401dd2015-03-27 17:31:15 +0000105cl::opt<bool> llvm::Raw("raw",
106 cl::desc("Have -section dump the raw binary contents"));
107
Kevin Enderbycd66be52015-03-11 22:06:32 +0000108cl::opt<bool>
109 llvm::InfoPlist("info-plist",
110 cl::desc("Print the info plist section as strings for "
111 "Mach-O objects (requires -macho)"));
112
Kevin Enderbyf0640752015-03-13 17:56:32 +0000113cl::opt<bool>
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000114 llvm::DylibsUsed("dylibs-used",
115 cl::desc("Print the shared libraries used for linked "
116 "Mach-O files (requires -macho)"));
117
118cl::opt<bool>
119 llvm::DylibId("dylib-id",
120 cl::desc("Print the shared library's id for the dylib Mach-O "
121 "file (requires -macho)"));
122
123cl::opt<bool>
Kevin Enderbyf0640752015-03-13 17:56:32 +0000124 llvm::NonVerbose("non-verbose",
125 cl::desc("Print the info for Mach-O objects in "
126 "non-verbose or numeric form (requires -macho)"));
127
Kevin Enderby0fc11822015-04-01 20:57:01 +0000128cl::opt<bool>
129 llvm::ObjcMetaData("objc-meta-data",
130 cl::desc("Print the Objective-C runtime meta data for "
131 "Mach-O files (requires -macho)"));
132
Kevin Enderby6a221752015-03-17 17:10:57 +0000133cl::opt<std::string> llvm::DisSymName(
134 "dis-symname",
135 cl::desc("disassemble just this symbol's instructions (requires -macho"));
136
Kevin Enderby8e29ec92015-03-17 22:26:11 +0000137static cl::opt<bool> NoSymbolicOperands(
138 "no-symbolic-operands",
139 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
140
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000141static cl::list<std::string>
142 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
143 cl::ZeroOrMore);
144bool ArchAll = false;
145
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000146static std::string ThumbTripleName;
147
148static const Target *GetTarget(const MachOObjectFile *MachOObj,
149 const char **McpuDefault,
150 const Target **ThumbTarget) {
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000151 // Figure out the target triple.
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000152 if (TripleName.empty()) {
153 llvm::Triple TT("unknown-unknown-unknown");
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000154 llvm::Triple ThumbTriple = Triple();
155 TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000156 TripleName = TT.str();
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000157 ThumbTripleName = ThumbTriple.str();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000158 }
159
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000160 // Get the target specific parser.
161 std::string Error;
162 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000163 if (TheTarget && ThumbTripleName.empty())
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000164 return TheTarget;
165
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000166 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
167 if (*ThumbTarget)
168 return TheTarget;
169
170 errs() << "llvm-objdump: error: unable to get target for '";
171 if (!TheTarget)
172 errs() << TripleName;
173 else
174 errs() << ThumbTripleName;
175 errs() << "', see --version and --triple.\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000176 return nullptr;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000177}
178
Owen Andersond9243c42011-10-17 21:37:35 +0000179struct SymbolSorter {
180 bool operator()(const SymbolRef &A, const SymbolRef &B) {
181 SymbolRef::Type AType, BType;
182 A.getType(AType);
183 B.getType(BType);
184
185 uint64_t AAddr, BAddr;
186 if (AType != SymbolRef::ST_Function)
187 AAddr = 0;
188 else
189 A.getAddress(AAddr);
190 if (BType != SymbolRef::ST_Function)
191 BAddr = 0;
192 else
193 B.getAddress(BAddr);
194 return AAddr < BAddr;
195 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000196};
197
Kevin Enderby273ae012013-06-06 17:20:50 +0000198// Types for the storted data in code table that is built before disassembly
199// and the predicate function to sort them.
200typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
201typedef std::vector<DiceTableEntry> DiceTable;
202typedef DiceTable::iterator dice_table_iterator;
203
Kevin Enderby930fdc72014-11-06 19:00:13 +0000204// This is used to search for a data in code table entry for the PC being
205// disassembled. The j parameter has the PC in j.first. A single data in code
206// table entry can cover many bytes for each of its Kind's. So if the offset,
207// aka the i.first value, of the data in code table entry plus its Length
208// covers the PC being searched for this will return true. If not it will
209// return false.
David Majnemerea9b8ee2014-11-04 08:41:48 +0000210static bool compareDiceTableEntries(const DiceTableEntry &i,
211 const DiceTableEntry &j) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000212 uint16_t Length;
213 i.second.getLength(Length);
214
215 return j.first >= i.first && j.first < i.first + Length;
Kevin Enderby273ae012013-06-06 17:20:50 +0000216}
217
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000218static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
Kevin Enderby930fdc72014-11-06 19:00:13 +0000219 unsigned short Kind) {
220 uint32_t Value, Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000221
222 switch (Kind) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000223 default:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000224 case MachO::DICE_KIND_DATA:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000225 if (Length >= 4) {
226 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000227 dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000228 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000229 outs() << "\t.long " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000230 Size = 4;
231 } else if (Length >= 2) {
232 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000233 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000234 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000235 outs() << "\t.short " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000236 Size = 2;
237 } else {
238 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000239 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000240 Value = bytes[0];
241 outs() << "\t.byte " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000242 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000243 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000244 if (Kind == MachO::DICE_KIND_DATA)
245 outs() << "\t@ KIND_DATA\n";
246 else
247 outs() << "\t@ data in code kind = " << Kind << "\n";
Kevin Enderby273ae012013-06-06 17:20:50 +0000248 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000249 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000250 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000251 dumpBytes(ArrayRef<uint8_t>(bytes, 1), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000252 Value = bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000253 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
254 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000255 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000256 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000257 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000258 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000259 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000260 outs() << "\t.short " << format("%5u", Value & 0xffff)
261 << "\t@ KIND_JUMP_TABLE16\n";
262 Size = 2;
Kevin Enderby273ae012013-06-06 17:20:50 +0000263 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000264 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000265 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
266 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000267 dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000268 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000269 outs() << "\t.long " << Value;
270 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
271 outs() << "\t@ KIND_JUMP_TABLE32\n";
272 else
273 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
274 Size = 4;
Kevin Enderby273ae012013-06-06 17:20:50 +0000275 break;
276 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000277 return Size;
Kevin Enderby273ae012013-06-06 17:20:50 +0000278}
279
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000280static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000281 std::vector<SectionRef> &Sections,
282 std::vector<SymbolRef> &Symbols,
283 SmallVectorImpl<uint64_t> &FoundFns,
284 uint64_t &BaseSegmentAddress) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000285 for (const SymbolRef &Symbol : MachOObj->symbols()) {
286 StringRef SymName;
287 Symbol.getName(SymName);
288 if (!SymName.startswith("ltmp"))
289 Symbols.push_back(Symbol);
290 }
Owen Andersond9243c42011-10-17 21:37:35 +0000291
Alexey Samsonov48803e52014-03-13 14:37:36 +0000292 for (const SectionRef &Section : MachOObj->sections()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000293 StringRef SectName;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000294 Section.getName(SectName);
295 Sections.push_back(Section);
Owen Andersond9243c42011-10-17 21:37:35 +0000296 }
297
Kevin Enderby273ae012013-06-06 17:20:50 +0000298 bool BaseSegmentAddressSet = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000299 for (const auto &Command : MachOObj->load_commands()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000300 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000301 // We found a function starts segment, parse the addresses for later
302 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000303 MachO::linkedit_data_command LLC =
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000304 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000305
Charles Davis8bdfafd2013-09-01 04:28:48 +0000306 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000307 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
308 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000309 StringRef SegName = SLC.segname;
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000310 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
Kevin Enderby273ae012013-06-06 17:20:50 +0000311 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000312 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000313 }
314 }
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000315 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000316}
317
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000318static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
319 uint32_t n, uint32_t count,
320 uint32_t stride, uint64_t addr) {
321 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
322 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
323 if (n > nindirectsyms)
324 outs() << " (entries start past the end of the indirect symbol "
325 "table) (reserved1 field greater than the table size)";
326 else if (n + count > nindirectsyms)
327 outs() << " (entries extends past the end of the indirect symbol "
328 "table)";
329 outs() << "\n";
330 uint32_t cputype = O->getHeader().cputype;
331 if (cputype & MachO::CPU_ARCH_ABI64)
332 outs() << "address index";
333 else
334 outs() << "address index";
335 if (verbose)
336 outs() << " name\n";
337 else
338 outs() << "\n";
339 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
340 if (cputype & MachO::CPU_ARCH_ABI64)
341 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
342 else
343 outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
344 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
345 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
346 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
347 outs() << "LOCAL\n";
348 continue;
349 }
350 if (indirect_symbol ==
351 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
352 outs() << "LOCAL ABSOLUTE\n";
353 continue;
354 }
355 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
356 outs() << "ABSOLUTE\n";
357 continue;
358 }
359 outs() << format("%5u ", indirect_symbol);
Kevin Enderbyf0640752015-03-13 17:56:32 +0000360 if (verbose) {
361 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
362 if (indirect_symbol < Symtab.nsyms) {
363 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
364 SymbolRef Symbol = *Sym;
365 StringRef SymName;
366 Symbol.getName(SymName);
367 outs() << SymName;
368 } else {
369 outs() << "?";
370 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000371 }
372 outs() << "\n";
373 }
374}
375
376static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000377 for (const auto &Load : O->load_commands()) {
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000378 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
379 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
380 for (unsigned J = 0; J < Seg.nsects; ++J) {
381 MachO::section_64 Sec = O->getSection64(Load, J);
382 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
383 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
384 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
385 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
386 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
387 section_type == MachO::S_SYMBOL_STUBS) {
388 uint32_t stride;
389 if (section_type == MachO::S_SYMBOL_STUBS)
390 stride = Sec.reserved2;
391 else
392 stride = 8;
393 if (stride == 0) {
394 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
395 << Sec.sectname << ") "
396 << "(size of stubs in reserved2 field is zero)\n";
397 continue;
398 }
399 uint32_t count = Sec.size / stride;
400 outs() << "Indirect symbols for (" << Sec.segname << ","
401 << Sec.sectname << ") " << count << " entries";
402 uint32_t n = Sec.reserved1;
403 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
404 }
405 }
406 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
407 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
408 for (unsigned J = 0; J < Seg.nsects; ++J) {
409 MachO::section Sec = O->getSection(Load, J);
410 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
411 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
412 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
413 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
414 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
415 section_type == MachO::S_SYMBOL_STUBS) {
416 uint32_t stride;
417 if (section_type == MachO::S_SYMBOL_STUBS)
418 stride = Sec.reserved2;
419 else
420 stride = 4;
421 if (stride == 0) {
422 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
423 << Sec.sectname << ") "
424 << "(size of stubs in reserved2 field is zero)\n";
425 continue;
426 }
427 uint32_t count = Sec.size / stride;
428 outs() << "Indirect symbols for (" << Sec.segname << ","
429 << Sec.sectname << ") " << count << " entries";
430 uint32_t n = Sec.reserved1;
431 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
432 }
433 }
434 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000435 }
436}
437
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000438static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
439 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
440 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
441 outs() << "Data in code table (" << nentries << " entries)\n";
442 outs() << "offset length kind\n";
443 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
444 ++DI) {
445 uint32_t Offset;
446 DI->getOffset(Offset);
447 outs() << format("0x%08" PRIx32, Offset) << " ";
448 uint16_t Length;
449 DI->getLength(Length);
450 outs() << format("%6u", Length) << " ";
451 uint16_t Kind;
452 DI->getKind(Kind);
453 if (verbose) {
454 switch (Kind) {
455 case MachO::DICE_KIND_DATA:
456 outs() << "DATA";
457 break;
458 case MachO::DICE_KIND_JUMP_TABLE8:
459 outs() << "JUMP_TABLE8";
460 break;
461 case MachO::DICE_KIND_JUMP_TABLE16:
462 outs() << "JUMP_TABLE16";
463 break;
464 case MachO::DICE_KIND_JUMP_TABLE32:
465 outs() << "JUMP_TABLE32";
466 break;
467 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
468 outs() << "ABS_JUMP_TABLE32";
469 break;
470 default:
471 outs() << format("0x%04" PRIx32, Kind);
472 break;
473 }
474 } else
475 outs() << format("0x%04" PRIx32, Kind);
476 outs() << "\n";
477 }
478}
479
Kevin Enderby9a509442015-01-27 21:28:24 +0000480static void PrintLinkOptHints(MachOObjectFile *O) {
481 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
482 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
483 uint32_t nloh = LohLC.datasize;
484 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
485 for (uint32_t i = 0; i < nloh;) {
486 unsigned n;
487 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
488 i += n;
489 outs() << " identifier " << identifier << " ";
490 if (i >= nloh)
491 return;
492 switch (identifier) {
493 case 1:
494 outs() << "AdrpAdrp\n";
495 break;
496 case 2:
497 outs() << "AdrpLdr\n";
498 break;
499 case 3:
500 outs() << "AdrpAddLdr\n";
501 break;
502 case 4:
503 outs() << "AdrpLdrGotLdr\n";
504 break;
505 case 5:
506 outs() << "AdrpAddStr\n";
507 break;
508 case 6:
509 outs() << "AdrpLdrGotStr\n";
510 break;
511 case 7:
512 outs() << "AdrpAdd\n";
513 break;
514 case 8:
515 outs() << "AdrpLdrGot\n";
516 break;
517 default:
518 outs() << "Unknown identifier value\n";
519 break;
520 }
521 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
522 i += n;
523 outs() << " narguments " << narguments << "\n";
524 if (i >= nloh)
525 return;
526
527 for (uint32_t j = 0; j < narguments; j++) {
528 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
529 i += n;
530 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
531 if (i >= nloh)
532 return;
533 }
534 }
535}
536
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000537static void PrintDylibs(MachOObjectFile *O, bool JustId) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000538 unsigned Index = 0;
539 for (const auto &Load : O->load_commands()) {
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000540 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
541 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
542 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
543 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
544 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
545 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
546 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
547 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
548 if (dl.dylib.name < dl.cmdsize) {
549 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
550 if (JustId)
551 outs() << p << "\n";
552 else {
553 outs() << "\t" << p;
554 outs() << " (compatibility version "
555 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
556 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
557 << (dl.dylib.compatibility_version & 0xff) << ",";
558 outs() << " current version "
559 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
560 << ((dl.dylib.current_version >> 8) & 0xff) << "."
561 << (dl.dylib.current_version & 0xff) << ")\n";
562 }
563 } else {
564 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
565 if (Load.C.cmd == MachO::LC_ID_DYLIB)
566 outs() << "LC_ID_DYLIB ";
567 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
568 outs() << "LC_LOAD_DYLIB ";
569 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
570 outs() << "LC_LOAD_WEAK_DYLIB ";
571 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
572 outs() << "LC_LAZY_LOAD_DYLIB ";
573 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
574 outs() << "LC_REEXPORT_DYLIB ";
575 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
576 outs() << "LC_LOAD_UPWARD_DYLIB ";
577 else
578 outs() << "LC_??? ";
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000579 outs() << "command " << Index++ << "\n";
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000580 }
581 }
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000582 }
583}
584
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000585typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
586
587static void CreateSymbolAddressMap(MachOObjectFile *O,
588 SymbolAddressMap *AddrMap) {
589 // Create a map of symbol addresses to symbol names.
590 for (const SymbolRef &Symbol : O->symbols()) {
591 SymbolRef::Type ST;
592 Symbol.getType(ST);
593 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
594 ST == SymbolRef::ST_Other) {
595 uint64_t Address;
596 Symbol.getAddress(Address);
597 StringRef SymName;
598 Symbol.getName(SymName);
Kevin Enderby846c0002015-04-16 17:19:59 +0000599 if (!SymName.startswith(".objc"))
600 (*AddrMap)[Address] = SymName;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000601 }
602 }
603}
604
605// GuessSymbolName is passed the address of what might be a symbol and a
606// pointer to the SymbolAddressMap. It returns the name of a symbol
607// with that address or nullptr if no symbol is found with that address.
608static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
609 const char *SymbolName = nullptr;
610 // A DenseMap can't lookup up some values.
611 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
612 StringRef name = AddrMap->lookup(value);
613 if (!name.empty())
614 SymbolName = name.data();
615 }
616 return SymbolName;
617}
618
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000619static void DumpCstringChar(const char c) {
620 char p[2];
621 p[0] = c;
622 p[1] = '\0';
623 outs().write_escaped(p);
624}
625
Kevin Enderby10ba0412015-02-04 21:38:42 +0000626static void DumpCstringSection(MachOObjectFile *O, const char *sect,
627 uint32_t sect_size, uint64_t sect_addr,
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000628 bool print_addresses) {
Kevin Enderby10ba0412015-02-04 21:38:42 +0000629 for (uint32_t i = 0; i < sect_size; i++) {
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000630 if (print_addresses) {
631 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000632 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000633 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000634 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000635 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000636 for (; i < sect_size && sect[i] != '\0'; i++)
637 DumpCstringChar(sect[i]);
Kevin Enderby10ba0412015-02-04 21:38:42 +0000638 if (i < sect_size && sect[i] == '\0')
639 outs() << "\n";
640 }
641}
642
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000643static void DumpLiteral4(uint32_t l, float f) {
644 outs() << format("0x%08" PRIx32, l);
645 if ((l & 0x7f800000) != 0x7f800000)
646 outs() << format(" (%.16e)\n", f);
647 else {
648 if (l == 0x7f800000)
649 outs() << " (+Infinity)\n";
650 else if (l == 0xff800000)
651 outs() << " (-Infinity)\n";
652 else if ((l & 0x00400000) == 0x00400000)
653 outs() << " (non-signaling Not-a-Number)\n";
654 else
655 outs() << " (signaling Not-a-Number)\n";
656 }
657}
658
659static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
660 uint32_t sect_size, uint64_t sect_addr,
661 bool print_addresses) {
662 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
663 if (print_addresses) {
664 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000665 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000666 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000667 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000668 }
669 float f;
670 memcpy(&f, sect + i, sizeof(float));
671 if (O->isLittleEndian() != sys::IsLittleEndianHost)
672 sys::swapByteOrder(f);
673 uint32_t l;
674 memcpy(&l, sect + i, sizeof(uint32_t));
675 if (O->isLittleEndian() != sys::IsLittleEndianHost)
676 sys::swapByteOrder(l);
677 DumpLiteral4(l, f);
678 }
679}
680
681static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
682 double d) {
683 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
684 uint32_t Hi, Lo;
685 if (O->isLittleEndian()) {
686 Hi = l1;
687 Lo = l0;
688 } else {
689 Hi = l0;
690 Lo = l1;
691 }
692 // Hi is the high word, so this is equivalent to if(isfinite(d))
693 if ((Hi & 0x7ff00000) != 0x7ff00000)
694 outs() << format(" (%.16e)\n", d);
695 else {
696 if (Hi == 0x7ff00000 && Lo == 0)
697 outs() << " (+Infinity)\n";
698 else if (Hi == 0xfff00000 && Lo == 0)
699 outs() << " (-Infinity)\n";
700 else if ((Hi & 0x00080000) == 0x00080000)
701 outs() << " (non-signaling Not-a-Number)\n";
702 else
703 outs() << " (signaling Not-a-Number)\n";
704 }
705}
706
707static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
708 uint32_t sect_size, uint64_t sect_addr,
709 bool print_addresses) {
710 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
711 if (print_addresses) {
712 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000713 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000714 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000715 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000716 }
717 double d;
718 memcpy(&d, sect + i, sizeof(double));
719 if (O->isLittleEndian() != sys::IsLittleEndianHost)
720 sys::swapByteOrder(d);
721 uint32_t l0, l1;
722 memcpy(&l0, sect + i, sizeof(uint32_t));
723 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
724 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
725 sys::swapByteOrder(l0);
726 sys::swapByteOrder(l1);
727 }
728 DumpLiteral8(O, l0, l1, d);
729 }
730}
731
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000732static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
733 outs() << format("0x%08" PRIx32, l0) << " ";
734 outs() << format("0x%08" PRIx32, l1) << " ";
735 outs() << format("0x%08" PRIx32, l2) << " ";
736 outs() << format("0x%08" PRIx32, l3) << "\n";
737}
738
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000739static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
740 uint32_t sect_size, uint64_t sect_addr,
741 bool print_addresses) {
742 for (uint32_t i = 0; i < sect_size; i += 16) {
743 if (print_addresses) {
744 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000745 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000746 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000747 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000748 }
749 uint32_t l0, l1, l2, l3;
750 memcpy(&l0, sect + i, sizeof(uint32_t));
751 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
752 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
753 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
754 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
755 sys::swapByteOrder(l0);
756 sys::swapByteOrder(l1);
757 sys::swapByteOrder(l2);
758 sys::swapByteOrder(l3);
759 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000760 DumpLiteral16(l0, l1, l2, l3);
761 }
762}
763
764static void DumpLiteralPointerSection(MachOObjectFile *O,
765 const SectionRef &Section,
766 const char *sect, uint32_t sect_size,
767 uint64_t sect_addr,
768 bool print_addresses) {
769 // Collect the literal sections in this Mach-O file.
770 std::vector<SectionRef> LiteralSections;
771 for (const SectionRef &Section : O->sections()) {
772 DataRefImpl Ref = Section.getRawDataRefImpl();
773 uint32_t section_type;
774 if (O->is64Bit()) {
775 const MachO::section_64 Sec = O->getSection64(Ref);
776 section_type = Sec.flags & MachO::SECTION_TYPE;
777 } else {
778 const MachO::section Sec = O->getSection(Ref);
779 section_type = Sec.flags & MachO::SECTION_TYPE;
780 }
781 if (section_type == MachO::S_CSTRING_LITERALS ||
782 section_type == MachO::S_4BYTE_LITERALS ||
783 section_type == MachO::S_8BYTE_LITERALS ||
784 section_type == MachO::S_16BYTE_LITERALS)
785 LiteralSections.push_back(Section);
786 }
787
788 // Set the size of the literal pointer.
789 uint32_t lp_size = O->is64Bit() ? 8 : 4;
790
Eric Christopher572e03a2015-06-19 01:53:21 +0000791 // Collect the external relocation symbols for the literal pointers.
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000792 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
793 for (const RelocationRef &Reloc : Section.relocations()) {
794 DataRefImpl Rel;
795 MachO::any_relocation_info RE;
796 bool isExtern = false;
797 Rel = Reloc.getRawDataRefImpl();
798 RE = O->getRelocation(Rel);
799 isExtern = O->getPlainRelocationExternal(RE);
800 if (isExtern) {
801 uint64_t RelocOffset;
802 Reloc.getOffset(RelocOffset);
803 symbol_iterator RelocSym = Reloc.getSymbol();
804 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
805 }
806 }
807 array_pod_sort(Relocs.begin(), Relocs.end());
808
809 // Dump each literal pointer.
810 for (uint32_t i = 0; i < sect_size; i += lp_size) {
811 if (print_addresses) {
812 if (O->is64Bit())
813 outs() << format("%016" PRIx64, sect_addr + i) << " ";
814 else
815 outs() << format("%08" PRIx64, sect_addr + i) << " ";
816 }
817 uint64_t lp;
818 if (O->is64Bit()) {
819 memcpy(&lp, sect + i, sizeof(uint64_t));
820 if (O->isLittleEndian() != sys::IsLittleEndianHost)
821 sys::swapByteOrder(lp);
822 } else {
823 uint32_t li;
824 memcpy(&li, sect + i, sizeof(uint32_t));
825 if (O->isLittleEndian() != sys::IsLittleEndianHost)
826 sys::swapByteOrder(li);
827 lp = li;
828 }
829
830 // First look for an external relocation entry for this literal pointer.
David Blaikie33dd45d02015-03-23 18:39:02 +0000831 auto Reloc = std::find_if(
832 Relocs.begin(), Relocs.end(),
833 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
834 if (Reloc != Relocs.end()) {
835 symbol_iterator RelocSym = Reloc->second;
836 StringRef SymName;
837 RelocSym->getName(SymName);
838 outs() << "external relocation entry for symbol:" << SymName << "\n";
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000839 continue;
David Blaikie33dd45d02015-03-23 18:39:02 +0000840 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000841
842 // For local references see what the section the literal pointer points to.
David Blaikie33dd45d02015-03-23 18:39:02 +0000843 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
844 [&](const SectionRef &R) {
845 return lp >= R.getAddress() &&
846 lp < R.getAddress() + R.getSize();
847 });
848 if (Sect == LiteralSections.end()) {
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000849 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
David Blaikie33dd45d02015-03-23 18:39:02 +0000850 continue;
851 }
852
853 uint64_t SectAddress = Sect->getAddress();
854 uint64_t SectSize = Sect->getSize();
855
856 StringRef SectName;
857 Sect->getName(SectName);
858 DataRefImpl Ref = Sect->getRawDataRefImpl();
859 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
860 outs() << SegmentName << ":" << SectName << ":";
861
862 uint32_t section_type;
863 if (O->is64Bit()) {
864 const MachO::section_64 Sec = O->getSection64(Ref);
865 section_type = Sec.flags & MachO::SECTION_TYPE;
866 } else {
867 const MachO::section Sec = O->getSection(Ref);
868 section_type = Sec.flags & MachO::SECTION_TYPE;
869 }
870
871 StringRef BytesStr;
872 Sect->getContents(BytesStr);
873 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
874
875 switch (section_type) {
876 case MachO::S_CSTRING_LITERALS:
877 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
878 i++) {
879 DumpCstringChar(Contents[i]);
880 }
881 outs() << "\n";
882 break;
883 case MachO::S_4BYTE_LITERALS:
884 float f;
885 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
886 uint32_t l;
887 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
888 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
889 sys::swapByteOrder(f);
890 sys::swapByteOrder(l);
891 }
892 DumpLiteral4(l, f);
893 break;
894 case MachO::S_8BYTE_LITERALS: {
895 double d;
896 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
897 uint32_t l0, l1;
898 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
899 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
900 sizeof(uint32_t));
901 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
902 sys::swapByteOrder(f);
903 sys::swapByteOrder(l0);
904 sys::swapByteOrder(l1);
905 }
906 DumpLiteral8(O, l0, l1, d);
907 break;
908 }
909 case MachO::S_16BYTE_LITERALS: {
910 uint32_t l0, l1, l2, l3;
911 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
912 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
913 sizeof(uint32_t));
914 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
915 sizeof(uint32_t));
916 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
917 sizeof(uint32_t));
918 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
919 sys::swapByteOrder(l0);
920 sys::swapByteOrder(l1);
921 sys::swapByteOrder(l2);
922 sys::swapByteOrder(l3);
923 }
924 DumpLiteral16(l0, l1, l2, l3);
925 break;
926 }
927 }
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000928 }
929}
930
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000931static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
932 uint32_t sect_size, uint64_t sect_addr,
933 SymbolAddressMap *AddrMap,
934 bool verbose) {
935 uint32_t stride;
936 if (O->is64Bit())
937 stride = sizeof(uint64_t);
938 else
939 stride = sizeof(uint32_t);
940 for (uint32_t i = 0; i < sect_size; i += stride) {
941 const char *SymbolName = nullptr;
942 if (O->is64Bit()) {
943 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
944 uint64_t pointer_value;
945 memcpy(&pointer_value, sect + i, stride);
946 if (O->isLittleEndian() != sys::IsLittleEndianHost)
947 sys::swapByteOrder(pointer_value);
948 outs() << format("0x%016" PRIx64, pointer_value);
949 if (verbose)
950 SymbolName = GuessSymbolName(pointer_value, AddrMap);
951 } else {
952 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
953 uint32_t pointer_value;
954 memcpy(&pointer_value, sect + i, stride);
955 if (O->isLittleEndian() != sys::IsLittleEndianHost)
956 sys::swapByteOrder(pointer_value);
957 outs() << format("0x%08" PRIx32, pointer_value);
958 if (verbose)
959 SymbolName = GuessSymbolName(pointer_value, AddrMap);
960 }
961 if (SymbolName)
962 outs() << " " << SymbolName;
963 outs() << "\n";
964 }
965}
966
967static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
968 uint32_t size, uint64_t addr) {
969 uint32_t cputype = O->getHeader().cputype;
970 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
971 uint32_t j;
972 for (uint32_t i = 0; i < size; i += j, addr += j) {
973 if (O->is64Bit())
974 outs() << format("%016" PRIx64, addr) << "\t";
975 else
Kevin Enderbyf0640752015-03-13 17:56:32 +0000976 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000977 for (j = 0; j < 16 && i + j < size; j++) {
978 uint8_t byte_word = *(sect + i + j);
979 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
980 }
981 outs() << "\n";
982 }
983 } else {
984 uint32_t j;
985 for (uint32_t i = 0; i < size; i += j, addr += j) {
986 if (O->is64Bit())
987 outs() << format("%016" PRIx64, addr) << "\t";
988 else
989 outs() << format("%08" PRIx64, sect) << "\t";
990 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
991 j += sizeof(int32_t)) {
992 if (i + j + sizeof(int32_t) < size) {
993 uint32_t long_word;
994 memcpy(&long_word, sect + i + j, sizeof(int32_t));
995 if (O->isLittleEndian() != sys::IsLittleEndianHost)
996 sys::swapByteOrder(long_word);
997 outs() << format("%08" PRIx32, long_word) << " ";
998 } else {
999 for (uint32_t k = 0; i + j + k < size; k++) {
1000 uint8_t byte_word = *(sect + i + j);
1001 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1002 }
1003 }
1004 }
1005 outs() << "\n";
1006 }
1007 }
1008}
1009
Kevin Enderby95df54c2015-02-04 01:01:38 +00001010static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1011 StringRef DisSegName, StringRef DisSectName);
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001012static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1013 uint32_t size, uint32_t addr);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001014
1015static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1016 bool verbose) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001017 SymbolAddressMap AddrMap;
1018 if (verbose)
1019 CreateSymbolAddressMap(O, &AddrMap);
1020
1021 for (unsigned i = 0; i < DumpSections.size(); ++i) {
1022 StringRef DumpSection = DumpSections[i];
1023 std::pair<StringRef, StringRef> DumpSegSectName;
1024 DumpSegSectName = DumpSection.split(',');
1025 StringRef DumpSegName, DumpSectName;
1026 if (DumpSegSectName.second.size()) {
1027 DumpSegName = DumpSegSectName.first;
1028 DumpSectName = DumpSegSectName.second;
1029 } else {
1030 DumpSegName = "";
1031 DumpSectName = DumpSegSectName.first;
1032 }
1033 for (const SectionRef &Section : O->sections()) {
1034 StringRef SectName;
1035 Section.getName(SectName);
1036 DataRefImpl Ref = Section.getRawDataRefImpl();
1037 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1038 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1039 (SectName == DumpSectName)) {
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001040
Kevin Enderby95df54c2015-02-04 01:01:38 +00001041 uint32_t section_flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001042 if (O->is64Bit()) {
1043 const MachO::section_64 Sec = O->getSection64(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001044 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001045
1046 } else {
1047 const MachO::section Sec = O->getSection(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001048 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001049 }
Kevin Enderby95df54c2015-02-04 01:01:38 +00001050 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001051
1052 StringRef BytesStr;
1053 Section.getContents(BytesStr);
1054 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1055 uint32_t sect_size = BytesStr.size();
1056 uint64_t sect_addr = Section.getAddress();
1057
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001058 if (Raw) {
1059 outs().write(BytesStr.data(), BytesStr.size());
1060 continue;
1061 }
1062
1063 outs() << "Contents of (" << SegName << "," << SectName
1064 << ") section\n";
1065
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001066 if (verbose) {
Kevin Enderby95df54c2015-02-04 01:01:38 +00001067 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1068 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1069 DisassembleMachO(Filename, O, SegName, SectName);
1070 continue;
1071 }
Kevin Enderbycd66be52015-03-11 22:06:32 +00001072 if (SegName == "__TEXT" && SectName == "__info_plist") {
1073 outs() << sect;
1074 continue;
1075 }
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001076 if (SegName == "__OBJC" && SectName == "__protocol") {
1077 DumpProtocolSection(O, sect, sect_size, sect_addr);
1078 continue;
1079 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001080 switch (section_type) {
1081 case MachO::S_REGULAR:
1082 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1083 break;
1084 case MachO::S_ZEROFILL:
1085 outs() << "zerofill section and has no contents in the file\n";
1086 break;
Kevin Enderby10ba0412015-02-04 21:38:42 +00001087 case MachO::S_CSTRING_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001088 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby10ba0412015-02-04 21:38:42 +00001089 break;
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001090 case MachO::S_4BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001091 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001092 break;
1093 case MachO::S_8BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001094 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001095 break;
1096 case MachO::S_16BYTE_LITERALS:
Kevin Enderby0fc11822015-04-01 20:57:01 +00001097 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1098 break;
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001099 case MachO::S_LITERAL_POINTERS:
1100 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001101 !NoLeadingAddr);
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001102 break;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001103 case MachO::S_MOD_INIT_FUNC_POINTERS:
1104 case MachO::S_MOD_TERM_FUNC_POINTERS:
1105 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1106 verbose);
1107 break;
1108 default:
1109 outs() << "Unknown section type ("
1110 << format("0x%08" PRIx32, section_type) << ")\n";
1111 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1112 break;
1113 }
1114 } else {
1115 if (section_type == MachO::S_ZEROFILL)
1116 outs() << "zerofill section and has no contents in the file\n";
1117 else
1118 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1119 }
1120 }
1121 }
1122 }
1123}
1124
Kevin Enderbycd66be52015-03-11 22:06:32 +00001125static void DumpInfoPlistSectionContents(StringRef Filename,
1126 MachOObjectFile *O) {
1127 for (const SectionRef &Section : O->sections()) {
1128 StringRef SectName;
1129 Section.getName(SectName);
1130 DataRefImpl Ref = Section.getRawDataRefImpl();
1131 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1132 if (SegName == "__TEXT" && SectName == "__info_plist") {
1133 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1134 StringRef BytesStr;
1135 Section.getContents(BytesStr);
1136 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1137 outs() << sect;
1138 return;
1139 }
1140 }
1141}
1142
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001143// checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1144// and if it is and there is a list of architecture flags is specified then
1145// check to make sure this Mach-O file is one of those architectures or all
1146// architectures were specified. If not then an error is generated and this
1147// routine returns false. Else it returns true.
1148static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1149 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1150 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1151 bool ArchFound = false;
1152 MachO::mach_header H;
1153 MachO::mach_header_64 H_64;
1154 Triple T;
1155 if (MachO->is64Bit()) {
1156 H_64 = MachO->MachOObjectFile::getHeader64();
1157 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1158 } else {
1159 H = MachO->MachOObjectFile::getHeader();
1160 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1161 }
1162 unsigned i;
1163 for (i = 0; i < ArchFlags.size(); ++i) {
1164 if (ArchFlags[i] == T.getArchName())
1165 ArchFound = true;
1166 break;
1167 }
1168 if (!ArchFound) {
1169 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1170 << "architecture: " + ArchFlags[i] + "\n";
1171 return false;
1172 }
1173 }
1174 return true;
1175}
1176
Kevin Enderby0fc11822015-04-01 20:57:01 +00001177static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1178
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001179// ProcessMachO() is passed a single opened Mach-O file, which may be an
1180// archive member and or in a slice of a universal file. It prints the
1181// the file name and header info and then processes it according to the
1182// command line options.
1183static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1184 StringRef ArchiveMemberName = StringRef(),
1185 StringRef ArchitectureName = StringRef()) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001186 // If we are doing some processing here on the Mach-O file print the header
1187 // info. And don't print it otherwise like in the case of printing the
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001188 // UniversalHeaders or ArchiveHeaders.
Kevin Enderby131d1772015-01-09 19:22:37 +00001189 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001190 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
Kevin Enderby0fc11822015-04-01 20:57:01 +00001191 DylibsUsed || DylibId || ObjcMetaData ||
1192 (DumpSections.size() != 0 && !Raw)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001193 outs() << Filename;
1194 if (!ArchiveMemberName.empty())
1195 outs() << '(' << ArchiveMemberName << ')';
1196 if (!ArchitectureName.empty())
1197 outs() << " (architecture " << ArchitectureName << ")";
1198 outs() << ":\n";
1199 }
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001200
1201 if (Disassemble)
Kevin Enderby95df54c2015-02-04 01:01:38 +00001202 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001203 if (IndirectSymbols)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001204 PrintIndirectSymbols(MachOOF, !NonVerbose);
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001205 if (DataInCode)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001206 PrintDataInCodeTable(MachOOF, !NonVerbose);
Kevin Enderby9a509442015-01-27 21:28:24 +00001207 if (LinkOptHints)
1208 PrintLinkOptHints(MachOOF);
Kevin Enderby98da6132015-01-20 21:47:46 +00001209 if (Relocations)
1210 PrintRelocations(MachOOF);
1211 if (SectionHeaders)
1212 PrintSectionHeaders(MachOOF);
1213 if (SectionContents)
1214 PrintSectionContents(MachOOF);
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001215 if (DumpSections.size() != 0)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001216 DumpSectionContents(Filename, MachOOF, !NonVerbose);
Kevin Enderbycd66be52015-03-11 22:06:32 +00001217 if (InfoPlist)
1218 DumpInfoPlistSectionContents(Filename, MachOOF);
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001219 if (DylibsUsed)
1220 PrintDylibs(MachOOF, false);
1221 if (DylibId)
1222 PrintDylibs(MachOOF, true);
Kevin Enderby98da6132015-01-20 21:47:46 +00001223 if (SymbolTable)
1224 PrintSymbolTable(MachOOF);
1225 if (UnwindInfo)
1226 printMachOUnwindInfo(MachOOF);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001227 if (PrivateHeaders)
1228 printMachOFileHeader(MachOOF);
Kevin Enderby0fc11822015-04-01 20:57:01 +00001229 if (ObjcMetaData)
1230 printObjcMetaData(MachOOF, !NonVerbose);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001231 if (ExportsTrie)
1232 printExportsTrie(MachOOF);
1233 if (Rebase)
1234 printRebaseTable(MachOOF);
1235 if (Bind)
1236 printBindTable(MachOOF);
1237 if (LazyBind)
1238 printLazyBindTable(MachOOF);
1239 if (WeakBind)
1240 printWeakBindTable(MachOOF);
1241}
1242
Kevin Enderby131d1772015-01-09 19:22:37 +00001243// printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1244static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1245 outs() << " cputype (" << cputype << ")\n";
1246 outs() << " cpusubtype (" << cpusubtype << ")\n";
1247}
1248
1249// printCPUType() helps print_fat_headers by printing the cputype and
1250// pusubtype (symbolically for the one's it knows about).
1251static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1252 switch (cputype) {
1253 case MachO::CPU_TYPE_I386:
1254 switch (cpusubtype) {
1255 case MachO::CPU_SUBTYPE_I386_ALL:
1256 outs() << " cputype CPU_TYPE_I386\n";
1257 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1258 break;
1259 default:
1260 printUnknownCPUType(cputype, cpusubtype);
1261 break;
1262 }
1263 break;
1264 case MachO::CPU_TYPE_X86_64:
1265 switch (cpusubtype) {
1266 case MachO::CPU_SUBTYPE_X86_64_ALL:
1267 outs() << " cputype CPU_TYPE_X86_64\n";
1268 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1269 break;
1270 case MachO::CPU_SUBTYPE_X86_64_H:
1271 outs() << " cputype CPU_TYPE_X86_64\n";
1272 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1273 break;
1274 default:
1275 printUnknownCPUType(cputype, cpusubtype);
1276 break;
1277 }
1278 break;
1279 case MachO::CPU_TYPE_ARM:
1280 switch (cpusubtype) {
1281 case MachO::CPU_SUBTYPE_ARM_ALL:
1282 outs() << " cputype CPU_TYPE_ARM\n";
1283 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1284 break;
1285 case MachO::CPU_SUBTYPE_ARM_V4T:
1286 outs() << " cputype CPU_TYPE_ARM\n";
1287 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1288 break;
1289 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1290 outs() << " cputype CPU_TYPE_ARM\n";
1291 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1292 break;
1293 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1294 outs() << " cputype CPU_TYPE_ARM\n";
1295 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1296 break;
1297 case MachO::CPU_SUBTYPE_ARM_V6:
1298 outs() << " cputype CPU_TYPE_ARM\n";
1299 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1300 break;
1301 case MachO::CPU_SUBTYPE_ARM_V6M:
1302 outs() << " cputype CPU_TYPE_ARM\n";
1303 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1304 break;
1305 case MachO::CPU_SUBTYPE_ARM_V7:
1306 outs() << " cputype CPU_TYPE_ARM\n";
1307 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1308 break;
1309 case MachO::CPU_SUBTYPE_ARM_V7EM:
1310 outs() << " cputype CPU_TYPE_ARM\n";
1311 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1312 break;
1313 case MachO::CPU_SUBTYPE_ARM_V7K:
1314 outs() << " cputype CPU_TYPE_ARM\n";
1315 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1316 break;
1317 case MachO::CPU_SUBTYPE_ARM_V7M:
1318 outs() << " cputype CPU_TYPE_ARM\n";
1319 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1320 break;
1321 case MachO::CPU_SUBTYPE_ARM_V7S:
1322 outs() << " cputype CPU_TYPE_ARM\n";
1323 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1324 break;
1325 default:
1326 printUnknownCPUType(cputype, cpusubtype);
1327 break;
1328 }
1329 break;
1330 case MachO::CPU_TYPE_ARM64:
1331 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1332 case MachO::CPU_SUBTYPE_ARM64_ALL:
1333 outs() << " cputype CPU_TYPE_ARM64\n";
1334 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1335 break;
1336 default:
1337 printUnknownCPUType(cputype, cpusubtype);
1338 break;
1339 }
1340 break;
1341 default:
1342 printUnknownCPUType(cputype, cpusubtype);
1343 break;
1344 }
1345}
1346
1347static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1348 bool verbose) {
1349 outs() << "Fat headers\n";
1350 if (verbose)
1351 outs() << "fat_magic FAT_MAGIC\n";
1352 else
1353 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1354
1355 uint32_t nfat_arch = UB->getNumberOfObjects();
1356 StringRef Buf = UB->getData();
1357 uint64_t size = Buf.size();
1358 uint64_t big_size = sizeof(struct MachO::fat_header) +
1359 nfat_arch * sizeof(struct MachO::fat_arch);
1360 outs() << "nfat_arch " << UB->getNumberOfObjects();
1361 if (nfat_arch == 0)
1362 outs() << " (malformed, contains zero architecture types)\n";
1363 else if (big_size > size)
1364 outs() << " (malformed, architectures past end of file)\n";
1365 else
1366 outs() << "\n";
1367
1368 for (uint32_t i = 0; i < nfat_arch; ++i) {
1369 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1370 uint32_t cputype = OFA.getCPUType();
1371 uint32_t cpusubtype = OFA.getCPUSubType();
1372 outs() << "architecture ";
1373 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1374 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1375 uint32_t other_cputype = other_OFA.getCPUType();
1376 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
Kevin Enderby0512bd72015-01-09 21:55:03 +00001377 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
Kevin Enderby131d1772015-01-09 19:22:37 +00001378 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
Kevin Enderby0512bd72015-01-09 21:55:03 +00001379 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001380 outs() << "(illegal duplicate architecture) ";
1381 break;
Kevin Enderby0512bd72015-01-09 21:55:03 +00001382 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001383 }
1384 if (verbose) {
1385 outs() << OFA.getArchTypeName() << "\n";
1386 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1387 } else {
1388 outs() << i << "\n";
1389 outs() << " cputype " << cputype << "\n";
1390 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1391 << "\n";
1392 }
1393 if (verbose &&
1394 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1395 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1396 else
1397 outs() << " capabilities "
1398 << format("0x%" PRIx32,
1399 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1400 outs() << " offset " << OFA.getOffset();
1401 if (OFA.getOffset() > size)
1402 outs() << " (past end of file)";
1403 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1404 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1405 outs() << "\n";
1406 outs() << " size " << OFA.getSize();
1407 big_size = OFA.getOffset() + OFA.getSize();
1408 if (big_size > size)
1409 outs() << " (past end of file)";
1410 outs() << "\n";
1411 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1412 << ")\n";
1413 }
1414}
1415
Kevin Enderby13023a12015-01-15 23:19:11 +00001416static void printArchiveChild(Archive::Child &C, bool verbose,
1417 bool print_offset) {
1418 if (print_offset)
1419 outs() << C.getChildOffset() << "\t";
1420 sys::fs::perms Mode = C.getAccessMode();
1421 if (verbose) {
1422 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1423 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1424 outs() << "-";
1425 if (Mode & sys::fs::owner_read)
1426 outs() << "r";
1427 else
1428 outs() << "-";
1429 if (Mode & sys::fs::owner_write)
1430 outs() << "w";
1431 else
1432 outs() << "-";
1433 if (Mode & sys::fs::owner_exe)
1434 outs() << "x";
1435 else
1436 outs() << "-";
1437 if (Mode & sys::fs::group_read)
1438 outs() << "r";
1439 else
1440 outs() << "-";
1441 if (Mode & sys::fs::group_write)
1442 outs() << "w";
1443 else
1444 outs() << "-";
1445 if (Mode & sys::fs::group_exe)
1446 outs() << "x";
1447 else
1448 outs() << "-";
1449 if (Mode & sys::fs::others_read)
1450 outs() << "r";
1451 else
1452 outs() << "-";
1453 if (Mode & sys::fs::others_write)
1454 outs() << "w";
1455 else
1456 outs() << "-";
1457 if (Mode & sys::fs::others_exe)
1458 outs() << "x";
1459 else
1460 outs() << "-";
1461 } else {
1462 outs() << format("0%o ", Mode);
1463 }
1464
1465 unsigned UID = C.getUID();
1466 outs() << format("%3d/", UID);
1467 unsigned GID = C.getGID();
1468 outs() << format("%-3d ", GID);
Kevin Enderbyc12718932015-01-16 22:10:36 +00001469 uint64_t Size = C.getRawSize();
Kevin Enderby479ee612015-01-23 21:02:44 +00001470 outs() << format("%5" PRId64, Size) << " ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001471
1472 StringRef RawLastModified = C.getRawLastModified();
1473 if (verbose) {
1474 unsigned Seconds;
1475 if (RawLastModified.getAsInteger(10, Seconds))
1476 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1477 else {
1478 // Since cime(3) returns a 26 character string of the form:
1479 // "Sun Sep 16 01:03:52 1973\n\0"
1480 // just print 24 characters.
1481 time_t t = Seconds;
1482 outs() << format("%.24s ", ctime(&t));
1483 }
1484 } else {
1485 outs() << RawLastModified << " ";
1486 }
1487
1488 if (verbose) {
1489 ErrorOr<StringRef> NameOrErr = C.getName();
1490 if (NameOrErr.getError()) {
1491 StringRef RawName = C.getRawName();
1492 outs() << RawName << "\n";
1493 } else {
1494 StringRef Name = NameOrErr.get();
1495 outs() << Name << "\n";
1496 }
1497 } else {
1498 StringRef RawName = C.getRawName();
1499 outs() << RawName << "\n";
1500 }
1501}
1502
1503static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
1504 if (A->hasSymbolTable()) {
1505 Archive::child_iterator S = A->getSymbolTableChild();
1506 Archive::Child C = *S;
1507 printArchiveChild(C, verbose, print_offset);
1508 }
1509 for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E;
1510 ++I) {
1511 Archive::Child C = *I;
1512 printArchiveChild(C, verbose, print_offset);
1513 }
1514}
1515
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001516// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1517// -arch flags selecting just those slices as specified by them and also parses
1518// archive files. Then for each individual Mach-O file ProcessMachO() is
1519// called to process the file based on the command line options.
1520void llvm::ParseInputMachO(StringRef Filename) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001521 // Check for -arch all and verifiy the -arch flags are valid.
1522 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1523 if (ArchFlags[i] == "all") {
1524 ArchAll = true;
1525 } else {
1526 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1527 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1528 "'for the -arch option\n";
1529 return;
1530 }
1531 }
1532 }
1533
1534 // Attempt to open the binary.
1535 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1536 if (std::error_code EC = BinaryOrErr.getError()) {
1537 errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n";
Rafael Espindolade882cd2014-12-03 23:29:34 +00001538 return;
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001539 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001540 Binary &Bin = *BinaryOrErr.get().getBinary();
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001541
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001542 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1543 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001544 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001545 printArchiveHeaders(A, !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001546 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1547 I != E; ++I) {
1548 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary();
1549 if (ChildOrErr.getError())
1550 continue;
1551 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1552 if (!checkMachOAndArchFlags(O, Filename))
1553 return;
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001554 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001555 }
1556 }
1557 return;
1558 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001559 if (UniversalHeaders) {
1560 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
Kevin Enderbyf0640752015-03-13 17:56:32 +00001561 printMachOUniversalHeaders(UB, !NonVerbose);
Kevin Enderby131d1772015-01-09 19:22:37 +00001562 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001563 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1564 // If we have a list of architecture flags specified dump only those.
1565 if (!ArchAll && ArchFlags.size() != 0) {
1566 // Look for a slice in the universal binary that matches each ArchFlag.
1567 bool ArchFound;
1568 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1569 ArchFound = false;
1570 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1571 E = UB->end_objects();
1572 I != E; ++I) {
1573 if (ArchFlags[i] == I->getArchTypeName()) {
1574 ArchFound = true;
1575 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1576 I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001577 std::string ArchitectureName = "";
1578 if (ArchFlags.size() > 1)
1579 ArchitectureName = I->getArchTypeName();
1580 if (ObjOrErr) {
1581 ObjectFile &O = *ObjOrErr.get();
1582 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001583 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001584 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1585 I->getAsArchive()) {
1586 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001587 outs() << "Archive : " << Filename;
1588 if (!ArchitectureName.empty())
1589 outs() << " (architecture " << ArchitectureName << ")";
1590 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001591 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001592 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001593 for (Archive::child_iterator AI = A->child_begin(),
1594 AE = A->child_end();
1595 AI != AE; ++AI) {
1596 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1597 if (ChildOrErr.getError())
1598 continue;
1599 if (MachOObjectFile *O =
1600 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001601 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001602 }
1603 }
1604 }
1605 }
1606 if (!ArchFound) {
1607 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1608 << "architecture: " + ArchFlags[i] + "\n";
1609 return;
1610 }
1611 }
1612 return;
1613 }
1614 // No architecture flags were specified so if this contains a slice that
1615 // matches the host architecture dump only that.
1616 if (!ArchAll) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001617 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1618 E = UB->end_objects();
1619 I != E; ++I) {
Kevin Enderby0512bd72015-01-09 21:55:03 +00001620 if (MachOObjectFile::getHostArch().getArchName() ==
1621 I->getArchTypeName()) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001622 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001623 std::string ArchiveName;
1624 ArchiveName.clear();
1625 if (ObjOrErr) {
1626 ObjectFile &O = *ObjOrErr.get();
1627 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001628 ProcessMachO(Filename, MachOOF);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001629 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1630 I->getAsArchive()) {
1631 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001632 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001633 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001634 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001635 for (Archive::child_iterator AI = A->child_begin(),
1636 AE = A->child_end();
1637 AI != AE; ++AI) {
1638 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1639 if (ChildOrErr.getError())
1640 continue;
1641 if (MachOObjectFile *O =
1642 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001643 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001644 }
1645 }
1646 return;
1647 }
1648 }
1649 }
1650 // Either all architectures have been specified or none have been specified
1651 // and this does not contain the host architecture so dump all the slices.
1652 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1653 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1654 E = UB->end_objects();
1655 I != E; ++I) {
1656 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001657 std::string ArchitectureName = "";
1658 if (moreThanOneArch)
1659 ArchitectureName = I->getArchTypeName();
1660 if (ObjOrErr) {
1661 ObjectFile &Obj = *ObjOrErr.get();
1662 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001663 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001664 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1665 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001666 outs() << "Archive : " << Filename;
1667 if (!ArchitectureName.empty())
1668 outs() << " (architecture " << ArchitectureName << ")";
1669 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001670 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001671 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001672 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1673 AI != AE; ++AI) {
1674 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1675 if (ChildOrErr.getError())
1676 continue;
1677 if (MachOObjectFile *O =
1678 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1679 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001680 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1681 ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001682 }
1683 }
1684 }
1685 }
1686 return;
1687 }
1688 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1689 if (!checkMachOAndArchFlags(O, Filename))
1690 return;
1691 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001692 ProcessMachO(Filename, MachOOF);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001693 } else
1694 errs() << "llvm-objdump: '" << Filename << "': "
1695 << "Object is not a Mach-O file type.\n";
1696 } else
1697 errs() << "llvm-objdump: '" << Filename << "': "
1698 << "Unrecognized file type.\n";
Rafael Espindola9b709252013-04-13 01:45:40 +00001699}
1700
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001701typedef std::pair<uint64_t, const char *> BindInfoEntry;
1702typedef std::vector<BindInfoEntry> BindTable;
1703typedef BindTable::iterator bind_table_iterator;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001704
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001705// The block of info used by the Symbolizer call backs.
1706struct DisassembleInfo {
1707 bool verbose;
1708 MachOObjectFile *O;
1709 SectionRef S;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001710 SymbolAddressMap *AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001711 std::vector<SectionRef> *Sections;
1712 const char *class_name;
1713 const char *selector_name;
1714 char *method;
Kevin Enderby04bf6932014-10-28 23:39:46 +00001715 char *demangled_name;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001716 uint64_t adrp_addr;
1717 uint32_t adrp_inst;
Kevin Enderby078be602014-10-23 19:53:12 +00001718 BindTable *bindtable;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001719};
1720
1721// SymbolizerGetOpInfo() is the operand information call back function.
1722// This is called to get the symbolic information for operand(s) of an
1723// instruction when it is being done. This routine does this from
1724// the relocation information, symbol table, etc. That block of information
1725// is a pointer to the struct DisassembleInfo that was passed when the
1726// disassembler context was created and passed to back to here when
1727// called back by the disassembler for instruction operands that could have
1728// relocation information. The address of the instruction containing operand is
1729// at the Pc parameter. The immediate value the operand has is passed in
1730// op_info->Value and is at Offset past the start of the instruction and has a
1731// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1732// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1733// names and addends of the symbolic expression to add for the operand. The
1734// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1735// information is returned then this function returns 1 else it returns 0.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00001736static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1737 uint64_t Size, int TagType, void *TagBuf) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001738 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1739 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001740 uint64_t value = op_info->Value;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001741
1742 // Make sure all fields returned are zero if we don't set them.
1743 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1744 op_info->Value = value;
1745
1746 // If the TagType is not the value 1 which it code knows about or if no
1747 // verbose symbolic information is wanted then just return 0, indicating no
1748 // information is being returned.
David Blaikie33dd45d02015-03-23 18:39:02 +00001749 if (TagType != 1 || !info->verbose)
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001750 return 0;
1751
1752 unsigned int Arch = info->O->getArch();
1753 if (Arch == Triple::x86) {
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001754 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1755 return 0;
1756 // First search the section's relocation entries (if any) for an entry
1757 // for this section offset.
1758 uint32_t sect_addr = info->S.getAddress();
1759 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1760 bool reloc_found = false;
1761 DataRefImpl Rel;
1762 MachO::any_relocation_info RE;
1763 bool isExtern = false;
1764 SymbolRef Symbol;
1765 bool r_scattered = false;
1766 uint32_t r_value, pair_r_value, r_type;
1767 for (const RelocationRef &Reloc : info->S.relocations()) {
1768 uint64_t RelocOffset;
1769 Reloc.getOffset(RelocOffset);
1770 if (RelocOffset == sect_offset) {
1771 Rel = Reloc.getRawDataRefImpl();
1772 RE = info->O->getRelocation(Rel);
Kevin Enderby3eb73e12014-11-11 19:16:45 +00001773 r_type = info->O->getAnyRelocationType(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001774 r_scattered = info->O->isRelocationScattered(RE);
1775 if (r_scattered) {
1776 r_value = info->O->getScatteredRelocationValue(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001777 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1778 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1779 DataRefImpl RelNext = Rel;
1780 info->O->moveRelocationNext(RelNext);
1781 MachO::any_relocation_info RENext;
1782 RENext = info->O->getRelocation(RelNext);
1783 if (info->O->isRelocationScattered(RENext))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001784 pair_r_value = info->O->getScatteredRelocationValue(RENext);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001785 else
1786 return 0;
1787 }
1788 } else {
1789 isExtern = info->O->getPlainRelocationExternal(RE);
1790 if (isExtern) {
1791 symbol_iterator RelocSym = Reloc.getSymbol();
1792 Symbol = *RelocSym;
1793 }
1794 }
1795 reloc_found = true;
1796 break;
1797 }
1798 }
1799 if (reloc_found && isExtern) {
1800 StringRef SymName;
1801 Symbol.getName(SymName);
1802 const char *name = SymName.data();
1803 op_info->AddSymbol.Present = 1;
1804 op_info->AddSymbol.Name = name;
1805 // For i386 extern relocation entries the value in the instruction is
1806 // the offset from the symbol, and value is already set in op_info->Value.
1807 return 1;
1808 }
1809 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1810 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001811 const char *add = GuessSymbolName(r_value, info->AddrMap);
1812 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001813 uint32_t offset = value - (r_value - pair_r_value);
1814 op_info->AddSymbol.Present = 1;
1815 if (add != nullptr)
1816 op_info->AddSymbol.Name = add;
1817 else
1818 op_info->AddSymbol.Value = r_value;
1819 op_info->SubtractSymbol.Present = 1;
1820 if (sub != nullptr)
1821 op_info->SubtractSymbol.Name = sub;
1822 else
1823 op_info->SubtractSymbol.Value = pair_r_value;
1824 op_info->Value = offset;
1825 return 1;
1826 }
1827 // TODO:
1828 // Second search the external relocation entries of a fully linked image
1829 // (if any) for an entry that matches this segment offset.
1830 // uint32_t seg_offset = (Pc + Offset);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001831 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001832 }
1833 if (Arch == Triple::x86_64) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001834 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1835 return 0;
1836 // First search the section's relocation entries (if any) for an entry
1837 // for this section offset.
Rafael Espindola80291272014-10-08 15:28:58 +00001838 uint64_t sect_addr = info->S.getAddress();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001839 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1840 bool reloc_found = false;
1841 DataRefImpl Rel;
1842 MachO::any_relocation_info RE;
1843 bool isExtern = false;
1844 SymbolRef Symbol;
1845 for (const RelocationRef &Reloc : info->S.relocations()) {
1846 uint64_t RelocOffset;
1847 Reloc.getOffset(RelocOffset);
1848 if (RelocOffset == sect_offset) {
1849 Rel = Reloc.getRawDataRefImpl();
1850 RE = info->O->getRelocation(Rel);
1851 // NOTE: Scattered relocations don't exist on x86_64.
1852 isExtern = info->O->getPlainRelocationExternal(RE);
1853 if (isExtern) {
1854 symbol_iterator RelocSym = Reloc.getSymbol();
1855 Symbol = *RelocSym;
1856 }
1857 reloc_found = true;
1858 break;
1859 }
1860 }
1861 if (reloc_found && isExtern) {
1862 // The Value passed in will be adjusted by the Pc if the instruction
1863 // adds the Pc. But for x86_64 external relocation entries the Value
1864 // is the offset from the external symbol.
1865 if (info->O->getAnyRelocationPCRel(RE))
1866 op_info->Value -= Pc + Offset + Size;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001867 StringRef SymName;
1868 Symbol.getName(SymName);
1869 const char *name = SymName.data();
1870 unsigned Type = info->O->getAnyRelocationType(RE);
1871 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1872 DataRefImpl RelNext = Rel;
1873 info->O->moveRelocationNext(RelNext);
1874 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1875 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1876 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1877 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1878 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1879 op_info->SubtractSymbol.Present = 1;
1880 op_info->SubtractSymbol.Name = name;
1881 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1882 Symbol = *RelocSymNext;
1883 StringRef SymNameNext;
1884 Symbol.getName(SymNameNext);
1885 name = SymNameNext.data();
1886 }
1887 }
1888 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1889 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1890 op_info->AddSymbol.Present = 1;
1891 op_info->AddSymbol.Name = name;
1892 return 1;
1893 }
1894 // TODO:
1895 // Second search the external relocation entries of a fully linked image
1896 // (if any) for an entry that matches this segment offset.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001897 // uint64_t seg_offset = (Pc + Offset);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001898 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001899 }
1900 if (Arch == Triple::arm) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001901 if (Offset != 0 || (Size != 4 && Size != 2))
1902 return 0;
1903 // First search the section's relocation entries (if any) for an entry
1904 // for this section offset.
1905 uint32_t sect_addr = info->S.getAddress();
1906 uint32_t sect_offset = (Pc + Offset) - sect_addr;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001907 DataRefImpl Rel;
1908 MachO::any_relocation_info RE;
1909 bool isExtern = false;
1910 SymbolRef Symbol;
1911 bool r_scattered = false;
1912 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
David Blaikie33dd45d02015-03-23 18:39:02 +00001913 auto Reloc =
1914 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1915 [&](const RelocationRef &Reloc) {
1916 uint64_t RelocOffset;
1917 Reloc.getOffset(RelocOffset);
1918 return RelocOffset == sect_offset;
1919 });
1920
1921 if (Reloc == info->S.relocations().end())
1922 return 0;
1923
1924 Rel = Reloc->getRawDataRefImpl();
1925 RE = info->O->getRelocation(Rel);
1926 r_length = info->O->getAnyRelocationLength(RE);
1927 r_scattered = info->O->isRelocationScattered(RE);
1928 if (r_scattered) {
1929 r_value = info->O->getScatteredRelocationValue(RE);
1930 r_type = info->O->getScatteredRelocationType(RE);
1931 } else {
1932 r_type = info->O->getAnyRelocationType(RE);
1933 isExtern = info->O->getPlainRelocationExternal(RE);
1934 if (isExtern) {
1935 symbol_iterator RelocSym = Reloc->getSymbol();
1936 Symbol = *RelocSym;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001937 }
1938 }
David Blaikie33dd45d02015-03-23 18:39:02 +00001939 if (r_type == MachO::ARM_RELOC_HALF ||
1940 r_type == MachO::ARM_RELOC_SECTDIFF ||
1941 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
1942 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1943 DataRefImpl RelNext = Rel;
1944 info->O->moveRelocationNext(RelNext);
1945 MachO::any_relocation_info RENext;
1946 RENext = info->O->getRelocation(RelNext);
1947 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
1948 if (info->O->isRelocationScattered(RENext))
1949 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1950 }
1951
1952 if (isExtern) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001953 StringRef SymName;
1954 Symbol.getName(SymName);
1955 const char *name = SymName.data();
1956 op_info->AddSymbol.Present = 1;
1957 op_info->AddSymbol.Name = name;
Sylvestre Ledru648cced2015-02-05 17:00:23 +00001958 switch (r_type) {
1959 case MachO::ARM_RELOC_HALF:
1960 if ((r_length & 0x1) == 1) {
1961 op_info->Value = value << 16 | other_half;
1962 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1963 } else {
1964 op_info->Value = other_half << 16 | value;
1965 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Sylvestre Ledrufe0c7ad2015-02-05 16:35:44 +00001966 }
Sylvestre Ledru648cced2015-02-05 17:00:23 +00001967 break;
1968 default:
1969 break;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001970 }
1971 return 1;
1972 }
1973 // If we have a branch that is not an external relocation entry then
1974 // return 0 so the code in tryAddingSymbolicOperand() can use the
1975 // SymbolLookUp call back with the branch target address to look up the
1976 // symbol and possiblity add an annotation for a symbol stub.
David Blaikie33dd45d02015-03-23 18:39:02 +00001977 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
1978 r_type == MachO::ARM_THUMB_RELOC_BR22))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001979 return 0;
1980
1981 uint32_t offset = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001982 if (r_type == MachO::ARM_RELOC_HALF ||
1983 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1984 if ((r_length & 0x1) == 1)
1985 value = value << 16 | other_half;
1986 else
1987 value = other_half << 16 | value;
1988 }
1989 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
1990 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
1991 offset = value - r_value;
1992 value = r_value;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001993 }
1994
David Blaikie33dd45d02015-03-23 18:39:02 +00001995 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001996 if ((r_length & 0x1) == 1)
1997 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1998 else
1999 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002000 const char *add = GuessSymbolName(r_value, info->AddrMap);
2001 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002002 int32_t offset = value - (r_value - pair_r_value);
2003 op_info->AddSymbol.Present = 1;
2004 if (add != nullptr)
2005 op_info->AddSymbol.Name = add;
2006 else
2007 op_info->AddSymbol.Value = r_value;
2008 op_info->SubtractSymbol.Present = 1;
2009 if (sub != nullptr)
2010 op_info->SubtractSymbol.Name = sub;
2011 else
2012 op_info->SubtractSymbol.Value = pair_r_value;
2013 op_info->Value = offset;
2014 return 1;
2015 }
2016
Kevin Enderby930fdc72014-11-06 19:00:13 +00002017 op_info->AddSymbol.Present = 1;
2018 op_info->Value = offset;
David Blaikie33dd45d02015-03-23 18:39:02 +00002019 if (r_type == MachO::ARM_RELOC_HALF) {
2020 if ((r_length & 0x1) == 1)
2021 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2022 else
2023 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002024 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002025 const char *add = GuessSymbolName(value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002026 if (add != nullptr) {
2027 op_info->AddSymbol.Name = add;
2028 return 1;
2029 }
2030 op_info->AddSymbol.Value = value;
2031 return 1;
David Blaikie33dd45d02015-03-23 18:39:02 +00002032 }
2033 if (Arch == Triple::aarch64) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002034 if (Offset != 0 || Size != 4)
2035 return 0;
2036 // First search the section's relocation entries (if any) for an entry
2037 // for this section offset.
2038 uint64_t sect_addr = info->S.getAddress();
2039 uint64_t sect_offset = (Pc + Offset) - sect_addr;
David Blaikie33dd45d02015-03-23 18:39:02 +00002040 auto Reloc =
2041 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2042 [&](const RelocationRef &Reloc) {
2043 uint64_t RelocOffset;
2044 Reloc.getOffset(RelocOffset);
2045 return RelocOffset == sect_offset;
2046 });
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002047
David Blaikie33dd45d02015-03-23 18:39:02 +00002048 if (Reloc == info->S.relocations().end())
2049 return 0;
2050
2051 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2052 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2053 uint32_t r_type = info->O->getAnyRelocationType(RE);
2054 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2055 DataRefImpl RelNext = Rel;
2056 info->O->moveRelocationNext(RelNext);
2057 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2058 if (value == 0) {
2059 value = info->O->getPlainRelocationSymbolNum(RENext);
2060 op_info->Value = value;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002061 }
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002062 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002063 // NOTE: Scattered relocations don't exist on arm64.
2064 if (!info->O->getPlainRelocationExternal(RE))
2065 return 0;
2066 StringRef SymName;
2067 Reloc->getSymbol()->getName(SymName);
2068 const char *name = SymName.data();
2069 op_info->AddSymbol.Present = 1;
2070 op_info->AddSymbol.Name = name;
2071
2072 switch (r_type) {
2073 case MachO::ARM64_RELOC_PAGE21:
2074 /* @page */
2075 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2076 break;
2077 case MachO::ARM64_RELOC_PAGEOFF12:
2078 /* @pageoff */
2079 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2080 break;
2081 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2082 /* @gotpage */
2083 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2084 break;
2085 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2086 /* @gotpageoff */
2087 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2088 break;
2089 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2090 /* @tvlppage is not implemented in llvm-mc */
2091 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2092 break;
2093 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2094 /* @tvlppageoff is not implemented in llvm-mc */
2095 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2096 break;
2097 default:
2098 case MachO::ARM64_RELOC_BRANCH26:
2099 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2100 break;
2101 }
2102 return 1;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002103 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002104 return 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002105}
2106
Kevin Enderbybf246f52014-09-24 23:08:22 +00002107// GuessCstringPointer is passed the address of what might be a pointer to a
2108// literal string in a cstring section. If that address is in a cstring section
2109// it returns a pointer to that string. Else it returns nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002110static const char *GuessCstringPointer(uint64_t ReferenceValue,
2111 struct DisassembleInfo *info) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002112 for (const auto &Load : info->O->load_commands()) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00002113 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2114 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2115 for (unsigned J = 0; J < Seg.nsects; ++J) {
2116 MachO::section_64 Sec = info->O->getSection64(Load, J);
2117 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2118 if (section_type == MachO::S_CSTRING_LITERALS &&
2119 ReferenceValue >= Sec.addr &&
2120 ReferenceValue < Sec.addr + Sec.size) {
2121 uint64_t sect_offset = ReferenceValue - Sec.addr;
2122 uint64_t object_offset = Sec.offset + sect_offset;
2123 StringRef MachOContents = info->O->getData();
2124 uint64_t object_size = MachOContents.size();
2125 const char *object_addr = (const char *)MachOContents.data();
2126 if (object_offset < object_size) {
2127 const char *name = object_addr + object_offset;
2128 return name;
2129 } else {
2130 return nullptr;
2131 }
2132 }
2133 }
2134 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2135 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2136 for (unsigned J = 0; J < Seg.nsects; ++J) {
2137 MachO::section Sec = info->O->getSection(Load, J);
2138 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2139 if (section_type == MachO::S_CSTRING_LITERALS &&
2140 ReferenceValue >= Sec.addr &&
2141 ReferenceValue < Sec.addr + Sec.size) {
2142 uint64_t sect_offset = ReferenceValue - Sec.addr;
2143 uint64_t object_offset = Sec.offset + sect_offset;
2144 StringRef MachOContents = info->O->getData();
2145 uint64_t object_size = MachOContents.size();
2146 const char *object_addr = (const char *)MachOContents.data();
2147 if (object_offset < object_size) {
2148 const char *name = object_addr + object_offset;
2149 return name;
2150 } else {
2151 return nullptr;
2152 }
2153 }
2154 }
2155 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00002156 }
2157 return nullptr;
2158}
2159
Kevin Enderby85974882014-09-26 22:20:44 +00002160// GuessIndirectSymbol returns the name of the indirect symbol for the
2161// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2162// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2163// symbol name being referenced by the stub or pointer.
2164static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2165 struct DisassembleInfo *info) {
Kevin Enderby85974882014-09-26 22:20:44 +00002166 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2167 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002168 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby85974882014-09-26 22:20:44 +00002169 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2170 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2171 for (unsigned J = 0; J < Seg.nsects; ++J) {
2172 MachO::section_64 Sec = info->O->getSection64(Load, J);
2173 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2174 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2175 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2176 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2177 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2178 section_type == MachO::S_SYMBOL_STUBS) &&
2179 ReferenceValue >= Sec.addr &&
2180 ReferenceValue < Sec.addr + Sec.size) {
2181 uint32_t stride;
2182 if (section_type == MachO::S_SYMBOL_STUBS)
2183 stride = Sec.reserved2;
2184 else
2185 stride = 8;
2186 if (stride == 0)
2187 return nullptr;
2188 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2189 if (index < Dysymtab.nindirectsyms) {
2190 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002191 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002192 if (indirect_symbol < Symtab.nsyms) {
2193 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2194 SymbolRef Symbol = *Sym;
2195 StringRef SymName;
2196 Symbol.getName(SymName);
2197 const char *name = SymName.data();
2198 return name;
2199 }
2200 }
2201 }
2202 }
2203 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2204 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2205 for (unsigned J = 0; J < Seg.nsects; ++J) {
2206 MachO::section Sec = info->O->getSection(Load, J);
2207 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2208 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2209 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2210 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2211 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2212 section_type == MachO::S_SYMBOL_STUBS) &&
2213 ReferenceValue >= Sec.addr &&
2214 ReferenceValue < Sec.addr + Sec.size) {
2215 uint32_t stride;
2216 if (section_type == MachO::S_SYMBOL_STUBS)
2217 stride = Sec.reserved2;
2218 else
2219 stride = 4;
2220 if (stride == 0)
2221 return nullptr;
2222 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2223 if (index < Dysymtab.nindirectsyms) {
2224 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002225 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002226 if (indirect_symbol < Symtab.nsyms) {
2227 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2228 SymbolRef Symbol = *Sym;
2229 StringRef SymName;
2230 Symbol.getName(SymName);
2231 const char *name = SymName.data();
2232 return name;
2233 }
2234 }
2235 }
2236 }
2237 }
Kevin Enderby85974882014-09-26 22:20:44 +00002238 }
2239 return nullptr;
2240}
2241
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002242// method_reference() is called passing it the ReferenceName that might be
2243// a reference it to an Objective-C method call. If so then it allocates and
2244// assembles a method call string with the values last seen and saved in
2245// the DisassembleInfo's class_name and selector_name fields. This is saved
2246// into the method field of the info and any previous string is free'ed.
2247// Then the class_name field in the info is set to nullptr. The method call
2248// string is set into ReferenceName and ReferenceType is set to
2249// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2250// then both ReferenceType and ReferenceName are left unchanged.
2251static void method_reference(struct DisassembleInfo *info,
2252 uint64_t *ReferenceType,
2253 const char **ReferenceName) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002254 unsigned int Arch = info->O->getArch();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002255 if (*ReferenceName != nullptr) {
2256 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002257 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002258 if (info->method != nullptr)
2259 free(info->method);
2260 if (info->class_name != nullptr) {
2261 info->method = (char *)malloc(5 + strlen(info->class_name) +
2262 strlen(info->selector_name));
2263 if (info->method != nullptr) {
2264 strcpy(info->method, "+[");
2265 strcat(info->method, info->class_name);
2266 strcat(info->method, " ");
2267 strcat(info->method, info->selector_name);
2268 strcat(info->method, "]");
2269 *ReferenceName = info->method;
2270 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2271 }
2272 } else {
2273 info->method = (char *)malloc(9 + strlen(info->selector_name));
2274 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002275 if (Arch == Triple::x86_64)
2276 strcpy(info->method, "-[%rdi ");
2277 else if (Arch == Triple::aarch64)
2278 strcpy(info->method, "-[x0 ");
2279 else
2280 strcpy(info->method, "-[r? ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002281 strcat(info->method, info->selector_name);
2282 strcat(info->method, "]");
2283 *ReferenceName = info->method;
2284 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2285 }
2286 }
2287 info->class_name = nullptr;
2288 }
2289 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002290 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002291 if (info->method != nullptr)
2292 free(info->method);
2293 info->method = (char *)malloc(17 + strlen(info->selector_name));
2294 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002295 if (Arch == Triple::x86_64)
2296 strcpy(info->method, "-[[%rdi super] ");
2297 else if (Arch == Triple::aarch64)
2298 strcpy(info->method, "-[[x0 super] ");
2299 else
2300 strcpy(info->method, "-[[r? super] ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002301 strcat(info->method, info->selector_name);
2302 strcat(info->method, "]");
2303 *ReferenceName = info->method;
2304 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2305 }
2306 info->class_name = nullptr;
2307 }
2308 }
2309 }
2310}
2311
2312// GuessPointerPointer() is passed the address of what might be a pointer to
2313// a reference to an Objective-C class, selector, message ref or cfstring.
2314// If so the value of the pointer is returned and one of the booleans are set
2315// to true. If not zero is returned and all the booleans are set to false.
2316static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2317 struct DisassembleInfo *info,
2318 bool &classref, bool &selref, bool &msgref,
2319 bool &cfstring) {
2320 classref = false;
2321 selref = false;
2322 msgref = false;
2323 cfstring = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002324 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002325 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2326 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2327 for (unsigned J = 0; J < Seg.nsects; ++J) {
2328 MachO::section_64 Sec = info->O->getSection64(Load, J);
2329 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2330 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2331 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2332 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2333 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2334 ReferenceValue >= Sec.addr &&
2335 ReferenceValue < Sec.addr + Sec.size) {
2336 uint64_t sect_offset = ReferenceValue - Sec.addr;
2337 uint64_t object_offset = Sec.offset + sect_offset;
2338 StringRef MachOContents = info->O->getData();
2339 uint64_t object_size = MachOContents.size();
2340 const char *object_addr = (const char *)MachOContents.data();
2341 if (object_offset < object_size) {
2342 uint64_t pointer_value;
2343 memcpy(&pointer_value, object_addr + object_offset,
2344 sizeof(uint64_t));
2345 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2346 sys::swapByteOrder(pointer_value);
2347 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2348 selref = true;
2349 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2350 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2351 classref = true;
2352 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2353 ReferenceValue + 8 < Sec.addr + Sec.size) {
2354 msgref = true;
2355 memcpy(&pointer_value, object_addr + object_offset + 8,
2356 sizeof(uint64_t));
2357 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2358 sys::swapByteOrder(pointer_value);
2359 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2360 cfstring = true;
2361 return pointer_value;
2362 } else {
2363 return 0;
2364 }
2365 }
2366 }
2367 }
2368 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002369 }
2370 return 0;
2371}
2372
2373// get_pointer_64 returns a pointer to the bytes in the object file at the
2374// Address from a section in the Mach-O file. And indirectly returns the
2375// offset into the section, number of bytes left in the section past the offset
2376// and which section is was being referenced. If the Address is not in a
2377// section nullptr is returned.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002378static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2379 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002380 DisassembleInfo *info,
2381 bool objc_only = false) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002382 offset = 0;
2383 left = 0;
2384 S = SectionRef();
2385 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2386 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2387 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
Kevin Enderby846c0002015-04-16 17:19:59 +00002388 if (objc_only) {
2389 StringRef SectName;
2390 ((*(info->Sections))[SectIdx]).getName(SectName);
2391 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2392 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2393 if (SegName != "__OBJC" && SectName != "__cstring")
2394 continue;
2395 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002396 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2397 S = (*(info->Sections))[SectIdx];
2398 offset = Address - SectAddress;
2399 left = SectSize - offset;
2400 StringRef SectContents;
2401 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2402 return SectContents.data() + offset;
2403 }
2404 }
2405 return nullptr;
2406}
2407
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002408static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2409 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002410 DisassembleInfo *info,
2411 bool objc_only = false) {
2412 return get_pointer_64(Address, offset, left, S, info, objc_only);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002413}
2414
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002415// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2416// the symbol indirectly through n_value. Based on the relocation information
2417// for the specified section offset in the specified section reference.
Kevin Enderby0fc11822015-04-01 20:57:01 +00002418// If no relocation information is found and a non-zero ReferenceValue for the
2419// symbol is passed, look up that address in the info's AddrMap.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002420static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2421 DisassembleInfo *info, uint64_t &n_value,
2422 uint64_t ReferenceValue = UnknownAddress) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002423 n_value = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002424 if (!info->verbose)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002425 return nullptr;
2426
2427 // See if there is an external relocation entry at the sect_offset.
2428 bool reloc_found = false;
2429 DataRefImpl Rel;
2430 MachO::any_relocation_info RE;
2431 bool isExtern = false;
2432 SymbolRef Symbol;
2433 for (const RelocationRef &Reloc : S.relocations()) {
2434 uint64_t RelocOffset;
2435 Reloc.getOffset(RelocOffset);
2436 if (RelocOffset == sect_offset) {
2437 Rel = Reloc.getRawDataRefImpl();
2438 RE = info->O->getRelocation(Rel);
2439 if (info->O->isRelocationScattered(RE))
2440 continue;
2441 isExtern = info->O->getPlainRelocationExternal(RE);
2442 if (isExtern) {
2443 symbol_iterator RelocSym = Reloc.getSymbol();
2444 Symbol = *RelocSym;
2445 }
2446 reloc_found = true;
2447 break;
2448 }
2449 }
2450 // If there is an external relocation entry for a symbol in this section
2451 // at this section_offset then use that symbol's value for the n_value
2452 // and return its name.
2453 const char *SymbolName = nullptr;
2454 if (reloc_found && isExtern) {
2455 Symbol.getAddress(n_value);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002456 if (n_value == UnknownAddress)
Kevin Enderby0fc11822015-04-01 20:57:01 +00002457 n_value = 0;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002458 StringRef name;
2459 Symbol.getName(name);
2460 if (!name.empty()) {
2461 SymbolName = name.data();
2462 return SymbolName;
2463 }
2464 }
2465
2466 // TODO: For fully linked images, look through the external relocation
2467 // entries off the dynamic symtab command. For these the r_offset is from the
2468 // start of the first writeable segment in the Mach-O file. So the offset
2469 // to this section from that segment is passed to this routine by the caller,
2470 // as the database_offset. Which is the difference of the section's starting
2471 // address and the first writable segment.
2472 //
2473 // NOTE: need add passing the database_offset to this routine.
2474
Kevin Enderby0fc11822015-04-01 20:57:01 +00002475 // We did not find an external relocation entry so look up the ReferenceValue
2476 // as an address of a symbol and if found return that symbol's name.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002477 if (ReferenceValue != UnknownAddress)
Kevin Enderby0fc11822015-04-01 20:57:01 +00002478 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002479
2480 return SymbolName;
2481}
2482
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002483static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2484 DisassembleInfo *info,
2485 uint32_t ReferenceValue) {
2486 uint64_t n_value64;
2487 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2488}
2489
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002490// These are structs in the Objective-C meta data and read to produce the
2491// comments for disassembly. While these are part of the ABI they are no
2492// public defintions. So the are here not in include/llvm/Support/MachO.h .
2493
2494// The cfstring object in a 64-bit Mach-O file.
2495struct cfstring64_t {
2496 uint64_t isa; // class64_t * (64-bit pointer)
2497 uint64_t flags; // flag bits
2498 uint64_t characters; // char * (64-bit pointer)
2499 uint64_t length; // number of non-NULL characters in above
2500};
2501
2502// The class object in a 64-bit Mach-O file.
2503struct class64_t {
2504 uint64_t isa; // class64_t * (64-bit pointer)
2505 uint64_t superclass; // class64_t * (64-bit pointer)
2506 uint64_t cache; // Cache (64-bit pointer)
2507 uint64_t vtable; // IMP * (64-bit pointer)
2508 uint64_t data; // class_ro64_t * (64-bit pointer)
2509};
2510
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002511struct class32_t {
2512 uint32_t isa; /* class32_t * (32-bit pointer) */
2513 uint32_t superclass; /* class32_t * (32-bit pointer) */
2514 uint32_t cache; /* Cache (32-bit pointer) */
2515 uint32_t vtable; /* IMP * (32-bit pointer) */
2516 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2517};
2518
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002519struct class_ro64_t {
2520 uint32_t flags;
2521 uint32_t instanceStart;
2522 uint32_t instanceSize;
2523 uint32_t reserved;
2524 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2525 uint64_t name; // const char * (64-bit pointer)
2526 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2527 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2528 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2529 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2530 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2531};
2532
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002533struct class_ro32_t {
2534 uint32_t flags;
2535 uint32_t instanceStart;
2536 uint32_t instanceSize;
2537 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2538 uint32_t name; /* const char * (32-bit pointer) */
2539 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2540 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2541 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2542 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2543 uint32_t baseProperties; /* const struct objc_property_list *
2544 (32-bit pointer) */
2545};
2546
2547/* Values for class_ro{64,32}_t->flags */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002548#define RO_META (1 << 0)
2549#define RO_ROOT (1 << 1)
2550#define RO_HAS_CXX_STRUCTORS (1 << 2)
2551
2552struct method_list64_t {
2553 uint32_t entsize;
2554 uint32_t count;
2555 /* struct method64_t first; These structures follow inline */
2556};
2557
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002558struct method_list32_t {
2559 uint32_t entsize;
2560 uint32_t count;
2561 /* struct method32_t first; These structures follow inline */
2562};
2563
Kevin Enderby0fc11822015-04-01 20:57:01 +00002564struct method64_t {
2565 uint64_t name; /* SEL (64-bit pointer) */
2566 uint64_t types; /* const char * (64-bit pointer) */
2567 uint64_t imp; /* IMP (64-bit pointer) */
2568};
2569
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002570struct method32_t {
2571 uint32_t name; /* SEL (32-bit pointer) */
2572 uint32_t types; /* const char * (32-bit pointer) */
2573 uint32_t imp; /* IMP (32-bit pointer) */
2574};
2575
Kevin Enderby0fc11822015-04-01 20:57:01 +00002576struct protocol_list64_t {
2577 uint64_t count; /* uintptr_t (a 64-bit value) */
2578 /* struct protocol64_t * list[0]; These pointers follow inline */
2579};
2580
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002581struct protocol_list32_t {
2582 uint32_t count; /* uintptr_t (a 32-bit value) */
2583 /* struct protocol32_t * list[0]; These pointers follow inline */
2584};
2585
Kevin Enderby0fc11822015-04-01 20:57:01 +00002586struct protocol64_t {
2587 uint64_t isa; /* id * (64-bit pointer) */
2588 uint64_t name; /* const char * (64-bit pointer) */
2589 uint64_t protocols; /* struct protocol_list64_t *
2590 (64-bit pointer) */
2591 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2592 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2593 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2594 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2595 uint64_t instanceProperties; /* struct objc_property_list *
2596 (64-bit pointer) */
2597};
2598
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002599struct protocol32_t {
2600 uint32_t isa; /* id * (32-bit pointer) */
2601 uint32_t name; /* const char * (32-bit pointer) */
2602 uint32_t protocols; /* struct protocol_list_t *
2603 (32-bit pointer) */
2604 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2605 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2606 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2607 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2608 uint32_t instanceProperties; /* struct objc_property_list *
2609 (32-bit pointer) */
2610};
2611
Kevin Enderby0fc11822015-04-01 20:57:01 +00002612struct ivar_list64_t {
2613 uint32_t entsize;
2614 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002615 /* struct ivar64_t first; These structures follow inline */
2616};
2617
2618struct ivar_list32_t {
2619 uint32_t entsize;
2620 uint32_t count;
2621 /* struct ivar32_t first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002622};
2623
2624struct ivar64_t {
2625 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2626 uint64_t name; /* const char * (64-bit pointer) */
2627 uint64_t type; /* const char * (64-bit pointer) */
2628 uint32_t alignment;
2629 uint32_t size;
2630};
2631
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002632struct ivar32_t {
2633 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2634 uint32_t name; /* const char * (32-bit pointer) */
2635 uint32_t type; /* const char * (32-bit pointer) */
2636 uint32_t alignment;
2637 uint32_t size;
2638};
2639
Kevin Enderby0fc11822015-04-01 20:57:01 +00002640struct objc_property_list64 {
2641 uint32_t entsize;
2642 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002643 /* struct objc_property64 first; These structures follow inline */
2644};
2645
2646struct objc_property_list32 {
2647 uint32_t entsize;
2648 uint32_t count;
2649 /* struct objc_property32 first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002650};
2651
2652struct objc_property64 {
2653 uint64_t name; /* const char * (64-bit pointer) */
2654 uint64_t attributes; /* const char * (64-bit pointer) */
2655};
2656
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002657struct objc_property32 {
2658 uint32_t name; /* const char * (32-bit pointer) */
2659 uint32_t attributes; /* const char * (32-bit pointer) */
2660};
2661
Kevin Enderby0fc11822015-04-01 20:57:01 +00002662struct category64_t {
2663 uint64_t name; /* const char * (64-bit pointer) */
2664 uint64_t cls; /* struct class_t * (64-bit pointer) */
2665 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2666 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2667 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2668 uint64_t instanceProperties; /* struct objc_property_list *
2669 (64-bit pointer) */
2670};
2671
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002672struct category32_t {
2673 uint32_t name; /* const char * (32-bit pointer) */
2674 uint32_t cls; /* struct class_t * (32-bit pointer) */
2675 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2676 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2677 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2678 uint32_t instanceProperties; /* struct objc_property_list *
2679 (32-bit pointer) */
2680};
2681
Kevin Enderby0fc11822015-04-01 20:57:01 +00002682struct objc_image_info64 {
2683 uint32_t version;
2684 uint32_t flags;
2685};
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002686struct objc_image_info32 {
2687 uint32_t version;
2688 uint32_t flags;
2689};
Kevin Enderby846c0002015-04-16 17:19:59 +00002690struct imageInfo_t {
2691 uint32_t version;
2692 uint32_t flags;
2693};
Kevin Enderby0fc11822015-04-01 20:57:01 +00002694/* masks for objc_image_info.flags */
2695#define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2696#define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2697
2698struct message_ref64 {
2699 uint64_t imp; /* IMP (64-bit pointer) */
2700 uint64_t sel; /* SEL (64-bit pointer) */
2701};
2702
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002703struct message_ref32 {
2704 uint32_t imp; /* IMP (32-bit pointer) */
2705 uint32_t sel; /* SEL (32-bit pointer) */
2706};
2707
Kevin Enderby846c0002015-04-16 17:19:59 +00002708// Objective-C 1 (32-bit only) meta data structs.
2709
2710struct objc_module_t {
2711 uint32_t version;
2712 uint32_t size;
2713 uint32_t name; /* char * (32-bit pointer) */
2714 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2715};
2716
2717struct objc_symtab_t {
2718 uint32_t sel_ref_cnt;
2719 uint32_t refs; /* SEL * (32-bit pointer) */
2720 uint16_t cls_def_cnt;
2721 uint16_t cat_def_cnt;
2722 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2723};
2724
2725struct objc_class_t {
2726 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2727 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2728 uint32_t name; /* const char * (32-bit pointer) */
2729 int32_t version;
2730 int32_t info;
2731 int32_t instance_size;
2732 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2733 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2734 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2735 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2736};
2737
2738#define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2739// class is not a metaclass
2740#define CLS_CLASS 0x1
2741// class is a metaclass
2742#define CLS_META 0x2
2743
2744struct objc_category_t {
2745 uint32_t category_name; /* char * (32-bit pointer) */
2746 uint32_t class_name; /* char * (32-bit pointer) */
2747 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2748 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2749 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2750};
2751
2752struct objc_ivar_t {
2753 uint32_t ivar_name; /* char * (32-bit pointer) */
2754 uint32_t ivar_type; /* char * (32-bit pointer) */
2755 int32_t ivar_offset;
2756};
2757
2758struct objc_ivar_list_t {
2759 int32_t ivar_count;
2760 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2761};
2762
2763struct objc_method_list_t {
2764 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2765 int32_t method_count;
2766 // struct objc_method_t method_list[1]; /* variable length structure */
2767};
2768
2769struct objc_method_t {
2770 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2771 uint32_t method_types; /* char * (32-bit pointer) */
2772 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2773 (32-bit pointer) */
2774};
2775
2776struct objc_protocol_list_t {
2777 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2778 int32_t count;
2779 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2780 // (32-bit pointer) */
2781};
2782
2783struct objc_protocol_t {
2784 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2785 uint32_t protocol_name; /* char * (32-bit pointer) */
2786 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2787 uint32_t instance_methods; /* struct objc_method_description_list *
2788 (32-bit pointer) */
2789 uint32_t class_methods; /* struct objc_method_description_list *
2790 (32-bit pointer) */
2791};
2792
2793struct objc_method_description_list_t {
2794 int32_t count;
2795 // struct objc_method_description_t list[1];
2796};
2797
2798struct objc_method_description_t {
2799 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2800 uint32_t types; /* char * (32-bit pointer) */
2801};
2802
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002803inline void swapStruct(struct cfstring64_t &cfs) {
2804 sys::swapByteOrder(cfs.isa);
2805 sys::swapByteOrder(cfs.flags);
2806 sys::swapByteOrder(cfs.characters);
2807 sys::swapByteOrder(cfs.length);
2808}
2809
2810inline void swapStruct(struct class64_t &c) {
2811 sys::swapByteOrder(c.isa);
2812 sys::swapByteOrder(c.superclass);
2813 sys::swapByteOrder(c.cache);
2814 sys::swapByteOrder(c.vtable);
2815 sys::swapByteOrder(c.data);
2816}
2817
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002818inline void swapStruct(struct class32_t &c) {
2819 sys::swapByteOrder(c.isa);
2820 sys::swapByteOrder(c.superclass);
2821 sys::swapByteOrder(c.cache);
2822 sys::swapByteOrder(c.vtable);
2823 sys::swapByteOrder(c.data);
2824}
2825
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002826inline void swapStruct(struct class_ro64_t &cro) {
2827 sys::swapByteOrder(cro.flags);
2828 sys::swapByteOrder(cro.instanceStart);
2829 sys::swapByteOrder(cro.instanceSize);
2830 sys::swapByteOrder(cro.reserved);
2831 sys::swapByteOrder(cro.ivarLayout);
2832 sys::swapByteOrder(cro.name);
2833 sys::swapByteOrder(cro.baseMethods);
2834 sys::swapByteOrder(cro.baseProtocols);
2835 sys::swapByteOrder(cro.ivars);
2836 sys::swapByteOrder(cro.weakIvarLayout);
2837 sys::swapByteOrder(cro.baseProperties);
2838}
2839
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002840inline void swapStruct(struct class_ro32_t &cro) {
2841 sys::swapByteOrder(cro.flags);
2842 sys::swapByteOrder(cro.instanceStart);
2843 sys::swapByteOrder(cro.instanceSize);
2844 sys::swapByteOrder(cro.ivarLayout);
2845 sys::swapByteOrder(cro.name);
2846 sys::swapByteOrder(cro.baseMethods);
2847 sys::swapByteOrder(cro.baseProtocols);
2848 sys::swapByteOrder(cro.ivars);
2849 sys::swapByteOrder(cro.weakIvarLayout);
2850 sys::swapByteOrder(cro.baseProperties);
2851}
2852
Kevin Enderby0fc11822015-04-01 20:57:01 +00002853inline void swapStruct(struct method_list64_t &ml) {
2854 sys::swapByteOrder(ml.entsize);
2855 sys::swapByteOrder(ml.count);
2856}
2857
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002858inline void swapStruct(struct method_list32_t &ml) {
2859 sys::swapByteOrder(ml.entsize);
2860 sys::swapByteOrder(ml.count);
2861}
2862
Kevin Enderby0fc11822015-04-01 20:57:01 +00002863inline void swapStruct(struct method64_t &m) {
2864 sys::swapByteOrder(m.name);
2865 sys::swapByteOrder(m.types);
2866 sys::swapByteOrder(m.imp);
2867}
2868
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002869inline void swapStruct(struct method32_t &m) {
2870 sys::swapByteOrder(m.name);
2871 sys::swapByteOrder(m.types);
2872 sys::swapByteOrder(m.imp);
2873}
2874
Kevin Enderby0fc11822015-04-01 20:57:01 +00002875inline void swapStruct(struct protocol_list64_t &pl) {
2876 sys::swapByteOrder(pl.count);
2877}
2878
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002879inline void swapStruct(struct protocol_list32_t &pl) {
2880 sys::swapByteOrder(pl.count);
2881}
2882
Kevin Enderby0fc11822015-04-01 20:57:01 +00002883inline void swapStruct(struct protocol64_t &p) {
2884 sys::swapByteOrder(p.isa);
2885 sys::swapByteOrder(p.name);
2886 sys::swapByteOrder(p.protocols);
2887 sys::swapByteOrder(p.instanceMethods);
2888 sys::swapByteOrder(p.classMethods);
2889 sys::swapByteOrder(p.optionalInstanceMethods);
2890 sys::swapByteOrder(p.optionalClassMethods);
2891 sys::swapByteOrder(p.instanceProperties);
2892}
2893
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002894inline void swapStruct(struct protocol32_t &p) {
2895 sys::swapByteOrder(p.isa);
2896 sys::swapByteOrder(p.name);
2897 sys::swapByteOrder(p.protocols);
2898 sys::swapByteOrder(p.instanceMethods);
2899 sys::swapByteOrder(p.classMethods);
2900 sys::swapByteOrder(p.optionalInstanceMethods);
2901 sys::swapByteOrder(p.optionalClassMethods);
2902 sys::swapByteOrder(p.instanceProperties);
2903}
2904
Kevin Enderby0fc11822015-04-01 20:57:01 +00002905inline void swapStruct(struct ivar_list64_t &il) {
2906 sys::swapByteOrder(il.entsize);
2907 sys::swapByteOrder(il.count);
2908}
2909
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002910inline void swapStruct(struct ivar_list32_t &il) {
2911 sys::swapByteOrder(il.entsize);
2912 sys::swapByteOrder(il.count);
2913}
2914
Kevin Enderby0fc11822015-04-01 20:57:01 +00002915inline void swapStruct(struct ivar64_t &i) {
2916 sys::swapByteOrder(i.offset);
2917 sys::swapByteOrder(i.name);
2918 sys::swapByteOrder(i.type);
2919 sys::swapByteOrder(i.alignment);
2920 sys::swapByteOrder(i.size);
2921}
2922
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002923inline void swapStruct(struct ivar32_t &i) {
2924 sys::swapByteOrder(i.offset);
2925 sys::swapByteOrder(i.name);
2926 sys::swapByteOrder(i.type);
2927 sys::swapByteOrder(i.alignment);
2928 sys::swapByteOrder(i.size);
2929}
2930
Kevin Enderby0fc11822015-04-01 20:57:01 +00002931inline void swapStruct(struct objc_property_list64 &pl) {
2932 sys::swapByteOrder(pl.entsize);
2933 sys::swapByteOrder(pl.count);
2934}
2935
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002936inline void swapStruct(struct objc_property_list32 &pl) {
2937 sys::swapByteOrder(pl.entsize);
2938 sys::swapByteOrder(pl.count);
2939}
2940
Kevin Enderby0fc11822015-04-01 20:57:01 +00002941inline void swapStruct(struct objc_property64 &op) {
2942 sys::swapByteOrder(op.name);
2943 sys::swapByteOrder(op.attributes);
2944}
2945
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002946inline void swapStruct(struct objc_property32 &op) {
2947 sys::swapByteOrder(op.name);
2948 sys::swapByteOrder(op.attributes);
2949}
2950
Kevin Enderby0fc11822015-04-01 20:57:01 +00002951inline void swapStruct(struct category64_t &c) {
2952 sys::swapByteOrder(c.name);
2953 sys::swapByteOrder(c.cls);
2954 sys::swapByteOrder(c.instanceMethods);
2955 sys::swapByteOrder(c.classMethods);
2956 sys::swapByteOrder(c.protocols);
2957 sys::swapByteOrder(c.instanceProperties);
2958}
2959
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002960inline void swapStruct(struct category32_t &c) {
2961 sys::swapByteOrder(c.name);
2962 sys::swapByteOrder(c.cls);
2963 sys::swapByteOrder(c.instanceMethods);
2964 sys::swapByteOrder(c.classMethods);
2965 sys::swapByteOrder(c.protocols);
2966 sys::swapByteOrder(c.instanceProperties);
2967}
2968
Kevin Enderby0fc11822015-04-01 20:57:01 +00002969inline void swapStruct(struct objc_image_info64 &o) {
2970 sys::swapByteOrder(o.version);
2971 sys::swapByteOrder(o.flags);
2972}
2973
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002974inline void swapStruct(struct objc_image_info32 &o) {
2975 sys::swapByteOrder(o.version);
2976 sys::swapByteOrder(o.flags);
2977}
2978
Kevin Enderby846c0002015-04-16 17:19:59 +00002979inline void swapStruct(struct imageInfo_t &o) {
2980 sys::swapByteOrder(o.version);
2981 sys::swapByteOrder(o.flags);
2982}
2983
Kevin Enderby0fc11822015-04-01 20:57:01 +00002984inline void swapStruct(struct message_ref64 &mr) {
2985 sys::swapByteOrder(mr.imp);
2986 sys::swapByteOrder(mr.sel);
2987}
2988
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002989inline void swapStruct(struct message_ref32 &mr) {
2990 sys::swapByteOrder(mr.imp);
2991 sys::swapByteOrder(mr.sel);
2992}
2993
Kevin Enderby846c0002015-04-16 17:19:59 +00002994inline void swapStruct(struct objc_module_t &module) {
2995 sys::swapByteOrder(module.version);
2996 sys::swapByteOrder(module.size);
2997 sys::swapByteOrder(module.name);
2998 sys::swapByteOrder(module.symtab);
Jingyue Wufedecc42015-04-16 18:43:44 +00002999}
Kevin Enderby846c0002015-04-16 17:19:59 +00003000
3001inline void swapStruct(struct objc_symtab_t &symtab) {
3002 sys::swapByteOrder(symtab.sel_ref_cnt);
3003 sys::swapByteOrder(symtab.refs);
3004 sys::swapByteOrder(symtab.cls_def_cnt);
3005 sys::swapByteOrder(symtab.cat_def_cnt);
Jingyue Wufedecc42015-04-16 18:43:44 +00003006}
Kevin Enderby846c0002015-04-16 17:19:59 +00003007
3008inline void swapStruct(struct objc_class_t &objc_class) {
3009 sys::swapByteOrder(objc_class.isa);
3010 sys::swapByteOrder(objc_class.super_class);
3011 sys::swapByteOrder(objc_class.name);
3012 sys::swapByteOrder(objc_class.version);
3013 sys::swapByteOrder(objc_class.info);
3014 sys::swapByteOrder(objc_class.instance_size);
3015 sys::swapByteOrder(objc_class.ivars);
3016 sys::swapByteOrder(objc_class.methodLists);
3017 sys::swapByteOrder(objc_class.cache);
3018 sys::swapByteOrder(objc_class.protocols);
Jingyue Wufedecc42015-04-16 18:43:44 +00003019}
Kevin Enderby846c0002015-04-16 17:19:59 +00003020
3021inline void swapStruct(struct objc_category_t &objc_category) {
3022 sys::swapByteOrder(objc_category.category_name);
3023 sys::swapByteOrder(objc_category.class_name);
3024 sys::swapByteOrder(objc_category.instance_methods);
3025 sys::swapByteOrder(objc_category.class_methods);
3026 sys::swapByteOrder(objc_category.protocols);
3027}
3028
3029inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3030 sys::swapByteOrder(objc_ivar_list.ivar_count);
3031}
3032
3033inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3034 sys::swapByteOrder(objc_ivar.ivar_name);
3035 sys::swapByteOrder(objc_ivar.ivar_type);
3036 sys::swapByteOrder(objc_ivar.ivar_offset);
Jingyue Wufedecc42015-04-16 18:43:44 +00003037}
Kevin Enderby846c0002015-04-16 17:19:59 +00003038
3039inline void swapStruct(struct objc_method_list_t &method_list) {
3040 sys::swapByteOrder(method_list.obsolete);
3041 sys::swapByteOrder(method_list.method_count);
3042}
3043
3044inline void swapStruct(struct objc_method_t &method) {
3045 sys::swapByteOrder(method.method_name);
3046 sys::swapByteOrder(method.method_types);
3047 sys::swapByteOrder(method.method_imp);
3048}
3049
3050inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3051 sys::swapByteOrder(protocol_list.next);
3052 sys::swapByteOrder(protocol_list.count);
3053}
3054
3055inline void swapStruct(struct objc_protocol_t &protocol) {
3056 sys::swapByteOrder(protocol.isa);
3057 sys::swapByteOrder(protocol.protocol_name);
3058 sys::swapByteOrder(protocol.protocol_list);
3059 sys::swapByteOrder(protocol.instance_methods);
3060 sys::swapByteOrder(protocol.class_methods);
3061}
3062
3063inline void swapStruct(struct objc_method_description_list_t &mdl) {
3064 sys::swapByteOrder(mdl.count);
3065}
3066
3067inline void swapStruct(struct objc_method_description_t &md) {
3068 sys::swapByteOrder(md.name);
3069 sys::swapByteOrder(md.types);
3070}
3071
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003072static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3073 struct DisassembleInfo *info);
3074
3075// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3076// to an Objective-C class and returns the class name. It is also passed the
3077// address of the pointer, so when the pointer is zero as it can be in an .o
3078// file, that is used to look for an external relocation entry with a symbol
3079// name.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003080static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3081 uint64_t ReferenceValue,
3082 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003083 const char *r;
3084 uint32_t offset, left;
3085 SectionRef S;
3086
3087 // The pointer_value can be 0 in an object file and have a relocation
3088 // entry for the class symbol at the ReferenceValue (the address of the
3089 // pointer).
3090 if (pointer_value == 0) {
3091 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3092 if (r == nullptr || left < sizeof(uint64_t))
3093 return nullptr;
3094 uint64_t n_value;
3095 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3096 if (symbol_name == nullptr)
3097 return nullptr;
Hans Wennborgdb53e302014-10-23 21:59:17 +00003098 const char *class_name = strrchr(symbol_name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003099 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3100 return class_name + 2;
3101 else
3102 return nullptr;
3103 }
3104
3105 // The case were the pointer_value is non-zero and points to a class defined
3106 // in this Mach-O file.
3107 r = get_pointer_64(pointer_value, offset, left, S, info);
3108 if (r == nullptr || left < sizeof(struct class64_t))
3109 return nullptr;
3110 struct class64_t c;
3111 memcpy(&c, r, sizeof(struct class64_t));
3112 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3113 swapStruct(c);
3114 if (c.data == 0)
3115 return nullptr;
3116 r = get_pointer_64(c.data, offset, left, S, info);
3117 if (r == nullptr || left < sizeof(struct class_ro64_t))
3118 return nullptr;
3119 struct class_ro64_t cro;
3120 memcpy(&cro, r, sizeof(struct class_ro64_t));
3121 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3122 swapStruct(cro);
3123 if (cro.name == 0)
3124 return nullptr;
3125 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3126 return name;
3127}
3128
3129// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3130// pointer to a cfstring and returns its name or nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003131static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3132 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003133 const char *r, *name;
3134 uint32_t offset, left;
3135 SectionRef S;
3136 struct cfstring64_t cfs;
3137 uint64_t cfs_characters;
3138
3139 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3140 if (r == nullptr || left < sizeof(struct cfstring64_t))
3141 return nullptr;
3142 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3143 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3144 swapStruct(cfs);
3145 if (cfs.characters == 0) {
3146 uint64_t n_value;
3147 const char *symbol_name = get_symbol_64(
3148 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3149 if (symbol_name == nullptr)
3150 return nullptr;
3151 cfs_characters = n_value;
3152 } else
3153 cfs_characters = cfs.characters;
3154 name = get_pointer_64(cfs_characters, offset, left, S, info);
3155
3156 return name;
3157}
3158
3159// get_objc2_64bit_selref() is used for disassembly and is passed a the address
3160// of a pointer to an Objective-C selector reference when the pointer value is
3161// zero as in a .o file and is likely to have a external relocation entry with
3162// who's symbol's n_value is the real pointer to the selector name. If that is
3163// the case the real pointer to the selector name is returned else 0 is
3164// returned
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003165static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3166 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003167 uint32_t offset, left;
3168 SectionRef S;
3169
3170 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3171 if (r == nullptr || left < sizeof(uint64_t))
3172 return 0;
3173 uint64_t n_value;
3174 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3175 if (symbol_name == nullptr)
3176 return 0;
3177 return n_value;
3178}
3179
Kevin Enderby0fc11822015-04-01 20:57:01 +00003180static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3181 const char *sectname) {
3182 for (const SectionRef &Section : O->sections()) {
3183 StringRef SectName;
3184 Section.getName(SectName);
3185 DataRefImpl Ref = Section.getRawDataRefImpl();
3186 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3187 if (SegName == segname && SectName == sectname)
3188 return Section;
3189 }
3190 return SectionRef();
3191}
3192
3193static void
3194walk_pointer_list_64(const char *listname, const SectionRef S,
3195 MachOObjectFile *O, struct DisassembleInfo *info,
3196 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3197 if (S == SectionRef())
3198 return;
3199
3200 StringRef SectName;
3201 S.getName(SectName);
3202 DataRefImpl Ref = S.getRawDataRefImpl();
3203 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3204 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3205
3206 StringRef BytesStr;
3207 S.getContents(BytesStr);
3208 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3209
3210 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3211 uint32_t left = S.getSize() - i;
3212 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3213 uint64_t p = 0;
3214 memcpy(&p, Contents + i, size);
3215 if (i + sizeof(uint64_t) > S.getSize())
3216 outs() << listname << " list pointer extends past end of (" << SegName
3217 << "," << SectName << ") section\n";
3218 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3219
3220 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3221 sys::swapByteOrder(p);
3222
3223 uint64_t n_value = 0;
3224 const char *name = get_symbol_64(i, S, info, n_value, p);
3225 if (name == nullptr)
3226 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3227
3228 if (n_value != 0) {
3229 outs() << format("0x%" PRIx64, n_value);
3230 if (p != 0)
3231 outs() << " + " << format("0x%" PRIx64, p);
3232 } else
3233 outs() << format("0x%" PRIx64, p);
3234 if (name != nullptr)
3235 outs() << " " << name;
3236 outs() << "\n";
3237
3238 p += n_value;
3239 if (func)
3240 func(p, info);
3241 }
3242}
3243
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003244static void
3245walk_pointer_list_32(const char *listname, const SectionRef S,
3246 MachOObjectFile *O, struct DisassembleInfo *info,
3247 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3248 if (S == SectionRef())
3249 return;
3250
3251 StringRef SectName;
3252 S.getName(SectName);
3253 DataRefImpl Ref = S.getRawDataRefImpl();
3254 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3255 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3256
3257 StringRef BytesStr;
3258 S.getContents(BytesStr);
3259 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3260
3261 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3262 uint32_t left = S.getSize() - i;
3263 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3264 uint32_t p = 0;
3265 memcpy(&p, Contents + i, size);
3266 if (i + sizeof(uint32_t) > S.getSize())
3267 outs() << listname << " list pointer extends past end of (" << SegName
3268 << "," << SectName << ") section\n";
Kevin Enderbycf261312015-04-06 22:33:43 +00003269 uint32_t Address = S.getAddress() + i;
3270 outs() << format("%08" PRIx32, Address) << " ";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003271
3272 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3273 sys::swapByteOrder(p);
3274 outs() << format("0x%" PRIx32, p);
3275
3276 const char *name = get_symbol_32(i, S, info, p);
3277 if (name != nullptr)
3278 outs() << " " << name;
3279 outs() << "\n";
3280
3281 if (func)
3282 func(p, info);
3283 }
3284}
3285
3286static void print_layout_map(const char *layout_map, uint32_t left) {
3287 outs() << " layout map: ";
3288 do {
3289 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3290 left--;
3291 layout_map++;
3292 } while (*layout_map != '\0' && left != 0);
3293 outs() << "\n";
3294}
3295
Kevin Enderby0fc11822015-04-01 20:57:01 +00003296static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3297 uint32_t offset, left;
3298 SectionRef S;
3299 const char *layout_map;
3300
3301 if (p == 0)
3302 return;
3303 layout_map = get_pointer_64(p, offset, left, S, info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003304 print_layout_map(layout_map, left);
3305}
3306
3307static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3308 uint32_t offset, left;
3309 SectionRef S;
3310 const char *layout_map;
3311
3312 if (p == 0)
3313 return;
3314 layout_map = get_pointer_32(p, offset, left, S, info);
3315 print_layout_map(layout_map, left);
Kevin Enderby0fc11822015-04-01 20:57:01 +00003316}
3317
3318static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3319 const char *indent) {
3320 struct method_list64_t ml;
3321 struct method64_t m;
3322 const char *r;
3323 uint32_t offset, xoffset, left, i;
3324 SectionRef S, xS;
3325 const char *name, *sym_name;
3326 uint64_t n_value;
3327
3328 r = get_pointer_64(p, offset, left, S, info);
3329 if (r == nullptr)
3330 return;
3331 memset(&ml, '\0', sizeof(struct method_list64_t));
3332 if (left < sizeof(struct method_list64_t)) {
3333 memcpy(&ml, r, left);
3334 outs() << " (method_list_t entends past the end of the section)\n";
3335 } else
3336 memcpy(&ml, r, sizeof(struct method_list64_t));
3337 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3338 swapStruct(ml);
3339 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3340 outs() << indent << "\t\t count " << ml.count << "\n";
3341
3342 p += sizeof(struct method_list64_t);
3343 offset += sizeof(struct method_list64_t);
3344 for (i = 0; i < ml.count; i++) {
3345 r = get_pointer_64(p, offset, left, S, info);
3346 if (r == nullptr)
3347 return;
3348 memset(&m, '\0', sizeof(struct method64_t));
3349 if (left < sizeof(struct method64_t)) {
3350 memcpy(&ml, r, left);
3351 outs() << indent << " (method_t entends past the end of the section)\n";
3352 } else
3353 memcpy(&m, r, sizeof(struct method64_t));
3354 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3355 swapStruct(m);
3356
3357 outs() << indent << "\t\t name ";
3358 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3359 info, n_value, m.name);
3360 if (n_value != 0) {
3361 if (info->verbose && sym_name != nullptr)
3362 outs() << sym_name;
3363 else
3364 outs() << format("0x%" PRIx64, n_value);
3365 if (m.name != 0)
3366 outs() << " + " << format("0x%" PRIx64, m.name);
3367 } else
3368 outs() << format("0x%" PRIx64, m.name);
3369 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3370 if (name != nullptr)
3371 outs() << format(" %.*s", left, name);
3372 outs() << "\n";
3373
3374 outs() << indent << "\t\t types ";
3375 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3376 info, n_value, m.types);
3377 if (n_value != 0) {
3378 if (info->verbose && sym_name != nullptr)
3379 outs() << sym_name;
3380 else
3381 outs() << format("0x%" PRIx64, n_value);
3382 if (m.types != 0)
3383 outs() << " + " << format("0x%" PRIx64, m.types);
3384 } else
3385 outs() << format("0x%" PRIx64, m.types);
3386 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3387 if (name != nullptr)
3388 outs() << format(" %.*s", left, name);
3389 outs() << "\n";
3390
3391 outs() << indent << "\t\t imp ";
3392 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3393 n_value, m.imp);
3394 if (info->verbose && name == nullptr) {
3395 if (n_value != 0) {
3396 outs() << format("0x%" PRIx64, n_value) << " ";
3397 if (m.imp != 0)
3398 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3399 } else
3400 outs() << format("0x%" PRIx64, m.imp) << " ";
3401 }
3402 if (name != nullptr)
3403 outs() << name;
3404 outs() << "\n";
3405
3406 p += sizeof(struct method64_t);
3407 offset += sizeof(struct method64_t);
3408 }
3409}
3410
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003411static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3412 const char *indent) {
3413 struct method_list32_t ml;
3414 struct method32_t m;
Kevin Enderby846c0002015-04-16 17:19:59 +00003415 const char *r, *name;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003416 uint32_t offset, xoffset, left, i;
3417 SectionRef S, xS;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003418
3419 r = get_pointer_32(p, offset, left, S, info);
3420 if (r == nullptr)
3421 return;
3422 memset(&ml, '\0', sizeof(struct method_list32_t));
3423 if (left < sizeof(struct method_list32_t)) {
3424 memcpy(&ml, r, left);
3425 outs() << " (method_list_t entends past the end of the section)\n";
3426 } else
3427 memcpy(&ml, r, sizeof(struct method_list32_t));
3428 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3429 swapStruct(ml);
3430 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3431 outs() << indent << "\t\t count " << ml.count << "\n";
3432
3433 p += sizeof(struct method_list32_t);
3434 offset += sizeof(struct method_list32_t);
3435 for (i = 0; i < ml.count; i++) {
3436 r = get_pointer_32(p, offset, left, S, info);
3437 if (r == nullptr)
3438 return;
3439 memset(&m, '\0', sizeof(struct method32_t));
3440 if (left < sizeof(struct method32_t)) {
3441 memcpy(&ml, r, left);
3442 outs() << indent << " (method_t entends past the end of the section)\n";
3443 } else
3444 memcpy(&m, r, sizeof(struct method32_t));
3445 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3446 swapStruct(m);
3447
3448 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3449 name = get_pointer_32(m.name, xoffset, left, xS, info);
3450 if (name != nullptr)
3451 outs() << format(" %.*s", left, name);
3452 outs() << "\n";
3453
3454 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3455 name = get_pointer_32(m.types, xoffset, left, xS, info);
3456 if (name != nullptr)
3457 outs() << format(" %.*s", left, name);
3458 outs() << "\n";
3459
3460 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3461 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3462 m.imp);
3463 if (name != nullptr)
3464 outs() << " " << name;
3465 outs() << "\n";
3466
3467 p += sizeof(struct method32_t);
3468 offset += sizeof(struct method32_t);
3469 }
3470}
3471
Kevin Enderby846c0002015-04-16 17:19:59 +00003472static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3473 uint32_t offset, left, xleft;
3474 SectionRef S;
3475 struct objc_method_list_t method_list;
3476 struct objc_method_t method;
3477 const char *r, *methods, *name, *SymbolName;
3478 int32_t i;
3479
3480 r = get_pointer_32(p, offset, left, S, info, true);
3481 if (r == nullptr)
3482 return true;
3483
3484 outs() << "\n";
3485 if (left > sizeof(struct objc_method_list_t)) {
3486 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3487 } else {
3488 outs() << "\t\t objc_method_list extends past end of the section\n";
3489 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3490 memcpy(&method_list, r, left);
3491 }
3492 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3493 swapStruct(method_list);
3494
3495 outs() << "\t\t obsolete "
3496 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3497 outs() << "\t\t method_count " << method_list.method_count << "\n";
3498
3499 methods = r + sizeof(struct objc_method_list_t);
3500 for (i = 0; i < method_list.method_count; i++) {
3501 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3502 outs() << "\t\t remaining method's extend past the of the section\n";
3503 break;
3504 }
3505 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3506 sizeof(struct objc_method_t));
3507 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3508 swapStruct(method);
3509
3510 outs() << "\t\t method_name "
3511 << format("0x%08" PRIx32, method.method_name);
3512 if (info->verbose) {
3513 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3514 if (name != nullptr)
3515 outs() << format(" %.*s", xleft, name);
3516 else
3517 outs() << " (not in an __OBJC section)";
3518 }
3519 outs() << "\n";
3520
3521 outs() << "\t\t method_types "
3522 << format("0x%08" PRIx32, method.method_types);
3523 if (info->verbose) {
3524 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3525 if (name != nullptr)
3526 outs() << format(" %.*s", xleft, name);
3527 else
3528 outs() << " (not in an __OBJC section)";
3529 }
3530 outs() << "\n";
3531
3532 outs() << "\t\t method_imp "
3533 << format("0x%08" PRIx32, method.method_imp) << " ";
3534 if (info->verbose) {
3535 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3536 if (SymbolName != nullptr)
3537 outs() << SymbolName;
3538 }
3539 outs() << "\n";
3540 }
3541 return false;
3542}
3543
Kevin Enderby0fc11822015-04-01 20:57:01 +00003544static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3545 struct protocol_list64_t pl;
3546 uint64_t q, n_value;
3547 struct protocol64_t pc;
3548 const char *r;
3549 uint32_t offset, xoffset, left, i;
3550 SectionRef S, xS;
3551 const char *name, *sym_name;
3552
3553 r = get_pointer_64(p, offset, left, S, info);
3554 if (r == nullptr)
3555 return;
3556 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3557 if (left < sizeof(struct protocol_list64_t)) {
3558 memcpy(&pl, r, left);
3559 outs() << " (protocol_list_t entends past the end of the section)\n";
3560 } else
3561 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3562 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3563 swapStruct(pl);
3564 outs() << " count " << pl.count << "\n";
3565
3566 p += sizeof(struct protocol_list64_t);
3567 offset += sizeof(struct protocol_list64_t);
3568 for (i = 0; i < pl.count; i++) {
3569 r = get_pointer_64(p, offset, left, S, info);
3570 if (r == nullptr)
3571 return;
3572 q = 0;
3573 if (left < sizeof(uint64_t)) {
3574 memcpy(&q, r, left);
3575 outs() << " (protocol_t * entends past the end of the section)\n";
3576 } else
3577 memcpy(&q, r, sizeof(uint64_t));
3578 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3579 sys::swapByteOrder(q);
3580
3581 outs() << "\t\t list[" << i << "] ";
3582 sym_name = get_symbol_64(offset, S, info, n_value, q);
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 (q != 0)
3589 outs() << " + " << format("0x%" PRIx64, q);
3590 } else
3591 outs() << format("0x%" PRIx64, q);
3592 outs() << " (struct protocol_t *)\n";
3593
3594 r = get_pointer_64(q + n_value, offset, left, S, info);
3595 if (r == nullptr)
3596 return;
3597 memset(&pc, '\0', sizeof(struct protocol64_t));
3598 if (left < sizeof(struct protocol64_t)) {
3599 memcpy(&pc, r, left);
3600 outs() << " (protocol_t entends past the end of the section)\n";
3601 } else
3602 memcpy(&pc, r, sizeof(struct protocol64_t));
3603 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3604 swapStruct(pc);
3605
3606 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3607
3608 outs() << "\t\t\t name ";
3609 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3610 info, n_value, pc.name);
3611 if (n_value != 0) {
3612 if (info->verbose && sym_name != nullptr)
3613 outs() << sym_name;
3614 else
3615 outs() << format("0x%" PRIx64, n_value);
3616 if (pc.name != 0)
3617 outs() << " + " << format("0x%" PRIx64, pc.name);
3618 } else
3619 outs() << format("0x%" PRIx64, pc.name);
3620 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3621 if (name != nullptr)
3622 outs() << format(" %.*s", left, name);
3623 outs() << "\n";
3624
3625 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3626
3627 outs() << "\t\t instanceMethods ";
3628 sym_name =
3629 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3630 S, info, n_value, pc.instanceMethods);
3631 if (n_value != 0) {
3632 if (info->verbose && sym_name != nullptr)
3633 outs() << sym_name;
3634 else
3635 outs() << format("0x%" PRIx64, n_value);
3636 if (pc.instanceMethods != 0)
3637 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3638 } else
3639 outs() << format("0x%" PRIx64, pc.instanceMethods);
3640 outs() << " (struct method_list_t *)\n";
3641 if (pc.instanceMethods + n_value != 0)
3642 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3643
3644 outs() << "\t\t classMethods ";
3645 sym_name =
3646 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3647 info, n_value, pc.classMethods);
3648 if (n_value != 0) {
3649 if (info->verbose && sym_name != nullptr)
3650 outs() << sym_name;
3651 else
3652 outs() << format("0x%" PRIx64, n_value);
3653 if (pc.classMethods != 0)
3654 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3655 } else
3656 outs() << format("0x%" PRIx64, pc.classMethods);
3657 outs() << " (struct method_list_t *)\n";
3658 if (pc.classMethods + n_value != 0)
3659 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3660
3661 outs() << "\t optionalInstanceMethods "
3662 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3663 outs() << "\t optionalClassMethods "
3664 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3665 outs() << "\t instanceProperties "
3666 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3667
3668 p += sizeof(uint64_t);
3669 offset += sizeof(uint64_t);
3670 }
3671}
3672
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003673static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3674 struct protocol_list32_t pl;
3675 uint32_t q;
3676 struct protocol32_t pc;
3677 const char *r;
3678 uint32_t offset, xoffset, left, i;
3679 SectionRef S, xS;
3680 const char *name;
3681
3682 r = get_pointer_32(p, offset, left, S, info);
3683 if (r == nullptr)
3684 return;
3685 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3686 if (left < sizeof(struct protocol_list32_t)) {
3687 memcpy(&pl, r, left);
3688 outs() << " (protocol_list_t entends past the end of the section)\n";
3689 } else
3690 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3691 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3692 swapStruct(pl);
3693 outs() << " count " << pl.count << "\n";
3694
3695 p += sizeof(struct protocol_list32_t);
3696 offset += sizeof(struct protocol_list32_t);
3697 for (i = 0; i < pl.count; i++) {
3698 r = get_pointer_32(p, offset, left, S, info);
3699 if (r == nullptr)
3700 return;
3701 q = 0;
3702 if (left < sizeof(uint32_t)) {
3703 memcpy(&q, r, left);
3704 outs() << " (protocol_t * entends past the end of the section)\n";
3705 } else
3706 memcpy(&q, r, sizeof(uint32_t));
3707 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3708 sys::swapByteOrder(q);
3709 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3710 << " (struct protocol_t *)\n";
3711 r = get_pointer_32(q, offset, left, S, info);
3712 if (r == nullptr)
3713 return;
3714 memset(&pc, '\0', sizeof(struct protocol32_t));
3715 if (left < sizeof(struct protocol32_t)) {
3716 memcpy(&pc, r, left);
3717 outs() << " (protocol_t entends past the end of the section)\n";
3718 } else
3719 memcpy(&pc, r, sizeof(struct protocol32_t));
3720 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3721 swapStruct(pc);
3722 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3723 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3724 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3725 if (name != nullptr)
3726 outs() << format(" %.*s", left, name);
3727 outs() << "\n";
3728 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3729 outs() << "\t\t instanceMethods "
3730 << format("0x%" PRIx32, pc.instanceMethods)
3731 << " (struct method_list_t *)\n";
3732 if (pc.instanceMethods != 0)
3733 print_method_list32_t(pc.instanceMethods, info, "\t");
3734 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3735 << " (struct method_list_t *)\n";
3736 if (pc.classMethods != 0)
3737 print_method_list32_t(pc.classMethods, info, "\t");
3738 outs() << "\t optionalInstanceMethods "
3739 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3740 outs() << "\t optionalClassMethods "
3741 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3742 outs() << "\t instanceProperties "
3743 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3744 p += sizeof(uint32_t);
3745 offset += sizeof(uint32_t);
3746 }
3747}
3748
Kevin Enderby846c0002015-04-16 17:19:59 +00003749static void print_indent(uint32_t indent) {
3750 for (uint32_t i = 0; i < indent;) {
3751 if (indent - i >= 8) {
3752 outs() << "\t";
3753 i += 8;
3754 } else {
3755 for (uint32_t j = i; j < indent; j++)
3756 outs() << " ";
3757 return;
3758 }
3759 }
3760}
3761
3762static bool print_method_description_list(uint32_t p, uint32_t indent,
3763 struct DisassembleInfo *info) {
3764 uint32_t offset, left, xleft;
3765 SectionRef S;
3766 struct objc_method_description_list_t mdl;
3767 struct objc_method_description_t md;
3768 const char *r, *list, *name;
3769 int32_t i;
3770
3771 r = get_pointer_32(p, offset, left, S, info, true);
3772 if (r == nullptr)
3773 return true;
3774
3775 outs() << "\n";
3776 if (left > sizeof(struct objc_method_description_list_t)) {
3777 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3778 } else {
3779 print_indent(indent);
3780 outs() << " objc_method_description_list extends past end of the section\n";
3781 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3782 memcpy(&mdl, r, left);
3783 }
3784 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3785 swapStruct(mdl);
3786
3787 print_indent(indent);
3788 outs() << " count " << mdl.count << "\n";
3789
3790 list = r + sizeof(struct objc_method_description_list_t);
3791 for (i = 0; i < mdl.count; i++) {
3792 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3793 print_indent(indent);
3794 outs() << " remaining list entries extend past the of the section\n";
3795 break;
3796 }
3797 print_indent(indent);
3798 outs() << " list[" << i << "]\n";
3799 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3800 sizeof(struct objc_method_description_t));
3801 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3802 swapStruct(md);
3803
3804 print_indent(indent);
3805 outs() << " name " << format("0x%08" PRIx32, md.name);
3806 if (info->verbose) {
3807 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3808 if (name != nullptr)
3809 outs() << format(" %.*s", xleft, name);
3810 else
3811 outs() << " (not in an __OBJC section)";
3812 }
3813 outs() << "\n";
3814
3815 print_indent(indent);
3816 outs() << " types " << format("0x%08" PRIx32, md.types);
3817 if (info->verbose) {
3818 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3819 if (name != nullptr)
3820 outs() << format(" %.*s", xleft, name);
3821 else
3822 outs() << " (not in an __OBJC section)";
3823 }
3824 outs() << "\n";
3825 }
3826 return false;
3827}
3828
3829static bool print_protocol_list(uint32_t p, uint32_t indent,
3830 struct DisassembleInfo *info);
3831
3832static bool print_protocol(uint32_t p, uint32_t indent,
3833 struct DisassembleInfo *info) {
3834 uint32_t offset, left;
3835 SectionRef S;
3836 struct objc_protocol_t protocol;
3837 const char *r, *name;
3838
3839 r = get_pointer_32(p, offset, left, S, info, true);
3840 if (r == nullptr)
3841 return true;
3842
3843 outs() << "\n";
3844 if (left >= sizeof(struct objc_protocol_t)) {
3845 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3846 } else {
3847 print_indent(indent);
3848 outs() << " Protocol extends past end of the section\n";
3849 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3850 memcpy(&protocol, r, left);
3851 }
3852 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3853 swapStruct(protocol);
3854
3855 print_indent(indent);
3856 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3857 << "\n";
3858
3859 print_indent(indent);
3860 outs() << " protocol_name "
3861 << format("0x%08" PRIx32, protocol.protocol_name);
3862 if (info->verbose) {
3863 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3864 if (name != nullptr)
3865 outs() << format(" %.*s", left, name);
3866 else
3867 outs() << " (not in an __OBJC section)";
3868 }
3869 outs() << "\n";
3870
3871 print_indent(indent);
3872 outs() << " protocol_list "
3873 << format("0x%08" PRIx32, protocol.protocol_list);
3874 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3875 outs() << " (not in an __OBJC section)\n";
3876
3877 print_indent(indent);
3878 outs() << " instance_methods "
3879 << format("0x%08" PRIx32, protocol.instance_methods);
3880 if (print_method_description_list(protocol.instance_methods, indent, info))
3881 outs() << " (not in an __OBJC section)\n";
3882
3883 print_indent(indent);
3884 outs() << " class_methods "
3885 << format("0x%08" PRIx32, protocol.class_methods);
3886 if (print_method_description_list(protocol.class_methods, indent, info))
3887 outs() << " (not in an __OBJC section)\n";
3888
3889 return false;
3890}
3891
3892static bool print_protocol_list(uint32_t p, uint32_t indent,
3893 struct DisassembleInfo *info) {
3894 uint32_t offset, left, l;
3895 SectionRef S;
3896 struct objc_protocol_list_t protocol_list;
3897 const char *r, *list;
3898 int32_t i;
3899
3900 r = get_pointer_32(p, offset, left, S, info, true);
3901 if (r == nullptr)
3902 return true;
3903
3904 outs() << "\n";
3905 if (left > sizeof(struct objc_protocol_list_t)) {
3906 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
3907 } else {
3908 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
3909 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
3910 memcpy(&protocol_list, r, left);
3911 }
3912 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3913 swapStruct(protocol_list);
3914
3915 print_indent(indent);
3916 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
3917 << "\n";
3918 print_indent(indent);
3919 outs() << " count " << protocol_list.count << "\n";
3920
3921 list = r + sizeof(struct objc_protocol_list_t);
3922 for (i = 0; i < protocol_list.count; i++) {
3923 if ((i + 1) * sizeof(uint32_t) > left) {
3924 outs() << "\t\t remaining list entries extend past the of the section\n";
3925 break;
3926 }
3927 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
3928 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3929 sys::swapByteOrder(l);
3930
3931 print_indent(indent);
3932 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
3933 if (print_protocol(l, indent, info))
3934 outs() << "(not in an __OBJC section)\n";
3935 }
3936 return false;
3937}
3938
Kevin Enderby0fc11822015-04-01 20:57:01 +00003939static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3940 struct ivar_list64_t il;
3941 struct ivar64_t i;
3942 const char *r;
3943 uint32_t offset, xoffset, left, j;
3944 SectionRef S, xS;
3945 const char *name, *sym_name, *ivar_offset_p;
3946 uint64_t ivar_offset, n_value;
3947
3948 r = get_pointer_64(p, offset, left, S, info);
3949 if (r == nullptr)
3950 return;
3951 memset(&il, '\0', sizeof(struct ivar_list64_t));
3952 if (left < sizeof(struct ivar_list64_t)) {
3953 memcpy(&il, r, left);
3954 outs() << " (ivar_list_t entends past the end of the section)\n";
3955 } else
3956 memcpy(&il, r, sizeof(struct ivar_list64_t));
3957 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3958 swapStruct(il);
3959 outs() << " entsize " << il.entsize << "\n";
3960 outs() << " count " << il.count << "\n";
3961
3962 p += sizeof(struct ivar_list64_t);
3963 offset += sizeof(struct ivar_list64_t);
3964 for (j = 0; j < il.count; j++) {
3965 r = get_pointer_64(p, offset, left, S, info);
3966 if (r == nullptr)
3967 return;
3968 memset(&i, '\0', sizeof(struct ivar64_t));
3969 if (left < sizeof(struct ivar64_t)) {
3970 memcpy(&i, r, left);
3971 outs() << " (ivar_t entends past the end of the section)\n";
3972 } else
3973 memcpy(&i, r, sizeof(struct ivar64_t));
3974 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3975 swapStruct(i);
3976
3977 outs() << "\t\t\t offset ";
3978 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
3979 info, n_value, i.offset);
3980 if (n_value != 0) {
3981 if (info->verbose && sym_name != nullptr)
3982 outs() << sym_name;
3983 else
3984 outs() << format("0x%" PRIx64, n_value);
3985 if (i.offset != 0)
3986 outs() << " + " << format("0x%" PRIx64, i.offset);
3987 } else
3988 outs() << format("0x%" PRIx64, i.offset);
3989 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
3990 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
3991 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
3992 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3993 sys::swapByteOrder(ivar_offset);
3994 outs() << " " << ivar_offset << "\n";
3995 } else
3996 outs() << "\n";
3997
3998 outs() << "\t\t\t name ";
3999 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4000 n_value, i.name);
4001 if (n_value != 0) {
4002 if (info->verbose && sym_name != nullptr)
4003 outs() << sym_name;
4004 else
4005 outs() << format("0x%" PRIx64, n_value);
4006 if (i.name != 0)
4007 outs() << " + " << format("0x%" PRIx64, i.name);
4008 } else
4009 outs() << format("0x%" PRIx64, i.name);
4010 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4011 if (name != nullptr)
4012 outs() << format(" %.*s", left, name);
4013 outs() << "\n";
4014
4015 outs() << "\t\t\t type ";
4016 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4017 n_value, i.name);
4018 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4019 if (n_value != 0) {
4020 if (info->verbose && sym_name != nullptr)
4021 outs() << sym_name;
4022 else
4023 outs() << format("0x%" PRIx64, n_value);
4024 if (i.type != 0)
4025 outs() << " + " << format("0x%" PRIx64, i.type);
4026 } else
4027 outs() << format("0x%" PRIx64, i.type);
4028 if (name != nullptr)
4029 outs() << format(" %.*s", left, name);
4030 outs() << "\n";
4031
4032 outs() << "\t\t\talignment " << i.alignment << "\n";
4033 outs() << "\t\t\t size " << i.size << "\n";
4034
4035 p += sizeof(struct ivar64_t);
4036 offset += sizeof(struct ivar64_t);
4037 }
4038}
4039
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004040static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4041 struct ivar_list32_t il;
4042 struct ivar32_t i;
4043 const char *r;
4044 uint32_t offset, xoffset, left, j;
4045 SectionRef S, xS;
4046 const char *name, *ivar_offset_p;
4047 uint32_t ivar_offset;
4048
4049 r = get_pointer_32(p, offset, left, S, info);
4050 if (r == nullptr)
4051 return;
4052 memset(&il, '\0', sizeof(struct ivar_list32_t));
4053 if (left < sizeof(struct ivar_list32_t)) {
4054 memcpy(&il, r, left);
4055 outs() << " (ivar_list_t entends past the end of the section)\n";
4056 } else
4057 memcpy(&il, r, sizeof(struct ivar_list32_t));
4058 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4059 swapStruct(il);
4060 outs() << " entsize " << il.entsize << "\n";
4061 outs() << " count " << il.count << "\n";
4062
4063 p += sizeof(struct ivar_list32_t);
4064 offset += sizeof(struct ivar_list32_t);
4065 for (j = 0; j < il.count; j++) {
4066 r = get_pointer_32(p, offset, left, S, info);
4067 if (r == nullptr)
4068 return;
4069 memset(&i, '\0', sizeof(struct ivar32_t));
4070 if (left < sizeof(struct ivar32_t)) {
4071 memcpy(&i, r, left);
4072 outs() << " (ivar_t entends past the end of the section)\n";
4073 } else
4074 memcpy(&i, r, sizeof(struct ivar32_t));
4075 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4076 swapStruct(i);
4077
4078 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4079 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4080 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4081 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4082 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4083 sys::swapByteOrder(ivar_offset);
4084 outs() << " " << ivar_offset << "\n";
4085 } else
4086 outs() << "\n";
4087
4088 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4089 name = get_pointer_32(i.name, xoffset, left, xS, info);
4090 if (name != nullptr)
4091 outs() << format(" %.*s", left, name);
4092 outs() << "\n";
4093
4094 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4095 name = get_pointer_32(i.type, xoffset, left, xS, info);
4096 if (name != nullptr)
4097 outs() << format(" %.*s", left, name);
4098 outs() << "\n";
4099
4100 outs() << "\t\t\talignment " << i.alignment << "\n";
4101 outs() << "\t\t\t size " << i.size << "\n";
4102
4103 p += sizeof(struct ivar32_t);
4104 offset += sizeof(struct ivar32_t);
4105 }
4106}
4107
Kevin Enderby0fc11822015-04-01 20:57:01 +00004108static void print_objc_property_list64(uint64_t p,
4109 struct DisassembleInfo *info) {
4110 struct objc_property_list64 opl;
4111 struct objc_property64 op;
4112 const char *r;
4113 uint32_t offset, xoffset, left, j;
4114 SectionRef S, xS;
4115 const char *name, *sym_name;
4116 uint64_t n_value;
4117
4118 r = get_pointer_64(p, offset, left, S, info);
4119 if (r == nullptr)
4120 return;
4121 memset(&opl, '\0', sizeof(struct objc_property_list64));
4122 if (left < sizeof(struct objc_property_list64)) {
4123 memcpy(&opl, r, left);
4124 outs() << " (objc_property_list entends past the end of the section)\n";
4125 } else
4126 memcpy(&opl, r, sizeof(struct objc_property_list64));
4127 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4128 swapStruct(opl);
4129 outs() << " entsize " << opl.entsize << "\n";
4130 outs() << " count " << opl.count << "\n";
4131
4132 p += sizeof(struct objc_property_list64);
4133 offset += sizeof(struct objc_property_list64);
4134 for (j = 0; j < opl.count; j++) {
4135 r = get_pointer_64(p, offset, left, S, info);
4136 if (r == nullptr)
4137 return;
4138 memset(&op, '\0', sizeof(struct objc_property64));
4139 if (left < sizeof(struct objc_property64)) {
4140 memcpy(&op, r, left);
4141 outs() << " (objc_property entends past the end of the section)\n";
4142 } else
4143 memcpy(&op, r, sizeof(struct objc_property64));
4144 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4145 swapStruct(op);
4146
4147 outs() << "\t\t\t name ";
4148 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4149 info, n_value, op.name);
4150 if (n_value != 0) {
4151 if (info->verbose && sym_name != nullptr)
4152 outs() << sym_name;
4153 else
4154 outs() << format("0x%" PRIx64, n_value);
4155 if (op.name != 0)
4156 outs() << " + " << format("0x%" PRIx64, op.name);
4157 } else
4158 outs() << format("0x%" PRIx64, op.name);
4159 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4160 if (name != nullptr)
4161 outs() << format(" %.*s", left, name);
4162 outs() << "\n";
4163
4164 outs() << "\t\t\tattributes ";
4165 sym_name =
4166 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4167 info, n_value, op.attributes);
4168 if (n_value != 0) {
4169 if (info->verbose && sym_name != nullptr)
4170 outs() << sym_name;
4171 else
4172 outs() << format("0x%" PRIx64, n_value);
4173 if (op.attributes != 0)
4174 outs() << " + " << format("0x%" PRIx64, op.attributes);
4175 } else
4176 outs() << format("0x%" PRIx64, op.attributes);
4177 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4178 if (name != nullptr)
4179 outs() << format(" %.*s", left, name);
4180 outs() << "\n";
4181
4182 p += sizeof(struct objc_property64);
4183 offset += sizeof(struct objc_property64);
4184 }
4185}
4186
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004187static void print_objc_property_list32(uint32_t p,
4188 struct DisassembleInfo *info) {
4189 struct objc_property_list32 opl;
4190 struct objc_property32 op;
4191 const char *r;
4192 uint32_t offset, xoffset, left, j;
4193 SectionRef S, xS;
4194 const char *name;
4195
4196 r = get_pointer_32(p, offset, left, S, info);
4197 if (r == nullptr)
4198 return;
4199 memset(&opl, '\0', sizeof(struct objc_property_list32));
4200 if (left < sizeof(struct objc_property_list32)) {
4201 memcpy(&opl, r, left);
4202 outs() << " (objc_property_list entends past the end of the section)\n";
4203 } else
4204 memcpy(&opl, r, sizeof(struct objc_property_list32));
4205 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4206 swapStruct(opl);
4207 outs() << " entsize " << opl.entsize << "\n";
4208 outs() << " count " << opl.count << "\n";
4209
4210 p += sizeof(struct objc_property_list32);
4211 offset += sizeof(struct objc_property_list32);
4212 for (j = 0; j < opl.count; j++) {
4213 r = get_pointer_32(p, offset, left, S, info);
4214 if (r == nullptr)
4215 return;
4216 memset(&op, '\0', sizeof(struct objc_property32));
4217 if (left < sizeof(struct objc_property32)) {
4218 memcpy(&op, r, left);
4219 outs() << " (objc_property entends past the end of the section)\n";
4220 } else
4221 memcpy(&op, r, sizeof(struct objc_property32));
4222 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4223 swapStruct(op);
4224
4225 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4226 name = get_pointer_32(op.name, xoffset, left, xS, info);
4227 if (name != nullptr)
4228 outs() << format(" %.*s", left, name);
4229 outs() << "\n";
4230
4231 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4232 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4233 if (name != nullptr)
4234 outs() << format(" %.*s", left, name);
4235 outs() << "\n";
4236
4237 p += sizeof(struct objc_property32);
4238 offset += sizeof(struct objc_property32);
4239 }
4240}
4241
Kevin Enderby0fc11822015-04-01 20:57:01 +00004242static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4243 bool &is_meta_class) {
4244 struct class_ro64_t cro;
4245 const char *r;
4246 uint32_t offset, xoffset, left;
4247 SectionRef S, xS;
4248 const char *name, *sym_name;
4249 uint64_t n_value;
4250
4251 r = get_pointer_64(p, offset, left, S, info);
4252 if (r == nullptr || left < sizeof(struct class_ro64_t))
4253 return;
4254 memset(&cro, '\0', sizeof(struct class_ro64_t));
4255 if (left < sizeof(struct class_ro64_t)) {
4256 memcpy(&cro, r, left);
4257 outs() << " (class_ro_t entends past the end of the section)\n";
4258 } else
4259 memcpy(&cro, r, sizeof(struct class_ro64_t));
4260 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4261 swapStruct(cro);
4262 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4263 if (cro.flags & RO_META)
4264 outs() << " RO_META";
4265 if (cro.flags & RO_ROOT)
4266 outs() << " RO_ROOT";
4267 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4268 outs() << " RO_HAS_CXX_STRUCTORS";
4269 outs() << "\n";
4270 outs() << " instanceStart " << cro.instanceStart << "\n";
4271 outs() << " instanceSize " << cro.instanceSize << "\n";
4272 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4273 << "\n";
4274 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4275 << "\n";
4276 print_layout_map64(cro.ivarLayout, info);
4277
4278 outs() << " name ";
4279 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4280 info, n_value, cro.name);
4281 if (n_value != 0) {
4282 if (info->verbose && sym_name != nullptr)
4283 outs() << sym_name;
4284 else
4285 outs() << format("0x%" PRIx64, n_value);
4286 if (cro.name != 0)
4287 outs() << " + " << format("0x%" PRIx64, cro.name);
4288 } else
4289 outs() << format("0x%" PRIx64, cro.name);
4290 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4291 if (name != nullptr)
4292 outs() << format(" %.*s", left, name);
4293 outs() << "\n";
4294
4295 outs() << " baseMethods ";
4296 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4297 S, info, n_value, cro.baseMethods);
4298 if (n_value != 0) {
4299 if (info->verbose && sym_name != nullptr)
4300 outs() << sym_name;
4301 else
4302 outs() << format("0x%" PRIx64, n_value);
4303 if (cro.baseMethods != 0)
4304 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4305 } else
4306 outs() << format("0x%" PRIx64, cro.baseMethods);
4307 outs() << " (struct method_list_t *)\n";
4308 if (cro.baseMethods + n_value != 0)
4309 print_method_list64_t(cro.baseMethods + n_value, info, "");
4310
4311 outs() << " baseProtocols ";
4312 sym_name =
4313 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4314 info, n_value, cro.baseProtocols);
4315 if (n_value != 0) {
4316 if (info->verbose && sym_name != nullptr)
4317 outs() << sym_name;
4318 else
4319 outs() << format("0x%" PRIx64, n_value);
4320 if (cro.baseProtocols != 0)
4321 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4322 } else
4323 outs() << format("0x%" PRIx64, cro.baseProtocols);
4324 outs() << "\n";
4325 if (cro.baseProtocols + n_value != 0)
4326 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4327
4328 outs() << " ivars ";
4329 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004330 info, n_value, cro.ivars);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004331 if (n_value != 0) {
4332 if (info->verbose && sym_name != nullptr)
4333 outs() << sym_name;
4334 else
4335 outs() << format("0x%" PRIx64, n_value);
4336 if (cro.ivars != 0)
4337 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4338 } else
4339 outs() << format("0x%" PRIx64, cro.ivars);
4340 outs() << "\n";
4341 if (cro.ivars + n_value != 0)
4342 print_ivar_list64_t(cro.ivars + n_value, info);
4343
4344 outs() << " weakIvarLayout ";
4345 sym_name =
4346 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4347 info, n_value, cro.weakIvarLayout);
4348 if (n_value != 0) {
4349 if (info->verbose && sym_name != nullptr)
4350 outs() << sym_name;
4351 else
4352 outs() << format("0x%" PRIx64, n_value);
4353 if (cro.weakIvarLayout != 0)
4354 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4355 } else
4356 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4357 outs() << "\n";
4358 print_layout_map64(cro.weakIvarLayout + n_value, info);
4359
4360 outs() << " baseProperties ";
4361 sym_name =
4362 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4363 info, n_value, cro.baseProperties);
4364 if (n_value != 0) {
4365 if (info->verbose && sym_name != nullptr)
4366 outs() << sym_name;
4367 else
4368 outs() << format("0x%" PRIx64, n_value);
4369 if (cro.baseProperties != 0)
4370 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4371 } else
4372 outs() << format("0x%" PRIx64, cro.baseProperties);
4373 outs() << "\n";
4374 if (cro.baseProperties + n_value != 0)
4375 print_objc_property_list64(cro.baseProperties + n_value, info);
4376
4377 is_meta_class = (cro.flags & RO_META) ? true : false;
4378}
4379
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004380static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4381 bool &is_meta_class) {
4382 struct class_ro32_t cro;
4383 const char *r;
4384 uint32_t offset, xoffset, left;
4385 SectionRef S, xS;
4386 const char *name;
4387
4388 r = get_pointer_32(p, offset, left, S, info);
4389 if (r == nullptr)
4390 return;
4391 memset(&cro, '\0', sizeof(struct class_ro32_t));
4392 if (left < sizeof(struct class_ro32_t)) {
4393 memcpy(&cro, r, left);
4394 outs() << " (class_ro_t entends past the end of the section)\n";
4395 } else
4396 memcpy(&cro, r, sizeof(struct class_ro32_t));
4397 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4398 swapStruct(cro);
4399 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4400 if (cro.flags & RO_META)
4401 outs() << " RO_META";
4402 if (cro.flags & RO_ROOT)
4403 outs() << " RO_ROOT";
4404 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4405 outs() << " RO_HAS_CXX_STRUCTORS";
4406 outs() << "\n";
4407 outs() << " instanceStart " << cro.instanceStart << "\n";
4408 outs() << " instanceSize " << cro.instanceSize << "\n";
4409 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4410 << "\n";
4411 print_layout_map32(cro.ivarLayout, info);
4412
4413 outs() << " name " << format("0x%" PRIx32, cro.name);
4414 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4415 if (name != nullptr)
4416 outs() << format(" %.*s", left, name);
4417 outs() << "\n";
4418
4419 outs() << " baseMethods "
4420 << format("0x%" PRIx32, cro.baseMethods)
4421 << " (struct method_list_t *)\n";
4422 if (cro.baseMethods != 0)
4423 print_method_list32_t(cro.baseMethods, info, "");
4424
4425 outs() << " baseProtocols "
4426 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4427 if (cro.baseProtocols != 0)
4428 print_protocol_list32_t(cro.baseProtocols, info);
4429 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4430 << "\n";
4431 if (cro.ivars != 0)
4432 print_ivar_list32_t(cro.ivars, info);
4433 outs() << " weakIvarLayout "
4434 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4435 print_layout_map32(cro.weakIvarLayout, info);
4436 outs() << " baseProperties "
4437 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4438 if (cro.baseProperties != 0)
4439 print_objc_property_list32(cro.baseProperties, info);
4440 is_meta_class = (cro.flags & RO_META) ? true : false;
4441}
4442
Kevin Enderby0fc11822015-04-01 20:57:01 +00004443static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4444 struct class64_t c;
4445 const char *r;
4446 uint32_t offset, left;
4447 SectionRef S;
4448 const char *name;
4449 uint64_t isa_n_value, n_value;
4450
4451 r = get_pointer_64(p, offset, left, S, info);
4452 if (r == nullptr || left < sizeof(struct class64_t))
4453 return;
4454 memset(&c, '\0', sizeof(struct class64_t));
4455 if (left < sizeof(struct class64_t)) {
4456 memcpy(&c, r, left);
4457 outs() << " (class_t entends past the end of the section)\n";
4458 } else
4459 memcpy(&c, r, sizeof(struct class64_t));
4460 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4461 swapStruct(c);
4462
4463 outs() << " isa " << format("0x%" PRIx64, c.isa);
4464 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4465 isa_n_value, c.isa);
4466 if (name != nullptr)
4467 outs() << " " << name;
4468 outs() << "\n";
4469
4470 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4471 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4472 n_value, c.superclass);
4473 if (name != nullptr)
4474 outs() << " " << name;
4475 outs() << "\n";
4476
4477 outs() << " cache " << format("0x%" PRIx64, c.cache);
4478 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4479 n_value, c.cache);
4480 if (name != nullptr)
4481 outs() << " " << name;
4482 outs() << "\n";
4483
4484 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4485 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4486 n_value, c.vtable);
4487 if (name != nullptr)
4488 outs() << " " << name;
4489 outs() << "\n";
4490
4491 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4492 n_value, c.data);
4493 outs() << " data ";
4494 if (n_value != 0) {
4495 if (info->verbose && name != nullptr)
4496 outs() << name;
4497 else
4498 outs() << format("0x%" PRIx64, n_value);
4499 if (c.data != 0)
4500 outs() << " + " << format("0x%" PRIx64, c.data);
4501 } else
4502 outs() << format("0x%" PRIx64, c.data);
4503 outs() << " (struct class_ro_t *)";
4504
4505 // This is a Swift class if some of the low bits of the pointer are set.
4506 if ((c.data + n_value) & 0x7)
4507 outs() << " Swift class";
4508 outs() << "\n";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004509 bool is_meta_class;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004510 print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class);
4511
4512 if (is_meta_class == false) {
4513 outs() << "Meta Class\n";
4514 print_class64_t(c.isa + isa_n_value, info);
4515 }
4516}
4517
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004518static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4519 struct class32_t c;
4520 const char *r;
4521 uint32_t offset, left;
4522 SectionRef S;
4523 const char *name;
4524
4525 r = get_pointer_32(p, offset, left, S, info);
4526 if (r == nullptr)
4527 return;
4528 memset(&c, '\0', sizeof(struct class32_t));
4529 if (left < sizeof(struct class32_t)) {
4530 memcpy(&c, r, left);
4531 outs() << " (class_t entends past the end of the section)\n";
4532 } else
4533 memcpy(&c, r, sizeof(struct class32_t));
4534 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4535 swapStruct(c);
4536
4537 outs() << " isa " << format("0x%" PRIx32, c.isa);
4538 name =
4539 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4540 if (name != nullptr)
4541 outs() << " " << name;
4542 outs() << "\n";
4543
4544 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4545 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4546 c.superclass);
4547 if (name != nullptr)
4548 outs() << " " << name;
4549 outs() << "\n";
4550
4551 outs() << " cache " << format("0x%" PRIx32, c.cache);
4552 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4553 c.cache);
4554 if (name != nullptr)
4555 outs() << " " << name;
4556 outs() << "\n";
4557
4558 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4559 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4560 c.vtable);
4561 if (name != nullptr)
4562 outs() << " " << name;
4563 outs() << "\n";
4564
4565 name =
4566 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4567 outs() << " data " << format("0x%" PRIx32, c.data)
4568 << " (struct class_ro_t *)";
4569
4570 // This is a Swift class if some of the low bits of the pointer are set.
4571 if (c.data & 0x3)
4572 outs() << " Swift class";
4573 outs() << "\n";
4574 bool is_meta_class;
4575 print_class_ro32_t(c.data & ~0x3, info, is_meta_class);
4576
4577 if (is_meta_class == false) {
4578 outs() << "Meta Class\n";
4579 print_class32_t(c.isa, info);
4580 }
4581}
4582
Kevin Enderby846c0002015-04-16 17:19:59 +00004583static void print_objc_class_t(struct objc_class_t *objc_class,
4584 struct DisassembleInfo *info) {
4585 uint32_t offset, left, xleft;
4586 const char *name, *p, *ivar_list;
4587 SectionRef S;
4588 int32_t i;
4589 struct objc_ivar_list_t objc_ivar_list;
4590 struct objc_ivar_t ivar;
4591
4592 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4593 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4594 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4595 if (name != nullptr)
4596 outs() << format(" %.*s", left, name);
4597 else
4598 outs() << " (not in an __OBJC section)";
4599 }
4600 outs() << "\n";
4601
4602 outs() << "\t super_class "
4603 << format("0x%08" PRIx32, objc_class->super_class);
4604 if (info->verbose) {
4605 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4606 if (name != nullptr)
4607 outs() << format(" %.*s", left, name);
4608 else
4609 outs() << " (not in an __OBJC section)";
4610 }
4611 outs() << "\n";
4612
4613 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4614 if (info->verbose) {
4615 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4616 if (name != nullptr)
4617 outs() << format(" %.*s", left, name);
4618 else
4619 outs() << " (not in an __OBJC section)";
4620 }
4621 outs() << "\n";
4622
4623 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4624 << "\n";
4625
4626 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4627 if (info->verbose) {
4628 if (CLS_GETINFO(objc_class, CLS_CLASS))
4629 outs() << " CLS_CLASS";
4630 else if (CLS_GETINFO(objc_class, CLS_META))
4631 outs() << " CLS_META";
4632 }
4633 outs() << "\n";
4634
4635 outs() << "\t instance_size "
4636 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4637
4638 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4639 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4640 if (p != nullptr) {
4641 if (left > sizeof(struct objc_ivar_list_t)) {
4642 outs() << "\n";
4643 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4644 } else {
4645 outs() << " (entends past the end of the section)\n";
4646 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4647 memcpy(&objc_ivar_list, p, left);
4648 }
4649 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4650 swapStruct(objc_ivar_list);
4651 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4652 ivar_list = p + sizeof(struct objc_ivar_list_t);
4653 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4654 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4655 outs() << "\t\t remaining ivar's extend past the of the section\n";
4656 break;
4657 }
4658 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4659 sizeof(struct objc_ivar_t));
4660 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4661 swapStruct(ivar);
4662
4663 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4664 if (info->verbose) {
4665 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4666 if (name != nullptr)
4667 outs() << format(" %.*s", xleft, name);
4668 else
4669 outs() << " (not in an __OBJC section)";
4670 }
4671 outs() << "\n";
4672
4673 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4674 if (info->verbose) {
4675 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4676 if (name != nullptr)
4677 outs() << format(" %.*s", xleft, name);
4678 else
4679 outs() << " (not in an __OBJC section)";
4680 }
4681 outs() << "\n";
4682
4683 outs() << "\t\t ivar_offset "
4684 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4685 }
4686 } else {
4687 outs() << " (not in an __OBJC section)\n";
4688 }
4689
4690 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4691 if (print_method_list(objc_class->methodLists, info))
4692 outs() << " (not in an __OBJC section)\n";
4693
4694 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4695 << "\n";
4696
4697 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4698 if (print_protocol_list(objc_class->protocols, 16, info))
4699 outs() << " (not in an __OBJC section)\n";
4700}
4701
4702static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4703 struct DisassembleInfo *info) {
4704 uint32_t offset, left;
4705 const char *name;
4706 SectionRef S;
4707
4708 outs() << "\t category name "
4709 << format("0x%08" PRIx32, objc_category->category_name);
4710 if (info->verbose) {
4711 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4712 true);
4713 if (name != nullptr)
4714 outs() << format(" %.*s", left, name);
4715 else
4716 outs() << " (not in an __OBJC section)";
4717 }
4718 outs() << "\n";
4719
4720 outs() << "\t\t class name "
4721 << format("0x%08" PRIx32, objc_category->class_name);
4722 if (info->verbose) {
4723 name =
4724 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4725 if (name != nullptr)
4726 outs() << format(" %.*s", left, name);
4727 else
4728 outs() << " (not in an __OBJC section)";
4729 }
4730 outs() << "\n";
4731
4732 outs() << "\t instance methods "
4733 << format("0x%08" PRIx32, objc_category->instance_methods);
4734 if (print_method_list(objc_category->instance_methods, info))
4735 outs() << " (not in an __OBJC section)\n";
4736
4737 outs() << "\t class methods "
4738 << format("0x%08" PRIx32, objc_category->class_methods);
4739 if (print_method_list(objc_category->class_methods, info))
4740 outs() << " (not in an __OBJC section)\n";
4741}
4742
Kevin Enderby0fc11822015-04-01 20:57:01 +00004743static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4744 struct category64_t c;
4745 const char *r;
4746 uint32_t offset, xoffset, left;
4747 SectionRef S, xS;
4748 const char *name, *sym_name;
4749 uint64_t n_value;
4750
4751 r = get_pointer_64(p, offset, left, S, info);
4752 if (r == nullptr)
4753 return;
4754 memset(&c, '\0', sizeof(struct category64_t));
4755 if (left < sizeof(struct category64_t)) {
4756 memcpy(&c, r, left);
4757 outs() << " (category_t entends past the end of the section)\n";
4758 } else
4759 memcpy(&c, r, sizeof(struct category64_t));
4760 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4761 swapStruct(c);
4762
4763 outs() << " name ";
4764 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4765 info, n_value, c.name);
4766 if (n_value != 0) {
4767 if (info->verbose && sym_name != nullptr)
4768 outs() << sym_name;
4769 else
4770 outs() << format("0x%" PRIx64, n_value);
4771 if (c.name != 0)
4772 outs() << " + " << format("0x%" PRIx64, c.name);
4773 } else
4774 outs() << format("0x%" PRIx64, c.name);
4775 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4776 if (name != nullptr)
4777 outs() << format(" %.*s", left, name);
4778 outs() << "\n";
4779
4780 outs() << " cls ";
4781 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4782 n_value, c.cls);
4783 if (n_value != 0) {
4784 if (info->verbose && sym_name != nullptr)
4785 outs() << sym_name;
4786 else
4787 outs() << format("0x%" PRIx64, n_value);
4788 if (c.cls != 0)
4789 outs() << " + " << format("0x%" PRIx64, c.cls);
4790 } else
4791 outs() << format("0x%" PRIx64, c.cls);
4792 outs() << "\n";
4793 if (c.cls + n_value != 0)
4794 print_class64_t(c.cls + n_value, info);
4795
4796 outs() << " instanceMethods ";
4797 sym_name =
4798 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4799 info, n_value, c.instanceMethods);
4800 if (n_value != 0) {
4801 if (info->verbose && sym_name != nullptr)
4802 outs() << sym_name;
4803 else
4804 outs() << format("0x%" PRIx64, n_value);
4805 if (c.instanceMethods != 0)
4806 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4807 } else
4808 outs() << format("0x%" PRIx64, c.instanceMethods);
4809 outs() << "\n";
4810 if (c.instanceMethods + n_value != 0)
4811 print_method_list64_t(c.instanceMethods + n_value, info, "");
4812
4813 outs() << " classMethods ";
4814 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4815 S, info, n_value, c.classMethods);
4816 if (n_value != 0) {
4817 if (info->verbose && sym_name != nullptr)
4818 outs() << sym_name;
4819 else
4820 outs() << format("0x%" PRIx64, n_value);
4821 if (c.classMethods != 0)
4822 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4823 } else
4824 outs() << format("0x%" PRIx64, c.classMethods);
4825 outs() << "\n";
4826 if (c.classMethods + n_value != 0)
4827 print_method_list64_t(c.classMethods + n_value, info, "");
4828
4829 outs() << " protocols ";
4830 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4831 info, n_value, c.protocols);
4832 if (n_value != 0) {
4833 if (info->verbose && sym_name != nullptr)
4834 outs() << sym_name;
4835 else
4836 outs() << format("0x%" PRIx64, n_value);
4837 if (c.protocols != 0)
4838 outs() << " + " << format("0x%" PRIx64, c.protocols);
4839 } else
4840 outs() << format("0x%" PRIx64, c.protocols);
4841 outs() << "\n";
4842 if (c.protocols + n_value != 0)
4843 print_protocol_list64_t(c.protocols + n_value, info);
4844
4845 outs() << "instanceProperties ";
4846 sym_name =
4847 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4848 S, info, n_value, c.instanceProperties);
4849 if (n_value != 0) {
4850 if (info->verbose && sym_name != nullptr)
4851 outs() << sym_name;
4852 else
4853 outs() << format("0x%" PRIx64, n_value);
4854 if (c.instanceProperties != 0)
4855 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4856 } else
4857 outs() << format("0x%" PRIx64, c.instanceProperties);
4858 outs() << "\n";
4859 if (c.instanceProperties + n_value != 0)
4860 print_objc_property_list64(c.instanceProperties + n_value, info);
4861}
4862
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004863static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4864 struct category32_t c;
4865 const char *r;
4866 uint32_t offset, left;
4867 SectionRef S, xS;
4868 const char *name;
4869
4870 r = get_pointer_32(p, offset, left, S, info);
4871 if (r == nullptr)
4872 return;
4873 memset(&c, '\0', sizeof(struct category32_t));
4874 if (left < sizeof(struct category32_t)) {
4875 memcpy(&c, r, left);
4876 outs() << " (category_t entends past the end of the section)\n";
4877 } else
4878 memcpy(&c, r, sizeof(struct category32_t));
4879 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4880 swapStruct(c);
4881
4882 outs() << " name " << format("0x%" PRIx32, c.name);
4883 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4884 c.name);
4885 if (name != NULL)
4886 outs() << " " << name;
4887 outs() << "\n";
4888
4889 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
4890 if (c.cls != 0)
4891 print_class32_t(c.cls, info);
4892 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4893 << "\n";
4894 if (c.instanceMethods != 0)
4895 print_method_list32_t(c.instanceMethods, info, "");
4896 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
4897 << "\n";
4898 if (c.classMethods != 0)
4899 print_method_list32_t(c.classMethods, info, "");
4900 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
4901 if (c.protocols != 0)
4902 print_protocol_list32_t(c.protocols, info);
4903 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
4904 << "\n";
4905 if (c.instanceProperties != 0)
4906 print_objc_property_list32(c.instanceProperties, info);
4907}
4908
Kevin Enderby0fc11822015-04-01 20:57:01 +00004909static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
4910 uint32_t i, left, offset, xoffset;
4911 uint64_t p, n_value;
4912 struct message_ref64 mr;
4913 const char *name, *sym_name;
4914 const char *r;
4915 SectionRef xS;
4916
4917 if (S == SectionRef())
4918 return;
4919
4920 StringRef SectName;
4921 S.getName(SectName);
4922 DataRefImpl Ref = S.getRawDataRefImpl();
4923 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4924 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4925 offset = 0;
4926 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4927 p = S.getAddress() + i;
4928 r = get_pointer_64(p, offset, left, S, info);
4929 if (r == nullptr)
4930 return;
4931 memset(&mr, '\0', sizeof(struct message_ref64));
4932 if (left < sizeof(struct message_ref64)) {
4933 memcpy(&mr, r, left);
4934 outs() << " (message_ref entends past the end of the section)\n";
4935 } else
4936 memcpy(&mr, r, sizeof(struct message_ref64));
4937 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4938 swapStruct(mr);
4939
4940 outs() << " imp ";
4941 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
4942 n_value, mr.imp);
4943 if (n_value != 0) {
4944 outs() << format("0x%" PRIx64, n_value) << " ";
4945 if (mr.imp != 0)
4946 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
4947 } else
4948 outs() << format("0x%" PRIx64, mr.imp) << " ";
4949 if (name != nullptr)
4950 outs() << " " << name;
4951 outs() << "\n";
4952
4953 outs() << " sel ";
4954 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
4955 info, n_value, mr.sel);
4956 if (n_value != 0) {
4957 if (info->verbose && sym_name != nullptr)
4958 outs() << sym_name;
4959 else
4960 outs() << format("0x%" PRIx64, n_value);
4961 if (mr.sel != 0)
4962 outs() << " + " << format("0x%" PRIx64, mr.sel);
4963 } else
4964 outs() << format("0x%" PRIx64, mr.sel);
4965 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
4966 if (name != nullptr)
4967 outs() << format(" %.*s", left, name);
4968 outs() << "\n";
4969
4970 offset += sizeof(struct message_ref64);
4971 }
4972}
4973
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004974static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
4975 uint32_t i, left, offset, xoffset, p;
4976 struct message_ref32 mr;
4977 const char *name, *r;
4978 SectionRef xS;
4979
4980 if (S == SectionRef())
4981 return;
4982
4983 StringRef SectName;
4984 S.getName(SectName);
4985 DataRefImpl Ref = S.getRawDataRefImpl();
4986 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4987 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4988 offset = 0;
4989 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4990 p = S.getAddress() + i;
4991 r = get_pointer_32(p, offset, left, S, info);
4992 if (r == nullptr)
4993 return;
4994 memset(&mr, '\0', sizeof(struct message_ref32));
4995 if (left < sizeof(struct message_ref32)) {
4996 memcpy(&mr, r, left);
4997 outs() << " (message_ref entends past the end of the section)\n";
4998 } else
4999 memcpy(&mr, r, sizeof(struct message_ref32));
5000 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5001 swapStruct(mr);
5002
5003 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5004 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5005 mr.imp);
5006 if (name != nullptr)
5007 outs() << " " << name;
5008 outs() << "\n";
5009
5010 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5011 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5012 if (name != nullptr)
5013 outs() << " " << name;
5014 outs() << "\n";
5015
5016 offset += sizeof(struct message_ref32);
5017 }
5018}
5019
Kevin Enderby0fc11822015-04-01 20:57:01 +00005020static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5021 uint32_t left, offset, swift_version;
5022 uint64_t p;
5023 struct objc_image_info64 o;
5024 const char *r;
5025
5026 StringRef SectName;
5027 S.getName(SectName);
5028 DataRefImpl Ref = S.getRawDataRefImpl();
5029 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5030 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5031 p = S.getAddress();
5032 r = get_pointer_64(p, offset, left, S, info);
5033 if (r == nullptr)
5034 return;
5035 memset(&o, '\0', sizeof(struct objc_image_info64));
5036 if (left < sizeof(struct objc_image_info64)) {
5037 memcpy(&o, r, left);
5038 outs() << " (objc_image_info entends past the end of the section)\n";
5039 } else
5040 memcpy(&o, r, sizeof(struct objc_image_info64));
5041 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5042 swapStruct(o);
5043 outs() << " version " << o.version << "\n";
5044 outs() << " flags " << format("0x%" PRIx32, o.flags);
5045 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5046 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5047 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5048 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5049 swift_version = (o.flags >> 8) & 0xff;
5050 if (swift_version != 0) {
5051 if (swift_version == 1)
5052 outs() << " Swift 1.0";
5053 else if (swift_version == 2)
5054 outs() << " Swift 1.1";
5055 else
5056 outs() << " unknown future Swift version (" << swift_version << ")";
5057 }
5058 outs() << "\n";
5059}
5060
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005061static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5062 uint32_t left, offset, swift_version, p;
5063 struct objc_image_info32 o;
5064 const char *r;
5065
5066 StringRef SectName;
5067 S.getName(SectName);
5068 DataRefImpl Ref = S.getRawDataRefImpl();
5069 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5070 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5071 p = S.getAddress();
5072 r = get_pointer_32(p, offset, left, S, info);
5073 if (r == nullptr)
5074 return;
5075 memset(&o, '\0', sizeof(struct objc_image_info32));
5076 if (left < sizeof(struct objc_image_info32)) {
5077 memcpy(&o, r, left);
5078 outs() << " (objc_image_info entends past the end of the section)\n";
5079 } else
5080 memcpy(&o, r, sizeof(struct objc_image_info32));
5081 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5082 swapStruct(o);
5083 outs() << " version " << o.version << "\n";
5084 outs() << " flags " << format("0x%" PRIx32, o.flags);
5085 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5086 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5087 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5088 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5089 swift_version = (o.flags >> 8) & 0xff;
5090 if (swift_version != 0) {
5091 if (swift_version == 1)
5092 outs() << " Swift 1.0";
5093 else if (swift_version == 2)
5094 outs() << " Swift 1.1";
5095 else
5096 outs() << " unknown future Swift version (" << swift_version << ")";
5097 }
5098 outs() << "\n";
5099}
5100
Kevin Enderby846c0002015-04-16 17:19:59 +00005101static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5102 uint32_t left, offset, p;
5103 struct imageInfo_t o;
5104 const char *r;
5105
5106 StringRef SectName;
5107 S.getName(SectName);
5108 DataRefImpl Ref = S.getRawDataRefImpl();
5109 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5110 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5111 p = S.getAddress();
5112 r = get_pointer_32(p, offset, left, S, info);
5113 if (r == nullptr)
5114 return;
5115 memset(&o, '\0', sizeof(struct imageInfo_t));
5116 if (left < sizeof(struct imageInfo_t)) {
5117 memcpy(&o, r, left);
5118 outs() << " (imageInfo entends past the end of the section)\n";
5119 } else
5120 memcpy(&o, r, sizeof(struct imageInfo_t));
5121 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5122 swapStruct(o);
5123 outs() << " version " << o.version << "\n";
5124 outs() << " flags " << format("0x%" PRIx32, o.flags);
5125 if (o.flags & 0x1)
5126 outs() << " F&C";
5127 if (o.flags & 0x2)
5128 outs() << " GC";
5129 if (o.flags & 0x4)
5130 outs() << " GC-only";
5131 else
5132 outs() << " RR";
5133 outs() << "\n";
5134}
5135
Kevin Enderby0fc11822015-04-01 20:57:01 +00005136static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5137 SymbolAddressMap AddrMap;
5138 if (verbose)
5139 CreateSymbolAddressMap(O, &AddrMap);
5140
5141 std::vector<SectionRef> Sections;
5142 for (const SectionRef &Section : O->sections()) {
5143 StringRef SectName;
5144 Section.getName(SectName);
5145 Sections.push_back(Section);
5146 }
5147
5148 struct DisassembleInfo info;
5149 // Set up the block of info used by the Symbolizer call backs.
5150 info.verbose = verbose;
5151 info.O = O;
5152 info.AddrMap = &AddrMap;
5153 info.Sections = &Sections;
5154 info.class_name = nullptr;
5155 info.selector_name = nullptr;
5156 info.method = nullptr;
5157 info.demangled_name = nullptr;
5158 info.bindtable = nullptr;
5159 info.adrp_addr = 0;
5160 info.adrp_inst = 0;
5161
5162 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5163 if (CL != SectionRef()) {
5164 info.S = CL;
5165 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5166 } else {
5167 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5168 info.S = CL;
5169 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5170 }
5171
5172 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5173 if (CR != SectionRef()) {
5174 info.S = CR;
5175 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5176 } else {
5177 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5178 info.S = CR;
5179 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5180 }
5181
5182 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5183 if (SR != SectionRef()) {
5184 info.S = SR;
5185 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5186 } else {
5187 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5188 info.S = SR;
5189 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5190 }
5191
5192 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5193 if (CA != SectionRef()) {
5194 info.S = CA;
5195 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5196 } else {
5197 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5198 info.S = CA;
5199 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5200 }
5201
5202 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5203 if (PL != SectionRef()) {
5204 info.S = PL;
5205 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5206 } else {
5207 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5208 info.S = PL;
5209 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5210 }
5211
5212 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5213 if (MR != SectionRef()) {
5214 info.S = MR;
5215 print_message_refs64(MR, &info);
5216 } else {
5217 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5218 info.S = MR;
5219 print_message_refs64(MR, &info);
5220 }
5221
5222 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5223 if (II != SectionRef()) {
5224 info.S = II;
5225 print_image_info64(II, &info);
5226 } else {
5227 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5228 info.S = II;
5229 print_image_info64(II, &info);
5230 }
Kevin Enderby0bc6ed42015-04-01 21:50:45 +00005231
5232 if (info.bindtable != nullptr)
5233 delete info.bindtable;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005234}
5235
5236static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005237 SymbolAddressMap AddrMap;
5238 if (verbose)
5239 CreateSymbolAddressMap(O, &AddrMap);
5240
5241 std::vector<SectionRef> Sections;
5242 for (const SectionRef &Section : O->sections()) {
5243 StringRef SectName;
5244 Section.getName(SectName);
5245 Sections.push_back(Section);
5246 }
5247
5248 struct DisassembleInfo info;
5249 // Set up the block of info used by the Symbolizer call backs.
5250 info.verbose = verbose;
5251 info.O = O;
5252 info.AddrMap = &AddrMap;
5253 info.Sections = &Sections;
5254 info.class_name = nullptr;
5255 info.selector_name = nullptr;
5256 info.method = nullptr;
5257 info.demangled_name = nullptr;
5258 info.bindtable = nullptr;
5259 info.adrp_addr = 0;
5260 info.adrp_inst = 0;
5261
5262 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5263 if (CL != SectionRef()) {
5264 info.S = CL;
5265 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5266 } else {
5267 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5268 info.S = CL;
5269 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5270 }
5271
5272 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5273 if (CR != SectionRef()) {
5274 info.S = CR;
5275 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5276 } else {
5277 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5278 info.S = CR;
5279 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5280 }
5281
5282 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5283 if (SR != SectionRef()) {
5284 info.S = SR;
5285 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5286 } else {
5287 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5288 info.S = SR;
5289 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5290 }
5291
5292 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5293 if (CA != SectionRef()) {
5294 info.S = CA;
5295 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5296 } else {
5297 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5298 info.S = CA;
5299 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5300 }
5301
5302 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5303 if (PL != SectionRef()) {
5304 info.S = PL;
5305 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5306 } else {
5307 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5308 info.S = PL;
5309 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5310 }
5311
5312 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5313 if (MR != SectionRef()) {
5314 info.S = MR;
5315 print_message_refs32(MR, &info);
5316 } else {
5317 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5318 info.S = MR;
5319 print_message_refs32(MR, &info);
5320 }
5321
5322 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5323 if (II != SectionRef()) {
5324 info.S = II;
5325 print_image_info32(II, &info);
5326 } else {
5327 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5328 info.S = II;
5329 print_image_info32(II, &info);
5330 }
Kevin Enderby0fc11822015-04-01 20:57:01 +00005331}
5332
5333static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby846c0002015-04-16 17:19:59 +00005334 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5335 const char *r, *name, *defs;
5336 struct objc_module_t module;
5337 SectionRef S, xS;
5338 struct objc_symtab_t symtab;
5339 struct objc_class_t objc_class;
5340 struct objc_category_t objc_category;
5341
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005342 outs() << "Objective-C segment\n";
Kevin Enderby846c0002015-04-16 17:19:59 +00005343 S = get_section(O, "__OBJC", "__module_info");
5344 if (S == SectionRef())
5345 return false;
5346
5347 SymbolAddressMap AddrMap;
5348 if (verbose)
5349 CreateSymbolAddressMap(O, &AddrMap);
5350
5351 std::vector<SectionRef> Sections;
5352 for (const SectionRef &Section : O->sections()) {
5353 StringRef SectName;
5354 Section.getName(SectName);
5355 Sections.push_back(Section);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005356 }
Kevin Enderby846c0002015-04-16 17:19:59 +00005357
5358 struct DisassembleInfo info;
5359 // Set up the block of info used by the Symbolizer call backs.
5360 info.verbose = verbose;
5361 info.O = O;
5362 info.AddrMap = &AddrMap;
5363 info.Sections = &Sections;
5364 info.class_name = nullptr;
5365 info.selector_name = nullptr;
5366 info.method = nullptr;
5367 info.demangled_name = nullptr;
5368 info.bindtable = nullptr;
5369 info.adrp_addr = 0;
5370 info.adrp_inst = 0;
5371
5372 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5373 p = S.getAddress() + i;
5374 r = get_pointer_32(p, offset, left, S, &info, true);
5375 if (r == nullptr)
5376 return true;
5377 memset(&module, '\0', sizeof(struct objc_module_t));
5378 if (left < sizeof(struct objc_module_t)) {
5379 memcpy(&module, r, left);
5380 outs() << " (module extends past end of __module_info section)\n";
5381 } else
5382 memcpy(&module, r, sizeof(struct objc_module_t));
5383 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5384 swapStruct(module);
5385
5386 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5387 outs() << " version " << module.version << "\n";
5388 outs() << " size " << module.size << "\n";
5389 outs() << " name ";
5390 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5391 if (name != nullptr)
5392 outs() << format("%.*s", left, name);
5393 else
5394 outs() << format("0x%08" PRIx32, module.name)
5395 << "(not in an __OBJC section)";
5396 outs() << "\n";
5397
5398 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5399 if (module.symtab == 0 || r == nullptr) {
5400 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5401 << " (not in an __OBJC section)\n";
5402 continue;
5403 }
5404 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5405 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5406 defs_left = 0;
5407 defs = nullptr;
5408 if (left < sizeof(struct objc_symtab_t)) {
5409 memcpy(&symtab, r, left);
5410 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5411 } else {
5412 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5413 if (left > sizeof(struct objc_symtab_t)) {
5414 defs_left = left - sizeof(struct objc_symtab_t);
5415 defs = r + sizeof(struct objc_symtab_t);
5416 }
5417 }
5418 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5419 swapStruct(symtab);
5420
5421 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5422 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5423 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5424 if (r == nullptr)
5425 outs() << " (not in an __OBJC section)";
5426 outs() << "\n";
5427 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5428 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5429 if (symtab.cls_def_cnt > 0)
5430 outs() << "\tClass Definitions\n";
5431 for (j = 0; j < symtab.cls_def_cnt; j++) {
5432 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5433 outs() << "\t(remaining class defs entries entends past the end of the "
5434 << "section)\n";
5435 break;
5436 }
5437 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5438 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5439 sys::swapByteOrder(def);
5440
5441 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5442 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5443 if (r != nullptr) {
5444 if (left > sizeof(struct objc_class_t)) {
5445 outs() << "\n";
5446 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5447 } else {
5448 outs() << " (entends past the end of the section)\n";
5449 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5450 memcpy(&objc_class, r, left);
5451 }
5452 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5453 swapStruct(objc_class);
5454 print_objc_class_t(&objc_class, &info);
5455 } else {
5456 outs() << "(not in an __OBJC section)\n";
5457 }
5458
5459 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5460 outs() << "\tMeta Class";
5461 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5462 if (r != nullptr) {
5463 if (left > sizeof(struct objc_class_t)) {
5464 outs() << "\n";
5465 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5466 } else {
5467 outs() << " (entends past the end of the section)\n";
5468 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5469 memcpy(&objc_class, r, left);
5470 }
5471 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5472 swapStruct(objc_class);
5473 print_objc_class_t(&objc_class, &info);
5474 } else {
5475 outs() << "(not in an __OBJC section)\n";
5476 }
5477 }
5478 }
5479 if (symtab.cat_def_cnt > 0)
5480 outs() << "\tCategory Definitions\n";
5481 for (j = 0; j < symtab.cat_def_cnt; j++) {
5482 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5483 outs() << "\t(remaining category defs entries entends past the end of "
5484 << "the section)\n";
5485 break;
5486 }
5487 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5488 sizeof(uint32_t));
5489 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5490 sys::swapByteOrder(def);
5491
5492 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5493 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5494 << format("0x%08" PRIx32, def);
5495 if (r != nullptr) {
5496 if (left > sizeof(struct objc_category_t)) {
5497 outs() << "\n";
5498 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5499 } else {
5500 outs() << " (entends past the end of the section)\n";
5501 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5502 memcpy(&objc_category, r, left);
5503 }
5504 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5505 swapStruct(objc_category);
5506 print_objc_objc_category_t(&objc_category, &info);
5507 } else {
5508 outs() << "(not in an __OBJC section)\n";
5509 }
5510 }
5511 }
5512 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5513 if (II != SectionRef())
5514 print_image_info(II, &info);
5515
5516 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005517}
5518
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00005519static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5520 uint32_t size, uint32_t addr) {
5521 SymbolAddressMap AddrMap;
5522 CreateSymbolAddressMap(O, &AddrMap);
5523
5524 std::vector<SectionRef> Sections;
5525 for (const SectionRef &Section : O->sections()) {
5526 StringRef SectName;
5527 Section.getName(SectName);
5528 Sections.push_back(Section);
5529 }
5530
5531 struct DisassembleInfo info;
5532 // Set up the block of info used by the Symbolizer call backs.
5533 info.verbose = true;
5534 info.O = O;
5535 info.AddrMap = &AddrMap;
5536 info.Sections = &Sections;
5537 info.class_name = nullptr;
5538 info.selector_name = nullptr;
5539 info.method = nullptr;
5540 info.demangled_name = nullptr;
5541 info.bindtable = nullptr;
5542 info.adrp_addr = 0;
5543 info.adrp_inst = 0;
5544
5545 const char *p;
5546 struct objc_protocol_t protocol;
5547 uint32_t left, paddr;
5548 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5549 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5550 left = size - (p - sect);
5551 if (left < sizeof(struct objc_protocol_t)) {
5552 outs() << "Protocol extends past end of __protocol section\n";
5553 memcpy(&protocol, p, left);
5554 } else
5555 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5556 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5557 swapStruct(protocol);
5558 paddr = addr + (p - sect);
5559 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5560 if (print_protocol(paddr, 0, &info))
5561 outs() << "(not in an __OBJC section)\n";
5562 }
5563}
5564
Kevin Enderby0fc11822015-04-01 20:57:01 +00005565static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
5566 if (O->is64Bit())
5567 printObjc2_64bit_MetaData(O, verbose);
5568 else {
5569 MachO::mach_header H;
5570 H = O->getHeader();
5571 if (H.cputype == MachO::CPU_TYPE_ARM)
5572 printObjc2_32bit_MetaData(O, verbose);
5573 else {
5574 // This is the 32-bit non-arm cputype case. Which is normally
5575 // the first Objective-C ABI. But it may be the case of a
5576 // binary for the iOS simulator which is the second Objective-C
5577 // ABI. In that case printObjc1_32bit_MetaData() will determine that
5578 // and return false.
5579 if (printObjc1_32bit_MetaData(O, verbose) == false)
5580 printObjc2_32bit_MetaData(O, verbose);
5581 }
5582 }
5583}
5584
Kevin Enderbybf246f52014-09-24 23:08:22 +00005585// GuessLiteralPointer returns a string which for the item in the Mach-O file
5586// for the address passed in as ReferenceValue for printing as a comment with
5587// the instruction and also returns the corresponding type of that item
5588// indirectly through ReferenceType.
5589//
5590// If ReferenceValue is an address of literal cstring then a pointer to the
5591// cstring is returned and ReferenceType is set to
5592// LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
5593//
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005594// If ReferenceValue is an address of an Objective-C CFString, Selector ref or
5595// Class ref that name is returned and the ReferenceType is set accordingly.
5596//
5597// Lastly, literals which are Symbol address in a literal pool are looked for
5598// and if found the symbol name is returned and ReferenceType is set to
5599// LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
5600//
5601// If there is no item in the Mach-O file for the address passed in as
5602// ReferenceValue nullptr is returned and ReferenceType is unchanged.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00005603static const char *GuessLiteralPointer(uint64_t ReferenceValue,
5604 uint64_t ReferencePC,
5605 uint64_t *ReferenceType,
5606 struct DisassembleInfo *info) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005607 // First see if there is an external relocation entry at the ReferencePC.
Rafael Espindola80291272014-10-08 15:28:58 +00005608 uint64_t sect_addr = info->S.getAddress();
Kevin Enderbybf246f52014-09-24 23:08:22 +00005609 uint64_t sect_offset = ReferencePC - sect_addr;
5610 bool reloc_found = false;
5611 DataRefImpl Rel;
5612 MachO::any_relocation_info RE;
5613 bool isExtern = false;
5614 SymbolRef Symbol;
5615 for (const RelocationRef &Reloc : info->S.relocations()) {
5616 uint64_t RelocOffset;
5617 Reloc.getOffset(RelocOffset);
5618 if (RelocOffset == sect_offset) {
5619 Rel = Reloc.getRawDataRefImpl();
5620 RE = info->O->getRelocation(Rel);
5621 if (info->O->isRelocationScattered(RE))
5622 continue;
5623 isExtern = info->O->getPlainRelocationExternal(RE);
5624 if (isExtern) {
5625 symbol_iterator RelocSym = Reloc.getSymbol();
5626 Symbol = *RelocSym;
5627 }
5628 reloc_found = true;
5629 break;
5630 }
5631 }
5632 // If there is an external relocation entry for a symbol in a section
5633 // then used that symbol's value for the value of the reference.
5634 if (reloc_found && isExtern) {
5635 if (info->O->getAnyRelocationPCRel(RE)) {
5636 unsigned Type = info->O->getAnyRelocationType(RE);
5637 if (Type == MachO::X86_64_RELOC_SIGNED) {
5638 Symbol.getAddress(ReferenceValue);
5639 }
5640 }
5641 }
5642
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005643 // Look for literals such as Objective-C CFStrings refs, Selector refs,
5644 // Message refs and Class refs.
5645 bool classref, selref, msgref, cfstring;
5646 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
5647 selref, msgref, cfstring);
David Blaikie33dd45d02015-03-23 18:39:02 +00005648 if (classref && pointer_value == 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005649 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
5650 // And the pointer_value in that section is typically zero as it will be
5651 // set by dyld as part of the "bind information".
5652 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
5653 if (name != nullptr) {
5654 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
Hans Wennborgdb53e302014-10-23 21:59:17 +00005655 const char *class_name = strrchr(name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005656 if (class_name != nullptr && class_name[1] == '_' &&
5657 class_name[2] != '\0') {
5658 info->class_name = class_name + 2;
5659 return name;
5660 }
5661 }
5662 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005663
David Blaikie33dd45d02015-03-23 18:39:02 +00005664 if (classref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005665 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5666 const char *name =
5667 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
5668 if (name != nullptr)
5669 info->class_name = name;
5670 else
5671 name = "bad class ref";
Kevin Enderbybf246f52014-09-24 23:08:22 +00005672 return name;
5673 }
5674
David Blaikie33dd45d02015-03-23 18:39:02 +00005675 if (cfstring) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005676 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
5677 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
5678 return name;
5679 }
5680
David Blaikie33dd45d02015-03-23 18:39:02 +00005681 if (selref && pointer_value == 0)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005682 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
5683
5684 if (pointer_value != 0)
5685 ReferenceValue = pointer_value;
5686
5687 const char *name = GuessCstringPointer(ReferenceValue, info);
5688 if (name) {
David Blaikie33dd45d02015-03-23 18:39:02 +00005689 if (pointer_value != 0 && selref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005690 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
5691 info->selector_name = name;
David Blaikie33dd45d02015-03-23 18:39:02 +00005692 } else if (pointer_value != 0 && msgref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005693 info->class_name = nullptr;
5694 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
5695 info->selector_name = name;
5696 } else
5697 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
5698 return name;
5699 }
5700
5701 // Lastly look for an indirect symbol with this ReferenceValue which is in
5702 // a literal pool. If found return that symbol name.
5703 name = GuessIndirectSymbol(ReferenceValue, info);
5704 if (name) {
5705 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
5706 return name;
5707 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005708
5709 return nullptr;
5710}
5711
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005712// SymbolizerSymbolLookUp is the symbol lookup function passed when creating
Kevin Enderbybf246f52014-09-24 23:08:22 +00005713// the Symbolizer. It looks up the ReferenceValue using the info passed via the
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005714// pointer to the struct DisassembleInfo that was passed when MCSymbolizer
5715// is created and returns the symbol name that matches the ReferenceValue or
5716// nullptr if none. The ReferenceType is passed in for the IN type of
5717// reference the instruction is making from the values in defined in the header
5718// "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
5719// Out type and the ReferenceName will also be set which is added as a comment
5720// to the disassembled instruction.
5721//
Kevin Enderby04bf6932014-10-28 23:39:46 +00005722#if HAVE_CXXABI_H
5723// If the symbol name is a C++ mangled name then the demangled name is
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005724// returned through ReferenceName and ReferenceType is set to
5725// LLVMDisassembler_ReferenceType_DeMangled_Name .
Kevin Enderby04bf6932014-10-28 23:39:46 +00005726#endif
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005727//
5728// When this is called to get a symbol name for a branch target then the
5729// ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
5730// SymbolValue will be looked for in the indirect symbol table to determine if
5731// it is an address for a symbol stub. If so then the symbol name for that
5732// stub is returned indirectly through ReferenceName and then ReferenceType is
5733// set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
5734//
Kevin Enderbybf246f52014-09-24 23:08:22 +00005735// When this is called with an value loaded via a PC relative load then
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005736// ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
5737// SymbolValue is checked to be an address of literal pointer, symbol pointer,
5738// or an Objective-C meta data reference. If so the output ReferenceType is
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005739// set to correspond to that as well as setting the ReferenceName.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00005740static const char *SymbolizerSymbolLookUp(void *DisInfo,
5741 uint64_t ReferenceValue,
5742 uint64_t *ReferenceType,
5743 uint64_t ReferencePC,
5744 const char **ReferenceName) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005745 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
Kevin Enderbybf246f52014-09-24 23:08:22 +00005746 // If no verbose symbolic information is wanted then just return nullptr.
David Blaikie33dd45d02015-03-23 18:39:02 +00005747 if (!info->verbose) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005748 *ReferenceName = nullptr;
5749 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005750 return nullptr;
5751 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005752
Kevin Enderbyf6d25852015-01-31 00:37:11 +00005753 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderbybf246f52014-09-24 23:08:22 +00005754
Kevin Enderby85974882014-09-26 22:20:44 +00005755 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
5756 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005757 if (*ReferenceName != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005758 method_reference(info, ReferenceType, ReferenceName);
5759 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
5760 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
5761 } else
Kevin Enderby04bf6932014-10-28 23:39:46 +00005762#if HAVE_CXXABI_H
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005763 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00005764 if (info->demangled_name != nullptr)
5765 free(info->demangled_name);
5766 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005767 info->demangled_name =
5768 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005769 if (info->demangled_name != nullptr) {
5770 *ReferenceName = info->demangled_name;
5771 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5772 } else
5773 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5774 } else
5775#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005776 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5777 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
5778 *ReferenceName =
5779 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
Kevin Enderby85974882014-09-26 22:20:44 +00005780 if (*ReferenceName)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005781 method_reference(info, ReferenceType, ReferenceName);
Kevin Enderby85974882014-09-26 22:20:44 +00005782 else
5783 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005784 // If this is arm64 and the reference is an adrp instruction save the
5785 // instruction, passed in ReferenceValue and the address of the instruction
5786 // for use later if we see and add immediate instruction.
5787 } else if (info->O->getArch() == Triple::aarch64 &&
5788 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
5789 info->adrp_inst = ReferenceValue;
5790 info->adrp_addr = ReferencePC;
5791 SymbolName = nullptr;
5792 *ReferenceName = nullptr;
5793 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5794 // If this is arm64 and reference is an add immediate instruction and we
5795 // have
5796 // seen an adrp instruction just before it and the adrp's Xd register
5797 // matches
5798 // this add's Xn register reconstruct the value being referenced and look to
5799 // see if it is a literal pointer. Note the add immediate instruction is
5800 // passed in ReferenceValue.
5801 } else if (info->O->getArch() == Triple::aarch64 &&
5802 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
5803 ReferencePC - 4 == info->adrp_addr &&
5804 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5805 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5806 uint32_t addxri_inst;
5807 uint64_t adrp_imm, addxri_imm;
5808
5809 adrp_imm =
5810 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5811 if (info->adrp_inst & 0x0200000)
5812 adrp_imm |= 0xfffffffffc000000LL;
5813
5814 addxri_inst = ReferenceValue;
5815 addxri_imm = (addxri_inst >> 10) & 0xfff;
5816 if (((addxri_inst >> 22) & 0x3) == 1)
5817 addxri_imm <<= 12;
5818
5819 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5820 (adrp_imm << 12) + addxri_imm;
5821
5822 *ReferenceName =
5823 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5824 if (*ReferenceName == nullptr)
5825 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5826 // If this is arm64 and the reference is a load register instruction and we
5827 // have seen an adrp instruction just before it and the adrp's Xd register
5828 // matches this add's Xn register reconstruct the value being referenced and
5829 // look to see if it is a literal pointer. Note the load register
5830 // instruction is passed in ReferenceValue.
5831 } else if (info->O->getArch() == Triple::aarch64 &&
5832 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
5833 ReferencePC - 4 == info->adrp_addr &&
5834 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5835 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5836 uint32_t ldrxui_inst;
5837 uint64_t adrp_imm, ldrxui_imm;
5838
5839 adrp_imm =
5840 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5841 if (info->adrp_inst & 0x0200000)
5842 adrp_imm |= 0xfffffffffc000000LL;
5843
5844 ldrxui_inst = ReferenceValue;
5845 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
5846
5847 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5848 (adrp_imm << 12) + (ldrxui_imm << 3);
5849
5850 *ReferenceName =
5851 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5852 if (*ReferenceName == nullptr)
5853 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5854 }
5855 // If this arm64 and is an load register (PC-relative) instruction the
5856 // ReferenceValue is the PC plus the immediate value.
5857 else if (info->O->getArch() == Triple::aarch64 &&
5858 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
5859 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
5860 *ReferenceName =
5861 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5862 if (*ReferenceName == nullptr)
5863 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby85974882014-09-26 22:20:44 +00005864 }
Kevin Enderby04bf6932014-10-28 23:39:46 +00005865#if HAVE_CXXABI_H
5866 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5867 if (info->demangled_name != nullptr)
5868 free(info->demangled_name);
5869 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005870 info->demangled_name =
5871 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005872 if (info->demangled_name != nullptr) {
5873 *ReferenceName = info->demangled_name;
5874 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5875 }
5876 }
5877#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005878 else {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005879 *ReferenceName = nullptr;
5880 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5881 }
5882
5883 return SymbolName;
5884}
5885
Kevin Enderbybf246f52014-09-24 23:08:22 +00005886/// \brief Emits the comments that are stored in the CommentStream.
5887/// Each comment in the CommentStream must end with a newline.
5888static void emitComments(raw_svector_ostream &CommentStream,
5889 SmallString<128> &CommentsToEmit,
5890 formatted_raw_ostream &FormattedOS,
5891 const MCAsmInfo &MAI) {
5892 // Flush the stream before taking its content.
5893 CommentStream.flush();
5894 StringRef Comments = CommentsToEmit.str();
5895 // Get the default information for printing a comment.
5896 const char *CommentBegin = MAI.getCommentString();
5897 unsigned CommentColumn = MAI.getCommentColumn();
5898 bool IsFirst = true;
5899 while (!Comments.empty()) {
5900 if (!IsFirst)
5901 FormattedOS << '\n';
5902 // Emit a line of comments.
5903 FormattedOS.PadToColumn(CommentColumn);
5904 size_t Position = Comments.find('\n');
5905 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
5906 // Move after the newline character.
5907 Comments = Comments.substr(Position + 1);
5908 IsFirst = false;
5909 }
5910 FormattedOS.flush();
5911
5912 // Tell the comment stream that the vector changed underneath it.
5913 CommentsToEmit.clear();
5914 CommentStream.resync();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005915}
5916
Kevin Enderby95df54c2015-02-04 01:01:38 +00005917static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
5918 StringRef DisSegName, StringRef DisSectName) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005919 const char *McpuDefault = nullptr;
5920 const Target *ThumbTarget = nullptr;
5921 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005922 if (!TheTarget) {
5923 // GetTarget prints out stuff.
5924 return;
5925 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005926 if (MCPU.empty() && McpuDefault)
5927 MCPU = McpuDefault;
5928
Ahmed Charles56440fd2014-03-06 05:51:42 +00005929 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005930 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005931 if (ThumbTarget)
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005932 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005933
Kevin Enderbyc9595622014-08-06 23:24:41 +00005934 // Package up features to be passed to target/subtarget
5935 std::string FeaturesStr;
5936 if (MAttrs.size()) {
5937 SubtargetFeatures Features;
5938 for (unsigned i = 0; i != MAttrs.size(); ++i)
5939 Features.AddFeature(MAttrs[i]);
5940 FeaturesStr = Features.getString();
5941 }
5942
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005943 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00005944 std::unique_ptr<const MCRegisterInfo> MRI(
5945 TheTarget->createMCRegInfo(TripleName));
5946 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +00005947 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +00005948 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +00005949 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Craig Toppere6cb63e2014-04-25 04:24:47 +00005950 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005951 std::unique_ptr<MCDisassembler> DisAsm(
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005952 TheTarget->createMCDisassembler(*STI, Ctx));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005953 std::unique_ptr<MCSymbolizer> Symbolizer;
5954 struct DisassembleInfo SymbolizerInfo;
5955 std::unique_ptr<MCRelocationInfo> RelInfo(
5956 TheTarget->createMCRelocationInfo(TripleName, Ctx));
5957 if (RelInfo) {
5958 Symbolizer.reset(TheTarget->createMCSymbolizer(
5959 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00005960 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005961 DisAsm->setSymbolizer(std::move(Symbolizer));
5962 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005963 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +00005964 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +00005965 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00005966 // Set the display preference for hex vs. decimal immediates.
5967 IP->setPrintImmHex(PrintImmHex);
5968 // Comment stream and backing vector.
5969 SmallString<128> CommentsToEmit;
5970 raw_svector_ostream CommentStream(CommentsToEmit);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00005971 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
5972 // if it is done then arm64 comments for string literals don't get printed
5973 // and some constant get printed instead and not setting it causes intel
5974 // (32-bit and 64-bit) comments printed with different spacing before the
5975 // comment causing different diffs with the 'C' disassembler library API.
5976 // IP->setCommentStream(CommentStream);
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005977
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005978 if (!AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +00005979 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005980 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005981 return;
5982 }
5983
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005984 // Set up thumb disassembler.
5985 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
5986 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
5987 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005988 std::unique_ptr<MCDisassembler> ThumbDisAsm;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005989 std::unique_ptr<MCInstPrinter> ThumbIP;
5990 std::unique_ptr<MCContext> ThumbCtx;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005991 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
5992 struct DisassembleInfo ThumbSymbolizerInfo;
5993 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005994 if (ThumbTarget) {
5995 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
5996 ThumbAsmInfo.reset(
5997 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
5998 ThumbSTI.reset(
5999 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
6000 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
6001 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006002 MCContext *PtrThumbCtx = ThumbCtx.get();
6003 ThumbRelInfo.reset(
6004 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
6005 if (ThumbRelInfo) {
6006 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6007 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00006008 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006009 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6010 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006011 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6012 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +00006013 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6014 *ThumbInstrInfo, *ThumbMRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006015 // Set the display preference for hex vs. decimal immediates.
6016 ThumbIP->setPrintImmHex(PrintImmHex);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006017 }
6018
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006019 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006020 errs() << "error: couldn't initialize disassembler for target "
6021 << ThumbTripleName << '\n';
6022 return;
6023 }
6024
Charles Davis8bdfafd2013-09-01 04:28:48 +00006025 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006026
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006027 // FIXME: Using the -cfg command line option, this code used to be able to
6028 // annotate relocations with the referenced symbol's name, and if this was
6029 // inside a __[cf]string section, the data it points to. This is now replaced
6030 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +00006031 std::vector<SectionRef> Sections;
6032 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006033 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +00006034 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006035
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00006036 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
Kevin Enderby273ae012013-06-06 17:20:50 +00006037 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006038
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006039 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +00006040 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006041
Kevin Enderby273ae012013-06-06 17:20:50 +00006042 // Build a data in code table that is sorted on by the address of each entry.
6043 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +00006044 if (Header.filetype == MachO::MH_OBJECT)
Rafael Espindola80291272014-10-08 15:28:58 +00006045 BaseAddress = Sections[0].getAddress();
Kevin Enderby273ae012013-06-06 17:20:50 +00006046 else
6047 BaseAddress = BaseSegmentAddress;
6048 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +00006049 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +00006050 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006051 uint32_t Offset;
6052 DI->getOffset(Offset);
6053 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6054 }
6055 array_pod_sort(Dices.begin(), Dices.end());
6056
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006057#ifndef NDEBUG
6058 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6059#else
6060 raw_ostream &DebugOut = nulls();
6061#endif
6062
Ahmed Charles56440fd2014-03-06 05:51:42 +00006063 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +00006064 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +00006065 // Try to find debug info and set up the DIContext for it.
6066 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +00006067 // A separate DSym file path was specified, parse it as a macho file,
6068 // get the sections and supply it to the section name parsing machinery.
6069 if (!DSYMFile.empty()) {
Rafael Espindola48af1c22014-08-19 18:44:46 +00006070 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006071 MemoryBuffer::getFileOrSTDIN(DSYMFile);
Rafael Espindola48af1c22014-08-19 18:44:46 +00006072 if (std::error_code EC = BufOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006073 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
Benjamin Kramer699128e2011-09-21 01:13:19 +00006074 return;
6075 }
Rafael Espindola48af1c22014-08-19 18:44:46 +00006076 DbgObj =
6077 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6078 .get()
6079 .release();
Benjamin Kramer699128e2011-09-21 01:13:19 +00006080 }
6081
Eric Christopher7370b552012-11-12 21:40:38 +00006082 // Setup the DIContext
Zachary Turner6489d7b2015-04-23 17:37:47 +00006083 diContext.reset(new DWARFContextInMemory(*DbgObj));
Benjamin Kramer699128e2011-09-21 01:13:19 +00006084 }
6085
Kevin Enderby95df54c2015-02-04 01:01:38 +00006086 if (DumpSections.size() == 0)
6087 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006088
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006089 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00006090 StringRef SectName;
Kevin Enderby95df54c2015-02-04 01:01:38 +00006091 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6092 continue;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006093
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006094 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006095
Rafael Espindolab0f76a42013-04-05 15:15:22 +00006096 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
Kevin Enderby95df54c2015-02-04 01:01:38 +00006097 if (SegmentName != DisSegName)
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006098 continue;
6099
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006100 StringRef BytesStr;
6101 Sections[SectIdx].getContents(BytesStr);
Aaron Ballman106fd7b2014-11-12 14:01:17 +00006102 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6103 BytesStr.size());
Rafael Espindola80291272014-10-08 15:28:58 +00006104 uint64_t SectAddress = Sections[SectIdx].getAddress();
Rafael Espindolabd604f22014-11-07 00:52:15 +00006105
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006106 bool symbolTableWorked = false;
6107
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006108 // Parse relocations.
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00006109 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
6110 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
Rafael Espindola80291272014-10-08 15:28:58 +00006111 uint64_t RelocOffset;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00006112 Reloc.getOffset(RelocOffset);
Rafael Espindola80291272014-10-08 15:28:58 +00006113 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Owen Andersond9243c42011-10-17 21:37:35 +00006114 RelocOffset -= SectionAddress;
6115
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00006116 symbol_iterator RelocSym = Reloc.getSymbol();
Owen Andersond9243c42011-10-17 21:37:35 +00006117
Rafael Espindola806f0062013-06-05 01:33:53 +00006118 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006119 }
6120 array_pod_sort(Relocs.begin(), Relocs.end());
6121
Kevin Enderbybf246f52014-09-24 23:08:22 +00006122 // Create a map of symbol addresses to symbol names for use by
6123 // the SymbolizerSymbolLookUp() routine.
6124 SymbolAddressMap AddrMap;
Kevin Enderby6a221752015-03-17 17:10:57 +00006125 bool DisSymNameFound = false;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006126 for (const SymbolRef &Symbol : MachOOF->symbols()) {
6127 SymbolRef::Type ST;
6128 Symbol.getType(ST);
6129 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6130 ST == SymbolRef::ST_Other) {
6131 uint64_t Address;
6132 Symbol.getAddress(Address);
6133 StringRef SymName;
6134 Symbol.getName(SymName);
6135 AddrMap[Address] = SymName;
Kevin Enderby6a221752015-03-17 17:10:57 +00006136 if (!DisSymName.empty() && DisSymName == SymName)
6137 DisSymNameFound = true;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006138 }
6139 }
David Blaikie33dd45d02015-03-23 18:39:02 +00006140 if (!DisSymName.empty() && !DisSymNameFound) {
Kevin Enderby6a221752015-03-17 17:10:57 +00006141 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6142 return;
6143 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006144 // Set up the block of info used by the Symbolizer call backs.
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006145 SymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006146 SymbolizerInfo.O = MachOOF;
6147 SymbolizerInfo.S = Sections[SectIdx];
Kevin Enderbybf246f52014-09-24 23:08:22 +00006148 SymbolizerInfo.AddrMap = &AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006149 SymbolizerInfo.Sections = &Sections;
6150 SymbolizerInfo.class_name = nullptr;
6151 SymbolizerInfo.selector_name = nullptr;
6152 SymbolizerInfo.method = nullptr;
Kevin Enderby04bf6932014-10-28 23:39:46 +00006153 SymbolizerInfo.demangled_name = nullptr;
Kevin Enderby078be602014-10-23 19:53:12 +00006154 SymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006155 SymbolizerInfo.adrp_addr = 0;
6156 SymbolizerInfo.adrp_inst = 0;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006157 // Same for the ThumbSymbolizer
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006158 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006159 ThumbSymbolizerInfo.O = MachOOF;
6160 ThumbSymbolizerInfo.S = Sections[SectIdx];
6161 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6162 ThumbSymbolizerInfo.Sections = &Sections;
6163 ThumbSymbolizerInfo.class_name = nullptr;
6164 ThumbSymbolizerInfo.selector_name = nullptr;
6165 ThumbSymbolizerInfo.method = nullptr;
6166 ThumbSymbolizerInfo.demangled_name = nullptr;
6167 ThumbSymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006168 ThumbSymbolizerInfo.adrp_addr = 0;
6169 ThumbSymbolizerInfo.adrp_inst = 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006170
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006171 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006172 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00006173 StringRef SymName;
6174 Symbols[SymIdx].getName(SymName);
6175
6176 SymbolRef::Type ST;
6177 Symbols[SymIdx].getType(ST);
6178 if (ST != SymbolRef::ST_Function)
6179 continue;
6180
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006181 // Make sure the symbol is defined in this section.
Rafael Espindola80291272014-10-08 15:28:58 +00006182 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Owen Andersond9243c42011-10-17 21:37:35 +00006183 if (!containsSym)
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006184 continue;
6185
Kevin Enderby6a221752015-03-17 17:10:57 +00006186 // If we are only disassembling one symbol see if this is that symbol.
6187 if (!DisSymName.empty() && DisSymName != SymName)
6188 continue;
6189
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006190 // Start at the address of the symbol relative to the section's address.
Owen Andersond9243c42011-10-17 21:37:35 +00006191 uint64_t Start = 0;
Rafael Espindola80291272014-10-08 15:28:58 +00006192 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00006193 Symbols[SymIdx].getAddress(Start);
Cameron Zwarich54478a52012-02-03 05:42:17 +00006194 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006195
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006196 // Stop disassembling either at the beginning of the next symbol or at
6197 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +00006198 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +00006199 uint64_t NextSym = 0;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006200 uint64_t NextSymIdx = SymIdx + 1;
Owen Andersond9243c42011-10-17 21:37:35 +00006201 while (Symbols.size() > NextSymIdx) {
6202 SymbolRef::Type NextSymType;
6203 Symbols[NextSymIdx].getType(NextSymType);
6204 if (NextSymType == SymbolRef::ST_Function) {
Rafael Espindola80291272014-10-08 15:28:58 +00006205 containsNextSym =
6206 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00006207 Symbols[NextSymIdx].getAddress(NextSym);
Cameron Zwarich54478a52012-02-03 05:42:17 +00006208 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006209 break;
6210 }
6211 ++NextSymIdx;
6212 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006213
Rafael Espindola80291272014-10-08 15:28:58 +00006214 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006215 uint64_t End = containsNextSym ? NextSym : SectSize;
Owen Andersond9243c42011-10-17 21:37:35 +00006216 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006217
6218 symbolTableWorked = true;
Rafael Espindolabd604f22014-11-07 00:52:15 +00006219
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006220 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6221 bool isThumb =
6222 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
6223
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006224 outs() << SymName << ":\n";
6225 DILineInfo lastLine;
6226 for (uint64_t Index = Start; Index < End; Index += Size) {
6227 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +00006228
Kevin Enderbybf246f52014-09-24 23:08:22 +00006229 uint64_t PC = SectAddress + Index;
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006230 if (!NoLeadingAddr) {
6231 if (FullLeadingAddr) {
6232 if (MachOOF->is64Bit())
6233 outs() << format("%016" PRIx64, PC);
6234 else
6235 outs() << format("%08" PRIx64, PC);
6236 } else {
6237 outs() << format("%8" PRIx64 ":", PC);
6238 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006239 }
6240 if (!NoShowRawInsn)
6241 outs() << "\t";
Kevin Enderby273ae012013-06-06 17:20:50 +00006242
6243 // Check the data in code table here to see if this is data not an
6244 // instruction to be disassembled.
6245 DiceTable Dice;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006246 Dice.push_back(std::make_pair(PC, DiceRef()));
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006247 dice_table_iterator DTI =
6248 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6249 compareDiceTableEntries);
6250 if (DTI != Dices.end()) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006251 uint16_t Length;
6252 DTI->second.getLength(Length);
Kevin Enderby273ae012013-06-06 17:20:50 +00006253 uint16_t Kind;
6254 DTI->second.getKind(Kind);
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00006255 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006256 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6257 (PC == (DTI->first + Length - 1)) && (Length & 1))
6258 Size++;
Kevin Enderby273ae012013-06-06 17:20:50 +00006259 continue;
6260 }
6261
Kevin Enderbybf246f52014-09-24 23:08:22 +00006262 SmallVector<char, 64> AnnotationsBytes;
6263 raw_svector_ostream Annotations(AnnotationsBytes);
6264
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006265 bool gotInst;
6266 if (isThumb)
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006267 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006268 PC, DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006269 else
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006270 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
Kevin Enderbybf246f52014-09-24 23:08:22 +00006271 DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006272 if (gotInst) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006273 if (!NoShowRawInsn) {
Colin LeMahieu2048ea42015-05-28 18:39:50 +00006274 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006275 }
6276 formatted_raw_ostream FormattedOS(outs());
6277 Annotations.flush();
6278 StringRef AnnotationsStr = Annotations.str();
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006279 if (isThumb)
Akira Hatanakab46d0232015-03-27 20:36:02 +00006280 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006281 else
Akira Hatanaka1d079942015-03-28 20:44:05 +00006282 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006283 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
Owen Andersond9243c42011-10-17 21:37:35 +00006284
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006285 // Print debug info.
6286 if (diContext) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006287 DILineInfo dli = diContext->getLineInfoForAddress(PC);
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006288 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +00006289 if (dli != lastLine && dli.Line != 0)
6290 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6291 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006292 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006293 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006294 outs() << "\n";
6295 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006296 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006297 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006298 outs() << format("\t.byte 0x%02x #bad opcode\n",
6299 *(Bytes.data() + Index) & 0xff);
6300 Size = 1; // skip exactly one illegible byte and move on.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006301 } else if (Arch == Triple::aarch64) {
6302 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6303 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6304 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6305 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6306 outs() << format("\t.long\t0x%08x\n", opcode);
6307 Size = 4;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006308 } else {
6309 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6310 if (Size == 0)
6311 Size = 1; // skip illegible bytes
6312 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006313 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006314 }
6315 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006316 if (!symbolTableWorked) {
Rafael Espindola80291272014-10-08 15:28:58 +00006317 // Reading the symbol table didn't work, disassemble the whole section.
6318 uint64_t SectAddress = Sections[SectIdx].getAddress();
6319 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbybadd1002012-05-18 00:13:56 +00006320 uint64_t InstSize;
6321 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +00006322 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +00006323
Kevin Enderbybf246f52014-09-24 23:08:22 +00006324 uint64_t PC = SectAddress + Index;
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006325 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6326 DebugOut, nulls())) {
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006327 if (!NoLeadingAddr) {
6328 if (FullLeadingAddr) {
6329 if (MachOOF->is64Bit())
6330 outs() << format("%016" PRIx64, PC);
6331 else
6332 outs() << format("%08" PRIx64, PC);
6333 } else {
6334 outs() << format("%8" PRIx64 ":", PC);
6335 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006336 }
6337 if (!NoShowRawInsn) {
6338 outs() << "\t";
Colin LeMahieu2048ea42015-05-28 18:39:50 +00006339 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006340 }
Akira Hatanaka1d079942015-03-28 20:44:05 +00006341 IP->printInst(&Inst, outs(), "", *STI);
Bill Wendling4e68e062012-07-19 00:17:40 +00006342 outs() << "\n";
6343 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006344 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006345 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006346 outs() << format("\t.byte 0x%02x #bad opcode\n",
6347 *(Bytes.data() + Index) & 0xff);
6348 InstSize = 1; // skip exactly one illegible byte and move on.
6349 } else {
6350 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6351 if (InstSize == 0)
6352 InstSize = 1; // skip illegible bytes
6353 }
Bill Wendling4e68e062012-07-19 00:17:40 +00006354 }
Kevin Enderbybadd1002012-05-18 00:13:56 +00006355 }
6356 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006357 // The TripleName's need to be reset if we are called again for a different
6358 // archtecture.
6359 TripleName = "";
6360 ThumbTripleName = "";
6361
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006362 if (SymbolizerInfo.method != nullptr)
6363 free(SymbolizerInfo.method);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006364 if (SymbolizerInfo.demangled_name != nullptr)
6365 free(SymbolizerInfo.demangled_name);
Kevin Enderby078be602014-10-23 19:53:12 +00006366 if (SymbolizerInfo.bindtable != nullptr)
6367 delete SymbolizerInfo.bindtable;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006368 if (ThumbSymbolizerInfo.method != nullptr)
6369 free(ThumbSymbolizerInfo.method);
6370 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6371 free(ThumbSymbolizerInfo.demangled_name);
6372 if (ThumbSymbolizerInfo.bindtable != nullptr)
6373 delete ThumbSymbolizerInfo.bindtable;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006374 }
6375}
Tim Northover4bd286a2014-08-01 13:07:19 +00006376
Tim Northover39c70bb2014-08-12 11:52:59 +00006377//===----------------------------------------------------------------------===//
6378// __compact_unwind section dumping
6379//===----------------------------------------------------------------------===//
6380
Tim Northover4bd286a2014-08-01 13:07:19 +00006381namespace {
Tim Northover39c70bb2014-08-12 11:52:59 +00006382
6383template <typename T> static uint64_t readNext(const char *&Buf) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006384 using llvm::support::little;
6385 using llvm::support::unaligned;
Tim Northover39c70bb2014-08-12 11:52:59 +00006386
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006387 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6388 Buf += sizeof(T);
6389 return Val;
6390}
Tim Northover39c70bb2014-08-12 11:52:59 +00006391
Tim Northover4bd286a2014-08-01 13:07:19 +00006392struct CompactUnwindEntry {
6393 uint32_t OffsetInSection;
6394
6395 uint64_t FunctionAddr;
6396 uint32_t Length;
6397 uint32_t CompactEncoding;
6398 uint64_t PersonalityAddr;
6399 uint64_t LSDAAddr;
6400
6401 RelocationRef FunctionReloc;
6402 RelocationRef PersonalityReloc;
6403 RelocationRef LSDAReloc;
6404
6405 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006406 : OffsetInSection(Offset) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006407 if (Is64)
6408 read<uint64_t>(Contents.data() + Offset);
6409 else
6410 read<uint32_t>(Contents.data() + Offset);
6411 }
6412
6413private:
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006414 template <typename UIntPtr> void read(const char *Buf) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006415 FunctionAddr = readNext<UIntPtr>(Buf);
6416 Length = readNext<uint32_t>(Buf);
6417 CompactEncoding = readNext<uint32_t>(Buf);
6418 PersonalityAddr = readNext<UIntPtr>(Buf);
6419 LSDAAddr = readNext<UIntPtr>(Buf);
6420 }
6421};
6422}
6423
6424/// Given a relocation from __compact_unwind, consisting of the RelocationRef
6425/// and data being relocated, determine the best base Name and Addend to use for
6426/// display purposes.
6427///
6428/// 1. An Extern relocation will directly reference a symbol (and the data is
6429/// then already an addend), so use that.
6430/// 2. Otherwise the data is an offset in the object file's layout; try to find
6431// a symbol before it in the same section, and use the offset from there.
6432/// 3. Finally, if all that fails, fall back to an offset from the start of the
6433/// referenced section.
6434static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6435 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006436 const RelocationRef &Reloc, uint64_t Addr,
Tim Northover4bd286a2014-08-01 13:07:19 +00006437 StringRef &Name, uint64_t &Addend) {
6438 if (Reloc.getSymbol() != Obj->symbol_end()) {
6439 Reloc.getSymbol()->getName(Name);
6440 Addend = Addr;
6441 return;
6442 }
6443
6444 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
Keno Fischerc780e8e2015-05-21 21:24:32 +00006445 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
Tim Northover4bd286a2014-08-01 13:07:19 +00006446
Rafael Espindola80291272014-10-08 15:28:58 +00006447 uint64_t SectionAddr = RelocSection.getAddress();
Tim Northover4bd286a2014-08-01 13:07:19 +00006448
6449 auto Sym = Symbols.upper_bound(Addr);
6450 if (Sym == Symbols.begin()) {
6451 // The first symbol in the object is after this reference, the best we can
6452 // do is section-relative notation.
6453 RelocSection.getName(Name);
6454 Addend = Addr - SectionAddr;
6455 return;
6456 }
6457
6458 // Go back one so that SymbolAddress <= Addr.
6459 --Sym;
6460
6461 section_iterator SymSection = Obj->section_end();
6462 Sym->second.getSection(SymSection);
6463 if (RelocSection == *SymSection) {
6464 // There's a valid symbol in the same section before this reference.
6465 Sym->second.getName(Name);
6466 Addend = Addr - Sym->first;
6467 return;
6468 }
6469
6470 // There is a symbol before this reference, but it's in a different
6471 // section. Probably not helpful to mention it, so use the section name.
6472 RelocSection.getName(Name);
6473 Addend = Addr - SectionAddr;
6474}
6475
6476static void printUnwindRelocDest(const MachOObjectFile *Obj,
6477 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006478 const RelocationRef &Reloc, uint64_t Addr) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006479 StringRef Name;
6480 uint64_t Addend;
6481
Tim Northover0b0add52014-09-09 10:45:06 +00006482 if (!Reloc.getObjectFile())
6483 return;
6484
Tim Northover4bd286a2014-08-01 13:07:19 +00006485 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6486
6487 outs() << Name;
6488 if (Addend)
Tim Northover63a25622014-08-11 09:14:06 +00006489 outs() << " + " << format("0x%" PRIx64, Addend);
Tim Northover4bd286a2014-08-01 13:07:19 +00006490}
6491
6492static void
6493printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6494 std::map<uint64_t, SymbolRef> &Symbols,
6495 const SectionRef &CompactUnwind) {
6496
6497 assert(Obj->isLittleEndian() &&
6498 "There should not be a big-endian .o with __compact_unwind");
6499
6500 bool Is64 = Obj->is64Bit();
6501 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6502 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6503
6504 StringRef Contents;
6505 CompactUnwind.getContents(Contents);
6506
6507 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6508
6509 // First populate the initial raw offsets, encodings and so on from the entry.
6510 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6511 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
6512 CompactUnwinds.push_back(Entry);
6513 }
6514
6515 // Next we need to look at the relocations to find out what objects are
6516 // actually being referred to.
6517 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
6518 uint64_t RelocAddress;
6519 Reloc.getOffset(RelocAddress);
6520
6521 uint32_t EntryIdx = RelocAddress / EntrySize;
6522 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
6523 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
6524
6525 if (OffsetInEntry == 0)
6526 Entry.FunctionReloc = Reloc;
6527 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
6528 Entry.PersonalityReloc = Reloc;
6529 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
6530 Entry.LSDAReloc = Reloc;
6531 else
6532 llvm_unreachable("Unexpected relocation in __compact_unwind section");
6533 }
6534
6535 // Finally, we're ready to print the data we've gathered.
6536 outs() << "Contents of __compact_unwind section:\n";
6537 for (auto &Entry : CompactUnwinds) {
Tim Northover06af2602014-08-08 12:08:51 +00006538 outs() << " Entry at offset "
6539 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00006540
6541 // 1. Start of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006542 outs() << " start: " << format("0x%" PRIx64,
6543 Entry.FunctionAddr) << ' ';
6544 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
Tim Northover4bd286a2014-08-01 13:07:19 +00006545 outs() << '\n';
6546
6547 // 2. Length of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006548 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
6549 << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00006550 // 3. The 32-bit compact encoding.
6551 outs() << " compact encoding: "
Tim Northoverb911bf82014-08-08 12:00:09 +00006552 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00006553
6554 // 4. The personality function, if present.
6555 if (Entry.PersonalityReloc.getObjectFile()) {
6556 outs() << " personality function: "
Tim Northoverb911bf82014-08-08 12:00:09 +00006557 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00006558 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
6559 Entry.PersonalityAddr);
6560 outs() << '\n';
6561 }
6562
6563 // 5. This entry's language-specific data area.
6564 if (Entry.LSDAReloc.getObjectFile()) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006565 outs() << " LSDA: " << format("0x%" PRIx64,
6566 Entry.LSDAAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00006567 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
6568 outs() << '\n';
6569 }
6570 }
6571}
6572
Tim Northover39c70bb2014-08-12 11:52:59 +00006573//===----------------------------------------------------------------------===//
6574// __unwind_info section dumping
6575//===----------------------------------------------------------------------===//
6576
6577static void printRegularSecondLevelUnwindPage(const char *PageStart) {
6578 const char *Pos = PageStart;
6579 uint32_t Kind = readNext<uint32_t>(Pos);
6580 (void)Kind;
6581 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
6582
6583 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6584 uint16_t NumEntries = readNext<uint16_t>(Pos);
6585
6586 Pos = PageStart + EntriesStart;
6587 for (unsigned i = 0; i < NumEntries; ++i) {
6588 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6589 uint32_t Encoding = readNext<uint32_t>(Pos);
6590
6591 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006592 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6593 << ", "
6594 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006595 }
6596}
6597
6598static void printCompressedSecondLevelUnwindPage(
6599 const char *PageStart, uint32_t FunctionBase,
6600 const SmallVectorImpl<uint32_t> &CommonEncodings) {
6601 const char *Pos = PageStart;
6602 uint32_t Kind = readNext<uint32_t>(Pos);
6603 (void)Kind;
6604 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
6605
6606 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6607 uint16_t NumEntries = readNext<uint16_t>(Pos);
6608
6609 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
6610 readNext<uint16_t>(Pos);
Aaron Ballman80930af2014-08-14 13:53:19 +00006611 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
6612 PageStart + EncodingsStart);
Tim Northover39c70bb2014-08-12 11:52:59 +00006613
6614 Pos = PageStart + EntriesStart;
6615 for (unsigned i = 0; i < NumEntries; ++i) {
6616 uint32_t Entry = readNext<uint32_t>(Pos);
6617 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
6618 uint32_t EncodingIdx = Entry >> 24;
6619
6620 uint32_t Encoding;
6621 if (EncodingIdx < CommonEncodings.size())
6622 Encoding = CommonEncodings[EncodingIdx];
6623 else
6624 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
6625
6626 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006627 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6628 << ", "
6629 << "encoding[" << EncodingIdx
6630 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006631 }
6632}
6633
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006634static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
6635 std::map<uint64_t, SymbolRef> &Symbols,
6636 const SectionRef &UnwindInfo) {
Tim Northover39c70bb2014-08-12 11:52:59 +00006637
6638 assert(Obj->isLittleEndian() &&
6639 "There should not be a big-endian .o with __unwind_info");
6640
6641 outs() << "Contents of __unwind_info section:\n";
6642
6643 StringRef Contents;
6644 UnwindInfo.getContents(Contents);
6645 const char *Pos = Contents.data();
6646
6647 //===----------------------------------
6648 // Section header
6649 //===----------------------------------
6650
6651 uint32_t Version = readNext<uint32_t>(Pos);
6652 outs() << " Version: "
6653 << format("0x%" PRIx32, Version) << '\n';
6654 assert(Version == 1 && "only understand version 1");
6655
6656 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
6657 outs() << " Common encodings array section offset: "
6658 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
6659 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
6660 outs() << " Number of common encodings in array: "
6661 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
6662
6663 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
6664 outs() << " Personality function array section offset: "
6665 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
6666 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
6667 outs() << " Number of personality functions in array: "
6668 << format("0x%" PRIx32, NumPersonalities) << '\n';
6669
6670 uint32_t IndicesStart = readNext<uint32_t>(Pos);
6671 outs() << " Index array section offset: "
6672 << format("0x%" PRIx32, IndicesStart) << '\n';
6673 uint32_t NumIndices = readNext<uint32_t>(Pos);
6674 outs() << " Number of indices in array: "
6675 << format("0x%" PRIx32, NumIndices) << '\n';
6676
6677 //===----------------------------------
6678 // A shared list of common encodings
6679 //===----------------------------------
6680
6681 // These occupy indices in the range [0, N] whenever an encoding is referenced
6682 // from a compressed 2nd level index table. In practice the linker only
6683 // creates ~128 of these, so that indices are available to embed encodings in
6684 // the 2nd level index.
6685
6686 SmallVector<uint32_t, 64> CommonEncodings;
6687 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
6688 Pos = Contents.data() + CommonEncodingsStart;
6689 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
6690 uint32_t Encoding = readNext<uint32_t>(Pos);
6691 CommonEncodings.push_back(Encoding);
6692
6693 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
6694 << '\n';
6695 }
6696
Tim Northover39c70bb2014-08-12 11:52:59 +00006697 //===----------------------------------
6698 // Personality functions used in this executable
6699 //===----------------------------------
6700
6701 // There should be only a handful of these (one per source language,
6702 // roughly). Particularly since they only get 2 bits in the compact encoding.
6703
6704 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
6705 Pos = Contents.data() + PersonalitiesStart;
6706 for (unsigned i = 0; i < NumPersonalities; ++i) {
6707 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
6708 outs() << " personality[" << i + 1
6709 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
6710 }
6711
6712 //===----------------------------------
6713 // The level 1 index entries
6714 //===----------------------------------
6715
6716 // These specify an approximate place to start searching for the more detailed
6717 // information, sorted by PC.
6718
6719 struct IndexEntry {
6720 uint32_t FunctionOffset;
6721 uint32_t SecondLevelPageStart;
6722 uint32_t LSDAStart;
6723 };
6724
6725 SmallVector<IndexEntry, 4> IndexEntries;
6726
6727 outs() << " Top level indices: (count = " << NumIndices << ")\n";
6728 Pos = Contents.data() + IndicesStart;
6729 for (unsigned i = 0; i < NumIndices; ++i) {
6730 IndexEntry Entry;
6731
6732 Entry.FunctionOffset = readNext<uint32_t>(Pos);
6733 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
6734 Entry.LSDAStart = readNext<uint32_t>(Pos);
6735 IndexEntries.push_back(Entry);
6736
6737 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006738 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
6739 << ", "
Tim Northover39c70bb2014-08-12 11:52:59 +00006740 << "2nd level page offset="
6741 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006742 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006743 }
6744
Tim Northover39c70bb2014-08-12 11:52:59 +00006745 //===----------------------------------
6746 // Next come the LSDA tables
6747 //===----------------------------------
6748
6749 // The LSDA layout is rather implicit: it's a contiguous array of entries from
6750 // the first top-level index's LSDAOffset to the last (sentinel).
6751
6752 outs() << " LSDA descriptors:\n";
6753 Pos = Contents.data() + IndexEntries[0].LSDAStart;
6754 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
6755 (2 * sizeof(uint32_t));
6756 for (int i = 0; i < NumLSDAs; ++i) {
6757 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6758 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
6759 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006760 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6761 << ", "
6762 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006763 }
6764
6765 //===----------------------------------
6766 // Finally, the 2nd level indices
6767 //===----------------------------------
6768
6769 // Generally these are 4K in size, and have 2 possible forms:
6770 // + Regular stores up to 511 entries with disparate encodings
6771 // + Compressed stores up to 1021 entries if few enough compact encoding
6772 // values are used.
6773 outs() << " Second level indices:\n";
6774 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
6775 // The final sentinel top-level index has no associated 2nd level page
6776 if (IndexEntries[i].SecondLevelPageStart == 0)
6777 break;
6778
6779 outs() << " Second level index[" << i << "]: "
6780 << "offset in section="
6781 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
6782 << ", "
6783 << "base function offset="
6784 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
6785
6786 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
Aaron Ballman80930af2014-08-14 13:53:19 +00006787 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00006788 if (Kind == 2)
6789 printRegularSecondLevelUnwindPage(Pos);
6790 else if (Kind == 3)
6791 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
6792 CommonEncodings);
6793 else
6794 llvm_unreachable("Do not know how to print this kind of 2nd level page");
Tim Northover39c70bb2014-08-12 11:52:59 +00006795 }
6796}
6797
Tim Northover4bd286a2014-08-01 13:07:19 +00006798void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
6799 std::map<uint64_t, SymbolRef> Symbols;
6800 for (const SymbolRef &SymRef : Obj->symbols()) {
6801 // Discard any undefined or absolute symbols. They're not going to take part
6802 // in the convenience lookup for unwind info and just take up resources.
6803 section_iterator Section = Obj->section_end();
6804 SymRef.getSection(Section);
6805 if (Section == Obj->section_end())
6806 continue;
6807
6808 uint64_t Addr;
6809 SymRef.getAddress(Addr);
6810 Symbols.insert(std::make_pair(Addr, SymRef));
6811 }
6812
6813 for (const SectionRef &Section : Obj->sections()) {
6814 StringRef SectName;
6815 Section.getName(SectName);
6816 if (SectName == "__compact_unwind")
6817 printMachOCompactUnwindSection(Obj, Symbols, Section);
6818 else if (SectName == "__unwind_info")
Tim Northover39c70bb2014-08-12 11:52:59 +00006819 printMachOUnwindInfoSection(Obj, Symbols, Section);
Tim Northover4bd286a2014-08-01 13:07:19 +00006820 else if (SectName == "__eh_frame")
6821 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00006822 }
6823}
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006824
6825static void PrintMachHeader(uint32_t magic, uint32_t cputype,
6826 uint32_t cpusubtype, uint32_t filetype,
6827 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
6828 bool verbose) {
6829 outs() << "Mach header\n";
6830 outs() << " magic cputype cpusubtype caps filetype ncmds "
6831 "sizeofcmds flags\n";
6832 if (verbose) {
6833 if (magic == MachO::MH_MAGIC)
6834 outs() << " MH_MAGIC";
6835 else if (magic == MachO::MH_MAGIC_64)
6836 outs() << "MH_MAGIC_64";
6837 else
6838 outs() << format(" 0x%08" PRIx32, magic);
6839 switch (cputype) {
6840 case MachO::CPU_TYPE_I386:
6841 outs() << " I386";
6842 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6843 case MachO::CPU_SUBTYPE_I386_ALL:
6844 outs() << " ALL";
6845 break;
6846 default:
6847 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6848 break;
6849 }
6850 break;
6851 case MachO::CPU_TYPE_X86_64:
6852 outs() << " X86_64";
Kevin Enderby131d1772015-01-09 19:22:37 +00006853 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6854 case MachO::CPU_SUBTYPE_X86_64_ALL:
6855 outs() << " ALL";
6856 break;
6857 case MachO::CPU_SUBTYPE_X86_64_H:
6858 outs() << " Haswell";
6859 break;
6860 default:
6861 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6862 break;
6863 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006864 break;
6865 case MachO::CPU_TYPE_ARM:
6866 outs() << " ARM";
6867 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6868 case MachO::CPU_SUBTYPE_ARM_ALL:
6869 outs() << " ALL";
6870 break;
6871 case MachO::CPU_SUBTYPE_ARM_V4T:
6872 outs() << " V4T";
6873 break;
6874 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6875 outs() << " V5TEJ";
6876 break;
6877 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6878 outs() << " XSCALE";
6879 break;
6880 case MachO::CPU_SUBTYPE_ARM_V6:
6881 outs() << " V6";
6882 break;
6883 case MachO::CPU_SUBTYPE_ARM_V6M:
6884 outs() << " V6M";
6885 break;
6886 case MachO::CPU_SUBTYPE_ARM_V7:
6887 outs() << " V7";
6888 break;
6889 case MachO::CPU_SUBTYPE_ARM_V7EM:
6890 outs() << " V7EM";
6891 break;
6892 case MachO::CPU_SUBTYPE_ARM_V7K:
6893 outs() << " V7K";
6894 break;
6895 case MachO::CPU_SUBTYPE_ARM_V7M:
6896 outs() << " V7M";
6897 break;
6898 case MachO::CPU_SUBTYPE_ARM_V7S:
6899 outs() << " V7S";
6900 break;
6901 default:
6902 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6903 break;
6904 }
6905 break;
6906 case MachO::CPU_TYPE_ARM64:
6907 outs() << " ARM64";
6908 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6909 case MachO::CPU_SUBTYPE_ARM64_ALL:
6910 outs() << " ALL";
6911 break;
6912 default:
6913 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6914 break;
6915 }
6916 break;
6917 case MachO::CPU_TYPE_POWERPC:
6918 outs() << " PPC";
6919 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6920 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6921 outs() << " ALL";
6922 break;
6923 default:
6924 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6925 break;
6926 }
6927 break;
6928 case MachO::CPU_TYPE_POWERPC64:
6929 outs() << " PPC64";
6930 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6931 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6932 outs() << " ALL";
6933 break;
6934 default:
6935 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6936 break;
6937 }
6938 break;
6939 }
6940 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00006941 outs() << " LIB64";
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006942 } else {
6943 outs() << format(" 0x%02" PRIx32,
6944 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6945 }
6946 switch (filetype) {
6947 case MachO::MH_OBJECT:
6948 outs() << " OBJECT";
6949 break;
6950 case MachO::MH_EXECUTE:
6951 outs() << " EXECUTE";
6952 break;
6953 case MachO::MH_FVMLIB:
6954 outs() << " FVMLIB";
6955 break;
6956 case MachO::MH_CORE:
6957 outs() << " CORE";
6958 break;
6959 case MachO::MH_PRELOAD:
6960 outs() << " PRELOAD";
6961 break;
6962 case MachO::MH_DYLIB:
6963 outs() << " DYLIB";
6964 break;
6965 case MachO::MH_DYLIB_STUB:
6966 outs() << " DYLIB_STUB";
6967 break;
6968 case MachO::MH_DYLINKER:
6969 outs() << " DYLINKER";
6970 break;
6971 case MachO::MH_BUNDLE:
6972 outs() << " BUNDLE";
6973 break;
6974 case MachO::MH_DSYM:
6975 outs() << " DSYM";
6976 break;
6977 case MachO::MH_KEXT_BUNDLE:
6978 outs() << " KEXTBUNDLE";
6979 break;
6980 default:
6981 outs() << format(" %10u", filetype);
6982 break;
6983 }
6984 outs() << format(" %5u", ncmds);
6985 outs() << format(" %10u", sizeofcmds);
6986 uint32_t f = flags;
6987 if (f & MachO::MH_NOUNDEFS) {
6988 outs() << " NOUNDEFS";
6989 f &= ~MachO::MH_NOUNDEFS;
6990 }
6991 if (f & MachO::MH_INCRLINK) {
6992 outs() << " INCRLINK";
6993 f &= ~MachO::MH_INCRLINK;
6994 }
6995 if (f & MachO::MH_DYLDLINK) {
6996 outs() << " DYLDLINK";
6997 f &= ~MachO::MH_DYLDLINK;
6998 }
6999 if (f & MachO::MH_BINDATLOAD) {
7000 outs() << " BINDATLOAD";
7001 f &= ~MachO::MH_BINDATLOAD;
7002 }
7003 if (f & MachO::MH_PREBOUND) {
7004 outs() << " PREBOUND";
7005 f &= ~MachO::MH_PREBOUND;
7006 }
7007 if (f & MachO::MH_SPLIT_SEGS) {
7008 outs() << " SPLIT_SEGS";
7009 f &= ~MachO::MH_SPLIT_SEGS;
7010 }
7011 if (f & MachO::MH_LAZY_INIT) {
7012 outs() << " LAZY_INIT";
7013 f &= ~MachO::MH_LAZY_INIT;
7014 }
7015 if (f & MachO::MH_TWOLEVEL) {
7016 outs() << " TWOLEVEL";
7017 f &= ~MachO::MH_TWOLEVEL;
7018 }
7019 if (f & MachO::MH_FORCE_FLAT) {
7020 outs() << " FORCE_FLAT";
7021 f &= ~MachO::MH_FORCE_FLAT;
7022 }
7023 if (f & MachO::MH_NOMULTIDEFS) {
7024 outs() << " NOMULTIDEFS";
7025 f &= ~MachO::MH_NOMULTIDEFS;
7026 }
7027 if (f & MachO::MH_NOFIXPREBINDING) {
7028 outs() << " NOFIXPREBINDING";
7029 f &= ~MachO::MH_NOFIXPREBINDING;
7030 }
7031 if (f & MachO::MH_PREBINDABLE) {
7032 outs() << " PREBINDABLE";
7033 f &= ~MachO::MH_PREBINDABLE;
7034 }
7035 if (f & MachO::MH_ALLMODSBOUND) {
7036 outs() << " ALLMODSBOUND";
7037 f &= ~MachO::MH_ALLMODSBOUND;
7038 }
7039 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7040 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7041 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7042 }
7043 if (f & MachO::MH_CANONICAL) {
7044 outs() << " CANONICAL";
7045 f &= ~MachO::MH_CANONICAL;
7046 }
7047 if (f & MachO::MH_WEAK_DEFINES) {
7048 outs() << " WEAK_DEFINES";
7049 f &= ~MachO::MH_WEAK_DEFINES;
7050 }
7051 if (f & MachO::MH_BINDS_TO_WEAK) {
7052 outs() << " BINDS_TO_WEAK";
7053 f &= ~MachO::MH_BINDS_TO_WEAK;
7054 }
7055 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7056 outs() << " ALLOW_STACK_EXECUTION";
7057 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7058 }
7059 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7060 outs() << " DEAD_STRIPPABLE_DYLIB";
7061 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7062 }
7063 if (f & MachO::MH_PIE) {
7064 outs() << " PIE";
7065 f &= ~MachO::MH_PIE;
7066 }
7067 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7068 outs() << " NO_REEXPORTED_DYLIBS";
7069 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7070 }
7071 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7072 outs() << " MH_HAS_TLV_DESCRIPTORS";
7073 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7074 }
7075 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7076 outs() << " MH_NO_HEAP_EXECUTION";
7077 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7078 }
7079 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7080 outs() << " APP_EXTENSION_SAFE";
7081 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7082 }
7083 if (f != 0 || flags == 0)
7084 outs() << format(" 0x%08" PRIx32, f);
7085 } else {
7086 outs() << format(" 0x%08" PRIx32, magic);
7087 outs() << format(" %7d", cputype);
7088 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7089 outs() << format(" 0x%02" PRIx32,
7090 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7091 outs() << format(" %10u", filetype);
7092 outs() << format(" %5u", ncmds);
7093 outs() << format(" %10u", sizeofcmds);
7094 outs() << format(" 0x%08" PRIx32, flags);
7095 }
7096 outs() << "\n";
7097}
7098
Kevin Enderby956366c2014-08-29 22:30:52 +00007099static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7100 StringRef SegName, uint64_t vmaddr,
7101 uint64_t vmsize, uint64_t fileoff,
7102 uint64_t filesize, uint32_t maxprot,
7103 uint32_t initprot, uint32_t nsects,
7104 uint32_t flags, uint32_t object_size,
7105 bool verbose) {
7106 uint64_t expected_cmdsize;
7107 if (cmd == MachO::LC_SEGMENT) {
7108 outs() << " cmd LC_SEGMENT\n";
7109 expected_cmdsize = nsects;
7110 expected_cmdsize *= sizeof(struct MachO::section);
7111 expected_cmdsize += sizeof(struct MachO::segment_command);
7112 } else {
7113 outs() << " cmd LC_SEGMENT_64\n";
7114 expected_cmdsize = nsects;
7115 expected_cmdsize *= sizeof(struct MachO::section_64);
7116 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7117 }
7118 outs() << " cmdsize " << cmdsize;
7119 if (cmdsize != expected_cmdsize)
7120 outs() << " Inconsistent size\n";
7121 else
7122 outs() << "\n";
7123 outs() << " segname " << SegName << "\n";
7124 if (cmd == MachO::LC_SEGMENT_64) {
7125 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7126 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7127 } else {
Kevin Enderbyadb7c432014-12-16 18:58:11 +00007128 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7129 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007130 }
7131 outs() << " fileoff " << fileoff;
7132 if (fileoff > object_size)
7133 outs() << " (past end of file)\n";
7134 else
7135 outs() << "\n";
7136 outs() << " filesize " << filesize;
7137 if (fileoff + filesize > object_size)
7138 outs() << " (past end of file)\n";
7139 else
7140 outs() << "\n";
7141 if (verbose) {
7142 if ((maxprot &
7143 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7144 MachO::VM_PROT_EXECUTE)) != 0)
7145 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7146 else {
7147 if (maxprot & MachO::VM_PROT_READ)
7148 outs() << " maxprot r";
7149 else
7150 outs() << " maxprot -";
7151 if (maxprot & MachO::VM_PROT_WRITE)
7152 outs() << "w";
7153 else
7154 outs() << "-";
7155 if (maxprot & MachO::VM_PROT_EXECUTE)
7156 outs() << "x\n";
7157 else
7158 outs() << "-\n";
7159 }
7160 if ((initprot &
7161 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7162 MachO::VM_PROT_EXECUTE)) != 0)
7163 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7164 else {
7165 if (initprot & MachO::VM_PROT_READ)
7166 outs() << " initprot r";
7167 else
7168 outs() << " initprot -";
7169 if (initprot & MachO::VM_PROT_WRITE)
7170 outs() << "w";
7171 else
7172 outs() << "-";
7173 if (initprot & MachO::VM_PROT_EXECUTE)
7174 outs() << "x\n";
7175 else
7176 outs() << "-\n";
7177 }
7178 } else {
7179 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7180 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7181 }
7182 outs() << " nsects " << nsects << "\n";
7183 if (verbose) {
7184 outs() << " flags";
7185 if (flags == 0)
7186 outs() << " (none)\n";
7187 else {
7188 if (flags & MachO::SG_HIGHVM) {
7189 outs() << " HIGHVM";
7190 flags &= ~MachO::SG_HIGHVM;
7191 }
7192 if (flags & MachO::SG_FVMLIB) {
7193 outs() << " FVMLIB";
7194 flags &= ~MachO::SG_FVMLIB;
7195 }
7196 if (flags & MachO::SG_NORELOC) {
7197 outs() << " NORELOC";
7198 flags &= ~MachO::SG_NORELOC;
7199 }
7200 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7201 outs() << " PROTECTED_VERSION_1";
7202 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7203 }
7204 if (flags)
7205 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7206 else
7207 outs() << "\n";
7208 }
7209 } else {
7210 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7211 }
7212}
7213
7214static void PrintSection(const char *sectname, const char *segname,
7215 uint64_t addr, uint64_t size, uint32_t offset,
7216 uint32_t align, uint32_t reloff, uint32_t nreloc,
7217 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7218 uint32_t cmd, const char *sg_segname,
7219 uint32_t filetype, uint32_t object_size,
7220 bool verbose) {
7221 outs() << "Section\n";
7222 outs() << " sectname " << format("%.16s\n", sectname);
7223 outs() << " segname " << format("%.16s", segname);
7224 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7225 outs() << " (does not match segment)\n";
7226 else
7227 outs() << "\n";
7228 if (cmd == MachO::LC_SEGMENT_64) {
7229 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7230 outs() << " size " << format("0x%016" PRIx64, size);
7231 } else {
Kevin Enderby75594b62014-12-16 21:00:25 +00007232 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7233 outs() << " size " << format("0x%08" PRIx64, size);
Kevin Enderby956366c2014-08-29 22:30:52 +00007234 }
7235 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7236 outs() << " (past end of file)\n";
7237 else
7238 outs() << "\n";
7239 outs() << " offset " << offset;
7240 if (offset > object_size)
7241 outs() << " (past end of file)\n";
7242 else
7243 outs() << "\n";
7244 uint32_t align_shifted = 1 << align;
7245 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7246 outs() << " reloff " << reloff;
7247 if (reloff > object_size)
7248 outs() << " (past end of file)\n";
7249 else
7250 outs() << "\n";
7251 outs() << " nreloc " << nreloc;
7252 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7253 outs() << " (past end of file)\n";
7254 else
7255 outs() << "\n";
7256 uint32_t section_type = flags & MachO::SECTION_TYPE;
7257 if (verbose) {
7258 outs() << " type";
7259 if (section_type == MachO::S_REGULAR)
7260 outs() << " S_REGULAR\n";
7261 else if (section_type == MachO::S_ZEROFILL)
7262 outs() << " S_ZEROFILL\n";
7263 else if (section_type == MachO::S_CSTRING_LITERALS)
7264 outs() << " S_CSTRING_LITERALS\n";
7265 else if (section_type == MachO::S_4BYTE_LITERALS)
7266 outs() << " S_4BYTE_LITERALS\n";
7267 else if (section_type == MachO::S_8BYTE_LITERALS)
7268 outs() << " S_8BYTE_LITERALS\n";
7269 else if (section_type == MachO::S_16BYTE_LITERALS)
7270 outs() << " S_16BYTE_LITERALS\n";
7271 else if (section_type == MachO::S_LITERAL_POINTERS)
7272 outs() << " S_LITERAL_POINTERS\n";
7273 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7274 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7275 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7276 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7277 else if (section_type == MachO::S_SYMBOL_STUBS)
7278 outs() << " S_SYMBOL_STUBS\n";
7279 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7280 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7281 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7282 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7283 else if (section_type == MachO::S_COALESCED)
7284 outs() << " S_COALESCED\n";
7285 else if (section_type == MachO::S_INTERPOSING)
7286 outs() << " S_INTERPOSING\n";
7287 else if (section_type == MachO::S_DTRACE_DOF)
7288 outs() << " S_DTRACE_DOF\n";
7289 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7290 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7291 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7292 outs() << " S_THREAD_LOCAL_REGULAR\n";
7293 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7294 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7295 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7296 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7297 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7298 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7299 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7300 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7301 else
7302 outs() << format("0x%08" PRIx32, section_type) << "\n";
7303 outs() << "attributes";
7304 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7305 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7306 outs() << " PURE_INSTRUCTIONS";
7307 if (section_attributes & MachO::S_ATTR_NO_TOC)
7308 outs() << " NO_TOC";
7309 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7310 outs() << " STRIP_STATIC_SYMS";
7311 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7312 outs() << " NO_DEAD_STRIP";
7313 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7314 outs() << " LIVE_SUPPORT";
7315 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7316 outs() << " SELF_MODIFYING_CODE";
7317 if (section_attributes & MachO::S_ATTR_DEBUG)
7318 outs() << " DEBUG";
7319 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7320 outs() << " SOME_INSTRUCTIONS";
7321 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7322 outs() << " EXT_RELOC";
7323 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7324 outs() << " LOC_RELOC";
7325 if (section_attributes == 0)
7326 outs() << " (none)";
7327 outs() << "\n";
7328 } else
7329 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7330 outs() << " reserved1 " << reserved1;
7331 if (section_type == MachO::S_SYMBOL_STUBS ||
7332 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7333 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7334 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7335 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7336 outs() << " (index into indirect symbol table)\n";
7337 else
7338 outs() << "\n";
7339 outs() << " reserved2 " << reserved2;
7340 if (section_type == MachO::S_SYMBOL_STUBS)
7341 outs() << " (size of stubs)\n";
7342 else
7343 outs() << "\n";
7344}
7345
David Majnemer73cc6ff2014-11-13 19:48:56 +00007346static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
Kevin Enderby956366c2014-08-29 22:30:52 +00007347 uint32_t object_size) {
7348 outs() << " cmd LC_SYMTAB\n";
7349 outs() << " cmdsize " << st.cmdsize;
7350 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7351 outs() << " Incorrect size\n";
7352 else
7353 outs() << "\n";
7354 outs() << " symoff " << st.symoff;
7355 if (st.symoff > object_size)
7356 outs() << " (past end of file)\n";
7357 else
7358 outs() << "\n";
7359 outs() << " nsyms " << st.nsyms;
7360 uint64_t big_size;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007361 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007362 big_size = st.nsyms;
7363 big_size *= sizeof(struct MachO::nlist_64);
7364 big_size += st.symoff;
7365 if (big_size > object_size)
7366 outs() << " (past end of file)\n";
7367 else
7368 outs() << "\n";
7369 } else {
7370 big_size = st.nsyms;
7371 big_size *= sizeof(struct MachO::nlist);
7372 big_size += st.symoff;
7373 if (big_size > object_size)
7374 outs() << " (past end of file)\n";
7375 else
7376 outs() << "\n";
7377 }
7378 outs() << " stroff " << st.stroff;
7379 if (st.stroff > object_size)
7380 outs() << " (past end of file)\n";
7381 else
7382 outs() << "\n";
7383 outs() << " strsize " << st.strsize;
7384 big_size = st.stroff;
7385 big_size += st.strsize;
7386 if (big_size > object_size)
7387 outs() << " (past end of file)\n";
7388 else
7389 outs() << "\n";
7390}
7391
7392static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7393 uint32_t nsyms, uint32_t object_size,
David Majnemer73cc6ff2014-11-13 19:48:56 +00007394 bool Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007395 outs() << " cmd LC_DYSYMTAB\n";
7396 outs() << " cmdsize " << dyst.cmdsize;
7397 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7398 outs() << " Incorrect size\n";
7399 else
7400 outs() << "\n";
7401 outs() << " ilocalsym " << dyst.ilocalsym;
7402 if (dyst.ilocalsym > nsyms)
7403 outs() << " (greater than the number of symbols)\n";
7404 else
7405 outs() << "\n";
7406 outs() << " nlocalsym " << dyst.nlocalsym;
7407 uint64_t big_size;
7408 big_size = dyst.ilocalsym;
7409 big_size += dyst.nlocalsym;
7410 if (big_size > nsyms)
7411 outs() << " (past the end of the symbol table)\n";
7412 else
7413 outs() << "\n";
7414 outs() << " iextdefsym " << dyst.iextdefsym;
7415 if (dyst.iextdefsym > nsyms)
7416 outs() << " (greater than the number of symbols)\n";
7417 else
7418 outs() << "\n";
7419 outs() << " nextdefsym " << dyst.nextdefsym;
7420 big_size = dyst.iextdefsym;
7421 big_size += dyst.nextdefsym;
7422 if (big_size > nsyms)
7423 outs() << " (past the end of the symbol table)\n";
7424 else
7425 outs() << "\n";
7426 outs() << " iundefsym " << dyst.iundefsym;
7427 if (dyst.iundefsym > nsyms)
7428 outs() << " (greater than the number of symbols)\n";
7429 else
7430 outs() << "\n";
7431 outs() << " nundefsym " << dyst.nundefsym;
7432 big_size = dyst.iundefsym;
7433 big_size += dyst.nundefsym;
7434 if (big_size > nsyms)
7435 outs() << " (past the end of the symbol table)\n";
7436 else
7437 outs() << "\n";
7438 outs() << " tocoff " << dyst.tocoff;
7439 if (dyst.tocoff > object_size)
7440 outs() << " (past end of file)\n";
7441 else
7442 outs() << "\n";
7443 outs() << " ntoc " << dyst.ntoc;
7444 big_size = dyst.ntoc;
7445 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7446 big_size += dyst.tocoff;
7447 if (big_size > object_size)
7448 outs() << " (past end of file)\n";
7449 else
7450 outs() << "\n";
7451 outs() << " modtaboff " << dyst.modtaboff;
7452 if (dyst.modtaboff > object_size)
7453 outs() << " (past end of file)\n";
7454 else
7455 outs() << "\n";
7456 outs() << " nmodtab " << dyst.nmodtab;
7457 uint64_t modtabend;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007458 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007459 modtabend = dyst.nmodtab;
7460 modtabend *= sizeof(struct MachO::dylib_module_64);
7461 modtabend += dyst.modtaboff;
7462 } else {
7463 modtabend = dyst.nmodtab;
7464 modtabend *= sizeof(struct MachO::dylib_module);
7465 modtabend += dyst.modtaboff;
7466 }
7467 if (modtabend > object_size)
7468 outs() << " (past end of file)\n";
7469 else
7470 outs() << "\n";
7471 outs() << " extrefsymoff " << dyst.extrefsymoff;
7472 if (dyst.extrefsymoff > object_size)
7473 outs() << " (past end of file)\n";
7474 else
7475 outs() << "\n";
7476 outs() << " nextrefsyms " << dyst.nextrefsyms;
7477 big_size = dyst.nextrefsyms;
7478 big_size *= sizeof(struct MachO::dylib_reference);
7479 big_size += dyst.extrefsymoff;
7480 if (big_size > object_size)
7481 outs() << " (past end of file)\n";
7482 else
7483 outs() << "\n";
7484 outs() << " indirectsymoff " << dyst.indirectsymoff;
7485 if (dyst.indirectsymoff > object_size)
7486 outs() << " (past end of file)\n";
7487 else
7488 outs() << "\n";
7489 outs() << " nindirectsyms " << dyst.nindirectsyms;
7490 big_size = dyst.nindirectsyms;
7491 big_size *= sizeof(uint32_t);
7492 big_size += dyst.indirectsymoff;
7493 if (big_size > object_size)
7494 outs() << " (past end of file)\n";
7495 else
7496 outs() << "\n";
7497 outs() << " extreloff " << dyst.extreloff;
7498 if (dyst.extreloff > object_size)
7499 outs() << " (past end of file)\n";
7500 else
7501 outs() << "\n";
7502 outs() << " nextrel " << dyst.nextrel;
7503 big_size = dyst.nextrel;
7504 big_size *= sizeof(struct MachO::relocation_info);
7505 big_size += dyst.extreloff;
7506 if (big_size > object_size)
7507 outs() << " (past end of file)\n";
7508 else
7509 outs() << "\n";
7510 outs() << " locreloff " << dyst.locreloff;
7511 if (dyst.locreloff > object_size)
7512 outs() << " (past end of file)\n";
7513 else
7514 outs() << "\n";
7515 outs() << " nlocrel " << dyst.nlocrel;
7516 big_size = dyst.nlocrel;
7517 big_size *= sizeof(struct MachO::relocation_info);
7518 big_size += dyst.locreloff;
7519 if (big_size > object_size)
7520 outs() << " (past end of file)\n";
7521 else
7522 outs() << "\n";
7523}
7524
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007525static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
7526 uint32_t object_size) {
7527 if (dc.cmd == MachO::LC_DYLD_INFO)
7528 outs() << " cmd LC_DYLD_INFO\n";
7529 else
7530 outs() << " cmd LC_DYLD_INFO_ONLY\n";
7531 outs() << " cmdsize " << dc.cmdsize;
7532 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
7533 outs() << " Incorrect size\n";
7534 else
7535 outs() << "\n";
7536 outs() << " rebase_off " << dc.rebase_off;
7537 if (dc.rebase_off > object_size)
7538 outs() << " (past end of file)\n";
7539 else
7540 outs() << "\n";
7541 outs() << " rebase_size " << dc.rebase_size;
7542 uint64_t big_size;
7543 big_size = dc.rebase_off;
7544 big_size += dc.rebase_size;
7545 if (big_size > object_size)
7546 outs() << " (past end of file)\n";
7547 else
7548 outs() << "\n";
7549 outs() << " bind_off " << dc.bind_off;
7550 if (dc.bind_off > object_size)
7551 outs() << " (past end of file)\n";
7552 else
7553 outs() << "\n";
7554 outs() << " bind_size " << dc.bind_size;
7555 big_size = dc.bind_off;
7556 big_size += dc.bind_size;
7557 if (big_size > object_size)
7558 outs() << " (past end of file)\n";
7559 else
7560 outs() << "\n";
7561 outs() << " weak_bind_off " << dc.weak_bind_off;
7562 if (dc.weak_bind_off > object_size)
7563 outs() << " (past end of file)\n";
7564 else
7565 outs() << "\n";
7566 outs() << " weak_bind_size " << dc.weak_bind_size;
7567 big_size = dc.weak_bind_off;
7568 big_size += dc.weak_bind_size;
7569 if (big_size > object_size)
7570 outs() << " (past end of file)\n";
7571 else
7572 outs() << "\n";
7573 outs() << " lazy_bind_off " << dc.lazy_bind_off;
7574 if (dc.lazy_bind_off > object_size)
7575 outs() << " (past end of file)\n";
7576 else
7577 outs() << "\n";
7578 outs() << " lazy_bind_size " << dc.lazy_bind_size;
7579 big_size = dc.lazy_bind_off;
7580 big_size += dc.lazy_bind_size;
7581 if (big_size > object_size)
7582 outs() << " (past end of file)\n";
7583 else
7584 outs() << "\n";
7585 outs() << " export_off " << dc.export_off;
7586 if (dc.export_off > object_size)
7587 outs() << " (past end of file)\n";
7588 else
7589 outs() << "\n";
7590 outs() << " export_size " << dc.export_size;
7591 big_size = dc.export_off;
7592 big_size += dc.export_size;
7593 if (big_size > object_size)
7594 outs() << " (past end of file)\n";
7595 else
7596 outs() << "\n";
7597}
7598
7599static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
7600 const char *Ptr) {
7601 if (dyld.cmd == MachO::LC_ID_DYLINKER)
7602 outs() << " cmd LC_ID_DYLINKER\n";
7603 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
7604 outs() << " cmd LC_LOAD_DYLINKER\n";
7605 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
7606 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
7607 else
7608 outs() << " cmd ?(" << dyld.cmd << ")\n";
7609 outs() << " cmdsize " << dyld.cmdsize;
7610 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
7611 outs() << " Incorrect size\n";
7612 else
7613 outs() << "\n";
7614 if (dyld.name >= dyld.cmdsize)
7615 outs() << " name ?(bad offset " << dyld.name << ")\n";
7616 else {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007617 const char *P = (const char *)(Ptr) + dyld.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007618 outs() << " name " << P << " (offset " << dyld.name << ")\n";
7619 }
7620}
7621
7622static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
7623 outs() << " cmd LC_UUID\n";
7624 outs() << " cmdsize " << uuid.cmdsize;
7625 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
7626 outs() << " Incorrect size\n";
7627 else
7628 outs() << "\n";
7629 outs() << " uuid ";
7630 outs() << format("%02" PRIX32, uuid.uuid[0]);
7631 outs() << format("%02" PRIX32, uuid.uuid[1]);
7632 outs() << format("%02" PRIX32, uuid.uuid[2]);
7633 outs() << format("%02" PRIX32, uuid.uuid[3]);
7634 outs() << "-";
7635 outs() << format("%02" PRIX32, uuid.uuid[4]);
7636 outs() << format("%02" PRIX32, uuid.uuid[5]);
7637 outs() << "-";
7638 outs() << format("%02" PRIX32, uuid.uuid[6]);
7639 outs() << format("%02" PRIX32, uuid.uuid[7]);
7640 outs() << "-";
7641 outs() << format("%02" PRIX32, uuid.uuid[8]);
7642 outs() << format("%02" PRIX32, uuid.uuid[9]);
7643 outs() << "-";
7644 outs() << format("%02" PRIX32, uuid.uuid[10]);
7645 outs() << format("%02" PRIX32, uuid.uuid[11]);
7646 outs() << format("%02" PRIX32, uuid.uuid[12]);
7647 outs() << format("%02" PRIX32, uuid.uuid[13]);
7648 outs() << format("%02" PRIX32, uuid.uuid[14]);
7649 outs() << format("%02" PRIX32, uuid.uuid[15]);
7650 outs() << "\n";
7651}
7652
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007653static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00007654 outs() << " cmd LC_RPATH\n";
7655 outs() << " cmdsize " << rpath.cmdsize;
7656 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
7657 outs() << " Incorrect size\n";
7658 else
7659 outs() << "\n";
7660 if (rpath.path >= rpath.cmdsize)
7661 outs() << " path ?(bad offset " << rpath.path << ")\n";
7662 else {
7663 const char *P = (const char *)(Ptr) + rpath.path;
7664 outs() << " path " << P << " (offset " << rpath.path << ")\n";
7665 }
7666}
7667
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007668static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
7669 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
7670 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
7671 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
7672 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
7673 else
7674 outs() << " cmd " << vd.cmd << " (?)\n";
7675 outs() << " cmdsize " << vd.cmdsize;
7676 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
7677 outs() << " Incorrect size\n";
7678 else
7679 outs() << "\n";
7680 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
7681 << ((vd.version >> 8) & 0xff);
7682 if ((vd.version & 0xff) != 0)
7683 outs() << "." << (vd.version & 0xff);
7684 outs() << "\n";
7685 if (vd.sdk == 0)
Kevin Enderby57538292014-12-17 01:01:30 +00007686 outs() << " sdk n/a";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007687 else {
7688 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
7689 << ((vd.sdk >> 8) & 0xff);
7690 }
7691 if ((vd.sdk & 0xff) != 0)
7692 outs() << "." << (vd.sdk & 0xff);
7693 outs() << "\n";
7694}
7695
7696static void PrintSourceVersionCommand(MachO::source_version_command sd) {
7697 outs() << " cmd LC_SOURCE_VERSION\n";
7698 outs() << " cmdsize " << sd.cmdsize;
7699 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
7700 outs() << " Incorrect size\n";
7701 else
7702 outs() << "\n";
7703 uint64_t a = (sd.version >> 40) & 0xffffff;
7704 uint64_t b = (sd.version >> 30) & 0x3ff;
7705 uint64_t c = (sd.version >> 20) & 0x3ff;
7706 uint64_t d = (sd.version >> 10) & 0x3ff;
7707 uint64_t e = sd.version & 0x3ff;
7708 outs() << " version " << a << "." << b;
7709 if (e != 0)
7710 outs() << "." << c << "." << d << "." << e;
7711 else if (d != 0)
7712 outs() << "." << c << "." << d;
7713 else if (c != 0)
7714 outs() << "." << c;
7715 outs() << "\n";
7716}
7717
7718static void PrintEntryPointCommand(MachO::entry_point_command ep) {
7719 outs() << " cmd LC_MAIN\n";
7720 outs() << " cmdsize " << ep.cmdsize;
7721 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
7722 outs() << " Incorrect size\n";
7723 else
7724 outs() << "\n";
7725 outs() << " entryoff " << ep.entryoff << "\n";
7726 outs() << " stacksize " << ep.stacksize << "\n";
7727}
7728
Kevin Enderby0804f4672014-12-16 23:25:52 +00007729static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
7730 uint32_t object_size) {
7731 outs() << " cmd LC_ENCRYPTION_INFO\n";
7732 outs() << " cmdsize " << ec.cmdsize;
7733 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
7734 outs() << " Incorrect size\n";
7735 else
7736 outs() << "\n";
7737 outs() << " cryptoff " << ec.cryptoff;
7738 if (ec.cryptoff > object_size)
7739 outs() << " (past end of file)\n";
7740 else
7741 outs() << "\n";
7742 outs() << " cryptsize " << ec.cryptsize;
7743 if (ec.cryptsize > object_size)
7744 outs() << " (past end of file)\n";
7745 else
7746 outs() << "\n";
7747 outs() << " cryptid " << ec.cryptid << "\n";
7748}
7749
Kevin Enderby57538292014-12-17 01:01:30 +00007750static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007751 uint32_t object_size) {
Kevin Enderby57538292014-12-17 01:01:30 +00007752 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
7753 outs() << " cmdsize " << ec.cmdsize;
7754 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
7755 outs() << " Incorrect size\n";
7756 else
7757 outs() << "\n";
7758 outs() << " cryptoff " << ec.cryptoff;
7759 if (ec.cryptoff > object_size)
7760 outs() << " (past end of file)\n";
7761 else
7762 outs() << "\n";
7763 outs() << " cryptsize " << ec.cryptsize;
7764 if (ec.cryptsize > object_size)
7765 outs() << " (past end of file)\n";
7766 else
7767 outs() << "\n";
7768 outs() << " cryptid " << ec.cryptid << "\n";
7769 outs() << " pad " << ec.pad << "\n";
7770}
7771
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007772static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
7773 const char *Ptr) {
7774 outs() << " cmd LC_LINKER_OPTION\n";
7775 outs() << " cmdsize " << lo.cmdsize;
7776 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
7777 outs() << " Incorrect size\n";
7778 else
7779 outs() << "\n";
7780 outs() << " count " << lo.count << "\n";
7781 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
7782 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
7783 uint32_t i = 0;
7784 while (left > 0) {
7785 while (*string == '\0' && left > 0) {
7786 string++;
7787 left--;
7788 }
7789 if (left > 0) {
7790 i++;
7791 outs() << " string #" << i << " " << format("%.*s\n", left, string);
David Majnemerd4449ed2014-12-20 08:24:43 +00007792 uint32_t NullPos = StringRef(string, left).find('\0');
7793 uint32_t len = std::min(NullPos, left) + 1;
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007794 string += len;
7795 left -= len;
7796 }
7797 }
7798 if (lo.count != i)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007799 outs() << " count " << lo.count << " does not match number of strings "
7800 << i << "\n";
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007801}
7802
Kevin Enderbyb4b79312014-12-18 19:24:35 +00007803static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
7804 const char *Ptr) {
7805 outs() << " cmd LC_SUB_FRAMEWORK\n";
7806 outs() << " cmdsize " << sub.cmdsize;
7807 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
7808 outs() << " Incorrect size\n";
7809 else
7810 outs() << "\n";
7811 if (sub.umbrella < sub.cmdsize) {
7812 const char *P = Ptr + sub.umbrella;
7813 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
7814 } else {
7815 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
7816 }
7817}
7818
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00007819static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
7820 const char *Ptr) {
7821 outs() << " cmd LC_SUB_UMBRELLA\n";
7822 outs() << " cmdsize " << sub.cmdsize;
7823 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
7824 outs() << " Incorrect size\n";
7825 else
7826 outs() << "\n";
7827 if (sub.sub_umbrella < sub.cmdsize) {
7828 const char *P = Ptr + sub.sub_umbrella;
7829 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
7830 } else {
7831 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
7832 }
7833}
7834
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007835static void PrintSubLibraryCommand(MachO::sub_library_command sub,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007836 const char *Ptr) {
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007837 outs() << " cmd LC_SUB_LIBRARY\n";
7838 outs() << " cmdsize " << sub.cmdsize;
7839 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
7840 outs() << " Incorrect size\n";
7841 else
7842 outs() << "\n";
7843 if (sub.sub_library < sub.cmdsize) {
7844 const char *P = Ptr + sub.sub_library;
7845 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7846 } else {
7847 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7848 }
7849}
7850
Kevin Enderby186eac32014-12-19 21:06:24 +00007851static void PrintSubClientCommand(MachO::sub_client_command sub,
7852 const char *Ptr) {
7853 outs() << " cmd LC_SUB_CLIENT\n";
7854 outs() << " cmdsize " << sub.cmdsize;
7855 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7856 outs() << " Incorrect size\n";
7857 else
7858 outs() << "\n";
7859 if (sub.client < sub.cmdsize) {
7860 const char *P = Ptr + sub.client;
7861 outs() << " client " << P << " (offset " << sub.client << ")\n";
7862 } else {
7863 outs() << " client ?(bad offset " << sub.client << ")\n";
7864 }
7865}
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007866
Kevin Enderby52e4ce42014-12-19 22:25:22 +00007867static void PrintRoutinesCommand(MachO::routines_command r) {
7868 outs() << " cmd LC_ROUTINES\n";
7869 outs() << " cmdsize " << r.cmdsize;
7870 if (r.cmdsize != sizeof(struct MachO::routines_command))
7871 outs() << " Incorrect size\n";
7872 else
7873 outs() << "\n";
7874 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7875 outs() << " init_module " << r.init_module << "\n";
7876 outs() << " reserved1 " << r.reserved1 << "\n";
7877 outs() << " reserved2 " << r.reserved2 << "\n";
7878 outs() << " reserved3 " << r.reserved3 << "\n";
7879 outs() << " reserved4 " << r.reserved4 << "\n";
7880 outs() << " reserved5 " << r.reserved5 << "\n";
7881 outs() << " reserved6 " << r.reserved6 << "\n";
7882}
7883
7884static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7885 outs() << " cmd LC_ROUTINES_64\n";
7886 outs() << " cmdsize " << r.cmdsize;
7887 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7888 outs() << " Incorrect size\n";
7889 else
7890 outs() << "\n";
7891 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
7892 outs() << " init_module " << r.init_module << "\n";
7893 outs() << " reserved1 " << r.reserved1 << "\n";
7894 outs() << " reserved2 " << r.reserved2 << "\n";
7895 outs() << " reserved3 " << r.reserved3 << "\n";
7896 outs() << " reserved4 " << r.reserved4 << "\n";
7897 outs() << " reserved5 " << r.reserved5 << "\n";
7898 outs() << " reserved6 " << r.reserved6 << "\n";
7899}
7900
Kevin Enderby48ef5342014-12-23 22:56:39 +00007901static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7902 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7903 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7904 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7905 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7906 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7907 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7908 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7909 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7910 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7911 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7912 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7913 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7914 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7915 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7916 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7917 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7918 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7919 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7920 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7921 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7922 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7923}
7924
Kevin Enderby227df342014-12-23 23:43:59 +00007925static void Print_mmst_reg(MachO::mmst_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007926 uint32_t f;
7927 outs() << "\t mmst_reg ";
7928 for (f = 0; f < 10; f++)
7929 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7930 outs() << "\n";
7931 outs() << "\t mmst_rsrv ";
7932 for (f = 0; f < 6; f++)
7933 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7934 outs() << "\n";
7935}
7936
Kevin Enderbyaefb0032014-12-24 00:16:51 +00007937static void Print_xmm_reg(MachO::xmm_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007938 uint32_t f;
7939 outs() << "\t xmm_reg ";
7940 for (f = 0; f < 16; f++)
7941 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7942 outs() << "\n";
7943}
7944
7945static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7946 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7947 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7948 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7949 outs() << " denorm " << fpu.fpu_fcw.denorm;
7950 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7951 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7952 outs() << " undfl " << fpu.fpu_fcw.undfl;
7953 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7954 outs() << "\t\t pc ";
7955 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7956 outs() << "FP_PREC_24B ";
7957 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
7958 outs() << "FP_PREC_53B ";
7959 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
7960 outs() << "FP_PREC_64B ";
7961 else
7962 outs() << fpu.fpu_fcw.pc << " ";
7963 outs() << "rc ";
7964 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
7965 outs() << "FP_RND_NEAR ";
7966 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
7967 outs() << "FP_RND_DOWN ";
7968 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
7969 outs() << "FP_RND_UP ";
7970 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007971 outs() << "FP_CHOP ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00007972 outs() << "\n";
7973 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
7974 outs() << " denorm " << fpu.fpu_fsw.denorm;
7975 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
7976 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
7977 outs() << " undfl " << fpu.fpu_fsw.undfl;
7978 outs() << " precis " << fpu.fpu_fsw.precis;
7979 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
7980 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
7981 outs() << " c0 " << fpu.fpu_fsw.c0;
7982 outs() << " c1 " << fpu.fpu_fsw.c1;
7983 outs() << " c2 " << fpu.fpu_fsw.c2;
7984 outs() << " tos " << fpu.fpu_fsw.tos;
7985 outs() << " c3 " << fpu.fpu_fsw.c3;
7986 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
7987 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
7988 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
7989 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
7990 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
7991 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
7992 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
7993 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
7994 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
7995 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
7996 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
7997 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
7998 outs() << "\n";
7999 outs() << "\t fpu_stmm0:\n";
8000 Print_mmst_reg(fpu.fpu_stmm0);
8001 outs() << "\t fpu_stmm1:\n";
8002 Print_mmst_reg(fpu.fpu_stmm1);
8003 outs() << "\t fpu_stmm2:\n";
8004 Print_mmst_reg(fpu.fpu_stmm2);
8005 outs() << "\t fpu_stmm3:\n";
8006 Print_mmst_reg(fpu.fpu_stmm3);
8007 outs() << "\t fpu_stmm4:\n";
8008 Print_mmst_reg(fpu.fpu_stmm4);
8009 outs() << "\t fpu_stmm5:\n";
8010 Print_mmst_reg(fpu.fpu_stmm5);
8011 outs() << "\t fpu_stmm6:\n";
8012 Print_mmst_reg(fpu.fpu_stmm6);
8013 outs() << "\t fpu_stmm7:\n";
8014 Print_mmst_reg(fpu.fpu_stmm7);
8015 outs() << "\t fpu_xmm0:\n";
8016 Print_xmm_reg(fpu.fpu_xmm0);
8017 outs() << "\t fpu_xmm1:\n";
8018 Print_xmm_reg(fpu.fpu_xmm1);
8019 outs() << "\t fpu_xmm2:\n";
8020 Print_xmm_reg(fpu.fpu_xmm2);
8021 outs() << "\t fpu_xmm3:\n";
8022 Print_xmm_reg(fpu.fpu_xmm3);
8023 outs() << "\t fpu_xmm4:\n";
8024 Print_xmm_reg(fpu.fpu_xmm4);
8025 outs() << "\t fpu_xmm5:\n";
8026 Print_xmm_reg(fpu.fpu_xmm5);
8027 outs() << "\t fpu_xmm6:\n";
8028 Print_xmm_reg(fpu.fpu_xmm6);
8029 outs() << "\t fpu_xmm7:\n";
8030 Print_xmm_reg(fpu.fpu_xmm7);
8031 outs() << "\t fpu_xmm8:\n";
8032 Print_xmm_reg(fpu.fpu_xmm8);
8033 outs() << "\t fpu_xmm9:\n";
8034 Print_xmm_reg(fpu.fpu_xmm9);
8035 outs() << "\t fpu_xmm10:\n";
8036 Print_xmm_reg(fpu.fpu_xmm10);
8037 outs() << "\t fpu_xmm11:\n";
8038 Print_xmm_reg(fpu.fpu_xmm11);
8039 outs() << "\t fpu_xmm12:\n";
8040 Print_xmm_reg(fpu.fpu_xmm12);
8041 outs() << "\t fpu_xmm13:\n";
8042 Print_xmm_reg(fpu.fpu_xmm13);
8043 outs() << "\t fpu_xmm14:\n";
8044 Print_xmm_reg(fpu.fpu_xmm14);
8045 outs() << "\t fpu_xmm15:\n";
8046 Print_xmm_reg(fpu.fpu_xmm15);
8047 outs() << "\t fpu_rsrv4:\n";
8048 for (uint32_t f = 0; f < 6; f++) {
8049 outs() << "\t ";
8050 for (uint32_t g = 0; g < 16; g++)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008051 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008052 outs() << "\n";
8053 }
8054 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8055 outs() << "\n";
8056}
8057
8058static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8059 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8060 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8061 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8062}
8063
8064static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8065 bool isLittleEndian, uint32_t cputype) {
8066 if (t.cmd == MachO::LC_THREAD)
8067 outs() << " cmd LC_THREAD\n";
8068 else if (t.cmd == MachO::LC_UNIXTHREAD)
8069 outs() << " cmd LC_UNIXTHREAD\n";
8070 else
8071 outs() << " cmd " << t.cmd << " (unknown)\n";
8072 outs() << " cmdsize " << t.cmdsize;
8073 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8074 outs() << " Incorrect size\n";
8075 else
8076 outs() << "\n";
8077
8078 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8079 const char *end = Ptr + t.cmdsize;
8080 uint32_t flavor, count, left;
8081 if (cputype == MachO::CPU_TYPE_X86_64) {
8082 while (begin < end) {
8083 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8084 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8085 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008086 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008087 flavor = 0;
8088 begin = end;
8089 }
8090 if (isLittleEndian != sys::IsLittleEndianHost)
8091 sys::swapByteOrder(flavor);
8092 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8093 memcpy((char *)&count, begin, sizeof(uint32_t));
8094 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008095 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008096 count = 0;
8097 begin = end;
8098 }
8099 if (isLittleEndian != sys::IsLittleEndianHost)
8100 sys::swapByteOrder(count);
8101 if (flavor == MachO::x86_THREAD_STATE64) {
8102 outs() << " flavor x86_THREAD_STATE64\n";
8103 if (count == MachO::x86_THREAD_STATE64_COUNT)
8104 outs() << " count x86_THREAD_STATE64_COUNT\n";
8105 else
8106 outs() << " count " << count
8107 << " (not x86_THREAD_STATE64_COUNT)\n";
8108 MachO::x86_thread_state64_t cpu64;
8109 left = end - begin;
8110 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8111 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8112 begin += sizeof(MachO::x86_thread_state64_t);
8113 } else {
8114 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8115 memcpy(&cpu64, begin, left);
8116 begin += left;
8117 }
8118 if (isLittleEndian != sys::IsLittleEndianHost)
8119 swapStruct(cpu64);
8120 Print_x86_thread_state64_t(cpu64);
8121 } else if (flavor == MachO::x86_THREAD_STATE) {
8122 outs() << " flavor x86_THREAD_STATE\n";
8123 if (count == MachO::x86_THREAD_STATE_COUNT)
8124 outs() << " count x86_THREAD_STATE_COUNT\n";
8125 else
8126 outs() << " count " << count
8127 << " (not x86_THREAD_STATE_COUNT)\n";
8128 struct MachO::x86_thread_state_t ts;
8129 left = end - begin;
8130 if (left >= sizeof(MachO::x86_thread_state_t)) {
8131 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8132 begin += sizeof(MachO::x86_thread_state_t);
8133 } else {
8134 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8135 memcpy(&ts, begin, left);
8136 begin += left;
8137 }
8138 if (isLittleEndian != sys::IsLittleEndianHost)
8139 swapStruct(ts);
8140 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8141 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8142 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8143 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8144 else
8145 outs() << "tsh.count " << ts.tsh.count
8146 << " (not x86_THREAD_STATE64_COUNT\n";
8147 Print_x86_thread_state64_t(ts.uts.ts64);
8148 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008149 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8150 << ts.tsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008151 }
8152 } else if (flavor == MachO::x86_FLOAT_STATE) {
8153 outs() << " flavor x86_FLOAT_STATE\n";
8154 if (count == MachO::x86_FLOAT_STATE_COUNT)
8155 outs() << " count x86_FLOAT_STATE_COUNT\n";
8156 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008157 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008158 struct MachO::x86_float_state_t fs;
8159 left = end - begin;
8160 if (left >= sizeof(MachO::x86_float_state_t)) {
8161 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8162 begin += sizeof(MachO::x86_float_state_t);
8163 } else {
8164 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8165 memcpy(&fs, begin, left);
8166 begin += left;
8167 }
8168 if (isLittleEndian != sys::IsLittleEndianHost)
8169 swapStruct(fs);
8170 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8171 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008172 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008173 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8174 else
8175 outs() << "fsh.count " << fs.fsh.count
8176 << " (not x86_FLOAT_STATE64_COUNT\n";
8177 Print_x86_float_state_t(fs.ufs.fs64);
8178 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008179 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8180 << fs.fsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008181 }
8182 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8183 outs() << " flavor x86_EXCEPTION_STATE\n";
8184 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8185 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8186 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008187 outs() << " count " << count
Kevin Enderby48ef5342014-12-23 22:56:39 +00008188 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8189 struct MachO::x86_exception_state_t es;
8190 left = end - begin;
8191 if (left >= sizeof(MachO::x86_exception_state_t)) {
8192 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8193 begin += sizeof(MachO::x86_exception_state_t);
8194 } else {
8195 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8196 memcpy(&es, begin, left);
8197 begin += left;
8198 }
8199 if (isLittleEndian != sys::IsLittleEndianHost)
8200 swapStruct(es);
8201 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8202 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008203 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008204 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8205 else
8206 outs() << "\t esh.count " << es.esh.count
8207 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8208 Print_x86_exception_state_t(es.ues.es64);
8209 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008210 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8211 << es.esh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008212 }
8213 } else {
8214 outs() << " flavor " << flavor << " (unknown)\n";
8215 outs() << " count " << count << "\n";
8216 outs() << " state (unknown)\n";
8217 begin += count * sizeof(uint32_t);
8218 }
8219 }
8220 } else {
8221 while (begin < end) {
8222 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8223 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8224 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008225 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008226 flavor = 0;
8227 begin = end;
8228 }
8229 if (isLittleEndian != sys::IsLittleEndianHost)
8230 sys::swapByteOrder(flavor);
8231 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8232 memcpy((char *)&count, begin, sizeof(uint32_t));
8233 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008234 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008235 count = 0;
8236 begin = end;
8237 }
8238 if (isLittleEndian != sys::IsLittleEndianHost)
8239 sys::swapByteOrder(count);
8240 outs() << " flavor " << flavor << "\n";
8241 outs() << " count " << count << "\n";
8242 outs() << " state (Unknown cputype/cpusubtype)\n";
8243 begin += count * sizeof(uint32_t);
8244 }
8245 }
8246}
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008247
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008248static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8249 if (dl.cmd == MachO::LC_ID_DYLIB)
8250 outs() << " cmd LC_ID_DYLIB\n";
8251 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8252 outs() << " cmd LC_LOAD_DYLIB\n";
8253 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8254 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8255 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8256 outs() << " cmd LC_REEXPORT_DYLIB\n";
8257 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8258 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8259 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8260 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8261 else
8262 outs() << " cmd " << dl.cmd << " (unknown)\n";
8263 outs() << " cmdsize " << dl.cmdsize;
8264 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8265 outs() << " Incorrect size\n";
8266 else
8267 outs() << "\n";
8268 if (dl.dylib.name < dl.cmdsize) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008269 const char *P = (const char *)(Ptr) + dl.dylib.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008270 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8271 } else {
8272 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8273 }
8274 outs() << " time stamp " << dl.dylib.timestamp << " ";
8275 time_t t = dl.dylib.timestamp;
8276 outs() << ctime(&t);
8277 outs() << " current version ";
8278 if (dl.dylib.current_version == 0xffffffff)
8279 outs() << "n/a\n";
8280 else
8281 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8282 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8283 << (dl.dylib.current_version & 0xff) << "\n";
8284 outs() << "compatibility version ";
8285 if (dl.dylib.compatibility_version == 0xffffffff)
8286 outs() << "n/a\n";
8287 else
8288 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8289 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8290 << (dl.dylib.compatibility_version & 0xff) << "\n";
8291}
8292
8293static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8294 uint32_t object_size) {
8295 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8296 outs() << " cmd LC_FUNCTION_STARTS\n";
8297 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8298 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8299 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8300 outs() << " cmd LC_FUNCTION_STARTS\n";
8301 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8302 outs() << " cmd LC_DATA_IN_CODE\n";
8303 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8304 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8305 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8306 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8307 else
8308 outs() << " cmd " << ld.cmd << " (?)\n";
8309 outs() << " cmdsize " << ld.cmdsize;
8310 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8311 outs() << " Incorrect size\n";
8312 else
8313 outs() << "\n";
8314 outs() << " dataoff " << ld.dataoff;
8315 if (ld.dataoff > object_size)
8316 outs() << " (past end of file)\n";
8317 else
8318 outs() << "\n";
8319 outs() << " datasize " << ld.datasize;
8320 uint64_t big_size = ld.dataoff;
8321 big_size += ld.datasize;
8322 if (big_size > object_size)
8323 outs() << " (past end of file)\n";
8324 else
8325 outs() << "\n";
8326}
8327
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008328static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8329 uint32_t cputype, bool verbose) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008330 StringRef Buf = Obj->getData();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008331 unsigned Index = 0;
8332 for (const auto &Command : Obj->load_commands()) {
8333 outs() << "Load command " << Index++ << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00008334 if (Command.C.cmd == MachO::LC_SEGMENT) {
8335 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8336 const char *sg_segname = SLC.segname;
8337 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8338 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8339 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8340 verbose);
8341 for (unsigned j = 0; j < SLC.nsects; j++) {
Kevin Enderbyc9713382014-12-16 01:14:45 +00008342 MachO::section S = Obj->getSection(Command, j);
Kevin Enderby956366c2014-08-29 22:30:52 +00008343 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8344 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8345 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8346 }
8347 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8348 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8349 const char *sg_segname = SLC_64.segname;
8350 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8351 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8352 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8353 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8354 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8355 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8356 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8357 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8358 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8359 sg_segname, filetype, Buf.size(), verbose);
8360 }
8361 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8362 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008363 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008364 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8365 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8366 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008367 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8368 Obj->is64Bit());
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008369 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8370 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8371 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8372 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8373 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8374 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8375 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8376 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8377 PrintDyldLoadCommand(Dyld, Command.Ptr);
8378 } else if (Command.C.cmd == MachO::LC_UUID) {
8379 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8380 PrintUuidLoadCommand(Uuid);
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008381 } else if (Command.C.cmd == MachO::LC_RPATH) {
8382 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8383 PrintRpathLoadCommand(Rpath, Command.Ptr);
Kevin Enderby1ff0ecc2014-12-16 21:48:27 +00008384 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
8385 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008386 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8387 PrintVersionMinLoadCommand(Vd);
8388 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8389 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8390 PrintSourceVersionCommand(Sd);
8391 } else if (Command.C.cmd == MachO::LC_MAIN) {
8392 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8393 PrintEntryPointCommand(Ep);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008394 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008395 MachO::encryption_info_command Ei =
8396 Obj->getEncryptionInfoCommand(Command);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008397 PrintEncryptionInfoCommand(Ei, Buf.size());
Kevin Enderby57538292014-12-17 01:01:30 +00008398 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008399 MachO::encryption_info_command_64 Ei =
8400 Obj->getEncryptionInfoCommand64(Command);
Kevin Enderby57538292014-12-17 01:01:30 +00008401 PrintEncryptionInfoCommand64(Ei, Buf.size());
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008402 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008403 MachO::linker_option_command Lo =
8404 Obj->getLinkerOptionLoadCommand(Command);
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008405 PrintLinkerOptionCommand(Lo, Command.Ptr);
Kevin Enderbyb4b79312014-12-18 19:24:35 +00008406 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8407 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8408 PrintSubFrameworkCommand(Sf, Command.Ptr);
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00008409 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8410 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8411 PrintSubUmbrellaCommand(Sf, Command.Ptr);
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008412 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8413 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8414 PrintSubLibraryCommand(Sl, Command.Ptr);
Kevin Enderby186eac32014-12-19 21:06:24 +00008415 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8416 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8417 PrintSubClientCommand(Sc, Command.Ptr);
Kevin Enderby52e4ce42014-12-19 22:25:22 +00008418 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8419 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8420 PrintRoutinesCommand(Rc);
8421 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8422 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8423 PrintRoutinesCommand64(Rc);
Kevin Enderby48ef5342014-12-23 22:56:39 +00008424 } else if (Command.C.cmd == MachO::LC_THREAD ||
8425 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8426 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8427 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
Nick Kledzik15558912014-10-16 18:58:20 +00008428 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8429 Command.C.cmd == MachO::LC_ID_DYLIB ||
8430 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8431 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8432 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8433 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008434 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8435 PrintDylibCommand(Dl, Command.Ptr);
8436 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8437 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8438 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8439 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8440 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8441 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8442 MachO::linkedit_data_command Ld =
8443 Obj->getLinkeditDataLoadCommand(Command);
8444 PrintLinkEditDataCommand(Ld, Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008445 } else {
8446 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8447 << ")\n";
8448 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8449 // TODO: get and print the raw bytes of the load command.
8450 }
8451 // TODO: print all the other kinds of load commands.
Kevin Enderby956366c2014-08-29 22:30:52 +00008452 }
8453}
8454
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008455static void getAndPrintMachHeader(const MachOObjectFile *Obj,
Kevin Enderby956366c2014-08-29 22:30:52 +00008456 uint32_t &filetype, uint32_t &cputype,
8457 bool verbose) {
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008458 if (Obj->is64Bit()) {
8459 MachO::mach_header_64 H_64;
8460 H_64 = Obj->getHeader64();
8461 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8462 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00008463 filetype = H_64.filetype;
8464 cputype = H_64.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008465 } else {
8466 MachO::mach_header H;
8467 H = Obj->getHeader();
8468 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8469 H.sizeofcmds, H.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00008470 filetype = H.filetype;
8471 cputype = H.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008472 }
8473}
8474
8475void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8476 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby956366c2014-08-29 22:30:52 +00008477 uint32_t filetype = 0;
8478 uint32_t cputype = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008479 getAndPrintMachHeader(file, filetype, cputype, !NonVerbose);
8480 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008481}
Nick Kledzikd04bc352014-08-30 00:20:14 +00008482
8483//===----------------------------------------------------------------------===//
8484// export trie dumping
8485//===----------------------------------------------------------------------===//
8486
8487void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008488 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
8489 uint64_t Flags = Entry.flags();
Nick Kledzikd04bc352014-08-30 00:20:14 +00008490 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
8491 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
8492 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8493 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
8494 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8495 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
8496 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
8497 if (ReExport)
8498 outs() << "[re-export] ";
8499 else
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008500 outs() << format("0x%08llX ",
8501 Entry.address()); // FIXME:add in base address
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008502 outs() << Entry.name();
Nick Kledzikd04bc352014-08-30 00:20:14 +00008503 if (WeakDef || ThreadLocal || Resolver || Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008504 bool NeedsComma = false;
Nick Kledzik1d1ac4b2014-09-03 01:12:52 +00008505 outs() << " [";
Nick Kledzikd04bc352014-08-30 00:20:14 +00008506 if (WeakDef) {
8507 outs() << "weak_def";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008508 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008509 }
8510 if (ThreadLocal) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008511 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008512 outs() << ", ";
8513 outs() << "per-thread";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008514 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008515 }
8516 if (Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008517 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008518 outs() << ", ";
8519 outs() << "absolute";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008520 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008521 }
8522 if (Resolver) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008523 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008524 outs() << ", ";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008525 outs() << format("resolver=0x%08llX", Entry.other());
8526 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008527 }
8528 outs() << "]";
8529 }
8530 if (ReExport) {
8531 StringRef DylibName = "unknown";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008532 int Ordinal = Entry.other() - 1;
8533 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
8534 if (Entry.otherName().empty())
Nick Kledzikd04bc352014-08-30 00:20:14 +00008535 outs() << " (from " << DylibName << ")";
8536 else
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008537 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
Nick Kledzikd04bc352014-08-30 00:20:14 +00008538 }
8539 outs() << "\n";
8540 }
8541}
Nick Kledzikac431442014-09-12 21:34:15 +00008542
Nick Kledzikac431442014-09-12 21:34:15 +00008543//===----------------------------------------------------------------------===//
8544// rebase table dumping
8545//===----------------------------------------------------------------------===//
8546
8547namespace {
8548class SegInfo {
8549public:
8550 SegInfo(const object::MachOObjectFile *Obj);
8551
8552 StringRef segmentName(uint32_t SegIndex);
8553 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
8554 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
8555
8556private:
8557 struct SectionInfo {
8558 uint64_t Address;
8559 uint64_t Size;
8560 StringRef SectionName;
8561 StringRef SegmentName;
8562 uint64_t OffsetInSegment;
8563 uint64_t SegmentStartAddress;
8564 uint32_t SegmentIndex;
8565 };
8566 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
8567 SmallVector<SectionInfo, 32> Sections;
8568};
8569}
8570
8571SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
8572 // Build table of sections so segIndex/offset pairs can be translated.
Nick Kledzik56ebef42014-09-16 01:41:51 +00008573 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
Nick Kledzikac431442014-09-12 21:34:15 +00008574 StringRef CurSegName;
8575 uint64_t CurSegAddress;
8576 for (const SectionRef &Section : Obj->sections()) {
8577 SectionInfo Info;
8578 if (error(Section.getName(Info.SectionName)))
8579 return;
Rafael Espindola80291272014-10-08 15:28:58 +00008580 Info.Address = Section.getAddress();
8581 Info.Size = Section.getSize();
Nick Kledzikac431442014-09-12 21:34:15 +00008582 Info.SegmentName =
8583 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
8584 if (!Info.SegmentName.equals(CurSegName)) {
8585 ++CurSegIndex;
8586 CurSegName = Info.SegmentName;
8587 CurSegAddress = Info.Address;
8588 }
8589 Info.SegmentIndex = CurSegIndex - 1;
8590 Info.OffsetInSegment = Info.Address - CurSegAddress;
8591 Info.SegmentStartAddress = CurSegAddress;
8592 Sections.push_back(Info);
8593 }
8594}
8595
8596StringRef SegInfo::segmentName(uint32_t SegIndex) {
8597 for (const SectionInfo &SI : Sections) {
8598 if (SI.SegmentIndex == SegIndex)
8599 return SI.SegmentName;
8600 }
8601 llvm_unreachable("invalid segIndex");
8602}
8603
8604const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
8605 uint64_t OffsetInSeg) {
8606 for (const SectionInfo &SI : Sections) {
8607 if (SI.SegmentIndex != SegIndex)
8608 continue;
8609 if (SI.OffsetInSegment > OffsetInSeg)
8610 continue;
8611 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8612 continue;
8613 return SI;
8614 }
8615 llvm_unreachable("segIndex and offset not in any section");
8616}
8617
8618StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
8619 return findSection(SegIndex, OffsetInSeg).SectionName;
8620}
8621
8622uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
8623 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
8624 return SI.SegmentStartAddress + OffsetInSeg;
8625}
8626
8627void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
8628 // Build table of sections so names can used in final output.
8629 SegInfo sectionTable(Obj);
8630
8631 outs() << "segment section address type\n";
8632 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
8633 uint32_t SegIndex = Entry.segmentIndex();
8634 uint64_t OffsetInSeg = Entry.segmentOffset();
8635 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8636 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8637 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8638
8639 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008640 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
8641 SegmentName.str().c_str(), SectionName.str().c_str(),
8642 Address, Entry.typeName().str().c_str());
Nick Kledzikac431442014-09-12 21:34:15 +00008643 }
8644}
Nick Kledzik56ebef42014-09-16 01:41:51 +00008645
8646static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
8647 StringRef DylibName;
8648 switch (Ordinal) {
8649 case MachO::BIND_SPECIAL_DYLIB_SELF:
8650 return "this-image";
8651 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
8652 return "main-executable";
8653 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
8654 return "flat-namespace";
8655 default:
Nick Kledzikabd29872014-09-16 22:03:13 +00008656 if (Ordinal > 0) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008657 std::error_code EC =
8658 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
Nick Kledzikabd29872014-09-16 22:03:13 +00008659 if (EC)
Nick Kledzik51d2c2b2014-10-14 23:29:38 +00008660 return "<<bad library ordinal>>";
Nick Kledzikabd29872014-09-16 22:03:13 +00008661 return DylibName;
8662 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00008663 }
Nick Kledzikabd29872014-09-16 22:03:13 +00008664 return "<<unknown special ordinal>>";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008665}
8666
8667//===----------------------------------------------------------------------===//
8668// bind table dumping
8669//===----------------------------------------------------------------------===//
8670
8671void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
8672 // Build table of sections so names can used in final output.
8673 SegInfo sectionTable(Obj);
8674
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008675 outs() << "segment section address type "
8676 "addend dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008677 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
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 __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008686 StringRef Attr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00008687 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008688 Attr = " (weak_import)";
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008689 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008690 << left_justify(SectionName, 18) << " "
8691 << format_hex(Address, 10, true) << " "
8692 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008693 << format_decimal(Entry.addend(), 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008694 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008695 << Entry.symbolName() << Attr << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008696 }
8697}
8698
8699//===----------------------------------------------------------------------===//
8700// lazy bind table dumping
8701//===----------------------------------------------------------------------===//
8702
8703void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
8704 // Build table of sections so names can used in final output.
8705 SegInfo sectionTable(Obj);
8706
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008707 outs() << "segment section address "
8708 "dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008709 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
8710 uint32_t SegIndex = Entry.segmentIndex();
8711 uint64_t OffsetInSeg = Entry.segmentOffset();
8712 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8713 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8714 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8715
8716 // Table lines look like:
8717 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008718 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008719 << left_justify(SectionName, 18) << " "
8720 << format_hex(Address, 10, true) << " "
8721 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Nick Kledzik56ebef42014-09-16 01:41:51 +00008722 << Entry.symbolName() << "\n";
8723 }
8724}
8725
Nick Kledzik56ebef42014-09-16 01:41:51 +00008726//===----------------------------------------------------------------------===//
8727// weak bind table dumping
8728//===----------------------------------------------------------------------===//
8729
8730void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
8731 // Build table of sections so names can used in final output.
8732 SegInfo sectionTable(Obj);
8733
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008734 outs() << "segment section address "
8735 "type addend symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008736 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
8737 // Strong symbols don't have a location to update.
8738 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008739 outs() << " strong "
Nick Kledzik56ebef42014-09-16 01:41:51 +00008740 << Entry.symbolName() << "\n";
8741 continue;
8742 }
8743 uint32_t SegIndex = Entry.segmentIndex();
8744 uint64_t OffsetInSeg = Entry.segmentOffset();
8745 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8746 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8747 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8748
8749 // Table lines look like:
8750 // __DATA __data 0x00001000 pointer 0 _foo
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008751 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008752 << left_justify(SectionName, 18) << " "
8753 << format_hex(Address, 10, true) << " "
8754 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008755 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
8756 << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008757 }
8758}
8759
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008760// get_dyld_bind_info_symbolname() is used for disassembly and passed an
8761// address, ReferenceValue, in the Mach-O file and looks in the dyld bind
8762// information for that address. If the address is found its binding symbol
8763// name is returned. If not nullptr is returned.
8764static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
8765 struct DisassembleInfo *info) {
Kevin Enderby078be602014-10-23 19:53:12 +00008766 if (info->bindtable == nullptr) {
8767 info->bindtable = new (BindTable);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008768 SegInfo sectionTable(info->O);
8769 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
8770 uint32_t SegIndex = Entry.segmentIndex();
8771 uint64_t OffsetInSeg = Entry.segmentOffset();
8772 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8773 const char *SymbolName = nullptr;
8774 StringRef name = Entry.symbolName();
8775 if (!name.empty())
8776 SymbolName = name.data();
Kevin Enderby078be602014-10-23 19:53:12 +00008777 info->bindtable->push_back(std::make_pair(Address, SymbolName));
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008778 }
8779 }
Kevin Enderby078be602014-10-23 19:53:12 +00008780 for (bind_table_iterator BI = info->bindtable->begin(),
8781 BE = info->bindtable->end();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008782 BI != BE; ++BI) {
8783 uint64_t Address = BI->first;
8784 if (ReferenceValue == Address) {
8785 const char *SymbolName = BI->second;
8786 return SymbolName;
8787 }
8788 }
8789 return nullptr;
8790}