blob: fa5906bb2396ce4592eca7e4830b6deabedb201f [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,
2416 uint64_t ReferenceValue = UnknownAddress) {
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 Espindolad7a32ea2015-06-24 10:20:30 +00002449 if (n_value == UnknownAddress)
Kevin Enderby0fc11822015-04-01 20:57:01 +00002450 n_value = 0;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002451 ErrorOr<StringRef> NameOrError = Symbol.getName();
2452 if (std::error_code EC = NameOrError.getError())
2453 report_fatal_error(EC.message());
2454 StringRef Name = *NameOrError;
2455 if (!Name.empty()) {
2456 SymbolName = Name.data();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002457 return SymbolName;
2458 }
2459 }
2460
2461 // TODO: For fully linked images, look through the external relocation
2462 // entries off the dynamic symtab command. For these the r_offset is from the
2463 // start of the first writeable segment in the Mach-O file. So the offset
2464 // to this section from that segment is passed to this routine by the caller,
2465 // as the database_offset. Which is the difference of the section's starting
2466 // address and the first writable segment.
2467 //
2468 // NOTE: need add passing the database_offset to this routine.
2469
Kevin Enderby0fc11822015-04-01 20:57:01 +00002470 // We did not find an external relocation entry so look up the ReferenceValue
2471 // as an address of a symbol and if found return that symbol's name.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002472 if (ReferenceValue != UnknownAddress)
Kevin Enderby0fc11822015-04-01 20:57:01 +00002473 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002474
2475 return SymbolName;
2476}
2477
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002478static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2479 DisassembleInfo *info,
2480 uint32_t ReferenceValue) {
2481 uint64_t n_value64;
2482 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2483}
2484
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002485// These are structs in the Objective-C meta data and read to produce the
2486// comments for disassembly. While these are part of the ABI they are no
2487// public defintions. So the are here not in include/llvm/Support/MachO.h .
2488
2489// The cfstring object in a 64-bit Mach-O file.
2490struct cfstring64_t {
2491 uint64_t isa; // class64_t * (64-bit pointer)
2492 uint64_t flags; // flag bits
2493 uint64_t characters; // char * (64-bit pointer)
2494 uint64_t length; // number of non-NULL characters in above
2495};
2496
2497// The class object in a 64-bit Mach-O file.
2498struct class64_t {
2499 uint64_t isa; // class64_t * (64-bit pointer)
2500 uint64_t superclass; // class64_t * (64-bit pointer)
2501 uint64_t cache; // Cache (64-bit pointer)
2502 uint64_t vtable; // IMP * (64-bit pointer)
2503 uint64_t data; // class_ro64_t * (64-bit pointer)
2504};
2505
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002506struct class32_t {
2507 uint32_t isa; /* class32_t * (32-bit pointer) */
2508 uint32_t superclass; /* class32_t * (32-bit pointer) */
2509 uint32_t cache; /* Cache (32-bit pointer) */
2510 uint32_t vtable; /* IMP * (32-bit pointer) */
2511 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2512};
2513
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002514struct class_ro64_t {
2515 uint32_t flags;
2516 uint32_t instanceStart;
2517 uint32_t instanceSize;
2518 uint32_t reserved;
2519 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2520 uint64_t name; // const char * (64-bit pointer)
2521 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2522 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2523 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2524 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2525 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2526};
2527
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002528struct class_ro32_t {
2529 uint32_t flags;
2530 uint32_t instanceStart;
2531 uint32_t instanceSize;
2532 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2533 uint32_t name; /* const char * (32-bit pointer) */
2534 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2535 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2536 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2537 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2538 uint32_t baseProperties; /* const struct objc_property_list *
2539 (32-bit pointer) */
2540};
2541
2542/* Values for class_ro{64,32}_t->flags */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002543#define RO_META (1 << 0)
2544#define RO_ROOT (1 << 1)
2545#define RO_HAS_CXX_STRUCTORS (1 << 2)
2546
2547struct method_list64_t {
2548 uint32_t entsize;
2549 uint32_t count;
2550 /* struct method64_t first; These structures follow inline */
2551};
2552
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002553struct method_list32_t {
2554 uint32_t entsize;
2555 uint32_t count;
2556 /* struct method32_t first; These structures follow inline */
2557};
2558
Kevin Enderby0fc11822015-04-01 20:57:01 +00002559struct method64_t {
2560 uint64_t name; /* SEL (64-bit pointer) */
2561 uint64_t types; /* const char * (64-bit pointer) */
2562 uint64_t imp; /* IMP (64-bit pointer) */
2563};
2564
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002565struct method32_t {
2566 uint32_t name; /* SEL (32-bit pointer) */
2567 uint32_t types; /* const char * (32-bit pointer) */
2568 uint32_t imp; /* IMP (32-bit pointer) */
2569};
2570
Kevin Enderby0fc11822015-04-01 20:57:01 +00002571struct protocol_list64_t {
2572 uint64_t count; /* uintptr_t (a 64-bit value) */
2573 /* struct protocol64_t * list[0]; These pointers follow inline */
2574};
2575
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002576struct protocol_list32_t {
2577 uint32_t count; /* uintptr_t (a 32-bit value) */
2578 /* struct protocol32_t * list[0]; These pointers follow inline */
2579};
2580
Kevin Enderby0fc11822015-04-01 20:57:01 +00002581struct protocol64_t {
2582 uint64_t isa; /* id * (64-bit pointer) */
2583 uint64_t name; /* const char * (64-bit pointer) */
2584 uint64_t protocols; /* struct protocol_list64_t *
2585 (64-bit pointer) */
2586 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2587 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2588 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2589 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2590 uint64_t instanceProperties; /* struct objc_property_list *
2591 (64-bit pointer) */
2592};
2593
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002594struct protocol32_t {
2595 uint32_t isa; /* id * (32-bit pointer) */
2596 uint32_t name; /* const char * (32-bit pointer) */
2597 uint32_t protocols; /* struct protocol_list_t *
2598 (32-bit pointer) */
2599 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2600 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2601 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2602 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2603 uint32_t instanceProperties; /* struct objc_property_list *
2604 (32-bit pointer) */
2605};
2606
Kevin Enderby0fc11822015-04-01 20:57:01 +00002607struct ivar_list64_t {
2608 uint32_t entsize;
2609 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002610 /* struct ivar64_t first; These structures follow inline */
2611};
2612
2613struct ivar_list32_t {
2614 uint32_t entsize;
2615 uint32_t count;
2616 /* struct ivar32_t first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002617};
2618
2619struct ivar64_t {
2620 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2621 uint64_t name; /* const char * (64-bit pointer) */
2622 uint64_t type; /* const char * (64-bit pointer) */
2623 uint32_t alignment;
2624 uint32_t size;
2625};
2626
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002627struct ivar32_t {
2628 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2629 uint32_t name; /* const char * (32-bit pointer) */
2630 uint32_t type; /* const char * (32-bit pointer) */
2631 uint32_t alignment;
2632 uint32_t size;
2633};
2634
Kevin Enderby0fc11822015-04-01 20:57:01 +00002635struct objc_property_list64 {
2636 uint32_t entsize;
2637 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002638 /* struct objc_property64 first; These structures follow inline */
2639};
2640
2641struct objc_property_list32 {
2642 uint32_t entsize;
2643 uint32_t count;
2644 /* struct objc_property32 first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002645};
2646
2647struct objc_property64 {
2648 uint64_t name; /* const char * (64-bit pointer) */
2649 uint64_t attributes; /* const char * (64-bit pointer) */
2650};
2651
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002652struct objc_property32 {
2653 uint32_t name; /* const char * (32-bit pointer) */
2654 uint32_t attributes; /* const char * (32-bit pointer) */
2655};
2656
Kevin Enderby0fc11822015-04-01 20:57:01 +00002657struct category64_t {
2658 uint64_t name; /* const char * (64-bit pointer) */
2659 uint64_t cls; /* struct class_t * (64-bit pointer) */
2660 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2661 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2662 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2663 uint64_t instanceProperties; /* struct objc_property_list *
2664 (64-bit pointer) */
2665};
2666
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002667struct category32_t {
2668 uint32_t name; /* const char * (32-bit pointer) */
2669 uint32_t cls; /* struct class_t * (32-bit pointer) */
2670 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2671 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2672 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2673 uint32_t instanceProperties; /* struct objc_property_list *
2674 (32-bit pointer) */
2675};
2676
Kevin Enderby0fc11822015-04-01 20:57:01 +00002677struct objc_image_info64 {
2678 uint32_t version;
2679 uint32_t flags;
2680};
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002681struct objc_image_info32 {
2682 uint32_t version;
2683 uint32_t flags;
2684};
Kevin Enderby846c0002015-04-16 17:19:59 +00002685struct imageInfo_t {
2686 uint32_t version;
2687 uint32_t flags;
2688};
Kevin Enderby0fc11822015-04-01 20:57:01 +00002689/* masks for objc_image_info.flags */
2690#define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2691#define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2692
2693struct message_ref64 {
2694 uint64_t imp; /* IMP (64-bit pointer) */
2695 uint64_t sel; /* SEL (64-bit pointer) */
2696};
2697
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002698struct message_ref32 {
2699 uint32_t imp; /* IMP (32-bit pointer) */
2700 uint32_t sel; /* SEL (32-bit pointer) */
2701};
2702
Kevin Enderby846c0002015-04-16 17:19:59 +00002703// Objective-C 1 (32-bit only) meta data structs.
2704
2705struct objc_module_t {
2706 uint32_t version;
2707 uint32_t size;
2708 uint32_t name; /* char * (32-bit pointer) */
2709 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2710};
2711
2712struct objc_symtab_t {
2713 uint32_t sel_ref_cnt;
2714 uint32_t refs; /* SEL * (32-bit pointer) */
2715 uint16_t cls_def_cnt;
2716 uint16_t cat_def_cnt;
2717 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2718};
2719
2720struct objc_class_t {
2721 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2722 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2723 uint32_t name; /* const char * (32-bit pointer) */
2724 int32_t version;
2725 int32_t info;
2726 int32_t instance_size;
2727 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2728 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2729 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2730 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2731};
2732
2733#define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2734// class is not a metaclass
2735#define CLS_CLASS 0x1
2736// class is a metaclass
2737#define CLS_META 0x2
2738
2739struct objc_category_t {
2740 uint32_t category_name; /* char * (32-bit pointer) */
2741 uint32_t class_name; /* char * (32-bit pointer) */
2742 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2743 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2744 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2745};
2746
2747struct objc_ivar_t {
2748 uint32_t ivar_name; /* char * (32-bit pointer) */
2749 uint32_t ivar_type; /* char * (32-bit pointer) */
2750 int32_t ivar_offset;
2751};
2752
2753struct objc_ivar_list_t {
2754 int32_t ivar_count;
2755 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2756};
2757
2758struct objc_method_list_t {
2759 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2760 int32_t method_count;
2761 // struct objc_method_t method_list[1]; /* variable length structure */
2762};
2763
2764struct objc_method_t {
2765 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2766 uint32_t method_types; /* char * (32-bit pointer) */
2767 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2768 (32-bit pointer) */
2769};
2770
2771struct objc_protocol_list_t {
2772 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2773 int32_t count;
2774 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2775 // (32-bit pointer) */
2776};
2777
2778struct objc_protocol_t {
2779 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2780 uint32_t protocol_name; /* char * (32-bit pointer) */
2781 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2782 uint32_t instance_methods; /* struct objc_method_description_list *
2783 (32-bit pointer) */
2784 uint32_t class_methods; /* struct objc_method_description_list *
2785 (32-bit pointer) */
2786};
2787
2788struct objc_method_description_list_t {
2789 int32_t count;
2790 // struct objc_method_description_t list[1];
2791};
2792
2793struct objc_method_description_t {
2794 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2795 uint32_t types; /* char * (32-bit pointer) */
2796};
2797
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002798inline void swapStruct(struct cfstring64_t &cfs) {
2799 sys::swapByteOrder(cfs.isa);
2800 sys::swapByteOrder(cfs.flags);
2801 sys::swapByteOrder(cfs.characters);
2802 sys::swapByteOrder(cfs.length);
2803}
2804
2805inline void swapStruct(struct class64_t &c) {
2806 sys::swapByteOrder(c.isa);
2807 sys::swapByteOrder(c.superclass);
2808 sys::swapByteOrder(c.cache);
2809 sys::swapByteOrder(c.vtable);
2810 sys::swapByteOrder(c.data);
2811}
2812
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002813inline void swapStruct(struct class32_t &c) {
2814 sys::swapByteOrder(c.isa);
2815 sys::swapByteOrder(c.superclass);
2816 sys::swapByteOrder(c.cache);
2817 sys::swapByteOrder(c.vtable);
2818 sys::swapByteOrder(c.data);
2819}
2820
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002821inline void swapStruct(struct class_ro64_t &cro) {
2822 sys::swapByteOrder(cro.flags);
2823 sys::swapByteOrder(cro.instanceStart);
2824 sys::swapByteOrder(cro.instanceSize);
2825 sys::swapByteOrder(cro.reserved);
2826 sys::swapByteOrder(cro.ivarLayout);
2827 sys::swapByteOrder(cro.name);
2828 sys::swapByteOrder(cro.baseMethods);
2829 sys::swapByteOrder(cro.baseProtocols);
2830 sys::swapByteOrder(cro.ivars);
2831 sys::swapByteOrder(cro.weakIvarLayout);
2832 sys::swapByteOrder(cro.baseProperties);
2833}
2834
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002835inline void swapStruct(struct class_ro32_t &cro) {
2836 sys::swapByteOrder(cro.flags);
2837 sys::swapByteOrder(cro.instanceStart);
2838 sys::swapByteOrder(cro.instanceSize);
2839 sys::swapByteOrder(cro.ivarLayout);
2840 sys::swapByteOrder(cro.name);
2841 sys::swapByteOrder(cro.baseMethods);
2842 sys::swapByteOrder(cro.baseProtocols);
2843 sys::swapByteOrder(cro.ivars);
2844 sys::swapByteOrder(cro.weakIvarLayout);
2845 sys::swapByteOrder(cro.baseProperties);
2846}
2847
Kevin Enderby0fc11822015-04-01 20:57:01 +00002848inline void swapStruct(struct method_list64_t &ml) {
2849 sys::swapByteOrder(ml.entsize);
2850 sys::swapByteOrder(ml.count);
2851}
2852
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002853inline void swapStruct(struct method_list32_t &ml) {
2854 sys::swapByteOrder(ml.entsize);
2855 sys::swapByteOrder(ml.count);
2856}
2857
Kevin Enderby0fc11822015-04-01 20:57:01 +00002858inline void swapStruct(struct method64_t &m) {
2859 sys::swapByteOrder(m.name);
2860 sys::swapByteOrder(m.types);
2861 sys::swapByteOrder(m.imp);
2862}
2863
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002864inline void swapStruct(struct method32_t &m) {
2865 sys::swapByteOrder(m.name);
2866 sys::swapByteOrder(m.types);
2867 sys::swapByteOrder(m.imp);
2868}
2869
Kevin Enderby0fc11822015-04-01 20:57:01 +00002870inline void swapStruct(struct protocol_list64_t &pl) {
2871 sys::swapByteOrder(pl.count);
2872}
2873
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002874inline void swapStruct(struct protocol_list32_t &pl) {
2875 sys::swapByteOrder(pl.count);
2876}
2877
Kevin Enderby0fc11822015-04-01 20:57:01 +00002878inline void swapStruct(struct protocol64_t &p) {
2879 sys::swapByteOrder(p.isa);
2880 sys::swapByteOrder(p.name);
2881 sys::swapByteOrder(p.protocols);
2882 sys::swapByteOrder(p.instanceMethods);
2883 sys::swapByteOrder(p.classMethods);
2884 sys::swapByteOrder(p.optionalInstanceMethods);
2885 sys::swapByteOrder(p.optionalClassMethods);
2886 sys::swapByteOrder(p.instanceProperties);
2887}
2888
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002889inline void swapStruct(struct protocol32_t &p) {
2890 sys::swapByteOrder(p.isa);
2891 sys::swapByteOrder(p.name);
2892 sys::swapByteOrder(p.protocols);
2893 sys::swapByteOrder(p.instanceMethods);
2894 sys::swapByteOrder(p.classMethods);
2895 sys::swapByteOrder(p.optionalInstanceMethods);
2896 sys::swapByteOrder(p.optionalClassMethods);
2897 sys::swapByteOrder(p.instanceProperties);
2898}
2899
Kevin Enderby0fc11822015-04-01 20:57:01 +00002900inline void swapStruct(struct ivar_list64_t &il) {
2901 sys::swapByteOrder(il.entsize);
2902 sys::swapByteOrder(il.count);
2903}
2904
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002905inline void swapStruct(struct ivar_list32_t &il) {
2906 sys::swapByteOrder(il.entsize);
2907 sys::swapByteOrder(il.count);
2908}
2909
Kevin Enderby0fc11822015-04-01 20:57:01 +00002910inline void swapStruct(struct ivar64_t &i) {
2911 sys::swapByteOrder(i.offset);
2912 sys::swapByteOrder(i.name);
2913 sys::swapByteOrder(i.type);
2914 sys::swapByteOrder(i.alignment);
2915 sys::swapByteOrder(i.size);
2916}
2917
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002918inline void swapStruct(struct ivar32_t &i) {
2919 sys::swapByteOrder(i.offset);
2920 sys::swapByteOrder(i.name);
2921 sys::swapByteOrder(i.type);
2922 sys::swapByteOrder(i.alignment);
2923 sys::swapByteOrder(i.size);
2924}
2925
Kevin Enderby0fc11822015-04-01 20:57:01 +00002926inline void swapStruct(struct objc_property_list64 &pl) {
2927 sys::swapByteOrder(pl.entsize);
2928 sys::swapByteOrder(pl.count);
2929}
2930
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002931inline void swapStruct(struct objc_property_list32 &pl) {
2932 sys::swapByteOrder(pl.entsize);
2933 sys::swapByteOrder(pl.count);
2934}
2935
Kevin Enderby0fc11822015-04-01 20:57:01 +00002936inline void swapStruct(struct objc_property64 &op) {
2937 sys::swapByteOrder(op.name);
2938 sys::swapByteOrder(op.attributes);
2939}
2940
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002941inline void swapStruct(struct objc_property32 &op) {
2942 sys::swapByteOrder(op.name);
2943 sys::swapByteOrder(op.attributes);
2944}
2945
Kevin Enderby0fc11822015-04-01 20:57:01 +00002946inline void swapStruct(struct category64_t &c) {
2947 sys::swapByteOrder(c.name);
2948 sys::swapByteOrder(c.cls);
2949 sys::swapByteOrder(c.instanceMethods);
2950 sys::swapByteOrder(c.classMethods);
2951 sys::swapByteOrder(c.protocols);
2952 sys::swapByteOrder(c.instanceProperties);
2953}
2954
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002955inline void swapStruct(struct category32_t &c) {
2956 sys::swapByteOrder(c.name);
2957 sys::swapByteOrder(c.cls);
2958 sys::swapByteOrder(c.instanceMethods);
2959 sys::swapByteOrder(c.classMethods);
2960 sys::swapByteOrder(c.protocols);
2961 sys::swapByteOrder(c.instanceProperties);
2962}
2963
Kevin Enderby0fc11822015-04-01 20:57:01 +00002964inline void swapStruct(struct objc_image_info64 &o) {
2965 sys::swapByteOrder(o.version);
2966 sys::swapByteOrder(o.flags);
2967}
2968
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002969inline void swapStruct(struct objc_image_info32 &o) {
2970 sys::swapByteOrder(o.version);
2971 sys::swapByteOrder(o.flags);
2972}
2973
Kevin Enderby846c0002015-04-16 17:19:59 +00002974inline void swapStruct(struct imageInfo_t &o) {
2975 sys::swapByteOrder(o.version);
2976 sys::swapByteOrder(o.flags);
2977}
2978
Kevin Enderby0fc11822015-04-01 20:57:01 +00002979inline void swapStruct(struct message_ref64 &mr) {
2980 sys::swapByteOrder(mr.imp);
2981 sys::swapByteOrder(mr.sel);
2982}
2983
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002984inline void swapStruct(struct message_ref32 &mr) {
2985 sys::swapByteOrder(mr.imp);
2986 sys::swapByteOrder(mr.sel);
2987}
2988
Kevin Enderby846c0002015-04-16 17:19:59 +00002989inline void swapStruct(struct objc_module_t &module) {
2990 sys::swapByteOrder(module.version);
2991 sys::swapByteOrder(module.size);
2992 sys::swapByteOrder(module.name);
2993 sys::swapByteOrder(module.symtab);
Jingyue Wufedecc42015-04-16 18:43:44 +00002994}
Kevin Enderby846c0002015-04-16 17:19:59 +00002995
2996inline void swapStruct(struct objc_symtab_t &symtab) {
2997 sys::swapByteOrder(symtab.sel_ref_cnt);
2998 sys::swapByteOrder(symtab.refs);
2999 sys::swapByteOrder(symtab.cls_def_cnt);
3000 sys::swapByteOrder(symtab.cat_def_cnt);
Jingyue Wufedecc42015-04-16 18:43:44 +00003001}
Kevin Enderby846c0002015-04-16 17:19:59 +00003002
3003inline void swapStruct(struct objc_class_t &objc_class) {
3004 sys::swapByteOrder(objc_class.isa);
3005 sys::swapByteOrder(objc_class.super_class);
3006 sys::swapByteOrder(objc_class.name);
3007 sys::swapByteOrder(objc_class.version);
3008 sys::swapByteOrder(objc_class.info);
3009 sys::swapByteOrder(objc_class.instance_size);
3010 sys::swapByteOrder(objc_class.ivars);
3011 sys::swapByteOrder(objc_class.methodLists);
3012 sys::swapByteOrder(objc_class.cache);
3013 sys::swapByteOrder(objc_class.protocols);
Jingyue Wufedecc42015-04-16 18:43:44 +00003014}
Kevin Enderby846c0002015-04-16 17:19:59 +00003015
3016inline void swapStruct(struct objc_category_t &objc_category) {
3017 sys::swapByteOrder(objc_category.category_name);
3018 sys::swapByteOrder(objc_category.class_name);
3019 sys::swapByteOrder(objc_category.instance_methods);
3020 sys::swapByteOrder(objc_category.class_methods);
3021 sys::swapByteOrder(objc_category.protocols);
3022}
3023
3024inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3025 sys::swapByteOrder(objc_ivar_list.ivar_count);
3026}
3027
3028inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3029 sys::swapByteOrder(objc_ivar.ivar_name);
3030 sys::swapByteOrder(objc_ivar.ivar_type);
3031 sys::swapByteOrder(objc_ivar.ivar_offset);
Jingyue Wufedecc42015-04-16 18:43:44 +00003032}
Kevin Enderby846c0002015-04-16 17:19:59 +00003033
3034inline void swapStruct(struct objc_method_list_t &method_list) {
3035 sys::swapByteOrder(method_list.obsolete);
3036 sys::swapByteOrder(method_list.method_count);
3037}
3038
3039inline void swapStruct(struct objc_method_t &method) {
3040 sys::swapByteOrder(method.method_name);
3041 sys::swapByteOrder(method.method_types);
3042 sys::swapByteOrder(method.method_imp);
3043}
3044
3045inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3046 sys::swapByteOrder(protocol_list.next);
3047 sys::swapByteOrder(protocol_list.count);
3048}
3049
3050inline void swapStruct(struct objc_protocol_t &protocol) {
3051 sys::swapByteOrder(protocol.isa);
3052 sys::swapByteOrder(protocol.protocol_name);
3053 sys::swapByteOrder(protocol.protocol_list);
3054 sys::swapByteOrder(protocol.instance_methods);
3055 sys::swapByteOrder(protocol.class_methods);
3056}
3057
3058inline void swapStruct(struct objc_method_description_list_t &mdl) {
3059 sys::swapByteOrder(mdl.count);
3060}
3061
3062inline void swapStruct(struct objc_method_description_t &md) {
3063 sys::swapByteOrder(md.name);
3064 sys::swapByteOrder(md.types);
3065}
3066
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003067static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3068 struct DisassembleInfo *info);
3069
3070// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3071// to an Objective-C class and returns the class name. It is also passed the
3072// address of the pointer, so when the pointer is zero as it can be in an .o
3073// file, that is used to look for an external relocation entry with a symbol
3074// name.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003075static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3076 uint64_t ReferenceValue,
3077 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003078 const char *r;
3079 uint32_t offset, left;
3080 SectionRef S;
3081
3082 // The pointer_value can be 0 in an object file and have a relocation
3083 // entry for the class symbol at the ReferenceValue (the address of the
3084 // pointer).
3085 if (pointer_value == 0) {
3086 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3087 if (r == nullptr || left < sizeof(uint64_t))
3088 return nullptr;
3089 uint64_t n_value;
3090 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3091 if (symbol_name == nullptr)
3092 return nullptr;
Hans Wennborgdb53e302014-10-23 21:59:17 +00003093 const char *class_name = strrchr(symbol_name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003094 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3095 return class_name + 2;
3096 else
3097 return nullptr;
3098 }
3099
3100 // The case were the pointer_value is non-zero and points to a class defined
3101 // in this Mach-O file.
3102 r = get_pointer_64(pointer_value, offset, left, S, info);
3103 if (r == nullptr || left < sizeof(struct class64_t))
3104 return nullptr;
3105 struct class64_t c;
3106 memcpy(&c, r, sizeof(struct class64_t));
3107 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3108 swapStruct(c);
3109 if (c.data == 0)
3110 return nullptr;
3111 r = get_pointer_64(c.data, offset, left, S, info);
3112 if (r == nullptr || left < sizeof(struct class_ro64_t))
3113 return nullptr;
3114 struct class_ro64_t cro;
3115 memcpy(&cro, r, sizeof(struct class_ro64_t));
3116 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3117 swapStruct(cro);
3118 if (cro.name == 0)
3119 return nullptr;
3120 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3121 return name;
3122}
3123
3124// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3125// pointer to a cfstring and returns its name or nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003126static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3127 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003128 const char *r, *name;
3129 uint32_t offset, left;
3130 SectionRef S;
3131 struct cfstring64_t cfs;
3132 uint64_t cfs_characters;
3133
3134 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3135 if (r == nullptr || left < sizeof(struct cfstring64_t))
3136 return nullptr;
3137 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3138 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3139 swapStruct(cfs);
3140 if (cfs.characters == 0) {
3141 uint64_t n_value;
3142 const char *symbol_name = get_symbol_64(
3143 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3144 if (symbol_name == nullptr)
3145 return nullptr;
3146 cfs_characters = n_value;
3147 } else
3148 cfs_characters = cfs.characters;
3149 name = get_pointer_64(cfs_characters, offset, left, S, info);
3150
3151 return name;
3152}
3153
3154// get_objc2_64bit_selref() is used for disassembly and is passed a the address
3155// of a pointer to an Objective-C selector reference when the pointer value is
3156// zero as in a .o file and is likely to have a external relocation entry with
3157// who's symbol's n_value is the real pointer to the selector name. If that is
3158// the case the real pointer to the selector name is returned else 0 is
3159// returned
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003160static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3161 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003162 uint32_t offset, left;
3163 SectionRef S;
3164
3165 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3166 if (r == nullptr || left < sizeof(uint64_t))
3167 return 0;
3168 uint64_t n_value;
3169 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3170 if (symbol_name == nullptr)
3171 return 0;
3172 return n_value;
3173}
3174
Kevin Enderby0fc11822015-04-01 20:57:01 +00003175static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3176 const char *sectname) {
3177 for (const SectionRef &Section : O->sections()) {
3178 StringRef SectName;
3179 Section.getName(SectName);
3180 DataRefImpl Ref = Section.getRawDataRefImpl();
3181 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3182 if (SegName == segname && SectName == sectname)
3183 return Section;
3184 }
3185 return SectionRef();
3186}
3187
3188static void
3189walk_pointer_list_64(const char *listname, const SectionRef S,
3190 MachOObjectFile *O, struct DisassembleInfo *info,
3191 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3192 if (S == SectionRef())
3193 return;
3194
3195 StringRef SectName;
3196 S.getName(SectName);
3197 DataRefImpl Ref = S.getRawDataRefImpl();
3198 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3199 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3200
3201 StringRef BytesStr;
3202 S.getContents(BytesStr);
3203 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3204
3205 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3206 uint32_t left = S.getSize() - i;
3207 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3208 uint64_t p = 0;
3209 memcpy(&p, Contents + i, size);
3210 if (i + sizeof(uint64_t) > S.getSize())
3211 outs() << listname << " list pointer extends past end of (" << SegName
3212 << "," << SectName << ") section\n";
3213 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3214
3215 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3216 sys::swapByteOrder(p);
3217
3218 uint64_t n_value = 0;
3219 const char *name = get_symbol_64(i, S, info, n_value, p);
3220 if (name == nullptr)
3221 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3222
3223 if (n_value != 0) {
3224 outs() << format("0x%" PRIx64, n_value);
3225 if (p != 0)
3226 outs() << " + " << format("0x%" PRIx64, p);
3227 } else
3228 outs() << format("0x%" PRIx64, p);
3229 if (name != nullptr)
3230 outs() << " " << name;
3231 outs() << "\n";
3232
3233 p += n_value;
3234 if (func)
3235 func(p, info);
3236 }
3237}
3238
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003239static void
3240walk_pointer_list_32(const char *listname, const SectionRef S,
3241 MachOObjectFile *O, struct DisassembleInfo *info,
3242 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3243 if (S == SectionRef())
3244 return;
3245
3246 StringRef SectName;
3247 S.getName(SectName);
3248 DataRefImpl Ref = S.getRawDataRefImpl();
3249 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3250 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3251
3252 StringRef BytesStr;
3253 S.getContents(BytesStr);
3254 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3255
3256 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3257 uint32_t left = S.getSize() - i;
3258 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3259 uint32_t p = 0;
3260 memcpy(&p, Contents + i, size);
3261 if (i + sizeof(uint32_t) > S.getSize())
3262 outs() << listname << " list pointer extends past end of (" << SegName
3263 << "," << SectName << ") section\n";
Kevin Enderbycf261312015-04-06 22:33:43 +00003264 uint32_t Address = S.getAddress() + i;
3265 outs() << format("%08" PRIx32, Address) << " ";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003266
3267 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3268 sys::swapByteOrder(p);
3269 outs() << format("0x%" PRIx32, p);
3270
3271 const char *name = get_symbol_32(i, S, info, p);
3272 if (name != nullptr)
3273 outs() << " " << name;
3274 outs() << "\n";
3275
3276 if (func)
3277 func(p, info);
3278 }
3279}
3280
3281static void print_layout_map(const char *layout_map, uint32_t left) {
3282 outs() << " layout map: ";
3283 do {
3284 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3285 left--;
3286 layout_map++;
3287 } while (*layout_map != '\0' && left != 0);
3288 outs() << "\n";
3289}
3290
Kevin Enderby0fc11822015-04-01 20:57:01 +00003291static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3292 uint32_t offset, left;
3293 SectionRef S;
3294 const char *layout_map;
3295
3296 if (p == 0)
3297 return;
3298 layout_map = get_pointer_64(p, offset, left, S, info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003299 print_layout_map(layout_map, left);
3300}
3301
3302static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3303 uint32_t offset, left;
3304 SectionRef S;
3305 const char *layout_map;
3306
3307 if (p == 0)
3308 return;
3309 layout_map = get_pointer_32(p, offset, left, S, info);
3310 print_layout_map(layout_map, left);
Kevin Enderby0fc11822015-04-01 20:57:01 +00003311}
3312
3313static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3314 const char *indent) {
3315 struct method_list64_t ml;
3316 struct method64_t m;
3317 const char *r;
3318 uint32_t offset, xoffset, left, i;
3319 SectionRef S, xS;
3320 const char *name, *sym_name;
3321 uint64_t n_value;
3322
3323 r = get_pointer_64(p, offset, left, S, info);
3324 if (r == nullptr)
3325 return;
3326 memset(&ml, '\0', sizeof(struct method_list64_t));
3327 if (left < sizeof(struct method_list64_t)) {
3328 memcpy(&ml, r, left);
3329 outs() << " (method_list_t entends past the end of the section)\n";
3330 } else
3331 memcpy(&ml, r, sizeof(struct method_list64_t));
3332 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3333 swapStruct(ml);
3334 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3335 outs() << indent << "\t\t count " << ml.count << "\n";
3336
3337 p += sizeof(struct method_list64_t);
3338 offset += sizeof(struct method_list64_t);
3339 for (i = 0; i < ml.count; i++) {
3340 r = get_pointer_64(p, offset, left, S, info);
3341 if (r == nullptr)
3342 return;
3343 memset(&m, '\0', sizeof(struct method64_t));
3344 if (left < sizeof(struct method64_t)) {
3345 memcpy(&ml, r, left);
3346 outs() << indent << " (method_t entends past the end of the section)\n";
3347 } else
3348 memcpy(&m, r, sizeof(struct method64_t));
3349 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3350 swapStruct(m);
3351
3352 outs() << indent << "\t\t name ";
3353 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3354 info, n_value, m.name);
3355 if (n_value != 0) {
3356 if (info->verbose && sym_name != nullptr)
3357 outs() << sym_name;
3358 else
3359 outs() << format("0x%" PRIx64, n_value);
3360 if (m.name != 0)
3361 outs() << " + " << format("0x%" PRIx64, m.name);
3362 } else
3363 outs() << format("0x%" PRIx64, m.name);
3364 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3365 if (name != nullptr)
3366 outs() << format(" %.*s", left, name);
3367 outs() << "\n";
3368
3369 outs() << indent << "\t\t types ";
3370 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3371 info, n_value, m.types);
3372 if (n_value != 0) {
3373 if (info->verbose && sym_name != nullptr)
3374 outs() << sym_name;
3375 else
3376 outs() << format("0x%" PRIx64, n_value);
3377 if (m.types != 0)
3378 outs() << " + " << format("0x%" PRIx64, m.types);
3379 } else
3380 outs() << format("0x%" PRIx64, m.types);
3381 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3382 if (name != nullptr)
3383 outs() << format(" %.*s", left, name);
3384 outs() << "\n";
3385
3386 outs() << indent << "\t\t imp ";
3387 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3388 n_value, m.imp);
3389 if (info->verbose && name == nullptr) {
3390 if (n_value != 0) {
3391 outs() << format("0x%" PRIx64, n_value) << " ";
3392 if (m.imp != 0)
3393 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3394 } else
3395 outs() << format("0x%" PRIx64, m.imp) << " ";
3396 }
3397 if (name != nullptr)
3398 outs() << name;
3399 outs() << "\n";
3400
3401 p += sizeof(struct method64_t);
3402 offset += sizeof(struct method64_t);
3403 }
3404}
3405
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003406static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3407 const char *indent) {
3408 struct method_list32_t ml;
3409 struct method32_t m;
Kevin Enderby846c0002015-04-16 17:19:59 +00003410 const char *r, *name;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003411 uint32_t offset, xoffset, left, i;
3412 SectionRef S, xS;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003413
3414 r = get_pointer_32(p, offset, left, S, info);
3415 if (r == nullptr)
3416 return;
3417 memset(&ml, '\0', sizeof(struct method_list32_t));
3418 if (left < sizeof(struct method_list32_t)) {
3419 memcpy(&ml, r, left);
3420 outs() << " (method_list_t entends past the end of the section)\n";
3421 } else
3422 memcpy(&ml, r, sizeof(struct method_list32_t));
3423 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3424 swapStruct(ml);
3425 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3426 outs() << indent << "\t\t count " << ml.count << "\n";
3427
3428 p += sizeof(struct method_list32_t);
3429 offset += sizeof(struct method_list32_t);
3430 for (i = 0; i < ml.count; i++) {
3431 r = get_pointer_32(p, offset, left, S, info);
3432 if (r == nullptr)
3433 return;
3434 memset(&m, '\0', sizeof(struct method32_t));
3435 if (left < sizeof(struct method32_t)) {
3436 memcpy(&ml, r, left);
3437 outs() << indent << " (method_t entends past the end of the section)\n";
3438 } else
3439 memcpy(&m, r, sizeof(struct method32_t));
3440 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3441 swapStruct(m);
3442
3443 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3444 name = get_pointer_32(m.name, xoffset, left, xS, info);
3445 if (name != nullptr)
3446 outs() << format(" %.*s", left, name);
3447 outs() << "\n";
3448
3449 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3450 name = get_pointer_32(m.types, xoffset, left, xS, info);
3451 if (name != nullptr)
3452 outs() << format(" %.*s", left, name);
3453 outs() << "\n";
3454
3455 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3456 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3457 m.imp);
3458 if (name != nullptr)
3459 outs() << " " << name;
3460 outs() << "\n";
3461
3462 p += sizeof(struct method32_t);
3463 offset += sizeof(struct method32_t);
3464 }
3465}
3466
Kevin Enderby846c0002015-04-16 17:19:59 +00003467static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3468 uint32_t offset, left, xleft;
3469 SectionRef S;
3470 struct objc_method_list_t method_list;
3471 struct objc_method_t method;
3472 const char *r, *methods, *name, *SymbolName;
3473 int32_t i;
3474
3475 r = get_pointer_32(p, offset, left, S, info, true);
3476 if (r == nullptr)
3477 return true;
3478
3479 outs() << "\n";
3480 if (left > sizeof(struct objc_method_list_t)) {
3481 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3482 } else {
3483 outs() << "\t\t objc_method_list extends past end of the section\n";
3484 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3485 memcpy(&method_list, r, left);
3486 }
3487 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3488 swapStruct(method_list);
3489
3490 outs() << "\t\t obsolete "
3491 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3492 outs() << "\t\t method_count " << method_list.method_count << "\n";
3493
3494 methods = r + sizeof(struct objc_method_list_t);
3495 for (i = 0; i < method_list.method_count; i++) {
3496 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3497 outs() << "\t\t remaining method's extend past the of the section\n";
3498 break;
3499 }
3500 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3501 sizeof(struct objc_method_t));
3502 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3503 swapStruct(method);
3504
3505 outs() << "\t\t method_name "
3506 << format("0x%08" PRIx32, method.method_name);
3507 if (info->verbose) {
3508 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3509 if (name != nullptr)
3510 outs() << format(" %.*s", xleft, name);
3511 else
3512 outs() << " (not in an __OBJC section)";
3513 }
3514 outs() << "\n";
3515
3516 outs() << "\t\t method_types "
3517 << format("0x%08" PRIx32, method.method_types);
3518 if (info->verbose) {
3519 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3520 if (name != nullptr)
3521 outs() << format(" %.*s", xleft, name);
3522 else
3523 outs() << " (not in an __OBJC section)";
3524 }
3525 outs() << "\n";
3526
3527 outs() << "\t\t method_imp "
3528 << format("0x%08" PRIx32, method.method_imp) << " ";
3529 if (info->verbose) {
3530 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3531 if (SymbolName != nullptr)
3532 outs() << SymbolName;
3533 }
3534 outs() << "\n";
3535 }
3536 return false;
3537}
3538
Kevin Enderby0fc11822015-04-01 20:57:01 +00003539static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3540 struct protocol_list64_t pl;
3541 uint64_t q, n_value;
3542 struct protocol64_t pc;
3543 const char *r;
3544 uint32_t offset, xoffset, left, i;
3545 SectionRef S, xS;
3546 const char *name, *sym_name;
3547
3548 r = get_pointer_64(p, offset, left, S, info);
3549 if (r == nullptr)
3550 return;
3551 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3552 if (left < sizeof(struct protocol_list64_t)) {
3553 memcpy(&pl, r, left);
3554 outs() << " (protocol_list_t entends past the end of the section)\n";
3555 } else
3556 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3557 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3558 swapStruct(pl);
3559 outs() << " count " << pl.count << "\n";
3560
3561 p += sizeof(struct protocol_list64_t);
3562 offset += sizeof(struct protocol_list64_t);
3563 for (i = 0; i < pl.count; i++) {
3564 r = get_pointer_64(p, offset, left, S, info);
3565 if (r == nullptr)
3566 return;
3567 q = 0;
3568 if (left < sizeof(uint64_t)) {
3569 memcpy(&q, r, left);
3570 outs() << " (protocol_t * entends past the end of the section)\n";
3571 } else
3572 memcpy(&q, r, sizeof(uint64_t));
3573 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3574 sys::swapByteOrder(q);
3575
3576 outs() << "\t\t list[" << i << "] ";
3577 sym_name = get_symbol_64(offset, S, info, n_value, q);
3578 if (n_value != 0) {
3579 if (info->verbose && sym_name != nullptr)
3580 outs() << sym_name;
3581 else
3582 outs() << format("0x%" PRIx64, n_value);
3583 if (q != 0)
3584 outs() << " + " << format("0x%" PRIx64, q);
3585 } else
3586 outs() << format("0x%" PRIx64, q);
3587 outs() << " (struct protocol_t *)\n";
3588
3589 r = get_pointer_64(q + n_value, offset, left, S, info);
3590 if (r == nullptr)
3591 return;
3592 memset(&pc, '\0', sizeof(struct protocol64_t));
3593 if (left < sizeof(struct protocol64_t)) {
3594 memcpy(&pc, r, left);
3595 outs() << " (protocol_t entends past the end of the section)\n";
3596 } else
3597 memcpy(&pc, r, sizeof(struct protocol64_t));
3598 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3599 swapStruct(pc);
3600
3601 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3602
3603 outs() << "\t\t\t name ";
3604 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3605 info, n_value, pc.name);
3606 if (n_value != 0) {
3607 if (info->verbose && sym_name != nullptr)
3608 outs() << sym_name;
3609 else
3610 outs() << format("0x%" PRIx64, n_value);
3611 if (pc.name != 0)
3612 outs() << " + " << format("0x%" PRIx64, pc.name);
3613 } else
3614 outs() << format("0x%" PRIx64, pc.name);
3615 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3616 if (name != nullptr)
3617 outs() << format(" %.*s", left, name);
3618 outs() << "\n";
3619
3620 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3621
3622 outs() << "\t\t instanceMethods ";
3623 sym_name =
3624 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3625 S, info, n_value, pc.instanceMethods);
3626 if (n_value != 0) {
3627 if (info->verbose && sym_name != nullptr)
3628 outs() << sym_name;
3629 else
3630 outs() << format("0x%" PRIx64, n_value);
3631 if (pc.instanceMethods != 0)
3632 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3633 } else
3634 outs() << format("0x%" PRIx64, pc.instanceMethods);
3635 outs() << " (struct method_list_t *)\n";
3636 if (pc.instanceMethods + n_value != 0)
3637 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3638
3639 outs() << "\t\t classMethods ";
3640 sym_name =
3641 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3642 info, n_value, pc.classMethods);
3643 if (n_value != 0) {
3644 if (info->verbose && sym_name != nullptr)
3645 outs() << sym_name;
3646 else
3647 outs() << format("0x%" PRIx64, n_value);
3648 if (pc.classMethods != 0)
3649 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3650 } else
3651 outs() << format("0x%" PRIx64, pc.classMethods);
3652 outs() << " (struct method_list_t *)\n";
3653 if (pc.classMethods + n_value != 0)
3654 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3655
3656 outs() << "\t optionalInstanceMethods "
3657 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3658 outs() << "\t optionalClassMethods "
3659 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3660 outs() << "\t instanceProperties "
3661 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3662
3663 p += sizeof(uint64_t);
3664 offset += sizeof(uint64_t);
3665 }
3666}
3667
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003668static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3669 struct protocol_list32_t pl;
3670 uint32_t q;
3671 struct protocol32_t pc;
3672 const char *r;
3673 uint32_t offset, xoffset, left, i;
3674 SectionRef S, xS;
3675 const char *name;
3676
3677 r = get_pointer_32(p, offset, left, S, info);
3678 if (r == nullptr)
3679 return;
3680 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3681 if (left < sizeof(struct protocol_list32_t)) {
3682 memcpy(&pl, r, left);
3683 outs() << " (protocol_list_t entends past the end of the section)\n";
3684 } else
3685 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3686 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3687 swapStruct(pl);
3688 outs() << " count " << pl.count << "\n";
3689
3690 p += sizeof(struct protocol_list32_t);
3691 offset += sizeof(struct protocol_list32_t);
3692 for (i = 0; i < pl.count; i++) {
3693 r = get_pointer_32(p, offset, left, S, info);
3694 if (r == nullptr)
3695 return;
3696 q = 0;
3697 if (left < sizeof(uint32_t)) {
3698 memcpy(&q, r, left);
3699 outs() << " (protocol_t * entends past the end of the section)\n";
3700 } else
3701 memcpy(&q, r, sizeof(uint32_t));
3702 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3703 sys::swapByteOrder(q);
3704 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3705 << " (struct protocol_t *)\n";
3706 r = get_pointer_32(q, offset, left, S, info);
3707 if (r == nullptr)
3708 return;
3709 memset(&pc, '\0', sizeof(struct protocol32_t));
3710 if (left < sizeof(struct protocol32_t)) {
3711 memcpy(&pc, r, left);
3712 outs() << " (protocol_t entends past the end of the section)\n";
3713 } else
3714 memcpy(&pc, r, sizeof(struct protocol32_t));
3715 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3716 swapStruct(pc);
3717 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3718 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3719 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3720 if (name != nullptr)
3721 outs() << format(" %.*s", left, name);
3722 outs() << "\n";
3723 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3724 outs() << "\t\t instanceMethods "
3725 << format("0x%" PRIx32, pc.instanceMethods)
3726 << " (struct method_list_t *)\n";
3727 if (pc.instanceMethods != 0)
3728 print_method_list32_t(pc.instanceMethods, info, "\t");
3729 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3730 << " (struct method_list_t *)\n";
3731 if (pc.classMethods != 0)
3732 print_method_list32_t(pc.classMethods, info, "\t");
3733 outs() << "\t optionalInstanceMethods "
3734 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3735 outs() << "\t optionalClassMethods "
3736 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3737 outs() << "\t instanceProperties "
3738 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3739 p += sizeof(uint32_t);
3740 offset += sizeof(uint32_t);
3741 }
3742}
3743
Kevin Enderby846c0002015-04-16 17:19:59 +00003744static void print_indent(uint32_t indent) {
3745 for (uint32_t i = 0; i < indent;) {
3746 if (indent - i >= 8) {
3747 outs() << "\t";
3748 i += 8;
3749 } else {
3750 for (uint32_t j = i; j < indent; j++)
3751 outs() << " ";
3752 return;
3753 }
3754 }
3755}
3756
3757static bool print_method_description_list(uint32_t p, uint32_t indent,
3758 struct DisassembleInfo *info) {
3759 uint32_t offset, left, xleft;
3760 SectionRef S;
3761 struct objc_method_description_list_t mdl;
3762 struct objc_method_description_t md;
3763 const char *r, *list, *name;
3764 int32_t i;
3765
3766 r = get_pointer_32(p, offset, left, S, info, true);
3767 if (r == nullptr)
3768 return true;
3769
3770 outs() << "\n";
3771 if (left > sizeof(struct objc_method_description_list_t)) {
3772 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3773 } else {
3774 print_indent(indent);
3775 outs() << " objc_method_description_list extends past end of the section\n";
3776 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3777 memcpy(&mdl, r, left);
3778 }
3779 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3780 swapStruct(mdl);
3781
3782 print_indent(indent);
3783 outs() << " count " << mdl.count << "\n";
3784
3785 list = r + sizeof(struct objc_method_description_list_t);
3786 for (i = 0; i < mdl.count; i++) {
3787 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3788 print_indent(indent);
3789 outs() << " remaining list entries extend past the of the section\n";
3790 break;
3791 }
3792 print_indent(indent);
3793 outs() << " list[" << i << "]\n";
3794 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3795 sizeof(struct objc_method_description_t));
3796 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3797 swapStruct(md);
3798
3799 print_indent(indent);
3800 outs() << " name " << format("0x%08" PRIx32, md.name);
3801 if (info->verbose) {
3802 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3803 if (name != nullptr)
3804 outs() << format(" %.*s", xleft, name);
3805 else
3806 outs() << " (not in an __OBJC section)";
3807 }
3808 outs() << "\n";
3809
3810 print_indent(indent);
3811 outs() << " types " << format("0x%08" PRIx32, md.types);
3812 if (info->verbose) {
3813 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3814 if (name != nullptr)
3815 outs() << format(" %.*s", xleft, name);
3816 else
3817 outs() << " (not in an __OBJC section)";
3818 }
3819 outs() << "\n";
3820 }
3821 return false;
3822}
3823
3824static bool print_protocol_list(uint32_t p, uint32_t indent,
3825 struct DisassembleInfo *info);
3826
3827static bool print_protocol(uint32_t p, uint32_t indent,
3828 struct DisassembleInfo *info) {
3829 uint32_t offset, left;
3830 SectionRef S;
3831 struct objc_protocol_t protocol;
3832 const char *r, *name;
3833
3834 r = get_pointer_32(p, offset, left, S, info, true);
3835 if (r == nullptr)
3836 return true;
3837
3838 outs() << "\n";
3839 if (left >= sizeof(struct objc_protocol_t)) {
3840 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3841 } else {
3842 print_indent(indent);
3843 outs() << " Protocol extends past end of the section\n";
3844 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3845 memcpy(&protocol, r, left);
3846 }
3847 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3848 swapStruct(protocol);
3849
3850 print_indent(indent);
3851 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3852 << "\n";
3853
3854 print_indent(indent);
3855 outs() << " protocol_name "
3856 << format("0x%08" PRIx32, protocol.protocol_name);
3857 if (info->verbose) {
3858 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3859 if (name != nullptr)
3860 outs() << format(" %.*s", left, name);
3861 else
3862 outs() << " (not in an __OBJC section)";
3863 }
3864 outs() << "\n";
3865
3866 print_indent(indent);
3867 outs() << " protocol_list "
3868 << format("0x%08" PRIx32, protocol.protocol_list);
3869 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3870 outs() << " (not in an __OBJC section)\n";
3871
3872 print_indent(indent);
3873 outs() << " instance_methods "
3874 << format("0x%08" PRIx32, protocol.instance_methods);
3875 if (print_method_description_list(protocol.instance_methods, indent, info))
3876 outs() << " (not in an __OBJC section)\n";
3877
3878 print_indent(indent);
3879 outs() << " class_methods "
3880 << format("0x%08" PRIx32, protocol.class_methods);
3881 if (print_method_description_list(protocol.class_methods, indent, info))
3882 outs() << " (not in an __OBJC section)\n";
3883
3884 return false;
3885}
3886
3887static bool print_protocol_list(uint32_t p, uint32_t indent,
3888 struct DisassembleInfo *info) {
3889 uint32_t offset, left, l;
3890 SectionRef S;
3891 struct objc_protocol_list_t protocol_list;
3892 const char *r, *list;
3893 int32_t i;
3894
3895 r = get_pointer_32(p, offset, left, S, info, true);
3896 if (r == nullptr)
3897 return true;
3898
3899 outs() << "\n";
3900 if (left > sizeof(struct objc_protocol_list_t)) {
3901 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
3902 } else {
3903 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
3904 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
3905 memcpy(&protocol_list, r, left);
3906 }
3907 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3908 swapStruct(protocol_list);
3909
3910 print_indent(indent);
3911 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
3912 << "\n";
3913 print_indent(indent);
3914 outs() << " count " << protocol_list.count << "\n";
3915
3916 list = r + sizeof(struct objc_protocol_list_t);
3917 for (i = 0; i < protocol_list.count; i++) {
3918 if ((i + 1) * sizeof(uint32_t) > left) {
3919 outs() << "\t\t remaining list entries extend past the of the section\n";
3920 break;
3921 }
3922 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
3923 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3924 sys::swapByteOrder(l);
3925
3926 print_indent(indent);
3927 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
3928 if (print_protocol(l, indent, info))
3929 outs() << "(not in an __OBJC section)\n";
3930 }
3931 return false;
3932}
3933
Kevin Enderby0fc11822015-04-01 20:57:01 +00003934static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3935 struct ivar_list64_t il;
3936 struct ivar64_t i;
3937 const char *r;
3938 uint32_t offset, xoffset, left, j;
3939 SectionRef S, xS;
3940 const char *name, *sym_name, *ivar_offset_p;
3941 uint64_t ivar_offset, n_value;
3942
3943 r = get_pointer_64(p, offset, left, S, info);
3944 if (r == nullptr)
3945 return;
3946 memset(&il, '\0', sizeof(struct ivar_list64_t));
3947 if (left < sizeof(struct ivar_list64_t)) {
3948 memcpy(&il, r, left);
3949 outs() << " (ivar_list_t entends past the end of the section)\n";
3950 } else
3951 memcpy(&il, r, sizeof(struct ivar_list64_t));
3952 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3953 swapStruct(il);
3954 outs() << " entsize " << il.entsize << "\n";
3955 outs() << " count " << il.count << "\n";
3956
3957 p += sizeof(struct ivar_list64_t);
3958 offset += sizeof(struct ivar_list64_t);
3959 for (j = 0; j < il.count; j++) {
3960 r = get_pointer_64(p, offset, left, S, info);
3961 if (r == nullptr)
3962 return;
3963 memset(&i, '\0', sizeof(struct ivar64_t));
3964 if (left < sizeof(struct ivar64_t)) {
3965 memcpy(&i, r, left);
3966 outs() << " (ivar_t entends past the end of the section)\n";
3967 } else
3968 memcpy(&i, r, sizeof(struct ivar64_t));
3969 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3970 swapStruct(i);
3971
3972 outs() << "\t\t\t offset ";
3973 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
3974 info, n_value, i.offset);
3975 if (n_value != 0) {
3976 if (info->verbose && sym_name != nullptr)
3977 outs() << sym_name;
3978 else
3979 outs() << format("0x%" PRIx64, n_value);
3980 if (i.offset != 0)
3981 outs() << " + " << format("0x%" PRIx64, i.offset);
3982 } else
3983 outs() << format("0x%" PRIx64, i.offset);
3984 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
3985 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
3986 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
3987 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3988 sys::swapByteOrder(ivar_offset);
3989 outs() << " " << ivar_offset << "\n";
3990 } else
3991 outs() << "\n";
3992
3993 outs() << "\t\t\t name ";
3994 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
3995 n_value, i.name);
3996 if (n_value != 0) {
3997 if (info->verbose && sym_name != nullptr)
3998 outs() << sym_name;
3999 else
4000 outs() << format("0x%" PRIx64, n_value);
4001 if (i.name != 0)
4002 outs() << " + " << format("0x%" PRIx64, i.name);
4003 } else
4004 outs() << format("0x%" PRIx64, i.name);
4005 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4006 if (name != nullptr)
4007 outs() << format(" %.*s", left, name);
4008 outs() << "\n";
4009
4010 outs() << "\t\t\t type ";
4011 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4012 n_value, i.name);
4013 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4014 if (n_value != 0) {
4015 if (info->verbose && sym_name != nullptr)
4016 outs() << sym_name;
4017 else
4018 outs() << format("0x%" PRIx64, n_value);
4019 if (i.type != 0)
4020 outs() << " + " << format("0x%" PRIx64, i.type);
4021 } else
4022 outs() << format("0x%" PRIx64, i.type);
4023 if (name != nullptr)
4024 outs() << format(" %.*s", left, name);
4025 outs() << "\n";
4026
4027 outs() << "\t\t\talignment " << i.alignment << "\n";
4028 outs() << "\t\t\t size " << i.size << "\n";
4029
4030 p += sizeof(struct ivar64_t);
4031 offset += sizeof(struct ivar64_t);
4032 }
4033}
4034
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004035static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4036 struct ivar_list32_t il;
4037 struct ivar32_t i;
4038 const char *r;
4039 uint32_t offset, xoffset, left, j;
4040 SectionRef S, xS;
4041 const char *name, *ivar_offset_p;
4042 uint32_t ivar_offset;
4043
4044 r = get_pointer_32(p, offset, left, S, info);
4045 if (r == nullptr)
4046 return;
4047 memset(&il, '\0', sizeof(struct ivar_list32_t));
4048 if (left < sizeof(struct ivar_list32_t)) {
4049 memcpy(&il, r, left);
4050 outs() << " (ivar_list_t entends past the end of the section)\n";
4051 } else
4052 memcpy(&il, r, sizeof(struct ivar_list32_t));
4053 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4054 swapStruct(il);
4055 outs() << " entsize " << il.entsize << "\n";
4056 outs() << " count " << il.count << "\n";
4057
4058 p += sizeof(struct ivar_list32_t);
4059 offset += sizeof(struct ivar_list32_t);
4060 for (j = 0; j < il.count; j++) {
4061 r = get_pointer_32(p, offset, left, S, info);
4062 if (r == nullptr)
4063 return;
4064 memset(&i, '\0', sizeof(struct ivar32_t));
4065 if (left < sizeof(struct ivar32_t)) {
4066 memcpy(&i, r, left);
4067 outs() << " (ivar_t entends past the end of the section)\n";
4068 } else
4069 memcpy(&i, r, sizeof(struct ivar32_t));
4070 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4071 swapStruct(i);
4072
4073 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4074 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4075 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4076 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4077 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4078 sys::swapByteOrder(ivar_offset);
4079 outs() << " " << ivar_offset << "\n";
4080 } else
4081 outs() << "\n";
4082
4083 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4084 name = get_pointer_32(i.name, xoffset, left, xS, info);
4085 if (name != nullptr)
4086 outs() << format(" %.*s", left, name);
4087 outs() << "\n";
4088
4089 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4090 name = get_pointer_32(i.type, xoffset, left, xS, info);
4091 if (name != nullptr)
4092 outs() << format(" %.*s", left, name);
4093 outs() << "\n";
4094
4095 outs() << "\t\t\talignment " << i.alignment << "\n";
4096 outs() << "\t\t\t size " << i.size << "\n";
4097
4098 p += sizeof(struct ivar32_t);
4099 offset += sizeof(struct ivar32_t);
4100 }
4101}
4102
Kevin Enderby0fc11822015-04-01 20:57:01 +00004103static void print_objc_property_list64(uint64_t p,
4104 struct DisassembleInfo *info) {
4105 struct objc_property_list64 opl;
4106 struct objc_property64 op;
4107 const char *r;
4108 uint32_t offset, xoffset, left, j;
4109 SectionRef S, xS;
4110 const char *name, *sym_name;
4111 uint64_t n_value;
4112
4113 r = get_pointer_64(p, offset, left, S, info);
4114 if (r == nullptr)
4115 return;
4116 memset(&opl, '\0', sizeof(struct objc_property_list64));
4117 if (left < sizeof(struct objc_property_list64)) {
4118 memcpy(&opl, r, left);
4119 outs() << " (objc_property_list entends past the end of the section)\n";
4120 } else
4121 memcpy(&opl, r, sizeof(struct objc_property_list64));
4122 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4123 swapStruct(opl);
4124 outs() << " entsize " << opl.entsize << "\n";
4125 outs() << " count " << opl.count << "\n";
4126
4127 p += sizeof(struct objc_property_list64);
4128 offset += sizeof(struct objc_property_list64);
4129 for (j = 0; j < opl.count; j++) {
4130 r = get_pointer_64(p, offset, left, S, info);
4131 if (r == nullptr)
4132 return;
4133 memset(&op, '\0', sizeof(struct objc_property64));
4134 if (left < sizeof(struct objc_property64)) {
4135 memcpy(&op, r, left);
4136 outs() << " (objc_property entends past the end of the section)\n";
4137 } else
4138 memcpy(&op, r, sizeof(struct objc_property64));
4139 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4140 swapStruct(op);
4141
4142 outs() << "\t\t\t name ";
4143 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4144 info, n_value, op.name);
4145 if (n_value != 0) {
4146 if (info->verbose && sym_name != nullptr)
4147 outs() << sym_name;
4148 else
4149 outs() << format("0x%" PRIx64, n_value);
4150 if (op.name != 0)
4151 outs() << " + " << format("0x%" PRIx64, op.name);
4152 } else
4153 outs() << format("0x%" PRIx64, op.name);
4154 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4155 if (name != nullptr)
4156 outs() << format(" %.*s", left, name);
4157 outs() << "\n";
4158
4159 outs() << "\t\t\tattributes ";
4160 sym_name =
4161 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4162 info, n_value, op.attributes);
4163 if (n_value != 0) {
4164 if (info->verbose && sym_name != nullptr)
4165 outs() << sym_name;
4166 else
4167 outs() << format("0x%" PRIx64, n_value);
4168 if (op.attributes != 0)
4169 outs() << " + " << format("0x%" PRIx64, op.attributes);
4170 } else
4171 outs() << format("0x%" PRIx64, op.attributes);
4172 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4173 if (name != nullptr)
4174 outs() << format(" %.*s", left, name);
4175 outs() << "\n";
4176
4177 p += sizeof(struct objc_property64);
4178 offset += sizeof(struct objc_property64);
4179 }
4180}
4181
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004182static void print_objc_property_list32(uint32_t p,
4183 struct DisassembleInfo *info) {
4184 struct objc_property_list32 opl;
4185 struct objc_property32 op;
4186 const char *r;
4187 uint32_t offset, xoffset, left, j;
4188 SectionRef S, xS;
4189 const char *name;
4190
4191 r = get_pointer_32(p, offset, left, S, info);
4192 if (r == nullptr)
4193 return;
4194 memset(&opl, '\0', sizeof(struct objc_property_list32));
4195 if (left < sizeof(struct objc_property_list32)) {
4196 memcpy(&opl, r, left);
4197 outs() << " (objc_property_list entends past the end of the section)\n";
4198 } else
4199 memcpy(&opl, r, sizeof(struct objc_property_list32));
4200 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4201 swapStruct(opl);
4202 outs() << " entsize " << opl.entsize << "\n";
4203 outs() << " count " << opl.count << "\n";
4204
4205 p += sizeof(struct objc_property_list32);
4206 offset += sizeof(struct objc_property_list32);
4207 for (j = 0; j < opl.count; j++) {
4208 r = get_pointer_32(p, offset, left, S, info);
4209 if (r == nullptr)
4210 return;
4211 memset(&op, '\0', sizeof(struct objc_property32));
4212 if (left < sizeof(struct objc_property32)) {
4213 memcpy(&op, r, left);
4214 outs() << " (objc_property entends past the end of the section)\n";
4215 } else
4216 memcpy(&op, r, sizeof(struct objc_property32));
4217 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4218 swapStruct(op);
4219
4220 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4221 name = get_pointer_32(op.name, xoffset, left, xS, info);
4222 if (name != nullptr)
4223 outs() << format(" %.*s", left, name);
4224 outs() << "\n";
4225
4226 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4227 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4228 if (name != nullptr)
4229 outs() << format(" %.*s", left, name);
4230 outs() << "\n";
4231
4232 p += sizeof(struct objc_property32);
4233 offset += sizeof(struct objc_property32);
4234 }
4235}
4236
Kevin Enderby0fc11822015-04-01 20:57:01 +00004237static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4238 bool &is_meta_class) {
4239 struct class_ro64_t cro;
4240 const char *r;
4241 uint32_t offset, xoffset, left;
4242 SectionRef S, xS;
4243 const char *name, *sym_name;
4244 uint64_t n_value;
4245
4246 r = get_pointer_64(p, offset, left, S, info);
4247 if (r == nullptr || left < sizeof(struct class_ro64_t))
4248 return;
4249 memset(&cro, '\0', sizeof(struct class_ro64_t));
4250 if (left < sizeof(struct class_ro64_t)) {
4251 memcpy(&cro, r, left);
4252 outs() << " (class_ro_t entends past the end of the section)\n";
4253 } else
4254 memcpy(&cro, r, sizeof(struct class_ro64_t));
4255 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4256 swapStruct(cro);
4257 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4258 if (cro.flags & RO_META)
4259 outs() << " RO_META";
4260 if (cro.flags & RO_ROOT)
4261 outs() << " RO_ROOT";
4262 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4263 outs() << " RO_HAS_CXX_STRUCTORS";
4264 outs() << "\n";
4265 outs() << " instanceStart " << cro.instanceStart << "\n";
4266 outs() << " instanceSize " << cro.instanceSize << "\n";
4267 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4268 << "\n";
4269 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4270 << "\n";
4271 print_layout_map64(cro.ivarLayout, info);
4272
4273 outs() << " name ";
4274 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4275 info, n_value, cro.name);
4276 if (n_value != 0) {
4277 if (info->verbose && sym_name != nullptr)
4278 outs() << sym_name;
4279 else
4280 outs() << format("0x%" PRIx64, n_value);
4281 if (cro.name != 0)
4282 outs() << " + " << format("0x%" PRIx64, cro.name);
4283 } else
4284 outs() << format("0x%" PRIx64, cro.name);
4285 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4286 if (name != nullptr)
4287 outs() << format(" %.*s", left, name);
4288 outs() << "\n";
4289
4290 outs() << " baseMethods ";
4291 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4292 S, info, n_value, cro.baseMethods);
4293 if (n_value != 0) {
4294 if (info->verbose && sym_name != nullptr)
4295 outs() << sym_name;
4296 else
4297 outs() << format("0x%" PRIx64, n_value);
4298 if (cro.baseMethods != 0)
4299 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4300 } else
4301 outs() << format("0x%" PRIx64, cro.baseMethods);
4302 outs() << " (struct method_list_t *)\n";
4303 if (cro.baseMethods + n_value != 0)
4304 print_method_list64_t(cro.baseMethods + n_value, info, "");
4305
4306 outs() << " baseProtocols ";
4307 sym_name =
4308 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4309 info, n_value, cro.baseProtocols);
4310 if (n_value != 0) {
4311 if (info->verbose && sym_name != nullptr)
4312 outs() << sym_name;
4313 else
4314 outs() << format("0x%" PRIx64, n_value);
4315 if (cro.baseProtocols != 0)
4316 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4317 } else
4318 outs() << format("0x%" PRIx64, cro.baseProtocols);
4319 outs() << "\n";
4320 if (cro.baseProtocols + n_value != 0)
4321 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4322
4323 outs() << " ivars ";
4324 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004325 info, n_value, cro.ivars);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004326 if (n_value != 0) {
4327 if (info->verbose && sym_name != nullptr)
4328 outs() << sym_name;
4329 else
4330 outs() << format("0x%" PRIx64, n_value);
4331 if (cro.ivars != 0)
4332 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4333 } else
4334 outs() << format("0x%" PRIx64, cro.ivars);
4335 outs() << "\n";
4336 if (cro.ivars + n_value != 0)
4337 print_ivar_list64_t(cro.ivars + n_value, info);
4338
4339 outs() << " weakIvarLayout ";
4340 sym_name =
4341 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4342 info, n_value, cro.weakIvarLayout);
4343 if (n_value != 0) {
4344 if (info->verbose && sym_name != nullptr)
4345 outs() << sym_name;
4346 else
4347 outs() << format("0x%" PRIx64, n_value);
4348 if (cro.weakIvarLayout != 0)
4349 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4350 } else
4351 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4352 outs() << "\n";
4353 print_layout_map64(cro.weakIvarLayout + n_value, info);
4354
4355 outs() << " baseProperties ";
4356 sym_name =
4357 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4358 info, n_value, cro.baseProperties);
4359 if (n_value != 0) {
4360 if (info->verbose && sym_name != nullptr)
4361 outs() << sym_name;
4362 else
4363 outs() << format("0x%" PRIx64, n_value);
4364 if (cro.baseProperties != 0)
4365 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4366 } else
4367 outs() << format("0x%" PRIx64, cro.baseProperties);
4368 outs() << "\n";
4369 if (cro.baseProperties + n_value != 0)
4370 print_objc_property_list64(cro.baseProperties + n_value, info);
4371
4372 is_meta_class = (cro.flags & RO_META) ? true : false;
4373}
4374
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004375static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4376 bool &is_meta_class) {
4377 struct class_ro32_t cro;
4378 const char *r;
4379 uint32_t offset, xoffset, left;
4380 SectionRef S, xS;
4381 const char *name;
4382
4383 r = get_pointer_32(p, offset, left, S, info);
4384 if (r == nullptr)
4385 return;
4386 memset(&cro, '\0', sizeof(struct class_ro32_t));
4387 if (left < sizeof(struct class_ro32_t)) {
4388 memcpy(&cro, r, left);
4389 outs() << " (class_ro_t entends past the end of the section)\n";
4390 } else
4391 memcpy(&cro, r, sizeof(struct class_ro32_t));
4392 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4393 swapStruct(cro);
4394 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4395 if (cro.flags & RO_META)
4396 outs() << " RO_META";
4397 if (cro.flags & RO_ROOT)
4398 outs() << " RO_ROOT";
4399 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4400 outs() << " RO_HAS_CXX_STRUCTORS";
4401 outs() << "\n";
4402 outs() << " instanceStart " << cro.instanceStart << "\n";
4403 outs() << " instanceSize " << cro.instanceSize << "\n";
4404 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4405 << "\n";
4406 print_layout_map32(cro.ivarLayout, info);
4407
4408 outs() << " name " << format("0x%" PRIx32, cro.name);
4409 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4410 if (name != nullptr)
4411 outs() << format(" %.*s", left, name);
4412 outs() << "\n";
4413
4414 outs() << " baseMethods "
4415 << format("0x%" PRIx32, cro.baseMethods)
4416 << " (struct method_list_t *)\n";
4417 if (cro.baseMethods != 0)
4418 print_method_list32_t(cro.baseMethods, info, "");
4419
4420 outs() << " baseProtocols "
4421 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4422 if (cro.baseProtocols != 0)
4423 print_protocol_list32_t(cro.baseProtocols, info);
4424 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4425 << "\n";
4426 if (cro.ivars != 0)
4427 print_ivar_list32_t(cro.ivars, info);
4428 outs() << " weakIvarLayout "
4429 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4430 print_layout_map32(cro.weakIvarLayout, info);
4431 outs() << " baseProperties "
4432 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4433 if (cro.baseProperties != 0)
4434 print_objc_property_list32(cro.baseProperties, info);
4435 is_meta_class = (cro.flags & RO_META) ? true : false;
4436}
4437
Kevin Enderby0fc11822015-04-01 20:57:01 +00004438static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4439 struct class64_t c;
4440 const char *r;
4441 uint32_t offset, left;
4442 SectionRef S;
4443 const char *name;
4444 uint64_t isa_n_value, n_value;
4445
4446 r = get_pointer_64(p, offset, left, S, info);
4447 if (r == nullptr || left < sizeof(struct class64_t))
4448 return;
4449 memset(&c, '\0', sizeof(struct class64_t));
4450 if (left < sizeof(struct class64_t)) {
4451 memcpy(&c, r, left);
4452 outs() << " (class_t entends past the end of the section)\n";
4453 } else
4454 memcpy(&c, r, sizeof(struct class64_t));
4455 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4456 swapStruct(c);
4457
4458 outs() << " isa " << format("0x%" PRIx64, c.isa);
4459 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4460 isa_n_value, c.isa);
4461 if (name != nullptr)
4462 outs() << " " << name;
4463 outs() << "\n";
4464
4465 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4466 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4467 n_value, c.superclass);
4468 if (name != nullptr)
4469 outs() << " " << name;
4470 outs() << "\n";
4471
4472 outs() << " cache " << format("0x%" PRIx64, c.cache);
4473 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4474 n_value, c.cache);
4475 if (name != nullptr)
4476 outs() << " " << name;
4477 outs() << "\n";
4478
4479 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4480 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4481 n_value, c.vtable);
4482 if (name != nullptr)
4483 outs() << " " << name;
4484 outs() << "\n";
4485
4486 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4487 n_value, c.data);
4488 outs() << " data ";
4489 if (n_value != 0) {
4490 if (info->verbose && name != nullptr)
4491 outs() << name;
4492 else
4493 outs() << format("0x%" PRIx64, n_value);
4494 if (c.data != 0)
4495 outs() << " + " << format("0x%" PRIx64, c.data);
4496 } else
4497 outs() << format("0x%" PRIx64, c.data);
4498 outs() << " (struct class_ro_t *)";
4499
4500 // This is a Swift class if some of the low bits of the pointer are set.
4501 if ((c.data + n_value) & 0x7)
4502 outs() << " Swift class";
4503 outs() << "\n";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004504 bool is_meta_class;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004505 print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class);
4506
4507 if (is_meta_class == false) {
4508 outs() << "Meta Class\n";
4509 print_class64_t(c.isa + isa_n_value, info);
4510 }
4511}
4512
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004513static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4514 struct class32_t c;
4515 const char *r;
4516 uint32_t offset, left;
4517 SectionRef S;
4518 const char *name;
4519
4520 r = get_pointer_32(p, offset, left, S, info);
4521 if (r == nullptr)
4522 return;
4523 memset(&c, '\0', sizeof(struct class32_t));
4524 if (left < sizeof(struct class32_t)) {
4525 memcpy(&c, r, left);
4526 outs() << " (class_t entends past the end of the section)\n";
4527 } else
4528 memcpy(&c, r, sizeof(struct class32_t));
4529 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4530 swapStruct(c);
4531
4532 outs() << " isa " << format("0x%" PRIx32, c.isa);
4533 name =
4534 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4535 if (name != nullptr)
4536 outs() << " " << name;
4537 outs() << "\n";
4538
4539 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4540 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4541 c.superclass);
4542 if (name != nullptr)
4543 outs() << " " << name;
4544 outs() << "\n";
4545
4546 outs() << " cache " << format("0x%" PRIx32, c.cache);
4547 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4548 c.cache);
4549 if (name != nullptr)
4550 outs() << " " << name;
4551 outs() << "\n";
4552
4553 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4554 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4555 c.vtable);
4556 if (name != nullptr)
4557 outs() << " " << name;
4558 outs() << "\n";
4559
4560 name =
4561 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4562 outs() << " data " << format("0x%" PRIx32, c.data)
4563 << " (struct class_ro_t *)";
4564
4565 // This is a Swift class if some of the low bits of the pointer are set.
4566 if (c.data & 0x3)
4567 outs() << " Swift class";
4568 outs() << "\n";
4569 bool is_meta_class;
4570 print_class_ro32_t(c.data & ~0x3, info, is_meta_class);
4571
4572 if (is_meta_class == false) {
4573 outs() << "Meta Class\n";
4574 print_class32_t(c.isa, info);
4575 }
4576}
4577
Kevin Enderby846c0002015-04-16 17:19:59 +00004578static void print_objc_class_t(struct objc_class_t *objc_class,
4579 struct DisassembleInfo *info) {
4580 uint32_t offset, left, xleft;
4581 const char *name, *p, *ivar_list;
4582 SectionRef S;
4583 int32_t i;
4584 struct objc_ivar_list_t objc_ivar_list;
4585 struct objc_ivar_t ivar;
4586
4587 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4588 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4589 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4590 if (name != nullptr)
4591 outs() << format(" %.*s", left, name);
4592 else
4593 outs() << " (not in an __OBJC section)";
4594 }
4595 outs() << "\n";
4596
4597 outs() << "\t super_class "
4598 << format("0x%08" PRIx32, objc_class->super_class);
4599 if (info->verbose) {
4600 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4601 if (name != nullptr)
4602 outs() << format(" %.*s", left, name);
4603 else
4604 outs() << " (not in an __OBJC section)";
4605 }
4606 outs() << "\n";
4607
4608 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4609 if (info->verbose) {
4610 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4611 if (name != nullptr)
4612 outs() << format(" %.*s", left, name);
4613 else
4614 outs() << " (not in an __OBJC section)";
4615 }
4616 outs() << "\n";
4617
4618 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4619 << "\n";
4620
4621 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4622 if (info->verbose) {
4623 if (CLS_GETINFO(objc_class, CLS_CLASS))
4624 outs() << " CLS_CLASS";
4625 else if (CLS_GETINFO(objc_class, CLS_META))
4626 outs() << " CLS_META";
4627 }
4628 outs() << "\n";
4629
4630 outs() << "\t instance_size "
4631 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4632
4633 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4634 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4635 if (p != nullptr) {
4636 if (left > sizeof(struct objc_ivar_list_t)) {
4637 outs() << "\n";
4638 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4639 } else {
4640 outs() << " (entends past the end of the section)\n";
4641 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4642 memcpy(&objc_ivar_list, p, left);
4643 }
4644 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4645 swapStruct(objc_ivar_list);
4646 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4647 ivar_list = p + sizeof(struct objc_ivar_list_t);
4648 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4649 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4650 outs() << "\t\t remaining ivar's extend past the of the section\n";
4651 break;
4652 }
4653 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4654 sizeof(struct objc_ivar_t));
4655 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4656 swapStruct(ivar);
4657
4658 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4659 if (info->verbose) {
4660 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4661 if (name != nullptr)
4662 outs() << format(" %.*s", xleft, name);
4663 else
4664 outs() << " (not in an __OBJC section)";
4665 }
4666 outs() << "\n";
4667
4668 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4669 if (info->verbose) {
4670 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4671 if (name != nullptr)
4672 outs() << format(" %.*s", xleft, name);
4673 else
4674 outs() << " (not in an __OBJC section)";
4675 }
4676 outs() << "\n";
4677
4678 outs() << "\t\t ivar_offset "
4679 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4680 }
4681 } else {
4682 outs() << " (not in an __OBJC section)\n";
4683 }
4684
4685 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4686 if (print_method_list(objc_class->methodLists, info))
4687 outs() << " (not in an __OBJC section)\n";
4688
4689 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4690 << "\n";
4691
4692 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4693 if (print_protocol_list(objc_class->protocols, 16, info))
4694 outs() << " (not in an __OBJC section)\n";
4695}
4696
4697static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4698 struct DisassembleInfo *info) {
4699 uint32_t offset, left;
4700 const char *name;
4701 SectionRef S;
4702
4703 outs() << "\t category name "
4704 << format("0x%08" PRIx32, objc_category->category_name);
4705 if (info->verbose) {
4706 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4707 true);
4708 if (name != nullptr)
4709 outs() << format(" %.*s", left, name);
4710 else
4711 outs() << " (not in an __OBJC section)";
4712 }
4713 outs() << "\n";
4714
4715 outs() << "\t\t class name "
4716 << format("0x%08" PRIx32, objc_category->class_name);
4717 if (info->verbose) {
4718 name =
4719 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4720 if (name != nullptr)
4721 outs() << format(" %.*s", left, name);
4722 else
4723 outs() << " (not in an __OBJC section)";
4724 }
4725 outs() << "\n";
4726
4727 outs() << "\t instance methods "
4728 << format("0x%08" PRIx32, objc_category->instance_methods);
4729 if (print_method_list(objc_category->instance_methods, info))
4730 outs() << " (not in an __OBJC section)\n";
4731
4732 outs() << "\t class methods "
4733 << format("0x%08" PRIx32, objc_category->class_methods);
4734 if (print_method_list(objc_category->class_methods, info))
4735 outs() << " (not in an __OBJC section)\n";
4736}
4737
Kevin Enderby0fc11822015-04-01 20:57:01 +00004738static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4739 struct category64_t c;
4740 const char *r;
4741 uint32_t offset, xoffset, left;
4742 SectionRef S, xS;
4743 const char *name, *sym_name;
4744 uint64_t n_value;
4745
4746 r = get_pointer_64(p, offset, left, S, info);
4747 if (r == nullptr)
4748 return;
4749 memset(&c, '\0', sizeof(struct category64_t));
4750 if (left < sizeof(struct category64_t)) {
4751 memcpy(&c, r, left);
4752 outs() << " (category_t entends past the end of the section)\n";
4753 } else
4754 memcpy(&c, r, sizeof(struct category64_t));
4755 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4756 swapStruct(c);
4757
4758 outs() << " name ";
4759 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4760 info, n_value, c.name);
4761 if (n_value != 0) {
4762 if (info->verbose && sym_name != nullptr)
4763 outs() << sym_name;
4764 else
4765 outs() << format("0x%" PRIx64, n_value);
4766 if (c.name != 0)
4767 outs() << " + " << format("0x%" PRIx64, c.name);
4768 } else
4769 outs() << format("0x%" PRIx64, c.name);
4770 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4771 if (name != nullptr)
4772 outs() << format(" %.*s", left, name);
4773 outs() << "\n";
4774
4775 outs() << " cls ";
4776 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4777 n_value, c.cls);
4778 if (n_value != 0) {
4779 if (info->verbose && sym_name != nullptr)
4780 outs() << sym_name;
4781 else
4782 outs() << format("0x%" PRIx64, n_value);
4783 if (c.cls != 0)
4784 outs() << " + " << format("0x%" PRIx64, c.cls);
4785 } else
4786 outs() << format("0x%" PRIx64, c.cls);
4787 outs() << "\n";
4788 if (c.cls + n_value != 0)
4789 print_class64_t(c.cls + n_value, info);
4790
4791 outs() << " instanceMethods ";
4792 sym_name =
4793 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4794 info, n_value, c.instanceMethods);
4795 if (n_value != 0) {
4796 if (info->verbose && sym_name != nullptr)
4797 outs() << sym_name;
4798 else
4799 outs() << format("0x%" PRIx64, n_value);
4800 if (c.instanceMethods != 0)
4801 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4802 } else
4803 outs() << format("0x%" PRIx64, c.instanceMethods);
4804 outs() << "\n";
4805 if (c.instanceMethods + n_value != 0)
4806 print_method_list64_t(c.instanceMethods + n_value, info, "");
4807
4808 outs() << " classMethods ";
4809 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4810 S, info, n_value, c.classMethods);
4811 if (n_value != 0) {
4812 if (info->verbose && sym_name != nullptr)
4813 outs() << sym_name;
4814 else
4815 outs() << format("0x%" PRIx64, n_value);
4816 if (c.classMethods != 0)
4817 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4818 } else
4819 outs() << format("0x%" PRIx64, c.classMethods);
4820 outs() << "\n";
4821 if (c.classMethods + n_value != 0)
4822 print_method_list64_t(c.classMethods + n_value, info, "");
4823
4824 outs() << " protocols ";
4825 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4826 info, n_value, c.protocols);
4827 if (n_value != 0) {
4828 if (info->verbose && sym_name != nullptr)
4829 outs() << sym_name;
4830 else
4831 outs() << format("0x%" PRIx64, n_value);
4832 if (c.protocols != 0)
4833 outs() << " + " << format("0x%" PRIx64, c.protocols);
4834 } else
4835 outs() << format("0x%" PRIx64, c.protocols);
4836 outs() << "\n";
4837 if (c.protocols + n_value != 0)
4838 print_protocol_list64_t(c.protocols + n_value, info);
4839
4840 outs() << "instanceProperties ";
4841 sym_name =
4842 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4843 S, info, n_value, c.instanceProperties);
4844 if (n_value != 0) {
4845 if (info->verbose && sym_name != nullptr)
4846 outs() << sym_name;
4847 else
4848 outs() << format("0x%" PRIx64, n_value);
4849 if (c.instanceProperties != 0)
4850 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4851 } else
4852 outs() << format("0x%" PRIx64, c.instanceProperties);
4853 outs() << "\n";
4854 if (c.instanceProperties + n_value != 0)
4855 print_objc_property_list64(c.instanceProperties + n_value, info);
4856}
4857
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004858static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4859 struct category32_t c;
4860 const char *r;
4861 uint32_t offset, left;
4862 SectionRef S, xS;
4863 const char *name;
4864
4865 r = get_pointer_32(p, offset, left, S, info);
4866 if (r == nullptr)
4867 return;
4868 memset(&c, '\0', sizeof(struct category32_t));
4869 if (left < sizeof(struct category32_t)) {
4870 memcpy(&c, r, left);
4871 outs() << " (category_t entends past the end of the section)\n";
4872 } else
4873 memcpy(&c, r, sizeof(struct category32_t));
4874 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4875 swapStruct(c);
4876
4877 outs() << " name " << format("0x%" PRIx32, c.name);
4878 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4879 c.name);
4880 if (name != NULL)
4881 outs() << " " << name;
4882 outs() << "\n";
4883
4884 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
4885 if (c.cls != 0)
4886 print_class32_t(c.cls, info);
4887 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4888 << "\n";
4889 if (c.instanceMethods != 0)
4890 print_method_list32_t(c.instanceMethods, info, "");
4891 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
4892 << "\n";
4893 if (c.classMethods != 0)
4894 print_method_list32_t(c.classMethods, info, "");
4895 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
4896 if (c.protocols != 0)
4897 print_protocol_list32_t(c.protocols, info);
4898 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
4899 << "\n";
4900 if (c.instanceProperties != 0)
4901 print_objc_property_list32(c.instanceProperties, info);
4902}
4903
Kevin Enderby0fc11822015-04-01 20:57:01 +00004904static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
4905 uint32_t i, left, offset, xoffset;
4906 uint64_t p, n_value;
4907 struct message_ref64 mr;
4908 const char *name, *sym_name;
4909 const char *r;
4910 SectionRef xS;
4911
4912 if (S == SectionRef())
4913 return;
4914
4915 StringRef SectName;
4916 S.getName(SectName);
4917 DataRefImpl Ref = S.getRawDataRefImpl();
4918 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4919 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4920 offset = 0;
4921 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4922 p = S.getAddress() + i;
4923 r = get_pointer_64(p, offset, left, S, info);
4924 if (r == nullptr)
4925 return;
4926 memset(&mr, '\0', sizeof(struct message_ref64));
4927 if (left < sizeof(struct message_ref64)) {
4928 memcpy(&mr, r, left);
4929 outs() << " (message_ref entends past the end of the section)\n";
4930 } else
4931 memcpy(&mr, r, sizeof(struct message_ref64));
4932 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4933 swapStruct(mr);
4934
4935 outs() << " imp ";
4936 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
4937 n_value, mr.imp);
4938 if (n_value != 0) {
4939 outs() << format("0x%" PRIx64, n_value) << " ";
4940 if (mr.imp != 0)
4941 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
4942 } else
4943 outs() << format("0x%" PRIx64, mr.imp) << " ";
4944 if (name != nullptr)
4945 outs() << " " << name;
4946 outs() << "\n";
4947
4948 outs() << " sel ";
4949 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
4950 info, n_value, mr.sel);
4951 if (n_value != 0) {
4952 if (info->verbose && sym_name != nullptr)
4953 outs() << sym_name;
4954 else
4955 outs() << format("0x%" PRIx64, n_value);
4956 if (mr.sel != 0)
4957 outs() << " + " << format("0x%" PRIx64, mr.sel);
4958 } else
4959 outs() << format("0x%" PRIx64, mr.sel);
4960 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
4961 if (name != nullptr)
4962 outs() << format(" %.*s", left, name);
4963 outs() << "\n";
4964
4965 offset += sizeof(struct message_ref64);
4966 }
4967}
4968
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004969static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
4970 uint32_t i, left, offset, xoffset, p;
4971 struct message_ref32 mr;
4972 const char *name, *r;
4973 SectionRef xS;
4974
4975 if (S == SectionRef())
4976 return;
4977
4978 StringRef SectName;
4979 S.getName(SectName);
4980 DataRefImpl Ref = S.getRawDataRefImpl();
4981 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4982 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4983 offset = 0;
4984 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4985 p = S.getAddress() + i;
4986 r = get_pointer_32(p, offset, left, S, info);
4987 if (r == nullptr)
4988 return;
4989 memset(&mr, '\0', sizeof(struct message_ref32));
4990 if (left < sizeof(struct message_ref32)) {
4991 memcpy(&mr, r, left);
4992 outs() << " (message_ref entends past the end of the section)\n";
4993 } else
4994 memcpy(&mr, r, sizeof(struct message_ref32));
4995 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4996 swapStruct(mr);
4997
4998 outs() << " imp " << format("0x%" PRIx32, mr.imp);
4999 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5000 mr.imp);
5001 if (name != nullptr)
5002 outs() << " " << name;
5003 outs() << "\n";
5004
5005 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5006 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5007 if (name != nullptr)
5008 outs() << " " << name;
5009 outs() << "\n";
5010
5011 offset += sizeof(struct message_ref32);
5012 }
5013}
5014
Kevin Enderby0fc11822015-04-01 20:57:01 +00005015static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5016 uint32_t left, offset, swift_version;
5017 uint64_t p;
5018 struct objc_image_info64 o;
5019 const char *r;
5020
5021 StringRef SectName;
5022 S.getName(SectName);
5023 DataRefImpl Ref = S.getRawDataRefImpl();
5024 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5025 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5026 p = S.getAddress();
5027 r = get_pointer_64(p, offset, left, S, info);
5028 if (r == nullptr)
5029 return;
5030 memset(&o, '\0', sizeof(struct objc_image_info64));
5031 if (left < sizeof(struct objc_image_info64)) {
5032 memcpy(&o, r, left);
5033 outs() << " (objc_image_info entends past the end of the section)\n";
5034 } else
5035 memcpy(&o, r, sizeof(struct objc_image_info64));
5036 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5037 swapStruct(o);
5038 outs() << " version " << o.version << "\n";
5039 outs() << " flags " << format("0x%" PRIx32, o.flags);
5040 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5041 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5042 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5043 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5044 swift_version = (o.flags >> 8) & 0xff;
5045 if (swift_version != 0) {
5046 if (swift_version == 1)
5047 outs() << " Swift 1.0";
5048 else if (swift_version == 2)
5049 outs() << " Swift 1.1";
5050 else
5051 outs() << " unknown future Swift version (" << swift_version << ")";
5052 }
5053 outs() << "\n";
5054}
5055
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005056static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5057 uint32_t left, offset, swift_version, p;
5058 struct objc_image_info32 o;
5059 const char *r;
5060
5061 StringRef SectName;
5062 S.getName(SectName);
5063 DataRefImpl Ref = S.getRawDataRefImpl();
5064 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5065 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5066 p = S.getAddress();
5067 r = get_pointer_32(p, offset, left, S, info);
5068 if (r == nullptr)
5069 return;
5070 memset(&o, '\0', sizeof(struct objc_image_info32));
5071 if (left < sizeof(struct objc_image_info32)) {
5072 memcpy(&o, r, left);
5073 outs() << " (objc_image_info entends past the end of the section)\n";
5074 } else
5075 memcpy(&o, r, sizeof(struct objc_image_info32));
5076 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5077 swapStruct(o);
5078 outs() << " version " << o.version << "\n";
5079 outs() << " flags " << format("0x%" PRIx32, o.flags);
5080 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5081 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5082 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5083 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5084 swift_version = (o.flags >> 8) & 0xff;
5085 if (swift_version != 0) {
5086 if (swift_version == 1)
5087 outs() << " Swift 1.0";
5088 else if (swift_version == 2)
5089 outs() << " Swift 1.1";
5090 else
5091 outs() << " unknown future Swift version (" << swift_version << ")";
5092 }
5093 outs() << "\n";
5094}
5095
Kevin Enderby846c0002015-04-16 17:19:59 +00005096static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5097 uint32_t left, offset, p;
5098 struct imageInfo_t o;
5099 const char *r;
5100
5101 StringRef SectName;
5102 S.getName(SectName);
5103 DataRefImpl Ref = S.getRawDataRefImpl();
5104 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5105 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5106 p = S.getAddress();
5107 r = get_pointer_32(p, offset, left, S, info);
5108 if (r == nullptr)
5109 return;
5110 memset(&o, '\0', sizeof(struct imageInfo_t));
5111 if (left < sizeof(struct imageInfo_t)) {
5112 memcpy(&o, r, left);
5113 outs() << " (imageInfo entends past the end of the section)\n";
5114 } else
5115 memcpy(&o, r, sizeof(struct imageInfo_t));
5116 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5117 swapStruct(o);
5118 outs() << " version " << o.version << "\n";
5119 outs() << " flags " << format("0x%" PRIx32, o.flags);
5120 if (o.flags & 0x1)
5121 outs() << " F&C";
5122 if (o.flags & 0x2)
5123 outs() << " GC";
5124 if (o.flags & 0x4)
5125 outs() << " GC-only";
5126 else
5127 outs() << " RR";
5128 outs() << "\n";
5129}
5130
Kevin Enderby0fc11822015-04-01 20:57:01 +00005131static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5132 SymbolAddressMap AddrMap;
5133 if (verbose)
5134 CreateSymbolAddressMap(O, &AddrMap);
5135
5136 std::vector<SectionRef> Sections;
5137 for (const SectionRef &Section : O->sections()) {
5138 StringRef SectName;
5139 Section.getName(SectName);
5140 Sections.push_back(Section);
5141 }
5142
5143 struct DisassembleInfo info;
5144 // Set up the block of info used by the Symbolizer call backs.
5145 info.verbose = verbose;
5146 info.O = O;
5147 info.AddrMap = &AddrMap;
5148 info.Sections = &Sections;
5149 info.class_name = nullptr;
5150 info.selector_name = nullptr;
5151 info.method = nullptr;
5152 info.demangled_name = nullptr;
5153 info.bindtable = nullptr;
5154 info.adrp_addr = 0;
5155 info.adrp_inst = 0;
5156
5157 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5158 if (CL != SectionRef()) {
5159 info.S = CL;
5160 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5161 } else {
5162 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5163 info.S = CL;
5164 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5165 }
5166
5167 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5168 if (CR != SectionRef()) {
5169 info.S = CR;
5170 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5171 } else {
5172 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5173 info.S = CR;
5174 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5175 }
5176
5177 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5178 if (SR != SectionRef()) {
5179 info.S = SR;
5180 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5181 } else {
5182 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5183 info.S = SR;
5184 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5185 }
5186
5187 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5188 if (CA != SectionRef()) {
5189 info.S = CA;
5190 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5191 } else {
5192 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5193 info.S = CA;
5194 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5195 }
5196
5197 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5198 if (PL != SectionRef()) {
5199 info.S = PL;
5200 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5201 } else {
5202 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5203 info.S = PL;
5204 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5205 }
5206
5207 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5208 if (MR != SectionRef()) {
5209 info.S = MR;
5210 print_message_refs64(MR, &info);
5211 } else {
5212 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5213 info.S = MR;
5214 print_message_refs64(MR, &info);
5215 }
5216
5217 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5218 if (II != SectionRef()) {
5219 info.S = II;
5220 print_image_info64(II, &info);
5221 } else {
5222 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5223 info.S = II;
5224 print_image_info64(II, &info);
5225 }
Kevin Enderby0bc6ed42015-04-01 21:50:45 +00005226
5227 if (info.bindtable != nullptr)
5228 delete info.bindtable;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005229}
5230
5231static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005232 SymbolAddressMap AddrMap;
5233 if (verbose)
5234 CreateSymbolAddressMap(O, &AddrMap);
5235
5236 std::vector<SectionRef> Sections;
5237 for (const SectionRef &Section : O->sections()) {
5238 StringRef SectName;
5239 Section.getName(SectName);
5240 Sections.push_back(Section);
5241 }
5242
5243 struct DisassembleInfo info;
5244 // Set up the block of info used by the Symbolizer call backs.
5245 info.verbose = verbose;
5246 info.O = O;
5247 info.AddrMap = &AddrMap;
5248 info.Sections = &Sections;
5249 info.class_name = nullptr;
5250 info.selector_name = nullptr;
5251 info.method = nullptr;
5252 info.demangled_name = nullptr;
5253 info.bindtable = nullptr;
5254 info.adrp_addr = 0;
5255 info.adrp_inst = 0;
5256
5257 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5258 if (CL != SectionRef()) {
5259 info.S = CL;
5260 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5261 } else {
5262 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5263 info.S = CL;
5264 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5265 }
5266
5267 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5268 if (CR != SectionRef()) {
5269 info.S = CR;
5270 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5271 } else {
5272 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5273 info.S = CR;
5274 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5275 }
5276
5277 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5278 if (SR != SectionRef()) {
5279 info.S = SR;
5280 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5281 } else {
5282 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5283 info.S = SR;
5284 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5285 }
5286
5287 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5288 if (CA != SectionRef()) {
5289 info.S = CA;
5290 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5291 } else {
5292 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5293 info.S = CA;
5294 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5295 }
5296
5297 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5298 if (PL != SectionRef()) {
5299 info.S = PL;
5300 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5301 } else {
5302 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5303 info.S = PL;
5304 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5305 }
5306
5307 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5308 if (MR != SectionRef()) {
5309 info.S = MR;
5310 print_message_refs32(MR, &info);
5311 } else {
5312 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5313 info.S = MR;
5314 print_message_refs32(MR, &info);
5315 }
5316
5317 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5318 if (II != SectionRef()) {
5319 info.S = II;
5320 print_image_info32(II, &info);
5321 } else {
5322 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5323 info.S = II;
5324 print_image_info32(II, &info);
5325 }
Kevin Enderby0fc11822015-04-01 20:57:01 +00005326}
5327
5328static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby846c0002015-04-16 17:19:59 +00005329 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5330 const char *r, *name, *defs;
5331 struct objc_module_t module;
5332 SectionRef S, xS;
5333 struct objc_symtab_t symtab;
5334 struct objc_class_t objc_class;
5335 struct objc_category_t objc_category;
5336
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005337 outs() << "Objective-C segment\n";
Kevin Enderby846c0002015-04-16 17:19:59 +00005338 S = get_section(O, "__OBJC", "__module_info");
5339 if (S == SectionRef())
5340 return false;
5341
5342 SymbolAddressMap AddrMap;
5343 if (verbose)
5344 CreateSymbolAddressMap(O, &AddrMap);
5345
5346 std::vector<SectionRef> Sections;
5347 for (const SectionRef &Section : O->sections()) {
5348 StringRef SectName;
5349 Section.getName(SectName);
5350 Sections.push_back(Section);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005351 }
Kevin Enderby846c0002015-04-16 17:19:59 +00005352
5353 struct DisassembleInfo info;
5354 // Set up the block of info used by the Symbolizer call backs.
5355 info.verbose = verbose;
5356 info.O = O;
5357 info.AddrMap = &AddrMap;
5358 info.Sections = &Sections;
5359 info.class_name = nullptr;
5360 info.selector_name = nullptr;
5361 info.method = nullptr;
5362 info.demangled_name = nullptr;
5363 info.bindtable = nullptr;
5364 info.adrp_addr = 0;
5365 info.adrp_inst = 0;
5366
5367 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5368 p = S.getAddress() + i;
5369 r = get_pointer_32(p, offset, left, S, &info, true);
5370 if (r == nullptr)
5371 return true;
5372 memset(&module, '\0', sizeof(struct objc_module_t));
5373 if (left < sizeof(struct objc_module_t)) {
5374 memcpy(&module, r, left);
5375 outs() << " (module extends past end of __module_info section)\n";
5376 } else
5377 memcpy(&module, r, sizeof(struct objc_module_t));
5378 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5379 swapStruct(module);
5380
5381 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5382 outs() << " version " << module.version << "\n";
5383 outs() << " size " << module.size << "\n";
5384 outs() << " name ";
5385 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5386 if (name != nullptr)
5387 outs() << format("%.*s", left, name);
5388 else
5389 outs() << format("0x%08" PRIx32, module.name)
5390 << "(not in an __OBJC section)";
5391 outs() << "\n";
5392
5393 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5394 if (module.symtab == 0 || r == nullptr) {
5395 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5396 << " (not in an __OBJC section)\n";
5397 continue;
5398 }
5399 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5400 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5401 defs_left = 0;
5402 defs = nullptr;
5403 if (left < sizeof(struct objc_symtab_t)) {
5404 memcpy(&symtab, r, left);
5405 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5406 } else {
5407 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5408 if (left > sizeof(struct objc_symtab_t)) {
5409 defs_left = left - sizeof(struct objc_symtab_t);
5410 defs = r + sizeof(struct objc_symtab_t);
5411 }
5412 }
5413 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5414 swapStruct(symtab);
5415
5416 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5417 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5418 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5419 if (r == nullptr)
5420 outs() << " (not in an __OBJC section)";
5421 outs() << "\n";
5422 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5423 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5424 if (symtab.cls_def_cnt > 0)
5425 outs() << "\tClass Definitions\n";
5426 for (j = 0; j < symtab.cls_def_cnt; j++) {
5427 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5428 outs() << "\t(remaining class defs entries entends past the end of the "
5429 << "section)\n";
5430 break;
5431 }
5432 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5433 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5434 sys::swapByteOrder(def);
5435
5436 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5437 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5438 if (r != nullptr) {
5439 if (left > sizeof(struct objc_class_t)) {
5440 outs() << "\n";
5441 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5442 } else {
5443 outs() << " (entends past the end of the section)\n";
5444 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5445 memcpy(&objc_class, r, left);
5446 }
5447 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5448 swapStruct(objc_class);
5449 print_objc_class_t(&objc_class, &info);
5450 } else {
5451 outs() << "(not in an __OBJC section)\n";
5452 }
5453
5454 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5455 outs() << "\tMeta Class";
5456 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5457 if (r != nullptr) {
5458 if (left > sizeof(struct objc_class_t)) {
5459 outs() << "\n";
5460 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5461 } else {
5462 outs() << " (entends past the end of the section)\n";
5463 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5464 memcpy(&objc_class, r, left);
5465 }
5466 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5467 swapStruct(objc_class);
5468 print_objc_class_t(&objc_class, &info);
5469 } else {
5470 outs() << "(not in an __OBJC section)\n";
5471 }
5472 }
5473 }
5474 if (symtab.cat_def_cnt > 0)
5475 outs() << "\tCategory Definitions\n";
5476 for (j = 0; j < symtab.cat_def_cnt; j++) {
5477 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5478 outs() << "\t(remaining category defs entries entends past the end of "
5479 << "the section)\n";
5480 break;
5481 }
5482 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5483 sizeof(uint32_t));
5484 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5485 sys::swapByteOrder(def);
5486
5487 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5488 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5489 << format("0x%08" PRIx32, def);
5490 if (r != nullptr) {
5491 if (left > sizeof(struct objc_category_t)) {
5492 outs() << "\n";
5493 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5494 } else {
5495 outs() << " (entends past the end of the section)\n";
5496 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5497 memcpy(&objc_category, r, left);
5498 }
5499 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5500 swapStruct(objc_category);
5501 print_objc_objc_category_t(&objc_category, &info);
5502 } else {
5503 outs() << "(not in an __OBJC section)\n";
5504 }
5505 }
5506 }
5507 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5508 if (II != SectionRef())
5509 print_image_info(II, &info);
5510
5511 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005512}
5513
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00005514static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5515 uint32_t size, uint32_t addr) {
5516 SymbolAddressMap AddrMap;
5517 CreateSymbolAddressMap(O, &AddrMap);
5518
5519 std::vector<SectionRef> Sections;
5520 for (const SectionRef &Section : O->sections()) {
5521 StringRef SectName;
5522 Section.getName(SectName);
5523 Sections.push_back(Section);
5524 }
5525
5526 struct DisassembleInfo info;
5527 // Set up the block of info used by the Symbolizer call backs.
5528 info.verbose = true;
5529 info.O = O;
5530 info.AddrMap = &AddrMap;
5531 info.Sections = &Sections;
5532 info.class_name = nullptr;
5533 info.selector_name = nullptr;
5534 info.method = nullptr;
5535 info.demangled_name = nullptr;
5536 info.bindtable = nullptr;
5537 info.adrp_addr = 0;
5538 info.adrp_inst = 0;
5539
5540 const char *p;
5541 struct objc_protocol_t protocol;
5542 uint32_t left, paddr;
5543 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5544 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5545 left = size - (p - sect);
5546 if (left < sizeof(struct objc_protocol_t)) {
5547 outs() << "Protocol extends past end of __protocol section\n";
5548 memcpy(&protocol, p, left);
5549 } else
5550 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5551 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5552 swapStruct(protocol);
5553 paddr = addr + (p - sect);
5554 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5555 if (print_protocol(paddr, 0, &info))
5556 outs() << "(not in an __OBJC section)\n";
5557 }
5558}
5559
Kevin Enderby0fc11822015-04-01 20:57:01 +00005560static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
5561 if (O->is64Bit())
5562 printObjc2_64bit_MetaData(O, verbose);
5563 else {
5564 MachO::mach_header H;
5565 H = O->getHeader();
5566 if (H.cputype == MachO::CPU_TYPE_ARM)
5567 printObjc2_32bit_MetaData(O, verbose);
5568 else {
5569 // This is the 32-bit non-arm cputype case. Which is normally
5570 // the first Objective-C ABI. But it may be the case of a
5571 // binary for the iOS simulator which is the second Objective-C
5572 // ABI. In that case printObjc1_32bit_MetaData() will determine that
5573 // and return false.
5574 if (printObjc1_32bit_MetaData(O, verbose) == false)
5575 printObjc2_32bit_MetaData(O, verbose);
5576 }
5577 }
5578}
5579
Kevin Enderbybf246f52014-09-24 23:08:22 +00005580// GuessLiteralPointer returns a string which for the item in the Mach-O file
5581// for the address passed in as ReferenceValue for printing as a comment with
5582// the instruction and also returns the corresponding type of that item
5583// indirectly through ReferenceType.
5584//
5585// If ReferenceValue is an address of literal cstring then a pointer to the
5586// cstring is returned and ReferenceType is set to
5587// LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
5588//
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005589// If ReferenceValue is an address of an Objective-C CFString, Selector ref or
5590// Class ref that name is returned and the ReferenceType is set accordingly.
5591//
5592// Lastly, literals which are Symbol address in a literal pool are looked for
5593// and if found the symbol name is returned and ReferenceType is set to
5594// LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
5595//
5596// If there is no item in the Mach-O file for the address passed in as
5597// ReferenceValue nullptr is returned and ReferenceType is unchanged.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00005598static const char *GuessLiteralPointer(uint64_t ReferenceValue,
5599 uint64_t ReferencePC,
5600 uint64_t *ReferenceType,
5601 struct DisassembleInfo *info) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005602 // First see if there is an external relocation entry at the ReferencePC.
Rafael Espindola80291272014-10-08 15:28:58 +00005603 uint64_t sect_addr = info->S.getAddress();
Kevin Enderbybf246f52014-09-24 23:08:22 +00005604 uint64_t sect_offset = ReferencePC - sect_addr;
5605 bool reloc_found = false;
5606 DataRefImpl Rel;
5607 MachO::any_relocation_info RE;
5608 bool isExtern = false;
5609 SymbolRef Symbol;
5610 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00005611 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderbybf246f52014-09-24 23:08:22 +00005612 if (RelocOffset == sect_offset) {
5613 Rel = Reloc.getRawDataRefImpl();
5614 RE = info->O->getRelocation(Rel);
5615 if (info->O->isRelocationScattered(RE))
5616 continue;
5617 isExtern = info->O->getPlainRelocationExternal(RE);
5618 if (isExtern) {
5619 symbol_iterator RelocSym = Reloc.getSymbol();
5620 Symbol = *RelocSym;
5621 }
5622 reloc_found = true;
5623 break;
5624 }
5625 }
5626 // If there is an external relocation entry for a symbol in a section
5627 // then used that symbol's value for the value of the reference.
5628 if (reloc_found && isExtern) {
5629 if (info->O->getAnyRelocationPCRel(RE)) {
5630 unsigned Type = info->O->getAnyRelocationType(RE);
5631 if (Type == MachO::X86_64_RELOC_SIGNED) {
Rafael Espindoladea00162015-07-03 17:44:18 +00005632 ReferenceValue = Symbol.getValue();
Kevin Enderbybf246f52014-09-24 23:08:22 +00005633 }
5634 }
5635 }
5636
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005637 // Look for literals such as Objective-C CFStrings refs, Selector refs,
5638 // Message refs and Class refs.
5639 bool classref, selref, msgref, cfstring;
5640 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
5641 selref, msgref, cfstring);
David Blaikie33dd45d02015-03-23 18:39:02 +00005642 if (classref && pointer_value == 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005643 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
5644 // And the pointer_value in that section is typically zero as it will be
5645 // set by dyld as part of the "bind information".
5646 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
5647 if (name != nullptr) {
5648 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
Hans Wennborgdb53e302014-10-23 21:59:17 +00005649 const char *class_name = strrchr(name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005650 if (class_name != nullptr && class_name[1] == '_' &&
5651 class_name[2] != '\0') {
5652 info->class_name = class_name + 2;
5653 return name;
5654 }
5655 }
5656 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005657
David Blaikie33dd45d02015-03-23 18:39:02 +00005658 if (classref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005659 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5660 const char *name =
5661 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
5662 if (name != nullptr)
5663 info->class_name = name;
5664 else
5665 name = "bad class ref";
Kevin Enderbybf246f52014-09-24 23:08:22 +00005666 return name;
5667 }
5668
David Blaikie33dd45d02015-03-23 18:39:02 +00005669 if (cfstring) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005670 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
5671 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
5672 return name;
5673 }
5674
David Blaikie33dd45d02015-03-23 18:39:02 +00005675 if (selref && pointer_value == 0)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005676 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
5677
5678 if (pointer_value != 0)
5679 ReferenceValue = pointer_value;
5680
5681 const char *name = GuessCstringPointer(ReferenceValue, info);
5682 if (name) {
David Blaikie33dd45d02015-03-23 18:39:02 +00005683 if (pointer_value != 0 && selref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005684 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
5685 info->selector_name = name;
David Blaikie33dd45d02015-03-23 18:39:02 +00005686 } else if (pointer_value != 0 && msgref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005687 info->class_name = nullptr;
5688 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
5689 info->selector_name = name;
5690 } else
5691 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
5692 return name;
5693 }
5694
5695 // Lastly look for an indirect symbol with this ReferenceValue which is in
5696 // a literal pool. If found return that symbol name.
5697 name = GuessIndirectSymbol(ReferenceValue, info);
5698 if (name) {
5699 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
5700 return name;
5701 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005702
5703 return nullptr;
5704}
5705
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005706// SymbolizerSymbolLookUp is the symbol lookup function passed when creating
Kevin Enderbybf246f52014-09-24 23:08:22 +00005707// the Symbolizer. It looks up the ReferenceValue using the info passed via the
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005708// pointer to the struct DisassembleInfo that was passed when MCSymbolizer
5709// is created and returns the symbol name that matches the ReferenceValue or
5710// nullptr if none. The ReferenceType is passed in for the IN type of
5711// reference the instruction is making from the values in defined in the header
5712// "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
5713// Out type and the ReferenceName will also be set which is added as a comment
5714// to the disassembled instruction.
5715//
Kevin Enderby04bf6932014-10-28 23:39:46 +00005716#if HAVE_CXXABI_H
5717// If the symbol name is a C++ mangled name then the demangled name is
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005718// returned through ReferenceName and ReferenceType is set to
5719// LLVMDisassembler_ReferenceType_DeMangled_Name .
Kevin Enderby04bf6932014-10-28 23:39:46 +00005720#endif
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005721//
5722// When this is called to get a symbol name for a branch target then the
5723// ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
5724// SymbolValue will be looked for in the indirect symbol table to determine if
5725// it is an address for a symbol stub. If so then the symbol name for that
5726// stub is returned indirectly through ReferenceName and then ReferenceType is
5727// set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
5728//
Kevin Enderbybf246f52014-09-24 23:08:22 +00005729// When this is called with an value loaded via a PC relative load then
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005730// ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
5731// SymbolValue is checked to be an address of literal pointer, symbol pointer,
5732// or an Objective-C meta data reference. If so the output ReferenceType is
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005733// set to correspond to that as well as setting the ReferenceName.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00005734static const char *SymbolizerSymbolLookUp(void *DisInfo,
5735 uint64_t ReferenceValue,
5736 uint64_t *ReferenceType,
5737 uint64_t ReferencePC,
5738 const char **ReferenceName) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005739 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
Kevin Enderbybf246f52014-09-24 23:08:22 +00005740 // If no verbose symbolic information is wanted then just return nullptr.
David Blaikie33dd45d02015-03-23 18:39:02 +00005741 if (!info->verbose) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005742 *ReferenceName = nullptr;
5743 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005744 return nullptr;
5745 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005746
Kevin Enderbyf6d25852015-01-31 00:37:11 +00005747 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderbybf246f52014-09-24 23:08:22 +00005748
Kevin Enderby85974882014-09-26 22:20:44 +00005749 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
5750 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005751 if (*ReferenceName != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005752 method_reference(info, ReferenceType, ReferenceName);
5753 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
5754 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
5755 } else
Kevin Enderby04bf6932014-10-28 23:39:46 +00005756#if HAVE_CXXABI_H
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005757 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00005758 if (info->demangled_name != nullptr)
5759 free(info->demangled_name);
5760 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005761 info->demangled_name =
5762 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005763 if (info->demangled_name != nullptr) {
5764 *ReferenceName = info->demangled_name;
5765 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5766 } else
5767 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5768 } else
5769#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005770 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5771 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
5772 *ReferenceName =
5773 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
Kevin Enderby85974882014-09-26 22:20:44 +00005774 if (*ReferenceName)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005775 method_reference(info, ReferenceType, ReferenceName);
Kevin Enderby85974882014-09-26 22:20:44 +00005776 else
5777 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005778 // If this is arm64 and the reference is an adrp instruction save the
5779 // instruction, passed in ReferenceValue and the address of the instruction
5780 // for use later if we see and add immediate instruction.
5781 } else if (info->O->getArch() == Triple::aarch64 &&
5782 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
5783 info->adrp_inst = ReferenceValue;
5784 info->adrp_addr = ReferencePC;
5785 SymbolName = nullptr;
5786 *ReferenceName = nullptr;
5787 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5788 // If this is arm64 and reference is an add immediate instruction and we
5789 // have
5790 // seen an adrp instruction just before it and the adrp's Xd register
5791 // matches
5792 // this add's Xn register reconstruct the value being referenced and look to
5793 // see if it is a literal pointer. Note the add immediate instruction is
5794 // passed in ReferenceValue.
5795 } else if (info->O->getArch() == Triple::aarch64 &&
5796 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
5797 ReferencePC - 4 == info->adrp_addr &&
5798 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5799 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5800 uint32_t addxri_inst;
5801 uint64_t adrp_imm, addxri_imm;
5802
5803 adrp_imm =
5804 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5805 if (info->adrp_inst & 0x0200000)
5806 adrp_imm |= 0xfffffffffc000000LL;
5807
5808 addxri_inst = ReferenceValue;
5809 addxri_imm = (addxri_inst >> 10) & 0xfff;
5810 if (((addxri_inst >> 22) & 0x3) == 1)
5811 addxri_imm <<= 12;
5812
5813 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5814 (adrp_imm << 12) + addxri_imm;
5815
5816 *ReferenceName =
5817 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5818 if (*ReferenceName == nullptr)
5819 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5820 // If this is arm64 and the reference is a load register instruction and we
5821 // have seen an adrp instruction just before it and the adrp's Xd register
5822 // matches this add's Xn register reconstruct the value being referenced and
5823 // look to see if it is a literal pointer. Note the load register
5824 // instruction is passed in ReferenceValue.
5825 } else if (info->O->getArch() == Triple::aarch64 &&
5826 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
5827 ReferencePC - 4 == info->adrp_addr &&
5828 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5829 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5830 uint32_t ldrxui_inst;
5831 uint64_t adrp_imm, ldrxui_imm;
5832
5833 adrp_imm =
5834 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5835 if (info->adrp_inst & 0x0200000)
5836 adrp_imm |= 0xfffffffffc000000LL;
5837
5838 ldrxui_inst = ReferenceValue;
5839 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
5840
5841 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5842 (adrp_imm << 12) + (ldrxui_imm << 3);
5843
5844 *ReferenceName =
5845 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5846 if (*ReferenceName == nullptr)
5847 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5848 }
5849 // If this arm64 and is an load register (PC-relative) instruction the
5850 // ReferenceValue is the PC plus the immediate value.
5851 else if (info->O->getArch() == Triple::aarch64 &&
5852 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
5853 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
5854 *ReferenceName =
5855 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5856 if (*ReferenceName == nullptr)
5857 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby85974882014-09-26 22:20:44 +00005858 }
Kevin Enderby04bf6932014-10-28 23:39:46 +00005859#if HAVE_CXXABI_H
5860 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5861 if (info->demangled_name != nullptr)
5862 free(info->demangled_name);
5863 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005864 info->demangled_name =
5865 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005866 if (info->demangled_name != nullptr) {
5867 *ReferenceName = info->demangled_name;
5868 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5869 }
5870 }
5871#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005872 else {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005873 *ReferenceName = nullptr;
5874 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5875 }
5876
5877 return SymbolName;
5878}
5879
Kevin Enderbybf246f52014-09-24 23:08:22 +00005880/// \brief Emits the comments that are stored in the CommentStream.
5881/// Each comment in the CommentStream must end with a newline.
5882static void emitComments(raw_svector_ostream &CommentStream,
5883 SmallString<128> &CommentsToEmit,
5884 formatted_raw_ostream &FormattedOS,
5885 const MCAsmInfo &MAI) {
5886 // Flush the stream before taking its content.
5887 CommentStream.flush();
5888 StringRef Comments = CommentsToEmit.str();
5889 // Get the default information for printing a comment.
5890 const char *CommentBegin = MAI.getCommentString();
5891 unsigned CommentColumn = MAI.getCommentColumn();
5892 bool IsFirst = true;
5893 while (!Comments.empty()) {
5894 if (!IsFirst)
5895 FormattedOS << '\n';
5896 // Emit a line of comments.
5897 FormattedOS.PadToColumn(CommentColumn);
5898 size_t Position = Comments.find('\n');
5899 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
5900 // Move after the newline character.
5901 Comments = Comments.substr(Position + 1);
5902 IsFirst = false;
5903 }
5904 FormattedOS.flush();
5905
5906 // Tell the comment stream that the vector changed underneath it.
5907 CommentsToEmit.clear();
5908 CommentStream.resync();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005909}
5910
Kevin Enderby95df54c2015-02-04 01:01:38 +00005911static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
5912 StringRef DisSegName, StringRef DisSectName) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005913 const char *McpuDefault = nullptr;
5914 const Target *ThumbTarget = nullptr;
5915 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005916 if (!TheTarget) {
5917 // GetTarget prints out stuff.
5918 return;
5919 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005920 if (MCPU.empty() && McpuDefault)
5921 MCPU = McpuDefault;
5922
Ahmed Charles56440fd2014-03-06 05:51:42 +00005923 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005924 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005925 if (ThumbTarget)
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005926 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005927
Kevin Enderbyc9595622014-08-06 23:24:41 +00005928 // Package up features to be passed to target/subtarget
5929 std::string FeaturesStr;
5930 if (MAttrs.size()) {
5931 SubtargetFeatures Features;
5932 for (unsigned i = 0; i != MAttrs.size(); ++i)
5933 Features.AddFeature(MAttrs[i]);
5934 FeaturesStr = Features.getString();
5935 }
5936
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005937 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00005938 std::unique_ptr<const MCRegisterInfo> MRI(
5939 TheTarget->createMCRegInfo(TripleName));
5940 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +00005941 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +00005942 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +00005943 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Craig Toppere6cb63e2014-04-25 04:24:47 +00005944 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005945 std::unique_ptr<MCDisassembler> DisAsm(
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005946 TheTarget->createMCDisassembler(*STI, Ctx));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005947 std::unique_ptr<MCSymbolizer> Symbolizer;
5948 struct DisassembleInfo SymbolizerInfo;
5949 std::unique_ptr<MCRelocationInfo> RelInfo(
5950 TheTarget->createMCRelocationInfo(TripleName, Ctx));
5951 if (RelInfo) {
5952 Symbolizer.reset(TheTarget->createMCSymbolizer(
5953 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00005954 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005955 DisAsm->setSymbolizer(std::move(Symbolizer));
5956 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005957 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +00005958 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +00005959 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00005960 // Set the display preference for hex vs. decimal immediates.
5961 IP->setPrintImmHex(PrintImmHex);
5962 // Comment stream and backing vector.
5963 SmallString<128> CommentsToEmit;
5964 raw_svector_ostream CommentStream(CommentsToEmit);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00005965 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
5966 // if it is done then arm64 comments for string literals don't get printed
5967 // and some constant get printed instead and not setting it causes intel
5968 // (32-bit and 64-bit) comments printed with different spacing before the
5969 // comment causing different diffs with the 'C' disassembler library API.
5970 // IP->setCommentStream(CommentStream);
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005971
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005972 if (!AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +00005973 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005974 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005975 return;
5976 }
5977
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005978 // Set up thumb disassembler.
5979 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
5980 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
5981 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005982 std::unique_ptr<MCDisassembler> ThumbDisAsm;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005983 std::unique_ptr<MCInstPrinter> ThumbIP;
5984 std::unique_ptr<MCContext> ThumbCtx;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005985 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
5986 struct DisassembleInfo ThumbSymbolizerInfo;
5987 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005988 if (ThumbTarget) {
5989 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
5990 ThumbAsmInfo.reset(
5991 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
5992 ThumbSTI.reset(
5993 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
5994 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
5995 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
Kevin Enderby930fdc72014-11-06 19:00:13 +00005996 MCContext *PtrThumbCtx = ThumbCtx.get();
5997 ThumbRelInfo.reset(
5998 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
5999 if (ThumbRelInfo) {
6000 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6001 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00006002 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006003 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6004 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006005 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6006 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +00006007 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6008 *ThumbInstrInfo, *ThumbMRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006009 // Set the display preference for hex vs. decimal immediates.
6010 ThumbIP->setPrintImmHex(PrintImmHex);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006011 }
6012
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006013 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006014 errs() << "error: couldn't initialize disassembler for target "
6015 << ThumbTripleName << '\n';
6016 return;
6017 }
6018
Charles Davis8bdfafd2013-09-01 04:28:48 +00006019 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006020
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006021 // FIXME: Using the -cfg command line option, this code used to be able to
6022 // annotate relocations with the referenced symbol's name, and if this was
6023 // inside a __[cf]string section, the data it points to. This is now replaced
6024 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +00006025 std::vector<SectionRef> Sections;
6026 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006027 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +00006028 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006029
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00006030 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
Kevin Enderby273ae012013-06-06 17:20:50 +00006031 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006032
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006033 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +00006034 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006035
Kevin Enderby273ae012013-06-06 17:20:50 +00006036 // Build a data in code table that is sorted on by the address of each entry.
6037 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +00006038 if (Header.filetype == MachO::MH_OBJECT)
Rafael Espindola80291272014-10-08 15:28:58 +00006039 BaseAddress = Sections[0].getAddress();
Kevin Enderby273ae012013-06-06 17:20:50 +00006040 else
6041 BaseAddress = BaseSegmentAddress;
6042 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +00006043 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +00006044 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006045 uint32_t Offset;
6046 DI->getOffset(Offset);
6047 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6048 }
6049 array_pod_sort(Dices.begin(), Dices.end());
6050
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006051#ifndef NDEBUG
6052 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6053#else
6054 raw_ostream &DebugOut = nulls();
6055#endif
6056
Ahmed Charles56440fd2014-03-06 05:51:42 +00006057 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +00006058 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +00006059 // Try to find debug info and set up the DIContext for it.
6060 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +00006061 // A separate DSym file path was specified, parse it as a macho file,
6062 // get the sections and supply it to the section name parsing machinery.
6063 if (!DSYMFile.empty()) {
Rafael Espindola48af1c22014-08-19 18:44:46 +00006064 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006065 MemoryBuffer::getFileOrSTDIN(DSYMFile);
Rafael Espindola48af1c22014-08-19 18:44:46 +00006066 if (std::error_code EC = BufOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006067 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
Benjamin Kramer699128e2011-09-21 01:13:19 +00006068 return;
6069 }
Rafael Espindola48af1c22014-08-19 18:44:46 +00006070 DbgObj =
6071 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6072 .get()
6073 .release();
Benjamin Kramer699128e2011-09-21 01:13:19 +00006074 }
6075
Eric Christopher7370b552012-11-12 21:40:38 +00006076 // Setup the DIContext
Zachary Turner6489d7b2015-04-23 17:37:47 +00006077 diContext.reset(new DWARFContextInMemory(*DbgObj));
Benjamin Kramer699128e2011-09-21 01:13:19 +00006078 }
6079
Kevin Enderby95df54c2015-02-04 01:01:38 +00006080 if (DumpSections.size() == 0)
6081 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006082
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006083 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00006084 StringRef SectName;
Kevin Enderby95df54c2015-02-04 01:01:38 +00006085 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6086 continue;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006087
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006088 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006089
Rafael Espindolab0f76a42013-04-05 15:15:22 +00006090 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
Kevin Enderby95df54c2015-02-04 01:01:38 +00006091 if (SegmentName != DisSegName)
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006092 continue;
6093
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006094 StringRef BytesStr;
6095 Sections[SectIdx].getContents(BytesStr);
Aaron Ballman106fd7b2014-11-12 14:01:17 +00006096 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6097 BytesStr.size());
Rafael Espindola80291272014-10-08 15:28:58 +00006098 uint64_t SectAddress = Sections[SectIdx].getAddress();
Rafael Espindolabd604f22014-11-07 00:52:15 +00006099
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006100 bool symbolTableWorked = false;
6101
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006102 // Parse relocations.
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00006103 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
6104 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00006105 uint64_t RelocOffset = Reloc.getOffset();
Rafael Espindola80291272014-10-08 15:28:58 +00006106 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Owen Andersond9243c42011-10-17 21:37:35 +00006107 RelocOffset -= SectionAddress;
6108
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00006109 symbol_iterator RelocSym = Reloc.getSymbol();
Owen Andersond9243c42011-10-17 21:37:35 +00006110
Rafael Espindola806f0062013-06-05 01:33:53 +00006111 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006112 }
6113 array_pod_sort(Relocs.begin(), Relocs.end());
6114
Kevin Enderbybf246f52014-09-24 23:08:22 +00006115 // Create a map of symbol addresses to symbol names for use by
6116 // the SymbolizerSymbolLookUp() routine.
6117 SymbolAddressMap AddrMap;
Kevin Enderby6a221752015-03-17 17:10:57 +00006118 bool DisSymNameFound = false;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006119 for (const SymbolRef &Symbol : MachOOF->symbols()) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006120 SymbolRef::Type ST = Symbol.getType();
Kevin Enderbybf246f52014-09-24 23:08:22 +00006121 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6122 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +00006123 uint64_t Address = Symbol.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006124 ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
6125 if (std::error_code EC = SymNameOrErr.getError())
6126 report_fatal_error(EC.message());
6127 StringRef SymName = *SymNameOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006128 AddrMap[Address] = SymName;
Kevin Enderby6a221752015-03-17 17:10:57 +00006129 if (!DisSymName.empty() && DisSymName == SymName)
6130 DisSymNameFound = true;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006131 }
6132 }
David Blaikie33dd45d02015-03-23 18:39:02 +00006133 if (!DisSymName.empty() && !DisSymNameFound) {
Kevin Enderby6a221752015-03-17 17:10:57 +00006134 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6135 return;
6136 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006137 // Set up the block of info used by the Symbolizer call backs.
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006138 SymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006139 SymbolizerInfo.O = MachOOF;
6140 SymbolizerInfo.S = Sections[SectIdx];
Kevin Enderbybf246f52014-09-24 23:08:22 +00006141 SymbolizerInfo.AddrMap = &AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006142 SymbolizerInfo.Sections = &Sections;
6143 SymbolizerInfo.class_name = nullptr;
6144 SymbolizerInfo.selector_name = nullptr;
6145 SymbolizerInfo.method = nullptr;
Kevin Enderby04bf6932014-10-28 23:39:46 +00006146 SymbolizerInfo.demangled_name = nullptr;
Kevin Enderby078be602014-10-23 19:53:12 +00006147 SymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006148 SymbolizerInfo.adrp_addr = 0;
6149 SymbolizerInfo.adrp_inst = 0;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006150 // Same for the ThumbSymbolizer
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006151 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006152 ThumbSymbolizerInfo.O = MachOOF;
6153 ThumbSymbolizerInfo.S = Sections[SectIdx];
6154 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6155 ThumbSymbolizerInfo.Sections = &Sections;
6156 ThumbSymbolizerInfo.class_name = nullptr;
6157 ThumbSymbolizerInfo.selector_name = nullptr;
6158 ThumbSymbolizerInfo.method = nullptr;
6159 ThumbSymbolizerInfo.demangled_name = nullptr;
6160 ThumbSymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006161 ThumbSymbolizerInfo.adrp_addr = 0;
6162 ThumbSymbolizerInfo.adrp_inst = 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006163
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006164 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006165 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006166 ErrorOr<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
6167 if (std::error_code EC = SymNameOrErr.getError())
6168 report_fatal_error(EC.message());
6169 StringRef SymName = *SymNameOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006170
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006171 SymbolRef::Type ST = Symbols[SymIdx].getType();
Owen Andersond9243c42011-10-17 21:37:35 +00006172 if (ST != SymbolRef::ST_Function)
6173 continue;
6174
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006175 // Make sure the symbol is defined in this section.
Rafael Espindola80291272014-10-08 15:28:58 +00006176 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Owen Andersond9243c42011-10-17 21:37:35 +00006177 if (!containsSym)
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006178 continue;
6179
Kevin Enderby6a221752015-03-17 17:10:57 +00006180 // If we are only disassembling one symbol see if this is that symbol.
6181 if (!DisSymName.empty() && DisSymName != SymName)
6182 continue;
6183
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006184 // Start at the address of the symbol relative to the section's address.
Rafael Espindoladea00162015-07-03 17:44:18 +00006185 uint64_t Start = Symbols[SymIdx].getValue();
Rafael Espindola80291272014-10-08 15:28:58 +00006186 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006187 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006188
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006189 // Stop disassembling either at the beginning of the next symbol or at
6190 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +00006191 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +00006192 uint64_t NextSym = 0;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006193 uint64_t NextSymIdx = SymIdx + 1;
Owen Andersond9243c42011-10-17 21:37:35 +00006194 while (Symbols.size() > NextSymIdx) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006195 SymbolRef::Type NextSymType = Symbols[NextSymIdx].getType();
Owen Andersond9243c42011-10-17 21:37:35 +00006196 if (NextSymType == SymbolRef::ST_Function) {
Rafael Espindola80291272014-10-08 15:28:58 +00006197 containsNextSym =
6198 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Rafael Espindoladea00162015-07-03 17:44:18 +00006199 NextSym = Symbols[NextSymIdx].getValue();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006200 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006201 break;
6202 }
6203 ++NextSymIdx;
6204 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006205
Rafael Espindola80291272014-10-08 15:28:58 +00006206 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006207 uint64_t End = containsNextSym ? NextSym : SectSize;
Owen Andersond9243c42011-10-17 21:37:35 +00006208 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006209
6210 symbolTableWorked = true;
Rafael Espindolabd604f22014-11-07 00:52:15 +00006211
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006212 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6213 bool isThumb =
6214 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
6215
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006216 outs() << SymName << ":\n";
6217 DILineInfo lastLine;
6218 for (uint64_t Index = Start; Index < End; Index += Size) {
6219 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +00006220
Kevin Enderbybf246f52014-09-24 23:08:22 +00006221 uint64_t PC = SectAddress + Index;
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006222 if (!NoLeadingAddr) {
6223 if (FullLeadingAddr) {
6224 if (MachOOF->is64Bit())
6225 outs() << format("%016" PRIx64, PC);
6226 else
6227 outs() << format("%08" PRIx64, PC);
6228 } else {
6229 outs() << format("%8" PRIx64 ":", PC);
6230 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006231 }
6232 if (!NoShowRawInsn)
6233 outs() << "\t";
Kevin Enderby273ae012013-06-06 17:20:50 +00006234
6235 // Check the data in code table here to see if this is data not an
6236 // instruction to be disassembled.
6237 DiceTable Dice;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006238 Dice.push_back(std::make_pair(PC, DiceRef()));
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006239 dice_table_iterator DTI =
6240 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6241 compareDiceTableEntries);
6242 if (DTI != Dices.end()) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006243 uint16_t Length;
6244 DTI->second.getLength(Length);
Kevin Enderby273ae012013-06-06 17:20:50 +00006245 uint16_t Kind;
6246 DTI->second.getKind(Kind);
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00006247 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006248 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6249 (PC == (DTI->first + Length - 1)) && (Length & 1))
6250 Size++;
Kevin Enderby273ae012013-06-06 17:20:50 +00006251 continue;
6252 }
6253
Kevin Enderbybf246f52014-09-24 23:08:22 +00006254 SmallVector<char, 64> AnnotationsBytes;
6255 raw_svector_ostream Annotations(AnnotationsBytes);
6256
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006257 bool gotInst;
6258 if (isThumb)
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006259 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006260 PC, DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006261 else
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006262 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
Kevin Enderbybf246f52014-09-24 23:08:22 +00006263 DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006264 if (gotInst) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006265 if (!NoShowRawInsn) {
Colin LeMahieu2048ea42015-05-28 18:39:50 +00006266 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006267 }
6268 formatted_raw_ostream FormattedOS(outs());
6269 Annotations.flush();
6270 StringRef AnnotationsStr = Annotations.str();
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006271 if (isThumb)
Akira Hatanakab46d0232015-03-27 20:36:02 +00006272 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006273 else
Akira Hatanaka1d079942015-03-28 20:44:05 +00006274 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006275 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
Owen Andersond9243c42011-10-17 21:37:35 +00006276
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006277 // Print debug info.
6278 if (diContext) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006279 DILineInfo dli = diContext->getLineInfoForAddress(PC);
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006280 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +00006281 if (dli != lastLine && dli.Line != 0)
6282 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6283 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006284 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006285 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006286 outs() << "\n";
6287 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006288 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006289 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006290 outs() << format("\t.byte 0x%02x #bad opcode\n",
6291 *(Bytes.data() + Index) & 0xff);
6292 Size = 1; // skip exactly one illegible byte and move on.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006293 } else if (Arch == Triple::aarch64) {
6294 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6295 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6296 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6297 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6298 outs() << format("\t.long\t0x%08x\n", opcode);
6299 Size = 4;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006300 } else {
6301 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6302 if (Size == 0)
6303 Size = 1; // skip illegible bytes
6304 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006305 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006306 }
6307 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006308 if (!symbolTableWorked) {
Rafael Espindola80291272014-10-08 15:28:58 +00006309 // Reading the symbol table didn't work, disassemble the whole section.
6310 uint64_t SectAddress = Sections[SectIdx].getAddress();
6311 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbybadd1002012-05-18 00:13:56 +00006312 uint64_t InstSize;
6313 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +00006314 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +00006315
Kevin Enderbybf246f52014-09-24 23:08:22 +00006316 uint64_t PC = SectAddress + Index;
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006317 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6318 DebugOut, nulls())) {
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006319 if (!NoLeadingAddr) {
6320 if (FullLeadingAddr) {
6321 if (MachOOF->is64Bit())
6322 outs() << format("%016" PRIx64, PC);
6323 else
6324 outs() << format("%08" PRIx64, PC);
6325 } else {
6326 outs() << format("%8" PRIx64 ":", PC);
6327 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006328 }
6329 if (!NoShowRawInsn) {
6330 outs() << "\t";
Colin LeMahieu2048ea42015-05-28 18:39:50 +00006331 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006332 }
Akira Hatanaka1d079942015-03-28 20:44:05 +00006333 IP->printInst(&Inst, outs(), "", *STI);
Bill Wendling4e68e062012-07-19 00:17:40 +00006334 outs() << "\n";
6335 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006336 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006337 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006338 outs() << format("\t.byte 0x%02x #bad opcode\n",
6339 *(Bytes.data() + Index) & 0xff);
6340 InstSize = 1; // skip exactly one illegible byte and move on.
6341 } else {
6342 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6343 if (InstSize == 0)
6344 InstSize = 1; // skip illegible bytes
6345 }
Bill Wendling4e68e062012-07-19 00:17:40 +00006346 }
Kevin Enderbybadd1002012-05-18 00:13:56 +00006347 }
6348 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006349 // The TripleName's need to be reset if we are called again for a different
6350 // archtecture.
6351 TripleName = "";
6352 ThumbTripleName = "";
6353
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006354 if (SymbolizerInfo.method != nullptr)
6355 free(SymbolizerInfo.method);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006356 if (SymbolizerInfo.demangled_name != nullptr)
6357 free(SymbolizerInfo.demangled_name);
Kevin Enderby078be602014-10-23 19:53:12 +00006358 if (SymbolizerInfo.bindtable != nullptr)
6359 delete SymbolizerInfo.bindtable;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006360 if (ThumbSymbolizerInfo.method != nullptr)
6361 free(ThumbSymbolizerInfo.method);
6362 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6363 free(ThumbSymbolizerInfo.demangled_name);
6364 if (ThumbSymbolizerInfo.bindtable != nullptr)
6365 delete ThumbSymbolizerInfo.bindtable;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006366 }
6367}
Tim Northover4bd286a2014-08-01 13:07:19 +00006368
Tim Northover39c70bb2014-08-12 11:52:59 +00006369//===----------------------------------------------------------------------===//
6370// __compact_unwind section dumping
6371//===----------------------------------------------------------------------===//
6372
Tim Northover4bd286a2014-08-01 13:07:19 +00006373namespace {
Tim Northover39c70bb2014-08-12 11:52:59 +00006374
6375template <typename T> static uint64_t readNext(const char *&Buf) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006376 using llvm::support::little;
6377 using llvm::support::unaligned;
Tim Northover39c70bb2014-08-12 11:52:59 +00006378
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006379 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6380 Buf += sizeof(T);
6381 return Val;
6382}
Tim Northover39c70bb2014-08-12 11:52:59 +00006383
Tim Northover4bd286a2014-08-01 13:07:19 +00006384struct CompactUnwindEntry {
6385 uint32_t OffsetInSection;
6386
6387 uint64_t FunctionAddr;
6388 uint32_t Length;
6389 uint32_t CompactEncoding;
6390 uint64_t PersonalityAddr;
6391 uint64_t LSDAAddr;
6392
6393 RelocationRef FunctionReloc;
6394 RelocationRef PersonalityReloc;
6395 RelocationRef LSDAReloc;
6396
6397 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006398 : OffsetInSection(Offset) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006399 if (Is64)
6400 read<uint64_t>(Contents.data() + Offset);
6401 else
6402 read<uint32_t>(Contents.data() + Offset);
6403 }
6404
6405private:
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006406 template <typename UIntPtr> void read(const char *Buf) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006407 FunctionAddr = readNext<UIntPtr>(Buf);
6408 Length = readNext<uint32_t>(Buf);
6409 CompactEncoding = readNext<uint32_t>(Buf);
6410 PersonalityAddr = readNext<UIntPtr>(Buf);
6411 LSDAAddr = readNext<UIntPtr>(Buf);
6412 }
6413};
6414}
6415
6416/// Given a relocation from __compact_unwind, consisting of the RelocationRef
6417/// and data being relocated, determine the best base Name and Addend to use for
6418/// display purposes.
6419///
6420/// 1. An Extern relocation will directly reference a symbol (and the data is
6421/// then already an addend), so use that.
6422/// 2. Otherwise the data is an offset in the object file's layout; try to find
6423// a symbol before it in the same section, and use the offset from there.
6424/// 3. Finally, if all that fails, fall back to an offset from the start of the
6425/// referenced section.
6426static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6427 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006428 const RelocationRef &Reloc, uint64_t Addr,
Tim Northover4bd286a2014-08-01 13:07:19 +00006429 StringRef &Name, uint64_t &Addend) {
6430 if (Reloc.getSymbol() != Obj->symbol_end()) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006431 ErrorOr<StringRef> NameOrErr = Reloc.getSymbol()->getName();
6432 if (std::error_code EC = NameOrErr.getError())
6433 report_fatal_error(EC.message());
6434 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00006435 Addend = Addr;
6436 return;
6437 }
6438
6439 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
Keno Fischerc780e8e2015-05-21 21:24:32 +00006440 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
Tim Northover4bd286a2014-08-01 13:07:19 +00006441
Rafael Espindola80291272014-10-08 15:28:58 +00006442 uint64_t SectionAddr = RelocSection.getAddress();
Tim Northover4bd286a2014-08-01 13:07:19 +00006443
6444 auto Sym = Symbols.upper_bound(Addr);
6445 if (Sym == Symbols.begin()) {
6446 // The first symbol in the object is after this reference, the best we can
6447 // do is section-relative notation.
6448 RelocSection.getName(Name);
6449 Addend = Addr - SectionAddr;
6450 return;
6451 }
6452
6453 // Go back one so that SymbolAddress <= Addr.
6454 --Sym;
6455
6456 section_iterator SymSection = Obj->section_end();
6457 Sym->second.getSection(SymSection);
6458 if (RelocSection == *SymSection) {
6459 // There's a valid symbol in the same section before this reference.
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006460 ErrorOr<StringRef> NameOrErr = Sym->second.getName();
6461 if (std::error_code EC = NameOrErr.getError())
6462 report_fatal_error(EC.message());
6463 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00006464 Addend = Addr - Sym->first;
6465 return;
6466 }
6467
6468 // There is a symbol before this reference, but it's in a different
6469 // section. Probably not helpful to mention it, so use the section name.
6470 RelocSection.getName(Name);
6471 Addend = Addr - SectionAddr;
6472}
6473
6474static void printUnwindRelocDest(const MachOObjectFile *Obj,
6475 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006476 const RelocationRef &Reloc, uint64_t Addr) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006477 StringRef Name;
6478 uint64_t Addend;
6479
Rafael Espindola854038e2015-06-26 14:51:16 +00006480 if (!Reloc.getObject())
Tim Northover0b0add52014-09-09 10:45:06 +00006481 return;
6482
Tim Northover4bd286a2014-08-01 13:07:19 +00006483 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6484
6485 outs() << Name;
6486 if (Addend)
Tim Northover63a25622014-08-11 09:14:06 +00006487 outs() << " + " << format("0x%" PRIx64, Addend);
Tim Northover4bd286a2014-08-01 13:07:19 +00006488}
6489
6490static void
6491printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6492 std::map<uint64_t, SymbolRef> &Symbols,
6493 const SectionRef &CompactUnwind) {
6494
6495 assert(Obj->isLittleEndian() &&
6496 "There should not be a big-endian .o with __compact_unwind");
6497
6498 bool Is64 = Obj->is64Bit();
6499 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6500 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6501
6502 StringRef Contents;
6503 CompactUnwind.getContents(Contents);
6504
6505 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6506
6507 // First populate the initial raw offsets, encodings and so on from the entry.
6508 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6509 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
6510 CompactUnwinds.push_back(Entry);
6511 }
6512
6513 // Next we need to look at the relocations to find out what objects are
6514 // actually being referred to.
6515 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00006516 uint64_t RelocAddress = Reloc.getOffset();
Tim Northover4bd286a2014-08-01 13:07:19 +00006517
6518 uint32_t EntryIdx = RelocAddress / EntrySize;
6519 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
6520 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
6521
6522 if (OffsetInEntry == 0)
6523 Entry.FunctionReloc = Reloc;
6524 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
6525 Entry.PersonalityReloc = Reloc;
6526 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
6527 Entry.LSDAReloc = Reloc;
6528 else
6529 llvm_unreachable("Unexpected relocation in __compact_unwind section");
6530 }
6531
6532 // Finally, we're ready to print the data we've gathered.
6533 outs() << "Contents of __compact_unwind section:\n";
6534 for (auto &Entry : CompactUnwinds) {
Tim Northover06af2602014-08-08 12:08:51 +00006535 outs() << " Entry at offset "
6536 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00006537
6538 // 1. Start of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006539 outs() << " start: " << format("0x%" PRIx64,
6540 Entry.FunctionAddr) << ' ';
6541 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
Tim Northover4bd286a2014-08-01 13:07:19 +00006542 outs() << '\n';
6543
6544 // 2. Length of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006545 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
6546 << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00006547 // 3. The 32-bit compact encoding.
6548 outs() << " compact encoding: "
Tim Northoverb911bf82014-08-08 12:00:09 +00006549 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00006550
6551 // 4. The personality function, if present.
Rafael Espindola854038e2015-06-26 14:51:16 +00006552 if (Entry.PersonalityReloc.getObject()) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006553 outs() << " personality function: "
Tim Northoverb911bf82014-08-08 12:00:09 +00006554 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00006555 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
6556 Entry.PersonalityAddr);
6557 outs() << '\n';
6558 }
6559
6560 // 5. This entry's language-specific data area.
Rafael Espindola854038e2015-06-26 14:51:16 +00006561 if (Entry.LSDAReloc.getObject()) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006562 outs() << " LSDA: " << format("0x%" PRIx64,
6563 Entry.LSDAAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00006564 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
6565 outs() << '\n';
6566 }
6567 }
6568}
6569
Tim Northover39c70bb2014-08-12 11:52:59 +00006570//===----------------------------------------------------------------------===//
6571// __unwind_info section dumping
6572//===----------------------------------------------------------------------===//
6573
6574static void printRegularSecondLevelUnwindPage(const char *PageStart) {
6575 const char *Pos = PageStart;
6576 uint32_t Kind = readNext<uint32_t>(Pos);
6577 (void)Kind;
6578 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
6579
6580 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6581 uint16_t NumEntries = readNext<uint16_t>(Pos);
6582
6583 Pos = PageStart + EntriesStart;
6584 for (unsigned i = 0; i < NumEntries; ++i) {
6585 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6586 uint32_t Encoding = readNext<uint32_t>(Pos);
6587
6588 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006589 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6590 << ", "
6591 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006592 }
6593}
6594
6595static void printCompressedSecondLevelUnwindPage(
6596 const char *PageStart, uint32_t FunctionBase,
6597 const SmallVectorImpl<uint32_t> &CommonEncodings) {
6598 const char *Pos = PageStart;
6599 uint32_t Kind = readNext<uint32_t>(Pos);
6600 (void)Kind;
6601 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
6602
6603 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6604 uint16_t NumEntries = readNext<uint16_t>(Pos);
6605
6606 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
6607 readNext<uint16_t>(Pos);
Aaron Ballman80930af2014-08-14 13:53:19 +00006608 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
6609 PageStart + EncodingsStart);
Tim Northover39c70bb2014-08-12 11:52:59 +00006610
6611 Pos = PageStart + EntriesStart;
6612 for (unsigned i = 0; i < NumEntries; ++i) {
6613 uint32_t Entry = readNext<uint32_t>(Pos);
6614 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
6615 uint32_t EncodingIdx = Entry >> 24;
6616
6617 uint32_t Encoding;
6618 if (EncodingIdx < CommonEncodings.size())
6619 Encoding = CommonEncodings[EncodingIdx];
6620 else
6621 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
6622
6623 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006624 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6625 << ", "
6626 << "encoding[" << EncodingIdx
6627 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006628 }
6629}
6630
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006631static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
6632 std::map<uint64_t, SymbolRef> &Symbols,
6633 const SectionRef &UnwindInfo) {
Tim Northover39c70bb2014-08-12 11:52:59 +00006634
6635 assert(Obj->isLittleEndian() &&
6636 "There should not be a big-endian .o with __unwind_info");
6637
6638 outs() << "Contents of __unwind_info section:\n";
6639
6640 StringRef Contents;
6641 UnwindInfo.getContents(Contents);
6642 const char *Pos = Contents.data();
6643
6644 //===----------------------------------
6645 // Section header
6646 //===----------------------------------
6647
6648 uint32_t Version = readNext<uint32_t>(Pos);
6649 outs() << " Version: "
6650 << format("0x%" PRIx32, Version) << '\n';
6651 assert(Version == 1 && "only understand version 1");
6652
6653 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
6654 outs() << " Common encodings array section offset: "
6655 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
6656 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
6657 outs() << " Number of common encodings in array: "
6658 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
6659
6660 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
6661 outs() << " Personality function array section offset: "
6662 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
6663 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
6664 outs() << " Number of personality functions in array: "
6665 << format("0x%" PRIx32, NumPersonalities) << '\n';
6666
6667 uint32_t IndicesStart = readNext<uint32_t>(Pos);
6668 outs() << " Index array section offset: "
6669 << format("0x%" PRIx32, IndicesStart) << '\n';
6670 uint32_t NumIndices = readNext<uint32_t>(Pos);
6671 outs() << " Number of indices in array: "
6672 << format("0x%" PRIx32, NumIndices) << '\n';
6673
6674 //===----------------------------------
6675 // A shared list of common encodings
6676 //===----------------------------------
6677
6678 // These occupy indices in the range [0, N] whenever an encoding is referenced
6679 // from a compressed 2nd level index table. In practice the linker only
6680 // creates ~128 of these, so that indices are available to embed encodings in
6681 // the 2nd level index.
6682
6683 SmallVector<uint32_t, 64> CommonEncodings;
6684 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
6685 Pos = Contents.data() + CommonEncodingsStart;
6686 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
6687 uint32_t Encoding = readNext<uint32_t>(Pos);
6688 CommonEncodings.push_back(Encoding);
6689
6690 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
6691 << '\n';
6692 }
6693
Tim Northover39c70bb2014-08-12 11:52:59 +00006694 //===----------------------------------
6695 // Personality functions used in this executable
6696 //===----------------------------------
6697
6698 // There should be only a handful of these (one per source language,
6699 // roughly). Particularly since they only get 2 bits in the compact encoding.
6700
6701 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
6702 Pos = Contents.data() + PersonalitiesStart;
6703 for (unsigned i = 0; i < NumPersonalities; ++i) {
6704 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
6705 outs() << " personality[" << i + 1
6706 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
6707 }
6708
6709 //===----------------------------------
6710 // The level 1 index entries
6711 //===----------------------------------
6712
6713 // These specify an approximate place to start searching for the more detailed
6714 // information, sorted by PC.
6715
6716 struct IndexEntry {
6717 uint32_t FunctionOffset;
6718 uint32_t SecondLevelPageStart;
6719 uint32_t LSDAStart;
6720 };
6721
6722 SmallVector<IndexEntry, 4> IndexEntries;
6723
6724 outs() << " Top level indices: (count = " << NumIndices << ")\n";
6725 Pos = Contents.data() + IndicesStart;
6726 for (unsigned i = 0; i < NumIndices; ++i) {
6727 IndexEntry Entry;
6728
6729 Entry.FunctionOffset = readNext<uint32_t>(Pos);
6730 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
6731 Entry.LSDAStart = readNext<uint32_t>(Pos);
6732 IndexEntries.push_back(Entry);
6733
6734 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006735 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
6736 << ", "
Tim Northover39c70bb2014-08-12 11:52:59 +00006737 << "2nd level page offset="
6738 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006739 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006740 }
6741
Tim Northover39c70bb2014-08-12 11:52:59 +00006742 //===----------------------------------
6743 // Next come the LSDA tables
6744 //===----------------------------------
6745
6746 // The LSDA layout is rather implicit: it's a contiguous array of entries from
6747 // the first top-level index's LSDAOffset to the last (sentinel).
6748
6749 outs() << " LSDA descriptors:\n";
6750 Pos = Contents.data() + IndexEntries[0].LSDAStart;
6751 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
6752 (2 * sizeof(uint32_t));
6753 for (int i = 0; i < NumLSDAs; ++i) {
6754 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6755 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
6756 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006757 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6758 << ", "
6759 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006760 }
6761
6762 //===----------------------------------
6763 // Finally, the 2nd level indices
6764 //===----------------------------------
6765
6766 // Generally these are 4K in size, and have 2 possible forms:
6767 // + Regular stores up to 511 entries with disparate encodings
6768 // + Compressed stores up to 1021 entries if few enough compact encoding
6769 // values are used.
6770 outs() << " Second level indices:\n";
6771 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
6772 // The final sentinel top-level index has no associated 2nd level page
6773 if (IndexEntries[i].SecondLevelPageStart == 0)
6774 break;
6775
6776 outs() << " Second level index[" << i << "]: "
6777 << "offset in section="
6778 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
6779 << ", "
6780 << "base function offset="
6781 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
6782
6783 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
Aaron Ballman80930af2014-08-14 13:53:19 +00006784 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00006785 if (Kind == 2)
6786 printRegularSecondLevelUnwindPage(Pos);
6787 else if (Kind == 3)
6788 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
6789 CommonEncodings);
6790 else
6791 llvm_unreachable("Do not know how to print this kind of 2nd level page");
Tim Northover39c70bb2014-08-12 11:52:59 +00006792 }
6793}
6794
Tim Northover4bd286a2014-08-01 13:07:19 +00006795void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
6796 std::map<uint64_t, SymbolRef> Symbols;
6797 for (const SymbolRef &SymRef : Obj->symbols()) {
6798 // Discard any undefined or absolute symbols. They're not going to take part
6799 // in the convenience lookup for unwind info and just take up resources.
6800 section_iterator Section = Obj->section_end();
6801 SymRef.getSection(Section);
6802 if (Section == Obj->section_end())
6803 continue;
6804
Rafael Espindoladea00162015-07-03 17:44:18 +00006805 uint64_t Addr = SymRef.getValue();
Tim Northover4bd286a2014-08-01 13:07:19 +00006806 Symbols.insert(std::make_pair(Addr, SymRef));
6807 }
6808
6809 for (const SectionRef &Section : Obj->sections()) {
6810 StringRef SectName;
6811 Section.getName(SectName);
6812 if (SectName == "__compact_unwind")
6813 printMachOCompactUnwindSection(Obj, Symbols, Section);
6814 else if (SectName == "__unwind_info")
Tim Northover39c70bb2014-08-12 11:52:59 +00006815 printMachOUnwindInfoSection(Obj, Symbols, Section);
Tim Northover4bd286a2014-08-01 13:07:19 +00006816 else if (SectName == "__eh_frame")
6817 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00006818 }
6819}
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006820
6821static void PrintMachHeader(uint32_t magic, uint32_t cputype,
6822 uint32_t cpusubtype, uint32_t filetype,
6823 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
6824 bool verbose) {
6825 outs() << "Mach header\n";
6826 outs() << " magic cputype cpusubtype caps filetype ncmds "
6827 "sizeofcmds flags\n";
6828 if (verbose) {
6829 if (magic == MachO::MH_MAGIC)
6830 outs() << " MH_MAGIC";
6831 else if (magic == MachO::MH_MAGIC_64)
6832 outs() << "MH_MAGIC_64";
6833 else
6834 outs() << format(" 0x%08" PRIx32, magic);
6835 switch (cputype) {
6836 case MachO::CPU_TYPE_I386:
6837 outs() << " I386";
6838 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6839 case MachO::CPU_SUBTYPE_I386_ALL:
6840 outs() << " ALL";
6841 break;
6842 default:
6843 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6844 break;
6845 }
6846 break;
6847 case MachO::CPU_TYPE_X86_64:
6848 outs() << " X86_64";
Kevin Enderby131d1772015-01-09 19:22:37 +00006849 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6850 case MachO::CPU_SUBTYPE_X86_64_ALL:
6851 outs() << " ALL";
6852 break;
6853 case MachO::CPU_SUBTYPE_X86_64_H:
6854 outs() << " Haswell";
6855 break;
6856 default:
6857 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6858 break;
6859 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006860 break;
6861 case MachO::CPU_TYPE_ARM:
6862 outs() << " ARM";
6863 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6864 case MachO::CPU_SUBTYPE_ARM_ALL:
6865 outs() << " ALL";
6866 break;
6867 case MachO::CPU_SUBTYPE_ARM_V4T:
6868 outs() << " V4T";
6869 break;
6870 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6871 outs() << " V5TEJ";
6872 break;
6873 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6874 outs() << " XSCALE";
6875 break;
6876 case MachO::CPU_SUBTYPE_ARM_V6:
6877 outs() << " V6";
6878 break;
6879 case MachO::CPU_SUBTYPE_ARM_V6M:
6880 outs() << " V6M";
6881 break;
6882 case MachO::CPU_SUBTYPE_ARM_V7:
6883 outs() << " V7";
6884 break;
6885 case MachO::CPU_SUBTYPE_ARM_V7EM:
6886 outs() << " V7EM";
6887 break;
6888 case MachO::CPU_SUBTYPE_ARM_V7K:
6889 outs() << " V7K";
6890 break;
6891 case MachO::CPU_SUBTYPE_ARM_V7M:
6892 outs() << " V7M";
6893 break;
6894 case MachO::CPU_SUBTYPE_ARM_V7S:
6895 outs() << " V7S";
6896 break;
6897 default:
6898 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6899 break;
6900 }
6901 break;
6902 case MachO::CPU_TYPE_ARM64:
6903 outs() << " ARM64";
6904 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6905 case MachO::CPU_SUBTYPE_ARM64_ALL:
6906 outs() << " ALL";
6907 break;
6908 default:
6909 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6910 break;
6911 }
6912 break;
6913 case MachO::CPU_TYPE_POWERPC:
6914 outs() << " PPC";
6915 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6916 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6917 outs() << " ALL";
6918 break;
6919 default:
6920 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6921 break;
6922 }
6923 break;
6924 case MachO::CPU_TYPE_POWERPC64:
6925 outs() << " PPC64";
6926 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6927 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6928 outs() << " ALL";
6929 break;
6930 default:
6931 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6932 break;
6933 }
6934 break;
6935 }
6936 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00006937 outs() << " LIB64";
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006938 } else {
6939 outs() << format(" 0x%02" PRIx32,
6940 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6941 }
6942 switch (filetype) {
6943 case MachO::MH_OBJECT:
6944 outs() << " OBJECT";
6945 break;
6946 case MachO::MH_EXECUTE:
6947 outs() << " EXECUTE";
6948 break;
6949 case MachO::MH_FVMLIB:
6950 outs() << " FVMLIB";
6951 break;
6952 case MachO::MH_CORE:
6953 outs() << " CORE";
6954 break;
6955 case MachO::MH_PRELOAD:
6956 outs() << " PRELOAD";
6957 break;
6958 case MachO::MH_DYLIB:
6959 outs() << " DYLIB";
6960 break;
6961 case MachO::MH_DYLIB_STUB:
6962 outs() << " DYLIB_STUB";
6963 break;
6964 case MachO::MH_DYLINKER:
6965 outs() << " DYLINKER";
6966 break;
6967 case MachO::MH_BUNDLE:
6968 outs() << " BUNDLE";
6969 break;
6970 case MachO::MH_DSYM:
6971 outs() << " DSYM";
6972 break;
6973 case MachO::MH_KEXT_BUNDLE:
6974 outs() << " KEXTBUNDLE";
6975 break;
6976 default:
6977 outs() << format(" %10u", filetype);
6978 break;
6979 }
6980 outs() << format(" %5u", ncmds);
6981 outs() << format(" %10u", sizeofcmds);
6982 uint32_t f = flags;
6983 if (f & MachO::MH_NOUNDEFS) {
6984 outs() << " NOUNDEFS";
6985 f &= ~MachO::MH_NOUNDEFS;
6986 }
6987 if (f & MachO::MH_INCRLINK) {
6988 outs() << " INCRLINK";
6989 f &= ~MachO::MH_INCRLINK;
6990 }
6991 if (f & MachO::MH_DYLDLINK) {
6992 outs() << " DYLDLINK";
6993 f &= ~MachO::MH_DYLDLINK;
6994 }
6995 if (f & MachO::MH_BINDATLOAD) {
6996 outs() << " BINDATLOAD";
6997 f &= ~MachO::MH_BINDATLOAD;
6998 }
6999 if (f & MachO::MH_PREBOUND) {
7000 outs() << " PREBOUND";
7001 f &= ~MachO::MH_PREBOUND;
7002 }
7003 if (f & MachO::MH_SPLIT_SEGS) {
7004 outs() << " SPLIT_SEGS";
7005 f &= ~MachO::MH_SPLIT_SEGS;
7006 }
7007 if (f & MachO::MH_LAZY_INIT) {
7008 outs() << " LAZY_INIT";
7009 f &= ~MachO::MH_LAZY_INIT;
7010 }
7011 if (f & MachO::MH_TWOLEVEL) {
7012 outs() << " TWOLEVEL";
7013 f &= ~MachO::MH_TWOLEVEL;
7014 }
7015 if (f & MachO::MH_FORCE_FLAT) {
7016 outs() << " FORCE_FLAT";
7017 f &= ~MachO::MH_FORCE_FLAT;
7018 }
7019 if (f & MachO::MH_NOMULTIDEFS) {
7020 outs() << " NOMULTIDEFS";
7021 f &= ~MachO::MH_NOMULTIDEFS;
7022 }
7023 if (f & MachO::MH_NOFIXPREBINDING) {
7024 outs() << " NOFIXPREBINDING";
7025 f &= ~MachO::MH_NOFIXPREBINDING;
7026 }
7027 if (f & MachO::MH_PREBINDABLE) {
7028 outs() << " PREBINDABLE";
7029 f &= ~MachO::MH_PREBINDABLE;
7030 }
7031 if (f & MachO::MH_ALLMODSBOUND) {
7032 outs() << " ALLMODSBOUND";
7033 f &= ~MachO::MH_ALLMODSBOUND;
7034 }
7035 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7036 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7037 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7038 }
7039 if (f & MachO::MH_CANONICAL) {
7040 outs() << " CANONICAL";
7041 f &= ~MachO::MH_CANONICAL;
7042 }
7043 if (f & MachO::MH_WEAK_DEFINES) {
7044 outs() << " WEAK_DEFINES";
7045 f &= ~MachO::MH_WEAK_DEFINES;
7046 }
7047 if (f & MachO::MH_BINDS_TO_WEAK) {
7048 outs() << " BINDS_TO_WEAK";
7049 f &= ~MachO::MH_BINDS_TO_WEAK;
7050 }
7051 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7052 outs() << " ALLOW_STACK_EXECUTION";
7053 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7054 }
7055 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7056 outs() << " DEAD_STRIPPABLE_DYLIB";
7057 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7058 }
7059 if (f & MachO::MH_PIE) {
7060 outs() << " PIE";
7061 f &= ~MachO::MH_PIE;
7062 }
7063 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7064 outs() << " NO_REEXPORTED_DYLIBS";
7065 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7066 }
7067 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7068 outs() << " MH_HAS_TLV_DESCRIPTORS";
7069 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7070 }
7071 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7072 outs() << " MH_NO_HEAP_EXECUTION";
7073 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7074 }
7075 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7076 outs() << " APP_EXTENSION_SAFE";
7077 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7078 }
7079 if (f != 0 || flags == 0)
7080 outs() << format(" 0x%08" PRIx32, f);
7081 } else {
7082 outs() << format(" 0x%08" PRIx32, magic);
7083 outs() << format(" %7d", cputype);
7084 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7085 outs() << format(" 0x%02" PRIx32,
7086 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7087 outs() << format(" %10u", filetype);
7088 outs() << format(" %5u", ncmds);
7089 outs() << format(" %10u", sizeofcmds);
7090 outs() << format(" 0x%08" PRIx32, flags);
7091 }
7092 outs() << "\n";
7093}
7094
Kevin Enderby956366c2014-08-29 22:30:52 +00007095static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7096 StringRef SegName, uint64_t vmaddr,
7097 uint64_t vmsize, uint64_t fileoff,
7098 uint64_t filesize, uint32_t maxprot,
7099 uint32_t initprot, uint32_t nsects,
7100 uint32_t flags, uint32_t object_size,
7101 bool verbose) {
7102 uint64_t expected_cmdsize;
7103 if (cmd == MachO::LC_SEGMENT) {
7104 outs() << " cmd LC_SEGMENT\n";
7105 expected_cmdsize = nsects;
7106 expected_cmdsize *= sizeof(struct MachO::section);
7107 expected_cmdsize += sizeof(struct MachO::segment_command);
7108 } else {
7109 outs() << " cmd LC_SEGMENT_64\n";
7110 expected_cmdsize = nsects;
7111 expected_cmdsize *= sizeof(struct MachO::section_64);
7112 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7113 }
7114 outs() << " cmdsize " << cmdsize;
7115 if (cmdsize != expected_cmdsize)
7116 outs() << " Inconsistent size\n";
7117 else
7118 outs() << "\n";
7119 outs() << " segname " << SegName << "\n";
7120 if (cmd == MachO::LC_SEGMENT_64) {
7121 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7122 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7123 } else {
Kevin Enderbyadb7c432014-12-16 18:58:11 +00007124 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7125 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007126 }
7127 outs() << " fileoff " << fileoff;
7128 if (fileoff > object_size)
7129 outs() << " (past end of file)\n";
7130 else
7131 outs() << "\n";
7132 outs() << " filesize " << filesize;
7133 if (fileoff + filesize > object_size)
7134 outs() << " (past end of file)\n";
7135 else
7136 outs() << "\n";
7137 if (verbose) {
7138 if ((maxprot &
7139 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7140 MachO::VM_PROT_EXECUTE)) != 0)
7141 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7142 else {
7143 if (maxprot & MachO::VM_PROT_READ)
7144 outs() << " maxprot r";
7145 else
7146 outs() << " maxprot -";
7147 if (maxprot & MachO::VM_PROT_WRITE)
7148 outs() << "w";
7149 else
7150 outs() << "-";
7151 if (maxprot & MachO::VM_PROT_EXECUTE)
7152 outs() << "x\n";
7153 else
7154 outs() << "-\n";
7155 }
7156 if ((initprot &
7157 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7158 MachO::VM_PROT_EXECUTE)) != 0)
7159 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7160 else {
7161 if (initprot & MachO::VM_PROT_READ)
7162 outs() << " initprot r";
7163 else
7164 outs() << " initprot -";
7165 if (initprot & MachO::VM_PROT_WRITE)
7166 outs() << "w";
7167 else
7168 outs() << "-";
7169 if (initprot & MachO::VM_PROT_EXECUTE)
7170 outs() << "x\n";
7171 else
7172 outs() << "-\n";
7173 }
7174 } else {
7175 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7176 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7177 }
7178 outs() << " nsects " << nsects << "\n";
7179 if (verbose) {
7180 outs() << " flags";
7181 if (flags == 0)
7182 outs() << " (none)\n";
7183 else {
7184 if (flags & MachO::SG_HIGHVM) {
7185 outs() << " HIGHVM";
7186 flags &= ~MachO::SG_HIGHVM;
7187 }
7188 if (flags & MachO::SG_FVMLIB) {
7189 outs() << " FVMLIB";
7190 flags &= ~MachO::SG_FVMLIB;
7191 }
7192 if (flags & MachO::SG_NORELOC) {
7193 outs() << " NORELOC";
7194 flags &= ~MachO::SG_NORELOC;
7195 }
7196 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7197 outs() << " PROTECTED_VERSION_1";
7198 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7199 }
7200 if (flags)
7201 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7202 else
7203 outs() << "\n";
7204 }
7205 } else {
7206 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7207 }
7208}
7209
7210static void PrintSection(const char *sectname, const char *segname,
7211 uint64_t addr, uint64_t size, uint32_t offset,
7212 uint32_t align, uint32_t reloff, uint32_t nreloc,
7213 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7214 uint32_t cmd, const char *sg_segname,
7215 uint32_t filetype, uint32_t object_size,
7216 bool verbose) {
7217 outs() << "Section\n";
7218 outs() << " sectname " << format("%.16s\n", sectname);
7219 outs() << " segname " << format("%.16s", segname);
7220 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7221 outs() << " (does not match segment)\n";
7222 else
7223 outs() << "\n";
7224 if (cmd == MachO::LC_SEGMENT_64) {
7225 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7226 outs() << " size " << format("0x%016" PRIx64, size);
7227 } else {
Kevin Enderby75594b62014-12-16 21:00:25 +00007228 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7229 outs() << " size " << format("0x%08" PRIx64, size);
Kevin Enderby956366c2014-08-29 22:30:52 +00007230 }
7231 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7232 outs() << " (past end of file)\n";
7233 else
7234 outs() << "\n";
7235 outs() << " offset " << offset;
7236 if (offset > object_size)
7237 outs() << " (past end of file)\n";
7238 else
7239 outs() << "\n";
7240 uint32_t align_shifted = 1 << align;
7241 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7242 outs() << " reloff " << reloff;
7243 if (reloff > object_size)
7244 outs() << " (past end of file)\n";
7245 else
7246 outs() << "\n";
7247 outs() << " nreloc " << nreloc;
7248 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7249 outs() << " (past end of file)\n";
7250 else
7251 outs() << "\n";
7252 uint32_t section_type = flags & MachO::SECTION_TYPE;
7253 if (verbose) {
7254 outs() << " type";
7255 if (section_type == MachO::S_REGULAR)
7256 outs() << " S_REGULAR\n";
7257 else if (section_type == MachO::S_ZEROFILL)
7258 outs() << " S_ZEROFILL\n";
7259 else if (section_type == MachO::S_CSTRING_LITERALS)
7260 outs() << " S_CSTRING_LITERALS\n";
7261 else if (section_type == MachO::S_4BYTE_LITERALS)
7262 outs() << " S_4BYTE_LITERALS\n";
7263 else if (section_type == MachO::S_8BYTE_LITERALS)
7264 outs() << " S_8BYTE_LITERALS\n";
7265 else if (section_type == MachO::S_16BYTE_LITERALS)
7266 outs() << " S_16BYTE_LITERALS\n";
7267 else if (section_type == MachO::S_LITERAL_POINTERS)
7268 outs() << " S_LITERAL_POINTERS\n";
7269 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7270 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7271 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7272 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7273 else if (section_type == MachO::S_SYMBOL_STUBS)
7274 outs() << " S_SYMBOL_STUBS\n";
7275 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7276 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7277 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7278 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7279 else if (section_type == MachO::S_COALESCED)
7280 outs() << " S_COALESCED\n";
7281 else if (section_type == MachO::S_INTERPOSING)
7282 outs() << " S_INTERPOSING\n";
7283 else if (section_type == MachO::S_DTRACE_DOF)
7284 outs() << " S_DTRACE_DOF\n";
7285 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7286 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7287 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7288 outs() << " S_THREAD_LOCAL_REGULAR\n";
7289 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7290 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7291 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7292 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7293 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7294 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7295 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7296 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7297 else
7298 outs() << format("0x%08" PRIx32, section_type) << "\n";
7299 outs() << "attributes";
7300 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7301 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7302 outs() << " PURE_INSTRUCTIONS";
7303 if (section_attributes & MachO::S_ATTR_NO_TOC)
7304 outs() << " NO_TOC";
7305 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7306 outs() << " STRIP_STATIC_SYMS";
7307 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7308 outs() << " NO_DEAD_STRIP";
7309 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7310 outs() << " LIVE_SUPPORT";
7311 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7312 outs() << " SELF_MODIFYING_CODE";
7313 if (section_attributes & MachO::S_ATTR_DEBUG)
7314 outs() << " DEBUG";
7315 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7316 outs() << " SOME_INSTRUCTIONS";
7317 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7318 outs() << " EXT_RELOC";
7319 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7320 outs() << " LOC_RELOC";
7321 if (section_attributes == 0)
7322 outs() << " (none)";
7323 outs() << "\n";
7324 } else
7325 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7326 outs() << " reserved1 " << reserved1;
7327 if (section_type == MachO::S_SYMBOL_STUBS ||
7328 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7329 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7330 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7331 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7332 outs() << " (index into indirect symbol table)\n";
7333 else
7334 outs() << "\n";
7335 outs() << " reserved2 " << reserved2;
7336 if (section_type == MachO::S_SYMBOL_STUBS)
7337 outs() << " (size of stubs)\n";
7338 else
7339 outs() << "\n";
7340}
7341
David Majnemer73cc6ff2014-11-13 19:48:56 +00007342static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
Kevin Enderby956366c2014-08-29 22:30:52 +00007343 uint32_t object_size) {
7344 outs() << " cmd LC_SYMTAB\n";
7345 outs() << " cmdsize " << st.cmdsize;
7346 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7347 outs() << " Incorrect size\n";
7348 else
7349 outs() << "\n";
7350 outs() << " symoff " << st.symoff;
7351 if (st.symoff > object_size)
7352 outs() << " (past end of file)\n";
7353 else
7354 outs() << "\n";
7355 outs() << " nsyms " << st.nsyms;
7356 uint64_t big_size;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007357 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007358 big_size = st.nsyms;
7359 big_size *= sizeof(struct MachO::nlist_64);
7360 big_size += st.symoff;
7361 if (big_size > object_size)
7362 outs() << " (past end of file)\n";
7363 else
7364 outs() << "\n";
7365 } else {
7366 big_size = st.nsyms;
7367 big_size *= sizeof(struct MachO::nlist);
7368 big_size += st.symoff;
7369 if (big_size > object_size)
7370 outs() << " (past end of file)\n";
7371 else
7372 outs() << "\n";
7373 }
7374 outs() << " stroff " << st.stroff;
7375 if (st.stroff > object_size)
7376 outs() << " (past end of file)\n";
7377 else
7378 outs() << "\n";
7379 outs() << " strsize " << st.strsize;
7380 big_size = st.stroff;
7381 big_size += st.strsize;
7382 if (big_size > object_size)
7383 outs() << " (past end of file)\n";
7384 else
7385 outs() << "\n";
7386}
7387
7388static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7389 uint32_t nsyms, uint32_t object_size,
David Majnemer73cc6ff2014-11-13 19:48:56 +00007390 bool Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007391 outs() << " cmd LC_DYSYMTAB\n";
7392 outs() << " cmdsize " << dyst.cmdsize;
7393 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7394 outs() << " Incorrect size\n";
7395 else
7396 outs() << "\n";
7397 outs() << " ilocalsym " << dyst.ilocalsym;
7398 if (dyst.ilocalsym > nsyms)
7399 outs() << " (greater than the number of symbols)\n";
7400 else
7401 outs() << "\n";
7402 outs() << " nlocalsym " << dyst.nlocalsym;
7403 uint64_t big_size;
7404 big_size = dyst.ilocalsym;
7405 big_size += dyst.nlocalsym;
7406 if (big_size > nsyms)
7407 outs() << " (past the end of the symbol table)\n";
7408 else
7409 outs() << "\n";
7410 outs() << " iextdefsym " << dyst.iextdefsym;
7411 if (dyst.iextdefsym > nsyms)
7412 outs() << " (greater than the number of symbols)\n";
7413 else
7414 outs() << "\n";
7415 outs() << " nextdefsym " << dyst.nextdefsym;
7416 big_size = dyst.iextdefsym;
7417 big_size += dyst.nextdefsym;
7418 if (big_size > nsyms)
7419 outs() << " (past the end of the symbol table)\n";
7420 else
7421 outs() << "\n";
7422 outs() << " iundefsym " << dyst.iundefsym;
7423 if (dyst.iundefsym > nsyms)
7424 outs() << " (greater than the number of symbols)\n";
7425 else
7426 outs() << "\n";
7427 outs() << " nundefsym " << dyst.nundefsym;
7428 big_size = dyst.iundefsym;
7429 big_size += dyst.nundefsym;
7430 if (big_size > nsyms)
7431 outs() << " (past the end of the symbol table)\n";
7432 else
7433 outs() << "\n";
7434 outs() << " tocoff " << dyst.tocoff;
7435 if (dyst.tocoff > object_size)
7436 outs() << " (past end of file)\n";
7437 else
7438 outs() << "\n";
7439 outs() << " ntoc " << dyst.ntoc;
7440 big_size = dyst.ntoc;
7441 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7442 big_size += dyst.tocoff;
7443 if (big_size > object_size)
7444 outs() << " (past end of file)\n";
7445 else
7446 outs() << "\n";
7447 outs() << " modtaboff " << dyst.modtaboff;
7448 if (dyst.modtaboff > object_size)
7449 outs() << " (past end of file)\n";
7450 else
7451 outs() << "\n";
7452 outs() << " nmodtab " << dyst.nmodtab;
7453 uint64_t modtabend;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007454 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007455 modtabend = dyst.nmodtab;
7456 modtabend *= sizeof(struct MachO::dylib_module_64);
7457 modtabend += dyst.modtaboff;
7458 } else {
7459 modtabend = dyst.nmodtab;
7460 modtabend *= sizeof(struct MachO::dylib_module);
7461 modtabend += dyst.modtaboff;
7462 }
7463 if (modtabend > object_size)
7464 outs() << " (past end of file)\n";
7465 else
7466 outs() << "\n";
7467 outs() << " extrefsymoff " << dyst.extrefsymoff;
7468 if (dyst.extrefsymoff > object_size)
7469 outs() << " (past end of file)\n";
7470 else
7471 outs() << "\n";
7472 outs() << " nextrefsyms " << dyst.nextrefsyms;
7473 big_size = dyst.nextrefsyms;
7474 big_size *= sizeof(struct MachO::dylib_reference);
7475 big_size += dyst.extrefsymoff;
7476 if (big_size > object_size)
7477 outs() << " (past end of file)\n";
7478 else
7479 outs() << "\n";
7480 outs() << " indirectsymoff " << dyst.indirectsymoff;
7481 if (dyst.indirectsymoff > object_size)
7482 outs() << " (past end of file)\n";
7483 else
7484 outs() << "\n";
7485 outs() << " nindirectsyms " << dyst.nindirectsyms;
7486 big_size = dyst.nindirectsyms;
7487 big_size *= sizeof(uint32_t);
7488 big_size += dyst.indirectsymoff;
7489 if (big_size > object_size)
7490 outs() << " (past end of file)\n";
7491 else
7492 outs() << "\n";
7493 outs() << " extreloff " << dyst.extreloff;
7494 if (dyst.extreloff > object_size)
7495 outs() << " (past end of file)\n";
7496 else
7497 outs() << "\n";
7498 outs() << " nextrel " << dyst.nextrel;
7499 big_size = dyst.nextrel;
7500 big_size *= sizeof(struct MachO::relocation_info);
7501 big_size += dyst.extreloff;
7502 if (big_size > object_size)
7503 outs() << " (past end of file)\n";
7504 else
7505 outs() << "\n";
7506 outs() << " locreloff " << dyst.locreloff;
7507 if (dyst.locreloff > object_size)
7508 outs() << " (past end of file)\n";
7509 else
7510 outs() << "\n";
7511 outs() << " nlocrel " << dyst.nlocrel;
7512 big_size = dyst.nlocrel;
7513 big_size *= sizeof(struct MachO::relocation_info);
7514 big_size += dyst.locreloff;
7515 if (big_size > object_size)
7516 outs() << " (past end of file)\n";
7517 else
7518 outs() << "\n";
7519}
7520
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007521static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
7522 uint32_t object_size) {
7523 if (dc.cmd == MachO::LC_DYLD_INFO)
7524 outs() << " cmd LC_DYLD_INFO\n";
7525 else
7526 outs() << " cmd LC_DYLD_INFO_ONLY\n";
7527 outs() << " cmdsize " << dc.cmdsize;
7528 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
7529 outs() << " Incorrect size\n";
7530 else
7531 outs() << "\n";
7532 outs() << " rebase_off " << dc.rebase_off;
7533 if (dc.rebase_off > object_size)
7534 outs() << " (past end of file)\n";
7535 else
7536 outs() << "\n";
7537 outs() << " rebase_size " << dc.rebase_size;
7538 uint64_t big_size;
7539 big_size = dc.rebase_off;
7540 big_size += dc.rebase_size;
7541 if (big_size > object_size)
7542 outs() << " (past end of file)\n";
7543 else
7544 outs() << "\n";
7545 outs() << " bind_off " << dc.bind_off;
7546 if (dc.bind_off > object_size)
7547 outs() << " (past end of file)\n";
7548 else
7549 outs() << "\n";
7550 outs() << " bind_size " << dc.bind_size;
7551 big_size = dc.bind_off;
7552 big_size += dc.bind_size;
7553 if (big_size > object_size)
7554 outs() << " (past end of file)\n";
7555 else
7556 outs() << "\n";
7557 outs() << " weak_bind_off " << dc.weak_bind_off;
7558 if (dc.weak_bind_off > object_size)
7559 outs() << " (past end of file)\n";
7560 else
7561 outs() << "\n";
7562 outs() << " weak_bind_size " << dc.weak_bind_size;
7563 big_size = dc.weak_bind_off;
7564 big_size += dc.weak_bind_size;
7565 if (big_size > object_size)
7566 outs() << " (past end of file)\n";
7567 else
7568 outs() << "\n";
7569 outs() << " lazy_bind_off " << dc.lazy_bind_off;
7570 if (dc.lazy_bind_off > object_size)
7571 outs() << " (past end of file)\n";
7572 else
7573 outs() << "\n";
7574 outs() << " lazy_bind_size " << dc.lazy_bind_size;
7575 big_size = dc.lazy_bind_off;
7576 big_size += dc.lazy_bind_size;
7577 if (big_size > object_size)
7578 outs() << " (past end of file)\n";
7579 else
7580 outs() << "\n";
7581 outs() << " export_off " << dc.export_off;
7582 if (dc.export_off > object_size)
7583 outs() << " (past end of file)\n";
7584 else
7585 outs() << "\n";
7586 outs() << " export_size " << dc.export_size;
7587 big_size = dc.export_off;
7588 big_size += dc.export_size;
7589 if (big_size > object_size)
7590 outs() << " (past end of file)\n";
7591 else
7592 outs() << "\n";
7593}
7594
7595static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
7596 const char *Ptr) {
7597 if (dyld.cmd == MachO::LC_ID_DYLINKER)
7598 outs() << " cmd LC_ID_DYLINKER\n";
7599 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
7600 outs() << " cmd LC_LOAD_DYLINKER\n";
7601 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
7602 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
7603 else
7604 outs() << " cmd ?(" << dyld.cmd << ")\n";
7605 outs() << " cmdsize " << dyld.cmdsize;
7606 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
7607 outs() << " Incorrect size\n";
7608 else
7609 outs() << "\n";
7610 if (dyld.name >= dyld.cmdsize)
7611 outs() << " name ?(bad offset " << dyld.name << ")\n";
7612 else {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007613 const char *P = (const char *)(Ptr) + dyld.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007614 outs() << " name " << P << " (offset " << dyld.name << ")\n";
7615 }
7616}
7617
7618static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
7619 outs() << " cmd LC_UUID\n";
7620 outs() << " cmdsize " << uuid.cmdsize;
7621 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
7622 outs() << " Incorrect size\n";
7623 else
7624 outs() << "\n";
7625 outs() << " uuid ";
7626 outs() << format("%02" PRIX32, uuid.uuid[0]);
7627 outs() << format("%02" PRIX32, uuid.uuid[1]);
7628 outs() << format("%02" PRIX32, uuid.uuid[2]);
7629 outs() << format("%02" PRIX32, uuid.uuid[3]);
7630 outs() << "-";
7631 outs() << format("%02" PRIX32, uuid.uuid[4]);
7632 outs() << format("%02" PRIX32, uuid.uuid[5]);
7633 outs() << "-";
7634 outs() << format("%02" PRIX32, uuid.uuid[6]);
7635 outs() << format("%02" PRIX32, uuid.uuid[7]);
7636 outs() << "-";
7637 outs() << format("%02" PRIX32, uuid.uuid[8]);
7638 outs() << format("%02" PRIX32, uuid.uuid[9]);
7639 outs() << "-";
7640 outs() << format("%02" PRIX32, uuid.uuid[10]);
7641 outs() << format("%02" PRIX32, uuid.uuid[11]);
7642 outs() << format("%02" PRIX32, uuid.uuid[12]);
7643 outs() << format("%02" PRIX32, uuid.uuid[13]);
7644 outs() << format("%02" PRIX32, uuid.uuid[14]);
7645 outs() << format("%02" PRIX32, uuid.uuid[15]);
7646 outs() << "\n";
7647}
7648
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007649static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00007650 outs() << " cmd LC_RPATH\n";
7651 outs() << " cmdsize " << rpath.cmdsize;
7652 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
7653 outs() << " Incorrect size\n";
7654 else
7655 outs() << "\n";
7656 if (rpath.path >= rpath.cmdsize)
7657 outs() << " path ?(bad offset " << rpath.path << ")\n";
7658 else {
7659 const char *P = (const char *)(Ptr) + rpath.path;
7660 outs() << " path " << P << " (offset " << rpath.path << ")\n";
7661 }
7662}
7663
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007664static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
7665 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
7666 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
7667 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
7668 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
7669 else
7670 outs() << " cmd " << vd.cmd << " (?)\n";
7671 outs() << " cmdsize " << vd.cmdsize;
7672 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
7673 outs() << " Incorrect size\n";
7674 else
7675 outs() << "\n";
7676 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
7677 << ((vd.version >> 8) & 0xff);
7678 if ((vd.version & 0xff) != 0)
7679 outs() << "." << (vd.version & 0xff);
7680 outs() << "\n";
7681 if (vd.sdk == 0)
Kevin Enderby57538292014-12-17 01:01:30 +00007682 outs() << " sdk n/a";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007683 else {
7684 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
7685 << ((vd.sdk >> 8) & 0xff);
7686 }
7687 if ((vd.sdk & 0xff) != 0)
7688 outs() << "." << (vd.sdk & 0xff);
7689 outs() << "\n";
7690}
7691
7692static void PrintSourceVersionCommand(MachO::source_version_command sd) {
7693 outs() << " cmd LC_SOURCE_VERSION\n";
7694 outs() << " cmdsize " << sd.cmdsize;
7695 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
7696 outs() << " Incorrect size\n";
7697 else
7698 outs() << "\n";
7699 uint64_t a = (sd.version >> 40) & 0xffffff;
7700 uint64_t b = (sd.version >> 30) & 0x3ff;
7701 uint64_t c = (sd.version >> 20) & 0x3ff;
7702 uint64_t d = (sd.version >> 10) & 0x3ff;
7703 uint64_t e = sd.version & 0x3ff;
7704 outs() << " version " << a << "." << b;
7705 if (e != 0)
7706 outs() << "." << c << "." << d << "." << e;
7707 else if (d != 0)
7708 outs() << "." << c << "." << d;
7709 else if (c != 0)
7710 outs() << "." << c;
7711 outs() << "\n";
7712}
7713
7714static void PrintEntryPointCommand(MachO::entry_point_command ep) {
7715 outs() << " cmd LC_MAIN\n";
7716 outs() << " cmdsize " << ep.cmdsize;
7717 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
7718 outs() << " Incorrect size\n";
7719 else
7720 outs() << "\n";
7721 outs() << " entryoff " << ep.entryoff << "\n";
7722 outs() << " stacksize " << ep.stacksize << "\n";
7723}
7724
Kevin Enderby0804f4672014-12-16 23:25:52 +00007725static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
7726 uint32_t object_size) {
7727 outs() << " cmd LC_ENCRYPTION_INFO\n";
7728 outs() << " cmdsize " << ec.cmdsize;
7729 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
7730 outs() << " Incorrect size\n";
7731 else
7732 outs() << "\n";
7733 outs() << " cryptoff " << ec.cryptoff;
7734 if (ec.cryptoff > object_size)
7735 outs() << " (past end of file)\n";
7736 else
7737 outs() << "\n";
7738 outs() << " cryptsize " << ec.cryptsize;
7739 if (ec.cryptsize > object_size)
7740 outs() << " (past end of file)\n";
7741 else
7742 outs() << "\n";
7743 outs() << " cryptid " << ec.cryptid << "\n";
7744}
7745
Kevin Enderby57538292014-12-17 01:01:30 +00007746static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007747 uint32_t object_size) {
Kevin Enderby57538292014-12-17 01:01:30 +00007748 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
7749 outs() << " cmdsize " << ec.cmdsize;
7750 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
7751 outs() << " Incorrect size\n";
7752 else
7753 outs() << "\n";
7754 outs() << " cryptoff " << ec.cryptoff;
7755 if (ec.cryptoff > object_size)
7756 outs() << " (past end of file)\n";
7757 else
7758 outs() << "\n";
7759 outs() << " cryptsize " << ec.cryptsize;
7760 if (ec.cryptsize > object_size)
7761 outs() << " (past end of file)\n";
7762 else
7763 outs() << "\n";
7764 outs() << " cryptid " << ec.cryptid << "\n";
7765 outs() << " pad " << ec.pad << "\n";
7766}
7767
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007768static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
7769 const char *Ptr) {
7770 outs() << " cmd LC_LINKER_OPTION\n";
7771 outs() << " cmdsize " << lo.cmdsize;
7772 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
7773 outs() << " Incorrect size\n";
7774 else
7775 outs() << "\n";
7776 outs() << " count " << lo.count << "\n";
7777 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
7778 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
7779 uint32_t i = 0;
7780 while (left > 0) {
7781 while (*string == '\0' && left > 0) {
7782 string++;
7783 left--;
7784 }
7785 if (left > 0) {
7786 i++;
7787 outs() << " string #" << i << " " << format("%.*s\n", left, string);
David Majnemerd4449ed2014-12-20 08:24:43 +00007788 uint32_t NullPos = StringRef(string, left).find('\0');
7789 uint32_t len = std::min(NullPos, left) + 1;
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007790 string += len;
7791 left -= len;
7792 }
7793 }
7794 if (lo.count != i)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007795 outs() << " count " << lo.count << " does not match number of strings "
7796 << i << "\n";
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007797}
7798
Kevin Enderbyb4b79312014-12-18 19:24:35 +00007799static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
7800 const char *Ptr) {
7801 outs() << " cmd LC_SUB_FRAMEWORK\n";
7802 outs() << " cmdsize " << sub.cmdsize;
7803 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
7804 outs() << " Incorrect size\n";
7805 else
7806 outs() << "\n";
7807 if (sub.umbrella < sub.cmdsize) {
7808 const char *P = Ptr + sub.umbrella;
7809 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
7810 } else {
7811 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
7812 }
7813}
7814
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00007815static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
7816 const char *Ptr) {
7817 outs() << " cmd LC_SUB_UMBRELLA\n";
7818 outs() << " cmdsize " << sub.cmdsize;
7819 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
7820 outs() << " Incorrect size\n";
7821 else
7822 outs() << "\n";
7823 if (sub.sub_umbrella < sub.cmdsize) {
7824 const char *P = Ptr + sub.sub_umbrella;
7825 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
7826 } else {
7827 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
7828 }
7829}
7830
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007831static void PrintSubLibraryCommand(MachO::sub_library_command sub,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007832 const char *Ptr) {
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007833 outs() << " cmd LC_SUB_LIBRARY\n";
7834 outs() << " cmdsize " << sub.cmdsize;
7835 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
7836 outs() << " Incorrect size\n";
7837 else
7838 outs() << "\n";
7839 if (sub.sub_library < sub.cmdsize) {
7840 const char *P = Ptr + sub.sub_library;
7841 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7842 } else {
7843 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7844 }
7845}
7846
Kevin Enderby186eac32014-12-19 21:06:24 +00007847static void PrintSubClientCommand(MachO::sub_client_command sub,
7848 const char *Ptr) {
7849 outs() << " cmd LC_SUB_CLIENT\n";
7850 outs() << " cmdsize " << sub.cmdsize;
7851 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7852 outs() << " Incorrect size\n";
7853 else
7854 outs() << "\n";
7855 if (sub.client < sub.cmdsize) {
7856 const char *P = Ptr + sub.client;
7857 outs() << " client " << P << " (offset " << sub.client << ")\n";
7858 } else {
7859 outs() << " client ?(bad offset " << sub.client << ")\n";
7860 }
7861}
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007862
Kevin Enderby52e4ce42014-12-19 22:25:22 +00007863static void PrintRoutinesCommand(MachO::routines_command r) {
7864 outs() << " cmd LC_ROUTINES\n";
7865 outs() << " cmdsize " << r.cmdsize;
7866 if (r.cmdsize != sizeof(struct MachO::routines_command))
7867 outs() << " Incorrect size\n";
7868 else
7869 outs() << "\n";
7870 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7871 outs() << " init_module " << r.init_module << "\n";
7872 outs() << " reserved1 " << r.reserved1 << "\n";
7873 outs() << " reserved2 " << r.reserved2 << "\n";
7874 outs() << " reserved3 " << r.reserved3 << "\n";
7875 outs() << " reserved4 " << r.reserved4 << "\n";
7876 outs() << " reserved5 " << r.reserved5 << "\n";
7877 outs() << " reserved6 " << r.reserved6 << "\n";
7878}
7879
7880static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7881 outs() << " cmd LC_ROUTINES_64\n";
7882 outs() << " cmdsize " << r.cmdsize;
7883 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7884 outs() << " Incorrect size\n";
7885 else
7886 outs() << "\n";
7887 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
7888 outs() << " init_module " << r.init_module << "\n";
7889 outs() << " reserved1 " << r.reserved1 << "\n";
7890 outs() << " reserved2 " << r.reserved2 << "\n";
7891 outs() << " reserved3 " << r.reserved3 << "\n";
7892 outs() << " reserved4 " << r.reserved4 << "\n";
7893 outs() << " reserved5 " << r.reserved5 << "\n";
7894 outs() << " reserved6 " << r.reserved6 << "\n";
7895}
7896
Kevin Enderby48ef5342014-12-23 22:56:39 +00007897static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7898 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7899 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7900 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7901 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7902 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7903 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7904 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7905 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7906 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7907 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7908 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7909 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7910 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7911 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7912 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7913 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7914 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7915 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7916 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7917 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7918 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7919}
7920
Kevin Enderby227df342014-12-23 23:43:59 +00007921static void Print_mmst_reg(MachO::mmst_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007922 uint32_t f;
7923 outs() << "\t mmst_reg ";
7924 for (f = 0; f < 10; f++)
7925 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7926 outs() << "\n";
7927 outs() << "\t mmst_rsrv ";
7928 for (f = 0; f < 6; f++)
7929 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7930 outs() << "\n";
7931}
7932
Kevin Enderbyaefb0032014-12-24 00:16:51 +00007933static void Print_xmm_reg(MachO::xmm_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007934 uint32_t f;
7935 outs() << "\t xmm_reg ";
7936 for (f = 0; f < 16; f++)
7937 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7938 outs() << "\n";
7939}
7940
7941static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7942 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7943 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7944 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7945 outs() << " denorm " << fpu.fpu_fcw.denorm;
7946 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7947 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7948 outs() << " undfl " << fpu.fpu_fcw.undfl;
7949 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7950 outs() << "\t\t pc ";
7951 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7952 outs() << "FP_PREC_24B ";
7953 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
7954 outs() << "FP_PREC_53B ";
7955 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
7956 outs() << "FP_PREC_64B ";
7957 else
7958 outs() << fpu.fpu_fcw.pc << " ";
7959 outs() << "rc ";
7960 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
7961 outs() << "FP_RND_NEAR ";
7962 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
7963 outs() << "FP_RND_DOWN ";
7964 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
7965 outs() << "FP_RND_UP ";
7966 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007967 outs() << "FP_CHOP ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00007968 outs() << "\n";
7969 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
7970 outs() << " denorm " << fpu.fpu_fsw.denorm;
7971 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
7972 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
7973 outs() << " undfl " << fpu.fpu_fsw.undfl;
7974 outs() << " precis " << fpu.fpu_fsw.precis;
7975 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
7976 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
7977 outs() << " c0 " << fpu.fpu_fsw.c0;
7978 outs() << " c1 " << fpu.fpu_fsw.c1;
7979 outs() << " c2 " << fpu.fpu_fsw.c2;
7980 outs() << " tos " << fpu.fpu_fsw.tos;
7981 outs() << " c3 " << fpu.fpu_fsw.c3;
7982 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
7983 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
7984 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
7985 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
7986 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
7987 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
7988 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
7989 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
7990 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
7991 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
7992 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
7993 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
7994 outs() << "\n";
7995 outs() << "\t fpu_stmm0:\n";
7996 Print_mmst_reg(fpu.fpu_stmm0);
7997 outs() << "\t fpu_stmm1:\n";
7998 Print_mmst_reg(fpu.fpu_stmm1);
7999 outs() << "\t fpu_stmm2:\n";
8000 Print_mmst_reg(fpu.fpu_stmm2);
8001 outs() << "\t fpu_stmm3:\n";
8002 Print_mmst_reg(fpu.fpu_stmm3);
8003 outs() << "\t fpu_stmm4:\n";
8004 Print_mmst_reg(fpu.fpu_stmm4);
8005 outs() << "\t fpu_stmm5:\n";
8006 Print_mmst_reg(fpu.fpu_stmm5);
8007 outs() << "\t fpu_stmm6:\n";
8008 Print_mmst_reg(fpu.fpu_stmm6);
8009 outs() << "\t fpu_stmm7:\n";
8010 Print_mmst_reg(fpu.fpu_stmm7);
8011 outs() << "\t fpu_xmm0:\n";
8012 Print_xmm_reg(fpu.fpu_xmm0);
8013 outs() << "\t fpu_xmm1:\n";
8014 Print_xmm_reg(fpu.fpu_xmm1);
8015 outs() << "\t fpu_xmm2:\n";
8016 Print_xmm_reg(fpu.fpu_xmm2);
8017 outs() << "\t fpu_xmm3:\n";
8018 Print_xmm_reg(fpu.fpu_xmm3);
8019 outs() << "\t fpu_xmm4:\n";
8020 Print_xmm_reg(fpu.fpu_xmm4);
8021 outs() << "\t fpu_xmm5:\n";
8022 Print_xmm_reg(fpu.fpu_xmm5);
8023 outs() << "\t fpu_xmm6:\n";
8024 Print_xmm_reg(fpu.fpu_xmm6);
8025 outs() << "\t fpu_xmm7:\n";
8026 Print_xmm_reg(fpu.fpu_xmm7);
8027 outs() << "\t fpu_xmm8:\n";
8028 Print_xmm_reg(fpu.fpu_xmm8);
8029 outs() << "\t fpu_xmm9:\n";
8030 Print_xmm_reg(fpu.fpu_xmm9);
8031 outs() << "\t fpu_xmm10:\n";
8032 Print_xmm_reg(fpu.fpu_xmm10);
8033 outs() << "\t fpu_xmm11:\n";
8034 Print_xmm_reg(fpu.fpu_xmm11);
8035 outs() << "\t fpu_xmm12:\n";
8036 Print_xmm_reg(fpu.fpu_xmm12);
8037 outs() << "\t fpu_xmm13:\n";
8038 Print_xmm_reg(fpu.fpu_xmm13);
8039 outs() << "\t fpu_xmm14:\n";
8040 Print_xmm_reg(fpu.fpu_xmm14);
8041 outs() << "\t fpu_xmm15:\n";
8042 Print_xmm_reg(fpu.fpu_xmm15);
8043 outs() << "\t fpu_rsrv4:\n";
8044 for (uint32_t f = 0; f < 6; f++) {
8045 outs() << "\t ";
8046 for (uint32_t g = 0; g < 16; g++)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008047 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008048 outs() << "\n";
8049 }
8050 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8051 outs() << "\n";
8052}
8053
8054static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8055 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8056 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8057 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8058}
8059
8060static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8061 bool isLittleEndian, uint32_t cputype) {
8062 if (t.cmd == MachO::LC_THREAD)
8063 outs() << " cmd LC_THREAD\n";
8064 else if (t.cmd == MachO::LC_UNIXTHREAD)
8065 outs() << " cmd LC_UNIXTHREAD\n";
8066 else
8067 outs() << " cmd " << t.cmd << " (unknown)\n";
8068 outs() << " cmdsize " << t.cmdsize;
8069 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8070 outs() << " Incorrect size\n";
8071 else
8072 outs() << "\n";
8073
8074 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8075 const char *end = Ptr + t.cmdsize;
8076 uint32_t flavor, count, left;
8077 if (cputype == MachO::CPU_TYPE_X86_64) {
8078 while (begin < end) {
8079 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8080 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8081 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008082 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008083 flavor = 0;
8084 begin = end;
8085 }
8086 if (isLittleEndian != sys::IsLittleEndianHost)
8087 sys::swapByteOrder(flavor);
8088 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8089 memcpy((char *)&count, begin, sizeof(uint32_t));
8090 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008091 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008092 count = 0;
8093 begin = end;
8094 }
8095 if (isLittleEndian != sys::IsLittleEndianHost)
8096 sys::swapByteOrder(count);
8097 if (flavor == MachO::x86_THREAD_STATE64) {
8098 outs() << " flavor x86_THREAD_STATE64\n";
8099 if (count == MachO::x86_THREAD_STATE64_COUNT)
8100 outs() << " count x86_THREAD_STATE64_COUNT\n";
8101 else
8102 outs() << " count " << count
8103 << " (not x86_THREAD_STATE64_COUNT)\n";
8104 MachO::x86_thread_state64_t cpu64;
8105 left = end - begin;
8106 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8107 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8108 begin += sizeof(MachO::x86_thread_state64_t);
8109 } else {
8110 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8111 memcpy(&cpu64, begin, left);
8112 begin += left;
8113 }
8114 if (isLittleEndian != sys::IsLittleEndianHost)
8115 swapStruct(cpu64);
8116 Print_x86_thread_state64_t(cpu64);
8117 } else if (flavor == MachO::x86_THREAD_STATE) {
8118 outs() << " flavor x86_THREAD_STATE\n";
8119 if (count == MachO::x86_THREAD_STATE_COUNT)
8120 outs() << " count x86_THREAD_STATE_COUNT\n";
8121 else
8122 outs() << " count " << count
8123 << " (not x86_THREAD_STATE_COUNT)\n";
8124 struct MachO::x86_thread_state_t ts;
8125 left = end - begin;
8126 if (left >= sizeof(MachO::x86_thread_state_t)) {
8127 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8128 begin += sizeof(MachO::x86_thread_state_t);
8129 } else {
8130 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8131 memcpy(&ts, begin, left);
8132 begin += left;
8133 }
8134 if (isLittleEndian != sys::IsLittleEndianHost)
8135 swapStruct(ts);
8136 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8137 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8138 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8139 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8140 else
8141 outs() << "tsh.count " << ts.tsh.count
8142 << " (not x86_THREAD_STATE64_COUNT\n";
8143 Print_x86_thread_state64_t(ts.uts.ts64);
8144 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008145 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8146 << ts.tsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008147 }
8148 } else if (flavor == MachO::x86_FLOAT_STATE) {
8149 outs() << " flavor x86_FLOAT_STATE\n";
8150 if (count == MachO::x86_FLOAT_STATE_COUNT)
8151 outs() << " count x86_FLOAT_STATE_COUNT\n";
8152 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008153 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008154 struct MachO::x86_float_state_t fs;
8155 left = end - begin;
8156 if (left >= sizeof(MachO::x86_float_state_t)) {
8157 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8158 begin += sizeof(MachO::x86_float_state_t);
8159 } else {
8160 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8161 memcpy(&fs, begin, left);
8162 begin += left;
8163 }
8164 if (isLittleEndian != sys::IsLittleEndianHost)
8165 swapStruct(fs);
8166 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8167 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008168 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008169 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8170 else
8171 outs() << "fsh.count " << fs.fsh.count
8172 << " (not x86_FLOAT_STATE64_COUNT\n";
8173 Print_x86_float_state_t(fs.ufs.fs64);
8174 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008175 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8176 << fs.fsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008177 }
8178 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8179 outs() << " flavor x86_EXCEPTION_STATE\n";
8180 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8181 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8182 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008183 outs() << " count " << count
Kevin Enderby48ef5342014-12-23 22:56:39 +00008184 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8185 struct MachO::x86_exception_state_t es;
8186 left = end - begin;
8187 if (left >= sizeof(MachO::x86_exception_state_t)) {
8188 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8189 begin += sizeof(MachO::x86_exception_state_t);
8190 } else {
8191 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8192 memcpy(&es, begin, left);
8193 begin += left;
8194 }
8195 if (isLittleEndian != sys::IsLittleEndianHost)
8196 swapStruct(es);
8197 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8198 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008199 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008200 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8201 else
8202 outs() << "\t esh.count " << es.esh.count
8203 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8204 Print_x86_exception_state_t(es.ues.es64);
8205 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008206 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8207 << es.esh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008208 }
8209 } else {
8210 outs() << " flavor " << flavor << " (unknown)\n";
8211 outs() << " count " << count << "\n";
8212 outs() << " state (unknown)\n";
8213 begin += count * sizeof(uint32_t);
8214 }
8215 }
8216 } else {
8217 while (begin < end) {
8218 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8219 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8220 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008221 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008222 flavor = 0;
8223 begin = end;
8224 }
8225 if (isLittleEndian != sys::IsLittleEndianHost)
8226 sys::swapByteOrder(flavor);
8227 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8228 memcpy((char *)&count, begin, sizeof(uint32_t));
8229 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008230 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008231 count = 0;
8232 begin = end;
8233 }
8234 if (isLittleEndian != sys::IsLittleEndianHost)
8235 sys::swapByteOrder(count);
8236 outs() << " flavor " << flavor << "\n";
8237 outs() << " count " << count << "\n";
8238 outs() << " state (Unknown cputype/cpusubtype)\n";
8239 begin += count * sizeof(uint32_t);
8240 }
8241 }
8242}
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008243
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008244static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8245 if (dl.cmd == MachO::LC_ID_DYLIB)
8246 outs() << " cmd LC_ID_DYLIB\n";
8247 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8248 outs() << " cmd LC_LOAD_DYLIB\n";
8249 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8250 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8251 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8252 outs() << " cmd LC_REEXPORT_DYLIB\n";
8253 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8254 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8255 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8256 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8257 else
8258 outs() << " cmd " << dl.cmd << " (unknown)\n";
8259 outs() << " cmdsize " << dl.cmdsize;
8260 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8261 outs() << " Incorrect size\n";
8262 else
8263 outs() << "\n";
8264 if (dl.dylib.name < dl.cmdsize) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008265 const char *P = (const char *)(Ptr) + dl.dylib.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008266 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8267 } else {
8268 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8269 }
8270 outs() << " time stamp " << dl.dylib.timestamp << " ";
8271 time_t t = dl.dylib.timestamp;
8272 outs() << ctime(&t);
8273 outs() << " current version ";
8274 if (dl.dylib.current_version == 0xffffffff)
8275 outs() << "n/a\n";
8276 else
8277 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8278 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8279 << (dl.dylib.current_version & 0xff) << "\n";
8280 outs() << "compatibility version ";
8281 if (dl.dylib.compatibility_version == 0xffffffff)
8282 outs() << "n/a\n";
8283 else
8284 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8285 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8286 << (dl.dylib.compatibility_version & 0xff) << "\n";
8287}
8288
8289static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8290 uint32_t object_size) {
8291 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8292 outs() << " cmd LC_FUNCTION_STARTS\n";
8293 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8294 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8295 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8296 outs() << " cmd LC_FUNCTION_STARTS\n";
8297 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8298 outs() << " cmd LC_DATA_IN_CODE\n";
8299 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8300 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8301 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8302 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8303 else
8304 outs() << " cmd " << ld.cmd << " (?)\n";
8305 outs() << " cmdsize " << ld.cmdsize;
8306 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8307 outs() << " Incorrect size\n";
8308 else
8309 outs() << "\n";
8310 outs() << " dataoff " << ld.dataoff;
8311 if (ld.dataoff > object_size)
8312 outs() << " (past end of file)\n";
8313 else
8314 outs() << "\n";
8315 outs() << " datasize " << ld.datasize;
8316 uint64_t big_size = ld.dataoff;
8317 big_size += ld.datasize;
8318 if (big_size > object_size)
8319 outs() << " (past end of file)\n";
8320 else
8321 outs() << "\n";
8322}
8323
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008324static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8325 uint32_t cputype, bool verbose) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008326 StringRef Buf = Obj->getData();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008327 unsigned Index = 0;
8328 for (const auto &Command : Obj->load_commands()) {
8329 outs() << "Load command " << Index++ << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00008330 if (Command.C.cmd == MachO::LC_SEGMENT) {
8331 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8332 const char *sg_segname = SLC.segname;
8333 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8334 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8335 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8336 verbose);
8337 for (unsigned j = 0; j < SLC.nsects; j++) {
Kevin Enderbyc9713382014-12-16 01:14:45 +00008338 MachO::section S = Obj->getSection(Command, j);
Kevin Enderby956366c2014-08-29 22:30:52 +00008339 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8340 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8341 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8342 }
8343 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8344 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8345 const char *sg_segname = SLC_64.segname;
8346 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8347 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8348 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8349 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8350 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8351 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8352 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8353 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8354 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8355 sg_segname, filetype, Buf.size(), verbose);
8356 }
8357 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8358 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008359 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008360 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8361 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8362 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008363 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8364 Obj->is64Bit());
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008365 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8366 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8367 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8368 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8369 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8370 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8371 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8372 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8373 PrintDyldLoadCommand(Dyld, Command.Ptr);
8374 } else if (Command.C.cmd == MachO::LC_UUID) {
8375 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8376 PrintUuidLoadCommand(Uuid);
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008377 } else if (Command.C.cmd == MachO::LC_RPATH) {
8378 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8379 PrintRpathLoadCommand(Rpath, Command.Ptr);
Kevin Enderby1ff0ecc2014-12-16 21:48:27 +00008380 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
8381 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008382 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8383 PrintVersionMinLoadCommand(Vd);
8384 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8385 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8386 PrintSourceVersionCommand(Sd);
8387 } else if (Command.C.cmd == MachO::LC_MAIN) {
8388 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8389 PrintEntryPointCommand(Ep);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008390 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008391 MachO::encryption_info_command Ei =
8392 Obj->getEncryptionInfoCommand(Command);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008393 PrintEncryptionInfoCommand(Ei, Buf.size());
Kevin Enderby57538292014-12-17 01:01:30 +00008394 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008395 MachO::encryption_info_command_64 Ei =
8396 Obj->getEncryptionInfoCommand64(Command);
Kevin Enderby57538292014-12-17 01:01:30 +00008397 PrintEncryptionInfoCommand64(Ei, Buf.size());
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008398 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008399 MachO::linker_option_command Lo =
8400 Obj->getLinkerOptionLoadCommand(Command);
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008401 PrintLinkerOptionCommand(Lo, Command.Ptr);
Kevin Enderbyb4b79312014-12-18 19:24:35 +00008402 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8403 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8404 PrintSubFrameworkCommand(Sf, Command.Ptr);
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00008405 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8406 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8407 PrintSubUmbrellaCommand(Sf, Command.Ptr);
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008408 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8409 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8410 PrintSubLibraryCommand(Sl, Command.Ptr);
Kevin Enderby186eac32014-12-19 21:06:24 +00008411 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8412 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8413 PrintSubClientCommand(Sc, Command.Ptr);
Kevin Enderby52e4ce42014-12-19 22:25:22 +00008414 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8415 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8416 PrintRoutinesCommand(Rc);
8417 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8418 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8419 PrintRoutinesCommand64(Rc);
Kevin Enderby48ef5342014-12-23 22:56:39 +00008420 } else if (Command.C.cmd == MachO::LC_THREAD ||
8421 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8422 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8423 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
Nick Kledzik15558912014-10-16 18:58:20 +00008424 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8425 Command.C.cmd == MachO::LC_ID_DYLIB ||
8426 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8427 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8428 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8429 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008430 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8431 PrintDylibCommand(Dl, Command.Ptr);
8432 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8433 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8434 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8435 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8436 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8437 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8438 MachO::linkedit_data_command Ld =
8439 Obj->getLinkeditDataLoadCommand(Command);
8440 PrintLinkEditDataCommand(Ld, Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008441 } else {
8442 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8443 << ")\n";
8444 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8445 // TODO: get and print the raw bytes of the load command.
8446 }
8447 // TODO: print all the other kinds of load commands.
Kevin Enderby956366c2014-08-29 22:30:52 +00008448 }
8449}
8450
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008451static void getAndPrintMachHeader(const MachOObjectFile *Obj,
Kevin Enderby956366c2014-08-29 22:30:52 +00008452 uint32_t &filetype, uint32_t &cputype,
8453 bool verbose) {
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008454 if (Obj->is64Bit()) {
8455 MachO::mach_header_64 H_64;
8456 H_64 = Obj->getHeader64();
8457 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8458 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00008459 filetype = H_64.filetype;
8460 cputype = H_64.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008461 } else {
8462 MachO::mach_header H;
8463 H = Obj->getHeader();
8464 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8465 H.sizeofcmds, H.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00008466 filetype = H.filetype;
8467 cputype = H.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008468 }
8469}
8470
8471void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8472 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby956366c2014-08-29 22:30:52 +00008473 uint32_t filetype = 0;
8474 uint32_t cputype = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008475 getAndPrintMachHeader(file, filetype, cputype, !NonVerbose);
8476 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008477}
Nick Kledzikd04bc352014-08-30 00:20:14 +00008478
8479//===----------------------------------------------------------------------===//
8480// export trie dumping
8481//===----------------------------------------------------------------------===//
8482
8483void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008484 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
8485 uint64_t Flags = Entry.flags();
Nick Kledzikd04bc352014-08-30 00:20:14 +00008486 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
8487 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
8488 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8489 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
8490 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8491 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
8492 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
8493 if (ReExport)
8494 outs() << "[re-export] ";
8495 else
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008496 outs() << format("0x%08llX ",
8497 Entry.address()); // FIXME:add in base address
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008498 outs() << Entry.name();
Nick Kledzikd04bc352014-08-30 00:20:14 +00008499 if (WeakDef || ThreadLocal || Resolver || Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008500 bool NeedsComma = false;
Nick Kledzik1d1ac4b2014-09-03 01:12:52 +00008501 outs() << " [";
Nick Kledzikd04bc352014-08-30 00:20:14 +00008502 if (WeakDef) {
8503 outs() << "weak_def";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008504 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008505 }
8506 if (ThreadLocal) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008507 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008508 outs() << ", ";
8509 outs() << "per-thread";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008510 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008511 }
8512 if (Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008513 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008514 outs() << ", ";
8515 outs() << "absolute";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008516 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008517 }
8518 if (Resolver) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008519 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008520 outs() << ", ";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008521 outs() << format("resolver=0x%08llX", Entry.other());
8522 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008523 }
8524 outs() << "]";
8525 }
8526 if (ReExport) {
8527 StringRef DylibName = "unknown";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008528 int Ordinal = Entry.other() - 1;
8529 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
8530 if (Entry.otherName().empty())
Nick Kledzikd04bc352014-08-30 00:20:14 +00008531 outs() << " (from " << DylibName << ")";
8532 else
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008533 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
Nick Kledzikd04bc352014-08-30 00:20:14 +00008534 }
8535 outs() << "\n";
8536 }
8537}
Nick Kledzikac431442014-09-12 21:34:15 +00008538
Nick Kledzikac431442014-09-12 21:34:15 +00008539//===----------------------------------------------------------------------===//
8540// rebase table dumping
8541//===----------------------------------------------------------------------===//
8542
8543namespace {
8544class SegInfo {
8545public:
8546 SegInfo(const object::MachOObjectFile *Obj);
8547
8548 StringRef segmentName(uint32_t SegIndex);
8549 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
8550 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
8551
8552private:
8553 struct SectionInfo {
8554 uint64_t Address;
8555 uint64_t Size;
8556 StringRef SectionName;
8557 StringRef SegmentName;
8558 uint64_t OffsetInSegment;
8559 uint64_t SegmentStartAddress;
8560 uint32_t SegmentIndex;
8561 };
8562 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
8563 SmallVector<SectionInfo, 32> Sections;
8564};
8565}
8566
8567SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
8568 // Build table of sections so segIndex/offset pairs can be translated.
Nick Kledzik56ebef42014-09-16 01:41:51 +00008569 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
Nick Kledzikac431442014-09-12 21:34:15 +00008570 StringRef CurSegName;
8571 uint64_t CurSegAddress;
8572 for (const SectionRef &Section : Obj->sections()) {
8573 SectionInfo Info;
8574 if (error(Section.getName(Info.SectionName)))
8575 return;
Rafael Espindola80291272014-10-08 15:28:58 +00008576 Info.Address = Section.getAddress();
8577 Info.Size = Section.getSize();
Nick Kledzikac431442014-09-12 21:34:15 +00008578 Info.SegmentName =
8579 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
8580 if (!Info.SegmentName.equals(CurSegName)) {
8581 ++CurSegIndex;
8582 CurSegName = Info.SegmentName;
8583 CurSegAddress = Info.Address;
8584 }
8585 Info.SegmentIndex = CurSegIndex - 1;
8586 Info.OffsetInSegment = Info.Address - CurSegAddress;
8587 Info.SegmentStartAddress = CurSegAddress;
8588 Sections.push_back(Info);
8589 }
8590}
8591
8592StringRef SegInfo::segmentName(uint32_t SegIndex) {
8593 for (const SectionInfo &SI : Sections) {
8594 if (SI.SegmentIndex == SegIndex)
8595 return SI.SegmentName;
8596 }
8597 llvm_unreachable("invalid segIndex");
8598}
8599
8600const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
8601 uint64_t OffsetInSeg) {
8602 for (const SectionInfo &SI : Sections) {
8603 if (SI.SegmentIndex != SegIndex)
8604 continue;
8605 if (SI.OffsetInSegment > OffsetInSeg)
8606 continue;
8607 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8608 continue;
8609 return SI;
8610 }
8611 llvm_unreachable("segIndex and offset not in any section");
8612}
8613
8614StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
8615 return findSection(SegIndex, OffsetInSeg).SectionName;
8616}
8617
8618uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
8619 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
8620 return SI.SegmentStartAddress + OffsetInSeg;
8621}
8622
8623void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
8624 // Build table of sections so names can used in final output.
8625 SegInfo sectionTable(Obj);
8626
8627 outs() << "segment section address type\n";
8628 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
8629 uint32_t SegIndex = Entry.segmentIndex();
8630 uint64_t OffsetInSeg = Entry.segmentOffset();
8631 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8632 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8633 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8634
8635 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008636 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
8637 SegmentName.str().c_str(), SectionName.str().c_str(),
8638 Address, Entry.typeName().str().c_str());
Nick Kledzikac431442014-09-12 21:34:15 +00008639 }
8640}
Nick Kledzik56ebef42014-09-16 01:41:51 +00008641
8642static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
8643 StringRef DylibName;
8644 switch (Ordinal) {
8645 case MachO::BIND_SPECIAL_DYLIB_SELF:
8646 return "this-image";
8647 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
8648 return "main-executable";
8649 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
8650 return "flat-namespace";
8651 default:
Nick Kledzikabd29872014-09-16 22:03:13 +00008652 if (Ordinal > 0) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008653 std::error_code EC =
8654 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
Nick Kledzikabd29872014-09-16 22:03:13 +00008655 if (EC)
Nick Kledzik51d2c2b2014-10-14 23:29:38 +00008656 return "<<bad library ordinal>>";
Nick Kledzikabd29872014-09-16 22:03:13 +00008657 return DylibName;
8658 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00008659 }
Nick Kledzikabd29872014-09-16 22:03:13 +00008660 return "<<unknown special ordinal>>";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008661}
8662
8663//===----------------------------------------------------------------------===//
8664// bind table dumping
8665//===----------------------------------------------------------------------===//
8666
8667void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
8668 // Build table of sections so names can used in final output.
8669 SegInfo sectionTable(Obj);
8670
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008671 outs() << "segment section address type "
8672 "addend dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008673 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
8674 uint32_t SegIndex = Entry.segmentIndex();
8675 uint64_t OffsetInSeg = Entry.segmentOffset();
8676 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8677 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8678 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8679
8680 // Table lines look like:
8681 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008682 StringRef Attr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00008683 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008684 Attr = " (weak_import)";
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008685 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008686 << left_justify(SectionName, 18) << " "
8687 << format_hex(Address, 10, true) << " "
8688 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008689 << format_decimal(Entry.addend(), 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008690 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008691 << Entry.symbolName() << Attr << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008692 }
8693}
8694
8695//===----------------------------------------------------------------------===//
8696// lazy bind table dumping
8697//===----------------------------------------------------------------------===//
8698
8699void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
8700 // Build table of sections so names can used in final output.
8701 SegInfo sectionTable(Obj);
8702
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008703 outs() << "segment section address "
8704 "dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008705 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
8706 uint32_t SegIndex = Entry.segmentIndex();
8707 uint64_t OffsetInSeg = Entry.segmentOffset();
8708 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8709 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8710 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8711
8712 // Table lines look like:
8713 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008714 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008715 << left_justify(SectionName, 18) << " "
8716 << format_hex(Address, 10, true) << " "
8717 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Nick Kledzik56ebef42014-09-16 01:41:51 +00008718 << Entry.symbolName() << "\n";
8719 }
8720}
8721
Nick Kledzik56ebef42014-09-16 01:41:51 +00008722//===----------------------------------------------------------------------===//
8723// weak bind table dumping
8724//===----------------------------------------------------------------------===//
8725
8726void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
8727 // Build table of sections so names can used in final output.
8728 SegInfo sectionTable(Obj);
8729
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008730 outs() << "segment section address "
8731 "type addend symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008732 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
8733 // Strong symbols don't have a location to update.
8734 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008735 outs() << " strong "
Nick Kledzik56ebef42014-09-16 01:41:51 +00008736 << Entry.symbolName() << "\n";
8737 continue;
8738 }
8739 uint32_t SegIndex = Entry.segmentIndex();
8740 uint64_t OffsetInSeg = Entry.segmentOffset();
8741 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8742 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8743 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8744
8745 // Table lines look like:
8746 // __DATA __data 0x00001000 pointer 0 _foo
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008747 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008748 << left_justify(SectionName, 18) << " "
8749 << format_hex(Address, 10, true) << " "
8750 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008751 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
8752 << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008753 }
8754}
8755
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008756// get_dyld_bind_info_symbolname() is used for disassembly and passed an
8757// address, ReferenceValue, in the Mach-O file and looks in the dyld bind
8758// information for that address. If the address is found its binding symbol
8759// name is returned. If not nullptr is returned.
8760static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
8761 struct DisassembleInfo *info) {
Kevin Enderby078be602014-10-23 19:53:12 +00008762 if (info->bindtable == nullptr) {
8763 info->bindtable = new (BindTable);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008764 SegInfo sectionTable(info->O);
8765 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
8766 uint32_t SegIndex = Entry.segmentIndex();
8767 uint64_t OffsetInSeg = Entry.segmentOffset();
8768 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8769 const char *SymbolName = nullptr;
8770 StringRef name = Entry.symbolName();
8771 if (!name.empty())
8772 SymbolName = name.data();
Kevin Enderby078be602014-10-23 19:53:12 +00008773 info->bindtable->push_back(std::make_pair(Address, SymbolName));
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008774 }
8775 }
Kevin Enderby078be602014-10-23 19:53:12 +00008776 for (bind_table_iterator BI = info->bindtable->begin(),
8777 BE = info->bindtable->end();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008778 BI != BE; ++BI) {
8779 uint64_t Address = BI->first;
8780 if (ReferenceValue == Address) {
8781 const char *SymbolName = BI->second;
8782 return SymbolName;
8783 }
8784 }
8785 return nullptr;
8786}