blob: 249feee0bb743fb6942b7eaa261ba8c356b6ddfa [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) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000181 SymbolRef::Type AType = A.getType();
182 SymbolRef::Type BType = B.getType();
Owen Andersond9243c42011-10-17 21:37:35 +0000183
184 uint64_t AAddr, BAddr;
185 if (AType != SymbolRef::ST_Function)
186 AAddr = 0;
187 else
188 A.getAddress(AAddr);
189 if (BType != SymbolRef::ST_Function)
190 BAddr = 0;
191 else
192 B.getAddress(BAddr);
193 return AAddr < BAddr;
194 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000195};
196
Kevin Enderby273ae012013-06-06 17:20:50 +0000197// Types for the storted data in code table that is built before disassembly
198// and the predicate function to sort them.
199typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
200typedef std::vector<DiceTableEntry> DiceTable;
201typedef DiceTable::iterator dice_table_iterator;
202
Kevin Enderby930fdc72014-11-06 19:00:13 +0000203// This is used to search for a data in code table entry for the PC being
204// disassembled. The j parameter has the PC in j.first. A single data in code
205// table entry can cover many bytes for each of its Kind's. So if the offset,
206// aka the i.first value, of the data in code table entry plus its Length
207// covers the PC being searched for this will return true. If not it will
208// return false.
David Majnemerea9b8ee2014-11-04 08:41:48 +0000209static bool compareDiceTableEntries(const DiceTableEntry &i,
210 const DiceTableEntry &j) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000211 uint16_t Length;
212 i.second.getLength(Length);
213
214 return j.first >= i.first && j.first < i.first + Length;
Kevin Enderby273ae012013-06-06 17:20:50 +0000215}
216
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000217static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
Kevin Enderby930fdc72014-11-06 19:00:13 +0000218 unsigned short Kind) {
219 uint32_t Value, Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000220
221 switch (Kind) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000222 default:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000223 case MachO::DICE_KIND_DATA:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000224 if (Length >= 4) {
225 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000226 dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000227 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000228 outs() << "\t.long " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000229 Size = 4;
230 } else if (Length >= 2) {
231 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000232 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000233 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000234 outs() << "\t.short " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000235 Size = 2;
236 } else {
237 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000238 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000239 Value = bytes[0];
240 outs() << "\t.byte " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000241 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000242 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000243 if (Kind == MachO::DICE_KIND_DATA)
244 outs() << "\t@ KIND_DATA\n";
245 else
246 outs() << "\t@ data in code kind = " << Kind << "\n";
Kevin Enderby273ae012013-06-06 17:20:50 +0000247 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000248 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000249 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000250 dumpBytes(ArrayRef<uint8_t>(bytes, 1), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000251 Value = bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000252 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
253 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000254 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000255 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000256 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000257 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000258 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000259 outs() << "\t.short " << format("%5u", Value & 0xffff)
260 << "\t@ KIND_JUMP_TABLE16\n";
261 Size = 2;
Kevin Enderby273ae012013-06-06 17:20:50 +0000262 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000263 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000264 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
265 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000266 dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000267 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000268 outs() << "\t.long " << Value;
269 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
270 outs() << "\t@ KIND_JUMP_TABLE32\n";
271 else
272 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
273 Size = 4;
Kevin Enderby273ae012013-06-06 17:20:50 +0000274 break;
275 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000276 return Size;
Kevin Enderby273ae012013-06-06 17:20:50 +0000277}
278
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000279static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000280 std::vector<SectionRef> &Sections,
281 std::vector<SymbolRef> &Symbols,
282 SmallVectorImpl<uint64_t> &FoundFns,
283 uint64_t &BaseSegmentAddress) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000284 for (const SymbolRef &Symbol : MachOObj->symbols()) {
285 StringRef SymName;
286 Symbol.getName(SymName);
287 if (!SymName.startswith("ltmp"))
288 Symbols.push_back(Symbol);
289 }
Owen Andersond9243c42011-10-17 21:37:35 +0000290
Alexey Samsonov48803e52014-03-13 14:37:36 +0000291 for (const SectionRef &Section : MachOObj->sections()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000292 StringRef SectName;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000293 Section.getName(SectName);
294 Sections.push_back(Section);
Owen Andersond9243c42011-10-17 21:37:35 +0000295 }
296
Kevin Enderby273ae012013-06-06 17:20:50 +0000297 bool BaseSegmentAddressSet = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000298 for (const auto &Command : MachOObj->load_commands()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000299 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000300 // We found a function starts segment, parse the addresses for later
301 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000302 MachO::linkedit_data_command LLC =
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000303 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000304
Charles Davis8bdfafd2013-09-01 04:28:48 +0000305 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000306 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
307 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000308 StringRef SegName = SLC.segname;
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000309 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
Kevin Enderby273ae012013-06-06 17:20:50 +0000310 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000311 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000312 }
313 }
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000314 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000315}
316
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000317static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
318 uint32_t n, uint32_t count,
319 uint32_t stride, uint64_t addr) {
320 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
321 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
322 if (n > nindirectsyms)
323 outs() << " (entries start past the end of the indirect symbol "
324 "table) (reserved1 field greater than the table size)";
325 else if (n + count > nindirectsyms)
326 outs() << " (entries extends past the end of the indirect symbol "
327 "table)";
328 outs() << "\n";
329 uint32_t cputype = O->getHeader().cputype;
330 if (cputype & MachO::CPU_ARCH_ABI64)
331 outs() << "address index";
332 else
333 outs() << "address index";
334 if (verbose)
335 outs() << " name\n";
336 else
337 outs() << "\n";
338 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
339 if (cputype & MachO::CPU_ARCH_ABI64)
340 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
341 else
342 outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
343 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
344 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
345 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
346 outs() << "LOCAL\n";
347 continue;
348 }
349 if (indirect_symbol ==
350 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
351 outs() << "LOCAL ABSOLUTE\n";
352 continue;
353 }
354 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
355 outs() << "ABSOLUTE\n";
356 continue;
357 }
358 outs() << format("%5u ", indirect_symbol);
Kevin Enderbyf0640752015-03-13 17:56:32 +0000359 if (verbose) {
360 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
361 if (indirect_symbol < Symtab.nsyms) {
362 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
363 SymbolRef Symbol = *Sym;
364 StringRef SymName;
365 Symbol.getName(SymName);
366 outs() << SymName;
367 } else {
368 outs() << "?";
369 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000370 }
371 outs() << "\n";
372 }
373}
374
375static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000376 for (const auto &Load : O->load_commands()) {
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000377 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
378 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
379 for (unsigned J = 0; J < Seg.nsects; ++J) {
380 MachO::section_64 Sec = O->getSection64(Load, J);
381 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
382 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
383 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
384 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
385 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
386 section_type == MachO::S_SYMBOL_STUBS) {
387 uint32_t stride;
388 if (section_type == MachO::S_SYMBOL_STUBS)
389 stride = Sec.reserved2;
390 else
391 stride = 8;
392 if (stride == 0) {
393 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
394 << Sec.sectname << ") "
395 << "(size of stubs in reserved2 field is zero)\n";
396 continue;
397 }
398 uint32_t count = Sec.size / stride;
399 outs() << "Indirect symbols for (" << Sec.segname << ","
400 << Sec.sectname << ") " << count << " entries";
401 uint32_t n = Sec.reserved1;
402 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
403 }
404 }
405 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
406 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
407 for (unsigned J = 0; J < Seg.nsects; ++J) {
408 MachO::section Sec = O->getSection(Load, J);
409 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
410 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
411 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
412 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
413 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
414 section_type == MachO::S_SYMBOL_STUBS) {
415 uint32_t stride;
416 if (section_type == MachO::S_SYMBOL_STUBS)
417 stride = Sec.reserved2;
418 else
419 stride = 4;
420 if (stride == 0) {
421 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
422 << Sec.sectname << ") "
423 << "(size of stubs in reserved2 field is zero)\n";
424 continue;
425 }
426 uint32_t count = Sec.size / stride;
427 outs() << "Indirect symbols for (" << Sec.segname << ","
428 << Sec.sectname << ") " << count << " entries";
429 uint32_t n = Sec.reserved1;
430 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
431 }
432 }
433 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000434 }
435}
436
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000437static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
438 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
439 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
440 outs() << "Data in code table (" << nentries << " entries)\n";
441 outs() << "offset length kind\n";
442 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
443 ++DI) {
444 uint32_t Offset;
445 DI->getOffset(Offset);
446 outs() << format("0x%08" PRIx32, Offset) << " ";
447 uint16_t Length;
448 DI->getLength(Length);
449 outs() << format("%6u", Length) << " ";
450 uint16_t Kind;
451 DI->getKind(Kind);
452 if (verbose) {
453 switch (Kind) {
454 case MachO::DICE_KIND_DATA:
455 outs() << "DATA";
456 break;
457 case MachO::DICE_KIND_JUMP_TABLE8:
458 outs() << "JUMP_TABLE8";
459 break;
460 case MachO::DICE_KIND_JUMP_TABLE16:
461 outs() << "JUMP_TABLE16";
462 break;
463 case MachO::DICE_KIND_JUMP_TABLE32:
464 outs() << "JUMP_TABLE32";
465 break;
466 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
467 outs() << "ABS_JUMP_TABLE32";
468 break;
469 default:
470 outs() << format("0x%04" PRIx32, Kind);
471 break;
472 }
473 } else
474 outs() << format("0x%04" PRIx32, Kind);
475 outs() << "\n";
476 }
477}
478
Kevin Enderby9a509442015-01-27 21:28:24 +0000479static void PrintLinkOptHints(MachOObjectFile *O) {
480 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
481 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
482 uint32_t nloh = LohLC.datasize;
483 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
484 for (uint32_t i = 0; i < nloh;) {
485 unsigned n;
486 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
487 i += n;
488 outs() << " identifier " << identifier << " ";
489 if (i >= nloh)
490 return;
491 switch (identifier) {
492 case 1:
493 outs() << "AdrpAdrp\n";
494 break;
495 case 2:
496 outs() << "AdrpLdr\n";
497 break;
498 case 3:
499 outs() << "AdrpAddLdr\n";
500 break;
501 case 4:
502 outs() << "AdrpLdrGotLdr\n";
503 break;
504 case 5:
505 outs() << "AdrpAddStr\n";
506 break;
507 case 6:
508 outs() << "AdrpLdrGotStr\n";
509 break;
510 case 7:
511 outs() << "AdrpAdd\n";
512 break;
513 case 8:
514 outs() << "AdrpLdrGot\n";
515 break;
516 default:
517 outs() << "Unknown identifier value\n";
518 break;
519 }
520 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
521 i += n;
522 outs() << " narguments " << narguments << "\n";
523 if (i >= nloh)
524 return;
525
526 for (uint32_t j = 0; j < narguments; j++) {
527 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
528 i += n;
529 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
530 if (i >= nloh)
531 return;
532 }
533 }
534}
535
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000536static void PrintDylibs(MachOObjectFile *O, bool JustId) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000537 unsigned Index = 0;
538 for (const auto &Load : O->load_commands()) {
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000539 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
540 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
541 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
542 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
543 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
544 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
545 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
546 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
547 if (dl.dylib.name < dl.cmdsize) {
548 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
549 if (JustId)
550 outs() << p << "\n";
551 else {
552 outs() << "\t" << p;
553 outs() << " (compatibility version "
554 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
555 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
556 << (dl.dylib.compatibility_version & 0xff) << ",";
557 outs() << " current version "
558 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
559 << ((dl.dylib.current_version >> 8) & 0xff) << "."
560 << (dl.dylib.current_version & 0xff) << ")\n";
561 }
562 } else {
563 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
564 if (Load.C.cmd == MachO::LC_ID_DYLIB)
565 outs() << "LC_ID_DYLIB ";
566 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
567 outs() << "LC_LOAD_DYLIB ";
568 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
569 outs() << "LC_LOAD_WEAK_DYLIB ";
570 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
571 outs() << "LC_LAZY_LOAD_DYLIB ";
572 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
573 outs() << "LC_REEXPORT_DYLIB ";
574 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
575 outs() << "LC_LOAD_UPWARD_DYLIB ";
576 else
577 outs() << "LC_??? ";
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000578 outs() << "command " << Index++ << "\n";
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000579 }
580 }
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000581 }
582}
583
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000584typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
585
586static void CreateSymbolAddressMap(MachOObjectFile *O,
587 SymbolAddressMap *AddrMap) {
588 // Create a map of symbol addresses to symbol names.
589 for (const SymbolRef &Symbol : O->symbols()) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000590 SymbolRef::Type ST = Symbol.getType();
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000591 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
592 ST == SymbolRef::ST_Other) {
593 uint64_t Address;
594 Symbol.getAddress(Address);
595 StringRef SymName;
596 Symbol.getName(SymName);
Kevin Enderby846c0002015-04-16 17:19:59 +0000597 if (!SymName.startswith(".objc"))
598 (*AddrMap)[Address] = SymName;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000599 }
600 }
601}
602
603// GuessSymbolName is passed the address of what might be a symbol and a
604// pointer to the SymbolAddressMap. It returns the name of a symbol
605// with that address or nullptr if no symbol is found with that address.
606static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
607 const char *SymbolName = nullptr;
608 // A DenseMap can't lookup up some values.
609 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
610 StringRef name = AddrMap->lookup(value);
611 if (!name.empty())
612 SymbolName = name.data();
613 }
614 return SymbolName;
615}
616
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000617static void DumpCstringChar(const char c) {
618 char p[2];
619 p[0] = c;
620 p[1] = '\0';
621 outs().write_escaped(p);
622}
623
Kevin Enderby10ba0412015-02-04 21:38:42 +0000624static void DumpCstringSection(MachOObjectFile *O, const char *sect,
625 uint32_t sect_size, uint64_t sect_addr,
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000626 bool print_addresses) {
Kevin Enderby10ba0412015-02-04 21:38:42 +0000627 for (uint32_t i = 0; i < sect_size; i++) {
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000628 if (print_addresses) {
629 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000630 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000631 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000632 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000633 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000634 for (; i < sect_size && sect[i] != '\0'; i++)
635 DumpCstringChar(sect[i]);
Kevin Enderby10ba0412015-02-04 21:38:42 +0000636 if (i < sect_size && sect[i] == '\0')
637 outs() << "\n";
638 }
639}
640
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000641static void DumpLiteral4(uint32_t l, float f) {
642 outs() << format("0x%08" PRIx32, l);
643 if ((l & 0x7f800000) != 0x7f800000)
644 outs() << format(" (%.16e)\n", f);
645 else {
646 if (l == 0x7f800000)
647 outs() << " (+Infinity)\n";
648 else if (l == 0xff800000)
649 outs() << " (-Infinity)\n";
650 else if ((l & 0x00400000) == 0x00400000)
651 outs() << " (non-signaling Not-a-Number)\n";
652 else
653 outs() << " (signaling Not-a-Number)\n";
654 }
655}
656
657static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
658 uint32_t sect_size, uint64_t sect_addr,
659 bool print_addresses) {
660 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
661 if (print_addresses) {
662 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000663 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000664 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000665 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000666 }
667 float f;
668 memcpy(&f, sect + i, sizeof(float));
669 if (O->isLittleEndian() != sys::IsLittleEndianHost)
670 sys::swapByteOrder(f);
671 uint32_t l;
672 memcpy(&l, sect + i, sizeof(uint32_t));
673 if (O->isLittleEndian() != sys::IsLittleEndianHost)
674 sys::swapByteOrder(l);
675 DumpLiteral4(l, f);
676 }
677}
678
679static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
680 double d) {
681 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
682 uint32_t Hi, Lo;
683 if (O->isLittleEndian()) {
684 Hi = l1;
685 Lo = l0;
686 } else {
687 Hi = l0;
688 Lo = l1;
689 }
690 // Hi is the high word, so this is equivalent to if(isfinite(d))
691 if ((Hi & 0x7ff00000) != 0x7ff00000)
692 outs() << format(" (%.16e)\n", d);
693 else {
694 if (Hi == 0x7ff00000 && Lo == 0)
695 outs() << " (+Infinity)\n";
696 else if (Hi == 0xfff00000 && Lo == 0)
697 outs() << " (-Infinity)\n";
698 else if ((Hi & 0x00080000) == 0x00080000)
699 outs() << " (non-signaling Not-a-Number)\n";
700 else
701 outs() << " (signaling Not-a-Number)\n";
702 }
703}
704
705static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
706 uint32_t sect_size, uint64_t sect_addr,
707 bool print_addresses) {
708 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
709 if (print_addresses) {
710 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000711 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000712 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000713 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000714 }
715 double d;
716 memcpy(&d, sect + i, sizeof(double));
717 if (O->isLittleEndian() != sys::IsLittleEndianHost)
718 sys::swapByteOrder(d);
719 uint32_t l0, l1;
720 memcpy(&l0, sect + i, sizeof(uint32_t));
721 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
722 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
723 sys::swapByteOrder(l0);
724 sys::swapByteOrder(l1);
725 }
726 DumpLiteral8(O, l0, l1, d);
727 }
728}
729
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000730static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
731 outs() << format("0x%08" PRIx32, l0) << " ";
732 outs() << format("0x%08" PRIx32, l1) << " ";
733 outs() << format("0x%08" PRIx32, l2) << " ";
734 outs() << format("0x%08" PRIx32, l3) << "\n";
735}
736
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000737static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
738 uint32_t sect_size, uint64_t sect_addr,
739 bool print_addresses) {
740 for (uint32_t i = 0; i < sect_size; i += 16) {
741 if (print_addresses) {
742 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000743 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000744 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000745 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000746 }
747 uint32_t l0, l1, l2, l3;
748 memcpy(&l0, sect + i, sizeof(uint32_t));
749 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
750 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
751 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
752 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
753 sys::swapByteOrder(l0);
754 sys::swapByteOrder(l1);
755 sys::swapByteOrder(l2);
756 sys::swapByteOrder(l3);
757 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000758 DumpLiteral16(l0, l1, l2, l3);
759 }
760}
761
762static void DumpLiteralPointerSection(MachOObjectFile *O,
763 const SectionRef &Section,
764 const char *sect, uint32_t sect_size,
765 uint64_t sect_addr,
766 bool print_addresses) {
767 // Collect the literal sections in this Mach-O file.
768 std::vector<SectionRef> LiteralSections;
769 for (const SectionRef &Section : O->sections()) {
770 DataRefImpl Ref = Section.getRawDataRefImpl();
771 uint32_t section_type;
772 if (O->is64Bit()) {
773 const MachO::section_64 Sec = O->getSection64(Ref);
774 section_type = Sec.flags & MachO::SECTION_TYPE;
775 } else {
776 const MachO::section Sec = O->getSection(Ref);
777 section_type = Sec.flags & MachO::SECTION_TYPE;
778 }
779 if (section_type == MachO::S_CSTRING_LITERALS ||
780 section_type == MachO::S_4BYTE_LITERALS ||
781 section_type == MachO::S_8BYTE_LITERALS ||
782 section_type == MachO::S_16BYTE_LITERALS)
783 LiteralSections.push_back(Section);
784 }
785
786 // Set the size of the literal pointer.
787 uint32_t lp_size = O->is64Bit() ? 8 : 4;
788
Eric Christopher572e03a2015-06-19 01:53:21 +0000789 // Collect the external relocation symbols for the literal pointers.
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000790 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
791 for (const RelocationRef &Reloc : Section.relocations()) {
792 DataRefImpl Rel;
793 MachO::any_relocation_info RE;
794 bool isExtern = false;
795 Rel = Reloc.getRawDataRefImpl();
796 RE = O->getRelocation(Rel);
797 isExtern = O->getPlainRelocationExternal(RE);
798 if (isExtern) {
Rafael Espindola96d071c2015-06-29 23:29:12 +0000799 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000800 symbol_iterator RelocSym = Reloc.getSymbol();
801 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
802 }
803 }
804 array_pod_sort(Relocs.begin(), Relocs.end());
805
806 // Dump each literal pointer.
807 for (uint32_t i = 0; i < sect_size; i += lp_size) {
808 if (print_addresses) {
809 if (O->is64Bit())
810 outs() << format("%016" PRIx64, sect_addr + i) << " ";
811 else
812 outs() << format("%08" PRIx64, sect_addr + i) << " ";
813 }
814 uint64_t lp;
815 if (O->is64Bit()) {
816 memcpy(&lp, sect + i, sizeof(uint64_t));
817 if (O->isLittleEndian() != sys::IsLittleEndianHost)
818 sys::swapByteOrder(lp);
819 } else {
820 uint32_t li;
821 memcpy(&li, sect + i, sizeof(uint32_t));
822 if (O->isLittleEndian() != sys::IsLittleEndianHost)
823 sys::swapByteOrder(li);
824 lp = li;
825 }
826
827 // First look for an external relocation entry for this literal pointer.
David Blaikie33dd45d02015-03-23 18:39:02 +0000828 auto Reloc = std::find_if(
829 Relocs.begin(), Relocs.end(),
830 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
831 if (Reloc != Relocs.end()) {
832 symbol_iterator RelocSym = Reloc->second;
833 StringRef SymName;
834 RelocSym->getName(SymName);
835 outs() << "external relocation entry for symbol:" << SymName << "\n";
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000836 continue;
David Blaikie33dd45d02015-03-23 18:39:02 +0000837 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000838
839 // For local references see what the section the literal pointer points to.
David Blaikie33dd45d02015-03-23 18:39:02 +0000840 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
841 [&](const SectionRef &R) {
842 return lp >= R.getAddress() &&
843 lp < R.getAddress() + R.getSize();
844 });
845 if (Sect == LiteralSections.end()) {
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000846 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
David Blaikie33dd45d02015-03-23 18:39:02 +0000847 continue;
848 }
849
850 uint64_t SectAddress = Sect->getAddress();
851 uint64_t SectSize = Sect->getSize();
852
853 StringRef SectName;
854 Sect->getName(SectName);
855 DataRefImpl Ref = Sect->getRawDataRefImpl();
856 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
857 outs() << SegmentName << ":" << SectName << ":";
858
859 uint32_t section_type;
860 if (O->is64Bit()) {
861 const MachO::section_64 Sec = O->getSection64(Ref);
862 section_type = Sec.flags & MachO::SECTION_TYPE;
863 } else {
864 const MachO::section Sec = O->getSection(Ref);
865 section_type = Sec.flags & MachO::SECTION_TYPE;
866 }
867
868 StringRef BytesStr;
869 Sect->getContents(BytesStr);
870 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
871
872 switch (section_type) {
873 case MachO::S_CSTRING_LITERALS:
874 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
875 i++) {
876 DumpCstringChar(Contents[i]);
877 }
878 outs() << "\n";
879 break;
880 case MachO::S_4BYTE_LITERALS:
881 float f;
882 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
883 uint32_t l;
884 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
885 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
886 sys::swapByteOrder(f);
887 sys::swapByteOrder(l);
888 }
889 DumpLiteral4(l, f);
890 break;
891 case MachO::S_8BYTE_LITERALS: {
892 double d;
893 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
894 uint32_t l0, l1;
895 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
896 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
897 sizeof(uint32_t));
898 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
899 sys::swapByteOrder(f);
900 sys::swapByteOrder(l0);
901 sys::swapByteOrder(l1);
902 }
903 DumpLiteral8(O, l0, l1, d);
904 break;
905 }
906 case MachO::S_16BYTE_LITERALS: {
907 uint32_t l0, l1, l2, l3;
908 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
909 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
910 sizeof(uint32_t));
911 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
912 sizeof(uint32_t));
913 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
914 sizeof(uint32_t));
915 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
916 sys::swapByteOrder(l0);
917 sys::swapByteOrder(l1);
918 sys::swapByteOrder(l2);
919 sys::swapByteOrder(l3);
920 }
921 DumpLiteral16(l0, l1, l2, l3);
922 break;
923 }
924 }
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000925 }
926}
927
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000928static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
929 uint32_t sect_size, uint64_t sect_addr,
930 SymbolAddressMap *AddrMap,
931 bool verbose) {
932 uint32_t stride;
933 if (O->is64Bit())
934 stride = sizeof(uint64_t);
935 else
936 stride = sizeof(uint32_t);
937 for (uint32_t i = 0; i < sect_size; i += stride) {
938 const char *SymbolName = nullptr;
939 if (O->is64Bit()) {
940 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
941 uint64_t pointer_value;
942 memcpy(&pointer_value, sect + i, stride);
943 if (O->isLittleEndian() != sys::IsLittleEndianHost)
944 sys::swapByteOrder(pointer_value);
945 outs() << format("0x%016" PRIx64, pointer_value);
946 if (verbose)
947 SymbolName = GuessSymbolName(pointer_value, AddrMap);
948 } else {
949 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
950 uint32_t pointer_value;
951 memcpy(&pointer_value, sect + i, stride);
952 if (O->isLittleEndian() != sys::IsLittleEndianHost)
953 sys::swapByteOrder(pointer_value);
954 outs() << format("0x%08" PRIx32, pointer_value);
955 if (verbose)
956 SymbolName = GuessSymbolName(pointer_value, AddrMap);
957 }
958 if (SymbolName)
959 outs() << " " << SymbolName;
960 outs() << "\n";
961 }
962}
963
964static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
965 uint32_t size, uint64_t addr) {
966 uint32_t cputype = O->getHeader().cputype;
967 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
968 uint32_t j;
969 for (uint32_t i = 0; i < size; i += j, addr += j) {
970 if (O->is64Bit())
971 outs() << format("%016" PRIx64, addr) << "\t";
972 else
Kevin Enderbyf0640752015-03-13 17:56:32 +0000973 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000974 for (j = 0; j < 16 && i + j < size; j++) {
975 uint8_t byte_word = *(sect + i + j);
976 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
977 }
978 outs() << "\n";
979 }
980 } else {
981 uint32_t j;
982 for (uint32_t i = 0; i < size; i += j, addr += j) {
983 if (O->is64Bit())
984 outs() << format("%016" PRIx64, addr) << "\t";
985 else
986 outs() << format("%08" PRIx64, sect) << "\t";
987 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
988 j += sizeof(int32_t)) {
989 if (i + j + sizeof(int32_t) < size) {
990 uint32_t long_word;
991 memcpy(&long_word, sect + i + j, sizeof(int32_t));
992 if (O->isLittleEndian() != sys::IsLittleEndianHost)
993 sys::swapByteOrder(long_word);
994 outs() << format("%08" PRIx32, long_word) << " ";
995 } else {
996 for (uint32_t k = 0; i + j + k < size; k++) {
997 uint8_t byte_word = *(sect + i + j);
998 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
999 }
1000 }
1001 }
1002 outs() << "\n";
1003 }
1004 }
1005}
1006
Kevin Enderby95df54c2015-02-04 01:01:38 +00001007static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1008 StringRef DisSegName, StringRef DisSectName);
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001009static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1010 uint32_t size, uint32_t addr);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001011
1012static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1013 bool verbose) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001014 SymbolAddressMap AddrMap;
1015 if (verbose)
1016 CreateSymbolAddressMap(O, &AddrMap);
1017
1018 for (unsigned i = 0; i < DumpSections.size(); ++i) {
1019 StringRef DumpSection = DumpSections[i];
1020 std::pair<StringRef, StringRef> DumpSegSectName;
1021 DumpSegSectName = DumpSection.split(',');
1022 StringRef DumpSegName, DumpSectName;
1023 if (DumpSegSectName.second.size()) {
1024 DumpSegName = DumpSegSectName.first;
1025 DumpSectName = DumpSegSectName.second;
1026 } else {
1027 DumpSegName = "";
1028 DumpSectName = DumpSegSectName.first;
1029 }
1030 for (const SectionRef &Section : O->sections()) {
1031 StringRef SectName;
1032 Section.getName(SectName);
1033 DataRefImpl Ref = Section.getRawDataRefImpl();
1034 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1035 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1036 (SectName == DumpSectName)) {
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001037
Kevin Enderby95df54c2015-02-04 01:01:38 +00001038 uint32_t section_flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001039 if (O->is64Bit()) {
1040 const MachO::section_64 Sec = O->getSection64(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001041 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001042
1043 } else {
1044 const MachO::section Sec = O->getSection(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001045 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001046 }
Kevin Enderby95df54c2015-02-04 01:01:38 +00001047 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001048
1049 StringRef BytesStr;
1050 Section.getContents(BytesStr);
1051 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1052 uint32_t sect_size = BytesStr.size();
1053 uint64_t sect_addr = Section.getAddress();
1054
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001055 if (Raw) {
1056 outs().write(BytesStr.data(), BytesStr.size());
1057 continue;
1058 }
1059
1060 outs() << "Contents of (" << SegName << "," << SectName
1061 << ") section\n";
1062
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001063 if (verbose) {
Kevin Enderby95df54c2015-02-04 01:01:38 +00001064 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1065 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1066 DisassembleMachO(Filename, O, SegName, SectName);
1067 continue;
1068 }
Kevin Enderbycd66be52015-03-11 22:06:32 +00001069 if (SegName == "__TEXT" && SectName == "__info_plist") {
1070 outs() << sect;
1071 continue;
1072 }
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001073 if (SegName == "__OBJC" && SectName == "__protocol") {
1074 DumpProtocolSection(O, sect, sect_size, sect_addr);
1075 continue;
1076 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001077 switch (section_type) {
1078 case MachO::S_REGULAR:
1079 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1080 break;
1081 case MachO::S_ZEROFILL:
1082 outs() << "zerofill section and has no contents in the file\n";
1083 break;
Kevin Enderby10ba0412015-02-04 21:38:42 +00001084 case MachO::S_CSTRING_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001085 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby10ba0412015-02-04 21:38:42 +00001086 break;
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001087 case MachO::S_4BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001088 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001089 break;
1090 case MachO::S_8BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001091 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001092 break;
1093 case MachO::S_16BYTE_LITERALS:
Kevin Enderby0fc11822015-04-01 20:57:01 +00001094 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1095 break;
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001096 case MachO::S_LITERAL_POINTERS:
1097 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001098 !NoLeadingAddr);
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001099 break;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001100 case MachO::S_MOD_INIT_FUNC_POINTERS:
1101 case MachO::S_MOD_TERM_FUNC_POINTERS:
1102 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1103 verbose);
1104 break;
1105 default:
1106 outs() << "Unknown section type ("
1107 << format("0x%08" PRIx32, section_type) << ")\n";
1108 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1109 break;
1110 }
1111 } else {
1112 if (section_type == MachO::S_ZEROFILL)
1113 outs() << "zerofill section and has no contents in the file\n";
1114 else
1115 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1116 }
1117 }
1118 }
1119 }
1120}
1121
Kevin Enderbycd66be52015-03-11 22:06:32 +00001122static void DumpInfoPlistSectionContents(StringRef Filename,
1123 MachOObjectFile *O) {
1124 for (const SectionRef &Section : O->sections()) {
1125 StringRef SectName;
1126 Section.getName(SectName);
1127 DataRefImpl Ref = Section.getRawDataRefImpl();
1128 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1129 if (SegName == "__TEXT" && SectName == "__info_plist") {
1130 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1131 StringRef BytesStr;
1132 Section.getContents(BytesStr);
1133 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1134 outs() << sect;
1135 return;
1136 }
1137 }
1138}
1139
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001140// checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1141// and if it is and there is a list of architecture flags is specified then
1142// check to make sure this Mach-O file is one of those architectures or all
1143// architectures were specified. If not then an error is generated and this
1144// routine returns false. Else it returns true.
1145static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1146 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1147 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1148 bool ArchFound = false;
1149 MachO::mach_header H;
1150 MachO::mach_header_64 H_64;
1151 Triple T;
1152 if (MachO->is64Bit()) {
1153 H_64 = MachO->MachOObjectFile::getHeader64();
1154 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1155 } else {
1156 H = MachO->MachOObjectFile::getHeader();
1157 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1158 }
1159 unsigned i;
1160 for (i = 0; i < ArchFlags.size(); ++i) {
1161 if (ArchFlags[i] == T.getArchName())
1162 ArchFound = true;
1163 break;
1164 }
1165 if (!ArchFound) {
1166 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1167 << "architecture: " + ArchFlags[i] + "\n";
1168 return false;
1169 }
1170 }
1171 return true;
1172}
1173
Kevin Enderby0fc11822015-04-01 20:57:01 +00001174static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1175
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001176// ProcessMachO() is passed a single opened Mach-O file, which may be an
1177// archive member and or in a slice of a universal file. It prints the
1178// the file name and header info and then processes it according to the
1179// command line options.
1180static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1181 StringRef ArchiveMemberName = StringRef(),
1182 StringRef ArchitectureName = StringRef()) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001183 // If we are doing some processing here on the Mach-O file print the header
1184 // info. And don't print it otherwise like in the case of printing the
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001185 // UniversalHeaders or ArchiveHeaders.
Kevin Enderby131d1772015-01-09 19:22:37 +00001186 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001187 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
Kevin Enderby0fc11822015-04-01 20:57:01 +00001188 DylibsUsed || DylibId || ObjcMetaData ||
1189 (DumpSections.size() != 0 && !Raw)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001190 outs() << Filename;
1191 if (!ArchiveMemberName.empty())
1192 outs() << '(' << ArchiveMemberName << ')';
1193 if (!ArchitectureName.empty())
1194 outs() << " (architecture " << ArchitectureName << ")";
1195 outs() << ":\n";
1196 }
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001197
1198 if (Disassemble)
Kevin Enderby95df54c2015-02-04 01:01:38 +00001199 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001200 if (IndirectSymbols)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001201 PrintIndirectSymbols(MachOOF, !NonVerbose);
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001202 if (DataInCode)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001203 PrintDataInCodeTable(MachOOF, !NonVerbose);
Kevin Enderby9a509442015-01-27 21:28:24 +00001204 if (LinkOptHints)
1205 PrintLinkOptHints(MachOOF);
Kevin Enderby98da6132015-01-20 21:47:46 +00001206 if (Relocations)
1207 PrintRelocations(MachOOF);
1208 if (SectionHeaders)
1209 PrintSectionHeaders(MachOOF);
1210 if (SectionContents)
1211 PrintSectionContents(MachOOF);
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001212 if (DumpSections.size() != 0)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001213 DumpSectionContents(Filename, MachOOF, !NonVerbose);
Kevin Enderbycd66be52015-03-11 22:06:32 +00001214 if (InfoPlist)
1215 DumpInfoPlistSectionContents(Filename, MachOOF);
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001216 if (DylibsUsed)
1217 PrintDylibs(MachOOF, false);
1218 if (DylibId)
1219 PrintDylibs(MachOOF, true);
Kevin Enderby98da6132015-01-20 21:47:46 +00001220 if (SymbolTable)
1221 PrintSymbolTable(MachOOF);
1222 if (UnwindInfo)
1223 printMachOUnwindInfo(MachOOF);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001224 if (PrivateHeaders)
1225 printMachOFileHeader(MachOOF);
Kevin Enderby0fc11822015-04-01 20:57:01 +00001226 if (ObjcMetaData)
1227 printObjcMetaData(MachOOF, !NonVerbose);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001228 if (ExportsTrie)
1229 printExportsTrie(MachOOF);
1230 if (Rebase)
1231 printRebaseTable(MachOOF);
1232 if (Bind)
1233 printBindTable(MachOOF);
1234 if (LazyBind)
1235 printLazyBindTable(MachOOF);
1236 if (WeakBind)
1237 printWeakBindTable(MachOOF);
1238}
1239
Kevin Enderby131d1772015-01-09 19:22:37 +00001240// printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1241static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1242 outs() << " cputype (" << cputype << ")\n";
1243 outs() << " cpusubtype (" << cpusubtype << ")\n";
1244}
1245
1246// printCPUType() helps print_fat_headers by printing the cputype and
1247// pusubtype (symbolically for the one's it knows about).
1248static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1249 switch (cputype) {
1250 case MachO::CPU_TYPE_I386:
1251 switch (cpusubtype) {
1252 case MachO::CPU_SUBTYPE_I386_ALL:
1253 outs() << " cputype CPU_TYPE_I386\n";
1254 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1255 break;
1256 default:
1257 printUnknownCPUType(cputype, cpusubtype);
1258 break;
1259 }
1260 break;
1261 case MachO::CPU_TYPE_X86_64:
1262 switch (cpusubtype) {
1263 case MachO::CPU_SUBTYPE_X86_64_ALL:
1264 outs() << " cputype CPU_TYPE_X86_64\n";
1265 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1266 break;
1267 case MachO::CPU_SUBTYPE_X86_64_H:
1268 outs() << " cputype CPU_TYPE_X86_64\n";
1269 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1270 break;
1271 default:
1272 printUnknownCPUType(cputype, cpusubtype);
1273 break;
1274 }
1275 break;
1276 case MachO::CPU_TYPE_ARM:
1277 switch (cpusubtype) {
1278 case MachO::CPU_SUBTYPE_ARM_ALL:
1279 outs() << " cputype CPU_TYPE_ARM\n";
1280 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1281 break;
1282 case MachO::CPU_SUBTYPE_ARM_V4T:
1283 outs() << " cputype CPU_TYPE_ARM\n";
1284 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1285 break;
1286 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1287 outs() << " cputype CPU_TYPE_ARM\n";
1288 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1289 break;
1290 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1291 outs() << " cputype CPU_TYPE_ARM\n";
1292 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1293 break;
1294 case MachO::CPU_SUBTYPE_ARM_V6:
1295 outs() << " cputype CPU_TYPE_ARM\n";
1296 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1297 break;
1298 case MachO::CPU_SUBTYPE_ARM_V6M:
1299 outs() << " cputype CPU_TYPE_ARM\n";
1300 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1301 break;
1302 case MachO::CPU_SUBTYPE_ARM_V7:
1303 outs() << " cputype CPU_TYPE_ARM\n";
1304 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1305 break;
1306 case MachO::CPU_SUBTYPE_ARM_V7EM:
1307 outs() << " cputype CPU_TYPE_ARM\n";
1308 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1309 break;
1310 case MachO::CPU_SUBTYPE_ARM_V7K:
1311 outs() << " cputype CPU_TYPE_ARM\n";
1312 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1313 break;
1314 case MachO::CPU_SUBTYPE_ARM_V7M:
1315 outs() << " cputype CPU_TYPE_ARM\n";
1316 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1317 break;
1318 case MachO::CPU_SUBTYPE_ARM_V7S:
1319 outs() << " cputype CPU_TYPE_ARM\n";
1320 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1321 break;
1322 default:
1323 printUnknownCPUType(cputype, cpusubtype);
1324 break;
1325 }
1326 break;
1327 case MachO::CPU_TYPE_ARM64:
1328 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1329 case MachO::CPU_SUBTYPE_ARM64_ALL:
1330 outs() << " cputype CPU_TYPE_ARM64\n";
1331 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1332 break;
1333 default:
1334 printUnknownCPUType(cputype, cpusubtype);
1335 break;
1336 }
1337 break;
1338 default:
1339 printUnknownCPUType(cputype, cpusubtype);
1340 break;
1341 }
1342}
1343
1344static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1345 bool verbose) {
1346 outs() << "Fat headers\n";
1347 if (verbose)
1348 outs() << "fat_magic FAT_MAGIC\n";
1349 else
1350 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1351
1352 uint32_t nfat_arch = UB->getNumberOfObjects();
1353 StringRef Buf = UB->getData();
1354 uint64_t size = Buf.size();
1355 uint64_t big_size = sizeof(struct MachO::fat_header) +
1356 nfat_arch * sizeof(struct MachO::fat_arch);
1357 outs() << "nfat_arch " << UB->getNumberOfObjects();
1358 if (nfat_arch == 0)
1359 outs() << " (malformed, contains zero architecture types)\n";
1360 else if (big_size > size)
1361 outs() << " (malformed, architectures past end of file)\n";
1362 else
1363 outs() << "\n";
1364
1365 for (uint32_t i = 0; i < nfat_arch; ++i) {
1366 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1367 uint32_t cputype = OFA.getCPUType();
1368 uint32_t cpusubtype = OFA.getCPUSubType();
1369 outs() << "architecture ";
1370 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1371 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1372 uint32_t other_cputype = other_OFA.getCPUType();
1373 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
Kevin Enderby0512bd72015-01-09 21:55:03 +00001374 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
Kevin Enderby131d1772015-01-09 19:22:37 +00001375 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
Kevin Enderby0512bd72015-01-09 21:55:03 +00001376 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001377 outs() << "(illegal duplicate architecture) ";
1378 break;
Kevin Enderby0512bd72015-01-09 21:55:03 +00001379 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001380 }
1381 if (verbose) {
1382 outs() << OFA.getArchTypeName() << "\n";
1383 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1384 } else {
1385 outs() << i << "\n";
1386 outs() << " cputype " << cputype << "\n";
1387 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1388 << "\n";
1389 }
1390 if (verbose &&
1391 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1392 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1393 else
1394 outs() << " capabilities "
1395 << format("0x%" PRIx32,
1396 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1397 outs() << " offset " << OFA.getOffset();
1398 if (OFA.getOffset() > size)
1399 outs() << " (past end of file)";
1400 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1401 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1402 outs() << "\n";
1403 outs() << " size " << OFA.getSize();
1404 big_size = OFA.getOffset() + OFA.getSize();
1405 if (big_size > size)
1406 outs() << " (past end of file)";
1407 outs() << "\n";
1408 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1409 << ")\n";
1410 }
1411}
1412
Kevin Enderby13023a12015-01-15 23:19:11 +00001413static void printArchiveChild(Archive::Child &C, bool verbose,
1414 bool print_offset) {
1415 if (print_offset)
1416 outs() << C.getChildOffset() << "\t";
1417 sys::fs::perms Mode = C.getAccessMode();
1418 if (verbose) {
1419 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1420 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1421 outs() << "-";
1422 if (Mode & sys::fs::owner_read)
1423 outs() << "r";
1424 else
1425 outs() << "-";
1426 if (Mode & sys::fs::owner_write)
1427 outs() << "w";
1428 else
1429 outs() << "-";
1430 if (Mode & sys::fs::owner_exe)
1431 outs() << "x";
1432 else
1433 outs() << "-";
1434 if (Mode & sys::fs::group_read)
1435 outs() << "r";
1436 else
1437 outs() << "-";
1438 if (Mode & sys::fs::group_write)
1439 outs() << "w";
1440 else
1441 outs() << "-";
1442 if (Mode & sys::fs::group_exe)
1443 outs() << "x";
1444 else
1445 outs() << "-";
1446 if (Mode & sys::fs::others_read)
1447 outs() << "r";
1448 else
1449 outs() << "-";
1450 if (Mode & sys::fs::others_write)
1451 outs() << "w";
1452 else
1453 outs() << "-";
1454 if (Mode & sys::fs::others_exe)
1455 outs() << "x";
1456 else
1457 outs() << "-";
1458 } else {
1459 outs() << format("0%o ", Mode);
1460 }
1461
1462 unsigned UID = C.getUID();
1463 outs() << format("%3d/", UID);
1464 unsigned GID = C.getGID();
1465 outs() << format("%-3d ", GID);
Kevin Enderbyc12718932015-01-16 22:10:36 +00001466 uint64_t Size = C.getRawSize();
Kevin Enderby479ee612015-01-23 21:02:44 +00001467 outs() << format("%5" PRId64, Size) << " ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001468
1469 StringRef RawLastModified = C.getRawLastModified();
1470 if (verbose) {
1471 unsigned Seconds;
1472 if (RawLastModified.getAsInteger(10, Seconds))
1473 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1474 else {
1475 // Since cime(3) returns a 26 character string of the form:
1476 // "Sun Sep 16 01:03:52 1973\n\0"
1477 // just print 24 characters.
1478 time_t t = Seconds;
1479 outs() << format("%.24s ", ctime(&t));
1480 }
1481 } else {
1482 outs() << RawLastModified << " ";
1483 }
1484
1485 if (verbose) {
1486 ErrorOr<StringRef> NameOrErr = C.getName();
1487 if (NameOrErr.getError()) {
1488 StringRef RawName = C.getRawName();
1489 outs() << RawName << "\n";
1490 } else {
1491 StringRef Name = NameOrErr.get();
1492 outs() << Name << "\n";
1493 }
1494 } else {
1495 StringRef RawName = C.getRawName();
1496 outs() << RawName << "\n";
1497 }
1498}
1499
1500static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
1501 if (A->hasSymbolTable()) {
1502 Archive::child_iterator S = A->getSymbolTableChild();
1503 Archive::Child C = *S;
1504 printArchiveChild(C, verbose, print_offset);
1505 }
1506 for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E;
1507 ++I) {
1508 Archive::Child C = *I;
1509 printArchiveChild(C, verbose, print_offset);
1510 }
1511}
1512
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001513// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1514// -arch flags selecting just those slices as specified by them and also parses
1515// archive files. Then for each individual Mach-O file ProcessMachO() is
1516// called to process the file based on the command line options.
1517void llvm::ParseInputMachO(StringRef Filename) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001518 // Check for -arch all and verifiy the -arch flags are valid.
1519 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1520 if (ArchFlags[i] == "all") {
1521 ArchAll = true;
1522 } else {
1523 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1524 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1525 "'for the -arch option\n";
1526 return;
1527 }
1528 }
1529 }
1530
1531 // Attempt to open the binary.
1532 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1533 if (std::error_code EC = BinaryOrErr.getError()) {
1534 errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n";
Rafael Espindolade882cd2014-12-03 23:29:34 +00001535 return;
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001536 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001537 Binary &Bin = *BinaryOrErr.get().getBinary();
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001538
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001539 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1540 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001541 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001542 printArchiveHeaders(A, !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001543 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1544 I != E; ++I) {
1545 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary();
1546 if (ChildOrErr.getError())
1547 continue;
1548 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1549 if (!checkMachOAndArchFlags(O, Filename))
1550 return;
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001551 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001552 }
1553 }
1554 return;
1555 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001556 if (UniversalHeaders) {
1557 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
Kevin Enderbyf0640752015-03-13 17:56:32 +00001558 printMachOUniversalHeaders(UB, !NonVerbose);
Kevin Enderby131d1772015-01-09 19:22:37 +00001559 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001560 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1561 // If we have a list of architecture flags specified dump only those.
1562 if (!ArchAll && ArchFlags.size() != 0) {
1563 // Look for a slice in the universal binary that matches each ArchFlag.
1564 bool ArchFound;
1565 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1566 ArchFound = false;
1567 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1568 E = UB->end_objects();
1569 I != E; ++I) {
1570 if (ArchFlags[i] == I->getArchTypeName()) {
1571 ArchFound = true;
1572 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1573 I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001574 std::string ArchitectureName = "";
1575 if (ArchFlags.size() > 1)
1576 ArchitectureName = I->getArchTypeName();
1577 if (ObjOrErr) {
1578 ObjectFile &O = *ObjOrErr.get();
1579 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001580 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001581 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1582 I->getAsArchive()) {
1583 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001584 outs() << "Archive : " << Filename;
1585 if (!ArchitectureName.empty())
1586 outs() << " (architecture " << ArchitectureName << ")";
1587 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001588 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001589 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001590 for (Archive::child_iterator AI = A->child_begin(),
1591 AE = A->child_end();
1592 AI != AE; ++AI) {
1593 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1594 if (ChildOrErr.getError())
1595 continue;
1596 if (MachOObjectFile *O =
1597 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001598 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001599 }
1600 }
1601 }
1602 }
1603 if (!ArchFound) {
1604 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1605 << "architecture: " + ArchFlags[i] + "\n";
1606 return;
1607 }
1608 }
1609 return;
1610 }
1611 // No architecture flags were specified so if this contains a slice that
1612 // matches the host architecture dump only that.
1613 if (!ArchAll) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001614 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1615 E = UB->end_objects();
1616 I != E; ++I) {
Kevin Enderby0512bd72015-01-09 21:55:03 +00001617 if (MachOObjectFile::getHostArch().getArchName() ==
1618 I->getArchTypeName()) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001619 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001620 std::string ArchiveName;
1621 ArchiveName.clear();
1622 if (ObjOrErr) {
1623 ObjectFile &O = *ObjOrErr.get();
1624 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001625 ProcessMachO(Filename, MachOOF);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001626 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1627 I->getAsArchive()) {
1628 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001629 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001630 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001631 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001632 for (Archive::child_iterator AI = A->child_begin(),
1633 AE = A->child_end();
1634 AI != AE; ++AI) {
1635 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1636 if (ChildOrErr.getError())
1637 continue;
1638 if (MachOObjectFile *O =
1639 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001640 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001641 }
1642 }
1643 return;
1644 }
1645 }
1646 }
1647 // Either all architectures have been specified or none have been specified
1648 // and this does not contain the host architecture so dump all the slices.
1649 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1650 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1651 E = UB->end_objects();
1652 I != E; ++I) {
1653 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001654 std::string ArchitectureName = "";
1655 if (moreThanOneArch)
1656 ArchitectureName = I->getArchTypeName();
1657 if (ObjOrErr) {
1658 ObjectFile &Obj = *ObjOrErr.get();
1659 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001660 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001661 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1662 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001663 outs() << "Archive : " << Filename;
1664 if (!ArchitectureName.empty())
1665 outs() << " (architecture " << ArchitectureName << ")";
1666 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001667 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001668 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001669 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1670 AI != AE; ++AI) {
1671 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1672 if (ChildOrErr.getError())
1673 continue;
1674 if (MachOObjectFile *O =
1675 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1676 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001677 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1678 ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001679 }
1680 }
1681 }
1682 }
1683 return;
1684 }
1685 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1686 if (!checkMachOAndArchFlags(O, Filename))
1687 return;
1688 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001689 ProcessMachO(Filename, MachOOF);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001690 } else
1691 errs() << "llvm-objdump: '" << Filename << "': "
1692 << "Object is not a Mach-O file type.\n";
1693 } else
1694 errs() << "llvm-objdump: '" << Filename << "': "
1695 << "Unrecognized file type.\n";
Rafael Espindola9b709252013-04-13 01:45:40 +00001696}
1697
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001698typedef std::pair<uint64_t, const char *> BindInfoEntry;
1699typedef std::vector<BindInfoEntry> BindTable;
1700typedef BindTable::iterator bind_table_iterator;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001701
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001702// The block of info used by the Symbolizer call backs.
1703struct DisassembleInfo {
1704 bool verbose;
1705 MachOObjectFile *O;
1706 SectionRef S;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001707 SymbolAddressMap *AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001708 std::vector<SectionRef> *Sections;
1709 const char *class_name;
1710 const char *selector_name;
1711 char *method;
Kevin Enderby04bf6932014-10-28 23:39:46 +00001712 char *demangled_name;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001713 uint64_t adrp_addr;
1714 uint32_t adrp_inst;
Kevin Enderby078be602014-10-23 19:53:12 +00001715 BindTable *bindtable;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001716};
1717
1718// SymbolizerGetOpInfo() is the operand information call back function.
1719// This is called to get the symbolic information for operand(s) of an
1720// instruction when it is being done. This routine does this from
1721// the relocation information, symbol table, etc. That block of information
1722// is a pointer to the struct DisassembleInfo that was passed when the
1723// disassembler context was created and passed to back to here when
1724// called back by the disassembler for instruction operands that could have
1725// relocation information. The address of the instruction containing operand is
1726// at the Pc parameter. The immediate value the operand has is passed in
1727// op_info->Value and is at Offset past the start of the instruction and has a
1728// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1729// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1730// names and addends of the symbolic expression to add for the operand. The
1731// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1732// information is returned then this function returns 1 else it returns 0.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00001733static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1734 uint64_t Size, int TagType, void *TagBuf) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001735 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1736 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001737 uint64_t value = op_info->Value;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001738
1739 // Make sure all fields returned are zero if we don't set them.
1740 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1741 op_info->Value = value;
1742
1743 // If the TagType is not the value 1 which it code knows about or if no
1744 // verbose symbolic information is wanted then just return 0, indicating no
1745 // information is being returned.
David Blaikie33dd45d02015-03-23 18:39:02 +00001746 if (TagType != 1 || !info->verbose)
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001747 return 0;
1748
1749 unsigned int Arch = info->O->getArch();
1750 if (Arch == Triple::x86) {
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001751 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1752 return 0;
1753 // First search the section's relocation entries (if any) for an entry
1754 // for this section offset.
1755 uint32_t sect_addr = info->S.getAddress();
1756 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1757 bool reloc_found = false;
1758 DataRefImpl Rel;
1759 MachO::any_relocation_info RE;
1760 bool isExtern = false;
1761 SymbolRef Symbol;
1762 bool r_scattered = false;
1763 uint32_t r_value, pair_r_value, r_type;
1764 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001765 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001766 if (RelocOffset == sect_offset) {
1767 Rel = Reloc.getRawDataRefImpl();
1768 RE = info->O->getRelocation(Rel);
Kevin Enderby3eb73e12014-11-11 19:16:45 +00001769 r_type = info->O->getAnyRelocationType(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001770 r_scattered = info->O->isRelocationScattered(RE);
1771 if (r_scattered) {
1772 r_value = info->O->getScatteredRelocationValue(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001773 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1774 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1775 DataRefImpl RelNext = Rel;
1776 info->O->moveRelocationNext(RelNext);
1777 MachO::any_relocation_info RENext;
1778 RENext = info->O->getRelocation(RelNext);
1779 if (info->O->isRelocationScattered(RENext))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001780 pair_r_value = info->O->getScatteredRelocationValue(RENext);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001781 else
1782 return 0;
1783 }
1784 } else {
1785 isExtern = info->O->getPlainRelocationExternal(RE);
1786 if (isExtern) {
1787 symbol_iterator RelocSym = Reloc.getSymbol();
1788 Symbol = *RelocSym;
1789 }
1790 }
1791 reloc_found = true;
1792 break;
1793 }
1794 }
1795 if (reloc_found && isExtern) {
1796 StringRef SymName;
1797 Symbol.getName(SymName);
1798 const char *name = SymName.data();
1799 op_info->AddSymbol.Present = 1;
1800 op_info->AddSymbol.Name = name;
1801 // For i386 extern relocation entries the value in the instruction is
1802 // the offset from the symbol, and value is already set in op_info->Value.
1803 return 1;
1804 }
1805 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1806 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001807 const char *add = GuessSymbolName(r_value, info->AddrMap);
1808 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001809 uint32_t offset = value - (r_value - pair_r_value);
1810 op_info->AddSymbol.Present = 1;
1811 if (add != nullptr)
1812 op_info->AddSymbol.Name = add;
1813 else
1814 op_info->AddSymbol.Value = r_value;
1815 op_info->SubtractSymbol.Present = 1;
1816 if (sub != nullptr)
1817 op_info->SubtractSymbol.Name = sub;
1818 else
1819 op_info->SubtractSymbol.Value = pair_r_value;
1820 op_info->Value = offset;
1821 return 1;
1822 }
1823 // TODO:
1824 // Second search the external relocation entries of a fully linked image
1825 // (if any) for an entry that matches this segment offset.
1826 // uint32_t seg_offset = (Pc + Offset);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001827 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001828 }
1829 if (Arch == Triple::x86_64) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001830 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1831 return 0;
1832 // First search the section's relocation entries (if any) for an entry
1833 // for this section offset.
Rafael Espindola80291272014-10-08 15:28:58 +00001834 uint64_t sect_addr = info->S.getAddress();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001835 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1836 bool reloc_found = false;
1837 DataRefImpl Rel;
1838 MachO::any_relocation_info RE;
1839 bool isExtern = false;
1840 SymbolRef Symbol;
1841 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001842 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001843 if (RelocOffset == sect_offset) {
1844 Rel = Reloc.getRawDataRefImpl();
1845 RE = info->O->getRelocation(Rel);
1846 // NOTE: Scattered relocations don't exist on x86_64.
1847 isExtern = info->O->getPlainRelocationExternal(RE);
1848 if (isExtern) {
1849 symbol_iterator RelocSym = Reloc.getSymbol();
1850 Symbol = *RelocSym;
1851 }
1852 reloc_found = true;
1853 break;
1854 }
1855 }
1856 if (reloc_found && isExtern) {
1857 // The Value passed in will be adjusted by the Pc if the instruction
1858 // adds the Pc. But for x86_64 external relocation entries the Value
1859 // is the offset from the external symbol.
1860 if (info->O->getAnyRelocationPCRel(RE))
1861 op_info->Value -= Pc + Offset + Size;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001862 StringRef SymName;
1863 Symbol.getName(SymName);
1864 const char *name = SymName.data();
1865 unsigned Type = info->O->getAnyRelocationType(RE);
1866 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1867 DataRefImpl RelNext = Rel;
1868 info->O->moveRelocationNext(RelNext);
1869 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1870 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1871 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1872 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1873 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1874 op_info->SubtractSymbol.Present = 1;
1875 op_info->SubtractSymbol.Name = name;
1876 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1877 Symbol = *RelocSymNext;
1878 StringRef SymNameNext;
1879 Symbol.getName(SymNameNext);
1880 name = SymNameNext.data();
1881 }
1882 }
1883 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1884 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1885 op_info->AddSymbol.Present = 1;
1886 op_info->AddSymbol.Name = name;
1887 return 1;
1888 }
1889 // TODO:
1890 // Second search the external relocation entries of a fully linked image
1891 // (if any) for an entry that matches this segment offset.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001892 // uint64_t seg_offset = (Pc + Offset);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001893 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001894 }
1895 if (Arch == Triple::arm) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001896 if (Offset != 0 || (Size != 4 && Size != 2))
1897 return 0;
1898 // First search the section's relocation entries (if any) for an entry
1899 // for this section offset.
1900 uint32_t sect_addr = info->S.getAddress();
1901 uint32_t sect_offset = (Pc + Offset) - sect_addr;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001902 DataRefImpl Rel;
1903 MachO::any_relocation_info RE;
1904 bool isExtern = false;
1905 SymbolRef Symbol;
1906 bool r_scattered = false;
1907 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
David Blaikie33dd45d02015-03-23 18:39:02 +00001908 auto Reloc =
1909 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1910 [&](const RelocationRef &Reloc) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001911 uint64_t RelocOffset = Reloc.getOffset();
David Blaikie33dd45d02015-03-23 18:39:02 +00001912 return RelocOffset == sect_offset;
1913 });
1914
1915 if (Reloc == info->S.relocations().end())
1916 return 0;
1917
1918 Rel = Reloc->getRawDataRefImpl();
1919 RE = info->O->getRelocation(Rel);
1920 r_length = info->O->getAnyRelocationLength(RE);
1921 r_scattered = info->O->isRelocationScattered(RE);
1922 if (r_scattered) {
1923 r_value = info->O->getScatteredRelocationValue(RE);
1924 r_type = info->O->getScatteredRelocationType(RE);
1925 } else {
1926 r_type = info->O->getAnyRelocationType(RE);
1927 isExtern = info->O->getPlainRelocationExternal(RE);
1928 if (isExtern) {
1929 symbol_iterator RelocSym = Reloc->getSymbol();
1930 Symbol = *RelocSym;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001931 }
1932 }
David Blaikie33dd45d02015-03-23 18:39:02 +00001933 if (r_type == MachO::ARM_RELOC_HALF ||
1934 r_type == MachO::ARM_RELOC_SECTDIFF ||
1935 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
1936 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1937 DataRefImpl RelNext = Rel;
1938 info->O->moveRelocationNext(RelNext);
1939 MachO::any_relocation_info RENext;
1940 RENext = info->O->getRelocation(RelNext);
1941 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
1942 if (info->O->isRelocationScattered(RENext))
1943 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1944 }
1945
1946 if (isExtern) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001947 StringRef SymName;
1948 Symbol.getName(SymName);
1949 const char *name = SymName.data();
1950 op_info->AddSymbol.Present = 1;
1951 op_info->AddSymbol.Name = name;
Sylvestre Ledru648cced2015-02-05 17:00:23 +00001952 switch (r_type) {
1953 case MachO::ARM_RELOC_HALF:
1954 if ((r_length & 0x1) == 1) {
1955 op_info->Value = value << 16 | other_half;
1956 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1957 } else {
1958 op_info->Value = other_half << 16 | value;
1959 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Sylvestre Ledrufe0c7ad2015-02-05 16:35:44 +00001960 }
Sylvestre Ledru648cced2015-02-05 17:00:23 +00001961 break;
1962 default:
1963 break;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001964 }
1965 return 1;
1966 }
1967 // If we have a branch that is not an external relocation entry then
1968 // return 0 so the code in tryAddingSymbolicOperand() can use the
1969 // SymbolLookUp call back with the branch target address to look up the
1970 // symbol and possiblity add an annotation for a symbol stub.
David Blaikie33dd45d02015-03-23 18:39:02 +00001971 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
1972 r_type == MachO::ARM_THUMB_RELOC_BR22))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001973 return 0;
1974
1975 uint32_t offset = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001976 if (r_type == MachO::ARM_RELOC_HALF ||
1977 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1978 if ((r_length & 0x1) == 1)
1979 value = value << 16 | other_half;
1980 else
1981 value = other_half << 16 | value;
1982 }
1983 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
1984 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
1985 offset = value - r_value;
1986 value = r_value;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001987 }
1988
David Blaikie33dd45d02015-03-23 18:39:02 +00001989 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001990 if ((r_length & 0x1) == 1)
1991 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1992 else
1993 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001994 const char *add = GuessSymbolName(r_value, info->AddrMap);
1995 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00001996 int32_t offset = value - (r_value - pair_r_value);
1997 op_info->AddSymbol.Present = 1;
1998 if (add != nullptr)
1999 op_info->AddSymbol.Name = add;
2000 else
2001 op_info->AddSymbol.Value = r_value;
2002 op_info->SubtractSymbol.Present = 1;
2003 if (sub != nullptr)
2004 op_info->SubtractSymbol.Name = sub;
2005 else
2006 op_info->SubtractSymbol.Value = pair_r_value;
2007 op_info->Value = offset;
2008 return 1;
2009 }
2010
Kevin Enderby930fdc72014-11-06 19:00:13 +00002011 op_info->AddSymbol.Present = 1;
2012 op_info->Value = offset;
David Blaikie33dd45d02015-03-23 18:39:02 +00002013 if (r_type == MachO::ARM_RELOC_HALF) {
2014 if ((r_length & 0x1) == 1)
2015 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2016 else
2017 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002018 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002019 const char *add = GuessSymbolName(value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002020 if (add != nullptr) {
2021 op_info->AddSymbol.Name = add;
2022 return 1;
2023 }
2024 op_info->AddSymbol.Value = value;
2025 return 1;
David Blaikie33dd45d02015-03-23 18:39:02 +00002026 }
2027 if (Arch == Triple::aarch64) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002028 if (Offset != 0 || Size != 4)
2029 return 0;
2030 // First search the section's relocation entries (if any) for an entry
2031 // for this section offset.
2032 uint64_t sect_addr = info->S.getAddress();
2033 uint64_t sect_offset = (Pc + Offset) - sect_addr;
David Blaikie33dd45d02015-03-23 18:39:02 +00002034 auto Reloc =
2035 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2036 [&](const RelocationRef &Reloc) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002037 uint64_t RelocOffset = Reloc.getOffset();
David Blaikie33dd45d02015-03-23 18:39:02 +00002038 return RelocOffset == sect_offset;
2039 });
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002040
David Blaikie33dd45d02015-03-23 18:39:02 +00002041 if (Reloc == info->S.relocations().end())
2042 return 0;
2043
2044 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2045 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2046 uint32_t r_type = info->O->getAnyRelocationType(RE);
2047 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2048 DataRefImpl RelNext = Rel;
2049 info->O->moveRelocationNext(RelNext);
2050 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2051 if (value == 0) {
2052 value = info->O->getPlainRelocationSymbolNum(RENext);
2053 op_info->Value = value;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002054 }
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002055 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002056 // NOTE: Scattered relocations don't exist on arm64.
2057 if (!info->O->getPlainRelocationExternal(RE))
2058 return 0;
2059 StringRef SymName;
2060 Reloc->getSymbol()->getName(SymName);
2061 const char *name = SymName.data();
2062 op_info->AddSymbol.Present = 1;
2063 op_info->AddSymbol.Name = name;
2064
2065 switch (r_type) {
2066 case MachO::ARM64_RELOC_PAGE21:
2067 /* @page */
2068 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2069 break;
2070 case MachO::ARM64_RELOC_PAGEOFF12:
2071 /* @pageoff */
2072 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2073 break;
2074 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2075 /* @gotpage */
2076 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2077 break;
2078 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2079 /* @gotpageoff */
2080 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2081 break;
2082 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2083 /* @tvlppage is not implemented in llvm-mc */
2084 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2085 break;
2086 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2087 /* @tvlppageoff is not implemented in llvm-mc */
2088 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2089 break;
2090 default:
2091 case MachO::ARM64_RELOC_BRANCH26:
2092 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2093 break;
2094 }
2095 return 1;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002096 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002097 return 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002098}
2099
Kevin Enderbybf246f52014-09-24 23:08:22 +00002100// GuessCstringPointer is passed the address of what might be a pointer to a
2101// literal string in a cstring section. If that address is in a cstring section
2102// it returns a pointer to that string. Else it returns nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002103static const char *GuessCstringPointer(uint64_t ReferenceValue,
2104 struct DisassembleInfo *info) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002105 for (const auto &Load : info->O->load_commands()) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00002106 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2107 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2108 for (unsigned J = 0; J < Seg.nsects; ++J) {
2109 MachO::section_64 Sec = info->O->getSection64(Load, J);
2110 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2111 if (section_type == MachO::S_CSTRING_LITERALS &&
2112 ReferenceValue >= Sec.addr &&
2113 ReferenceValue < Sec.addr + Sec.size) {
2114 uint64_t sect_offset = ReferenceValue - Sec.addr;
2115 uint64_t object_offset = Sec.offset + sect_offset;
2116 StringRef MachOContents = info->O->getData();
2117 uint64_t object_size = MachOContents.size();
2118 const char *object_addr = (const char *)MachOContents.data();
2119 if (object_offset < object_size) {
2120 const char *name = object_addr + object_offset;
2121 return name;
2122 } else {
2123 return nullptr;
2124 }
2125 }
2126 }
2127 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2128 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2129 for (unsigned J = 0; J < Seg.nsects; ++J) {
2130 MachO::section Sec = info->O->getSection(Load, J);
2131 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2132 if (section_type == MachO::S_CSTRING_LITERALS &&
2133 ReferenceValue >= Sec.addr &&
2134 ReferenceValue < Sec.addr + Sec.size) {
2135 uint64_t sect_offset = ReferenceValue - Sec.addr;
2136 uint64_t object_offset = Sec.offset + sect_offset;
2137 StringRef MachOContents = info->O->getData();
2138 uint64_t object_size = MachOContents.size();
2139 const char *object_addr = (const char *)MachOContents.data();
2140 if (object_offset < object_size) {
2141 const char *name = object_addr + object_offset;
2142 return name;
2143 } else {
2144 return nullptr;
2145 }
2146 }
2147 }
2148 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00002149 }
2150 return nullptr;
2151}
2152
Kevin Enderby85974882014-09-26 22:20:44 +00002153// GuessIndirectSymbol returns the name of the indirect symbol for the
2154// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2155// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2156// symbol name being referenced by the stub or pointer.
2157static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2158 struct DisassembleInfo *info) {
Kevin Enderby85974882014-09-26 22:20:44 +00002159 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2160 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002161 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby85974882014-09-26 22:20:44 +00002162 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2163 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2164 for (unsigned J = 0; J < Seg.nsects; ++J) {
2165 MachO::section_64 Sec = info->O->getSection64(Load, J);
2166 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2167 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2168 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2169 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2170 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2171 section_type == MachO::S_SYMBOL_STUBS) &&
2172 ReferenceValue >= Sec.addr &&
2173 ReferenceValue < Sec.addr + Sec.size) {
2174 uint32_t stride;
2175 if (section_type == MachO::S_SYMBOL_STUBS)
2176 stride = Sec.reserved2;
2177 else
2178 stride = 8;
2179 if (stride == 0)
2180 return nullptr;
2181 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2182 if (index < Dysymtab.nindirectsyms) {
2183 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002184 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002185 if (indirect_symbol < Symtab.nsyms) {
2186 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2187 SymbolRef Symbol = *Sym;
2188 StringRef SymName;
2189 Symbol.getName(SymName);
2190 const char *name = SymName.data();
2191 return name;
2192 }
2193 }
2194 }
2195 }
2196 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2197 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2198 for (unsigned J = 0; J < Seg.nsects; ++J) {
2199 MachO::section Sec = info->O->getSection(Load, J);
2200 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2201 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2202 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2203 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2204 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2205 section_type == MachO::S_SYMBOL_STUBS) &&
2206 ReferenceValue >= Sec.addr &&
2207 ReferenceValue < Sec.addr + Sec.size) {
2208 uint32_t stride;
2209 if (section_type == MachO::S_SYMBOL_STUBS)
2210 stride = Sec.reserved2;
2211 else
2212 stride = 4;
2213 if (stride == 0)
2214 return nullptr;
2215 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2216 if (index < Dysymtab.nindirectsyms) {
2217 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002218 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002219 if (indirect_symbol < Symtab.nsyms) {
2220 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2221 SymbolRef Symbol = *Sym;
2222 StringRef SymName;
2223 Symbol.getName(SymName);
2224 const char *name = SymName.data();
2225 return name;
2226 }
2227 }
2228 }
2229 }
2230 }
Kevin Enderby85974882014-09-26 22:20:44 +00002231 }
2232 return nullptr;
2233}
2234
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002235// method_reference() is called passing it the ReferenceName that might be
2236// a reference it to an Objective-C method call. If so then it allocates and
2237// assembles a method call string with the values last seen and saved in
2238// the DisassembleInfo's class_name and selector_name fields. This is saved
2239// into the method field of the info and any previous string is free'ed.
2240// Then the class_name field in the info is set to nullptr. The method call
2241// string is set into ReferenceName and ReferenceType is set to
2242// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2243// then both ReferenceType and ReferenceName are left unchanged.
2244static void method_reference(struct DisassembleInfo *info,
2245 uint64_t *ReferenceType,
2246 const char **ReferenceName) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002247 unsigned int Arch = info->O->getArch();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002248 if (*ReferenceName != nullptr) {
2249 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002250 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002251 if (info->method != nullptr)
2252 free(info->method);
2253 if (info->class_name != nullptr) {
2254 info->method = (char *)malloc(5 + strlen(info->class_name) +
2255 strlen(info->selector_name));
2256 if (info->method != nullptr) {
2257 strcpy(info->method, "+[");
2258 strcat(info->method, info->class_name);
2259 strcat(info->method, " ");
2260 strcat(info->method, info->selector_name);
2261 strcat(info->method, "]");
2262 *ReferenceName = info->method;
2263 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2264 }
2265 } else {
2266 info->method = (char *)malloc(9 + strlen(info->selector_name));
2267 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002268 if (Arch == Triple::x86_64)
2269 strcpy(info->method, "-[%rdi ");
2270 else if (Arch == Triple::aarch64)
2271 strcpy(info->method, "-[x0 ");
2272 else
2273 strcpy(info->method, "-[r? ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002274 strcat(info->method, info->selector_name);
2275 strcat(info->method, "]");
2276 *ReferenceName = info->method;
2277 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2278 }
2279 }
2280 info->class_name = nullptr;
2281 }
2282 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002283 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002284 if (info->method != nullptr)
2285 free(info->method);
2286 info->method = (char *)malloc(17 + strlen(info->selector_name));
2287 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002288 if (Arch == Triple::x86_64)
2289 strcpy(info->method, "-[[%rdi super] ");
2290 else if (Arch == Triple::aarch64)
2291 strcpy(info->method, "-[[x0 super] ");
2292 else
2293 strcpy(info->method, "-[[r? super] ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002294 strcat(info->method, info->selector_name);
2295 strcat(info->method, "]");
2296 *ReferenceName = info->method;
2297 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2298 }
2299 info->class_name = nullptr;
2300 }
2301 }
2302 }
2303}
2304
2305// GuessPointerPointer() is passed the address of what might be a pointer to
2306// a reference to an Objective-C class, selector, message ref or cfstring.
2307// If so the value of the pointer is returned and one of the booleans are set
2308// to true. If not zero is returned and all the booleans are set to false.
2309static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2310 struct DisassembleInfo *info,
2311 bool &classref, bool &selref, bool &msgref,
2312 bool &cfstring) {
2313 classref = false;
2314 selref = false;
2315 msgref = false;
2316 cfstring = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002317 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002318 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2319 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2320 for (unsigned J = 0; J < Seg.nsects; ++J) {
2321 MachO::section_64 Sec = info->O->getSection64(Load, J);
2322 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2323 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2324 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2325 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2326 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2327 ReferenceValue >= Sec.addr &&
2328 ReferenceValue < Sec.addr + Sec.size) {
2329 uint64_t sect_offset = ReferenceValue - Sec.addr;
2330 uint64_t object_offset = Sec.offset + sect_offset;
2331 StringRef MachOContents = info->O->getData();
2332 uint64_t object_size = MachOContents.size();
2333 const char *object_addr = (const char *)MachOContents.data();
2334 if (object_offset < object_size) {
2335 uint64_t pointer_value;
2336 memcpy(&pointer_value, object_addr + object_offset,
2337 sizeof(uint64_t));
2338 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2339 sys::swapByteOrder(pointer_value);
2340 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2341 selref = true;
2342 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2343 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2344 classref = true;
2345 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2346 ReferenceValue + 8 < Sec.addr + Sec.size) {
2347 msgref = true;
2348 memcpy(&pointer_value, object_addr + object_offset + 8,
2349 sizeof(uint64_t));
2350 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2351 sys::swapByteOrder(pointer_value);
2352 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2353 cfstring = true;
2354 return pointer_value;
2355 } else {
2356 return 0;
2357 }
2358 }
2359 }
2360 }
2361 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002362 }
2363 return 0;
2364}
2365
2366// get_pointer_64 returns a pointer to the bytes in the object file at the
2367// Address from a section in the Mach-O file. And indirectly returns the
2368// offset into the section, number of bytes left in the section past the offset
2369// and which section is was being referenced. If the Address is not in a
2370// section nullptr is returned.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002371static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2372 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002373 DisassembleInfo *info,
2374 bool objc_only = false) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002375 offset = 0;
2376 left = 0;
2377 S = SectionRef();
2378 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2379 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2380 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
Kevin Enderby846c0002015-04-16 17:19:59 +00002381 if (objc_only) {
2382 StringRef SectName;
2383 ((*(info->Sections))[SectIdx]).getName(SectName);
2384 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2385 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2386 if (SegName != "__OBJC" && SectName != "__cstring")
2387 continue;
2388 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002389 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2390 S = (*(info->Sections))[SectIdx];
2391 offset = Address - SectAddress;
2392 left = SectSize - offset;
2393 StringRef SectContents;
2394 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2395 return SectContents.data() + offset;
2396 }
2397 }
2398 return nullptr;
2399}
2400
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002401static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2402 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002403 DisassembleInfo *info,
2404 bool objc_only = false) {
2405 return get_pointer_64(Address, offset, left, S, info, objc_only);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002406}
2407
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002408// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2409// the symbol indirectly through n_value. Based on the relocation information
2410// for the specified section offset in the specified section reference.
Kevin Enderby0fc11822015-04-01 20:57:01 +00002411// If no relocation information is found and a non-zero ReferenceValue for the
2412// symbol is passed, look up that address in the info's AddrMap.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002413static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2414 DisassembleInfo *info, uint64_t &n_value,
2415 uint64_t ReferenceValue = UnknownAddress) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002416 n_value = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002417 if (!info->verbose)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002418 return nullptr;
2419
2420 // See if there is an external relocation entry at the sect_offset.
2421 bool reloc_found = false;
2422 DataRefImpl Rel;
2423 MachO::any_relocation_info RE;
2424 bool isExtern = false;
2425 SymbolRef Symbol;
2426 for (const RelocationRef &Reloc : S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002427 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002428 if (RelocOffset == sect_offset) {
2429 Rel = Reloc.getRawDataRefImpl();
2430 RE = info->O->getRelocation(Rel);
2431 if (info->O->isRelocationScattered(RE))
2432 continue;
2433 isExtern = info->O->getPlainRelocationExternal(RE);
2434 if (isExtern) {
2435 symbol_iterator RelocSym = Reloc.getSymbol();
2436 Symbol = *RelocSym;
2437 }
2438 reloc_found = true;
2439 break;
2440 }
2441 }
2442 // If there is an external relocation entry for a symbol in this section
2443 // at this section_offset then use that symbol's value for the n_value
2444 // and return its name.
2445 const char *SymbolName = nullptr;
2446 if (reloc_found && isExtern) {
2447 Symbol.getAddress(n_value);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002448 if (n_value == UnknownAddress)
Kevin Enderby0fc11822015-04-01 20:57:01 +00002449 n_value = 0;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002450 StringRef name;
2451 Symbol.getName(name);
2452 if (!name.empty()) {
2453 SymbolName = name.data();
2454 return SymbolName;
2455 }
2456 }
2457
2458 // TODO: For fully linked images, look through the external relocation
2459 // entries off the dynamic symtab command. For these the r_offset is from the
2460 // start of the first writeable segment in the Mach-O file. So the offset
2461 // to this section from that segment is passed to this routine by the caller,
2462 // as the database_offset. Which is the difference of the section's starting
2463 // address and the first writable segment.
2464 //
2465 // NOTE: need add passing the database_offset to this routine.
2466
Kevin Enderby0fc11822015-04-01 20:57:01 +00002467 // We did not find an external relocation entry so look up the ReferenceValue
2468 // as an address of a symbol and if found return that symbol's name.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002469 if (ReferenceValue != UnknownAddress)
Kevin Enderby0fc11822015-04-01 20:57:01 +00002470 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002471
2472 return SymbolName;
2473}
2474
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002475static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2476 DisassembleInfo *info,
2477 uint32_t ReferenceValue) {
2478 uint64_t n_value64;
2479 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2480}
2481
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002482// These are structs in the Objective-C meta data and read to produce the
2483// comments for disassembly. While these are part of the ABI they are no
2484// public defintions. So the are here not in include/llvm/Support/MachO.h .
2485
2486// The cfstring object in a 64-bit Mach-O file.
2487struct cfstring64_t {
2488 uint64_t isa; // class64_t * (64-bit pointer)
2489 uint64_t flags; // flag bits
2490 uint64_t characters; // char * (64-bit pointer)
2491 uint64_t length; // number of non-NULL characters in above
2492};
2493
2494// The class object in a 64-bit Mach-O file.
2495struct class64_t {
2496 uint64_t isa; // class64_t * (64-bit pointer)
2497 uint64_t superclass; // class64_t * (64-bit pointer)
2498 uint64_t cache; // Cache (64-bit pointer)
2499 uint64_t vtable; // IMP * (64-bit pointer)
2500 uint64_t data; // class_ro64_t * (64-bit pointer)
2501};
2502
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002503struct class32_t {
2504 uint32_t isa; /* class32_t * (32-bit pointer) */
2505 uint32_t superclass; /* class32_t * (32-bit pointer) */
2506 uint32_t cache; /* Cache (32-bit pointer) */
2507 uint32_t vtable; /* IMP * (32-bit pointer) */
2508 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2509};
2510
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002511struct class_ro64_t {
2512 uint32_t flags;
2513 uint32_t instanceStart;
2514 uint32_t instanceSize;
2515 uint32_t reserved;
2516 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2517 uint64_t name; // const char * (64-bit pointer)
2518 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2519 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2520 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2521 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2522 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2523};
2524
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002525struct class_ro32_t {
2526 uint32_t flags;
2527 uint32_t instanceStart;
2528 uint32_t instanceSize;
2529 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2530 uint32_t name; /* const char * (32-bit pointer) */
2531 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2532 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2533 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2534 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2535 uint32_t baseProperties; /* const struct objc_property_list *
2536 (32-bit pointer) */
2537};
2538
2539/* Values for class_ro{64,32}_t->flags */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002540#define RO_META (1 << 0)
2541#define RO_ROOT (1 << 1)
2542#define RO_HAS_CXX_STRUCTORS (1 << 2)
2543
2544struct method_list64_t {
2545 uint32_t entsize;
2546 uint32_t count;
2547 /* struct method64_t first; These structures follow inline */
2548};
2549
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002550struct method_list32_t {
2551 uint32_t entsize;
2552 uint32_t count;
2553 /* struct method32_t first; These structures follow inline */
2554};
2555
Kevin Enderby0fc11822015-04-01 20:57:01 +00002556struct method64_t {
2557 uint64_t name; /* SEL (64-bit pointer) */
2558 uint64_t types; /* const char * (64-bit pointer) */
2559 uint64_t imp; /* IMP (64-bit pointer) */
2560};
2561
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002562struct method32_t {
2563 uint32_t name; /* SEL (32-bit pointer) */
2564 uint32_t types; /* const char * (32-bit pointer) */
2565 uint32_t imp; /* IMP (32-bit pointer) */
2566};
2567
Kevin Enderby0fc11822015-04-01 20:57:01 +00002568struct protocol_list64_t {
2569 uint64_t count; /* uintptr_t (a 64-bit value) */
2570 /* struct protocol64_t * list[0]; These pointers follow inline */
2571};
2572
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002573struct protocol_list32_t {
2574 uint32_t count; /* uintptr_t (a 32-bit value) */
2575 /* struct protocol32_t * list[0]; These pointers follow inline */
2576};
2577
Kevin Enderby0fc11822015-04-01 20:57:01 +00002578struct protocol64_t {
2579 uint64_t isa; /* id * (64-bit pointer) */
2580 uint64_t name; /* const char * (64-bit pointer) */
2581 uint64_t protocols; /* struct protocol_list64_t *
2582 (64-bit pointer) */
2583 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2584 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2585 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2586 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2587 uint64_t instanceProperties; /* struct objc_property_list *
2588 (64-bit pointer) */
2589};
2590
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002591struct protocol32_t {
2592 uint32_t isa; /* id * (32-bit pointer) */
2593 uint32_t name; /* const char * (32-bit pointer) */
2594 uint32_t protocols; /* struct protocol_list_t *
2595 (32-bit pointer) */
2596 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2597 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2598 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2599 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2600 uint32_t instanceProperties; /* struct objc_property_list *
2601 (32-bit pointer) */
2602};
2603
Kevin Enderby0fc11822015-04-01 20:57:01 +00002604struct ivar_list64_t {
2605 uint32_t entsize;
2606 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002607 /* struct ivar64_t first; These structures follow inline */
2608};
2609
2610struct ivar_list32_t {
2611 uint32_t entsize;
2612 uint32_t count;
2613 /* struct ivar32_t first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002614};
2615
2616struct ivar64_t {
2617 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2618 uint64_t name; /* const char * (64-bit pointer) */
2619 uint64_t type; /* const char * (64-bit pointer) */
2620 uint32_t alignment;
2621 uint32_t size;
2622};
2623
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002624struct ivar32_t {
2625 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2626 uint32_t name; /* const char * (32-bit pointer) */
2627 uint32_t type; /* const char * (32-bit pointer) */
2628 uint32_t alignment;
2629 uint32_t size;
2630};
2631
Kevin Enderby0fc11822015-04-01 20:57:01 +00002632struct objc_property_list64 {
2633 uint32_t entsize;
2634 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002635 /* struct objc_property64 first; These structures follow inline */
2636};
2637
2638struct objc_property_list32 {
2639 uint32_t entsize;
2640 uint32_t count;
2641 /* struct objc_property32 first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002642};
2643
2644struct objc_property64 {
2645 uint64_t name; /* const char * (64-bit pointer) */
2646 uint64_t attributes; /* const char * (64-bit pointer) */
2647};
2648
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002649struct objc_property32 {
2650 uint32_t name; /* const char * (32-bit pointer) */
2651 uint32_t attributes; /* const char * (32-bit pointer) */
2652};
2653
Kevin Enderby0fc11822015-04-01 20:57:01 +00002654struct category64_t {
2655 uint64_t name; /* const char * (64-bit pointer) */
2656 uint64_t cls; /* struct class_t * (64-bit pointer) */
2657 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2658 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2659 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2660 uint64_t instanceProperties; /* struct objc_property_list *
2661 (64-bit pointer) */
2662};
2663
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002664struct category32_t {
2665 uint32_t name; /* const char * (32-bit pointer) */
2666 uint32_t cls; /* struct class_t * (32-bit pointer) */
2667 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2668 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2669 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2670 uint32_t instanceProperties; /* struct objc_property_list *
2671 (32-bit pointer) */
2672};
2673
Kevin Enderby0fc11822015-04-01 20:57:01 +00002674struct objc_image_info64 {
2675 uint32_t version;
2676 uint32_t flags;
2677};
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002678struct objc_image_info32 {
2679 uint32_t version;
2680 uint32_t flags;
2681};
Kevin Enderby846c0002015-04-16 17:19:59 +00002682struct imageInfo_t {
2683 uint32_t version;
2684 uint32_t flags;
2685};
Kevin Enderby0fc11822015-04-01 20:57:01 +00002686/* masks for objc_image_info.flags */
2687#define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2688#define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2689
2690struct message_ref64 {
2691 uint64_t imp; /* IMP (64-bit pointer) */
2692 uint64_t sel; /* SEL (64-bit pointer) */
2693};
2694
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002695struct message_ref32 {
2696 uint32_t imp; /* IMP (32-bit pointer) */
2697 uint32_t sel; /* SEL (32-bit pointer) */
2698};
2699
Kevin Enderby846c0002015-04-16 17:19:59 +00002700// Objective-C 1 (32-bit only) meta data structs.
2701
2702struct objc_module_t {
2703 uint32_t version;
2704 uint32_t size;
2705 uint32_t name; /* char * (32-bit pointer) */
2706 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2707};
2708
2709struct objc_symtab_t {
2710 uint32_t sel_ref_cnt;
2711 uint32_t refs; /* SEL * (32-bit pointer) */
2712 uint16_t cls_def_cnt;
2713 uint16_t cat_def_cnt;
2714 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2715};
2716
2717struct objc_class_t {
2718 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2719 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2720 uint32_t name; /* const char * (32-bit pointer) */
2721 int32_t version;
2722 int32_t info;
2723 int32_t instance_size;
2724 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2725 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2726 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2727 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2728};
2729
2730#define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2731// class is not a metaclass
2732#define CLS_CLASS 0x1
2733// class is a metaclass
2734#define CLS_META 0x2
2735
2736struct objc_category_t {
2737 uint32_t category_name; /* char * (32-bit pointer) */
2738 uint32_t class_name; /* char * (32-bit pointer) */
2739 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2740 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2741 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2742};
2743
2744struct objc_ivar_t {
2745 uint32_t ivar_name; /* char * (32-bit pointer) */
2746 uint32_t ivar_type; /* char * (32-bit pointer) */
2747 int32_t ivar_offset;
2748};
2749
2750struct objc_ivar_list_t {
2751 int32_t ivar_count;
2752 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2753};
2754
2755struct objc_method_list_t {
2756 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2757 int32_t method_count;
2758 // struct objc_method_t method_list[1]; /* variable length structure */
2759};
2760
2761struct objc_method_t {
2762 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2763 uint32_t method_types; /* char * (32-bit pointer) */
2764 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2765 (32-bit pointer) */
2766};
2767
2768struct objc_protocol_list_t {
2769 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2770 int32_t count;
2771 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2772 // (32-bit pointer) */
2773};
2774
2775struct objc_protocol_t {
2776 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2777 uint32_t protocol_name; /* char * (32-bit pointer) */
2778 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2779 uint32_t instance_methods; /* struct objc_method_description_list *
2780 (32-bit pointer) */
2781 uint32_t class_methods; /* struct objc_method_description_list *
2782 (32-bit pointer) */
2783};
2784
2785struct objc_method_description_list_t {
2786 int32_t count;
2787 // struct objc_method_description_t list[1];
2788};
2789
2790struct objc_method_description_t {
2791 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2792 uint32_t types; /* char * (32-bit pointer) */
2793};
2794
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002795inline void swapStruct(struct cfstring64_t &cfs) {
2796 sys::swapByteOrder(cfs.isa);
2797 sys::swapByteOrder(cfs.flags);
2798 sys::swapByteOrder(cfs.characters);
2799 sys::swapByteOrder(cfs.length);
2800}
2801
2802inline void swapStruct(struct class64_t &c) {
2803 sys::swapByteOrder(c.isa);
2804 sys::swapByteOrder(c.superclass);
2805 sys::swapByteOrder(c.cache);
2806 sys::swapByteOrder(c.vtable);
2807 sys::swapByteOrder(c.data);
2808}
2809
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002810inline void swapStruct(struct class32_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 Enderby6f326ce2014-10-23 19:37:31 +00002818inline void swapStruct(struct class_ro64_t &cro) {
2819 sys::swapByteOrder(cro.flags);
2820 sys::swapByteOrder(cro.instanceStart);
2821 sys::swapByteOrder(cro.instanceSize);
2822 sys::swapByteOrder(cro.reserved);
2823 sys::swapByteOrder(cro.ivarLayout);
2824 sys::swapByteOrder(cro.name);
2825 sys::swapByteOrder(cro.baseMethods);
2826 sys::swapByteOrder(cro.baseProtocols);
2827 sys::swapByteOrder(cro.ivars);
2828 sys::swapByteOrder(cro.weakIvarLayout);
2829 sys::swapByteOrder(cro.baseProperties);
2830}
2831
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002832inline void swapStruct(struct class_ro32_t &cro) {
2833 sys::swapByteOrder(cro.flags);
2834 sys::swapByteOrder(cro.instanceStart);
2835 sys::swapByteOrder(cro.instanceSize);
2836 sys::swapByteOrder(cro.ivarLayout);
2837 sys::swapByteOrder(cro.name);
2838 sys::swapByteOrder(cro.baseMethods);
2839 sys::swapByteOrder(cro.baseProtocols);
2840 sys::swapByteOrder(cro.ivars);
2841 sys::swapByteOrder(cro.weakIvarLayout);
2842 sys::swapByteOrder(cro.baseProperties);
2843}
2844
Kevin Enderby0fc11822015-04-01 20:57:01 +00002845inline void swapStruct(struct method_list64_t &ml) {
2846 sys::swapByteOrder(ml.entsize);
2847 sys::swapByteOrder(ml.count);
2848}
2849
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002850inline void swapStruct(struct method_list32_t &ml) {
2851 sys::swapByteOrder(ml.entsize);
2852 sys::swapByteOrder(ml.count);
2853}
2854
Kevin Enderby0fc11822015-04-01 20:57:01 +00002855inline void swapStruct(struct method64_t &m) {
2856 sys::swapByteOrder(m.name);
2857 sys::swapByteOrder(m.types);
2858 sys::swapByteOrder(m.imp);
2859}
2860
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002861inline void swapStruct(struct method32_t &m) {
2862 sys::swapByteOrder(m.name);
2863 sys::swapByteOrder(m.types);
2864 sys::swapByteOrder(m.imp);
2865}
2866
Kevin Enderby0fc11822015-04-01 20:57:01 +00002867inline void swapStruct(struct protocol_list64_t &pl) {
2868 sys::swapByteOrder(pl.count);
2869}
2870
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002871inline void swapStruct(struct protocol_list32_t &pl) {
2872 sys::swapByteOrder(pl.count);
2873}
2874
Kevin Enderby0fc11822015-04-01 20:57:01 +00002875inline void swapStruct(struct protocol64_t &p) {
2876 sys::swapByteOrder(p.isa);
2877 sys::swapByteOrder(p.name);
2878 sys::swapByteOrder(p.protocols);
2879 sys::swapByteOrder(p.instanceMethods);
2880 sys::swapByteOrder(p.classMethods);
2881 sys::swapByteOrder(p.optionalInstanceMethods);
2882 sys::swapByteOrder(p.optionalClassMethods);
2883 sys::swapByteOrder(p.instanceProperties);
2884}
2885
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002886inline void swapStruct(struct protocol32_t &p) {
2887 sys::swapByteOrder(p.isa);
2888 sys::swapByteOrder(p.name);
2889 sys::swapByteOrder(p.protocols);
2890 sys::swapByteOrder(p.instanceMethods);
2891 sys::swapByteOrder(p.classMethods);
2892 sys::swapByteOrder(p.optionalInstanceMethods);
2893 sys::swapByteOrder(p.optionalClassMethods);
2894 sys::swapByteOrder(p.instanceProperties);
2895}
2896
Kevin Enderby0fc11822015-04-01 20:57:01 +00002897inline void swapStruct(struct ivar_list64_t &il) {
2898 sys::swapByteOrder(il.entsize);
2899 sys::swapByteOrder(il.count);
2900}
2901
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002902inline void swapStruct(struct ivar_list32_t &il) {
2903 sys::swapByteOrder(il.entsize);
2904 sys::swapByteOrder(il.count);
2905}
2906
Kevin Enderby0fc11822015-04-01 20:57:01 +00002907inline void swapStruct(struct ivar64_t &i) {
2908 sys::swapByteOrder(i.offset);
2909 sys::swapByteOrder(i.name);
2910 sys::swapByteOrder(i.type);
2911 sys::swapByteOrder(i.alignment);
2912 sys::swapByteOrder(i.size);
2913}
2914
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002915inline void swapStruct(struct ivar32_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 Enderby0fc11822015-04-01 20:57:01 +00002923inline void swapStruct(struct objc_property_list64 &pl) {
2924 sys::swapByteOrder(pl.entsize);
2925 sys::swapByteOrder(pl.count);
2926}
2927
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002928inline void swapStruct(struct objc_property_list32 &pl) {
2929 sys::swapByteOrder(pl.entsize);
2930 sys::swapByteOrder(pl.count);
2931}
2932
Kevin Enderby0fc11822015-04-01 20:57:01 +00002933inline void swapStruct(struct objc_property64 &op) {
2934 sys::swapByteOrder(op.name);
2935 sys::swapByteOrder(op.attributes);
2936}
2937
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002938inline void swapStruct(struct objc_property32 &op) {
2939 sys::swapByteOrder(op.name);
2940 sys::swapByteOrder(op.attributes);
2941}
2942
Kevin Enderby0fc11822015-04-01 20:57:01 +00002943inline void swapStruct(struct category64_t &c) {
2944 sys::swapByteOrder(c.name);
2945 sys::swapByteOrder(c.cls);
2946 sys::swapByteOrder(c.instanceMethods);
2947 sys::swapByteOrder(c.classMethods);
2948 sys::swapByteOrder(c.protocols);
2949 sys::swapByteOrder(c.instanceProperties);
2950}
2951
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002952inline void swapStruct(struct category32_t &c) {
2953 sys::swapByteOrder(c.name);
2954 sys::swapByteOrder(c.cls);
2955 sys::swapByteOrder(c.instanceMethods);
2956 sys::swapByteOrder(c.classMethods);
2957 sys::swapByteOrder(c.protocols);
2958 sys::swapByteOrder(c.instanceProperties);
2959}
2960
Kevin Enderby0fc11822015-04-01 20:57:01 +00002961inline void swapStruct(struct objc_image_info64 &o) {
2962 sys::swapByteOrder(o.version);
2963 sys::swapByteOrder(o.flags);
2964}
2965
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002966inline void swapStruct(struct objc_image_info32 &o) {
2967 sys::swapByteOrder(o.version);
2968 sys::swapByteOrder(o.flags);
2969}
2970
Kevin Enderby846c0002015-04-16 17:19:59 +00002971inline void swapStruct(struct imageInfo_t &o) {
2972 sys::swapByteOrder(o.version);
2973 sys::swapByteOrder(o.flags);
2974}
2975
Kevin Enderby0fc11822015-04-01 20:57:01 +00002976inline void swapStruct(struct message_ref64 &mr) {
2977 sys::swapByteOrder(mr.imp);
2978 sys::swapByteOrder(mr.sel);
2979}
2980
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002981inline void swapStruct(struct message_ref32 &mr) {
2982 sys::swapByteOrder(mr.imp);
2983 sys::swapByteOrder(mr.sel);
2984}
2985
Kevin Enderby846c0002015-04-16 17:19:59 +00002986inline void swapStruct(struct objc_module_t &module) {
2987 sys::swapByteOrder(module.version);
2988 sys::swapByteOrder(module.size);
2989 sys::swapByteOrder(module.name);
2990 sys::swapByteOrder(module.symtab);
Jingyue Wufedecc42015-04-16 18:43:44 +00002991}
Kevin Enderby846c0002015-04-16 17:19:59 +00002992
2993inline void swapStruct(struct objc_symtab_t &symtab) {
2994 sys::swapByteOrder(symtab.sel_ref_cnt);
2995 sys::swapByteOrder(symtab.refs);
2996 sys::swapByteOrder(symtab.cls_def_cnt);
2997 sys::swapByteOrder(symtab.cat_def_cnt);
Jingyue Wufedecc42015-04-16 18:43:44 +00002998}
Kevin Enderby846c0002015-04-16 17:19:59 +00002999
3000inline void swapStruct(struct objc_class_t &objc_class) {
3001 sys::swapByteOrder(objc_class.isa);
3002 sys::swapByteOrder(objc_class.super_class);
3003 sys::swapByteOrder(objc_class.name);
3004 sys::swapByteOrder(objc_class.version);
3005 sys::swapByteOrder(objc_class.info);
3006 sys::swapByteOrder(objc_class.instance_size);
3007 sys::swapByteOrder(objc_class.ivars);
3008 sys::swapByteOrder(objc_class.methodLists);
3009 sys::swapByteOrder(objc_class.cache);
3010 sys::swapByteOrder(objc_class.protocols);
Jingyue Wufedecc42015-04-16 18:43:44 +00003011}
Kevin Enderby846c0002015-04-16 17:19:59 +00003012
3013inline void swapStruct(struct objc_category_t &objc_category) {
3014 sys::swapByteOrder(objc_category.category_name);
3015 sys::swapByteOrder(objc_category.class_name);
3016 sys::swapByteOrder(objc_category.instance_methods);
3017 sys::swapByteOrder(objc_category.class_methods);
3018 sys::swapByteOrder(objc_category.protocols);
3019}
3020
3021inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3022 sys::swapByteOrder(objc_ivar_list.ivar_count);
3023}
3024
3025inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3026 sys::swapByteOrder(objc_ivar.ivar_name);
3027 sys::swapByteOrder(objc_ivar.ivar_type);
3028 sys::swapByteOrder(objc_ivar.ivar_offset);
Jingyue Wufedecc42015-04-16 18:43:44 +00003029}
Kevin Enderby846c0002015-04-16 17:19:59 +00003030
3031inline void swapStruct(struct objc_method_list_t &method_list) {
3032 sys::swapByteOrder(method_list.obsolete);
3033 sys::swapByteOrder(method_list.method_count);
3034}
3035
3036inline void swapStruct(struct objc_method_t &method) {
3037 sys::swapByteOrder(method.method_name);
3038 sys::swapByteOrder(method.method_types);
3039 sys::swapByteOrder(method.method_imp);
3040}
3041
3042inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3043 sys::swapByteOrder(protocol_list.next);
3044 sys::swapByteOrder(protocol_list.count);
3045}
3046
3047inline void swapStruct(struct objc_protocol_t &protocol) {
3048 sys::swapByteOrder(protocol.isa);
3049 sys::swapByteOrder(protocol.protocol_name);
3050 sys::swapByteOrder(protocol.protocol_list);
3051 sys::swapByteOrder(protocol.instance_methods);
3052 sys::swapByteOrder(protocol.class_methods);
3053}
3054
3055inline void swapStruct(struct objc_method_description_list_t &mdl) {
3056 sys::swapByteOrder(mdl.count);
3057}
3058
3059inline void swapStruct(struct objc_method_description_t &md) {
3060 sys::swapByteOrder(md.name);
3061 sys::swapByteOrder(md.types);
3062}
3063
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003064static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3065 struct DisassembleInfo *info);
3066
3067// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3068// to an Objective-C class and returns the class name. It is also passed the
3069// address of the pointer, so when the pointer is zero as it can be in an .o
3070// file, that is used to look for an external relocation entry with a symbol
3071// name.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003072static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3073 uint64_t ReferenceValue,
3074 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003075 const char *r;
3076 uint32_t offset, left;
3077 SectionRef S;
3078
3079 // The pointer_value can be 0 in an object file and have a relocation
3080 // entry for the class symbol at the ReferenceValue (the address of the
3081 // pointer).
3082 if (pointer_value == 0) {
3083 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3084 if (r == nullptr || left < sizeof(uint64_t))
3085 return nullptr;
3086 uint64_t n_value;
3087 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3088 if (symbol_name == nullptr)
3089 return nullptr;
Hans Wennborgdb53e302014-10-23 21:59:17 +00003090 const char *class_name = strrchr(symbol_name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003091 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3092 return class_name + 2;
3093 else
3094 return nullptr;
3095 }
3096
3097 // The case were the pointer_value is non-zero and points to a class defined
3098 // in this Mach-O file.
3099 r = get_pointer_64(pointer_value, offset, left, S, info);
3100 if (r == nullptr || left < sizeof(struct class64_t))
3101 return nullptr;
3102 struct class64_t c;
3103 memcpy(&c, r, sizeof(struct class64_t));
3104 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3105 swapStruct(c);
3106 if (c.data == 0)
3107 return nullptr;
3108 r = get_pointer_64(c.data, offset, left, S, info);
3109 if (r == nullptr || left < sizeof(struct class_ro64_t))
3110 return nullptr;
3111 struct class_ro64_t cro;
3112 memcpy(&cro, r, sizeof(struct class_ro64_t));
3113 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3114 swapStruct(cro);
3115 if (cro.name == 0)
3116 return nullptr;
3117 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3118 return name;
3119}
3120
3121// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3122// pointer to a cfstring and returns its name or nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003123static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3124 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003125 const char *r, *name;
3126 uint32_t offset, left;
3127 SectionRef S;
3128 struct cfstring64_t cfs;
3129 uint64_t cfs_characters;
3130
3131 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3132 if (r == nullptr || left < sizeof(struct cfstring64_t))
3133 return nullptr;
3134 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3135 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3136 swapStruct(cfs);
3137 if (cfs.characters == 0) {
3138 uint64_t n_value;
3139 const char *symbol_name = get_symbol_64(
3140 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3141 if (symbol_name == nullptr)
3142 return nullptr;
3143 cfs_characters = n_value;
3144 } else
3145 cfs_characters = cfs.characters;
3146 name = get_pointer_64(cfs_characters, offset, left, S, info);
3147
3148 return name;
3149}
3150
3151// get_objc2_64bit_selref() is used for disassembly and is passed a the address
3152// of a pointer to an Objective-C selector reference when the pointer value is
3153// zero as in a .o file and is likely to have a external relocation entry with
3154// who's symbol's n_value is the real pointer to the selector name. If that is
3155// the case the real pointer to the selector name is returned else 0 is
3156// returned
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003157static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3158 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003159 uint32_t offset, left;
3160 SectionRef S;
3161
3162 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3163 if (r == nullptr || left < sizeof(uint64_t))
3164 return 0;
3165 uint64_t n_value;
3166 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3167 if (symbol_name == nullptr)
3168 return 0;
3169 return n_value;
3170}
3171
Kevin Enderby0fc11822015-04-01 20:57:01 +00003172static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3173 const char *sectname) {
3174 for (const SectionRef &Section : O->sections()) {
3175 StringRef SectName;
3176 Section.getName(SectName);
3177 DataRefImpl Ref = Section.getRawDataRefImpl();
3178 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3179 if (SegName == segname && SectName == sectname)
3180 return Section;
3181 }
3182 return SectionRef();
3183}
3184
3185static void
3186walk_pointer_list_64(const char *listname, const SectionRef S,
3187 MachOObjectFile *O, struct DisassembleInfo *info,
3188 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3189 if (S == SectionRef())
3190 return;
3191
3192 StringRef SectName;
3193 S.getName(SectName);
3194 DataRefImpl Ref = S.getRawDataRefImpl();
3195 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3196 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3197
3198 StringRef BytesStr;
3199 S.getContents(BytesStr);
3200 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3201
3202 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3203 uint32_t left = S.getSize() - i;
3204 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3205 uint64_t p = 0;
3206 memcpy(&p, Contents + i, size);
3207 if (i + sizeof(uint64_t) > S.getSize())
3208 outs() << listname << " list pointer extends past end of (" << SegName
3209 << "," << SectName << ") section\n";
3210 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3211
3212 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3213 sys::swapByteOrder(p);
3214
3215 uint64_t n_value = 0;
3216 const char *name = get_symbol_64(i, S, info, n_value, p);
3217 if (name == nullptr)
3218 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3219
3220 if (n_value != 0) {
3221 outs() << format("0x%" PRIx64, n_value);
3222 if (p != 0)
3223 outs() << " + " << format("0x%" PRIx64, p);
3224 } else
3225 outs() << format("0x%" PRIx64, p);
3226 if (name != nullptr)
3227 outs() << " " << name;
3228 outs() << "\n";
3229
3230 p += n_value;
3231 if (func)
3232 func(p, info);
3233 }
3234}
3235
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003236static void
3237walk_pointer_list_32(const char *listname, const SectionRef S,
3238 MachOObjectFile *O, struct DisassembleInfo *info,
3239 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3240 if (S == SectionRef())
3241 return;
3242
3243 StringRef SectName;
3244 S.getName(SectName);
3245 DataRefImpl Ref = S.getRawDataRefImpl();
3246 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3247 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3248
3249 StringRef BytesStr;
3250 S.getContents(BytesStr);
3251 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3252
3253 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3254 uint32_t left = S.getSize() - i;
3255 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3256 uint32_t p = 0;
3257 memcpy(&p, Contents + i, size);
3258 if (i + sizeof(uint32_t) > S.getSize())
3259 outs() << listname << " list pointer extends past end of (" << SegName
3260 << "," << SectName << ") section\n";
Kevin Enderbycf261312015-04-06 22:33:43 +00003261 uint32_t Address = S.getAddress() + i;
3262 outs() << format("%08" PRIx32, Address) << " ";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003263
3264 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3265 sys::swapByteOrder(p);
3266 outs() << format("0x%" PRIx32, p);
3267
3268 const char *name = get_symbol_32(i, S, info, p);
3269 if (name != nullptr)
3270 outs() << " " << name;
3271 outs() << "\n";
3272
3273 if (func)
3274 func(p, info);
3275 }
3276}
3277
3278static void print_layout_map(const char *layout_map, uint32_t left) {
3279 outs() << " layout map: ";
3280 do {
3281 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3282 left--;
3283 layout_map++;
3284 } while (*layout_map != '\0' && left != 0);
3285 outs() << "\n";
3286}
3287
Kevin Enderby0fc11822015-04-01 20:57:01 +00003288static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3289 uint32_t offset, left;
3290 SectionRef S;
3291 const char *layout_map;
3292
3293 if (p == 0)
3294 return;
3295 layout_map = get_pointer_64(p, offset, left, S, info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003296 print_layout_map(layout_map, left);
3297}
3298
3299static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3300 uint32_t offset, left;
3301 SectionRef S;
3302 const char *layout_map;
3303
3304 if (p == 0)
3305 return;
3306 layout_map = get_pointer_32(p, offset, left, S, info);
3307 print_layout_map(layout_map, left);
Kevin Enderby0fc11822015-04-01 20:57:01 +00003308}
3309
3310static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3311 const char *indent) {
3312 struct method_list64_t ml;
3313 struct method64_t m;
3314 const char *r;
3315 uint32_t offset, xoffset, left, i;
3316 SectionRef S, xS;
3317 const char *name, *sym_name;
3318 uint64_t n_value;
3319
3320 r = get_pointer_64(p, offset, left, S, info);
3321 if (r == nullptr)
3322 return;
3323 memset(&ml, '\0', sizeof(struct method_list64_t));
3324 if (left < sizeof(struct method_list64_t)) {
3325 memcpy(&ml, r, left);
3326 outs() << " (method_list_t entends past the end of the section)\n";
3327 } else
3328 memcpy(&ml, r, sizeof(struct method_list64_t));
3329 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3330 swapStruct(ml);
3331 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3332 outs() << indent << "\t\t count " << ml.count << "\n";
3333
3334 p += sizeof(struct method_list64_t);
3335 offset += sizeof(struct method_list64_t);
3336 for (i = 0; i < ml.count; i++) {
3337 r = get_pointer_64(p, offset, left, S, info);
3338 if (r == nullptr)
3339 return;
3340 memset(&m, '\0', sizeof(struct method64_t));
3341 if (left < sizeof(struct method64_t)) {
3342 memcpy(&ml, r, left);
3343 outs() << indent << " (method_t entends past the end of the section)\n";
3344 } else
3345 memcpy(&m, r, sizeof(struct method64_t));
3346 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3347 swapStruct(m);
3348
3349 outs() << indent << "\t\t name ";
3350 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3351 info, n_value, m.name);
3352 if (n_value != 0) {
3353 if (info->verbose && sym_name != nullptr)
3354 outs() << sym_name;
3355 else
3356 outs() << format("0x%" PRIx64, n_value);
3357 if (m.name != 0)
3358 outs() << " + " << format("0x%" PRIx64, m.name);
3359 } else
3360 outs() << format("0x%" PRIx64, m.name);
3361 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3362 if (name != nullptr)
3363 outs() << format(" %.*s", left, name);
3364 outs() << "\n";
3365
3366 outs() << indent << "\t\t types ";
3367 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3368 info, n_value, m.types);
3369 if (n_value != 0) {
3370 if (info->verbose && sym_name != nullptr)
3371 outs() << sym_name;
3372 else
3373 outs() << format("0x%" PRIx64, n_value);
3374 if (m.types != 0)
3375 outs() << " + " << format("0x%" PRIx64, m.types);
3376 } else
3377 outs() << format("0x%" PRIx64, m.types);
3378 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3379 if (name != nullptr)
3380 outs() << format(" %.*s", left, name);
3381 outs() << "\n";
3382
3383 outs() << indent << "\t\t imp ";
3384 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3385 n_value, m.imp);
3386 if (info->verbose && name == nullptr) {
3387 if (n_value != 0) {
3388 outs() << format("0x%" PRIx64, n_value) << " ";
3389 if (m.imp != 0)
3390 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3391 } else
3392 outs() << format("0x%" PRIx64, m.imp) << " ";
3393 }
3394 if (name != nullptr)
3395 outs() << name;
3396 outs() << "\n";
3397
3398 p += sizeof(struct method64_t);
3399 offset += sizeof(struct method64_t);
3400 }
3401}
3402
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003403static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3404 const char *indent) {
3405 struct method_list32_t ml;
3406 struct method32_t m;
Kevin Enderby846c0002015-04-16 17:19:59 +00003407 const char *r, *name;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003408 uint32_t offset, xoffset, left, i;
3409 SectionRef S, xS;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003410
3411 r = get_pointer_32(p, offset, left, S, info);
3412 if (r == nullptr)
3413 return;
3414 memset(&ml, '\0', sizeof(struct method_list32_t));
3415 if (left < sizeof(struct method_list32_t)) {
3416 memcpy(&ml, r, left);
3417 outs() << " (method_list_t entends past the end of the section)\n";
3418 } else
3419 memcpy(&ml, r, sizeof(struct method_list32_t));
3420 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3421 swapStruct(ml);
3422 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3423 outs() << indent << "\t\t count " << ml.count << "\n";
3424
3425 p += sizeof(struct method_list32_t);
3426 offset += sizeof(struct method_list32_t);
3427 for (i = 0; i < ml.count; i++) {
3428 r = get_pointer_32(p, offset, left, S, info);
3429 if (r == nullptr)
3430 return;
3431 memset(&m, '\0', sizeof(struct method32_t));
3432 if (left < sizeof(struct method32_t)) {
3433 memcpy(&ml, r, left);
3434 outs() << indent << " (method_t entends past the end of the section)\n";
3435 } else
3436 memcpy(&m, r, sizeof(struct method32_t));
3437 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3438 swapStruct(m);
3439
3440 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3441 name = get_pointer_32(m.name, xoffset, left, xS, info);
3442 if (name != nullptr)
3443 outs() << format(" %.*s", left, name);
3444 outs() << "\n";
3445
3446 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3447 name = get_pointer_32(m.types, xoffset, left, xS, info);
3448 if (name != nullptr)
3449 outs() << format(" %.*s", left, name);
3450 outs() << "\n";
3451
3452 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3453 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3454 m.imp);
3455 if (name != nullptr)
3456 outs() << " " << name;
3457 outs() << "\n";
3458
3459 p += sizeof(struct method32_t);
3460 offset += sizeof(struct method32_t);
3461 }
3462}
3463
Kevin Enderby846c0002015-04-16 17:19:59 +00003464static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3465 uint32_t offset, left, xleft;
3466 SectionRef S;
3467 struct objc_method_list_t method_list;
3468 struct objc_method_t method;
3469 const char *r, *methods, *name, *SymbolName;
3470 int32_t i;
3471
3472 r = get_pointer_32(p, offset, left, S, info, true);
3473 if (r == nullptr)
3474 return true;
3475
3476 outs() << "\n";
3477 if (left > sizeof(struct objc_method_list_t)) {
3478 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3479 } else {
3480 outs() << "\t\t objc_method_list extends past end of the section\n";
3481 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3482 memcpy(&method_list, r, left);
3483 }
3484 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3485 swapStruct(method_list);
3486
3487 outs() << "\t\t obsolete "
3488 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3489 outs() << "\t\t method_count " << method_list.method_count << "\n";
3490
3491 methods = r + sizeof(struct objc_method_list_t);
3492 for (i = 0; i < method_list.method_count; i++) {
3493 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3494 outs() << "\t\t remaining method's extend past the of the section\n";
3495 break;
3496 }
3497 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3498 sizeof(struct objc_method_t));
3499 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3500 swapStruct(method);
3501
3502 outs() << "\t\t method_name "
3503 << format("0x%08" PRIx32, method.method_name);
3504 if (info->verbose) {
3505 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3506 if (name != nullptr)
3507 outs() << format(" %.*s", xleft, name);
3508 else
3509 outs() << " (not in an __OBJC section)";
3510 }
3511 outs() << "\n";
3512
3513 outs() << "\t\t method_types "
3514 << format("0x%08" PRIx32, method.method_types);
3515 if (info->verbose) {
3516 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3517 if (name != nullptr)
3518 outs() << format(" %.*s", xleft, name);
3519 else
3520 outs() << " (not in an __OBJC section)";
3521 }
3522 outs() << "\n";
3523
3524 outs() << "\t\t method_imp "
3525 << format("0x%08" PRIx32, method.method_imp) << " ";
3526 if (info->verbose) {
3527 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3528 if (SymbolName != nullptr)
3529 outs() << SymbolName;
3530 }
3531 outs() << "\n";
3532 }
3533 return false;
3534}
3535
Kevin Enderby0fc11822015-04-01 20:57:01 +00003536static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3537 struct protocol_list64_t pl;
3538 uint64_t q, n_value;
3539 struct protocol64_t pc;
3540 const char *r;
3541 uint32_t offset, xoffset, left, i;
3542 SectionRef S, xS;
3543 const char *name, *sym_name;
3544
3545 r = get_pointer_64(p, offset, left, S, info);
3546 if (r == nullptr)
3547 return;
3548 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3549 if (left < sizeof(struct protocol_list64_t)) {
3550 memcpy(&pl, r, left);
3551 outs() << " (protocol_list_t entends past the end of the section)\n";
3552 } else
3553 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3554 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3555 swapStruct(pl);
3556 outs() << " count " << pl.count << "\n";
3557
3558 p += sizeof(struct protocol_list64_t);
3559 offset += sizeof(struct protocol_list64_t);
3560 for (i = 0; i < pl.count; i++) {
3561 r = get_pointer_64(p, offset, left, S, info);
3562 if (r == nullptr)
3563 return;
3564 q = 0;
3565 if (left < sizeof(uint64_t)) {
3566 memcpy(&q, r, left);
3567 outs() << " (protocol_t * entends past the end of the section)\n";
3568 } else
3569 memcpy(&q, r, sizeof(uint64_t));
3570 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3571 sys::swapByteOrder(q);
3572
3573 outs() << "\t\t list[" << i << "] ";
3574 sym_name = get_symbol_64(offset, S, info, n_value, q);
3575 if (n_value != 0) {
3576 if (info->verbose && sym_name != nullptr)
3577 outs() << sym_name;
3578 else
3579 outs() << format("0x%" PRIx64, n_value);
3580 if (q != 0)
3581 outs() << " + " << format("0x%" PRIx64, q);
3582 } else
3583 outs() << format("0x%" PRIx64, q);
3584 outs() << " (struct protocol_t *)\n";
3585
3586 r = get_pointer_64(q + n_value, offset, left, S, info);
3587 if (r == nullptr)
3588 return;
3589 memset(&pc, '\0', sizeof(struct protocol64_t));
3590 if (left < sizeof(struct protocol64_t)) {
3591 memcpy(&pc, r, left);
3592 outs() << " (protocol_t entends past the end of the section)\n";
3593 } else
3594 memcpy(&pc, r, sizeof(struct protocol64_t));
3595 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3596 swapStruct(pc);
3597
3598 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3599
3600 outs() << "\t\t\t name ";
3601 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3602 info, n_value, pc.name);
3603 if (n_value != 0) {
3604 if (info->verbose && sym_name != nullptr)
3605 outs() << sym_name;
3606 else
3607 outs() << format("0x%" PRIx64, n_value);
3608 if (pc.name != 0)
3609 outs() << " + " << format("0x%" PRIx64, pc.name);
3610 } else
3611 outs() << format("0x%" PRIx64, pc.name);
3612 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3613 if (name != nullptr)
3614 outs() << format(" %.*s", left, name);
3615 outs() << "\n";
3616
3617 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3618
3619 outs() << "\t\t instanceMethods ";
3620 sym_name =
3621 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3622 S, info, n_value, pc.instanceMethods);
3623 if (n_value != 0) {
3624 if (info->verbose && sym_name != nullptr)
3625 outs() << sym_name;
3626 else
3627 outs() << format("0x%" PRIx64, n_value);
3628 if (pc.instanceMethods != 0)
3629 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3630 } else
3631 outs() << format("0x%" PRIx64, pc.instanceMethods);
3632 outs() << " (struct method_list_t *)\n";
3633 if (pc.instanceMethods + n_value != 0)
3634 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3635
3636 outs() << "\t\t classMethods ";
3637 sym_name =
3638 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3639 info, n_value, pc.classMethods);
3640 if (n_value != 0) {
3641 if (info->verbose && sym_name != nullptr)
3642 outs() << sym_name;
3643 else
3644 outs() << format("0x%" PRIx64, n_value);
3645 if (pc.classMethods != 0)
3646 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3647 } else
3648 outs() << format("0x%" PRIx64, pc.classMethods);
3649 outs() << " (struct method_list_t *)\n";
3650 if (pc.classMethods + n_value != 0)
3651 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3652
3653 outs() << "\t optionalInstanceMethods "
3654 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3655 outs() << "\t optionalClassMethods "
3656 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3657 outs() << "\t instanceProperties "
3658 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3659
3660 p += sizeof(uint64_t);
3661 offset += sizeof(uint64_t);
3662 }
3663}
3664
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003665static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3666 struct protocol_list32_t pl;
3667 uint32_t q;
3668 struct protocol32_t pc;
3669 const char *r;
3670 uint32_t offset, xoffset, left, i;
3671 SectionRef S, xS;
3672 const char *name;
3673
3674 r = get_pointer_32(p, offset, left, S, info);
3675 if (r == nullptr)
3676 return;
3677 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3678 if (left < sizeof(struct protocol_list32_t)) {
3679 memcpy(&pl, r, left);
3680 outs() << " (protocol_list_t entends past the end of the section)\n";
3681 } else
3682 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3683 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3684 swapStruct(pl);
3685 outs() << " count " << pl.count << "\n";
3686
3687 p += sizeof(struct protocol_list32_t);
3688 offset += sizeof(struct protocol_list32_t);
3689 for (i = 0; i < pl.count; i++) {
3690 r = get_pointer_32(p, offset, left, S, info);
3691 if (r == nullptr)
3692 return;
3693 q = 0;
3694 if (left < sizeof(uint32_t)) {
3695 memcpy(&q, r, left);
3696 outs() << " (protocol_t * entends past the end of the section)\n";
3697 } else
3698 memcpy(&q, r, sizeof(uint32_t));
3699 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3700 sys::swapByteOrder(q);
3701 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3702 << " (struct protocol_t *)\n";
3703 r = get_pointer_32(q, offset, left, S, info);
3704 if (r == nullptr)
3705 return;
3706 memset(&pc, '\0', sizeof(struct protocol32_t));
3707 if (left < sizeof(struct protocol32_t)) {
3708 memcpy(&pc, r, left);
3709 outs() << " (protocol_t entends past the end of the section)\n";
3710 } else
3711 memcpy(&pc, r, sizeof(struct protocol32_t));
3712 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3713 swapStruct(pc);
3714 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3715 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3716 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3717 if (name != nullptr)
3718 outs() << format(" %.*s", left, name);
3719 outs() << "\n";
3720 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3721 outs() << "\t\t instanceMethods "
3722 << format("0x%" PRIx32, pc.instanceMethods)
3723 << " (struct method_list_t *)\n";
3724 if (pc.instanceMethods != 0)
3725 print_method_list32_t(pc.instanceMethods, info, "\t");
3726 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3727 << " (struct method_list_t *)\n";
3728 if (pc.classMethods != 0)
3729 print_method_list32_t(pc.classMethods, info, "\t");
3730 outs() << "\t optionalInstanceMethods "
3731 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3732 outs() << "\t optionalClassMethods "
3733 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3734 outs() << "\t instanceProperties "
3735 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3736 p += sizeof(uint32_t);
3737 offset += sizeof(uint32_t);
3738 }
3739}
3740
Kevin Enderby846c0002015-04-16 17:19:59 +00003741static void print_indent(uint32_t indent) {
3742 for (uint32_t i = 0; i < indent;) {
3743 if (indent - i >= 8) {
3744 outs() << "\t";
3745 i += 8;
3746 } else {
3747 for (uint32_t j = i; j < indent; j++)
3748 outs() << " ";
3749 return;
3750 }
3751 }
3752}
3753
3754static bool print_method_description_list(uint32_t p, uint32_t indent,
3755 struct DisassembleInfo *info) {
3756 uint32_t offset, left, xleft;
3757 SectionRef S;
3758 struct objc_method_description_list_t mdl;
3759 struct objc_method_description_t md;
3760 const char *r, *list, *name;
3761 int32_t i;
3762
3763 r = get_pointer_32(p, offset, left, S, info, true);
3764 if (r == nullptr)
3765 return true;
3766
3767 outs() << "\n";
3768 if (left > sizeof(struct objc_method_description_list_t)) {
3769 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3770 } else {
3771 print_indent(indent);
3772 outs() << " objc_method_description_list extends past end of the section\n";
3773 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3774 memcpy(&mdl, r, left);
3775 }
3776 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3777 swapStruct(mdl);
3778
3779 print_indent(indent);
3780 outs() << " count " << mdl.count << "\n";
3781
3782 list = r + sizeof(struct objc_method_description_list_t);
3783 for (i = 0; i < mdl.count; i++) {
3784 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3785 print_indent(indent);
3786 outs() << " remaining list entries extend past the of the section\n";
3787 break;
3788 }
3789 print_indent(indent);
3790 outs() << " list[" << i << "]\n";
3791 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3792 sizeof(struct objc_method_description_t));
3793 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3794 swapStruct(md);
3795
3796 print_indent(indent);
3797 outs() << " name " << format("0x%08" PRIx32, md.name);
3798 if (info->verbose) {
3799 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3800 if (name != nullptr)
3801 outs() << format(" %.*s", xleft, name);
3802 else
3803 outs() << " (not in an __OBJC section)";
3804 }
3805 outs() << "\n";
3806
3807 print_indent(indent);
3808 outs() << " types " << format("0x%08" PRIx32, md.types);
3809 if (info->verbose) {
3810 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3811 if (name != nullptr)
3812 outs() << format(" %.*s", xleft, name);
3813 else
3814 outs() << " (not in an __OBJC section)";
3815 }
3816 outs() << "\n";
3817 }
3818 return false;
3819}
3820
3821static bool print_protocol_list(uint32_t p, uint32_t indent,
3822 struct DisassembleInfo *info);
3823
3824static bool print_protocol(uint32_t p, uint32_t indent,
3825 struct DisassembleInfo *info) {
3826 uint32_t offset, left;
3827 SectionRef S;
3828 struct objc_protocol_t protocol;
3829 const char *r, *name;
3830
3831 r = get_pointer_32(p, offset, left, S, info, true);
3832 if (r == nullptr)
3833 return true;
3834
3835 outs() << "\n";
3836 if (left >= sizeof(struct objc_protocol_t)) {
3837 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3838 } else {
3839 print_indent(indent);
3840 outs() << " Protocol extends past end of the section\n";
3841 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3842 memcpy(&protocol, r, left);
3843 }
3844 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3845 swapStruct(protocol);
3846
3847 print_indent(indent);
3848 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3849 << "\n";
3850
3851 print_indent(indent);
3852 outs() << " protocol_name "
3853 << format("0x%08" PRIx32, protocol.protocol_name);
3854 if (info->verbose) {
3855 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3856 if (name != nullptr)
3857 outs() << format(" %.*s", left, name);
3858 else
3859 outs() << " (not in an __OBJC section)";
3860 }
3861 outs() << "\n";
3862
3863 print_indent(indent);
3864 outs() << " protocol_list "
3865 << format("0x%08" PRIx32, protocol.protocol_list);
3866 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3867 outs() << " (not in an __OBJC section)\n";
3868
3869 print_indent(indent);
3870 outs() << " instance_methods "
3871 << format("0x%08" PRIx32, protocol.instance_methods);
3872 if (print_method_description_list(protocol.instance_methods, indent, info))
3873 outs() << " (not in an __OBJC section)\n";
3874
3875 print_indent(indent);
3876 outs() << " class_methods "
3877 << format("0x%08" PRIx32, protocol.class_methods);
3878 if (print_method_description_list(protocol.class_methods, indent, info))
3879 outs() << " (not in an __OBJC section)\n";
3880
3881 return false;
3882}
3883
3884static bool print_protocol_list(uint32_t p, uint32_t indent,
3885 struct DisassembleInfo *info) {
3886 uint32_t offset, left, l;
3887 SectionRef S;
3888 struct objc_protocol_list_t protocol_list;
3889 const char *r, *list;
3890 int32_t i;
3891
3892 r = get_pointer_32(p, offset, left, S, info, true);
3893 if (r == nullptr)
3894 return true;
3895
3896 outs() << "\n";
3897 if (left > sizeof(struct objc_protocol_list_t)) {
3898 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
3899 } else {
3900 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
3901 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
3902 memcpy(&protocol_list, r, left);
3903 }
3904 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3905 swapStruct(protocol_list);
3906
3907 print_indent(indent);
3908 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
3909 << "\n";
3910 print_indent(indent);
3911 outs() << " count " << protocol_list.count << "\n";
3912
3913 list = r + sizeof(struct objc_protocol_list_t);
3914 for (i = 0; i < protocol_list.count; i++) {
3915 if ((i + 1) * sizeof(uint32_t) > left) {
3916 outs() << "\t\t remaining list entries extend past the of the section\n";
3917 break;
3918 }
3919 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
3920 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3921 sys::swapByteOrder(l);
3922
3923 print_indent(indent);
3924 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
3925 if (print_protocol(l, indent, info))
3926 outs() << "(not in an __OBJC section)\n";
3927 }
3928 return false;
3929}
3930
Kevin Enderby0fc11822015-04-01 20:57:01 +00003931static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3932 struct ivar_list64_t il;
3933 struct ivar64_t i;
3934 const char *r;
3935 uint32_t offset, xoffset, left, j;
3936 SectionRef S, xS;
3937 const char *name, *sym_name, *ivar_offset_p;
3938 uint64_t ivar_offset, n_value;
3939
3940 r = get_pointer_64(p, offset, left, S, info);
3941 if (r == nullptr)
3942 return;
3943 memset(&il, '\0', sizeof(struct ivar_list64_t));
3944 if (left < sizeof(struct ivar_list64_t)) {
3945 memcpy(&il, r, left);
3946 outs() << " (ivar_list_t entends past the end of the section)\n";
3947 } else
3948 memcpy(&il, r, sizeof(struct ivar_list64_t));
3949 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3950 swapStruct(il);
3951 outs() << " entsize " << il.entsize << "\n";
3952 outs() << " count " << il.count << "\n";
3953
3954 p += sizeof(struct ivar_list64_t);
3955 offset += sizeof(struct ivar_list64_t);
3956 for (j = 0; j < il.count; j++) {
3957 r = get_pointer_64(p, offset, left, S, info);
3958 if (r == nullptr)
3959 return;
3960 memset(&i, '\0', sizeof(struct ivar64_t));
3961 if (left < sizeof(struct ivar64_t)) {
3962 memcpy(&i, r, left);
3963 outs() << " (ivar_t entends past the end of the section)\n";
3964 } else
3965 memcpy(&i, r, sizeof(struct ivar64_t));
3966 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3967 swapStruct(i);
3968
3969 outs() << "\t\t\t offset ";
3970 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
3971 info, n_value, i.offset);
3972 if (n_value != 0) {
3973 if (info->verbose && sym_name != nullptr)
3974 outs() << sym_name;
3975 else
3976 outs() << format("0x%" PRIx64, n_value);
3977 if (i.offset != 0)
3978 outs() << " + " << format("0x%" PRIx64, i.offset);
3979 } else
3980 outs() << format("0x%" PRIx64, i.offset);
3981 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
3982 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
3983 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
3984 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3985 sys::swapByteOrder(ivar_offset);
3986 outs() << " " << ivar_offset << "\n";
3987 } else
3988 outs() << "\n";
3989
3990 outs() << "\t\t\t name ";
3991 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
3992 n_value, i.name);
3993 if (n_value != 0) {
3994 if (info->verbose && sym_name != nullptr)
3995 outs() << sym_name;
3996 else
3997 outs() << format("0x%" PRIx64, n_value);
3998 if (i.name != 0)
3999 outs() << " + " << format("0x%" PRIx64, i.name);
4000 } else
4001 outs() << format("0x%" PRIx64, i.name);
4002 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4003 if (name != nullptr)
4004 outs() << format(" %.*s", left, name);
4005 outs() << "\n";
4006
4007 outs() << "\t\t\t type ";
4008 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4009 n_value, i.name);
4010 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4011 if (n_value != 0) {
4012 if (info->verbose && sym_name != nullptr)
4013 outs() << sym_name;
4014 else
4015 outs() << format("0x%" PRIx64, n_value);
4016 if (i.type != 0)
4017 outs() << " + " << format("0x%" PRIx64, i.type);
4018 } else
4019 outs() << format("0x%" PRIx64, i.type);
4020 if (name != nullptr)
4021 outs() << format(" %.*s", left, name);
4022 outs() << "\n";
4023
4024 outs() << "\t\t\talignment " << i.alignment << "\n";
4025 outs() << "\t\t\t size " << i.size << "\n";
4026
4027 p += sizeof(struct ivar64_t);
4028 offset += sizeof(struct ivar64_t);
4029 }
4030}
4031
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004032static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4033 struct ivar_list32_t il;
4034 struct ivar32_t i;
4035 const char *r;
4036 uint32_t offset, xoffset, left, j;
4037 SectionRef S, xS;
4038 const char *name, *ivar_offset_p;
4039 uint32_t ivar_offset;
4040
4041 r = get_pointer_32(p, offset, left, S, info);
4042 if (r == nullptr)
4043 return;
4044 memset(&il, '\0', sizeof(struct ivar_list32_t));
4045 if (left < sizeof(struct ivar_list32_t)) {
4046 memcpy(&il, r, left);
4047 outs() << " (ivar_list_t entends past the end of the section)\n";
4048 } else
4049 memcpy(&il, r, sizeof(struct ivar_list32_t));
4050 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4051 swapStruct(il);
4052 outs() << " entsize " << il.entsize << "\n";
4053 outs() << " count " << il.count << "\n";
4054
4055 p += sizeof(struct ivar_list32_t);
4056 offset += sizeof(struct ivar_list32_t);
4057 for (j = 0; j < il.count; j++) {
4058 r = get_pointer_32(p, offset, left, S, info);
4059 if (r == nullptr)
4060 return;
4061 memset(&i, '\0', sizeof(struct ivar32_t));
4062 if (left < sizeof(struct ivar32_t)) {
4063 memcpy(&i, r, left);
4064 outs() << " (ivar_t entends past the end of the section)\n";
4065 } else
4066 memcpy(&i, r, sizeof(struct ivar32_t));
4067 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4068 swapStruct(i);
4069
4070 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4071 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4072 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4073 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4074 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4075 sys::swapByteOrder(ivar_offset);
4076 outs() << " " << ivar_offset << "\n";
4077 } else
4078 outs() << "\n";
4079
4080 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4081 name = get_pointer_32(i.name, xoffset, left, xS, info);
4082 if (name != nullptr)
4083 outs() << format(" %.*s", left, name);
4084 outs() << "\n";
4085
4086 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4087 name = get_pointer_32(i.type, xoffset, left, xS, info);
4088 if (name != nullptr)
4089 outs() << format(" %.*s", left, name);
4090 outs() << "\n";
4091
4092 outs() << "\t\t\talignment " << i.alignment << "\n";
4093 outs() << "\t\t\t size " << i.size << "\n";
4094
4095 p += sizeof(struct ivar32_t);
4096 offset += sizeof(struct ivar32_t);
4097 }
4098}
4099
Kevin Enderby0fc11822015-04-01 20:57:01 +00004100static void print_objc_property_list64(uint64_t p,
4101 struct DisassembleInfo *info) {
4102 struct objc_property_list64 opl;
4103 struct objc_property64 op;
4104 const char *r;
4105 uint32_t offset, xoffset, left, j;
4106 SectionRef S, xS;
4107 const char *name, *sym_name;
4108 uint64_t n_value;
4109
4110 r = get_pointer_64(p, offset, left, S, info);
4111 if (r == nullptr)
4112 return;
4113 memset(&opl, '\0', sizeof(struct objc_property_list64));
4114 if (left < sizeof(struct objc_property_list64)) {
4115 memcpy(&opl, r, left);
4116 outs() << " (objc_property_list entends past the end of the section)\n";
4117 } else
4118 memcpy(&opl, r, sizeof(struct objc_property_list64));
4119 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4120 swapStruct(opl);
4121 outs() << " entsize " << opl.entsize << "\n";
4122 outs() << " count " << opl.count << "\n";
4123
4124 p += sizeof(struct objc_property_list64);
4125 offset += sizeof(struct objc_property_list64);
4126 for (j = 0; j < opl.count; j++) {
4127 r = get_pointer_64(p, offset, left, S, info);
4128 if (r == nullptr)
4129 return;
4130 memset(&op, '\0', sizeof(struct objc_property64));
4131 if (left < sizeof(struct objc_property64)) {
4132 memcpy(&op, r, left);
4133 outs() << " (objc_property entends past the end of the section)\n";
4134 } else
4135 memcpy(&op, r, sizeof(struct objc_property64));
4136 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4137 swapStruct(op);
4138
4139 outs() << "\t\t\t name ";
4140 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4141 info, n_value, op.name);
4142 if (n_value != 0) {
4143 if (info->verbose && sym_name != nullptr)
4144 outs() << sym_name;
4145 else
4146 outs() << format("0x%" PRIx64, n_value);
4147 if (op.name != 0)
4148 outs() << " + " << format("0x%" PRIx64, op.name);
4149 } else
4150 outs() << format("0x%" PRIx64, op.name);
4151 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4152 if (name != nullptr)
4153 outs() << format(" %.*s", left, name);
4154 outs() << "\n";
4155
4156 outs() << "\t\t\tattributes ";
4157 sym_name =
4158 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4159 info, n_value, op.attributes);
4160 if (n_value != 0) {
4161 if (info->verbose && sym_name != nullptr)
4162 outs() << sym_name;
4163 else
4164 outs() << format("0x%" PRIx64, n_value);
4165 if (op.attributes != 0)
4166 outs() << " + " << format("0x%" PRIx64, op.attributes);
4167 } else
4168 outs() << format("0x%" PRIx64, op.attributes);
4169 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4170 if (name != nullptr)
4171 outs() << format(" %.*s", left, name);
4172 outs() << "\n";
4173
4174 p += sizeof(struct objc_property64);
4175 offset += sizeof(struct objc_property64);
4176 }
4177}
4178
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004179static void print_objc_property_list32(uint32_t p,
4180 struct DisassembleInfo *info) {
4181 struct objc_property_list32 opl;
4182 struct objc_property32 op;
4183 const char *r;
4184 uint32_t offset, xoffset, left, j;
4185 SectionRef S, xS;
4186 const char *name;
4187
4188 r = get_pointer_32(p, offset, left, S, info);
4189 if (r == nullptr)
4190 return;
4191 memset(&opl, '\0', sizeof(struct objc_property_list32));
4192 if (left < sizeof(struct objc_property_list32)) {
4193 memcpy(&opl, r, left);
4194 outs() << " (objc_property_list entends past the end of the section)\n";
4195 } else
4196 memcpy(&opl, r, sizeof(struct objc_property_list32));
4197 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4198 swapStruct(opl);
4199 outs() << " entsize " << opl.entsize << "\n";
4200 outs() << " count " << opl.count << "\n";
4201
4202 p += sizeof(struct objc_property_list32);
4203 offset += sizeof(struct objc_property_list32);
4204 for (j = 0; j < opl.count; j++) {
4205 r = get_pointer_32(p, offset, left, S, info);
4206 if (r == nullptr)
4207 return;
4208 memset(&op, '\0', sizeof(struct objc_property32));
4209 if (left < sizeof(struct objc_property32)) {
4210 memcpy(&op, r, left);
4211 outs() << " (objc_property entends past the end of the section)\n";
4212 } else
4213 memcpy(&op, r, sizeof(struct objc_property32));
4214 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4215 swapStruct(op);
4216
4217 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4218 name = get_pointer_32(op.name, xoffset, left, xS, info);
4219 if (name != nullptr)
4220 outs() << format(" %.*s", left, name);
4221 outs() << "\n";
4222
4223 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4224 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4225 if (name != nullptr)
4226 outs() << format(" %.*s", left, name);
4227 outs() << "\n";
4228
4229 p += sizeof(struct objc_property32);
4230 offset += sizeof(struct objc_property32);
4231 }
4232}
4233
Kevin Enderby0fc11822015-04-01 20:57:01 +00004234static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4235 bool &is_meta_class) {
4236 struct class_ro64_t cro;
4237 const char *r;
4238 uint32_t offset, xoffset, left;
4239 SectionRef S, xS;
4240 const char *name, *sym_name;
4241 uint64_t n_value;
4242
4243 r = get_pointer_64(p, offset, left, S, info);
4244 if (r == nullptr || left < sizeof(struct class_ro64_t))
4245 return;
4246 memset(&cro, '\0', sizeof(struct class_ro64_t));
4247 if (left < sizeof(struct class_ro64_t)) {
4248 memcpy(&cro, r, left);
4249 outs() << " (class_ro_t entends past the end of the section)\n";
4250 } else
4251 memcpy(&cro, r, sizeof(struct class_ro64_t));
4252 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4253 swapStruct(cro);
4254 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4255 if (cro.flags & RO_META)
4256 outs() << " RO_META";
4257 if (cro.flags & RO_ROOT)
4258 outs() << " RO_ROOT";
4259 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4260 outs() << " RO_HAS_CXX_STRUCTORS";
4261 outs() << "\n";
4262 outs() << " instanceStart " << cro.instanceStart << "\n";
4263 outs() << " instanceSize " << cro.instanceSize << "\n";
4264 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4265 << "\n";
4266 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4267 << "\n";
4268 print_layout_map64(cro.ivarLayout, info);
4269
4270 outs() << " name ";
4271 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4272 info, n_value, cro.name);
4273 if (n_value != 0) {
4274 if (info->verbose && sym_name != nullptr)
4275 outs() << sym_name;
4276 else
4277 outs() << format("0x%" PRIx64, n_value);
4278 if (cro.name != 0)
4279 outs() << " + " << format("0x%" PRIx64, cro.name);
4280 } else
4281 outs() << format("0x%" PRIx64, cro.name);
4282 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4283 if (name != nullptr)
4284 outs() << format(" %.*s", left, name);
4285 outs() << "\n";
4286
4287 outs() << " baseMethods ";
4288 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4289 S, info, n_value, cro.baseMethods);
4290 if (n_value != 0) {
4291 if (info->verbose && sym_name != nullptr)
4292 outs() << sym_name;
4293 else
4294 outs() << format("0x%" PRIx64, n_value);
4295 if (cro.baseMethods != 0)
4296 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4297 } else
4298 outs() << format("0x%" PRIx64, cro.baseMethods);
4299 outs() << " (struct method_list_t *)\n";
4300 if (cro.baseMethods + n_value != 0)
4301 print_method_list64_t(cro.baseMethods + n_value, info, "");
4302
4303 outs() << " baseProtocols ";
4304 sym_name =
4305 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4306 info, n_value, cro.baseProtocols);
4307 if (n_value != 0) {
4308 if (info->verbose && sym_name != nullptr)
4309 outs() << sym_name;
4310 else
4311 outs() << format("0x%" PRIx64, n_value);
4312 if (cro.baseProtocols != 0)
4313 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4314 } else
4315 outs() << format("0x%" PRIx64, cro.baseProtocols);
4316 outs() << "\n";
4317 if (cro.baseProtocols + n_value != 0)
4318 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4319
4320 outs() << " ivars ";
4321 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004322 info, n_value, cro.ivars);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004323 if (n_value != 0) {
4324 if (info->verbose && sym_name != nullptr)
4325 outs() << sym_name;
4326 else
4327 outs() << format("0x%" PRIx64, n_value);
4328 if (cro.ivars != 0)
4329 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4330 } else
4331 outs() << format("0x%" PRIx64, cro.ivars);
4332 outs() << "\n";
4333 if (cro.ivars + n_value != 0)
4334 print_ivar_list64_t(cro.ivars + n_value, info);
4335
4336 outs() << " weakIvarLayout ";
4337 sym_name =
4338 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4339 info, n_value, cro.weakIvarLayout);
4340 if (n_value != 0) {
4341 if (info->verbose && sym_name != nullptr)
4342 outs() << sym_name;
4343 else
4344 outs() << format("0x%" PRIx64, n_value);
4345 if (cro.weakIvarLayout != 0)
4346 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4347 } else
4348 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4349 outs() << "\n";
4350 print_layout_map64(cro.weakIvarLayout + n_value, info);
4351
4352 outs() << " baseProperties ";
4353 sym_name =
4354 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4355 info, n_value, cro.baseProperties);
4356 if (n_value != 0) {
4357 if (info->verbose && sym_name != nullptr)
4358 outs() << sym_name;
4359 else
4360 outs() << format("0x%" PRIx64, n_value);
4361 if (cro.baseProperties != 0)
4362 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4363 } else
4364 outs() << format("0x%" PRIx64, cro.baseProperties);
4365 outs() << "\n";
4366 if (cro.baseProperties + n_value != 0)
4367 print_objc_property_list64(cro.baseProperties + n_value, info);
4368
4369 is_meta_class = (cro.flags & RO_META) ? true : false;
4370}
4371
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004372static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4373 bool &is_meta_class) {
4374 struct class_ro32_t cro;
4375 const char *r;
4376 uint32_t offset, xoffset, left;
4377 SectionRef S, xS;
4378 const char *name;
4379
4380 r = get_pointer_32(p, offset, left, S, info);
4381 if (r == nullptr)
4382 return;
4383 memset(&cro, '\0', sizeof(struct class_ro32_t));
4384 if (left < sizeof(struct class_ro32_t)) {
4385 memcpy(&cro, r, left);
4386 outs() << " (class_ro_t entends past the end of the section)\n";
4387 } else
4388 memcpy(&cro, r, sizeof(struct class_ro32_t));
4389 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4390 swapStruct(cro);
4391 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4392 if (cro.flags & RO_META)
4393 outs() << " RO_META";
4394 if (cro.flags & RO_ROOT)
4395 outs() << " RO_ROOT";
4396 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4397 outs() << " RO_HAS_CXX_STRUCTORS";
4398 outs() << "\n";
4399 outs() << " instanceStart " << cro.instanceStart << "\n";
4400 outs() << " instanceSize " << cro.instanceSize << "\n";
4401 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4402 << "\n";
4403 print_layout_map32(cro.ivarLayout, info);
4404
4405 outs() << " name " << format("0x%" PRIx32, cro.name);
4406 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4407 if (name != nullptr)
4408 outs() << format(" %.*s", left, name);
4409 outs() << "\n";
4410
4411 outs() << " baseMethods "
4412 << format("0x%" PRIx32, cro.baseMethods)
4413 << " (struct method_list_t *)\n";
4414 if (cro.baseMethods != 0)
4415 print_method_list32_t(cro.baseMethods, info, "");
4416
4417 outs() << " baseProtocols "
4418 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4419 if (cro.baseProtocols != 0)
4420 print_protocol_list32_t(cro.baseProtocols, info);
4421 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4422 << "\n";
4423 if (cro.ivars != 0)
4424 print_ivar_list32_t(cro.ivars, info);
4425 outs() << " weakIvarLayout "
4426 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4427 print_layout_map32(cro.weakIvarLayout, info);
4428 outs() << " baseProperties "
4429 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4430 if (cro.baseProperties != 0)
4431 print_objc_property_list32(cro.baseProperties, info);
4432 is_meta_class = (cro.flags & RO_META) ? true : false;
4433}
4434
Kevin Enderby0fc11822015-04-01 20:57:01 +00004435static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4436 struct class64_t c;
4437 const char *r;
4438 uint32_t offset, left;
4439 SectionRef S;
4440 const char *name;
4441 uint64_t isa_n_value, n_value;
4442
4443 r = get_pointer_64(p, offset, left, S, info);
4444 if (r == nullptr || left < sizeof(struct class64_t))
4445 return;
4446 memset(&c, '\0', sizeof(struct class64_t));
4447 if (left < sizeof(struct class64_t)) {
4448 memcpy(&c, r, left);
4449 outs() << " (class_t entends past the end of the section)\n";
4450 } else
4451 memcpy(&c, r, sizeof(struct class64_t));
4452 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4453 swapStruct(c);
4454
4455 outs() << " isa " << format("0x%" PRIx64, c.isa);
4456 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4457 isa_n_value, c.isa);
4458 if (name != nullptr)
4459 outs() << " " << name;
4460 outs() << "\n";
4461
4462 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4463 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4464 n_value, c.superclass);
4465 if (name != nullptr)
4466 outs() << " " << name;
4467 outs() << "\n";
4468
4469 outs() << " cache " << format("0x%" PRIx64, c.cache);
4470 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4471 n_value, c.cache);
4472 if (name != nullptr)
4473 outs() << " " << name;
4474 outs() << "\n";
4475
4476 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4477 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4478 n_value, c.vtable);
4479 if (name != nullptr)
4480 outs() << " " << name;
4481 outs() << "\n";
4482
4483 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4484 n_value, c.data);
4485 outs() << " data ";
4486 if (n_value != 0) {
4487 if (info->verbose && name != nullptr)
4488 outs() << name;
4489 else
4490 outs() << format("0x%" PRIx64, n_value);
4491 if (c.data != 0)
4492 outs() << " + " << format("0x%" PRIx64, c.data);
4493 } else
4494 outs() << format("0x%" PRIx64, c.data);
4495 outs() << " (struct class_ro_t *)";
4496
4497 // This is a Swift class if some of the low bits of the pointer are set.
4498 if ((c.data + n_value) & 0x7)
4499 outs() << " Swift class";
4500 outs() << "\n";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004501 bool is_meta_class;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004502 print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class);
4503
4504 if (is_meta_class == false) {
4505 outs() << "Meta Class\n";
4506 print_class64_t(c.isa + isa_n_value, info);
4507 }
4508}
4509
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004510static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4511 struct class32_t c;
4512 const char *r;
4513 uint32_t offset, left;
4514 SectionRef S;
4515 const char *name;
4516
4517 r = get_pointer_32(p, offset, left, S, info);
4518 if (r == nullptr)
4519 return;
4520 memset(&c, '\0', sizeof(struct class32_t));
4521 if (left < sizeof(struct class32_t)) {
4522 memcpy(&c, r, left);
4523 outs() << " (class_t entends past the end of the section)\n";
4524 } else
4525 memcpy(&c, r, sizeof(struct class32_t));
4526 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4527 swapStruct(c);
4528
4529 outs() << " isa " << format("0x%" PRIx32, c.isa);
4530 name =
4531 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4532 if (name != nullptr)
4533 outs() << " " << name;
4534 outs() << "\n";
4535
4536 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4537 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4538 c.superclass);
4539 if (name != nullptr)
4540 outs() << " " << name;
4541 outs() << "\n";
4542
4543 outs() << " cache " << format("0x%" PRIx32, c.cache);
4544 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4545 c.cache);
4546 if (name != nullptr)
4547 outs() << " " << name;
4548 outs() << "\n";
4549
4550 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4551 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4552 c.vtable);
4553 if (name != nullptr)
4554 outs() << " " << name;
4555 outs() << "\n";
4556
4557 name =
4558 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4559 outs() << " data " << format("0x%" PRIx32, c.data)
4560 << " (struct class_ro_t *)";
4561
4562 // This is a Swift class if some of the low bits of the pointer are set.
4563 if (c.data & 0x3)
4564 outs() << " Swift class";
4565 outs() << "\n";
4566 bool is_meta_class;
4567 print_class_ro32_t(c.data & ~0x3, info, is_meta_class);
4568
4569 if (is_meta_class == false) {
4570 outs() << "Meta Class\n";
4571 print_class32_t(c.isa, info);
4572 }
4573}
4574
Kevin Enderby846c0002015-04-16 17:19:59 +00004575static void print_objc_class_t(struct objc_class_t *objc_class,
4576 struct DisassembleInfo *info) {
4577 uint32_t offset, left, xleft;
4578 const char *name, *p, *ivar_list;
4579 SectionRef S;
4580 int32_t i;
4581 struct objc_ivar_list_t objc_ivar_list;
4582 struct objc_ivar_t ivar;
4583
4584 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4585 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4586 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4587 if (name != nullptr)
4588 outs() << format(" %.*s", left, name);
4589 else
4590 outs() << " (not in an __OBJC section)";
4591 }
4592 outs() << "\n";
4593
4594 outs() << "\t super_class "
4595 << format("0x%08" PRIx32, objc_class->super_class);
4596 if (info->verbose) {
4597 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4598 if (name != nullptr)
4599 outs() << format(" %.*s", left, name);
4600 else
4601 outs() << " (not in an __OBJC section)";
4602 }
4603 outs() << "\n";
4604
4605 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4606 if (info->verbose) {
4607 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4608 if (name != nullptr)
4609 outs() << format(" %.*s", left, name);
4610 else
4611 outs() << " (not in an __OBJC section)";
4612 }
4613 outs() << "\n";
4614
4615 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4616 << "\n";
4617
4618 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4619 if (info->verbose) {
4620 if (CLS_GETINFO(objc_class, CLS_CLASS))
4621 outs() << " CLS_CLASS";
4622 else if (CLS_GETINFO(objc_class, CLS_META))
4623 outs() << " CLS_META";
4624 }
4625 outs() << "\n";
4626
4627 outs() << "\t instance_size "
4628 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4629
4630 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4631 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4632 if (p != nullptr) {
4633 if (left > sizeof(struct objc_ivar_list_t)) {
4634 outs() << "\n";
4635 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4636 } else {
4637 outs() << " (entends past the end of the section)\n";
4638 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4639 memcpy(&objc_ivar_list, p, left);
4640 }
4641 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4642 swapStruct(objc_ivar_list);
4643 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4644 ivar_list = p + sizeof(struct objc_ivar_list_t);
4645 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4646 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4647 outs() << "\t\t remaining ivar's extend past the of the section\n";
4648 break;
4649 }
4650 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4651 sizeof(struct objc_ivar_t));
4652 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4653 swapStruct(ivar);
4654
4655 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4656 if (info->verbose) {
4657 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4658 if (name != nullptr)
4659 outs() << format(" %.*s", xleft, name);
4660 else
4661 outs() << " (not in an __OBJC section)";
4662 }
4663 outs() << "\n";
4664
4665 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4666 if (info->verbose) {
4667 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4668 if (name != nullptr)
4669 outs() << format(" %.*s", xleft, name);
4670 else
4671 outs() << " (not in an __OBJC section)";
4672 }
4673 outs() << "\n";
4674
4675 outs() << "\t\t ivar_offset "
4676 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4677 }
4678 } else {
4679 outs() << " (not in an __OBJC section)\n";
4680 }
4681
4682 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4683 if (print_method_list(objc_class->methodLists, info))
4684 outs() << " (not in an __OBJC section)\n";
4685
4686 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4687 << "\n";
4688
4689 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4690 if (print_protocol_list(objc_class->protocols, 16, info))
4691 outs() << " (not in an __OBJC section)\n";
4692}
4693
4694static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4695 struct DisassembleInfo *info) {
4696 uint32_t offset, left;
4697 const char *name;
4698 SectionRef S;
4699
4700 outs() << "\t category name "
4701 << format("0x%08" PRIx32, objc_category->category_name);
4702 if (info->verbose) {
4703 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4704 true);
4705 if (name != nullptr)
4706 outs() << format(" %.*s", left, name);
4707 else
4708 outs() << " (not in an __OBJC section)";
4709 }
4710 outs() << "\n";
4711
4712 outs() << "\t\t class name "
4713 << format("0x%08" PRIx32, objc_category->class_name);
4714 if (info->verbose) {
4715 name =
4716 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4717 if (name != nullptr)
4718 outs() << format(" %.*s", left, name);
4719 else
4720 outs() << " (not in an __OBJC section)";
4721 }
4722 outs() << "\n";
4723
4724 outs() << "\t instance methods "
4725 << format("0x%08" PRIx32, objc_category->instance_methods);
4726 if (print_method_list(objc_category->instance_methods, info))
4727 outs() << " (not in an __OBJC section)\n";
4728
4729 outs() << "\t class methods "
4730 << format("0x%08" PRIx32, objc_category->class_methods);
4731 if (print_method_list(objc_category->class_methods, info))
4732 outs() << " (not in an __OBJC section)\n";
4733}
4734
Kevin Enderby0fc11822015-04-01 20:57:01 +00004735static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4736 struct category64_t c;
4737 const char *r;
4738 uint32_t offset, xoffset, left;
4739 SectionRef S, xS;
4740 const char *name, *sym_name;
4741 uint64_t n_value;
4742
4743 r = get_pointer_64(p, offset, left, S, info);
4744 if (r == nullptr)
4745 return;
4746 memset(&c, '\0', sizeof(struct category64_t));
4747 if (left < sizeof(struct category64_t)) {
4748 memcpy(&c, r, left);
4749 outs() << " (category_t entends past the end of the section)\n";
4750 } else
4751 memcpy(&c, r, sizeof(struct category64_t));
4752 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4753 swapStruct(c);
4754
4755 outs() << " name ";
4756 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4757 info, n_value, c.name);
4758 if (n_value != 0) {
4759 if (info->verbose && sym_name != nullptr)
4760 outs() << sym_name;
4761 else
4762 outs() << format("0x%" PRIx64, n_value);
4763 if (c.name != 0)
4764 outs() << " + " << format("0x%" PRIx64, c.name);
4765 } else
4766 outs() << format("0x%" PRIx64, c.name);
4767 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4768 if (name != nullptr)
4769 outs() << format(" %.*s", left, name);
4770 outs() << "\n";
4771
4772 outs() << " cls ";
4773 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4774 n_value, c.cls);
4775 if (n_value != 0) {
4776 if (info->verbose && sym_name != nullptr)
4777 outs() << sym_name;
4778 else
4779 outs() << format("0x%" PRIx64, n_value);
4780 if (c.cls != 0)
4781 outs() << " + " << format("0x%" PRIx64, c.cls);
4782 } else
4783 outs() << format("0x%" PRIx64, c.cls);
4784 outs() << "\n";
4785 if (c.cls + n_value != 0)
4786 print_class64_t(c.cls + n_value, info);
4787
4788 outs() << " instanceMethods ";
4789 sym_name =
4790 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4791 info, n_value, c.instanceMethods);
4792 if (n_value != 0) {
4793 if (info->verbose && sym_name != nullptr)
4794 outs() << sym_name;
4795 else
4796 outs() << format("0x%" PRIx64, n_value);
4797 if (c.instanceMethods != 0)
4798 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4799 } else
4800 outs() << format("0x%" PRIx64, c.instanceMethods);
4801 outs() << "\n";
4802 if (c.instanceMethods + n_value != 0)
4803 print_method_list64_t(c.instanceMethods + n_value, info, "");
4804
4805 outs() << " classMethods ";
4806 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4807 S, info, n_value, c.classMethods);
4808 if (n_value != 0) {
4809 if (info->verbose && sym_name != nullptr)
4810 outs() << sym_name;
4811 else
4812 outs() << format("0x%" PRIx64, n_value);
4813 if (c.classMethods != 0)
4814 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4815 } else
4816 outs() << format("0x%" PRIx64, c.classMethods);
4817 outs() << "\n";
4818 if (c.classMethods + n_value != 0)
4819 print_method_list64_t(c.classMethods + n_value, info, "");
4820
4821 outs() << " protocols ";
4822 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4823 info, n_value, c.protocols);
4824 if (n_value != 0) {
4825 if (info->verbose && sym_name != nullptr)
4826 outs() << sym_name;
4827 else
4828 outs() << format("0x%" PRIx64, n_value);
4829 if (c.protocols != 0)
4830 outs() << " + " << format("0x%" PRIx64, c.protocols);
4831 } else
4832 outs() << format("0x%" PRIx64, c.protocols);
4833 outs() << "\n";
4834 if (c.protocols + n_value != 0)
4835 print_protocol_list64_t(c.protocols + n_value, info);
4836
4837 outs() << "instanceProperties ";
4838 sym_name =
4839 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4840 S, info, n_value, c.instanceProperties);
4841 if (n_value != 0) {
4842 if (info->verbose && sym_name != nullptr)
4843 outs() << sym_name;
4844 else
4845 outs() << format("0x%" PRIx64, n_value);
4846 if (c.instanceProperties != 0)
4847 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4848 } else
4849 outs() << format("0x%" PRIx64, c.instanceProperties);
4850 outs() << "\n";
4851 if (c.instanceProperties + n_value != 0)
4852 print_objc_property_list64(c.instanceProperties + n_value, info);
4853}
4854
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004855static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4856 struct category32_t c;
4857 const char *r;
4858 uint32_t offset, left;
4859 SectionRef S, xS;
4860 const char *name;
4861
4862 r = get_pointer_32(p, offset, left, S, info);
4863 if (r == nullptr)
4864 return;
4865 memset(&c, '\0', sizeof(struct category32_t));
4866 if (left < sizeof(struct category32_t)) {
4867 memcpy(&c, r, left);
4868 outs() << " (category_t entends past the end of the section)\n";
4869 } else
4870 memcpy(&c, r, sizeof(struct category32_t));
4871 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4872 swapStruct(c);
4873
4874 outs() << " name " << format("0x%" PRIx32, c.name);
4875 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4876 c.name);
4877 if (name != NULL)
4878 outs() << " " << name;
4879 outs() << "\n";
4880
4881 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
4882 if (c.cls != 0)
4883 print_class32_t(c.cls, info);
4884 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4885 << "\n";
4886 if (c.instanceMethods != 0)
4887 print_method_list32_t(c.instanceMethods, info, "");
4888 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
4889 << "\n";
4890 if (c.classMethods != 0)
4891 print_method_list32_t(c.classMethods, info, "");
4892 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
4893 if (c.protocols != 0)
4894 print_protocol_list32_t(c.protocols, info);
4895 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
4896 << "\n";
4897 if (c.instanceProperties != 0)
4898 print_objc_property_list32(c.instanceProperties, info);
4899}
4900
Kevin Enderby0fc11822015-04-01 20:57:01 +00004901static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
4902 uint32_t i, left, offset, xoffset;
4903 uint64_t p, n_value;
4904 struct message_ref64 mr;
4905 const char *name, *sym_name;
4906 const char *r;
4907 SectionRef xS;
4908
4909 if (S == SectionRef())
4910 return;
4911
4912 StringRef SectName;
4913 S.getName(SectName);
4914 DataRefImpl Ref = S.getRawDataRefImpl();
4915 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4916 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4917 offset = 0;
4918 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4919 p = S.getAddress() + i;
4920 r = get_pointer_64(p, offset, left, S, info);
4921 if (r == nullptr)
4922 return;
4923 memset(&mr, '\0', sizeof(struct message_ref64));
4924 if (left < sizeof(struct message_ref64)) {
4925 memcpy(&mr, r, left);
4926 outs() << " (message_ref entends past the end of the section)\n";
4927 } else
4928 memcpy(&mr, r, sizeof(struct message_ref64));
4929 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4930 swapStruct(mr);
4931
4932 outs() << " imp ";
4933 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
4934 n_value, mr.imp);
4935 if (n_value != 0) {
4936 outs() << format("0x%" PRIx64, n_value) << " ";
4937 if (mr.imp != 0)
4938 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
4939 } else
4940 outs() << format("0x%" PRIx64, mr.imp) << " ";
4941 if (name != nullptr)
4942 outs() << " " << name;
4943 outs() << "\n";
4944
4945 outs() << " sel ";
4946 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
4947 info, n_value, mr.sel);
4948 if (n_value != 0) {
4949 if (info->verbose && sym_name != nullptr)
4950 outs() << sym_name;
4951 else
4952 outs() << format("0x%" PRIx64, n_value);
4953 if (mr.sel != 0)
4954 outs() << " + " << format("0x%" PRIx64, mr.sel);
4955 } else
4956 outs() << format("0x%" PRIx64, mr.sel);
4957 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
4958 if (name != nullptr)
4959 outs() << format(" %.*s", left, name);
4960 outs() << "\n";
4961
4962 offset += sizeof(struct message_ref64);
4963 }
4964}
4965
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004966static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
4967 uint32_t i, left, offset, xoffset, p;
4968 struct message_ref32 mr;
4969 const char *name, *r;
4970 SectionRef xS;
4971
4972 if (S == SectionRef())
4973 return;
4974
4975 StringRef SectName;
4976 S.getName(SectName);
4977 DataRefImpl Ref = S.getRawDataRefImpl();
4978 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4979 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4980 offset = 0;
4981 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4982 p = S.getAddress() + i;
4983 r = get_pointer_32(p, offset, left, S, info);
4984 if (r == nullptr)
4985 return;
4986 memset(&mr, '\0', sizeof(struct message_ref32));
4987 if (left < sizeof(struct message_ref32)) {
4988 memcpy(&mr, r, left);
4989 outs() << " (message_ref entends past the end of the section)\n";
4990 } else
4991 memcpy(&mr, r, sizeof(struct message_ref32));
4992 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4993 swapStruct(mr);
4994
4995 outs() << " imp " << format("0x%" PRIx32, mr.imp);
4996 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
4997 mr.imp);
4998 if (name != nullptr)
4999 outs() << " " << name;
5000 outs() << "\n";
5001
5002 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5003 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5004 if (name != nullptr)
5005 outs() << " " << name;
5006 outs() << "\n";
5007
5008 offset += sizeof(struct message_ref32);
5009 }
5010}
5011
Kevin Enderby0fc11822015-04-01 20:57:01 +00005012static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5013 uint32_t left, offset, swift_version;
5014 uint64_t p;
5015 struct objc_image_info64 o;
5016 const char *r;
5017
5018 StringRef SectName;
5019 S.getName(SectName);
5020 DataRefImpl Ref = S.getRawDataRefImpl();
5021 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5022 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5023 p = S.getAddress();
5024 r = get_pointer_64(p, offset, left, S, info);
5025 if (r == nullptr)
5026 return;
5027 memset(&o, '\0', sizeof(struct objc_image_info64));
5028 if (left < sizeof(struct objc_image_info64)) {
5029 memcpy(&o, r, left);
5030 outs() << " (objc_image_info entends past the end of the section)\n";
5031 } else
5032 memcpy(&o, r, sizeof(struct objc_image_info64));
5033 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5034 swapStruct(o);
5035 outs() << " version " << o.version << "\n";
5036 outs() << " flags " << format("0x%" PRIx32, o.flags);
5037 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5038 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5039 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5040 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5041 swift_version = (o.flags >> 8) & 0xff;
5042 if (swift_version != 0) {
5043 if (swift_version == 1)
5044 outs() << " Swift 1.0";
5045 else if (swift_version == 2)
5046 outs() << " Swift 1.1";
5047 else
5048 outs() << " unknown future Swift version (" << swift_version << ")";
5049 }
5050 outs() << "\n";
5051}
5052
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005053static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5054 uint32_t left, offset, swift_version, p;
5055 struct objc_image_info32 o;
5056 const char *r;
5057
5058 StringRef SectName;
5059 S.getName(SectName);
5060 DataRefImpl Ref = S.getRawDataRefImpl();
5061 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5062 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5063 p = S.getAddress();
5064 r = get_pointer_32(p, offset, left, S, info);
5065 if (r == nullptr)
5066 return;
5067 memset(&o, '\0', sizeof(struct objc_image_info32));
5068 if (left < sizeof(struct objc_image_info32)) {
5069 memcpy(&o, r, left);
5070 outs() << " (objc_image_info entends past the end of the section)\n";
5071 } else
5072 memcpy(&o, r, sizeof(struct objc_image_info32));
5073 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5074 swapStruct(o);
5075 outs() << " version " << o.version << "\n";
5076 outs() << " flags " << format("0x%" PRIx32, o.flags);
5077 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5078 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5079 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5080 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5081 swift_version = (o.flags >> 8) & 0xff;
5082 if (swift_version != 0) {
5083 if (swift_version == 1)
5084 outs() << " Swift 1.0";
5085 else if (swift_version == 2)
5086 outs() << " Swift 1.1";
5087 else
5088 outs() << " unknown future Swift version (" << swift_version << ")";
5089 }
5090 outs() << "\n";
5091}
5092
Kevin Enderby846c0002015-04-16 17:19:59 +00005093static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5094 uint32_t left, offset, p;
5095 struct imageInfo_t o;
5096 const char *r;
5097
5098 StringRef SectName;
5099 S.getName(SectName);
5100 DataRefImpl Ref = S.getRawDataRefImpl();
5101 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5102 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5103 p = S.getAddress();
5104 r = get_pointer_32(p, offset, left, S, info);
5105 if (r == nullptr)
5106 return;
5107 memset(&o, '\0', sizeof(struct imageInfo_t));
5108 if (left < sizeof(struct imageInfo_t)) {
5109 memcpy(&o, r, left);
5110 outs() << " (imageInfo entends past the end of the section)\n";
5111 } else
5112 memcpy(&o, r, sizeof(struct imageInfo_t));
5113 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5114 swapStruct(o);
5115 outs() << " version " << o.version << "\n";
5116 outs() << " flags " << format("0x%" PRIx32, o.flags);
5117 if (o.flags & 0x1)
5118 outs() << " F&C";
5119 if (o.flags & 0x2)
5120 outs() << " GC";
5121 if (o.flags & 0x4)
5122 outs() << " GC-only";
5123 else
5124 outs() << " RR";
5125 outs() << "\n";
5126}
5127
Kevin Enderby0fc11822015-04-01 20:57:01 +00005128static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5129 SymbolAddressMap AddrMap;
5130 if (verbose)
5131 CreateSymbolAddressMap(O, &AddrMap);
5132
5133 std::vector<SectionRef> Sections;
5134 for (const SectionRef &Section : O->sections()) {
5135 StringRef SectName;
5136 Section.getName(SectName);
5137 Sections.push_back(Section);
5138 }
5139
5140 struct DisassembleInfo info;
5141 // Set up the block of info used by the Symbolizer call backs.
5142 info.verbose = verbose;
5143 info.O = O;
5144 info.AddrMap = &AddrMap;
5145 info.Sections = &Sections;
5146 info.class_name = nullptr;
5147 info.selector_name = nullptr;
5148 info.method = nullptr;
5149 info.demangled_name = nullptr;
5150 info.bindtable = nullptr;
5151 info.adrp_addr = 0;
5152 info.adrp_inst = 0;
5153
5154 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5155 if (CL != SectionRef()) {
5156 info.S = CL;
5157 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5158 } else {
5159 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5160 info.S = CL;
5161 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5162 }
5163
5164 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5165 if (CR != SectionRef()) {
5166 info.S = CR;
5167 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5168 } else {
5169 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5170 info.S = CR;
5171 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5172 }
5173
5174 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5175 if (SR != SectionRef()) {
5176 info.S = SR;
5177 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5178 } else {
5179 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5180 info.S = SR;
5181 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5182 }
5183
5184 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5185 if (CA != SectionRef()) {
5186 info.S = CA;
5187 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5188 } else {
5189 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5190 info.S = CA;
5191 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5192 }
5193
5194 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5195 if (PL != SectionRef()) {
5196 info.S = PL;
5197 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5198 } else {
5199 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5200 info.S = PL;
5201 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5202 }
5203
5204 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5205 if (MR != SectionRef()) {
5206 info.S = MR;
5207 print_message_refs64(MR, &info);
5208 } else {
5209 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5210 info.S = MR;
5211 print_message_refs64(MR, &info);
5212 }
5213
5214 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5215 if (II != SectionRef()) {
5216 info.S = II;
5217 print_image_info64(II, &info);
5218 } else {
5219 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5220 info.S = II;
5221 print_image_info64(II, &info);
5222 }
Kevin Enderby0bc6ed42015-04-01 21:50:45 +00005223
5224 if (info.bindtable != nullptr)
5225 delete info.bindtable;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005226}
5227
5228static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005229 SymbolAddressMap AddrMap;
5230 if (verbose)
5231 CreateSymbolAddressMap(O, &AddrMap);
5232
5233 std::vector<SectionRef> Sections;
5234 for (const SectionRef &Section : O->sections()) {
5235 StringRef SectName;
5236 Section.getName(SectName);
5237 Sections.push_back(Section);
5238 }
5239
5240 struct DisassembleInfo info;
5241 // Set up the block of info used by the Symbolizer call backs.
5242 info.verbose = verbose;
5243 info.O = O;
5244 info.AddrMap = &AddrMap;
5245 info.Sections = &Sections;
5246 info.class_name = nullptr;
5247 info.selector_name = nullptr;
5248 info.method = nullptr;
5249 info.demangled_name = nullptr;
5250 info.bindtable = nullptr;
5251 info.adrp_addr = 0;
5252 info.adrp_inst = 0;
5253
5254 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5255 if (CL != SectionRef()) {
5256 info.S = CL;
5257 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5258 } else {
5259 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5260 info.S = CL;
5261 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5262 }
5263
5264 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5265 if (CR != SectionRef()) {
5266 info.S = CR;
5267 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5268 } else {
5269 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5270 info.S = CR;
5271 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5272 }
5273
5274 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5275 if (SR != SectionRef()) {
5276 info.S = SR;
5277 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5278 } else {
5279 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5280 info.S = SR;
5281 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5282 }
5283
5284 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5285 if (CA != SectionRef()) {
5286 info.S = CA;
5287 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5288 } else {
5289 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5290 info.S = CA;
5291 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5292 }
5293
5294 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5295 if (PL != SectionRef()) {
5296 info.S = PL;
5297 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5298 } else {
5299 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5300 info.S = PL;
5301 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5302 }
5303
5304 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5305 if (MR != SectionRef()) {
5306 info.S = MR;
5307 print_message_refs32(MR, &info);
5308 } else {
5309 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5310 info.S = MR;
5311 print_message_refs32(MR, &info);
5312 }
5313
5314 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5315 if (II != SectionRef()) {
5316 info.S = II;
5317 print_image_info32(II, &info);
5318 } else {
5319 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5320 info.S = II;
5321 print_image_info32(II, &info);
5322 }
Kevin Enderby0fc11822015-04-01 20:57:01 +00005323}
5324
5325static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby846c0002015-04-16 17:19:59 +00005326 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5327 const char *r, *name, *defs;
5328 struct objc_module_t module;
5329 SectionRef S, xS;
5330 struct objc_symtab_t symtab;
5331 struct objc_class_t objc_class;
5332 struct objc_category_t objc_category;
5333
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005334 outs() << "Objective-C segment\n";
Kevin Enderby846c0002015-04-16 17:19:59 +00005335 S = get_section(O, "__OBJC", "__module_info");
5336 if (S == SectionRef())
5337 return false;
5338
5339 SymbolAddressMap AddrMap;
5340 if (verbose)
5341 CreateSymbolAddressMap(O, &AddrMap);
5342
5343 std::vector<SectionRef> Sections;
5344 for (const SectionRef &Section : O->sections()) {
5345 StringRef SectName;
5346 Section.getName(SectName);
5347 Sections.push_back(Section);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005348 }
Kevin Enderby846c0002015-04-16 17:19:59 +00005349
5350 struct DisassembleInfo info;
5351 // Set up the block of info used by the Symbolizer call backs.
5352 info.verbose = verbose;
5353 info.O = O;
5354 info.AddrMap = &AddrMap;
5355 info.Sections = &Sections;
5356 info.class_name = nullptr;
5357 info.selector_name = nullptr;
5358 info.method = nullptr;
5359 info.demangled_name = nullptr;
5360 info.bindtable = nullptr;
5361 info.adrp_addr = 0;
5362 info.adrp_inst = 0;
5363
5364 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5365 p = S.getAddress() + i;
5366 r = get_pointer_32(p, offset, left, S, &info, true);
5367 if (r == nullptr)
5368 return true;
5369 memset(&module, '\0', sizeof(struct objc_module_t));
5370 if (left < sizeof(struct objc_module_t)) {
5371 memcpy(&module, r, left);
5372 outs() << " (module extends past end of __module_info section)\n";
5373 } else
5374 memcpy(&module, r, sizeof(struct objc_module_t));
5375 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5376 swapStruct(module);
5377
5378 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5379 outs() << " version " << module.version << "\n";
5380 outs() << " size " << module.size << "\n";
5381 outs() << " name ";
5382 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5383 if (name != nullptr)
5384 outs() << format("%.*s", left, name);
5385 else
5386 outs() << format("0x%08" PRIx32, module.name)
5387 << "(not in an __OBJC section)";
5388 outs() << "\n";
5389
5390 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5391 if (module.symtab == 0 || r == nullptr) {
5392 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5393 << " (not in an __OBJC section)\n";
5394 continue;
5395 }
5396 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5397 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5398 defs_left = 0;
5399 defs = nullptr;
5400 if (left < sizeof(struct objc_symtab_t)) {
5401 memcpy(&symtab, r, left);
5402 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5403 } else {
5404 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5405 if (left > sizeof(struct objc_symtab_t)) {
5406 defs_left = left - sizeof(struct objc_symtab_t);
5407 defs = r + sizeof(struct objc_symtab_t);
5408 }
5409 }
5410 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5411 swapStruct(symtab);
5412
5413 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5414 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5415 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5416 if (r == nullptr)
5417 outs() << " (not in an __OBJC section)";
5418 outs() << "\n";
5419 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5420 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5421 if (symtab.cls_def_cnt > 0)
5422 outs() << "\tClass Definitions\n";
5423 for (j = 0; j < symtab.cls_def_cnt; j++) {
5424 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5425 outs() << "\t(remaining class defs entries entends past the end of the "
5426 << "section)\n";
5427 break;
5428 }
5429 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5430 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5431 sys::swapByteOrder(def);
5432
5433 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5434 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5435 if (r != nullptr) {
5436 if (left > sizeof(struct objc_class_t)) {
5437 outs() << "\n";
5438 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5439 } else {
5440 outs() << " (entends past the end of the section)\n";
5441 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5442 memcpy(&objc_class, r, left);
5443 }
5444 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5445 swapStruct(objc_class);
5446 print_objc_class_t(&objc_class, &info);
5447 } else {
5448 outs() << "(not in an __OBJC section)\n";
5449 }
5450
5451 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5452 outs() << "\tMeta Class";
5453 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5454 if (r != nullptr) {
5455 if (left > sizeof(struct objc_class_t)) {
5456 outs() << "\n";
5457 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5458 } else {
5459 outs() << " (entends past the end of the section)\n";
5460 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5461 memcpy(&objc_class, r, left);
5462 }
5463 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5464 swapStruct(objc_class);
5465 print_objc_class_t(&objc_class, &info);
5466 } else {
5467 outs() << "(not in an __OBJC section)\n";
5468 }
5469 }
5470 }
5471 if (symtab.cat_def_cnt > 0)
5472 outs() << "\tCategory Definitions\n";
5473 for (j = 0; j < symtab.cat_def_cnt; j++) {
5474 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5475 outs() << "\t(remaining category defs entries entends past the end of "
5476 << "the section)\n";
5477 break;
5478 }
5479 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5480 sizeof(uint32_t));
5481 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5482 sys::swapByteOrder(def);
5483
5484 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5485 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5486 << format("0x%08" PRIx32, def);
5487 if (r != nullptr) {
5488 if (left > sizeof(struct objc_category_t)) {
5489 outs() << "\n";
5490 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5491 } else {
5492 outs() << " (entends past the end of the section)\n";
5493 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5494 memcpy(&objc_category, r, left);
5495 }
5496 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5497 swapStruct(objc_category);
5498 print_objc_objc_category_t(&objc_category, &info);
5499 } else {
5500 outs() << "(not in an __OBJC section)\n";
5501 }
5502 }
5503 }
5504 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5505 if (II != SectionRef())
5506 print_image_info(II, &info);
5507
5508 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005509}
5510
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00005511static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5512 uint32_t size, uint32_t addr) {
5513 SymbolAddressMap AddrMap;
5514 CreateSymbolAddressMap(O, &AddrMap);
5515
5516 std::vector<SectionRef> Sections;
5517 for (const SectionRef &Section : O->sections()) {
5518 StringRef SectName;
5519 Section.getName(SectName);
5520 Sections.push_back(Section);
5521 }
5522
5523 struct DisassembleInfo info;
5524 // Set up the block of info used by the Symbolizer call backs.
5525 info.verbose = true;
5526 info.O = O;
5527 info.AddrMap = &AddrMap;
5528 info.Sections = &Sections;
5529 info.class_name = nullptr;
5530 info.selector_name = nullptr;
5531 info.method = nullptr;
5532 info.demangled_name = nullptr;
5533 info.bindtable = nullptr;
5534 info.adrp_addr = 0;
5535 info.adrp_inst = 0;
5536
5537 const char *p;
5538 struct objc_protocol_t protocol;
5539 uint32_t left, paddr;
5540 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5541 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5542 left = size - (p - sect);
5543 if (left < sizeof(struct objc_protocol_t)) {
5544 outs() << "Protocol extends past end of __protocol section\n";
5545 memcpy(&protocol, p, left);
5546 } else
5547 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5548 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5549 swapStruct(protocol);
5550 paddr = addr + (p - sect);
5551 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5552 if (print_protocol(paddr, 0, &info))
5553 outs() << "(not in an __OBJC section)\n";
5554 }
5555}
5556
Kevin Enderby0fc11822015-04-01 20:57:01 +00005557static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
5558 if (O->is64Bit())
5559 printObjc2_64bit_MetaData(O, verbose);
5560 else {
5561 MachO::mach_header H;
5562 H = O->getHeader();
5563 if (H.cputype == MachO::CPU_TYPE_ARM)
5564 printObjc2_32bit_MetaData(O, verbose);
5565 else {
5566 // This is the 32-bit non-arm cputype case. Which is normally
5567 // the first Objective-C ABI. But it may be the case of a
5568 // binary for the iOS simulator which is the second Objective-C
5569 // ABI. In that case printObjc1_32bit_MetaData() will determine that
5570 // and return false.
5571 if (printObjc1_32bit_MetaData(O, verbose) == false)
5572 printObjc2_32bit_MetaData(O, verbose);
5573 }
5574 }
5575}
5576
Kevin Enderbybf246f52014-09-24 23:08:22 +00005577// GuessLiteralPointer returns a string which for the item in the Mach-O file
5578// for the address passed in as ReferenceValue for printing as a comment with
5579// the instruction and also returns the corresponding type of that item
5580// indirectly through ReferenceType.
5581//
5582// If ReferenceValue is an address of literal cstring then a pointer to the
5583// cstring is returned and ReferenceType is set to
5584// LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
5585//
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005586// If ReferenceValue is an address of an Objective-C CFString, Selector ref or
5587// Class ref that name is returned and the ReferenceType is set accordingly.
5588//
5589// Lastly, literals which are Symbol address in a literal pool are looked for
5590// and if found the symbol name is returned and ReferenceType is set to
5591// LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
5592//
5593// If there is no item in the Mach-O file for the address passed in as
5594// ReferenceValue nullptr is returned and ReferenceType is unchanged.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00005595static const char *GuessLiteralPointer(uint64_t ReferenceValue,
5596 uint64_t ReferencePC,
5597 uint64_t *ReferenceType,
5598 struct DisassembleInfo *info) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005599 // First see if there is an external relocation entry at the ReferencePC.
Rafael Espindola80291272014-10-08 15:28:58 +00005600 uint64_t sect_addr = info->S.getAddress();
Kevin Enderbybf246f52014-09-24 23:08:22 +00005601 uint64_t sect_offset = ReferencePC - sect_addr;
5602 bool reloc_found = false;
5603 DataRefImpl Rel;
5604 MachO::any_relocation_info RE;
5605 bool isExtern = false;
5606 SymbolRef Symbol;
5607 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00005608 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderbybf246f52014-09-24 23:08:22 +00005609 if (RelocOffset == sect_offset) {
5610 Rel = Reloc.getRawDataRefImpl();
5611 RE = info->O->getRelocation(Rel);
5612 if (info->O->isRelocationScattered(RE))
5613 continue;
5614 isExtern = info->O->getPlainRelocationExternal(RE);
5615 if (isExtern) {
5616 symbol_iterator RelocSym = Reloc.getSymbol();
5617 Symbol = *RelocSym;
5618 }
5619 reloc_found = true;
5620 break;
5621 }
5622 }
5623 // If there is an external relocation entry for a symbol in a section
5624 // then used that symbol's value for the value of the reference.
5625 if (reloc_found && isExtern) {
5626 if (info->O->getAnyRelocationPCRel(RE)) {
5627 unsigned Type = info->O->getAnyRelocationType(RE);
5628 if (Type == MachO::X86_64_RELOC_SIGNED) {
5629 Symbol.getAddress(ReferenceValue);
5630 }
5631 }
5632 }
5633
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005634 // Look for literals such as Objective-C CFStrings refs, Selector refs,
5635 // Message refs and Class refs.
5636 bool classref, selref, msgref, cfstring;
5637 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
5638 selref, msgref, cfstring);
David Blaikie33dd45d02015-03-23 18:39:02 +00005639 if (classref && pointer_value == 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005640 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
5641 // And the pointer_value in that section is typically zero as it will be
5642 // set by dyld as part of the "bind information".
5643 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
5644 if (name != nullptr) {
5645 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
Hans Wennborgdb53e302014-10-23 21:59:17 +00005646 const char *class_name = strrchr(name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005647 if (class_name != nullptr && class_name[1] == '_' &&
5648 class_name[2] != '\0') {
5649 info->class_name = class_name + 2;
5650 return name;
5651 }
5652 }
5653 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005654
David Blaikie33dd45d02015-03-23 18:39:02 +00005655 if (classref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005656 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5657 const char *name =
5658 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
5659 if (name != nullptr)
5660 info->class_name = name;
5661 else
5662 name = "bad class ref";
Kevin Enderbybf246f52014-09-24 23:08:22 +00005663 return name;
5664 }
5665
David Blaikie33dd45d02015-03-23 18:39:02 +00005666 if (cfstring) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005667 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
5668 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
5669 return name;
5670 }
5671
David Blaikie33dd45d02015-03-23 18:39:02 +00005672 if (selref && pointer_value == 0)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005673 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
5674
5675 if (pointer_value != 0)
5676 ReferenceValue = pointer_value;
5677
5678 const char *name = GuessCstringPointer(ReferenceValue, info);
5679 if (name) {
David Blaikie33dd45d02015-03-23 18:39:02 +00005680 if (pointer_value != 0 && selref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005681 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
5682 info->selector_name = name;
David Blaikie33dd45d02015-03-23 18:39:02 +00005683 } else if (pointer_value != 0 && msgref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005684 info->class_name = nullptr;
5685 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
5686 info->selector_name = name;
5687 } else
5688 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
5689 return name;
5690 }
5691
5692 // Lastly look for an indirect symbol with this ReferenceValue which is in
5693 // a literal pool. If found return that symbol name.
5694 name = GuessIndirectSymbol(ReferenceValue, info);
5695 if (name) {
5696 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
5697 return name;
5698 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005699
5700 return nullptr;
5701}
5702
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005703// SymbolizerSymbolLookUp is the symbol lookup function passed when creating
Kevin Enderbybf246f52014-09-24 23:08:22 +00005704// the Symbolizer. It looks up the ReferenceValue using the info passed via the
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005705// pointer to the struct DisassembleInfo that was passed when MCSymbolizer
5706// is created and returns the symbol name that matches the ReferenceValue or
5707// nullptr if none. The ReferenceType is passed in for the IN type of
5708// reference the instruction is making from the values in defined in the header
5709// "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
5710// Out type and the ReferenceName will also be set which is added as a comment
5711// to the disassembled instruction.
5712//
Kevin Enderby04bf6932014-10-28 23:39:46 +00005713#if HAVE_CXXABI_H
5714// If the symbol name is a C++ mangled name then the demangled name is
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005715// returned through ReferenceName and ReferenceType is set to
5716// LLVMDisassembler_ReferenceType_DeMangled_Name .
Kevin Enderby04bf6932014-10-28 23:39:46 +00005717#endif
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005718//
5719// When this is called to get a symbol name for a branch target then the
5720// ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
5721// SymbolValue will be looked for in the indirect symbol table to determine if
5722// it is an address for a symbol stub. If so then the symbol name for that
5723// stub is returned indirectly through ReferenceName and then ReferenceType is
5724// set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
5725//
Kevin Enderbybf246f52014-09-24 23:08:22 +00005726// When this is called with an value loaded via a PC relative load then
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005727// ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
5728// SymbolValue is checked to be an address of literal pointer, symbol pointer,
5729// or an Objective-C meta data reference. If so the output ReferenceType is
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005730// set to correspond to that as well as setting the ReferenceName.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00005731static const char *SymbolizerSymbolLookUp(void *DisInfo,
5732 uint64_t ReferenceValue,
5733 uint64_t *ReferenceType,
5734 uint64_t ReferencePC,
5735 const char **ReferenceName) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005736 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
Kevin Enderbybf246f52014-09-24 23:08:22 +00005737 // If no verbose symbolic information is wanted then just return nullptr.
David Blaikie33dd45d02015-03-23 18:39:02 +00005738 if (!info->verbose) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005739 *ReferenceName = nullptr;
5740 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005741 return nullptr;
5742 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005743
Kevin Enderbyf6d25852015-01-31 00:37:11 +00005744 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderbybf246f52014-09-24 23:08:22 +00005745
Kevin Enderby85974882014-09-26 22:20:44 +00005746 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
5747 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005748 if (*ReferenceName != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005749 method_reference(info, ReferenceType, ReferenceName);
5750 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
5751 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
5752 } else
Kevin Enderby04bf6932014-10-28 23:39:46 +00005753#if HAVE_CXXABI_H
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005754 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00005755 if (info->demangled_name != nullptr)
5756 free(info->demangled_name);
5757 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005758 info->demangled_name =
5759 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005760 if (info->demangled_name != nullptr) {
5761 *ReferenceName = info->demangled_name;
5762 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5763 } else
5764 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5765 } else
5766#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005767 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5768 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
5769 *ReferenceName =
5770 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
Kevin Enderby85974882014-09-26 22:20:44 +00005771 if (*ReferenceName)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005772 method_reference(info, ReferenceType, ReferenceName);
Kevin Enderby85974882014-09-26 22:20:44 +00005773 else
5774 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005775 // If this is arm64 and the reference is an adrp instruction save the
5776 // instruction, passed in ReferenceValue and the address of the instruction
5777 // for use later if we see and add immediate instruction.
5778 } else if (info->O->getArch() == Triple::aarch64 &&
5779 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
5780 info->adrp_inst = ReferenceValue;
5781 info->adrp_addr = ReferencePC;
5782 SymbolName = nullptr;
5783 *ReferenceName = nullptr;
5784 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5785 // If this is arm64 and reference is an add immediate instruction and we
5786 // have
5787 // seen an adrp instruction just before it and the adrp's Xd register
5788 // matches
5789 // this add's Xn register reconstruct the value being referenced and look to
5790 // see if it is a literal pointer. Note the add immediate instruction is
5791 // passed in ReferenceValue.
5792 } else if (info->O->getArch() == Triple::aarch64 &&
5793 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
5794 ReferencePC - 4 == info->adrp_addr &&
5795 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5796 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5797 uint32_t addxri_inst;
5798 uint64_t adrp_imm, addxri_imm;
5799
5800 adrp_imm =
5801 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5802 if (info->adrp_inst & 0x0200000)
5803 adrp_imm |= 0xfffffffffc000000LL;
5804
5805 addxri_inst = ReferenceValue;
5806 addxri_imm = (addxri_inst >> 10) & 0xfff;
5807 if (((addxri_inst >> 22) & 0x3) == 1)
5808 addxri_imm <<= 12;
5809
5810 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5811 (adrp_imm << 12) + addxri_imm;
5812
5813 *ReferenceName =
5814 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5815 if (*ReferenceName == nullptr)
5816 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5817 // If this is arm64 and the reference is a load register instruction and we
5818 // have seen an adrp instruction just before it and the adrp's Xd register
5819 // matches this add's Xn register reconstruct the value being referenced and
5820 // look to see if it is a literal pointer. Note the load register
5821 // instruction is passed in ReferenceValue.
5822 } else if (info->O->getArch() == Triple::aarch64 &&
5823 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
5824 ReferencePC - 4 == info->adrp_addr &&
5825 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5826 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5827 uint32_t ldrxui_inst;
5828 uint64_t adrp_imm, ldrxui_imm;
5829
5830 adrp_imm =
5831 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5832 if (info->adrp_inst & 0x0200000)
5833 adrp_imm |= 0xfffffffffc000000LL;
5834
5835 ldrxui_inst = ReferenceValue;
5836 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
5837
5838 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5839 (adrp_imm << 12) + (ldrxui_imm << 3);
5840
5841 *ReferenceName =
5842 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5843 if (*ReferenceName == nullptr)
5844 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5845 }
5846 // If this arm64 and is an load register (PC-relative) instruction the
5847 // ReferenceValue is the PC plus the immediate value.
5848 else if (info->O->getArch() == Triple::aarch64 &&
5849 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
5850 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
5851 *ReferenceName =
5852 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5853 if (*ReferenceName == nullptr)
5854 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby85974882014-09-26 22:20:44 +00005855 }
Kevin Enderby04bf6932014-10-28 23:39:46 +00005856#if HAVE_CXXABI_H
5857 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5858 if (info->demangled_name != nullptr)
5859 free(info->demangled_name);
5860 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005861 info->demangled_name =
5862 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005863 if (info->demangled_name != nullptr) {
5864 *ReferenceName = info->demangled_name;
5865 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5866 }
5867 }
5868#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005869 else {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005870 *ReferenceName = nullptr;
5871 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5872 }
5873
5874 return SymbolName;
5875}
5876
Kevin Enderbybf246f52014-09-24 23:08:22 +00005877/// \brief Emits the comments that are stored in the CommentStream.
5878/// Each comment in the CommentStream must end with a newline.
5879static void emitComments(raw_svector_ostream &CommentStream,
5880 SmallString<128> &CommentsToEmit,
5881 formatted_raw_ostream &FormattedOS,
5882 const MCAsmInfo &MAI) {
5883 // Flush the stream before taking its content.
5884 CommentStream.flush();
5885 StringRef Comments = CommentsToEmit.str();
5886 // Get the default information for printing a comment.
5887 const char *CommentBegin = MAI.getCommentString();
5888 unsigned CommentColumn = MAI.getCommentColumn();
5889 bool IsFirst = true;
5890 while (!Comments.empty()) {
5891 if (!IsFirst)
5892 FormattedOS << '\n';
5893 // Emit a line of comments.
5894 FormattedOS.PadToColumn(CommentColumn);
5895 size_t Position = Comments.find('\n');
5896 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
5897 // Move after the newline character.
5898 Comments = Comments.substr(Position + 1);
5899 IsFirst = false;
5900 }
5901 FormattedOS.flush();
5902
5903 // Tell the comment stream that the vector changed underneath it.
5904 CommentsToEmit.clear();
5905 CommentStream.resync();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005906}
5907
Kevin Enderby95df54c2015-02-04 01:01:38 +00005908static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
5909 StringRef DisSegName, StringRef DisSectName) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005910 const char *McpuDefault = nullptr;
5911 const Target *ThumbTarget = nullptr;
5912 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005913 if (!TheTarget) {
5914 // GetTarget prints out stuff.
5915 return;
5916 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005917 if (MCPU.empty() && McpuDefault)
5918 MCPU = McpuDefault;
5919
Ahmed Charles56440fd2014-03-06 05:51:42 +00005920 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005921 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005922 if (ThumbTarget)
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005923 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005924
Kevin Enderbyc9595622014-08-06 23:24:41 +00005925 // Package up features to be passed to target/subtarget
5926 std::string FeaturesStr;
5927 if (MAttrs.size()) {
5928 SubtargetFeatures Features;
5929 for (unsigned i = 0; i != MAttrs.size(); ++i)
5930 Features.AddFeature(MAttrs[i]);
5931 FeaturesStr = Features.getString();
5932 }
5933
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005934 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00005935 std::unique_ptr<const MCRegisterInfo> MRI(
5936 TheTarget->createMCRegInfo(TripleName));
5937 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +00005938 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +00005939 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +00005940 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Craig Toppere6cb63e2014-04-25 04:24:47 +00005941 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005942 std::unique_ptr<MCDisassembler> DisAsm(
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005943 TheTarget->createMCDisassembler(*STI, Ctx));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005944 std::unique_ptr<MCSymbolizer> Symbolizer;
5945 struct DisassembleInfo SymbolizerInfo;
5946 std::unique_ptr<MCRelocationInfo> RelInfo(
5947 TheTarget->createMCRelocationInfo(TripleName, Ctx));
5948 if (RelInfo) {
5949 Symbolizer.reset(TheTarget->createMCSymbolizer(
5950 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00005951 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005952 DisAsm->setSymbolizer(std::move(Symbolizer));
5953 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005954 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +00005955 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +00005956 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00005957 // Set the display preference for hex vs. decimal immediates.
5958 IP->setPrintImmHex(PrintImmHex);
5959 // Comment stream and backing vector.
5960 SmallString<128> CommentsToEmit;
5961 raw_svector_ostream CommentStream(CommentsToEmit);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00005962 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
5963 // if it is done then arm64 comments for string literals don't get printed
5964 // and some constant get printed instead and not setting it causes intel
5965 // (32-bit and 64-bit) comments printed with different spacing before the
5966 // comment causing different diffs with the 'C' disassembler library API.
5967 // IP->setCommentStream(CommentStream);
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005968
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005969 if (!AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +00005970 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005971 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005972 return;
5973 }
5974
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005975 // Set up thumb disassembler.
5976 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
5977 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
5978 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005979 std::unique_ptr<MCDisassembler> ThumbDisAsm;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005980 std::unique_ptr<MCInstPrinter> ThumbIP;
5981 std::unique_ptr<MCContext> ThumbCtx;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005982 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
5983 struct DisassembleInfo ThumbSymbolizerInfo;
5984 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005985 if (ThumbTarget) {
5986 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
5987 ThumbAsmInfo.reset(
5988 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
5989 ThumbSTI.reset(
5990 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
5991 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
5992 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
Kevin Enderby930fdc72014-11-06 19:00:13 +00005993 MCContext *PtrThumbCtx = ThumbCtx.get();
5994 ThumbRelInfo.reset(
5995 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
5996 if (ThumbRelInfo) {
5997 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
5998 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00005999 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006000 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6001 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006002 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6003 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +00006004 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6005 *ThumbInstrInfo, *ThumbMRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006006 // Set the display preference for hex vs. decimal immediates.
6007 ThumbIP->setPrintImmHex(PrintImmHex);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006008 }
6009
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006010 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006011 errs() << "error: couldn't initialize disassembler for target "
6012 << ThumbTripleName << '\n';
6013 return;
6014 }
6015
Charles Davis8bdfafd2013-09-01 04:28:48 +00006016 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006017
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006018 // FIXME: Using the -cfg command line option, this code used to be able to
6019 // annotate relocations with the referenced symbol's name, and if this was
6020 // inside a __[cf]string section, the data it points to. This is now replaced
6021 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +00006022 std::vector<SectionRef> Sections;
6023 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006024 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +00006025 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006026
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00006027 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
Kevin Enderby273ae012013-06-06 17:20:50 +00006028 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006029
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006030 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +00006031 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006032
Kevin Enderby273ae012013-06-06 17:20:50 +00006033 // Build a data in code table that is sorted on by the address of each entry.
6034 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +00006035 if (Header.filetype == MachO::MH_OBJECT)
Rafael Espindola80291272014-10-08 15:28:58 +00006036 BaseAddress = Sections[0].getAddress();
Kevin Enderby273ae012013-06-06 17:20:50 +00006037 else
6038 BaseAddress = BaseSegmentAddress;
6039 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +00006040 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +00006041 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006042 uint32_t Offset;
6043 DI->getOffset(Offset);
6044 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6045 }
6046 array_pod_sort(Dices.begin(), Dices.end());
6047
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006048#ifndef NDEBUG
6049 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6050#else
6051 raw_ostream &DebugOut = nulls();
6052#endif
6053
Ahmed Charles56440fd2014-03-06 05:51:42 +00006054 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +00006055 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +00006056 // Try to find debug info and set up the DIContext for it.
6057 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +00006058 // A separate DSym file path was specified, parse it as a macho file,
6059 // get the sections and supply it to the section name parsing machinery.
6060 if (!DSYMFile.empty()) {
Rafael Espindola48af1c22014-08-19 18:44:46 +00006061 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006062 MemoryBuffer::getFileOrSTDIN(DSYMFile);
Rafael Espindola48af1c22014-08-19 18:44:46 +00006063 if (std::error_code EC = BufOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006064 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
Benjamin Kramer699128e2011-09-21 01:13:19 +00006065 return;
6066 }
Rafael Espindola48af1c22014-08-19 18:44:46 +00006067 DbgObj =
6068 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6069 .get()
6070 .release();
Benjamin Kramer699128e2011-09-21 01:13:19 +00006071 }
6072
Eric Christopher7370b552012-11-12 21:40:38 +00006073 // Setup the DIContext
Zachary Turner6489d7b2015-04-23 17:37:47 +00006074 diContext.reset(new DWARFContextInMemory(*DbgObj));
Benjamin Kramer699128e2011-09-21 01:13:19 +00006075 }
6076
Kevin Enderby95df54c2015-02-04 01:01:38 +00006077 if (DumpSections.size() == 0)
6078 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006079
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006080 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00006081 StringRef SectName;
Kevin Enderby95df54c2015-02-04 01:01:38 +00006082 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6083 continue;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006084
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006085 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006086
Rafael Espindolab0f76a42013-04-05 15:15:22 +00006087 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
Kevin Enderby95df54c2015-02-04 01:01:38 +00006088 if (SegmentName != DisSegName)
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006089 continue;
6090
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006091 StringRef BytesStr;
6092 Sections[SectIdx].getContents(BytesStr);
Aaron Ballman106fd7b2014-11-12 14:01:17 +00006093 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6094 BytesStr.size());
Rafael Espindola80291272014-10-08 15:28:58 +00006095 uint64_t SectAddress = Sections[SectIdx].getAddress();
Rafael Espindolabd604f22014-11-07 00:52:15 +00006096
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006097 bool symbolTableWorked = false;
6098
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006099 // Parse relocations.
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00006100 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
6101 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00006102 uint64_t RelocOffset = Reloc.getOffset();
Rafael Espindola80291272014-10-08 15:28:58 +00006103 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Owen Andersond9243c42011-10-17 21:37:35 +00006104 RelocOffset -= SectionAddress;
6105
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00006106 symbol_iterator RelocSym = Reloc.getSymbol();
Owen Andersond9243c42011-10-17 21:37:35 +00006107
Rafael Espindola806f0062013-06-05 01:33:53 +00006108 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006109 }
6110 array_pod_sort(Relocs.begin(), Relocs.end());
6111
Kevin Enderbybf246f52014-09-24 23:08:22 +00006112 // Create a map of symbol addresses to symbol names for use by
6113 // the SymbolizerSymbolLookUp() routine.
6114 SymbolAddressMap AddrMap;
Kevin Enderby6a221752015-03-17 17:10:57 +00006115 bool DisSymNameFound = false;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006116 for (const SymbolRef &Symbol : MachOOF->symbols()) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006117 SymbolRef::Type ST = Symbol.getType();
Kevin Enderbybf246f52014-09-24 23:08:22 +00006118 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6119 ST == SymbolRef::ST_Other) {
6120 uint64_t Address;
6121 Symbol.getAddress(Address);
6122 StringRef SymName;
6123 Symbol.getName(SymName);
6124 AddrMap[Address] = SymName;
Kevin Enderby6a221752015-03-17 17:10:57 +00006125 if (!DisSymName.empty() && DisSymName == SymName)
6126 DisSymNameFound = true;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006127 }
6128 }
David Blaikie33dd45d02015-03-23 18:39:02 +00006129 if (!DisSymName.empty() && !DisSymNameFound) {
Kevin Enderby6a221752015-03-17 17:10:57 +00006130 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6131 return;
6132 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006133 // Set up the block of info used by the Symbolizer call backs.
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006134 SymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006135 SymbolizerInfo.O = MachOOF;
6136 SymbolizerInfo.S = Sections[SectIdx];
Kevin Enderbybf246f52014-09-24 23:08:22 +00006137 SymbolizerInfo.AddrMap = &AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006138 SymbolizerInfo.Sections = &Sections;
6139 SymbolizerInfo.class_name = nullptr;
6140 SymbolizerInfo.selector_name = nullptr;
6141 SymbolizerInfo.method = nullptr;
Kevin Enderby04bf6932014-10-28 23:39:46 +00006142 SymbolizerInfo.demangled_name = nullptr;
Kevin Enderby078be602014-10-23 19:53:12 +00006143 SymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006144 SymbolizerInfo.adrp_addr = 0;
6145 SymbolizerInfo.adrp_inst = 0;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006146 // Same for the ThumbSymbolizer
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006147 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006148 ThumbSymbolizerInfo.O = MachOOF;
6149 ThumbSymbolizerInfo.S = Sections[SectIdx];
6150 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6151 ThumbSymbolizerInfo.Sections = &Sections;
6152 ThumbSymbolizerInfo.class_name = nullptr;
6153 ThumbSymbolizerInfo.selector_name = nullptr;
6154 ThumbSymbolizerInfo.method = nullptr;
6155 ThumbSymbolizerInfo.demangled_name = nullptr;
6156 ThumbSymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006157 ThumbSymbolizerInfo.adrp_addr = 0;
6158 ThumbSymbolizerInfo.adrp_inst = 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006159
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006160 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006161 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00006162 StringRef SymName;
6163 Symbols[SymIdx].getName(SymName);
6164
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006165 SymbolRef::Type ST = Symbols[SymIdx].getType();
Owen Andersond9243c42011-10-17 21:37:35 +00006166 if (ST != SymbolRef::ST_Function)
6167 continue;
6168
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006169 // Make sure the symbol is defined in this section.
Rafael Espindola80291272014-10-08 15:28:58 +00006170 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Owen Andersond9243c42011-10-17 21:37:35 +00006171 if (!containsSym)
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006172 continue;
6173
Kevin Enderby6a221752015-03-17 17:10:57 +00006174 // If we are only disassembling one symbol see if this is that symbol.
6175 if (!DisSymName.empty() && DisSymName != SymName)
6176 continue;
6177
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006178 // Start at the address of the symbol relative to the section's address.
Owen Andersond9243c42011-10-17 21:37:35 +00006179 uint64_t Start = 0;
Rafael Espindola80291272014-10-08 15:28:58 +00006180 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00006181 Symbols[SymIdx].getAddress(Start);
Cameron Zwarich54478a52012-02-03 05:42:17 +00006182 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006183
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006184 // Stop disassembling either at the beginning of the next symbol or at
6185 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +00006186 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +00006187 uint64_t NextSym = 0;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006188 uint64_t NextSymIdx = SymIdx + 1;
Owen Andersond9243c42011-10-17 21:37:35 +00006189 while (Symbols.size() > NextSymIdx) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006190 SymbolRef::Type NextSymType = Symbols[NextSymIdx].getType();
Owen Andersond9243c42011-10-17 21:37:35 +00006191 if (NextSymType == SymbolRef::ST_Function) {
Rafael Espindola80291272014-10-08 15:28:58 +00006192 containsNextSym =
6193 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00006194 Symbols[NextSymIdx].getAddress(NextSym);
Cameron Zwarich54478a52012-02-03 05:42:17 +00006195 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006196 break;
6197 }
6198 ++NextSymIdx;
6199 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006200
Rafael Espindola80291272014-10-08 15:28:58 +00006201 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006202 uint64_t End = containsNextSym ? NextSym : SectSize;
Owen Andersond9243c42011-10-17 21:37:35 +00006203 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006204
6205 symbolTableWorked = true;
Rafael Espindolabd604f22014-11-07 00:52:15 +00006206
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006207 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6208 bool isThumb =
6209 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
6210
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006211 outs() << SymName << ":\n";
6212 DILineInfo lastLine;
6213 for (uint64_t Index = Start; Index < End; Index += Size) {
6214 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +00006215
Kevin Enderbybf246f52014-09-24 23:08:22 +00006216 uint64_t PC = SectAddress + Index;
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006217 if (!NoLeadingAddr) {
6218 if (FullLeadingAddr) {
6219 if (MachOOF->is64Bit())
6220 outs() << format("%016" PRIx64, PC);
6221 else
6222 outs() << format("%08" PRIx64, PC);
6223 } else {
6224 outs() << format("%8" PRIx64 ":", PC);
6225 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006226 }
6227 if (!NoShowRawInsn)
6228 outs() << "\t";
Kevin Enderby273ae012013-06-06 17:20:50 +00006229
6230 // Check the data in code table here to see if this is data not an
6231 // instruction to be disassembled.
6232 DiceTable Dice;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006233 Dice.push_back(std::make_pair(PC, DiceRef()));
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006234 dice_table_iterator DTI =
6235 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6236 compareDiceTableEntries);
6237 if (DTI != Dices.end()) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006238 uint16_t Length;
6239 DTI->second.getLength(Length);
Kevin Enderby273ae012013-06-06 17:20:50 +00006240 uint16_t Kind;
6241 DTI->second.getKind(Kind);
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00006242 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006243 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6244 (PC == (DTI->first + Length - 1)) && (Length & 1))
6245 Size++;
Kevin Enderby273ae012013-06-06 17:20:50 +00006246 continue;
6247 }
6248
Kevin Enderbybf246f52014-09-24 23:08:22 +00006249 SmallVector<char, 64> AnnotationsBytes;
6250 raw_svector_ostream Annotations(AnnotationsBytes);
6251
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006252 bool gotInst;
6253 if (isThumb)
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006254 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006255 PC, DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006256 else
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006257 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
Kevin Enderbybf246f52014-09-24 23:08:22 +00006258 DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006259 if (gotInst) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006260 if (!NoShowRawInsn) {
Colin LeMahieu2048ea42015-05-28 18:39:50 +00006261 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006262 }
6263 formatted_raw_ostream FormattedOS(outs());
6264 Annotations.flush();
6265 StringRef AnnotationsStr = Annotations.str();
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006266 if (isThumb)
Akira Hatanakab46d0232015-03-27 20:36:02 +00006267 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006268 else
Akira Hatanaka1d079942015-03-28 20:44:05 +00006269 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006270 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
Owen Andersond9243c42011-10-17 21:37:35 +00006271
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006272 // Print debug info.
6273 if (diContext) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006274 DILineInfo dli = diContext->getLineInfoForAddress(PC);
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006275 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +00006276 if (dli != lastLine && dli.Line != 0)
6277 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6278 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006279 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006280 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006281 outs() << "\n";
6282 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006283 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006284 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006285 outs() << format("\t.byte 0x%02x #bad opcode\n",
6286 *(Bytes.data() + Index) & 0xff);
6287 Size = 1; // skip exactly one illegible byte and move on.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006288 } else if (Arch == Triple::aarch64) {
6289 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6290 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6291 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6292 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6293 outs() << format("\t.long\t0x%08x\n", opcode);
6294 Size = 4;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006295 } else {
6296 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6297 if (Size == 0)
6298 Size = 1; // skip illegible bytes
6299 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006300 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006301 }
6302 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006303 if (!symbolTableWorked) {
Rafael Espindola80291272014-10-08 15:28:58 +00006304 // Reading the symbol table didn't work, disassemble the whole section.
6305 uint64_t SectAddress = Sections[SectIdx].getAddress();
6306 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbybadd1002012-05-18 00:13:56 +00006307 uint64_t InstSize;
6308 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +00006309 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +00006310
Kevin Enderbybf246f52014-09-24 23:08:22 +00006311 uint64_t PC = SectAddress + Index;
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006312 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6313 DebugOut, nulls())) {
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006314 if (!NoLeadingAddr) {
6315 if (FullLeadingAddr) {
6316 if (MachOOF->is64Bit())
6317 outs() << format("%016" PRIx64, PC);
6318 else
6319 outs() << format("%08" PRIx64, PC);
6320 } else {
6321 outs() << format("%8" PRIx64 ":", PC);
6322 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006323 }
6324 if (!NoShowRawInsn) {
6325 outs() << "\t";
Colin LeMahieu2048ea42015-05-28 18:39:50 +00006326 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006327 }
Akira Hatanaka1d079942015-03-28 20:44:05 +00006328 IP->printInst(&Inst, outs(), "", *STI);
Bill Wendling4e68e062012-07-19 00:17:40 +00006329 outs() << "\n";
6330 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006331 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006332 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006333 outs() << format("\t.byte 0x%02x #bad opcode\n",
6334 *(Bytes.data() + Index) & 0xff);
6335 InstSize = 1; // skip exactly one illegible byte and move on.
6336 } else {
6337 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6338 if (InstSize == 0)
6339 InstSize = 1; // skip illegible bytes
6340 }
Bill Wendling4e68e062012-07-19 00:17:40 +00006341 }
Kevin Enderbybadd1002012-05-18 00:13:56 +00006342 }
6343 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006344 // The TripleName's need to be reset if we are called again for a different
6345 // archtecture.
6346 TripleName = "";
6347 ThumbTripleName = "";
6348
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006349 if (SymbolizerInfo.method != nullptr)
6350 free(SymbolizerInfo.method);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006351 if (SymbolizerInfo.demangled_name != nullptr)
6352 free(SymbolizerInfo.demangled_name);
Kevin Enderby078be602014-10-23 19:53:12 +00006353 if (SymbolizerInfo.bindtable != nullptr)
6354 delete SymbolizerInfo.bindtable;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006355 if (ThumbSymbolizerInfo.method != nullptr)
6356 free(ThumbSymbolizerInfo.method);
6357 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6358 free(ThumbSymbolizerInfo.demangled_name);
6359 if (ThumbSymbolizerInfo.bindtable != nullptr)
6360 delete ThumbSymbolizerInfo.bindtable;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006361 }
6362}
Tim Northover4bd286a2014-08-01 13:07:19 +00006363
Tim Northover39c70bb2014-08-12 11:52:59 +00006364//===----------------------------------------------------------------------===//
6365// __compact_unwind section dumping
6366//===----------------------------------------------------------------------===//
6367
Tim Northover4bd286a2014-08-01 13:07:19 +00006368namespace {
Tim Northover39c70bb2014-08-12 11:52:59 +00006369
6370template <typename T> static uint64_t readNext(const char *&Buf) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006371 using llvm::support::little;
6372 using llvm::support::unaligned;
Tim Northover39c70bb2014-08-12 11:52:59 +00006373
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006374 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6375 Buf += sizeof(T);
6376 return Val;
6377}
Tim Northover39c70bb2014-08-12 11:52:59 +00006378
Tim Northover4bd286a2014-08-01 13:07:19 +00006379struct CompactUnwindEntry {
6380 uint32_t OffsetInSection;
6381
6382 uint64_t FunctionAddr;
6383 uint32_t Length;
6384 uint32_t CompactEncoding;
6385 uint64_t PersonalityAddr;
6386 uint64_t LSDAAddr;
6387
6388 RelocationRef FunctionReloc;
6389 RelocationRef PersonalityReloc;
6390 RelocationRef LSDAReloc;
6391
6392 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006393 : OffsetInSection(Offset) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006394 if (Is64)
6395 read<uint64_t>(Contents.data() + Offset);
6396 else
6397 read<uint32_t>(Contents.data() + Offset);
6398 }
6399
6400private:
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006401 template <typename UIntPtr> void read(const char *Buf) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006402 FunctionAddr = readNext<UIntPtr>(Buf);
6403 Length = readNext<uint32_t>(Buf);
6404 CompactEncoding = readNext<uint32_t>(Buf);
6405 PersonalityAddr = readNext<UIntPtr>(Buf);
6406 LSDAAddr = readNext<UIntPtr>(Buf);
6407 }
6408};
6409}
6410
6411/// Given a relocation from __compact_unwind, consisting of the RelocationRef
6412/// and data being relocated, determine the best base Name and Addend to use for
6413/// display purposes.
6414///
6415/// 1. An Extern relocation will directly reference a symbol (and the data is
6416/// then already an addend), so use that.
6417/// 2. Otherwise the data is an offset in the object file's layout; try to find
6418// a symbol before it in the same section, and use the offset from there.
6419/// 3. Finally, if all that fails, fall back to an offset from the start of the
6420/// referenced section.
6421static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6422 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006423 const RelocationRef &Reloc, uint64_t Addr,
Tim Northover4bd286a2014-08-01 13:07:19 +00006424 StringRef &Name, uint64_t &Addend) {
6425 if (Reloc.getSymbol() != Obj->symbol_end()) {
6426 Reloc.getSymbol()->getName(Name);
6427 Addend = Addr;
6428 return;
6429 }
6430
6431 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
Keno Fischerc780e8e2015-05-21 21:24:32 +00006432 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
Tim Northover4bd286a2014-08-01 13:07:19 +00006433
Rafael Espindola80291272014-10-08 15:28:58 +00006434 uint64_t SectionAddr = RelocSection.getAddress();
Tim Northover4bd286a2014-08-01 13:07:19 +00006435
6436 auto Sym = Symbols.upper_bound(Addr);
6437 if (Sym == Symbols.begin()) {
6438 // The first symbol in the object is after this reference, the best we can
6439 // do is section-relative notation.
6440 RelocSection.getName(Name);
6441 Addend = Addr - SectionAddr;
6442 return;
6443 }
6444
6445 // Go back one so that SymbolAddress <= Addr.
6446 --Sym;
6447
6448 section_iterator SymSection = Obj->section_end();
6449 Sym->second.getSection(SymSection);
6450 if (RelocSection == *SymSection) {
6451 // There's a valid symbol in the same section before this reference.
6452 Sym->second.getName(Name);
6453 Addend = Addr - Sym->first;
6454 return;
6455 }
6456
6457 // There is a symbol before this reference, but it's in a different
6458 // section. Probably not helpful to mention it, so use the section name.
6459 RelocSection.getName(Name);
6460 Addend = Addr - SectionAddr;
6461}
6462
6463static void printUnwindRelocDest(const MachOObjectFile *Obj,
6464 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006465 const RelocationRef &Reloc, uint64_t Addr) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006466 StringRef Name;
6467 uint64_t Addend;
6468
Rafael Espindola854038e2015-06-26 14:51:16 +00006469 if (!Reloc.getObject())
Tim Northover0b0add52014-09-09 10:45:06 +00006470 return;
6471
Tim Northover4bd286a2014-08-01 13:07:19 +00006472 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6473
6474 outs() << Name;
6475 if (Addend)
Tim Northover63a25622014-08-11 09:14:06 +00006476 outs() << " + " << format("0x%" PRIx64, Addend);
Tim Northover4bd286a2014-08-01 13:07:19 +00006477}
6478
6479static void
6480printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6481 std::map<uint64_t, SymbolRef> &Symbols,
6482 const SectionRef &CompactUnwind) {
6483
6484 assert(Obj->isLittleEndian() &&
6485 "There should not be a big-endian .o with __compact_unwind");
6486
6487 bool Is64 = Obj->is64Bit();
6488 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6489 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6490
6491 StringRef Contents;
6492 CompactUnwind.getContents(Contents);
6493
6494 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6495
6496 // First populate the initial raw offsets, encodings and so on from the entry.
6497 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6498 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
6499 CompactUnwinds.push_back(Entry);
6500 }
6501
6502 // Next we need to look at the relocations to find out what objects are
6503 // actually being referred to.
6504 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00006505 uint64_t RelocAddress = Reloc.getOffset();
Tim Northover4bd286a2014-08-01 13:07:19 +00006506
6507 uint32_t EntryIdx = RelocAddress / EntrySize;
6508 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
6509 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
6510
6511 if (OffsetInEntry == 0)
6512 Entry.FunctionReloc = Reloc;
6513 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
6514 Entry.PersonalityReloc = Reloc;
6515 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
6516 Entry.LSDAReloc = Reloc;
6517 else
6518 llvm_unreachable("Unexpected relocation in __compact_unwind section");
6519 }
6520
6521 // Finally, we're ready to print the data we've gathered.
6522 outs() << "Contents of __compact_unwind section:\n";
6523 for (auto &Entry : CompactUnwinds) {
Tim Northover06af2602014-08-08 12:08:51 +00006524 outs() << " Entry at offset "
6525 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00006526
6527 // 1. Start of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006528 outs() << " start: " << format("0x%" PRIx64,
6529 Entry.FunctionAddr) << ' ';
6530 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
Tim Northover4bd286a2014-08-01 13:07:19 +00006531 outs() << '\n';
6532
6533 // 2. Length of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006534 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
6535 << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00006536 // 3. The 32-bit compact encoding.
6537 outs() << " compact encoding: "
Tim Northoverb911bf82014-08-08 12:00:09 +00006538 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00006539
6540 // 4. The personality function, if present.
Rafael Espindola854038e2015-06-26 14:51:16 +00006541 if (Entry.PersonalityReloc.getObject()) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006542 outs() << " personality function: "
Tim Northoverb911bf82014-08-08 12:00:09 +00006543 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00006544 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
6545 Entry.PersonalityAddr);
6546 outs() << '\n';
6547 }
6548
6549 // 5. This entry's language-specific data area.
Rafael Espindola854038e2015-06-26 14:51:16 +00006550 if (Entry.LSDAReloc.getObject()) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006551 outs() << " LSDA: " << format("0x%" PRIx64,
6552 Entry.LSDAAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00006553 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
6554 outs() << '\n';
6555 }
6556 }
6557}
6558
Tim Northover39c70bb2014-08-12 11:52:59 +00006559//===----------------------------------------------------------------------===//
6560// __unwind_info section dumping
6561//===----------------------------------------------------------------------===//
6562
6563static void printRegularSecondLevelUnwindPage(const char *PageStart) {
6564 const char *Pos = PageStart;
6565 uint32_t Kind = readNext<uint32_t>(Pos);
6566 (void)Kind;
6567 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
6568
6569 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6570 uint16_t NumEntries = readNext<uint16_t>(Pos);
6571
6572 Pos = PageStart + EntriesStart;
6573 for (unsigned i = 0; i < NumEntries; ++i) {
6574 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6575 uint32_t Encoding = readNext<uint32_t>(Pos);
6576
6577 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006578 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6579 << ", "
6580 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006581 }
6582}
6583
6584static void printCompressedSecondLevelUnwindPage(
6585 const char *PageStart, uint32_t FunctionBase,
6586 const SmallVectorImpl<uint32_t> &CommonEncodings) {
6587 const char *Pos = PageStart;
6588 uint32_t Kind = readNext<uint32_t>(Pos);
6589 (void)Kind;
6590 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
6591
6592 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6593 uint16_t NumEntries = readNext<uint16_t>(Pos);
6594
6595 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
6596 readNext<uint16_t>(Pos);
Aaron Ballman80930af2014-08-14 13:53:19 +00006597 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
6598 PageStart + EncodingsStart);
Tim Northover39c70bb2014-08-12 11:52:59 +00006599
6600 Pos = PageStart + EntriesStart;
6601 for (unsigned i = 0; i < NumEntries; ++i) {
6602 uint32_t Entry = readNext<uint32_t>(Pos);
6603 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
6604 uint32_t EncodingIdx = Entry >> 24;
6605
6606 uint32_t Encoding;
6607 if (EncodingIdx < CommonEncodings.size())
6608 Encoding = CommonEncodings[EncodingIdx];
6609 else
6610 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
6611
6612 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006613 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6614 << ", "
6615 << "encoding[" << EncodingIdx
6616 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006617 }
6618}
6619
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006620static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
6621 std::map<uint64_t, SymbolRef> &Symbols,
6622 const SectionRef &UnwindInfo) {
Tim Northover39c70bb2014-08-12 11:52:59 +00006623
6624 assert(Obj->isLittleEndian() &&
6625 "There should not be a big-endian .o with __unwind_info");
6626
6627 outs() << "Contents of __unwind_info section:\n";
6628
6629 StringRef Contents;
6630 UnwindInfo.getContents(Contents);
6631 const char *Pos = Contents.data();
6632
6633 //===----------------------------------
6634 // Section header
6635 //===----------------------------------
6636
6637 uint32_t Version = readNext<uint32_t>(Pos);
6638 outs() << " Version: "
6639 << format("0x%" PRIx32, Version) << '\n';
6640 assert(Version == 1 && "only understand version 1");
6641
6642 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
6643 outs() << " Common encodings array section offset: "
6644 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
6645 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
6646 outs() << " Number of common encodings in array: "
6647 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
6648
6649 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
6650 outs() << " Personality function array section offset: "
6651 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
6652 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
6653 outs() << " Number of personality functions in array: "
6654 << format("0x%" PRIx32, NumPersonalities) << '\n';
6655
6656 uint32_t IndicesStart = readNext<uint32_t>(Pos);
6657 outs() << " Index array section offset: "
6658 << format("0x%" PRIx32, IndicesStart) << '\n';
6659 uint32_t NumIndices = readNext<uint32_t>(Pos);
6660 outs() << " Number of indices in array: "
6661 << format("0x%" PRIx32, NumIndices) << '\n';
6662
6663 //===----------------------------------
6664 // A shared list of common encodings
6665 //===----------------------------------
6666
6667 // These occupy indices in the range [0, N] whenever an encoding is referenced
6668 // from a compressed 2nd level index table. In practice the linker only
6669 // creates ~128 of these, so that indices are available to embed encodings in
6670 // the 2nd level index.
6671
6672 SmallVector<uint32_t, 64> CommonEncodings;
6673 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
6674 Pos = Contents.data() + CommonEncodingsStart;
6675 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
6676 uint32_t Encoding = readNext<uint32_t>(Pos);
6677 CommonEncodings.push_back(Encoding);
6678
6679 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
6680 << '\n';
6681 }
6682
Tim Northover39c70bb2014-08-12 11:52:59 +00006683 //===----------------------------------
6684 // Personality functions used in this executable
6685 //===----------------------------------
6686
6687 // There should be only a handful of these (one per source language,
6688 // roughly). Particularly since they only get 2 bits in the compact encoding.
6689
6690 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
6691 Pos = Contents.data() + PersonalitiesStart;
6692 for (unsigned i = 0; i < NumPersonalities; ++i) {
6693 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
6694 outs() << " personality[" << i + 1
6695 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
6696 }
6697
6698 //===----------------------------------
6699 // The level 1 index entries
6700 //===----------------------------------
6701
6702 // These specify an approximate place to start searching for the more detailed
6703 // information, sorted by PC.
6704
6705 struct IndexEntry {
6706 uint32_t FunctionOffset;
6707 uint32_t SecondLevelPageStart;
6708 uint32_t LSDAStart;
6709 };
6710
6711 SmallVector<IndexEntry, 4> IndexEntries;
6712
6713 outs() << " Top level indices: (count = " << NumIndices << ")\n";
6714 Pos = Contents.data() + IndicesStart;
6715 for (unsigned i = 0; i < NumIndices; ++i) {
6716 IndexEntry Entry;
6717
6718 Entry.FunctionOffset = readNext<uint32_t>(Pos);
6719 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
6720 Entry.LSDAStart = readNext<uint32_t>(Pos);
6721 IndexEntries.push_back(Entry);
6722
6723 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006724 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
6725 << ", "
Tim Northover39c70bb2014-08-12 11:52:59 +00006726 << "2nd level page offset="
6727 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006728 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006729 }
6730
Tim Northover39c70bb2014-08-12 11:52:59 +00006731 //===----------------------------------
6732 // Next come the LSDA tables
6733 //===----------------------------------
6734
6735 // The LSDA layout is rather implicit: it's a contiguous array of entries from
6736 // the first top-level index's LSDAOffset to the last (sentinel).
6737
6738 outs() << " LSDA descriptors:\n";
6739 Pos = Contents.data() + IndexEntries[0].LSDAStart;
6740 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
6741 (2 * sizeof(uint32_t));
6742 for (int i = 0; i < NumLSDAs; ++i) {
6743 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6744 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
6745 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006746 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6747 << ", "
6748 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006749 }
6750
6751 //===----------------------------------
6752 // Finally, the 2nd level indices
6753 //===----------------------------------
6754
6755 // Generally these are 4K in size, and have 2 possible forms:
6756 // + Regular stores up to 511 entries with disparate encodings
6757 // + Compressed stores up to 1021 entries if few enough compact encoding
6758 // values are used.
6759 outs() << " Second level indices:\n";
6760 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
6761 // The final sentinel top-level index has no associated 2nd level page
6762 if (IndexEntries[i].SecondLevelPageStart == 0)
6763 break;
6764
6765 outs() << " Second level index[" << i << "]: "
6766 << "offset in section="
6767 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
6768 << ", "
6769 << "base function offset="
6770 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
6771
6772 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
Aaron Ballman80930af2014-08-14 13:53:19 +00006773 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00006774 if (Kind == 2)
6775 printRegularSecondLevelUnwindPage(Pos);
6776 else if (Kind == 3)
6777 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
6778 CommonEncodings);
6779 else
6780 llvm_unreachable("Do not know how to print this kind of 2nd level page");
Tim Northover39c70bb2014-08-12 11:52:59 +00006781 }
6782}
6783
Tim Northover4bd286a2014-08-01 13:07:19 +00006784void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
6785 std::map<uint64_t, SymbolRef> Symbols;
6786 for (const SymbolRef &SymRef : Obj->symbols()) {
6787 // Discard any undefined or absolute symbols. They're not going to take part
6788 // in the convenience lookup for unwind info and just take up resources.
6789 section_iterator Section = Obj->section_end();
6790 SymRef.getSection(Section);
6791 if (Section == Obj->section_end())
6792 continue;
6793
6794 uint64_t Addr;
6795 SymRef.getAddress(Addr);
6796 Symbols.insert(std::make_pair(Addr, SymRef));
6797 }
6798
6799 for (const SectionRef &Section : Obj->sections()) {
6800 StringRef SectName;
6801 Section.getName(SectName);
6802 if (SectName == "__compact_unwind")
6803 printMachOCompactUnwindSection(Obj, Symbols, Section);
6804 else if (SectName == "__unwind_info")
Tim Northover39c70bb2014-08-12 11:52:59 +00006805 printMachOUnwindInfoSection(Obj, Symbols, Section);
Tim Northover4bd286a2014-08-01 13:07:19 +00006806 else if (SectName == "__eh_frame")
6807 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00006808 }
6809}
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006810
6811static void PrintMachHeader(uint32_t magic, uint32_t cputype,
6812 uint32_t cpusubtype, uint32_t filetype,
6813 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
6814 bool verbose) {
6815 outs() << "Mach header\n";
6816 outs() << " magic cputype cpusubtype caps filetype ncmds "
6817 "sizeofcmds flags\n";
6818 if (verbose) {
6819 if (magic == MachO::MH_MAGIC)
6820 outs() << " MH_MAGIC";
6821 else if (magic == MachO::MH_MAGIC_64)
6822 outs() << "MH_MAGIC_64";
6823 else
6824 outs() << format(" 0x%08" PRIx32, magic);
6825 switch (cputype) {
6826 case MachO::CPU_TYPE_I386:
6827 outs() << " I386";
6828 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6829 case MachO::CPU_SUBTYPE_I386_ALL:
6830 outs() << " ALL";
6831 break;
6832 default:
6833 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6834 break;
6835 }
6836 break;
6837 case MachO::CPU_TYPE_X86_64:
6838 outs() << " X86_64";
Kevin Enderby131d1772015-01-09 19:22:37 +00006839 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6840 case MachO::CPU_SUBTYPE_X86_64_ALL:
6841 outs() << " ALL";
6842 break;
6843 case MachO::CPU_SUBTYPE_X86_64_H:
6844 outs() << " Haswell";
6845 break;
6846 default:
6847 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6848 break;
6849 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006850 break;
6851 case MachO::CPU_TYPE_ARM:
6852 outs() << " ARM";
6853 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6854 case MachO::CPU_SUBTYPE_ARM_ALL:
6855 outs() << " ALL";
6856 break;
6857 case MachO::CPU_SUBTYPE_ARM_V4T:
6858 outs() << " V4T";
6859 break;
6860 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6861 outs() << " V5TEJ";
6862 break;
6863 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6864 outs() << " XSCALE";
6865 break;
6866 case MachO::CPU_SUBTYPE_ARM_V6:
6867 outs() << " V6";
6868 break;
6869 case MachO::CPU_SUBTYPE_ARM_V6M:
6870 outs() << " V6M";
6871 break;
6872 case MachO::CPU_SUBTYPE_ARM_V7:
6873 outs() << " V7";
6874 break;
6875 case MachO::CPU_SUBTYPE_ARM_V7EM:
6876 outs() << " V7EM";
6877 break;
6878 case MachO::CPU_SUBTYPE_ARM_V7K:
6879 outs() << " V7K";
6880 break;
6881 case MachO::CPU_SUBTYPE_ARM_V7M:
6882 outs() << " V7M";
6883 break;
6884 case MachO::CPU_SUBTYPE_ARM_V7S:
6885 outs() << " V7S";
6886 break;
6887 default:
6888 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6889 break;
6890 }
6891 break;
6892 case MachO::CPU_TYPE_ARM64:
6893 outs() << " ARM64";
6894 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6895 case MachO::CPU_SUBTYPE_ARM64_ALL:
6896 outs() << " ALL";
6897 break;
6898 default:
6899 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6900 break;
6901 }
6902 break;
6903 case MachO::CPU_TYPE_POWERPC:
6904 outs() << " PPC";
6905 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6906 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6907 outs() << " ALL";
6908 break;
6909 default:
6910 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6911 break;
6912 }
6913 break;
6914 case MachO::CPU_TYPE_POWERPC64:
6915 outs() << " PPC64";
6916 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6917 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6918 outs() << " ALL";
6919 break;
6920 default:
6921 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6922 break;
6923 }
6924 break;
6925 }
6926 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00006927 outs() << " LIB64";
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006928 } else {
6929 outs() << format(" 0x%02" PRIx32,
6930 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6931 }
6932 switch (filetype) {
6933 case MachO::MH_OBJECT:
6934 outs() << " OBJECT";
6935 break;
6936 case MachO::MH_EXECUTE:
6937 outs() << " EXECUTE";
6938 break;
6939 case MachO::MH_FVMLIB:
6940 outs() << " FVMLIB";
6941 break;
6942 case MachO::MH_CORE:
6943 outs() << " CORE";
6944 break;
6945 case MachO::MH_PRELOAD:
6946 outs() << " PRELOAD";
6947 break;
6948 case MachO::MH_DYLIB:
6949 outs() << " DYLIB";
6950 break;
6951 case MachO::MH_DYLIB_STUB:
6952 outs() << " DYLIB_STUB";
6953 break;
6954 case MachO::MH_DYLINKER:
6955 outs() << " DYLINKER";
6956 break;
6957 case MachO::MH_BUNDLE:
6958 outs() << " BUNDLE";
6959 break;
6960 case MachO::MH_DSYM:
6961 outs() << " DSYM";
6962 break;
6963 case MachO::MH_KEXT_BUNDLE:
6964 outs() << " KEXTBUNDLE";
6965 break;
6966 default:
6967 outs() << format(" %10u", filetype);
6968 break;
6969 }
6970 outs() << format(" %5u", ncmds);
6971 outs() << format(" %10u", sizeofcmds);
6972 uint32_t f = flags;
6973 if (f & MachO::MH_NOUNDEFS) {
6974 outs() << " NOUNDEFS";
6975 f &= ~MachO::MH_NOUNDEFS;
6976 }
6977 if (f & MachO::MH_INCRLINK) {
6978 outs() << " INCRLINK";
6979 f &= ~MachO::MH_INCRLINK;
6980 }
6981 if (f & MachO::MH_DYLDLINK) {
6982 outs() << " DYLDLINK";
6983 f &= ~MachO::MH_DYLDLINK;
6984 }
6985 if (f & MachO::MH_BINDATLOAD) {
6986 outs() << " BINDATLOAD";
6987 f &= ~MachO::MH_BINDATLOAD;
6988 }
6989 if (f & MachO::MH_PREBOUND) {
6990 outs() << " PREBOUND";
6991 f &= ~MachO::MH_PREBOUND;
6992 }
6993 if (f & MachO::MH_SPLIT_SEGS) {
6994 outs() << " SPLIT_SEGS";
6995 f &= ~MachO::MH_SPLIT_SEGS;
6996 }
6997 if (f & MachO::MH_LAZY_INIT) {
6998 outs() << " LAZY_INIT";
6999 f &= ~MachO::MH_LAZY_INIT;
7000 }
7001 if (f & MachO::MH_TWOLEVEL) {
7002 outs() << " TWOLEVEL";
7003 f &= ~MachO::MH_TWOLEVEL;
7004 }
7005 if (f & MachO::MH_FORCE_FLAT) {
7006 outs() << " FORCE_FLAT";
7007 f &= ~MachO::MH_FORCE_FLAT;
7008 }
7009 if (f & MachO::MH_NOMULTIDEFS) {
7010 outs() << " NOMULTIDEFS";
7011 f &= ~MachO::MH_NOMULTIDEFS;
7012 }
7013 if (f & MachO::MH_NOFIXPREBINDING) {
7014 outs() << " NOFIXPREBINDING";
7015 f &= ~MachO::MH_NOFIXPREBINDING;
7016 }
7017 if (f & MachO::MH_PREBINDABLE) {
7018 outs() << " PREBINDABLE";
7019 f &= ~MachO::MH_PREBINDABLE;
7020 }
7021 if (f & MachO::MH_ALLMODSBOUND) {
7022 outs() << " ALLMODSBOUND";
7023 f &= ~MachO::MH_ALLMODSBOUND;
7024 }
7025 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7026 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7027 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7028 }
7029 if (f & MachO::MH_CANONICAL) {
7030 outs() << " CANONICAL";
7031 f &= ~MachO::MH_CANONICAL;
7032 }
7033 if (f & MachO::MH_WEAK_DEFINES) {
7034 outs() << " WEAK_DEFINES";
7035 f &= ~MachO::MH_WEAK_DEFINES;
7036 }
7037 if (f & MachO::MH_BINDS_TO_WEAK) {
7038 outs() << " BINDS_TO_WEAK";
7039 f &= ~MachO::MH_BINDS_TO_WEAK;
7040 }
7041 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7042 outs() << " ALLOW_STACK_EXECUTION";
7043 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7044 }
7045 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7046 outs() << " DEAD_STRIPPABLE_DYLIB";
7047 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7048 }
7049 if (f & MachO::MH_PIE) {
7050 outs() << " PIE";
7051 f &= ~MachO::MH_PIE;
7052 }
7053 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7054 outs() << " NO_REEXPORTED_DYLIBS";
7055 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7056 }
7057 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7058 outs() << " MH_HAS_TLV_DESCRIPTORS";
7059 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7060 }
7061 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7062 outs() << " MH_NO_HEAP_EXECUTION";
7063 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7064 }
7065 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7066 outs() << " APP_EXTENSION_SAFE";
7067 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7068 }
7069 if (f != 0 || flags == 0)
7070 outs() << format(" 0x%08" PRIx32, f);
7071 } else {
7072 outs() << format(" 0x%08" PRIx32, magic);
7073 outs() << format(" %7d", cputype);
7074 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7075 outs() << format(" 0x%02" PRIx32,
7076 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7077 outs() << format(" %10u", filetype);
7078 outs() << format(" %5u", ncmds);
7079 outs() << format(" %10u", sizeofcmds);
7080 outs() << format(" 0x%08" PRIx32, flags);
7081 }
7082 outs() << "\n";
7083}
7084
Kevin Enderby956366c2014-08-29 22:30:52 +00007085static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7086 StringRef SegName, uint64_t vmaddr,
7087 uint64_t vmsize, uint64_t fileoff,
7088 uint64_t filesize, uint32_t maxprot,
7089 uint32_t initprot, uint32_t nsects,
7090 uint32_t flags, uint32_t object_size,
7091 bool verbose) {
7092 uint64_t expected_cmdsize;
7093 if (cmd == MachO::LC_SEGMENT) {
7094 outs() << " cmd LC_SEGMENT\n";
7095 expected_cmdsize = nsects;
7096 expected_cmdsize *= sizeof(struct MachO::section);
7097 expected_cmdsize += sizeof(struct MachO::segment_command);
7098 } else {
7099 outs() << " cmd LC_SEGMENT_64\n";
7100 expected_cmdsize = nsects;
7101 expected_cmdsize *= sizeof(struct MachO::section_64);
7102 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7103 }
7104 outs() << " cmdsize " << cmdsize;
7105 if (cmdsize != expected_cmdsize)
7106 outs() << " Inconsistent size\n";
7107 else
7108 outs() << "\n";
7109 outs() << " segname " << SegName << "\n";
7110 if (cmd == MachO::LC_SEGMENT_64) {
7111 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7112 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7113 } else {
Kevin Enderbyadb7c432014-12-16 18:58:11 +00007114 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7115 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007116 }
7117 outs() << " fileoff " << fileoff;
7118 if (fileoff > object_size)
7119 outs() << " (past end of file)\n";
7120 else
7121 outs() << "\n";
7122 outs() << " filesize " << filesize;
7123 if (fileoff + filesize > object_size)
7124 outs() << " (past end of file)\n";
7125 else
7126 outs() << "\n";
7127 if (verbose) {
7128 if ((maxprot &
7129 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7130 MachO::VM_PROT_EXECUTE)) != 0)
7131 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7132 else {
7133 if (maxprot & MachO::VM_PROT_READ)
7134 outs() << " maxprot r";
7135 else
7136 outs() << " maxprot -";
7137 if (maxprot & MachO::VM_PROT_WRITE)
7138 outs() << "w";
7139 else
7140 outs() << "-";
7141 if (maxprot & MachO::VM_PROT_EXECUTE)
7142 outs() << "x\n";
7143 else
7144 outs() << "-\n";
7145 }
7146 if ((initprot &
7147 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7148 MachO::VM_PROT_EXECUTE)) != 0)
7149 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7150 else {
7151 if (initprot & MachO::VM_PROT_READ)
7152 outs() << " initprot r";
7153 else
7154 outs() << " initprot -";
7155 if (initprot & MachO::VM_PROT_WRITE)
7156 outs() << "w";
7157 else
7158 outs() << "-";
7159 if (initprot & MachO::VM_PROT_EXECUTE)
7160 outs() << "x\n";
7161 else
7162 outs() << "-\n";
7163 }
7164 } else {
7165 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7166 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7167 }
7168 outs() << " nsects " << nsects << "\n";
7169 if (verbose) {
7170 outs() << " flags";
7171 if (flags == 0)
7172 outs() << " (none)\n";
7173 else {
7174 if (flags & MachO::SG_HIGHVM) {
7175 outs() << " HIGHVM";
7176 flags &= ~MachO::SG_HIGHVM;
7177 }
7178 if (flags & MachO::SG_FVMLIB) {
7179 outs() << " FVMLIB";
7180 flags &= ~MachO::SG_FVMLIB;
7181 }
7182 if (flags & MachO::SG_NORELOC) {
7183 outs() << " NORELOC";
7184 flags &= ~MachO::SG_NORELOC;
7185 }
7186 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7187 outs() << " PROTECTED_VERSION_1";
7188 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7189 }
7190 if (flags)
7191 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7192 else
7193 outs() << "\n";
7194 }
7195 } else {
7196 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7197 }
7198}
7199
7200static void PrintSection(const char *sectname, const char *segname,
7201 uint64_t addr, uint64_t size, uint32_t offset,
7202 uint32_t align, uint32_t reloff, uint32_t nreloc,
7203 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7204 uint32_t cmd, const char *sg_segname,
7205 uint32_t filetype, uint32_t object_size,
7206 bool verbose) {
7207 outs() << "Section\n";
7208 outs() << " sectname " << format("%.16s\n", sectname);
7209 outs() << " segname " << format("%.16s", segname);
7210 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7211 outs() << " (does not match segment)\n";
7212 else
7213 outs() << "\n";
7214 if (cmd == MachO::LC_SEGMENT_64) {
7215 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7216 outs() << " size " << format("0x%016" PRIx64, size);
7217 } else {
Kevin Enderby75594b62014-12-16 21:00:25 +00007218 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7219 outs() << " size " << format("0x%08" PRIx64, size);
Kevin Enderby956366c2014-08-29 22:30:52 +00007220 }
7221 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7222 outs() << " (past end of file)\n";
7223 else
7224 outs() << "\n";
7225 outs() << " offset " << offset;
7226 if (offset > object_size)
7227 outs() << " (past end of file)\n";
7228 else
7229 outs() << "\n";
7230 uint32_t align_shifted = 1 << align;
7231 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7232 outs() << " reloff " << reloff;
7233 if (reloff > object_size)
7234 outs() << " (past end of file)\n";
7235 else
7236 outs() << "\n";
7237 outs() << " nreloc " << nreloc;
7238 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7239 outs() << " (past end of file)\n";
7240 else
7241 outs() << "\n";
7242 uint32_t section_type = flags & MachO::SECTION_TYPE;
7243 if (verbose) {
7244 outs() << " type";
7245 if (section_type == MachO::S_REGULAR)
7246 outs() << " S_REGULAR\n";
7247 else if (section_type == MachO::S_ZEROFILL)
7248 outs() << " S_ZEROFILL\n";
7249 else if (section_type == MachO::S_CSTRING_LITERALS)
7250 outs() << " S_CSTRING_LITERALS\n";
7251 else if (section_type == MachO::S_4BYTE_LITERALS)
7252 outs() << " S_4BYTE_LITERALS\n";
7253 else if (section_type == MachO::S_8BYTE_LITERALS)
7254 outs() << " S_8BYTE_LITERALS\n";
7255 else if (section_type == MachO::S_16BYTE_LITERALS)
7256 outs() << " S_16BYTE_LITERALS\n";
7257 else if (section_type == MachO::S_LITERAL_POINTERS)
7258 outs() << " S_LITERAL_POINTERS\n";
7259 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7260 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7261 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7262 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7263 else if (section_type == MachO::S_SYMBOL_STUBS)
7264 outs() << " S_SYMBOL_STUBS\n";
7265 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7266 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7267 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7268 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7269 else if (section_type == MachO::S_COALESCED)
7270 outs() << " S_COALESCED\n";
7271 else if (section_type == MachO::S_INTERPOSING)
7272 outs() << " S_INTERPOSING\n";
7273 else if (section_type == MachO::S_DTRACE_DOF)
7274 outs() << " S_DTRACE_DOF\n";
7275 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7276 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7277 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7278 outs() << " S_THREAD_LOCAL_REGULAR\n";
7279 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7280 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7281 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7282 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7283 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7284 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7285 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7286 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7287 else
7288 outs() << format("0x%08" PRIx32, section_type) << "\n";
7289 outs() << "attributes";
7290 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7291 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7292 outs() << " PURE_INSTRUCTIONS";
7293 if (section_attributes & MachO::S_ATTR_NO_TOC)
7294 outs() << " NO_TOC";
7295 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7296 outs() << " STRIP_STATIC_SYMS";
7297 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7298 outs() << " NO_DEAD_STRIP";
7299 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7300 outs() << " LIVE_SUPPORT";
7301 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7302 outs() << " SELF_MODIFYING_CODE";
7303 if (section_attributes & MachO::S_ATTR_DEBUG)
7304 outs() << " DEBUG";
7305 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7306 outs() << " SOME_INSTRUCTIONS";
7307 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7308 outs() << " EXT_RELOC";
7309 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7310 outs() << " LOC_RELOC";
7311 if (section_attributes == 0)
7312 outs() << " (none)";
7313 outs() << "\n";
7314 } else
7315 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7316 outs() << " reserved1 " << reserved1;
7317 if (section_type == MachO::S_SYMBOL_STUBS ||
7318 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7319 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7320 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7321 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7322 outs() << " (index into indirect symbol table)\n";
7323 else
7324 outs() << "\n";
7325 outs() << " reserved2 " << reserved2;
7326 if (section_type == MachO::S_SYMBOL_STUBS)
7327 outs() << " (size of stubs)\n";
7328 else
7329 outs() << "\n";
7330}
7331
David Majnemer73cc6ff2014-11-13 19:48:56 +00007332static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
Kevin Enderby956366c2014-08-29 22:30:52 +00007333 uint32_t object_size) {
7334 outs() << " cmd LC_SYMTAB\n";
7335 outs() << " cmdsize " << st.cmdsize;
7336 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7337 outs() << " Incorrect size\n";
7338 else
7339 outs() << "\n";
7340 outs() << " symoff " << st.symoff;
7341 if (st.symoff > object_size)
7342 outs() << " (past end of file)\n";
7343 else
7344 outs() << "\n";
7345 outs() << " nsyms " << st.nsyms;
7346 uint64_t big_size;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007347 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007348 big_size = st.nsyms;
7349 big_size *= sizeof(struct MachO::nlist_64);
7350 big_size += st.symoff;
7351 if (big_size > object_size)
7352 outs() << " (past end of file)\n";
7353 else
7354 outs() << "\n";
7355 } else {
7356 big_size = st.nsyms;
7357 big_size *= sizeof(struct MachO::nlist);
7358 big_size += st.symoff;
7359 if (big_size > object_size)
7360 outs() << " (past end of file)\n";
7361 else
7362 outs() << "\n";
7363 }
7364 outs() << " stroff " << st.stroff;
7365 if (st.stroff > object_size)
7366 outs() << " (past end of file)\n";
7367 else
7368 outs() << "\n";
7369 outs() << " strsize " << st.strsize;
7370 big_size = st.stroff;
7371 big_size += st.strsize;
7372 if (big_size > object_size)
7373 outs() << " (past end of file)\n";
7374 else
7375 outs() << "\n";
7376}
7377
7378static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7379 uint32_t nsyms, uint32_t object_size,
David Majnemer73cc6ff2014-11-13 19:48:56 +00007380 bool Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007381 outs() << " cmd LC_DYSYMTAB\n";
7382 outs() << " cmdsize " << dyst.cmdsize;
7383 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7384 outs() << " Incorrect size\n";
7385 else
7386 outs() << "\n";
7387 outs() << " ilocalsym " << dyst.ilocalsym;
7388 if (dyst.ilocalsym > nsyms)
7389 outs() << " (greater than the number of symbols)\n";
7390 else
7391 outs() << "\n";
7392 outs() << " nlocalsym " << dyst.nlocalsym;
7393 uint64_t big_size;
7394 big_size = dyst.ilocalsym;
7395 big_size += dyst.nlocalsym;
7396 if (big_size > nsyms)
7397 outs() << " (past the end of the symbol table)\n";
7398 else
7399 outs() << "\n";
7400 outs() << " iextdefsym " << dyst.iextdefsym;
7401 if (dyst.iextdefsym > nsyms)
7402 outs() << " (greater than the number of symbols)\n";
7403 else
7404 outs() << "\n";
7405 outs() << " nextdefsym " << dyst.nextdefsym;
7406 big_size = dyst.iextdefsym;
7407 big_size += dyst.nextdefsym;
7408 if (big_size > nsyms)
7409 outs() << " (past the end of the symbol table)\n";
7410 else
7411 outs() << "\n";
7412 outs() << " iundefsym " << dyst.iundefsym;
7413 if (dyst.iundefsym > nsyms)
7414 outs() << " (greater than the number of symbols)\n";
7415 else
7416 outs() << "\n";
7417 outs() << " nundefsym " << dyst.nundefsym;
7418 big_size = dyst.iundefsym;
7419 big_size += dyst.nundefsym;
7420 if (big_size > nsyms)
7421 outs() << " (past the end of the symbol table)\n";
7422 else
7423 outs() << "\n";
7424 outs() << " tocoff " << dyst.tocoff;
7425 if (dyst.tocoff > object_size)
7426 outs() << " (past end of file)\n";
7427 else
7428 outs() << "\n";
7429 outs() << " ntoc " << dyst.ntoc;
7430 big_size = dyst.ntoc;
7431 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7432 big_size += dyst.tocoff;
7433 if (big_size > object_size)
7434 outs() << " (past end of file)\n";
7435 else
7436 outs() << "\n";
7437 outs() << " modtaboff " << dyst.modtaboff;
7438 if (dyst.modtaboff > object_size)
7439 outs() << " (past end of file)\n";
7440 else
7441 outs() << "\n";
7442 outs() << " nmodtab " << dyst.nmodtab;
7443 uint64_t modtabend;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007444 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007445 modtabend = dyst.nmodtab;
7446 modtabend *= sizeof(struct MachO::dylib_module_64);
7447 modtabend += dyst.modtaboff;
7448 } else {
7449 modtabend = dyst.nmodtab;
7450 modtabend *= sizeof(struct MachO::dylib_module);
7451 modtabend += dyst.modtaboff;
7452 }
7453 if (modtabend > object_size)
7454 outs() << " (past end of file)\n";
7455 else
7456 outs() << "\n";
7457 outs() << " extrefsymoff " << dyst.extrefsymoff;
7458 if (dyst.extrefsymoff > object_size)
7459 outs() << " (past end of file)\n";
7460 else
7461 outs() << "\n";
7462 outs() << " nextrefsyms " << dyst.nextrefsyms;
7463 big_size = dyst.nextrefsyms;
7464 big_size *= sizeof(struct MachO::dylib_reference);
7465 big_size += dyst.extrefsymoff;
7466 if (big_size > object_size)
7467 outs() << " (past end of file)\n";
7468 else
7469 outs() << "\n";
7470 outs() << " indirectsymoff " << dyst.indirectsymoff;
7471 if (dyst.indirectsymoff > object_size)
7472 outs() << " (past end of file)\n";
7473 else
7474 outs() << "\n";
7475 outs() << " nindirectsyms " << dyst.nindirectsyms;
7476 big_size = dyst.nindirectsyms;
7477 big_size *= sizeof(uint32_t);
7478 big_size += dyst.indirectsymoff;
7479 if (big_size > object_size)
7480 outs() << " (past end of file)\n";
7481 else
7482 outs() << "\n";
7483 outs() << " extreloff " << dyst.extreloff;
7484 if (dyst.extreloff > object_size)
7485 outs() << " (past end of file)\n";
7486 else
7487 outs() << "\n";
7488 outs() << " nextrel " << dyst.nextrel;
7489 big_size = dyst.nextrel;
7490 big_size *= sizeof(struct MachO::relocation_info);
7491 big_size += dyst.extreloff;
7492 if (big_size > object_size)
7493 outs() << " (past end of file)\n";
7494 else
7495 outs() << "\n";
7496 outs() << " locreloff " << dyst.locreloff;
7497 if (dyst.locreloff > object_size)
7498 outs() << " (past end of file)\n";
7499 else
7500 outs() << "\n";
7501 outs() << " nlocrel " << dyst.nlocrel;
7502 big_size = dyst.nlocrel;
7503 big_size *= sizeof(struct MachO::relocation_info);
7504 big_size += dyst.locreloff;
7505 if (big_size > object_size)
7506 outs() << " (past end of file)\n";
7507 else
7508 outs() << "\n";
7509}
7510
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007511static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
7512 uint32_t object_size) {
7513 if (dc.cmd == MachO::LC_DYLD_INFO)
7514 outs() << " cmd LC_DYLD_INFO\n";
7515 else
7516 outs() << " cmd LC_DYLD_INFO_ONLY\n";
7517 outs() << " cmdsize " << dc.cmdsize;
7518 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
7519 outs() << " Incorrect size\n";
7520 else
7521 outs() << "\n";
7522 outs() << " rebase_off " << dc.rebase_off;
7523 if (dc.rebase_off > object_size)
7524 outs() << " (past end of file)\n";
7525 else
7526 outs() << "\n";
7527 outs() << " rebase_size " << dc.rebase_size;
7528 uint64_t big_size;
7529 big_size = dc.rebase_off;
7530 big_size += dc.rebase_size;
7531 if (big_size > object_size)
7532 outs() << " (past end of file)\n";
7533 else
7534 outs() << "\n";
7535 outs() << " bind_off " << dc.bind_off;
7536 if (dc.bind_off > object_size)
7537 outs() << " (past end of file)\n";
7538 else
7539 outs() << "\n";
7540 outs() << " bind_size " << dc.bind_size;
7541 big_size = dc.bind_off;
7542 big_size += dc.bind_size;
7543 if (big_size > object_size)
7544 outs() << " (past end of file)\n";
7545 else
7546 outs() << "\n";
7547 outs() << " weak_bind_off " << dc.weak_bind_off;
7548 if (dc.weak_bind_off > object_size)
7549 outs() << " (past end of file)\n";
7550 else
7551 outs() << "\n";
7552 outs() << " weak_bind_size " << dc.weak_bind_size;
7553 big_size = dc.weak_bind_off;
7554 big_size += dc.weak_bind_size;
7555 if (big_size > object_size)
7556 outs() << " (past end of file)\n";
7557 else
7558 outs() << "\n";
7559 outs() << " lazy_bind_off " << dc.lazy_bind_off;
7560 if (dc.lazy_bind_off > object_size)
7561 outs() << " (past end of file)\n";
7562 else
7563 outs() << "\n";
7564 outs() << " lazy_bind_size " << dc.lazy_bind_size;
7565 big_size = dc.lazy_bind_off;
7566 big_size += dc.lazy_bind_size;
7567 if (big_size > object_size)
7568 outs() << " (past end of file)\n";
7569 else
7570 outs() << "\n";
7571 outs() << " export_off " << dc.export_off;
7572 if (dc.export_off > object_size)
7573 outs() << " (past end of file)\n";
7574 else
7575 outs() << "\n";
7576 outs() << " export_size " << dc.export_size;
7577 big_size = dc.export_off;
7578 big_size += dc.export_size;
7579 if (big_size > object_size)
7580 outs() << " (past end of file)\n";
7581 else
7582 outs() << "\n";
7583}
7584
7585static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
7586 const char *Ptr) {
7587 if (dyld.cmd == MachO::LC_ID_DYLINKER)
7588 outs() << " cmd LC_ID_DYLINKER\n";
7589 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
7590 outs() << " cmd LC_LOAD_DYLINKER\n";
7591 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
7592 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
7593 else
7594 outs() << " cmd ?(" << dyld.cmd << ")\n";
7595 outs() << " cmdsize " << dyld.cmdsize;
7596 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
7597 outs() << " Incorrect size\n";
7598 else
7599 outs() << "\n";
7600 if (dyld.name >= dyld.cmdsize)
7601 outs() << " name ?(bad offset " << dyld.name << ")\n";
7602 else {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007603 const char *P = (const char *)(Ptr) + dyld.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007604 outs() << " name " << P << " (offset " << dyld.name << ")\n";
7605 }
7606}
7607
7608static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
7609 outs() << " cmd LC_UUID\n";
7610 outs() << " cmdsize " << uuid.cmdsize;
7611 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
7612 outs() << " Incorrect size\n";
7613 else
7614 outs() << "\n";
7615 outs() << " uuid ";
7616 outs() << format("%02" PRIX32, uuid.uuid[0]);
7617 outs() << format("%02" PRIX32, uuid.uuid[1]);
7618 outs() << format("%02" PRIX32, uuid.uuid[2]);
7619 outs() << format("%02" PRIX32, uuid.uuid[3]);
7620 outs() << "-";
7621 outs() << format("%02" PRIX32, uuid.uuid[4]);
7622 outs() << format("%02" PRIX32, uuid.uuid[5]);
7623 outs() << "-";
7624 outs() << format("%02" PRIX32, uuid.uuid[6]);
7625 outs() << format("%02" PRIX32, uuid.uuid[7]);
7626 outs() << "-";
7627 outs() << format("%02" PRIX32, uuid.uuid[8]);
7628 outs() << format("%02" PRIX32, uuid.uuid[9]);
7629 outs() << "-";
7630 outs() << format("%02" PRIX32, uuid.uuid[10]);
7631 outs() << format("%02" PRIX32, uuid.uuid[11]);
7632 outs() << format("%02" PRIX32, uuid.uuid[12]);
7633 outs() << format("%02" PRIX32, uuid.uuid[13]);
7634 outs() << format("%02" PRIX32, uuid.uuid[14]);
7635 outs() << format("%02" PRIX32, uuid.uuid[15]);
7636 outs() << "\n";
7637}
7638
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007639static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00007640 outs() << " cmd LC_RPATH\n";
7641 outs() << " cmdsize " << rpath.cmdsize;
7642 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
7643 outs() << " Incorrect size\n";
7644 else
7645 outs() << "\n";
7646 if (rpath.path >= rpath.cmdsize)
7647 outs() << " path ?(bad offset " << rpath.path << ")\n";
7648 else {
7649 const char *P = (const char *)(Ptr) + rpath.path;
7650 outs() << " path " << P << " (offset " << rpath.path << ")\n";
7651 }
7652}
7653
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007654static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
7655 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
7656 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
7657 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
7658 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
7659 else
7660 outs() << " cmd " << vd.cmd << " (?)\n";
7661 outs() << " cmdsize " << vd.cmdsize;
7662 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
7663 outs() << " Incorrect size\n";
7664 else
7665 outs() << "\n";
7666 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
7667 << ((vd.version >> 8) & 0xff);
7668 if ((vd.version & 0xff) != 0)
7669 outs() << "." << (vd.version & 0xff);
7670 outs() << "\n";
7671 if (vd.sdk == 0)
Kevin Enderby57538292014-12-17 01:01:30 +00007672 outs() << " sdk n/a";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007673 else {
7674 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
7675 << ((vd.sdk >> 8) & 0xff);
7676 }
7677 if ((vd.sdk & 0xff) != 0)
7678 outs() << "." << (vd.sdk & 0xff);
7679 outs() << "\n";
7680}
7681
7682static void PrintSourceVersionCommand(MachO::source_version_command sd) {
7683 outs() << " cmd LC_SOURCE_VERSION\n";
7684 outs() << " cmdsize " << sd.cmdsize;
7685 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
7686 outs() << " Incorrect size\n";
7687 else
7688 outs() << "\n";
7689 uint64_t a = (sd.version >> 40) & 0xffffff;
7690 uint64_t b = (sd.version >> 30) & 0x3ff;
7691 uint64_t c = (sd.version >> 20) & 0x3ff;
7692 uint64_t d = (sd.version >> 10) & 0x3ff;
7693 uint64_t e = sd.version & 0x3ff;
7694 outs() << " version " << a << "." << b;
7695 if (e != 0)
7696 outs() << "." << c << "." << d << "." << e;
7697 else if (d != 0)
7698 outs() << "." << c << "." << d;
7699 else if (c != 0)
7700 outs() << "." << c;
7701 outs() << "\n";
7702}
7703
7704static void PrintEntryPointCommand(MachO::entry_point_command ep) {
7705 outs() << " cmd LC_MAIN\n";
7706 outs() << " cmdsize " << ep.cmdsize;
7707 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
7708 outs() << " Incorrect size\n";
7709 else
7710 outs() << "\n";
7711 outs() << " entryoff " << ep.entryoff << "\n";
7712 outs() << " stacksize " << ep.stacksize << "\n";
7713}
7714
Kevin Enderby0804f4672014-12-16 23:25:52 +00007715static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
7716 uint32_t object_size) {
7717 outs() << " cmd LC_ENCRYPTION_INFO\n";
7718 outs() << " cmdsize " << ec.cmdsize;
7719 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
7720 outs() << " Incorrect size\n";
7721 else
7722 outs() << "\n";
7723 outs() << " cryptoff " << ec.cryptoff;
7724 if (ec.cryptoff > object_size)
7725 outs() << " (past end of file)\n";
7726 else
7727 outs() << "\n";
7728 outs() << " cryptsize " << ec.cryptsize;
7729 if (ec.cryptsize > object_size)
7730 outs() << " (past end of file)\n";
7731 else
7732 outs() << "\n";
7733 outs() << " cryptid " << ec.cryptid << "\n";
7734}
7735
Kevin Enderby57538292014-12-17 01:01:30 +00007736static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007737 uint32_t object_size) {
Kevin Enderby57538292014-12-17 01:01:30 +00007738 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
7739 outs() << " cmdsize " << ec.cmdsize;
7740 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
7741 outs() << " Incorrect size\n";
7742 else
7743 outs() << "\n";
7744 outs() << " cryptoff " << ec.cryptoff;
7745 if (ec.cryptoff > object_size)
7746 outs() << " (past end of file)\n";
7747 else
7748 outs() << "\n";
7749 outs() << " cryptsize " << ec.cryptsize;
7750 if (ec.cryptsize > object_size)
7751 outs() << " (past end of file)\n";
7752 else
7753 outs() << "\n";
7754 outs() << " cryptid " << ec.cryptid << "\n";
7755 outs() << " pad " << ec.pad << "\n";
7756}
7757
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007758static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
7759 const char *Ptr) {
7760 outs() << " cmd LC_LINKER_OPTION\n";
7761 outs() << " cmdsize " << lo.cmdsize;
7762 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
7763 outs() << " Incorrect size\n";
7764 else
7765 outs() << "\n";
7766 outs() << " count " << lo.count << "\n";
7767 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
7768 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
7769 uint32_t i = 0;
7770 while (left > 0) {
7771 while (*string == '\0' && left > 0) {
7772 string++;
7773 left--;
7774 }
7775 if (left > 0) {
7776 i++;
7777 outs() << " string #" << i << " " << format("%.*s\n", left, string);
David Majnemerd4449ed2014-12-20 08:24:43 +00007778 uint32_t NullPos = StringRef(string, left).find('\0');
7779 uint32_t len = std::min(NullPos, left) + 1;
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007780 string += len;
7781 left -= len;
7782 }
7783 }
7784 if (lo.count != i)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007785 outs() << " count " << lo.count << " does not match number of strings "
7786 << i << "\n";
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007787}
7788
Kevin Enderbyb4b79312014-12-18 19:24:35 +00007789static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
7790 const char *Ptr) {
7791 outs() << " cmd LC_SUB_FRAMEWORK\n";
7792 outs() << " cmdsize " << sub.cmdsize;
7793 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
7794 outs() << " Incorrect size\n";
7795 else
7796 outs() << "\n";
7797 if (sub.umbrella < sub.cmdsize) {
7798 const char *P = Ptr + sub.umbrella;
7799 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
7800 } else {
7801 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
7802 }
7803}
7804
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00007805static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
7806 const char *Ptr) {
7807 outs() << " cmd LC_SUB_UMBRELLA\n";
7808 outs() << " cmdsize " << sub.cmdsize;
7809 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
7810 outs() << " Incorrect size\n";
7811 else
7812 outs() << "\n";
7813 if (sub.sub_umbrella < sub.cmdsize) {
7814 const char *P = Ptr + sub.sub_umbrella;
7815 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
7816 } else {
7817 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
7818 }
7819}
7820
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007821static void PrintSubLibraryCommand(MachO::sub_library_command sub,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007822 const char *Ptr) {
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007823 outs() << " cmd LC_SUB_LIBRARY\n";
7824 outs() << " cmdsize " << sub.cmdsize;
7825 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
7826 outs() << " Incorrect size\n";
7827 else
7828 outs() << "\n";
7829 if (sub.sub_library < sub.cmdsize) {
7830 const char *P = Ptr + sub.sub_library;
7831 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7832 } else {
7833 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7834 }
7835}
7836
Kevin Enderby186eac32014-12-19 21:06:24 +00007837static void PrintSubClientCommand(MachO::sub_client_command sub,
7838 const char *Ptr) {
7839 outs() << " cmd LC_SUB_CLIENT\n";
7840 outs() << " cmdsize " << sub.cmdsize;
7841 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7842 outs() << " Incorrect size\n";
7843 else
7844 outs() << "\n";
7845 if (sub.client < sub.cmdsize) {
7846 const char *P = Ptr + sub.client;
7847 outs() << " client " << P << " (offset " << sub.client << ")\n";
7848 } else {
7849 outs() << " client ?(bad offset " << sub.client << ")\n";
7850 }
7851}
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007852
Kevin Enderby52e4ce42014-12-19 22:25:22 +00007853static void PrintRoutinesCommand(MachO::routines_command r) {
7854 outs() << " cmd LC_ROUTINES\n";
7855 outs() << " cmdsize " << r.cmdsize;
7856 if (r.cmdsize != sizeof(struct MachO::routines_command))
7857 outs() << " Incorrect size\n";
7858 else
7859 outs() << "\n";
7860 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7861 outs() << " init_module " << r.init_module << "\n";
7862 outs() << " reserved1 " << r.reserved1 << "\n";
7863 outs() << " reserved2 " << r.reserved2 << "\n";
7864 outs() << " reserved3 " << r.reserved3 << "\n";
7865 outs() << " reserved4 " << r.reserved4 << "\n";
7866 outs() << " reserved5 " << r.reserved5 << "\n";
7867 outs() << " reserved6 " << r.reserved6 << "\n";
7868}
7869
7870static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7871 outs() << " cmd LC_ROUTINES_64\n";
7872 outs() << " cmdsize " << r.cmdsize;
7873 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7874 outs() << " Incorrect size\n";
7875 else
7876 outs() << "\n";
7877 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
7878 outs() << " init_module " << r.init_module << "\n";
7879 outs() << " reserved1 " << r.reserved1 << "\n";
7880 outs() << " reserved2 " << r.reserved2 << "\n";
7881 outs() << " reserved3 " << r.reserved3 << "\n";
7882 outs() << " reserved4 " << r.reserved4 << "\n";
7883 outs() << " reserved5 " << r.reserved5 << "\n";
7884 outs() << " reserved6 " << r.reserved6 << "\n";
7885}
7886
Kevin Enderby48ef5342014-12-23 22:56:39 +00007887static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7888 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7889 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7890 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7891 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7892 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7893 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7894 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7895 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7896 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7897 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7898 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7899 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7900 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7901 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7902 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7903 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7904 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7905 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7906 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7907 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7908 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7909}
7910
Kevin Enderby227df342014-12-23 23:43:59 +00007911static void Print_mmst_reg(MachO::mmst_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007912 uint32_t f;
7913 outs() << "\t mmst_reg ";
7914 for (f = 0; f < 10; f++)
7915 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7916 outs() << "\n";
7917 outs() << "\t mmst_rsrv ";
7918 for (f = 0; f < 6; f++)
7919 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7920 outs() << "\n";
7921}
7922
Kevin Enderbyaefb0032014-12-24 00:16:51 +00007923static void Print_xmm_reg(MachO::xmm_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007924 uint32_t f;
7925 outs() << "\t xmm_reg ";
7926 for (f = 0; f < 16; f++)
7927 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7928 outs() << "\n";
7929}
7930
7931static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7932 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7933 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7934 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7935 outs() << " denorm " << fpu.fpu_fcw.denorm;
7936 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7937 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7938 outs() << " undfl " << fpu.fpu_fcw.undfl;
7939 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7940 outs() << "\t\t pc ";
7941 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7942 outs() << "FP_PREC_24B ";
7943 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
7944 outs() << "FP_PREC_53B ";
7945 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
7946 outs() << "FP_PREC_64B ";
7947 else
7948 outs() << fpu.fpu_fcw.pc << " ";
7949 outs() << "rc ";
7950 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
7951 outs() << "FP_RND_NEAR ";
7952 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
7953 outs() << "FP_RND_DOWN ";
7954 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
7955 outs() << "FP_RND_UP ";
7956 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007957 outs() << "FP_CHOP ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00007958 outs() << "\n";
7959 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
7960 outs() << " denorm " << fpu.fpu_fsw.denorm;
7961 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
7962 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
7963 outs() << " undfl " << fpu.fpu_fsw.undfl;
7964 outs() << " precis " << fpu.fpu_fsw.precis;
7965 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
7966 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
7967 outs() << " c0 " << fpu.fpu_fsw.c0;
7968 outs() << " c1 " << fpu.fpu_fsw.c1;
7969 outs() << " c2 " << fpu.fpu_fsw.c2;
7970 outs() << " tos " << fpu.fpu_fsw.tos;
7971 outs() << " c3 " << fpu.fpu_fsw.c3;
7972 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
7973 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
7974 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
7975 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
7976 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
7977 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
7978 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
7979 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
7980 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
7981 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
7982 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
7983 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
7984 outs() << "\n";
7985 outs() << "\t fpu_stmm0:\n";
7986 Print_mmst_reg(fpu.fpu_stmm0);
7987 outs() << "\t fpu_stmm1:\n";
7988 Print_mmst_reg(fpu.fpu_stmm1);
7989 outs() << "\t fpu_stmm2:\n";
7990 Print_mmst_reg(fpu.fpu_stmm2);
7991 outs() << "\t fpu_stmm3:\n";
7992 Print_mmst_reg(fpu.fpu_stmm3);
7993 outs() << "\t fpu_stmm4:\n";
7994 Print_mmst_reg(fpu.fpu_stmm4);
7995 outs() << "\t fpu_stmm5:\n";
7996 Print_mmst_reg(fpu.fpu_stmm5);
7997 outs() << "\t fpu_stmm6:\n";
7998 Print_mmst_reg(fpu.fpu_stmm6);
7999 outs() << "\t fpu_stmm7:\n";
8000 Print_mmst_reg(fpu.fpu_stmm7);
8001 outs() << "\t fpu_xmm0:\n";
8002 Print_xmm_reg(fpu.fpu_xmm0);
8003 outs() << "\t fpu_xmm1:\n";
8004 Print_xmm_reg(fpu.fpu_xmm1);
8005 outs() << "\t fpu_xmm2:\n";
8006 Print_xmm_reg(fpu.fpu_xmm2);
8007 outs() << "\t fpu_xmm3:\n";
8008 Print_xmm_reg(fpu.fpu_xmm3);
8009 outs() << "\t fpu_xmm4:\n";
8010 Print_xmm_reg(fpu.fpu_xmm4);
8011 outs() << "\t fpu_xmm5:\n";
8012 Print_xmm_reg(fpu.fpu_xmm5);
8013 outs() << "\t fpu_xmm6:\n";
8014 Print_xmm_reg(fpu.fpu_xmm6);
8015 outs() << "\t fpu_xmm7:\n";
8016 Print_xmm_reg(fpu.fpu_xmm7);
8017 outs() << "\t fpu_xmm8:\n";
8018 Print_xmm_reg(fpu.fpu_xmm8);
8019 outs() << "\t fpu_xmm9:\n";
8020 Print_xmm_reg(fpu.fpu_xmm9);
8021 outs() << "\t fpu_xmm10:\n";
8022 Print_xmm_reg(fpu.fpu_xmm10);
8023 outs() << "\t fpu_xmm11:\n";
8024 Print_xmm_reg(fpu.fpu_xmm11);
8025 outs() << "\t fpu_xmm12:\n";
8026 Print_xmm_reg(fpu.fpu_xmm12);
8027 outs() << "\t fpu_xmm13:\n";
8028 Print_xmm_reg(fpu.fpu_xmm13);
8029 outs() << "\t fpu_xmm14:\n";
8030 Print_xmm_reg(fpu.fpu_xmm14);
8031 outs() << "\t fpu_xmm15:\n";
8032 Print_xmm_reg(fpu.fpu_xmm15);
8033 outs() << "\t fpu_rsrv4:\n";
8034 for (uint32_t f = 0; f < 6; f++) {
8035 outs() << "\t ";
8036 for (uint32_t g = 0; g < 16; g++)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008037 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008038 outs() << "\n";
8039 }
8040 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8041 outs() << "\n";
8042}
8043
8044static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8045 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8046 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8047 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8048}
8049
8050static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8051 bool isLittleEndian, uint32_t cputype) {
8052 if (t.cmd == MachO::LC_THREAD)
8053 outs() << " cmd LC_THREAD\n";
8054 else if (t.cmd == MachO::LC_UNIXTHREAD)
8055 outs() << " cmd LC_UNIXTHREAD\n";
8056 else
8057 outs() << " cmd " << t.cmd << " (unknown)\n";
8058 outs() << " cmdsize " << t.cmdsize;
8059 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8060 outs() << " Incorrect size\n";
8061 else
8062 outs() << "\n";
8063
8064 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8065 const char *end = Ptr + t.cmdsize;
8066 uint32_t flavor, count, left;
8067 if (cputype == MachO::CPU_TYPE_X86_64) {
8068 while (begin < end) {
8069 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8070 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8071 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008072 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008073 flavor = 0;
8074 begin = end;
8075 }
8076 if (isLittleEndian != sys::IsLittleEndianHost)
8077 sys::swapByteOrder(flavor);
8078 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8079 memcpy((char *)&count, begin, sizeof(uint32_t));
8080 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008081 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008082 count = 0;
8083 begin = end;
8084 }
8085 if (isLittleEndian != sys::IsLittleEndianHost)
8086 sys::swapByteOrder(count);
8087 if (flavor == MachO::x86_THREAD_STATE64) {
8088 outs() << " flavor x86_THREAD_STATE64\n";
8089 if (count == MachO::x86_THREAD_STATE64_COUNT)
8090 outs() << " count x86_THREAD_STATE64_COUNT\n";
8091 else
8092 outs() << " count " << count
8093 << " (not x86_THREAD_STATE64_COUNT)\n";
8094 MachO::x86_thread_state64_t cpu64;
8095 left = end - begin;
8096 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8097 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8098 begin += sizeof(MachO::x86_thread_state64_t);
8099 } else {
8100 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8101 memcpy(&cpu64, begin, left);
8102 begin += left;
8103 }
8104 if (isLittleEndian != sys::IsLittleEndianHost)
8105 swapStruct(cpu64);
8106 Print_x86_thread_state64_t(cpu64);
8107 } else if (flavor == MachO::x86_THREAD_STATE) {
8108 outs() << " flavor x86_THREAD_STATE\n";
8109 if (count == MachO::x86_THREAD_STATE_COUNT)
8110 outs() << " count x86_THREAD_STATE_COUNT\n";
8111 else
8112 outs() << " count " << count
8113 << " (not x86_THREAD_STATE_COUNT)\n";
8114 struct MachO::x86_thread_state_t ts;
8115 left = end - begin;
8116 if (left >= sizeof(MachO::x86_thread_state_t)) {
8117 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8118 begin += sizeof(MachO::x86_thread_state_t);
8119 } else {
8120 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8121 memcpy(&ts, begin, left);
8122 begin += left;
8123 }
8124 if (isLittleEndian != sys::IsLittleEndianHost)
8125 swapStruct(ts);
8126 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8127 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8128 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8129 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8130 else
8131 outs() << "tsh.count " << ts.tsh.count
8132 << " (not x86_THREAD_STATE64_COUNT\n";
8133 Print_x86_thread_state64_t(ts.uts.ts64);
8134 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008135 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8136 << ts.tsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008137 }
8138 } else if (flavor == MachO::x86_FLOAT_STATE) {
8139 outs() << " flavor x86_FLOAT_STATE\n";
8140 if (count == MachO::x86_FLOAT_STATE_COUNT)
8141 outs() << " count x86_FLOAT_STATE_COUNT\n";
8142 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008143 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008144 struct MachO::x86_float_state_t fs;
8145 left = end - begin;
8146 if (left >= sizeof(MachO::x86_float_state_t)) {
8147 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8148 begin += sizeof(MachO::x86_float_state_t);
8149 } else {
8150 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8151 memcpy(&fs, begin, left);
8152 begin += left;
8153 }
8154 if (isLittleEndian != sys::IsLittleEndianHost)
8155 swapStruct(fs);
8156 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8157 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008158 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008159 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8160 else
8161 outs() << "fsh.count " << fs.fsh.count
8162 << " (not x86_FLOAT_STATE64_COUNT\n";
8163 Print_x86_float_state_t(fs.ufs.fs64);
8164 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008165 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8166 << fs.fsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008167 }
8168 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8169 outs() << " flavor x86_EXCEPTION_STATE\n";
8170 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8171 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8172 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008173 outs() << " count " << count
Kevin Enderby48ef5342014-12-23 22:56:39 +00008174 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8175 struct MachO::x86_exception_state_t es;
8176 left = end - begin;
8177 if (left >= sizeof(MachO::x86_exception_state_t)) {
8178 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8179 begin += sizeof(MachO::x86_exception_state_t);
8180 } else {
8181 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8182 memcpy(&es, begin, left);
8183 begin += left;
8184 }
8185 if (isLittleEndian != sys::IsLittleEndianHost)
8186 swapStruct(es);
8187 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8188 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008189 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008190 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8191 else
8192 outs() << "\t esh.count " << es.esh.count
8193 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8194 Print_x86_exception_state_t(es.ues.es64);
8195 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008196 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8197 << es.esh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008198 }
8199 } else {
8200 outs() << " flavor " << flavor << " (unknown)\n";
8201 outs() << " count " << count << "\n";
8202 outs() << " state (unknown)\n";
8203 begin += count * sizeof(uint32_t);
8204 }
8205 }
8206 } else {
8207 while (begin < end) {
8208 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8209 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8210 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008211 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008212 flavor = 0;
8213 begin = end;
8214 }
8215 if (isLittleEndian != sys::IsLittleEndianHost)
8216 sys::swapByteOrder(flavor);
8217 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8218 memcpy((char *)&count, begin, sizeof(uint32_t));
8219 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008220 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008221 count = 0;
8222 begin = end;
8223 }
8224 if (isLittleEndian != sys::IsLittleEndianHost)
8225 sys::swapByteOrder(count);
8226 outs() << " flavor " << flavor << "\n";
8227 outs() << " count " << count << "\n";
8228 outs() << " state (Unknown cputype/cpusubtype)\n";
8229 begin += count * sizeof(uint32_t);
8230 }
8231 }
8232}
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008233
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008234static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8235 if (dl.cmd == MachO::LC_ID_DYLIB)
8236 outs() << " cmd LC_ID_DYLIB\n";
8237 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8238 outs() << " cmd LC_LOAD_DYLIB\n";
8239 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8240 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8241 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8242 outs() << " cmd LC_REEXPORT_DYLIB\n";
8243 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8244 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8245 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8246 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8247 else
8248 outs() << " cmd " << dl.cmd << " (unknown)\n";
8249 outs() << " cmdsize " << dl.cmdsize;
8250 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8251 outs() << " Incorrect size\n";
8252 else
8253 outs() << "\n";
8254 if (dl.dylib.name < dl.cmdsize) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008255 const char *P = (const char *)(Ptr) + dl.dylib.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008256 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8257 } else {
8258 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8259 }
8260 outs() << " time stamp " << dl.dylib.timestamp << " ";
8261 time_t t = dl.dylib.timestamp;
8262 outs() << ctime(&t);
8263 outs() << " current version ";
8264 if (dl.dylib.current_version == 0xffffffff)
8265 outs() << "n/a\n";
8266 else
8267 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8268 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8269 << (dl.dylib.current_version & 0xff) << "\n";
8270 outs() << "compatibility version ";
8271 if (dl.dylib.compatibility_version == 0xffffffff)
8272 outs() << "n/a\n";
8273 else
8274 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8275 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8276 << (dl.dylib.compatibility_version & 0xff) << "\n";
8277}
8278
8279static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8280 uint32_t object_size) {
8281 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8282 outs() << " cmd LC_FUNCTION_STARTS\n";
8283 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8284 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8285 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8286 outs() << " cmd LC_FUNCTION_STARTS\n";
8287 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8288 outs() << " cmd LC_DATA_IN_CODE\n";
8289 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8290 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8291 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8292 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8293 else
8294 outs() << " cmd " << ld.cmd << " (?)\n";
8295 outs() << " cmdsize " << ld.cmdsize;
8296 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8297 outs() << " Incorrect size\n";
8298 else
8299 outs() << "\n";
8300 outs() << " dataoff " << ld.dataoff;
8301 if (ld.dataoff > object_size)
8302 outs() << " (past end of file)\n";
8303 else
8304 outs() << "\n";
8305 outs() << " datasize " << ld.datasize;
8306 uint64_t big_size = ld.dataoff;
8307 big_size += ld.datasize;
8308 if (big_size > object_size)
8309 outs() << " (past end of file)\n";
8310 else
8311 outs() << "\n";
8312}
8313
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008314static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8315 uint32_t cputype, bool verbose) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008316 StringRef Buf = Obj->getData();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008317 unsigned Index = 0;
8318 for (const auto &Command : Obj->load_commands()) {
8319 outs() << "Load command " << Index++ << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00008320 if (Command.C.cmd == MachO::LC_SEGMENT) {
8321 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8322 const char *sg_segname = SLC.segname;
8323 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8324 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8325 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8326 verbose);
8327 for (unsigned j = 0; j < SLC.nsects; j++) {
Kevin Enderbyc9713382014-12-16 01:14:45 +00008328 MachO::section S = Obj->getSection(Command, j);
Kevin Enderby956366c2014-08-29 22:30:52 +00008329 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8330 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8331 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8332 }
8333 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8334 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8335 const char *sg_segname = SLC_64.segname;
8336 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8337 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8338 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8339 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8340 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8341 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8342 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8343 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8344 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8345 sg_segname, filetype, Buf.size(), verbose);
8346 }
8347 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8348 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008349 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008350 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8351 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8352 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008353 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8354 Obj->is64Bit());
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008355 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8356 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8357 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8358 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8359 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8360 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8361 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8362 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8363 PrintDyldLoadCommand(Dyld, Command.Ptr);
8364 } else if (Command.C.cmd == MachO::LC_UUID) {
8365 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8366 PrintUuidLoadCommand(Uuid);
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008367 } else if (Command.C.cmd == MachO::LC_RPATH) {
8368 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8369 PrintRpathLoadCommand(Rpath, Command.Ptr);
Kevin Enderby1ff0ecc2014-12-16 21:48:27 +00008370 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
8371 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008372 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8373 PrintVersionMinLoadCommand(Vd);
8374 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8375 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8376 PrintSourceVersionCommand(Sd);
8377 } else if (Command.C.cmd == MachO::LC_MAIN) {
8378 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8379 PrintEntryPointCommand(Ep);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008380 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008381 MachO::encryption_info_command Ei =
8382 Obj->getEncryptionInfoCommand(Command);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008383 PrintEncryptionInfoCommand(Ei, Buf.size());
Kevin Enderby57538292014-12-17 01:01:30 +00008384 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008385 MachO::encryption_info_command_64 Ei =
8386 Obj->getEncryptionInfoCommand64(Command);
Kevin Enderby57538292014-12-17 01:01:30 +00008387 PrintEncryptionInfoCommand64(Ei, Buf.size());
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008388 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008389 MachO::linker_option_command Lo =
8390 Obj->getLinkerOptionLoadCommand(Command);
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008391 PrintLinkerOptionCommand(Lo, Command.Ptr);
Kevin Enderbyb4b79312014-12-18 19:24:35 +00008392 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8393 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8394 PrintSubFrameworkCommand(Sf, Command.Ptr);
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00008395 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8396 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8397 PrintSubUmbrellaCommand(Sf, Command.Ptr);
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008398 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8399 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8400 PrintSubLibraryCommand(Sl, Command.Ptr);
Kevin Enderby186eac32014-12-19 21:06:24 +00008401 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8402 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8403 PrintSubClientCommand(Sc, Command.Ptr);
Kevin Enderby52e4ce42014-12-19 22:25:22 +00008404 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8405 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8406 PrintRoutinesCommand(Rc);
8407 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8408 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8409 PrintRoutinesCommand64(Rc);
Kevin Enderby48ef5342014-12-23 22:56:39 +00008410 } else if (Command.C.cmd == MachO::LC_THREAD ||
8411 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8412 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8413 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
Nick Kledzik15558912014-10-16 18:58:20 +00008414 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8415 Command.C.cmd == MachO::LC_ID_DYLIB ||
8416 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8417 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8418 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8419 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008420 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8421 PrintDylibCommand(Dl, Command.Ptr);
8422 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8423 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8424 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8425 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8426 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8427 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8428 MachO::linkedit_data_command Ld =
8429 Obj->getLinkeditDataLoadCommand(Command);
8430 PrintLinkEditDataCommand(Ld, Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008431 } else {
8432 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8433 << ")\n";
8434 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8435 // TODO: get and print the raw bytes of the load command.
8436 }
8437 // TODO: print all the other kinds of load commands.
Kevin Enderby956366c2014-08-29 22:30:52 +00008438 }
8439}
8440
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008441static void getAndPrintMachHeader(const MachOObjectFile *Obj,
Kevin Enderby956366c2014-08-29 22:30:52 +00008442 uint32_t &filetype, uint32_t &cputype,
8443 bool verbose) {
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008444 if (Obj->is64Bit()) {
8445 MachO::mach_header_64 H_64;
8446 H_64 = Obj->getHeader64();
8447 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8448 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00008449 filetype = H_64.filetype;
8450 cputype = H_64.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008451 } else {
8452 MachO::mach_header H;
8453 H = Obj->getHeader();
8454 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8455 H.sizeofcmds, H.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00008456 filetype = H.filetype;
8457 cputype = H.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008458 }
8459}
8460
8461void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8462 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby956366c2014-08-29 22:30:52 +00008463 uint32_t filetype = 0;
8464 uint32_t cputype = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008465 getAndPrintMachHeader(file, filetype, cputype, !NonVerbose);
8466 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008467}
Nick Kledzikd04bc352014-08-30 00:20:14 +00008468
8469//===----------------------------------------------------------------------===//
8470// export trie dumping
8471//===----------------------------------------------------------------------===//
8472
8473void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008474 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
8475 uint64_t Flags = Entry.flags();
Nick Kledzikd04bc352014-08-30 00:20:14 +00008476 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
8477 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
8478 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8479 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
8480 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8481 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
8482 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
8483 if (ReExport)
8484 outs() << "[re-export] ";
8485 else
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008486 outs() << format("0x%08llX ",
8487 Entry.address()); // FIXME:add in base address
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008488 outs() << Entry.name();
Nick Kledzikd04bc352014-08-30 00:20:14 +00008489 if (WeakDef || ThreadLocal || Resolver || Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008490 bool NeedsComma = false;
Nick Kledzik1d1ac4b2014-09-03 01:12:52 +00008491 outs() << " [";
Nick Kledzikd04bc352014-08-30 00:20:14 +00008492 if (WeakDef) {
8493 outs() << "weak_def";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008494 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008495 }
8496 if (ThreadLocal) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008497 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008498 outs() << ", ";
8499 outs() << "per-thread";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008500 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008501 }
8502 if (Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008503 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008504 outs() << ", ";
8505 outs() << "absolute";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008506 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008507 }
8508 if (Resolver) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008509 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008510 outs() << ", ";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008511 outs() << format("resolver=0x%08llX", Entry.other());
8512 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008513 }
8514 outs() << "]";
8515 }
8516 if (ReExport) {
8517 StringRef DylibName = "unknown";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008518 int Ordinal = Entry.other() - 1;
8519 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
8520 if (Entry.otherName().empty())
Nick Kledzikd04bc352014-08-30 00:20:14 +00008521 outs() << " (from " << DylibName << ")";
8522 else
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008523 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
Nick Kledzikd04bc352014-08-30 00:20:14 +00008524 }
8525 outs() << "\n";
8526 }
8527}
Nick Kledzikac431442014-09-12 21:34:15 +00008528
Nick Kledzikac431442014-09-12 21:34:15 +00008529//===----------------------------------------------------------------------===//
8530// rebase table dumping
8531//===----------------------------------------------------------------------===//
8532
8533namespace {
8534class SegInfo {
8535public:
8536 SegInfo(const object::MachOObjectFile *Obj);
8537
8538 StringRef segmentName(uint32_t SegIndex);
8539 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
8540 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
8541
8542private:
8543 struct SectionInfo {
8544 uint64_t Address;
8545 uint64_t Size;
8546 StringRef SectionName;
8547 StringRef SegmentName;
8548 uint64_t OffsetInSegment;
8549 uint64_t SegmentStartAddress;
8550 uint32_t SegmentIndex;
8551 };
8552 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
8553 SmallVector<SectionInfo, 32> Sections;
8554};
8555}
8556
8557SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
8558 // Build table of sections so segIndex/offset pairs can be translated.
Nick Kledzik56ebef42014-09-16 01:41:51 +00008559 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
Nick Kledzikac431442014-09-12 21:34:15 +00008560 StringRef CurSegName;
8561 uint64_t CurSegAddress;
8562 for (const SectionRef &Section : Obj->sections()) {
8563 SectionInfo Info;
8564 if (error(Section.getName(Info.SectionName)))
8565 return;
Rafael Espindola80291272014-10-08 15:28:58 +00008566 Info.Address = Section.getAddress();
8567 Info.Size = Section.getSize();
Nick Kledzikac431442014-09-12 21:34:15 +00008568 Info.SegmentName =
8569 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
8570 if (!Info.SegmentName.equals(CurSegName)) {
8571 ++CurSegIndex;
8572 CurSegName = Info.SegmentName;
8573 CurSegAddress = Info.Address;
8574 }
8575 Info.SegmentIndex = CurSegIndex - 1;
8576 Info.OffsetInSegment = Info.Address - CurSegAddress;
8577 Info.SegmentStartAddress = CurSegAddress;
8578 Sections.push_back(Info);
8579 }
8580}
8581
8582StringRef SegInfo::segmentName(uint32_t SegIndex) {
8583 for (const SectionInfo &SI : Sections) {
8584 if (SI.SegmentIndex == SegIndex)
8585 return SI.SegmentName;
8586 }
8587 llvm_unreachable("invalid segIndex");
8588}
8589
8590const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
8591 uint64_t OffsetInSeg) {
8592 for (const SectionInfo &SI : Sections) {
8593 if (SI.SegmentIndex != SegIndex)
8594 continue;
8595 if (SI.OffsetInSegment > OffsetInSeg)
8596 continue;
8597 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8598 continue;
8599 return SI;
8600 }
8601 llvm_unreachable("segIndex and offset not in any section");
8602}
8603
8604StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
8605 return findSection(SegIndex, OffsetInSeg).SectionName;
8606}
8607
8608uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
8609 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
8610 return SI.SegmentStartAddress + OffsetInSeg;
8611}
8612
8613void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
8614 // Build table of sections so names can used in final output.
8615 SegInfo sectionTable(Obj);
8616
8617 outs() << "segment section address type\n";
8618 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
8619 uint32_t SegIndex = Entry.segmentIndex();
8620 uint64_t OffsetInSeg = Entry.segmentOffset();
8621 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8622 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8623 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8624
8625 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008626 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
8627 SegmentName.str().c_str(), SectionName.str().c_str(),
8628 Address, Entry.typeName().str().c_str());
Nick Kledzikac431442014-09-12 21:34:15 +00008629 }
8630}
Nick Kledzik56ebef42014-09-16 01:41:51 +00008631
8632static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
8633 StringRef DylibName;
8634 switch (Ordinal) {
8635 case MachO::BIND_SPECIAL_DYLIB_SELF:
8636 return "this-image";
8637 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
8638 return "main-executable";
8639 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
8640 return "flat-namespace";
8641 default:
Nick Kledzikabd29872014-09-16 22:03:13 +00008642 if (Ordinal > 0) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008643 std::error_code EC =
8644 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
Nick Kledzikabd29872014-09-16 22:03:13 +00008645 if (EC)
Nick Kledzik51d2c2b2014-10-14 23:29:38 +00008646 return "<<bad library ordinal>>";
Nick Kledzikabd29872014-09-16 22:03:13 +00008647 return DylibName;
8648 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00008649 }
Nick Kledzikabd29872014-09-16 22:03:13 +00008650 return "<<unknown special ordinal>>";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008651}
8652
8653//===----------------------------------------------------------------------===//
8654// bind table dumping
8655//===----------------------------------------------------------------------===//
8656
8657void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
8658 // Build table of sections so names can used in final output.
8659 SegInfo sectionTable(Obj);
8660
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008661 outs() << "segment section address type "
8662 "addend dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008663 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
8664 uint32_t SegIndex = Entry.segmentIndex();
8665 uint64_t OffsetInSeg = Entry.segmentOffset();
8666 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8667 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8668 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8669
8670 // Table lines look like:
8671 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008672 StringRef Attr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00008673 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008674 Attr = " (weak_import)";
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008675 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008676 << left_justify(SectionName, 18) << " "
8677 << format_hex(Address, 10, true) << " "
8678 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008679 << format_decimal(Entry.addend(), 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008680 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008681 << Entry.symbolName() << Attr << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008682 }
8683}
8684
8685//===----------------------------------------------------------------------===//
8686// lazy bind table dumping
8687//===----------------------------------------------------------------------===//
8688
8689void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
8690 // Build table of sections so names can used in final output.
8691 SegInfo sectionTable(Obj);
8692
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008693 outs() << "segment section address "
8694 "dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008695 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
8696 uint32_t SegIndex = Entry.segmentIndex();
8697 uint64_t OffsetInSeg = Entry.segmentOffset();
8698 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8699 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8700 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8701
8702 // Table lines look like:
8703 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008704 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008705 << left_justify(SectionName, 18) << " "
8706 << format_hex(Address, 10, true) << " "
8707 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Nick Kledzik56ebef42014-09-16 01:41:51 +00008708 << Entry.symbolName() << "\n";
8709 }
8710}
8711
Nick Kledzik56ebef42014-09-16 01:41:51 +00008712//===----------------------------------------------------------------------===//
8713// weak bind table dumping
8714//===----------------------------------------------------------------------===//
8715
8716void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
8717 // Build table of sections so names can used in final output.
8718 SegInfo sectionTable(Obj);
8719
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008720 outs() << "segment section address "
8721 "type addend symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008722 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
8723 // Strong symbols don't have a location to update.
8724 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008725 outs() << " strong "
Nick Kledzik56ebef42014-09-16 01:41:51 +00008726 << Entry.symbolName() << "\n";
8727 continue;
8728 }
8729 uint32_t SegIndex = Entry.segmentIndex();
8730 uint64_t OffsetInSeg = Entry.segmentOffset();
8731 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8732 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8733 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8734
8735 // Table lines look like:
8736 // __DATA __data 0x00001000 pointer 0 _foo
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008737 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008738 << left_justify(SectionName, 18) << " "
8739 << format_hex(Address, 10, true) << " "
8740 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008741 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
8742 << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008743 }
8744}
8745
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008746// get_dyld_bind_info_symbolname() is used for disassembly and passed an
8747// address, ReferenceValue, in the Mach-O file and looks in the dyld bind
8748// information for that address. If the address is found its binding symbol
8749// name is returned. If not nullptr is returned.
8750static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
8751 struct DisassembleInfo *info) {
Kevin Enderby078be602014-10-23 19:53:12 +00008752 if (info->bindtable == nullptr) {
8753 info->bindtable = new (BindTable);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008754 SegInfo sectionTable(info->O);
8755 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
8756 uint32_t SegIndex = Entry.segmentIndex();
8757 uint64_t OffsetInSeg = Entry.segmentOffset();
8758 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8759 const char *SymbolName = nullptr;
8760 StringRef name = Entry.symbolName();
8761 if (!name.empty())
8762 SymbolName = name.data();
Kevin Enderby078be602014-10-23 19:53:12 +00008763 info->bindtable->push_back(std::make_pair(Address, SymbolName));
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008764 }
8765 }
Kevin Enderby078be602014-10-23 19:53:12 +00008766 for (bind_table_iterator BI = info->bindtable->begin(),
8767 BE = info->bindtable->end();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008768 BI != BE; ++BI) {
8769 uint64_t Address = BI->first;
8770 if (ReferenceValue == Address) {
8771 const char *SymbolName = BI->second;
8772 return SymbolName;
8773 }
8774 }
8775 return nullptr;
8776}