blob: 7b868fa9d0de8716b5ab220adc14bcba4de6172d [file] [log] [blame]
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001//===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the MachO-specific dumper for llvm-objdump.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm-objdump.h"
Kevin Enderby98c9acc2014-09-16 18:00:57 +000015#include "llvm-c/Disassembler.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000016#include "llvm/ADT/STLExtras.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000017#include "llvm/ADT/StringExtras.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000018#include "llvm/ADT/Triple.h"
Kevin Enderby04bf6932014-10-28 23:39:46 +000019#include "llvm/Config/config.h"
Zachary Turner6489d7b2015-04-23 17:37:47 +000020#include "llvm/DebugInfo/DIContext.h"
21#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000022#include "llvm/MC/MCAsmInfo.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000023#include "llvm/MC/MCContext.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000024#include "llvm/MC/MCDisassembler.h"
25#include "llvm/MC/MCInst.h"
26#include "llvm/MC/MCInstPrinter.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000027#include "llvm/MC/MCInstrDesc.h"
28#include "llvm/MC/MCInstrInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000029#include "llvm/MC/MCRegisterInfo.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000030#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000031#include "llvm/Object/MachO.h"
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +000032#include "llvm/Object/MachOUniversal.h"
Rafael Espindola9b709252013-04-13 01:45:40 +000033#include "llvm/Support/Casting.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000034#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/Debug.h"
Tim Northover4bd286a2014-08-01 13:07:19 +000036#include "llvm/Support/Endian.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000037#include "llvm/Support/Format.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000038#include "llvm/Support/FormattedStream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000039#include "llvm/Support/GraphWriter.h"
Kevin Enderby9a509442015-01-27 21:28:24 +000040#include "llvm/Support/LEB128.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000041#include "llvm/Support/MachO.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000042#include "llvm/Support/MemoryBuffer.h"
43#include "llvm/Support/TargetRegistry.h"
44#include "llvm/Support/TargetSelect.h"
45#include "llvm/Support/raw_ostream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000046#include <algorithm>
47#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000048#include <system_error>
Kevin Enderby04bf6932014-10-28 23:39:46 +000049
50#if HAVE_CXXABI_H
51#include <cxxabi.h>
52#endif
53
Benjamin Kramer43a772e2011-09-19 17:56:04 +000054using namespace llvm;
55using namespace object;
56
57static cl::opt<bool>
Kevin Enderbyb28ed012014-10-29 21:28:24 +000058 UseDbg("g",
59 cl::desc("Print line information from debug info if available"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000060
Kevin Enderbyb28ed012014-10-29 21:28:24 +000061static cl::opt<std::string> DSYMFile("dsym",
62 cl::desc("Use .dSYM file for debug info"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000063
Kevin Enderbyb28ed012014-10-29 21:28:24 +000064static cl::opt<bool> FullLeadingAddr("full-leading-addr",
65 cl::desc("Print full leading address"));
Kevin Enderbybf246f52014-09-24 23:08:22 +000066
Kevin Enderbyab5e6c92015-03-17 21:07:39 +000067static cl::opt<bool> NoLeadingAddr("no-leading-addr",
68 cl::desc("Print no leading address"));
69
Kevin Enderby13023a12015-01-15 23:19:11 +000070cl::opt<bool> llvm::UniversalHeaders("universal-headers",
71 cl::desc("Print Mach-O universal headers "
72 "(requires -macho)"));
73
Kevin Enderby131d1772015-01-09 19:22:37 +000074cl::opt<bool>
Kevin Enderby13023a12015-01-15 23:19:11 +000075 llvm::ArchiveHeaders("archive-headers",
76 cl::desc("Print archive headers for Mach-O archives "
77 "(requires -macho)"));
Kevin Enderby131d1772015-01-09 19:22:37 +000078
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000079cl::opt<bool>
Kevin Enderby8972e482015-04-30 20:30:42 +000080 ArchiveMemberOffsets("archive-member-offsets",
81 cl::desc("Print the offset to each archive member for "
82 "Mach-O archives (requires -macho and "
83 "-archive-headers)"));
84
85cl::opt<bool>
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000086 llvm::IndirectSymbols("indirect-symbols",
87 cl::desc("Print indirect symbol table for Mach-O "
88 "objects (requires -macho)"));
89
Kevin Enderby69fe98d2015-01-23 18:52:17 +000090cl::opt<bool>
91 llvm::DataInCode("data-in-code",
92 cl::desc("Print the data in code table for Mach-O objects "
93 "(requires -macho)"));
94
Kevin Enderby9a509442015-01-27 21:28:24 +000095cl::opt<bool>
96 llvm::LinkOptHints("link-opt-hints",
97 cl::desc("Print the linker optimization hints for "
98 "Mach-O objects (requires -macho)"));
99
Kevin Enderbycd66be52015-03-11 22:06:32 +0000100cl::opt<bool>
101 llvm::InfoPlist("info-plist",
102 cl::desc("Print the info plist section as strings for "
103 "Mach-O objects (requires -macho)"));
104
Kevin Enderbyf0640752015-03-13 17:56:32 +0000105cl::opt<bool>
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000106 llvm::DylibsUsed("dylibs-used",
107 cl::desc("Print the shared libraries used for linked "
108 "Mach-O files (requires -macho)"));
109
110cl::opt<bool>
111 llvm::DylibId("dylib-id",
112 cl::desc("Print the shared library's id for the dylib Mach-O "
113 "file (requires -macho)"));
114
115cl::opt<bool>
Kevin Enderbyf0640752015-03-13 17:56:32 +0000116 llvm::NonVerbose("non-verbose",
117 cl::desc("Print the info for Mach-O objects in "
118 "non-verbose or numeric form (requires -macho)"));
119
Kevin Enderby0fc11822015-04-01 20:57:01 +0000120cl::opt<bool>
121 llvm::ObjcMetaData("objc-meta-data",
122 cl::desc("Print the Objective-C runtime meta data for "
123 "Mach-O files (requires -macho)"));
124
Kevin Enderby6a221752015-03-17 17:10:57 +0000125cl::opt<std::string> llvm::DisSymName(
126 "dis-symname",
127 cl::desc("disassemble just this symbol's instructions (requires -macho"));
128
Kevin Enderby8e29ec92015-03-17 22:26:11 +0000129static cl::opt<bool> NoSymbolicOperands(
130 "no-symbolic-operands",
131 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
132
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000133static cl::list<std::string>
134 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
135 cl::ZeroOrMore);
136bool ArchAll = false;
137
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000138static std::string ThumbTripleName;
139
140static const Target *GetTarget(const MachOObjectFile *MachOObj,
141 const char **McpuDefault,
142 const Target **ThumbTarget) {
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000143 // Figure out the target triple.
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000144 if (TripleName.empty()) {
145 llvm::Triple TT("unknown-unknown-unknown");
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000146 llvm::Triple ThumbTriple = Triple();
147 TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000148 TripleName = TT.str();
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000149 ThumbTripleName = ThumbTriple.str();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000150 }
151
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000152 // Get the target specific parser.
153 std::string Error;
154 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000155 if (TheTarget && ThumbTripleName.empty())
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000156 return TheTarget;
157
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000158 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
159 if (*ThumbTarget)
160 return TheTarget;
161
162 errs() << "llvm-objdump: error: unable to get target for '";
163 if (!TheTarget)
164 errs() << TripleName;
165 else
166 errs() << ThumbTripleName;
167 errs() << "', see --version and --triple.\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000168 return nullptr;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000169}
170
Owen Andersond9243c42011-10-17 21:37:35 +0000171struct SymbolSorter {
172 bool operator()(const SymbolRef &A, const SymbolRef &B) {
Rafael Espindoladea00162015-07-03 17:44:18 +0000173 uint64_t AAddr = (A.getType() != SymbolRef::ST_Function) ? 0 : A.getValue();
174 uint64_t BAddr = (B.getType() != SymbolRef::ST_Function) ? 0 : B.getValue();
Owen Andersond9243c42011-10-17 21:37:35 +0000175 return AAddr < BAddr;
176 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000177};
178
Kevin Enderby273ae012013-06-06 17:20:50 +0000179// Types for the storted data in code table that is built before disassembly
180// and the predicate function to sort them.
181typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
182typedef std::vector<DiceTableEntry> DiceTable;
183typedef DiceTable::iterator dice_table_iterator;
184
Kevin Enderby930fdc72014-11-06 19:00:13 +0000185// This is used to search for a data in code table entry for the PC being
186// disassembled. The j parameter has the PC in j.first. A single data in code
187// table entry can cover many bytes for each of its Kind's. So if the offset,
188// aka the i.first value, of the data in code table entry plus its Length
189// covers the PC being searched for this will return true. If not it will
190// return false.
David Majnemerea9b8ee2014-11-04 08:41:48 +0000191static bool compareDiceTableEntries(const DiceTableEntry &i,
192 const DiceTableEntry &j) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000193 uint16_t Length;
194 i.second.getLength(Length);
195
196 return j.first >= i.first && j.first < i.first + Length;
Kevin Enderby273ae012013-06-06 17:20:50 +0000197}
198
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000199static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
Kevin Enderby930fdc72014-11-06 19:00:13 +0000200 unsigned short Kind) {
201 uint32_t Value, Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000202
203 switch (Kind) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000204 default:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000205 case MachO::DICE_KIND_DATA:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000206 if (Length >= 4) {
207 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000208 dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000209 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000210 outs() << "\t.long " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000211 Size = 4;
212 } else if (Length >= 2) {
213 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000214 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000215 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000216 outs() << "\t.short " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000217 Size = 2;
218 } else {
219 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000220 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000221 Value = bytes[0];
222 outs() << "\t.byte " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000223 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000224 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000225 if (Kind == MachO::DICE_KIND_DATA)
226 outs() << "\t@ KIND_DATA\n";
227 else
228 outs() << "\t@ data in code kind = " << Kind << "\n";
Kevin Enderby273ae012013-06-06 17:20:50 +0000229 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000230 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000231 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000232 dumpBytes(ArrayRef<uint8_t>(bytes, 1), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000233 Value = bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000234 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
235 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000236 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000237 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000238 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000239 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000240 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000241 outs() << "\t.short " << format("%5u", Value & 0xffff)
242 << "\t@ KIND_JUMP_TABLE16\n";
243 Size = 2;
Kevin Enderby273ae012013-06-06 17:20:50 +0000244 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000245 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000246 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
247 if (!NoShowRawInsn)
Colin LeMahieu2048ea42015-05-28 18:39:50 +0000248 dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000249 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000250 outs() << "\t.long " << Value;
251 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
252 outs() << "\t@ KIND_JUMP_TABLE32\n";
253 else
254 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
255 Size = 4;
Kevin Enderby273ae012013-06-06 17:20:50 +0000256 break;
257 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000258 return Size;
Kevin Enderby273ae012013-06-06 17:20:50 +0000259}
260
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000261static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000262 std::vector<SectionRef> &Sections,
263 std::vector<SymbolRef> &Symbols,
264 SmallVectorImpl<uint64_t> &FoundFns,
265 uint64_t &BaseSegmentAddress) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000266 for (const SymbolRef &Symbol : MachOObj->symbols()) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000267 ErrorOr<StringRef> SymName = Symbol.getName();
268 if (std::error_code EC = SymName.getError())
269 report_fatal_error(EC.message());
270 if (!SymName->startswith("ltmp"))
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000271 Symbols.push_back(Symbol);
272 }
Owen Andersond9243c42011-10-17 21:37:35 +0000273
Alexey Samsonov48803e52014-03-13 14:37:36 +0000274 for (const SectionRef &Section : MachOObj->sections()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000275 StringRef SectName;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000276 Section.getName(SectName);
277 Sections.push_back(Section);
Owen Andersond9243c42011-10-17 21:37:35 +0000278 }
279
Kevin Enderby273ae012013-06-06 17:20:50 +0000280 bool BaseSegmentAddressSet = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000281 for (const auto &Command : MachOObj->load_commands()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000282 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000283 // We found a function starts segment, parse the addresses for later
284 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000285 MachO::linkedit_data_command LLC =
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000286 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000287
Charles Davis8bdfafd2013-09-01 04:28:48 +0000288 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000289 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
290 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000291 StringRef SegName = SLC.segname;
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000292 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
Kevin Enderby273ae012013-06-06 17:20:50 +0000293 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000294 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000295 }
296 }
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000297 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000298}
299
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000300static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
301 uint32_t n, uint32_t count,
302 uint32_t stride, uint64_t addr) {
303 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
304 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
305 if (n > nindirectsyms)
306 outs() << " (entries start past the end of the indirect symbol "
307 "table) (reserved1 field greater than the table size)";
308 else if (n + count > nindirectsyms)
309 outs() << " (entries extends past the end of the indirect symbol "
310 "table)";
311 outs() << "\n";
312 uint32_t cputype = O->getHeader().cputype;
313 if (cputype & MachO::CPU_ARCH_ABI64)
314 outs() << "address index";
315 else
316 outs() << "address index";
317 if (verbose)
318 outs() << " name\n";
319 else
320 outs() << "\n";
321 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
322 if (cputype & MachO::CPU_ARCH_ABI64)
323 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
324 else
325 outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
326 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
327 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
328 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
329 outs() << "LOCAL\n";
330 continue;
331 }
332 if (indirect_symbol ==
333 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
334 outs() << "LOCAL ABSOLUTE\n";
335 continue;
336 }
337 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
338 outs() << "ABSOLUTE\n";
339 continue;
340 }
341 outs() << format("%5u ", indirect_symbol);
Kevin Enderbyf0640752015-03-13 17:56:32 +0000342 if (verbose) {
343 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
344 if (indirect_symbol < Symtab.nsyms) {
345 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
346 SymbolRef Symbol = *Sym;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000347 ErrorOr<StringRef> SymName = Symbol.getName();
348 if (std::error_code EC = SymName.getError())
349 report_fatal_error(EC.message());
350 outs() << *SymName;
Kevin Enderbyf0640752015-03-13 17:56:32 +0000351 } else {
352 outs() << "?";
353 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000354 }
355 outs() << "\n";
356 }
357}
358
359static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000360 for (const auto &Load : O->load_commands()) {
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000361 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
362 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
363 for (unsigned J = 0; J < Seg.nsects; ++J) {
364 MachO::section_64 Sec = O->getSection64(Load, J);
365 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
366 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
367 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
368 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
369 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
370 section_type == MachO::S_SYMBOL_STUBS) {
371 uint32_t stride;
372 if (section_type == MachO::S_SYMBOL_STUBS)
373 stride = Sec.reserved2;
374 else
375 stride = 8;
376 if (stride == 0) {
377 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
378 << Sec.sectname << ") "
379 << "(size of stubs in reserved2 field is zero)\n";
380 continue;
381 }
382 uint32_t count = Sec.size / stride;
383 outs() << "Indirect symbols for (" << Sec.segname << ","
384 << Sec.sectname << ") " << count << " entries";
385 uint32_t n = Sec.reserved1;
386 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
387 }
388 }
389 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
390 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
391 for (unsigned J = 0; J < Seg.nsects; ++J) {
392 MachO::section Sec = O->getSection(Load, J);
393 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
394 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
395 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
396 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
397 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
398 section_type == MachO::S_SYMBOL_STUBS) {
399 uint32_t stride;
400 if (section_type == MachO::S_SYMBOL_STUBS)
401 stride = Sec.reserved2;
402 else
403 stride = 4;
404 if (stride == 0) {
405 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
406 << Sec.sectname << ") "
407 << "(size of stubs in reserved2 field is zero)\n";
408 continue;
409 }
410 uint32_t count = Sec.size / stride;
411 outs() << "Indirect symbols for (" << Sec.segname << ","
412 << Sec.sectname << ") " << count << " entries";
413 uint32_t n = Sec.reserved1;
414 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
415 }
416 }
417 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000418 }
419}
420
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000421static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
422 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
423 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
424 outs() << "Data in code table (" << nentries << " entries)\n";
425 outs() << "offset length kind\n";
426 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
427 ++DI) {
428 uint32_t Offset;
429 DI->getOffset(Offset);
430 outs() << format("0x%08" PRIx32, Offset) << " ";
431 uint16_t Length;
432 DI->getLength(Length);
433 outs() << format("%6u", Length) << " ";
434 uint16_t Kind;
435 DI->getKind(Kind);
436 if (verbose) {
437 switch (Kind) {
438 case MachO::DICE_KIND_DATA:
439 outs() << "DATA";
440 break;
441 case MachO::DICE_KIND_JUMP_TABLE8:
442 outs() << "JUMP_TABLE8";
443 break;
444 case MachO::DICE_KIND_JUMP_TABLE16:
445 outs() << "JUMP_TABLE16";
446 break;
447 case MachO::DICE_KIND_JUMP_TABLE32:
448 outs() << "JUMP_TABLE32";
449 break;
450 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
451 outs() << "ABS_JUMP_TABLE32";
452 break;
453 default:
454 outs() << format("0x%04" PRIx32, Kind);
455 break;
456 }
457 } else
458 outs() << format("0x%04" PRIx32, Kind);
459 outs() << "\n";
460 }
461}
462
Kevin Enderby9a509442015-01-27 21:28:24 +0000463static void PrintLinkOptHints(MachOObjectFile *O) {
464 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
465 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
466 uint32_t nloh = LohLC.datasize;
467 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
468 for (uint32_t i = 0; i < nloh;) {
469 unsigned n;
470 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
471 i += n;
472 outs() << " identifier " << identifier << " ";
473 if (i >= nloh)
474 return;
475 switch (identifier) {
476 case 1:
477 outs() << "AdrpAdrp\n";
478 break;
479 case 2:
480 outs() << "AdrpLdr\n";
481 break;
482 case 3:
483 outs() << "AdrpAddLdr\n";
484 break;
485 case 4:
486 outs() << "AdrpLdrGotLdr\n";
487 break;
488 case 5:
489 outs() << "AdrpAddStr\n";
490 break;
491 case 6:
492 outs() << "AdrpLdrGotStr\n";
493 break;
494 case 7:
495 outs() << "AdrpAdd\n";
496 break;
497 case 8:
498 outs() << "AdrpLdrGot\n";
499 break;
500 default:
501 outs() << "Unknown identifier value\n";
502 break;
503 }
504 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
505 i += n;
506 outs() << " narguments " << narguments << "\n";
507 if (i >= nloh)
508 return;
509
510 for (uint32_t j = 0; j < narguments; j++) {
511 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
512 i += n;
513 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
514 if (i >= nloh)
515 return;
516 }
517 }
518}
519
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000520static void PrintDylibs(MachOObjectFile *O, bool JustId) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000521 unsigned Index = 0;
522 for (const auto &Load : O->load_commands()) {
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000523 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
524 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
525 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
526 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
527 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
528 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
529 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
530 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
531 if (dl.dylib.name < dl.cmdsize) {
532 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
533 if (JustId)
534 outs() << p << "\n";
535 else {
536 outs() << "\t" << p;
537 outs() << " (compatibility version "
538 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
539 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
540 << (dl.dylib.compatibility_version & 0xff) << ",";
541 outs() << " current version "
542 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
543 << ((dl.dylib.current_version >> 8) & 0xff) << "."
544 << (dl.dylib.current_version & 0xff) << ")\n";
545 }
546 } else {
547 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
548 if (Load.C.cmd == MachO::LC_ID_DYLIB)
549 outs() << "LC_ID_DYLIB ";
550 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
551 outs() << "LC_LOAD_DYLIB ";
552 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
553 outs() << "LC_LOAD_WEAK_DYLIB ";
554 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
555 outs() << "LC_LAZY_LOAD_DYLIB ";
556 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
557 outs() << "LC_REEXPORT_DYLIB ";
558 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
559 outs() << "LC_LOAD_UPWARD_DYLIB ";
560 else
561 outs() << "LC_??? ";
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000562 outs() << "command " << Index++ << "\n";
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000563 }
564 }
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000565 }
566}
567
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000568typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
569
570static void CreateSymbolAddressMap(MachOObjectFile *O,
571 SymbolAddressMap *AddrMap) {
572 // Create a map of symbol addresses to symbol names.
573 for (const SymbolRef &Symbol : O->symbols()) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000574 SymbolRef::Type ST = Symbol.getType();
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000575 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
576 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +0000577 uint64_t Address = Symbol.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000578 ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
579 if (std::error_code EC = SymNameOrErr.getError())
580 report_fatal_error(EC.message());
581 StringRef SymName = *SymNameOrErr;
Kevin Enderby846c0002015-04-16 17:19:59 +0000582 if (!SymName.startswith(".objc"))
583 (*AddrMap)[Address] = SymName;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000584 }
585 }
586}
587
588// GuessSymbolName is passed the address of what might be a symbol and a
589// pointer to the SymbolAddressMap. It returns the name of a symbol
590// with that address or nullptr if no symbol is found with that address.
591static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
592 const char *SymbolName = nullptr;
593 // A DenseMap can't lookup up some values.
594 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
595 StringRef name = AddrMap->lookup(value);
596 if (!name.empty())
597 SymbolName = name.data();
598 }
599 return SymbolName;
600}
601
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000602static void DumpCstringChar(const char c) {
603 char p[2];
604 p[0] = c;
605 p[1] = '\0';
606 outs().write_escaped(p);
607}
608
Kevin Enderby10ba0412015-02-04 21:38:42 +0000609static void DumpCstringSection(MachOObjectFile *O, const char *sect,
610 uint32_t sect_size, uint64_t sect_addr,
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000611 bool print_addresses) {
Kevin Enderby10ba0412015-02-04 21:38:42 +0000612 for (uint32_t i = 0; i < sect_size; i++) {
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000613 if (print_addresses) {
614 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000615 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000616 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000617 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000618 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000619 for (; i < sect_size && sect[i] != '\0'; i++)
620 DumpCstringChar(sect[i]);
Kevin Enderby10ba0412015-02-04 21:38:42 +0000621 if (i < sect_size && sect[i] == '\0')
622 outs() << "\n";
623 }
624}
625
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000626static void DumpLiteral4(uint32_t l, float f) {
627 outs() << format("0x%08" PRIx32, l);
628 if ((l & 0x7f800000) != 0x7f800000)
629 outs() << format(" (%.16e)\n", f);
630 else {
631 if (l == 0x7f800000)
632 outs() << " (+Infinity)\n";
633 else if (l == 0xff800000)
634 outs() << " (-Infinity)\n";
635 else if ((l & 0x00400000) == 0x00400000)
636 outs() << " (non-signaling Not-a-Number)\n";
637 else
638 outs() << " (signaling Not-a-Number)\n";
639 }
640}
641
642static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
643 uint32_t sect_size, uint64_t sect_addr,
644 bool print_addresses) {
645 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
646 if (print_addresses) {
647 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000648 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000649 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000650 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000651 }
652 float f;
653 memcpy(&f, sect + i, sizeof(float));
654 if (O->isLittleEndian() != sys::IsLittleEndianHost)
655 sys::swapByteOrder(f);
656 uint32_t l;
657 memcpy(&l, sect + i, sizeof(uint32_t));
658 if (O->isLittleEndian() != sys::IsLittleEndianHost)
659 sys::swapByteOrder(l);
660 DumpLiteral4(l, f);
661 }
662}
663
664static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
665 double d) {
666 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
667 uint32_t Hi, Lo;
668 if (O->isLittleEndian()) {
669 Hi = l1;
670 Lo = l0;
671 } else {
672 Hi = l0;
673 Lo = l1;
674 }
675 // Hi is the high word, so this is equivalent to if(isfinite(d))
676 if ((Hi & 0x7ff00000) != 0x7ff00000)
677 outs() << format(" (%.16e)\n", d);
678 else {
679 if (Hi == 0x7ff00000 && Lo == 0)
680 outs() << " (+Infinity)\n";
681 else if (Hi == 0xfff00000 && Lo == 0)
682 outs() << " (-Infinity)\n";
683 else if ((Hi & 0x00080000) == 0x00080000)
684 outs() << " (non-signaling Not-a-Number)\n";
685 else
686 outs() << " (signaling Not-a-Number)\n";
687 }
688}
689
690static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
691 uint32_t sect_size, uint64_t sect_addr,
692 bool print_addresses) {
693 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
694 if (print_addresses) {
695 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000696 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000697 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000698 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000699 }
700 double d;
701 memcpy(&d, sect + i, sizeof(double));
702 if (O->isLittleEndian() != sys::IsLittleEndianHost)
703 sys::swapByteOrder(d);
704 uint32_t l0, l1;
705 memcpy(&l0, sect + i, sizeof(uint32_t));
706 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
707 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
708 sys::swapByteOrder(l0);
709 sys::swapByteOrder(l1);
710 }
711 DumpLiteral8(O, l0, l1, d);
712 }
713}
714
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000715static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
716 outs() << format("0x%08" PRIx32, l0) << " ";
717 outs() << format("0x%08" PRIx32, l1) << " ";
718 outs() << format("0x%08" PRIx32, l2) << " ";
719 outs() << format("0x%08" PRIx32, l3) << "\n";
720}
721
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000722static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
723 uint32_t sect_size, uint64_t sect_addr,
724 bool print_addresses) {
725 for (uint32_t i = 0; i < sect_size; i += 16) {
726 if (print_addresses) {
727 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000728 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000729 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000730 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000731 }
732 uint32_t l0, l1, l2, l3;
733 memcpy(&l0, sect + i, sizeof(uint32_t));
734 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
735 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
736 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
737 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
738 sys::swapByteOrder(l0);
739 sys::swapByteOrder(l1);
740 sys::swapByteOrder(l2);
741 sys::swapByteOrder(l3);
742 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000743 DumpLiteral16(l0, l1, l2, l3);
744 }
745}
746
747static void DumpLiteralPointerSection(MachOObjectFile *O,
748 const SectionRef &Section,
749 const char *sect, uint32_t sect_size,
750 uint64_t sect_addr,
751 bool print_addresses) {
752 // Collect the literal sections in this Mach-O file.
753 std::vector<SectionRef> LiteralSections;
754 for (const SectionRef &Section : O->sections()) {
755 DataRefImpl Ref = Section.getRawDataRefImpl();
756 uint32_t section_type;
757 if (O->is64Bit()) {
758 const MachO::section_64 Sec = O->getSection64(Ref);
759 section_type = Sec.flags & MachO::SECTION_TYPE;
760 } else {
761 const MachO::section Sec = O->getSection(Ref);
762 section_type = Sec.flags & MachO::SECTION_TYPE;
763 }
764 if (section_type == MachO::S_CSTRING_LITERALS ||
765 section_type == MachO::S_4BYTE_LITERALS ||
766 section_type == MachO::S_8BYTE_LITERALS ||
767 section_type == MachO::S_16BYTE_LITERALS)
768 LiteralSections.push_back(Section);
769 }
770
771 // Set the size of the literal pointer.
772 uint32_t lp_size = O->is64Bit() ? 8 : 4;
773
Eric Christopher572e03a2015-06-19 01:53:21 +0000774 // Collect the external relocation symbols for the literal pointers.
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000775 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
776 for (const RelocationRef &Reloc : Section.relocations()) {
777 DataRefImpl Rel;
778 MachO::any_relocation_info RE;
779 bool isExtern = false;
780 Rel = Reloc.getRawDataRefImpl();
781 RE = O->getRelocation(Rel);
782 isExtern = O->getPlainRelocationExternal(RE);
783 if (isExtern) {
Rafael Espindola96d071c2015-06-29 23:29:12 +0000784 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000785 symbol_iterator RelocSym = Reloc.getSymbol();
786 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
787 }
788 }
789 array_pod_sort(Relocs.begin(), Relocs.end());
790
791 // Dump each literal pointer.
792 for (uint32_t i = 0; i < sect_size; i += lp_size) {
793 if (print_addresses) {
794 if (O->is64Bit())
795 outs() << format("%016" PRIx64, sect_addr + i) << " ";
796 else
797 outs() << format("%08" PRIx64, sect_addr + i) << " ";
798 }
799 uint64_t lp;
800 if (O->is64Bit()) {
801 memcpy(&lp, sect + i, sizeof(uint64_t));
802 if (O->isLittleEndian() != sys::IsLittleEndianHost)
803 sys::swapByteOrder(lp);
804 } else {
805 uint32_t li;
806 memcpy(&li, sect + i, sizeof(uint32_t));
807 if (O->isLittleEndian() != sys::IsLittleEndianHost)
808 sys::swapByteOrder(li);
809 lp = li;
810 }
811
812 // First look for an external relocation entry for this literal pointer.
David Blaikie33dd45d02015-03-23 18:39:02 +0000813 auto Reloc = std::find_if(
814 Relocs.begin(), Relocs.end(),
815 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
816 if (Reloc != Relocs.end()) {
817 symbol_iterator RelocSym = Reloc->second;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000818 ErrorOr<StringRef> SymName = RelocSym->getName();
819 if (std::error_code EC = SymName.getError())
820 report_fatal_error(EC.message());
821 outs() << "external relocation entry for symbol:" << *SymName << "\n";
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000822 continue;
David Blaikie33dd45d02015-03-23 18:39:02 +0000823 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000824
825 // For local references see what the section the literal pointer points to.
David Blaikie33dd45d02015-03-23 18:39:02 +0000826 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
827 [&](const SectionRef &R) {
828 return lp >= R.getAddress() &&
829 lp < R.getAddress() + R.getSize();
830 });
831 if (Sect == LiteralSections.end()) {
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000832 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
David Blaikie33dd45d02015-03-23 18:39:02 +0000833 continue;
834 }
835
836 uint64_t SectAddress = Sect->getAddress();
837 uint64_t SectSize = Sect->getSize();
838
839 StringRef SectName;
840 Sect->getName(SectName);
841 DataRefImpl Ref = Sect->getRawDataRefImpl();
842 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
843 outs() << SegmentName << ":" << SectName << ":";
844
845 uint32_t section_type;
846 if (O->is64Bit()) {
847 const MachO::section_64 Sec = O->getSection64(Ref);
848 section_type = Sec.flags & MachO::SECTION_TYPE;
849 } else {
850 const MachO::section Sec = O->getSection(Ref);
851 section_type = Sec.flags & MachO::SECTION_TYPE;
852 }
853
854 StringRef BytesStr;
855 Sect->getContents(BytesStr);
856 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
857
858 switch (section_type) {
859 case MachO::S_CSTRING_LITERALS:
860 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
861 i++) {
862 DumpCstringChar(Contents[i]);
863 }
864 outs() << "\n";
865 break;
866 case MachO::S_4BYTE_LITERALS:
867 float f;
868 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
869 uint32_t l;
870 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
871 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
872 sys::swapByteOrder(f);
873 sys::swapByteOrder(l);
874 }
875 DumpLiteral4(l, f);
876 break;
877 case MachO::S_8BYTE_LITERALS: {
878 double d;
879 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
880 uint32_t l0, l1;
881 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
882 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
883 sizeof(uint32_t));
884 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
885 sys::swapByteOrder(f);
886 sys::swapByteOrder(l0);
887 sys::swapByteOrder(l1);
888 }
889 DumpLiteral8(O, l0, l1, d);
890 break;
891 }
892 case MachO::S_16BYTE_LITERALS: {
893 uint32_t l0, l1, l2, l3;
894 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
895 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
896 sizeof(uint32_t));
897 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
898 sizeof(uint32_t));
899 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
900 sizeof(uint32_t));
901 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
902 sys::swapByteOrder(l0);
903 sys::swapByteOrder(l1);
904 sys::swapByteOrder(l2);
905 sys::swapByteOrder(l3);
906 }
907 DumpLiteral16(l0, l1, l2, l3);
908 break;
909 }
910 }
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000911 }
912}
913
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000914static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
915 uint32_t sect_size, uint64_t sect_addr,
916 SymbolAddressMap *AddrMap,
917 bool verbose) {
918 uint32_t stride;
919 if (O->is64Bit())
920 stride = sizeof(uint64_t);
921 else
922 stride = sizeof(uint32_t);
923 for (uint32_t i = 0; i < sect_size; i += stride) {
924 const char *SymbolName = nullptr;
925 if (O->is64Bit()) {
926 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
927 uint64_t pointer_value;
928 memcpy(&pointer_value, sect + i, stride);
929 if (O->isLittleEndian() != sys::IsLittleEndianHost)
930 sys::swapByteOrder(pointer_value);
931 outs() << format("0x%016" PRIx64, pointer_value);
932 if (verbose)
933 SymbolName = GuessSymbolName(pointer_value, AddrMap);
934 } else {
935 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
936 uint32_t pointer_value;
937 memcpy(&pointer_value, sect + i, stride);
938 if (O->isLittleEndian() != sys::IsLittleEndianHost)
939 sys::swapByteOrder(pointer_value);
940 outs() << format("0x%08" PRIx32, pointer_value);
941 if (verbose)
942 SymbolName = GuessSymbolName(pointer_value, AddrMap);
943 }
944 if (SymbolName)
945 outs() << " " << SymbolName;
946 outs() << "\n";
947 }
948}
949
950static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
951 uint32_t size, uint64_t addr) {
952 uint32_t cputype = O->getHeader().cputype;
953 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
954 uint32_t j;
955 for (uint32_t i = 0; i < size; i += j, addr += j) {
956 if (O->is64Bit())
957 outs() << format("%016" PRIx64, addr) << "\t";
958 else
Kevin Enderbyf0640752015-03-13 17:56:32 +0000959 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000960 for (j = 0; j < 16 && i + j < size; j++) {
961 uint8_t byte_word = *(sect + i + j);
962 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
963 }
964 outs() << "\n";
965 }
966 } else {
967 uint32_t j;
968 for (uint32_t i = 0; i < size; i += j, addr += j) {
969 if (O->is64Bit())
970 outs() << format("%016" PRIx64, addr) << "\t";
971 else
972 outs() << format("%08" PRIx64, sect) << "\t";
973 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
974 j += sizeof(int32_t)) {
975 if (i + j + sizeof(int32_t) < size) {
976 uint32_t long_word;
977 memcpy(&long_word, sect + i + j, sizeof(int32_t));
978 if (O->isLittleEndian() != sys::IsLittleEndianHost)
979 sys::swapByteOrder(long_word);
980 outs() << format("%08" PRIx32, long_word) << " ";
981 } else {
982 for (uint32_t k = 0; i + j + k < size; k++) {
983 uint8_t byte_word = *(sect + i + j);
984 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
985 }
986 }
987 }
988 outs() << "\n";
989 }
990 }
991}
992
Kevin Enderby95df54c2015-02-04 01:01:38 +0000993static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
994 StringRef DisSegName, StringRef DisSectName);
Kevin Enderby4ad9bde2015-04-16 22:33:20 +0000995static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
996 uint32_t size, uint32_t addr);
Kevin Enderby95df54c2015-02-04 01:01:38 +0000997
998static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
999 bool verbose) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001000 SymbolAddressMap AddrMap;
1001 if (verbose)
1002 CreateSymbolAddressMap(O, &AddrMap);
1003
Colin LeMahieufcc32762015-07-29 19:08:10 +00001004 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1005 StringRef DumpSection = FilterSections[i];
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001006 std::pair<StringRef, StringRef> DumpSegSectName;
1007 DumpSegSectName = DumpSection.split(',');
1008 StringRef DumpSegName, DumpSectName;
1009 if (DumpSegSectName.second.size()) {
1010 DumpSegName = DumpSegSectName.first;
1011 DumpSectName = DumpSegSectName.second;
1012 } else {
1013 DumpSegName = "";
1014 DumpSectName = DumpSegSectName.first;
1015 }
1016 for (const SectionRef &Section : O->sections()) {
1017 StringRef SectName;
1018 Section.getName(SectName);
1019 DataRefImpl Ref = Section.getRawDataRefImpl();
1020 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1021 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1022 (SectName == DumpSectName)) {
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001023
Kevin Enderby95df54c2015-02-04 01:01:38 +00001024 uint32_t section_flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001025 if (O->is64Bit()) {
1026 const MachO::section_64 Sec = O->getSection64(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001027 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001028
1029 } else {
1030 const MachO::section Sec = O->getSection(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001031 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001032 }
Kevin Enderby95df54c2015-02-04 01:01:38 +00001033 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001034
1035 StringRef BytesStr;
1036 Section.getContents(BytesStr);
1037 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1038 uint32_t sect_size = BytesStr.size();
1039 uint64_t sect_addr = Section.getAddress();
1040
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001041 outs() << "Contents of (" << SegName << "," << SectName
1042 << ") section\n";
1043
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001044 if (verbose) {
Kevin Enderby95df54c2015-02-04 01:01:38 +00001045 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1046 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1047 DisassembleMachO(Filename, O, SegName, SectName);
1048 continue;
1049 }
Kevin Enderbycd66be52015-03-11 22:06:32 +00001050 if (SegName == "__TEXT" && SectName == "__info_plist") {
1051 outs() << sect;
1052 continue;
1053 }
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001054 if (SegName == "__OBJC" && SectName == "__protocol") {
1055 DumpProtocolSection(O, sect, sect_size, sect_addr);
1056 continue;
1057 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001058 switch (section_type) {
1059 case MachO::S_REGULAR:
1060 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1061 break;
1062 case MachO::S_ZEROFILL:
1063 outs() << "zerofill section and has no contents in the file\n";
1064 break;
Kevin Enderby10ba0412015-02-04 21:38:42 +00001065 case MachO::S_CSTRING_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001066 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby10ba0412015-02-04 21:38:42 +00001067 break;
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001068 case MachO::S_4BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001069 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001070 break;
1071 case MachO::S_8BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001072 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001073 break;
1074 case MachO::S_16BYTE_LITERALS:
Kevin Enderby0fc11822015-04-01 20:57:01 +00001075 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1076 break;
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001077 case MachO::S_LITERAL_POINTERS:
1078 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001079 !NoLeadingAddr);
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001080 break;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001081 case MachO::S_MOD_INIT_FUNC_POINTERS:
1082 case MachO::S_MOD_TERM_FUNC_POINTERS:
1083 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1084 verbose);
1085 break;
1086 default:
1087 outs() << "Unknown section type ("
1088 << format("0x%08" PRIx32, section_type) << ")\n";
1089 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1090 break;
1091 }
1092 } else {
1093 if (section_type == MachO::S_ZEROFILL)
1094 outs() << "zerofill section and has no contents in the file\n";
1095 else
1096 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1097 }
1098 }
1099 }
1100 }
1101}
1102
Kevin Enderbycd66be52015-03-11 22:06:32 +00001103static void DumpInfoPlistSectionContents(StringRef Filename,
1104 MachOObjectFile *O) {
1105 for (const SectionRef &Section : O->sections()) {
1106 StringRef SectName;
1107 Section.getName(SectName);
1108 DataRefImpl Ref = Section.getRawDataRefImpl();
1109 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1110 if (SegName == "__TEXT" && SectName == "__info_plist") {
1111 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1112 StringRef BytesStr;
1113 Section.getContents(BytesStr);
1114 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1115 outs() << sect;
1116 return;
1117 }
1118 }
1119}
1120
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001121// checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1122// and if it is and there is a list of architecture flags is specified then
1123// check to make sure this Mach-O file is one of those architectures or all
1124// architectures were specified. If not then an error is generated and this
1125// routine returns false. Else it returns true.
1126static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1127 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1128 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1129 bool ArchFound = false;
1130 MachO::mach_header H;
1131 MachO::mach_header_64 H_64;
1132 Triple T;
1133 if (MachO->is64Bit()) {
1134 H_64 = MachO->MachOObjectFile::getHeader64();
1135 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1136 } else {
1137 H = MachO->MachOObjectFile::getHeader();
1138 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1139 }
1140 unsigned i;
1141 for (i = 0; i < ArchFlags.size(); ++i) {
1142 if (ArchFlags[i] == T.getArchName())
1143 ArchFound = true;
1144 break;
1145 }
1146 if (!ArchFound) {
1147 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1148 << "architecture: " + ArchFlags[i] + "\n";
1149 return false;
1150 }
1151 }
1152 return true;
1153}
1154
Kevin Enderby0fc11822015-04-01 20:57:01 +00001155static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1156
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001157// ProcessMachO() is passed a single opened Mach-O file, which may be an
1158// archive member and or in a slice of a universal file. It prints the
1159// the file name and header info and then processes it according to the
1160// command line options.
1161static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1162 StringRef ArchiveMemberName = StringRef(),
1163 StringRef ArchitectureName = StringRef()) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001164 // If we are doing some processing here on the Mach-O file print the header
1165 // info. And don't print it otherwise like in the case of printing the
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001166 // UniversalHeaders or ArchiveHeaders.
Kevin Enderby131d1772015-01-09 19:22:37 +00001167 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001168 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
Colin LeMahieufcc32762015-07-29 19:08:10 +00001169 DylibsUsed || DylibId || ObjcMetaData || (FilterSections.size() != 0)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001170 outs() << Filename;
1171 if (!ArchiveMemberName.empty())
1172 outs() << '(' << ArchiveMemberName << ')';
1173 if (!ArchitectureName.empty())
1174 outs() << " (architecture " << ArchitectureName << ")";
1175 outs() << ":\n";
1176 }
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001177
1178 if (Disassemble)
Kevin Enderby95df54c2015-02-04 01:01:38 +00001179 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001180 if (IndirectSymbols)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001181 PrintIndirectSymbols(MachOOF, !NonVerbose);
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001182 if (DataInCode)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001183 PrintDataInCodeTable(MachOOF, !NonVerbose);
Kevin Enderby9a509442015-01-27 21:28:24 +00001184 if (LinkOptHints)
1185 PrintLinkOptHints(MachOOF);
Kevin Enderby98da6132015-01-20 21:47:46 +00001186 if (Relocations)
1187 PrintRelocations(MachOOF);
1188 if (SectionHeaders)
1189 PrintSectionHeaders(MachOOF);
1190 if (SectionContents)
1191 PrintSectionContents(MachOOF);
Colin LeMahieufcc32762015-07-29 19:08:10 +00001192 if (FilterSections.size() != 0)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001193 DumpSectionContents(Filename, MachOOF, !NonVerbose);
Kevin Enderbycd66be52015-03-11 22:06:32 +00001194 if (InfoPlist)
1195 DumpInfoPlistSectionContents(Filename, MachOOF);
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001196 if (DylibsUsed)
1197 PrintDylibs(MachOOF, false);
1198 if (DylibId)
1199 PrintDylibs(MachOOF, true);
Kevin Enderby98da6132015-01-20 21:47:46 +00001200 if (SymbolTable)
1201 PrintSymbolTable(MachOOF);
1202 if (UnwindInfo)
1203 printMachOUnwindInfo(MachOOF);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001204 if (PrivateHeaders)
1205 printMachOFileHeader(MachOOF);
Kevin Enderby0fc11822015-04-01 20:57:01 +00001206 if (ObjcMetaData)
1207 printObjcMetaData(MachOOF, !NonVerbose);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001208 if (ExportsTrie)
1209 printExportsTrie(MachOOF);
1210 if (Rebase)
1211 printRebaseTable(MachOOF);
1212 if (Bind)
1213 printBindTable(MachOOF);
1214 if (LazyBind)
1215 printLazyBindTable(MachOOF);
1216 if (WeakBind)
1217 printWeakBindTable(MachOOF);
1218}
1219
Kevin Enderby131d1772015-01-09 19:22:37 +00001220// printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1221static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1222 outs() << " cputype (" << cputype << ")\n";
1223 outs() << " cpusubtype (" << cpusubtype << ")\n";
1224}
1225
1226// printCPUType() helps print_fat_headers by printing the cputype and
1227// pusubtype (symbolically for the one's it knows about).
1228static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1229 switch (cputype) {
1230 case MachO::CPU_TYPE_I386:
1231 switch (cpusubtype) {
1232 case MachO::CPU_SUBTYPE_I386_ALL:
1233 outs() << " cputype CPU_TYPE_I386\n";
1234 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1235 break;
1236 default:
1237 printUnknownCPUType(cputype, cpusubtype);
1238 break;
1239 }
1240 break;
1241 case MachO::CPU_TYPE_X86_64:
1242 switch (cpusubtype) {
1243 case MachO::CPU_SUBTYPE_X86_64_ALL:
1244 outs() << " cputype CPU_TYPE_X86_64\n";
1245 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1246 break;
1247 case MachO::CPU_SUBTYPE_X86_64_H:
1248 outs() << " cputype CPU_TYPE_X86_64\n";
1249 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1250 break;
1251 default:
1252 printUnknownCPUType(cputype, cpusubtype);
1253 break;
1254 }
1255 break;
1256 case MachO::CPU_TYPE_ARM:
1257 switch (cpusubtype) {
1258 case MachO::CPU_SUBTYPE_ARM_ALL:
1259 outs() << " cputype CPU_TYPE_ARM\n";
1260 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1261 break;
1262 case MachO::CPU_SUBTYPE_ARM_V4T:
1263 outs() << " cputype CPU_TYPE_ARM\n";
1264 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1265 break;
1266 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1267 outs() << " cputype CPU_TYPE_ARM\n";
1268 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1269 break;
1270 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1271 outs() << " cputype CPU_TYPE_ARM\n";
1272 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1273 break;
1274 case MachO::CPU_SUBTYPE_ARM_V6:
1275 outs() << " cputype CPU_TYPE_ARM\n";
1276 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1277 break;
1278 case MachO::CPU_SUBTYPE_ARM_V6M:
1279 outs() << " cputype CPU_TYPE_ARM\n";
1280 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1281 break;
1282 case MachO::CPU_SUBTYPE_ARM_V7:
1283 outs() << " cputype CPU_TYPE_ARM\n";
1284 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1285 break;
1286 case MachO::CPU_SUBTYPE_ARM_V7EM:
1287 outs() << " cputype CPU_TYPE_ARM\n";
1288 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1289 break;
1290 case MachO::CPU_SUBTYPE_ARM_V7K:
1291 outs() << " cputype CPU_TYPE_ARM\n";
1292 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1293 break;
1294 case MachO::CPU_SUBTYPE_ARM_V7M:
1295 outs() << " cputype CPU_TYPE_ARM\n";
1296 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1297 break;
1298 case MachO::CPU_SUBTYPE_ARM_V7S:
1299 outs() << " cputype CPU_TYPE_ARM\n";
1300 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1301 break;
1302 default:
1303 printUnknownCPUType(cputype, cpusubtype);
1304 break;
1305 }
1306 break;
1307 case MachO::CPU_TYPE_ARM64:
1308 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1309 case MachO::CPU_SUBTYPE_ARM64_ALL:
1310 outs() << " cputype CPU_TYPE_ARM64\n";
1311 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1312 break;
1313 default:
1314 printUnknownCPUType(cputype, cpusubtype);
1315 break;
1316 }
1317 break;
1318 default:
1319 printUnknownCPUType(cputype, cpusubtype);
1320 break;
1321 }
1322}
1323
1324static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1325 bool verbose) {
1326 outs() << "Fat headers\n";
1327 if (verbose)
1328 outs() << "fat_magic FAT_MAGIC\n";
1329 else
1330 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1331
1332 uint32_t nfat_arch = UB->getNumberOfObjects();
1333 StringRef Buf = UB->getData();
1334 uint64_t size = Buf.size();
1335 uint64_t big_size = sizeof(struct MachO::fat_header) +
1336 nfat_arch * sizeof(struct MachO::fat_arch);
1337 outs() << "nfat_arch " << UB->getNumberOfObjects();
1338 if (nfat_arch == 0)
1339 outs() << " (malformed, contains zero architecture types)\n";
1340 else if (big_size > size)
1341 outs() << " (malformed, architectures past end of file)\n";
1342 else
1343 outs() << "\n";
1344
1345 for (uint32_t i = 0; i < nfat_arch; ++i) {
1346 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1347 uint32_t cputype = OFA.getCPUType();
1348 uint32_t cpusubtype = OFA.getCPUSubType();
1349 outs() << "architecture ";
1350 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1351 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1352 uint32_t other_cputype = other_OFA.getCPUType();
1353 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
Kevin Enderby0512bd72015-01-09 21:55:03 +00001354 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
Kevin Enderby131d1772015-01-09 19:22:37 +00001355 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
Kevin Enderby0512bd72015-01-09 21:55:03 +00001356 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001357 outs() << "(illegal duplicate architecture) ";
1358 break;
Kevin Enderby0512bd72015-01-09 21:55:03 +00001359 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001360 }
1361 if (verbose) {
1362 outs() << OFA.getArchTypeName() << "\n";
1363 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1364 } else {
1365 outs() << i << "\n";
1366 outs() << " cputype " << cputype << "\n";
1367 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1368 << "\n";
1369 }
1370 if (verbose &&
1371 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1372 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1373 else
1374 outs() << " capabilities "
1375 << format("0x%" PRIx32,
1376 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1377 outs() << " offset " << OFA.getOffset();
1378 if (OFA.getOffset() > size)
1379 outs() << " (past end of file)";
1380 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1381 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1382 outs() << "\n";
1383 outs() << " size " << OFA.getSize();
1384 big_size = OFA.getOffset() + OFA.getSize();
1385 if (big_size > size)
1386 outs() << " (past end of file)";
1387 outs() << "\n";
1388 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1389 << ")\n";
1390 }
1391}
1392
Kevin Enderby13023a12015-01-15 23:19:11 +00001393static void printArchiveChild(Archive::Child &C, bool verbose,
1394 bool print_offset) {
1395 if (print_offset)
1396 outs() << C.getChildOffset() << "\t";
1397 sys::fs::perms Mode = C.getAccessMode();
1398 if (verbose) {
1399 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1400 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1401 outs() << "-";
1402 if (Mode & sys::fs::owner_read)
1403 outs() << "r";
1404 else
1405 outs() << "-";
1406 if (Mode & sys::fs::owner_write)
1407 outs() << "w";
1408 else
1409 outs() << "-";
1410 if (Mode & sys::fs::owner_exe)
1411 outs() << "x";
1412 else
1413 outs() << "-";
1414 if (Mode & sys::fs::group_read)
1415 outs() << "r";
1416 else
1417 outs() << "-";
1418 if (Mode & sys::fs::group_write)
1419 outs() << "w";
1420 else
1421 outs() << "-";
1422 if (Mode & sys::fs::group_exe)
1423 outs() << "x";
1424 else
1425 outs() << "-";
1426 if (Mode & sys::fs::others_read)
1427 outs() << "r";
1428 else
1429 outs() << "-";
1430 if (Mode & sys::fs::others_write)
1431 outs() << "w";
1432 else
1433 outs() << "-";
1434 if (Mode & sys::fs::others_exe)
1435 outs() << "x";
1436 else
1437 outs() << "-";
1438 } else {
1439 outs() << format("0%o ", Mode);
1440 }
1441
1442 unsigned UID = C.getUID();
1443 outs() << format("%3d/", UID);
1444 unsigned GID = C.getGID();
1445 outs() << format("%-3d ", GID);
Kevin Enderbyc12718932015-01-16 22:10:36 +00001446 uint64_t Size = C.getRawSize();
Kevin Enderby479ee612015-01-23 21:02:44 +00001447 outs() << format("%5" PRId64, Size) << " ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001448
1449 StringRef RawLastModified = C.getRawLastModified();
1450 if (verbose) {
1451 unsigned Seconds;
1452 if (RawLastModified.getAsInteger(10, Seconds))
1453 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1454 else {
1455 // Since cime(3) returns a 26 character string of the form:
1456 // "Sun Sep 16 01:03:52 1973\n\0"
1457 // just print 24 characters.
1458 time_t t = Seconds;
1459 outs() << format("%.24s ", ctime(&t));
1460 }
1461 } else {
1462 outs() << RawLastModified << " ";
1463 }
1464
1465 if (verbose) {
1466 ErrorOr<StringRef> NameOrErr = C.getName();
1467 if (NameOrErr.getError()) {
1468 StringRef RawName = C.getRawName();
1469 outs() << RawName << "\n";
1470 } else {
1471 StringRef Name = NameOrErr.get();
1472 outs() << Name << "\n";
1473 }
1474 } else {
1475 StringRef RawName = C.getRawName();
1476 outs() << RawName << "\n";
1477 }
1478}
1479
1480static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
1481 if (A->hasSymbolTable()) {
1482 Archive::child_iterator S = A->getSymbolTableChild();
1483 Archive::Child C = *S;
1484 printArchiveChild(C, verbose, print_offset);
1485 }
1486 for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E;
1487 ++I) {
1488 Archive::Child C = *I;
1489 printArchiveChild(C, verbose, print_offset);
1490 }
1491}
1492
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001493// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1494// -arch flags selecting just those slices as specified by them and also parses
1495// archive files. Then for each individual Mach-O file ProcessMachO() is
1496// called to process the file based on the command line options.
1497void llvm::ParseInputMachO(StringRef Filename) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001498 // Check for -arch all and verifiy the -arch flags are valid.
1499 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1500 if (ArchFlags[i] == "all") {
1501 ArchAll = true;
1502 } else {
1503 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1504 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1505 "'for the -arch option\n";
1506 return;
1507 }
1508 }
1509 }
1510
1511 // Attempt to open the binary.
1512 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1513 if (std::error_code EC = BinaryOrErr.getError()) {
1514 errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n";
Rafael Espindolade882cd2014-12-03 23:29:34 +00001515 return;
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001516 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001517 Binary &Bin = *BinaryOrErr.get().getBinary();
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001518
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001519 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1520 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001521 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001522 printArchiveHeaders(A, !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001523 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1524 I != E; ++I) {
1525 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary();
1526 if (ChildOrErr.getError())
1527 continue;
1528 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1529 if (!checkMachOAndArchFlags(O, Filename))
1530 return;
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001531 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001532 }
1533 }
1534 return;
1535 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001536 if (UniversalHeaders) {
1537 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
Kevin Enderbyf0640752015-03-13 17:56:32 +00001538 printMachOUniversalHeaders(UB, !NonVerbose);
Kevin Enderby131d1772015-01-09 19:22:37 +00001539 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001540 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1541 // If we have a list of architecture flags specified dump only those.
1542 if (!ArchAll && ArchFlags.size() != 0) {
1543 // Look for a slice in the universal binary that matches each ArchFlag.
1544 bool ArchFound;
1545 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1546 ArchFound = false;
1547 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1548 E = UB->end_objects();
1549 I != E; ++I) {
1550 if (ArchFlags[i] == I->getArchTypeName()) {
1551 ArchFound = true;
1552 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1553 I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001554 std::string ArchitectureName = "";
1555 if (ArchFlags.size() > 1)
1556 ArchitectureName = I->getArchTypeName();
1557 if (ObjOrErr) {
1558 ObjectFile &O = *ObjOrErr.get();
1559 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001560 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001561 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1562 I->getAsArchive()) {
1563 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001564 outs() << "Archive : " << Filename;
1565 if (!ArchitectureName.empty())
1566 outs() << " (architecture " << ArchitectureName << ")";
1567 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001568 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001569 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001570 for (Archive::child_iterator AI = A->child_begin(),
1571 AE = A->child_end();
1572 AI != AE; ++AI) {
1573 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1574 if (ChildOrErr.getError())
1575 continue;
1576 if (MachOObjectFile *O =
1577 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001578 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001579 }
1580 }
1581 }
1582 }
1583 if (!ArchFound) {
1584 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1585 << "architecture: " + ArchFlags[i] + "\n";
1586 return;
1587 }
1588 }
1589 return;
1590 }
1591 // No architecture flags were specified so if this contains a slice that
1592 // matches the host architecture dump only that.
1593 if (!ArchAll) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001594 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1595 E = UB->end_objects();
1596 I != E; ++I) {
Kevin Enderby0512bd72015-01-09 21:55:03 +00001597 if (MachOObjectFile::getHostArch().getArchName() ==
1598 I->getArchTypeName()) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001599 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001600 std::string ArchiveName;
1601 ArchiveName.clear();
1602 if (ObjOrErr) {
1603 ObjectFile &O = *ObjOrErr.get();
1604 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001605 ProcessMachO(Filename, MachOOF);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001606 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1607 I->getAsArchive()) {
1608 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001609 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001610 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001611 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001612 for (Archive::child_iterator AI = A->child_begin(),
1613 AE = A->child_end();
1614 AI != AE; ++AI) {
1615 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1616 if (ChildOrErr.getError())
1617 continue;
1618 if (MachOObjectFile *O =
1619 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001620 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001621 }
1622 }
1623 return;
1624 }
1625 }
1626 }
1627 // Either all architectures have been specified or none have been specified
1628 // and this does not contain the host architecture so dump all the slices.
1629 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1630 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1631 E = UB->end_objects();
1632 I != E; ++I) {
1633 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001634 std::string ArchitectureName = "";
1635 if (moreThanOneArch)
1636 ArchitectureName = I->getArchTypeName();
1637 if (ObjOrErr) {
1638 ObjectFile &Obj = *ObjOrErr.get();
1639 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001640 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001641 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1642 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001643 outs() << "Archive : " << Filename;
1644 if (!ArchitectureName.empty())
1645 outs() << " (architecture " << ArchitectureName << ")";
1646 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001647 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001648 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001649 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1650 AI != AE; ++AI) {
1651 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1652 if (ChildOrErr.getError())
1653 continue;
1654 if (MachOObjectFile *O =
1655 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1656 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001657 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1658 ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001659 }
1660 }
1661 }
1662 }
1663 return;
1664 }
1665 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1666 if (!checkMachOAndArchFlags(O, Filename))
1667 return;
1668 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001669 ProcessMachO(Filename, MachOOF);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001670 } else
1671 errs() << "llvm-objdump: '" << Filename << "': "
1672 << "Object is not a Mach-O file type.\n";
1673 } else
1674 errs() << "llvm-objdump: '" << Filename << "': "
1675 << "Unrecognized file type.\n";
Rafael Espindola9b709252013-04-13 01:45:40 +00001676}
1677
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001678typedef std::pair<uint64_t, const char *> BindInfoEntry;
1679typedef std::vector<BindInfoEntry> BindTable;
1680typedef BindTable::iterator bind_table_iterator;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001681
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001682// The block of info used by the Symbolizer call backs.
1683struct DisassembleInfo {
1684 bool verbose;
1685 MachOObjectFile *O;
1686 SectionRef S;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001687 SymbolAddressMap *AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001688 std::vector<SectionRef> *Sections;
1689 const char *class_name;
1690 const char *selector_name;
1691 char *method;
Kevin Enderby04bf6932014-10-28 23:39:46 +00001692 char *demangled_name;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001693 uint64_t adrp_addr;
1694 uint32_t adrp_inst;
Kevin Enderby078be602014-10-23 19:53:12 +00001695 BindTable *bindtable;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001696};
1697
1698// SymbolizerGetOpInfo() is the operand information call back function.
1699// This is called to get the symbolic information for operand(s) of an
1700// instruction when it is being done. This routine does this from
1701// the relocation information, symbol table, etc. That block of information
1702// is a pointer to the struct DisassembleInfo that was passed when the
1703// disassembler context was created and passed to back to here when
1704// called back by the disassembler for instruction operands that could have
1705// relocation information. The address of the instruction containing operand is
1706// at the Pc parameter. The immediate value the operand has is passed in
1707// op_info->Value and is at Offset past the start of the instruction and has a
1708// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1709// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1710// names and addends of the symbolic expression to add for the operand. The
1711// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1712// information is returned then this function returns 1 else it returns 0.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00001713static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1714 uint64_t Size, int TagType, void *TagBuf) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001715 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1716 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001717 uint64_t value = op_info->Value;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001718
1719 // Make sure all fields returned are zero if we don't set them.
1720 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1721 op_info->Value = value;
1722
1723 // If the TagType is not the value 1 which it code knows about or if no
1724 // verbose symbolic information is wanted then just return 0, indicating no
1725 // information is being returned.
David Blaikie33dd45d02015-03-23 18:39:02 +00001726 if (TagType != 1 || !info->verbose)
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001727 return 0;
1728
1729 unsigned int Arch = info->O->getArch();
1730 if (Arch == Triple::x86) {
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001731 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1732 return 0;
1733 // First search the section's relocation entries (if any) for an entry
1734 // for this section offset.
1735 uint32_t sect_addr = info->S.getAddress();
1736 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1737 bool reloc_found = false;
1738 DataRefImpl Rel;
1739 MachO::any_relocation_info RE;
1740 bool isExtern = false;
1741 SymbolRef Symbol;
1742 bool r_scattered = false;
1743 uint32_t r_value, pair_r_value, r_type;
1744 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001745 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001746 if (RelocOffset == sect_offset) {
1747 Rel = Reloc.getRawDataRefImpl();
1748 RE = info->O->getRelocation(Rel);
Kevin Enderby3eb73e12014-11-11 19:16:45 +00001749 r_type = info->O->getAnyRelocationType(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001750 r_scattered = info->O->isRelocationScattered(RE);
1751 if (r_scattered) {
1752 r_value = info->O->getScatteredRelocationValue(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001753 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1754 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1755 DataRefImpl RelNext = Rel;
1756 info->O->moveRelocationNext(RelNext);
1757 MachO::any_relocation_info RENext;
1758 RENext = info->O->getRelocation(RelNext);
1759 if (info->O->isRelocationScattered(RENext))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001760 pair_r_value = info->O->getScatteredRelocationValue(RENext);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001761 else
1762 return 0;
1763 }
1764 } else {
1765 isExtern = info->O->getPlainRelocationExternal(RE);
1766 if (isExtern) {
1767 symbol_iterator RelocSym = Reloc.getSymbol();
1768 Symbol = *RelocSym;
1769 }
1770 }
1771 reloc_found = true;
1772 break;
1773 }
1774 }
1775 if (reloc_found && isExtern) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001776 ErrorOr<StringRef> SymName = Symbol.getName();
1777 if (std::error_code EC = SymName.getError())
1778 report_fatal_error(EC.message());
1779 const char *name = SymName->data();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001780 op_info->AddSymbol.Present = 1;
1781 op_info->AddSymbol.Name = name;
1782 // For i386 extern relocation entries the value in the instruction is
1783 // the offset from the symbol, and value is already set in op_info->Value.
1784 return 1;
1785 }
1786 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1787 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001788 const char *add = GuessSymbolName(r_value, info->AddrMap);
1789 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001790 uint32_t offset = value - (r_value - pair_r_value);
1791 op_info->AddSymbol.Present = 1;
1792 if (add != nullptr)
1793 op_info->AddSymbol.Name = add;
1794 else
1795 op_info->AddSymbol.Value = r_value;
1796 op_info->SubtractSymbol.Present = 1;
1797 if (sub != nullptr)
1798 op_info->SubtractSymbol.Name = sub;
1799 else
1800 op_info->SubtractSymbol.Value = pair_r_value;
1801 op_info->Value = offset;
1802 return 1;
1803 }
1804 // TODO:
1805 // Second search the external relocation entries of a fully linked image
1806 // (if any) for an entry that matches this segment offset.
1807 // uint32_t seg_offset = (Pc + Offset);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001808 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001809 }
1810 if (Arch == Triple::x86_64) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001811 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1812 return 0;
1813 // First search the section's relocation entries (if any) for an entry
1814 // for this section offset.
Rafael Espindola80291272014-10-08 15:28:58 +00001815 uint64_t sect_addr = info->S.getAddress();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001816 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1817 bool reloc_found = false;
1818 DataRefImpl Rel;
1819 MachO::any_relocation_info RE;
1820 bool isExtern = false;
1821 SymbolRef Symbol;
1822 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001823 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001824 if (RelocOffset == sect_offset) {
1825 Rel = Reloc.getRawDataRefImpl();
1826 RE = info->O->getRelocation(Rel);
1827 // NOTE: Scattered relocations don't exist on x86_64.
1828 isExtern = info->O->getPlainRelocationExternal(RE);
1829 if (isExtern) {
1830 symbol_iterator RelocSym = Reloc.getSymbol();
1831 Symbol = *RelocSym;
1832 }
1833 reloc_found = true;
1834 break;
1835 }
1836 }
1837 if (reloc_found && isExtern) {
1838 // The Value passed in will be adjusted by the Pc if the instruction
1839 // adds the Pc. But for x86_64 external relocation entries the Value
1840 // is the offset from the external symbol.
1841 if (info->O->getAnyRelocationPCRel(RE))
1842 op_info->Value -= Pc + Offset + Size;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001843 ErrorOr<StringRef> SymName = Symbol.getName();
1844 if (std::error_code EC = SymName.getError())
1845 report_fatal_error(EC.message());
1846 const char *name = SymName->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001847 unsigned Type = info->O->getAnyRelocationType(RE);
1848 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1849 DataRefImpl RelNext = Rel;
1850 info->O->moveRelocationNext(RelNext);
1851 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1852 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1853 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1854 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1855 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1856 op_info->SubtractSymbol.Present = 1;
1857 op_info->SubtractSymbol.Name = name;
1858 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1859 Symbol = *RelocSymNext;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001860 ErrorOr<StringRef> SymNameNext = Symbol.getName();
1861 if (std::error_code EC = SymNameNext.getError())
1862 report_fatal_error(EC.message());
1863 name = SymNameNext->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001864 }
1865 }
1866 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1867 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1868 op_info->AddSymbol.Present = 1;
1869 op_info->AddSymbol.Name = name;
1870 return 1;
1871 }
1872 // TODO:
1873 // Second search the external relocation entries of a fully linked image
1874 // (if any) for an entry that matches this segment offset.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001875 // uint64_t seg_offset = (Pc + Offset);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001876 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001877 }
1878 if (Arch == Triple::arm) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001879 if (Offset != 0 || (Size != 4 && Size != 2))
1880 return 0;
1881 // First search the section's relocation entries (if any) for an entry
1882 // for this section offset.
1883 uint32_t sect_addr = info->S.getAddress();
1884 uint32_t sect_offset = (Pc + Offset) - sect_addr;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001885 DataRefImpl Rel;
1886 MachO::any_relocation_info RE;
1887 bool isExtern = false;
1888 SymbolRef Symbol;
1889 bool r_scattered = false;
1890 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
David Blaikie33dd45d02015-03-23 18:39:02 +00001891 auto Reloc =
1892 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1893 [&](const RelocationRef &Reloc) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001894 uint64_t RelocOffset = Reloc.getOffset();
David Blaikie33dd45d02015-03-23 18:39:02 +00001895 return RelocOffset == sect_offset;
1896 });
1897
1898 if (Reloc == info->S.relocations().end())
1899 return 0;
1900
1901 Rel = Reloc->getRawDataRefImpl();
1902 RE = info->O->getRelocation(Rel);
1903 r_length = info->O->getAnyRelocationLength(RE);
1904 r_scattered = info->O->isRelocationScattered(RE);
1905 if (r_scattered) {
1906 r_value = info->O->getScatteredRelocationValue(RE);
1907 r_type = info->O->getScatteredRelocationType(RE);
1908 } else {
1909 r_type = info->O->getAnyRelocationType(RE);
1910 isExtern = info->O->getPlainRelocationExternal(RE);
1911 if (isExtern) {
1912 symbol_iterator RelocSym = Reloc->getSymbol();
1913 Symbol = *RelocSym;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001914 }
1915 }
David Blaikie33dd45d02015-03-23 18:39:02 +00001916 if (r_type == MachO::ARM_RELOC_HALF ||
1917 r_type == MachO::ARM_RELOC_SECTDIFF ||
1918 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
1919 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1920 DataRefImpl RelNext = Rel;
1921 info->O->moveRelocationNext(RelNext);
1922 MachO::any_relocation_info RENext;
1923 RENext = info->O->getRelocation(RelNext);
1924 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
1925 if (info->O->isRelocationScattered(RENext))
1926 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1927 }
1928
1929 if (isExtern) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001930 ErrorOr<StringRef> SymName = Symbol.getName();
1931 if (std::error_code EC = SymName.getError())
1932 report_fatal_error(EC.message());
1933 const char *name = SymName->data();
Kevin Enderby930fdc72014-11-06 19:00:13 +00001934 op_info->AddSymbol.Present = 1;
1935 op_info->AddSymbol.Name = name;
Sylvestre Ledru648cced2015-02-05 17:00:23 +00001936 switch (r_type) {
1937 case MachO::ARM_RELOC_HALF:
1938 if ((r_length & 0x1) == 1) {
1939 op_info->Value = value << 16 | other_half;
1940 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1941 } else {
1942 op_info->Value = other_half << 16 | value;
1943 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Sylvestre Ledrufe0c7ad2015-02-05 16:35:44 +00001944 }
Sylvestre Ledru648cced2015-02-05 17:00:23 +00001945 break;
1946 default:
1947 break;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001948 }
1949 return 1;
1950 }
1951 // If we have a branch that is not an external relocation entry then
1952 // return 0 so the code in tryAddingSymbolicOperand() can use the
1953 // SymbolLookUp call back with the branch target address to look up the
1954 // symbol and possiblity add an annotation for a symbol stub.
David Blaikie33dd45d02015-03-23 18:39:02 +00001955 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
1956 r_type == MachO::ARM_THUMB_RELOC_BR22))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001957 return 0;
1958
1959 uint32_t offset = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001960 if (r_type == MachO::ARM_RELOC_HALF ||
1961 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1962 if ((r_length & 0x1) == 1)
1963 value = value << 16 | other_half;
1964 else
1965 value = other_half << 16 | value;
1966 }
1967 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
1968 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
1969 offset = value - r_value;
1970 value = r_value;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001971 }
1972
David Blaikie33dd45d02015-03-23 18:39:02 +00001973 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001974 if ((r_length & 0x1) == 1)
1975 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1976 else
1977 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001978 const char *add = GuessSymbolName(r_value, info->AddrMap);
1979 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00001980 int32_t offset = value - (r_value - pair_r_value);
1981 op_info->AddSymbol.Present = 1;
1982 if (add != nullptr)
1983 op_info->AddSymbol.Name = add;
1984 else
1985 op_info->AddSymbol.Value = r_value;
1986 op_info->SubtractSymbol.Present = 1;
1987 if (sub != nullptr)
1988 op_info->SubtractSymbol.Name = sub;
1989 else
1990 op_info->SubtractSymbol.Value = pair_r_value;
1991 op_info->Value = offset;
1992 return 1;
1993 }
1994
Kevin Enderby930fdc72014-11-06 19:00:13 +00001995 op_info->AddSymbol.Present = 1;
1996 op_info->Value = offset;
David Blaikie33dd45d02015-03-23 18:39:02 +00001997 if (r_type == MachO::ARM_RELOC_HALF) {
1998 if ((r_length & 0x1) == 1)
1999 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2000 else
2001 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002002 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002003 const char *add = GuessSymbolName(value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002004 if (add != nullptr) {
2005 op_info->AddSymbol.Name = add;
2006 return 1;
2007 }
2008 op_info->AddSymbol.Value = value;
2009 return 1;
David Blaikie33dd45d02015-03-23 18:39:02 +00002010 }
2011 if (Arch == Triple::aarch64) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002012 if (Offset != 0 || Size != 4)
2013 return 0;
2014 // First search the section's relocation entries (if any) for an entry
2015 // for this section offset.
2016 uint64_t sect_addr = info->S.getAddress();
2017 uint64_t sect_offset = (Pc + Offset) - sect_addr;
David Blaikie33dd45d02015-03-23 18:39:02 +00002018 auto Reloc =
2019 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2020 [&](const RelocationRef &Reloc) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002021 uint64_t RelocOffset = Reloc.getOffset();
David Blaikie33dd45d02015-03-23 18:39:02 +00002022 return RelocOffset == sect_offset;
2023 });
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002024
David Blaikie33dd45d02015-03-23 18:39:02 +00002025 if (Reloc == info->S.relocations().end())
2026 return 0;
2027
2028 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2029 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2030 uint32_t r_type = info->O->getAnyRelocationType(RE);
2031 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2032 DataRefImpl RelNext = Rel;
2033 info->O->moveRelocationNext(RelNext);
2034 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2035 if (value == 0) {
2036 value = info->O->getPlainRelocationSymbolNum(RENext);
2037 op_info->Value = value;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002038 }
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002039 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002040 // NOTE: Scattered relocations don't exist on arm64.
2041 if (!info->O->getPlainRelocationExternal(RE))
2042 return 0;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002043 ErrorOr<StringRef> SymName = Reloc->getSymbol()->getName();
2044 if (std::error_code EC = SymName.getError())
2045 report_fatal_error(EC.message());
2046 const char *name = SymName->data();
David Blaikie33dd45d02015-03-23 18:39:02 +00002047 op_info->AddSymbol.Present = 1;
2048 op_info->AddSymbol.Name = name;
2049
2050 switch (r_type) {
2051 case MachO::ARM64_RELOC_PAGE21:
2052 /* @page */
2053 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2054 break;
2055 case MachO::ARM64_RELOC_PAGEOFF12:
2056 /* @pageoff */
2057 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2058 break;
2059 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2060 /* @gotpage */
2061 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2062 break;
2063 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2064 /* @gotpageoff */
2065 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2066 break;
2067 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2068 /* @tvlppage is not implemented in llvm-mc */
2069 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2070 break;
2071 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2072 /* @tvlppageoff is not implemented in llvm-mc */
2073 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2074 break;
2075 default:
2076 case MachO::ARM64_RELOC_BRANCH26:
2077 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2078 break;
2079 }
2080 return 1;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002081 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002082 return 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002083}
2084
Kevin Enderbybf246f52014-09-24 23:08:22 +00002085// GuessCstringPointer is passed the address of what might be a pointer to a
2086// literal string in a cstring section. If that address is in a cstring section
2087// it returns a pointer to that string. Else it returns nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002088static const char *GuessCstringPointer(uint64_t ReferenceValue,
2089 struct DisassembleInfo *info) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002090 for (const auto &Load : info->O->load_commands()) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00002091 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2092 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2093 for (unsigned J = 0; J < Seg.nsects; ++J) {
2094 MachO::section_64 Sec = info->O->getSection64(Load, J);
2095 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2096 if (section_type == MachO::S_CSTRING_LITERALS &&
2097 ReferenceValue >= Sec.addr &&
2098 ReferenceValue < Sec.addr + Sec.size) {
2099 uint64_t sect_offset = ReferenceValue - Sec.addr;
2100 uint64_t object_offset = Sec.offset + sect_offset;
2101 StringRef MachOContents = info->O->getData();
2102 uint64_t object_size = MachOContents.size();
2103 const char *object_addr = (const char *)MachOContents.data();
2104 if (object_offset < object_size) {
2105 const char *name = object_addr + object_offset;
2106 return name;
2107 } else {
2108 return nullptr;
2109 }
2110 }
2111 }
2112 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2113 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2114 for (unsigned J = 0; J < Seg.nsects; ++J) {
2115 MachO::section Sec = info->O->getSection(Load, J);
2116 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2117 if (section_type == MachO::S_CSTRING_LITERALS &&
2118 ReferenceValue >= Sec.addr &&
2119 ReferenceValue < Sec.addr + Sec.size) {
2120 uint64_t sect_offset = ReferenceValue - Sec.addr;
2121 uint64_t object_offset = Sec.offset + sect_offset;
2122 StringRef MachOContents = info->O->getData();
2123 uint64_t object_size = MachOContents.size();
2124 const char *object_addr = (const char *)MachOContents.data();
2125 if (object_offset < object_size) {
2126 const char *name = object_addr + object_offset;
2127 return name;
2128 } else {
2129 return nullptr;
2130 }
2131 }
2132 }
2133 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00002134 }
2135 return nullptr;
2136}
2137
Kevin Enderby85974882014-09-26 22:20:44 +00002138// GuessIndirectSymbol returns the name of the indirect symbol for the
2139// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2140// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2141// symbol name being referenced by the stub or pointer.
2142static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2143 struct DisassembleInfo *info) {
Kevin Enderby85974882014-09-26 22:20:44 +00002144 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2145 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002146 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby85974882014-09-26 22:20:44 +00002147 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2148 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2149 for (unsigned J = 0; J < Seg.nsects; ++J) {
2150 MachO::section_64 Sec = info->O->getSection64(Load, J);
2151 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2152 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2153 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2154 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2155 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2156 section_type == MachO::S_SYMBOL_STUBS) &&
2157 ReferenceValue >= Sec.addr &&
2158 ReferenceValue < Sec.addr + Sec.size) {
2159 uint32_t stride;
2160 if (section_type == MachO::S_SYMBOL_STUBS)
2161 stride = Sec.reserved2;
2162 else
2163 stride = 8;
2164 if (stride == 0)
2165 return nullptr;
2166 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2167 if (index < Dysymtab.nindirectsyms) {
2168 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002169 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002170 if (indirect_symbol < Symtab.nsyms) {
2171 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2172 SymbolRef Symbol = *Sym;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002173 ErrorOr<StringRef> SymName = Symbol.getName();
2174 if (std::error_code EC = SymName.getError())
2175 report_fatal_error(EC.message());
2176 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002177 return name;
2178 }
2179 }
2180 }
2181 }
2182 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2183 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2184 for (unsigned J = 0; J < Seg.nsects; ++J) {
2185 MachO::section Sec = info->O->getSection(Load, J);
2186 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2187 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2188 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2189 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2190 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2191 section_type == MachO::S_SYMBOL_STUBS) &&
2192 ReferenceValue >= Sec.addr &&
2193 ReferenceValue < Sec.addr + Sec.size) {
2194 uint32_t stride;
2195 if (section_type == MachO::S_SYMBOL_STUBS)
2196 stride = Sec.reserved2;
2197 else
2198 stride = 4;
2199 if (stride == 0)
2200 return nullptr;
2201 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2202 if (index < Dysymtab.nindirectsyms) {
2203 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002204 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002205 if (indirect_symbol < Symtab.nsyms) {
2206 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2207 SymbolRef Symbol = *Sym;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002208 ErrorOr<StringRef> SymName = Symbol.getName();
2209 if (std::error_code EC = SymName.getError())
2210 report_fatal_error(EC.message());
2211 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002212 return name;
2213 }
2214 }
2215 }
2216 }
2217 }
Kevin Enderby85974882014-09-26 22:20:44 +00002218 }
2219 return nullptr;
2220}
2221
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002222// method_reference() is called passing it the ReferenceName that might be
2223// a reference it to an Objective-C method call. If so then it allocates and
2224// assembles a method call string with the values last seen and saved in
2225// the DisassembleInfo's class_name and selector_name fields. This is saved
2226// into the method field of the info and any previous string is free'ed.
2227// Then the class_name field in the info is set to nullptr. The method call
2228// string is set into ReferenceName and ReferenceType is set to
2229// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2230// then both ReferenceType and ReferenceName are left unchanged.
2231static void method_reference(struct DisassembleInfo *info,
2232 uint64_t *ReferenceType,
2233 const char **ReferenceName) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002234 unsigned int Arch = info->O->getArch();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002235 if (*ReferenceName != nullptr) {
2236 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002237 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002238 if (info->method != nullptr)
2239 free(info->method);
2240 if (info->class_name != nullptr) {
2241 info->method = (char *)malloc(5 + strlen(info->class_name) +
2242 strlen(info->selector_name));
2243 if (info->method != nullptr) {
2244 strcpy(info->method, "+[");
2245 strcat(info->method, info->class_name);
2246 strcat(info->method, " ");
2247 strcat(info->method, info->selector_name);
2248 strcat(info->method, "]");
2249 *ReferenceName = info->method;
2250 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2251 }
2252 } else {
2253 info->method = (char *)malloc(9 + strlen(info->selector_name));
2254 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002255 if (Arch == Triple::x86_64)
2256 strcpy(info->method, "-[%rdi ");
2257 else if (Arch == Triple::aarch64)
2258 strcpy(info->method, "-[x0 ");
2259 else
2260 strcpy(info->method, "-[r? ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002261 strcat(info->method, info->selector_name);
2262 strcat(info->method, "]");
2263 *ReferenceName = info->method;
2264 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2265 }
2266 }
2267 info->class_name = nullptr;
2268 }
2269 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002270 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002271 if (info->method != nullptr)
2272 free(info->method);
2273 info->method = (char *)malloc(17 + strlen(info->selector_name));
2274 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002275 if (Arch == Triple::x86_64)
2276 strcpy(info->method, "-[[%rdi super] ");
2277 else if (Arch == Triple::aarch64)
2278 strcpy(info->method, "-[[x0 super] ");
2279 else
2280 strcpy(info->method, "-[[r? super] ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002281 strcat(info->method, info->selector_name);
2282 strcat(info->method, "]");
2283 *ReferenceName = info->method;
2284 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2285 }
2286 info->class_name = nullptr;
2287 }
2288 }
2289 }
2290}
2291
2292// GuessPointerPointer() is passed the address of what might be a pointer to
2293// a reference to an Objective-C class, selector, message ref or cfstring.
2294// If so the value of the pointer is returned and one of the booleans are set
2295// to true. If not zero is returned and all the booleans are set to false.
2296static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2297 struct DisassembleInfo *info,
2298 bool &classref, bool &selref, bool &msgref,
2299 bool &cfstring) {
2300 classref = false;
2301 selref = false;
2302 msgref = false;
2303 cfstring = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002304 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002305 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2306 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2307 for (unsigned J = 0; J < Seg.nsects; ++J) {
2308 MachO::section_64 Sec = info->O->getSection64(Load, J);
2309 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2310 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2311 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2312 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2313 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2314 ReferenceValue >= Sec.addr &&
2315 ReferenceValue < Sec.addr + Sec.size) {
2316 uint64_t sect_offset = ReferenceValue - Sec.addr;
2317 uint64_t object_offset = Sec.offset + sect_offset;
2318 StringRef MachOContents = info->O->getData();
2319 uint64_t object_size = MachOContents.size();
2320 const char *object_addr = (const char *)MachOContents.data();
2321 if (object_offset < object_size) {
2322 uint64_t pointer_value;
2323 memcpy(&pointer_value, object_addr + object_offset,
2324 sizeof(uint64_t));
2325 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2326 sys::swapByteOrder(pointer_value);
2327 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2328 selref = true;
2329 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2330 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2331 classref = true;
2332 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2333 ReferenceValue + 8 < Sec.addr + Sec.size) {
2334 msgref = true;
2335 memcpy(&pointer_value, object_addr + object_offset + 8,
2336 sizeof(uint64_t));
2337 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2338 sys::swapByteOrder(pointer_value);
2339 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2340 cfstring = true;
2341 return pointer_value;
2342 } else {
2343 return 0;
2344 }
2345 }
2346 }
2347 }
2348 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002349 }
2350 return 0;
2351}
2352
2353// get_pointer_64 returns a pointer to the bytes in the object file at the
2354// Address from a section in the Mach-O file. And indirectly returns the
2355// offset into the section, number of bytes left in the section past the offset
2356// and which section is was being referenced. If the Address is not in a
2357// section nullptr is returned.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002358static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2359 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002360 DisassembleInfo *info,
2361 bool objc_only = false) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002362 offset = 0;
2363 left = 0;
2364 S = SectionRef();
2365 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2366 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2367 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
Kevin Enderby846c0002015-04-16 17:19:59 +00002368 if (objc_only) {
2369 StringRef SectName;
2370 ((*(info->Sections))[SectIdx]).getName(SectName);
2371 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2372 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2373 if (SegName != "__OBJC" && SectName != "__cstring")
2374 continue;
2375 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002376 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2377 S = (*(info->Sections))[SectIdx];
2378 offset = Address - SectAddress;
2379 left = SectSize - offset;
2380 StringRef SectContents;
2381 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2382 return SectContents.data() + offset;
2383 }
2384 }
2385 return nullptr;
2386}
2387
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002388static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2389 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002390 DisassembleInfo *info,
2391 bool objc_only = false) {
2392 return get_pointer_64(Address, offset, left, S, info, objc_only);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002393}
2394
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002395// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2396// the symbol indirectly through n_value. Based on the relocation information
2397// for the specified section offset in the specified section reference.
Kevin Enderby0fc11822015-04-01 20:57:01 +00002398// If no relocation information is found and a non-zero ReferenceValue for the
2399// symbol is passed, look up that address in the info's AddrMap.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002400static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2401 DisassembleInfo *info, uint64_t &n_value,
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002402 uint64_t ReferenceValue = 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002403 n_value = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002404 if (!info->verbose)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002405 return nullptr;
2406
2407 // See if there is an external relocation entry at the sect_offset.
2408 bool reloc_found = false;
2409 DataRefImpl Rel;
2410 MachO::any_relocation_info RE;
2411 bool isExtern = false;
2412 SymbolRef Symbol;
2413 for (const RelocationRef &Reloc : S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002414 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002415 if (RelocOffset == sect_offset) {
2416 Rel = Reloc.getRawDataRefImpl();
2417 RE = info->O->getRelocation(Rel);
2418 if (info->O->isRelocationScattered(RE))
2419 continue;
2420 isExtern = info->O->getPlainRelocationExternal(RE);
2421 if (isExtern) {
2422 symbol_iterator RelocSym = Reloc.getSymbol();
2423 Symbol = *RelocSym;
2424 }
2425 reloc_found = true;
2426 break;
2427 }
2428 }
2429 // If there is an external relocation entry for a symbol in this section
2430 // at this section_offset then use that symbol's value for the n_value
2431 // and return its name.
2432 const char *SymbolName = nullptr;
2433 if (reloc_found && isExtern) {
Rafael Espindoladea00162015-07-03 17:44:18 +00002434 n_value = Symbol.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002435 ErrorOr<StringRef> NameOrError = Symbol.getName();
2436 if (std::error_code EC = NameOrError.getError())
2437 report_fatal_error(EC.message());
2438 StringRef Name = *NameOrError;
2439 if (!Name.empty()) {
2440 SymbolName = Name.data();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002441 return SymbolName;
2442 }
2443 }
2444
2445 // TODO: For fully linked images, look through the external relocation
2446 // entries off the dynamic symtab command. For these the r_offset is from the
2447 // start of the first writeable segment in the Mach-O file. So the offset
2448 // to this section from that segment is passed to this routine by the caller,
2449 // as the database_offset. Which is the difference of the section's starting
2450 // address and the first writable segment.
2451 //
2452 // NOTE: need add passing the database_offset to this routine.
2453
Kevin Enderby0fc11822015-04-01 20:57:01 +00002454 // We did not find an external relocation entry so look up the ReferenceValue
2455 // as an address of a symbol and if found return that symbol's name.
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002456 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002457
2458 return SymbolName;
2459}
2460
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002461static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2462 DisassembleInfo *info,
2463 uint32_t ReferenceValue) {
2464 uint64_t n_value64;
2465 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2466}
2467
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002468// These are structs in the Objective-C meta data and read to produce the
2469// comments for disassembly. While these are part of the ABI they are no
2470// public defintions. So the are here not in include/llvm/Support/MachO.h .
2471
2472// The cfstring object in a 64-bit Mach-O file.
2473struct cfstring64_t {
2474 uint64_t isa; // class64_t * (64-bit pointer)
2475 uint64_t flags; // flag bits
2476 uint64_t characters; // char * (64-bit pointer)
2477 uint64_t length; // number of non-NULL characters in above
2478};
2479
2480// The class object in a 64-bit Mach-O file.
2481struct class64_t {
2482 uint64_t isa; // class64_t * (64-bit pointer)
2483 uint64_t superclass; // class64_t * (64-bit pointer)
2484 uint64_t cache; // Cache (64-bit pointer)
2485 uint64_t vtable; // IMP * (64-bit pointer)
2486 uint64_t data; // class_ro64_t * (64-bit pointer)
2487};
2488
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002489struct class32_t {
2490 uint32_t isa; /* class32_t * (32-bit pointer) */
2491 uint32_t superclass; /* class32_t * (32-bit pointer) */
2492 uint32_t cache; /* Cache (32-bit pointer) */
2493 uint32_t vtable; /* IMP * (32-bit pointer) */
2494 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2495};
2496
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002497struct class_ro64_t {
2498 uint32_t flags;
2499 uint32_t instanceStart;
2500 uint32_t instanceSize;
2501 uint32_t reserved;
2502 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2503 uint64_t name; // const char * (64-bit pointer)
2504 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2505 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2506 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2507 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2508 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2509};
2510
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002511struct class_ro32_t {
2512 uint32_t flags;
2513 uint32_t instanceStart;
2514 uint32_t instanceSize;
2515 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2516 uint32_t name; /* const char * (32-bit pointer) */
2517 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2518 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2519 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2520 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2521 uint32_t baseProperties; /* const struct objc_property_list *
2522 (32-bit pointer) */
2523};
2524
2525/* Values for class_ro{64,32}_t->flags */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002526#define RO_META (1 << 0)
2527#define RO_ROOT (1 << 1)
2528#define RO_HAS_CXX_STRUCTORS (1 << 2)
2529
2530struct method_list64_t {
2531 uint32_t entsize;
2532 uint32_t count;
2533 /* struct method64_t first; These structures follow inline */
2534};
2535
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002536struct method_list32_t {
2537 uint32_t entsize;
2538 uint32_t count;
2539 /* struct method32_t first; These structures follow inline */
2540};
2541
Kevin Enderby0fc11822015-04-01 20:57:01 +00002542struct method64_t {
2543 uint64_t name; /* SEL (64-bit pointer) */
2544 uint64_t types; /* const char * (64-bit pointer) */
2545 uint64_t imp; /* IMP (64-bit pointer) */
2546};
2547
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002548struct method32_t {
2549 uint32_t name; /* SEL (32-bit pointer) */
2550 uint32_t types; /* const char * (32-bit pointer) */
2551 uint32_t imp; /* IMP (32-bit pointer) */
2552};
2553
Kevin Enderby0fc11822015-04-01 20:57:01 +00002554struct protocol_list64_t {
2555 uint64_t count; /* uintptr_t (a 64-bit value) */
2556 /* struct protocol64_t * list[0]; These pointers follow inline */
2557};
2558
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002559struct protocol_list32_t {
2560 uint32_t count; /* uintptr_t (a 32-bit value) */
2561 /* struct protocol32_t * list[0]; These pointers follow inline */
2562};
2563
Kevin Enderby0fc11822015-04-01 20:57:01 +00002564struct protocol64_t {
2565 uint64_t isa; /* id * (64-bit pointer) */
2566 uint64_t name; /* const char * (64-bit pointer) */
2567 uint64_t protocols; /* struct protocol_list64_t *
2568 (64-bit pointer) */
2569 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2570 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2571 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2572 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2573 uint64_t instanceProperties; /* struct objc_property_list *
2574 (64-bit pointer) */
2575};
2576
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002577struct protocol32_t {
2578 uint32_t isa; /* id * (32-bit pointer) */
2579 uint32_t name; /* const char * (32-bit pointer) */
2580 uint32_t protocols; /* struct protocol_list_t *
2581 (32-bit pointer) */
2582 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2583 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2584 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2585 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2586 uint32_t instanceProperties; /* struct objc_property_list *
2587 (32-bit pointer) */
2588};
2589
Kevin Enderby0fc11822015-04-01 20:57:01 +00002590struct ivar_list64_t {
2591 uint32_t entsize;
2592 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002593 /* struct ivar64_t first; These structures follow inline */
2594};
2595
2596struct ivar_list32_t {
2597 uint32_t entsize;
2598 uint32_t count;
2599 /* struct ivar32_t first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002600};
2601
2602struct ivar64_t {
2603 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2604 uint64_t name; /* const char * (64-bit pointer) */
2605 uint64_t type; /* const char * (64-bit pointer) */
2606 uint32_t alignment;
2607 uint32_t size;
2608};
2609
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002610struct ivar32_t {
2611 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2612 uint32_t name; /* const char * (32-bit pointer) */
2613 uint32_t type; /* const char * (32-bit pointer) */
2614 uint32_t alignment;
2615 uint32_t size;
2616};
2617
Kevin Enderby0fc11822015-04-01 20:57:01 +00002618struct objc_property_list64 {
2619 uint32_t entsize;
2620 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002621 /* struct objc_property64 first; These structures follow inline */
2622};
2623
2624struct objc_property_list32 {
2625 uint32_t entsize;
2626 uint32_t count;
2627 /* struct objc_property32 first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002628};
2629
2630struct objc_property64 {
2631 uint64_t name; /* const char * (64-bit pointer) */
2632 uint64_t attributes; /* const char * (64-bit pointer) */
2633};
2634
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002635struct objc_property32 {
2636 uint32_t name; /* const char * (32-bit pointer) */
2637 uint32_t attributes; /* const char * (32-bit pointer) */
2638};
2639
Kevin Enderby0fc11822015-04-01 20:57:01 +00002640struct category64_t {
2641 uint64_t name; /* const char * (64-bit pointer) */
2642 uint64_t cls; /* struct class_t * (64-bit pointer) */
2643 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2644 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2645 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2646 uint64_t instanceProperties; /* struct objc_property_list *
2647 (64-bit pointer) */
2648};
2649
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002650struct category32_t {
2651 uint32_t name; /* const char * (32-bit pointer) */
2652 uint32_t cls; /* struct class_t * (32-bit pointer) */
2653 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2654 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2655 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2656 uint32_t instanceProperties; /* struct objc_property_list *
2657 (32-bit pointer) */
2658};
2659
Kevin Enderby0fc11822015-04-01 20:57:01 +00002660struct objc_image_info64 {
2661 uint32_t version;
2662 uint32_t flags;
2663};
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002664struct objc_image_info32 {
2665 uint32_t version;
2666 uint32_t flags;
2667};
Kevin Enderby846c0002015-04-16 17:19:59 +00002668struct imageInfo_t {
2669 uint32_t version;
2670 uint32_t flags;
2671};
Kevin Enderby0fc11822015-04-01 20:57:01 +00002672/* masks for objc_image_info.flags */
2673#define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2674#define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2675
2676struct message_ref64 {
2677 uint64_t imp; /* IMP (64-bit pointer) */
2678 uint64_t sel; /* SEL (64-bit pointer) */
2679};
2680
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002681struct message_ref32 {
2682 uint32_t imp; /* IMP (32-bit pointer) */
2683 uint32_t sel; /* SEL (32-bit pointer) */
2684};
2685
Kevin Enderby846c0002015-04-16 17:19:59 +00002686// Objective-C 1 (32-bit only) meta data structs.
2687
2688struct objc_module_t {
2689 uint32_t version;
2690 uint32_t size;
2691 uint32_t name; /* char * (32-bit pointer) */
2692 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2693};
2694
2695struct objc_symtab_t {
2696 uint32_t sel_ref_cnt;
2697 uint32_t refs; /* SEL * (32-bit pointer) */
2698 uint16_t cls_def_cnt;
2699 uint16_t cat_def_cnt;
2700 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2701};
2702
2703struct objc_class_t {
2704 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2705 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2706 uint32_t name; /* const char * (32-bit pointer) */
2707 int32_t version;
2708 int32_t info;
2709 int32_t instance_size;
2710 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2711 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2712 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2713 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2714};
2715
2716#define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2717// class is not a metaclass
2718#define CLS_CLASS 0x1
2719// class is a metaclass
2720#define CLS_META 0x2
2721
2722struct objc_category_t {
2723 uint32_t category_name; /* char * (32-bit pointer) */
2724 uint32_t class_name; /* char * (32-bit pointer) */
2725 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2726 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2727 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2728};
2729
2730struct objc_ivar_t {
2731 uint32_t ivar_name; /* char * (32-bit pointer) */
2732 uint32_t ivar_type; /* char * (32-bit pointer) */
2733 int32_t ivar_offset;
2734};
2735
2736struct objc_ivar_list_t {
2737 int32_t ivar_count;
2738 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2739};
2740
2741struct objc_method_list_t {
2742 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2743 int32_t method_count;
2744 // struct objc_method_t method_list[1]; /* variable length structure */
2745};
2746
2747struct objc_method_t {
2748 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2749 uint32_t method_types; /* char * (32-bit pointer) */
2750 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2751 (32-bit pointer) */
2752};
2753
2754struct objc_protocol_list_t {
2755 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2756 int32_t count;
2757 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2758 // (32-bit pointer) */
2759};
2760
2761struct objc_protocol_t {
2762 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2763 uint32_t protocol_name; /* char * (32-bit pointer) */
2764 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2765 uint32_t instance_methods; /* struct objc_method_description_list *
2766 (32-bit pointer) */
2767 uint32_t class_methods; /* struct objc_method_description_list *
2768 (32-bit pointer) */
2769};
2770
2771struct objc_method_description_list_t {
2772 int32_t count;
2773 // struct objc_method_description_t list[1];
2774};
2775
2776struct objc_method_description_t {
2777 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2778 uint32_t types; /* char * (32-bit pointer) */
2779};
2780
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002781inline void swapStruct(struct cfstring64_t &cfs) {
2782 sys::swapByteOrder(cfs.isa);
2783 sys::swapByteOrder(cfs.flags);
2784 sys::swapByteOrder(cfs.characters);
2785 sys::swapByteOrder(cfs.length);
2786}
2787
2788inline void swapStruct(struct class64_t &c) {
2789 sys::swapByteOrder(c.isa);
2790 sys::swapByteOrder(c.superclass);
2791 sys::swapByteOrder(c.cache);
2792 sys::swapByteOrder(c.vtable);
2793 sys::swapByteOrder(c.data);
2794}
2795
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002796inline void swapStruct(struct class32_t &c) {
2797 sys::swapByteOrder(c.isa);
2798 sys::swapByteOrder(c.superclass);
2799 sys::swapByteOrder(c.cache);
2800 sys::swapByteOrder(c.vtable);
2801 sys::swapByteOrder(c.data);
2802}
2803
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002804inline void swapStruct(struct class_ro64_t &cro) {
2805 sys::swapByteOrder(cro.flags);
2806 sys::swapByteOrder(cro.instanceStart);
2807 sys::swapByteOrder(cro.instanceSize);
2808 sys::swapByteOrder(cro.reserved);
2809 sys::swapByteOrder(cro.ivarLayout);
2810 sys::swapByteOrder(cro.name);
2811 sys::swapByteOrder(cro.baseMethods);
2812 sys::swapByteOrder(cro.baseProtocols);
2813 sys::swapByteOrder(cro.ivars);
2814 sys::swapByteOrder(cro.weakIvarLayout);
2815 sys::swapByteOrder(cro.baseProperties);
2816}
2817
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002818inline void swapStruct(struct class_ro32_t &cro) {
2819 sys::swapByteOrder(cro.flags);
2820 sys::swapByteOrder(cro.instanceStart);
2821 sys::swapByteOrder(cro.instanceSize);
2822 sys::swapByteOrder(cro.ivarLayout);
2823 sys::swapByteOrder(cro.name);
2824 sys::swapByteOrder(cro.baseMethods);
2825 sys::swapByteOrder(cro.baseProtocols);
2826 sys::swapByteOrder(cro.ivars);
2827 sys::swapByteOrder(cro.weakIvarLayout);
2828 sys::swapByteOrder(cro.baseProperties);
2829}
2830
Kevin Enderby0fc11822015-04-01 20:57:01 +00002831inline void swapStruct(struct method_list64_t &ml) {
2832 sys::swapByteOrder(ml.entsize);
2833 sys::swapByteOrder(ml.count);
2834}
2835
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002836inline void swapStruct(struct method_list32_t &ml) {
2837 sys::swapByteOrder(ml.entsize);
2838 sys::swapByteOrder(ml.count);
2839}
2840
Kevin Enderby0fc11822015-04-01 20:57:01 +00002841inline void swapStruct(struct method64_t &m) {
2842 sys::swapByteOrder(m.name);
2843 sys::swapByteOrder(m.types);
2844 sys::swapByteOrder(m.imp);
2845}
2846
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002847inline void swapStruct(struct method32_t &m) {
2848 sys::swapByteOrder(m.name);
2849 sys::swapByteOrder(m.types);
2850 sys::swapByteOrder(m.imp);
2851}
2852
Kevin Enderby0fc11822015-04-01 20:57:01 +00002853inline void swapStruct(struct protocol_list64_t &pl) {
2854 sys::swapByteOrder(pl.count);
2855}
2856
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002857inline void swapStruct(struct protocol_list32_t &pl) {
2858 sys::swapByteOrder(pl.count);
2859}
2860
Kevin Enderby0fc11822015-04-01 20:57:01 +00002861inline void swapStruct(struct protocol64_t &p) {
2862 sys::swapByteOrder(p.isa);
2863 sys::swapByteOrder(p.name);
2864 sys::swapByteOrder(p.protocols);
2865 sys::swapByteOrder(p.instanceMethods);
2866 sys::swapByteOrder(p.classMethods);
2867 sys::swapByteOrder(p.optionalInstanceMethods);
2868 sys::swapByteOrder(p.optionalClassMethods);
2869 sys::swapByteOrder(p.instanceProperties);
2870}
2871
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002872inline void swapStruct(struct protocol32_t &p) {
2873 sys::swapByteOrder(p.isa);
2874 sys::swapByteOrder(p.name);
2875 sys::swapByteOrder(p.protocols);
2876 sys::swapByteOrder(p.instanceMethods);
2877 sys::swapByteOrder(p.classMethods);
2878 sys::swapByteOrder(p.optionalInstanceMethods);
2879 sys::swapByteOrder(p.optionalClassMethods);
2880 sys::swapByteOrder(p.instanceProperties);
2881}
2882
Kevin Enderby0fc11822015-04-01 20:57:01 +00002883inline void swapStruct(struct ivar_list64_t &il) {
2884 sys::swapByteOrder(il.entsize);
2885 sys::swapByteOrder(il.count);
2886}
2887
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002888inline void swapStruct(struct ivar_list32_t &il) {
2889 sys::swapByteOrder(il.entsize);
2890 sys::swapByteOrder(il.count);
2891}
2892
Kevin Enderby0fc11822015-04-01 20:57:01 +00002893inline void swapStruct(struct ivar64_t &i) {
2894 sys::swapByteOrder(i.offset);
2895 sys::swapByteOrder(i.name);
2896 sys::swapByteOrder(i.type);
2897 sys::swapByteOrder(i.alignment);
2898 sys::swapByteOrder(i.size);
2899}
2900
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002901inline void swapStruct(struct ivar32_t &i) {
2902 sys::swapByteOrder(i.offset);
2903 sys::swapByteOrder(i.name);
2904 sys::swapByteOrder(i.type);
2905 sys::swapByteOrder(i.alignment);
2906 sys::swapByteOrder(i.size);
2907}
2908
Kevin Enderby0fc11822015-04-01 20:57:01 +00002909inline void swapStruct(struct objc_property_list64 &pl) {
2910 sys::swapByteOrder(pl.entsize);
2911 sys::swapByteOrder(pl.count);
2912}
2913
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002914inline void swapStruct(struct objc_property_list32 &pl) {
2915 sys::swapByteOrder(pl.entsize);
2916 sys::swapByteOrder(pl.count);
2917}
2918
Kevin Enderby0fc11822015-04-01 20:57:01 +00002919inline void swapStruct(struct objc_property64 &op) {
2920 sys::swapByteOrder(op.name);
2921 sys::swapByteOrder(op.attributes);
2922}
2923
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002924inline void swapStruct(struct objc_property32 &op) {
2925 sys::swapByteOrder(op.name);
2926 sys::swapByteOrder(op.attributes);
2927}
2928
Kevin Enderby0fc11822015-04-01 20:57:01 +00002929inline void swapStruct(struct category64_t &c) {
2930 sys::swapByteOrder(c.name);
2931 sys::swapByteOrder(c.cls);
2932 sys::swapByteOrder(c.instanceMethods);
2933 sys::swapByteOrder(c.classMethods);
2934 sys::swapByteOrder(c.protocols);
2935 sys::swapByteOrder(c.instanceProperties);
2936}
2937
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002938inline void swapStruct(struct category32_t &c) {
2939 sys::swapByteOrder(c.name);
2940 sys::swapByteOrder(c.cls);
2941 sys::swapByteOrder(c.instanceMethods);
2942 sys::swapByteOrder(c.classMethods);
2943 sys::swapByteOrder(c.protocols);
2944 sys::swapByteOrder(c.instanceProperties);
2945}
2946
Kevin Enderby0fc11822015-04-01 20:57:01 +00002947inline void swapStruct(struct objc_image_info64 &o) {
2948 sys::swapByteOrder(o.version);
2949 sys::swapByteOrder(o.flags);
2950}
2951
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002952inline void swapStruct(struct objc_image_info32 &o) {
2953 sys::swapByteOrder(o.version);
2954 sys::swapByteOrder(o.flags);
2955}
2956
Kevin Enderby846c0002015-04-16 17:19:59 +00002957inline void swapStruct(struct imageInfo_t &o) {
2958 sys::swapByteOrder(o.version);
2959 sys::swapByteOrder(o.flags);
2960}
2961
Kevin Enderby0fc11822015-04-01 20:57:01 +00002962inline void swapStruct(struct message_ref64 &mr) {
2963 sys::swapByteOrder(mr.imp);
2964 sys::swapByteOrder(mr.sel);
2965}
2966
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002967inline void swapStruct(struct message_ref32 &mr) {
2968 sys::swapByteOrder(mr.imp);
2969 sys::swapByteOrder(mr.sel);
2970}
2971
Kevin Enderby846c0002015-04-16 17:19:59 +00002972inline void swapStruct(struct objc_module_t &module) {
2973 sys::swapByteOrder(module.version);
2974 sys::swapByteOrder(module.size);
2975 sys::swapByteOrder(module.name);
2976 sys::swapByteOrder(module.symtab);
Jingyue Wufedecc42015-04-16 18:43:44 +00002977}
Kevin Enderby846c0002015-04-16 17:19:59 +00002978
2979inline void swapStruct(struct objc_symtab_t &symtab) {
2980 sys::swapByteOrder(symtab.sel_ref_cnt);
2981 sys::swapByteOrder(symtab.refs);
2982 sys::swapByteOrder(symtab.cls_def_cnt);
2983 sys::swapByteOrder(symtab.cat_def_cnt);
Jingyue Wufedecc42015-04-16 18:43:44 +00002984}
Kevin Enderby846c0002015-04-16 17:19:59 +00002985
2986inline void swapStruct(struct objc_class_t &objc_class) {
2987 sys::swapByteOrder(objc_class.isa);
2988 sys::swapByteOrder(objc_class.super_class);
2989 sys::swapByteOrder(objc_class.name);
2990 sys::swapByteOrder(objc_class.version);
2991 sys::swapByteOrder(objc_class.info);
2992 sys::swapByteOrder(objc_class.instance_size);
2993 sys::swapByteOrder(objc_class.ivars);
2994 sys::swapByteOrder(objc_class.methodLists);
2995 sys::swapByteOrder(objc_class.cache);
2996 sys::swapByteOrder(objc_class.protocols);
Jingyue Wufedecc42015-04-16 18:43:44 +00002997}
Kevin Enderby846c0002015-04-16 17:19:59 +00002998
2999inline void swapStruct(struct objc_category_t &objc_category) {
3000 sys::swapByteOrder(objc_category.category_name);
3001 sys::swapByteOrder(objc_category.class_name);
3002 sys::swapByteOrder(objc_category.instance_methods);
3003 sys::swapByteOrder(objc_category.class_methods);
3004 sys::swapByteOrder(objc_category.protocols);
3005}
3006
3007inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3008 sys::swapByteOrder(objc_ivar_list.ivar_count);
3009}
3010
3011inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3012 sys::swapByteOrder(objc_ivar.ivar_name);
3013 sys::swapByteOrder(objc_ivar.ivar_type);
3014 sys::swapByteOrder(objc_ivar.ivar_offset);
Jingyue Wufedecc42015-04-16 18:43:44 +00003015}
Kevin Enderby846c0002015-04-16 17:19:59 +00003016
3017inline void swapStruct(struct objc_method_list_t &method_list) {
3018 sys::swapByteOrder(method_list.obsolete);
3019 sys::swapByteOrder(method_list.method_count);
3020}
3021
3022inline void swapStruct(struct objc_method_t &method) {
3023 sys::swapByteOrder(method.method_name);
3024 sys::swapByteOrder(method.method_types);
3025 sys::swapByteOrder(method.method_imp);
3026}
3027
3028inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3029 sys::swapByteOrder(protocol_list.next);
3030 sys::swapByteOrder(protocol_list.count);
3031}
3032
3033inline void swapStruct(struct objc_protocol_t &protocol) {
3034 sys::swapByteOrder(protocol.isa);
3035 sys::swapByteOrder(protocol.protocol_name);
3036 sys::swapByteOrder(protocol.protocol_list);
3037 sys::swapByteOrder(protocol.instance_methods);
3038 sys::swapByteOrder(protocol.class_methods);
3039}
3040
3041inline void swapStruct(struct objc_method_description_list_t &mdl) {
3042 sys::swapByteOrder(mdl.count);
3043}
3044
3045inline void swapStruct(struct objc_method_description_t &md) {
3046 sys::swapByteOrder(md.name);
3047 sys::swapByteOrder(md.types);
3048}
3049
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003050static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3051 struct DisassembleInfo *info);
3052
3053// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3054// to an Objective-C class and returns the class name. It is also passed the
3055// address of the pointer, so when the pointer is zero as it can be in an .o
3056// file, that is used to look for an external relocation entry with a symbol
3057// name.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003058static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3059 uint64_t ReferenceValue,
3060 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003061 const char *r;
3062 uint32_t offset, left;
3063 SectionRef S;
3064
3065 // The pointer_value can be 0 in an object file and have a relocation
3066 // entry for the class symbol at the ReferenceValue (the address of the
3067 // pointer).
3068 if (pointer_value == 0) {
3069 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3070 if (r == nullptr || left < sizeof(uint64_t))
3071 return nullptr;
3072 uint64_t n_value;
3073 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3074 if (symbol_name == nullptr)
3075 return nullptr;
Hans Wennborgdb53e302014-10-23 21:59:17 +00003076 const char *class_name = strrchr(symbol_name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003077 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3078 return class_name + 2;
3079 else
3080 return nullptr;
3081 }
3082
3083 // The case were the pointer_value is non-zero and points to a class defined
3084 // in this Mach-O file.
3085 r = get_pointer_64(pointer_value, offset, left, S, info);
3086 if (r == nullptr || left < sizeof(struct class64_t))
3087 return nullptr;
3088 struct class64_t c;
3089 memcpy(&c, r, sizeof(struct class64_t));
3090 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3091 swapStruct(c);
3092 if (c.data == 0)
3093 return nullptr;
3094 r = get_pointer_64(c.data, offset, left, S, info);
3095 if (r == nullptr || left < sizeof(struct class_ro64_t))
3096 return nullptr;
3097 struct class_ro64_t cro;
3098 memcpy(&cro, r, sizeof(struct class_ro64_t));
3099 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3100 swapStruct(cro);
3101 if (cro.name == 0)
3102 return nullptr;
3103 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3104 return name;
3105}
3106
3107// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3108// pointer to a cfstring and returns its name or nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003109static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3110 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003111 const char *r, *name;
3112 uint32_t offset, left;
3113 SectionRef S;
3114 struct cfstring64_t cfs;
3115 uint64_t cfs_characters;
3116
3117 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3118 if (r == nullptr || left < sizeof(struct cfstring64_t))
3119 return nullptr;
3120 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3121 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3122 swapStruct(cfs);
3123 if (cfs.characters == 0) {
3124 uint64_t n_value;
3125 const char *symbol_name = get_symbol_64(
3126 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3127 if (symbol_name == nullptr)
3128 return nullptr;
3129 cfs_characters = n_value;
3130 } else
3131 cfs_characters = cfs.characters;
3132 name = get_pointer_64(cfs_characters, offset, left, S, info);
3133
3134 return name;
3135}
3136
3137// get_objc2_64bit_selref() is used for disassembly and is passed a the address
3138// of a pointer to an Objective-C selector reference when the pointer value is
3139// zero as in a .o file and is likely to have a external relocation entry with
3140// who's symbol's n_value is the real pointer to the selector name. If that is
3141// the case the real pointer to the selector name is returned else 0 is
3142// returned
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003143static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3144 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003145 uint32_t offset, left;
3146 SectionRef S;
3147
3148 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3149 if (r == nullptr || left < sizeof(uint64_t))
3150 return 0;
3151 uint64_t n_value;
3152 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3153 if (symbol_name == nullptr)
3154 return 0;
3155 return n_value;
3156}
3157
Kevin Enderby0fc11822015-04-01 20:57:01 +00003158static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3159 const char *sectname) {
3160 for (const SectionRef &Section : O->sections()) {
3161 StringRef SectName;
3162 Section.getName(SectName);
3163 DataRefImpl Ref = Section.getRawDataRefImpl();
3164 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3165 if (SegName == segname && SectName == sectname)
3166 return Section;
3167 }
3168 return SectionRef();
3169}
3170
3171static void
3172walk_pointer_list_64(const char *listname, const SectionRef S,
3173 MachOObjectFile *O, struct DisassembleInfo *info,
3174 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3175 if (S == SectionRef())
3176 return;
3177
3178 StringRef SectName;
3179 S.getName(SectName);
3180 DataRefImpl Ref = S.getRawDataRefImpl();
3181 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3182 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3183
3184 StringRef BytesStr;
3185 S.getContents(BytesStr);
3186 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3187
3188 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3189 uint32_t left = S.getSize() - i;
3190 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3191 uint64_t p = 0;
3192 memcpy(&p, Contents + i, size);
3193 if (i + sizeof(uint64_t) > S.getSize())
3194 outs() << listname << " list pointer extends past end of (" << SegName
3195 << "," << SectName << ") section\n";
3196 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3197
3198 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3199 sys::swapByteOrder(p);
3200
3201 uint64_t n_value = 0;
3202 const char *name = get_symbol_64(i, S, info, n_value, p);
3203 if (name == nullptr)
3204 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3205
3206 if (n_value != 0) {
3207 outs() << format("0x%" PRIx64, n_value);
3208 if (p != 0)
3209 outs() << " + " << format("0x%" PRIx64, p);
3210 } else
3211 outs() << format("0x%" PRIx64, p);
3212 if (name != nullptr)
3213 outs() << " " << name;
3214 outs() << "\n";
3215
3216 p += n_value;
3217 if (func)
3218 func(p, info);
3219 }
3220}
3221
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003222static void
3223walk_pointer_list_32(const char *listname, const SectionRef S,
3224 MachOObjectFile *O, struct DisassembleInfo *info,
3225 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3226 if (S == SectionRef())
3227 return;
3228
3229 StringRef SectName;
3230 S.getName(SectName);
3231 DataRefImpl Ref = S.getRawDataRefImpl();
3232 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3233 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3234
3235 StringRef BytesStr;
3236 S.getContents(BytesStr);
3237 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3238
3239 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3240 uint32_t left = S.getSize() - i;
3241 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3242 uint32_t p = 0;
3243 memcpy(&p, Contents + i, size);
3244 if (i + sizeof(uint32_t) > S.getSize())
3245 outs() << listname << " list pointer extends past end of (" << SegName
3246 << "," << SectName << ") section\n";
Kevin Enderbycf261312015-04-06 22:33:43 +00003247 uint32_t Address = S.getAddress() + i;
3248 outs() << format("%08" PRIx32, Address) << " ";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003249
3250 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3251 sys::swapByteOrder(p);
3252 outs() << format("0x%" PRIx32, p);
3253
3254 const char *name = get_symbol_32(i, S, info, p);
3255 if (name != nullptr)
3256 outs() << " " << name;
3257 outs() << "\n";
3258
3259 if (func)
3260 func(p, info);
3261 }
3262}
3263
3264static void print_layout_map(const char *layout_map, uint32_t left) {
3265 outs() << " layout map: ";
3266 do {
3267 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3268 left--;
3269 layout_map++;
3270 } while (*layout_map != '\0' && left != 0);
3271 outs() << "\n";
3272}
3273
Kevin Enderby0fc11822015-04-01 20:57:01 +00003274static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3275 uint32_t offset, left;
3276 SectionRef S;
3277 const char *layout_map;
3278
3279 if (p == 0)
3280 return;
3281 layout_map = get_pointer_64(p, offset, left, S, info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003282 print_layout_map(layout_map, left);
3283}
3284
3285static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3286 uint32_t offset, left;
3287 SectionRef S;
3288 const char *layout_map;
3289
3290 if (p == 0)
3291 return;
3292 layout_map = get_pointer_32(p, offset, left, S, info);
3293 print_layout_map(layout_map, left);
Kevin Enderby0fc11822015-04-01 20:57:01 +00003294}
3295
3296static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3297 const char *indent) {
3298 struct method_list64_t ml;
3299 struct method64_t m;
3300 const char *r;
3301 uint32_t offset, xoffset, left, i;
3302 SectionRef S, xS;
3303 const char *name, *sym_name;
3304 uint64_t n_value;
3305
3306 r = get_pointer_64(p, offset, left, S, info);
3307 if (r == nullptr)
3308 return;
3309 memset(&ml, '\0', sizeof(struct method_list64_t));
3310 if (left < sizeof(struct method_list64_t)) {
3311 memcpy(&ml, r, left);
3312 outs() << " (method_list_t entends past the end of the section)\n";
3313 } else
3314 memcpy(&ml, r, sizeof(struct method_list64_t));
3315 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3316 swapStruct(ml);
3317 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3318 outs() << indent << "\t\t count " << ml.count << "\n";
3319
3320 p += sizeof(struct method_list64_t);
3321 offset += sizeof(struct method_list64_t);
3322 for (i = 0; i < ml.count; i++) {
3323 r = get_pointer_64(p, offset, left, S, info);
3324 if (r == nullptr)
3325 return;
3326 memset(&m, '\0', sizeof(struct method64_t));
3327 if (left < sizeof(struct method64_t)) {
3328 memcpy(&ml, r, left);
3329 outs() << indent << " (method_t entends past the end of the section)\n";
3330 } else
3331 memcpy(&m, r, sizeof(struct method64_t));
3332 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3333 swapStruct(m);
3334
3335 outs() << indent << "\t\t name ";
3336 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3337 info, n_value, m.name);
3338 if (n_value != 0) {
3339 if (info->verbose && sym_name != nullptr)
3340 outs() << sym_name;
3341 else
3342 outs() << format("0x%" PRIx64, n_value);
3343 if (m.name != 0)
3344 outs() << " + " << format("0x%" PRIx64, m.name);
3345 } else
3346 outs() << format("0x%" PRIx64, m.name);
3347 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3348 if (name != nullptr)
3349 outs() << format(" %.*s", left, name);
3350 outs() << "\n";
3351
3352 outs() << indent << "\t\t types ";
3353 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3354 info, n_value, m.types);
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.types != 0)
3361 outs() << " + " << format("0x%" PRIx64, m.types);
3362 } else
3363 outs() << format("0x%" PRIx64, m.types);
3364 name = get_pointer_64(m.types + 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 imp ";
3370 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3371 n_value, m.imp);
3372 if (info->verbose && name == nullptr) {
3373 if (n_value != 0) {
3374 outs() << format("0x%" PRIx64, n_value) << " ";
3375 if (m.imp != 0)
3376 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3377 } else
3378 outs() << format("0x%" PRIx64, m.imp) << " ";
3379 }
3380 if (name != nullptr)
3381 outs() << name;
3382 outs() << "\n";
3383
3384 p += sizeof(struct method64_t);
3385 offset += sizeof(struct method64_t);
3386 }
3387}
3388
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003389static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3390 const char *indent) {
3391 struct method_list32_t ml;
3392 struct method32_t m;
Kevin Enderby846c0002015-04-16 17:19:59 +00003393 const char *r, *name;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003394 uint32_t offset, xoffset, left, i;
3395 SectionRef S, xS;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003396
3397 r = get_pointer_32(p, offset, left, S, info);
3398 if (r == nullptr)
3399 return;
3400 memset(&ml, '\0', sizeof(struct method_list32_t));
3401 if (left < sizeof(struct method_list32_t)) {
3402 memcpy(&ml, r, left);
3403 outs() << " (method_list_t entends past the end of the section)\n";
3404 } else
3405 memcpy(&ml, r, sizeof(struct method_list32_t));
3406 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3407 swapStruct(ml);
3408 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3409 outs() << indent << "\t\t count " << ml.count << "\n";
3410
3411 p += sizeof(struct method_list32_t);
3412 offset += sizeof(struct method_list32_t);
3413 for (i = 0; i < ml.count; i++) {
3414 r = get_pointer_32(p, offset, left, S, info);
3415 if (r == nullptr)
3416 return;
3417 memset(&m, '\0', sizeof(struct method32_t));
3418 if (left < sizeof(struct method32_t)) {
3419 memcpy(&ml, r, left);
3420 outs() << indent << " (method_t entends past the end of the section)\n";
3421 } else
3422 memcpy(&m, r, sizeof(struct method32_t));
3423 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3424 swapStruct(m);
3425
3426 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3427 name = get_pointer_32(m.name, xoffset, left, xS, info);
3428 if (name != nullptr)
3429 outs() << format(" %.*s", left, name);
3430 outs() << "\n";
3431
3432 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3433 name = get_pointer_32(m.types, xoffset, left, xS, info);
3434 if (name != nullptr)
3435 outs() << format(" %.*s", left, name);
3436 outs() << "\n";
3437
3438 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3439 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3440 m.imp);
3441 if (name != nullptr)
3442 outs() << " " << name;
3443 outs() << "\n";
3444
3445 p += sizeof(struct method32_t);
3446 offset += sizeof(struct method32_t);
3447 }
3448}
3449
Kevin Enderby846c0002015-04-16 17:19:59 +00003450static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3451 uint32_t offset, left, xleft;
3452 SectionRef S;
3453 struct objc_method_list_t method_list;
3454 struct objc_method_t method;
3455 const char *r, *methods, *name, *SymbolName;
3456 int32_t i;
3457
3458 r = get_pointer_32(p, offset, left, S, info, true);
3459 if (r == nullptr)
3460 return true;
3461
3462 outs() << "\n";
3463 if (left > sizeof(struct objc_method_list_t)) {
3464 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3465 } else {
3466 outs() << "\t\t objc_method_list extends past end of the section\n";
3467 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3468 memcpy(&method_list, r, left);
3469 }
3470 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3471 swapStruct(method_list);
3472
3473 outs() << "\t\t obsolete "
3474 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3475 outs() << "\t\t method_count " << method_list.method_count << "\n";
3476
3477 methods = r + sizeof(struct objc_method_list_t);
3478 for (i = 0; i < method_list.method_count; i++) {
3479 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3480 outs() << "\t\t remaining method's extend past the of the section\n";
3481 break;
3482 }
3483 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3484 sizeof(struct objc_method_t));
3485 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3486 swapStruct(method);
3487
3488 outs() << "\t\t method_name "
3489 << format("0x%08" PRIx32, method.method_name);
3490 if (info->verbose) {
3491 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3492 if (name != nullptr)
3493 outs() << format(" %.*s", xleft, name);
3494 else
3495 outs() << " (not in an __OBJC section)";
3496 }
3497 outs() << "\n";
3498
3499 outs() << "\t\t method_types "
3500 << format("0x%08" PRIx32, method.method_types);
3501 if (info->verbose) {
3502 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3503 if (name != nullptr)
3504 outs() << format(" %.*s", xleft, name);
3505 else
3506 outs() << " (not in an __OBJC section)";
3507 }
3508 outs() << "\n";
3509
3510 outs() << "\t\t method_imp "
3511 << format("0x%08" PRIx32, method.method_imp) << " ";
3512 if (info->verbose) {
3513 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3514 if (SymbolName != nullptr)
3515 outs() << SymbolName;
3516 }
3517 outs() << "\n";
3518 }
3519 return false;
3520}
3521
Kevin Enderby0fc11822015-04-01 20:57:01 +00003522static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3523 struct protocol_list64_t pl;
3524 uint64_t q, n_value;
3525 struct protocol64_t pc;
3526 const char *r;
3527 uint32_t offset, xoffset, left, i;
3528 SectionRef S, xS;
3529 const char *name, *sym_name;
3530
3531 r = get_pointer_64(p, offset, left, S, info);
3532 if (r == nullptr)
3533 return;
3534 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3535 if (left < sizeof(struct protocol_list64_t)) {
3536 memcpy(&pl, r, left);
3537 outs() << " (protocol_list_t entends past the end of the section)\n";
3538 } else
3539 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3540 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3541 swapStruct(pl);
3542 outs() << " count " << pl.count << "\n";
3543
3544 p += sizeof(struct protocol_list64_t);
3545 offset += sizeof(struct protocol_list64_t);
3546 for (i = 0; i < pl.count; i++) {
3547 r = get_pointer_64(p, offset, left, S, info);
3548 if (r == nullptr)
3549 return;
3550 q = 0;
3551 if (left < sizeof(uint64_t)) {
3552 memcpy(&q, r, left);
3553 outs() << " (protocol_t * entends past the end of the section)\n";
3554 } else
3555 memcpy(&q, r, sizeof(uint64_t));
3556 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3557 sys::swapByteOrder(q);
3558
3559 outs() << "\t\t list[" << i << "] ";
3560 sym_name = get_symbol_64(offset, S, info, n_value, q);
3561 if (n_value != 0) {
3562 if (info->verbose && sym_name != nullptr)
3563 outs() << sym_name;
3564 else
3565 outs() << format("0x%" PRIx64, n_value);
3566 if (q != 0)
3567 outs() << " + " << format("0x%" PRIx64, q);
3568 } else
3569 outs() << format("0x%" PRIx64, q);
3570 outs() << " (struct protocol_t *)\n";
3571
3572 r = get_pointer_64(q + n_value, offset, left, S, info);
3573 if (r == nullptr)
3574 return;
3575 memset(&pc, '\0', sizeof(struct protocol64_t));
3576 if (left < sizeof(struct protocol64_t)) {
3577 memcpy(&pc, r, left);
3578 outs() << " (protocol_t entends past the end of the section)\n";
3579 } else
3580 memcpy(&pc, r, sizeof(struct protocol64_t));
3581 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3582 swapStruct(pc);
3583
3584 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3585
3586 outs() << "\t\t\t name ";
3587 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3588 info, n_value, pc.name);
3589 if (n_value != 0) {
3590 if (info->verbose && sym_name != nullptr)
3591 outs() << sym_name;
3592 else
3593 outs() << format("0x%" PRIx64, n_value);
3594 if (pc.name != 0)
3595 outs() << " + " << format("0x%" PRIx64, pc.name);
3596 } else
3597 outs() << format("0x%" PRIx64, pc.name);
3598 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3599 if (name != nullptr)
3600 outs() << format(" %.*s", left, name);
3601 outs() << "\n";
3602
3603 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3604
3605 outs() << "\t\t instanceMethods ";
3606 sym_name =
3607 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3608 S, info, n_value, pc.instanceMethods);
3609 if (n_value != 0) {
3610 if (info->verbose && sym_name != nullptr)
3611 outs() << sym_name;
3612 else
3613 outs() << format("0x%" PRIx64, n_value);
3614 if (pc.instanceMethods != 0)
3615 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3616 } else
3617 outs() << format("0x%" PRIx64, pc.instanceMethods);
3618 outs() << " (struct method_list_t *)\n";
3619 if (pc.instanceMethods + n_value != 0)
3620 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3621
3622 outs() << "\t\t classMethods ";
3623 sym_name =
3624 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3625 info, n_value, pc.classMethods);
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.classMethods != 0)
3632 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3633 } else
3634 outs() << format("0x%" PRIx64, pc.classMethods);
3635 outs() << " (struct method_list_t *)\n";
3636 if (pc.classMethods + n_value != 0)
3637 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3638
3639 outs() << "\t optionalInstanceMethods "
3640 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3641 outs() << "\t optionalClassMethods "
3642 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3643 outs() << "\t instanceProperties "
3644 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3645
3646 p += sizeof(uint64_t);
3647 offset += sizeof(uint64_t);
3648 }
3649}
3650
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003651static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3652 struct protocol_list32_t pl;
3653 uint32_t q;
3654 struct protocol32_t pc;
3655 const char *r;
3656 uint32_t offset, xoffset, left, i;
3657 SectionRef S, xS;
3658 const char *name;
3659
3660 r = get_pointer_32(p, offset, left, S, info);
3661 if (r == nullptr)
3662 return;
3663 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3664 if (left < sizeof(struct protocol_list32_t)) {
3665 memcpy(&pl, r, left);
3666 outs() << " (protocol_list_t entends past the end of the section)\n";
3667 } else
3668 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3669 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3670 swapStruct(pl);
3671 outs() << " count " << pl.count << "\n";
3672
3673 p += sizeof(struct protocol_list32_t);
3674 offset += sizeof(struct protocol_list32_t);
3675 for (i = 0; i < pl.count; i++) {
3676 r = get_pointer_32(p, offset, left, S, info);
3677 if (r == nullptr)
3678 return;
3679 q = 0;
3680 if (left < sizeof(uint32_t)) {
3681 memcpy(&q, r, left);
3682 outs() << " (protocol_t * entends past the end of the section)\n";
3683 } else
3684 memcpy(&q, r, sizeof(uint32_t));
3685 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3686 sys::swapByteOrder(q);
3687 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3688 << " (struct protocol_t *)\n";
3689 r = get_pointer_32(q, offset, left, S, info);
3690 if (r == nullptr)
3691 return;
3692 memset(&pc, '\0', sizeof(struct protocol32_t));
3693 if (left < sizeof(struct protocol32_t)) {
3694 memcpy(&pc, r, left);
3695 outs() << " (protocol_t entends past the end of the section)\n";
3696 } else
3697 memcpy(&pc, r, sizeof(struct protocol32_t));
3698 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3699 swapStruct(pc);
3700 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3701 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3702 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3703 if (name != nullptr)
3704 outs() << format(" %.*s", left, name);
3705 outs() << "\n";
3706 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3707 outs() << "\t\t instanceMethods "
3708 << format("0x%" PRIx32, pc.instanceMethods)
3709 << " (struct method_list_t *)\n";
3710 if (pc.instanceMethods != 0)
3711 print_method_list32_t(pc.instanceMethods, info, "\t");
3712 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3713 << " (struct method_list_t *)\n";
3714 if (pc.classMethods != 0)
3715 print_method_list32_t(pc.classMethods, info, "\t");
3716 outs() << "\t optionalInstanceMethods "
3717 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3718 outs() << "\t optionalClassMethods "
3719 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3720 outs() << "\t instanceProperties "
3721 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3722 p += sizeof(uint32_t);
3723 offset += sizeof(uint32_t);
3724 }
3725}
3726
Kevin Enderby846c0002015-04-16 17:19:59 +00003727static void print_indent(uint32_t indent) {
3728 for (uint32_t i = 0; i < indent;) {
3729 if (indent - i >= 8) {
3730 outs() << "\t";
3731 i += 8;
3732 } else {
3733 for (uint32_t j = i; j < indent; j++)
3734 outs() << " ";
3735 return;
3736 }
3737 }
3738}
3739
3740static bool print_method_description_list(uint32_t p, uint32_t indent,
3741 struct DisassembleInfo *info) {
3742 uint32_t offset, left, xleft;
3743 SectionRef S;
3744 struct objc_method_description_list_t mdl;
3745 struct objc_method_description_t md;
3746 const char *r, *list, *name;
3747 int32_t i;
3748
3749 r = get_pointer_32(p, offset, left, S, info, true);
3750 if (r == nullptr)
3751 return true;
3752
3753 outs() << "\n";
3754 if (left > sizeof(struct objc_method_description_list_t)) {
3755 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3756 } else {
3757 print_indent(indent);
3758 outs() << " objc_method_description_list extends past end of the section\n";
3759 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3760 memcpy(&mdl, r, left);
3761 }
3762 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3763 swapStruct(mdl);
3764
3765 print_indent(indent);
3766 outs() << " count " << mdl.count << "\n";
3767
3768 list = r + sizeof(struct objc_method_description_list_t);
3769 for (i = 0; i < mdl.count; i++) {
3770 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3771 print_indent(indent);
3772 outs() << " remaining list entries extend past the of the section\n";
3773 break;
3774 }
3775 print_indent(indent);
3776 outs() << " list[" << i << "]\n";
3777 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3778 sizeof(struct objc_method_description_t));
3779 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3780 swapStruct(md);
3781
3782 print_indent(indent);
3783 outs() << " name " << format("0x%08" PRIx32, md.name);
3784 if (info->verbose) {
3785 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3786 if (name != nullptr)
3787 outs() << format(" %.*s", xleft, name);
3788 else
3789 outs() << " (not in an __OBJC section)";
3790 }
3791 outs() << "\n";
3792
3793 print_indent(indent);
3794 outs() << " types " << format("0x%08" PRIx32, md.types);
3795 if (info->verbose) {
3796 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3797 if (name != nullptr)
3798 outs() << format(" %.*s", xleft, name);
3799 else
3800 outs() << " (not in an __OBJC section)";
3801 }
3802 outs() << "\n";
3803 }
3804 return false;
3805}
3806
3807static bool print_protocol_list(uint32_t p, uint32_t indent,
3808 struct DisassembleInfo *info);
3809
3810static bool print_protocol(uint32_t p, uint32_t indent,
3811 struct DisassembleInfo *info) {
3812 uint32_t offset, left;
3813 SectionRef S;
3814 struct objc_protocol_t protocol;
3815 const char *r, *name;
3816
3817 r = get_pointer_32(p, offset, left, S, info, true);
3818 if (r == nullptr)
3819 return true;
3820
3821 outs() << "\n";
3822 if (left >= sizeof(struct objc_protocol_t)) {
3823 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3824 } else {
3825 print_indent(indent);
3826 outs() << " Protocol extends past end of the section\n";
3827 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3828 memcpy(&protocol, r, left);
3829 }
3830 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3831 swapStruct(protocol);
3832
3833 print_indent(indent);
3834 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3835 << "\n";
3836
3837 print_indent(indent);
3838 outs() << " protocol_name "
3839 << format("0x%08" PRIx32, protocol.protocol_name);
3840 if (info->verbose) {
3841 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3842 if (name != nullptr)
3843 outs() << format(" %.*s", left, name);
3844 else
3845 outs() << " (not in an __OBJC section)";
3846 }
3847 outs() << "\n";
3848
3849 print_indent(indent);
3850 outs() << " protocol_list "
3851 << format("0x%08" PRIx32, protocol.protocol_list);
3852 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3853 outs() << " (not in an __OBJC section)\n";
3854
3855 print_indent(indent);
3856 outs() << " instance_methods "
3857 << format("0x%08" PRIx32, protocol.instance_methods);
3858 if (print_method_description_list(protocol.instance_methods, indent, info))
3859 outs() << " (not in an __OBJC section)\n";
3860
3861 print_indent(indent);
3862 outs() << " class_methods "
3863 << format("0x%08" PRIx32, protocol.class_methods);
3864 if (print_method_description_list(protocol.class_methods, indent, info))
3865 outs() << " (not in an __OBJC section)\n";
3866
3867 return false;
3868}
3869
3870static bool print_protocol_list(uint32_t p, uint32_t indent,
3871 struct DisassembleInfo *info) {
3872 uint32_t offset, left, l;
3873 SectionRef S;
3874 struct objc_protocol_list_t protocol_list;
3875 const char *r, *list;
3876 int32_t i;
3877
3878 r = get_pointer_32(p, offset, left, S, info, true);
3879 if (r == nullptr)
3880 return true;
3881
3882 outs() << "\n";
3883 if (left > sizeof(struct objc_protocol_list_t)) {
3884 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
3885 } else {
3886 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
3887 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
3888 memcpy(&protocol_list, r, left);
3889 }
3890 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3891 swapStruct(protocol_list);
3892
3893 print_indent(indent);
3894 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
3895 << "\n";
3896 print_indent(indent);
3897 outs() << " count " << protocol_list.count << "\n";
3898
3899 list = r + sizeof(struct objc_protocol_list_t);
3900 for (i = 0; i < protocol_list.count; i++) {
3901 if ((i + 1) * sizeof(uint32_t) > left) {
3902 outs() << "\t\t remaining list entries extend past the of the section\n";
3903 break;
3904 }
3905 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
3906 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3907 sys::swapByteOrder(l);
3908
3909 print_indent(indent);
3910 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
3911 if (print_protocol(l, indent, info))
3912 outs() << "(not in an __OBJC section)\n";
3913 }
3914 return false;
3915}
3916
Kevin Enderby0fc11822015-04-01 20:57:01 +00003917static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3918 struct ivar_list64_t il;
3919 struct ivar64_t i;
3920 const char *r;
3921 uint32_t offset, xoffset, left, j;
3922 SectionRef S, xS;
3923 const char *name, *sym_name, *ivar_offset_p;
3924 uint64_t ivar_offset, n_value;
3925
3926 r = get_pointer_64(p, offset, left, S, info);
3927 if (r == nullptr)
3928 return;
3929 memset(&il, '\0', sizeof(struct ivar_list64_t));
3930 if (left < sizeof(struct ivar_list64_t)) {
3931 memcpy(&il, r, left);
3932 outs() << " (ivar_list_t entends past the end of the section)\n";
3933 } else
3934 memcpy(&il, r, sizeof(struct ivar_list64_t));
3935 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3936 swapStruct(il);
3937 outs() << " entsize " << il.entsize << "\n";
3938 outs() << " count " << il.count << "\n";
3939
3940 p += sizeof(struct ivar_list64_t);
3941 offset += sizeof(struct ivar_list64_t);
3942 for (j = 0; j < il.count; j++) {
3943 r = get_pointer_64(p, offset, left, S, info);
3944 if (r == nullptr)
3945 return;
3946 memset(&i, '\0', sizeof(struct ivar64_t));
3947 if (left < sizeof(struct ivar64_t)) {
3948 memcpy(&i, r, left);
3949 outs() << " (ivar_t entends past the end of the section)\n";
3950 } else
3951 memcpy(&i, r, sizeof(struct ivar64_t));
3952 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3953 swapStruct(i);
3954
3955 outs() << "\t\t\t offset ";
3956 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
3957 info, n_value, i.offset);
3958 if (n_value != 0) {
3959 if (info->verbose && sym_name != nullptr)
3960 outs() << sym_name;
3961 else
3962 outs() << format("0x%" PRIx64, n_value);
3963 if (i.offset != 0)
3964 outs() << " + " << format("0x%" PRIx64, i.offset);
3965 } else
3966 outs() << format("0x%" PRIx64, i.offset);
3967 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
3968 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
3969 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
3970 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3971 sys::swapByteOrder(ivar_offset);
3972 outs() << " " << ivar_offset << "\n";
3973 } else
3974 outs() << "\n";
3975
3976 outs() << "\t\t\t name ";
3977 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
3978 n_value, i.name);
3979 if (n_value != 0) {
3980 if (info->verbose && sym_name != nullptr)
3981 outs() << sym_name;
3982 else
3983 outs() << format("0x%" PRIx64, n_value);
3984 if (i.name != 0)
3985 outs() << " + " << format("0x%" PRIx64, i.name);
3986 } else
3987 outs() << format("0x%" PRIx64, i.name);
3988 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
3989 if (name != nullptr)
3990 outs() << format(" %.*s", left, name);
3991 outs() << "\n";
3992
3993 outs() << "\t\t\t type ";
3994 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
3995 n_value, i.name);
3996 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
3997 if (n_value != 0) {
3998 if (info->verbose && sym_name != nullptr)
3999 outs() << sym_name;
4000 else
4001 outs() << format("0x%" PRIx64, n_value);
4002 if (i.type != 0)
4003 outs() << " + " << format("0x%" PRIx64, i.type);
4004 } else
4005 outs() << format("0x%" PRIx64, i.type);
4006 if (name != nullptr)
4007 outs() << format(" %.*s", left, name);
4008 outs() << "\n";
4009
4010 outs() << "\t\t\talignment " << i.alignment << "\n";
4011 outs() << "\t\t\t size " << i.size << "\n";
4012
4013 p += sizeof(struct ivar64_t);
4014 offset += sizeof(struct ivar64_t);
4015 }
4016}
4017
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004018static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4019 struct ivar_list32_t il;
4020 struct ivar32_t i;
4021 const char *r;
4022 uint32_t offset, xoffset, left, j;
4023 SectionRef S, xS;
4024 const char *name, *ivar_offset_p;
4025 uint32_t ivar_offset;
4026
4027 r = get_pointer_32(p, offset, left, S, info);
4028 if (r == nullptr)
4029 return;
4030 memset(&il, '\0', sizeof(struct ivar_list32_t));
4031 if (left < sizeof(struct ivar_list32_t)) {
4032 memcpy(&il, r, left);
4033 outs() << " (ivar_list_t entends past the end of the section)\n";
4034 } else
4035 memcpy(&il, r, sizeof(struct ivar_list32_t));
4036 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4037 swapStruct(il);
4038 outs() << " entsize " << il.entsize << "\n";
4039 outs() << " count " << il.count << "\n";
4040
4041 p += sizeof(struct ivar_list32_t);
4042 offset += sizeof(struct ivar_list32_t);
4043 for (j = 0; j < il.count; j++) {
4044 r = get_pointer_32(p, offset, left, S, info);
4045 if (r == nullptr)
4046 return;
4047 memset(&i, '\0', sizeof(struct ivar32_t));
4048 if (left < sizeof(struct ivar32_t)) {
4049 memcpy(&i, r, left);
4050 outs() << " (ivar_t entends past the end of the section)\n";
4051 } else
4052 memcpy(&i, r, sizeof(struct ivar32_t));
4053 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4054 swapStruct(i);
4055
4056 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4057 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4058 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4059 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4060 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4061 sys::swapByteOrder(ivar_offset);
4062 outs() << " " << ivar_offset << "\n";
4063 } else
4064 outs() << "\n";
4065
4066 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4067 name = get_pointer_32(i.name, xoffset, left, xS, info);
4068 if (name != nullptr)
4069 outs() << format(" %.*s", left, name);
4070 outs() << "\n";
4071
4072 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4073 name = get_pointer_32(i.type, xoffset, left, xS, info);
4074 if (name != nullptr)
4075 outs() << format(" %.*s", left, name);
4076 outs() << "\n";
4077
4078 outs() << "\t\t\talignment " << i.alignment << "\n";
4079 outs() << "\t\t\t size " << i.size << "\n";
4080
4081 p += sizeof(struct ivar32_t);
4082 offset += sizeof(struct ivar32_t);
4083 }
4084}
4085
Kevin Enderby0fc11822015-04-01 20:57:01 +00004086static void print_objc_property_list64(uint64_t p,
4087 struct DisassembleInfo *info) {
4088 struct objc_property_list64 opl;
4089 struct objc_property64 op;
4090 const char *r;
4091 uint32_t offset, xoffset, left, j;
4092 SectionRef S, xS;
4093 const char *name, *sym_name;
4094 uint64_t n_value;
4095
4096 r = get_pointer_64(p, offset, left, S, info);
4097 if (r == nullptr)
4098 return;
4099 memset(&opl, '\0', sizeof(struct objc_property_list64));
4100 if (left < sizeof(struct objc_property_list64)) {
4101 memcpy(&opl, r, left);
4102 outs() << " (objc_property_list entends past the end of the section)\n";
4103 } else
4104 memcpy(&opl, r, sizeof(struct objc_property_list64));
4105 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4106 swapStruct(opl);
4107 outs() << " entsize " << opl.entsize << "\n";
4108 outs() << " count " << opl.count << "\n";
4109
4110 p += sizeof(struct objc_property_list64);
4111 offset += sizeof(struct objc_property_list64);
4112 for (j = 0; j < opl.count; j++) {
4113 r = get_pointer_64(p, offset, left, S, info);
4114 if (r == nullptr)
4115 return;
4116 memset(&op, '\0', sizeof(struct objc_property64));
4117 if (left < sizeof(struct objc_property64)) {
4118 memcpy(&op, r, left);
4119 outs() << " (objc_property entends past the end of the section)\n";
4120 } else
4121 memcpy(&op, r, sizeof(struct objc_property64));
4122 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4123 swapStruct(op);
4124
4125 outs() << "\t\t\t name ";
4126 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4127 info, n_value, op.name);
4128 if (n_value != 0) {
4129 if (info->verbose && sym_name != nullptr)
4130 outs() << sym_name;
4131 else
4132 outs() << format("0x%" PRIx64, n_value);
4133 if (op.name != 0)
4134 outs() << " + " << format("0x%" PRIx64, op.name);
4135 } else
4136 outs() << format("0x%" PRIx64, op.name);
4137 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4138 if (name != nullptr)
4139 outs() << format(" %.*s", left, name);
4140 outs() << "\n";
4141
4142 outs() << "\t\t\tattributes ";
4143 sym_name =
4144 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4145 info, n_value, op.attributes);
4146 if (n_value != 0) {
4147 if (info->verbose && sym_name != nullptr)
4148 outs() << sym_name;
4149 else
4150 outs() << format("0x%" PRIx64, n_value);
4151 if (op.attributes != 0)
4152 outs() << " + " << format("0x%" PRIx64, op.attributes);
4153 } else
4154 outs() << format("0x%" PRIx64, op.attributes);
4155 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4156 if (name != nullptr)
4157 outs() << format(" %.*s", left, name);
4158 outs() << "\n";
4159
4160 p += sizeof(struct objc_property64);
4161 offset += sizeof(struct objc_property64);
4162 }
4163}
4164
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004165static void print_objc_property_list32(uint32_t p,
4166 struct DisassembleInfo *info) {
4167 struct objc_property_list32 opl;
4168 struct objc_property32 op;
4169 const char *r;
4170 uint32_t offset, xoffset, left, j;
4171 SectionRef S, xS;
4172 const char *name;
4173
4174 r = get_pointer_32(p, offset, left, S, info);
4175 if (r == nullptr)
4176 return;
4177 memset(&opl, '\0', sizeof(struct objc_property_list32));
4178 if (left < sizeof(struct objc_property_list32)) {
4179 memcpy(&opl, r, left);
4180 outs() << " (objc_property_list entends past the end of the section)\n";
4181 } else
4182 memcpy(&opl, r, sizeof(struct objc_property_list32));
4183 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4184 swapStruct(opl);
4185 outs() << " entsize " << opl.entsize << "\n";
4186 outs() << " count " << opl.count << "\n";
4187
4188 p += sizeof(struct objc_property_list32);
4189 offset += sizeof(struct objc_property_list32);
4190 for (j = 0; j < opl.count; j++) {
4191 r = get_pointer_32(p, offset, left, S, info);
4192 if (r == nullptr)
4193 return;
4194 memset(&op, '\0', sizeof(struct objc_property32));
4195 if (left < sizeof(struct objc_property32)) {
4196 memcpy(&op, r, left);
4197 outs() << " (objc_property entends past the end of the section)\n";
4198 } else
4199 memcpy(&op, r, sizeof(struct objc_property32));
4200 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4201 swapStruct(op);
4202
4203 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4204 name = get_pointer_32(op.name, xoffset, left, xS, info);
4205 if (name != nullptr)
4206 outs() << format(" %.*s", left, name);
4207 outs() << "\n";
4208
4209 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4210 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4211 if (name != nullptr)
4212 outs() << format(" %.*s", left, name);
4213 outs() << "\n";
4214
4215 p += sizeof(struct objc_property32);
4216 offset += sizeof(struct objc_property32);
4217 }
4218}
4219
Kevin Enderby0fc11822015-04-01 20:57:01 +00004220static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4221 bool &is_meta_class) {
4222 struct class_ro64_t cro;
4223 const char *r;
4224 uint32_t offset, xoffset, left;
4225 SectionRef S, xS;
4226 const char *name, *sym_name;
4227 uint64_t n_value;
4228
4229 r = get_pointer_64(p, offset, left, S, info);
4230 if (r == nullptr || left < sizeof(struct class_ro64_t))
4231 return;
4232 memset(&cro, '\0', sizeof(struct class_ro64_t));
4233 if (left < sizeof(struct class_ro64_t)) {
4234 memcpy(&cro, r, left);
4235 outs() << " (class_ro_t entends past the end of the section)\n";
4236 } else
4237 memcpy(&cro, r, sizeof(struct class_ro64_t));
4238 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4239 swapStruct(cro);
4240 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4241 if (cro.flags & RO_META)
4242 outs() << " RO_META";
4243 if (cro.flags & RO_ROOT)
4244 outs() << " RO_ROOT";
4245 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4246 outs() << " RO_HAS_CXX_STRUCTORS";
4247 outs() << "\n";
4248 outs() << " instanceStart " << cro.instanceStart << "\n";
4249 outs() << " instanceSize " << cro.instanceSize << "\n";
4250 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4251 << "\n";
4252 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4253 << "\n";
4254 print_layout_map64(cro.ivarLayout, info);
4255
4256 outs() << " name ";
4257 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4258 info, n_value, cro.name);
4259 if (n_value != 0) {
4260 if (info->verbose && sym_name != nullptr)
4261 outs() << sym_name;
4262 else
4263 outs() << format("0x%" PRIx64, n_value);
4264 if (cro.name != 0)
4265 outs() << " + " << format("0x%" PRIx64, cro.name);
4266 } else
4267 outs() << format("0x%" PRIx64, cro.name);
4268 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4269 if (name != nullptr)
4270 outs() << format(" %.*s", left, name);
4271 outs() << "\n";
4272
4273 outs() << " baseMethods ";
4274 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4275 S, info, n_value, cro.baseMethods);
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.baseMethods != 0)
4282 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4283 } else
4284 outs() << format("0x%" PRIx64, cro.baseMethods);
4285 outs() << " (struct method_list_t *)\n";
4286 if (cro.baseMethods + n_value != 0)
4287 print_method_list64_t(cro.baseMethods + n_value, info, "");
4288
4289 outs() << " baseProtocols ";
4290 sym_name =
4291 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4292 info, n_value, cro.baseProtocols);
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.baseProtocols != 0)
4299 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4300 } else
4301 outs() << format("0x%" PRIx64, cro.baseProtocols);
4302 outs() << "\n";
4303 if (cro.baseProtocols + n_value != 0)
4304 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4305
4306 outs() << " ivars ";
4307 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004308 info, n_value, cro.ivars);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004309 if (n_value != 0) {
4310 if (info->verbose && sym_name != nullptr)
4311 outs() << sym_name;
4312 else
4313 outs() << format("0x%" PRIx64, n_value);
4314 if (cro.ivars != 0)
4315 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4316 } else
4317 outs() << format("0x%" PRIx64, cro.ivars);
4318 outs() << "\n";
4319 if (cro.ivars + n_value != 0)
4320 print_ivar_list64_t(cro.ivars + n_value, info);
4321
4322 outs() << " weakIvarLayout ";
4323 sym_name =
4324 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4325 info, n_value, cro.weakIvarLayout);
4326 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.weakIvarLayout != 0)
4332 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4333 } else
4334 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4335 outs() << "\n";
4336 print_layout_map64(cro.weakIvarLayout + n_value, info);
4337
4338 outs() << " baseProperties ";
4339 sym_name =
4340 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4341 info, n_value, cro.baseProperties);
4342 if (n_value != 0) {
4343 if (info->verbose && sym_name != nullptr)
4344 outs() << sym_name;
4345 else
4346 outs() << format("0x%" PRIx64, n_value);
4347 if (cro.baseProperties != 0)
4348 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4349 } else
4350 outs() << format("0x%" PRIx64, cro.baseProperties);
4351 outs() << "\n";
4352 if (cro.baseProperties + n_value != 0)
4353 print_objc_property_list64(cro.baseProperties + n_value, info);
4354
4355 is_meta_class = (cro.flags & RO_META) ? true : false;
4356}
4357
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004358static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4359 bool &is_meta_class) {
4360 struct class_ro32_t cro;
4361 const char *r;
4362 uint32_t offset, xoffset, left;
4363 SectionRef S, xS;
4364 const char *name;
4365
4366 r = get_pointer_32(p, offset, left, S, info);
4367 if (r == nullptr)
4368 return;
4369 memset(&cro, '\0', sizeof(struct class_ro32_t));
4370 if (left < sizeof(struct class_ro32_t)) {
4371 memcpy(&cro, r, left);
4372 outs() << " (class_ro_t entends past the end of the section)\n";
4373 } else
4374 memcpy(&cro, r, sizeof(struct class_ro32_t));
4375 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4376 swapStruct(cro);
4377 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4378 if (cro.flags & RO_META)
4379 outs() << " RO_META";
4380 if (cro.flags & RO_ROOT)
4381 outs() << " RO_ROOT";
4382 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4383 outs() << " RO_HAS_CXX_STRUCTORS";
4384 outs() << "\n";
4385 outs() << " instanceStart " << cro.instanceStart << "\n";
4386 outs() << " instanceSize " << cro.instanceSize << "\n";
4387 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4388 << "\n";
4389 print_layout_map32(cro.ivarLayout, info);
4390
4391 outs() << " name " << format("0x%" PRIx32, cro.name);
4392 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4393 if (name != nullptr)
4394 outs() << format(" %.*s", left, name);
4395 outs() << "\n";
4396
4397 outs() << " baseMethods "
4398 << format("0x%" PRIx32, cro.baseMethods)
4399 << " (struct method_list_t *)\n";
4400 if (cro.baseMethods != 0)
4401 print_method_list32_t(cro.baseMethods, info, "");
4402
4403 outs() << " baseProtocols "
4404 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4405 if (cro.baseProtocols != 0)
4406 print_protocol_list32_t(cro.baseProtocols, info);
4407 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4408 << "\n";
4409 if (cro.ivars != 0)
4410 print_ivar_list32_t(cro.ivars, info);
4411 outs() << " weakIvarLayout "
4412 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4413 print_layout_map32(cro.weakIvarLayout, info);
4414 outs() << " baseProperties "
4415 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4416 if (cro.baseProperties != 0)
4417 print_objc_property_list32(cro.baseProperties, info);
4418 is_meta_class = (cro.flags & RO_META) ? true : false;
4419}
4420
Kevin Enderby0fc11822015-04-01 20:57:01 +00004421static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4422 struct class64_t c;
4423 const char *r;
4424 uint32_t offset, left;
4425 SectionRef S;
4426 const char *name;
4427 uint64_t isa_n_value, n_value;
4428
4429 r = get_pointer_64(p, offset, left, S, info);
4430 if (r == nullptr || left < sizeof(struct class64_t))
4431 return;
4432 memset(&c, '\0', sizeof(struct class64_t));
4433 if (left < sizeof(struct class64_t)) {
4434 memcpy(&c, r, left);
4435 outs() << " (class_t entends past the end of the section)\n";
4436 } else
4437 memcpy(&c, r, sizeof(struct class64_t));
4438 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4439 swapStruct(c);
4440
4441 outs() << " isa " << format("0x%" PRIx64, c.isa);
4442 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4443 isa_n_value, c.isa);
4444 if (name != nullptr)
4445 outs() << " " << name;
4446 outs() << "\n";
4447
4448 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4449 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4450 n_value, c.superclass);
4451 if (name != nullptr)
4452 outs() << " " << name;
4453 outs() << "\n";
4454
4455 outs() << " cache " << format("0x%" PRIx64, c.cache);
4456 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4457 n_value, c.cache);
4458 if (name != nullptr)
4459 outs() << " " << name;
4460 outs() << "\n";
4461
4462 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4463 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4464 n_value, c.vtable);
4465 if (name != nullptr)
4466 outs() << " " << name;
4467 outs() << "\n";
4468
4469 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4470 n_value, c.data);
4471 outs() << " data ";
4472 if (n_value != 0) {
4473 if (info->verbose && name != nullptr)
4474 outs() << name;
4475 else
4476 outs() << format("0x%" PRIx64, n_value);
4477 if (c.data != 0)
4478 outs() << " + " << format("0x%" PRIx64, c.data);
4479 } else
4480 outs() << format("0x%" PRIx64, c.data);
4481 outs() << " (struct class_ro_t *)";
4482
4483 // This is a Swift class if some of the low bits of the pointer are set.
4484 if ((c.data + n_value) & 0x7)
4485 outs() << " Swift class";
4486 outs() << "\n";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004487 bool is_meta_class;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004488 print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class);
4489
4490 if (is_meta_class == false) {
4491 outs() << "Meta Class\n";
4492 print_class64_t(c.isa + isa_n_value, info);
4493 }
4494}
4495
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004496static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4497 struct class32_t c;
4498 const char *r;
4499 uint32_t offset, left;
4500 SectionRef S;
4501 const char *name;
4502
4503 r = get_pointer_32(p, offset, left, S, info);
4504 if (r == nullptr)
4505 return;
4506 memset(&c, '\0', sizeof(struct class32_t));
4507 if (left < sizeof(struct class32_t)) {
4508 memcpy(&c, r, left);
4509 outs() << " (class_t entends past the end of the section)\n";
4510 } else
4511 memcpy(&c, r, sizeof(struct class32_t));
4512 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4513 swapStruct(c);
4514
4515 outs() << " isa " << format("0x%" PRIx32, c.isa);
4516 name =
4517 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4518 if (name != nullptr)
4519 outs() << " " << name;
4520 outs() << "\n";
4521
4522 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4523 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4524 c.superclass);
4525 if (name != nullptr)
4526 outs() << " " << name;
4527 outs() << "\n";
4528
4529 outs() << " cache " << format("0x%" PRIx32, c.cache);
4530 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4531 c.cache);
4532 if (name != nullptr)
4533 outs() << " " << name;
4534 outs() << "\n";
4535
4536 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4537 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4538 c.vtable);
4539 if (name != nullptr)
4540 outs() << " " << name;
4541 outs() << "\n";
4542
4543 name =
4544 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4545 outs() << " data " << format("0x%" PRIx32, c.data)
4546 << " (struct class_ro_t *)";
4547
4548 // This is a Swift class if some of the low bits of the pointer are set.
4549 if (c.data & 0x3)
4550 outs() << " Swift class";
4551 outs() << "\n";
4552 bool is_meta_class;
4553 print_class_ro32_t(c.data & ~0x3, info, is_meta_class);
4554
4555 if (is_meta_class == false) {
4556 outs() << "Meta Class\n";
4557 print_class32_t(c.isa, info);
4558 }
4559}
4560
Kevin Enderby846c0002015-04-16 17:19:59 +00004561static void print_objc_class_t(struct objc_class_t *objc_class,
4562 struct DisassembleInfo *info) {
4563 uint32_t offset, left, xleft;
4564 const char *name, *p, *ivar_list;
4565 SectionRef S;
4566 int32_t i;
4567 struct objc_ivar_list_t objc_ivar_list;
4568 struct objc_ivar_t ivar;
4569
4570 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4571 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4572 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4573 if (name != nullptr)
4574 outs() << format(" %.*s", left, name);
4575 else
4576 outs() << " (not in an __OBJC section)";
4577 }
4578 outs() << "\n";
4579
4580 outs() << "\t super_class "
4581 << format("0x%08" PRIx32, objc_class->super_class);
4582 if (info->verbose) {
4583 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4584 if (name != nullptr)
4585 outs() << format(" %.*s", left, name);
4586 else
4587 outs() << " (not in an __OBJC section)";
4588 }
4589 outs() << "\n";
4590
4591 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4592 if (info->verbose) {
4593 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4594 if (name != nullptr)
4595 outs() << format(" %.*s", left, name);
4596 else
4597 outs() << " (not in an __OBJC section)";
4598 }
4599 outs() << "\n";
4600
4601 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4602 << "\n";
4603
4604 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4605 if (info->verbose) {
4606 if (CLS_GETINFO(objc_class, CLS_CLASS))
4607 outs() << " CLS_CLASS";
4608 else if (CLS_GETINFO(objc_class, CLS_META))
4609 outs() << " CLS_META";
4610 }
4611 outs() << "\n";
4612
4613 outs() << "\t instance_size "
4614 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4615
4616 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4617 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4618 if (p != nullptr) {
4619 if (left > sizeof(struct objc_ivar_list_t)) {
4620 outs() << "\n";
4621 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4622 } else {
4623 outs() << " (entends past the end of the section)\n";
4624 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4625 memcpy(&objc_ivar_list, p, left);
4626 }
4627 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4628 swapStruct(objc_ivar_list);
4629 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4630 ivar_list = p + sizeof(struct objc_ivar_list_t);
4631 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4632 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4633 outs() << "\t\t remaining ivar's extend past the of the section\n";
4634 break;
4635 }
4636 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4637 sizeof(struct objc_ivar_t));
4638 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4639 swapStruct(ivar);
4640
4641 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4642 if (info->verbose) {
4643 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4644 if (name != nullptr)
4645 outs() << format(" %.*s", xleft, name);
4646 else
4647 outs() << " (not in an __OBJC section)";
4648 }
4649 outs() << "\n";
4650
4651 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4652 if (info->verbose) {
4653 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4654 if (name != nullptr)
4655 outs() << format(" %.*s", xleft, name);
4656 else
4657 outs() << " (not in an __OBJC section)";
4658 }
4659 outs() << "\n";
4660
4661 outs() << "\t\t ivar_offset "
4662 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4663 }
4664 } else {
4665 outs() << " (not in an __OBJC section)\n";
4666 }
4667
4668 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4669 if (print_method_list(objc_class->methodLists, info))
4670 outs() << " (not in an __OBJC section)\n";
4671
4672 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4673 << "\n";
4674
4675 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4676 if (print_protocol_list(objc_class->protocols, 16, info))
4677 outs() << " (not in an __OBJC section)\n";
4678}
4679
4680static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4681 struct DisassembleInfo *info) {
4682 uint32_t offset, left;
4683 const char *name;
4684 SectionRef S;
4685
4686 outs() << "\t category name "
4687 << format("0x%08" PRIx32, objc_category->category_name);
4688 if (info->verbose) {
4689 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4690 true);
4691 if (name != nullptr)
4692 outs() << format(" %.*s", left, name);
4693 else
4694 outs() << " (not in an __OBJC section)";
4695 }
4696 outs() << "\n";
4697
4698 outs() << "\t\t class name "
4699 << format("0x%08" PRIx32, objc_category->class_name);
4700 if (info->verbose) {
4701 name =
4702 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4703 if (name != nullptr)
4704 outs() << format(" %.*s", left, name);
4705 else
4706 outs() << " (not in an __OBJC section)";
4707 }
4708 outs() << "\n";
4709
4710 outs() << "\t instance methods "
4711 << format("0x%08" PRIx32, objc_category->instance_methods);
4712 if (print_method_list(objc_category->instance_methods, info))
4713 outs() << " (not in an __OBJC section)\n";
4714
4715 outs() << "\t class methods "
4716 << format("0x%08" PRIx32, objc_category->class_methods);
4717 if (print_method_list(objc_category->class_methods, info))
4718 outs() << " (not in an __OBJC section)\n";
4719}
4720
Kevin Enderby0fc11822015-04-01 20:57:01 +00004721static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4722 struct category64_t c;
4723 const char *r;
4724 uint32_t offset, xoffset, left;
4725 SectionRef S, xS;
4726 const char *name, *sym_name;
4727 uint64_t n_value;
4728
4729 r = get_pointer_64(p, offset, left, S, info);
4730 if (r == nullptr)
4731 return;
4732 memset(&c, '\0', sizeof(struct category64_t));
4733 if (left < sizeof(struct category64_t)) {
4734 memcpy(&c, r, left);
4735 outs() << " (category_t entends past the end of the section)\n";
4736 } else
4737 memcpy(&c, r, sizeof(struct category64_t));
4738 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4739 swapStruct(c);
4740
4741 outs() << " name ";
4742 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4743 info, n_value, c.name);
4744 if (n_value != 0) {
4745 if (info->verbose && sym_name != nullptr)
4746 outs() << sym_name;
4747 else
4748 outs() << format("0x%" PRIx64, n_value);
4749 if (c.name != 0)
4750 outs() << " + " << format("0x%" PRIx64, c.name);
4751 } else
4752 outs() << format("0x%" PRIx64, c.name);
4753 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4754 if (name != nullptr)
4755 outs() << format(" %.*s", left, name);
4756 outs() << "\n";
4757
4758 outs() << " cls ";
4759 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4760 n_value, c.cls);
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.cls != 0)
4767 outs() << " + " << format("0x%" PRIx64, c.cls);
4768 } else
4769 outs() << format("0x%" PRIx64, c.cls);
4770 outs() << "\n";
4771 if (c.cls + n_value != 0)
4772 print_class64_t(c.cls + n_value, info);
4773
4774 outs() << " instanceMethods ";
4775 sym_name =
4776 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4777 info, n_value, c.instanceMethods);
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.instanceMethods != 0)
4784 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4785 } else
4786 outs() << format("0x%" PRIx64, c.instanceMethods);
4787 outs() << "\n";
4788 if (c.instanceMethods + n_value != 0)
4789 print_method_list64_t(c.instanceMethods + n_value, info, "");
4790
4791 outs() << " classMethods ";
4792 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4793 S, info, n_value, c.classMethods);
4794 if (n_value != 0) {
4795 if (info->verbose && sym_name != nullptr)
4796 outs() << sym_name;
4797 else
4798 outs() << format("0x%" PRIx64, n_value);
4799 if (c.classMethods != 0)
4800 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4801 } else
4802 outs() << format("0x%" PRIx64, c.classMethods);
4803 outs() << "\n";
4804 if (c.classMethods + n_value != 0)
4805 print_method_list64_t(c.classMethods + n_value, info, "");
4806
4807 outs() << " protocols ";
4808 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4809 info, n_value, c.protocols);
4810 if (n_value != 0) {
4811 if (info->verbose && sym_name != nullptr)
4812 outs() << sym_name;
4813 else
4814 outs() << format("0x%" PRIx64, n_value);
4815 if (c.protocols != 0)
4816 outs() << " + " << format("0x%" PRIx64, c.protocols);
4817 } else
4818 outs() << format("0x%" PRIx64, c.protocols);
4819 outs() << "\n";
4820 if (c.protocols + n_value != 0)
4821 print_protocol_list64_t(c.protocols + n_value, info);
4822
4823 outs() << "instanceProperties ";
4824 sym_name =
4825 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4826 S, info, n_value, c.instanceProperties);
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.instanceProperties != 0)
4833 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4834 } else
4835 outs() << format("0x%" PRIx64, c.instanceProperties);
4836 outs() << "\n";
4837 if (c.instanceProperties + n_value != 0)
4838 print_objc_property_list64(c.instanceProperties + n_value, info);
4839}
4840
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004841static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4842 struct category32_t c;
4843 const char *r;
4844 uint32_t offset, left;
4845 SectionRef S, xS;
4846 const char *name;
4847
4848 r = get_pointer_32(p, offset, left, S, info);
4849 if (r == nullptr)
4850 return;
4851 memset(&c, '\0', sizeof(struct category32_t));
4852 if (left < sizeof(struct category32_t)) {
4853 memcpy(&c, r, left);
4854 outs() << " (category_t entends past the end of the section)\n";
4855 } else
4856 memcpy(&c, r, sizeof(struct category32_t));
4857 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4858 swapStruct(c);
4859
4860 outs() << " name " << format("0x%" PRIx32, c.name);
4861 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4862 c.name);
4863 if (name != NULL)
4864 outs() << " " << name;
4865 outs() << "\n";
4866
4867 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
4868 if (c.cls != 0)
4869 print_class32_t(c.cls, info);
4870 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4871 << "\n";
4872 if (c.instanceMethods != 0)
4873 print_method_list32_t(c.instanceMethods, info, "");
4874 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
4875 << "\n";
4876 if (c.classMethods != 0)
4877 print_method_list32_t(c.classMethods, info, "");
4878 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
4879 if (c.protocols != 0)
4880 print_protocol_list32_t(c.protocols, info);
4881 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
4882 << "\n";
4883 if (c.instanceProperties != 0)
4884 print_objc_property_list32(c.instanceProperties, info);
4885}
4886
Kevin Enderby0fc11822015-04-01 20:57:01 +00004887static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
4888 uint32_t i, left, offset, xoffset;
4889 uint64_t p, n_value;
4890 struct message_ref64 mr;
4891 const char *name, *sym_name;
4892 const char *r;
4893 SectionRef xS;
4894
4895 if (S == SectionRef())
4896 return;
4897
4898 StringRef SectName;
4899 S.getName(SectName);
4900 DataRefImpl Ref = S.getRawDataRefImpl();
4901 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4902 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4903 offset = 0;
4904 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4905 p = S.getAddress() + i;
4906 r = get_pointer_64(p, offset, left, S, info);
4907 if (r == nullptr)
4908 return;
4909 memset(&mr, '\0', sizeof(struct message_ref64));
4910 if (left < sizeof(struct message_ref64)) {
4911 memcpy(&mr, r, left);
4912 outs() << " (message_ref entends past the end of the section)\n";
4913 } else
4914 memcpy(&mr, r, sizeof(struct message_ref64));
4915 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4916 swapStruct(mr);
4917
4918 outs() << " imp ";
4919 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
4920 n_value, mr.imp);
4921 if (n_value != 0) {
4922 outs() << format("0x%" PRIx64, n_value) << " ";
4923 if (mr.imp != 0)
4924 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
4925 } else
4926 outs() << format("0x%" PRIx64, mr.imp) << " ";
4927 if (name != nullptr)
4928 outs() << " " << name;
4929 outs() << "\n";
4930
4931 outs() << " sel ";
4932 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
4933 info, n_value, mr.sel);
4934 if (n_value != 0) {
4935 if (info->verbose && sym_name != nullptr)
4936 outs() << sym_name;
4937 else
4938 outs() << format("0x%" PRIx64, n_value);
4939 if (mr.sel != 0)
4940 outs() << " + " << format("0x%" PRIx64, mr.sel);
4941 } else
4942 outs() << format("0x%" PRIx64, mr.sel);
4943 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
4944 if (name != nullptr)
4945 outs() << format(" %.*s", left, name);
4946 outs() << "\n";
4947
4948 offset += sizeof(struct message_ref64);
4949 }
4950}
4951
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004952static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
4953 uint32_t i, left, offset, xoffset, p;
4954 struct message_ref32 mr;
4955 const char *name, *r;
4956 SectionRef xS;
4957
4958 if (S == SectionRef())
4959 return;
4960
4961 StringRef SectName;
4962 S.getName(SectName);
4963 DataRefImpl Ref = S.getRawDataRefImpl();
4964 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4965 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4966 offset = 0;
4967 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4968 p = S.getAddress() + i;
4969 r = get_pointer_32(p, offset, left, S, info);
4970 if (r == nullptr)
4971 return;
4972 memset(&mr, '\0', sizeof(struct message_ref32));
4973 if (left < sizeof(struct message_ref32)) {
4974 memcpy(&mr, r, left);
4975 outs() << " (message_ref entends past the end of the section)\n";
4976 } else
4977 memcpy(&mr, r, sizeof(struct message_ref32));
4978 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4979 swapStruct(mr);
4980
4981 outs() << " imp " << format("0x%" PRIx32, mr.imp);
4982 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
4983 mr.imp);
4984 if (name != nullptr)
4985 outs() << " " << name;
4986 outs() << "\n";
4987
4988 outs() << " sel " << format("0x%" PRIx32, mr.sel);
4989 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
4990 if (name != nullptr)
4991 outs() << " " << name;
4992 outs() << "\n";
4993
4994 offset += sizeof(struct message_ref32);
4995 }
4996}
4997
Kevin Enderby0fc11822015-04-01 20:57:01 +00004998static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
4999 uint32_t left, offset, swift_version;
5000 uint64_t p;
5001 struct objc_image_info64 o;
5002 const char *r;
5003
5004 StringRef SectName;
5005 S.getName(SectName);
5006 DataRefImpl Ref = S.getRawDataRefImpl();
5007 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5008 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5009 p = S.getAddress();
5010 r = get_pointer_64(p, offset, left, S, info);
5011 if (r == nullptr)
5012 return;
5013 memset(&o, '\0', sizeof(struct objc_image_info64));
5014 if (left < sizeof(struct objc_image_info64)) {
5015 memcpy(&o, r, left);
5016 outs() << " (objc_image_info entends past the end of the section)\n";
5017 } else
5018 memcpy(&o, r, sizeof(struct objc_image_info64));
5019 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5020 swapStruct(o);
5021 outs() << " version " << o.version << "\n";
5022 outs() << " flags " << format("0x%" PRIx32, o.flags);
5023 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5024 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5025 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5026 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5027 swift_version = (o.flags >> 8) & 0xff;
5028 if (swift_version != 0) {
5029 if (swift_version == 1)
5030 outs() << " Swift 1.0";
5031 else if (swift_version == 2)
5032 outs() << " Swift 1.1";
5033 else
5034 outs() << " unknown future Swift version (" << swift_version << ")";
5035 }
5036 outs() << "\n";
5037}
5038
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005039static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5040 uint32_t left, offset, swift_version, p;
5041 struct objc_image_info32 o;
5042 const char *r;
5043
5044 StringRef SectName;
5045 S.getName(SectName);
5046 DataRefImpl Ref = S.getRawDataRefImpl();
5047 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5048 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5049 p = S.getAddress();
5050 r = get_pointer_32(p, offset, left, S, info);
5051 if (r == nullptr)
5052 return;
5053 memset(&o, '\0', sizeof(struct objc_image_info32));
5054 if (left < sizeof(struct objc_image_info32)) {
5055 memcpy(&o, r, left);
5056 outs() << " (objc_image_info entends past the end of the section)\n";
5057 } else
5058 memcpy(&o, r, sizeof(struct objc_image_info32));
5059 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5060 swapStruct(o);
5061 outs() << " version " << o.version << "\n";
5062 outs() << " flags " << format("0x%" PRIx32, o.flags);
5063 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5064 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5065 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5066 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5067 swift_version = (o.flags >> 8) & 0xff;
5068 if (swift_version != 0) {
5069 if (swift_version == 1)
5070 outs() << " Swift 1.0";
5071 else if (swift_version == 2)
5072 outs() << " Swift 1.1";
5073 else
5074 outs() << " unknown future Swift version (" << swift_version << ")";
5075 }
5076 outs() << "\n";
5077}
5078
Kevin Enderby846c0002015-04-16 17:19:59 +00005079static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5080 uint32_t left, offset, p;
5081 struct imageInfo_t o;
5082 const char *r;
5083
5084 StringRef SectName;
5085 S.getName(SectName);
5086 DataRefImpl Ref = S.getRawDataRefImpl();
5087 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5088 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5089 p = S.getAddress();
5090 r = get_pointer_32(p, offset, left, S, info);
5091 if (r == nullptr)
5092 return;
5093 memset(&o, '\0', sizeof(struct imageInfo_t));
5094 if (left < sizeof(struct imageInfo_t)) {
5095 memcpy(&o, r, left);
5096 outs() << " (imageInfo entends past the end of the section)\n";
5097 } else
5098 memcpy(&o, r, sizeof(struct imageInfo_t));
5099 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5100 swapStruct(o);
5101 outs() << " version " << o.version << "\n";
5102 outs() << " flags " << format("0x%" PRIx32, o.flags);
5103 if (o.flags & 0x1)
5104 outs() << " F&C";
5105 if (o.flags & 0x2)
5106 outs() << " GC";
5107 if (o.flags & 0x4)
5108 outs() << " GC-only";
5109 else
5110 outs() << " RR";
5111 outs() << "\n";
5112}
5113
Kevin Enderby0fc11822015-04-01 20:57:01 +00005114static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5115 SymbolAddressMap AddrMap;
5116 if (verbose)
5117 CreateSymbolAddressMap(O, &AddrMap);
5118
5119 std::vector<SectionRef> Sections;
5120 for (const SectionRef &Section : O->sections()) {
5121 StringRef SectName;
5122 Section.getName(SectName);
5123 Sections.push_back(Section);
5124 }
5125
5126 struct DisassembleInfo info;
5127 // Set up the block of info used by the Symbolizer call backs.
5128 info.verbose = verbose;
5129 info.O = O;
5130 info.AddrMap = &AddrMap;
5131 info.Sections = &Sections;
5132 info.class_name = nullptr;
5133 info.selector_name = nullptr;
5134 info.method = nullptr;
5135 info.demangled_name = nullptr;
5136 info.bindtable = nullptr;
5137 info.adrp_addr = 0;
5138 info.adrp_inst = 0;
5139
5140 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5141 if (CL != SectionRef()) {
5142 info.S = CL;
5143 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5144 } else {
5145 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5146 info.S = CL;
5147 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5148 }
5149
5150 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5151 if (CR != SectionRef()) {
5152 info.S = CR;
5153 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5154 } else {
5155 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5156 info.S = CR;
5157 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5158 }
5159
5160 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5161 if (SR != SectionRef()) {
5162 info.S = SR;
5163 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5164 } else {
5165 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5166 info.S = SR;
5167 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5168 }
5169
5170 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5171 if (CA != SectionRef()) {
5172 info.S = CA;
5173 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5174 } else {
5175 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5176 info.S = CA;
5177 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5178 }
5179
5180 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5181 if (PL != SectionRef()) {
5182 info.S = PL;
5183 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5184 } else {
5185 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5186 info.S = PL;
5187 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5188 }
5189
5190 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5191 if (MR != SectionRef()) {
5192 info.S = MR;
5193 print_message_refs64(MR, &info);
5194 } else {
5195 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5196 info.S = MR;
5197 print_message_refs64(MR, &info);
5198 }
5199
5200 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5201 if (II != SectionRef()) {
5202 info.S = II;
5203 print_image_info64(II, &info);
5204 } else {
5205 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5206 info.S = II;
5207 print_image_info64(II, &info);
5208 }
Kevin Enderby0bc6ed42015-04-01 21:50:45 +00005209
5210 if (info.bindtable != nullptr)
5211 delete info.bindtable;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005212}
5213
5214static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005215 SymbolAddressMap AddrMap;
5216 if (verbose)
5217 CreateSymbolAddressMap(O, &AddrMap);
5218
5219 std::vector<SectionRef> Sections;
5220 for (const SectionRef &Section : O->sections()) {
5221 StringRef SectName;
5222 Section.getName(SectName);
5223 Sections.push_back(Section);
5224 }
5225
5226 struct DisassembleInfo info;
5227 // Set up the block of info used by the Symbolizer call backs.
5228 info.verbose = verbose;
5229 info.O = O;
5230 info.AddrMap = &AddrMap;
5231 info.Sections = &Sections;
5232 info.class_name = nullptr;
5233 info.selector_name = nullptr;
5234 info.method = nullptr;
5235 info.demangled_name = nullptr;
5236 info.bindtable = nullptr;
5237 info.adrp_addr = 0;
5238 info.adrp_inst = 0;
5239
5240 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5241 if (CL != SectionRef()) {
5242 info.S = CL;
5243 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5244 } else {
5245 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5246 info.S = CL;
5247 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5248 }
5249
5250 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5251 if (CR != SectionRef()) {
5252 info.S = CR;
5253 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5254 } else {
5255 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5256 info.S = CR;
5257 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5258 }
5259
5260 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5261 if (SR != SectionRef()) {
5262 info.S = SR;
5263 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5264 } else {
5265 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5266 info.S = SR;
5267 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5268 }
5269
5270 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5271 if (CA != SectionRef()) {
5272 info.S = CA;
5273 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5274 } else {
5275 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5276 info.S = CA;
5277 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5278 }
5279
5280 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5281 if (PL != SectionRef()) {
5282 info.S = PL;
5283 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5284 } else {
5285 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5286 info.S = PL;
5287 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5288 }
5289
5290 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5291 if (MR != SectionRef()) {
5292 info.S = MR;
5293 print_message_refs32(MR, &info);
5294 } else {
5295 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5296 info.S = MR;
5297 print_message_refs32(MR, &info);
5298 }
5299
5300 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5301 if (II != SectionRef()) {
5302 info.S = II;
5303 print_image_info32(II, &info);
5304 } else {
5305 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5306 info.S = II;
5307 print_image_info32(II, &info);
5308 }
Kevin Enderby0fc11822015-04-01 20:57:01 +00005309}
5310
5311static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby846c0002015-04-16 17:19:59 +00005312 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5313 const char *r, *name, *defs;
5314 struct objc_module_t module;
5315 SectionRef S, xS;
5316 struct objc_symtab_t symtab;
5317 struct objc_class_t objc_class;
5318 struct objc_category_t objc_category;
5319
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005320 outs() << "Objective-C segment\n";
Kevin Enderby846c0002015-04-16 17:19:59 +00005321 S = get_section(O, "__OBJC", "__module_info");
5322 if (S == SectionRef())
5323 return false;
5324
5325 SymbolAddressMap AddrMap;
5326 if (verbose)
5327 CreateSymbolAddressMap(O, &AddrMap);
5328
5329 std::vector<SectionRef> Sections;
5330 for (const SectionRef &Section : O->sections()) {
5331 StringRef SectName;
5332 Section.getName(SectName);
5333 Sections.push_back(Section);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005334 }
Kevin Enderby846c0002015-04-16 17:19:59 +00005335
5336 struct DisassembleInfo info;
5337 // Set up the block of info used by the Symbolizer call backs.
5338 info.verbose = verbose;
5339 info.O = O;
5340 info.AddrMap = &AddrMap;
5341 info.Sections = &Sections;
5342 info.class_name = nullptr;
5343 info.selector_name = nullptr;
5344 info.method = nullptr;
5345 info.demangled_name = nullptr;
5346 info.bindtable = nullptr;
5347 info.adrp_addr = 0;
5348 info.adrp_inst = 0;
5349
5350 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5351 p = S.getAddress() + i;
5352 r = get_pointer_32(p, offset, left, S, &info, true);
5353 if (r == nullptr)
5354 return true;
5355 memset(&module, '\0', sizeof(struct objc_module_t));
5356 if (left < sizeof(struct objc_module_t)) {
5357 memcpy(&module, r, left);
5358 outs() << " (module extends past end of __module_info section)\n";
5359 } else
5360 memcpy(&module, r, sizeof(struct objc_module_t));
5361 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5362 swapStruct(module);
5363
5364 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5365 outs() << " version " << module.version << "\n";
5366 outs() << " size " << module.size << "\n";
5367 outs() << " name ";
5368 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5369 if (name != nullptr)
5370 outs() << format("%.*s", left, name);
5371 else
5372 outs() << format("0x%08" PRIx32, module.name)
5373 << "(not in an __OBJC section)";
5374 outs() << "\n";
5375
5376 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5377 if (module.symtab == 0 || r == nullptr) {
5378 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5379 << " (not in an __OBJC section)\n";
5380 continue;
5381 }
5382 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5383 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5384 defs_left = 0;
5385 defs = nullptr;
5386 if (left < sizeof(struct objc_symtab_t)) {
5387 memcpy(&symtab, r, left);
5388 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5389 } else {
5390 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5391 if (left > sizeof(struct objc_symtab_t)) {
5392 defs_left = left - sizeof(struct objc_symtab_t);
5393 defs = r + sizeof(struct objc_symtab_t);
5394 }
5395 }
5396 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5397 swapStruct(symtab);
5398
5399 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5400 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5401 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5402 if (r == nullptr)
5403 outs() << " (not in an __OBJC section)";
5404 outs() << "\n";
5405 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5406 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5407 if (symtab.cls_def_cnt > 0)
5408 outs() << "\tClass Definitions\n";
5409 for (j = 0; j < symtab.cls_def_cnt; j++) {
5410 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5411 outs() << "\t(remaining class defs entries entends past the end of the "
5412 << "section)\n";
5413 break;
5414 }
5415 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5416 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5417 sys::swapByteOrder(def);
5418
5419 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5420 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5421 if (r != nullptr) {
5422 if (left > sizeof(struct objc_class_t)) {
5423 outs() << "\n";
5424 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5425 } else {
5426 outs() << " (entends past the end of the section)\n";
5427 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5428 memcpy(&objc_class, r, left);
5429 }
5430 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5431 swapStruct(objc_class);
5432 print_objc_class_t(&objc_class, &info);
5433 } else {
5434 outs() << "(not in an __OBJC section)\n";
5435 }
5436
5437 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5438 outs() << "\tMeta Class";
5439 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5440 if (r != nullptr) {
5441 if (left > sizeof(struct objc_class_t)) {
5442 outs() << "\n";
5443 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5444 } else {
5445 outs() << " (entends past the end of the section)\n";
5446 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5447 memcpy(&objc_class, r, left);
5448 }
5449 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5450 swapStruct(objc_class);
5451 print_objc_class_t(&objc_class, &info);
5452 } else {
5453 outs() << "(not in an __OBJC section)\n";
5454 }
5455 }
5456 }
5457 if (symtab.cat_def_cnt > 0)
5458 outs() << "\tCategory Definitions\n";
5459 for (j = 0; j < symtab.cat_def_cnt; j++) {
5460 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5461 outs() << "\t(remaining category defs entries entends past the end of "
5462 << "the section)\n";
5463 break;
5464 }
5465 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5466 sizeof(uint32_t));
5467 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5468 sys::swapByteOrder(def);
5469
5470 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5471 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5472 << format("0x%08" PRIx32, def);
5473 if (r != nullptr) {
5474 if (left > sizeof(struct objc_category_t)) {
5475 outs() << "\n";
5476 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5477 } else {
5478 outs() << " (entends past the end of the section)\n";
5479 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5480 memcpy(&objc_category, r, left);
5481 }
5482 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5483 swapStruct(objc_category);
5484 print_objc_objc_category_t(&objc_category, &info);
5485 } else {
5486 outs() << "(not in an __OBJC section)\n";
5487 }
5488 }
5489 }
5490 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5491 if (II != SectionRef())
5492 print_image_info(II, &info);
5493
5494 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005495}
5496
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00005497static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5498 uint32_t size, uint32_t addr) {
5499 SymbolAddressMap AddrMap;
5500 CreateSymbolAddressMap(O, &AddrMap);
5501
5502 std::vector<SectionRef> Sections;
5503 for (const SectionRef &Section : O->sections()) {
5504 StringRef SectName;
5505 Section.getName(SectName);
5506 Sections.push_back(Section);
5507 }
5508
5509 struct DisassembleInfo info;
5510 // Set up the block of info used by the Symbolizer call backs.
5511 info.verbose = true;
5512 info.O = O;
5513 info.AddrMap = &AddrMap;
5514 info.Sections = &Sections;
5515 info.class_name = nullptr;
5516 info.selector_name = nullptr;
5517 info.method = nullptr;
5518 info.demangled_name = nullptr;
5519 info.bindtable = nullptr;
5520 info.adrp_addr = 0;
5521 info.adrp_inst = 0;
5522
5523 const char *p;
5524 struct objc_protocol_t protocol;
5525 uint32_t left, paddr;
5526 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5527 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5528 left = size - (p - sect);
5529 if (left < sizeof(struct objc_protocol_t)) {
5530 outs() << "Protocol extends past end of __protocol section\n";
5531 memcpy(&protocol, p, left);
5532 } else
5533 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5534 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5535 swapStruct(protocol);
5536 paddr = addr + (p - sect);
5537 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5538 if (print_protocol(paddr, 0, &info))
5539 outs() << "(not in an __OBJC section)\n";
5540 }
5541}
5542
Kevin Enderby0fc11822015-04-01 20:57:01 +00005543static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
5544 if (O->is64Bit())
5545 printObjc2_64bit_MetaData(O, verbose);
5546 else {
5547 MachO::mach_header H;
5548 H = O->getHeader();
5549 if (H.cputype == MachO::CPU_TYPE_ARM)
5550 printObjc2_32bit_MetaData(O, verbose);
5551 else {
5552 // This is the 32-bit non-arm cputype case. Which is normally
5553 // the first Objective-C ABI. But it may be the case of a
5554 // binary for the iOS simulator which is the second Objective-C
5555 // ABI. In that case printObjc1_32bit_MetaData() will determine that
5556 // and return false.
5557 if (printObjc1_32bit_MetaData(O, verbose) == false)
5558 printObjc2_32bit_MetaData(O, verbose);
5559 }
5560 }
5561}
5562
Kevin Enderbybf246f52014-09-24 23:08:22 +00005563// GuessLiteralPointer returns a string which for the item in the Mach-O file
5564// for the address passed in as ReferenceValue for printing as a comment with
5565// the instruction and also returns the corresponding type of that item
5566// indirectly through ReferenceType.
5567//
5568// If ReferenceValue is an address of literal cstring then a pointer to the
5569// cstring is returned and ReferenceType is set to
5570// LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
5571//
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005572// If ReferenceValue is an address of an Objective-C CFString, Selector ref or
5573// Class ref that name is returned and the ReferenceType is set accordingly.
5574//
5575// Lastly, literals which are Symbol address in a literal pool are looked for
5576// and if found the symbol name is returned and ReferenceType is set to
5577// LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
5578//
5579// If there is no item in the Mach-O file for the address passed in as
5580// ReferenceValue nullptr is returned and ReferenceType is unchanged.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00005581static const char *GuessLiteralPointer(uint64_t ReferenceValue,
5582 uint64_t ReferencePC,
5583 uint64_t *ReferenceType,
5584 struct DisassembleInfo *info) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005585 // First see if there is an external relocation entry at the ReferencePC.
Rafael Espindola80291272014-10-08 15:28:58 +00005586 uint64_t sect_addr = info->S.getAddress();
Kevin Enderbybf246f52014-09-24 23:08:22 +00005587 uint64_t sect_offset = ReferencePC - sect_addr;
5588 bool reloc_found = false;
5589 DataRefImpl Rel;
5590 MachO::any_relocation_info RE;
5591 bool isExtern = false;
5592 SymbolRef Symbol;
5593 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00005594 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderbybf246f52014-09-24 23:08:22 +00005595 if (RelocOffset == sect_offset) {
5596 Rel = Reloc.getRawDataRefImpl();
5597 RE = info->O->getRelocation(Rel);
5598 if (info->O->isRelocationScattered(RE))
5599 continue;
5600 isExtern = info->O->getPlainRelocationExternal(RE);
5601 if (isExtern) {
5602 symbol_iterator RelocSym = Reloc.getSymbol();
5603 Symbol = *RelocSym;
5604 }
5605 reloc_found = true;
5606 break;
5607 }
5608 }
5609 // If there is an external relocation entry for a symbol in a section
5610 // then used that symbol's value for the value of the reference.
5611 if (reloc_found && isExtern) {
5612 if (info->O->getAnyRelocationPCRel(RE)) {
5613 unsigned Type = info->O->getAnyRelocationType(RE);
5614 if (Type == MachO::X86_64_RELOC_SIGNED) {
Rafael Espindoladea00162015-07-03 17:44:18 +00005615 ReferenceValue = Symbol.getValue();
Kevin Enderbybf246f52014-09-24 23:08:22 +00005616 }
5617 }
5618 }
5619
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005620 // Look for literals such as Objective-C CFStrings refs, Selector refs,
5621 // Message refs and Class refs.
5622 bool classref, selref, msgref, cfstring;
5623 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
5624 selref, msgref, cfstring);
David Blaikie33dd45d02015-03-23 18:39:02 +00005625 if (classref && pointer_value == 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005626 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
5627 // And the pointer_value in that section is typically zero as it will be
5628 // set by dyld as part of the "bind information".
5629 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
5630 if (name != nullptr) {
5631 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
Hans Wennborgdb53e302014-10-23 21:59:17 +00005632 const char *class_name = strrchr(name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005633 if (class_name != nullptr && class_name[1] == '_' &&
5634 class_name[2] != '\0') {
5635 info->class_name = class_name + 2;
5636 return name;
5637 }
5638 }
5639 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005640
David Blaikie33dd45d02015-03-23 18:39:02 +00005641 if (classref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005642 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5643 const char *name =
5644 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
5645 if (name != nullptr)
5646 info->class_name = name;
5647 else
5648 name = "bad class ref";
Kevin Enderbybf246f52014-09-24 23:08:22 +00005649 return name;
5650 }
5651
David Blaikie33dd45d02015-03-23 18:39:02 +00005652 if (cfstring) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005653 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
5654 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
5655 return name;
5656 }
5657
David Blaikie33dd45d02015-03-23 18:39:02 +00005658 if (selref && pointer_value == 0)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005659 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
5660
5661 if (pointer_value != 0)
5662 ReferenceValue = pointer_value;
5663
5664 const char *name = GuessCstringPointer(ReferenceValue, info);
5665 if (name) {
David Blaikie33dd45d02015-03-23 18:39:02 +00005666 if (pointer_value != 0 && selref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005667 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
5668 info->selector_name = name;
David Blaikie33dd45d02015-03-23 18:39:02 +00005669 } else if (pointer_value != 0 && msgref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005670 info->class_name = nullptr;
5671 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
5672 info->selector_name = name;
5673 } else
5674 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
5675 return name;
5676 }
5677
5678 // Lastly look for an indirect symbol with this ReferenceValue which is in
5679 // a literal pool. If found return that symbol name.
5680 name = GuessIndirectSymbol(ReferenceValue, info);
5681 if (name) {
5682 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
5683 return name;
5684 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005685
5686 return nullptr;
5687}
5688
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005689// SymbolizerSymbolLookUp is the symbol lookup function passed when creating
Kevin Enderbybf246f52014-09-24 23:08:22 +00005690// the Symbolizer. It looks up the ReferenceValue using the info passed via the
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005691// pointer to the struct DisassembleInfo that was passed when MCSymbolizer
5692// is created and returns the symbol name that matches the ReferenceValue or
5693// nullptr if none. The ReferenceType is passed in for the IN type of
5694// reference the instruction is making from the values in defined in the header
5695// "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
5696// Out type and the ReferenceName will also be set which is added as a comment
5697// to the disassembled instruction.
5698//
Kevin Enderby04bf6932014-10-28 23:39:46 +00005699#if HAVE_CXXABI_H
5700// If the symbol name is a C++ mangled name then the demangled name is
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005701// returned through ReferenceName and ReferenceType is set to
5702// LLVMDisassembler_ReferenceType_DeMangled_Name .
Kevin Enderby04bf6932014-10-28 23:39:46 +00005703#endif
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005704//
5705// When this is called to get a symbol name for a branch target then the
5706// ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
5707// SymbolValue will be looked for in the indirect symbol table to determine if
5708// it is an address for a symbol stub. If so then the symbol name for that
5709// stub is returned indirectly through ReferenceName and then ReferenceType is
5710// set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
5711//
Kevin Enderbybf246f52014-09-24 23:08:22 +00005712// When this is called with an value loaded via a PC relative load then
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005713// ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
5714// SymbolValue is checked to be an address of literal pointer, symbol pointer,
5715// or an Objective-C meta data reference. If so the output ReferenceType is
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005716// set to correspond to that as well as setting the ReferenceName.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00005717static const char *SymbolizerSymbolLookUp(void *DisInfo,
5718 uint64_t ReferenceValue,
5719 uint64_t *ReferenceType,
5720 uint64_t ReferencePC,
5721 const char **ReferenceName) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005722 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
Kevin Enderbybf246f52014-09-24 23:08:22 +00005723 // If no verbose symbolic information is wanted then just return nullptr.
David Blaikie33dd45d02015-03-23 18:39:02 +00005724 if (!info->verbose) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005725 *ReferenceName = nullptr;
5726 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005727 return nullptr;
5728 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005729
Kevin Enderbyf6d25852015-01-31 00:37:11 +00005730 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderbybf246f52014-09-24 23:08:22 +00005731
Kevin Enderby85974882014-09-26 22:20:44 +00005732 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
5733 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005734 if (*ReferenceName != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005735 method_reference(info, ReferenceType, ReferenceName);
5736 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
5737 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
5738 } else
Kevin Enderby04bf6932014-10-28 23:39:46 +00005739#if HAVE_CXXABI_H
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005740 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00005741 if (info->demangled_name != nullptr)
5742 free(info->demangled_name);
5743 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005744 info->demangled_name =
5745 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005746 if (info->demangled_name != nullptr) {
5747 *ReferenceName = info->demangled_name;
5748 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5749 } else
5750 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5751 } else
5752#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005753 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5754 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
5755 *ReferenceName =
5756 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
Kevin Enderby85974882014-09-26 22:20:44 +00005757 if (*ReferenceName)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005758 method_reference(info, ReferenceType, ReferenceName);
Kevin Enderby85974882014-09-26 22:20:44 +00005759 else
5760 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005761 // If this is arm64 and the reference is an adrp instruction save the
5762 // instruction, passed in ReferenceValue and the address of the instruction
5763 // for use later if we see and add immediate instruction.
5764 } else if (info->O->getArch() == Triple::aarch64 &&
5765 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
5766 info->adrp_inst = ReferenceValue;
5767 info->adrp_addr = ReferencePC;
5768 SymbolName = nullptr;
5769 *ReferenceName = nullptr;
5770 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5771 // If this is arm64 and reference is an add immediate instruction and we
5772 // have
5773 // seen an adrp instruction just before it and the adrp's Xd register
5774 // matches
5775 // this add's Xn register reconstruct the value being referenced and look to
5776 // see if it is a literal pointer. Note the add immediate instruction is
5777 // passed in ReferenceValue.
5778 } else if (info->O->getArch() == Triple::aarch64 &&
5779 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
5780 ReferencePC - 4 == info->adrp_addr &&
5781 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5782 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5783 uint32_t addxri_inst;
5784 uint64_t adrp_imm, addxri_imm;
5785
5786 adrp_imm =
5787 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5788 if (info->adrp_inst & 0x0200000)
5789 adrp_imm |= 0xfffffffffc000000LL;
5790
5791 addxri_inst = ReferenceValue;
5792 addxri_imm = (addxri_inst >> 10) & 0xfff;
5793 if (((addxri_inst >> 22) & 0x3) == 1)
5794 addxri_imm <<= 12;
5795
5796 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5797 (adrp_imm << 12) + addxri_imm;
5798
5799 *ReferenceName =
5800 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5801 if (*ReferenceName == nullptr)
5802 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5803 // If this is arm64 and the reference is a load register instruction and we
5804 // have seen an adrp instruction just before it and the adrp's Xd register
5805 // matches this add's Xn register reconstruct the value being referenced and
5806 // look to see if it is a literal pointer. Note the load register
5807 // instruction is passed in ReferenceValue.
5808 } else if (info->O->getArch() == Triple::aarch64 &&
5809 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
5810 ReferencePC - 4 == info->adrp_addr &&
5811 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5812 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5813 uint32_t ldrxui_inst;
5814 uint64_t adrp_imm, ldrxui_imm;
5815
5816 adrp_imm =
5817 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5818 if (info->adrp_inst & 0x0200000)
5819 adrp_imm |= 0xfffffffffc000000LL;
5820
5821 ldrxui_inst = ReferenceValue;
5822 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
5823
5824 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5825 (adrp_imm << 12) + (ldrxui_imm << 3);
5826
5827 *ReferenceName =
5828 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5829 if (*ReferenceName == nullptr)
5830 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5831 }
5832 // If this arm64 and is an load register (PC-relative) instruction the
5833 // ReferenceValue is the PC plus the immediate value.
5834 else if (info->O->getArch() == Triple::aarch64 &&
5835 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
5836 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
5837 *ReferenceName =
5838 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5839 if (*ReferenceName == nullptr)
5840 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby85974882014-09-26 22:20:44 +00005841 }
Kevin Enderby04bf6932014-10-28 23:39:46 +00005842#if HAVE_CXXABI_H
5843 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5844 if (info->demangled_name != nullptr)
5845 free(info->demangled_name);
5846 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005847 info->demangled_name =
5848 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005849 if (info->demangled_name != nullptr) {
5850 *ReferenceName = info->demangled_name;
5851 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5852 }
5853 }
5854#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005855 else {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005856 *ReferenceName = nullptr;
5857 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5858 }
5859
5860 return SymbolName;
5861}
5862
Kevin Enderbybf246f52014-09-24 23:08:22 +00005863/// \brief Emits the comments that are stored in the CommentStream.
5864/// Each comment in the CommentStream must end with a newline.
5865static void emitComments(raw_svector_ostream &CommentStream,
5866 SmallString<128> &CommentsToEmit,
5867 formatted_raw_ostream &FormattedOS,
5868 const MCAsmInfo &MAI) {
5869 // Flush the stream before taking its content.
5870 CommentStream.flush();
5871 StringRef Comments = CommentsToEmit.str();
5872 // Get the default information for printing a comment.
5873 const char *CommentBegin = MAI.getCommentString();
5874 unsigned CommentColumn = MAI.getCommentColumn();
5875 bool IsFirst = true;
5876 while (!Comments.empty()) {
5877 if (!IsFirst)
5878 FormattedOS << '\n';
5879 // Emit a line of comments.
5880 FormattedOS.PadToColumn(CommentColumn);
5881 size_t Position = Comments.find('\n');
5882 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
5883 // Move after the newline character.
5884 Comments = Comments.substr(Position + 1);
5885 IsFirst = false;
5886 }
5887 FormattedOS.flush();
5888
5889 // Tell the comment stream that the vector changed underneath it.
5890 CommentsToEmit.clear();
5891 CommentStream.resync();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005892}
5893
Kevin Enderby95df54c2015-02-04 01:01:38 +00005894static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
5895 StringRef DisSegName, StringRef DisSectName) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005896 const char *McpuDefault = nullptr;
5897 const Target *ThumbTarget = nullptr;
5898 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005899 if (!TheTarget) {
5900 // GetTarget prints out stuff.
5901 return;
5902 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005903 if (MCPU.empty() && McpuDefault)
5904 MCPU = McpuDefault;
5905
Ahmed Charles56440fd2014-03-06 05:51:42 +00005906 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005907 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005908 if (ThumbTarget)
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005909 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005910
Kevin Enderbyc9595622014-08-06 23:24:41 +00005911 // Package up features to be passed to target/subtarget
5912 std::string FeaturesStr;
5913 if (MAttrs.size()) {
5914 SubtargetFeatures Features;
5915 for (unsigned i = 0; i != MAttrs.size(); ++i)
5916 Features.AddFeature(MAttrs[i]);
5917 FeaturesStr = Features.getString();
5918 }
5919
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005920 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00005921 std::unique_ptr<const MCRegisterInfo> MRI(
5922 TheTarget->createMCRegInfo(TripleName));
5923 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +00005924 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +00005925 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +00005926 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Craig Toppere6cb63e2014-04-25 04:24:47 +00005927 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005928 std::unique_ptr<MCDisassembler> DisAsm(
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005929 TheTarget->createMCDisassembler(*STI, Ctx));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005930 std::unique_ptr<MCSymbolizer> Symbolizer;
5931 struct DisassembleInfo SymbolizerInfo;
5932 std::unique_ptr<MCRelocationInfo> RelInfo(
5933 TheTarget->createMCRelocationInfo(TripleName, Ctx));
5934 if (RelInfo) {
5935 Symbolizer.reset(TheTarget->createMCSymbolizer(
5936 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00005937 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005938 DisAsm->setSymbolizer(std::move(Symbolizer));
5939 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005940 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +00005941 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +00005942 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00005943 // Set the display preference for hex vs. decimal immediates.
5944 IP->setPrintImmHex(PrintImmHex);
5945 // Comment stream and backing vector.
5946 SmallString<128> CommentsToEmit;
5947 raw_svector_ostream CommentStream(CommentsToEmit);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00005948 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
5949 // if it is done then arm64 comments for string literals don't get printed
5950 // and some constant get printed instead and not setting it causes intel
5951 // (32-bit and 64-bit) comments printed with different spacing before the
5952 // comment causing different diffs with the 'C' disassembler library API.
5953 // IP->setCommentStream(CommentStream);
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005954
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005955 if (!AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +00005956 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005957 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005958 return;
5959 }
5960
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005961 // Set up thumb disassembler.
5962 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
5963 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
5964 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005965 std::unique_ptr<MCDisassembler> ThumbDisAsm;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005966 std::unique_ptr<MCInstPrinter> ThumbIP;
5967 std::unique_ptr<MCContext> ThumbCtx;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005968 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
5969 struct DisassembleInfo ThumbSymbolizerInfo;
5970 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005971 if (ThumbTarget) {
5972 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
5973 ThumbAsmInfo.reset(
5974 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
5975 ThumbSTI.reset(
5976 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
5977 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
5978 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
Kevin Enderby930fdc72014-11-06 19:00:13 +00005979 MCContext *PtrThumbCtx = ThumbCtx.get();
5980 ThumbRelInfo.reset(
5981 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
5982 if (ThumbRelInfo) {
5983 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
5984 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00005985 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
Kevin Enderby930fdc72014-11-06 19:00:13 +00005986 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
5987 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005988 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
5989 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +00005990 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
5991 *ThumbInstrInfo, *ThumbMRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00005992 // Set the display preference for hex vs. decimal immediates.
5993 ThumbIP->setPrintImmHex(PrintImmHex);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005994 }
5995
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005996 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005997 errs() << "error: couldn't initialize disassembler for target "
5998 << ThumbTripleName << '\n';
5999 return;
6000 }
6001
Charles Davis8bdfafd2013-09-01 04:28:48 +00006002 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006003
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006004 // FIXME: Using the -cfg command line option, this code used to be able to
6005 // annotate relocations with the referenced symbol's name, and if this was
6006 // inside a __[cf]string section, the data it points to. This is now replaced
6007 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +00006008 std::vector<SectionRef> Sections;
6009 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006010 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +00006011 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006012
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00006013 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
Kevin Enderby273ae012013-06-06 17:20:50 +00006014 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006015
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006016 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +00006017 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006018
Kevin Enderby273ae012013-06-06 17:20:50 +00006019 // Build a data in code table that is sorted on by the address of each entry.
6020 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +00006021 if (Header.filetype == MachO::MH_OBJECT)
Rafael Espindola80291272014-10-08 15:28:58 +00006022 BaseAddress = Sections[0].getAddress();
Kevin Enderby273ae012013-06-06 17:20:50 +00006023 else
6024 BaseAddress = BaseSegmentAddress;
6025 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +00006026 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +00006027 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006028 uint32_t Offset;
6029 DI->getOffset(Offset);
6030 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6031 }
6032 array_pod_sort(Dices.begin(), Dices.end());
6033
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006034#ifndef NDEBUG
6035 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6036#else
6037 raw_ostream &DebugOut = nulls();
6038#endif
6039
Ahmed Charles56440fd2014-03-06 05:51:42 +00006040 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +00006041 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +00006042 // Try to find debug info and set up the DIContext for it.
6043 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +00006044 // A separate DSym file path was specified, parse it as a macho file,
6045 // get the sections and supply it to the section name parsing machinery.
6046 if (!DSYMFile.empty()) {
Rafael Espindola48af1c22014-08-19 18:44:46 +00006047 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006048 MemoryBuffer::getFileOrSTDIN(DSYMFile);
Rafael Espindola48af1c22014-08-19 18:44:46 +00006049 if (std::error_code EC = BufOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006050 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
Benjamin Kramer699128e2011-09-21 01:13:19 +00006051 return;
6052 }
Rafael Espindola48af1c22014-08-19 18:44:46 +00006053 DbgObj =
6054 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6055 .get()
6056 .release();
Benjamin Kramer699128e2011-09-21 01:13:19 +00006057 }
6058
Eric Christopher7370b552012-11-12 21:40:38 +00006059 // Setup the DIContext
Zachary Turner6489d7b2015-04-23 17:37:47 +00006060 diContext.reset(new DWARFContextInMemory(*DbgObj));
Benjamin Kramer699128e2011-09-21 01:13:19 +00006061 }
6062
Colin LeMahieufcc32762015-07-29 19:08:10 +00006063 if (FilterSections.size() == 0)
Kevin Enderby95df54c2015-02-04 01:01:38 +00006064 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006065
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006066 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00006067 StringRef SectName;
Kevin Enderby95df54c2015-02-04 01:01:38 +00006068 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6069 continue;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006070
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006071 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006072
Rafael Espindolab0f76a42013-04-05 15:15:22 +00006073 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
Kevin Enderby95df54c2015-02-04 01:01:38 +00006074 if (SegmentName != DisSegName)
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006075 continue;
6076
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006077 StringRef BytesStr;
6078 Sections[SectIdx].getContents(BytesStr);
Aaron Ballman106fd7b2014-11-12 14:01:17 +00006079 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6080 BytesStr.size());
Rafael Espindola80291272014-10-08 15:28:58 +00006081 uint64_t SectAddress = Sections[SectIdx].getAddress();
Rafael Espindolabd604f22014-11-07 00:52:15 +00006082
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006083 bool symbolTableWorked = false;
6084
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006085 // Parse relocations.
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00006086 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
6087 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00006088 uint64_t RelocOffset = Reloc.getOffset();
Rafael Espindola80291272014-10-08 15:28:58 +00006089 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Owen Andersond9243c42011-10-17 21:37:35 +00006090 RelocOffset -= SectionAddress;
6091
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00006092 symbol_iterator RelocSym = Reloc.getSymbol();
Owen Andersond9243c42011-10-17 21:37:35 +00006093
Rafael Espindola806f0062013-06-05 01:33:53 +00006094 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006095 }
6096 array_pod_sort(Relocs.begin(), Relocs.end());
6097
Kevin Enderbybf246f52014-09-24 23:08:22 +00006098 // Create a map of symbol addresses to symbol names for use by
6099 // the SymbolizerSymbolLookUp() routine.
6100 SymbolAddressMap AddrMap;
Kevin Enderby6a221752015-03-17 17:10:57 +00006101 bool DisSymNameFound = false;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006102 for (const SymbolRef &Symbol : MachOOF->symbols()) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006103 SymbolRef::Type ST = Symbol.getType();
Kevin Enderbybf246f52014-09-24 23:08:22 +00006104 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6105 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +00006106 uint64_t Address = Symbol.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006107 ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
6108 if (std::error_code EC = SymNameOrErr.getError())
6109 report_fatal_error(EC.message());
6110 StringRef SymName = *SymNameOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006111 AddrMap[Address] = SymName;
Kevin Enderby6a221752015-03-17 17:10:57 +00006112 if (!DisSymName.empty() && DisSymName == SymName)
6113 DisSymNameFound = true;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006114 }
6115 }
David Blaikie33dd45d02015-03-23 18:39:02 +00006116 if (!DisSymName.empty() && !DisSymNameFound) {
Kevin Enderby6a221752015-03-17 17:10:57 +00006117 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6118 return;
6119 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006120 // Set up the block of info used by the Symbolizer call backs.
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006121 SymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006122 SymbolizerInfo.O = MachOOF;
6123 SymbolizerInfo.S = Sections[SectIdx];
Kevin Enderbybf246f52014-09-24 23:08:22 +00006124 SymbolizerInfo.AddrMap = &AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006125 SymbolizerInfo.Sections = &Sections;
6126 SymbolizerInfo.class_name = nullptr;
6127 SymbolizerInfo.selector_name = nullptr;
6128 SymbolizerInfo.method = nullptr;
Kevin Enderby04bf6932014-10-28 23:39:46 +00006129 SymbolizerInfo.demangled_name = nullptr;
Kevin Enderby078be602014-10-23 19:53:12 +00006130 SymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006131 SymbolizerInfo.adrp_addr = 0;
6132 SymbolizerInfo.adrp_inst = 0;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006133 // Same for the ThumbSymbolizer
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006134 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006135 ThumbSymbolizerInfo.O = MachOOF;
6136 ThumbSymbolizerInfo.S = Sections[SectIdx];
6137 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6138 ThumbSymbolizerInfo.Sections = &Sections;
6139 ThumbSymbolizerInfo.class_name = nullptr;
6140 ThumbSymbolizerInfo.selector_name = nullptr;
6141 ThumbSymbolizerInfo.method = nullptr;
6142 ThumbSymbolizerInfo.demangled_name = nullptr;
6143 ThumbSymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006144 ThumbSymbolizerInfo.adrp_addr = 0;
6145 ThumbSymbolizerInfo.adrp_inst = 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006146
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006147 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006148 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006149 ErrorOr<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
6150 if (std::error_code EC = SymNameOrErr.getError())
6151 report_fatal_error(EC.message());
6152 StringRef SymName = *SymNameOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006153
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006154 SymbolRef::Type ST = Symbols[SymIdx].getType();
Owen Andersond9243c42011-10-17 21:37:35 +00006155 if (ST != SymbolRef::ST_Function)
6156 continue;
6157
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006158 // Make sure the symbol is defined in this section.
Rafael Espindola80291272014-10-08 15:28:58 +00006159 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Owen Andersond9243c42011-10-17 21:37:35 +00006160 if (!containsSym)
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006161 continue;
6162
Kevin Enderby6a221752015-03-17 17:10:57 +00006163 // If we are only disassembling one symbol see if this is that symbol.
6164 if (!DisSymName.empty() && DisSymName != SymName)
6165 continue;
6166
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006167 // Start at the address of the symbol relative to the section's address.
Rafael Espindoladea00162015-07-03 17:44:18 +00006168 uint64_t Start = Symbols[SymIdx].getValue();
Rafael Espindola80291272014-10-08 15:28:58 +00006169 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006170 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006171
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006172 // Stop disassembling either at the beginning of the next symbol or at
6173 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +00006174 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +00006175 uint64_t NextSym = 0;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006176 uint64_t NextSymIdx = SymIdx + 1;
Owen Andersond9243c42011-10-17 21:37:35 +00006177 while (Symbols.size() > NextSymIdx) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006178 SymbolRef::Type NextSymType = Symbols[NextSymIdx].getType();
Owen Andersond9243c42011-10-17 21:37:35 +00006179 if (NextSymType == SymbolRef::ST_Function) {
Rafael Espindola80291272014-10-08 15:28:58 +00006180 containsNextSym =
6181 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Rafael Espindoladea00162015-07-03 17:44:18 +00006182 NextSym = Symbols[NextSymIdx].getValue();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006183 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006184 break;
6185 }
6186 ++NextSymIdx;
6187 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006188
Rafael Espindola80291272014-10-08 15:28:58 +00006189 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006190 uint64_t End = containsNextSym ? NextSym : SectSize;
Owen Andersond9243c42011-10-17 21:37:35 +00006191 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006192
6193 symbolTableWorked = true;
Rafael Espindolabd604f22014-11-07 00:52:15 +00006194
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006195 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6196 bool isThumb =
6197 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
6198
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006199 outs() << SymName << ":\n";
6200 DILineInfo lastLine;
6201 for (uint64_t Index = Start; Index < End; Index += Size) {
6202 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +00006203
Kevin Enderbybf246f52014-09-24 23:08:22 +00006204 uint64_t PC = SectAddress + Index;
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006205 if (!NoLeadingAddr) {
6206 if (FullLeadingAddr) {
6207 if (MachOOF->is64Bit())
6208 outs() << format("%016" PRIx64, PC);
6209 else
6210 outs() << format("%08" PRIx64, PC);
6211 } else {
6212 outs() << format("%8" PRIx64 ":", PC);
6213 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006214 }
6215 if (!NoShowRawInsn)
6216 outs() << "\t";
Kevin Enderby273ae012013-06-06 17:20:50 +00006217
6218 // Check the data in code table here to see if this is data not an
6219 // instruction to be disassembled.
6220 DiceTable Dice;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006221 Dice.push_back(std::make_pair(PC, DiceRef()));
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006222 dice_table_iterator DTI =
6223 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6224 compareDiceTableEntries);
6225 if (DTI != Dices.end()) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006226 uint16_t Length;
6227 DTI->second.getLength(Length);
Kevin Enderby273ae012013-06-06 17:20:50 +00006228 uint16_t Kind;
6229 DTI->second.getKind(Kind);
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00006230 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006231 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6232 (PC == (DTI->first + Length - 1)) && (Length & 1))
6233 Size++;
Kevin Enderby273ae012013-06-06 17:20:50 +00006234 continue;
6235 }
6236
Kevin Enderbybf246f52014-09-24 23:08:22 +00006237 SmallVector<char, 64> AnnotationsBytes;
6238 raw_svector_ostream Annotations(AnnotationsBytes);
6239
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006240 bool gotInst;
6241 if (isThumb)
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006242 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006243 PC, DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006244 else
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006245 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
Kevin Enderbybf246f52014-09-24 23:08:22 +00006246 DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006247 if (gotInst) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006248 if (!NoShowRawInsn) {
Colin LeMahieu2048ea42015-05-28 18:39:50 +00006249 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006250 }
6251 formatted_raw_ostream FormattedOS(outs());
6252 Annotations.flush();
6253 StringRef AnnotationsStr = Annotations.str();
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006254 if (isThumb)
Akira Hatanakab46d0232015-03-27 20:36:02 +00006255 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006256 else
Akira Hatanaka1d079942015-03-28 20:44:05 +00006257 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006258 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
Owen Andersond9243c42011-10-17 21:37:35 +00006259
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006260 // Print debug info.
6261 if (diContext) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006262 DILineInfo dli = diContext->getLineInfoForAddress(PC);
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006263 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +00006264 if (dli != lastLine && dli.Line != 0)
6265 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6266 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006267 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006268 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006269 outs() << "\n";
6270 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006271 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006272 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006273 outs() << format("\t.byte 0x%02x #bad opcode\n",
6274 *(Bytes.data() + Index) & 0xff);
6275 Size = 1; // skip exactly one illegible byte and move on.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006276 } else if (Arch == Triple::aarch64) {
6277 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6278 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6279 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6280 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6281 outs() << format("\t.long\t0x%08x\n", opcode);
6282 Size = 4;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006283 } else {
6284 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6285 if (Size == 0)
6286 Size = 1; // skip illegible bytes
6287 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006288 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006289 }
6290 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006291 if (!symbolTableWorked) {
Rafael Espindola80291272014-10-08 15:28:58 +00006292 // Reading the symbol table didn't work, disassemble the whole section.
6293 uint64_t SectAddress = Sections[SectIdx].getAddress();
6294 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbybadd1002012-05-18 00:13:56 +00006295 uint64_t InstSize;
6296 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +00006297 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +00006298
Kevin Enderbybf246f52014-09-24 23:08:22 +00006299 uint64_t PC = SectAddress + Index;
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006300 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6301 DebugOut, nulls())) {
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006302 if (!NoLeadingAddr) {
6303 if (FullLeadingAddr) {
6304 if (MachOOF->is64Bit())
6305 outs() << format("%016" PRIx64, PC);
6306 else
6307 outs() << format("%08" PRIx64, PC);
6308 } else {
6309 outs() << format("%8" PRIx64 ":", PC);
6310 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006311 }
6312 if (!NoShowRawInsn) {
6313 outs() << "\t";
Colin LeMahieu2048ea42015-05-28 18:39:50 +00006314 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006315 }
Akira Hatanaka1d079942015-03-28 20:44:05 +00006316 IP->printInst(&Inst, outs(), "", *STI);
Bill Wendling4e68e062012-07-19 00:17:40 +00006317 outs() << "\n";
6318 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006319 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006320 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006321 outs() << format("\t.byte 0x%02x #bad opcode\n",
6322 *(Bytes.data() + Index) & 0xff);
6323 InstSize = 1; // skip exactly one illegible byte and move on.
6324 } else {
6325 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6326 if (InstSize == 0)
6327 InstSize = 1; // skip illegible bytes
6328 }
Bill Wendling4e68e062012-07-19 00:17:40 +00006329 }
Kevin Enderbybadd1002012-05-18 00:13:56 +00006330 }
6331 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006332 // The TripleName's need to be reset if we are called again for a different
6333 // archtecture.
6334 TripleName = "";
6335 ThumbTripleName = "";
6336
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006337 if (SymbolizerInfo.method != nullptr)
6338 free(SymbolizerInfo.method);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006339 if (SymbolizerInfo.demangled_name != nullptr)
6340 free(SymbolizerInfo.demangled_name);
Kevin Enderby078be602014-10-23 19:53:12 +00006341 if (SymbolizerInfo.bindtable != nullptr)
6342 delete SymbolizerInfo.bindtable;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006343 if (ThumbSymbolizerInfo.method != nullptr)
6344 free(ThumbSymbolizerInfo.method);
6345 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6346 free(ThumbSymbolizerInfo.demangled_name);
6347 if (ThumbSymbolizerInfo.bindtable != nullptr)
6348 delete ThumbSymbolizerInfo.bindtable;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006349 }
6350}
Tim Northover4bd286a2014-08-01 13:07:19 +00006351
Tim Northover39c70bb2014-08-12 11:52:59 +00006352//===----------------------------------------------------------------------===//
6353// __compact_unwind section dumping
6354//===----------------------------------------------------------------------===//
6355
Tim Northover4bd286a2014-08-01 13:07:19 +00006356namespace {
Tim Northover39c70bb2014-08-12 11:52:59 +00006357
6358template <typename T> static uint64_t readNext(const char *&Buf) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006359 using llvm::support::little;
6360 using llvm::support::unaligned;
Tim Northover39c70bb2014-08-12 11:52:59 +00006361
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006362 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6363 Buf += sizeof(T);
6364 return Val;
6365}
Tim Northover39c70bb2014-08-12 11:52:59 +00006366
Tim Northover4bd286a2014-08-01 13:07:19 +00006367struct CompactUnwindEntry {
6368 uint32_t OffsetInSection;
6369
6370 uint64_t FunctionAddr;
6371 uint32_t Length;
6372 uint32_t CompactEncoding;
6373 uint64_t PersonalityAddr;
6374 uint64_t LSDAAddr;
6375
6376 RelocationRef FunctionReloc;
6377 RelocationRef PersonalityReloc;
6378 RelocationRef LSDAReloc;
6379
6380 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006381 : OffsetInSection(Offset) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006382 if (Is64)
6383 read<uint64_t>(Contents.data() + Offset);
6384 else
6385 read<uint32_t>(Contents.data() + Offset);
6386 }
6387
6388private:
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006389 template <typename UIntPtr> void read(const char *Buf) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006390 FunctionAddr = readNext<UIntPtr>(Buf);
6391 Length = readNext<uint32_t>(Buf);
6392 CompactEncoding = readNext<uint32_t>(Buf);
6393 PersonalityAddr = readNext<UIntPtr>(Buf);
6394 LSDAAddr = readNext<UIntPtr>(Buf);
6395 }
6396};
6397}
6398
6399/// Given a relocation from __compact_unwind, consisting of the RelocationRef
6400/// and data being relocated, determine the best base Name and Addend to use for
6401/// display purposes.
6402///
6403/// 1. An Extern relocation will directly reference a symbol (and the data is
6404/// then already an addend), so use that.
6405/// 2. Otherwise the data is an offset in the object file's layout; try to find
6406// a symbol before it in the same section, and use the offset from there.
6407/// 3. Finally, if all that fails, fall back to an offset from the start of the
6408/// referenced section.
6409static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6410 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006411 const RelocationRef &Reloc, uint64_t Addr,
Tim Northover4bd286a2014-08-01 13:07:19 +00006412 StringRef &Name, uint64_t &Addend) {
6413 if (Reloc.getSymbol() != Obj->symbol_end()) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006414 ErrorOr<StringRef> NameOrErr = Reloc.getSymbol()->getName();
6415 if (std::error_code EC = NameOrErr.getError())
6416 report_fatal_error(EC.message());
6417 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00006418 Addend = Addr;
6419 return;
6420 }
6421
6422 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
Keno Fischerc780e8e2015-05-21 21:24:32 +00006423 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
Tim Northover4bd286a2014-08-01 13:07:19 +00006424
Rafael Espindola80291272014-10-08 15:28:58 +00006425 uint64_t SectionAddr = RelocSection.getAddress();
Tim Northover4bd286a2014-08-01 13:07:19 +00006426
6427 auto Sym = Symbols.upper_bound(Addr);
6428 if (Sym == Symbols.begin()) {
6429 // The first symbol in the object is after this reference, the best we can
6430 // do is section-relative notation.
6431 RelocSection.getName(Name);
6432 Addend = Addr - SectionAddr;
6433 return;
6434 }
6435
6436 // Go back one so that SymbolAddress <= Addr.
6437 --Sym;
6438
6439 section_iterator SymSection = Obj->section_end();
6440 Sym->second.getSection(SymSection);
6441 if (RelocSection == *SymSection) {
6442 // There's a valid symbol in the same section before this reference.
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006443 ErrorOr<StringRef> NameOrErr = Sym->second.getName();
6444 if (std::error_code EC = NameOrErr.getError())
6445 report_fatal_error(EC.message());
6446 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00006447 Addend = Addr - Sym->first;
6448 return;
6449 }
6450
6451 // There is a symbol before this reference, but it's in a different
6452 // section. Probably not helpful to mention it, so use the section name.
6453 RelocSection.getName(Name);
6454 Addend = Addr - SectionAddr;
6455}
6456
6457static void printUnwindRelocDest(const MachOObjectFile *Obj,
6458 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006459 const RelocationRef &Reloc, uint64_t Addr) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006460 StringRef Name;
6461 uint64_t Addend;
6462
Rafael Espindola854038e2015-06-26 14:51:16 +00006463 if (!Reloc.getObject())
Tim Northover0b0add52014-09-09 10:45:06 +00006464 return;
6465
Tim Northover4bd286a2014-08-01 13:07:19 +00006466 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6467
6468 outs() << Name;
6469 if (Addend)
Tim Northover63a25622014-08-11 09:14:06 +00006470 outs() << " + " << format("0x%" PRIx64, Addend);
Tim Northover4bd286a2014-08-01 13:07:19 +00006471}
6472
6473static void
6474printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6475 std::map<uint64_t, SymbolRef> &Symbols,
6476 const SectionRef &CompactUnwind) {
6477
6478 assert(Obj->isLittleEndian() &&
6479 "There should not be a big-endian .o with __compact_unwind");
6480
6481 bool Is64 = Obj->is64Bit();
6482 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6483 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6484
6485 StringRef Contents;
6486 CompactUnwind.getContents(Contents);
6487
6488 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6489
6490 // First populate the initial raw offsets, encodings and so on from the entry.
6491 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6492 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
6493 CompactUnwinds.push_back(Entry);
6494 }
6495
6496 // Next we need to look at the relocations to find out what objects are
6497 // actually being referred to.
6498 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00006499 uint64_t RelocAddress = Reloc.getOffset();
Tim Northover4bd286a2014-08-01 13:07:19 +00006500
6501 uint32_t EntryIdx = RelocAddress / EntrySize;
6502 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
6503 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
6504
6505 if (OffsetInEntry == 0)
6506 Entry.FunctionReloc = Reloc;
6507 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
6508 Entry.PersonalityReloc = Reloc;
6509 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
6510 Entry.LSDAReloc = Reloc;
6511 else
6512 llvm_unreachable("Unexpected relocation in __compact_unwind section");
6513 }
6514
6515 // Finally, we're ready to print the data we've gathered.
6516 outs() << "Contents of __compact_unwind section:\n";
6517 for (auto &Entry : CompactUnwinds) {
Tim Northover06af2602014-08-08 12:08:51 +00006518 outs() << " Entry at offset "
6519 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00006520
6521 // 1. Start of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006522 outs() << " start: " << format("0x%" PRIx64,
6523 Entry.FunctionAddr) << ' ';
6524 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
Tim Northover4bd286a2014-08-01 13:07:19 +00006525 outs() << '\n';
6526
6527 // 2. Length of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006528 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
6529 << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00006530 // 3. The 32-bit compact encoding.
6531 outs() << " compact encoding: "
Tim Northoverb911bf82014-08-08 12:00:09 +00006532 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00006533
6534 // 4. The personality function, if present.
Rafael Espindola854038e2015-06-26 14:51:16 +00006535 if (Entry.PersonalityReloc.getObject()) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006536 outs() << " personality function: "
Tim Northoverb911bf82014-08-08 12:00:09 +00006537 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00006538 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
6539 Entry.PersonalityAddr);
6540 outs() << '\n';
6541 }
6542
6543 // 5. This entry's language-specific data area.
Rafael Espindola854038e2015-06-26 14:51:16 +00006544 if (Entry.LSDAReloc.getObject()) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006545 outs() << " LSDA: " << format("0x%" PRIx64,
6546 Entry.LSDAAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00006547 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
6548 outs() << '\n';
6549 }
6550 }
6551}
6552
Tim Northover39c70bb2014-08-12 11:52:59 +00006553//===----------------------------------------------------------------------===//
6554// __unwind_info section dumping
6555//===----------------------------------------------------------------------===//
6556
6557static void printRegularSecondLevelUnwindPage(const char *PageStart) {
6558 const char *Pos = PageStart;
6559 uint32_t Kind = readNext<uint32_t>(Pos);
6560 (void)Kind;
6561 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
6562
6563 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6564 uint16_t NumEntries = readNext<uint16_t>(Pos);
6565
6566 Pos = PageStart + EntriesStart;
6567 for (unsigned i = 0; i < NumEntries; ++i) {
6568 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6569 uint32_t Encoding = readNext<uint32_t>(Pos);
6570
6571 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006572 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6573 << ", "
6574 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006575 }
6576}
6577
6578static void printCompressedSecondLevelUnwindPage(
6579 const char *PageStart, uint32_t FunctionBase,
6580 const SmallVectorImpl<uint32_t> &CommonEncodings) {
6581 const char *Pos = PageStart;
6582 uint32_t Kind = readNext<uint32_t>(Pos);
6583 (void)Kind;
6584 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
6585
6586 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6587 uint16_t NumEntries = readNext<uint16_t>(Pos);
6588
6589 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
6590 readNext<uint16_t>(Pos);
Aaron Ballman80930af2014-08-14 13:53:19 +00006591 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
6592 PageStart + EncodingsStart);
Tim Northover39c70bb2014-08-12 11:52:59 +00006593
6594 Pos = PageStart + EntriesStart;
6595 for (unsigned i = 0; i < NumEntries; ++i) {
6596 uint32_t Entry = readNext<uint32_t>(Pos);
6597 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
6598 uint32_t EncodingIdx = Entry >> 24;
6599
6600 uint32_t Encoding;
6601 if (EncodingIdx < CommonEncodings.size())
6602 Encoding = CommonEncodings[EncodingIdx];
6603 else
6604 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
6605
6606 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006607 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6608 << ", "
6609 << "encoding[" << EncodingIdx
6610 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006611 }
6612}
6613
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006614static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
6615 std::map<uint64_t, SymbolRef> &Symbols,
6616 const SectionRef &UnwindInfo) {
Tim Northover39c70bb2014-08-12 11:52:59 +00006617
6618 assert(Obj->isLittleEndian() &&
6619 "There should not be a big-endian .o with __unwind_info");
6620
6621 outs() << "Contents of __unwind_info section:\n";
6622
6623 StringRef Contents;
6624 UnwindInfo.getContents(Contents);
6625 const char *Pos = Contents.data();
6626
6627 //===----------------------------------
6628 // Section header
6629 //===----------------------------------
6630
6631 uint32_t Version = readNext<uint32_t>(Pos);
6632 outs() << " Version: "
6633 << format("0x%" PRIx32, Version) << '\n';
6634 assert(Version == 1 && "only understand version 1");
6635
6636 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
6637 outs() << " Common encodings array section offset: "
6638 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
6639 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
6640 outs() << " Number of common encodings in array: "
6641 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
6642
6643 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
6644 outs() << " Personality function array section offset: "
6645 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
6646 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
6647 outs() << " Number of personality functions in array: "
6648 << format("0x%" PRIx32, NumPersonalities) << '\n';
6649
6650 uint32_t IndicesStart = readNext<uint32_t>(Pos);
6651 outs() << " Index array section offset: "
6652 << format("0x%" PRIx32, IndicesStart) << '\n';
6653 uint32_t NumIndices = readNext<uint32_t>(Pos);
6654 outs() << " Number of indices in array: "
6655 << format("0x%" PRIx32, NumIndices) << '\n';
6656
6657 //===----------------------------------
6658 // A shared list of common encodings
6659 //===----------------------------------
6660
6661 // These occupy indices in the range [0, N] whenever an encoding is referenced
6662 // from a compressed 2nd level index table. In practice the linker only
6663 // creates ~128 of these, so that indices are available to embed encodings in
6664 // the 2nd level index.
6665
6666 SmallVector<uint32_t, 64> CommonEncodings;
6667 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
6668 Pos = Contents.data() + CommonEncodingsStart;
6669 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
6670 uint32_t Encoding = readNext<uint32_t>(Pos);
6671 CommonEncodings.push_back(Encoding);
6672
6673 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
6674 << '\n';
6675 }
6676
Tim Northover39c70bb2014-08-12 11:52:59 +00006677 //===----------------------------------
6678 // Personality functions used in this executable
6679 //===----------------------------------
6680
6681 // There should be only a handful of these (one per source language,
6682 // roughly). Particularly since they only get 2 bits in the compact encoding.
6683
6684 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
6685 Pos = Contents.data() + PersonalitiesStart;
6686 for (unsigned i = 0; i < NumPersonalities; ++i) {
6687 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
6688 outs() << " personality[" << i + 1
6689 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
6690 }
6691
6692 //===----------------------------------
6693 // The level 1 index entries
6694 //===----------------------------------
6695
6696 // These specify an approximate place to start searching for the more detailed
6697 // information, sorted by PC.
6698
6699 struct IndexEntry {
6700 uint32_t FunctionOffset;
6701 uint32_t SecondLevelPageStart;
6702 uint32_t LSDAStart;
6703 };
6704
6705 SmallVector<IndexEntry, 4> IndexEntries;
6706
6707 outs() << " Top level indices: (count = " << NumIndices << ")\n";
6708 Pos = Contents.data() + IndicesStart;
6709 for (unsigned i = 0; i < NumIndices; ++i) {
6710 IndexEntry Entry;
6711
6712 Entry.FunctionOffset = readNext<uint32_t>(Pos);
6713 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
6714 Entry.LSDAStart = readNext<uint32_t>(Pos);
6715 IndexEntries.push_back(Entry);
6716
6717 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006718 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
6719 << ", "
Tim Northover39c70bb2014-08-12 11:52:59 +00006720 << "2nd level page offset="
6721 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006722 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006723 }
6724
Tim Northover39c70bb2014-08-12 11:52:59 +00006725 //===----------------------------------
6726 // Next come the LSDA tables
6727 //===----------------------------------
6728
6729 // The LSDA layout is rather implicit: it's a contiguous array of entries from
6730 // the first top-level index's LSDAOffset to the last (sentinel).
6731
6732 outs() << " LSDA descriptors:\n";
6733 Pos = Contents.data() + IndexEntries[0].LSDAStart;
6734 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
6735 (2 * sizeof(uint32_t));
6736 for (int i = 0; i < NumLSDAs; ++i) {
6737 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6738 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
6739 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006740 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6741 << ", "
6742 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006743 }
6744
6745 //===----------------------------------
6746 // Finally, the 2nd level indices
6747 //===----------------------------------
6748
6749 // Generally these are 4K in size, and have 2 possible forms:
6750 // + Regular stores up to 511 entries with disparate encodings
6751 // + Compressed stores up to 1021 entries if few enough compact encoding
6752 // values are used.
6753 outs() << " Second level indices:\n";
6754 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
6755 // The final sentinel top-level index has no associated 2nd level page
6756 if (IndexEntries[i].SecondLevelPageStart == 0)
6757 break;
6758
6759 outs() << " Second level index[" << i << "]: "
6760 << "offset in section="
6761 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
6762 << ", "
6763 << "base function offset="
6764 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
6765
6766 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
Aaron Ballman80930af2014-08-14 13:53:19 +00006767 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00006768 if (Kind == 2)
6769 printRegularSecondLevelUnwindPage(Pos);
6770 else if (Kind == 3)
6771 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
6772 CommonEncodings);
6773 else
6774 llvm_unreachable("Do not know how to print this kind of 2nd level page");
Tim Northover39c70bb2014-08-12 11:52:59 +00006775 }
6776}
6777
Tim Northover4bd286a2014-08-01 13:07:19 +00006778void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
6779 std::map<uint64_t, SymbolRef> Symbols;
6780 for (const SymbolRef &SymRef : Obj->symbols()) {
6781 // Discard any undefined or absolute symbols. They're not going to take part
6782 // in the convenience lookup for unwind info and just take up resources.
6783 section_iterator Section = Obj->section_end();
6784 SymRef.getSection(Section);
6785 if (Section == Obj->section_end())
6786 continue;
6787
Rafael Espindoladea00162015-07-03 17:44:18 +00006788 uint64_t Addr = SymRef.getValue();
Tim Northover4bd286a2014-08-01 13:07:19 +00006789 Symbols.insert(std::make_pair(Addr, SymRef));
6790 }
6791
6792 for (const SectionRef &Section : Obj->sections()) {
6793 StringRef SectName;
6794 Section.getName(SectName);
6795 if (SectName == "__compact_unwind")
6796 printMachOCompactUnwindSection(Obj, Symbols, Section);
6797 else if (SectName == "__unwind_info")
Tim Northover39c70bb2014-08-12 11:52:59 +00006798 printMachOUnwindInfoSection(Obj, Symbols, Section);
Tim Northover4bd286a2014-08-01 13:07:19 +00006799 else if (SectName == "__eh_frame")
6800 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00006801 }
6802}
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006803
6804static void PrintMachHeader(uint32_t magic, uint32_t cputype,
6805 uint32_t cpusubtype, uint32_t filetype,
6806 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
6807 bool verbose) {
6808 outs() << "Mach header\n";
6809 outs() << " magic cputype cpusubtype caps filetype ncmds "
6810 "sizeofcmds flags\n";
6811 if (verbose) {
6812 if (magic == MachO::MH_MAGIC)
6813 outs() << " MH_MAGIC";
6814 else if (magic == MachO::MH_MAGIC_64)
6815 outs() << "MH_MAGIC_64";
6816 else
6817 outs() << format(" 0x%08" PRIx32, magic);
6818 switch (cputype) {
6819 case MachO::CPU_TYPE_I386:
6820 outs() << " I386";
6821 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6822 case MachO::CPU_SUBTYPE_I386_ALL:
6823 outs() << " ALL";
6824 break;
6825 default:
6826 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6827 break;
6828 }
6829 break;
6830 case MachO::CPU_TYPE_X86_64:
6831 outs() << " X86_64";
Kevin Enderby131d1772015-01-09 19:22:37 +00006832 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6833 case MachO::CPU_SUBTYPE_X86_64_ALL:
6834 outs() << " ALL";
6835 break;
6836 case MachO::CPU_SUBTYPE_X86_64_H:
6837 outs() << " Haswell";
6838 break;
6839 default:
6840 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6841 break;
6842 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006843 break;
6844 case MachO::CPU_TYPE_ARM:
6845 outs() << " ARM";
6846 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6847 case MachO::CPU_SUBTYPE_ARM_ALL:
6848 outs() << " ALL";
6849 break;
6850 case MachO::CPU_SUBTYPE_ARM_V4T:
6851 outs() << " V4T";
6852 break;
6853 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6854 outs() << " V5TEJ";
6855 break;
6856 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6857 outs() << " XSCALE";
6858 break;
6859 case MachO::CPU_SUBTYPE_ARM_V6:
6860 outs() << " V6";
6861 break;
6862 case MachO::CPU_SUBTYPE_ARM_V6M:
6863 outs() << " V6M";
6864 break;
6865 case MachO::CPU_SUBTYPE_ARM_V7:
6866 outs() << " V7";
6867 break;
6868 case MachO::CPU_SUBTYPE_ARM_V7EM:
6869 outs() << " V7EM";
6870 break;
6871 case MachO::CPU_SUBTYPE_ARM_V7K:
6872 outs() << " V7K";
6873 break;
6874 case MachO::CPU_SUBTYPE_ARM_V7M:
6875 outs() << " V7M";
6876 break;
6877 case MachO::CPU_SUBTYPE_ARM_V7S:
6878 outs() << " V7S";
6879 break;
6880 default:
6881 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6882 break;
6883 }
6884 break;
6885 case MachO::CPU_TYPE_ARM64:
6886 outs() << " ARM64";
6887 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6888 case MachO::CPU_SUBTYPE_ARM64_ALL:
6889 outs() << " ALL";
6890 break;
6891 default:
6892 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6893 break;
6894 }
6895 break;
6896 case MachO::CPU_TYPE_POWERPC:
6897 outs() << " PPC";
6898 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6899 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6900 outs() << " ALL";
6901 break;
6902 default:
6903 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6904 break;
6905 }
6906 break;
6907 case MachO::CPU_TYPE_POWERPC64:
6908 outs() << " PPC64";
6909 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6910 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6911 outs() << " ALL";
6912 break;
6913 default:
6914 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6915 break;
6916 }
6917 break;
6918 }
6919 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00006920 outs() << " LIB64";
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006921 } else {
6922 outs() << format(" 0x%02" PRIx32,
6923 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6924 }
6925 switch (filetype) {
6926 case MachO::MH_OBJECT:
6927 outs() << " OBJECT";
6928 break;
6929 case MachO::MH_EXECUTE:
6930 outs() << " EXECUTE";
6931 break;
6932 case MachO::MH_FVMLIB:
6933 outs() << " FVMLIB";
6934 break;
6935 case MachO::MH_CORE:
6936 outs() << " CORE";
6937 break;
6938 case MachO::MH_PRELOAD:
6939 outs() << " PRELOAD";
6940 break;
6941 case MachO::MH_DYLIB:
6942 outs() << " DYLIB";
6943 break;
6944 case MachO::MH_DYLIB_STUB:
6945 outs() << " DYLIB_STUB";
6946 break;
6947 case MachO::MH_DYLINKER:
6948 outs() << " DYLINKER";
6949 break;
6950 case MachO::MH_BUNDLE:
6951 outs() << " BUNDLE";
6952 break;
6953 case MachO::MH_DSYM:
6954 outs() << " DSYM";
6955 break;
6956 case MachO::MH_KEXT_BUNDLE:
6957 outs() << " KEXTBUNDLE";
6958 break;
6959 default:
6960 outs() << format(" %10u", filetype);
6961 break;
6962 }
6963 outs() << format(" %5u", ncmds);
6964 outs() << format(" %10u", sizeofcmds);
6965 uint32_t f = flags;
6966 if (f & MachO::MH_NOUNDEFS) {
6967 outs() << " NOUNDEFS";
6968 f &= ~MachO::MH_NOUNDEFS;
6969 }
6970 if (f & MachO::MH_INCRLINK) {
6971 outs() << " INCRLINK";
6972 f &= ~MachO::MH_INCRLINK;
6973 }
6974 if (f & MachO::MH_DYLDLINK) {
6975 outs() << " DYLDLINK";
6976 f &= ~MachO::MH_DYLDLINK;
6977 }
6978 if (f & MachO::MH_BINDATLOAD) {
6979 outs() << " BINDATLOAD";
6980 f &= ~MachO::MH_BINDATLOAD;
6981 }
6982 if (f & MachO::MH_PREBOUND) {
6983 outs() << " PREBOUND";
6984 f &= ~MachO::MH_PREBOUND;
6985 }
6986 if (f & MachO::MH_SPLIT_SEGS) {
6987 outs() << " SPLIT_SEGS";
6988 f &= ~MachO::MH_SPLIT_SEGS;
6989 }
6990 if (f & MachO::MH_LAZY_INIT) {
6991 outs() << " LAZY_INIT";
6992 f &= ~MachO::MH_LAZY_INIT;
6993 }
6994 if (f & MachO::MH_TWOLEVEL) {
6995 outs() << " TWOLEVEL";
6996 f &= ~MachO::MH_TWOLEVEL;
6997 }
6998 if (f & MachO::MH_FORCE_FLAT) {
6999 outs() << " FORCE_FLAT";
7000 f &= ~MachO::MH_FORCE_FLAT;
7001 }
7002 if (f & MachO::MH_NOMULTIDEFS) {
7003 outs() << " NOMULTIDEFS";
7004 f &= ~MachO::MH_NOMULTIDEFS;
7005 }
7006 if (f & MachO::MH_NOFIXPREBINDING) {
7007 outs() << " NOFIXPREBINDING";
7008 f &= ~MachO::MH_NOFIXPREBINDING;
7009 }
7010 if (f & MachO::MH_PREBINDABLE) {
7011 outs() << " PREBINDABLE";
7012 f &= ~MachO::MH_PREBINDABLE;
7013 }
7014 if (f & MachO::MH_ALLMODSBOUND) {
7015 outs() << " ALLMODSBOUND";
7016 f &= ~MachO::MH_ALLMODSBOUND;
7017 }
7018 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7019 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7020 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7021 }
7022 if (f & MachO::MH_CANONICAL) {
7023 outs() << " CANONICAL";
7024 f &= ~MachO::MH_CANONICAL;
7025 }
7026 if (f & MachO::MH_WEAK_DEFINES) {
7027 outs() << " WEAK_DEFINES";
7028 f &= ~MachO::MH_WEAK_DEFINES;
7029 }
7030 if (f & MachO::MH_BINDS_TO_WEAK) {
7031 outs() << " BINDS_TO_WEAK";
7032 f &= ~MachO::MH_BINDS_TO_WEAK;
7033 }
7034 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7035 outs() << " ALLOW_STACK_EXECUTION";
7036 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7037 }
7038 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7039 outs() << " DEAD_STRIPPABLE_DYLIB";
7040 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7041 }
7042 if (f & MachO::MH_PIE) {
7043 outs() << " PIE";
7044 f &= ~MachO::MH_PIE;
7045 }
7046 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7047 outs() << " NO_REEXPORTED_DYLIBS";
7048 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7049 }
7050 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7051 outs() << " MH_HAS_TLV_DESCRIPTORS";
7052 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7053 }
7054 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7055 outs() << " MH_NO_HEAP_EXECUTION";
7056 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7057 }
7058 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7059 outs() << " APP_EXTENSION_SAFE";
7060 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7061 }
7062 if (f != 0 || flags == 0)
7063 outs() << format(" 0x%08" PRIx32, f);
7064 } else {
7065 outs() << format(" 0x%08" PRIx32, magic);
7066 outs() << format(" %7d", cputype);
7067 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7068 outs() << format(" 0x%02" PRIx32,
7069 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7070 outs() << format(" %10u", filetype);
7071 outs() << format(" %5u", ncmds);
7072 outs() << format(" %10u", sizeofcmds);
7073 outs() << format(" 0x%08" PRIx32, flags);
7074 }
7075 outs() << "\n";
7076}
7077
Kevin Enderby956366c2014-08-29 22:30:52 +00007078static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7079 StringRef SegName, uint64_t vmaddr,
7080 uint64_t vmsize, uint64_t fileoff,
7081 uint64_t filesize, uint32_t maxprot,
7082 uint32_t initprot, uint32_t nsects,
7083 uint32_t flags, uint32_t object_size,
7084 bool verbose) {
7085 uint64_t expected_cmdsize;
7086 if (cmd == MachO::LC_SEGMENT) {
7087 outs() << " cmd LC_SEGMENT\n";
7088 expected_cmdsize = nsects;
7089 expected_cmdsize *= sizeof(struct MachO::section);
7090 expected_cmdsize += sizeof(struct MachO::segment_command);
7091 } else {
7092 outs() << " cmd LC_SEGMENT_64\n";
7093 expected_cmdsize = nsects;
7094 expected_cmdsize *= sizeof(struct MachO::section_64);
7095 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7096 }
7097 outs() << " cmdsize " << cmdsize;
7098 if (cmdsize != expected_cmdsize)
7099 outs() << " Inconsistent size\n";
7100 else
7101 outs() << "\n";
7102 outs() << " segname " << SegName << "\n";
7103 if (cmd == MachO::LC_SEGMENT_64) {
7104 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7105 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7106 } else {
Kevin Enderbyadb7c432014-12-16 18:58:11 +00007107 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7108 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007109 }
7110 outs() << " fileoff " << fileoff;
7111 if (fileoff > object_size)
7112 outs() << " (past end of file)\n";
7113 else
7114 outs() << "\n";
7115 outs() << " filesize " << filesize;
7116 if (fileoff + filesize > object_size)
7117 outs() << " (past end of file)\n";
7118 else
7119 outs() << "\n";
7120 if (verbose) {
7121 if ((maxprot &
7122 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7123 MachO::VM_PROT_EXECUTE)) != 0)
7124 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7125 else {
7126 if (maxprot & MachO::VM_PROT_READ)
7127 outs() << " maxprot r";
7128 else
7129 outs() << " maxprot -";
7130 if (maxprot & MachO::VM_PROT_WRITE)
7131 outs() << "w";
7132 else
7133 outs() << "-";
7134 if (maxprot & MachO::VM_PROT_EXECUTE)
7135 outs() << "x\n";
7136 else
7137 outs() << "-\n";
7138 }
7139 if ((initprot &
7140 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7141 MachO::VM_PROT_EXECUTE)) != 0)
7142 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7143 else {
7144 if (initprot & MachO::VM_PROT_READ)
7145 outs() << " initprot r";
7146 else
7147 outs() << " initprot -";
7148 if (initprot & MachO::VM_PROT_WRITE)
7149 outs() << "w";
7150 else
7151 outs() << "-";
7152 if (initprot & MachO::VM_PROT_EXECUTE)
7153 outs() << "x\n";
7154 else
7155 outs() << "-\n";
7156 }
7157 } else {
7158 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7159 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7160 }
7161 outs() << " nsects " << nsects << "\n";
7162 if (verbose) {
7163 outs() << " flags";
7164 if (flags == 0)
7165 outs() << " (none)\n";
7166 else {
7167 if (flags & MachO::SG_HIGHVM) {
7168 outs() << " HIGHVM";
7169 flags &= ~MachO::SG_HIGHVM;
7170 }
7171 if (flags & MachO::SG_FVMLIB) {
7172 outs() << " FVMLIB";
7173 flags &= ~MachO::SG_FVMLIB;
7174 }
7175 if (flags & MachO::SG_NORELOC) {
7176 outs() << " NORELOC";
7177 flags &= ~MachO::SG_NORELOC;
7178 }
7179 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7180 outs() << " PROTECTED_VERSION_1";
7181 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7182 }
7183 if (flags)
7184 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7185 else
7186 outs() << "\n";
7187 }
7188 } else {
7189 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7190 }
7191}
7192
7193static void PrintSection(const char *sectname, const char *segname,
7194 uint64_t addr, uint64_t size, uint32_t offset,
7195 uint32_t align, uint32_t reloff, uint32_t nreloc,
7196 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7197 uint32_t cmd, const char *sg_segname,
7198 uint32_t filetype, uint32_t object_size,
7199 bool verbose) {
7200 outs() << "Section\n";
7201 outs() << " sectname " << format("%.16s\n", sectname);
7202 outs() << " segname " << format("%.16s", segname);
7203 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7204 outs() << " (does not match segment)\n";
7205 else
7206 outs() << "\n";
7207 if (cmd == MachO::LC_SEGMENT_64) {
7208 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7209 outs() << " size " << format("0x%016" PRIx64, size);
7210 } else {
Kevin Enderby75594b62014-12-16 21:00:25 +00007211 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7212 outs() << " size " << format("0x%08" PRIx64, size);
Kevin Enderby956366c2014-08-29 22:30:52 +00007213 }
7214 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7215 outs() << " (past end of file)\n";
7216 else
7217 outs() << "\n";
7218 outs() << " offset " << offset;
7219 if (offset > object_size)
7220 outs() << " (past end of file)\n";
7221 else
7222 outs() << "\n";
7223 uint32_t align_shifted = 1 << align;
7224 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7225 outs() << " reloff " << reloff;
7226 if (reloff > object_size)
7227 outs() << " (past end of file)\n";
7228 else
7229 outs() << "\n";
7230 outs() << " nreloc " << nreloc;
7231 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7232 outs() << " (past end of file)\n";
7233 else
7234 outs() << "\n";
7235 uint32_t section_type = flags & MachO::SECTION_TYPE;
7236 if (verbose) {
7237 outs() << " type";
7238 if (section_type == MachO::S_REGULAR)
7239 outs() << " S_REGULAR\n";
7240 else if (section_type == MachO::S_ZEROFILL)
7241 outs() << " S_ZEROFILL\n";
7242 else if (section_type == MachO::S_CSTRING_LITERALS)
7243 outs() << " S_CSTRING_LITERALS\n";
7244 else if (section_type == MachO::S_4BYTE_LITERALS)
7245 outs() << " S_4BYTE_LITERALS\n";
7246 else if (section_type == MachO::S_8BYTE_LITERALS)
7247 outs() << " S_8BYTE_LITERALS\n";
7248 else if (section_type == MachO::S_16BYTE_LITERALS)
7249 outs() << " S_16BYTE_LITERALS\n";
7250 else if (section_type == MachO::S_LITERAL_POINTERS)
7251 outs() << " S_LITERAL_POINTERS\n";
7252 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7253 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7254 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7255 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7256 else if (section_type == MachO::S_SYMBOL_STUBS)
7257 outs() << " S_SYMBOL_STUBS\n";
7258 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7259 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7260 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7261 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7262 else if (section_type == MachO::S_COALESCED)
7263 outs() << " S_COALESCED\n";
7264 else if (section_type == MachO::S_INTERPOSING)
7265 outs() << " S_INTERPOSING\n";
7266 else if (section_type == MachO::S_DTRACE_DOF)
7267 outs() << " S_DTRACE_DOF\n";
7268 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7269 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7270 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7271 outs() << " S_THREAD_LOCAL_REGULAR\n";
7272 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7273 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7274 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7275 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7276 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7277 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7278 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7279 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7280 else
7281 outs() << format("0x%08" PRIx32, section_type) << "\n";
7282 outs() << "attributes";
7283 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7284 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7285 outs() << " PURE_INSTRUCTIONS";
7286 if (section_attributes & MachO::S_ATTR_NO_TOC)
7287 outs() << " NO_TOC";
7288 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7289 outs() << " STRIP_STATIC_SYMS";
7290 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7291 outs() << " NO_DEAD_STRIP";
7292 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7293 outs() << " LIVE_SUPPORT";
7294 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7295 outs() << " SELF_MODIFYING_CODE";
7296 if (section_attributes & MachO::S_ATTR_DEBUG)
7297 outs() << " DEBUG";
7298 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7299 outs() << " SOME_INSTRUCTIONS";
7300 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7301 outs() << " EXT_RELOC";
7302 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7303 outs() << " LOC_RELOC";
7304 if (section_attributes == 0)
7305 outs() << " (none)";
7306 outs() << "\n";
7307 } else
7308 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7309 outs() << " reserved1 " << reserved1;
7310 if (section_type == MachO::S_SYMBOL_STUBS ||
7311 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7312 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7313 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7314 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7315 outs() << " (index into indirect symbol table)\n";
7316 else
7317 outs() << "\n";
7318 outs() << " reserved2 " << reserved2;
7319 if (section_type == MachO::S_SYMBOL_STUBS)
7320 outs() << " (size of stubs)\n";
7321 else
7322 outs() << "\n";
7323}
7324
David Majnemer73cc6ff2014-11-13 19:48:56 +00007325static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
Kevin Enderby956366c2014-08-29 22:30:52 +00007326 uint32_t object_size) {
7327 outs() << " cmd LC_SYMTAB\n";
7328 outs() << " cmdsize " << st.cmdsize;
7329 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7330 outs() << " Incorrect size\n";
7331 else
7332 outs() << "\n";
7333 outs() << " symoff " << st.symoff;
7334 if (st.symoff > object_size)
7335 outs() << " (past end of file)\n";
7336 else
7337 outs() << "\n";
7338 outs() << " nsyms " << st.nsyms;
7339 uint64_t big_size;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007340 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007341 big_size = st.nsyms;
7342 big_size *= sizeof(struct MachO::nlist_64);
7343 big_size += st.symoff;
7344 if (big_size > object_size)
7345 outs() << " (past end of file)\n";
7346 else
7347 outs() << "\n";
7348 } else {
7349 big_size = st.nsyms;
7350 big_size *= sizeof(struct MachO::nlist);
7351 big_size += st.symoff;
7352 if (big_size > object_size)
7353 outs() << " (past end of file)\n";
7354 else
7355 outs() << "\n";
7356 }
7357 outs() << " stroff " << st.stroff;
7358 if (st.stroff > object_size)
7359 outs() << " (past end of file)\n";
7360 else
7361 outs() << "\n";
7362 outs() << " strsize " << st.strsize;
7363 big_size = st.stroff;
7364 big_size += st.strsize;
7365 if (big_size > object_size)
7366 outs() << " (past end of file)\n";
7367 else
7368 outs() << "\n";
7369}
7370
7371static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7372 uint32_t nsyms, uint32_t object_size,
David Majnemer73cc6ff2014-11-13 19:48:56 +00007373 bool Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007374 outs() << " cmd LC_DYSYMTAB\n";
7375 outs() << " cmdsize " << dyst.cmdsize;
7376 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7377 outs() << " Incorrect size\n";
7378 else
7379 outs() << "\n";
7380 outs() << " ilocalsym " << dyst.ilocalsym;
7381 if (dyst.ilocalsym > nsyms)
7382 outs() << " (greater than the number of symbols)\n";
7383 else
7384 outs() << "\n";
7385 outs() << " nlocalsym " << dyst.nlocalsym;
7386 uint64_t big_size;
7387 big_size = dyst.ilocalsym;
7388 big_size += dyst.nlocalsym;
7389 if (big_size > nsyms)
7390 outs() << " (past the end of the symbol table)\n";
7391 else
7392 outs() << "\n";
7393 outs() << " iextdefsym " << dyst.iextdefsym;
7394 if (dyst.iextdefsym > nsyms)
7395 outs() << " (greater than the number of symbols)\n";
7396 else
7397 outs() << "\n";
7398 outs() << " nextdefsym " << dyst.nextdefsym;
7399 big_size = dyst.iextdefsym;
7400 big_size += dyst.nextdefsym;
7401 if (big_size > nsyms)
7402 outs() << " (past the end of the symbol table)\n";
7403 else
7404 outs() << "\n";
7405 outs() << " iundefsym " << dyst.iundefsym;
7406 if (dyst.iundefsym > nsyms)
7407 outs() << " (greater than the number of symbols)\n";
7408 else
7409 outs() << "\n";
7410 outs() << " nundefsym " << dyst.nundefsym;
7411 big_size = dyst.iundefsym;
7412 big_size += dyst.nundefsym;
7413 if (big_size > nsyms)
7414 outs() << " (past the end of the symbol table)\n";
7415 else
7416 outs() << "\n";
7417 outs() << " tocoff " << dyst.tocoff;
7418 if (dyst.tocoff > object_size)
7419 outs() << " (past end of file)\n";
7420 else
7421 outs() << "\n";
7422 outs() << " ntoc " << dyst.ntoc;
7423 big_size = dyst.ntoc;
7424 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7425 big_size += dyst.tocoff;
7426 if (big_size > object_size)
7427 outs() << " (past end of file)\n";
7428 else
7429 outs() << "\n";
7430 outs() << " modtaboff " << dyst.modtaboff;
7431 if (dyst.modtaboff > object_size)
7432 outs() << " (past end of file)\n";
7433 else
7434 outs() << "\n";
7435 outs() << " nmodtab " << dyst.nmodtab;
7436 uint64_t modtabend;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007437 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007438 modtabend = dyst.nmodtab;
7439 modtabend *= sizeof(struct MachO::dylib_module_64);
7440 modtabend += dyst.modtaboff;
7441 } else {
7442 modtabend = dyst.nmodtab;
7443 modtabend *= sizeof(struct MachO::dylib_module);
7444 modtabend += dyst.modtaboff;
7445 }
7446 if (modtabend > object_size)
7447 outs() << " (past end of file)\n";
7448 else
7449 outs() << "\n";
7450 outs() << " extrefsymoff " << dyst.extrefsymoff;
7451 if (dyst.extrefsymoff > object_size)
7452 outs() << " (past end of file)\n";
7453 else
7454 outs() << "\n";
7455 outs() << " nextrefsyms " << dyst.nextrefsyms;
7456 big_size = dyst.nextrefsyms;
7457 big_size *= sizeof(struct MachO::dylib_reference);
7458 big_size += dyst.extrefsymoff;
7459 if (big_size > object_size)
7460 outs() << " (past end of file)\n";
7461 else
7462 outs() << "\n";
7463 outs() << " indirectsymoff " << dyst.indirectsymoff;
7464 if (dyst.indirectsymoff > object_size)
7465 outs() << " (past end of file)\n";
7466 else
7467 outs() << "\n";
7468 outs() << " nindirectsyms " << dyst.nindirectsyms;
7469 big_size = dyst.nindirectsyms;
7470 big_size *= sizeof(uint32_t);
7471 big_size += dyst.indirectsymoff;
7472 if (big_size > object_size)
7473 outs() << " (past end of file)\n";
7474 else
7475 outs() << "\n";
7476 outs() << " extreloff " << dyst.extreloff;
7477 if (dyst.extreloff > object_size)
7478 outs() << " (past end of file)\n";
7479 else
7480 outs() << "\n";
7481 outs() << " nextrel " << dyst.nextrel;
7482 big_size = dyst.nextrel;
7483 big_size *= sizeof(struct MachO::relocation_info);
7484 big_size += dyst.extreloff;
7485 if (big_size > object_size)
7486 outs() << " (past end of file)\n";
7487 else
7488 outs() << "\n";
7489 outs() << " locreloff " << dyst.locreloff;
7490 if (dyst.locreloff > object_size)
7491 outs() << " (past end of file)\n";
7492 else
7493 outs() << "\n";
7494 outs() << " nlocrel " << dyst.nlocrel;
7495 big_size = dyst.nlocrel;
7496 big_size *= sizeof(struct MachO::relocation_info);
7497 big_size += dyst.locreloff;
7498 if (big_size > object_size)
7499 outs() << " (past end of file)\n";
7500 else
7501 outs() << "\n";
7502}
7503
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007504static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
7505 uint32_t object_size) {
7506 if (dc.cmd == MachO::LC_DYLD_INFO)
7507 outs() << " cmd LC_DYLD_INFO\n";
7508 else
7509 outs() << " cmd LC_DYLD_INFO_ONLY\n";
7510 outs() << " cmdsize " << dc.cmdsize;
7511 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
7512 outs() << " Incorrect size\n";
7513 else
7514 outs() << "\n";
7515 outs() << " rebase_off " << dc.rebase_off;
7516 if (dc.rebase_off > object_size)
7517 outs() << " (past end of file)\n";
7518 else
7519 outs() << "\n";
7520 outs() << " rebase_size " << dc.rebase_size;
7521 uint64_t big_size;
7522 big_size = dc.rebase_off;
7523 big_size += dc.rebase_size;
7524 if (big_size > object_size)
7525 outs() << " (past end of file)\n";
7526 else
7527 outs() << "\n";
7528 outs() << " bind_off " << dc.bind_off;
7529 if (dc.bind_off > object_size)
7530 outs() << " (past end of file)\n";
7531 else
7532 outs() << "\n";
7533 outs() << " bind_size " << dc.bind_size;
7534 big_size = dc.bind_off;
7535 big_size += dc.bind_size;
7536 if (big_size > object_size)
7537 outs() << " (past end of file)\n";
7538 else
7539 outs() << "\n";
7540 outs() << " weak_bind_off " << dc.weak_bind_off;
7541 if (dc.weak_bind_off > object_size)
7542 outs() << " (past end of file)\n";
7543 else
7544 outs() << "\n";
7545 outs() << " weak_bind_size " << dc.weak_bind_size;
7546 big_size = dc.weak_bind_off;
7547 big_size += dc.weak_bind_size;
7548 if (big_size > object_size)
7549 outs() << " (past end of file)\n";
7550 else
7551 outs() << "\n";
7552 outs() << " lazy_bind_off " << dc.lazy_bind_off;
7553 if (dc.lazy_bind_off > object_size)
7554 outs() << " (past end of file)\n";
7555 else
7556 outs() << "\n";
7557 outs() << " lazy_bind_size " << dc.lazy_bind_size;
7558 big_size = dc.lazy_bind_off;
7559 big_size += dc.lazy_bind_size;
7560 if (big_size > object_size)
7561 outs() << " (past end of file)\n";
7562 else
7563 outs() << "\n";
7564 outs() << " export_off " << dc.export_off;
7565 if (dc.export_off > object_size)
7566 outs() << " (past end of file)\n";
7567 else
7568 outs() << "\n";
7569 outs() << " export_size " << dc.export_size;
7570 big_size = dc.export_off;
7571 big_size += dc.export_size;
7572 if (big_size > object_size)
7573 outs() << " (past end of file)\n";
7574 else
7575 outs() << "\n";
7576}
7577
7578static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
7579 const char *Ptr) {
7580 if (dyld.cmd == MachO::LC_ID_DYLINKER)
7581 outs() << " cmd LC_ID_DYLINKER\n";
7582 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
7583 outs() << " cmd LC_LOAD_DYLINKER\n";
7584 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
7585 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
7586 else
7587 outs() << " cmd ?(" << dyld.cmd << ")\n";
7588 outs() << " cmdsize " << dyld.cmdsize;
7589 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
7590 outs() << " Incorrect size\n";
7591 else
7592 outs() << "\n";
7593 if (dyld.name >= dyld.cmdsize)
7594 outs() << " name ?(bad offset " << dyld.name << ")\n";
7595 else {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007596 const char *P = (const char *)(Ptr) + dyld.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007597 outs() << " name " << P << " (offset " << dyld.name << ")\n";
7598 }
7599}
7600
7601static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
7602 outs() << " cmd LC_UUID\n";
7603 outs() << " cmdsize " << uuid.cmdsize;
7604 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
7605 outs() << " Incorrect size\n";
7606 else
7607 outs() << "\n";
7608 outs() << " uuid ";
7609 outs() << format("%02" PRIX32, uuid.uuid[0]);
7610 outs() << format("%02" PRIX32, uuid.uuid[1]);
7611 outs() << format("%02" PRIX32, uuid.uuid[2]);
7612 outs() << format("%02" PRIX32, uuid.uuid[3]);
7613 outs() << "-";
7614 outs() << format("%02" PRIX32, uuid.uuid[4]);
7615 outs() << format("%02" PRIX32, uuid.uuid[5]);
7616 outs() << "-";
7617 outs() << format("%02" PRIX32, uuid.uuid[6]);
7618 outs() << format("%02" PRIX32, uuid.uuid[7]);
7619 outs() << "-";
7620 outs() << format("%02" PRIX32, uuid.uuid[8]);
7621 outs() << format("%02" PRIX32, uuid.uuid[9]);
7622 outs() << "-";
7623 outs() << format("%02" PRIX32, uuid.uuid[10]);
7624 outs() << format("%02" PRIX32, uuid.uuid[11]);
7625 outs() << format("%02" PRIX32, uuid.uuid[12]);
7626 outs() << format("%02" PRIX32, uuid.uuid[13]);
7627 outs() << format("%02" PRIX32, uuid.uuid[14]);
7628 outs() << format("%02" PRIX32, uuid.uuid[15]);
7629 outs() << "\n";
7630}
7631
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007632static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00007633 outs() << " cmd LC_RPATH\n";
7634 outs() << " cmdsize " << rpath.cmdsize;
7635 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
7636 outs() << " Incorrect size\n";
7637 else
7638 outs() << "\n";
7639 if (rpath.path >= rpath.cmdsize)
7640 outs() << " path ?(bad offset " << rpath.path << ")\n";
7641 else {
7642 const char *P = (const char *)(Ptr) + rpath.path;
7643 outs() << " path " << P << " (offset " << rpath.path << ")\n";
7644 }
7645}
7646
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007647static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
7648 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
7649 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
7650 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
7651 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
7652 else
7653 outs() << " cmd " << vd.cmd << " (?)\n";
7654 outs() << " cmdsize " << vd.cmdsize;
7655 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
7656 outs() << " Incorrect size\n";
7657 else
7658 outs() << "\n";
7659 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
7660 << ((vd.version >> 8) & 0xff);
7661 if ((vd.version & 0xff) != 0)
7662 outs() << "." << (vd.version & 0xff);
7663 outs() << "\n";
7664 if (vd.sdk == 0)
Kevin Enderby57538292014-12-17 01:01:30 +00007665 outs() << " sdk n/a";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007666 else {
7667 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
7668 << ((vd.sdk >> 8) & 0xff);
7669 }
7670 if ((vd.sdk & 0xff) != 0)
7671 outs() << "." << (vd.sdk & 0xff);
7672 outs() << "\n";
7673}
7674
7675static void PrintSourceVersionCommand(MachO::source_version_command sd) {
7676 outs() << " cmd LC_SOURCE_VERSION\n";
7677 outs() << " cmdsize " << sd.cmdsize;
7678 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
7679 outs() << " Incorrect size\n";
7680 else
7681 outs() << "\n";
7682 uint64_t a = (sd.version >> 40) & 0xffffff;
7683 uint64_t b = (sd.version >> 30) & 0x3ff;
7684 uint64_t c = (sd.version >> 20) & 0x3ff;
7685 uint64_t d = (sd.version >> 10) & 0x3ff;
7686 uint64_t e = sd.version & 0x3ff;
7687 outs() << " version " << a << "." << b;
7688 if (e != 0)
7689 outs() << "." << c << "." << d << "." << e;
7690 else if (d != 0)
7691 outs() << "." << c << "." << d;
7692 else if (c != 0)
7693 outs() << "." << c;
7694 outs() << "\n";
7695}
7696
7697static void PrintEntryPointCommand(MachO::entry_point_command ep) {
7698 outs() << " cmd LC_MAIN\n";
7699 outs() << " cmdsize " << ep.cmdsize;
7700 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
7701 outs() << " Incorrect size\n";
7702 else
7703 outs() << "\n";
7704 outs() << " entryoff " << ep.entryoff << "\n";
7705 outs() << " stacksize " << ep.stacksize << "\n";
7706}
7707
Kevin Enderby0804f4672014-12-16 23:25:52 +00007708static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
7709 uint32_t object_size) {
7710 outs() << " cmd LC_ENCRYPTION_INFO\n";
7711 outs() << " cmdsize " << ec.cmdsize;
7712 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
7713 outs() << " Incorrect size\n";
7714 else
7715 outs() << "\n";
7716 outs() << " cryptoff " << ec.cryptoff;
7717 if (ec.cryptoff > object_size)
7718 outs() << " (past end of file)\n";
7719 else
7720 outs() << "\n";
7721 outs() << " cryptsize " << ec.cryptsize;
7722 if (ec.cryptsize > object_size)
7723 outs() << " (past end of file)\n";
7724 else
7725 outs() << "\n";
7726 outs() << " cryptid " << ec.cryptid << "\n";
7727}
7728
Kevin Enderby57538292014-12-17 01:01:30 +00007729static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007730 uint32_t object_size) {
Kevin Enderby57538292014-12-17 01:01:30 +00007731 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
7732 outs() << " cmdsize " << ec.cmdsize;
7733 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
7734 outs() << " Incorrect size\n";
7735 else
7736 outs() << "\n";
7737 outs() << " cryptoff " << ec.cryptoff;
7738 if (ec.cryptoff > object_size)
7739 outs() << " (past end of file)\n";
7740 else
7741 outs() << "\n";
7742 outs() << " cryptsize " << ec.cryptsize;
7743 if (ec.cryptsize > object_size)
7744 outs() << " (past end of file)\n";
7745 else
7746 outs() << "\n";
7747 outs() << " cryptid " << ec.cryptid << "\n";
7748 outs() << " pad " << ec.pad << "\n";
7749}
7750
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007751static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
7752 const char *Ptr) {
7753 outs() << " cmd LC_LINKER_OPTION\n";
7754 outs() << " cmdsize " << lo.cmdsize;
7755 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
7756 outs() << " Incorrect size\n";
7757 else
7758 outs() << "\n";
7759 outs() << " count " << lo.count << "\n";
7760 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
7761 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
7762 uint32_t i = 0;
7763 while (left > 0) {
7764 while (*string == '\0' && left > 0) {
7765 string++;
7766 left--;
7767 }
7768 if (left > 0) {
7769 i++;
7770 outs() << " string #" << i << " " << format("%.*s\n", left, string);
David Majnemerd4449ed2014-12-20 08:24:43 +00007771 uint32_t NullPos = StringRef(string, left).find('\0');
7772 uint32_t len = std::min(NullPos, left) + 1;
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007773 string += len;
7774 left -= len;
7775 }
7776 }
7777 if (lo.count != i)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007778 outs() << " count " << lo.count << " does not match number of strings "
7779 << i << "\n";
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007780}
7781
Kevin Enderbyb4b79312014-12-18 19:24:35 +00007782static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
7783 const char *Ptr) {
7784 outs() << " cmd LC_SUB_FRAMEWORK\n";
7785 outs() << " cmdsize " << sub.cmdsize;
7786 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
7787 outs() << " Incorrect size\n";
7788 else
7789 outs() << "\n";
7790 if (sub.umbrella < sub.cmdsize) {
7791 const char *P = Ptr + sub.umbrella;
7792 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
7793 } else {
7794 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
7795 }
7796}
7797
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00007798static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
7799 const char *Ptr) {
7800 outs() << " cmd LC_SUB_UMBRELLA\n";
7801 outs() << " cmdsize " << sub.cmdsize;
7802 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
7803 outs() << " Incorrect size\n";
7804 else
7805 outs() << "\n";
7806 if (sub.sub_umbrella < sub.cmdsize) {
7807 const char *P = Ptr + sub.sub_umbrella;
7808 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
7809 } else {
7810 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
7811 }
7812}
7813
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007814static void PrintSubLibraryCommand(MachO::sub_library_command sub,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007815 const char *Ptr) {
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007816 outs() << " cmd LC_SUB_LIBRARY\n";
7817 outs() << " cmdsize " << sub.cmdsize;
7818 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
7819 outs() << " Incorrect size\n";
7820 else
7821 outs() << "\n";
7822 if (sub.sub_library < sub.cmdsize) {
7823 const char *P = Ptr + sub.sub_library;
7824 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7825 } else {
7826 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7827 }
7828}
7829
Kevin Enderby186eac32014-12-19 21:06:24 +00007830static void PrintSubClientCommand(MachO::sub_client_command sub,
7831 const char *Ptr) {
7832 outs() << " cmd LC_SUB_CLIENT\n";
7833 outs() << " cmdsize " << sub.cmdsize;
7834 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7835 outs() << " Incorrect size\n";
7836 else
7837 outs() << "\n";
7838 if (sub.client < sub.cmdsize) {
7839 const char *P = Ptr + sub.client;
7840 outs() << " client " << P << " (offset " << sub.client << ")\n";
7841 } else {
7842 outs() << " client ?(bad offset " << sub.client << ")\n";
7843 }
7844}
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007845
Kevin Enderby52e4ce42014-12-19 22:25:22 +00007846static void PrintRoutinesCommand(MachO::routines_command r) {
7847 outs() << " cmd LC_ROUTINES\n";
7848 outs() << " cmdsize " << r.cmdsize;
7849 if (r.cmdsize != sizeof(struct MachO::routines_command))
7850 outs() << " Incorrect size\n";
7851 else
7852 outs() << "\n";
7853 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7854 outs() << " init_module " << r.init_module << "\n";
7855 outs() << " reserved1 " << r.reserved1 << "\n";
7856 outs() << " reserved2 " << r.reserved2 << "\n";
7857 outs() << " reserved3 " << r.reserved3 << "\n";
7858 outs() << " reserved4 " << r.reserved4 << "\n";
7859 outs() << " reserved5 " << r.reserved5 << "\n";
7860 outs() << " reserved6 " << r.reserved6 << "\n";
7861}
7862
7863static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7864 outs() << " cmd LC_ROUTINES_64\n";
7865 outs() << " cmdsize " << r.cmdsize;
7866 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7867 outs() << " Incorrect size\n";
7868 else
7869 outs() << "\n";
7870 outs() << " init_address " << format("0x%016" PRIx64, 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
Kevin Enderby48ef5342014-12-23 22:56:39 +00007880static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7881 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7882 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7883 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7884 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7885 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7886 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7887 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7888 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7889 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7890 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7891 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7892 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7893 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7894 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7895 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7896 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7897 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7898 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7899 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7900 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7901 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7902}
7903
Kevin Enderby227df342014-12-23 23:43:59 +00007904static void Print_mmst_reg(MachO::mmst_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007905 uint32_t f;
7906 outs() << "\t mmst_reg ";
7907 for (f = 0; f < 10; f++)
7908 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7909 outs() << "\n";
7910 outs() << "\t mmst_rsrv ";
7911 for (f = 0; f < 6; f++)
7912 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7913 outs() << "\n";
7914}
7915
Kevin Enderbyaefb0032014-12-24 00:16:51 +00007916static void Print_xmm_reg(MachO::xmm_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007917 uint32_t f;
7918 outs() << "\t xmm_reg ";
7919 for (f = 0; f < 16; f++)
7920 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7921 outs() << "\n";
7922}
7923
7924static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7925 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7926 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7927 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7928 outs() << " denorm " << fpu.fpu_fcw.denorm;
7929 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7930 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7931 outs() << " undfl " << fpu.fpu_fcw.undfl;
7932 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7933 outs() << "\t\t pc ";
7934 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7935 outs() << "FP_PREC_24B ";
7936 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
7937 outs() << "FP_PREC_53B ";
7938 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
7939 outs() << "FP_PREC_64B ";
7940 else
7941 outs() << fpu.fpu_fcw.pc << " ";
7942 outs() << "rc ";
7943 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
7944 outs() << "FP_RND_NEAR ";
7945 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
7946 outs() << "FP_RND_DOWN ";
7947 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
7948 outs() << "FP_RND_UP ";
7949 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007950 outs() << "FP_CHOP ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00007951 outs() << "\n";
7952 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
7953 outs() << " denorm " << fpu.fpu_fsw.denorm;
7954 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
7955 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
7956 outs() << " undfl " << fpu.fpu_fsw.undfl;
7957 outs() << " precis " << fpu.fpu_fsw.precis;
7958 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
7959 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
7960 outs() << " c0 " << fpu.fpu_fsw.c0;
7961 outs() << " c1 " << fpu.fpu_fsw.c1;
7962 outs() << " c2 " << fpu.fpu_fsw.c2;
7963 outs() << " tos " << fpu.fpu_fsw.tos;
7964 outs() << " c3 " << fpu.fpu_fsw.c3;
7965 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
7966 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
7967 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
7968 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
7969 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
7970 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
7971 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
7972 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
7973 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
7974 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
7975 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
7976 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
7977 outs() << "\n";
7978 outs() << "\t fpu_stmm0:\n";
7979 Print_mmst_reg(fpu.fpu_stmm0);
7980 outs() << "\t fpu_stmm1:\n";
7981 Print_mmst_reg(fpu.fpu_stmm1);
7982 outs() << "\t fpu_stmm2:\n";
7983 Print_mmst_reg(fpu.fpu_stmm2);
7984 outs() << "\t fpu_stmm3:\n";
7985 Print_mmst_reg(fpu.fpu_stmm3);
7986 outs() << "\t fpu_stmm4:\n";
7987 Print_mmst_reg(fpu.fpu_stmm4);
7988 outs() << "\t fpu_stmm5:\n";
7989 Print_mmst_reg(fpu.fpu_stmm5);
7990 outs() << "\t fpu_stmm6:\n";
7991 Print_mmst_reg(fpu.fpu_stmm6);
7992 outs() << "\t fpu_stmm7:\n";
7993 Print_mmst_reg(fpu.fpu_stmm7);
7994 outs() << "\t fpu_xmm0:\n";
7995 Print_xmm_reg(fpu.fpu_xmm0);
7996 outs() << "\t fpu_xmm1:\n";
7997 Print_xmm_reg(fpu.fpu_xmm1);
7998 outs() << "\t fpu_xmm2:\n";
7999 Print_xmm_reg(fpu.fpu_xmm2);
8000 outs() << "\t fpu_xmm3:\n";
8001 Print_xmm_reg(fpu.fpu_xmm3);
8002 outs() << "\t fpu_xmm4:\n";
8003 Print_xmm_reg(fpu.fpu_xmm4);
8004 outs() << "\t fpu_xmm5:\n";
8005 Print_xmm_reg(fpu.fpu_xmm5);
8006 outs() << "\t fpu_xmm6:\n";
8007 Print_xmm_reg(fpu.fpu_xmm6);
8008 outs() << "\t fpu_xmm7:\n";
8009 Print_xmm_reg(fpu.fpu_xmm7);
8010 outs() << "\t fpu_xmm8:\n";
8011 Print_xmm_reg(fpu.fpu_xmm8);
8012 outs() << "\t fpu_xmm9:\n";
8013 Print_xmm_reg(fpu.fpu_xmm9);
8014 outs() << "\t fpu_xmm10:\n";
8015 Print_xmm_reg(fpu.fpu_xmm10);
8016 outs() << "\t fpu_xmm11:\n";
8017 Print_xmm_reg(fpu.fpu_xmm11);
8018 outs() << "\t fpu_xmm12:\n";
8019 Print_xmm_reg(fpu.fpu_xmm12);
8020 outs() << "\t fpu_xmm13:\n";
8021 Print_xmm_reg(fpu.fpu_xmm13);
8022 outs() << "\t fpu_xmm14:\n";
8023 Print_xmm_reg(fpu.fpu_xmm14);
8024 outs() << "\t fpu_xmm15:\n";
8025 Print_xmm_reg(fpu.fpu_xmm15);
8026 outs() << "\t fpu_rsrv4:\n";
8027 for (uint32_t f = 0; f < 6; f++) {
8028 outs() << "\t ";
8029 for (uint32_t g = 0; g < 16; g++)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008030 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008031 outs() << "\n";
8032 }
8033 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8034 outs() << "\n";
8035}
8036
8037static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8038 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8039 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8040 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8041}
8042
8043static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8044 bool isLittleEndian, uint32_t cputype) {
8045 if (t.cmd == MachO::LC_THREAD)
8046 outs() << " cmd LC_THREAD\n";
8047 else if (t.cmd == MachO::LC_UNIXTHREAD)
8048 outs() << " cmd LC_UNIXTHREAD\n";
8049 else
8050 outs() << " cmd " << t.cmd << " (unknown)\n";
8051 outs() << " cmdsize " << t.cmdsize;
8052 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8053 outs() << " Incorrect size\n";
8054 else
8055 outs() << "\n";
8056
8057 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8058 const char *end = Ptr + t.cmdsize;
8059 uint32_t flavor, count, left;
8060 if (cputype == MachO::CPU_TYPE_X86_64) {
8061 while (begin < end) {
8062 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8063 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8064 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008065 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008066 flavor = 0;
8067 begin = end;
8068 }
8069 if (isLittleEndian != sys::IsLittleEndianHost)
8070 sys::swapByteOrder(flavor);
8071 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8072 memcpy((char *)&count, begin, sizeof(uint32_t));
8073 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008074 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008075 count = 0;
8076 begin = end;
8077 }
8078 if (isLittleEndian != sys::IsLittleEndianHost)
8079 sys::swapByteOrder(count);
8080 if (flavor == MachO::x86_THREAD_STATE64) {
8081 outs() << " flavor x86_THREAD_STATE64\n";
8082 if (count == MachO::x86_THREAD_STATE64_COUNT)
8083 outs() << " count x86_THREAD_STATE64_COUNT\n";
8084 else
8085 outs() << " count " << count
8086 << " (not x86_THREAD_STATE64_COUNT)\n";
8087 MachO::x86_thread_state64_t cpu64;
8088 left = end - begin;
8089 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8090 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8091 begin += sizeof(MachO::x86_thread_state64_t);
8092 } else {
8093 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8094 memcpy(&cpu64, begin, left);
8095 begin += left;
8096 }
8097 if (isLittleEndian != sys::IsLittleEndianHost)
8098 swapStruct(cpu64);
8099 Print_x86_thread_state64_t(cpu64);
8100 } else if (flavor == MachO::x86_THREAD_STATE) {
8101 outs() << " flavor x86_THREAD_STATE\n";
8102 if (count == MachO::x86_THREAD_STATE_COUNT)
8103 outs() << " count x86_THREAD_STATE_COUNT\n";
8104 else
8105 outs() << " count " << count
8106 << " (not x86_THREAD_STATE_COUNT)\n";
8107 struct MachO::x86_thread_state_t ts;
8108 left = end - begin;
8109 if (left >= sizeof(MachO::x86_thread_state_t)) {
8110 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8111 begin += sizeof(MachO::x86_thread_state_t);
8112 } else {
8113 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8114 memcpy(&ts, begin, left);
8115 begin += left;
8116 }
8117 if (isLittleEndian != sys::IsLittleEndianHost)
8118 swapStruct(ts);
8119 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8120 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8121 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8122 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8123 else
8124 outs() << "tsh.count " << ts.tsh.count
8125 << " (not x86_THREAD_STATE64_COUNT\n";
8126 Print_x86_thread_state64_t(ts.uts.ts64);
8127 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008128 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8129 << ts.tsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008130 }
8131 } else if (flavor == MachO::x86_FLOAT_STATE) {
8132 outs() << " flavor x86_FLOAT_STATE\n";
8133 if (count == MachO::x86_FLOAT_STATE_COUNT)
8134 outs() << " count x86_FLOAT_STATE_COUNT\n";
8135 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008136 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008137 struct MachO::x86_float_state_t fs;
8138 left = end - begin;
8139 if (left >= sizeof(MachO::x86_float_state_t)) {
8140 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8141 begin += sizeof(MachO::x86_float_state_t);
8142 } else {
8143 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8144 memcpy(&fs, begin, left);
8145 begin += left;
8146 }
8147 if (isLittleEndian != sys::IsLittleEndianHost)
8148 swapStruct(fs);
8149 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8150 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008151 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008152 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8153 else
8154 outs() << "fsh.count " << fs.fsh.count
8155 << " (not x86_FLOAT_STATE64_COUNT\n";
8156 Print_x86_float_state_t(fs.ufs.fs64);
8157 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008158 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8159 << fs.fsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008160 }
8161 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8162 outs() << " flavor x86_EXCEPTION_STATE\n";
8163 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8164 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8165 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008166 outs() << " count " << count
Kevin Enderby48ef5342014-12-23 22:56:39 +00008167 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8168 struct MachO::x86_exception_state_t es;
8169 left = end - begin;
8170 if (left >= sizeof(MachO::x86_exception_state_t)) {
8171 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8172 begin += sizeof(MachO::x86_exception_state_t);
8173 } else {
8174 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8175 memcpy(&es, begin, left);
8176 begin += left;
8177 }
8178 if (isLittleEndian != sys::IsLittleEndianHost)
8179 swapStruct(es);
8180 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8181 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008182 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008183 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8184 else
8185 outs() << "\t esh.count " << es.esh.count
8186 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8187 Print_x86_exception_state_t(es.ues.es64);
8188 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008189 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8190 << es.esh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008191 }
8192 } else {
8193 outs() << " flavor " << flavor << " (unknown)\n";
8194 outs() << " count " << count << "\n";
8195 outs() << " state (unknown)\n";
8196 begin += count * sizeof(uint32_t);
8197 }
8198 }
8199 } else {
8200 while (begin < end) {
8201 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8202 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8203 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008204 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008205 flavor = 0;
8206 begin = end;
8207 }
8208 if (isLittleEndian != sys::IsLittleEndianHost)
8209 sys::swapByteOrder(flavor);
8210 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8211 memcpy((char *)&count, begin, sizeof(uint32_t));
8212 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008213 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008214 count = 0;
8215 begin = end;
8216 }
8217 if (isLittleEndian != sys::IsLittleEndianHost)
8218 sys::swapByteOrder(count);
8219 outs() << " flavor " << flavor << "\n";
8220 outs() << " count " << count << "\n";
8221 outs() << " state (Unknown cputype/cpusubtype)\n";
8222 begin += count * sizeof(uint32_t);
8223 }
8224 }
8225}
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008226
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008227static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8228 if (dl.cmd == MachO::LC_ID_DYLIB)
8229 outs() << " cmd LC_ID_DYLIB\n";
8230 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8231 outs() << " cmd LC_LOAD_DYLIB\n";
8232 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8233 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8234 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8235 outs() << " cmd LC_REEXPORT_DYLIB\n";
8236 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8237 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8238 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8239 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8240 else
8241 outs() << " cmd " << dl.cmd << " (unknown)\n";
8242 outs() << " cmdsize " << dl.cmdsize;
8243 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8244 outs() << " Incorrect size\n";
8245 else
8246 outs() << "\n";
8247 if (dl.dylib.name < dl.cmdsize) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008248 const char *P = (const char *)(Ptr) + dl.dylib.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008249 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8250 } else {
8251 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8252 }
8253 outs() << " time stamp " << dl.dylib.timestamp << " ";
8254 time_t t = dl.dylib.timestamp;
8255 outs() << ctime(&t);
8256 outs() << " current version ";
8257 if (dl.dylib.current_version == 0xffffffff)
8258 outs() << "n/a\n";
8259 else
8260 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8261 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8262 << (dl.dylib.current_version & 0xff) << "\n";
8263 outs() << "compatibility version ";
8264 if (dl.dylib.compatibility_version == 0xffffffff)
8265 outs() << "n/a\n";
8266 else
8267 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8268 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8269 << (dl.dylib.compatibility_version & 0xff) << "\n";
8270}
8271
8272static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8273 uint32_t object_size) {
8274 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8275 outs() << " cmd LC_FUNCTION_STARTS\n";
8276 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8277 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8278 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8279 outs() << " cmd LC_FUNCTION_STARTS\n";
8280 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8281 outs() << " cmd LC_DATA_IN_CODE\n";
8282 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8283 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8284 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8285 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8286 else
8287 outs() << " cmd " << ld.cmd << " (?)\n";
8288 outs() << " cmdsize " << ld.cmdsize;
8289 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8290 outs() << " Incorrect size\n";
8291 else
8292 outs() << "\n";
8293 outs() << " dataoff " << ld.dataoff;
8294 if (ld.dataoff > object_size)
8295 outs() << " (past end of file)\n";
8296 else
8297 outs() << "\n";
8298 outs() << " datasize " << ld.datasize;
8299 uint64_t big_size = ld.dataoff;
8300 big_size += ld.datasize;
8301 if (big_size > object_size)
8302 outs() << " (past end of file)\n";
8303 else
8304 outs() << "\n";
8305}
8306
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008307static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8308 uint32_t cputype, bool verbose) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008309 StringRef Buf = Obj->getData();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008310 unsigned Index = 0;
8311 for (const auto &Command : Obj->load_commands()) {
8312 outs() << "Load command " << Index++ << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00008313 if (Command.C.cmd == MachO::LC_SEGMENT) {
8314 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8315 const char *sg_segname = SLC.segname;
8316 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8317 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8318 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8319 verbose);
8320 for (unsigned j = 0; j < SLC.nsects; j++) {
Kevin Enderbyc9713382014-12-16 01:14:45 +00008321 MachO::section S = Obj->getSection(Command, j);
Kevin Enderby956366c2014-08-29 22:30:52 +00008322 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8323 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8324 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8325 }
8326 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8327 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8328 const char *sg_segname = SLC_64.segname;
8329 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8330 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8331 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8332 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8333 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8334 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8335 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8336 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8337 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8338 sg_segname, filetype, Buf.size(), verbose);
8339 }
8340 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8341 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008342 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008343 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8344 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8345 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008346 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8347 Obj->is64Bit());
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008348 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8349 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8350 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8351 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8352 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8353 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8354 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8355 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8356 PrintDyldLoadCommand(Dyld, Command.Ptr);
8357 } else if (Command.C.cmd == MachO::LC_UUID) {
8358 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8359 PrintUuidLoadCommand(Uuid);
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008360 } else if (Command.C.cmd == MachO::LC_RPATH) {
8361 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8362 PrintRpathLoadCommand(Rpath, Command.Ptr);
Kevin Enderby1ff0ecc2014-12-16 21:48:27 +00008363 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
8364 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008365 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8366 PrintVersionMinLoadCommand(Vd);
8367 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8368 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8369 PrintSourceVersionCommand(Sd);
8370 } else if (Command.C.cmd == MachO::LC_MAIN) {
8371 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8372 PrintEntryPointCommand(Ep);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008373 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008374 MachO::encryption_info_command Ei =
8375 Obj->getEncryptionInfoCommand(Command);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008376 PrintEncryptionInfoCommand(Ei, Buf.size());
Kevin Enderby57538292014-12-17 01:01:30 +00008377 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008378 MachO::encryption_info_command_64 Ei =
8379 Obj->getEncryptionInfoCommand64(Command);
Kevin Enderby57538292014-12-17 01:01:30 +00008380 PrintEncryptionInfoCommand64(Ei, Buf.size());
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008381 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008382 MachO::linker_option_command Lo =
8383 Obj->getLinkerOptionLoadCommand(Command);
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008384 PrintLinkerOptionCommand(Lo, Command.Ptr);
Kevin Enderbyb4b79312014-12-18 19:24:35 +00008385 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8386 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8387 PrintSubFrameworkCommand(Sf, Command.Ptr);
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00008388 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8389 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8390 PrintSubUmbrellaCommand(Sf, Command.Ptr);
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008391 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8392 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8393 PrintSubLibraryCommand(Sl, Command.Ptr);
Kevin Enderby186eac32014-12-19 21:06:24 +00008394 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8395 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8396 PrintSubClientCommand(Sc, Command.Ptr);
Kevin Enderby52e4ce42014-12-19 22:25:22 +00008397 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8398 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8399 PrintRoutinesCommand(Rc);
8400 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8401 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8402 PrintRoutinesCommand64(Rc);
Kevin Enderby48ef5342014-12-23 22:56:39 +00008403 } else if (Command.C.cmd == MachO::LC_THREAD ||
8404 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8405 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8406 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
Nick Kledzik15558912014-10-16 18:58:20 +00008407 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8408 Command.C.cmd == MachO::LC_ID_DYLIB ||
8409 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8410 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8411 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8412 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008413 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8414 PrintDylibCommand(Dl, Command.Ptr);
8415 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8416 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8417 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8418 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8419 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8420 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8421 MachO::linkedit_data_command Ld =
8422 Obj->getLinkeditDataLoadCommand(Command);
8423 PrintLinkEditDataCommand(Ld, Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008424 } else {
8425 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8426 << ")\n";
8427 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8428 // TODO: get and print the raw bytes of the load command.
8429 }
8430 // TODO: print all the other kinds of load commands.
Kevin Enderby956366c2014-08-29 22:30:52 +00008431 }
8432}
8433
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008434static void getAndPrintMachHeader(const MachOObjectFile *Obj,
Kevin Enderby956366c2014-08-29 22:30:52 +00008435 uint32_t &filetype, uint32_t &cputype,
8436 bool verbose) {
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008437 if (Obj->is64Bit()) {
8438 MachO::mach_header_64 H_64;
8439 H_64 = Obj->getHeader64();
8440 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8441 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00008442 filetype = H_64.filetype;
8443 cputype = H_64.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008444 } else {
8445 MachO::mach_header H;
8446 H = Obj->getHeader();
8447 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8448 H.sizeofcmds, H.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00008449 filetype = H.filetype;
8450 cputype = H.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008451 }
8452}
8453
8454void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8455 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby956366c2014-08-29 22:30:52 +00008456 uint32_t filetype = 0;
8457 uint32_t cputype = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008458 getAndPrintMachHeader(file, filetype, cputype, !NonVerbose);
8459 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008460}
Nick Kledzikd04bc352014-08-30 00:20:14 +00008461
8462//===----------------------------------------------------------------------===//
8463// export trie dumping
8464//===----------------------------------------------------------------------===//
8465
8466void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008467 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
8468 uint64_t Flags = Entry.flags();
Nick Kledzikd04bc352014-08-30 00:20:14 +00008469 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
8470 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
8471 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8472 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
8473 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8474 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
8475 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
8476 if (ReExport)
8477 outs() << "[re-export] ";
8478 else
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008479 outs() << format("0x%08llX ",
8480 Entry.address()); // FIXME:add in base address
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008481 outs() << Entry.name();
Nick Kledzikd04bc352014-08-30 00:20:14 +00008482 if (WeakDef || ThreadLocal || Resolver || Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008483 bool NeedsComma = false;
Nick Kledzik1d1ac4b2014-09-03 01:12:52 +00008484 outs() << " [";
Nick Kledzikd04bc352014-08-30 00:20:14 +00008485 if (WeakDef) {
8486 outs() << "weak_def";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008487 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008488 }
8489 if (ThreadLocal) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008490 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008491 outs() << ", ";
8492 outs() << "per-thread";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008493 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008494 }
8495 if (Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008496 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008497 outs() << ", ";
8498 outs() << "absolute";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008499 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008500 }
8501 if (Resolver) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008502 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008503 outs() << ", ";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008504 outs() << format("resolver=0x%08llX", Entry.other());
8505 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008506 }
8507 outs() << "]";
8508 }
8509 if (ReExport) {
8510 StringRef DylibName = "unknown";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008511 int Ordinal = Entry.other() - 1;
8512 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
8513 if (Entry.otherName().empty())
Nick Kledzikd04bc352014-08-30 00:20:14 +00008514 outs() << " (from " << DylibName << ")";
8515 else
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008516 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
Nick Kledzikd04bc352014-08-30 00:20:14 +00008517 }
8518 outs() << "\n";
8519 }
8520}
Nick Kledzikac431442014-09-12 21:34:15 +00008521
Nick Kledzikac431442014-09-12 21:34:15 +00008522//===----------------------------------------------------------------------===//
8523// rebase table dumping
8524//===----------------------------------------------------------------------===//
8525
8526namespace {
8527class SegInfo {
8528public:
8529 SegInfo(const object::MachOObjectFile *Obj);
8530
8531 StringRef segmentName(uint32_t SegIndex);
8532 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
8533 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
8534
8535private:
8536 struct SectionInfo {
8537 uint64_t Address;
8538 uint64_t Size;
8539 StringRef SectionName;
8540 StringRef SegmentName;
8541 uint64_t OffsetInSegment;
8542 uint64_t SegmentStartAddress;
8543 uint32_t SegmentIndex;
8544 };
8545 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
8546 SmallVector<SectionInfo, 32> Sections;
8547};
8548}
8549
8550SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
8551 // Build table of sections so segIndex/offset pairs can be translated.
Nick Kledzik56ebef42014-09-16 01:41:51 +00008552 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
Nick Kledzikac431442014-09-12 21:34:15 +00008553 StringRef CurSegName;
8554 uint64_t CurSegAddress;
8555 for (const SectionRef &Section : Obj->sections()) {
8556 SectionInfo Info;
8557 if (error(Section.getName(Info.SectionName)))
8558 return;
Rafael Espindola80291272014-10-08 15:28:58 +00008559 Info.Address = Section.getAddress();
8560 Info.Size = Section.getSize();
Nick Kledzikac431442014-09-12 21:34:15 +00008561 Info.SegmentName =
8562 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
8563 if (!Info.SegmentName.equals(CurSegName)) {
8564 ++CurSegIndex;
8565 CurSegName = Info.SegmentName;
8566 CurSegAddress = Info.Address;
8567 }
8568 Info.SegmentIndex = CurSegIndex - 1;
8569 Info.OffsetInSegment = Info.Address - CurSegAddress;
8570 Info.SegmentStartAddress = CurSegAddress;
8571 Sections.push_back(Info);
8572 }
8573}
8574
8575StringRef SegInfo::segmentName(uint32_t SegIndex) {
8576 for (const SectionInfo &SI : Sections) {
8577 if (SI.SegmentIndex == SegIndex)
8578 return SI.SegmentName;
8579 }
8580 llvm_unreachable("invalid segIndex");
8581}
8582
8583const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
8584 uint64_t OffsetInSeg) {
8585 for (const SectionInfo &SI : Sections) {
8586 if (SI.SegmentIndex != SegIndex)
8587 continue;
8588 if (SI.OffsetInSegment > OffsetInSeg)
8589 continue;
8590 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8591 continue;
8592 return SI;
8593 }
8594 llvm_unreachable("segIndex and offset not in any section");
8595}
8596
8597StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
8598 return findSection(SegIndex, OffsetInSeg).SectionName;
8599}
8600
8601uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
8602 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
8603 return SI.SegmentStartAddress + OffsetInSeg;
8604}
8605
8606void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
8607 // Build table of sections so names can used in final output.
8608 SegInfo sectionTable(Obj);
8609
8610 outs() << "segment section address type\n";
8611 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
8612 uint32_t SegIndex = Entry.segmentIndex();
8613 uint64_t OffsetInSeg = Entry.segmentOffset();
8614 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8615 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8616 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8617
8618 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008619 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
8620 SegmentName.str().c_str(), SectionName.str().c_str(),
8621 Address, Entry.typeName().str().c_str());
Nick Kledzikac431442014-09-12 21:34:15 +00008622 }
8623}
Nick Kledzik56ebef42014-09-16 01:41:51 +00008624
8625static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
8626 StringRef DylibName;
8627 switch (Ordinal) {
8628 case MachO::BIND_SPECIAL_DYLIB_SELF:
8629 return "this-image";
8630 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
8631 return "main-executable";
8632 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
8633 return "flat-namespace";
8634 default:
Nick Kledzikabd29872014-09-16 22:03:13 +00008635 if (Ordinal > 0) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008636 std::error_code EC =
8637 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
Nick Kledzikabd29872014-09-16 22:03:13 +00008638 if (EC)
Nick Kledzik51d2c2b2014-10-14 23:29:38 +00008639 return "<<bad library ordinal>>";
Nick Kledzikabd29872014-09-16 22:03:13 +00008640 return DylibName;
8641 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00008642 }
Nick Kledzikabd29872014-09-16 22:03:13 +00008643 return "<<unknown special ordinal>>";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008644}
8645
8646//===----------------------------------------------------------------------===//
8647// bind table dumping
8648//===----------------------------------------------------------------------===//
8649
8650void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
8651 // Build table of sections so names can used in final output.
8652 SegInfo sectionTable(Obj);
8653
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008654 outs() << "segment section address type "
8655 "addend dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008656 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
8657 uint32_t SegIndex = Entry.segmentIndex();
8658 uint64_t OffsetInSeg = Entry.segmentOffset();
8659 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8660 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8661 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8662
8663 // Table lines look like:
8664 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008665 StringRef Attr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00008666 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008667 Attr = " (weak_import)";
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008668 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008669 << left_justify(SectionName, 18) << " "
8670 << format_hex(Address, 10, true) << " "
8671 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008672 << format_decimal(Entry.addend(), 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008673 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008674 << Entry.symbolName() << Attr << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008675 }
8676}
8677
8678//===----------------------------------------------------------------------===//
8679// lazy bind table dumping
8680//===----------------------------------------------------------------------===//
8681
8682void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
8683 // Build table of sections so names can used in final output.
8684 SegInfo sectionTable(Obj);
8685
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008686 outs() << "segment section address "
8687 "dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008688 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
8689 uint32_t SegIndex = Entry.segmentIndex();
8690 uint64_t OffsetInSeg = Entry.segmentOffset();
8691 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8692 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8693 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8694
8695 // Table lines look like:
8696 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008697 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008698 << left_justify(SectionName, 18) << " "
8699 << format_hex(Address, 10, true) << " "
8700 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Nick Kledzik56ebef42014-09-16 01:41:51 +00008701 << Entry.symbolName() << "\n";
8702 }
8703}
8704
Nick Kledzik56ebef42014-09-16 01:41:51 +00008705//===----------------------------------------------------------------------===//
8706// weak bind table dumping
8707//===----------------------------------------------------------------------===//
8708
8709void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
8710 // Build table of sections so names can used in final output.
8711 SegInfo sectionTable(Obj);
8712
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008713 outs() << "segment section address "
8714 "type addend symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008715 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
8716 // Strong symbols don't have a location to update.
8717 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008718 outs() << " strong "
Nick Kledzik56ebef42014-09-16 01:41:51 +00008719 << Entry.symbolName() << "\n";
8720 continue;
8721 }
8722 uint32_t SegIndex = Entry.segmentIndex();
8723 uint64_t OffsetInSeg = Entry.segmentOffset();
8724 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8725 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8726 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8727
8728 // Table lines look like:
8729 // __DATA __data 0x00001000 pointer 0 _foo
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008730 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008731 << left_justify(SectionName, 18) << " "
8732 << format_hex(Address, 10, true) << " "
8733 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008734 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
8735 << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008736 }
8737}
8738
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008739// get_dyld_bind_info_symbolname() is used for disassembly and passed an
8740// address, ReferenceValue, in the Mach-O file and looks in the dyld bind
8741// information for that address. If the address is found its binding symbol
8742// name is returned. If not nullptr is returned.
8743static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
8744 struct DisassembleInfo *info) {
Kevin Enderby078be602014-10-23 19:53:12 +00008745 if (info->bindtable == nullptr) {
8746 info->bindtable = new (BindTable);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008747 SegInfo sectionTable(info->O);
8748 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
8749 uint32_t SegIndex = Entry.segmentIndex();
8750 uint64_t OffsetInSeg = Entry.segmentOffset();
8751 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8752 const char *SymbolName = nullptr;
8753 StringRef name = Entry.symbolName();
8754 if (!name.empty())
8755 SymbolName = name.data();
Kevin Enderby078be602014-10-23 19:53:12 +00008756 info->bindtable->push_back(std::make_pair(Address, SymbolName));
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008757 }
8758 }
Kevin Enderby078be602014-10-23 19:53:12 +00008759 for (bind_table_iterator BI = info->bindtable->begin(),
8760 BE = info->bindtable->end();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008761 BI != BE; ++BI) {
8762 uint64_t Address = BI->first;
8763 if (ReferenceValue == Address) {
8764 const char *SymbolName = BI->second;
8765 return SymbolName;
8766 }
8767 }
8768 return nullptr;
8769}