blob: 239afaf31d43a39ce8f57d246d6d1528fb373cbf [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 Espindoladea00162015-07-03 17:44:18 +0000181 uint64_t AAddr = (A.getType() != SymbolRef::ST_Function) ? 0 : A.getValue();
182 uint64_t BAddr = (B.getType() != SymbolRef::ST_Function) ? 0 : B.getValue();
Owen Andersond9243c42011-10-17 21:37:35 +0000183 return AAddr < BAddr;
184 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000185};
186
Kevin Enderby273ae012013-06-06 17:20:50 +0000187// Types for the storted data in code table that is built before disassembly
188// and the predicate function to sort them.
189typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
190typedef std::vector<DiceTableEntry> DiceTable;
191typedef DiceTable::iterator dice_table_iterator;
192
Kevin Enderby930fdc72014-11-06 19:00:13 +0000193// This is used to search for a data in code table entry for the PC being
194// disassembled. The j parameter has the PC in j.first. A single data in code
195// table entry can cover many bytes for each of its Kind's. So if the offset,
196// aka the i.first value, of the data in code table entry plus its Length
197// covers the PC being searched for this will return true. If not it will
198// return false.
David Majnemerea9b8ee2014-11-04 08:41:48 +0000199static bool compareDiceTableEntries(const DiceTableEntry &i,
200 const DiceTableEntry &j) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000201 uint16_t Length;
202 i.second.getLength(Length);
203
204 return j.first >= i.first && j.first < i.first + Length;
Kevin Enderby273ae012013-06-06 17:20:50 +0000205}
206
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000207static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
Kevin Enderby930fdc72014-11-06 19:00:13 +0000208 unsigned short Kind) {
209 uint32_t Value, Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000210
211 switch (Kind) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000212 default:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000213 case MachO::DICE_KIND_DATA:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000214 if (Length >= 4) {
215 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000216 dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000217 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000218 outs() << "\t.long " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000219 Size = 4;
220 } else if (Length >= 2) {
221 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000222 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000223 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000224 outs() << "\t.short " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000225 Size = 2;
226 } else {
227 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000228 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000229 Value = bytes[0];
230 outs() << "\t.byte " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000231 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000232 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000233 if (Kind == MachO::DICE_KIND_DATA)
234 outs() << "\t@ KIND_DATA\n";
235 else
236 outs() << "\t@ data in code kind = " << Kind << "\n";
Kevin Enderby273ae012013-06-06 17:20:50 +0000237 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000238 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000239 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000240 dumpBytes(ArrayRef<uint8_t>(bytes, 1), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000241 Value = bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000242 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
243 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000244 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000245 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000246 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000247 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000248 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000249 outs() << "\t.short " << format("%5u", Value & 0xffff)
250 << "\t@ KIND_JUMP_TABLE16\n";
251 Size = 2;
Kevin Enderby273ae012013-06-06 17:20:50 +0000252 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000253 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000254 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
255 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000256 dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000257 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000258 outs() << "\t.long " << Value;
259 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
260 outs() << "\t@ KIND_JUMP_TABLE32\n";
261 else
262 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
263 Size = 4;
Kevin Enderby273ae012013-06-06 17:20:50 +0000264 break;
265 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000266 return Size;
Kevin Enderby273ae012013-06-06 17:20:50 +0000267}
268
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000269static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000270 std::vector<SectionRef> &Sections,
271 std::vector<SymbolRef> &Symbols,
272 SmallVectorImpl<uint64_t> &FoundFns,
273 uint64_t &BaseSegmentAddress) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000274 for (const SymbolRef &Symbol : MachOObj->symbols()) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000275 ErrorOr<StringRef> SymName = Symbol.getName();
276 if (std::error_code EC = SymName.getError())
277 report_fatal_error(EC.message());
278 if (!SymName->startswith("ltmp"))
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000279 Symbols.push_back(Symbol);
280 }
Owen Andersond9243c42011-10-17 21:37:35 +0000281
Alexey Samsonov48803e52014-03-13 14:37:36 +0000282 for (const SectionRef &Section : MachOObj->sections()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000283 StringRef SectName;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000284 Section.getName(SectName);
285 Sections.push_back(Section);
Owen Andersond9243c42011-10-17 21:37:35 +0000286 }
287
Kevin Enderby273ae012013-06-06 17:20:50 +0000288 bool BaseSegmentAddressSet = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000289 for (const auto &Command : MachOObj->load_commands()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000290 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000291 // We found a function starts segment, parse the addresses for later
292 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000293 MachO::linkedit_data_command LLC =
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000294 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000295
Charles Davis8bdfafd2013-09-01 04:28:48 +0000296 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000297 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
298 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000299 StringRef SegName = SLC.segname;
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000300 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
Kevin Enderby273ae012013-06-06 17:20:50 +0000301 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000302 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000303 }
304 }
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000305 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000306}
307
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000308static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
309 uint32_t n, uint32_t count,
310 uint32_t stride, uint64_t addr) {
311 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
312 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
313 if (n > nindirectsyms)
314 outs() << " (entries start past the end of the indirect symbol "
315 "table) (reserved1 field greater than the table size)";
316 else if (n + count > nindirectsyms)
317 outs() << " (entries extends past the end of the indirect symbol "
318 "table)";
319 outs() << "\n";
320 uint32_t cputype = O->getHeader().cputype;
321 if (cputype & MachO::CPU_ARCH_ABI64)
322 outs() << "address index";
323 else
324 outs() << "address index";
325 if (verbose)
326 outs() << " name\n";
327 else
328 outs() << "\n";
329 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
330 if (cputype & MachO::CPU_ARCH_ABI64)
331 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
332 else
333 outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
334 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
335 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
336 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
337 outs() << "LOCAL\n";
338 continue;
339 }
340 if (indirect_symbol ==
341 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
342 outs() << "LOCAL ABSOLUTE\n";
343 continue;
344 }
345 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
346 outs() << "ABSOLUTE\n";
347 continue;
348 }
349 outs() << format("%5u ", indirect_symbol);
Kevin Enderbyf0640752015-03-13 17:56:32 +0000350 if (verbose) {
351 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
352 if (indirect_symbol < Symtab.nsyms) {
353 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
354 SymbolRef Symbol = *Sym;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000355 ErrorOr<StringRef> SymName = Symbol.getName();
356 if (std::error_code EC = SymName.getError())
357 report_fatal_error(EC.message());
358 outs() << *SymName;
Kevin Enderbyf0640752015-03-13 17:56:32 +0000359 } else {
360 outs() << "?";
361 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000362 }
363 outs() << "\n";
364 }
365}
366
367static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000368 for (const auto &Load : O->load_commands()) {
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000369 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
370 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
371 for (unsigned J = 0; J < Seg.nsects; ++J) {
372 MachO::section_64 Sec = O->getSection64(Load, J);
373 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
374 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
375 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
376 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
377 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
378 section_type == MachO::S_SYMBOL_STUBS) {
379 uint32_t stride;
380 if (section_type == MachO::S_SYMBOL_STUBS)
381 stride = Sec.reserved2;
382 else
383 stride = 8;
384 if (stride == 0) {
385 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
386 << Sec.sectname << ") "
387 << "(size of stubs in reserved2 field is zero)\n";
388 continue;
389 }
390 uint32_t count = Sec.size / stride;
391 outs() << "Indirect symbols for (" << Sec.segname << ","
392 << Sec.sectname << ") " << count << " entries";
393 uint32_t n = Sec.reserved1;
394 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
395 }
396 }
397 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
398 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
399 for (unsigned J = 0; J < Seg.nsects; ++J) {
400 MachO::section Sec = O->getSection(Load, J);
401 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
402 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
403 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
404 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
405 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
406 section_type == MachO::S_SYMBOL_STUBS) {
407 uint32_t stride;
408 if (section_type == MachO::S_SYMBOL_STUBS)
409 stride = Sec.reserved2;
410 else
411 stride = 4;
412 if (stride == 0) {
413 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
414 << Sec.sectname << ") "
415 << "(size of stubs in reserved2 field is zero)\n";
416 continue;
417 }
418 uint32_t count = Sec.size / stride;
419 outs() << "Indirect symbols for (" << Sec.segname << ","
420 << Sec.sectname << ") " << count << " entries";
421 uint32_t n = Sec.reserved1;
422 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
423 }
424 }
425 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000426 }
427}
428
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000429static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
430 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
431 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
432 outs() << "Data in code table (" << nentries << " entries)\n";
433 outs() << "offset length kind\n";
434 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
435 ++DI) {
436 uint32_t Offset;
437 DI->getOffset(Offset);
438 outs() << format("0x%08" PRIx32, Offset) << " ";
439 uint16_t Length;
440 DI->getLength(Length);
441 outs() << format("%6u", Length) << " ";
442 uint16_t Kind;
443 DI->getKind(Kind);
444 if (verbose) {
445 switch (Kind) {
446 case MachO::DICE_KIND_DATA:
447 outs() << "DATA";
448 break;
449 case MachO::DICE_KIND_JUMP_TABLE8:
450 outs() << "JUMP_TABLE8";
451 break;
452 case MachO::DICE_KIND_JUMP_TABLE16:
453 outs() << "JUMP_TABLE16";
454 break;
455 case MachO::DICE_KIND_JUMP_TABLE32:
456 outs() << "JUMP_TABLE32";
457 break;
458 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
459 outs() << "ABS_JUMP_TABLE32";
460 break;
461 default:
462 outs() << format("0x%04" PRIx32, Kind);
463 break;
464 }
465 } else
466 outs() << format("0x%04" PRIx32, Kind);
467 outs() << "\n";
468 }
469}
470
Kevin Enderby9a509442015-01-27 21:28:24 +0000471static void PrintLinkOptHints(MachOObjectFile *O) {
472 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
473 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
474 uint32_t nloh = LohLC.datasize;
475 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
476 for (uint32_t i = 0; i < nloh;) {
477 unsigned n;
478 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
479 i += n;
480 outs() << " identifier " << identifier << " ";
481 if (i >= nloh)
482 return;
483 switch (identifier) {
484 case 1:
485 outs() << "AdrpAdrp\n";
486 break;
487 case 2:
488 outs() << "AdrpLdr\n";
489 break;
490 case 3:
491 outs() << "AdrpAddLdr\n";
492 break;
493 case 4:
494 outs() << "AdrpLdrGotLdr\n";
495 break;
496 case 5:
497 outs() << "AdrpAddStr\n";
498 break;
499 case 6:
500 outs() << "AdrpLdrGotStr\n";
501 break;
502 case 7:
503 outs() << "AdrpAdd\n";
504 break;
505 case 8:
506 outs() << "AdrpLdrGot\n";
507 break;
508 default:
509 outs() << "Unknown identifier value\n";
510 break;
511 }
512 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
513 i += n;
514 outs() << " narguments " << narguments << "\n";
515 if (i >= nloh)
516 return;
517
518 for (uint32_t j = 0; j < narguments; j++) {
519 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
520 i += n;
521 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
522 if (i >= nloh)
523 return;
524 }
525 }
526}
527
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000528static void PrintDylibs(MachOObjectFile *O, bool JustId) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000529 unsigned Index = 0;
530 for (const auto &Load : O->load_commands()) {
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000531 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
532 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
533 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
534 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
535 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
536 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
537 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
538 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
539 if (dl.dylib.name < dl.cmdsize) {
540 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
541 if (JustId)
542 outs() << p << "\n";
543 else {
544 outs() << "\t" << p;
545 outs() << " (compatibility version "
546 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
547 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
548 << (dl.dylib.compatibility_version & 0xff) << ",";
549 outs() << " current version "
550 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
551 << ((dl.dylib.current_version >> 8) & 0xff) << "."
552 << (dl.dylib.current_version & 0xff) << ")\n";
553 }
554 } else {
555 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
556 if (Load.C.cmd == MachO::LC_ID_DYLIB)
557 outs() << "LC_ID_DYLIB ";
558 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
559 outs() << "LC_LOAD_DYLIB ";
560 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
561 outs() << "LC_LOAD_WEAK_DYLIB ";
562 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
563 outs() << "LC_LAZY_LOAD_DYLIB ";
564 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
565 outs() << "LC_REEXPORT_DYLIB ";
566 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
567 outs() << "LC_LOAD_UPWARD_DYLIB ";
568 else
569 outs() << "LC_??? ";
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000570 outs() << "command " << Index++ << "\n";
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000571 }
572 }
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000573 }
574}
575
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000576typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
577
578static void CreateSymbolAddressMap(MachOObjectFile *O,
579 SymbolAddressMap *AddrMap) {
580 // Create a map of symbol addresses to symbol names.
581 for (const SymbolRef &Symbol : O->symbols()) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000582 SymbolRef::Type ST = Symbol.getType();
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000583 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
584 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +0000585 uint64_t Address = Symbol.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000586 ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
587 if (std::error_code EC = SymNameOrErr.getError())
588 report_fatal_error(EC.message());
589 StringRef SymName = *SymNameOrErr;
Kevin Enderby846c0002015-04-16 17:19:59 +0000590 if (!SymName.startswith(".objc"))
591 (*AddrMap)[Address] = SymName;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000592 }
593 }
594}
595
596// GuessSymbolName is passed the address of what might be a symbol and a
597// pointer to the SymbolAddressMap. It returns the name of a symbol
598// with that address or nullptr if no symbol is found with that address.
599static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
600 const char *SymbolName = nullptr;
601 // A DenseMap can't lookup up some values.
602 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
603 StringRef name = AddrMap->lookup(value);
604 if (!name.empty())
605 SymbolName = name.data();
606 }
607 return SymbolName;
608}
609
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000610static void DumpCstringChar(const char c) {
611 char p[2];
612 p[0] = c;
613 p[1] = '\0';
614 outs().write_escaped(p);
615}
616
Kevin Enderby10ba0412015-02-04 21:38:42 +0000617static void DumpCstringSection(MachOObjectFile *O, const char *sect,
618 uint32_t sect_size, uint64_t sect_addr,
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000619 bool print_addresses) {
Kevin Enderby10ba0412015-02-04 21:38:42 +0000620 for (uint32_t i = 0; i < sect_size; i++) {
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000621 if (print_addresses) {
622 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000623 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000624 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000625 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000626 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000627 for (; i < sect_size && sect[i] != '\0'; i++)
628 DumpCstringChar(sect[i]);
Kevin Enderby10ba0412015-02-04 21:38:42 +0000629 if (i < sect_size && sect[i] == '\0')
630 outs() << "\n";
631 }
632}
633
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000634static void DumpLiteral4(uint32_t l, float f) {
635 outs() << format("0x%08" PRIx32, l);
636 if ((l & 0x7f800000) != 0x7f800000)
637 outs() << format(" (%.16e)\n", f);
638 else {
639 if (l == 0x7f800000)
640 outs() << " (+Infinity)\n";
641 else if (l == 0xff800000)
642 outs() << " (-Infinity)\n";
643 else if ((l & 0x00400000) == 0x00400000)
644 outs() << " (non-signaling Not-a-Number)\n";
645 else
646 outs() << " (signaling Not-a-Number)\n";
647 }
648}
649
650static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
651 uint32_t sect_size, uint64_t sect_addr,
652 bool print_addresses) {
653 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
654 if (print_addresses) {
655 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000656 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000657 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000658 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000659 }
660 float f;
661 memcpy(&f, sect + i, sizeof(float));
662 if (O->isLittleEndian() != sys::IsLittleEndianHost)
663 sys::swapByteOrder(f);
664 uint32_t l;
665 memcpy(&l, sect + i, sizeof(uint32_t));
666 if (O->isLittleEndian() != sys::IsLittleEndianHost)
667 sys::swapByteOrder(l);
668 DumpLiteral4(l, f);
669 }
670}
671
672static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
673 double d) {
674 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
675 uint32_t Hi, Lo;
676 if (O->isLittleEndian()) {
677 Hi = l1;
678 Lo = l0;
679 } else {
680 Hi = l0;
681 Lo = l1;
682 }
683 // Hi is the high word, so this is equivalent to if(isfinite(d))
684 if ((Hi & 0x7ff00000) != 0x7ff00000)
685 outs() << format(" (%.16e)\n", d);
686 else {
687 if (Hi == 0x7ff00000 && Lo == 0)
688 outs() << " (+Infinity)\n";
689 else if (Hi == 0xfff00000 && Lo == 0)
690 outs() << " (-Infinity)\n";
691 else if ((Hi & 0x00080000) == 0x00080000)
692 outs() << " (non-signaling Not-a-Number)\n";
693 else
694 outs() << " (signaling Not-a-Number)\n";
695 }
696}
697
698static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
699 uint32_t sect_size, uint64_t sect_addr,
700 bool print_addresses) {
701 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
702 if (print_addresses) {
703 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000704 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000705 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000706 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000707 }
708 double d;
709 memcpy(&d, sect + i, sizeof(double));
710 if (O->isLittleEndian() != sys::IsLittleEndianHost)
711 sys::swapByteOrder(d);
712 uint32_t l0, l1;
713 memcpy(&l0, sect + i, sizeof(uint32_t));
714 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
715 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
716 sys::swapByteOrder(l0);
717 sys::swapByteOrder(l1);
718 }
719 DumpLiteral8(O, l0, l1, d);
720 }
721}
722
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000723static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
724 outs() << format("0x%08" PRIx32, l0) << " ";
725 outs() << format("0x%08" PRIx32, l1) << " ";
726 outs() << format("0x%08" PRIx32, l2) << " ";
727 outs() << format("0x%08" PRIx32, l3) << "\n";
728}
729
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000730static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
731 uint32_t sect_size, uint64_t sect_addr,
732 bool print_addresses) {
733 for (uint32_t i = 0; i < sect_size; i += 16) {
734 if (print_addresses) {
735 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000736 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000737 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000738 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000739 }
740 uint32_t l0, l1, l2, l3;
741 memcpy(&l0, sect + i, sizeof(uint32_t));
742 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
743 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
744 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
745 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
746 sys::swapByteOrder(l0);
747 sys::swapByteOrder(l1);
748 sys::swapByteOrder(l2);
749 sys::swapByteOrder(l3);
750 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000751 DumpLiteral16(l0, l1, l2, l3);
752 }
753}
754
755static void DumpLiteralPointerSection(MachOObjectFile *O,
756 const SectionRef &Section,
757 const char *sect, uint32_t sect_size,
758 uint64_t sect_addr,
759 bool print_addresses) {
760 // Collect the literal sections in this Mach-O file.
761 std::vector<SectionRef> LiteralSections;
762 for (const SectionRef &Section : O->sections()) {
763 DataRefImpl Ref = Section.getRawDataRefImpl();
764 uint32_t section_type;
765 if (O->is64Bit()) {
766 const MachO::section_64 Sec = O->getSection64(Ref);
767 section_type = Sec.flags & MachO::SECTION_TYPE;
768 } else {
769 const MachO::section Sec = O->getSection(Ref);
770 section_type = Sec.flags & MachO::SECTION_TYPE;
771 }
772 if (section_type == MachO::S_CSTRING_LITERALS ||
773 section_type == MachO::S_4BYTE_LITERALS ||
774 section_type == MachO::S_8BYTE_LITERALS ||
775 section_type == MachO::S_16BYTE_LITERALS)
776 LiteralSections.push_back(Section);
777 }
778
779 // Set the size of the literal pointer.
780 uint32_t lp_size = O->is64Bit() ? 8 : 4;
781
Eric Christopher572e03a2015-06-19 01:53:21 +0000782 // Collect the external relocation symbols for the literal pointers.
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000783 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
784 for (const RelocationRef &Reloc : Section.relocations()) {
785 DataRefImpl Rel;
786 MachO::any_relocation_info RE;
787 bool isExtern = false;
788 Rel = Reloc.getRawDataRefImpl();
789 RE = O->getRelocation(Rel);
790 isExtern = O->getPlainRelocationExternal(RE);
791 if (isExtern) {
Rafael Espindola96d071c2015-06-29 23:29:12 +0000792 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000793 symbol_iterator RelocSym = Reloc.getSymbol();
794 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
795 }
796 }
797 array_pod_sort(Relocs.begin(), Relocs.end());
798
799 // Dump each literal pointer.
800 for (uint32_t i = 0; i < sect_size; i += lp_size) {
801 if (print_addresses) {
802 if (O->is64Bit())
803 outs() << format("%016" PRIx64, sect_addr + i) << " ";
804 else
805 outs() << format("%08" PRIx64, sect_addr + i) << " ";
806 }
807 uint64_t lp;
808 if (O->is64Bit()) {
809 memcpy(&lp, sect + i, sizeof(uint64_t));
810 if (O->isLittleEndian() != sys::IsLittleEndianHost)
811 sys::swapByteOrder(lp);
812 } else {
813 uint32_t li;
814 memcpy(&li, sect + i, sizeof(uint32_t));
815 if (O->isLittleEndian() != sys::IsLittleEndianHost)
816 sys::swapByteOrder(li);
817 lp = li;
818 }
819
820 // First look for an external relocation entry for this literal pointer.
David Blaikie33dd45d02015-03-23 18:39:02 +0000821 auto Reloc = std::find_if(
822 Relocs.begin(), Relocs.end(),
823 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
824 if (Reloc != Relocs.end()) {
825 symbol_iterator RelocSym = Reloc->second;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000826 ErrorOr<StringRef> SymName = RelocSym->getName();
827 if (std::error_code EC = SymName.getError())
828 report_fatal_error(EC.message());
829 outs() << "external relocation entry for symbol:" << *SymName << "\n";
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000830 continue;
David Blaikie33dd45d02015-03-23 18:39:02 +0000831 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000832
833 // For local references see what the section the literal pointer points to.
David Blaikie33dd45d02015-03-23 18:39:02 +0000834 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
835 [&](const SectionRef &R) {
836 return lp >= R.getAddress() &&
837 lp < R.getAddress() + R.getSize();
838 });
839 if (Sect == LiteralSections.end()) {
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000840 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
David Blaikie33dd45d02015-03-23 18:39:02 +0000841 continue;
842 }
843
844 uint64_t SectAddress = Sect->getAddress();
845 uint64_t SectSize = Sect->getSize();
846
847 StringRef SectName;
848 Sect->getName(SectName);
849 DataRefImpl Ref = Sect->getRawDataRefImpl();
850 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
851 outs() << SegmentName << ":" << SectName << ":";
852
853 uint32_t section_type;
854 if (O->is64Bit()) {
855 const MachO::section_64 Sec = O->getSection64(Ref);
856 section_type = Sec.flags & MachO::SECTION_TYPE;
857 } else {
858 const MachO::section Sec = O->getSection(Ref);
859 section_type = Sec.flags & MachO::SECTION_TYPE;
860 }
861
862 StringRef BytesStr;
863 Sect->getContents(BytesStr);
864 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
865
866 switch (section_type) {
867 case MachO::S_CSTRING_LITERALS:
868 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
869 i++) {
870 DumpCstringChar(Contents[i]);
871 }
872 outs() << "\n";
873 break;
874 case MachO::S_4BYTE_LITERALS:
875 float f;
876 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
877 uint32_t l;
878 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
879 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
880 sys::swapByteOrder(f);
881 sys::swapByteOrder(l);
882 }
883 DumpLiteral4(l, f);
884 break;
885 case MachO::S_8BYTE_LITERALS: {
886 double d;
887 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
888 uint32_t l0, l1;
889 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
890 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
891 sizeof(uint32_t));
892 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
893 sys::swapByteOrder(f);
894 sys::swapByteOrder(l0);
895 sys::swapByteOrder(l1);
896 }
897 DumpLiteral8(O, l0, l1, d);
898 break;
899 }
900 case MachO::S_16BYTE_LITERALS: {
901 uint32_t l0, l1, l2, l3;
902 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
903 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
904 sizeof(uint32_t));
905 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
906 sizeof(uint32_t));
907 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
908 sizeof(uint32_t));
909 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
910 sys::swapByteOrder(l0);
911 sys::swapByteOrder(l1);
912 sys::swapByteOrder(l2);
913 sys::swapByteOrder(l3);
914 }
915 DumpLiteral16(l0, l1, l2, l3);
916 break;
917 }
918 }
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000919 }
920}
921
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000922static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
923 uint32_t sect_size, uint64_t sect_addr,
924 SymbolAddressMap *AddrMap,
925 bool verbose) {
926 uint32_t stride;
927 if (O->is64Bit())
928 stride = sizeof(uint64_t);
929 else
930 stride = sizeof(uint32_t);
931 for (uint32_t i = 0; i < sect_size; i += stride) {
932 const char *SymbolName = nullptr;
933 if (O->is64Bit()) {
934 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
935 uint64_t pointer_value;
936 memcpy(&pointer_value, sect + i, stride);
937 if (O->isLittleEndian() != sys::IsLittleEndianHost)
938 sys::swapByteOrder(pointer_value);
939 outs() << format("0x%016" PRIx64, pointer_value);
940 if (verbose)
941 SymbolName = GuessSymbolName(pointer_value, AddrMap);
942 } else {
943 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
944 uint32_t pointer_value;
945 memcpy(&pointer_value, sect + i, stride);
946 if (O->isLittleEndian() != sys::IsLittleEndianHost)
947 sys::swapByteOrder(pointer_value);
948 outs() << format("0x%08" PRIx32, pointer_value);
949 if (verbose)
950 SymbolName = GuessSymbolName(pointer_value, AddrMap);
951 }
952 if (SymbolName)
953 outs() << " " << SymbolName;
954 outs() << "\n";
955 }
956}
957
958static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
959 uint32_t size, uint64_t addr) {
960 uint32_t cputype = O->getHeader().cputype;
961 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
962 uint32_t j;
963 for (uint32_t i = 0; i < size; i += j, addr += j) {
964 if (O->is64Bit())
965 outs() << format("%016" PRIx64, addr) << "\t";
966 else
Kevin Enderbyf0640752015-03-13 17:56:32 +0000967 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000968 for (j = 0; j < 16 && i + j < size; j++) {
969 uint8_t byte_word = *(sect + i + j);
970 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
971 }
972 outs() << "\n";
973 }
974 } else {
975 uint32_t j;
976 for (uint32_t i = 0; i < size; i += j, addr += j) {
977 if (O->is64Bit())
978 outs() << format("%016" PRIx64, addr) << "\t";
979 else
980 outs() << format("%08" PRIx64, sect) << "\t";
981 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
982 j += sizeof(int32_t)) {
983 if (i + j + sizeof(int32_t) < size) {
984 uint32_t long_word;
985 memcpy(&long_word, sect + i + j, sizeof(int32_t));
986 if (O->isLittleEndian() != sys::IsLittleEndianHost)
987 sys::swapByteOrder(long_word);
988 outs() << format("%08" PRIx32, long_word) << " ";
989 } else {
990 for (uint32_t k = 0; i + j + k < size; k++) {
991 uint8_t byte_word = *(sect + i + j);
992 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
993 }
994 }
995 }
996 outs() << "\n";
997 }
998 }
999}
1000
Kevin Enderby95df54c2015-02-04 01:01:38 +00001001static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1002 StringRef DisSegName, StringRef DisSectName);
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001003static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1004 uint32_t size, uint32_t addr);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001005
1006static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1007 bool verbose) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001008 SymbolAddressMap AddrMap;
1009 if (verbose)
1010 CreateSymbolAddressMap(O, &AddrMap);
1011
1012 for (unsigned i = 0; i < DumpSections.size(); ++i) {
1013 StringRef DumpSection = DumpSections[i];
1014 std::pair<StringRef, StringRef> DumpSegSectName;
1015 DumpSegSectName = DumpSection.split(',');
1016 StringRef DumpSegName, DumpSectName;
1017 if (DumpSegSectName.second.size()) {
1018 DumpSegName = DumpSegSectName.first;
1019 DumpSectName = DumpSegSectName.second;
1020 } else {
1021 DumpSegName = "";
1022 DumpSectName = DumpSegSectName.first;
1023 }
1024 for (const SectionRef &Section : O->sections()) {
1025 StringRef SectName;
1026 Section.getName(SectName);
1027 DataRefImpl Ref = Section.getRawDataRefImpl();
1028 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1029 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1030 (SectName == DumpSectName)) {
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001031
Kevin Enderby95df54c2015-02-04 01:01:38 +00001032 uint32_t section_flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001033 if (O->is64Bit()) {
1034 const MachO::section_64 Sec = O->getSection64(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001035 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001036
1037 } else {
1038 const MachO::section Sec = O->getSection(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001039 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001040 }
Kevin Enderby95df54c2015-02-04 01:01:38 +00001041 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001042
1043 StringRef BytesStr;
1044 Section.getContents(BytesStr);
1045 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1046 uint32_t sect_size = BytesStr.size();
1047 uint64_t sect_addr = Section.getAddress();
1048
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001049 if (Raw) {
1050 outs().write(BytesStr.data(), BytesStr.size());
1051 continue;
1052 }
1053
1054 outs() << "Contents of (" << SegName << "," << SectName
1055 << ") section\n";
1056
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001057 if (verbose) {
Kevin Enderby95df54c2015-02-04 01:01:38 +00001058 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1059 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1060 DisassembleMachO(Filename, O, SegName, SectName);
1061 continue;
1062 }
Kevin Enderbycd66be52015-03-11 22:06:32 +00001063 if (SegName == "__TEXT" && SectName == "__info_plist") {
1064 outs() << sect;
1065 continue;
1066 }
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001067 if (SegName == "__OBJC" && SectName == "__protocol") {
1068 DumpProtocolSection(O, sect, sect_size, sect_addr);
1069 continue;
1070 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001071 switch (section_type) {
1072 case MachO::S_REGULAR:
1073 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1074 break;
1075 case MachO::S_ZEROFILL:
1076 outs() << "zerofill section and has no contents in the file\n";
1077 break;
Kevin Enderby10ba0412015-02-04 21:38:42 +00001078 case MachO::S_CSTRING_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001079 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby10ba0412015-02-04 21:38:42 +00001080 break;
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001081 case MachO::S_4BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001082 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001083 break;
1084 case MachO::S_8BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001085 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001086 break;
1087 case MachO::S_16BYTE_LITERALS:
Kevin Enderby0fc11822015-04-01 20:57:01 +00001088 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1089 break;
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001090 case MachO::S_LITERAL_POINTERS:
1091 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001092 !NoLeadingAddr);
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001093 break;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001094 case MachO::S_MOD_INIT_FUNC_POINTERS:
1095 case MachO::S_MOD_TERM_FUNC_POINTERS:
1096 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1097 verbose);
1098 break;
1099 default:
1100 outs() << "Unknown section type ("
1101 << format("0x%08" PRIx32, section_type) << ")\n";
1102 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1103 break;
1104 }
1105 } else {
1106 if (section_type == MachO::S_ZEROFILL)
1107 outs() << "zerofill section and has no contents in the file\n";
1108 else
1109 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1110 }
1111 }
1112 }
1113 }
1114}
1115
Kevin Enderbycd66be52015-03-11 22:06:32 +00001116static void DumpInfoPlistSectionContents(StringRef Filename,
1117 MachOObjectFile *O) {
1118 for (const SectionRef &Section : O->sections()) {
1119 StringRef SectName;
1120 Section.getName(SectName);
1121 DataRefImpl Ref = Section.getRawDataRefImpl();
1122 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1123 if (SegName == "__TEXT" && SectName == "__info_plist") {
1124 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1125 StringRef BytesStr;
1126 Section.getContents(BytesStr);
1127 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1128 outs() << sect;
1129 return;
1130 }
1131 }
1132}
1133
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001134// checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1135// and if it is and there is a list of architecture flags is specified then
1136// check to make sure this Mach-O file is one of those architectures or all
1137// architectures were specified. If not then an error is generated and this
1138// routine returns false. Else it returns true.
1139static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1140 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1141 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1142 bool ArchFound = false;
1143 MachO::mach_header H;
1144 MachO::mach_header_64 H_64;
1145 Triple T;
1146 if (MachO->is64Bit()) {
1147 H_64 = MachO->MachOObjectFile::getHeader64();
1148 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1149 } else {
1150 H = MachO->MachOObjectFile::getHeader();
1151 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1152 }
1153 unsigned i;
1154 for (i = 0; i < ArchFlags.size(); ++i) {
1155 if (ArchFlags[i] == T.getArchName())
1156 ArchFound = true;
1157 break;
1158 }
1159 if (!ArchFound) {
1160 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1161 << "architecture: " + ArchFlags[i] + "\n";
1162 return false;
1163 }
1164 }
1165 return true;
1166}
1167
Kevin Enderby0fc11822015-04-01 20:57:01 +00001168static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1169
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001170// ProcessMachO() is passed a single opened Mach-O file, which may be an
1171// archive member and or in a slice of a universal file. It prints the
1172// the file name and header info and then processes it according to the
1173// command line options.
1174static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1175 StringRef ArchiveMemberName = StringRef(),
1176 StringRef ArchitectureName = StringRef()) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001177 // If we are doing some processing here on the Mach-O file print the header
1178 // info. And don't print it otherwise like in the case of printing the
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001179 // UniversalHeaders or ArchiveHeaders.
Kevin Enderby131d1772015-01-09 19:22:37 +00001180 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001181 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
Kevin Enderby0fc11822015-04-01 20:57:01 +00001182 DylibsUsed || DylibId || ObjcMetaData ||
1183 (DumpSections.size() != 0 && !Raw)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001184 outs() << Filename;
1185 if (!ArchiveMemberName.empty())
1186 outs() << '(' << ArchiveMemberName << ')';
1187 if (!ArchitectureName.empty())
1188 outs() << " (architecture " << ArchitectureName << ")";
1189 outs() << ":\n";
1190 }
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001191
1192 if (Disassemble)
Kevin Enderby95df54c2015-02-04 01:01:38 +00001193 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001194 if (IndirectSymbols)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001195 PrintIndirectSymbols(MachOOF, !NonVerbose);
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001196 if (DataInCode)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001197 PrintDataInCodeTable(MachOOF, !NonVerbose);
Kevin Enderby9a509442015-01-27 21:28:24 +00001198 if (LinkOptHints)
1199 PrintLinkOptHints(MachOOF);
Kevin Enderby98da6132015-01-20 21:47:46 +00001200 if (Relocations)
1201 PrintRelocations(MachOOF);
1202 if (SectionHeaders)
1203 PrintSectionHeaders(MachOOF);
1204 if (SectionContents)
1205 PrintSectionContents(MachOOF);
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001206 if (DumpSections.size() != 0)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001207 DumpSectionContents(Filename, MachOOF, !NonVerbose);
Kevin Enderbycd66be52015-03-11 22:06:32 +00001208 if (InfoPlist)
1209 DumpInfoPlistSectionContents(Filename, MachOOF);
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001210 if (DylibsUsed)
1211 PrintDylibs(MachOOF, false);
1212 if (DylibId)
1213 PrintDylibs(MachOOF, true);
Kevin Enderby98da6132015-01-20 21:47:46 +00001214 if (SymbolTable)
1215 PrintSymbolTable(MachOOF);
1216 if (UnwindInfo)
1217 printMachOUnwindInfo(MachOOF);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001218 if (PrivateHeaders)
1219 printMachOFileHeader(MachOOF);
Kevin Enderby0fc11822015-04-01 20:57:01 +00001220 if (ObjcMetaData)
1221 printObjcMetaData(MachOOF, !NonVerbose);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001222 if (ExportsTrie)
1223 printExportsTrie(MachOOF);
1224 if (Rebase)
1225 printRebaseTable(MachOOF);
1226 if (Bind)
1227 printBindTable(MachOOF);
1228 if (LazyBind)
1229 printLazyBindTable(MachOOF);
1230 if (WeakBind)
1231 printWeakBindTable(MachOOF);
1232}
1233
Kevin Enderby131d1772015-01-09 19:22:37 +00001234// printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1235static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1236 outs() << " cputype (" << cputype << ")\n";
1237 outs() << " cpusubtype (" << cpusubtype << ")\n";
1238}
1239
1240// printCPUType() helps print_fat_headers by printing the cputype and
1241// pusubtype (symbolically for the one's it knows about).
1242static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1243 switch (cputype) {
1244 case MachO::CPU_TYPE_I386:
1245 switch (cpusubtype) {
1246 case MachO::CPU_SUBTYPE_I386_ALL:
1247 outs() << " cputype CPU_TYPE_I386\n";
1248 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1249 break;
1250 default:
1251 printUnknownCPUType(cputype, cpusubtype);
1252 break;
1253 }
1254 break;
1255 case MachO::CPU_TYPE_X86_64:
1256 switch (cpusubtype) {
1257 case MachO::CPU_SUBTYPE_X86_64_ALL:
1258 outs() << " cputype CPU_TYPE_X86_64\n";
1259 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1260 break;
1261 case MachO::CPU_SUBTYPE_X86_64_H:
1262 outs() << " cputype CPU_TYPE_X86_64\n";
1263 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1264 break;
1265 default:
1266 printUnknownCPUType(cputype, cpusubtype);
1267 break;
1268 }
1269 break;
1270 case MachO::CPU_TYPE_ARM:
1271 switch (cpusubtype) {
1272 case MachO::CPU_SUBTYPE_ARM_ALL:
1273 outs() << " cputype CPU_TYPE_ARM\n";
1274 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1275 break;
1276 case MachO::CPU_SUBTYPE_ARM_V4T:
1277 outs() << " cputype CPU_TYPE_ARM\n";
1278 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1279 break;
1280 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1281 outs() << " cputype CPU_TYPE_ARM\n";
1282 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1283 break;
1284 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1285 outs() << " cputype CPU_TYPE_ARM\n";
1286 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1287 break;
1288 case MachO::CPU_SUBTYPE_ARM_V6:
1289 outs() << " cputype CPU_TYPE_ARM\n";
1290 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1291 break;
1292 case MachO::CPU_SUBTYPE_ARM_V6M:
1293 outs() << " cputype CPU_TYPE_ARM\n";
1294 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1295 break;
1296 case MachO::CPU_SUBTYPE_ARM_V7:
1297 outs() << " cputype CPU_TYPE_ARM\n";
1298 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1299 break;
1300 case MachO::CPU_SUBTYPE_ARM_V7EM:
1301 outs() << " cputype CPU_TYPE_ARM\n";
1302 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1303 break;
1304 case MachO::CPU_SUBTYPE_ARM_V7K:
1305 outs() << " cputype CPU_TYPE_ARM\n";
1306 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1307 break;
1308 case MachO::CPU_SUBTYPE_ARM_V7M:
1309 outs() << " cputype CPU_TYPE_ARM\n";
1310 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1311 break;
1312 case MachO::CPU_SUBTYPE_ARM_V7S:
1313 outs() << " cputype CPU_TYPE_ARM\n";
1314 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1315 break;
1316 default:
1317 printUnknownCPUType(cputype, cpusubtype);
1318 break;
1319 }
1320 break;
1321 case MachO::CPU_TYPE_ARM64:
1322 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1323 case MachO::CPU_SUBTYPE_ARM64_ALL:
1324 outs() << " cputype CPU_TYPE_ARM64\n";
1325 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1326 break;
1327 default:
1328 printUnknownCPUType(cputype, cpusubtype);
1329 break;
1330 }
1331 break;
1332 default:
1333 printUnknownCPUType(cputype, cpusubtype);
1334 break;
1335 }
1336}
1337
1338static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1339 bool verbose) {
1340 outs() << "Fat headers\n";
1341 if (verbose)
1342 outs() << "fat_magic FAT_MAGIC\n";
1343 else
1344 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1345
1346 uint32_t nfat_arch = UB->getNumberOfObjects();
1347 StringRef Buf = UB->getData();
1348 uint64_t size = Buf.size();
1349 uint64_t big_size = sizeof(struct MachO::fat_header) +
1350 nfat_arch * sizeof(struct MachO::fat_arch);
1351 outs() << "nfat_arch " << UB->getNumberOfObjects();
1352 if (nfat_arch == 0)
1353 outs() << " (malformed, contains zero architecture types)\n";
1354 else if (big_size > size)
1355 outs() << " (malformed, architectures past end of file)\n";
1356 else
1357 outs() << "\n";
1358
1359 for (uint32_t i = 0; i < nfat_arch; ++i) {
1360 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1361 uint32_t cputype = OFA.getCPUType();
1362 uint32_t cpusubtype = OFA.getCPUSubType();
1363 outs() << "architecture ";
1364 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1365 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1366 uint32_t other_cputype = other_OFA.getCPUType();
1367 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
Kevin Enderby0512bd72015-01-09 21:55:03 +00001368 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
Kevin Enderby131d1772015-01-09 19:22:37 +00001369 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
Kevin Enderby0512bd72015-01-09 21:55:03 +00001370 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001371 outs() << "(illegal duplicate architecture) ";
1372 break;
Kevin Enderby0512bd72015-01-09 21:55:03 +00001373 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001374 }
1375 if (verbose) {
1376 outs() << OFA.getArchTypeName() << "\n";
1377 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1378 } else {
1379 outs() << i << "\n";
1380 outs() << " cputype " << cputype << "\n";
1381 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1382 << "\n";
1383 }
1384 if (verbose &&
1385 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1386 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1387 else
1388 outs() << " capabilities "
1389 << format("0x%" PRIx32,
1390 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1391 outs() << " offset " << OFA.getOffset();
1392 if (OFA.getOffset() > size)
1393 outs() << " (past end of file)";
1394 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1395 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1396 outs() << "\n";
1397 outs() << " size " << OFA.getSize();
1398 big_size = OFA.getOffset() + OFA.getSize();
1399 if (big_size > size)
1400 outs() << " (past end of file)";
1401 outs() << "\n";
1402 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1403 << ")\n";
1404 }
1405}
1406
Kevin Enderby13023a12015-01-15 23:19:11 +00001407static void printArchiveChild(Archive::Child &C, bool verbose,
1408 bool print_offset) {
1409 if (print_offset)
1410 outs() << C.getChildOffset() << "\t";
1411 sys::fs::perms Mode = C.getAccessMode();
1412 if (verbose) {
1413 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1414 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1415 outs() << "-";
1416 if (Mode & sys::fs::owner_read)
1417 outs() << "r";
1418 else
1419 outs() << "-";
1420 if (Mode & sys::fs::owner_write)
1421 outs() << "w";
1422 else
1423 outs() << "-";
1424 if (Mode & sys::fs::owner_exe)
1425 outs() << "x";
1426 else
1427 outs() << "-";
1428 if (Mode & sys::fs::group_read)
1429 outs() << "r";
1430 else
1431 outs() << "-";
1432 if (Mode & sys::fs::group_write)
1433 outs() << "w";
1434 else
1435 outs() << "-";
1436 if (Mode & sys::fs::group_exe)
1437 outs() << "x";
1438 else
1439 outs() << "-";
1440 if (Mode & sys::fs::others_read)
1441 outs() << "r";
1442 else
1443 outs() << "-";
1444 if (Mode & sys::fs::others_write)
1445 outs() << "w";
1446 else
1447 outs() << "-";
1448 if (Mode & sys::fs::others_exe)
1449 outs() << "x";
1450 else
1451 outs() << "-";
1452 } else {
1453 outs() << format("0%o ", Mode);
1454 }
1455
1456 unsigned UID = C.getUID();
1457 outs() << format("%3d/", UID);
1458 unsigned GID = C.getGID();
1459 outs() << format("%-3d ", GID);
Kevin Enderbyc12718932015-01-16 22:10:36 +00001460 uint64_t Size = C.getRawSize();
Kevin Enderby479ee612015-01-23 21:02:44 +00001461 outs() << format("%5" PRId64, Size) << " ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001462
1463 StringRef RawLastModified = C.getRawLastModified();
1464 if (verbose) {
1465 unsigned Seconds;
1466 if (RawLastModified.getAsInteger(10, Seconds))
1467 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1468 else {
1469 // Since cime(3) returns a 26 character string of the form:
1470 // "Sun Sep 16 01:03:52 1973\n\0"
1471 // just print 24 characters.
1472 time_t t = Seconds;
1473 outs() << format("%.24s ", ctime(&t));
1474 }
1475 } else {
1476 outs() << RawLastModified << " ";
1477 }
1478
1479 if (verbose) {
1480 ErrorOr<StringRef> NameOrErr = C.getName();
1481 if (NameOrErr.getError()) {
1482 StringRef RawName = C.getRawName();
1483 outs() << RawName << "\n";
1484 } else {
1485 StringRef Name = NameOrErr.get();
1486 outs() << Name << "\n";
1487 }
1488 } else {
1489 StringRef RawName = C.getRawName();
1490 outs() << RawName << "\n";
1491 }
1492}
1493
1494static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
1495 if (A->hasSymbolTable()) {
1496 Archive::child_iterator S = A->getSymbolTableChild();
1497 Archive::Child C = *S;
1498 printArchiveChild(C, verbose, print_offset);
1499 }
1500 for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E;
1501 ++I) {
1502 Archive::Child C = *I;
1503 printArchiveChild(C, verbose, print_offset);
1504 }
1505}
1506
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001507// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1508// -arch flags selecting just those slices as specified by them and also parses
1509// archive files. Then for each individual Mach-O file ProcessMachO() is
1510// called to process the file based on the command line options.
1511void llvm::ParseInputMachO(StringRef Filename) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001512 // Check for -arch all and verifiy the -arch flags are valid.
1513 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1514 if (ArchFlags[i] == "all") {
1515 ArchAll = true;
1516 } else {
1517 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1518 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1519 "'for the -arch option\n";
1520 return;
1521 }
1522 }
1523 }
1524
1525 // Attempt to open the binary.
1526 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1527 if (std::error_code EC = BinaryOrErr.getError()) {
1528 errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n";
Rafael Espindolade882cd2014-12-03 23:29:34 +00001529 return;
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001530 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001531 Binary &Bin = *BinaryOrErr.get().getBinary();
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001532
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001533 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1534 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001535 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001536 printArchiveHeaders(A, !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001537 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1538 I != E; ++I) {
1539 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary();
1540 if (ChildOrErr.getError())
1541 continue;
1542 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1543 if (!checkMachOAndArchFlags(O, Filename))
1544 return;
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001545 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001546 }
1547 }
1548 return;
1549 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001550 if (UniversalHeaders) {
1551 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
Kevin Enderbyf0640752015-03-13 17:56:32 +00001552 printMachOUniversalHeaders(UB, !NonVerbose);
Kevin Enderby131d1772015-01-09 19:22:37 +00001553 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001554 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1555 // If we have a list of architecture flags specified dump only those.
1556 if (!ArchAll && ArchFlags.size() != 0) {
1557 // Look for a slice in the universal binary that matches each ArchFlag.
1558 bool ArchFound;
1559 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1560 ArchFound = false;
1561 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1562 E = UB->end_objects();
1563 I != E; ++I) {
1564 if (ArchFlags[i] == I->getArchTypeName()) {
1565 ArchFound = true;
1566 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1567 I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001568 std::string ArchitectureName = "";
1569 if (ArchFlags.size() > 1)
1570 ArchitectureName = I->getArchTypeName();
1571 if (ObjOrErr) {
1572 ObjectFile &O = *ObjOrErr.get();
1573 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001574 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001575 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1576 I->getAsArchive()) {
1577 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001578 outs() << "Archive : " << Filename;
1579 if (!ArchitectureName.empty())
1580 outs() << " (architecture " << ArchitectureName << ")";
1581 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001582 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001583 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001584 for (Archive::child_iterator AI = A->child_begin(),
1585 AE = A->child_end();
1586 AI != AE; ++AI) {
1587 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1588 if (ChildOrErr.getError())
1589 continue;
1590 if (MachOObjectFile *O =
1591 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001592 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001593 }
1594 }
1595 }
1596 }
1597 if (!ArchFound) {
1598 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1599 << "architecture: " + ArchFlags[i] + "\n";
1600 return;
1601 }
1602 }
1603 return;
1604 }
1605 // No architecture flags were specified so if this contains a slice that
1606 // matches the host architecture dump only that.
1607 if (!ArchAll) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001608 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1609 E = UB->end_objects();
1610 I != E; ++I) {
Kevin Enderby0512bd72015-01-09 21:55:03 +00001611 if (MachOObjectFile::getHostArch().getArchName() ==
1612 I->getArchTypeName()) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001613 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001614 std::string ArchiveName;
1615 ArchiveName.clear();
1616 if (ObjOrErr) {
1617 ObjectFile &O = *ObjOrErr.get();
1618 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001619 ProcessMachO(Filename, MachOOF);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001620 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1621 I->getAsArchive()) {
1622 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001623 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001624 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001625 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001626 for (Archive::child_iterator AI = A->child_begin(),
1627 AE = A->child_end();
1628 AI != AE; ++AI) {
1629 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1630 if (ChildOrErr.getError())
1631 continue;
1632 if (MachOObjectFile *O =
1633 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001634 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001635 }
1636 }
1637 return;
1638 }
1639 }
1640 }
1641 // Either all architectures have been specified or none have been specified
1642 // and this does not contain the host architecture so dump all the slices.
1643 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1644 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1645 E = UB->end_objects();
1646 I != E; ++I) {
1647 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001648 std::string ArchitectureName = "";
1649 if (moreThanOneArch)
1650 ArchitectureName = I->getArchTypeName();
1651 if (ObjOrErr) {
1652 ObjectFile &Obj = *ObjOrErr.get();
1653 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001654 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001655 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1656 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001657 outs() << "Archive : " << Filename;
1658 if (!ArchitectureName.empty())
1659 outs() << " (architecture " << ArchitectureName << ")";
1660 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001661 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001662 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001663 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1664 AI != AE; ++AI) {
1665 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1666 if (ChildOrErr.getError())
1667 continue;
1668 if (MachOObjectFile *O =
1669 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1670 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001671 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1672 ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001673 }
1674 }
1675 }
1676 }
1677 return;
1678 }
1679 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1680 if (!checkMachOAndArchFlags(O, Filename))
1681 return;
1682 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001683 ProcessMachO(Filename, MachOOF);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001684 } else
1685 errs() << "llvm-objdump: '" << Filename << "': "
1686 << "Object is not a Mach-O file type.\n";
1687 } else
1688 errs() << "llvm-objdump: '" << Filename << "': "
1689 << "Unrecognized file type.\n";
Rafael Espindola9b709252013-04-13 01:45:40 +00001690}
1691
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001692typedef std::pair<uint64_t, const char *> BindInfoEntry;
1693typedef std::vector<BindInfoEntry> BindTable;
1694typedef BindTable::iterator bind_table_iterator;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001695
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001696// The block of info used by the Symbolizer call backs.
1697struct DisassembleInfo {
1698 bool verbose;
1699 MachOObjectFile *O;
1700 SectionRef S;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001701 SymbolAddressMap *AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001702 std::vector<SectionRef> *Sections;
1703 const char *class_name;
1704 const char *selector_name;
1705 char *method;
Kevin Enderby04bf6932014-10-28 23:39:46 +00001706 char *demangled_name;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001707 uint64_t adrp_addr;
1708 uint32_t adrp_inst;
Kevin Enderby078be602014-10-23 19:53:12 +00001709 BindTable *bindtable;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001710};
1711
1712// SymbolizerGetOpInfo() is the operand information call back function.
1713// This is called to get the symbolic information for operand(s) of an
1714// instruction when it is being done. This routine does this from
1715// the relocation information, symbol table, etc. That block of information
1716// is a pointer to the struct DisassembleInfo that was passed when the
1717// disassembler context was created and passed to back to here when
1718// called back by the disassembler for instruction operands that could have
1719// relocation information. The address of the instruction containing operand is
1720// at the Pc parameter. The immediate value the operand has is passed in
1721// op_info->Value and is at Offset past the start of the instruction and has a
1722// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1723// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1724// names and addends of the symbolic expression to add for the operand. The
1725// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1726// information is returned then this function returns 1 else it returns 0.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00001727static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1728 uint64_t Size, int TagType, void *TagBuf) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001729 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1730 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001731 uint64_t value = op_info->Value;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001732
1733 // Make sure all fields returned are zero if we don't set them.
1734 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1735 op_info->Value = value;
1736
1737 // If the TagType is not the value 1 which it code knows about or if no
1738 // verbose symbolic information is wanted then just return 0, indicating no
1739 // information is being returned.
David Blaikie33dd45d02015-03-23 18:39:02 +00001740 if (TagType != 1 || !info->verbose)
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001741 return 0;
1742
1743 unsigned int Arch = info->O->getArch();
1744 if (Arch == Triple::x86) {
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001745 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1746 return 0;
1747 // First search the section's relocation entries (if any) for an entry
1748 // for this section offset.
1749 uint32_t sect_addr = info->S.getAddress();
1750 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1751 bool reloc_found = false;
1752 DataRefImpl Rel;
1753 MachO::any_relocation_info RE;
1754 bool isExtern = false;
1755 SymbolRef Symbol;
1756 bool r_scattered = false;
1757 uint32_t r_value, pair_r_value, r_type;
1758 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001759 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001760 if (RelocOffset == sect_offset) {
1761 Rel = Reloc.getRawDataRefImpl();
1762 RE = info->O->getRelocation(Rel);
Kevin Enderby3eb73e12014-11-11 19:16:45 +00001763 r_type = info->O->getAnyRelocationType(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001764 r_scattered = info->O->isRelocationScattered(RE);
1765 if (r_scattered) {
1766 r_value = info->O->getScatteredRelocationValue(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001767 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1768 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1769 DataRefImpl RelNext = Rel;
1770 info->O->moveRelocationNext(RelNext);
1771 MachO::any_relocation_info RENext;
1772 RENext = info->O->getRelocation(RelNext);
1773 if (info->O->isRelocationScattered(RENext))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001774 pair_r_value = info->O->getScatteredRelocationValue(RENext);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001775 else
1776 return 0;
1777 }
1778 } else {
1779 isExtern = info->O->getPlainRelocationExternal(RE);
1780 if (isExtern) {
1781 symbol_iterator RelocSym = Reloc.getSymbol();
1782 Symbol = *RelocSym;
1783 }
1784 }
1785 reloc_found = true;
1786 break;
1787 }
1788 }
1789 if (reloc_found && isExtern) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001790 ErrorOr<StringRef> SymName = Symbol.getName();
1791 if (std::error_code EC = SymName.getError())
1792 report_fatal_error(EC.message());
1793 const char *name = SymName->data();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001794 op_info->AddSymbol.Present = 1;
1795 op_info->AddSymbol.Name = name;
1796 // For i386 extern relocation entries the value in the instruction is
1797 // the offset from the symbol, and value is already set in op_info->Value.
1798 return 1;
1799 }
1800 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1801 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001802 const char *add = GuessSymbolName(r_value, info->AddrMap);
1803 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001804 uint32_t offset = value - (r_value - pair_r_value);
1805 op_info->AddSymbol.Present = 1;
1806 if (add != nullptr)
1807 op_info->AddSymbol.Name = add;
1808 else
1809 op_info->AddSymbol.Value = r_value;
1810 op_info->SubtractSymbol.Present = 1;
1811 if (sub != nullptr)
1812 op_info->SubtractSymbol.Name = sub;
1813 else
1814 op_info->SubtractSymbol.Value = pair_r_value;
1815 op_info->Value = offset;
1816 return 1;
1817 }
1818 // TODO:
1819 // Second search the external relocation entries of a fully linked image
1820 // (if any) for an entry that matches this segment offset.
1821 // uint32_t seg_offset = (Pc + Offset);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001822 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001823 }
1824 if (Arch == Triple::x86_64) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001825 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1826 return 0;
1827 // First search the section's relocation entries (if any) for an entry
1828 // for this section offset.
Rafael Espindola80291272014-10-08 15:28:58 +00001829 uint64_t sect_addr = info->S.getAddress();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001830 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1831 bool reloc_found = false;
1832 DataRefImpl Rel;
1833 MachO::any_relocation_info RE;
1834 bool isExtern = false;
1835 SymbolRef Symbol;
1836 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001837 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001838 if (RelocOffset == sect_offset) {
1839 Rel = Reloc.getRawDataRefImpl();
1840 RE = info->O->getRelocation(Rel);
1841 // NOTE: Scattered relocations don't exist on x86_64.
1842 isExtern = info->O->getPlainRelocationExternal(RE);
1843 if (isExtern) {
1844 symbol_iterator RelocSym = Reloc.getSymbol();
1845 Symbol = *RelocSym;
1846 }
1847 reloc_found = true;
1848 break;
1849 }
1850 }
1851 if (reloc_found && isExtern) {
1852 // The Value passed in will be adjusted by the Pc if the instruction
1853 // adds the Pc. But for x86_64 external relocation entries the Value
1854 // is the offset from the external symbol.
1855 if (info->O->getAnyRelocationPCRel(RE))
1856 op_info->Value -= Pc + Offset + Size;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001857 ErrorOr<StringRef> SymName = Symbol.getName();
1858 if (std::error_code EC = SymName.getError())
1859 report_fatal_error(EC.message());
1860 const char *name = SymName->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001861 unsigned Type = info->O->getAnyRelocationType(RE);
1862 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1863 DataRefImpl RelNext = Rel;
1864 info->O->moveRelocationNext(RelNext);
1865 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1866 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1867 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1868 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1869 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1870 op_info->SubtractSymbol.Present = 1;
1871 op_info->SubtractSymbol.Name = name;
1872 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1873 Symbol = *RelocSymNext;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001874 ErrorOr<StringRef> SymNameNext = Symbol.getName();
1875 if (std::error_code EC = SymNameNext.getError())
1876 report_fatal_error(EC.message());
1877 name = SymNameNext->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001878 }
1879 }
1880 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1881 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1882 op_info->AddSymbol.Present = 1;
1883 op_info->AddSymbol.Name = name;
1884 return 1;
1885 }
1886 // TODO:
1887 // Second search the external relocation entries of a fully linked image
1888 // (if any) for an entry that matches this segment offset.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001889 // uint64_t seg_offset = (Pc + Offset);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001890 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001891 }
1892 if (Arch == Triple::arm) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001893 if (Offset != 0 || (Size != 4 && Size != 2))
1894 return 0;
1895 // First search the section's relocation entries (if any) for an entry
1896 // for this section offset.
1897 uint32_t sect_addr = info->S.getAddress();
1898 uint32_t sect_offset = (Pc + Offset) - sect_addr;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001899 DataRefImpl Rel;
1900 MachO::any_relocation_info RE;
1901 bool isExtern = false;
1902 SymbolRef Symbol;
1903 bool r_scattered = false;
1904 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
David Blaikie33dd45d02015-03-23 18:39:02 +00001905 auto Reloc =
1906 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1907 [&](const RelocationRef &Reloc) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001908 uint64_t RelocOffset = Reloc.getOffset();
David Blaikie33dd45d02015-03-23 18:39:02 +00001909 return RelocOffset == sect_offset;
1910 });
1911
1912 if (Reloc == info->S.relocations().end())
1913 return 0;
1914
1915 Rel = Reloc->getRawDataRefImpl();
1916 RE = info->O->getRelocation(Rel);
1917 r_length = info->O->getAnyRelocationLength(RE);
1918 r_scattered = info->O->isRelocationScattered(RE);
1919 if (r_scattered) {
1920 r_value = info->O->getScatteredRelocationValue(RE);
1921 r_type = info->O->getScatteredRelocationType(RE);
1922 } else {
1923 r_type = info->O->getAnyRelocationType(RE);
1924 isExtern = info->O->getPlainRelocationExternal(RE);
1925 if (isExtern) {
1926 symbol_iterator RelocSym = Reloc->getSymbol();
1927 Symbol = *RelocSym;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001928 }
1929 }
David Blaikie33dd45d02015-03-23 18:39:02 +00001930 if (r_type == MachO::ARM_RELOC_HALF ||
1931 r_type == MachO::ARM_RELOC_SECTDIFF ||
1932 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
1933 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1934 DataRefImpl RelNext = Rel;
1935 info->O->moveRelocationNext(RelNext);
1936 MachO::any_relocation_info RENext;
1937 RENext = info->O->getRelocation(RelNext);
1938 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
1939 if (info->O->isRelocationScattered(RENext))
1940 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1941 }
1942
1943 if (isExtern) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001944 ErrorOr<StringRef> SymName = Symbol.getName();
1945 if (std::error_code EC = SymName.getError())
1946 report_fatal_error(EC.message());
1947 const char *name = SymName->data();
Kevin Enderby930fdc72014-11-06 19:00:13 +00001948 op_info->AddSymbol.Present = 1;
1949 op_info->AddSymbol.Name = name;
Sylvestre Ledru648cced2015-02-05 17:00:23 +00001950 switch (r_type) {
1951 case MachO::ARM_RELOC_HALF:
1952 if ((r_length & 0x1) == 1) {
1953 op_info->Value = value << 16 | other_half;
1954 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1955 } else {
1956 op_info->Value = other_half << 16 | value;
1957 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Sylvestre Ledrufe0c7ad2015-02-05 16:35:44 +00001958 }
Sylvestre Ledru648cced2015-02-05 17:00:23 +00001959 break;
1960 default:
1961 break;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001962 }
1963 return 1;
1964 }
1965 // If we have a branch that is not an external relocation entry then
1966 // return 0 so the code in tryAddingSymbolicOperand() can use the
1967 // SymbolLookUp call back with the branch target address to look up the
1968 // symbol and possiblity add an annotation for a symbol stub.
David Blaikie33dd45d02015-03-23 18:39:02 +00001969 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
1970 r_type == MachO::ARM_THUMB_RELOC_BR22))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001971 return 0;
1972
1973 uint32_t offset = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001974 if (r_type == MachO::ARM_RELOC_HALF ||
1975 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1976 if ((r_length & 0x1) == 1)
1977 value = value << 16 | other_half;
1978 else
1979 value = other_half << 16 | value;
1980 }
1981 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
1982 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
1983 offset = value - r_value;
1984 value = r_value;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001985 }
1986
David Blaikie33dd45d02015-03-23 18:39:02 +00001987 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001988 if ((r_length & 0x1) == 1)
1989 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1990 else
1991 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001992 const char *add = GuessSymbolName(r_value, info->AddrMap);
1993 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00001994 int32_t offset = value - (r_value - pair_r_value);
1995 op_info->AddSymbol.Present = 1;
1996 if (add != nullptr)
1997 op_info->AddSymbol.Name = add;
1998 else
1999 op_info->AddSymbol.Value = r_value;
2000 op_info->SubtractSymbol.Present = 1;
2001 if (sub != nullptr)
2002 op_info->SubtractSymbol.Name = sub;
2003 else
2004 op_info->SubtractSymbol.Value = pair_r_value;
2005 op_info->Value = offset;
2006 return 1;
2007 }
2008
Kevin Enderby930fdc72014-11-06 19:00:13 +00002009 op_info->AddSymbol.Present = 1;
2010 op_info->Value = offset;
David Blaikie33dd45d02015-03-23 18:39:02 +00002011 if (r_type == MachO::ARM_RELOC_HALF) {
2012 if ((r_length & 0x1) == 1)
2013 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2014 else
2015 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002016 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002017 const char *add = GuessSymbolName(value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002018 if (add != nullptr) {
2019 op_info->AddSymbol.Name = add;
2020 return 1;
2021 }
2022 op_info->AddSymbol.Value = value;
2023 return 1;
David Blaikie33dd45d02015-03-23 18:39:02 +00002024 }
2025 if (Arch == Triple::aarch64) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002026 if (Offset != 0 || Size != 4)
2027 return 0;
2028 // First search the section's relocation entries (if any) for an entry
2029 // for this section offset.
2030 uint64_t sect_addr = info->S.getAddress();
2031 uint64_t sect_offset = (Pc + Offset) - sect_addr;
David Blaikie33dd45d02015-03-23 18:39:02 +00002032 auto Reloc =
2033 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2034 [&](const RelocationRef &Reloc) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002035 uint64_t RelocOffset = Reloc.getOffset();
David Blaikie33dd45d02015-03-23 18:39:02 +00002036 return RelocOffset == sect_offset;
2037 });
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002038
David Blaikie33dd45d02015-03-23 18:39:02 +00002039 if (Reloc == info->S.relocations().end())
2040 return 0;
2041
2042 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2043 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2044 uint32_t r_type = info->O->getAnyRelocationType(RE);
2045 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2046 DataRefImpl RelNext = Rel;
2047 info->O->moveRelocationNext(RelNext);
2048 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2049 if (value == 0) {
2050 value = info->O->getPlainRelocationSymbolNum(RENext);
2051 op_info->Value = value;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002052 }
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002053 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002054 // NOTE: Scattered relocations don't exist on arm64.
2055 if (!info->O->getPlainRelocationExternal(RE))
2056 return 0;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002057 ErrorOr<StringRef> SymName = Reloc->getSymbol()->getName();
2058 if (std::error_code EC = SymName.getError())
2059 report_fatal_error(EC.message());
2060 const char *name = SymName->data();
David Blaikie33dd45d02015-03-23 18:39:02 +00002061 op_info->AddSymbol.Present = 1;
2062 op_info->AddSymbol.Name = name;
2063
2064 switch (r_type) {
2065 case MachO::ARM64_RELOC_PAGE21:
2066 /* @page */
2067 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2068 break;
2069 case MachO::ARM64_RELOC_PAGEOFF12:
2070 /* @pageoff */
2071 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2072 break;
2073 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2074 /* @gotpage */
2075 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2076 break;
2077 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2078 /* @gotpageoff */
2079 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2080 break;
2081 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2082 /* @tvlppage is not implemented in llvm-mc */
2083 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2084 break;
2085 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2086 /* @tvlppageoff is not implemented in llvm-mc */
2087 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2088 break;
2089 default:
2090 case MachO::ARM64_RELOC_BRANCH26:
2091 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2092 break;
2093 }
2094 return 1;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002095 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002096 return 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002097}
2098
Kevin Enderbybf246f52014-09-24 23:08:22 +00002099// GuessCstringPointer is passed the address of what might be a pointer to a
2100// literal string in a cstring section. If that address is in a cstring section
2101// it returns a pointer to that string. Else it returns nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002102static const char *GuessCstringPointer(uint64_t ReferenceValue,
2103 struct DisassembleInfo *info) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002104 for (const auto &Load : info->O->load_commands()) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00002105 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2106 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2107 for (unsigned J = 0; J < Seg.nsects; ++J) {
2108 MachO::section_64 Sec = info->O->getSection64(Load, J);
2109 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2110 if (section_type == MachO::S_CSTRING_LITERALS &&
2111 ReferenceValue >= Sec.addr &&
2112 ReferenceValue < Sec.addr + Sec.size) {
2113 uint64_t sect_offset = ReferenceValue - Sec.addr;
2114 uint64_t object_offset = Sec.offset + sect_offset;
2115 StringRef MachOContents = info->O->getData();
2116 uint64_t object_size = MachOContents.size();
2117 const char *object_addr = (const char *)MachOContents.data();
2118 if (object_offset < object_size) {
2119 const char *name = object_addr + object_offset;
2120 return name;
2121 } else {
2122 return nullptr;
2123 }
2124 }
2125 }
2126 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2127 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2128 for (unsigned J = 0; J < Seg.nsects; ++J) {
2129 MachO::section Sec = info->O->getSection(Load, J);
2130 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2131 if (section_type == MachO::S_CSTRING_LITERALS &&
2132 ReferenceValue >= Sec.addr &&
2133 ReferenceValue < Sec.addr + Sec.size) {
2134 uint64_t sect_offset = ReferenceValue - Sec.addr;
2135 uint64_t object_offset = Sec.offset + sect_offset;
2136 StringRef MachOContents = info->O->getData();
2137 uint64_t object_size = MachOContents.size();
2138 const char *object_addr = (const char *)MachOContents.data();
2139 if (object_offset < object_size) {
2140 const char *name = object_addr + object_offset;
2141 return name;
2142 } else {
2143 return nullptr;
2144 }
2145 }
2146 }
2147 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00002148 }
2149 return nullptr;
2150}
2151
Kevin Enderby85974882014-09-26 22:20:44 +00002152// GuessIndirectSymbol returns the name of the indirect symbol for the
2153// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2154// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2155// symbol name being referenced by the stub or pointer.
2156static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2157 struct DisassembleInfo *info) {
Kevin Enderby85974882014-09-26 22:20:44 +00002158 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2159 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002160 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby85974882014-09-26 22:20:44 +00002161 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2162 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2163 for (unsigned J = 0; J < Seg.nsects; ++J) {
2164 MachO::section_64 Sec = info->O->getSection64(Load, J);
2165 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2166 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2167 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2168 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2169 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2170 section_type == MachO::S_SYMBOL_STUBS) &&
2171 ReferenceValue >= Sec.addr &&
2172 ReferenceValue < Sec.addr + Sec.size) {
2173 uint32_t stride;
2174 if (section_type == MachO::S_SYMBOL_STUBS)
2175 stride = Sec.reserved2;
2176 else
2177 stride = 8;
2178 if (stride == 0)
2179 return nullptr;
2180 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2181 if (index < Dysymtab.nindirectsyms) {
2182 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002183 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002184 if (indirect_symbol < Symtab.nsyms) {
2185 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2186 SymbolRef Symbol = *Sym;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002187 ErrorOr<StringRef> SymName = Symbol.getName();
2188 if (std::error_code EC = SymName.getError())
2189 report_fatal_error(EC.message());
2190 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002191 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;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002222 ErrorOr<StringRef> SymName = Symbol.getName();
2223 if (std::error_code EC = SymName.getError())
2224 report_fatal_error(EC.message());
2225 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002226 return name;
2227 }
2228 }
2229 }
2230 }
2231 }
Kevin Enderby85974882014-09-26 22:20:44 +00002232 }
2233 return nullptr;
2234}
2235
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002236// method_reference() is called passing it the ReferenceName that might be
2237// a reference it to an Objective-C method call. If so then it allocates and
2238// assembles a method call string with the values last seen and saved in
2239// the DisassembleInfo's class_name and selector_name fields. This is saved
2240// into the method field of the info and any previous string is free'ed.
2241// Then the class_name field in the info is set to nullptr. The method call
2242// string is set into ReferenceName and ReferenceType is set to
2243// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2244// then both ReferenceType and ReferenceName are left unchanged.
2245static void method_reference(struct DisassembleInfo *info,
2246 uint64_t *ReferenceType,
2247 const char **ReferenceName) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002248 unsigned int Arch = info->O->getArch();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002249 if (*ReferenceName != nullptr) {
2250 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002251 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002252 if (info->method != nullptr)
2253 free(info->method);
2254 if (info->class_name != nullptr) {
2255 info->method = (char *)malloc(5 + strlen(info->class_name) +
2256 strlen(info->selector_name));
2257 if (info->method != nullptr) {
2258 strcpy(info->method, "+[");
2259 strcat(info->method, info->class_name);
2260 strcat(info->method, " ");
2261 strcat(info->method, info->selector_name);
2262 strcat(info->method, "]");
2263 *ReferenceName = info->method;
2264 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2265 }
2266 } else {
2267 info->method = (char *)malloc(9 + strlen(info->selector_name));
2268 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002269 if (Arch == Triple::x86_64)
2270 strcpy(info->method, "-[%rdi ");
2271 else if (Arch == Triple::aarch64)
2272 strcpy(info->method, "-[x0 ");
2273 else
2274 strcpy(info->method, "-[r? ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002275 strcat(info->method, info->selector_name);
2276 strcat(info->method, "]");
2277 *ReferenceName = info->method;
2278 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2279 }
2280 }
2281 info->class_name = nullptr;
2282 }
2283 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002284 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002285 if (info->method != nullptr)
2286 free(info->method);
2287 info->method = (char *)malloc(17 + strlen(info->selector_name));
2288 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002289 if (Arch == Triple::x86_64)
2290 strcpy(info->method, "-[[%rdi super] ");
2291 else if (Arch == Triple::aarch64)
2292 strcpy(info->method, "-[[x0 super] ");
2293 else
2294 strcpy(info->method, "-[[r? super] ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002295 strcat(info->method, info->selector_name);
2296 strcat(info->method, "]");
2297 *ReferenceName = info->method;
2298 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2299 }
2300 info->class_name = nullptr;
2301 }
2302 }
2303 }
2304}
2305
2306// GuessPointerPointer() is passed the address of what might be a pointer to
2307// a reference to an Objective-C class, selector, message ref or cfstring.
2308// If so the value of the pointer is returned and one of the booleans are set
2309// to true. If not zero is returned and all the booleans are set to false.
2310static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2311 struct DisassembleInfo *info,
2312 bool &classref, bool &selref, bool &msgref,
2313 bool &cfstring) {
2314 classref = false;
2315 selref = false;
2316 msgref = false;
2317 cfstring = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002318 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002319 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2320 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2321 for (unsigned J = 0; J < Seg.nsects; ++J) {
2322 MachO::section_64 Sec = info->O->getSection64(Load, J);
2323 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2324 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2325 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2326 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2327 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2328 ReferenceValue >= Sec.addr &&
2329 ReferenceValue < Sec.addr + Sec.size) {
2330 uint64_t sect_offset = ReferenceValue - Sec.addr;
2331 uint64_t object_offset = Sec.offset + sect_offset;
2332 StringRef MachOContents = info->O->getData();
2333 uint64_t object_size = MachOContents.size();
2334 const char *object_addr = (const char *)MachOContents.data();
2335 if (object_offset < object_size) {
2336 uint64_t pointer_value;
2337 memcpy(&pointer_value, object_addr + object_offset,
2338 sizeof(uint64_t));
2339 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2340 sys::swapByteOrder(pointer_value);
2341 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2342 selref = true;
2343 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2344 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2345 classref = true;
2346 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2347 ReferenceValue + 8 < Sec.addr + Sec.size) {
2348 msgref = true;
2349 memcpy(&pointer_value, object_addr + object_offset + 8,
2350 sizeof(uint64_t));
2351 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2352 sys::swapByteOrder(pointer_value);
2353 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2354 cfstring = true;
2355 return pointer_value;
2356 } else {
2357 return 0;
2358 }
2359 }
2360 }
2361 }
2362 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002363 }
2364 return 0;
2365}
2366
2367// get_pointer_64 returns a pointer to the bytes in the object file at the
2368// Address from a section in the Mach-O file. And indirectly returns the
2369// offset into the section, number of bytes left in the section past the offset
2370// and which section is was being referenced. If the Address is not in a
2371// section nullptr is returned.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002372static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2373 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002374 DisassembleInfo *info,
2375 bool objc_only = false) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002376 offset = 0;
2377 left = 0;
2378 S = SectionRef();
2379 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2380 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2381 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
Kevin Enderby846c0002015-04-16 17:19:59 +00002382 if (objc_only) {
2383 StringRef SectName;
2384 ((*(info->Sections))[SectIdx]).getName(SectName);
2385 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2386 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2387 if (SegName != "__OBJC" && SectName != "__cstring")
2388 continue;
2389 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002390 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2391 S = (*(info->Sections))[SectIdx];
2392 offset = Address - SectAddress;
2393 left = SectSize - offset;
2394 StringRef SectContents;
2395 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2396 return SectContents.data() + offset;
2397 }
2398 }
2399 return nullptr;
2400}
2401
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002402static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2403 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002404 DisassembleInfo *info,
2405 bool objc_only = false) {
2406 return get_pointer_64(Address, offset, left, S, info, objc_only);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002407}
2408
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002409// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2410// the symbol indirectly through n_value. Based on the relocation information
2411// for the specified section offset in the specified section reference.
Kevin Enderby0fc11822015-04-01 20:57:01 +00002412// If no relocation information is found and a non-zero ReferenceValue for the
2413// symbol is passed, look up that address in the info's AddrMap.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002414static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2415 DisassembleInfo *info, uint64_t &n_value,
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002416 uint64_t ReferenceValue = 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002417 n_value = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002418 if (!info->verbose)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002419 return nullptr;
2420
2421 // See if there is an external relocation entry at the sect_offset.
2422 bool reloc_found = false;
2423 DataRefImpl Rel;
2424 MachO::any_relocation_info RE;
2425 bool isExtern = false;
2426 SymbolRef Symbol;
2427 for (const RelocationRef &Reloc : S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002428 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002429 if (RelocOffset == sect_offset) {
2430 Rel = Reloc.getRawDataRefImpl();
2431 RE = info->O->getRelocation(Rel);
2432 if (info->O->isRelocationScattered(RE))
2433 continue;
2434 isExtern = info->O->getPlainRelocationExternal(RE);
2435 if (isExtern) {
2436 symbol_iterator RelocSym = Reloc.getSymbol();
2437 Symbol = *RelocSym;
2438 }
2439 reloc_found = true;
2440 break;
2441 }
2442 }
2443 // If there is an external relocation entry for a symbol in this section
2444 // at this section_offset then use that symbol's value for the n_value
2445 // and return its name.
2446 const char *SymbolName = nullptr;
2447 if (reloc_found && isExtern) {
Rafael Espindoladea00162015-07-03 17:44:18 +00002448 n_value = Symbol.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002449 ErrorOr<StringRef> NameOrError = Symbol.getName();
2450 if (std::error_code EC = NameOrError.getError())
2451 report_fatal_error(EC.message());
2452 StringRef Name = *NameOrError;
2453 if (!Name.empty()) {
2454 SymbolName = Name.data();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002455 return SymbolName;
2456 }
2457 }
2458
2459 // TODO: For fully linked images, look through the external relocation
2460 // entries off the dynamic symtab command. For these the r_offset is from the
2461 // start of the first writeable segment in the Mach-O file. So the offset
2462 // to this section from that segment is passed to this routine by the caller,
2463 // as the database_offset. Which is the difference of the section's starting
2464 // address and the first writable segment.
2465 //
2466 // NOTE: need add passing the database_offset to this routine.
2467
Kevin Enderby0fc11822015-04-01 20:57:01 +00002468 // We did not find an external relocation entry so look up the ReferenceValue
2469 // as an address of a symbol and if found return that symbol's name.
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +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) {
Rafael Espindoladea00162015-07-03 17:44:18 +00005629 ReferenceValue = Symbol.getValue();
Kevin Enderbybf246f52014-09-24 23:08:22 +00005630 }
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) {
Rafael Espindoladea00162015-07-03 17:44:18 +00006120 uint64_t Address = Symbol.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006121 ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
6122 if (std::error_code EC = SymNameOrErr.getError())
6123 report_fatal_error(EC.message());
6124 StringRef SymName = *SymNameOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006125 AddrMap[Address] = SymName;
Kevin Enderby6a221752015-03-17 17:10:57 +00006126 if (!DisSymName.empty() && DisSymName == SymName)
6127 DisSymNameFound = true;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006128 }
6129 }
David Blaikie33dd45d02015-03-23 18:39:02 +00006130 if (!DisSymName.empty() && !DisSymNameFound) {
Kevin Enderby6a221752015-03-17 17:10:57 +00006131 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6132 return;
6133 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006134 // Set up the block of info used by the Symbolizer call backs.
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006135 SymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006136 SymbolizerInfo.O = MachOOF;
6137 SymbolizerInfo.S = Sections[SectIdx];
Kevin Enderbybf246f52014-09-24 23:08:22 +00006138 SymbolizerInfo.AddrMap = &AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006139 SymbolizerInfo.Sections = &Sections;
6140 SymbolizerInfo.class_name = nullptr;
6141 SymbolizerInfo.selector_name = nullptr;
6142 SymbolizerInfo.method = nullptr;
Kevin Enderby04bf6932014-10-28 23:39:46 +00006143 SymbolizerInfo.demangled_name = nullptr;
Kevin Enderby078be602014-10-23 19:53:12 +00006144 SymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006145 SymbolizerInfo.adrp_addr = 0;
6146 SymbolizerInfo.adrp_inst = 0;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006147 // Same for the ThumbSymbolizer
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006148 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006149 ThumbSymbolizerInfo.O = MachOOF;
6150 ThumbSymbolizerInfo.S = Sections[SectIdx];
6151 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6152 ThumbSymbolizerInfo.Sections = &Sections;
6153 ThumbSymbolizerInfo.class_name = nullptr;
6154 ThumbSymbolizerInfo.selector_name = nullptr;
6155 ThumbSymbolizerInfo.method = nullptr;
6156 ThumbSymbolizerInfo.demangled_name = nullptr;
6157 ThumbSymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006158 ThumbSymbolizerInfo.adrp_addr = 0;
6159 ThumbSymbolizerInfo.adrp_inst = 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006160
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006161 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006162 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006163 ErrorOr<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
6164 if (std::error_code EC = SymNameOrErr.getError())
6165 report_fatal_error(EC.message());
6166 StringRef SymName = *SymNameOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006167
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006168 SymbolRef::Type ST = Symbols[SymIdx].getType();
Owen Andersond9243c42011-10-17 21:37:35 +00006169 if (ST != SymbolRef::ST_Function)
6170 continue;
6171
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006172 // Make sure the symbol is defined in this section.
Rafael Espindola80291272014-10-08 15:28:58 +00006173 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Owen Andersond9243c42011-10-17 21:37:35 +00006174 if (!containsSym)
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006175 continue;
6176
Kevin Enderby6a221752015-03-17 17:10:57 +00006177 // If we are only disassembling one symbol see if this is that symbol.
6178 if (!DisSymName.empty() && DisSymName != SymName)
6179 continue;
6180
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006181 // Start at the address of the symbol relative to the section's address.
Rafael Espindoladea00162015-07-03 17:44:18 +00006182 uint64_t Start = Symbols[SymIdx].getValue();
Rafael Espindola80291272014-10-08 15:28:58 +00006183 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006184 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006185
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006186 // Stop disassembling either at the beginning of the next symbol or at
6187 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +00006188 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +00006189 uint64_t NextSym = 0;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006190 uint64_t NextSymIdx = SymIdx + 1;
Owen Andersond9243c42011-10-17 21:37:35 +00006191 while (Symbols.size() > NextSymIdx) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006192 SymbolRef::Type NextSymType = Symbols[NextSymIdx].getType();
Owen Andersond9243c42011-10-17 21:37:35 +00006193 if (NextSymType == SymbolRef::ST_Function) {
Rafael Espindola80291272014-10-08 15:28:58 +00006194 containsNextSym =
6195 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Rafael Espindoladea00162015-07-03 17:44:18 +00006196 NextSym = Symbols[NextSymIdx].getValue();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006197 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006198 break;
6199 }
6200 ++NextSymIdx;
6201 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006202
Rafael Espindola80291272014-10-08 15:28:58 +00006203 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006204 uint64_t End = containsNextSym ? NextSym : SectSize;
Owen Andersond9243c42011-10-17 21:37:35 +00006205 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006206
6207 symbolTableWorked = true;
Rafael Espindolabd604f22014-11-07 00:52:15 +00006208
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006209 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6210 bool isThumb =
6211 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
6212
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006213 outs() << SymName << ":\n";
6214 DILineInfo lastLine;
6215 for (uint64_t Index = Start; Index < End; Index += Size) {
6216 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +00006217
Kevin Enderbybf246f52014-09-24 23:08:22 +00006218 uint64_t PC = SectAddress + Index;
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006219 if (!NoLeadingAddr) {
6220 if (FullLeadingAddr) {
6221 if (MachOOF->is64Bit())
6222 outs() << format("%016" PRIx64, PC);
6223 else
6224 outs() << format("%08" PRIx64, PC);
6225 } else {
6226 outs() << format("%8" PRIx64 ":", PC);
6227 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006228 }
6229 if (!NoShowRawInsn)
6230 outs() << "\t";
Kevin Enderby273ae012013-06-06 17:20:50 +00006231
6232 // Check the data in code table here to see if this is data not an
6233 // instruction to be disassembled.
6234 DiceTable Dice;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006235 Dice.push_back(std::make_pair(PC, DiceRef()));
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006236 dice_table_iterator DTI =
6237 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6238 compareDiceTableEntries);
6239 if (DTI != Dices.end()) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006240 uint16_t Length;
6241 DTI->second.getLength(Length);
Kevin Enderby273ae012013-06-06 17:20:50 +00006242 uint16_t Kind;
6243 DTI->second.getKind(Kind);
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00006244 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006245 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6246 (PC == (DTI->first + Length - 1)) && (Length & 1))
6247 Size++;
Kevin Enderby273ae012013-06-06 17:20:50 +00006248 continue;
6249 }
6250
Kevin Enderbybf246f52014-09-24 23:08:22 +00006251 SmallVector<char, 64> AnnotationsBytes;
6252 raw_svector_ostream Annotations(AnnotationsBytes);
6253
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006254 bool gotInst;
6255 if (isThumb)
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006256 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006257 PC, DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006258 else
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006259 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
Kevin Enderbybf246f52014-09-24 23:08:22 +00006260 DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006261 if (gotInst) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006262 if (!NoShowRawInsn) {
Colin LeMahieu2048ea42015-05-28 18:39:50 +00006263 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006264 }
6265 formatted_raw_ostream FormattedOS(outs());
6266 Annotations.flush();
6267 StringRef AnnotationsStr = Annotations.str();
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006268 if (isThumb)
Akira Hatanakab46d0232015-03-27 20:36:02 +00006269 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006270 else
Akira Hatanaka1d079942015-03-28 20:44:05 +00006271 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006272 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
Owen Andersond9243c42011-10-17 21:37:35 +00006273
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006274 // Print debug info.
6275 if (diContext) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006276 DILineInfo dli = diContext->getLineInfoForAddress(PC);
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006277 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +00006278 if (dli != lastLine && dli.Line != 0)
6279 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6280 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006281 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006282 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006283 outs() << "\n";
6284 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006285 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006286 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006287 outs() << format("\t.byte 0x%02x #bad opcode\n",
6288 *(Bytes.data() + Index) & 0xff);
6289 Size = 1; // skip exactly one illegible byte and move on.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006290 } else if (Arch == Triple::aarch64) {
6291 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6292 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6293 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6294 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6295 outs() << format("\t.long\t0x%08x\n", opcode);
6296 Size = 4;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006297 } else {
6298 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6299 if (Size == 0)
6300 Size = 1; // skip illegible bytes
6301 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006302 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006303 }
6304 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006305 if (!symbolTableWorked) {
Rafael Espindola80291272014-10-08 15:28:58 +00006306 // Reading the symbol table didn't work, disassemble the whole section.
6307 uint64_t SectAddress = Sections[SectIdx].getAddress();
6308 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbybadd1002012-05-18 00:13:56 +00006309 uint64_t InstSize;
6310 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +00006311 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +00006312
Kevin Enderbybf246f52014-09-24 23:08:22 +00006313 uint64_t PC = SectAddress + Index;
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006314 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6315 DebugOut, nulls())) {
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006316 if (!NoLeadingAddr) {
6317 if (FullLeadingAddr) {
6318 if (MachOOF->is64Bit())
6319 outs() << format("%016" PRIx64, PC);
6320 else
6321 outs() << format("%08" PRIx64, PC);
6322 } else {
6323 outs() << format("%8" PRIx64 ":", PC);
6324 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006325 }
6326 if (!NoShowRawInsn) {
6327 outs() << "\t";
Colin LeMahieu2048ea42015-05-28 18:39:50 +00006328 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006329 }
Akira Hatanaka1d079942015-03-28 20:44:05 +00006330 IP->printInst(&Inst, outs(), "", *STI);
Bill Wendling4e68e062012-07-19 00:17:40 +00006331 outs() << "\n";
6332 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006333 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006334 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006335 outs() << format("\t.byte 0x%02x #bad opcode\n",
6336 *(Bytes.data() + Index) & 0xff);
6337 InstSize = 1; // skip exactly one illegible byte and move on.
6338 } else {
6339 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6340 if (InstSize == 0)
6341 InstSize = 1; // skip illegible bytes
6342 }
Bill Wendling4e68e062012-07-19 00:17:40 +00006343 }
Kevin Enderbybadd1002012-05-18 00:13:56 +00006344 }
6345 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006346 // The TripleName's need to be reset if we are called again for a different
6347 // archtecture.
6348 TripleName = "";
6349 ThumbTripleName = "";
6350
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006351 if (SymbolizerInfo.method != nullptr)
6352 free(SymbolizerInfo.method);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006353 if (SymbolizerInfo.demangled_name != nullptr)
6354 free(SymbolizerInfo.demangled_name);
Kevin Enderby078be602014-10-23 19:53:12 +00006355 if (SymbolizerInfo.bindtable != nullptr)
6356 delete SymbolizerInfo.bindtable;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006357 if (ThumbSymbolizerInfo.method != nullptr)
6358 free(ThumbSymbolizerInfo.method);
6359 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6360 free(ThumbSymbolizerInfo.demangled_name);
6361 if (ThumbSymbolizerInfo.bindtable != nullptr)
6362 delete ThumbSymbolizerInfo.bindtable;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006363 }
6364}
Tim Northover4bd286a2014-08-01 13:07:19 +00006365
Tim Northover39c70bb2014-08-12 11:52:59 +00006366//===----------------------------------------------------------------------===//
6367// __compact_unwind section dumping
6368//===----------------------------------------------------------------------===//
6369
Tim Northover4bd286a2014-08-01 13:07:19 +00006370namespace {
Tim Northover39c70bb2014-08-12 11:52:59 +00006371
6372template <typename T> static uint64_t readNext(const char *&Buf) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006373 using llvm::support::little;
6374 using llvm::support::unaligned;
Tim Northover39c70bb2014-08-12 11:52:59 +00006375
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006376 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6377 Buf += sizeof(T);
6378 return Val;
6379}
Tim Northover39c70bb2014-08-12 11:52:59 +00006380
Tim Northover4bd286a2014-08-01 13:07:19 +00006381struct CompactUnwindEntry {
6382 uint32_t OffsetInSection;
6383
6384 uint64_t FunctionAddr;
6385 uint32_t Length;
6386 uint32_t CompactEncoding;
6387 uint64_t PersonalityAddr;
6388 uint64_t LSDAAddr;
6389
6390 RelocationRef FunctionReloc;
6391 RelocationRef PersonalityReloc;
6392 RelocationRef LSDAReloc;
6393
6394 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006395 : OffsetInSection(Offset) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006396 if (Is64)
6397 read<uint64_t>(Contents.data() + Offset);
6398 else
6399 read<uint32_t>(Contents.data() + Offset);
6400 }
6401
6402private:
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006403 template <typename UIntPtr> void read(const char *Buf) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006404 FunctionAddr = readNext<UIntPtr>(Buf);
6405 Length = readNext<uint32_t>(Buf);
6406 CompactEncoding = readNext<uint32_t>(Buf);
6407 PersonalityAddr = readNext<UIntPtr>(Buf);
6408 LSDAAddr = readNext<UIntPtr>(Buf);
6409 }
6410};
6411}
6412
6413/// Given a relocation from __compact_unwind, consisting of the RelocationRef
6414/// and data being relocated, determine the best base Name and Addend to use for
6415/// display purposes.
6416///
6417/// 1. An Extern relocation will directly reference a symbol (and the data is
6418/// then already an addend), so use that.
6419/// 2. Otherwise the data is an offset in the object file's layout; try to find
6420// a symbol before it in the same section, and use the offset from there.
6421/// 3. Finally, if all that fails, fall back to an offset from the start of the
6422/// referenced section.
6423static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6424 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006425 const RelocationRef &Reloc, uint64_t Addr,
Tim Northover4bd286a2014-08-01 13:07:19 +00006426 StringRef &Name, uint64_t &Addend) {
6427 if (Reloc.getSymbol() != Obj->symbol_end()) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006428 ErrorOr<StringRef> NameOrErr = Reloc.getSymbol()->getName();
6429 if (std::error_code EC = NameOrErr.getError())
6430 report_fatal_error(EC.message());
6431 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00006432 Addend = Addr;
6433 return;
6434 }
6435
6436 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
Keno Fischerc780e8e2015-05-21 21:24:32 +00006437 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
Tim Northover4bd286a2014-08-01 13:07:19 +00006438
Rafael Espindola80291272014-10-08 15:28:58 +00006439 uint64_t SectionAddr = RelocSection.getAddress();
Tim Northover4bd286a2014-08-01 13:07:19 +00006440
6441 auto Sym = Symbols.upper_bound(Addr);
6442 if (Sym == Symbols.begin()) {
6443 // The first symbol in the object is after this reference, the best we can
6444 // do is section-relative notation.
6445 RelocSection.getName(Name);
6446 Addend = Addr - SectionAddr;
6447 return;
6448 }
6449
6450 // Go back one so that SymbolAddress <= Addr.
6451 --Sym;
6452
6453 section_iterator SymSection = Obj->section_end();
6454 Sym->second.getSection(SymSection);
6455 if (RelocSection == *SymSection) {
6456 // There's a valid symbol in the same section before this reference.
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006457 ErrorOr<StringRef> NameOrErr = Sym->second.getName();
6458 if (std::error_code EC = NameOrErr.getError())
6459 report_fatal_error(EC.message());
6460 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00006461 Addend = Addr - Sym->first;
6462 return;
6463 }
6464
6465 // There is a symbol before this reference, but it's in a different
6466 // section. Probably not helpful to mention it, so use the section name.
6467 RelocSection.getName(Name);
6468 Addend = Addr - SectionAddr;
6469}
6470
6471static void printUnwindRelocDest(const MachOObjectFile *Obj,
6472 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006473 const RelocationRef &Reloc, uint64_t Addr) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006474 StringRef Name;
6475 uint64_t Addend;
6476
Rafael Espindola854038e2015-06-26 14:51:16 +00006477 if (!Reloc.getObject())
Tim Northover0b0add52014-09-09 10:45:06 +00006478 return;
6479
Tim Northover4bd286a2014-08-01 13:07:19 +00006480 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6481
6482 outs() << Name;
6483 if (Addend)
Tim Northover63a25622014-08-11 09:14:06 +00006484 outs() << " + " << format("0x%" PRIx64, Addend);
Tim Northover4bd286a2014-08-01 13:07:19 +00006485}
6486
6487static void
6488printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6489 std::map<uint64_t, SymbolRef> &Symbols,
6490 const SectionRef &CompactUnwind) {
6491
6492 assert(Obj->isLittleEndian() &&
6493 "There should not be a big-endian .o with __compact_unwind");
6494
6495 bool Is64 = Obj->is64Bit();
6496 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6497 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6498
6499 StringRef Contents;
6500 CompactUnwind.getContents(Contents);
6501
6502 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6503
6504 // First populate the initial raw offsets, encodings and so on from the entry.
6505 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6506 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
6507 CompactUnwinds.push_back(Entry);
6508 }
6509
6510 // Next we need to look at the relocations to find out what objects are
6511 // actually being referred to.
6512 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00006513 uint64_t RelocAddress = Reloc.getOffset();
Tim Northover4bd286a2014-08-01 13:07:19 +00006514
6515 uint32_t EntryIdx = RelocAddress / EntrySize;
6516 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
6517 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
6518
6519 if (OffsetInEntry == 0)
6520 Entry.FunctionReloc = Reloc;
6521 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
6522 Entry.PersonalityReloc = Reloc;
6523 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
6524 Entry.LSDAReloc = Reloc;
6525 else
6526 llvm_unreachable("Unexpected relocation in __compact_unwind section");
6527 }
6528
6529 // Finally, we're ready to print the data we've gathered.
6530 outs() << "Contents of __compact_unwind section:\n";
6531 for (auto &Entry : CompactUnwinds) {
Tim Northover06af2602014-08-08 12:08:51 +00006532 outs() << " Entry at offset "
6533 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00006534
6535 // 1. Start of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006536 outs() << " start: " << format("0x%" PRIx64,
6537 Entry.FunctionAddr) << ' ';
6538 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
Tim Northover4bd286a2014-08-01 13:07:19 +00006539 outs() << '\n';
6540
6541 // 2. Length of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006542 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
6543 << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00006544 // 3. The 32-bit compact encoding.
6545 outs() << " compact encoding: "
Tim Northoverb911bf82014-08-08 12:00:09 +00006546 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00006547
6548 // 4. The personality function, if present.
Rafael Espindola854038e2015-06-26 14:51:16 +00006549 if (Entry.PersonalityReloc.getObject()) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006550 outs() << " personality function: "
Tim Northoverb911bf82014-08-08 12:00:09 +00006551 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00006552 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
6553 Entry.PersonalityAddr);
6554 outs() << '\n';
6555 }
6556
6557 // 5. This entry's language-specific data area.
Rafael Espindola854038e2015-06-26 14:51:16 +00006558 if (Entry.LSDAReloc.getObject()) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006559 outs() << " LSDA: " << format("0x%" PRIx64,
6560 Entry.LSDAAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00006561 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
6562 outs() << '\n';
6563 }
6564 }
6565}
6566
Tim Northover39c70bb2014-08-12 11:52:59 +00006567//===----------------------------------------------------------------------===//
6568// __unwind_info section dumping
6569//===----------------------------------------------------------------------===//
6570
6571static void printRegularSecondLevelUnwindPage(const char *PageStart) {
6572 const char *Pos = PageStart;
6573 uint32_t Kind = readNext<uint32_t>(Pos);
6574 (void)Kind;
6575 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
6576
6577 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6578 uint16_t NumEntries = readNext<uint16_t>(Pos);
6579
6580 Pos = PageStart + EntriesStart;
6581 for (unsigned i = 0; i < NumEntries; ++i) {
6582 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6583 uint32_t Encoding = readNext<uint32_t>(Pos);
6584
6585 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006586 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6587 << ", "
6588 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006589 }
6590}
6591
6592static void printCompressedSecondLevelUnwindPage(
6593 const char *PageStart, uint32_t FunctionBase,
6594 const SmallVectorImpl<uint32_t> &CommonEncodings) {
6595 const char *Pos = PageStart;
6596 uint32_t Kind = readNext<uint32_t>(Pos);
6597 (void)Kind;
6598 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
6599
6600 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6601 uint16_t NumEntries = readNext<uint16_t>(Pos);
6602
6603 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
6604 readNext<uint16_t>(Pos);
Aaron Ballman80930af2014-08-14 13:53:19 +00006605 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
6606 PageStart + EncodingsStart);
Tim Northover39c70bb2014-08-12 11:52:59 +00006607
6608 Pos = PageStart + EntriesStart;
6609 for (unsigned i = 0; i < NumEntries; ++i) {
6610 uint32_t Entry = readNext<uint32_t>(Pos);
6611 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
6612 uint32_t EncodingIdx = Entry >> 24;
6613
6614 uint32_t Encoding;
6615 if (EncodingIdx < CommonEncodings.size())
6616 Encoding = CommonEncodings[EncodingIdx];
6617 else
6618 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
6619
6620 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006621 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6622 << ", "
6623 << "encoding[" << EncodingIdx
6624 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006625 }
6626}
6627
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006628static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
6629 std::map<uint64_t, SymbolRef> &Symbols,
6630 const SectionRef &UnwindInfo) {
Tim Northover39c70bb2014-08-12 11:52:59 +00006631
6632 assert(Obj->isLittleEndian() &&
6633 "There should not be a big-endian .o with __unwind_info");
6634
6635 outs() << "Contents of __unwind_info section:\n";
6636
6637 StringRef Contents;
6638 UnwindInfo.getContents(Contents);
6639 const char *Pos = Contents.data();
6640
6641 //===----------------------------------
6642 // Section header
6643 //===----------------------------------
6644
6645 uint32_t Version = readNext<uint32_t>(Pos);
6646 outs() << " Version: "
6647 << format("0x%" PRIx32, Version) << '\n';
6648 assert(Version == 1 && "only understand version 1");
6649
6650 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
6651 outs() << " Common encodings array section offset: "
6652 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
6653 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
6654 outs() << " Number of common encodings in array: "
6655 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
6656
6657 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
6658 outs() << " Personality function array section offset: "
6659 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
6660 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
6661 outs() << " Number of personality functions in array: "
6662 << format("0x%" PRIx32, NumPersonalities) << '\n';
6663
6664 uint32_t IndicesStart = readNext<uint32_t>(Pos);
6665 outs() << " Index array section offset: "
6666 << format("0x%" PRIx32, IndicesStart) << '\n';
6667 uint32_t NumIndices = readNext<uint32_t>(Pos);
6668 outs() << " Number of indices in array: "
6669 << format("0x%" PRIx32, NumIndices) << '\n';
6670
6671 //===----------------------------------
6672 // A shared list of common encodings
6673 //===----------------------------------
6674
6675 // These occupy indices in the range [0, N] whenever an encoding is referenced
6676 // from a compressed 2nd level index table. In practice the linker only
6677 // creates ~128 of these, so that indices are available to embed encodings in
6678 // the 2nd level index.
6679
6680 SmallVector<uint32_t, 64> CommonEncodings;
6681 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
6682 Pos = Contents.data() + CommonEncodingsStart;
6683 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
6684 uint32_t Encoding = readNext<uint32_t>(Pos);
6685 CommonEncodings.push_back(Encoding);
6686
6687 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
6688 << '\n';
6689 }
6690
Tim Northover39c70bb2014-08-12 11:52:59 +00006691 //===----------------------------------
6692 // Personality functions used in this executable
6693 //===----------------------------------
6694
6695 // There should be only a handful of these (one per source language,
6696 // roughly). Particularly since they only get 2 bits in the compact encoding.
6697
6698 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
6699 Pos = Contents.data() + PersonalitiesStart;
6700 for (unsigned i = 0; i < NumPersonalities; ++i) {
6701 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
6702 outs() << " personality[" << i + 1
6703 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
6704 }
6705
6706 //===----------------------------------
6707 // The level 1 index entries
6708 //===----------------------------------
6709
6710 // These specify an approximate place to start searching for the more detailed
6711 // information, sorted by PC.
6712
6713 struct IndexEntry {
6714 uint32_t FunctionOffset;
6715 uint32_t SecondLevelPageStart;
6716 uint32_t LSDAStart;
6717 };
6718
6719 SmallVector<IndexEntry, 4> IndexEntries;
6720
6721 outs() << " Top level indices: (count = " << NumIndices << ")\n";
6722 Pos = Contents.data() + IndicesStart;
6723 for (unsigned i = 0; i < NumIndices; ++i) {
6724 IndexEntry Entry;
6725
6726 Entry.FunctionOffset = readNext<uint32_t>(Pos);
6727 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
6728 Entry.LSDAStart = readNext<uint32_t>(Pos);
6729 IndexEntries.push_back(Entry);
6730
6731 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006732 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
6733 << ", "
Tim Northover39c70bb2014-08-12 11:52:59 +00006734 << "2nd level page offset="
6735 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006736 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006737 }
6738
Tim Northover39c70bb2014-08-12 11:52:59 +00006739 //===----------------------------------
6740 // Next come the LSDA tables
6741 //===----------------------------------
6742
6743 // The LSDA layout is rather implicit: it's a contiguous array of entries from
6744 // the first top-level index's LSDAOffset to the last (sentinel).
6745
6746 outs() << " LSDA descriptors:\n";
6747 Pos = Contents.data() + IndexEntries[0].LSDAStart;
6748 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
6749 (2 * sizeof(uint32_t));
6750 for (int i = 0; i < NumLSDAs; ++i) {
6751 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6752 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
6753 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006754 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6755 << ", "
6756 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006757 }
6758
6759 //===----------------------------------
6760 // Finally, the 2nd level indices
6761 //===----------------------------------
6762
6763 // Generally these are 4K in size, and have 2 possible forms:
6764 // + Regular stores up to 511 entries with disparate encodings
6765 // + Compressed stores up to 1021 entries if few enough compact encoding
6766 // values are used.
6767 outs() << " Second level indices:\n";
6768 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
6769 // The final sentinel top-level index has no associated 2nd level page
6770 if (IndexEntries[i].SecondLevelPageStart == 0)
6771 break;
6772
6773 outs() << " Second level index[" << i << "]: "
6774 << "offset in section="
6775 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
6776 << ", "
6777 << "base function offset="
6778 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
6779
6780 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
Aaron Ballman80930af2014-08-14 13:53:19 +00006781 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00006782 if (Kind == 2)
6783 printRegularSecondLevelUnwindPage(Pos);
6784 else if (Kind == 3)
6785 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
6786 CommonEncodings);
6787 else
6788 llvm_unreachable("Do not know how to print this kind of 2nd level page");
Tim Northover39c70bb2014-08-12 11:52:59 +00006789 }
6790}
6791
Tim Northover4bd286a2014-08-01 13:07:19 +00006792void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
6793 std::map<uint64_t, SymbolRef> Symbols;
6794 for (const SymbolRef &SymRef : Obj->symbols()) {
6795 // Discard any undefined or absolute symbols. They're not going to take part
6796 // in the convenience lookup for unwind info and just take up resources.
6797 section_iterator Section = Obj->section_end();
6798 SymRef.getSection(Section);
6799 if (Section == Obj->section_end())
6800 continue;
6801
Rafael Espindoladea00162015-07-03 17:44:18 +00006802 uint64_t Addr = SymRef.getValue();
Tim Northover4bd286a2014-08-01 13:07:19 +00006803 Symbols.insert(std::make_pair(Addr, SymRef));
6804 }
6805
6806 for (const SectionRef &Section : Obj->sections()) {
6807 StringRef SectName;
6808 Section.getName(SectName);
6809 if (SectName == "__compact_unwind")
6810 printMachOCompactUnwindSection(Obj, Symbols, Section);
6811 else if (SectName == "__unwind_info")
Tim Northover39c70bb2014-08-12 11:52:59 +00006812 printMachOUnwindInfoSection(Obj, Symbols, Section);
Tim Northover4bd286a2014-08-01 13:07:19 +00006813 else if (SectName == "__eh_frame")
6814 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00006815 }
6816}
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006817
6818static void PrintMachHeader(uint32_t magic, uint32_t cputype,
6819 uint32_t cpusubtype, uint32_t filetype,
6820 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
6821 bool verbose) {
6822 outs() << "Mach header\n";
6823 outs() << " magic cputype cpusubtype caps filetype ncmds "
6824 "sizeofcmds flags\n";
6825 if (verbose) {
6826 if (magic == MachO::MH_MAGIC)
6827 outs() << " MH_MAGIC";
6828 else if (magic == MachO::MH_MAGIC_64)
6829 outs() << "MH_MAGIC_64";
6830 else
6831 outs() << format(" 0x%08" PRIx32, magic);
6832 switch (cputype) {
6833 case MachO::CPU_TYPE_I386:
6834 outs() << " I386";
6835 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6836 case MachO::CPU_SUBTYPE_I386_ALL:
6837 outs() << " ALL";
6838 break;
6839 default:
6840 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6841 break;
6842 }
6843 break;
6844 case MachO::CPU_TYPE_X86_64:
6845 outs() << " X86_64";
Kevin Enderby131d1772015-01-09 19:22:37 +00006846 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6847 case MachO::CPU_SUBTYPE_X86_64_ALL:
6848 outs() << " ALL";
6849 break;
6850 case MachO::CPU_SUBTYPE_X86_64_H:
6851 outs() << " Haswell";
6852 break;
6853 default:
6854 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6855 break;
6856 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006857 break;
6858 case MachO::CPU_TYPE_ARM:
6859 outs() << " ARM";
6860 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6861 case MachO::CPU_SUBTYPE_ARM_ALL:
6862 outs() << " ALL";
6863 break;
6864 case MachO::CPU_SUBTYPE_ARM_V4T:
6865 outs() << " V4T";
6866 break;
6867 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6868 outs() << " V5TEJ";
6869 break;
6870 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6871 outs() << " XSCALE";
6872 break;
6873 case MachO::CPU_SUBTYPE_ARM_V6:
6874 outs() << " V6";
6875 break;
6876 case MachO::CPU_SUBTYPE_ARM_V6M:
6877 outs() << " V6M";
6878 break;
6879 case MachO::CPU_SUBTYPE_ARM_V7:
6880 outs() << " V7";
6881 break;
6882 case MachO::CPU_SUBTYPE_ARM_V7EM:
6883 outs() << " V7EM";
6884 break;
6885 case MachO::CPU_SUBTYPE_ARM_V7K:
6886 outs() << " V7K";
6887 break;
6888 case MachO::CPU_SUBTYPE_ARM_V7M:
6889 outs() << " V7M";
6890 break;
6891 case MachO::CPU_SUBTYPE_ARM_V7S:
6892 outs() << " V7S";
6893 break;
6894 default:
6895 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6896 break;
6897 }
6898 break;
6899 case MachO::CPU_TYPE_ARM64:
6900 outs() << " ARM64";
6901 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6902 case MachO::CPU_SUBTYPE_ARM64_ALL:
6903 outs() << " ALL";
6904 break;
6905 default:
6906 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6907 break;
6908 }
6909 break;
6910 case MachO::CPU_TYPE_POWERPC:
6911 outs() << " PPC";
6912 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6913 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6914 outs() << " ALL";
6915 break;
6916 default:
6917 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6918 break;
6919 }
6920 break;
6921 case MachO::CPU_TYPE_POWERPC64:
6922 outs() << " PPC64";
6923 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6924 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6925 outs() << " ALL";
6926 break;
6927 default:
6928 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6929 break;
6930 }
6931 break;
6932 }
6933 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00006934 outs() << " LIB64";
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006935 } else {
6936 outs() << format(" 0x%02" PRIx32,
6937 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6938 }
6939 switch (filetype) {
6940 case MachO::MH_OBJECT:
6941 outs() << " OBJECT";
6942 break;
6943 case MachO::MH_EXECUTE:
6944 outs() << " EXECUTE";
6945 break;
6946 case MachO::MH_FVMLIB:
6947 outs() << " FVMLIB";
6948 break;
6949 case MachO::MH_CORE:
6950 outs() << " CORE";
6951 break;
6952 case MachO::MH_PRELOAD:
6953 outs() << " PRELOAD";
6954 break;
6955 case MachO::MH_DYLIB:
6956 outs() << " DYLIB";
6957 break;
6958 case MachO::MH_DYLIB_STUB:
6959 outs() << " DYLIB_STUB";
6960 break;
6961 case MachO::MH_DYLINKER:
6962 outs() << " DYLINKER";
6963 break;
6964 case MachO::MH_BUNDLE:
6965 outs() << " BUNDLE";
6966 break;
6967 case MachO::MH_DSYM:
6968 outs() << " DSYM";
6969 break;
6970 case MachO::MH_KEXT_BUNDLE:
6971 outs() << " KEXTBUNDLE";
6972 break;
6973 default:
6974 outs() << format(" %10u", filetype);
6975 break;
6976 }
6977 outs() << format(" %5u", ncmds);
6978 outs() << format(" %10u", sizeofcmds);
6979 uint32_t f = flags;
6980 if (f & MachO::MH_NOUNDEFS) {
6981 outs() << " NOUNDEFS";
6982 f &= ~MachO::MH_NOUNDEFS;
6983 }
6984 if (f & MachO::MH_INCRLINK) {
6985 outs() << " INCRLINK";
6986 f &= ~MachO::MH_INCRLINK;
6987 }
6988 if (f & MachO::MH_DYLDLINK) {
6989 outs() << " DYLDLINK";
6990 f &= ~MachO::MH_DYLDLINK;
6991 }
6992 if (f & MachO::MH_BINDATLOAD) {
6993 outs() << " BINDATLOAD";
6994 f &= ~MachO::MH_BINDATLOAD;
6995 }
6996 if (f & MachO::MH_PREBOUND) {
6997 outs() << " PREBOUND";
6998 f &= ~MachO::MH_PREBOUND;
6999 }
7000 if (f & MachO::MH_SPLIT_SEGS) {
7001 outs() << " SPLIT_SEGS";
7002 f &= ~MachO::MH_SPLIT_SEGS;
7003 }
7004 if (f & MachO::MH_LAZY_INIT) {
7005 outs() << " LAZY_INIT";
7006 f &= ~MachO::MH_LAZY_INIT;
7007 }
7008 if (f & MachO::MH_TWOLEVEL) {
7009 outs() << " TWOLEVEL";
7010 f &= ~MachO::MH_TWOLEVEL;
7011 }
7012 if (f & MachO::MH_FORCE_FLAT) {
7013 outs() << " FORCE_FLAT";
7014 f &= ~MachO::MH_FORCE_FLAT;
7015 }
7016 if (f & MachO::MH_NOMULTIDEFS) {
7017 outs() << " NOMULTIDEFS";
7018 f &= ~MachO::MH_NOMULTIDEFS;
7019 }
7020 if (f & MachO::MH_NOFIXPREBINDING) {
7021 outs() << " NOFIXPREBINDING";
7022 f &= ~MachO::MH_NOFIXPREBINDING;
7023 }
7024 if (f & MachO::MH_PREBINDABLE) {
7025 outs() << " PREBINDABLE";
7026 f &= ~MachO::MH_PREBINDABLE;
7027 }
7028 if (f & MachO::MH_ALLMODSBOUND) {
7029 outs() << " ALLMODSBOUND";
7030 f &= ~MachO::MH_ALLMODSBOUND;
7031 }
7032 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7033 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7034 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7035 }
7036 if (f & MachO::MH_CANONICAL) {
7037 outs() << " CANONICAL";
7038 f &= ~MachO::MH_CANONICAL;
7039 }
7040 if (f & MachO::MH_WEAK_DEFINES) {
7041 outs() << " WEAK_DEFINES";
7042 f &= ~MachO::MH_WEAK_DEFINES;
7043 }
7044 if (f & MachO::MH_BINDS_TO_WEAK) {
7045 outs() << " BINDS_TO_WEAK";
7046 f &= ~MachO::MH_BINDS_TO_WEAK;
7047 }
7048 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7049 outs() << " ALLOW_STACK_EXECUTION";
7050 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7051 }
7052 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7053 outs() << " DEAD_STRIPPABLE_DYLIB";
7054 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7055 }
7056 if (f & MachO::MH_PIE) {
7057 outs() << " PIE";
7058 f &= ~MachO::MH_PIE;
7059 }
7060 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7061 outs() << " NO_REEXPORTED_DYLIBS";
7062 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7063 }
7064 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7065 outs() << " MH_HAS_TLV_DESCRIPTORS";
7066 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7067 }
7068 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7069 outs() << " MH_NO_HEAP_EXECUTION";
7070 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7071 }
7072 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7073 outs() << " APP_EXTENSION_SAFE";
7074 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7075 }
7076 if (f != 0 || flags == 0)
7077 outs() << format(" 0x%08" PRIx32, f);
7078 } else {
7079 outs() << format(" 0x%08" PRIx32, magic);
7080 outs() << format(" %7d", cputype);
7081 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7082 outs() << format(" 0x%02" PRIx32,
7083 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7084 outs() << format(" %10u", filetype);
7085 outs() << format(" %5u", ncmds);
7086 outs() << format(" %10u", sizeofcmds);
7087 outs() << format(" 0x%08" PRIx32, flags);
7088 }
7089 outs() << "\n";
7090}
7091
Kevin Enderby956366c2014-08-29 22:30:52 +00007092static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7093 StringRef SegName, uint64_t vmaddr,
7094 uint64_t vmsize, uint64_t fileoff,
7095 uint64_t filesize, uint32_t maxprot,
7096 uint32_t initprot, uint32_t nsects,
7097 uint32_t flags, uint32_t object_size,
7098 bool verbose) {
7099 uint64_t expected_cmdsize;
7100 if (cmd == MachO::LC_SEGMENT) {
7101 outs() << " cmd LC_SEGMENT\n";
7102 expected_cmdsize = nsects;
7103 expected_cmdsize *= sizeof(struct MachO::section);
7104 expected_cmdsize += sizeof(struct MachO::segment_command);
7105 } else {
7106 outs() << " cmd LC_SEGMENT_64\n";
7107 expected_cmdsize = nsects;
7108 expected_cmdsize *= sizeof(struct MachO::section_64);
7109 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7110 }
7111 outs() << " cmdsize " << cmdsize;
7112 if (cmdsize != expected_cmdsize)
7113 outs() << " Inconsistent size\n";
7114 else
7115 outs() << "\n";
7116 outs() << " segname " << SegName << "\n";
7117 if (cmd == MachO::LC_SEGMENT_64) {
7118 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7119 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7120 } else {
Kevin Enderbyadb7c432014-12-16 18:58:11 +00007121 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7122 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007123 }
7124 outs() << " fileoff " << fileoff;
7125 if (fileoff > object_size)
7126 outs() << " (past end of file)\n";
7127 else
7128 outs() << "\n";
7129 outs() << " filesize " << filesize;
7130 if (fileoff + filesize > object_size)
7131 outs() << " (past end of file)\n";
7132 else
7133 outs() << "\n";
7134 if (verbose) {
7135 if ((maxprot &
7136 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7137 MachO::VM_PROT_EXECUTE)) != 0)
7138 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7139 else {
7140 if (maxprot & MachO::VM_PROT_READ)
7141 outs() << " maxprot r";
7142 else
7143 outs() << " maxprot -";
7144 if (maxprot & MachO::VM_PROT_WRITE)
7145 outs() << "w";
7146 else
7147 outs() << "-";
7148 if (maxprot & MachO::VM_PROT_EXECUTE)
7149 outs() << "x\n";
7150 else
7151 outs() << "-\n";
7152 }
7153 if ((initprot &
7154 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7155 MachO::VM_PROT_EXECUTE)) != 0)
7156 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7157 else {
7158 if (initprot & MachO::VM_PROT_READ)
7159 outs() << " initprot r";
7160 else
7161 outs() << " initprot -";
7162 if (initprot & MachO::VM_PROT_WRITE)
7163 outs() << "w";
7164 else
7165 outs() << "-";
7166 if (initprot & MachO::VM_PROT_EXECUTE)
7167 outs() << "x\n";
7168 else
7169 outs() << "-\n";
7170 }
7171 } else {
7172 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7173 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7174 }
7175 outs() << " nsects " << nsects << "\n";
7176 if (verbose) {
7177 outs() << " flags";
7178 if (flags == 0)
7179 outs() << " (none)\n";
7180 else {
7181 if (flags & MachO::SG_HIGHVM) {
7182 outs() << " HIGHVM";
7183 flags &= ~MachO::SG_HIGHVM;
7184 }
7185 if (flags & MachO::SG_FVMLIB) {
7186 outs() << " FVMLIB";
7187 flags &= ~MachO::SG_FVMLIB;
7188 }
7189 if (flags & MachO::SG_NORELOC) {
7190 outs() << " NORELOC";
7191 flags &= ~MachO::SG_NORELOC;
7192 }
7193 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7194 outs() << " PROTECTED_VERSION_1";
7195 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7196 }
7197 if (flags)
7198 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7199 else
7200 outs() << "\n";
7201 }
7202 } else {
7203 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7204 }
7205}
7206
7207static void PrintSection(const char *sectname, const char *segname,
7208 uint64_t addr, uint64_t size, uint32_t offset,
7209 uint32_t align, uint32_t reloff, uint32_t nreloc,
7210 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7211 uint32_t cmd, const char *sg_segname,
7212 uint32_t filetype, uint32_t object_size,
7213 bool verbose) {
7214 outs() << "Section\n";
7215 outs() << " sectname " << format("%.16s\n", sectname);
7216 outs() << " segname " << format("%.16s", segname);
7217 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7218 outs() << " (does not match segment)\n";
7219 else
7220 outs() << "\n";
7221 if (cmd == MachO::LC_SEGMENT_64) {
7222 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7223 outs() << " size " << format("0x%016" PRIx64, size);
7224 } else {
Kevin Enderby75594b62014-12-16 21:00:25 +00007225 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7226 outs() << " size " << format("0x%08" PRIx64, size);
Kevin Enderby956366c2014-08-29 22:30:52 +00007227 }
7228 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7229 outs() << " (past end of file)\n";
7230 else
7231 outs() << "\n";
7232 outs() << " offset " << offset;
7233 if (offset > object_size)
7234 outs() << " (past end of file)\n";
7235 else
7236 outs() << "\n";
7237 uint32_t align_shifted = 1 << align;
7238 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7239 outs() << " reloff " << reloff;
7240 if (reloff > object_size)
7241 outs() << " (past end of file)\n";
7242 else
7243 outs() << "\n";
7244 outs() << " nreloc " << nreloc;
7245 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7246 outs() << " (past end of file)\n";
7247 else
7248 outs() << "\n";
7249 uint32_t section_type = flags & MachO::SECTION_TYPE;
7250 if (verbose) {
7251 outs() << " type";
7252 if (section_type == MachO::S_REGULAR)
7253 outs() << " S_REGULAR\n";
7254 else if (section_type == MachO::S_ZEROFILL)
7255 outs() << " S_ZEROFILL\n";
7256 else if (section_type == MachO::S_CSTRING_LITERALS)
7257 outs() << " S_CSTRING_LITERALS\n";
7258 else if (section_type == MachO::S_4BYTE_LITERALS)
7259 outs() << " S_4BYTE_LITERALS\n";
7260 else if (section_type == MachO::S_8BYTE_LITERALS)
7261 outs() << " S_8BYTE_LITERALS\n";
7262 else if (section_type == MachO::S_16BYTE_LITERALS)
7263 outs() << " S_16BYTE_LITERALS\n";
7264 else if (section_type == MachO::S_LITERAL_POINTERS)
7265 outs() << " S_LITERAL_POINTERS\n";
7266 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7267 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7268 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7269 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7270 else if (section_type == MachO::S_SYMBOL_STUBS)
7271 outs() << " S_SYMBOL_STUBS\n";
7272 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7273 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7274 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7275 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7276 else if (section_type == MachO::S_COALESCED)
7277 outs() << " S_COALESCED\n";
7278 else if (section_type == MachO::S_INTERPOSING)
7279 outs() << " S_INTERPOSING\n";
7280 else if (section_type == MachO::S_DTRACE_DOF)
7281 outs() << " S_DTRACE_DOF\n";
7282 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7283 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7284 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7285 outs() << " S_THREAD_LOCAL_REGULAR\n";
7286 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7287 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7288 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7289 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7290 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7291 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7292 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7293 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7294 else
7295 outs() << format("0x%08" PRIx32, section_type) << "\n";
7296 outs() << "attributes";
7297 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7298 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7299 outs() << " PURE_INSTRUCTIONS";
7300 if (section_attributes & MachO::S_ATTR_NO_TOC)
7301 outs() << " NO_TOC";
7302 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7303 outs() << " STRIP_STATIC_SYMS";
7304 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7305 outs() << " NO_DEAD_STRIP";
7306 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7307 outs() << " LIVE_SUPPORT";
7308 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7309 outs() << " SELF_MODIFYING_CODE";
7310 if (section_attributes & MachO::S_ATTR_DEBUG)
7311 outs() << " DEBUG";
7312 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7313 outs() << " SOME_INSTRUCTIONS";
7314 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7315 outs() << " EXT_RELOC";
7316 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7317 outs() << " LOC_RELOC";
7318 if (section_attributes == 0)
7319 outs() << " (none)";
7320 outs() << "\n";
7321 } else
7322 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7323 outs() << " reserved1 " << reserved1;
7324 if (section_type == MachO::S_SYMBOL_STUBS ||
7325 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7326 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7327 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7328 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7329 outs() << " (index into indirect symbol table)\n";
7330 else
7331 outs() << "\n";
7332 outs() << " reserved2 " << reserved2;
7333 if (section_type == MachO::S_SYMBOL_STUBS)
7334 outs() << " (size of stubs)\n";
7335 else
7336 outs() << "\n";
7337}
7338
David Majnemer73cc6ff2014-11-13 19:48:56 +00007339static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
Kevin Enderby956366c2014-08-29 22:30:52 +00007340 uint32_t object_size) {
7341 outs() << " cmd LC_SYMTAB\n";
7342 outs() << " cmdsize " << st.cmdsize;
7343 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7344 outs() << " Incorrect size\n";
7345 else
7346 outs() << "\n";
7347 outs() << " symoff " << st.symoff;
7348 if (st.symoff > object_size)
7349 outs() << " (past end of file)\n";
7350 else
7351 outs() << "\n";
7352 outs() << " nsyms " << st.nsyms;
7353 uint64_t big_size;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007354 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007355 big_size = st.nsyms;
7356 big_size *= sizeof(struct MachO::nlist_64);
7357 big_size += st.symoff;
7358 if (big_size > object_size)
7359 outs() << " (past end of file)\n";
7360 else
7361 outs() << "\n";
7362 } else {
7363 big_size = st.nsyms;
7364 big_size *= sizeof(struct MachO::nlist);
7365 big_size += st.symoff;
7366 if (big_size > object_size)
7367 outs() << " (past end of file)\n";
7368 else
7369 outs() << "\n";
7370 }
7371 outs() << " stroff " << st.stroff;
7372 if (st.stroff > object_size)
7373 outs() << " (past end of file)\n";
7374 else
7375 outs() << "\n";
7376 outs() << " strsize " << st.strsize;
7377 big_size = st.stroff;
7378 big_size += st.strsize;
7379 if (big_size > object_size)
7380 outs() << " (past end of file)\n";
7381 else
7382 outs() << "\n";
7383}
7384
7385static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7386 uint32_t nsyms, uint32_t object_size,
David Majnemer73cc6ff2014-11-13 19:48:56 +00007387 bool Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007388 outs() << " cmd LC_DYSYMTAB\n";
7389 outs() << " cmdsize " << dyst.cmdsize;
7390 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7391 outs() << " Incorrect size\n";
7392 else
7393 outs() << "\n";
7394 outs() << " ilocalsym " << dyst.ilocalsym;
7395 if (dyst.ilocalsym > nsyms)
7396 outs() << " (greater than the number of symbols)\n";
7397 else
7398 outs() << "\n";
7399 outs() << " nlocalsym " << dyst.nlocalsym;
7400 uint64_t big_size;
7401 big_size = dyst.ilocalsym;
7402 big_size += dyst.nlocalsym;
7403 if (big_size > nsyms)
7404 outs() << " (past the end of the symbol table)\n";
7405 else
7406 outs() << "\n";
7407 outs() << " iextdefsym " << dyst.iextdefsym;
7408 if (dyst.iextdefsym > nsyms)
7409 outs() << " (greater than the number of symbols)\n";
7410 else
7411 outs() << "\n";
7412 outs() << " nextdefsym " << dyst.nextdefsym;
7413 big_size = dyst.iextdefsym;
7414 big_size += dyst.nextdefsym;
7415 if (big_size > nsyms)
7416 outs() << " (past the end of the symbol table)\n";
7417 else
7418 outs() << "\n";
7419 outs() << " iundefsym " << dyst.iundefsym;
7420 if (dyst.iundefsym > nsyms)
7421 outs() << " (greater than the number of symbols)\n";
7422 else
7423 outs() << "\n";
7424 outs() << " nundefsym " << dyst.nundefsym;
7425 big_size = dyst.iundefsym;
7426 big_size += dyst.nundefsym;
7427 if (big_size > nsyms)
7428 outs() << " (past the end of the symbol table)\n";
7429 else
7430 outs() << "\n";
7431 outs() << " tocoff " << dyst.tocoff;
7432 if (dyst.tocoff > object_size)
7433 outs() << " (past end of file)\n";
7434 else
7435 outs() << "\n";
7436 outs() << " ntoc " << dyst.ntoc;
7437 big_size = dyst.ntoc;
7438 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7439 big_size += dyst.tocoff;
7440 if (big_size > object_size)
7441 outs() << " (past end of file)\n";
7442 else
7443 outs() << "\n";
7444 outs() << " modtaboff " << dyst.modtaboff;
7445 if (dyst.modtaboff > object_size)
7446 outs() << " (past end of file)\n";
7447 else
7448 outs() << "\n";
7449 outs() << " nmodtab " << dyst.nmodtab;
7450 uint64_t modtabend;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007451 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007452 modtabend = dyst.nmodtab;
7453 modtabend *= sizeof(struct MachO::dylib_module_64);
7454 modtabend += dyst.modtaboff;
7455 } else {
7456 modtabend = dyst.nmodtab;
7457 modtabend *= sizeof(struct MachO::dylib_module);
7458 modtabend += dyst.modtaboff;
7459 }
7460 if (modtabend > object_size)
7461 outs() << " (past end of file)\n";
7462 else
7463 outs() << "\n";
7464 outs() << " extrefsymoff " << dyst.extrefsymoff;
7465 if (dyst.extrefsymoff > object_size)
7466 outs() << " (past end of file)\n";
7467 else
7468 outs() << "\n";
7469 outs() << " nextrefsyms " << dyst.nextrefsyms;
7470 big_size = dyst.nextrefsyms;
7471 big_size *= sizeof(struct MachO::dylib_reference);
7472 big_size += dyst.extrefsymoff;
7473 if (big_size > object_size)
7474 outs() << " (past end of file)\n";
7475 else
7476 outs() << "\n";
7477 outs() << " indirectsymoff " << dyst.indirectsymoff;
7478 if (dyst.indirectsymoff > object_size)
7479 outs() << " (past end of file)\n";
7480 else
7481 outs() << "\n";
7482 outs() << " nindirectsyms " << dyst.nindirectsyms;
7483 big_size = dyst.nindirectsyms;
7484 big_size *= sizeof(uint32_t);
7485 big_size += dyst.indirectsymoff;
7486 if (big_size > object_size)
7487 outs() << " (past end of file)\n";
7488 else
7489 outs() << "\n";
7490 outs() << " extreloff " << dyst.extreloff;
7491 if (dyst.extreloff > object_size)
7492 outs() << " (past end of file)\n";
7493 else
7494 outs() << "\n";
7495 outs() << " nextrel " << dyst.nextrel;
7496 big_size = dyst.nextrel;
7497 big_size *= sizeof(struct MachO::relocation_info);
7498 big_size += dyst.extreloff;
7499 if (big_size > object_size)
7500 outs() << " (past end of file)\n";
7501 else
7502 outs() << "\n";
7503 outs() << " locreloff " << dyst.locreloff;
7504 if (dyst.locreloff > object_size)
7505 outs() << " (past end of file)\n";
7506 else
7507 outs() << "\n";
7508 outs() << " nlocrel " << dyst.nlocrel;
7509 big_size = dyst.nlocrel;
7510 big_size *= sizeof(struct MachO::relocation_info);
7511 big_size += dyst.locreloff;
7512 if (big_size > object_size)
7513 outs() << " (past end of file)\n";
7514 else
7515 outs() << "\n";
7516}
7517
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007518static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
7519 uint32_t object_size) {
7520 if (dc.cmd == MachO::LC_DYLD_INFO)
7521 outs() << " cmd LC_DYLD_INFO\n";
7522 else
7523 outs() << " cmd LC_DYLD_INFO_ONLY\n";
7524 outs() << " cmdsize " << dc.cmdsize;
7525 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
7526 outs() << " Incorrect size\n";
7527 else
7528 outs() << "\n";
7529 outs() << " rebase_off " << dc.rebase_off;
7530 if (dc.rebase_off > object_size)
7531 outs() << " (past end of file)\n";
7532 else
7533 outs() << "\n";
7534 outs() << " rebase_size " << dc.rebase_size;
7535 uint64_t big_size;
7536 big_size = dc.rebase_off;
7537 big_size += dc.rebase_size;
7538 if (big_size > object_size)
7539 outs() << " (past end of file)\n";
7540 else
7541 outs() << "\n";
7542 outs() << " bind_off " << dc.bind_off;
7543 if (dc.bind_off > object_size)
7544 outs() << " (past end of file)\n";
7545 else
7546 outs() << "\n";
7547 outs() << " bind_size " << dc.bind_size;
7548 big_size = dc.bind_off;
7549 big_size += dc.bind_size;
7550 if (big_size > object_size)
7551 outs() << " (past end of file)\n";
7552 else
7553 outs() << "\n";
7554 outs() << " weak_bind_off " << dc.weak_bind_off;
7555 if (dc.weak_bind_off > object_size)
7556 outs() << " (past end of file)\n";
7557 else
7558 outs() << "\n";
7559 outs() << " weak_bind_size " << dc.weak_bind_size;
7560 big_size = dc.weak_bind_off;
7561 big_size += dc.weak_bind_size;
7562 if (big_size > object_size)
7563 outs() << " (past end of file)\n";
7564 else
7565 outs() << "\n";
7566 outs() << " lazy_bind_off " << dc.lazy_bind_off;
7567 if (dc.lazy_bind_off > object_size)
7568 outs() << " (past end of file)\n";
7569 else
7570 outs() << "\n";
7571 outs() << " lazy_bind_size " << dc.lazy_bind_size;
7572 big_size = dc.lazy_bind_off;
7573 big_size += dc.lazy_bind_size;
7574 if (big_size > object_size)
7575 outs() << " (past end of file)\n";
7576 else
7577 outs() << "\n";
7578 outs() << " export_off " << dc.export_off;
7579 if (dc.export_off > object_size)
7580 outs() << " (past end of file)\n";
7581 else
7582 outs() << "\n";
7583 outs() << " export_size " << dc.export_size;
7584 big_size = dc.export_off;
7585 big_size += dc.export_size;
7586 if (big_size > object_size)
7587 outs() << " (past end of file)\n";
7588 else
7589 outs() << "\n";
7590}
7591
7592static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
7593 const char *Ptr) {
7594 if (dyld.cmd == MachO::LC_ID_DYLINKER)
7595 outs() << " cmd LC_ID_DYLINKER\n";
7596 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
7597 outs() << " cmd LC_LOAD_DYLINKER\n";
7598 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
7599 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
7600 else
7601 outs() << " cmd ?(" << dyld.cmd << ")\n";
7602 outs() << " cmdsize " << dyld.cmdsize;
7603 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
7604 outs() << " Incorrect size\n";
7605 else
7606 outs() << "\n";
7607 if (dyld.name >= dyld.cmdsize)
7608 outs() << " name ?(bad offset " << dyld.name << ")\n";
7609 else {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007610 const char *P = (const char *)(Ptr) + dyld.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007611 outs() << " name " << P << " (offset " << dyld.name << ")\n";
7612 }
7613}
7614
7615static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
7616 outs() << " cmd LC_UUID\n";
7617 outs() << " cmdsize " << uuid.cmdsize;
7618 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
7619 outs() << " Incorrect size\n";
7620 else
7621 outs() << "\n";
7622 outs() << " uuid ";
7623 outs() << format("%02" PRIX32, uuid.uuid[0]);
7624 outs() << format("%02" PRIX32, uuid.uuid[1]);
7625 outs() << format("%02" PRIX32, uuid.uuid[2]);
7626 outs() << format("%02" PRIX32, uuid.uuid[3]);
7627 outs() << "-";
7628 outs() << format("%02" PRIX32, uuid.uuid[4]);
7629 outs() << format("%02" PRIX32, uuid.uuid[5]);
7630 outs() << "-";
7631 outs() << format("%02" PRIX32, uuid.uuid[6]);
7632 outs() << format("%02" PRIX32, uuid.uuid[7]);
7633 outs() << "-";
7634 outs() << format("%02" PRIX32, uuid.uuid[8]);
7635 outs() << format("%02" PRIX32, uuid.uuid[9]);
7636 outs() << "-";
7637 outs() << format("%02" PRIX32, uuid.uuid[10]);
7638 outs() << format("%02" PRIX32, uuid.uuid[11]);
7639 outs() << format("%02" PRIX32, uuid.uuid[12]);
7640 outs() << format("%02" PRIX32, uuid.uuid[13]);
7641 outs() << format("%02" PRIX32, uuid.uuid[14]);
7642 outs() << format("%02" PRIX32, uuid.uuid[15]);
7643 outs() << "\n";
7644}
7645
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007646static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00007647 outs() << " cmd LC_RPATH\n";
7648 outs() << " cmdsize " << rpath.cmdsize;
7649 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
7650 outs() << " Incorrect size\n";
7651 else
7652 outs() << "\n";
7653 if (rpath.path >= rpath.cmdsize)
7654 outs() << " path ?(bad offset " << rpath.path << ")\n";
7655 else {
7656 const char *P = (const char *)(Ptr) + rpath.path;
7657 outs() << " path " << P << " (offset " << rpath.path << ")\n";
7658 }
7659}
7660
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007661static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
7662 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
7663 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
7664 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
7665 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
7666 else
7667 outs() << " cmd " << vd.cmd << " (?)\n";
7668 outs() << " cmdsize " << vd.cmdsize;
7669 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
7670 outs() << " Incorrect size\n";
7671 else
7672 outs() << "\n";
7673 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
7674 << ((vd.version >> 8) & 0xff);
7675 if ((vd.version & 0xff) != 0)
7676 outs() << "." << (vd.version & 0xff);
7677 outs() << "\n";
7678 if (vd.sdk == 0)
Kevin Enderby57538292014-12-17 01:01:30 +00007679 outs() << " sdk n/a";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007680 else {
7681 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
7682 << ((vd.sdk >> 8) & 0xff);
7683 }
7684 if ((vd.sdk & 0xff) != 0)
7685 outs() << "." << (vd.sdk & 0xff);
7686 outs() << "\n";
7687}
7688
7689static void PrintSourceVersionCommand(MachO::source_version_command sd) {
7690 outs() << " cmd LC_SOURCE_VERSION\n";
7691 outs() << " cmdsize " << sd.cmdsize;
7692 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
7693 outs() << " Incorrect size\n";
7694 else
7695 outs() << "\n";
7696 uint64_t a = (sd.version >> 40) & 0xffffff;
7697 uint64_t b = (sd.version >> 30) & 0x3ff;
7698 uint64_t c = (sd.version >> 20) & 0x3ff;
7699 uint64_t d = (sd.version >> 10) & 0x3ff;
7700 uint64_t e = sd.version & 0x3ff;
7701 outs() << " version " << a << "." << b;
7702 if (e != 0)
7703 outs() << "." << c << "." << d << "." << e;
7704 else if (d != 0)
7705 outs() << "." << c << "." << d;
7706 else if (c != 0)
7707 outs() << "." << c;
7708 outs() << "\n";
7709}
7710
7711static void PrintEntryPointCommand(MachO::entry_point_command ep) {
7712 outs() << " cmd LC_MAIN\n";
7713 outs() << " cmdsize " << ep.cmdsize;
7714 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
7715 outs() << " Incorrect size\n";
7716 else
7717 outs() << "\n";
7718 outs() << " entryoff " << ep.entryoff << "\n";
7719 outs() << " stacksize " << ep.stacksize << "\n";
7720}
7721
Kevin Enderby0804f4672014-12-16 23:25:52 +00007722static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
7723 uint32_t object_size) {
7724 outs() << " cmd LC_ENCRYPTION_INFO\n";
7725 outs() << " cmdsize " << ec.cmdsize;
7726 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
7727 outs() << " Incorrect size\n";
7728 else
7729 outs() << "\n";
7730 outs() << " cryptoff " << ec.cryptoff;
7731 if (ec.cryptoff > object_size)
7732 outs() << " (past end of file)\n";
7733 else
7734 outs() << "\n";
7735 outs() << " cryptsize " << ec.cryptsize;
7736 if (ec.cryptsize > object_size)
7737 outs() << " (past end of file)\n";
7738 else
7739 outs() << "\n";
7740 outs() << " cryptid " << ec.cryptid << "\n";
7741}
7742
Kevin Enderby57538292014-12-17 01:01:30 +00007743static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007744 uint32_t object_size) {
Kevin Enderby57538292014-12-17 01:01:30 +00007745 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
7746 outs() << " cmdsize " << ec.cmdsize;
7747 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
7748 outs() << " Incorrect size\n";
7749 else
7750 outs() << "\n";
7751 outs() << " cryptoff " << ec.cryptoff;
7752 if (ec.cryptoff > object_size)
7753 outs() << " (past end of file)\n";
7754 else
7755 outs() << "\n";
7756 outs() << " cryptsize " << ec.cryptsize;
7757 if (ec.cryptsize > object_size)
7758 outs() << " (past end of file)\n";
7759 else
7760 outs() << "\n";
7761 outs() << " cryptid " << ec.cryptid << "\n";
7762 outs() << " pad " << ec.pad << "\n";
7763}
7764
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007765static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
7766 const char *Ptr) {
7767 outs() << " cmd LC_LINKER_OPTION\n";
7768 outs() << " cmdsize " << lo.cmdsize;
7769 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
7770 outs() << " Incorrect size\n";
7771 else
7772 outs() << "\n";
7773 outs() << " count " << lo.count << "\n";
7774 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
7775 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
7776 uint32_t i = 0;
7777 while (left > 0) {
7778 while (*string == '\0' && left > 0) {
7779 string++;
7780 left--;
7781 }
7782 if (left > 0) {
7783 i++;
7784 outs() << " string #" << i << " " << format("%.*s\n", left, string);
David Majnemerd4449ed2014-12-20 08:24:43 +00007785 uint32_t NullPos = StringRef(string, left).find('\0');
7786 uint32_t len = std::min(NullPos, left) + 1;
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007787 string += len;
7788 left -= len;
7789 }
7790 }
7791 if (lo.count != i)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007792 outs() << " count " << lo.count << " does not match number of strings "
7793 << i << "\n";
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007794}
7795
Kevin Enderbyb4b79312014-12-18 19:24:35 +00007796static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
7797 const char *Ptr) {
7798 outs() << " cmd LC_SUB_FRAMEWORK\n";
7799 outs() << " cmdsize " << sub.cmdsize;
7800 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
7801 outs() << " Incorrect size\n";
7802 else
7803 outs() << "\n";
7804 if (sub.umbrella < sub.cmdsize) {
7805 const char *P = Ptr + sub.umbrella;
7806 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
7807 } else {
7808 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
7809 }
7810}
7811
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00007812static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
7813 const char *Ptr) {
7814 outs() << " cmd LC_SUB_UMBRELLA\n";
7815 outs() << " cmdsize " << sub.cmdsize;
7816 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
7817 outs() << " Incorrect size\n";
7818 else
7819 outs() << "\n";
7820 if (sub.sub_umbrella < sub.cmdsize) {
7821 const char *P = Ptr + sub.sub_umbrella;
7822 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
7823 } else {
7824 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
7825 }
7826}
7827
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007828static void PrintSubLibraryCommand(MachO::sub_library_command sub,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007829 const char *Ptr) {
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007830 outs() << " cmd LC_SUB_LIBRARY\n";
7831 outs() << " cmdsize " << sub.cmdsize;
7832 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
7833 outs() << " Incorrect size\n";
7834 else
7835 outs() << "\n";
7836 if (sub.sub_library < sub.cmdsize) {
7837 const char *P = Ptr + sub.sub_library;
7838 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7839 } else {
7840 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7841 }
7842}
7843
Kevin Enderby186eac32014-12-19 21:06:24 +00007844static void PrintSubClientCommand(MachO::sub_client_command sub,
7845 const char *Ptr) {
7846 outs() << " cmd LC_SUB_CLIENT\n";
7847 outs() << " cmdsize " << sub.cmdsize;
7848 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7849 outs() << " Incorrect size\n";
7850 else
7851 outs() << "\n";
7852 if (sub.client < sub.cmdsize) {
7853 const char *P = Ptr + sub.client;
7854 outs() << " client " << P << " (offset " << sub.client << ")\n";
7855 } else {
7856 outs() << " client ?(bad offset " << sub.client << ")\n";
7857 }
7858}
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007859
Kevin Enderby52e4ce42014-12-19 22:25:22 +00007860static void PrintRoutinesCommand(MachO::routines_command r) {
7861 outs() << " cmd LC_ROUTINES\n";
7862 outs() << " cmdsize " << r.cmdsize;
7863 if (r.cmdsize != sizeof(struct MachO::routines_command))
7864 outs() << " Incorrect size\n";
7865 else
7866 outs() << "\n";
7867 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7868 outs() << " init_module " << r.init_module << "\n";
7869 outs() << " reserved1 " << r.reserved1 << "\n";
7870 outs() << " reserved2 " << r.reserved2 << "\n";
7871 outs() << " reserved3 " << r.reserved3 << "\n";
7872 outs() << " reserved4 " << r.reserved4 << "\n";
7873 outs() << " reserved5 " << r.reserved5 << "\n";
7874 outs() << " reserved6 " << r.reserved6 << "\n";
7875}
7876
7877static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7878 outs() << " cmd LC_ROUTINES_64\n";
7879 outs() << " cmdsize " << r.cmdsize;
7880 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7881 outs() << " Incorrect size\n";
7882 else
7883 outs() << "\n";
7884 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
7885 outs() << " init_module " << r.init_module << "\n";
7886 outs() << " reserved1 " << r.reserved1 << "\n";
7887 outs() << " reserved2 " << r.reserved2 << "\n";
7888 outs() << " reserved3 " << r.reserved3 << "\n";
7889 outs() << " reserved4 " << r.reserved4 << "\n";
7890 outs() << " reserved5 " << r.reserved5 << "\n";
7891 outs() << " reserved6 " << r.reserved6 << "\n";
7892}
7893
Kevin Enderby48ef5342014-12-23 22:56:39 +00007894static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7895 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7896 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7897 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7898 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7899 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7900 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7901 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7902 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7903 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7904 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7905 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7906 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7907 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7908 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7909 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7910 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7911 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7912 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7913 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7914 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7915 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7916}
7917
Kevin Enderby227df342014-12-23 23:43:59 +00007918static void Print_mmst_reg(MachO::mmst_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007919 uint32_t f;
7920 outs() << "\t mmst_reg ";
7921 for (f = 0; f < 10; f++)
7922 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7923 outs() << "\n";
7924 outs() << "\t mmst_rsrv ";
7925 for (f = 0; f < 6; f++)
7926 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7927 outs() << "\n";
7928}
7929
Kevin Enderbyaefb0032014-12-24 00:16:51 +00007930static void Print_xmm_reg(MachO::xmm_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007931 uint32_t f;
7932 outs() << "\t xmm_reg ";
7933 for (f = 0; f < 16; f++)
7934 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7935 outs() << "\n";
7936}
7937
7938static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7939 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7940 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7941 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7942 outs() << " denorm " << fpu.fpu_fcw.denorm;
7943 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7944 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7945 outs() << " undfl " << fpu.fpu_fcw.undfl;
7946 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7947 outs() << "\t\t pc ";
7948 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7949 outs() << "FP_PREC_24B ";
7950 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
7951 outs() << "FP_PREC_53B ";
7952 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
7953 outs() << "FP_PREC_64B ";
7954 else
7955 outs() << fpu.fpu_fcw.pc << " ";
7956 outs() << "rc ";
7957 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
7958 outs() << "FP_RND_NEAR ";
7959 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
7960 outs() << "FP_RND_DOWN ";
7961 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
7962 outs() << "FP_RND_UP ";
7963 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007964 outs() << "FP_CHOP ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00007965 outs() << "\n";
7966 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
7967 outs() << " denorm " << fpu.fpu_fsw.denorm;
7968 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
7969 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
7970 outs() << " undfl " << fpu.fpu_fsw.undfl;
7971 outs() << " precis " << fpu.fpu_fsw.precis;
7972 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
7973 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
7974 outs() << " c0 " << fpu.fpu_fsw.c0;
7975 outs() << " c1 " << fpu.fpu_fsw.c1;
7976 outs() << " c2 " << fpu.fpu_fsw.c2;
7977 outs() << " tos " << fpu.fpu_fsw.tos;
7978 outs() << " c3 " << fpu.fpu_fsw.c3;
7979 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
7980 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
7981 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
7982 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
7983 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
7984 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
7985 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
7986 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
7987 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
7988 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
7989 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
7990 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
7991 outs() << "\n";
7992 outs() << "\t fpu_stmm0:\n";
7993 Print_mmst_reg(fpu.fpu_stmm0);
7994 outs() << "\t fpu_stmm1:\n";
7995 Print_mmst_reg(fpu.fpu_stmm1);
7996 outs() << "\t fpu_stmm2:\n";
7997 Print_mmst_reg(fpu.fpu_stmm2);
7998 outs() << "\t fpu_stmm3:\n";
7999 Print_mmst_reg(fpu.fpu_stmm3);
8000 outs() << "\t fpu_stmm4:\n";
8001 Print_mmst_reg(fpu.fpu_stmm4);
8002 outs() << "\t fpu_stmm5:\n";
8003 Print_mmst_reg(fpu.fpu_stmm5);
8004 outs() << "\t fpu_stmm6:\n";
8005 Print_mmst_reg(fpu.fpu_stmm6);
8006 outs() << "\t fpu_stmm7:\n";
8007 Print_mmst_reg(fpu.fpu_stmm7);
8008 outs() << "\t fpu_xmm0:\n";
8009 Print_xmm_reg(fpu.fpu_xmm0);
8010 outs() << "\t fpu_xmm1:\n";
8011 Print_xmm_reg(fpu.fpu_xmm1);
8012 outs() << "\t fpu_xmm2:\n";
8013 Print_xmm_reg(fpu.fpu_xmm2);
8014 outs() << "\t fpu_xmm3:\n";
8015 Print_xmm_reg(fpu.fpu_xmm3);
8016 outs() << "\t fpu_xmm4:\n";
8017 Print_xmm_reg(fpu.fpu_xmm4);
8018 outs() << "\t fpu_xmm5:\n";
8019 Print_xmm_reg(fpu.fpu_xmm5);
8020 outs() << "\t fpu_xmm6:\n";
8021 Print_xmm_reg(fpu.fpu_xmm6);
8022 outs() << "\t fpu_xmm7:\n";
8023 Print_xmm_reg(fpu.fpu_xmm7);
8024 outs() << "\t fpu_xmm8:\n";
8025 Print_xmm_reg(fpu.fpu_xmm8);
8026 outs() << "\t fpu_xmm9:\n";
8027 Print_xmm_reg(fpu.fpu_xmm9);
8028 outs() << "\t fpu_xmm10:\n";
8029 Print_xmm_reg(fpu.fpu_xmm10);
8030 outs() << "\t fpu_xmm11:\n";
8031 Print_xmm_reg(fpu.fpu_xmm11);
8032 outs() << "\t fpu_xmm12:\n";
8033 Print_xmm_reg(fpu.fpu_xmm12);
8034 outs() << "\t fpu_xmm13:\n";
8035 Print_xmm_reg(fpu.fpu_xmm13);
8036 outs() << "\t fpu_xmm14:\n";
8037 Print_xmm_reg(fpu.fpu_xmm14);
8038 outs() << "\t fpu_xmm15:\n";
8039 Print_xmm_reg(fpu.fpu_xmm15);
8040 outs() << "\t fpu_rsrv4:\n";
8041 for (uint32_t f = 0; f < 6; f++) {
8042 outs() << "\t ";
8043 for (uint32_t g = 0; g < 16; g++)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008044 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008045 outs() << "\n";
8046 }
8047 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8048 outs() << "\n";
8049}
8050
8051static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8052 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8053 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8054 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8055}
8056
8057static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8058 bool isLittleEndian, uint32_t cputype) {
8059 if (t.cmd == MachO::LC_THREAD)
8060 outs() << " cmd LC_THREAD\n";
8061 else if (t.cmd == MachO::LC_UNIXTHREAD)
8062 outs() << " cmd LC_UNIXTHREAD\n";
8063 else
8064 outs() << " cmd " << t.cmd << " (unknown)\n";
8065 outs() << " cmdsize " << t.cmdsize;
8066 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8067 outs() << " Incorrect size\n";
8068 else
8069 outs() << "\n";
8070
8071 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8072 const char *end = Ptr + t.cmdsize;
8073 uint32_t flavor, count, left;
8074 if (cputype == MachO::CPU_TYPE_X86_64) {
8075 while (begin < end) {
8076 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8077 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8078 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008079 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008080 flavor = 0;
8081 begin = end;
8082 }
8083 if (isLittleEndian != sys::IsLittleEndianHost)
8084 sys::swapByteOrder(flavor);
8085 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8086 memcpy((char *)&count, begin, sizeof(uint32_t));
8087 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008088 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008089 count = 0;
8090 begin = end;
8091 }
8092 if (isLittleEndian != sys::IsLittleEndianHost)
8093 sys::swapByteOrder(count);
8094 if (flavor == MachO::x86_THREAD_STATE64) {
8095 outs() << " flavor x86_THREAD_STATE64\n";
8096 if (count == MachO::x86_THREAD_STATE64_COUNT)
8097 outs() << " count x86_THREAD_STATE64_COUNT\n";
8098 else
8099 outs() << " count " << count
8100 << " (not x86_THREAD_STATE64_COUNT)\n";
8101 MachO::x86_thread_state64_t cpu64;
8102 left = end - begin;
8103 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8104 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8105 begin += sizeof(MachO::x86_thread_state64_t);
8106 } else {
8107 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8108 memcpy(&cpu64, begin, left);
8109 begin += left;
8110 }
8111 if (isLittleEndian != sys::IsLittleEndianHost)
8112 swapStruct(cpu64);
8113 Print_x86_thread_state64_t(cpu64);
8114 } else if (flavor == MachO::x86_THREAD_STATE) {
8115 outs() << " flavor x86_THREAD_STATE\n";
8116 if (count == MachO::x86_THREAD_STATE_COUNT)
8117 outs() << " count x86_THREAD_STATE_COUNT\n";
8118 else
8119 outs() << " count " << count
8120 << " (not x86_THREAD_STATE_COUNT)\n";
8121 struct MachO::x86_thread_state_t ts;
8122 left = end - begin;
8123 if (left >= sizeof(MachO::x86_thread_state_t)) {
8124 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8125 begin += sizeof(MachO::x86_thread_state_t);
8126 } else {
8127 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8128 memcpy(&ts, begin, left);
8129 begin += left;
8130 }
8131 if (isLittleEndian != sys::IsLittleEndianHost)
8132 swapStruct(ts);
8133 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8134 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8135 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8136 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8137 else
8138 outs() << "tsh.count " << ts.tsh.count
8139 << " (not x86_THREAD_STATE64_COUNT\n";
8140 Print_x86_thread_state64_t(ts.uts.ts64);
8141 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008142 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8143 << ts.tsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008144 }
8145 } else if (flavor == MachO::x86_FLOAT_STATE) {
8146 outs() << " flavor x86_FLOAT_STATE\n";
8147 if (count == MachO::x86_FLOAT_STATE_COUNT)
8148 outs() << " count x86_FLOAT_STATE_COUNT\n";
8149 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008150 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008151 struct MachO::x86_float_state_t fs;
8152 left = end - begin;
8153 if (left >= sizeof(MachO::x86_float_state_t)) {
8154 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8155 begin += sizeof(MachO::x86_float_state_t);
8156 } else {
8157 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8158 memcpy(&fs, begin, left);
8159 begin += left;
8160 }
8161 if (isLittleEndian != sys::IsLittleEndianHost)
8162 swapStruct(fs);
8163 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8164 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008165 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008166 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8167 else
8168 outs() << "fsh.count " << fs.fsh.count
8169 << " (not x86_FLOAT_STATE64_COUNT\n";
8170 Print_x86_float_state_t(fs.ufs.fs64);
8171 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008172 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8173 << fs.fsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008174 }
8175 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8176 outs() << " flavor x86_EXCEPTION_STATE\n";
8177 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8178 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8179 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008180 outs() << " count " << count
Kevin Enderby48ef5342014-12-23 22:56:39 +00008181 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8182 struct MachO::x86_exception_state_t es;
8183 left = end - begin;
8184 if (left >= sizeof(MachO::x86_exception_state_t)) {
8185 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8186 begin += sizeof(MachO::x86_exception_state_t);
8187 } else {
8188 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8189 memcpy(&es, begin, left);
8190 begin += left;
8191 }
8192 if (isLittleEndian != sys::IsLittleEndianHost)
8193 swapStruct(es);
8194 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8195 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008196 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008197 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8198 else
8199 outs() << "\t esh.count " << es.esh.count
8200 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8201 Print_x86_exception_state_t(es.ues.es64);
8202 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008203 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8204 << es.esh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008205 }
8206 } else {
8207 outs() << " flavor " << flavor << " (unknown)\n";
8208 outs() << " count " << count << "\n";
8209 outs() << " state (unknown)\n";
8210 begin += count * sizeof(uint32_t);
8211 }
8212 }
8213 } else {
8214 while (begin < end) {
8215 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8216 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8217 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008218 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008219 flavor = 0;
8220 begin = end;
8221 }
8222 if (isLittleEndian != sys::IsLittleEndianHost)
8223 sys::swapByteOrder(flavor);
8224 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8225 memcpy((char *)&count, begin, sizeof(uint32_t));
8226 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008227 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008228 count = 0;
8229 begin = end;
8230 }
8231 if (isLittleEndian != sys::IsLittleEndianHost)
8232 sys::swapByteOrder(count);
8233 outs() << " flavor " << flavor << "\n";
8234 outs() << " count " << count << "\n";
8235 outs() << " state (Unknown cputype/cpusubtype)\n";
8236 begin += count * sizeof(uint32_t);
8237 }
8238 }
8239}
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008240
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008241static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8242 if (dl.cmd == MachO::LC_ID_DYLIB)
8243 outs() << " cmd LC_ID_DYLIB\n";
8244 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8245 outs() << " cmd LC_LOAD_DYLIB\n";
8246 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8247 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8248 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8249 outs() << " cmd LC_REEXPORT_DYLIB\n";
8250 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8251 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8252 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8253 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8254 else
8255 outs() << " cmd " << dl.cmd << " (unknown)\n";
8256 outs() << " cmdsize " << dl.cmdsize;
8257 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8258 outs() << " Incorrect size\n";
8259 else
8260 outs() << "\n";
8261 if (dl.dylib.name < dl.cmdsize) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008262 const char *P = (const char *)(Ptr) + dl.dylib.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008263 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8264 } else {
8265 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8266 }
8267 outs() << " time stamp " << dl.dylib.timestamp << " ";
8268 time_t t = dl.dylib.timestamp;
8269 outs() << ctime(&t);
8270 outs() << " current version ";
8271 if (dl.dylib.current_version == 0xffffffff)
8272 outs() << "n/a\n";
8273 else
8274 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8275 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8276 << (dl.dylib.current_version & 0xff) << "\n";
8277 outs() << "compatibility version ";
8278 if (dl.dylib.compatibility_version == 0xffffffff)
8279 outs() << "n/a\n";
8280 else
8281 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8282 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8283 << (dl.dylib.compatibility_version & 0xff) << "\n";
8284}
8285
8286static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8287 uint32_t object_size) {
8288 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8289 outs() << " cmd LC_FUNCTION_STARTS\n";
8290 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8291 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8292 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8293 outs() << " cmd LC_FUNCTION_STARTS\n";
8294 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8295 outs() << " cmd LC_DATA_IN_CODE\n";
8296 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8297 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8298 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8299 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8300 else
8301 outs() << " cmd " << ld.cmd << " (?)\n";
8302 outs() << " cmdsize " << ld.cmdsize;
8303 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8304 outs() << " Incorrect size\n";
8305 else
8306 outs() << "\n";
8307 outs() << " dataoff " << ld.dataoff;
8308 if (ld.dataoff > object_size)
8309 outs() << " (past end of file)\n";
8310 else
8311 outs() << "\n";
8312 outs() << " datasize " << ld.datasize;
8313 uint64_t big_size = ld.dataoff;
8314 big_size += ld.datasize;
8315 if (big_size > object_size)
8316 outs() << " (past end of file)\n";
8317 else
8318 outs() << "\n";
8319}
8320
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008321static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8322 uint32_t cputype, bool verbose) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008323 StringRef Buf = Obj->getData();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008324 unsigned Index = 0;
8325 for (const auto &Command : Obj->load_commands()) {
8326 outs() << "Load command " << Index++ << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00008327 if (Command.C.cmd == MachO::LC_SEGMENT) {
8328 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8329 const char *sg_segname = SLC.segname;
8330 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8331 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8332 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8333 verbose);
8334 for (unsigned j = 0; j < SLC.nsects; j++) {
Kevin Enderbyc9713382014-12-16 01:14:45 +00008335 MachO::section S = Obj->getSection(Command, j);
Kevin Enderby956366c2014-08-29 22:30:52 +00008336 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8337 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8338 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8339 }
8340 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8341 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8342 const char *sg_segname = SLC_64.segname;
8343 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8344 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8345 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8346 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8347 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8348 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8349 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8350 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8351 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8352 sg_segname, filetype, Buf.size(), verbose);
8353 }
8354 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8355 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008356 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008357 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8358 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8359 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008360 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8361 Obj->is64Bit());
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008362 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8363 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8364 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8365 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8366 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8367 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8368 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8369 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8370 PrintDyldLoadCommand(Dyld, Command.Ptr);
8371 } else if (Command.C.cmd == MachO::LC_UUID) {
8372 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8373 PrintUuidLoadCommand(Uuid);
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008374 } else if (Command.C.cmd == MachO::LC_RPATH) {
8375 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8376 PrintRpathLoadCommand(Rpath, Command.Ptr);
Kevin Enderby1ff0ecc2014-12-16 21:48:27 +00008377 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
8378 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008379 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8380 PrintVersionMinLoadCommand(Vd);
8381 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8382 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8383 PrintSourceVersionCommand(Sd);
8384 } else if (Command.C.cmd == MachO::LC_MAIN) {
8385 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8386 PrintEntryPointCommand(Ep);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008387 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008388 MachO::encryption_info_command Ei =
8389 Obj->getEncryptionInfoCommand(Command);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008390 PrintEncryptionInfoCommand(Ei, Buf.size());
Kevin Enderby57538292014-12-17 01:01:30 +00008391 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008392 MachO::encryption_info_command_64 Ei =
8393 Obj->getEncryptionInfoCommand64(Command);
Kevin Enderby57538292014-12-17 01:01:30 +00008394 PrintEncryptionInfoCommand64(Ei, Buf.size());
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008395 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008396 MachO::linker_option_command Lo =
8397 Obj->getLinkerOptionLoadCommand(Command);
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008398 PrintLinkerOptionCommand(Lo, Command.Ptr);
Kevin Enderbyb4b79312014-12-18 19:24:35 +00008399 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8400 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8401 PrintSubFrameworkCommand(Sf, Command.Ptr);
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00008402 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8403 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8404 PrintSubUmbrellaCommand(Sf, Command.Ptr);
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008405 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8406 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8407 PrintSubLibraryCommand(Sl, Command.Ptr);
Kevin Enderby186eac32014-12-19 21:06:24 +00008408 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8409 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8410 PrintSubClientCommand(Sc, Command.Ptr);
Kevin Enderby52e4ce42014-12-19 22:25:22 +00008411 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8412 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8413 PrintRoutinesCommand(Rc);
8414 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8415 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8416 PrintRoutinesCommand64(Rc);
Kevin Enderby48ef5342014-12-23 22:56:39 +00008417 } else if (Command.C.cmd == MachO::LC_THREAD ||
8418 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8419 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8420 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
Nick Kledzik15558912014-10-16 18:58:20 +00008421 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8422 Command.C.cmd == MachO::LC_ID_DYLIB ||
8423 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8424 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8425 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8426 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008427 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8428 PrintDylibCommand(Dl, Command.Ptr);
8429 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8430 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8431 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8432 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8433 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8434 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8435 MachO::linkedit_data_command Ld =
8436 Obj->getLinkeditDataLoadCommand(Command);
8437 PrintLinkEditDataCommand(Ld, Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008438 } else {
8439 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8440 << ")\n";
8441 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8442 // TODO: get and print the raw bytes of the load command.
8443 }
8444 // TODO: print all the other kinds of load commands.
Kevin Enderby956366c2014-08-29 22:30:52 +00008445 }
8446}
8447
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008448static void getAndPrintMachHeader(const MachOObjectFile *Obj,
Kevin Enderby956366c2014-08-29 22:30:52 +00008449 uint32_t &filetype, uint32_t &cputype,
8450 bool verbose) {
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008451 if (Obj->is64Bit()) {
8452 MachO::mach_header_64 H_64;
8453 H_64 = Obj->getHeader64();
8454 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8455 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00008456 filetype = H_64.filetype;
8457 cputype = H_64.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008458 } else {
8459 MachO::mach_header H;
8460 H = Obj->getHeader();
8461 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8462 H.sizeofcmds, H.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00008463 filetype = H.filetype;
8464 cputype = H.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008465 }
8466}
8467
8468void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8469 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby956366c2014-08-29 22:30:52 +00008470 uint32_t filetype = 0;
8471 uint32_t cputype = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008472 getAndPrintMachHeader(file, filetype, cputype, !NonVerbose);
8473 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008474}
Nick Kledzikd04bc352014-08-30 00:20:14 +00008475
8476//===----------------------------------------------------------------------===//
8477// export trie dumping
8478//===----------------------------------------------------------------------===//
8479
8480void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008481 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
8482 uint64_t Flags = Entry.flags();
Nick Kledzikd04bc352014-08-30 00:20:14 +00008483 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
8484 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
8485 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8486 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
8487 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8488 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
8489 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
8490 if (ReExport)
8491 outs() << "[re-export] ";
8492 else
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008493 outs() << format("0x%08llX ",
8494 Entry.address()); // FIXME:add in base address
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008495 outs() << Entry.name();
Nick Kledzikd04bc352014-08-30 00:20:14 +00008496 if (WeakDef || ThreadLocal || Resolver || Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008497 bool NeedsComma = false;
Nick Kledzik1d1ac4b2014-09-03 01:12:52 +00008498 outs() << " [";
Nick Kledzikd04bc352014-08-30 00:20:14 +00008499 if (WeakDef) {
8500 outs() << "weak_def";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008501 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008502 }
8503 if (ThreadLocal) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008504 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008505 outs() << ", ";
8506 outs() << "per-thread";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008507 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008508 }
8509 if (Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008510 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008511 outs() << ", ";
8512 outs() << "absolute";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008513 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008514 }
8515 if (Resolver) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008516 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008517 outs() << ", ";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008518 outs() << format("resolver=0x%08llX", Entry.other());
8519 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008520 }
8521 outs() << "]";
8522 }
8523 if (ReExport) {
8524 StringRef DylibName = "unknown";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008525 int Ordinal = Entry.other() - 1;
8526 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
8527 if (Entry.otherName().empty())
Nick Kledzikd04bc352014-08-30 00:20:14 +00008528 outs() << " (from " << DylibName << ")";
8529 else
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008530 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
Nick Kledzikd04bc352014-08-30 00:20:14 +00008531 }
8532 outs() << "\n";
8533 }
8534}
Nick Kledzikac431442014-09-12 21:34:15 +00008535
Nick Kledzikac431442014-09-12 21:34:15 +00008536//===----------------------------------------------------------------------===//
8537// rebase table dumping
8538//===----------------------------------------------------------------------===//
8539
8540namespace {
8541class SegInfo {
8542public:
8543 SegInfo(const object::MachOObjectFile *Obj);
8544
8545 StringRef segmentName(uint32_t SegIndex);
8546 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
8547 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
8548
8549private:
8550 struct SectionInfo {
8551 uint64_t Address;
8552 uint64_t Size;
8553 StringRef SectionName;
8554 StringRef SegmentName;
8555 uint64_t OffsetInSegment;
8556 uint64_t SegmentStartAddress;
8557 uint32_t SegmentIndex;
8558 };
8559 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
8560 SmallVector<SectionInfo, 32> Sections;
8561};
8562}
8563
8564SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
8565 // Build table of sections so segIndex/offset pairs can be translated.
Nick Kledzik56ebef42014-09-16 01:41:51 +00008566 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
Nick Kledzikac431442014-09-12 21:34:15 +00008567 StringRef CurSegName;
8568 uint64_t CurSegAddress;
8569 for (const SectionRef &Section : Obj->sections()) {
8570 SectionInfo Info;
8571 if (error(Section.getName(Info.SectionName)))
8572 return;
Rafael Espindola80291272014-10-08 15:28:58 +00008573 Info.Address = Section.getAddress();
8574 Info.Size = Section.getSize();
Nick Kledzikac431442014-09-12 21:34:15 +00008575 Info.SegmentName =
8576 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
8577 if (!Info.SegmentName.equals(CurSegName)) {
8578 ++CurSegIndex;
8579 CurSegName = Info.SegmentName;
8580 CurSegAddress = Info.Address;
8581 }
8582 Info.SegmentIndex = CurSegIndex - 1;
8583 Info.OffsetInSegment = Info.Address - CurSegAddress;
8584 Info.SegmentStartAddress = CurSegAddress;
8585 Sections.push_back(Info);
8586 }
8587}
8588
8589StringRef SegInfo::segmentName(uint32_t SegIndex) {
8590 for (const SectionInfo &SI : Sections) {
8591 if (SI.SegmentIndex == SegIndex)
8592 return SI.SegmentName;
8593 }
8594 llvm_unreachable("invalid segIndex");
8595}
8596
8597const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
8598 uint64_t OffsetInSeg) {
8599 for (const SectionInfo &SI : Sections) {
8600 if (SI.SegmentIndex != SegIndex)
8601 continue;
8602 if (SI.OffsetInSegment > OffsetInSeg)
8603 continue;
8604 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8605 continue;
8606 return SI;
8607 }
8608 llvm_unreachable("segIndex and offset not in any section");
8609}
8610
8611StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
8612 return findSection(SegIndex, OffsetInSeg).SectionName;
8613}
8614
8615uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
8616 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
8617 return SI.SegmentStartAddress + OffsetInSeg;
8618}
8619
8620void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
8621 // Build table of sections so names can used in final output.
8622 SegInfo sectionTable(Obj);
8623
8624 outs() << "segment section address type\n";
8625 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
8626 uint32_t SegIndex = Entry.segmentIndex();
8627 uint64_t OffsetInSeg = Entry.segmentOffset();
8628 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8629 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8630 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8631
8632 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008633 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
8634 SegmentName.str().c_str(), SectionName.str().c_str(),
8635 Address, Entry.typeName().str().c_str());
Nick Kledzikac431442014-09-12 21:34:15 +00008636 }
8637}
Nick Kledzik56ebef42014-09-16 01:41:51 +00008638
8639static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
8640 StringRef DylibName;
8641 switch (Ordinal) {
8642 case MachO::BIND_SPECIAL_DYLIB_SELF:
8643 return "this-image";
8644 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
8645 return "main-executable";
8646 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
8647 return "flat-namespace";
8648 default:
Nick Kledzikabd29872014-09-16 22:03:13 +00008649 if (Ordinal > 0) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008650 std::error_code EC =
8651 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
Nick Kledzikabd29872014-09-16 22:03:13 +00008652 if (EC)
Nick Kledzik51d2c2b2014-10-14 23:29:38 +00008653 return "<<bad library ordinal>>";
Nick Kledzikabd29872014-09-16 22:03:13 +00008654 return DylibName;
8655 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00008656 }
Nick Kledzikabd29872014-09-16 22:03:13 +00008657 return "<<unknown special ordinal>>";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008658}
8659
8660//===----------------------------------------------------------------------===//
8661// bind table dumping
8662//===----------------------------------------------------------------------===//
8663
8664void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
8665 // Build table of sections so names can used in final output.
8666 SegInfo sectionTable(Obj);
8667
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008668 outs() << "segment section address type "
8669 "addend dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008670 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
8671 uint32_t SegIndex = Entry.segmentIndex();
8672 uint64_t OffsetInSeg = Entry.segmentOffset();
8673 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8674 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8675 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8676
8677 // Table lines look like:
8678 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008679 StringRef Attr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00008680 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008681 Attr = " (weak_import)";
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008682 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008683 << left_justify(SectionName, 18) << " "
8684 << format_hex(Address, 10, true) << " "
8685 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008686 << format_decimal(Entry.addend(), 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008687 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008688 << Entry.symbolName() << Attr << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008689 }
8690}
8691
8692//===----------------------------------------------------------------------===//
8693// lazy bind table dumping
8694//===----------------------------------------------------------------------===//
8695
8696void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
8697 // Build table of sections so names can used in final output.
8698 SegInfo sectionTable(Obj);
8699
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008700 outs() << "segment section address "
8701 "dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008702 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
8703 uint32_t SegIndex = Entry.segmentIndex();
8704 uint64_t OffsetInSeg = Entry.segmentOffset();
8705 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8706 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8707 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8708
8709 // Table lines look like:
8710 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008711 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008712 << left_justify(SectionName, 18) << " "
8713 << format_hex(Address, 10, true) << " "
8714 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Nick Kledzik56ebef42014-09-16 01:41:51 +00008715 << Entry.symbolName() << "\n";
8716 }
8717}
8718
Nick Kledzik56ebef42014-09-16 01:41:51 +00008719//===----------------------------------------------------------------------===//
8720// weak bind table dumping
8721//===----------------------------------------------------------------------===//
8722
8723void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
8724 // Build table of sections so names can used in final output.
8725 SegInfo sectionTable(Obj);
8726
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008727 outs() << "segment section address "
8728 "type addend symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008729 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
8730 // Strong symbols don't have a location to update.
8731 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008732 outs() << " strong "
Nick Kledzik56ebef42014-09-16 01:41:51 +00008733 << Entry.symbolName() << "\n";
8734 continue;
8735 }
8736 uint32_t SegIndex = Entry.segmentIndex();
8737 uint64_t OffsetInSeg = Entry.segmentOffset();
8738 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8739 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8740 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8741
8742 // Table lines look like:
8743 // __DATA __data 0x00001000 pointer 0 _foo
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008744 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008745 << left_justify(SectionName, 18) << " "
8746 << format_hex(Address, 10, true) << " "
8747 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008748 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
8749 << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008750 }
8751}
8752
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008753// get_dyld_bind_info_symbolname() is used for disassembly and passed an
8754// address, ReferenceValue, in the Mach-O file and looks in the dyld bind
8755// information for that address. If the address is found its binding symbol
8756// name is returned. If not nullptr is returned.
8757static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
8758 struct DisassembleInfo *info) {
Kevin Enderby078be602014-10-23 19:53:12 +00008759 if (info->bindtable == nullptr) {
8760 info->bindtable = new (BindTable);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008761 SegInfo sectionTable(info->O);
8762 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
8763 uint32_t SegIndex = Entry.segmentIndex();
8764 uint64_t OffsetInSeg = Entry.segmentOffset();
8765 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8766 const char *SymbolName = nullptr;
8767 StringRef name = Entry.symbolName();
8768 if (!name.empty())
8769 SymbolName = name.data();
Kevin Enderby078be602014-10-23 19:53:12 +00008770 info->bindtable->push_back(std::make_pair(Address, SymbolName));
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008771 }
8772 }
Kevin Enderby078be602014-10-23 19:53:12 +00008773 for (bind_table_iterator BI = info->bindtable->begin(),
8774 BE = info->bindtable->end();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008775 BI != BE; ++BI) {
8776 uint64_t Address = BI->first;
8777 if (ReferenceValue == Address) {
8778 const char *SymbolName = BI->second;
8779 return SymbolName;
8780 }
8781 }
8782 return nullptr;
8783}