blob: 36b83f7ef6334121cebce9afa77fbafc47042bf0 [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 Turner82af9432015-01-30 18:07:45 +000020#include "llvm/DebugInfo/DWARF/DIContext.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000021#include "llvm/MC/MCAsmInfo.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000022#include "llvm/MC/MCContext.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000023#include "llvm/MC/MCDisassembler.h"
24#include "llvm/MC/MCInst.h"
25#include "llvm/MC/MCInstPrinter.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000026#include "llvm/MC/MCInstrDesc.h"
27#include "llvm/MC/MCInstrInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000028#include "llvm/MC/MCRegisterInfo.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000029#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000030#include "llvm/Object/MachO.h"
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +000031#include "llvm/Object/MachOUniversal.h"
Rafael Espindola9b709252013-04-13 01:45:40 +000032#include "llvm/Support/Casting.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000033#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Debug.h"
Tim Northover4bd286a2014-08-01 13:07:19 +000035#include "llvm/Support/Endian.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000036#include "llvm/Support/Format.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000037#include "llvm/Support/FormattedStream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000038#include "llvm/Support/GraphWriter.h"
Kevin Enderby9a509442015-01-27 21:28:24 +000039#include "llvm/Support/LEB128.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000040#include "llvm/Support/MachO.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000041#include "llvm/Support/MemoryBuffer.h"
42#include "llvm/Support/TargetRegistry.h"
43#include "llvm/Support/TargetSelect.h"
44#include "llvm/Support/raw_ostream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000045#include <algorithm>
46#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000047#include <system_error>
Kevin Enderby04bf6932014-10-28 23:39:46 +000048
49#if HAVE_CXXABI_H
50#include <cxxabi.h>
51#endif
52
Benjamin Kramer43a772e2011-09-19 17:56:04 +000053using namespace llvm;
54using namespace object;
55
56static cl::opt<bool>
Kevin Enderbyb28ed012014-10-29 21:28:24 +000057 UseDbg("g",
58 cl::desc("Print line information from debug info if available"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000059
Kevin Enderbyb28ed012014-10-29 21:28:24 +000060static cl::opt<std::string> DSYMFile("dsym",
61 cl::desc("Use .dSYM file for debug info"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000062
Kevin Enderbyb28ed012014-10-29 21:28:24 +000063static cl::opt<bool> FullLeadingAddr("full-leading-addr",
64 cl::desc("Print full leading address"));
Kevin Enderbybf246f52014-09-24 23:08:22 +000065
Kevin Enderbyab5e6c92015-03-17 21:07:39 +000066static cl::opt<bool> NoLeadingAddr("no-leading-addr",
67 cl::desc("Print no leading address"));
68
Kevin Enderbybf246f52014-09-24 23:08:22 +000069static cl::opt<bool>
70 PrintImmHex("print-imm-hex",
71 cl::desc("Use hex format for immediate values"));
72
Kevin Enderby13023a12015-01-15 23:19:11 +000073cl::opt<bool> llvm::UniversalHeaders("universal-headers",
74 cl::desc("Print Mach-O universal headers "
75 "(requires -macho)"));
76
Kevin Enderby131d1772015-01-09 19:22:37 +000077cl::opt<bool>
Kevin Enderby13023a12015-01-15 23:19:11 +000078 llvm::ArchiveHeaders("archive-headers",
79 cl::desc("Print archive headers for Mach-O archives "
80 "(requires -macho)"));
Kevin Enderby131d1772015-01-09 19:22:37 +000081
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000082cl::opt<bool>
83 llvm::IndirectSymbols("indirect-symbols",
84 cl::desc("Print indirect symbol table for Mach-O "
85 "objects (requires -macho)"));
86
Kevin Enderby69fe98d2015-01-23 18:52:17 +000087cl::opt<bool>
88 llvm::DataInCode("data-in-code",
89 cl::desc("Print the data in code table for Mach-O objects "
90 "(requires -macho)"));
91
Kevin Enderby9a509442015-01-27 21:28:24 +000092cl::opt<bool>
93 llvm::LinkOptHints("link-opt-hints",
94 cl::desc("Print the linker optimization hints for "
95 "Mach-O objects (requires -macho)"));
96
Kevin Enderbyf6d25852015-01-31 00:37:11 +000097cl::list<std::string>
98 llvm::DumpSections("section",
99 cl::desc("Prints the specified segment,section for "
100 "Mach-O objects (requires -macho)"));
101
Adrian Prantlc2401dd2015-03-27 17:31:15 +0000102cl::opt<bool> llvm::Raw("raw",
103 cl::desc("Have -section dump the raw binary contents"));
104
Kevin Enderbycd66be52015-03-11 22:06:32 +0000105cl::opt<bool>
106 llvm::InfoPlist("info-plist",
107 cl::desc("Print the info plist section as strings for "
108 "Mach-O objects (requires -macho)"));
109
Kevin Enderbyf0640752015-03-13 17:56:32 +0000110cl::opt<bool>
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000111 llvm::DylibsUsed("dylibs-used",
112 cl::desc("Print the shared libraries used for linked "
113 "Mach-O files (requires -macho)"));
114
115cl::opt<bool>
116 llvm::DylibId("dylib-id",
117 cl::desc("Print the shared library's id for the dylib Mach-O "
118 "file (requires -macho)"));
119
120cl::opt<bool>
Kevin Enderbyf0640752015-03-13 17:56:32 +0000121 llvm::NonVerbose("non-verbose",
122 cl::desc("Print the info for Mach-O objects in "
123 "non-verbose or numeric form (requires -macho)"));
124
Kevin Enderby0fc11822015-04-01 20:57:01 +0000125cl::opt<bool>
126 llvm::ObjcMetaData("objc-meta-data",
127 cl::desc("Print the Objective-C runtime meta data for "
128 "Mach-O files (requires -macho)"));
129
Kevin Enderby6a221752015-03-17 17:10:57 +0000130cl::opt<std::string> llvm::DisSymName(
131 "dis-symname",
132 cl::desc("disassemble just this symbol's instructions (requires -macho"));
133
Kevin Enderby8e29ec92015-03-17 22:26:11 +0000134static cl::opt<bool> NoSymbolicOperands(
135 "no-symbolic-operands",
136 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
137
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000138static cl::list<std::string>
139 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
140 cl::ZeroOrMore);
141bool ArchAll = false;
142
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000143static std::string ThumbTripleName;
144
145static const Target *GetTarget(const MachOObjectFile *MachOObj,
146 const char **McpuDefault,
147 const Target **ThumbTarget) {
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000148 // Figure out the target triple.
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000149 if (TripleName.empty()) {
150 llvm::Triple TT("unknown-unknown-unknown");
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000151 llvm::Triple ThumbTriple = Triple();
152 TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000153 TripleName = TT.str();
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000154 ThumbTripleName = ThumbTriple.str();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000155 }
156
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000157 // Get the target specific parser.
158 std::string Error;
159 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000160 if (TheTarget && ThumbTripleName.empty())
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000161 return TheTarget;
162
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000163 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
164 if (*ThumbTarget)
165 return TheTarget;
166
167 errs() << "llvm-objdump: error: unable to get target for '";
168 if (!TheTarget)
169 errs() << TripleName;
170 else
171 errs() << ThumbTripleName;
172 errs() << "', see --version and --triple.\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000173 return nullptr;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000174}
175
Owen Andersond9243c42011-10-17 21:37:35 +0000176struct SymbolSorter {
177 bool operator()(const SymbolRef &A, const SymbolRef &B) {
178 SymbolRef::Type AType, BType;
179 A.getType(AType);
180 B.getType(BType);
181
182 uint64_t AAddr, BAddr;
183 if (AType != SymbolRef::ST_Function)
184 AAddr = 0;
185 else
186 A.getAddress(AAddr);
187 if (BType != SymbolRef::ST_Function)
188 BAddr = 0;
189 else
190 B.getAddress(BAddr);
191 return AAddr < BAddr;
192 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000193};
194
Kevin Enderby273ae012013-06-06 17:20:50 +0000195// Types for the storted data in code table that is built before disassembly
196// and the predicate function to sort them.
197typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
198typedef std::vector<DiceTableEntry> DiceTable;
199typedef DiceTable::iterator dice_table_iterator;
200
Kevin Enderby930fdc72014-11-06 19:00:13 +0000201// This is used to search for a data in code table entry for the PC being
202// disassembled. The j parameter has the PC in j.first. A single data in code
203// table entry can cover many bytes for each of its Kind's. So if the offset,
204// aka the i.first value, of the data in code table entry plus its Length
205// covers the PC being searched for this will return true. If not it will
206// return false.
David Majnemerea9b8ee2014-11-04 08:41:48 +0000207static bool compareDiceTableEntries(const DiceTableEntry &i,
208 const DiceTableEntry &j) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000209 uint16_t Length;
210 i.second.getLength(Length);
211
212 return j.first >= i.first && j.first < i.first + Length;
Kevin Enderby273ae012013-06-06 17:20:50 +0000213}
214
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000215static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
Kevin Enderby930fdc72014-11-06 19:00:13 +0000216 unsigned short Kind) {
217 uint32_t Value, Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000218
219 switch (Kind) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000220 default:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000221 case MachO::DICE_KIND_DATA:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000222 if (Length >= 4) {
223 if (!NoShowRawInsn)
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000224 DumpBytes(ArrayRef<uint8_t>(bytes, 4));
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000225 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000226 outs() << "\t.long " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000227 Size = 4;
228 } else if (Length >= 2) {
229 if (!NoShowRawInsn)
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000230 DumpBytes(ArrayRef<uint8_t>(bytes, 2));
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000231 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000232 outs() << "\t.short " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000233 Size = 2;
234 } else {
235 if (!NoShowRawInsn)
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000236 DumpBytes(ArrayRef<uint8_t>(bytes, 2));
Kevin Enderby273ae012013-06-06 17:20:50 +0000237 Value = bytes[0];
238 outs() << "\t.byte " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000239 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000240 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000241 if (Kind == MachO::DICE_KIND_DATA)
242 outs() << "\t@ KIND_DATA\n";
243 else
244 outs() << "\t@ data in code kind = " << Kind << "\n";
Kevin Enderby273ae012013-06-06 17:20:50 +0000245 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000246 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000247 if (!NoShowRawInsn)
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000248 DumpBytes(ArrayRef<uint8_t>(bytes, 1));
Kevin Enderby273ae012013-06-06 17:20:50 +0000249 Value = bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000250 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
251 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000252 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000253 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000254 if (!NoShowRawInsn)
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000255 DumpBytes(ArrayRef<uint8_t>(bytes, 2));
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000256 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000257 outs() << "\t.short " << format("%5u", Value & 0xffff)
258 << "\t@ KIND_JUMP_TABLE16\n";
259 Size = 2;
Kevin Enderby273ae012013-06-06 17:20:50 +0000260 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000261 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000262 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
263 if (!NoShowRawInsn)
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000264 DumpBytes(ArrayRef<uint8_t>(bytes, 4));
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000265 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000266 outs() << "\t.long " << Value;
267 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
268 outs() << "\t@ KIND_JUMP_TABLE32\n";
269 else
270 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
271 Size = 4;
Kevin Enderby273ae012013-06-06 17:20:50 +0000272 break;
273 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000274 return Size;
Kevin Enderby273ae012013-06-06 17:20:50 +0000275}
276
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000277static void getSectionsAndSymbols(const MachO::mach_header Header,
278 MachOObjectFile *MachOObj,
279 std::vector<SectionRef> &Sections,
280 std::vector<SymbolRef> &Symbols,
281 SmallVectorImpl<uint64_t> &FoundFns,
282 uint64_t &BaseSegmentAddress) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000283 for (const SymbolRef &Symbol : MachOObj->symbols()) {
284 StringRef SymName;
285 Symbol.getName(SymName);
286 if (!SymName.startswith("ltmp"))
287 Symbols.push_back(Symbol);
288 }
Owen Andersond9243c42011-10-17 21:37:35 +0000289
Alexey Samsonov48803e52014-03-13 14:37:36 +0000290 for (const SectionRef &Section : MachOObj->sections()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000291 StringRef SectName;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000292 Section.getName(SectName);
293 Sections.push_back(Section);
Owen Andersond9243c42011-10-17 21:37:35 +0000294 }
295
Rafael Espindola56f976f2013-04-18 18:08:55 +0000296 MachOObjectFile::LoadCommandInfo Command =
Alexey Samsonov48803e52014-03-13 14:37:36 +0000297 MachOObj->getFirstLoadCommandInfo();
Kevin Enderby273ae012013-06-06 17:20:50 +0000298 bool BaseSegmentAddressSet = false;
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000299 for (unsigned i = 0;; ++i) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000300 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000301 // We found a function starts segment, parse the addresses for later
302 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000303 MachO::linkedit_data_command LLC =
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000304 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000305
Charles Davis8bdfafd2013-09-01 04:28:48 +0000306 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000307 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
308 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000309 StringRef SegName = SLC.segname;
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000310 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
Kevin Enderby273ae012013-06-06 17:20:50 +0000311 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000312 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000313 }
314 }
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000315
Charles Davis8bdfafd2013-09-01 04:28:48 +0000316 if (i == Header.ncmds - 1)
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000317 break;
318 else
319 Command = MachOObj->getNextLoadCommandInfo(Command);
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000320 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000321}
322
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000323static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
324 uint32_t n, uint32_t count,
325 uint32_t stride, uint64_t addr) {
326 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
327 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
328 if (n > nindirectsyms)
329 outs() << " (entries start past the end of the indirect symbol "
330 "table) (reserved1 field greater than the table size)";
331 else if (n + count > nindirectsyms)
332 outs() << " (entries extends past the end of the indirect symbol "
333 "table)";
334 outs() << "\n";
335 uint32_t cputype = O->getHeader().cputype;
336 if (cputype & MachO::CPU_ARCH_ABI64)
337 outs() << "address index";
338 else
339 outs() << "address index";
340 if (verbose)
341 outs() << " name\n";
342 else
343 outs() << "\n";
344 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
345 if (cputype & MachO::CPU_ARCH_ABI64)
346 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
347 else
348 outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
349 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
350 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
351 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
352 outs() << "LOCAL\n";
353 continue;
354 }
355 if (indirect_symbol ==
356 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
357 outs() << "LOCAL ABSOLUTE\n";
358 continue;
359 }
360 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
361 outs() << "ABSOLUTE\n";
362 continue;
363 }
364 outs() << format("%5u ", indirect_symbol);
Kevin Enderbyf0640752015-03-13 17:56:32 +0000365 if (verbose) {
366 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
367 if (indirect_symbol < Symtab.nsyms) {
368 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
369 SymbolRef Symbol = *Sym;
370 StringRef SymName;
371 Symbol.getName(SymName);
372 outs() << SymName;
373 } else {
374 outs() << "?";
375 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000376 }
377 outs() << "\n";
378 }
379}
380
381static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
382 uint32_t LoadCommandCount = O->getHeader().ncmds;
383 MachOObjectFile::LoadCommandInfo Load = O->getFirstLoadCommandInfo();
384 for (unsigned I = 0;; ++I) {
385 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
386 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
387 for (unsigned J = 0; J < Seg.nsects; ++J) {
388 MachO::section_64 Sec = O->getSection64(Load, J);
389 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
390 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
391 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
392 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
393 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
394 section_type == MachO::S_SYMBOL_STUBS) {
395 uint32_t stride;
396 if (section_type == MachO::S_SYMBOL_STUBS)
397 stride = Sec.reserved2;
398 else
399 stride = 8;
400 if (stride == 0) {
401 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
402 << Sec.sectname << ") "
403 << "(size of stubs in reserved2 field is zero)\n";
404 continue;
405 }
406 uint32_t count = Sec.size / stride;
407 outs() << "Indirect symbols for (" << Sec.segname << ","
408 << Sec.sectname << ") " << count << " entries";
409 uint32_t n = Sec.reserved1;
410 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
411 }
412 }
413 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
414 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
415 for (unsigned J = 0; J < Seg.nsects; ++J) {
416 MachO::section Sec = O->getSection(Load, J);
417 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
418 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
419 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
420 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
421 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
422 section_type == MachO::S_SYMBOL_STUBS) {
423 uint32_t stride;
424 if (section_type == MachO::S_SYMBOL_STUBS)
425 stride = Sec.reserved2;
426 else
427 stride = 4;
428 if (stride == 0) {
429 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
430 << Sec.sectname << ") "
431 << "(size of stubs in reserved2 field is zero)\n";
432 continue;
433 }
434 uint32_t count = Sec.size / stride;
435 outs() << "Indirect symbols for (" << Sec.segname << ","
436 << Sec.sectname << ") " << count << " entries";
437 uint32_t n = Sec.reserved1;
438 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
439 }
440 }
441 }
442 if (I == LoadCommandCount - 1)
443 break;
444 else
445 Load = O->getNextLoadCommandInfo(Load);
446 }
447}
448
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000449static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
450 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
451 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
452 outs() << "Data in code table (" << nentries << " entries)\n";
453 outs() << "offset length kind\n";
454 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
455 ++DI) {
456 uint32_t Offset;
457 DI->getOffset(Offset);
458 outs() << format("0x%08" PRIx32, Offset) << " ";
459 uint16_t Length;
460 DI->getLength(Length);
461 outs() << format("%6u", Length) << " ";
462 uint16_t Kind;
463 DI->getKind(Kind);
464 if (verbose) {
465 switch (Kind) {
466 case MachO::DICE_KIND_DATA:
467 outs() << "DATA";
468 break;
469 case MachO::DICE_KIND_JUMP_TABLE8:
470 outs() << "JUMP_TABLE8";
471 break;
472 case MachO::DICE_KIND_JUMP_TABLE16:
473 outs() << "JUMP_TABLE16";
474 break;
475 case MachO::DICE_KIND_JUMP_TABLE32:
476 outs() << "JUMP_TABLE32";
477 break;
478 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
479 outs() << "ABS_JUMP_TABLE32";
480 break;
481 default:
482 outs() << format("0x%04" PRIx32, Kind);
483 break;
484 }
485 } else
486 outs() << format("0x%04" PRIx32, Kind);
487 outs() << "\n";
488 }
489}
490
Kevin Enderby9a509442015-01-27 21:28:24 +0000491static void PrintLinkOptHints(MachOObjectFile *O) {
492 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
493 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
494 uint32_t nloh = LohLC.datasize;
495 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
496 for (uint32_t i = 0; i < nloh;) {
497 unsigned n;
498 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
499 i += n;
500 outs() << " identifier " << identifier << " ";
501 if (i >= nloh)
502 return;
503 switch (identifier) {
504 case 1:
505 outs() << "AdrpAdrp\n";
506 break;
507 case 2:
508 outs() << "AdrpLdr\n";
509 break;
510 case 3:
511 outs() << "AdrpAddLdr\n";
512 break;
513 case 4:
514 outs() << "AdrpLdrGotLdr\n";
515 break;
516 case 5:
517 outs() << "AdrpAddStr\n";
518 break;
519 case 6:
520 outs() << "AdrpLdrGotStr\n";
521 break;
522 case 7:
523 outs() << "AdrpAdd\n";
524 break;
525 case 8:
526 outs() << "AdrpLdrGot\n";
527 break;
528 default:
529 outs() << "Unknown identifier value\n";
530 break;
531 }
532 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
533 i += n;
534 outs() << " narguments " << narguments << "\n";
535 if (i >= nloh)
536 return;
537
538 for (uint32_t j = 0; j < narguments; j++) {
539 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
540 i += n;
541 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
542 if (i >= nloh)
543 return;
544 }
545 }
546}
547
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000548static void PrintDylibs(MachOObjectFile *O, bool JustId) {
549 uint32_t LoadCommandCount = O->getHeader().ncmds;
550 MachOObjectFile::LoadCommandInfo Load = O->getFirstLoadCommandInfo();
551 for (unsigned I = 0;; ++I) {
552 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
553 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
554 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
555 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
556 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
557 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
558 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
559 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
560 if (dl.dylib.name < dl.cmdsize) {
561 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
562 if (JustId)
563 outs() << p << "\n";
564 else {
565 outs() << "\t" << p;
566 outs() << " (compatibility version "
567 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
568 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
569 << (dl.dylib.compatibility_version & 0xff) << ",";
570 outs() << " current version "
571 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
572 << ((dl.dylib.current_version >> 8) & 0xff) << "."
573 << (dl.dylib.current_version & 0xff) << ")\n";
574 }
575 } else {
576 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
577 if (Load.C.cmd == MachO::LC_ID_DYLIB)
578 outs() << "LC_ID_DYLIB ";
579 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
580 outs() << "LC_LOAD_DYLIB ";
581 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
582 outs() << "LC_LOAD_WEAK_DYLIB ";
583 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
584 outs() << "LC_LAZY_LOAD_DYLIB ";
585 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
586 outs() << "LC_REEXPORT_DYLIB ";
587 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
588 outs() << "LC_LOAD_UPWARD_DYLIB ";
589 else
590 outs() << "LC_??? ";
591 outs() << "command " << I << "\n";
592 }
593 }
594 if (I == LoadCommandCount - 1)
595 break;
596 else
597 Load = O->getNextLoadCommandInfo(Load);
598 }
599}
600
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000601typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
602
603static void CreateSymbolAddressMap(MachOObjectFile *O,
604 SymbolAddressMap *AddrMap) {
605 // Create a map of symbol addresses to symbol names.
606 for (const SymbolRef &Symbol : O->symbols()) {
607 SymbolRef::Type ST;
608 Symbol.getType(ST);
609 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
610 ST == SymbolRef::ST_Other) {
611 uint64_t Address;
612 Symbol.getAddress(Address);
613 StringRef SymName;
614 Symbol.getName(SymName);
615 (*AddrMap)[Address] = SymName;
616 }
617 }
618}
619
620// GuessSymbolName is passed the address of what might be a symbol and a
621// pointer to the SymbolAddressMap. It returns the name of a symbol
622// with that address or nullptr if no symbol is found with that address.
623static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
624 const char *SymbolName = nullptr;
625 // A DenseMap can't lookup up some values.
626 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
627 StringRef name = AddrMap->lookup(value);
628 if (!name.empty())
629 SymbolName = name.data();
630 }
631 return SymbolName;
632}
633
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000634static void DumpCstringChar(const char c) {
635 char p[2];
636 p[0] = c;
637 p[1] = '\0';
638 outs().write_escaped(p);
639}
640
Kevin Enderby10ba0412015-02-04 21:38:42 +0000641static void DumpCstringSection(MachOObjectFile *O, const char *sect,
642 uint32_t sect_size, uint64_t sect_addr,
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000643 bool print_addresses) {
Kevin Enderby10ba0412015-02-04 21:38:42 +0000644 for (uint32_t i = 0; i < sect_size; i++) {
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000645 if (print_addresses) {
646 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000647 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000648 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000649 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000650 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000651 for (; i < sect_size && sect[i] != '\0'; i++)
652 DumpCstringChar(sect[i]);
Kevin Enderby10ba0412015-02-04 21:38:42 +0000653 if (i < sect_size && sect[i] == '\0')
654 outs() << "\n";
655 }
656}
657
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000658static void DumpLiteral4(uint32_t l, float f) {
659 outs() << format("0x%08" PRIx32, l);
660 if ((l & 0x7f800000) != 0x7f800000)
661 outs() << format(" (%.16e)\n", f);
662 else {
663 if (l == 0x7f800000)
664 outs() << " (+Infinity)\n";
665 else if (l == 0xff800000)
666 outs() << " (-Infinity)\n";
667 else if ((l & 0x00400000) == 0x00400000)
668 outs() << " (non-signaling Not-a-Number)\n";
669 else
670 outs() << " (signaling Not-a-Number)\n";
671 }
672}
673
674static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
675 uint32_t sect_size, uint64_t sect_addr,
676 bool print_addresses) {
677 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
678 if (print_addresses) {
679 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000680 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000681 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000682 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000683 }
684 float f;
685 memcpy(&f, sect + i, sizeof(float));
686 if (O->isLittleEndian() != sys::IsLittleEndianHost)
687 sys::swapByteOrder(f);
688 uint32_t l;
689 memcpy(&l, sect + i, sizeof(uint32_t));
690 if (O->isLittleEndian() != sys::IsLittleEndianHost)
691 sys::swapByteOrder(l);
692 DumpLiteral4(l, f);
693 }
694}
695
696static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
697 double d) {
698 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
699 uint32_t Hi, Lo;
700 if (O->isLittleEndian()) {
701 Hi = l1;
702 Lo = l0;
703 } else {
704 Hi = l0;
705 Lo = l1;
706 }
707 // Hi is the high word, so this is equivalent to if(isfinite(d))
708 if ((Hi & 0x7ff00000) != 0x7ff00000)
709 outs() << format(" (%.16e)\n", d);
710 else {
711 if (Hi == 0x7ff00000 && Lo == 0)
712 outs() << " (+Infinity)\n";
713 else if (Hi == 0xfff00000 && Lo == 0)
714 outs() << " (-Infinity)\n";
715 else if ((Hi & 0x00080000) == 0x00080000)
716 outs() << " (non-signaling Not-a-Number)\n";
717 else
718 outs() << " (signaling Not-a-Number)\n";
719 }
720}
721
722static void DumpLiteral8Section(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 += sizeof(double)) {
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 double d;
733 memcpy(&d, sect + i, sizeof(double));
734 if (O->isLittleEndian() != sys::IsLittleEndianHost)
735 sys::swapByteOrder(d);
736 uint32_t l0, l1;
737 memcpy(&l0, sect + i, sizeof(uint32_t));
738 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
739 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
740 sys::swapByteOrder(l0);
741 sys::swapByteOrder(l1);
742 }
743 DumpLiteral8(O, l0, l1, d);
744 }
745}
746
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000747static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
748 outs() << format("0x%08" PRIx32, l0) << " ";
749 outs() << format("0x%08" PRIx32, l1) << " ";
750 outs() << format("0x%08" PRIx32, l2) << " ";
751 outs() << format("0x%08" PRIx32, l3) << "\n";
752}
753
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000754static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
755 uint32_t sect_size, uint64_t sect_addr,
756 bool print_addresses) {
757 for (uint32_t i = 0; i < sect_size; i += 16) {
758 if (print_addresses) {
759 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000760 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000761 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000762 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000763 }
764 uint32_t l0, l1, l2, l3;
765 memcpy(&l0, sect + i, sizeof(uint32_t));
766 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
767 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
768 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
769 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
770 sys::swapByteOrder(l0);
771 sys::swapByteOrder(l1);
772 sys::swapByteOrder(l2);
773 sys::swapByteOrder(l3);
774 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000775 DumpLiteral16(l0, l1, l2, l3);
776 }
777}
778
779static void DumpLiteralPointerSection(MachOObjectFile *O,
780 const SectionRef &Section,
781 const char *sect, uint32_t sect_size,
782 uint64_t sect_addr,
783 bool print_addresses) {
784 // Collect the literal sections in this Mach-O file.
785 std::vector<SectionRef> LiteralSections;
786 for (const SectionRef &Section : O->sections()) {
787 DataRefImpl Ref = Section.getRawDataRefImpl();
788 uint32_t section_type;
789 if (O->is64Bit()) {
790 const MachO::section_64 Sec = O->getSection64(Ref);
791 section_type = Sec.flags & MachO::SECTION_TYPE;
792 } else {
793 const MachO::section Sec = O->getSection(Ref);
794 section_type = Sec.flags & MachO::SECTION_TYPE;
795 }
796 if (section_type == MachO::S_CSTRING_LITERALS ||
797 section_type == MachO::S_4BYTE_LITERALS ||
798 section_type == MachO::S_8BYTE_LITERALS ||
799 section_type == MachO::S_16BYTE_LITERALS)
800 LiteralSections.push_back(Section);
801 }
802
803 // Set the size of the literal pointer.
804 uint32_t lp_size = O->is64Bit() ? 8 : 4;
805
806 // Collect the external relocation symbols for the the literal pointers.
807 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
808 for (const RelocationRef &Reloc : Section.relocations()) {
809 DataRefImpl Rel;
810 MachO::any_relocation_info RE;
811 bool isExtern = false;
812 Rel = Reloc.getRawDataRefImpl();
813 RE = O->getRelocation(Rel);
814 isExtern = O->getPlainRelocationExternal(RE);
815 if (isExtern) {
816 uint64_t RelocOffset;
817 Reloc.getOffset(RelocOffset);
818 symbol_iterator RelocSym = Reloc.getSymbol();
819 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
820 }
821 }
822 array_pod_sort(Relocs.begin(), Relocs.end());
823
824 // Dump each literal pointer.
825 for (uint32_t i = 0; i < sect_size; i += lp_size) {
826 if (print_addresses) {
827 if (O->is64Bit())
828 outs() << format("%016" PRIx64, sect_addr + i) << " ";
829 else
830 outs() << format("%08" PRIx64, sect_addr + i) << " ";
831 }
832 uint64_t lp;
833 if (O->is64Bit()) {
834 memcpy(&lp, sect + i, sizeof(uint64_t));
835 if (O->isLittleEndian() != sys::IsLittleEndianHost)
836 sys::swapByteOrder(lp);
837 } else {
838 uint32_t li;
839 memcpy(&li, sect + i, sizeof(uint32_t));
840 if (O->isLittleEndian() != sys::IsLittleEndianHost)
841 sys::swapByteOrder(li);
842 lp = li;
843 }
844
845 // First look for an external relocation entry for this literal pointer.
David Blaikie33dd45d02015-03-23 18:39:02 +0000846 auto Reloc = std::find_if(
847 Relocs.begin(), Relocs.end(),
848 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
849 if (Reloc != Relocs.end()) {
850 symbol_iterator RelocSym = Reloc->second;
851 StringRef SymName;
852 RelocSym->getName(SymName);
853 outs() << "external relocation entry for symbol:" << SymName << "\n";
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000854 continue;
David Blaikie33dd45d02015-03-23 18:39:02 +0000855 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000856
857 // For local references see what the section the literal pointer points to.
David Blaikie33dd45d02015-03-23 18:39:02 +0000858 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
859 [&](const SectionRef &R) {
860 return lp >= R.getAddress() &&
861 lp < R.getAddress() + R.getSize();
862 });
863 if (Sect == LiteralSections.end()) {
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000864 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
David Blaikie33dd45d02015-03-23 18:39:02 +0000865 continue;
866 }
867
868 uint64_t SectAddress = Sect->getAddress();
869 uint64_t SectSize = Sect->getSize();
870
871 StringRef SectName;
872 Sect->getName(SectName);
873 DataRefImpl Ref = Sect->getRawDataRefImpl();
874 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
875 outs() << SegmentName << ":" << SectName << ":";
876
877 uint32_t section_type;
878 if (O->is64Bit()) {
879 const MachO::section_64 Sec = O->getSection64(Ref);
880 section_type = Sec.flags & MachO::SECTION_TYPE;
881 } else {
882 const MachO::section Sec = O->getSection(Ref);
883 section_type = Sec.flags & MachO::SECTION_TYPE;
884 }
885
886 StringRef BytesStr;
887 Sect->getContents(BytesStr);
888 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
889
890 switch (section_type) {
891 case MachO::S_CSTRING_LITERALS:
892 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
893 i++) {
894 DumpCstringChar(Contents[i]);
895 }
896 outs() << "\n";
897 break;
898 case MachO::S_4BYTE_LITERALS:
899 float f;
900 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
901 uint32_t l;
902 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
903 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
904 sys::swapByteOrder(f);
905 sys::swapByteOrder(l);
906 }
907 DumpLiteral4(l, f);
908 break;
909 case MachO::S_8BYTE_LITERALS: {
910 double d;
911 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
912 uint32_t l0, l1;
913 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
914 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
915 sizeof(uint32_t));
916 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
917 sys::swapByteOrder(f);
918 sys::swapByteOrder(l0);
919 sys::swapByteOrder(l1);
920 }
921 DumpLiteral8(O, l0, l1, d);
922 break;
923 }
924 case MachO::S_16BYTE_LITERALS: {
925 uint32_t l0, l1, l2, l3;
926 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
927 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
928 sizeof(uint32_t));
929 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
930 sizeof(uint32_t));
931 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
932 sizeof(uint32_t));
933 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
934 sys::swapByteOrder(l0);
935 sys::swapByteOrder(l1);
936 sys::swapByteOrder(l2);
937 sys::swapByteOrder(l3);
938 }
939 DumpLiteral16(l0, l1, l2, l3);
940 break;
941 }
942 }
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000943 }
944}
945
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000946static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
947 uint32_t sect_size, uint64_t sect_addr,
948 SymbolAddressMap *AddrMap,
949 bool verbose) {
950 uint32_t stride;
951 if (O->is64Bit())
952 stride = sizeof(uint64_t);
953 else
954 stride = sizeof(uint32_t);
955 for (uint32_t i = 0; i < sect_size; i += stride) {
956 const char *SymbolName = nullptr;
957 if (O->is64Bit()) {
958 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
959 uint64_t pointer_value;
960 memcpy(&pointer_value, sect + i, stride);
961 if (O->isLittleEndian() != sys::IsLittleEndianHost)
962 sys::swapByteOrder(pointer_value);
963 outs() << format("0x%016" PRIx64, pointer_value);
964 if (verbose)
965 SymbolName = GuessSymbolName(pointer_value, AddrMap);
966 } else {
967 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
968 uint32_t pointer_value;
969 memcpy(&pointer_value, sect + i, stride);
970 if (O->isLittleEndian() != sys::IsLittleEndianHost)
971 sys::swapByteOrder(pointer_value);
972 outs() << format("0x%08" PRIx32, pointer_value);
973 if (verbose)
974 SymbolName = GuessSymbolName(pointer_value, AddrMap);
975 }
976 if (SymbolName)
977 outs() << " " << SymbolName;
978 outs() << "\n";
979 }
980}
981
982static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
983 uint32_t size, uint64_t addr) {
984 uint32_t cputype = O->getHeader().cputype;
985 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
986 uint32_t j;
987 for (uint32_t i = 0; i < size; i += j, addr += j) {
988 if (O->is64Bit())
989 outs() << format("%016" PRIx64, addr) << "\t";
990 else
Kevin Enderbyf0640752015-03-13 17:56:32 +0000991 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000992 for (j = 0; j < 16 && i + j < size; j++) {
993 uint8_t byte_word = *(sect + i + j);
994 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
995 }
996 outs() << "\n";
997 }
998 } else {
999 uint32_t j;
1000 for (uint32_t i = 0; i < size; i += j, addr += j) {
1001 if (O->is64Bit())
1002 outs() << format("%016" PRIx64, addr) << "\t";
1003 else
1004 outs() << format("%08" PRIx64, sect) << "\t";
1005 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
1006 j += sizeof(int32_t)) {
1007 if (i + j + sizeof(int32_t) < size) {
1008 uint32_t long_word;
1009 memcpy(&long_word, sect + i + j, sizeof(int32_t));
1010 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1011 sys::swapByteOrder(long_word);
1012 outs() << format("%08" PRIx32, long_word) << " ";
1013 } else {
1014 for (uint32_t k = 0; i + j + k < size; k++) {
1015 uint8_t byte_word = *(sect + i + j);
1016 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1017 }
1018 }
1019 }
1020 outs() << "\n";
1021 }
1022 }
1023}
1024
Kevin Enderby95df54c2015-02-04 01:01:38 +00001025static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1026 StringRef DisSegName, StringRef DisSectName);
1027
1028static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1029 bool verbose) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001030 SymbolAddressMap AddrMap;
1031 if (verbose)
1032 CreateSymbolAddressMap(O, &AddrMap);
1033
1034 for (unsigned i = 0; i < DumpSections.size(); ++i) {
1035 StringRef DumpSection = DumpSections[i];
1036 std::pair<StringRef, StringRef> DumpSegSectName;
1037 DumpSegSectName = DumpSection.split(',');
1038 StringRef DumpSegName, DumpSectName;
1039 if (DumpSegSectName.second.size()) {
1040 DumpSegName = DumpSegSectName.first;
1041 DumpSectName = DumpSegSectName.second;
1042 } else {
1043 DumpSegName = "";
1044 DumpSectName = DumpSegSectName.first;
1045 }
1046 for (const SectionRef &Section : O->sections()) {
1047 StringRef SectName;
1048 Section.getName(SectName);
1049 DataRefImpl Ref = Section.getRawDataRefImpl();
1050 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1051 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1052 (SectName == DumpSectName)) {
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001053
Kevin Enderby95df54c2015-02-04 01:01:38 +00001054 uint32_t section_flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001055 if (O->is64Bit()) {
1056 const MachO::section_64 Sec = O->getSection64(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001057 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001058
1059 } else {
1060 const MachO::section Sec = O->getSection(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001061 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001062 }
Kevin Enderby95df54c2015-02-04 01:01:38 +00001063 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001064
1065 StringRef BytesStr;
1066 Section.getContents(BytesStr);
1067 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1068 uint32_t sect_size = BytesStr.size();
1069 uint64_t sect_addr = Section.getAddress();
1070
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001071 if (Raw) {
1072 outs().write(BytesStr.data(), BytesStr.size());
1073 continue;
1074 }
1075
1076 outs() << "Contents of (" << SegName << "," << SectName
1077 << ") section\n";
1078
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001079 if (verbose) {
Kevin Enderby95df54c2015-02-04 01:01:38 +00001080 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1081 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1082 DisassembleMachO(Filename, O, SegName, SectName);
1083 continue;
1084 }
Kevin Enderbycd66be52015-03-11 22:06:32 +00001085 if (SegName == "__TEXT" && SectName == "__info_plist") {
1086 outs() << sect;
1087 continue;
1088 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001089 switch (section_type) {
1090 case MachO::S_REGULAR:
1091 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1092 break;
1093 case MachO::S_ZEROFILL:
1094 outs() << "zerofill section and has no contents in the file\n";
1095 break;
Kevin Enderby10ba0412015-02-04 21:38:42 +00001096 case MachO::S_CSTRING_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001097 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby10ba0412015-02-04 21:38:42 +00001098 break;
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001099 case MachO::S_4BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001100 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001101 break;
1102 case MachO::S_8BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001103 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001104 break;
1105 case MachO::S_16BYTE_LITERALS:
Kevin Enderby0fc11822015-04-01 20:57:01 +00001106 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1107 break;
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001108 case MachO::S_LITERAL_POINTERS:
1109 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001110 !NoLeadingAddr);
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001111 break;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001112 case MachO::S_MOD_INIT_FUNC_POINTERS:
1113 case MachO::S_MOD_TERM_FUNC_POINTERS:
1114 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1115 verbose);
1116 break;
1117 default:
1118 outs() << "Unknown section type ("
1119 << format("0x%08" PRIx32, section_type) << ")\n";
1120 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1121 break;
1122 }
1123 } else {
1124 if (section_type == MachO::S_ZEROFILL)
1125 outs() << "zerofill section and has no contents in the file\n";
1126 else
1127 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1128 }
1129 }
1130 }
1131 }
1132}
1133
Kevin Enderbycd66be52015-03-11 22:06:32 +00001134static void DumpInfoPlistSectionContents(StringRef Filename,
1135 MachOObjectFile *O) {
1136 for (const SectionRef &Section : O->sections()) {
1137 StringRef SectName;
1138 Section.getName(SectName);
1139 DataRefImpl Ref = Section.getRawDataRefImpl();
1140 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1141 if (SegName == "__TEXT" && SectName == "__info_plist") {
1142 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1143 StringRef BytesStr;
1144 Section.getContents(BytesStr);
1145 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1146 outs() << sect;
1147 return;
1148 }
1149 }
1150}
1151
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001152// checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1153// and if it is and there is a list of architecture flags is specified then
1154// check to make sure this Mach-O file is one of those architectures or all
1155// architectures were specified. If not then an error is generated and this
1156// routine returns false. Else it returns true.
1157static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1158 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1159 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1160 bool ArchFound = false;
1161 MachO::mach_header H;
1162 MachO::mach_header_64 H_64;
1163 Triple T;
1164 if (MachO->is64Bit()) {
1165 H_64 = MachO->MachOObjectFile::getHeader64();
1166 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1167 } else {
1168 H = MachO->MachOObjectFile::getHeader();
1169 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1170 }
1171 unsigned i;
1172 for (i = 0; i < ArchFlags.size(); ++i) {
1173 if (ArchFlags[i] == T.getArchName())
1174 ArchFound = true;
1175 break;
1176 }
1177 if (!ArchFound) {
1178 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1179 << "architecture: " + ArchFlags[i] + "\n";
1180 return false;
1181 }
1182 }
1183 return true;
1184}
1185
Kevin Enderby0fc11822015-04-01 20:57:01 +00001186static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1187
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001188// ProcessMachO() is passed a single opened Mach-O file, which may be an
1189// archive member and or in a slice of a universal file. It prints the
1190// the file name and header info and then processes it according to the
1191// command line options.
1192static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1193 StringRef ArchiveMemberName = StringRef(),
1194 StringRef ArchitectureName = StringRef()) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001195 // If we are doing some processing here on the Mach-O file print the header
1196 // info. And don't print it otherwise like in the case of printing the
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001197 // UniversalHeaders or ArchiveHeaders.
Kevin Enderby131d1772015-01-09 19:22:37 +00001198 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001199 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
Kevin Enderby0fc11822015-04-01 20:57:01 +00001200 DylibsUsed || DylibId || ObjcMetaData ||
1201 (DumpSections.size() != 0 && !Raw)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001202 outs() << Filename;
1203 if (!ArchiveMemberName.empty())
1204 outs() << '(' << ArchiveMemberName << ')';
1205 if (!ArchitectureName.empty())
1206 outs() << " (architecture " << ArchitectureName << ")";
1207 outs() << ":\n";
1208 }
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001209
1210 if (Disassemble)
Kevin Enderby95df54c2015-02-04 01:01:38 +00001211 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001212 if (IndirectSymbols)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001213 PrintIndirectSymbols(MachOOF, !NonVerbose);
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001214 if (DataInCode)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001215 PrintDataInCodeTable(MachOOF, !NonVerbose);
Kevin Enderby9a509442015-01-27 21:28:24 +00001216 if (LinkOptHints)
1217 PrintLinkOptHints(MachOOF);
Kevin Enderby98da6132015-01-20 21:47:46 +00001218 if (Relocations)
1219 PrintRelocations(MachOOF);
1220 if (SectionHeaders)
1221 PrintSectionHeaders(MachOOF);
1222 if (SectionContents)
1223 PrintSectionContents(MachOOF);
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001224 if (DumpSections.size() != 0)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001225 DumpSectionContents(Filename, MachOOF, !NonVerbose);
Kevin Enderbycd66be52015-03-11 22:06:32 +00001226 if (InfoPlist)
1227 DumpInfoPlistSectionContents(Filename, MachOOF);
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001228 if (DylibsUsed)
1229 PrintDylibs(MachOOF, false);
1230 if (DylibId)
1231 PrintDylibs(MachOOF, true);
Kevin Enderby98da6132015-01-20 21:47:46 +00001232 if (SymbolTable)
1233 PrintSymbolTable(MachOOF);
1234 if (UnwindInfo)
1235 printMachOUnwindInfo(MachOOF);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001236 if (PrivateHeaders)
1237 printMachOFileHeader(MachOOF);
Kevin Enderby0fc11822015-04-01 20:57:01 +00001238 if (ObjcMetaData)
1239 printObjcMetaData(MachOOF, !NonVerbose);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001240 if (ExportsTrie)
1241 printExportsTrie(MachOOF);
1242 if (Rebase)
1243 printRebaseTable(MachOOF);
1244 if (Bind)
1245 printBindTable(MachOOF);
1246 if (LazyBind)
1247 printLazyBindTable(MachOOF);
1248 if (WeakBind)
1249 printWeakBindTable(MachOOF);
1250}
1251
Kevin Enderby131d1772015-01-09 19:22:37 +00001252// printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1253static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1254 outs() << " cputype (" << cputype << ")\n";
1255 outs() << " cpusubtype (" << cpusubtype << ")\n";
1256}
1257
1258// printCPUType() helps print_fat_headers by printing the cputype and
1259// pusubtype (symbolically for the one's it knows about).
1260static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1261 switch (cputype) {
1262 case MachO::CPU_TYPE_I386:
1263 switch (cpusubtype) {
1264 case MachO::CPU_SUBTYPE_I386_ALL:
1265 outs() << " cputype CPU_TYPE_I386\n";
1266 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1267 break;
1268 default:
1269 printUnknownCPUType(cputype, cpusubtype);
1270 break;
1271 }
1272 break;
1273 case MachO::CPU_TYPE_X86_64:
1274 switch (cpusubtype) {
1275 case MachO::CPU_SUBTYPE_X86_64_ALL:
1276 outs() << " cputype CPU_TYPE_X86_64\n";
1277 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1278 break;
1279 case MachO::CPU_SUBTYPE_X86_64_H:
1280 outs() << " cputype CPU_TYPE_X86_64\n";
1281 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1282 break;
1283 default:
1284 printUnknownCPUType(cputype, cpusubtype);
1285 break;
1286 }
1287 break;
1288 case MachO::CPU_TYPE_ARM:
1289 switch (cpusubtype) {
1290 case MachO::CPU_SUBTYPE_ARM_ALL:
1291 outs() << " cputype CPU_TYPE_ARM\n";
1292 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1293 break;
1294 case MachO::CPU_SUBTYPE_ARM_V4T:
1295 outs() << " cputype CPU_TYPE_ARM\n";
1296 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1297 break;
1298 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1299 outs() << " cputype CPU_TYPE_ARM\n";
1300 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1301 break;
1302 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1303 outs() << " cputype CPU_TYPE_ARM\n";
1304 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1305 break;
1306 case MachO::CPU_SUBTYPE_ARM_V6:
1307 outs() << " cputype CPU_TYPE_ARM\n";
1308 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1309 break;
1310 case MachO::CPU_SUBTYPE_ARM_V6M:
1311 outs() << " cputype CPU_TYPE_ARM\n";
1312 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1313 break;
1314 case MachO::CPU_SUBTYPE_ARM_V7:
1315 outs() << " cputype CPU_TYPE_ARM\n";
1316 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1317 break;
1318 case MachO::CPU_SUBTYPE_ARM_V7EM:
1319 outs() << " cputype CPU_TYPE_ARM\n";
1320 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1321 break;
1322 case MachO::CPU_SUBTYPE_ARM_V7K:
1323 outs() << " cputype CPU_TYPE_ARM\n";
1324 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1325 break;
1326 case MachO::CPU_SUBTYPE_ARM_V7M:
1327 outs() << " cputype CPU_TYPE_ARM\n";
1328 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1329 break;
1330 case MachO::CPU_SUBTYPE_ARM_V7S:
1331 outs() << " cputype CPU_TYPE_ARM\n";
1332 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1333 break;
1334 default:
1335 printUnknownCPUType(cputype, cpusubtype);
1336 break;
1337 }
1338 break;
1339 case MachO::CPU_TYPE_ARM64:
1340 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1341 case MachO::CPU_SUBTYPE_ARM64_ALL:
1342 outs() << " cputype CPU_TYPE_ARM64\n";
1343 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1344 break;
1345 default:
1346 printUnknownCPUType(cputype, cpusubtype);
1347 break;
1348 }
1349 break;
1350 default:
1351 printUnknownCPUType(cputype, cpusubtype);
1352 break;
1353 }
1354}
1355
1356static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1357 bool verbose) {
1358 outs() << "Fat headers\n";
1359 if (verbose)
1360 outs() << "fat_magic FAT_MAGIC\n";
1361 else
1362 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1363
1364 uint32_t nfat_arch = UB->getNumberOfObjects();
1365 StringRef Buf = UB->getData();
1366 uint64_t size = Buf.size();
1367 uint64_t big_size = sizeof(struct MachO::fat_header) +
1368 nfat_arch * sizeof(struct MachO::fat_arch);
1369 outs() << "nfat_arch " << UB->getNumberOfObjects();
1370 if (nfat_arch == 0)
1371 outs() << " (malformed, contains zero architecture types)\n";
1372 else if (big_size > size)
1373 outs() << " (malformed, architectures past end of file)\n";
1374 else
1375 outs() << "\n";
1376
1377 for (uint32_t i = 0; i < nfat_arch; ++i) {
1378 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1379 uint32_t cputype = OFA.getCPUType();
1380 uint32_t cpusubtype = OFA.getCPUSubType();
1381 outs() << "architecture ";
1382 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1383 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1384 uint32_t other_cputype = other_OFA.getCPUType();
1385 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
Kevin Enderby0512bd72015-01-09 21:55:03 +00001386 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
Kevin Enderby131d1772015-01-09 19:22:37 +00001387 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
Kevin Enderby0512bd72015-01-09 21:55:03 +00001388 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001389 outs() << "(illegal duplicate architecture) ";
1390 break;
Kevin Enderby0512bd72015-01-09 21:55:03 +00001391 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001392 }
1393 if (verbose) {
1394 outs() << OFA.getArchTypeName() << "\n";
1395 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1396 } else {
1397 outs() << i << "\n";
1398 outs() << " cputype " << cputype << "\n";
1399 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1400 << "\n";
1401 }
1402 if (verbose &&
1403 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1404 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1405 else
1406 outs() << " capabilities "
1407 << format("0x%" PRIx32,
1408 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1409 outs() << " offset " << OFA.getOffset();
1410 if (OFA.getOffset() > size)
1411 outs() << " (past end of file)";
1412 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1413 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1414 outs() << "\n";
1415 outs() << " size " << OFA.getSize();
1416 big_size = OFA.getOffset() + OFA.getSize();
1417 if (big_size > size)
1418 outs() << " (past end of file)";
1419 outs() << "\n";
1420 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1421 << ")\n";
1422 }
1423}
1424
Kevin Enderby13023a12015-01-15 23:19:11 +00001425static void printArchiveChild(Archive::Child &C, bool verbose,
1426 bool print_offset) {
1427 if (print_offset)
1428 outs() << C.getChildOffset() << "\t";
1429 sys::fs::perms Mode = C.getAccessMode();
1430 if (verbose) {
1431 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1432 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1433 outs() << "-";
1434 if (Mode & sys::fs::owner_read)
1435 outs() << "r";
1436 else
1437 outs() << "-";
1438 if (Mode & sys::fs::owner_write)
1439 outs() << "w";
1440 else
1441 outs() << "-";
1442 if (Mode & sys::fs::owner_exe)
1443 outs() << "x";
1444 else
1445 outs() << "-";
1446 if (Mode & sys::fs::group_read)
1447 outs() << "r";
1448 else
1449 outs() << "-";
1450 if (Mode & sys::fs::group_write)
1451 outs() << "w";
1452 else
1453 outs() << "-";
1454 if (Mode & sys::fs::group_exe)
1455 outs() << "x";
1456 else
1457 outs() << "-";
1458 if (Mode & sys::fs::others_read)
1459 outs() << "r";
1460 else
1461 outs() << "-";
1462 if (Mode & sys::fs::others_write)
1463 outs() << "w";
1464 else
1465 outs() << "-";
1466 if (Mode & sys::fs::others_exe)
1467 outs() << "x";
1468 else
1469 outs() << "-";
1470 } else {
1471 outs() << format("0%o ", Mode);
1472 }
1473
1474 unsigned UID = C.getUID();
1475 outs() << format("%3d/", UID);
1476 unsigned GID = C.getGID();
1477 outs() << format("%-3d ", GID);
Kevin Enderbyc12718932015-01-16 22:10:36 +00001478 uint64_t Size = C.getRawSize();
Kevin Enderby479ee612015-01-23 21:02:44 +00001479 outs() << format("%5" PRId64, Size) << " ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001480
1481 StringRef RawLastModified = C.getRawLastModified();
1482 if (verbose) {
1483 unsigned Seconds;
1484 if (RawLastModified.getAsInteger(10, Seconds))
1485 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1486 else {
1487 // Since cime(3) returns a 26 character string of the form:
1488 // "Sun Sep 16 01:03:52 1973\n\0"
1489 // just print 24 characters.
1490 time_t t = Seconds;
1491 outs() << format("%.24s ", ctime(&t));
1492 }
1493 } else {
1494 outs() << RawLastModified << " ";
1495 }
1496
1497 if (verbose) {
1498 ErrorOr<StringRef> NameOrErr = C.getName();
1499 if (NameOrErr.getError()) {
1500 StringRef RawName = C.getRawName();
1501 outs() << RawName << "\n";
1502 } else {
1503 StringRef Name = NameOrErr.get();
1504 outs() << Name << "\n";
1505 }
1506 } else {
1507 StringRef RawName = C.getRawName();
1508 outs() << RawName << "\n";
1509 }
1510}
1511
1512static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
1513 if (A->hasSymbolTable()) {
1514 Archive::child_iterator S = A->getSymbolTableChild();
1515 Archive::Child C = *S;
1516 printArchiveChild(C, verbose, print_offset);
1517 }
1518 for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E;
1519 ++I) {
1520 Archive::Child C = *I;
1521 printArchiveChild(C, verbose, print_offset);
1522 }
1523}
1524
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001525// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1526// -arch flags selecting just those slices as specified by them and also parses
1527// archive files. Then for each individual Mach-O file ProcessMachO() is
1528// called to process the file based on the command line options.
1529void llvm::ParseInputMachO(StringRef Filename) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001530 // Check for -arch all and verifiy the -arch flags are valid.
1531 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1532 if (ArchFlags[i] == "all") {
1533 ArchAll = true;
1534 } else {
1535 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1536 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1537 "'for the -arch option\n";
1538 return;
1539 }
1540 }
1541 }
1542
1543 // Attempt to open the binary.
1544 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1545 if (std::error_code EC = BinaryOrErr.getError()) {
1546 errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n";
Rafael Espindolade882cd2014-12-03 23:29:34 +00001547 return;
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001548 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001549 Binary &Bin = *BinaryOrErr.get().getBinary();
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001550
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001551 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1552 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001553 if (ArchiveHeaders)
1554 printArchiveHeaders(A, true, false);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001555 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1556 I != E; ++I) {
1557 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary();
1558 if (ChildOrErr.getError())
1559 continue;
1560 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1561 if (!checkMachOAndArchFlags(O, Filename))
1562 return;
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001563 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001564 }
1565 }
1566 return;
1567 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001568 if (UniversalHeaders) {
1569 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
Kevin Enderbyf0640752015-03-13 17:56:32 +00001570 printMachOUniversalHeaders(UB, !NonVerbose);
Kevin Enderby131d1772015-01-09 19:22:37 +00001571 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001572 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1573 // If we have a list of architecture flags specified dump only those.
1574 if (!ArchAll && ArchFlags.size() != 0) {
1575 // Look for a slice in the universal binary that matches each ArchFlag.
1576 bool ArchFound;
1577 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1578 ArchFound = false;
1579 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1580 E = UB->end_objects();
1581 I != E; ++I) {
1582 if (ArchFlags[i] == I->getArchTypeName()) {
1583 ArchFound = true;
1584 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1585 I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001586 std::string ArchitectureName = "";
1587 if (ArchFlags.size() > 1)
1588 ArchitectureName = I->getArchTypeName();
1589 if (ObjOrErr) {
1590 ObjectFile &O = *ObjOrErr.get();
1591 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001592 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001593 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1594 I->getAsArchive()) {
1595 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001596 outs() << "Archive : " << Filename;
1597 if (!ArchitectureName.empty())
1598 outs() << " (architecture " << ArchitectureName << ")";
1599 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001600 if (ArchiveHeaders)
1601 printArchiveHeaders(A.get(), true, false);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001602 for (Archive::child_iterator AI = A->child_begin(),
1603 AE = A->child_end();
1604 AI != AE; ++AI) {
1605 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1606 if (ChildOrErr.getError())
1607 continue;
1608 if (MachOObjectFile *O =
1609 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001610 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001611 }
1612 }
1613 }
1614 }
1615 if (!ArchFound) {
1616 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1617 << "architecture: " + ArchFlags[i] + "\n";
1618 return;
1619 }
1620 }
1621 return;
1622 }
1623 // No architecture flags were specified so if this contains a slice that
1624 // matches the host architecture dump only that.
1625 if (!ArchAll) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001626 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1627 E = UB->end_objects();
1628 I != E; ++I) {
Kevin Enderby0512bd72015-01-09 21:55:03 +00001629 if (MachOObjectFile::getHostArch().getArchName() ==
1630 I->getArchTypeName()) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001631 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001632 std::string ArchiveName;
1633 ArchiveName.clear();
1634 if (ObjOrErr) {
1635 ObjectFile &O = *ObjOrErr.get();
1636 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001637 ProcessMachO(Filename, MachOOF);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001638 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1639 I->getAsArchive()) {
1640 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001641 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001642 if (ArchiveHeaders)
1643 printArchiveHeaders(A.get(), true, false);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001644 for (Archive::child_iterator AI = A->child_begin(),
1645 AE = A->child_end();
1646 AI != AE; ++AI) {
1647 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1648 if (ChildOrErr.getError())
1649 continue;
1650 if (MachOObjectFile *O =
1651 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001652 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001653 }
1654 }
1655 return;
1656 }
1657 }
1658 }
1659 // Either all architectures have been specified or none have been specified
1660 // and this does not contain the host architecture so dump all the slices.
1661 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1662 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1663 E = UB->end_objects();
1664 I != E; ++I) {
1665 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001666 std::string ArchitectureName = "";
1667 if (moreThanOneArch)
1668 ArchitectureName = I->getArchTypeName();
1669 if (ObjOrErr) {
1670 ObjectFile &Obj = *ObjOrErr.get();
1671 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001672 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001673 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1674 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001675 outs() << "Archive : " << Filename;
1676 if (!ArchitectureName.empty())
1677 outs() << " (architecture " << ArchitectureName << ")";
1678 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001679 if (ArchiveHeaders)
1680 printArchiveHeaders(A.get(), true, false);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001681 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1682 AI != AE; ++AI) {
1683 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1684 if (ChildOrErr.getError())
1685 continue;
1686 if (MachOObjectFile *O =
1687 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1688 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001689 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1690 ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001691 }
1692 }
1693 }
1694 }
1695 return;
1696 }
1697 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1698 if (!checkMachOAndArchFlags(O, Filename))
1699 return;
1700 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001701 ProcessMachO(Filename, MachOOF);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001702 } else
1703 errs() << "llvm-objdump: '" << Filename << "': "
1704 << "Object is not a Mach-O file type.\n";
1705 } else
1706 errs() << "llvm-objdump: '" << Filename << "': "
1707 << "Unrecognized file type.\n";
Rafael Espindola9b709252013-04-13 01:45:40 +00001708}
1709
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001710typedef std::pair<uint64_t, const char *> BindInfoEntry;
1711typedef std::vector<BindInfoEntry> BindTable;
1712typedef BindTable::iterator bind_table_iterator;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001713
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001714// The block of info used by the Symbolizer call backs.
1715struct DisassembleInfo {
1716 bool verbose;
1717 MachOObjectFile *O;
1718 SectionRef S;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001719 SymbolAddressMap *AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001720 std::vector<SectionRef> *Sections;
1721 const char *class_name;
1722 const char *selector_name;
1723 char *method;
Kevin Enderby04bf6932014-10-28 23:39:46 +00001724 char *demangled_name;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001725 uint64_t adrp_addr;
1726 uint32_t adrp_inst;
Kevin Enderby078be602014-10-23 19:53:12 +00001727 BindTable *bindtable;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001728};
1729
1730// SymbolizerGetOpInfo() is the operand information call back function.
1731// This is called to get the symbolic information for operand(s) of an
1732// instruction when it is being done. This routine does this from
1733// the relocation information, symbol table, etc. That block of information
1734// is a pointer to the struct DisassembleInfo that was passed when the
1735// disassembler context was created and passed to back to here when
1736// called back by the disassembler for instruction operands that could have
1737// relocation information. The address of the instruction containing operand is
1738// at the Pc parameter. The immediate value the operand has is passed in
1739// op_info->Value and is at Offset past the start of the instruction and has a
1740// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1741// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1742// names and addends of the symbolic expression to add for the operand. The
1743// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1744// information is returned then this function returns 1 else it returns 0.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00001745static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1746 uint64_t Size, int TagType, void *TagBuf) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001747 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1748 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001749 uint64_t value = op_info->Value;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001750
1751 // Make sure all fields returned are zero if we don't set them.
1752 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1753 op_info->Value = value;
1754
1755 // If the TagType is not the value 1 which it code knows about or if no
1756 // verbose symbolic information is wanted then just return 0, indicating no
1757 // information is being returned.
David Blaikie33dd45d02015-03-23 18:39:02 +00001758 if (TagType != 1 || !info->verbose)
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001759 return 0;
1760
1761 unsigned int Arch = info->O->getArch();
1762 if (Arch == Triple::x86) {
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001763 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1764 return 0;
1765 // First search the section's relocation entries (if any) for an entry
1766 // for this section offset.
1767 uint32_t sect_addr = info->S.getAddress();
1768 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1769 bool reloc_found = false;
1770 DataRefImpl Rel;
1771 MachO::any_relocation_info RE;
1772 bool isExtern = false;
1773 SymbolRef Symbol;
1774 bool r_scattered = false;
1775 uint32_t r_value, pair_r_value, r_type;
1776 for (const RelocationRef &Reloc : info->S.relocations()) {
1777 uint64_t RelocOffset;
1778 Reloc.getOffset(RelocOffset);
1779 if (RelocOffset == sect_offset) {
1780 Rel = Reloc.getRawDataRefImpl();
1781 RE = info->O->getRelocation(Rel);
Kevin Enderby3eb73e12014-11-11 19:16:45 +00001782 r_type = info->O->getAnyRelocationType(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001783 r_scattered = info->O->isRelocationScattered(RE);
1784 if (r_scattered) {
1785 r_value = info->O->getScatteredRelocationValue(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001786 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1787 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1788 DataRefImpl RelNext = Rel;
1789 info->O->moveRelocationNext(RelNext);
1790 MachO::any_relocation_info RENext;
1791 RENext = info->O->getRelocation(RelNext);
1792 if (info->O->isRelocationScattered(RENext))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001793 pair_r_value = info->O->getScatteredRelocationValue(RENext);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001794 else
1795 return 0;
1796 }
1797 } else {
1798 isExtern = info->O->getPlainRelocationExternal(RE);
1799 if (isExtern) {
1800 symbol_iterator RelocSym = Reloc.getSymbol();
1801 Symbol = *RelocSym;
1802 }
1803 }
1804 reloc_found = true;
1805 break;
1806 }
1807 }
1808 if (reloc_found && isExtern) {
1809 StringRef SymName;
1810 Symbol.getName(SymName);
1811 const char *name = SymName.data();
1812 op_info->AddSymbol.Present = 1;
1813 op_info->AddSymbol.Name = name;
1814 // For i386 extern relocation entries the value in the instruction is
1815 // the offset from the symbol, and value is already set in op_info->Value.
1816 return 1;
1817 }
1818 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1819 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001820 const char *add = GuessSymbolName(r_value, info->AddrMap);
1821 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001822 uint32_t offset = value - (r_value - pair_r_value);
1823 op_info->AddSymbol.Present = 1;
1824 if (add != nullptr)
1825 op_info->AddSymbol.Name = add;
1826 else
1827 op_info->AddSymbol.Value = r_value;
1828 op_info->SubtractSymbol.Present = 1;
1829 if (sub != nullptr)
1830 op_info->SubtractSymbol.Name = sub;
1831 else
1832 op_info->SubtractSymbol.Value = pair_r_value;
1833 op_info->Value = offset;
1834 return 1;
1835 }
1836 // TODO:
1837 // Second search the external relocation entries of a fully linked image
1838 // (if any) for an entry that matches this segment offset.
1839 // uint32_t seg_offset = (Pc + Offset);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001840 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001841 }
1842 if (Arch == Triple::x86_64) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001843 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1844 return 0;
1845 // First search the section's relocation entries (if any) for an entry
1846 // for this section offset.
Rafael Espindola80291272014-10-08 15:28:58 +00001847 uint64_t sect_addr = info->S.getAddress();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001848 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1849 bool reloc_found = false;
1850 DataRefImpl Rel;
1851 MachO::any_relocation_info RE;
1852 bool isExtern = false;
1853 SymbolRef Symbol;
1854 for (const RelocationRef &Reloc : info->S.relocations()) {
1855 uint64_t RelocOffset;
1856 Reloc.getOffset(RelocOffset);
1857 if (RelocOffset == sect_offset) {
1858 Rel = Reloc.getRawDataRefImpl();
1859 RE = info->O->getRelocation(Rel);
1860 // NOTE: Scattered relocations don't exist on x86_64.
1861 isExtern = info->O->getPlainRelocationExternal(RE);
1862 if (isExtern) {
1863 symbol_iterator RelocSym = Reloc.getSymbol();
1864 Symbol = *RelocSym;
1865 }
1866 reloc_found = true;
1867 break;
1868 }
1869 }
1870 if (reloc_found && isExtern) {
1871 // The Value passed in will be adjusted by the Pc if the instruction
1872 // adds the Pc. But for x86_64 external relocation entries the Value
1873 // is the offset from the external symbol.
1874 if (info->O->getAnyRelocationPCRel(RE))
1875 op_info->Value -= Pc + Offset + Size;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001876 StringRef SymName;
1877 Symbol.getName(SymName);
1878 const char *name = SymName.data();
1879 unsigned Type = info->O->getAnyRelocationType(RE);
1880 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1881 DataRefImpl RelNext = Rel;
1882 info->O->moveRelocationNext(RelNext);
1883 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1884 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1885 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1886 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1887 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1888 op_info->SubtractSymbol.Present = 1;
1889 op_info->SubtractSymbol.Name = name;
1890 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1891 Symbol = *RelocSymNext;
1892 StringRef SymNameNext;
1893 Symbol.getName(SymNameNext);
1894 name = SymNameNext.data();
1895 }
1896 }
1897 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1898 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1899 op_info->AddSymbol.Present = 1;
1900 op_info->AddSymbol.Name = name;
1901 return 1;
1902 }
1903 // TODO:
1904 // Second search the external relocation entries of a fully linked image
1905 // (if any) for an entry that matches this segment offset.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001906 // uint64_t seg_offset = (Pc + Offset);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001907 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001908 }
1909 if (Arch == Triple::arm) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001910 if (Offset != 0 || (Size != 4 && Size != 2))
1911 return 0;
1912 // First search the section's relocation entries (if any) for an entry
1913 // for this section offset.
1914 uint32_t sect_addr = info->S.getAddress();
1915 uint32_t sect_offset = (Pc + Offset) - sect_addr;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001916 DataRefImpl Rel;
1917 MachO::any_relocation_info RE;
1918 bool isExtern = false;
1919 SymbolRef Symbol;
1920 bool r_scattered = false;
1921 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
David Blaikie33dd45d02015-03-23 18:39:02 +00001922 auto Reloc =
1923 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1924 [&](const RelocationRef &Reloc) {
1925 uint64_t RelocOffset;
1926 Reloc.getOffset(RelocOffset);
1927 return RelocOffset == sect_offset;
1928 });
1929
1930 if (Reloc == info->S.relocations().end())
1931 return 0;
1932
1933 Rel = Reloc->getRawDataRefImpl();
1934 RE = info->O->getRelocation(Rel);
1935 r_length = info->O->getAnyRelocationLength(RE);
1936 r_scattered = info->O->isRelocationScattered(RE);
1937 if (r_scattered) {
1938 r_value = info->O->getScatteredRelocationValue(RE);
1939 r_type = info->O->getScatteredRelocationType(RE);
1940 } else {
1941 r_type = info->O->getAnyRelocationType(RE);
1942 isExtern = info->O->getPlainRelocationExternal(RE);
1943 if (isExtern) {
1944 symbol_iterator RelocSym = Reloc->getSymbol();
1945 Symbol = *RelocSym;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001946 }
1947 }
David Blaikie33dd45d02015-03-23 18:39:02 +00001948 if (r_type == MachO::ARM_RELOC_HALF ||
1949 r_type == MachO::ARM_RELOC_SECTDIFF ||
1950 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
1951 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1952 DataRefImpl RelNext = Rel;
1953 info->O->moveRelocationNext(RelNext);
1954 MachO::any_relocation_info RENext;
1955 RENext = info->O->getRelocation(RelNext);
1956 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
1957 if (info->O->isRelocationScattered(RENext))
1958 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1959 }
1960
1961 if (isExtern) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001962 StringRef SymName;
1963 Symbol.getName(SymName);
1964 const char *name = SymName.data();
1965 op_info->AddSymbol.Present = 1;
1966 op_info->AddSymbol.Name = name;
Sylvestre Ledru648cced2015-02-05 17:00:23 +00001967 switch (r_type) {
1968 case MachO::ARM_RELOC_HALF:
1969 if ((r_length & 0x1) == 1) {
1970 op_info->Value = value << 16 | other_half;
1971 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1972 } else {
1973 op_info->Value = other_half << 16 | value;
1974 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Sylvestre Ledrufe0c7ad2015-02-05 16:35:44 +00001975 }
Sylvestre Ledru648cced2015-02-05 17:00:23 +00001976 break;
1977 default:
1978 break;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001979 }
1980 return 1;
1981 }
1982 // If we have a branch that is not an external relocation entry then
1983 // return 0 so the code in tryAddingSymbolicOperand() can use the
1984 // SymbolLookUp call back with the branch target address to look up the
1985 // symbol and possiblity add an annotation for a symbol stub.
David Blaikie33dd45d02015-03-23 18:39:02 +00001986 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
1987 r_type == MachO::ARM_THUMB_RELOC_BR22))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001988 return 0;
1989
1990 uint32_t offset = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001991 if (r_type == MachO::ARM_RELOC_HALF ||
1992 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1993 if ((r_length & 0x1) == 1)
1994 value = value << 16 | other_half;
1995 else
1996 value = other_half << 16 | value;
1997 }
1998 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
1999 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2000 offset = value - r_value;
2001 value = r_value;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002002 }
2003
David Blaikie33dd45d02015-03-23 18:39:02 +00002004 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00002005 if ((r_length & 0x1) == 1)
2006 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2007 else
2008 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002009 const char *add = GuessSymbolName(r_value, info->AddrMap);
2010 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002011 int32_t offset = value - (r_value - pair_r_value);
2012 op_info->AddSymbol.Present = 1;
2013 if (add != nullptr)
2014 op_info->AddSymbol.Name = add;
2015 else
2016 op_info->AddSymbol.Value = r_value;
2017 op_info->SubtractSymbol.Present = 1;
2018 if (sub != nullptr)
2019 op_info->SubtractSymbol.Name = sub;
2020 else
2021 op_info->SubtractSymbol.Value = pair_r_value;
2022 op_info->Value = offset;
2023 return 1;
2024 }
2025
Kevin Enderby930fdc72014-11-06 19:00:13 +00002026 op_info->AddSymbol.Present = 1;
2027 op_info->Value = offset;
David Blaikie33dd45d02015-03-23 18:39:02 +00002028 if (r_type == MachO::ARM_RELOC_HALF) {
2029 if ((r_length & 0x1) == 1)
2030 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2031 else
2032 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002033 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002034 const char *add = GuessSymbolName(value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002035 if (add != nullptr) {
2036 op_info->AddSymbol.Name = add;
2037 return 1;
2038 }
2039 op_info->AddSymbol.Value = value;
2040 return 1;
David Blaikie33dd45d02015-03-23 18:39:02 +00002041 }
2042 if (Arch == Triple::aarch64) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002043 if (Offset != 0 || Size != 4)
2044 return 0;
2045 // First search the section's relocation entries (if any) for an entry
2046 // for this section offset.
2047 uint64_t sect_addr = info->S.getAddress();
2048 uint64_t sect_offset = (Pc + Offset) - sect_addr;
David Blaikie33dd45d02015-03-23 18:39:02 +00002049 auto Reloc =
2050 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2051 [&](const RelocationRef &Reloc) {
2052 uint64_t RelocOffset;
2053 Reloc.getOffset(RelocOffset);
2054 return RelocOffset == sect_offset;
2055 });
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002056
David Blaikie33dd45d02015-03-23 18:39:02 +00002057 if (Reloc == info->S.relocations().end())
2058 return 0;
2059
2060 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2061 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2062 uint32_t r_type = info->O->getAnyRelocationType(RE);
2063 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2064 DataRefImpl RelNext = Rel;
2065 info->O->moveRelocationNext(RelNext);
2066 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2067 if (value == 0) {
2068 value = info->O->getPlainRelocationSymbolNum(RENext);
2069 op_info->Value = value;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002070 }
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002071 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002072 // NOTE: Scattered relocations don't exist on arm64.
2073 if (!info->O->getPlainRelocationExternal(RE))
2074 return 0;
2075 StringRef SymName;
2076 Reloc->getSymbol()->getName(SymName);
2077 const char *name = SymName.data();
2078 op_info->AddSymbol.Present = 1;
2079 op_info->AddSymbol.Name = name;
2080
2081 switch (r_type) {
2082 case MachO::ARM64_RELOC_PAGE21:
2083 /* @page */
2084 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2085 break;
2086 case MachO::ARM64_RELOC_PAGEOFF12:
2087 /* @pageoff */
2088 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2089 break;
2090 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2091 /* @gotpage */
2092 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2093 break;
2094 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2095 /* @gotpageoff */
2096 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2097 break;
2098 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2099 /* @tvlppage is not implemented in llvm-mc */
2100 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2101 break;
2102 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2103 /* @tvlppageoff is not implemented in llvm-mc */
2104 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2105 break;
2106 default:
2107 case MachO::ARM64_RELOC_BRANCH26:
2108 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2109 break;
2110 }
2111 return 1;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002112 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002113 return 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002114}
2115
Kevin Enderbybf246f52014-09-24 23:08:22 +00002116// GuessCstringPointer is passed the address of what might be a pointer to a
2117// literal string in a cstring section. If that address is in a cstring section
2118// it returns a pointer to that string. Else it returns nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002119static const char *GuessCstringPointer(uint64_t ReferenceValue,
2120 struct DisassembleInfo *info) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00002121 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
2122 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
2123 for (unsigned I = 0;; ++I) {
2124 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2125 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2126 for (unsigned J = 0; J < Seg.nsects; ++J) {
2127 MachO::section_64 Sec = info->O->getSection64(Load, J);
2128 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2129 if (section_type == MachO::S_CSTRING_LITERALS &&
2130 ReferenceValue >= Sec.addr &&
2131 ReferenceValue < Sec.addr + Sec.size) {
2132 uint64_t sect_offset = ReferenceValue - Sec.addr;
2133 uint64_t object_offset = Sec.offset + sect_offset;
2134 StringRef MachOContents = info->O->getData();
2135 uint64_t object_size = MachOContents.size();
2136 const char *object_addr = (const char *)MachOContents.data();
2137 if (object_offset < object_size) {
2138 const char *name = object_addr + object_offset;
2139 return name;
2140 } else {
2141 return nullptr;
2142 }
2143 }
2144 }
2145 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2146 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2147 for (unsigned J = 0; J < Seg.nsects; ++J) {
2148 MachO::section Sec = info->O->getSection(Load, J);
2149 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2150 if (section_type == MachO::S_CSTRING_LITERALS &&
2151 ReferenceValue >= Sec.addr &&
2152 ReferenceValue < Sec.addr + Sec.size) {
2153 uint64_t sect_offset = ReferenceValue - Sec.addr;
2154 uint64_t object_offset = Sec.offset + sect_offset;
2155 StringRef MachOContents = info->O->getData();
2156 uint64_t object_size = MachOContents.size();
2157 const char *object_addr = (const char *)MachOContents.data();
2158 if (object_offset < object_size) {
2159 const char *name = object_addr + object_offset;
2160 return name;
2161 } else {
2162 return nullptr;
2163 }
2164 }
2165 }
2166 }
2167 if (I == LoadCommandCount - 1)
2168 break;
2169 else
2170 Load = info->O->getNextLoadCommandInfo(Load);
2171 }
2172 return nullptr;
2173}
2174
Kevin Enderby85974882014-09-26 22:20:44 +00002175// GuessIndirectSymbol returns the name of the indirect symbol for the
2176// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2177// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2178// symbol name being referenced by the stub or pointer.
2179static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2180 struct DisassembleInfo *info) {
2181 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
2182 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
2183 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2184 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
2185 for (unsigned I = 0;; ++I) {
2186 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2187 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2188 for (unsigned J = 0; J < Seg.nsects; ++J) {
2189 MachO::section_64 Sec = info->O->getSection64(Load, J);
2190 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2191 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2192 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2193 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2194 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2195 section_type == MachO::S_SYMBOL_STUBS) &&
2196 ReferenceValue >= Sec.addr &&
2197 ReferenceValue < Sec.addr + Sec.size) {
2198 uint32_t stride;
2199 if (section_type == MachO::S_SYMBOL_STUBS)
2200 stride = Sec.reserved2;
2201 else
2202 stride = 8;
2203 if (stride == 0)
2204 return nullptr;
2205 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2206 if (index < Dysymtab.nindirectsyms) {
2207 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002208 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002209 if (indirect_symbol < Symtab.nsyms) {
2210 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2211 SymbolRef Symbol = *Sym;
2212 StringRef SymName;
2213 Symbol.getName(SymName);
2214 const char *name = SymName.data();
2215 return name;
2216 }
2217 }
2218 }
2219 }
2220 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2221 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2222 for (unsigned J = 0; J < Seg.nsects; ++J) {
2223 MachO::section Sec = info->O->getSection(Load, J);
2224 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2225 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2226 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2227 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2228 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2229 section_type == MachO::S_SYMBOL_STUBS) &&
2230 ReferenceValue >= Sec.addr &&
2231 ReferenceValue < Sec.addr + Sec.size) {
2232 uint32_t stride;
2233 if (section_type == MachO::S_SYMBOL_STUBS)
2234 stride = Sec.reserved2;
2235 else
2236 stride = 4;
2237 if (stride == 0)
2238 return nullptr;
2239 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2240 if (index < Dysymtab.nindirectsyms) {
2241 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002242 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002243 if (indirect_symbol < Symtab.nsyms) {
2244 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2245 SymbolRef Symbol = *Sym;
2246 StringRef SymName;
2247 Symbol.getName(SymName);
2248 const char *name = SymName.data();
2249 return name;
2250 }
2251 }
2252 }
2253 }
2254 }
2255 if (I == LoadCommandCount - 1)
2256 break;
2257 else
2258 Load = info->O->getNextLoadCommandInfo(Load);
2259 }
2260 return nullptr;
2261}
2262
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002263// method_reference() is called passing it the ReferenceName that might be
2264// a reference it to an Objective-C method call. If so then it allocates and
2265// assembles a method call string with the values last seen and saved in
2266// the DisassembleInfo's class_name and selector_name fields. This is saved
2267// into the method field of the info and any previous string is free'ed.
2268// Then the class_name field in the info is set to nullptr. The method call
2269// string is set into ReferenceName and ReferenceType is set to
2270// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2271// then both ReferenceType and ReferenceName are left unchanged.
2272static void method_reference(struct DisassembleInfo *info,
2273 uint64_t *ReferenceType,
2274 const char **ReferenceName) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002275 unsigned int Arch = info->O->getArch();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002276 if (*ReferenceName != nullptr) {
2277 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002278 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002279 if (info->method != nullptr)
2280 free(info->method);
2281 if (info->class_name != nullptr) {
2282 info->method = (char *)malloc(5 + strlen(info->class_name) +
2283 strlen(info->selector_name));
2284 if (info->method != nullptr) {
2285 strcpy(info->method, "+[");
2286 strcat(info->method, info->class_name);
2287 strcat(info->method, " ");
2288 strcat(info->method, info->selector_name);
2289 strcat(info->method, "]");
2290 *ReferenceName = info->method;
2291 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2292 }
2293 } else {
2294 info->method = (char *)malloc(9 + strlen(info->selector_name));
2295 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002296 if (Arch == Triple::x86_64)
2297 strcpy(info->method, "-[%rdi ");
2298 else if (Arch == Triple::aarch64)
2299 strcpy(info->method, "-[x0 ");
2300 else
2301 strcpy(info->method, "-[r? ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002302 strcat(info->method, info->selector_name);
2303 strcat(info->method, "]");
2304 *ReferenceName = info->method;
2305 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2306 }
2307 }
2308 info->class_name = nullptr;
2309 }
2310 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002311 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002312 if (info->method != nullptr)
2313 free(info->method);
2314 info->method = (char *)malloc(17 + strlen(info->selector_name));
2315 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002316 if (Arch == Triple::x86_64)
2317 strcpy(info->method, "-[[%rdi super] ");
2318 else if (Arch == Triple::aarch64)
2319 strcpy(info->method, "-[[x0 super] ");
2320 else
2321 strcpy(info->method, "-[[r? super] ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002322 strcat(info->method, info->selector_name);
2323 strcat(info->method, "]");
2324 *ReferenceName = info->method;
2325 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2326 }
2327 info->class_name = nullptr;
2328 }
2329 }
2330 }
2331}
2332
2333// GuessPointerPointer() is passed the address of what might be a pointer to
2334// a reference to an Objective-C class, selector, message ref or cfstring.
2335// If so the value of the pointer is returned and one of the booleans are set
2336// to true. If not zero is returned and all the booleans are set to false.
2337static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2338 struct DisassembleInfo *info,
2339 bool &classref, bool &selref, bool &msgref,
2340 bool &cfstring) {
2341 classref = false;
2342 selref = false;
2343 msgref = false;
2344 cfstring = false;
2345 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
2346 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
2347 for (unsigned I = 0;; ++I) {
2348 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2349 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2350 for (unsigned J = 0; J < Seg.nsects; ++J) {
2351 MachO::section_64 Sec = info->O->getSection64(Load, J);
2352 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2353 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2354 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2355 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2356 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2357 ReferenceValue >= Sec.addr &&
2358 ReferenceValue < Sec.addr + Sec.size) {
2359 uint64_t sect_offset = ReferenceValue - Sec.addr;
2360 uint64_t object_offset = Sec.offset + sect_offset;
2361 StringRef MachOContents = info->O->getData();
2362 uint64_t object_size = MachOContents.size();
2363 const char *object_addr = (const char *)MachOContents.data();
2364 if (object_offset < object_size) {
2365 uint64_t pointer_value;
2366 memcpy(&pointer_value, object_addr + object_offset,
2367 sizeof(uint64_t));
2368 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2369 sys::swapByteOrder(pointer_value);
2370 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2371 selref = true;
2372 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2373 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2374 classref = true;
2375 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2376 ReferenceValue + 8 < Sec.addr + Sec.size) {
2377 msgref = true;
2378 memcpy(&pointer_value, object_addr + object_offset + 8,
2379 sizeof(uint64_t));
2380 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2381 sys::swapByteOrder(pointer_value);
2382 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2383 cfstring = true;
2384 return pointer_value;
2385 } else {
2386 return 0;
2387 }
2388 }
2389 }
2390 }
2391 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
2392 if (I == LoadCommandCount - 1)
2393 break;
2394 else
2395 Load = info->O->getNextLoadCommandInfo(Load);
2396 }
2397 return 0;
2398}
2399
2400// get_pointer_64 returns a pointer to the bytes in the object file at the
2401// Address from a section in the Mach-O file. And indirectly returns the
2402// offset into the section, number of bytes left in the section past the offset
2403// and which section is was being referenced. If the Address is not in a
2404// section nullptr is returned.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002405static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2406 uint32_t &left, SectionRef &S,
2407 DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002408 offset = 0;
2409 left = 0;
2410 S = SectionRef();
2411 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2412 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2413 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
2414 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2415 S = (*(info->Sections))[SectIdx];
2416 offset = Address - SectAddress;
2417 left = SectSize - offset;
2418 StringRef SectContents;
2419 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2420 return SectContents.data() + offset;
2421 }
2422 }
2423 return nullptr;
2424}
2425
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002426static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2427 uint32_t &left, SectionRef &S,
2428 DisassembleInfo *info) {
2429 return get_pointer_64(Address, offset, left, S, info);
2430}
2431
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002432// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2433// the symbol indirectly through n_value. Based on the relocation information
2434// for the specified section offset in the specified section reference.
Kevin Enderby0fc11822015-04-01 20:57:01 +00002435// If no relocation information is found and a non-zero ReferenceValue for the
2436// symbol is passed, look up that address in the info's AddrMap.
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002437static const char *
2438get_symbol_64(uint32_t sect_offset, SectionRef S, DisassembleInfo *info,
2439 uint64_t &n_value,
2440 uint64_t ReferenceValue = UnknownAddressOrSize) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002441 n_value = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002442 if (!info->verbose)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002443 return nullptr;
2444
2445 // See if there is an external relocation entry at the sect_offset.
2446 bool reloc_found = false;
2447 DataRefImpl Rel;
2448 MachO::any_relocation_info RE;
2449 bool isExtern = false;
2450 SymbolRef Symbol;
2451 for (const RelocationRef &Reloc : S.relocations()) {
2452 uint64_t RelocOffset;
2453 Reloc.getOffset(RelocOffset);
2454 if (RelocOffset == sect_offset) {
2455 Rel = Reloc.getRawDataRefImpl();
2456 RE = info->O->getRelocation(Rel);
2457 if (info->O->isRelocationScattered(RE))
2458 continue;
2459 isExtern = info->O->getPlainRelocationExternal(RE);
2460 if (isExtern) {
2461 symbol_iterator RelocSym = Reloc.getSymbol();
2462 Symbol = *RelocSym;
2463 }
2464 reloc_found = true;
2465 break;
2466 }
2467 }
2468 // If there is an external relocation entry for a symbol in this section
2469 // at this section_offset then use that symbol's value for the n_value
2470 // and return its name.
2471 const char *SymbolName = nullptr;
2472 if (reloc_found && isExtern) {
2473 Symbol.getAddress(n_value);
Kevin Enderby0fc11822015-04-01 20:57:01 +00002474 if (n_value == UnknownAddressOrSize)
2475 n_value = 0;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002476 StringRef name;
2477 Symbol.getName(name);
2478 if (!name.empty()) {
2479 SymbolName = name.data();
2480 return SymbolName;
2481 }
2482 }
2483
2484 // TODO: For fully linked images, look through the external relocation
2485 // entries off the dynamic symtab command. For these the r_offset is from the
2486 // start of the first writeable segment in the Mach-O file. So the offset
2487 // to this section from that segment is passed to this routine by the caller,
2488 // as the database_offset. Which is the difference of the section's starting
2489 // address and the first writable segment.
2490 //
2491 // NOTE: need add passing the database_offset to this routine.
2492
Kevin Enderby0fc11822015-04-01 20:57:01 +00002493 // We did not find an external relocation entry so look up the ReferenceValue
2494 // as an address of a symbol and if found return that symbol's name.
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002495 if (ReferenceValue != UnknownAddressOrSize)
Kevin Enderby0fc11822015-04-01 20:57:01 +00002496 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002497
2498 return SymbolName;
2499}
2500
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002501static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2502 DisassembleInfo *info,
2503 uint32_t ReferenceValue) {
2504 uint64_t n_value64;
2505 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2506}
2507
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002508// These are structs in the Objective-C meta data and read to produce the
2509// comments for disassembly. While these are part of the ABI they are no
2510// public defintions. So the are here not in include/llvm/Support/MachO.h .
2511
2512// The cfstring object in a 64-bit Mach-O file.
2513struct cfstring64_t {
2514 uint64_t isa; // class64_t * (64-bit pointer)
2515 uint64_t flags; // flag bits
2516 uint64_t characters; // char * (64-bit pointer)
2517 uint64_t length; // number of non-NULL characters in above
2518};
2519
2520// The class object in a 64-bit Mach-O file.
2521struct class64_t {
2522 uint64_t isa; // class64_t * (64-bit pointer)
2523 uint64_t superclass; // class64_t * (64-bit pointer)
2524 uint64_t cache; // Cache (64-bit pointer)
2525 uint64_t vtable; // IMP * (64-bit pointer)
2526 uint64_t data; // class_ro64_t * (64-bit pointer)
2527};
2528
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002529struct class32_t {
2530 uint32_t isa; /* class32_t * (32-bit pointer) */
2531 uint32_t superclass; /* class32_t * (32-bit pointer) */
2532 uint32_t cache; /* Cache (32-bit pointer) */
2533 uint32_t vtable; /* IMP * (32-bit pointer) */
2534 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2535};
2536
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002537struct class_ro64_t {
2538 uint32_t flags;
2539 uint32_t instanceStart;
2540 uint32_t instanceSize;
2541 uint32_t reserved;
2542 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2543 uint64_t name; // const char * (64-bit pointer)
2544 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2545 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2546 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2547 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2548 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2549};
2550
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002551struct class_ro32_t {
2552 uint32_t flags;
2553 uint32_t instanceStart;
2554 uint32_t instanceSize;
2555 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2556 uint32_t name; /* const char * (32-bit pointer) */
2557 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2558 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2559 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2560 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2561 uint32_t baseProperties; /* const struct objc_property_list *
2562 (32-bit pointer) */
2563};
2564
2565/* Values for class_ro{64,32}_t->flags */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002566#define RO_META (1 << 0)
2567#define RO_ROOT (1 << 1)
2568#define RO_HAS_CXX_STRUCTORS (1 << 2)
2569
2570struct method_list64_t {
2571 uint32_t entsize;
2572 uint32_t count;
2573 /* struct method64_t first; These structures follow inline */
2574};
2575
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002576struct method_list32_t {
2577 uint32_t entsize;
2578 uint32_t count;
2579 /* struct method32_t first; These structures follow inline */
2580};
2581
Kevin Enderby0fc11822015-04-01 20:57:01 +00002582struct method64_t {
2583 uint64_t name; /* SEL (64-bit pointer) */
2584 uint64_t types; /* const char * (64-bit pointer) */
2585 uint64_t imp; /* IMP (64-bit pointer) */
2586};
2587
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002588struct method32_t {
2589 uint32_t name; /* SEL (32-bit pointer) */
2590 uint32_t types; /* const char * (32-bit pointer) */
2591 uint32_t imp; /* IMP (32-bit pointer) */
2592};
2593
Kevin Enderby0fc11822015-04-01 20:57:01 +00002594struct protocol_list64_t {
2595 uint64_t count; /* uintptr_t (a 64-bit value) */
2596 /* struct protocol64_t * list[0]; These pointers follow inline */
2597};
2598
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002599struct protocol_list32_t {
2600 uint32_t count; /* uintptr_t (a 32-bit value) */
2601 /* struct protocol32_t * list[0]; These pointers follow inline */
2602};
2603
Kevin Enderby0fc11822015-04-01 20:57:01 +00002604struct protocol64_t {
2605 uint64_t isa; /* id * (64-bit pointer) */
2606 uint64_t name; /* const char * (64-bit pointer) */
2607 uint64_t protocols; /* struct protocol_list64_t *
2608 (64-bit pointer) */
2609 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2610 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2611 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2612 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2613 uint64_t instanceProperties; /* struct objc_property_list *
2614 (64-bit pointer) */
2615};
2616
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002617struct protocol32_t {
2618 uint32_t isa; /* id * (32-bit pointer) */
2619 uint32_t name; /* const char * (32-bit pointer) */
2620 uint32_t protocols; /* struct protocol_list_t *
2621 (32-bit pointer) */
2622 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2623 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2624 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2625 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2626 uint32_t instanceProperties; /* struct objc_property_list *
2627 (32-bit pointer) */
2628};
2629
Kevin Enderby0fc11822015-04-01 20:57:01 +00002630struct ivar_list64_t {
2631 uint32_t entsize;
2632 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002633 /* struct ivar64_t first; These structures follow inline */
2634};
2635
2636struct ivar_list32_t {
2637 uint32_t entsize;
2638 uint32_t count;
2639 /* struct ivar32_t first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002640};
2641
2642struct ivar64_t {
2643 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2644 uint64_t name; /* const char * (64-bit pointer) */
2645 uint64_t type; /* const char * (64-bit pointer) */
2646 uint32_t alignment;
2647 uint32_t size;
2648};
2649
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002650struct ivar32_t {
2651 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2652 uint32_t name; /* const char * (32-bit pointer) */
2653 uint32_t type; /* const char * (32-bit pointer) */
2654 uint32_t alignment;
2655 uint32_t size;
2656};
2657
Kevin Enderby0fc11822015-04-01 20:57:01 +00002658struct objc_property_list64 {
2659 uint32_t entsize;
2660 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002661 /* struct objc_property64 first; These structures follow inline */
2662};
2663
2664struct objc_property_list32 {
2665 uint32_t entsize;
2666 uint32_t count;
2667 /* struct objc_property32 first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002668};
2669
2670struct objc_property64 {
2671 uint64_t name; /* const char * (64-bit pointer) */
2672 uint64_t attributes; /* const char * (64-bit pointer) */
2673};
2674
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002675struct objc_property32 {
2676 uint32_t name; /* const char * (32-bit pointer) */
2677 uint32_t attributes; /* const char * (32-bit pointer) */
2678};
2679
Kevin Enderby0fc11822015-04-01 20:57:01 +00002680struct category64_t {
2681 uint64_t name; /* const char * (64-bit pointer) */
2682 uint64_t cls; /* struct class_t * (64-bit pointer) */
2683 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2684 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2685 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2686 uint64_t instanceProperties; /* struct objc_property_list *
2687 (64-bit pointer) */
2688};
2689
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002690struct category32_t {
2691 uint32_t name; /* const char * (32-bit pointer) */
2692 uint32_t cls; /* struct class_t * (32-bit pointer) */
2693 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2694 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2695 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2696 uint32_t instanceProperties; /* struct objc_property_list *
2697 (32-bit pointer) */
2698};
2699
Kevin Enderby0fc11822015-04-01 20:57:01 +00002700struct objc_image_info64 {
2701 uint32_t version;
2702 uint32_t flags;
2703};
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002704struct objc_image_info32 {
2705 uint32_t version;
2706 uint32_t flags;
2707};
Kevin Enderby0fc11822015-04-01 20:57:01 +00002708/* masks for objc_image_info.flags */
2709#define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2710#define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2711
2712struct message_ref64 {
2713 uint64_t imp; /* IMP (64-bit pointer) */
2714 uint64_t sel; /* SEL (64-bit pointer) */
2715};
2716
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002717struct message_ref32 {
2718 uint32_t imp; /* IMP (32-bit pointer) */
2719 uint32_t sel; /* SEL (32-bit pointer) */
2720};
2721
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002722inline void swapStruct(struct cfstring64_t &cfs) {
2723 sys::swapByteOrder(cfs.isa);
2724 sys::swapByteOrder(cfs.flags);
2725 sys::swapByteOrder(cfs.characters);
2726 sys::swapByteOrder(cfs.length);
2727}
2728
2729inline void swapStruct(struct class64_t &c) {
2730 sys::swapByteOrder(c.isa);
2731 sys::swapByteOrder(c.superclass);
2732 sys::swapByteOrder(c.cache);
2733 sys::swapByteOrder(c.vtable);
2734 sys::swapByteOrder(c.data);
2735}
2736
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002737inline void swapStruct(struct class32_t &c) {
2738 sys::swapByteOrder(c.isa);
2739 sys::swapByteOrder(c.superclass);
2740 sys::swapByteOrder(c.cache);
2741 sys::swapByteOrder(c.vtable);
2742 sys::swapByteOrder(c.data);
2743}
2744
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002745inline void swapStruct(struct class_ro64_t &cro) {
2746 sys::swapByteOrder(cro.flags);
2747 sys::swapByteOrder(cro.instanceStart);
2748 sys::swapByteOrder(cro.instanceSize);
2749 sys::swapByteOrder(cro.reserved);
2750 sys::swapByteOrder(cro.ivarLayout);
2751 sys::swapByteOrder(cro.name);
2752 sys::swapByteOrder(cro.baseMethods);
2753 sys::swapByteOrder(cro.baseProtocols);
2754 sys::swapByteOrder(cro.ivars);
2755 sys::swapByteOrder(cro.weakIvarLayout);
2756 sys::swapByteOrder(cro.baseProperties);
2757}
2758
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002759inline void swapStruct(struct class_ro32_t &cro) {
2760 sys::swapByteOrder(cro.flags);
2761 sys::swapByteOrder(cro.instanceStart);
2762 sys::swapByteOrder(cro.instanceSize);
2763 sys::swapByteOrder(cro.ivarLayout);
2764 sys::swapByteOrder(cro.name);
2765 sys::swapByteOrder(cro.baseMethods);
2766 sys::swapByteOrder(cro.baseProtocols);
2767 sys::swapByteOrder(cro.ivars);
2768 sys::swapByteOrder(cro.weakIvarLayout);
2769 sys::swapByteOrder(cro.baseProperties);
2770}
2771
Kevin Enderby0fc11822015-04-01 20:57:01 +00002772inline void swapStruct(struct method_list64_t &ml) {
2773 sys::swapByteOrder(ml.entsize);
2774 sys::swapByteOrder(ml.count);
2775}
2776
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002777inline void swapStruct(struct method_list32_t &ml) {
2778 sys::swapByteOrder(ml.entsize);
2779 sys::swapByteOrder(ml.count);
2780}
2781
Kevin Enderby0fc11822015-04-01 20:57:01 +00002782inline void swapStruct(struct method64_t &m) {
2783 sys::swapByteOrder(m.name);
2784 sys::swapByteOrder(m.types);
2785 sys::swapByteOrder(m.imp);
2786}
2787
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002788inline void swapStruct(struct method32_t &m) {
2789 sys::swapByteOrder(m.name);
2790 sys::swapByteOrder(m.types);
2791 sys::swapByteOrder(m.imp);
2792}
2793
Kevin Enderby0fc11822015-04-01 20:57:01 +00002794inline void swapStruct(struct protocol_list64_t &pl) {
2795 sys::swapByteOrder(pl.count);
2796}
2797
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002798inline void swapStruct(struct protocol_list32_t &pl) {
2799 sys::swapByteOrder(pl.count);
2800}
2801
Kevin Enderby0fc11822015-04-01 20:57:01 +00002802inline void swapStruct(struct protocol64_t &p) {
2803 sys::swapByteOrder(p.isa);
2804 sys::swapByteOrder(p.name);
2805 sys::swapByteOrder(p.protocols);
2806 sys::swapByteOrder(p.instanceMethods);
2807 sys::swapByteOrder(p.classMethods);
2808 sys::swapByteOrder(p.optionalInstanceMethods);
2809 sys::swapByteOrder(p.optionalClassMethods);
2810 sys::swapByteOrder(p.instanceProperties);
2811}
2812
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002813inline void swapStruct(struct protocol32_t &p) {
2814 sys::swapByteOrder(p.isa);
2815 sys::swapByteOrder(p.name);
2816 sys::swapByteOrder(p.protocols);
2817 sys::swapByteOrder(p.instanceMethods);
2818 sys::swapByteOrder(p.classMethods);
2819 sys::swapByteOrder(p.optionalInstanceMethods);
2820 sys::swapByteOrder(p.optionalClassMethods);
2821 sys::swapByteOrder(p.instanceProperties);
2822}
2823
Kevin Enderby0fc11822015-04-01 20:57:01 +00002824inline void swapStruct(struct ivar_list64_t &il) {
2825 sys::swapByteOrder(il.entsize);
2826 sys::swapByteOrder(il.count);
2827}
2828
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002829inline void swapStruct(struct ivar_list32_t &il) {
2830 sys::swapByteOrder(il.entsize);
2831 sys::swapByteOrder(il.count);
2832}
2833
Kevin Enderby0fc11822015-04-01 20:57:01 +00002834inline void swapStruct(struct ivar64_t &i) {
2835 sys::swapByteOrder(i.offset);
2836 sys::swapByteOrder(i.name);
2837 sys::swapByteOrder(i.type);
2838 sys::swapByteOrder(i.alignment);
2839 sys::swapByteOrder(i.size);
2840}
2841
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002842inline void swapStruct(struct ivar32_t &i) {
2843 sys::swapByteOrder(i.offset);
2844 sys::swapByteOrder(i.name);
2845 sys::swapByteOrder(i.type);
2846 sys::swapByteOrder(i.alignment);
2847 sys::swapByteOrder(i.size);
2848}
2849
Kevin Enderby0fc11822015-04-01 20:57:01 +00002850inline void swapStruct(struct objc_property_list64 &pl) {
2851 sys::swapByteOrder(pl.entsize);
2852 sys::swapByteOrder(pl.count);
2853}
2854
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002855inline void swapStruct(struct objc_property_list32 &pl) {
2856 sys::swapByteOrder(pl.entsize);
2857 sys::swapByteOrder(pl.count);
2858}
2859
Kevin Enderby0fc11822015-04-01 20:57:01 +00002860inline void swapStruct(struct objc_property64 &op) {
2861 sys::swapByteOrder(op.name);
2862 sys::swapByteOrder(op.attributes);
2863}
2864
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002865inline void swapStruct(struct objc_property32 &op) {
2866 sys::swapByteOrder(op.name);
2867 sys::swapByteOrder(op.attributes);
2868}
2869
Kevin Enderby0fc11822015-04-01 20:57:01 +00002870inline void swapStruct(struct category64_t &c) {
2871 sys::swapByteOrder(c.name);
2872 sys::swapByteOrder(c.cls);
2873 sys::swapByteOrder(c.instanceMethods);
2874 sys::swapByteOrder(c.classMethods);
2875 sys::swapByteOrder(c.protocols);
2876 sys::swapByteOrder(c.instanceProperties);
2877}
2878
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002879inline void swapStruct(struct category32_t &c) {
2880 sys::swapByteOrder(c.name);
2881 sys::swapByteOrder(c.cls);
2882 sys::swapByteOrder(c.instanceMethods);
2883 sys::swapByteOrder(c.classMethods);
2884 sys::swapByteOrder(c.protocols);
2885 sys::swapByteOrder(c.instanceProperties);
2886}
2887
Kevin Enderby0fc11822015-04-01 20:57:01 +00002888inline void swapStruct(struct objc_image_info64 &o) {
2889 sys::swapByteOrder(o.version);
2890 sys::swapByteOrder(o.flags);
2891}
2892
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002893inline void swapStruct(struct objc_image_info32 &o) {
2894 sys::swapByteOrder(o.version);
2895 sys::swapByteOrder(o.flags);
2896}
2897
Kevin Enderby0fc11822015-04-01 20:57:01 +00002898inline void swapStruct(struct message_ref64 &mr) {
2899 sys::swapByteOrder(mr.imp);
2900 sys::swapByteOrder(mr.sel);
2901}
2902
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002903inline void swapStruct(struct message_ref32 &mr) {
2904 sys::swapByteOrder(mr.imp);
2905 sys::swapByteOrder(mr.sel);
2906}
2907
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002908static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
2909 struct DisassembleInfo *info);
2910
2911// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
2912// to an Objective-C class and returns the class name. It is also passed the
2913// address of the pointer, so when the pointer is zero as it can be in an .o
2914// file, that is used to look for an external relocation entry with a symbol
2915// name.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002916static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
2917 uint64_t ReferenceValue,
2918 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002919 const char *r;
2920 uint32_t offset, left;
2921 SectionRef S;
2922
2923 // The pointer_value can be 0 in an object file and have a relocation
2924 // entry for the class symbol at the ReferenceValue (the address of the
2925 // pointer).
2926 if (pointer_value == 0) {
2927 r = get_pointer_64(ReferenceValue, offset, left, S, info);
2928 if (r == nullptr || left < sizeof(uint64_t))
2929 return nullptr;
2930 uint64_t n_value;
2931 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
2932 if (symbol_name == nullptr)
2933 return nullptr;
Hans Wennborgdb53e302014-10-23 21:59:17 +00002934 const char *class_name = strrchr(symbol_name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002935 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
2936 return class_name + 2;
2937 else
2938 return nullptr;
2939 }
2940
2941 // The case were the pointer_value is non-zero and points to a class defined
2942 // in this Mach-O file.
2943 r = get_pointer_64(pointer_value, offset, left, S, info);
2944 if (r == nullptr || left < sizeof(struct class64_t))
2945 return nullptr;
2946 struct class64_t c;
2947 memcpy(&c, r, sizeof(struct class64_t));
2948 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2949 swapStruct(c);
2950 if (c.data == 0)
2951 return nullptr;
2952 r = get_pointer_64(c.data, offset, left, S, info);
2953 if (r == nullptr || left < sizeof(struct class_ro64_t))
2954 return nullptr;
2955 struct class_ro64_t cro;
2956 memcpy(&cro, r, sizeof(struct class_ro64_t));
2957 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2958 swapStruct(cro);
2959 if (cro.name == 0)
2960 return nullptr;
2961 const char *name = get_pointer_64(cro.name, offset, left, S, info);
2962 return name;
2963}
2964
2965// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
2966// pointer to a cfstring and returns its name or nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002967static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
2968 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002969 const char *r, *name;
2970 uint32_t offset, left;
2971 SectionRef S;
2972 struct cfstring64_t cfs;
2973 uint64_t cfs_characters;
2974
2975 r = get_pointer_64(ReferenceValue, offset, left, S, info);
2976 if (r == nullptr || left < sizeof(struct cfstring64_t))
2977 return nullptr;
2978 memcpy(&cfs, r, sizeof(struct cfstring64_t));
2979 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2980 swapStruct(cfs);
2981 if (cfs.characters == 0) {
2982 uint64_t n_value;
2983 const char *symbol_name = get_symbol_64(
2984 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
2985 if (symbol_name == nullptr)
2986 return nullptr;
2987 cfs_characters = n_value;
2988 } else
2989 cfs_characters = cfs.characters;
2990 name = get_pointer_64(cfs_characters, offset, left, S, info);
2991
2992 return name;
2993}
2994
2995// get_objc2_64bit_selref() is used for disassembly and is passed a the address
2996// of a pointer to an Objective-C selector reference when the pointer value is
2997// zero as in a .o file and is likely to have a external relocation entry with
2998// who's symbol's n_value is the real pointer to the selector name. If that is
2999// the case the real pointer to the selector name is returned else 0 is
3000// returned
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003001static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3002 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003003 uint32_t offset, left;
3004 SectionRef S;
3005
3006 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3007 if (r == nullptr || left < sizeof(uint64_t))
3008 return 0;
3009 uint64_t n_value;
3010 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3011 if (symbol_name == nullptr)
3012 return 0;
3013 return n_value;
3014}
3015
Kevin Enderby0fc11822015-04-01 20:57:01 +00003016static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3017 const char *sectname) {
3018 for (const SectionRef &Section : O->sections()) {
3019 StringRef SectName;
3020 Section.getName(SectName);
3021 DataRefImpl Ref = Section.getRawDataRefImpl();
3022 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3023 if (SegName == segname && SectName == sectname)
3024 return Section;
3025 }
3026 return SectionRef();
3027}
3028
3029static void
3030walk_pointer_list_64(const char *listname, const SectionRef S,
3031 MachOObjectFile *O, struct DisassembleInfo *info,
3032 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3033 if (S == SectionRef())
3034 return;
3035
3036 StringRef SectName;
3037 S.getName(SectName);
3038 DataRefImpl Ref = S.getRawDataRefImpl();
3039 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3040 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3041
3042 StringRef BytesStr;
3043 S.getContents(BytesStr);
3044 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3045
3046 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3047 uint32_t left = S.getSize() - i;
3048 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3049 uint64_t p = 0;
3050 memcpy(&p, Contents + i, size);
3051 if (i + sizeof(uint64_t) > S.getSize())
3052 outs() << listname << " list pointer extends past end of (" << SegName
3053 << "," << SectName << ") section\n";
3054 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3055
3056 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3057 sys::swapByteOrder(p);
3058
3059 uint64_t n_value = 0;
3060 const char *name = get_symbol_64(i, S, info, n_value, p);
3061 if (name == nullptr)
3062 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3063
3064 if (n_value != 0) {
3065 outs() << format("0x%" PRIx64, n_value);
3066 if (p != 0)
3067 outs() << " + " << format("0x%" PRIx64, p);
3068 } else
3069 outs() << format("0x%" PRIx64, p);
3070 if (name != nullptr)
3071 outs() << " " << name;
3072 outs() << "\n";
3073
3074 p += n_value;
3075 if (func)
3076 func(p, info);
3077 }
3078}
3079
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003080static void
3081walk_pointer_list_32(const char *listname, const SectionRef S,
3082 MachOObjectFile *O, struct DisassembleInfo *info,
3083 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3084 if (S == SectionRef())
3085 return;
3086
3087 StringRef SectName;
3088 S.getName(SectName);
3089 DataRefImpl Ref = S.getRawDataRefImpl();
3090 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3091 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3092
3093 StringRef BytesStr;
3094 S.getContents(BytesStr);
3095 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3096
3097 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3098 uint32_t left = S.getSize() - i;
3099 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3100 uint32_t p = 0;
3101 memcpy(&p, Contents + i, size);
3102 if (i + sizeof(uint32_t) > S.getSize())
3103 outs() << listname << " list pointer extends past end of (" << SegName
3104 << "," << SectName << ") section\n";
Kevin Enderbycf261312015-04-06 22:33:43 +00003105 uint32_t Address = S.getAddress() + i;
3106 outs() << format("%08" PRIx32, Address) << " ";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003107
3108 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3109 sys::swapByteOrder(p);
3110 outs() << format("0x%" PRIx32, p);
3111
3112 const char *name = get_symbol_32(i, S, info, p);
3113 if (name != nullptr)
3114 outs() << " " << name;
3115 outs() << "\n";
3116
3117 if (func)
3118 func(p, info);
3119 }
3120}
3121
3122static void print_layout_map(const char *layout_map, uint32_t left) {
3123 outs() << " layout map: ";
3124 do {
3125 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3126 left--;
3127 layout_map++;
3128 } while (*layout_map != '\0' && left != 0);
3129 outs() << "\n";
3130}
3131
Kevin Enderby0fc11822015-04-01 20:57:01 +00003132static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3133 uint32_t offset, left;
3134 SectionRef S;
3135 const char *layout_map;
3136
3137 if (p == 0)
3138 return;
3139 layout_map = get_pointer_64(p, offset, left, S, info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003140 print_layout_map(layout_map, left);
3141}
3142
3143static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3144 uint32_t offset, left;
3145 SectionRef S;
3146 const char *layout_map;
3147
3148 if (p == 0)
3149 return;
3150 layout_map = get_pointer_32(p, offset, left, S, info);
3151 print_layout_map(layout_map, left);
Kevin Enderby0fc11822015-04-01 20:57:01 +00003152}
3153
3154static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3155 const char *indent) {
3156 struct method_list64_t ml;
3157 struct method64_t m;
3158 const char *r;
3159 uint32_t offset, xoffset, left, i;
3160 SectionRef S, xS;
3161 const char *name, *sym_name;
3162 uint64_t n_value;
3163
3164 r = get_pointer_64(p, offset, left, S, info);
3165 if (r == nullptr)
3166 return;
3167 memset(&ml, '\0', sizeof(struct method_list64_t));
3168 if (left < sizeof(struct method_list64_t)) {
3169 memcpy(&ml, r, left);
3170 outs() << " (method_list_t entends past the end of the section)\n";
3171 } else
3172 memcpy(&ml, r, sizeof(struct method_list64_t));
3173 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3174 swapStruct(ml);
3175 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3176 outs() << indent << "\t\t count " << ml.count << "\n";
3177
3178 p += sizeof(struct method_list64_t);
3179 offset += sizeof(struct method_list64_t);
3180 for (i = 0; i < ml.count; i++) {
3181 r = get_pointer_64(p, offset, left, S, info);
3182 if (r == nullptr)
3183 return;
3184 memset(&m, '\0', sizeof(struct method64_t));
3185 if (left < sizeof(struct method64_t)) {
3186 memcpy(&ml, r, left);
3187 outs() << indent << " (method_t entends past the end of the section)\n";
3188 } else
3189 memcpy(&m, r, sizeof(struct method64_t));
3190 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3191 swapStruct(m);
3192
3193 outs() << indent << "\t\t name ";
3194 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3195 info, n_value, m.name);
3196 if (n_value != 0) {
3197 if (info->verbose && sym_name != nullptr)
3198 outs() << sym_name;
3199 else
3200 outs() << format("0x%" PRIx64, n_value);
3201 if (m.name != 0)
3202 outs() << " + " << format("0x%" PRIx64, m.name);
3203 } else
3204 outs() << format("0x%" PRIx64, m.name);
3205 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3206 if (name != nullptr)
3207 outs() << format(" %.*s", left, name);
3208 outs() << "\n";
3209
3210 outs() << indent << "\t\t types ";
3211 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3212 info, n_value, m.types);
3213 if (n_value != 0) {
3214 if (info->verbose && sym_name != nullptr)
3215 outs() << sym_name;
3216 else
3217 outs() << format("0x%" PRIx64, n_value);
3218 if (m.types != 0)
3219 outs() << " + " << format("0x%" PRIx64, m.types);
3220 } else
3221 outs() << format("0x%" PRIx64, m.types);
3222 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3223 if (name != nullptr)
3224 outs() << format(" %.*s", left, name);
3225 outs() << "\n";
3226
3227 outs() << indent << "\t\t imp ";
3228 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3229 n_value, m.imp);
3230 if (info->verbose && name == nullptr) {
3231 if (n_value != 0) {
3232 outs() << format("0x%" PRIx64, n_value) << " ";
3233 if (m.imp != 0)
3234 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3235 } else
3236 outs() << format("0x%" PRIx64, m.imp) << " ";
3237 }
3238 if (name != nullptr)
3239 outs() << name;
3240 outs() << "\n";
3241
3242 p += sizeof(struct method64_t);
3243 offset += sizeof(struct method64_t);
3244 }
3245}
3246
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003247static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3248 const char *indent) {
3249 struct method_list32_t ml;
3250 struct method32_t m;
3251 const char *r;
3252 uint32_t offset, xoffset, left, i;
3253 SectionRef S, xS;
3254 const char *name;
3255
3256 r = get_pointer_32(p, offset, left, S, info);
3257 if (r == nullptr)
3258 return;
3259 memset(&ml, '\0', sizeof(struct method_list32_t));
3260 if (left < sizeof(struct method_list32_t)) {
3261 memcpy(&ml, r, left);
3262 outs() << " (method_list_t entends past the end of the section)\n";
3263 } else
3264 memcpy(&ml, r, sizeof(struct method_list32_t));
3265 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3266 swapStruct(ml);
3267 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3268 outs() << indent << "\t\t count " << ml.count << "\n";
3269
3270 p += sizeof(struct method_list32_t);
3271 offset += sizeof(struct method_list32_t);
3272 for (i = 0; i < ml.count; i++) {
3273 r = get_pointer_32(p, offset, left, S, info);
3274 if (r == nullptr)
3275 return;
3276 memset(&m, '\0', sizeof(struct method32_t));
3277 if (left < sizeof(struct method32_t)) {
3278 memcpy(&ml, r, left);
3279 outs() << indent << " (method_t entends past the end of the section)\n";
3280 } else
3281 memcpy(&m, r, sizeof(struct method32_t));
3282 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3283 swapStruct(m);
3284
3285 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3286 name = get_pointer_32(m.name, xoffset, left, xS, info);
3287 if (name != nullptr)
3288 outs() << format(" %.*s", left, name);
3289 outs() << "\n";
3290
3291 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3292 name = get_pointer_32(m.types, xoffset, left, xS, info);
3293 if (name != nullptr)
3294 outs() << format(" %.*s", left, name);
3295 outs() << "\n";
3296
3297 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3298 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3299 m.imp);
3300 if (name != nullptr)
3301 outs() << " " << name;
3302 outs() << "\n";
3303
3304 p += sizeof(struct method32_t);
3305 offset += sizeof(struct method32_t);
3306 }
3307}
3308
Kevin Enderby0fc11822015-04-01 20:57:01 +00003309static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3310 struct protocol_list64_t pl;
3311 uint64_t q, n_value;
3312 struct protocol64_t pc;
3313 const char *r;
3314 uint32_t offset, xoffset, left, i;
3315 SectionRef S, xS;
3316 const char *name, *sym_name;
3317
3318 r = get_pointer_64(p, offset, left, S, info);
3319 if (r == nullptr)
3320 return;
3321 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3322 if (left < sizeof(struct protocol_list64_t)) {
3323 memcpy(&pl, r, left);
3324 outs() << " (protocol_list_t entends past the end of the section)\n";
3325 } else
3326 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3327 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3328 swapStruct(pl);
3329 outs() << " count " << pl.count << "\n";
3330
3331 p += sizeof(struct protocol_list64_t);
3332 offset += sizeof(struct protocol_list64_t);
3333 for (i = 0; i < pl.count; i++) {
3334 r = get_pointer_64(p, offset, left, S, info);
3335 if (r == nullptr)
3336 return;
3337 q = 0;
3338 if (left < sizeof(uint64_t)) {
3339 memcpy(&q, r, left);
3340 outs() << " (protocol_t * entends past the end of the section)\n";
3341 } else
3342 memcpy(&q, r, sizeof(uint64_t));
3343 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3344 sys::swapByteOrder(q);
3345
3346 outs() << "\t\t list[" << i << "] ";
3347 sym_name = get_symbol_64(offset, S, info, n_value, q);
3348 if (n_value != 0) {
3349 if (info->verbose && sym_name != nullptr)
3350 outs() << sym_name;
3351 else
3352 outs() << format("0x%" PRIx64, n_value);
3353 if (q != 0)
3354 outs() << " + " << format("0x%" PRIx64, q);
3355 } else
3356 outs() << format("0x%" PRIx64, q);
3357 outs() << " (struct protocol_t *)\n";
3358
3359 r = get_pointer_64(q + n_value, offset, left, S, info);
3360 if (r == nullptr)
3361 return;
3362 memset(&pc, '\0', sizeof(struct protocol64_t));
3363 if (left < sizeof(struct protocol64_t)) {
3364 memcpy(&pc, r, left);
3365 outs() << " (protocol_t entends past the end of the section)\n";
3366 } else
3367 memcpy(&pc, r, sizeof(struct protocol64_t));
3368 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3369 swapStruct(pc);
3370
3371 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3372
3373 outs() << "\t\t\t name ";
3374 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3375 info, n_value, pc.name);
3376 if (n_value != 0) {
3377 if (info->verbose && sym_name != nullptr)
3378 outs() << sym_name;
3379 else
3380 outs() << format("0x%" PRIx64, n_value);
3381 if (pc.name != 0)
3382 outs() << " + " << format("0x%" PRIx64, pc.name);
3383 } else
3384 outs() << format("0x%" PRIx64, pc.name);
3385 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3386 if (name != nullptr)
3387 outs() << format(" %.*s", left, name);
3388 outs() << "\n";
3389
3390 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3391
3392 outs() << "\t\t instanceMethods ";
3393 sym_name =
3394 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3395 S, info, n_value, pc.instanceMethods);
3396 if (n_value != 0) {
3397 if (info->verbose && sym_name != nullptr)
3398 outs() << sym_name;
3399 else
3400 outs() << format("0x%" PRIx64, n_value);
3401 if (pc.instanceMethods != 0)
3402 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3403 } else
3404 outs() << format("0x%" PRIx64, pc.instanceMethods);
3405 outs() << " (struct method_list_t *)\n";
3406 if (pc.instanceMethods + n_value != 0)
3407 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3408
3409 outs() << "\t\t classMethods ";
3410 sym_name =
3411 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3412 info, n_value, pc.classMethods);
3413 if (n_value != 0) {
3414 if (info->verbose && sym_name != nullptr)
3415 outs() << sym_name;
3416 else
3417 outs() << format("0x%" PRIx64, n_value);
3418 if (pc.classMethods != 0)
3419 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3420 } else
3421 outs() << format("0x%" PRIx64, pc.classMethods);
3422 outs() << " (struct method_list_t *)\n";
3423 if (pc.classMethods + n_value != 0)
3424 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3425
3426 outs() << "\t optionalInstanceMethods "
3427 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3428 outs() << "\t optionalClassMethods "
3429 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3430 outs() << "\t instanceProperties "
3431 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3432
3433 p += sizeof(uint64_t);
3434 offset += sizeof(uint64_t);
3435 }
3436}
3437
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003438static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3439 struct protocol_list32_t pl;
3440 uint32_t q;
3441 struct protocol32_t pc;
3442 const char *r;
3443 uint32_t offset, xoffset, left, i;
3444 SectionRef S, xS;
3445 const char *name;
3446
3447 r = get_pointer_32(p, offset, left, S, info);
3448 if (r == nullptr)
3449 return;
3450 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3451 if (left < sizeof(struct protocol_list32_t)) {
3452 memcpy(&pl, r, left);
3453 outs() << " (protocol_list_t entends past the end of the section)\n";
3454 } else
3455 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3456 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3457 swapStruct(pl);
3458 outs() << " count " << pl.count << "\n";
3459
3460 p += sizeof(struct protocol_list32_t);
3461 offset += sizeof(struct protocol_list32_t);
3462 for (i = 0; i < pl.count; i++) {
3463 r = get_pointer_32(p, offset, left, S, info);
3464 if (r == nullptr)
3465 return;
3466 q = 0;
3467 if (left < sizeof(uint32_t)) {
3468 memcpy(&q, r, left);
3469 outs() << " (protocol_t * entends past the end of the section)\n";
3470 } else
3471 memcpy(&q, r, sizeof(uint32_t));
3472 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3473 sys::swapByteOrder(q);
3474 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3475 << " (struct protocol_t *)\n";
3476 r = get_pointer_32(q, offset, left, S, info);
3477 if (r == nullptr)
3478 return;
3479 memset(&pc, '\0', sizeof(struct protocol32_t));
3480 if (left < sizeof(struct protocol32_t)) {
3481 memcpy(&pc, r, left);
3482 outs() << " (protocol_t entends past the end of the section)\n";
3483 } else
3484 memcpy(&pc, r, sizeof(struct protocol32_t));
3485 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3486 swapStruct(pc);
3487 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3488 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3489 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3490 if (name != nullptr)
3491 outs() << format(" %.*s", left, name);
3492 outs() << "\n";
3493 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3494 outs() << "\t\t instanceMethods "
3495 << format("0x%" PRIx32, pc.instanceMethods)
3496 << " (struct method_list_t *)\n";
3497 if (pc.instanceMethods != 0)
3498 print_method_list32_t(pc.instanceMethods, info, "\t");
3499 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3500 << " (struct method_list_t *)\n";
3501 if (pc.classMethods != 0)
3502 print_method_list32_t(pc.classMethods, info, "\t");
3503 outs() << "\t optionalInstanceMethods "
3504 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3505 outs() << "\t optionalClassMethods "
3506 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3507 outs() << "\t instanceProperties "
3508 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3509 p += sizeof(uint32_t);
3510 offset += sizeof(uint32_t);
3511 }
3512}
3513
Kevin Enderby0fc11822015-04-01 20:57:01 +00003514static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3515 struct ivar_list64_t il;
3516 struct ivar64_t i;
3517 const char *r;
3518 uint32_t offset, xoffset, left, j;
3519 SectionRef S, xS;
3520 const char *name, *sym_name, *ivar_offset_p;
3521 uint64_t ivar_offset, n_value;
3522
3523 r = get_pointer_64(p, offset, left, S, info);
3524 if (r == nullptr)
3525 return;
3526 memset(&il, '\0', sizeof(struct ivar_list64_t));
3527 if (left < sizeof(struct ivar_list64_t)) {
3528 memcpy(&il, r, left);
3529 outs() << " (ivar_list_t entends past the end of the section)\n";
3530 } else
3531 memcpy(&il, r, sizeof(struct ivar_list64_t));
3532 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3533 swapStruct(il);
3534 outs() << " entsize " << il.entsize << "\n";
3535 outs() << " count " << il.count << "\n";
3536
3537 p += sizeof(struct ivar_list64_t);
3538 offset += sizeof(struct ivar_list64_t);
3539 for (j = 0; j < il.count; j++) {
3540 r = get_pointer_64(p, offset, left, S, info);
3541 if (r == nullptr)
3542 return;
3543 memset(&i, '\0', sizeof(struct ivar64_t));
3544 if (left < sizeof(struct ivar64_t)) {
3545 memcpy(&i, r, left);
3546 outs() << " (ivar_t entends past the end of the section)\n";
3547 } else
3548 memcpy(&i, r, sizeof(struct ivar64_t));
3549 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3550 swapStruct(i);
3551
3552 outs() << "\t\t\t offset ";
3553 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
3554 info, n_value, i.offset);
3555 if (n_value != 0) {
3556 if (info->verbose && sym_name != nullptr)
3557 outs() << sym_name;
3558 else
3559 outs() << format("0x%" PRIx64, n_value);
3560 if (i.offset != 0)
3561 outs() << " + " << format("0x%" PRIx64, i.offset);
3562 } else
3563 outs() << format("0x%" PRIx64, i.offset);
3564 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
3565 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
3566 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
3567 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3568 sys::swapByteOrder(ivar_offset);
3569 outs() << " " << ivar_offset << "\n";
3570 } else
3571 outs() << "\n";
3572
3573 outs() << "\t\t\t name ";
3574 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
3575 n_value, i.name);
3576 if (n_value != 0) {
3577 if (info->verbose && sym_name != nullptr)
3578 outs() << sym_name;
3579 else
3580 outs() << format("0x%" PRIx64, n_value);
3581 if (i.name != 0)
3582 outs() << " + " << format("0x%" PRIx64, i.name);
3583 } else
3584 outs() << format("0x%" PRIx64, i.name);
3585 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
3586 if (name != nullptr)
3587 outs() << format(" %.*s", left, name);
3588 outs() << "\n";
3589
3590 outs() << "\t\t\t type ";
3591 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
3592 n_value, i.name);
3593 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
3594 if (n_value != 0) {
3595 if (info->verbose && sym_name != nullptr)
3596 outs() << sym_name;
3597 else
3598 outs() << format("0x%" PRIx64, n_value);
3599 if (i.type != 0)
3600 outs() << " + " << format("0x%" PRIx64, i.type);
3601 } else
3602 outs() << format("0x%" PRIx64, i.type);
3603 if (name != nullptr)
3604 outs() << format(" %.*s", left, name);
3605 outs() << "\n";
3606
3607 outs() << "\t\t\talignment " << i.alignment << "\n";
3608 outs() << "\t\t\t size " << i.size << "\n";
3609
3610 p += sizeof(struct ivar64_t);
3611 offset += sizeof(struct ivar64_t);
3612 }
3613}
3614
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003615static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
3616 struct ivar_list32_t il;
3617 struct ivar32_t i;
3618 const char *r;
3619 uint32_t offset, xoffset, left, j;
3620 SectionRef S, xS;
3621 const char *name, *ivar_offset_p;
3622 uint32_t ivar_offset;
3623
3624 r = get_pointer_32(p, offset, left, S, info);
3625 if (r == nullptr)
3626 return;
3627 memset(&il, '\0', sizeof(struct ivar_list32_t));
3628 if (left < sizeof(struct ivar_list32_t)) {
3629 memcpy(&il, r, left);
3630 outs() << " (ivar_list_t entends past the end of the section)\n";
3631 } else
3632 memcpy(&il, r, sizeof(struct ivar_list32_t));
3633 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3634 swapStruct(il);
3635 outs() << " entsize " << il.entsize << "\n";
3636 outs() << " count " << il.count << "\n";
3637
3638 p += sizeof(struct ivar_list32_t);
3639 offset += sizeof(struct ivar_list32_t);
3640 for (j = 0; j < il.count; j++) {
3641 r = get_pointer_32(p, offset, left, S, info);
3642 if (r == nullptr)
3643 return;
3644 memset(&i, '\0', sizeof(struct ivar32_t));
3645 if (left < sizeof(struct ivar32_t)) {
3646 memcpy(&i, r, left);
3647 outs() << " (ivar_t entends past the end of the section)\n";
3648 } else
3649 memcpy(&i, r, sizeof(struct ivar32_t));
3650 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3651 swapStruct(i);
3652
3653 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
3654 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
3655 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
3656 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
3657 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3658 sys::swapByteOrder(ivar_offset);
3659 outs() << " " << ivar_offset << "\n";
3660 } else
3661 outs() << "\n";
3662
3663 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
3664 name = get_pointer_32(i.name, xoffset, left, xS, info);
3665 if (name != nullptr)
3666 outs() << format(" %.*s", left, name);
3667 outs() << "\n";
3668
3669 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
3670 name = get_pointer_32(i.type, xoffset, left, xS, info);
3671 if (name != nullptr)
3672 outs() << format(" %.*s", left, name);
3673 outs() << "\n";
3674
3675 outs() << "\t\t\talignment " << i.alignment << "\n";
3676 outs() << "\t\t\t size " << i.size << "\n";
3677
3678 p += sizeof(struct ivar32_t);
3679 offset += sizeof(struct ivar32_t);
3680 }
3681}
3682
Kevin Enderby0fc11822015-04-01 20:57:01 +00003683static void print_objc_property_list64(uint64_t p,
3684 struct DisassembleInfo *info) {
3685 struct objc_property_list64 opl;
3686 struct objc_property64 op;
3687 const char *r;
3688 uint32_t offset, xoffset, left, j;
3689 SectionRef S, xS;
3690 const char *name, *sym_name;
3691 uint64_t n_value;
3692
3693 r = get_pointer_64(p, offset, left, S, info);
3694 if (r == nullptr)
3695 return;
3696 memset(&opl, '\0', sizeof(struct objc_property_list64));
3697 if (left < sizeof(struct objc_property_list64)) {
3698 memcpy(&opl, r, left);
3699 outs() << " (objc_property_list entends past the end of the section)\n";
3700 } else
3701 memcpy(&opl, r, sizeof(struct objc_property_list64));
3702 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3703 swapStruct(opl);
3704 outs() << " entsize " << opl.entsize << "\n";
3705 outs() << " count " << opl.count << "\n";
3706
3707 p += sizeof(struct objc_property_list64);
3708 offset += sizeof(struct objc_property_list64);
3709 for (j = 0; j < opl.count; j++) {
3710 r = get_pointer_64(p, offset, left, S, info);
3711 if (r == nullptr)
3712 return;
3713 memset(&op, '\0', sizeof(struct objc_property64));
3714 if (left < sizeof(struct objc_property64)) {
3715 memcpy(&op, r, left);
3716 outs() << " (objc_property entends past the end of the section)\n";
3717 } else
3718 memcpy(&op, r, sizeof(struct objc_property64));
3719 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3720 swapStruct(op);
3721
3722 outs() << "\t\t\t name ";
3723 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
3724 info, n_value, op.name);
3725 if (n_value != 0) {
3726 if (info->verbose && sym_name != nullptr)
3727 outs() << sym_name;
3728 else
3729 outs() << format("0x%" PRIx64, n_value);
3730 if (op.name != 0)
3731 outs() << " + " << format("0x%" PRIx64, op.name);
3732 } else
3733 outs() << format("0x%" PRIx64, op.name);
3734 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
3735 if (name != nullptr)
3736 outs() << format(" %.*s", left, name);
3737 outs() << "\n";
3738
3739 outs() << "\t\t\tattributes ";
3740 sym_name =
3741 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
3742 info, n_value, op.attributes);
3743 if (n_value != 0) {
3744 if (info->verbose && sym_name != nullptr)
3745 outs() << sym_name;
3746 else
3747 outs() << format("0x%" PRIx64, n_value);
3748 if (op.attributes != 0)
3749 outs() << " + " << format("0x%" PRIx64, op.attributes);
3750 } else
3751 outs() << format("0x%" PRIx64, op.attributes);
3752 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
3753 if (name != nullptr)
3754 outs() << format(" %.*s", left, name);
3755 outs() << "\n";
3756
3757 p += sizeof(struct objc_property64);
3758 offset += sizeof(struct objc_property64);
3759 }
3760}
3761
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003762static void print_objc_property_list32(uint32_t p,
3763 struct DisassembleInfo *info) {
3764 struct objc_property_list32 opl;
3765 struct objc_property32 op;
3766 const char *r;
3767 uint32_t offset, xoffset, left, j;
3768 SectionRef S, xS;
3769 const char *name;
3770
3771 r = get_pointer_32(p, offset, left, S, info);
3772 if (r == nullptr)
3773 return;
3774 memset(&opl, '\0', sizeof(struct objc_property_list32));
3775 if (left < sizeof(struct objc_property_list32)) {
3776 memcpy(&opl, r, left);
3777 outs() << " (objc_property_list entends past the end of the section)\n";
3778 } else
3779 memcpy(&opl, r, sizeof(struct objc_property_list32));
3780 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3781 swapStruct(opl);
3782 outs() << " entsize " << opl.entsize << "\n";
3783 outs() << " count " << opl.count << "\n";
3784
3785 p += sizeof(struct objc_property_list32);
3786 offset += sizeof(struct objc_property_list32);
3787 for (j = 0; j < opl.count; j++) {
3788 r = get_pointer_32(p, offset, left, S, info);
3789 if (r == nullptr)
3790 return;
3791 memset(&op, '\0', sizeof(struct objc_property32));
3792 if (left < sizeof(struct objc_property32)) {
3793 memcpy(&op, r, left);
3794 outs() << " (objc_property entends past the end of the section)\n";
3795 } else
3796 memcpy(&op, r, sizeof(struct objc_property32));
3797 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3798 swapStruct(op);
3799
3800 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
3801 name = get_pointer_32(op.name, xoffset, left, xS, info);
3802 if (name != nullptr)
3803 outs() << format(" %.*s", left, name);
3804 outs() << "\n";
3805
3806 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
3807 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
3808 if (name != nullptr)
3809 outs() << format(" %.*s", left, name);
3810 outs() << "\n";
3811
3812 p += sizeof(struct objc_property32);
3813 offset += sizeof(struct objc_property32);
3814 }
3815}
3816
Kevin Enderby0fc11822015-04-01 20:57:01 +00003817static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
3818 bool &is_meta_class) {
3819 struct class_ro64_t cro;
3820 const char *r;
3821 uint32_t offset, xoffset, left;
3822 SectionRef S, xS;
3823 const char *name, *sym_name;
3824 uint64_t n_value;
3825
3826 r = get_pointer_64(p, offset, left, S, info);
3827 if (r == nullptr || left < sizeof(struct class_ro64_t))
3828 return;
3829 memset(&cro, '\0', sizeof(struct class_ro64_t));
3830 if (left < sizeof(struct class_ro64_t)) {
3831 memcpy(&cro, r, left);
3832 outs() << " (class_ro_t entends past the end of the section)\n";
3833 } else
3834 memcpy(&cro, r, sizeof(struct class_ro64_t));
3835 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3836 swapStruct(cro);
3837 outs() << " flags " << format("0x%" PRIx32, cro.flags);
3838 if (cro.flags & RO_META)
3839 outs() << " RO_META";
3840 if (cro.flags & RO_ROOT)
3841 outs() << " RO_ROOT";
3842 if (cro.flags & RO_HAS_CXX_STRUCTORS)
3843 outs() << " RO_HAS_CXX_STRUCTORS";
3844 outs() << "\n";
3845 outs() << " instanceStart " << cro.instanceStart << "\n";
3846 outs() << " instanceSize " << cro.instanceSize << "\n";
3847 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
3848 << "\n";
3849 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
3850 << "\n";
3851 print_layout_map64(cro.ivarLayout, info);
3852
3853 outs() << " name ";
3854 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
3855 info, n_value, cro.name);
3856 if (n_value != 0) {
3857 if (info->verbose && sym_name != nullptr)
3858 outs() << sym_name;
3859 else
3860 outs() << format("0x%" PRIx64, n_value);
3861 if (cro.name != 0)
3862 outs() << " + " << format("0x%" PRIx64, cro.name);
3863 } else
3864 outs() << format("0x%" PRIx64, cro.name);
3865 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
3866 if (name != nullptr)
3867 outs() << format(" %.*s", left, name);
3868 outs() << "\n";
3869
3870 outs() << " baseMethods ";
3871 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
3872 S, info, n_value, cro.baseMethods);
3873 if (n_value != 0) {
3874 if (info->verbose && sym_name != nullptr)
3875 outs() << sym_name;
3876 else
3877 outs() << format("0x%" PRIx64, n_value);
3878 if (cro.baseMethods != 0)
3879 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
3880 } else
3881 outs() << format("0x%" PRIx64, cro.baseMethods);
3882 outs() << " (struct method_list_t *)\n";
3883 if (cro.baseMethods + n_value != 0)
3884 print_method_list64_t(cro.baseMethods + n_value, info, "");
3885
3886 outs() << " baseProtocols ";
3887 sym_name =
3888 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
3889 info, n_value, cro.baseProtocols);
3890 if (n_value != 0) {
3891 if (info->verbose && sym_name != nullptr)
3892 outs() << sym_name;
3893 else
3894 outs() << format("0x%" PRIx64, n_value);
3895 if (cro.baseProtocols != 0)
3896 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
3897 } else
3898 outs() << format("0x%" PRIx64, cro.baseProtocols);
3899 outs() << "\n";
3900 if (cro.baseProtocols + n_value != 0)
3901 print_protocol_list64_t(cro.baseProtocols + n_value, info);
3902
3903 outs() << " ivars ";
3904 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003905 info, n_value, cro.ivars);
Kevin Enderby0fc11822015-04-01 20:57:01 +00003906 if (n_value != 0) {
3907 if (info->verbose && sym_name != nullptr)
3908 outs() << sym_name;
3909 else
3910 outs() << format("0x%" PRIx64, n_value);
3911 if (cro.ivars != 0)
3912 outs() << " + " << format("0x%" PRIx64, cro.ivars);
3913 } else
3914 outs() << format("0x%" PRIx64, cro.ivars);
3915 outs() << "\n";
3916 if (cro.ivars + n_value != 0)
3917 print_ivar_list64_t(cro.ivars + n_value, info);
3918
3919 outs() << " weakIvarLayout ";
3920 sym_name =
3921 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
3922 info, n_value, cro.weakIvarLayout);
3923 if (n_value != 0) {
3924 if (info->verbose && sym_name != nullptr)
3925 outs() << sym_name;
3926 else
3927 outs() << format("0x%" PRIx64, n_value);
3928 if (cro.weakIvarLayout != 0)
3929 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
3930 } else
3931 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
3932 outs() << "\n";
3933 print_layout_map64(cro.weakIvarLayout + n_value, info);
3934
3935 outs() << " baseProperties ";
3936 sym_name =
3937 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
3938 info, n_value, cro.baseProperties);
3939 if (n_value != 0) {
3940 if (info->verbose && sym_name != nullptr)
3941 outs() << sym_name;
3942 else
3943 outs() << format("0x%" PRIx64, n_value);
3944 if (cro.baseProperties != 0)
3945 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
3946 } else
3947 outs() << format("0x%" PRIx64, cro.baseProperties);
3948 outs() << "\n";
3949 if (cro.baseProperties + n_value != 0)
3950 print_objc_property_list64(cro.baseProperties + n_value, info);
3951
3952 is_meta_class = (cro.flags & RO_META) ? true : false;
3953}
3954
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003955static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
3956 bool &is_meta_class) {
3957 struct class_ro32_t cro;
3958 const char *r;
3959 uint32_t offset, xoffset, left;
3960 SectionRef S, xS;
3961 const char *name;
3962
3963 r = get_pointer_32(p, offset, left, S, info);
3964 if (r == nullptr)
3965 return;
3966 memset(&cro, '\0', sizeof(struct class_ro32_t));
3967 if (left < sizeof(struct class_ro32_t)) {
3968 memcpy(&cro, r, left);
3969 outs() << " (class_ro_t entends past the end of the section)\n";
3970 } else
3971 memcpy(&cro, r, sizeof(struct class_ro32_t));
3972 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3973 swapStruct(cro);
3974 outs() << " flags " << format("0x%" PRIx32, cro.flags);
3975 if (cro.flags & RO_META)
3976 outs() << " RO_META";
3977 if (cro.flags & RO_ROOT)
3978 outs() << " RO_ROOT";
3979 if (cro.flags & RO_HAS_CXX_STRUCTORS)
3980 outs() << " RO_HAS_CXX_STRUCTORS";
3981 outs() << "\n";
3982 outs() << " instanceStart " << cro.instanceStart << "\n";
3983 outs() << " instanceSize " << cro.instanceSize << "\n";
3984 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
3985 << "\n";
3986 print_layout_map32(cro.ivarLayout, info);
3987
3988 outs() << " name " << format("0x%" PRIx32, cro.name);
3989 name = get_pointer_32(cro.name, xoffset, left, xS, info);
3990 if (name != nullptr)
3991 outs() << format(" %.*s", left, name);
3992 outs() << "\n";
3993
3994 outs() << " baseMethods "
3995 << format("0x%" PRIx32, cro.baseMethods)
3996 << " (struct method_list_t *)\n";
3997 if (cro.baseMethods != 0)
3998 print_method_list32_t(cro.baseMethods, info, "");
3999
4000 outs() << " baseProtocols "
4001 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4002 if (cro.baseProtocols != 0)
4003 print_protocol_list32_t(cro.baseProtocols, info);
4004 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4005 << "\n";
4006 if (cro.ivars != 0)
4007 print_ivar_list32_t(cro.ivars, info);
4008 outs() << " weakIvarLayout "
4009 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4010 print_layout_map32(cro.weakIvarLayout, info);
4011 outs() << " baseProperties "
4012 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4013 if (cro.baseProperties != 0)
4014 print_objc_property_list32(cro.baseProperties, info);
4015 is_meta_class = (cro.flags & RO_META) ? true : false;
4016}
4017
Kevin Enderby0fc11822015-04-01 20:57:01 +00004018static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4019 struct class64_t c;
4020 const char *r;
4021 uint32_t offset, left;
4022 SectionRef S;
4023 const char *name;
4024 uint64_t isa_n_value, n_value;
4025
4026 r = get_pointer_64(p, offset, left, S, info);
4027 if (r == nullptr || left < sizeof(struct class64_t))
4028 return;
4029 memset(&c, '\0', sizeof(struct class64_t));
4030 if (left < sizeof(struct class64_t)) {
4031 memcpy(&c, r, left);
4032 outs() << " (class_t entends past the end of the section)\n";
4033 } else
4034 memcpy(&c, r, sizeof(struct class64_t));
4035 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4036 swapStruct(c);
4037
4038 outs() << " isa " << format("0x%" PRIx64, c.isa);
4039 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4040 isa_n_value, c.isa);
4041 if (name != nullptr)
4042 outs() << " " << name;
4043 outs() << "\n";
4044
4045 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4046 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4047 n_value, c.superclass);
4048 if (name != nullptr)
4049 outs() << " " << name;
4050 outs() << "\n";
4051
4052 outs() << " cache " << format("0x%" PRIx64, c.cache);
4053 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4054 n_value, c.cache);
4055 if (name != nullptr)
4056 outs() << " " << name;
4057 outs() << "\n";
4058
4059 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4060 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4061 n_value, c.vtable);
4062 if (name != nullptr)
4063 outs() << " " << name;
4064 outs() << "\n";
4065
4066 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4067 n_value, c.data);
4068 outs() << " data ";
4069 if (n_value != 0) {
4070 if (info->verbose && name != nullptr)
4071 outs() << name;
4072 else
4073 outs() << format("0x%" PRIx64, n_value);
4074 if (c.data != 0)
4075 outs() << " + " << format("0x%" PRIx64, c.data);
4076 } else
4077 outs() << format("0x%" PRIx64, c.data);
4078 outs() << " (struct class_ro_t *)";
4079
4080 // This is a Swift class if some of the low bits of the pointer are set.
4081 if ((c.data + n_value) & 0x7)
4082 outs() << " Swift class";
4083 outs() << "\n";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004084 bool is_meta_class;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004085 print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class);
4086
4087 if (is_meta_class == false) {
4088 outs() << "Meta Class\n";
4089 print_class64_t(c.isa + isa_n_value, info);
4090 }
4091}
4092
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004093static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4094 struct class32_t c;
4095 const char *r;
4096 uint32_t offset, left;
4097 SectionRef S;
4098 const char *name;
4099
4100 r = get_pointer_32(p, offset, left, S, info);
4101 if (r == nullptr)
4102 return;
4103 memset(&c, '\0', sizeof(struct class32_t));
4104 if (left < sizeof(struct class32_t)) {
4105 memcpy(&c, r, left);
4106 outs() << " (class_t entends past the end of the section)\n";
4107 } else
4108 memcpy(&c, r, sizeof(struct class32_t));
4109 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4110 swapStruct(c);
4111
4112 outs() << " isa " << format("0x%" PRIx32, c.isa);
4113 name =
4114 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4115 if (name != nullptr)
4116 outs() << " " << name;
4117 outs() << "\n";
4118
4119 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4120 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4121 c.superclass);
4122 if (name != nullptr)
4123 outs() << " " << name;
4124 outs() << "\n";
4125
4126 outs() << " cache " << format("0x%" PRIx32, c.cache);
4127 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4128 c.cache);
4129 if (name != nullptr)
4130 outs() << " " << name;
4131 outs() << "\n";
4132
4133 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4134 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4135 c.vtable);
4136 if (name != nullptr)
4137 outs() << " " << name;
4138 outs() << "\n";
4139
4140 name =
4141 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4142 outs() << " data " << format("0x%" PRIx32, c.data)
4143 << " (struct class_ro_t *)";
4144
4145 // This is a Swift class if some of the low bits of the pointer are set.
4146 if (c.data & 0x3)
4147 outs() << " Swift class";
4148 outs() << "\n";
4149 bool is_meta_class;
4150 print_class_ro32_t(c.data & ~0x3, info, is_meta_class);
4151
4152 if (is_meta_class == false) {
4153 outs() << "Meta Class\n";
4154 print_class32_t(c.isa, info);
4155 }
4156}
4157
Kevin Enderby0fc11822015-04-01 20:57:01 +00004158static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4159 struct category64_t c;
4160 const char *r;
4161 uint32_t offset, xoffset, left;
4162 SectionRef S, xS;
4163 const char *name, *sym_name;
4164 uint64_t n_value;
4165
4166 r = get_pointer_64(p, offset, left, S, info);
4167 if (r == nullptr)
4168 return;
4169 memset(&c, '\0', sizeof(struct category64_t));
4170 if (left < sizeof(struct category64_t)) {
4171 memcpy(&c, r, left);
4172 outs() << " (category_t entends past the end of the section)\n";
4173 } else
4174 memcpy(&c, r, sizeof(struct category64_t));
4175 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4176 swapStruct(c);
4177
4178 outs() << " name ";
4179 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4180 info, n_value, c.name);
4181 if (n_value != 0) {
4182 if (info->verbose && sym_name != nullptr)
4183 outs() << sym_name;
4184 else
4185 outs() << format("0x%" PRIx64, n_value);
4186 if (c.name != 0)
4187 outs() << " + " << format("0x%" PRIx64, c.name);
4188 } else
4189 outs() << format("0x%" PRIx64, c.name);
4190 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4191 if (name != nullptr)
4192 outs() << format(" %.*s", left, name);
4193 outs() << "\n";
4194
4195 outs() << " cls ";
4196 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4197 n_value, c.cls);
4198 if (n_value != 0) {
4199 if (info->verbose && sym_name != nullptr)
4200 outs() << sym_name;
4201 else
4202 outs() << format("0x%" PRIx64, n_value);
4203 if (c.cls != 0)
4204 outs() << " + " << format("0x%" PRIx64, c.cls);
4205 } else
4206 outs() << format("0x%" PRIx64, c.cls);
4207 outs() << "\n";
4208 if (c.cls + n_value != 0)
4209 print_class64_t(c.cls + n_value, info);
4210
4211 outs() << " instanceMethods ";
4212 sym_name =
4213 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4214 info, n_value, c.instanceMethods);
4215 if (n_value != 0) {
4216 if (info->verbose && sym_name != nullptr)
4217 outs() << sym_name;
4218 else
4219 outs() << format("0x%" PRIx64, n_value);
4220 if (c.instanceMethods != 0)
4221 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4222 } else
4223 outs() << format("0x%" PRIx64, c.instanceMethods);
4224 outs() << "\n";
4225 if (c.instanceMethods + n_value != 0)
4226 print_method_list64_t(c.instanceMethods + n_value, info, "");
4227
4228 outs() << " classMethods ";
4229 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4230 S, info, n_value, c.classMethods);
4231 if (n_value != 0) {
4232 if (info->verbose && sym_name != nullptr)
4233 outs() << sym_name;
4234 else
4235 outs() << format("0x%" PRIx64, n_value);
4236 if (c.classMethods != 0)
4237 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4238 } else
4239 outs() << format("0x%" PRIx64, c.classMethods);
4240 outs() << "\n";
4241 if (c.classMethods + n_value != 0)
4242 print_method_list64_t(c.classMethods + n_value, info, "");
4243
4244 outs() << " protocols ";
4245 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4246 info, n_value, c.protocols);
4247 if (n_value != 0) {
4248 if (info->verbose && sym_name != nullptr)
4249 outs() << sym_name;
4250 else
4251 outs() << format("0x%" PRIx64, n_value);
4252 if (c.protocols != 0)
4253 outs() << " + " << format("0x%" PRIx64, c.protocols);
4254 } else
4255 outs() << format("0x%" PRIx64, c.protocols);
4256 outs() << "\n";
4257 if (c.protocols + n_value != 0)
4258 print_protocol_list64_t(c.protocols + n_value, info);
4259
4260 outs() << "instanceProperties ";
4261 sym_name =
4262 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4263 S, info, n_value, c.instanceProperties);
4264 if (n_value != 0) {
4265 if (info->verbose && sym_name != nullptr)
4266 outs() << sym_name;
4267 else
4268 outs() << format("0x%" PRIx64, n_value);
4269 if (c.instanceProperties != 0)
4270 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4271 } else
4272 outs() << format("0x%" PRIx64, c.instanceProperties);
4273 outs() << "\n";
4274 if (c.instanceProperties + n_value != 0)
4275 print_objc_property_list64(c.instanceProperties + n_value, info);
4276}
4277
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004278static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4279 struct category32_t c;
4280 const char *r;
4281 uint32_t offset, left;
4282 SectionRef S, xS;
4283 const char *name;
4284
4285 r = get_pointer_32(p, offset, left, S, info);
4286 if (r == nullptr)
4287 return;
4288 memset(&c, '\0', sizeof(struct category32_t));
4289 if (left < sizeof(struct category32_t)) {
4290 memcpy(&c, r, left);
4291 outs() << " (category_t entends past the end of the section)\n";
4292 } else
4293 memcpy(&c, r, sizeof(struct category32_t));
4294 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4295 swapStruct(c);
4296
4297 outs() << " name " << format("0x%" PRIx32, c.name);
4298 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4299 c.name);
4300 if (name != NULL)
4301 outs() << " " << name;
4302 outs() << "\n";
4303
4304 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
4305 if (c.cls != 0)
4306 print_class32_t(c.cls, info);
4307 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4308 << "\n";
4309 if (c.instanceMethods != 0)
4310 print_method_list32_t(c.instanceMethods, info, "");
4311 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
4312 << "\n";
4313 if (c.classMethods != 0)
4314 print_method_list32_t(c.classMethods, info, "");
4315 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
4316 if (c.protocols != 0)
4317 print_protocol_list32_t(c.protocols, info);
4318 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
4319 << "\n";
4320 if (c.instanceProperties != 0)
4321 print_objc_property_list32(c.instanceProperties, info);
4322}
4323
Kevin Enderby0fc11822015-04-01 20:57:01 +00004324static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
4325 uint32_t i, left, offset, xoffset;
4326 uint64_t p, n_value;
4327 struct message_ref64 mr;
4328 const char *name, *sym_name;
4329 const char *r;
4330 SectionRef xS;
4331
4332 if (S == SectionRef())
4333 return;
4334
4335 StringRef SectName;
4336 S.getName(SectName);
4337 DataRefImpl Ref = S.getRawDataRefImpl();
4338 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4339 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4340 offset = 0;
4341 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4342 p = S.getAddress() + i;
4343 r = get_pointer_64(p, offset, left, S, info);
4344 if (r == nullptr)
4345 return;
4346 memset(&mr, '\0', sizeof(struct message_ref64));
4347 if (left < sizeof(struct message_ref64)) {
4348 memcpy(&mr, r, left);
4349 outs() << " (message_ref entends past the end of the section)\n";
4350 } else
4351 memcpy(&mr, r, sizeof(struct message_ref64));
4352 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4353 swapStruct(mr);
4354
4355 outs() << " imp ";
4356 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
4357 n_value, mr.imp);
4358 if (n_value != 0) {
4359 outs() << format("0x%" PRIx64, n_value) << " ";
4360 if (mr.imp != 0)
4361 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
4362 } else
4363 outs() << format("0x%" PRIx64, mr.imp) << " ";
4364 if (name != nullptr)
4365 outs() << " " << name;
4366 outs() << "\n";
4367
4368 outs() << " sel ";
4369 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
4370 info, n_value, mr.sel);
4371 if (n_value != 0) {
4372 if (info->verbose && sym_name != nullptr)
4373 outs() << sym_name;
4374 else
4375 outs() << format("0x%" PRIx64, n_value);
4376 if (mr.sel != 0)
4377 outs() << " + " << format("0x%" PRIx64, mr.sel);
4378 } else
4379 outs() << format("0x%" PRIx64, mr.sel);
4380 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
4381 if (name != nullptr)
4382 outs() << format(" %.*s", left, name);
4383 outs() << "\n";
4384
4385 offset += sizeof(struct message_ref64);
4386 }
4387}
4388
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004389static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
4390 uint32_t i, left, offset, xoffset, p;
4391 struct message_ref32 mr;
4392 const char *name, *r;
4393 SectionRef xS;
4394
4395 if (S == SectionRef())
4396 return;
4397
4398 StringRef SectName;
4399 S.getName(SectName);
4400 DataRefImpl Ref = S.getRawDataRefImpl();
4401 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4402 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4403 offset = 0;
4404 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4405 p = S.getAddress() + i;
4406 r = get_pointer_32(p, offset, left, S, info);
4407 if (r == nullptr)
4408 return;
4409 memset(&mr, '\0', sizeof(struct message_ref32));
4410 if (left < sizeof(struct message_ref32)) {
4411 memcpy(&mr, r, left);
4412 outs() << " (message_ref entends past the end of the section)\n";
4413 } else
4414 memcpy(&mr, r, sizeof(struct message_ref32));
4415 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4416 swapStruct(mr);
4417
4418 outs() << " imp " << format("0x%" PRIx32, mr.imp);
4419 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
4420 mr.imp);
4421 if (name != nullptr)
4422 outs() << " " << name;
4423 outs() << "\n";
4424
4425 outs() << " sel " << format("0x%" PRIx32, mr.sel);
4426 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
4427 if (name != nullptr)
4428 outs() << " " << name;
4429 outs() << "\n";
4430
4431 offset += sizeof(struct message_ref32);
4432 }
4433}
4434
Kevin Enderby0fc11822015-04-01 20:57:01 +00004435static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
4436 uint32_t left, offset, swift_version;
4437 uint64_t p;
4438 struct objc_image_info64 o;
4439 const char *r;
4440
4441 StringRef SectName;
4442 S.getName(SectName);
4443 DataRefImpl Ref = S.getRawDataRefImpl();
4444 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4445 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4446 p = S.getAddress();
4447 r = get_pointer_64(p, offset, left, S, info);
4448 if (r == nullptr)
4449 return;
4450 memset(&o, '\0', sizeof(struct objc_image_info64));
4451 if (left < sizeof(struct objc_image_info64)) {
4452 memcpy(&o, r, left);
4453 outs() << " (objc_image_info entends past the end of the section)\n";
4454 } else
4455 memcpy(&o, r, sizeof(struct objc_image_info64));
4456 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4457 swapStruct(o);
4458 outs() << " version " << o.version << "\n";
4459 outs() << " flags " << format("0x%" PRIx32, o.flags);
4460 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
4461 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
4462 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
4463 outs() << " OBJC_IMAGE_SUPPORTS_GC";
4464 swift_version = (o.flags >> 8) & 0xff;
4465 if (swift_version != 0) {
4466 if (swift_version == 1)
4467 outs() << " Swift 1.0";
4468 else if (swift_version == 2)
4469 outs() << " Swift 1.1";
4470 else
4471 outs() << " unknown future Swift version (" << swift_version << ")";
4472 }
4473 outs() << "\n";
4474}
4475
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004476static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
4477 uint32_t left, offset, swift_version, p;
4478 struct objc_image_info32 o;
4479 const char *r;
4480
4481 StringRef SectName;
4482 S.getName(SectName);
4483 DataRefImpl Ref = S.getRawDataRefImpl();
4484 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4485 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4486 p = S.getAddress();
4487 r = get_pointer_32(p, offset, left, S, info);
4488 if (r == nullptr)
4489 return;
4490 memset(&o, '\0', sizeof(struct objc_image_info32));
4491 if (left < sizeof(struct objc_image_info32)) {
4492 memcpy(&o, r, left);
4493 outs() << " (objc_image_info entends past the end of the section)\n";
4494 } else
4495 memcpy(&o, r, sizeof(struct objc_image_info32));
4496 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4497 swapStruct(o);
4498 outs() << " version " << o.version << "\n";
4499 outs() << " flags " << format("0x%" PRIx32, o.flags);
4500 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
4501 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
4502 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
4503 outs() << " OBJC_IMAGE_SUPPORTS_GC";
4504 swift_version = (o.flags >> 8) & 0xff;
4505 if (swift_version != 0) {
4506 if (swift_version == 1)
4507 outs() << " Swift 1.0";
4508 else if (swift_version == 2)
4509 outs() << " Swift 1.1";
4510 else
4511 outs() << " unknown future Swift version (" << swift_version << ")";
4512 }
4513 outs() << "\n";
4514}
4515
Kevin Enderby0fc11822015-04-01 20:57:01 +00004516static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
4517 SymbolAddressMap AddrMap;
4518 if (verbose)
4519 CreateSymbolAddressMap(O, &AddrMap);
4520
4521 std::vector<SectionRef> Sections;
4522 for (const SectionRef &Section : O->sections()) {
4523 StringRef SectName;
4524 Section.getName(SectName);
4525 Sections.push_back(Section);
4526 }
4527
4528 struct DisassembleInfo info;
4529 // Set up the block of info used by the Symbolizer call backs.
4530 info.verbose = verbose;
4531 info.O = O;
4532 info.AddrMap = &AddrMap;
4533 info.Sections = &Sections;
4534 info.class_name = nullptr;
4535 info.selector_name = nullptr;
4536 info.method = nullptr;
4537 info.demangled_name = nullptr;
4538 info.bindtable = nullptr;
4539 info.adrp_addr = 0;
4540 info.adrp_inst = 0;
4541
4542 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
4543 if (CL != SectionRef()) {
4544 info.S = CL;
4545 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
4546 } else {
4547 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
4548 info.S = CL;
4549 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
4550 }
4551
4552 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
4553 if (CR != SectionRef()) {
4554 info.S = CR;
4555 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
4556 } else {
4557 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
4558 info.S = CR;
4559 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
4560 }
4561
4562 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
4563 if (SR != SectionRef()) {
4564 info.S = SR;
4565 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
4566 } else {
4567 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
4568 info.S = SR;
4569 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
4570 }
4571
4572 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
4573 if (CA != SectionRef()) {
4574 info.S = CA;
4575 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
4576 } else {
4577 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
4578 info.S = CA;
4579 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
4580 }
4581
4582 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
4583 if (PL != SectionRef()) {
4584 info.S = PL;
4585 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
4586 } else {
4587 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
4588 info.S = PL;
4589 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
4590 }
4591
4592 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
4593 if (MR != SectionRef()) {
4594 info.S = MR;
4595 print_message_refs64(MR, &info);
4596 } else {
4597 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
4598 info.S = MR;
4599 print_message_refs64(MR, &info);
4600 }
4601
4602 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
4603 if (II != SectionRef()) {
4604 info.S = II;
4605 print_image_info64(II, &info);
4606 } else {
4607 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
4608 info.S = II;
4609 print_image_info64(II, &info);
4610 }
Kevin Enderby0bc6ed42015-04-01 21:50:45 +00004611
4612 if (info.bindtable != nullptr)
4613 delete info.bindtable;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004614}
4615
4616static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004617 SymbolAddressMap AddrMap;
4618 if (verbose)
4619 CreateSymbolAddressMap(O, &AddrMap);
4620
4621 std::vector<SectionRef> Sections;
4622 for (const SectionRef &Section : O->sections()) {
4623 StringRef SectName;
4624 Section.getName(SectName);
4625 Sections.push_back(Section);
4626 }
4627
4628 struct DisassembleInfo info;
4629 // Set up the block of info used by the Symbolizer call backs.
4630 info.verbose = verbose;
4631 info.O = O;
4632 info.AddrMap = &AddrMap;
4633 info.Sections = &Sections;
4634 info.class_name = nullptr;
4635 info.selector_name = nullptr;
4636 info.method = nullptr;
4637 info.demangled_name = nullptr;
4638 info.bindtable = nullptr;
4639 info.adrp_addr = 0;
4640 info.adrp_inst = 0;
4641
4642 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
4643 if (CL != SectionRef()) {
4644 info.S = CL;
4645 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
4646 } else {
4647 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
4648 info.S = CL;
4649 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
4650 }
4651
4652 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
4653 if (CR != SectionRef()) {
4654 info.S = CR;
4655 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
4656 } else {
4657 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
4658 info.S = CR;
4659 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
4660 }
4661
4662 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
4663 if (SR != SectionRef()) {
4664 info.S = SR;
4665 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
4666 } else {
4667 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
4668 info.S = SR;
4669 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
4670 }
4671
4672 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
4673 if (CA != SectionRef()) {
4674 info.S = CA;
4675 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
4676 } else {
4677 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
4678 info.S = CA;
4679 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
4680 }
4681
4682 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
4683 if (PL != SectionRef()) {
4684 info.S = PL;
4685 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
4686 } else {
4687 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
4688 info.S = PL;
4689 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
4690 }
4691
4692 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
4693 if (MR != SectionRef()) {
4694 info.S = MR;
4695 print_message_refs32(MR, &info);
4696 } else {
4697 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
4698 info.S = MR;
4699 print_message_refs32(MR, &info);
4700 }
4701
4702 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
4703 if (II != SectionRef()) {
4704 info.S = II;
4705 print_image_info32(II, &info);
4706 } else {
4707 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
4708 info.S = II;
4709 print_image_info32(II, &info);
4710 }
Kevin Enderby0fc11822015-04-01 20:57:01 +00004711}
4712
4713static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004714 outs() << "Objective-C segment\n";
Kevin Enderby0fc11822015-04-01 20:57:01 +00004715 const SectionRef S = get_section(O, "__OBJC", "__module_info");
4716 if (S != SectionRef()) {
4717 outs() << "Printing Objc1 32-bit MetaData not yet supported\n";
4718 return true;
4719 }
4720 return false;
4721}
4722
4723static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
4724 if (O->is64Bit())
4725 printObjc2_64bit_MetaData(O, verbose);
4726 else {
4727 MachO::mach_header H;
4728 H = O->getHeader();
4729 if (H.cputype == MachO::CPU_TYPE_ARM)
4730 printObjc2_32bit_MetaData(O, verbose);
4731 else {
4732 // This is the 32-bit non-arm cputype case. Which is normally
4733 // the first Objective-C ABI. But it may be the case of a
4734 // binary for the iOS simulator which is the second Objective-C
4735 // ABI. In that case printObjc1_32bit_MetaData() will determine that
4736 // and return false.
4737 if (printObjc1_32bit_MetaData(O, verbose) == false)
4738 printObjc2_32bit_MetaData(O, verbose);
4739 }
4740 }
4741}
4742
Kevin Enderbybf246f52014-09-24 23:08:22 +00004743// GuessLiteralPointer returns a string which for the item in the Mach-O file
4744// for the address passed in as ReferenceValue for printing as a comment with
4745// the instruction and also returns the corresponding type of that item
4746// indirectly through ReferenceType.
4747//
4748// If ReferenceValue is an address of literal cstring then a pointer to the
4749// cstring is returned and ReferenceType is set to
4750// LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
4751//
Kevin Enderby6f326ce2014-10-23 19:37:31 +00004752// If ReferenceValue is an address of an Objective-C CFString, Selector ref or
4753// Class ref that name is returned and the ReferenceType is set accordingly.
4754//
4755// Lastly, literals which are Symbol address in a literal pool are looked for
4756// and if found the symbol name is returned and ReferenceType is set to
4757// LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
4758//
4759// If there is no item in the Mach-O file for the address passed in as
4760// ReferenceValue nullptr is returned and ReferenceType is unchanged.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00004761static const char *GuessLiteralPointer(uint64_t ReferenceValue,
4762 uint64_t ReferencePC,
4763 uint64_t *ReferenceType,
4764 struct DisassembleInfo *info) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00004765 // First see if there is an external relocation entry at the ReferencePC.
Rafael Espindola80291272014-10-08 15:28:58 +00004766 uint64_t sect_addr = info->S.getAddress();
Kevin Enderbybf246f52014-09-24 23:08:22 +00004767 uint64_t sect_offset = ReferencePC - sect_addr;
4768 bool reloc_found = false;
4769 DataRefImpl Rel;
4770 MachO::any_relocation_info RE;
4771 bool isExtern = false;
4772 SymbolRef Symbol;
4773 for (const RelocationRef &Reloc : info->S.relocations()) {
4774 uint64_t RelocOffset;
4775 Reloc.getOffset(RelocOffset);
4776 if (RelocOffset == sect_offset) {
4777 Rel = Reloc.getRawDataRefImpl();
4778 RE = info->O->getRelocation(Rel);
4779 if (info->O->isRelocationScattered(RE))
4780 continue;
4781 isExtern = info->O->getPlainRelocationExternal(RE);
4782 if (isExtern) {
4783 symbol_iterator RelocSym = Reloc.getSymbol();
4784 Symbol = *RelocSym;
4785 }
4786 reloc_found = true;
4787 break;
4788 }
4789 }
4790 // If there is an external relocation entry for a symbol in a section
4791 // then used that symbol's value for the value of the reference.
4792 if (reloc_found && isExtern) {
4793 if (info->O->getAnyRelocationPCRel(RE)) {
4794 unsigned Type = info->O->getAnyRelocationType(RE);
4795 if (Type == MachO::X86_64_RELOC_SIGNED) {
4796 Symbol.getAddress(ReferenceValue);
4797 }
4798 }
4799 }
4800
Kevin Enderby6f326ce2014-10-23 19:37:31 +00004801 // Look for literals such as Objective-C CFStrings refs, Selector refs,
4802 // Message refs and Class refs.
4803 bool classref, selref, msgref, cfstring;
4804 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
4805 selref, msgref, cfstring);
David Blaikie33dd45d02015-03-23 18:39:02 +00004806 if (classref && pointer_value == 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00004807 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
4808 // And the pointer_value in that section is typically zero as it will be
4809 // set by dyld as part of the "bind information".
4810 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
4811 if (name != nullptr) {
4812 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
Hans Wennborgdb53e302014-10-23 21:59:17 +00004813 const char *class_name = strrchr(name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00004814 if (class_name != nullptr && class_name[1] == '_' &&
4815 class_name[2] != '\0') {
4816 info->class_name = class_name + 2;
4817 return name;
4818 }
4819 }
4820 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00004821
David Blaikie33dd45d02015-03-23 18:39:02 +00004822 if (classref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00004823 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
4824 const char *name =
4825 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
4826 if (name != nullptr)
4827 info->class_name = name;
4828 else
4829 name = "bad class ref";
Kevin Enderbybf246f52014-09-24 23:08:22 +00004830 return name;
4831 }
4832
David Blaikie33dd45d02015-03-23 18:39:02 +00004833 if (cfstring) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00004834 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
4835 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
4836 return name;
4837 }
4838
David Blaikie33dd45d02015-03-23 18:39:02 +00004839 if (selref && pointer_value == 0)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00004840 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
4841
4842 if (pointer_value != 0)
4843 ReferenceValue = pointer_value;
4844
4845 const char *name = GuessCstringPointer(ReferenceValue, info);
4846 if (name) {
David Blaikie33dd45d02015-03-23 18:39:02 +00004847 if (pointer_value != 0 && selref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00004848 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
4849 info->selector_name = name;
David Blaikie33dd45d02015-03-23 18:39:02 +00004850 } else if (pointer_value != 0 && msgref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00004851 info->class_name = nullptr;
4852 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
4853 info->selector_name = name;
4854 } else
4855 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
4856 return name;
4857 }
4858
4859 // Lastly look for an indirect symbol with this ReferenceValue which is in
4860 // a literal pool. If found return that symbol name.
4861 name = GuessIndirectSymbol(ReferenceValue, info);
4862 if (name) {
4863 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
4864 return name;
4865 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00004866
4867 return nullptr;
4868}
4869
Kevin Enderby98c9acc2014-09-16 18:00:57 +00004870// SymbolizerSymbolLookUp is the symbol lookup function passed when creating
Kevin Enderbybf246f52014-09-24 23:08:22 +00004871// the Symbolizer. It looks up the ReferenceValue using the info passed via the
Kevin Enderby98c9acc2014-09-16 18:00:57 +00004872// pointer to the struct DisassembleInfo that was passed when MCSymbolizer
4873// is created and returns the symbol name that matches the ReferenceValue or
4874// nullptr if none. The ReferenceType is passed in for the IN type of
4875// reference the instruction is making from the values in defined in the header
4876// "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
4877// Out type and the ReferenceName will also be set which is added as a comment
4878// to the disassembled instruction.
4879//
Kevin Enderby04bf6932014-10-28 23:39:46 +00004880#if HAVE_CXXABI_H
4881// If the symbol name is a C++ mangled name then the demangled name is
Kevin Enderby98c9acc2014-09-16 18:00:57 +00004882// returned through ReferenceName and ReferenceType is set to
4883// LLVMDisassembler_ReferenceType_DeMangled_Name .
Kevin Enderby04bf6932014-10-28 23:39:46 +00004884#endif
Kevin Enderby98c9acc2014-09-16 18:00:57 +00004885//
4886// When this is called to get a symbol name for a branch target then the
4887// ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
4888// SymbolValue will be looked for in the indirect symbol table to determine if
4889// it is an address for a symbol stub. If so then the symbol name for that
4890// stub is returned indirectly through ReferenceName and then ReferenceType is
4891// set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
4892//
Kevin Enderbybf246f52014-09-24 23:08:22 +00004893// When this is called with an value loaded via a PC relative load then
Kevin Enderby98c9acc2014-09-16 18:00:57 +00004894// ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
4895// SymbolValue is checked to be an address of literal pointer, symbol pointer,
4896// or an Objective-C meta data reference. If so the output ReferenceType is
Kevin Enderby6f326ce2014-10-23 19:37:31 +00004897// set to correspond to that as well as setting the ReferenceName.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00004898static const char *SymbolizerSymbolLookUp(void *DisInfo,
4899 uint64_t ReferenceValue,
4900 uint64_t *ReferenceType,
4901 uint64_t ReferencePC,
4902 const char **ReferenceName) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00004903 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
Kevin Enderbybf246f52014-09-24 23:08:22 +00004904 // If no verbose symbolic information is wanted then just return nullptr.
David Blaikie33dd45d02015-03-23 18:39:02 +00004905 if (!info->verbose) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00004906 *ReferenceName = nullptr;
4907 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00004908 return nullptr;
4909 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00004910
Kevin Enderbyf6d25852015-01-31 00:37:11 +00004911 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderbybf246f52014-09-24 23:08:22 +00004912
Kevin Enderby85974882014-09-26 22:20:44 +00004913 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
4914 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
Kevin Enderby04bf6932014-10-28 23:39:46 +00004915 if (*ReferenceName != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00004916 method_reference(info, ReferenceType, ReferenceName);
4917 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
4918 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
4919 } else
Kevin Enderby04bf6932014-10-28 23:39:46 +00004920#if HAVE_CXXABI_H
Kevin Enderbyb28ed012014-10-29 21:28:24 +00004921 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00004922 if (info->demangled_name != nullptr)
4923 free(info->demangled_name);
4924 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00004925 info->demangled_name =
4926 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00004927 if (info->demangled_name != nullptr) {
4928 *ReferenceName = info->demangled_name;
4929 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
4930 } else
4931 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
4932 } else
4933#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00004934 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
4935 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
4936 *ReferenceName =
4937 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
Kevin Enderby85974882014-09-26 22:20:44 +00004938 if (*ReferenceName)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00004939 method_reference(info, ReferenceType, ReferenceName);
Kevin Enderby85974882014-09-26 22:20:44 +00004940 else
4941 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00004942 // If this is arm64 and the reference is an adrp instruction save the
4943 // instruction, passed in ReferenceValue and the address of the instruction
4944 // for use later if we see and add immediate instruction.
4945 } else if (info->O->getArch() == Triple::aarch64 &&
4946 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
4947 info->adrp_inst = ReferenceValue;
4948 info->adrp_addr = ReferencePC;
4949 SymbolName = nullptr;
4950 *ReferenceName = nullptr;
4951 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
4952 // If this is arm64 and reference is an add immediate instruction and we
4953 // have
4954 // seen an adrp instruction just before it and the adrp's Xd register
4955 // matches
4956 // this add's Xn register reconstruct the value being referenced and look to
4957 // see if it is a literal pointer. Note the add immediate instruction is
4958 // passed in ReferenceValue.
4959 } else if (info->O->getArch() == Triple::aarch64 &&
4960 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
4961 ReferencePC - 4 == info->adrp_addr &&
4962 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
4963 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
4964 uint32_t addxri_inst;
4965 uint64_t adrp_imm, addxri_imm;
4966
4967 adrp_imm =
4968 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
4969 if (info->adrp_inst & 0x0200000)
4970 adrp_imm |= 0xfffffffffc000000LL;
4971
4972 addxri_inst = ReferenceValue;
4973 addxri_imm = (addxri_inst >> 10) & 0xfff;
4974 if (((addxri_inst >> 22) & 0x3) == 1)
4975 addxri_imm <<= 12;
4976
4977 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
4978 (adrp_imm << 12) + addxri_imm;
4979
4980 *ReferenceName =
4981 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
4982 if (*ReferenceName == nullptr)
4983 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
4984 // If this is arm64 and the reference is a load register instruction and we
4985 // have seen an adrp instruction just before it and the adrp's Xd register
4986 // matches this add's Xn register reconstruct the value being referenced and
4987 // look to see if it is a literal pointer. Note the load register
4988 // instruction is passed in ReferenceValue.
4989 } else if (info->O->getArch() == Triple::aarch64 &&
4990 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
4991 ReferencePC - 4 == info->adrp_addr &&
4992 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
4993 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
4994 uint32_t ldrxui_inst;
4995 uint64_t adrp_imm, ldrxui_imm;
4996
4997 adrp_imm =
4998 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
4999 if (info->adrp_inst & 0x0200000)
5000 adrp_imm |= 0xfffffffffc000000LL;
5001
5002 ldrxui_inst = ReferenceValue;
5003 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
5004
5005 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5006 (adrp_imm << 12) + (ldrxui_imm << 3);
5007
5008 *ReferenceName =
5009 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5010 if (*ReferenceName == nullptr)
5011 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5012 }
5013 // If this arm64 and is an load register (PC-relative) instruction the
5014 // ReferenceValue is the PC plus the immediate value.
5015 else if (info->O->getArch() == Triple::aarch64 &&
5016 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
5017 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
5018 *ReferenceName =
5019 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5020 if (*ReferenceName == nullptr)
5021 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby85974882014-09-26 22:20:44 +00005022 }
Kevin Enderby04bf6932014-10-28 23:39:46 +00005023#if HAVE_CXXABI_H
5024 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5025 if (info->demangled_name != nullptr)
5026 free(info->demangled_name);
5027 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005028 info->demangled_name =
5029 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005030 if (info->demangled_name != nullptr) {
5031 *ReferenceName = info->demangled_name;
5032 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5033 }
5034 }
5035#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005036 else {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005037 *ReferenceName = nullptr;
5038 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5039 }
5040
5041 return SymbolName;
5042}
5043
Kevin Enderbybf246f52014-09-24 23:08:22 +00005044/// \brief Emits the comments that are stored in the CommentStream.
5045/// Each comment in the CommentStream must end with a newline.
5046static void emitComments(raw_svector_ostream &CommentStream,
5047 SmallString<128> &CommentsToEmit,
5048 formatted_raw_ostream &FormattedOS,
5049 const MCAsmInfo &MAI) {
5050 // Flush the stream before taking its content.
5051 CommentStream.flush();
5052 StringRef Comments = CommentsToEmit.str();
5053 // Get the default information for printing a comment.
5054 const char *CommentBegin = MAI.getCommentString();
5055 unsigned CommentColumn = MAI.getCommentColumn();
5056 bool IsFirst = true;
5057 while (!Comments.empty()) {
5058 if (!IsFirst)
5059 FormattedOS << '\n';
5060 // Emit a line of comments.
5061 FormattedOS.PadToColumn(CommentColumn);
5062 size_t Position = Comments.find('\n');
5063 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
5064 // Move after the newline character.
5065 Comments = Comments.substr(Position + 1);
5066 IsFirst = false;
5067 }
5068 FormattedOS.flush();
5069
5070 // Tell the comment stream that the vector changed underneath it.
5071 CommentsToEmit.clear();
5072 CommentStream.resync();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005073}
5074
Kevin Enderby95df54c2015-02-04 01:01:38 +00005075static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
5076 StringRef DisSegName, StringRef DisSectName) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005077 const char *McpuDefault = nullptr;
5078 const Target *ThumbTarget = nullptr;
5079 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005080 if (!TheTarget) {
5081 // GetTarget prints out stuff.
5082 return;
5083 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005084 if (MCPU.empty() && McpuDefault)
5085 MCPU = McpuDefault;
5086
Ahmed Charles56440fd2014-03-06 05:51:42 +00005087 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005088 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005089 if (ThumbTarget)
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005090 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005091
Kevin Enderbyc9595622014-08-06 23:24:41 +00005092 // Package up features to be passed to target/subtarget
5093 std::string FeaturesStr;
5094 if (MAttrs.size()) {
5095 SubtargetFeatures Features;
5096 for (unsigned i = 0; i != MAttrs.size(); ++i)
5097 Features.AddFeature(MAttrs[i]);
5098 FeaturesStr = Features.getString();
5099 }
5100
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005101 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00005102 std::unique_ptr<const MCRegisterInfo> MRI(
5103 TheTarget->createMCRegInfo(TripleName));
5104 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +00005105 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +00005106 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +00005107 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Craig Toppere6cb63e2014-04-25 04:24:47 +00005108 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005109 std::unique_ptr<MCDisassembler> DisAsm(
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005110 TheTarget->createMCDisassembler(*STI, Ctx));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005111 std::unique_ptr<MCSymbolizer> Symbolizer;
5112 struct DisassembleInfo SymbolizerInfo;
5113 std::unique_ptr<MCRelocationInfo> RelInfo(
5114 TheTarget->createMCRelocationInfo(TripleName, Ctx));
5115 if (RelInfo) {
5116 Symbolizer.reset(TheTarget->createMCSymbolizer(
5117 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00005118 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005119 DisAsm->setSymbolizer(std::move(Symbolizer));
5120 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005121 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +00005122 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +00005123 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00005124 // Set the display preference for hex vs. decimal immediates.
5125 IP->setPrintImmHex(PrintImmHex);
5126 // Comment stream and backing vector.
5127 SmallString<128> CommentsToEmit;
5128 raw_svector_ostream CommentStream(CommentsToEmit);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00005129 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
5130 // if it is done then arm64 comments for string literals don't get printed
5131 // and some constant get printed instead and not setting it causes intel
5132 // (32-bit and 64-bit) comments printed with different spacing before the
5133 // comment causing different diffs with the 'C' disassembler library API.
5134 // IP->setCommentStream(CommentStream);
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005135
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005136 if (!AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +00005137 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005138 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005139 return;
5140 }
5141
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005142 // Set up thumb disassembler.
5143 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
5144 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
5145 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005146 std::unique_ptr<MCDisassembler> ThumbDisAsm;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005147 std::unique_ptr<MCInstPrinter> ThumbIP;
5148 std::unique_ptr<MCContext> ThumbCtx;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005149 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
5150 struct DisassembleInfo ThumbSymbolizerInfo;
5151 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005152 if (ThumbTarget) {
5153 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
5154 ThumbAsmInfo.reset(
5155 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
5156 ThumbSTI.reset(
5157 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
5158 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
5159 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
Kevin Enderby930fdc72014-11-06 19:00:13 +00005160 MCContext *PtrThumbCtx = ThumbCtx.get();
5161 ThumbRelInfo.reset(
5162 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
5163 if (ThumbRelInfo) {
5164 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
5165 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00005166 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
Kevin Enderby930fdc72014-11-06 19:00:13 +00005167 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
5168 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005169 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
5170 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +00005171 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
5172 *ThumbInstrInfo, *ThumbMRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00005173 // Set the display preference for hex vs. decimal immediates.
5174 ThumbIP->setPrintImmHex(PrintImmHex);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005175 }
5176
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005177 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005178 errs() << "error: couldn't initialize disassembler for target "
5179 << ThumbTripleName << '\n';
5180 return;
5181 }
5182
Charles Davis8bdfafd2013-09-01 04:28:48 +00005183 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005184
Ahmed Bougachaaa790682013-05-24 01:07:04 +00005185 // FIXME: Using the -cfg command line option, this code used to be able to
5186 // annotate relocations with the referenced symbol's name, and if this was
5187 // inside a __[cf]string section, the data it points to. This is now replaced
5188 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +00005189 std::vector<SectionRef> Sections;
5190 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005191 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +00005192 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005193
Kevin Enderby273ae012013-06-06 17:20:50 +00005194 getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns,
5195 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005196
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005197 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +00005198 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005199
Kevin Enderby273ae012013-06-06 17:20:50 +00005200 // Build a data in code table that is sorted on by the address of each entry.
5201 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +00005202 if (Header.filetype == MachO::MH_OBJECT)
Rafael Espindola80291272014-10-08 15:28:58 +00005203 BaseAddress = Sections[0].getAddress();
Kevin Enderby273ae012013-06-06 17:20:50 +00005204 else
5205 BaseAddress = BaseSegmentAddress;
5206 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +00005207 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +00005208 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +00005209 uint32_t Offset;
5210 DI->getOffset(Offset);
5211 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
5212 }
5213 array_pod_sort(Dices.begin(), Dices.end());
5214
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005215#ifndef NDEBUG
5216 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
5217#else
5218 raw_ostream &DebugOut = nulls();
5219#endif
5220
Ahmed Charles56440fd2014-03-06 05:51:42 +00005221 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +00005222 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +00005223 // Try to find debug info and set up the DIContext for it.
5224 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +00005225 // A separate DSym file path was specified, parse it as a macho file,
5226 // get the sections and supply it to the section name parsing machinery.
5227 if (!DSYMFile.empty()) {
Rafael Espindola48af1c22014-08-19 18:44:46 +00005228 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +00005229 MemoryBuffer::getFileOrSTDIN(DSYMFile);
Rafael Espindola48af1c22014-08-19 18:44:46 +00005230 if (std::error_code EC = BufOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00005231 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
Benjamin Kramer699128e2011-09-21 01:13:19 +00005232 return;
5233 }
Rafael Espindola48af1c22014-08-19 18:44:46 +00005234 DbgObj =
5235 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
5236 .get()
5237 .release();
Benjamin Kramer699128e2011-09-21 01:13:19 +00005238 }
5239
Eric Christopher7370b552012-11-12 21:40:38 +00005240 // Setup the DIContext
Rafael Espindolaa04bb5b2014-07-31 20:19:36 +00005241 diContext.reset(DIContext::getDWARFContext(*DbgObj));
Benjamin Kramer699128e2011-09-21 01:13:19 +00005242 }
5243
Kevin Enderby95df54c2015-02-04 01:01:38 +00005244 if (DumpSections.size() == 0)
5245 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00005246
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005247 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00005248 StringRef SectName;
Kevin Enderby95df54c2015-02-04 01:01:38 +00005249 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
5250 continue;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005251
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00005252 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +00005253
Rafael Espindolab0f76a42013-04-05 15:15:22 +00005254 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
Kevin Enderby95df54c2015-02-04 01:01:38 +00005255 if (SegmentName != DisSegName)
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00005256 continue;
5257
Rafael Espindola7fc5b872014-11-12 02:04:27 +00005258 StringRef BytesStr;
5259 Sections[SectIdx].getContents(BytesStr);
Aaron Ballman106fd7b2014-11-12 14:01:17 +00005260 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
5261 BytesStr.size());
Rafael Espindola80291272014-10-08 15:28:58 +00005262 uint64_t SectAddress = Sections[SectIdx].getAddress();
Rafael Espindolabd604f22014-11-07 00:52:15 +00005263
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005264 bool symbolTableWorked = false;
5265
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005266 // Parse relocations.
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00005267 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
5268 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
Rafael Espindola80291272014-10-08 15:28:58 +00005269 uint64_t RelocOffset;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00005270 Reloc.getOffset(RelocOffset);
Rafael Espindola80291272014-10-08 15:28:58 +00005271 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Owen Andersond9243c42011-10-17 21:37:35 +00005272 RelocOffset -= SectionAddress;
5273
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00005274 symbol_iterator RelocSym = Reloc.getSymbol();
Owen Andersond9243c42011-10-17 21:37:35 +00005275
Rafael Espindola806f0062013-06-05 01:33:53 +00005276 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005277 }
5278 array_pod_sort(Relocs.begin(), Relocs.end());
5279
Kevin Enderbybf246f52014-09-24 23:08:22 +00005280 // Create a map of symbol addresses to symbol names for use by
5281 // the SymbolizerSymbolLookUp() routine.
5282 SymbolAddressMap AddrMap;
Kevin Enderby6a221752015-03-17 17:10:57 +00005283 bool DisSymNameFound = false;
Kevin Enderbybf246f52014-09-24 23:08:22 +00005284 for (const SymbolRef &Symbol : MachOOF->symbols()) {
5285 SymbolRef::Type ST;
5286 Symbol.getType(ST);
5287 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
5288 ST == SymbolRef::ST_Other) {
5289 uint64_t Address;
5290 Symbol.getAddress(Address);
5291 StringRef SymName;
5292 Symbol.getName(SymName);
5293 AddrMap[Address] = SymName;
Kevin Enderby6a221752015-03-17 17:10:57 +00005294 if (!DisSymName.empty() && DisSymName == SymName)
5295 DisSymNameFound = true;
Kevin Enderbybf246f52014-09-24 23:08:22 +00005296 }
5297 }
David Blaikie33dd45d02015-03-23 18:39:02 +00005298 if (!DisSymName.empty() && !DisSymNameFound) {
Kevin Enderby6a221752015-03-17 17:10:57 +00005299 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
5300 return;
5301 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005302 // Set up the block of info used by the Symbolizer call backs.
Kevin Enderby8e29ec92015-03-17 22:26:11 +00005303 SymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005304 SymbolizerInfo.O = MachOOF;
5305 SymbolizerInfo.S = Sections[SectIdx];
Kevin Enderbybf246f52014-09-24 23:08:22 +00005306 SymbolizerInfo.AddrMap = &AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005307 SymbolizerInfo.Sections = &Sections;
5308 SymbolizerInfo.class_name = nullptr;
5309 SymbolizerInfo.selector_name = nullptr;
5310 SymbolizerInfo.method = nullptr;
Kevin Enderby04bf6932014-10-28 23:39:46 +00005311 SymbolizerInfo.demangled_name = nullptr;
Kevin Enderby078be602014-10-23 19:53:12 +00005312 SymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00005313 SymbolizerInfo.adrp_addr = 0;
5314 SymbolizerInfo.adrp_inst = 0;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005315 // Same for the ThumbSymbolizer
Kevin Enderby8e29ec92015-03-17 22:26:11 +00005316 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005317 ThumbSymbolizerInfo.O = MachOOF;
5318 ThumbSymbolizerInfo.S = Sections[SectIdx];
5319 ThumbSymbolizerInfo.AddrMap = &AddrMap;
5320 ThumbSymbolizerInfo.Sections = &Sections;
5321 ThumbSymbolizerInfo.class_name = nullptr;
5322 ThumbSymbolizerInfo.selector_name = nullptr;
5323 ThumbSymbolizerInfo.method = nullptr;
5324 ThumbSymbolizerInfo.demangled_name = nullptr;
5325 ThumbSymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00005326 ThumbSymbolizerInfo.adrp_addr = 0;
5327 ThumbSymbolizerInfo.adrp_inst = 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005328
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005329 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005330 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00005331 StringRef SymName;
5332 Symbols[SymIdx].getName(SymName);
5333
5334 SymbolRef::Type ST;
5335 Symbols[SymIdx].getType(ST);
5336 if (ST != SymbolRef::ST_Function)
5337 continue;
5338
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005339 // Make sure the symbol is defined in this section.
Rafael Espindola80291272014-10-08 15:28:58 +00005340 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Owen Andersond9243c42011-10-17 21:37:35 +00005341 if (!containsSym)
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005342 continue;
5343
Kevin Enderby6a221752015-03-17 17:10:57 +00005344 // If we are only disassembling one symbol see if this is that symbol.
5345 if (!DisSymName.empty() && DisSymName != SymName)
5346 continue;
5347
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005348 // Start at the address of the symbol relative to the section's address.
Owen Andersond9243c42011-10-17 21:37:35 +00005349 uint64_t Start = 0;
Rafael Espindola80291272014-10-08 15:28:58 +00005350 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00005351 Symbols[SymIdx].getAddress(Start);
Cameron Zwarich54478a52012-02-03 05:42:17 +00005352 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00005353
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005354 // Stop disassembling either at the beginning of the next symbol or at
5355 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +00005356 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +00005357 uint64_t NextSym = 0;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005358 uint64_t NextSymIdx = SymIdx + 1;
Owen Andersond9243c42011-10-17 21:37:35 +00005359 while (Symbols.size() > NextSymIdx) {
5360 SymbolRef::Type NextSymType;
5361 Symbols[NextSymIdx].getType(NextSymType);
5362 if (NextSymType == SymbolRef::ST_Function) {
Rafael Espindola80291272014-10-08 15:28:58 +00005363 containsNextSym =
5364 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00005365 Symbols[NextSymIdx].getAddress(NextSym);
Cameron Zwarich54478a52012-02-03 05:42:17 +00005366 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00005367 break;
5368 }
5369 ++NextSymIdx;
5370 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005371
Rafael Espindola80291272014-10-08 15:28:58 +00005372 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005373 uint64_t End = containsNextSym ? NextSym : SectSize;
Owen Andersond9243c42011-10-17 21:37:35 +00005374 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005375
5376 symbolTableWorked = true;
Rafael Espindolabd604f22014-11-07 00:52:15 +00005377
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005378 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
5379 bool isThumb =
5380 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
5381
Ahmed Bougachaaa790682013-05-24 01:07:04 +00005382 outs() << SymName << ":\n";
5383 DILineInfo lastLine;
5384 for (uint64_t Index = Start; Index < End; Index += Size) {
5385 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +00005386
Kevin Enderbybf246f52014-09-24 23:08:22 +00005387 uint64_t PC = SectAddress + Index;
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00005388 if (!NoLeadingAddr) {
5389 if (FullLeadingAddr) {
5390 if (MachOOF->is64Bit())
5391 outs() << format("%016" PRIx64, PC);
5392 else
5393 outs() << format("%08" PRIx64, PC);
5394 } else {
5395 outs() << format("%8" PRIx64 ":", PC);
5396 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005397 }
5398 if (!NoShowRawInsn)
5399 outs() << "\t";
Kevin Enderby273ae012013-06-06 17:20:50 +00005400
5401 // Check the data in code table here to see if this is data not an
5402 // instruction to be disassembled.
5403 DiceTable Dice;
Kevin Enderbybf246f52014-09-24 23:08:22 +00005404 Dice.push_back(std::make_pair(PC, DiceRef()));
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005405 dice_table_iterator DTI =
5406 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
5407 compareDiceTableEntries);
5408 if (DTI != Dices.end()) {
Kevin Enderby273ae012013-06-06 17:20:50 +00005409 uint16_t Length;
5410 DTI->second.getLength(Length);
Kevin Enderby273ae012013-06-06 17:20:50 +00005411 uint16_t Kind;
5412 DTI->second.getKind(Kind);
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00005413 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
Kevin Enderby930fdc72014-11-06 19:00:13 +00005414 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
5415 (PC == (DTI->first + Length - 1)) && (Length & 1))
5416 Size++;
Kevin Enderby273ae012013-06-06 17:20:50 +00005417 continue;
5418 }
5419
Kevin Enderbybf246f52014-09-24 23:08:22 +00005420 SmallVector<char, 64> AnnotationsBytes;
5421 raw_svector_ostream Annotations(AnnotationsBytes);
5422
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005423 bool gotInst;
5424 if (isThumb)
Rafael Espindola7fc5b872014-11-12 02:04:27 +00005425 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005426 PC, DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005427 else
Rafael Espindola7fc5b872014-11-12 02:04:27 +00005428 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
Kevin Enderbybf246f52014-09-24 23:08:22 +00005429 DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005430 if (gotInst) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005431 if (!NoShowRawInsn) {
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00005432 DumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size));
Kevin Enderbybf246f52014-09-24 23:08:22 +00005433 }
5434 formatted_raw_ostream FormattedOS(outs());
5435 Annotations.flush();
5436 StringRef AnnotationsStr = Annotations.str();
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005437 if (isThumb)
Akira Hatanakab46d0232015-03-27 20:36:02 +00005438 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005439 else
Akira Hatanaka1d079942015-03-28 20:44:05 +00005440 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
Kevin Enderbybf246f52014-09-24 23:08:22 +00005441 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
Owen Andersond9243c42011-10-17 21:37:35 +00005442
Ahmed Bougachaaa790682013-05-24 01:07:04 +00005443 // Print debug info.
5444 if (diContext) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005445 DILineInfo dli = diContext->getLineInfoForAddress(PC);
Ahmed Bougachaaa790682013-05-24 01:07:04 +00005446 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +00005447 if (dli != lastLine && dli.Line != 0)
5448 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
5449 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +00005450 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005451 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00005452 outs() << "\n";
5453 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005454 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005455 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005456 outs() << format("\t.byte 0x%02x #bad opcode\n",
5457 *(Bytes.data() + Index) & 0xff);
5458 Size = 1; // skip exactly one illegible byte and move on.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005459 } else if (Arch == Triple::aarch64) {
5460 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
5461 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
5462 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
5463 (*(Bytes.data() + Index + 3) & 0xff) << 24;
5464 outs() << format("\t.long\t0x%08x\n", opcode);
5465 Size = 4;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005466 } else {
5467 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
5468 if (Size == 0)
5469 Size = 1; // skip illegible bytes
5470 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005471 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005472 }
5473 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00005474 if (!symbolTableWorked) {
Rafael Espindola80291272014-10-08 15:28:58 +00005475 // Reading the symbol table didn't work, disassemble the whole section.
5476 uint64_t SectAddress = Sections[SectIdx].getAddress();
5477 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbybadd1002012-05-18 00:13:56 +00005478 uint64_t InstSize;
5479 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +00005480 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +00005481
Kevin Enderbybf246f52014-09-24 23:08:22 +00005482 uint64_t PC = SectAddress + Index;
Rafael Espindola7fc5b872014-11-12 02:04:27 +00005483 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
5484 DebugOut, nulls())) {
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00005485 if (!NoLeadingAddr) {
5486 if (FullLeadingAddr) {
5487 if (MachOOF->is64Bit())
5488 outs() << format("%016" PRIx64, PC);
5489 else
5490 outs() << format("%08" PRIx64, PC);
5491 } else {
5492 outs() << format("%8" PRIx64 ":", PC);
5493 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005494 }
5495 if (!NoShowRawInsn) {
5496 outs() << "\t";
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00005497 DumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize));
Kevin Enderbybf246f52014-09-24 23:08:22 +00005498 }
Akira Hatanaka1d079942015-03-28 20:44:05 +00005499 IP->printInst(&Inst, outs(), "", *STI);
Bill Wendling4e68e062012-07-19 00:17:40 +00005500 outs() << "\n";
5501 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005502 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005503 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005504 outs() << format("\t.byte 0x%02x #bad opcode\n",
5505 *(Bytes.data() + Index) & 0xff);
5506 InstSize = 1; // skip exactly one illegible byte and move on.
5507 } else {
5508 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
5509 if (InstSize == 0)
5510 InstSize = 1; // skip illegible bytes
5511 }
Bill Wendling4e68e062012-07-19 00:17:40 +00005512 }
Kevin Enderbybadd1002012-05-18 00:13:56 +00005513 }
5514 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00005515 // The TripleName's need to be reset if we are called again for a different
5516 // archtecture.
5517 TripleName = "";
5518 ThumbTripleName = "";
5519
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005520 if (SymbolizerInfo.method != nullptr)
5521 free(SymbolizerInfo.method);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005522 if (SymbolizerInfo.demangled_name != nullptr)
5523 free(SymbolizerInfo.demangled_name);
Kevin Enderby078be602014-10-23 19:53:12 +00005524 if (SymbolizerInfo.bindtable != nullptr)
5525 delete SymbolizerInfo.bindtable;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005526 if (ThumbSymbolizerInfo.method != nullptr)
5527 free(ThumbSymbolizerInfo.method);
5528 if (ThumbSymbolizerInfo.demangled_name != nullptr)
5529 free(ThumbSymbolizerInfo.demangled_name);
5530 if (ThumbSymbolizerInfo.bindtable != nullptr)
5531 delete ThumbSymbolizerInfo.bindtable;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005532 }
5533}
Tim Northover4bd286a2014-08-01 13:07:19 +00005534
Tim Northover39c70bb2014-08-12 11:52:59 +00005535//===----------------------------------------------------------------------===//
5536// __compact_unwind section dumping
5537//===----------------------------------------------------------------------===//
5538
Tim Northover4bd286a2014-08-01 13:07:19 +00005539namespace {
Tim Northover39c70bb2014-08-12 11:52:59 +00005540
5541template <typename T> static uint64_t readNext(const char *&Buf) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005542 using llvm::support::little;
5543 using llvm::support::unaligned;
Tim Northover39c70bb2014-08-12 11:52:59 +00005544
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005545 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
5546 Buf += sizeof(T);
5547 return Val;
5548}
Tim Northover39c70bb2014-08-12 11:52:59 +00005549
Tim Northover4bd286a2014-08-01 13:07:19 +00005550struct CompactUnwindEntry {
5551 uint32_t OffsetInSection;
5552
5553 uint64_t FunctionAddr;
5554 uint32_t Length;
5555 uint32_t CompactEncoding;
5556 uint64_t PersonalityAddr;
5557 uint64_t LSDAAddr;
5558
5559 RelocationRef FunctionReloc;
5560 RelocationRef PersonalityReloc;
5561 RelocationRef LSDAReloc;
5562
5563 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005564 : OffsetInSection(Offset) {
Tim Northover4bd286a2014-08-01 13:07:19 +00005565 if (Is64)
5566 read<uint64_t>(Contents.data() + Offset);
5567 else
5568 read<uint32_t>(Contents.data() + Offset);
5569 }
5570
5571private:
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005572 template <typename UIntPtr> void read(const char *Buf) {
Tim Northover4bd286a2014-08-01 13:07:19 +00005573 FunctionAddr = readNext<UIntPtr>(Buf);
5574 Length = readNext<uint32_t>(Buf);
5575 CompactEncoding = readNext<uint32_t>(Buf);
5576 PersonalityAddr = readNext<UIntPtr>(Buf);
5577 LSDAAddr = readNext<UIntPtr>(Buf);
5578 }
5579};
5580}
5581
5582/// Given a relocation from __compact_unwind, consisting of the RelocationRef
5583/// and data being relocated, determine the best base Name and Addend to use for
5584/// display purposes.
5585///
5586/// 1. An Extern relocation will directly reference a symbol (and the data is
5587/// then already an addend), so use that.
5588/// 2. Otherwise the data is an offset in the object file's layout; try to find
5589// a symbol before it in the same section, and use the offset from there.
5590/// 3. Finally, if all that fails, fall back to an offset from the start of the
5591/// referenced section.
5592static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
5593 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005594 const RelocationRef &Reloc, uint64_t Addr,
Tim Northover4bd286a2014-08-01 13:07:19 +00005595 StringRef &Name, uint64_t &Addend) {
5596 if (Reloc.getSymbol() != Obj->symbol_end()) {
5597 Reloc.getSymbol()->getName(Name);
5598 Addend = Addr;
5599 return;
5600 }
5601
5602 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
5603 SectionRef RelocSection = Obj->getRelocationSection(RE);
5604
Rafael Espindola80291272014-10-08 15:28:58 +00005605 uint64_t SectionAddr = RelocSection.getAddress();
Tim Northover4bd286a2014-08-01 13:07:19 +00005606
5607 auto Sym = Symbols.upper_bound(Addr);
5608 if (Sym == Symbols.begin()) {
5609 // The first symbol in the object is after this reference, the best we can
5610 // do is section-relative notation.
5611 RelocSection.getName(Name);
5612 Addend = Addr - SectionAddr;
5613 return;
5614 }
5615
5616 // Go back one so that SymbolAddress <= Addr.
5617 --Sym;
5618
5619 section_iterator SymSection = Obj->section_end();
5620 Sym->second.getSection(SymSection);
5621 if (RelocSection == *SymSection) {
5622 // There's a valid symbol in the same section before this reference.
5623 Sym->second.getName(Name);
5624 Addend = Addr - Sym->first;
5625 return;
5626 }
5627
5628 // There is a symbol before this reference, but it's in a different
5629 // section. Probably not helpful to mention it, so use the section name.
5630 RelocSection.getName(Name);
5631 Addend = Addr - SectionAddr;
5632}
5633
5634static void printUnwindRelocDest(const MachOObjectFile *Obj,
5635 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005636 const RelocationRef &Reloc, uint64_t Addr) {
Tim Northover4bd286a2014-08-01 13:07:19 +00005637 StringRef Name;
5638 uint64_t Addend;
5639
Tim Northover0b0add52014-09-09 10:45:06 +00005640 if (!Reloc.getObjectFile())
5641 return;
5642
Tim Northover4bd286a2014-08-01 13:07:19 +00005643 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
5644
5645 outs() << Name;
5646 if (Addend)
Tim Northover63a25622014-08-11 09:14:06 +00005647 outs() << " + " << format("0x%" PRIx64, Addend);
Tim Northover4bd286a2014-08-01 13:07:19 +00005648}
5649
5650static void
5651printMachOCompactUnwindSection(const MachOObjectFile *Obj,
5652 std::map<uint64_t, SymbolRef> &Symbols,
5653 const SectionRef &CompactUnwind) {
5654
5655 assert(Obj->isLittleEndian() &&
5656 "There should not be a big-endian .o with __compact_unwind");
5657
5658 bool Is64 = Obj->is64Bit();
5659 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
5660 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
5661
5662 StringRef Contents;
5663 CompactUnwind.getContents(Contents);
5664
5665 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
5666
5667 // First populate the initial raw offsets, encodings and so on from the entry.
5668 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
5669 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
5670 CompactUnwinds.push_back(Entry);
5671 }
5672
5673 // Next we need to look at the relocations to find out what objects are
5674 // actually being referred to.
5675 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
5676 uint64_t RelocAddress;
5677 Reloc.getOffset(RelocAddress);
5678
5679 uint32_t EntryIdx = RelocAddress / EntrySize;
5680 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
5681 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
5682
5683 if (OffsetInEntry == 0)
5684 Entry.FunctionReloc = Reloc;
5685 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
5686 Entry.PersonalityReloc = Reloc;
5687 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
5688 Entry.LSDAReloc = Reloc;
5689 else
5690 llvm_unreachable("Unexpected relocation in __compact_unwind section");
5691 }
5692
5693 // Finally, we're ready to print the data we've gathered.
5694 outs() << "Contents of __compact_unwind section:\n";
5695 for (auto &Entry : CompactUnwinds) {
Tim Northover06af2602014-08-08 12:08:51 +00005696 outs() << " Entry at offset "
5697 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00005698
5699 // 1. Start of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005700 outs() << " start: " << format("0x%" PRIx64,
5701 Entry.FunctionAddr) << ' ';
5702 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
Tim Northover4bd286a2014-08-01 13:07:19 +00005703 outs() << '\n';
5704
5705 // 2. Length of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005706 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
5707 << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00005708 // 3. The 32-bit compact encoding.
5709 outs() << " compact encoding: "
Tim Northoverb911bf82014-08-08 12:00:09 +00005710 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00005711
5712 // 4. The personality function, if present.
5713 if (Entry.PersonalityReloc.getObjectFile()) {
5714 outs() << " personality function: "
Tim Northoverb911bf82014-08-08 12:00:09 +00005715 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00005716 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
5717 Entry.PersonalityAddr);
5718 outs() << '\n';
5719 }
5720
5721 // 5. This entry's language-specific data area.
5722 if (Entry.LSDAReloc.getObjectFile()) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005723 outs() << " LSDA: " << format("0x%" PRIx64,
5724 Entry.LSDAAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00005725 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
5726 outs() << '\n';
5727 }
5728 }
5729}
5730
Tim Northover39c70bb2014-08-12 11:52:59 +00005731//===----------------------------------------------------------------------===//
5732// __unwind_info section dumping
5733//===----------------------------------------------------------------------===//
5734
5735static void printRegularSecondLevelUnwindPage(const char *PageStart) {
5736 const char *Pos = PageStart;
5737 uint32_t Kind = readNext<uint32_t>(Pos);
5738 (void)Kind;
5739 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
5740
5741 uint16_t EntriesStart = readNext<uint16_t>(Pos);
5742 uint16_t NumEntries = readNext<uint16_t>(Pos);
5743
5744 Pos = PageStart + EntriesStart;
5745 for (unsigned i = 0; i < NumEntries; ++i) {
5746 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
5747 uint32_t Encoding = readNext<uint32_t>(Pos);
5748
5749 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005750 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
5751 << ", "
5752 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00005753 }
5754}
5755
5756static void printCompressedSecondLevelUnwindPage(
5757 const char *PageStart, uint32_t FunctionBase,
5758 const SmallVectorImpl<uint32_t> &CommonEncodings) {
5759 const char *Pos = PageStart;
5760 uint32_t Kind = readNext<uint32_t>(Pos);
5761 (void)Kind;
5762 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
5763
5764 uint16_t EntriesStart = readNext<uint16_t>(Pos);
5765 uint16_t NumEntries = readNext<uint16_t>(Pos);
5766
5767 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
5768 readNext<uint16_t>(Pos);
Aaron Ballman80930af2014-08-14 13:53:19 +00005769 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
5770 PageStart + EncodingsStart);
Tim Northover39c70bb2014-08-12 11:52:59 +00005771
5772 Pos = PageStart + EntriesStart;
5773 for (unsigned i = 0; i < NumEntries; ++i) {
5774 uint32_t Entry = readNext<uint32_t>(Pos);
5775 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
5776 uint32_t EncodingIdx = Entry >> 24;
5777
5778 uint32_t Encoding;
5779 if (EncodingIdx < CommonEncodings.size())
5780 Encoding = CommonEncodings[EncodingIdx];
5781 else
5782 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
5783
5784 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005785 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
5786 << ", "
5787 << "encoding[" << EncodingIdx
5788 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00005789 }
5790}
5791
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005792static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
5793 std::map<uint64_t, SymbolRef> &Symbols,
5794 const SectionRef &UnwindInfo) {
Tim Northover39c70bb2014-08-12 11:52:59 +00005795
5796 assert(Obj->isLittleEndian() &&
5797 "There should not be a big-endian .o with __unwind_info");
5798
5799 outs() << "Contents of __unwind_info section:\n";
5800
5801 StringRef Contents;
5802 UnwindInfo.getContents(Contents);
5803 const char *Pos = Contents.data();
5804
5805 //===----------------------------------
5806 // Section header
5807 //===----------------------------------
5808
5809 uint32_t Version = readNext<uint32_t>(Pos);
5810 outs() << " Version: "
5811 << format("0x%" PRIx32, Version) << '\n';
5812 assert(Version == 1 && "only understand version 1");
5813
5814 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
5815 outs() << " Common encodings array section offset: "
5816 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
5817 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
5818 outs() << " Number of common encodings in array: "
5819 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
5820
5821 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
5822 outs() << " Personality function array section offset: "
5823 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
5824 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
5825 outs() << " Number of personality functions in array: "
5826 << format("0x%" PRIx32, NumPersonalities) << '\n';
5827
5828 uint32_t IndicesStart = readNext<uint32_t>(Pos);
5829 outs() << " Index array section offset: "
5830 << format("0x%" PRIx32, IndicesStart) << '\n';
5831 uint32_t NumIndices = readNext<uint32_t>(Pos);
5832 outs() << " Number of indices in array: "
5833 << format("0x%" PRIx32, NumIndices) << '\n';
5834
5835 //===----------------------------------
5836 // A shared list of common encodings
5837 //===----------------------------------
5838
5839 // These occupy indices in the range [0, N] whenever an encoding is referenced
5840 // from a compressed 2nd level index table. In practice the linker only
5841 // creates ~128 of these, so that indices are available to embed encodings in
5842 // the 2nd level index.
5843
5844 SmallVector<uint32_t, 64> CommonEncodings;
5845 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
5846 Pos = Contents.data() + CommonEncodingsStart;
5847 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
5848 uint32_t Encoding = readNext<uint32_t>(Pos);
5849 CommonEncodings.push_back(Encoding);
5850
5851 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
5852 << '\n';
5853 }
5854
Tim Northover39c70bb2014-08-12 11:52:59 +00005855 //===----------------------------------
5856 // Personality functions used in this executable
5857 //===----------------------------------
5858
5859 // There should be only a handful of these (one per source language,
5860 // roughly). Particularly since they only get 2 bits in the compact encoding.
5861
5862 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
5863 Pos = Contents.data() + PersonalitiesStart;
5864 for (unsigned i = 0; i < NumPersonalities; ++i) {
5865 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
5866 outs() << " personality[" << i + 1
5867 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
5868 }
5869
5870 //===----------------------------------
5871 // The level 1 index entries
5872 //===----------------------------------
5873
5874 // These specify an approximate place to start searching for the more detailed
5875 // information, sorted by PC.
5876
5877 struct IndexEntry {
5878 uint32_t FunctionOffset;
5879 uint32_t SecondLevelPageStart;
5880 uint32_t LSDAStart;
5881 };
5882
5883 SmallVector<IndexEntry, 4> IndexEntries;
5884
5885 outs() << " Top level indices: (count = " << NumIndices << ")\n";
5886 Pos = Contents.data() + IndicesStart;
5887 for (unsigned i = 0; i < NumIndices; ++i) {
5888 IndexEntry Entry;
5889
5890 Entry.FunctionOffset = readNext<uint32_t>(Pos);
5891 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
5892 Entry.LSDAStart = readNext<uint32_t>(Pos);
5893 IndexEntries.push_back(Entry);
5894
5895 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005896 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
5897 << ", "
Tim Northover39c70bb2014-08-12 11:52:59 +00005898 << "2nd level page offset="
5899 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005900 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00005901 }
5902
Tim Northover39c70bb2014-08-12 11:52:59 +00005903 //===----------------------------------
5904 // Next come the LSDA tables
5905 //===----------------------------------
5906
5907 // The LSDA layout is rather implicit: it's a contiguous array of entries from
5908 // the first top-level index's LSDAOffset to the last (sentinel).
5909
5910 outs() << " LSDA descriptors:\n";
5911 Pos = Contents.data() + IndexEntries[0].LSDAStart;
5912 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
5913 (2 * sizeof(uint32_t));
5914 for (int i = 0; i < NumLSDAs; ++i) {
5915 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
5916 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
5917 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005918 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
5919 << ", "
5920 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00005921 }
5922
5923 //===----------------------------------
5924 // Finally, the 2nd level indices
5925 //===----------------------------------
5926
5927 // Generally these are 4K in size, and have 2 possible forms:
5928 // + Regular stores up to 511 entries with disparate encodings
5929 // + Compressed stores up to 1021 entries if few enough compact encoding
5930 // values are used.
5931 outs() << " Second level indices:\n";
5932 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
5933 // The final sentinel top-level index has no associated 2nd level page
5934 if (IndexEntries[i].SecondLevelPageStart == 0)
5935 break;
5936
5937 outs() << " Second level index[" << i << "]: "
5938 << "offset in section="
5939 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
5940 << ", "
5941 << "base function offset="
5942 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
5943
5944 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
Aaron Ballman80930af2014-08-14 13:53:19 +00005945 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00005946 if (Kind == 2)
5947 printRegularSecondLevelUnwindPage(Pos);
5948 else if (Kind == 3)
5949 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
5950 CommonEncodings);
5951 else
5952 llvm_unreachable("Do not know how to print this kind of 2nd level page");
Tim Northover39c70bb2014-08-12 11:52:59 +00005953 }
5954}
5955
Tim Northover4bd286a2014-08-01 13:07:19 +00005956void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
5957 std::map<uint64_t, SymbolRef> Symbols;
5958 for (const SymbolRef &SymRef : Obj->symbols()) {
5959 // Discard any undefined or absolute symbols. They're not going to take part
5960 // in the convenience lookup for unwind info and just take up resources.
5961 section_iterator Section = Obj->section_end();
5962 SymRef.getSection(Section);
5963 if (Section == Obj->section_end())
5964 continue;
5965
5966 uint64_t Addr;
5967 SymRef.getAddress(Addr);
5968 Symbols.insert(std::make_pair(Addr, SymRef));
5969 }
5970
5971 for (const SectionRef &Section : Obj->sections()) {
5972 StringRef SectName;
5973 Section.getName(SectName);
5974 if (SectName == "__compact_unwind")
5975 printMachOCompactUnwindSection(Obj, Symbols, Section);
5976 else if (SectName == "__unwind_info")
Tim Northover39c70bb2014-08-12 11:52:59 +00005977 printMachOUnwindInfoSection(Obj, Symbols, Section);
Tim Northover4bd286a2014-08-01 13:07:19 +00005978 else if (SectName == "__eh_frame")
5979 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00005980 }
5981}
Kevin Enderbyb76d3862014-08-22 20:35:18 +00005982
5983static void PrintMachHeader(uint32_t magic, uint32_t cputype,
5984 uint32_t cpusubtype, uint32_t filetype,
5985 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
5986 bool verbose) {
5987 outs() << "Mach header\n";
5988 outs() << " magic cputype cpusubtype caps filetype ncmds "
5989 "sizeofcmds flags\n";
5990 if (verbose) {
5991 if (magic == MachO::MH_MAGIC)
5992 outs() << " MH_MAGIC";
5993 else if (magic == MachO::MH_MAGIC_64)
5994 outs() << "MH_MAGIC_64";
5995 else
5996 outs() << format(" 0x%08" PRIx32, magic);
5997 switch (cputype) {
5998 case MachO::CPU_TYPE_I386:
5999 outs() << " I386";
6000 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6001 case MachO::CPU_SUBTYPE_I386_ALL:
6002 outs() << " ALL";
6003 break;
6004 default:
6005 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6006 break;
6007 }
6008 break;
6009 case MachO::CPU_TYPE_X86_64:
6010 outs() << " X86_64";
Kevin Enderby131d1772015-01-09 19:22:37 +00006011 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6012 case MachO::CPU_SUBTYPE_X86_64_ALL:
6013 outs() << " ALL";
6014 break;
6015 case MachO::CPU_SUBTYPE_X86_64_H:
6016 outs() << " Haswell";
6017 break;
6018 default:
6019 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6020 break;
6021 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006022 break;
6023 case MachO::CPU_TYPE_ARM:
6024 outs() << " ARM";
6025 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6026 case MachO::CPU_SUBTYPE_ARM_ALL:
6027 outs() << " ALL";
6028 break;
6029 case MachO::CPU_SUBTYPE_ARM_V4T:
6030 outs() << " V4T";
6031 break;
6032 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6033 outs() << " V5TEJ";
6034 break;
6035 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6036 outs() << " XSCALE";
6037 break;
6038 case MachO::CPU_SUBTYPE_ARM_V6:
6039 outs() << " V6";
6040 break;
6041 case MachO::CPU_SUBTYPE_ARM_V6M:
6042 outs() << " V6M";
6043 break;
6044 case MachO::CPU_SUBTYPE_ARM_V7:
6045 outs() << " V7";
6046 break;
6047 case MachO::CPU_SUBTYPE_ARM_V7EM:
6048 outs() << " V7EM";
6049 break;
6050 case MachO::CPU_SUBTYPE_ARM_V7K:
6051 outs() << " V7K";
6052 break;
6053 case MachO::CPU_SUBTYPE_ARM_V7M:
6054 outs() << " V7M";
6055 break;
6056 case MachO::CPU_SUBTYPE_ARM_V7S:
6057 outs() << " V7S";
6058 break;
6059 default:
6060 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6061 break;
6062 }
6063 break;
6064 case MachO::CPU_TYPE_ARM64:
6065 outs() << " ARM64";
6066 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6067 case MachO::CPU_SUBTYPE_ARM64_ALL:
6068 outs() << " ALL";
6069 break;
6070 default:
6071 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6072 break;
6073 }
6074 break;
6075 case MachO::CPU_TYPE_POWERPC:
6076 outs() << " PPC";
6077 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6078 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6079 outs() << " ALL";
6080 break;
6081 default:
6082 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6083 break;
6084 }
6085 break;
6086 case MachO::CPU_TYPE_POWERPC64:
6087 outs() << " PPC64";
6088 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6089 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6090 outs() << " ALL";
6091 break;
6092 default:
6093 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6094 break;
6095 }
6096 break;
6097 }
6098 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00006099 outs() << " LIB64";
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006100 } else {
6101 outs() << format(" 0x%02" PRIx32,
6102 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6103 }
6104 switch (filetype) {
6105 case MachO::MH_OBJECT:
6106 outs() << " OBJECT";
6107 break;
6108 case MachO::MH_EXECUTE:
6109 outs() << " EXECUTE";
6110 break;
6111 case MachO::MH_FVMLIB:
6112 outs() << " FVMLIB";
6113 break;
6114 case MachO::MH_CORE:
6115 outs() << " CORE";
6116 break;
6117 case MachO::MH_PRELOAD:
6118 outs() << " PRELOAD";
6119 break;
6120 case MachO::MH_DYLIB:
6121 outs() << " DYLIB";
6122 break;
6123 case MachO::MH_DYLIB_STUB:
6124 outs() << " DYLIB_STUB";
6125 break;
6126 case MachO::MH_DYLINKER:
6127 outs() << " DYLINKER";
6128 break;
6129 case MachO::MH_BUNDLE:
6130 outs() << " BUNDLE";
6131 break;
6132 case MachO::MH_DSYM:
6133 outs() << " DSYM";
6134 break;
6135 case MachO::MH_KEXT_BUNDLE:
6136 outs() << " KEXTBUNDLE";
6137 break;
6138 default:
6139 outs() << format(" %10u", filetype);
6140 break;
6141 }
6142 outs() << format(" %5u", ncmds);
6143 outs() << format(" %10u", sizeofcmds);
6144 uint32_t f = flags;
6145 if (f & MachO::MH_NOUNDEFS) {
6146 outs() << " NOUNDEFS";
6147 f &= ~MachO::MH_NOUNDEFS;
6148 }
6149 if (f & MachO::MH_INCRLINK) {
6150 outs() << " INCRLINK";
6151 f &= ~MachO::MH_INCRLINK;
6152 }
6153 if (f & MachO::MH_DYLDLINK) {
6154 outs() << " DYLDLINK";
6155 f &= ~MachO::MH_DYLDLINK;
6156 }
6157 if (f & MachO::MH_BINDATLOAD) {
6158 outs() << " BINDATLOAD";
6159 f &= ~MachO::MH_BINDATLOAD;
6160 }
6161 if (f & MachO::MH_PREBOUND) {
6162 outs() << " PREBOUND";
6163 f &= ~MachO::MH_PREBOUND;
6164 }
6165 if (f & MachO::MH_SPLIT_SEGS) {
6166 outs() << " SPLIT_SEGS";
6167 f &= ~MachO::MH_SPLIT_SEGS;
6168 }
6169 if (f & MachO::MH_LAZY_INIT) {
6170 outs() << " LAZY_INIT";
6171 f &= ~MachO::MH_LAZY_INIT;
6172 }
6173 if (f & MachO::MH_TWOLEVEL) {
6174 outs() << " TWOLEVEL";
6175 f &= ~MachO::MH_TWOLEVEL;
6176 }
6177 if (f & MachO::MH_FORCE_FLAT) {
6178 outs() << " FORCE_FLAT";
6179 f &= ~MachO::MH_FORCE_FLAT;
6180 }
6181 if (f & MachO::MH_NOMULTIDEFS) {
6182 outs() << " NOMULTIDEFS";
6183 f &= ~MachO::MH_NOMULTIDEFS;
6184 }
6185 if (f & MachO::MH_NOFIXPREBINDING) {
6186 outs() << " NOFIXPREBINDING";
6187 f &= ~MachO::MH_NOFIXPREBINDING;
6188 }
6189 if (f & MachO::MH_PREBINDABLE) {
6190 outs() << " PREBINDABLE";
6191 f &= ~MachO::MH_PREBINDABLE;
6192 }
6193 if (f & MachO::MH_ALLMODSBOUND) {
6194 outs() << " ALLMODSBOUND";
6195 f &= ~MachO::MH_ALLMODSBOUND;
6196 }
6197 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
6198 outs() << " SUBSECTIONS_VIA_SYMBOLS";
6199 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
6200 }
6201 if (f & MachO::MH_CANONICAL) {
6202 outs() << " CANONICAL";
6203 f &= ~MachO::MH_CANONICAL;
6204 }
6205 if (f & MachO::MH_WEAK_DEFINES) {
6206 outs() << " WEAK_DEFINES";
6207 f &= ~MachO::MH_WEAK_DEFINES;
6208 }
6209 if (f & MachO::MH_BINDS_TO_WEAK) {
6210 outs() << " BINDS_TO_WEAK";
6211 f &= ~MachO::MH_BINDS_TO_WEAK;
6212 }
6213 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
6214 outs() << " ALLOW_STACK_EXECUTION";
6215 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
6216 }
6217 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
6218 outs() << " DEAD_STRIPPABLE_DYLIB";
6219 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
6220 }
6221 if (f & MachO::MH_PIE) {
6222 outs() << " PIE";
6223 f &= ~MachO::MH_PIE;
6224 }
6225 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
6226 outs() << " NO_REEXPORTED_DYLIBS";
6227 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
6228 }
6229 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
6230 outs() << " MH_HAS_TLV_DESCRIPTORS";
6231 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
6232 }
6233 if (f & MachO::MH_NO_HEAP_EXECUTION) {
6234 outs() << " MH_NO_HEAP_EXECUTION";
6235 f &= ~MachO::MH_NO_HEAP_EXECUTION;
6236 }
6237 if (f & MachO::MH_APP_EXTENSION_SAFE) {
6238 outs() << " APP_EXTENSION_SAFE";
6239 f &= ~MachO::MH_APP_EXTENSION_SAFE;
6240 }
6241 if (f != 0 || flags == 0)
6242 outs() << format(" 0x%08" PRIx32, f);
6243 } else {
6244 outs() << format(" 0x%08" PRIx32, magic);
6245 outs() << format(" %7d", cputype);
6246 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6247 outs() << format(" 0x%02" PRIx32,
6248 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6249 outs() << format(" %10u", filetype);
6250 outs() << format(" %5u", ncmds);
6251 outs() << format(" %10u", sizeofcmds);
6252 outs() << format(" 0x%08" PRIx32, flags);
6253 }
6254 outs() << "\n";
6255}
6256
Kevin Enderby956366c2014-08-29 22:30:52 +00006257static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
6258 StringRef SegName, uint64_t vmaddr,
6259 uint64_t vmsize, uint64_t fileoff,
6260 uint64_t filesize, uint32_t maxprot,
6261 uint32_t initprot, uint32_t nsects,
6262 uint32_t flags, uint32_t object_size,
6263 bool verbose) {
6264 uint64_t expected_cmdsize;
6265 if (cmd == MachO::LC_SEGMENT) {
6266 outs() << " cmd LC_SEGMENT\n";
6267 expected_cmdsize = nsects;
6268 expected_cmdsize *= sizeof(struct MachO::section);
6269 expected_cmdsize += sizeof(struct MachO::segment_command);
6270 } else {
6271 outs() << " cmd LC_SEGMENT_64\n";
6272 expected_cmdsize = nsects;
6273 expected_cmdsize *= sizeof(struct MachO::section_64);
6274 expected_cmdsize += sizeof(struct MachO::segment_command_64);
6275 }
6276 outs() << " cmdsize " << cmdsize;
6277 if (cmdsize != expected_cmdsize)
6278 outs() << " Inconsistent size\n";
6279 else
6280 outs() << "\n";
6281 outs() << " segname " << SegName << "\n";
6282 if (cmd == MachO::LC_SEGMENT_64) {
6283 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
6284 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
6285 } else {
Kevin Enderbyadb7c432014-12-16 18:58:11 +00006286 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
6287 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00006288 }
6289 outs() << " fileoff " << fileoff;
6290 if (fileoff > object_size)
6291 outs() << " (past end of file)\n";
6292 else
6293 outs() << "\n";
6294 outs() << " filesize " << filesize;
6295 if (fileoff + filesize > object_size)
6296 outs() << " (past end of file)\n";
6297 else
6298 outs() << "\n";
6299 if (verbose) {
6300 if ((maxprot &
6301 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
6302 MachO::VM_PROT_EXECUTE)) != 0)
6303 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
6304 else {
6305 if (maxprot & MachO::VM_PROT_READ)
6306 outs() << " maxprot r";
6307 else
6308 outs() << " maxprot -";
6309 if (maxprot & MachO::VM_PROT_WRITE)
6310 outs() << "w";
6311 else
6312 outs() << "-";
6313 if (maxprot & MachO::VM_PROT_EXECUTE)
6314 outs() << "x\n";
6315 else
6316 outs() << "-\n";
6317 }
6318 if ((initprot &
6319 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
6320 MachO::VM_PROT_EXECUTE)) != 0)
6321 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
6322 else {
6323 if (initprot & MachO::VM_PROT_READ)
6324 outs() << " initprot r";
6325 else
6326 outs() << " initprot -";
6327 if (initprot & MachO::VM_PROT_WRITE)
6328 outs() << "w";
6329 else
6330 outs() << "-";
6331 if (initprot & MachO::VM_PROT_EXECUTE)
6332 outs() << "x\n";
6333 else
6334 outs() << "-\n";
6335 }
6336 } else {
6337 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
6338 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
6339 }
6340 outs() << " nsects " << nsects << "\n";
6341 if (verbose) {
6342 outs() << " flags";
6343 if (flags == 0)
6344 outs() << " (none)\n";
6345 else {
6346 if (flags & MachO::SG_HIGHVM) {
6347 outs() << " HIGHVM";
6348 flags &= ~MachO::SG_HIGHVM;
6349 }
6350 if (flags & MachO::SG_FVMLIB) {
6351 outs() << " FVMLIB";
6352 flags &= ~MachO::SG_FVMLIB;
6353 }
6354 if (flags & MachO::SG_NORELOC) {
6355 outs() << " NORELOC";
6356 flags &= ~MachO::SG_NORELOC;
6357 }
6358 if (flags & MachO::SG_PROTECTED_VERSION_1) {
6359 outs() << " PROTECTED_VERSION_1";
6360 flags &= ~MachO::SG_PROTECTED_VERSION_1;
6361 }
6362 if (flags)
6363 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
6364 else
6365 outs() << "\n";
6366 }
6367 } else {
6368 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
6369 }
6370}
6371
6372static void PrintSection(const char *sectname, const char *segname,
6373 uint64_t addr, uint64_t size, uint32_t offset,
6374 uint32_t align, uint32_t reloff, uint32_t nreloc,
6375 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
6376 uint32_t cmd, const char *sg_segname,
6377 uint32_t filetype, uint32_t object_size,
6378 bool verbose) {
6379 outs() << "Section\n";
6380 outs() << " sectname " << format("%.16s\n", sectname);
6381 outs() << " segname " << format("%.16s", segname);
6382 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
6383 outs() << " (does not match segment)\n";
6384 else
6385 outs() << "\n";
6386 if (cmd == MachO::LC_SEGMENT_64) {
6387 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
6388 outs() << " size " << format("0x%016" PRIx64, size);
6389 } else {
Kevin Enderby75594b62014-12-16 21:00:25 +00006390 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
6391 outs() << " size " << format("0x%08" PRIx64, size);
Kevin Enderby956366c2014-08-29 22:30:52 +00006392 }
6393 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
6394 outs() << " (past end of file)\n";
6395 else
6396 outs() << "\n";
6397 outs() << " offset " << offset;
6398 if (offset > object_size)
6399 outs() << " (past end of file)\n";
6400 else
6401 outs() << "\n";
6402 uint32_t align_shifted = 1 << align;
6403 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
6404 outs() << " reloff " << reloff;
6405 if (reloff > object_size)
6406 outs() << " (past end of file)\n";
6407 else
6408 outs() << "\n";
6409 outs() << " nreloc " << nreloc;
6410 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
6411 outs() << " (past end of file)\n";
6412 else
6413 outs() << "\n";
6414 uint32_t section_type = flags & MachO::SECTION_TYPE;
6415 if (verbose) {
6416 outs() << " type";
6417 if (section_type == MachO::S_REGULAR)
6418 outs() << " S_REGULAR\n";
6419 else if (section_type == MachO::S_ZEROFILL)
6420 outs() << " S_ZEROFILL\n";
6421 else if (section_type == MachO::S_CSTRING_LITERALS)
6422 outs() << " S_CSTRING_LITERALS\n";
6423 else if (section_type == MachO::S_4BYTE_LITERALS)
6424 outs() << " S_4BYTE_LITERALS\n";
6425 else if (section_type == MachO::S_8BYTE_LITERALS)
6426 outs() << " S_8BYTE_LITERALS\n";
6427 else if (section_type == MachO::S_16BYTE_LITERALS)
6428 outs() << " S_16BYTE_LITERALS\n";
6429 else if (section_type == MachO::S_LITERAL_POINTERS)
6430 outs() << " S_LITERAL_POINTERS\n";
6431 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
6432 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
6433 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
6434 outs() << " S_LAZY_SYMBOL_POINTERS\n";
6435 else if (section_type == MachO::S_SYMBOL_STUBS)
6436 outs() << " S_SYMBOL_STUBS\n";
6437 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
6438 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
6439 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
6440 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
6441 else if (section_type == MachO::S_COALESCED)
6442 outs() << " S_COALESCED\n";
6443 else if (section_type == MachO::S_INTERPOSING)
6444 outs() << " S_INTERPOSING\n";
6445 else if (section_type == MachO::S_DTRACE_DOF)
6446 outs() << " S_DTRACE_DOF\n";
6447 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
6448 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
6449 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
6450 outs() << " S_THREAD_LOCAL_REGULAR\n";
6451 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
6452 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
6453 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
6454 outs() << " S_THREAD_LOCAL_VARIABLES\n";
6455 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
6456 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
6457 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
6458 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
6459 else
6460 outs() << format("0x%08" PRIx32, section_type) << "\n";
6461 outs() << "attributes";
6462 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
6463 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
6464 outs() << " PURE_INSTRUCTIONS";
6465 if (section_attributes & MachO::S_ATTR_NO_TOC)
6466 outs() << " NO_TOC";
6467 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
6468 outs() << " STRIP_STATIC_SYMS";
6469 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
6470 outs() << " NO_DEAD_STRIP";
6471 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
6472 outs() << " LIVE_SUPPORT";
6473 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
6474 outs() << " SELF_MODIFYING_CODE";
6475 if (section_attributes & MachO::S_ATTR_DEBUG)
6476 outs() << " DEBUG";
6477 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
6478 outs() << " SOME_INSTRUCTIONS";
6479 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
6480 outs() << " EXT_RELOC";
6481 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
6482 outs() << " LOC_RELOC";
6483 if (section_attributes == 0)
6484 outs() << " (none)";
6485 outs() << "\n";
6486 } else
6487 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
6488 outs() << " reserved1 " << reserved1;
6489 if (section_type == MachO::S_SYMBOL_STUBS ||
6490 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
6491 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
6492 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
6493 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
6494 outs() << " (index into indirect symbol table)\n";
6495 else
6496 outs() << "\n";
6497 outs() << " reserved2 " << reserved2;
6498 if (section_type == MachO::S_SYMBOL_STUBS)
6499 outs() << " (size of stubs)\n";
6500 else
6501 outs() << "\n";
6502}
6503
David Majnemer73cc6ff2014-11-13 19:48:56 +00006504static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
Kevin Enderby956366c2014-08-29 22:30:52 +00006505 uint32_t object_size) {
6506 outs() << " cmd LC_SYMTAB\n";
6507 outs() << " cmdsize " << st.cmdsize;
6508 if (st.cmdsize != sizeof(struct MachO::symtab_command))
6509 outs() << " Incorrect size\n";
6510 else
6511 outs() << "\n";
6512 outs() << " symoff " << st.symoff;
6513 if (st.symoff > object_size)
6514 outs() << " (past end of file)\n";
6515 else
6516 outs() << "\n";
6517 outs() << " nsyms " << st.nsyms;
6518 uint64_t big_size;
David Majnemer73cc6ff2014-11-13 19:48:56 +00006519 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00006520 big_size = st.nsyms;
6521 big_size *= sizeof(struct MachO::nlist_64);
6522 big_size += st.symoff;
6523 if (big_size > object_size)
6524 outs() << " (past end of file)\n";
6525 else
6526 outs() << "\n";
6527 } else {
6528 big_size = st.nsyms;
6529 big_size *= sizeof(struct MachO::nlist);
6530 big_size += st.symoff;
6531 if (big_size > object_size)
6532 outs() << " (past end of file)\n";
6533 else
6534 outs() << "\n";
6535 }
6536 outs() << " stroff " << st.stroff;
6537 if (st.stroff > object_size)
6538 outs() << " (past end of file)\n";
6539 else
6540 outs() << "\n";
6541 outs() << " strsize " << st.strsize;
6542 big_size = st.stroff;
6543 big_size += st.strsize;
6544 if (big_size > object_size)
6545 outs() << " (past end of file)\n";
6546 else
6547 outs() << "\n";
6548}
6549
6550static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
6551 uint32_t nsyms, uint32_t object_size,
David Majnemer73cc6ff2014-11-13 19:48:56 +00006552 bool Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00006553 outs() << " cmd LC_DYSYMTAB\n";
6554 outs() << " cmdsize " << dyst.cmdsize;
6555 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
6556 outs() << " Incorrect size\n";
6557 else
6558 outs() << "\n";
6559 outs() << " ilocalsym " << dyst.ilocalsym;
6560 if (dyst.ilocalsym > nsyms)
6561 outs() << " (greater than the number of symbols)\n";
6562 else
6563 outs() << "\n";
6564 outs() << " nlocalsym " << dyst.nlocalsym;
6565 uint64_t big_size;
6566 big_size = dyst.ilocalsym;
6567 big_size += dyst.nlocalsym;
6568 if (big_size > nsyms)
6569 outs() << " (past the end of the symbol table)\n";
6570 else
6571 outs() << "\n";
6572 outs() << " iextdefsym " << dyst.iextdefsym;
6573 if (dyst.iextdefsym > nsyms)
6574 outs() << " (greater than the number of symbols)\n";
6575 else
6576 outs() << "\n";
6577 outs() << " nextdefsym " << dyst.nextdefsym;
6578 big_size = dyst.iextdefsym;
6579 big_size += dyst.nextdefsym;
6580 if (big_size > nsyms)
6581 outs() << " (past the end of the symbol table)\n";
6582 else
6583 outs() << "\n";
6584 outs() << " iundefsym " << dyst.iundefsym;
6585 if (dyst.iundefsym > nsyms)
6586 outs() << " (greater than the number of symbols)\n";
6587 else
6588 outs() << "\n";
6589 outs() << " nundefsym " << dyst.nundefsym;
6590 big_size = dyst.iundefsym;
6591 big_size += dyst.nundefsym;
6592 if (big_size > nsyms)
6593 outs() << " (past the end of the symbol table)\n";
6594 else
6595 outs() << "\n";
6596 outs() << " tocoff " << dyst.tocoff;
6597 if (dyst.tocoff > object_size)
6598 outs() << " (past end of file)\n";
6599 else
6600 outs() << "\n";
6601 outs() << " ntoc " << dyst.ntoc;
6602 big_size = dyst.ntoc;
6603 big_size *= sizeof(struct MachO::dylib_table_of_contents);
6604 big_size += dyst.tocoff;
6605 if (big_size > object_size)
6606 outs() << " (past end of file)\n";
6607 else
6608 outs() << "\n";
6609 outs() << " modtaboff " << dyst.modtaboff;
6610 if (dyst.modtaboff > object_size)
6611 outs() << " (past end of file)\n";
6612 else
6613 outs() << "\n";
6614 outs() << " nmodtab " << dyst.nmodtab;
6615 uint64_t modtabend;
David Majnemer73cc6ff2014-11-13 19:48:56 +00006616 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00006617 modtabend = dyst.nmodtab;
6618 modtabend *= sizeof(struct MachO::dylib_module_64);
6619 modtabend += dyst.modtaboff;
6620 } else {
6621 modtabend = dyst.nmodtab;
6622 modtabend *= sizeof(struct MachO::dylib_module);
6623 modtabend += dyst.modtaboff;
6624 }
6625 if (modtabend > object_size)
6626 outs() << " (past end of file)\n";
6627 else
6628 outs() << "\n";
6629 outs() << " extrefsymoff " << dyst.extrefsymoff;
6630 if (dyst.extrefsymoff > object_size)
6631 outs() << " (past end of file)\n";
6632 else
6633 outs() << "\n";
6634 outs() << " nextrefsyms " << dyst.nextrefsyms;
6635 big_size = dyst.nextrefsyms;
6636 big_size *= sizeof(struct MachO::dylib_reference);
6637 big_size += dyst.extrefsymoff;
6638 if (big_size > object_size)
6639 outs() << " (past end of file)\n";
6640 else
6641 outs() << "\n";
6642 outs() << " indirectsymoff " << dyst.indirectsymoff;
6643 if (dyst.indirectsymoff > object_size)
6644 outs() << " (past end of file)\n";
6645 else
6646 outs() << "\n";
6647 outs() << " nindirectsyms " << dyst.nindirectsyms;
6648 big_size = dyst.nindirectsyms;
6649 big_size *= sizeof(uint32_t);
6650 big_size += dyst.indirectsymoff;
6651 if (big_size > object_size)
6652 outs() << " (past end of file)\n";
6653 else
6654 outs() << "\n";
6655 outs() << " extreloff " << dyst.extreloff;
6656 if (dyst.extreloff > object_size)
6657 outs() << " (past end of file)\n";
6658 else
6659 outs() << "\n";
6660 outs() << " nextrel " << dyst.nextrel;
6661 big_size = dyst.nextrel;
6662 big_size *= sizeof(struct MachO::relocation_info);
6663 big_size += dyst.extreloff;
6664 if (big_size > object_size)
6665 outs() << " (past end of file)\n";
6666 else
6667 outs() << "\n";
6668 outs() << " locreloff " << dyst.locreloff;
6669 if (dyst.locreloff > object_size)
6670 outs() << " (past end of file)\n";
6671 else
6672 outs() << "\n";
6673 outs() << " nlocrel " << dyst.nlocrel;
6674 big_size = dyst.nlocrel;
6675 big_size *= sizeof(struct MachO::relocation_info);
6676 big_size += dyst.locreloff;
6677 if (big_size > object_size)
6678 outs() << " (past end of file)\n";
6679 else
6680 outs() << "\n";
6681}
6682
Kevin Enderby8ae63c12014-09-04 16:54:47 +00006683static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
6684 uint32_t object_size) {
6685 if (dc.cmd == MachO::LC_DYLD_INFO)
6686 outs() << " cmd LC_DYLD_INFO\n";
6687 else
6688 outs() << " cmd LC_DYLD_INFO_ONLY\n";
6689 outs() << " cmdsize " << dc.cmdsize;
6690 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
6691 outs() << " Incorrect size\n";
6692 else
6693 outs() << "\n";
6694 outs() << " rebase_off " << dc.rebase_off;
6695 if (dc.rebase_off > object_size)
6696 outs() << " (past end of file)\n";
6697 else
6698 outs() << "\n";
6699 outs() << " rebase_size " << dc.rebase_size;
6700 uint64_t big_size;
6701 big_size = dc.rebase_off;
6702 big_size += dc.rebase_size;
6703 if (big_size > object_size)
6704 outs() << " (past end of file)\n";
6705 else
6706 outs() << "\n";
6707 outs() << " bind_off " << dc.bind_off;
6708 if (dc.bind_off > object_size)
6709 outs() << " (past end of file)\n";
6710 else
6711 outs() << "\n";
6712 outs() << " bind_size " << dc.bind_size;
6713 big_size = dc.bind_off;
6714 big_size += dc.bind_size;
6715 if (big_size > object_size)
6716 outs() << " (past end of file)\n";
6717 else
6718 outs() << "\n";
6719 outs() << " weak_bind_off " << dc.weak_bind_off;
6720 if (dc.weak_bind_off > object_size)
6721 outs() << " (past end of file)\n";
6722 else
6723 outs() << "\n";
6724 outs() << " weak_bind_size " << dc.weak_bind_size;
6725 big_size = dc.weak_bind_off;
6726 big_size += dc.weak_bind_size;
6727 if (big_size > object_size)
6728 outs() << " (past end of file)\n";
6729 else
6730 outs() << "\n";
6731 outs() << " lazy_bind_off " << dc.lazy_bind_off;
6732 if (dc.lazy_bind_off > object_size)
6733 outs() << " (past end of file)\n";
6734 else
6735 outs() << "\n";
6736 outs() << " lazy_bind_size " << dc.lazy_bind_size;
6737 big_size = dc.lazy_bind_off;
6738 big_size += dc.lazy_bind_size;
6739 if (big_size > object_size)
6740 outs() << " (past end of file)\n";
6741 else
6742 outs() << "\n";
6743 outs() << " export_off " << dc.export_off;
6744 if (dc.export_off > object_size)
6745 outs() << " (past end of file)\n";
6746 else
6747 outs() << "\n";
6748 outs() << " export_size " << dc.export_size;
6749 big_size = dc.export_off;
6750 big_size += dc.export_size;
6751 if (big_size > object_size)
6752 outs() << " (past end of file)\n";
6753 else
6754 outs() << "\n";
6755}
6756
6757static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
6758 const char *Ptr) {
6759 if (dyld.cmd == MachO::LC_ID_DYLINKER)
6760 outs() << " cmd LC_ID_DYLINKER\n";
6761 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
6762 outs() << " cmd LC_LOAD_DYLINKER\n";
6763 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
6764 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
6765 else
6766 outs() << " cmd ?(" << dyld.cmd << ")\n";
6767 outs() << " cmdsize " << dyld.cmdsize;
6768 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
6769 outs() << " Incorrect size\n";
6770 else
6771 outs() << "\n";
6772 if (dyld.name >= dyld.cmdsize)
6773 outs() << " name ?(bad offset " << dyld.name << ")\n";
6774 else {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006775 const char *P = (const char *)(Ptr) + dyld.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00006776 outs() << " name " << P << " (offset " << dyld.name << ")\n";
6777 }
6778}
6779
6780static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
6781 outs() << " cmd LC_UUID\n";
6782 outs() << " cmdsize " << uuid.cmdsize;
6783 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
6784 outs() << " Incorrect size\n";
6785 else
6786 outs() << "\n";
6787 outs() << " uuid ";
6788 outs() << format("%02" PRIX32, uuid.uuid[0]);
6789 outs() << format("%02" PRIX32, uuid.uuid[1]);
6790 outs() << format("%02" PRIX32, uuid.uuid[2]);
6791 outs() << format("%02" PRIX32, uuid.uuid[3]);
6792 outs() << "-";
6793 outs() << format("%02" PRIX32, uuid.uuid[4]);
6794 outs() << format("%02" PRIX32, uuid.uuid[5]);
6795 outs() << "-";
6796 outs() << format("%02" PRIX32, uuid.uuid[6]);
6797 outs() << format("%02" PRIX32, uuid.uuid[7]);
6798 outs() << "-";
6799 outs() << format("%02" PRIX32, uuid.uuid[8]);
6800 outs() << format("%02" PRIX32, uuid.uuid[9]);
6801 outs() << "-";
6802 outs() << format("%02" PRIX32, uuid.uuid[10]);
6803 outs() << format("%02" PRIX32, uuid.uuid[11]);
6804 outs() << format("%02" PRIX32, uuid.uuid[12]);
6805 outs() << format("%02" PRIX32, uuid.uuid[13]);
6806 outs() << format("%02" PRIX32, uuid.uuid[14]);
6807 outs() << format("%02" PRIX32, uuid.uuid[15]);
6808 outs() << "\n";
6809}
6810
Kevin Enderby66d51fc2015-01-08 00:25:24 +00006811static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00006812 outs() << " cmd LC_RPATH\n";
6813 outs() << " cmdsize " << rpath.cmdsize;
6814 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
6815 outs() << " Incorrect size\n";
6816 else
6817 outs() << "\n";
6818 if (rpath.path >= rpath.cmdsize)
6819 outs() << " path ?(bad offset " << rpath.path << ")\n";
6820 else {
6821 const char *P = (const char *)(Ptr) + rpath.path;
6822 outs() << " path " << P << " (offset " << rpath.path << ")\n";
6823 }
6824}
6825
Kevin Enderby8ae63c12014-09-04 16:54:47 +00006826static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
6827 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
6828 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
6829 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
6830 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
6831 else
6832 outs() << " cmd " << vd.cmd << " (?)\n";
6833 outs() << " cmdsize " << vd.cmdsize;
6834 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
6835 outs() << " Incorrect size\n";
6836 else
6837 outs() << "\n";
6838 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
6839 << ((vd.version >> 8) & 0xff);
6840 if ((vd.version & 0xff) != 0)
6841 outs() << "." << (vd.version & 0xff);
6842 outs() << "\n";
6843 if (vd.sdk == 0)
Kevin Enderby57538292014-12-17 01:01:30 +00006844 outs() << " sdk n/a";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00006845 else {
6846 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
6847 << ((vd.sdk >> 8) & 0xff);
6848 }
6849 if ((vd.sdk & 0xff) != 0)
6850 outs() << "." << (vd.sdk & 0xff);
6851 outs() << "\n";
6852}
6853
6854static void PrintSourceVersionCommand(MachO::source_version_command sd) {
6855 outs() << " cmd LC_SOURCE_VERSION\n";
6856 outs() << " cmdsize " << sd.cmdsize;
6857 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
6858 outs() << " Incorrect size\n";
6859 else
6860 outs() << "\n";
6861 uint64_t a = (sd.version >> 40) & 0xffffff;
6862 uint64_t b = (sd.version >> 30) & 0x3ff;
6863 uint64_t c = (sd.version >> 20) & 0x3ff;
6864 uint64_t d = (sd.version >> 10) & 0x3ff;
6865 uint64_t e = sd.version & 0x3ff;
6866 outs() << " version " << a << "." << b;
6867 if (e != 0)
6868 outs() << "." << c << "." << d << "." << e;
6869 else if (d != 0)
6870 outs() << "." << c << "." << d;
6871 else if (c != 0)
6872 outs() << "." << c;
6873 outs() << "\n";
6874}
6875
6876static void PrintEntryPointCommand(MachO::entry_point_command ep) {
6877 outs() << " cmd LC_MAIN\n";
6878 outs() << " cmdsize " << ep.cmdsize;
6879 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
6880 outs() << " Incorrect size\n";
6881 else
6882 outs() << "\n";
6883 outs() << " entryoff " << ep.entryoff << "\n";
6884 outs() << " stacksize " << ep.stacksize << "\n";
6885}
6886
Kevin Enderby0804f4672014-12-16 23:25:52 +00006887static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
6888 uint32_t object_size) {
6889 outs() << " cmd LC_ENCRYPTION_INFO\n";
6890 outs() << " cmdsize " << ec.cmdsize;
6891 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
6892 outs() << " Incorrect size\n";
6893 else
6894 outs() << "\n";
6895 outs() << " cryptoff " << ec.cryptoff;
6896 if (ec.cryptoff > object_size)
6897 outs() << " (past end of file)\n";
6898 else
6899 outs() << "\n";
6900 outs() << " cryptsize " << ec.cryptsize;
6901 if (ec.cryptsize > object_size)
6902 outs() << " (past end of file)\n";
6903 else
6904 outs() << "\n";
6905 outs() << " cryptid " << ec.cryptid << "\n";
6906}
6907
Kevin Enderby57538292014-12-17 01:01:30 +00006908static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00006909 uint32_t object_size) {
Kevin Enderby57538292014-12-17 01:01:30 +00006910 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
6911 outs() << " cmdsize " << ec.cmdsize;
6912 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
6913 outs() << " Incorrect size\n";
6914 else
6915 outs() << "\n";
6916 outs() << " cryptoff " << ec.cryptoff;
6917 if (ec.cryptoff > object_size)
6918 outs() << " (past end of file)\n";
6919 else
6920 outs() << "\n";
6921 outs() << " cryptsize " << ec.cryptsize;
6922 if (ec.cryptsize > object_size)
6923 outs() << " (past end of file)\n";
6924 else
6925 outs() << "\n";
6926 outs() << " cryptid " << ec.cryptid << "\n";
6927 outs() << " pad " << ec.pad << "\n";
6928}
6929
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00006930static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
6931 const char *Ptr) {
6932 outs() << " cmd LC_LINKER_OPTION\n";
6933 outs() << " cmdsize " << lo.cmdsize;
6934 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
6935 outs() << " Incorrect size\n";
6936 else
6937 outs() << "\n";
6938 outs() << " count " << lo.count << "\n";
6939 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
6940 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
6941 uint32_t i = 0;
6942 while (left > 0) {
6943 while (*string == '\0' && left > 0) {
6944 string++;
6945 left--;
6946 }
6947 if (left > 0) {
6948 i++;
6949 outs() << " string #" << i << " " << format("%.*s\n", left, string);
David Majnemerd4449ed2014-12-20 08:24:43 +00006950 uint32_t NullPos = StringRef(string, left).find('\0');
6951 uint32_t len = std::min(NullPos, left) + 1;
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00006952 string += len;
6953 left -= len;
6954 }
6955 }
6956 if (lo.count != i)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00006957 outs() << " count " << lo.count << " does not match number of strings "
6958 << i << "\n";
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00006959}
6960
Kevin Enderbyb4b79312014-12-18 19:24:35 +00006961static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
6962 const char *Ptr) {
6963 outs() << " cmd LC_SUB_FRAMEWORK\n";
6964 outs() << " cmdsize " << sub.cmdsize;
6965 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
6966 outs() << " Incorrect size\n";
6967 else
6968 outs() << "\n";
6969 if (sub.umbrella < sub.cmdsize) {
6970 const char *P = Ptr + sub.umbrella;
6971 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
6972 } else {
6973 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
6974 }
6975}
6976
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00006977static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
6978 const char *Ptr) {
6979 outs() << " cmd LC_SUB_UMBRELLA\n";
6980 outs() << " cmdsize " << sub.cmdsize;
6981 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
6982 outs() << " Incorrect size\n";
6983 else
6984 outs() << "\n";
6985 if (sub.sub_umbrella < sub.cmdsize) {
6986 const char *P = Ptr + sub.sub_umbrella;
6987 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
6988 } else {
6989 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
6990 }
6991}
6992
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00006993static void PrintSubLibraryCommand(MachO::sub_library_command sub,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00006994 const char *Ptr) {
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00006995 outs() << " cmd LC_SUB_LIBRARY\n";
6996 outs() << " cmdsize " << sub.cmdsize;
6997 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
6998 outs() << " Incorrect size\n";
6999 else
7000 outs() << "\n";
7001 if (sub.sub_library < sub.cmdsize) {
7002 const char *P = Ptr + sub.sub_library;
7003 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7004 } else {
7005 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7006 }
7007}
7008
Kevin Enderby186eac32014-12-19 21:06:24 +00007009static void PrintSubClientCommand(MachO::sub_client_command sub,
7010 const char *Ptr) {
7011 outs() << " cmd LC_SUB_CLIENT\n";
7012 outs() << " cmdsize " << sub.cmdsize;
7013 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7014 outs() << " Incorrect size\n";
7015 else
7016 outs() << "\n";
7017 if (sub.client < sub.cmdsize) {
7018 const char *P = Ptr + sub.client;
7019 outs() << " client " << P << " (offset " << sub.client << ")\n";
7020 } else {
7021 outs() << " client ?(bad offset " << sub.client << ")\n";
7022 }
7023}
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007024
Kevin Enderby52e4ce42014-12-19 22:25:22 +00007025static void PrintRoutinesCommand(MachO::routines_command r) {
7026 outs() << " cmd LC_ROUTINES\n";
7027 outs() << " cmdsize " << r.cmdsize;
7028 if (r.cmdsize != sizeof(struct MachO::routines_command))
7029 outs() << " Incorrect size\n";
7030 else
7031 outs() << "\n";
7032 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7033 outs() << " init_module " << r.init_module << "\n";
7034 outs() << " reserved1 " << r.reserved1 << "\n";
7035 outs() << " reserved2 " << r.reserved2 << "\n";
7036 outs() << " reserved3 " << r.reserved3 << "\n";
7037 outs() << " reserved4 " << r.reserved4 << "\n";
7038 outs() << " reserved5 " << r.reserved5 << "\n";
7039 outs() << " reserved6 " << r.reserved6 << "\n";
7040}
7041
7042static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7043 outs() << " cmd LC_ROUTINES_64\n";
7044 outs() << " cmdsize " << r.cmdsize;
7045 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7046 outs() << " Incorrect size\n";
7047 else
7048 outs() << "\n";
7049 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
7050 outs() << " init_module " << r.init_module << "\n";
7051 outs() << " reserved1 " << r.reserved1 << "\n";
7052 outs() << " reserved2 " << r.reserved2 << "\n";
7053 outs() << " reserved3 " << r.reserved3 << "\n";
7054 outs() << " reserved4 " << r.reserved4 << "\n";
7055 outs() << " reserved5 " << r.reserved5 << "\n";
7056 outs() << " reserved6 " << r.reserved6 << "\n";
7057}
7058
Kevin Enderby48ef5342014-12-23 22:56:39 +00007059static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7060 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7061 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7062 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7063 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7064 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7065 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7066 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7067 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7068 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7069 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7070 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7071 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7072 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7073 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7074 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7075 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7076 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7077 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7078 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7079 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7080 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7081}
7082
Kevin Enderby227df342014-12-23 23:43:59 +00007083static void Print_mmst_reg(MachO::mmst_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007084 uint32_t f;
7085 outs() << "\t mmst_reg ";
7086 for (f = 0; f < 10; f++)
7087 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7088 outs() << "\n";
7089 outs() << "\t mmst_rsrv ";
7090 for (f = 0; f < 6; f++)
7091 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7092 outs() << "\n";
7093}
7094
Kevin Enderbyaefb0032014-12-24 00:16:51 +00007095static void Print_xmm_reg(MachO::xmm_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007096 uint32_t f;
7097 outs() << "\t xmm_reg ";
7098 for (f = 0; f < 16; f++)
7099 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7100 outs() << "\n";
7101}
7102
7103static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7104 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7105 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7106 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7107 outs() << " denorm " << fpu.fpu_fcw.denorm;
7108 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7109 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7110 outs() << " undfl " << fpu.fpu_fcw.undfl;
7111 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7112 outs() << "\t\t pc ";
7113 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7114 outs() << "FP_PREC_24B ";
7115 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
7116 outs() << "FP_PREC_53B ";
7117 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
7118 outs() << "FP_PREC_64B ";
7119 else
7120 outs() << fpu.fpu_fcw.pc << " ";
7121 outs() << "rc ";
7122 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
7123 outs() << "FP_RND_NEAR ";
7124 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
7125 outs() << "FP_RND_DOWN ";
7126 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
7127 outs() << "FP_RND_UP ";
7128 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007129 outs() << "FP_CHOP ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00007130 outs() << "\n";
7131 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
7132 outs() << " denorm " << fpu.fpu_fsw.denorm;
7133 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
7134 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
7135 outs() << " undfl " << fpu.fpu_fsw.undfl;
7136 outs() << " precis " << fpu.fpu_fsw.precis;
7137 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
7138 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
7139 outs() << " c0 " << fpu.fpu_fsw.c0;
7140 outs() << " c1 " << fpu.fpu_fsw.c1;
7141 outs() << " c2 " << fpu.fpu_fsw.c2;
7142 outs() << " tos " << fpu.fpu_fsw.tos;
7143 outs() << " c3 " << fpu.fpu_fsw.c3;
7144 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
7145 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
7146 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
7147 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
7148 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
7149 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
7150 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
7151 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
7152 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
7153 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
7154 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
7155 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
7156 outs() << "\n";
7157 outs() << "\t fpu_stmm0:\n";
7158 Print_mmst_reg(fpu.fpu_stmm0);
7159 outs() << "\t fpu_stmm1:\n";
7160 Print_mmst_reg(fpu.fpu_stmm1);
7161 outs() << "\t fpu_stmm2:\n";
7162 Print_mmst_reg(fpu.fpu_stmm2);
7163 outs() << "\t fpu_stmm3:\n";
7164 Print_mmst_reg(fpu.fpu_stmm3);
7165 outs() << "\t fpu_stmm4:\n";
7166 Print_mmst_reg(fpu.fpu_stmm4);
7167 outs() << "\t fpu_stmm5:\n";
7168 Print_mmst_reg(fpu.fpu_stmm5);
7169 outs() << "\t fpu_stmm6:\n";
7170 Print_mmst_reg(fpu.fpu_stmm6);
7171 outs() << "\t fpu_stmm7:\n";
7172 Print_mmst_reg(fpu.fpu_stmm7);
7173 outs() << "\t fpu_xmm0:\n";
7174 Print_xmm_reg(fpu.fpu_xmm0);
7175 outs() << "\t fpu_xmm1:\n";
7176 Print_xmm_reg(fpu.fpu_xmm1);
7177 outs() << "\t fpu_xmm2:\n";
7178 Print_xmm_reg(fpu.fpu_xmm2);
7179 outs() << "\t fpu_xmm3:\n";
7180 Print_xmm_reg(fpu.fpu_xmm3);
7181 outs() << "\t fpu_xmm4:\n";
7182 Print_xmm_reg(fpu.fpu_xmm4);
7183 outs() << "\t fpu_xmm5:\n";
7184 Print_xmm_reg(fpu.fpu_xmm5);
7185 outs() << "\t fpu_xmm6:\n";
7186 Print_xmm_reg(fpu.fpu_xmm6);
7187 outs() << "\t fpu_xmm7:\n";
7188 Print_xmm_reg(fpu.fpu_xmm7);
7189 outs() << "\t fpu_xmm8:\n";
7190 Print_xmm_reg(fpu.fpu_xmm8);
7191 outs() << "\t fpu_xmm9:\n";
7192 Print_xmm_reg(fpu.fpu_xmm9);
7193 outs() << "\t fpu_xmm10:\n";
7194 Print_xmm_reg(fpu.fpu_xmm10);
7195 outs() << "\t fpu_xmm11:\n";
7196 Print_xmm_reg(fpu.fpu_xmm11);
7197 outs() << "\t fpu_xmm12:\n";
7198 Print_xmm_reg(fpu.fpu_xmm12);
7199 outs() << "\t fpu_xmm13:\n";
7200 Print_xmm_reg(fpu.fpu_xmm13);
7201 outs() << "\t fpu_xmm14:\n";
7202 Print_xmm_reg(fpu.fpu_xmm14);
7203 outs() << "\t fpu_xmm15:\n";
7204 Print_xmm_reg(fpu.fpu_xmm15);
7205 outs() << "\t fpu_rsrv4:\n";
7206 for (uint32_t f = 0; f < 6; f++) {
7207 outs() << "\t ";
7208 for (uint32_t g = 0; g < 16; g++)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007209 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00007210 outs() << "\n";
7211 }
7212 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
7213 outs() << "\n";
7214}
7215
7216static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
7217 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
7218 outs() << " err " << format("0x%08" PRIx32, exc64.err);
7219 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
7220}
7221
7222static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
7223 bool isLittleEndian, uint32_t cputype) {
7224 if (t.cmd == MachO::LC_THREAD)
7225 outs() << " cmd LC_THREAD\n";
7226 else if (t.cmd == MachO::LC_UNIXTHREAD)
7227 outs() << " cmd LC_UNIXTHREAD\n";
7228 else
7229 outs() << " cmd " << t.cmd << " (unknown)\n";
7230 outs() << " cmdsize " << t.cmdsize;
7231 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
7232 outs() << " Incorrect size\n";
7233 else
7234 outs() << "\n";
7235
7236 const char *begin = Ptr + sizeof(struct MachO::thread_command);
7237 const char *end = Ptr + t.cmdsize;
7238 uint32_t flavor, count, left;
7239 if (cputype == MachO::CPU_TYPE_X86_64) {
7240 while (begin < end) {
7241 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
7242 memcpy((char *)&flavor, begin, sizeof(uint32_t));
7243 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007244 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007245 flavor = 0;
7246 begin = end;
7247 }
7248 if (isLittleEndian != sys::IsLittleEndianHost)
7249 sys::swapByteOrder(flavor);
7250 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
7251 memcpy((char *)&count, begin, sizeof(uint32_t));
7252 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007253 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007254 count = 0;
7255 begin = end;
7256 }
7257 if (isLittleEndian != sys::IsLittleEndianHost)
7258 sys::swapByteOrder(count);
7259 if (flavor == MachO::x86_THREAD_STATE64) {
7260 outs() << " flavor x86_THREAD_STATE64\n";
7261 if (count == MachO::x86_THREAD_STATE64_COUNT)
7262 outs() << " count x86_THREAD_STATE64_COUNT\n";
7263 else
7264 outs() << " count " << count
7265 << " (not x86_THREAD_STATE64_COUNT)\n";
7266 MachO::x86_thread_state64_t cpu64;
7267 left = end - begin;
7268 if (left >= sizeof(MachO::x86_thread_state64_t)) {
7269 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
7270 begin += sizeof(MachO::x86_thread_state64_t);
7271 } else {
7272 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
7273 memcpy(&cpu64, begin, left);
7274 begin += left;
7275 }
7276 if (isLittleEndian != sys::IsLittleEndianHost)
7277 swapStruct(cpu64);
7278 Print_x86_thread_state64_t(cpu64);
7279 } else if (flavor == MachO::x86_THREAD_STATE) {
7280 outs() << " flavor x86_THREAD_STATE\n";
7281 if (count == MachO::x86_THREAD_STATE_COUNT)
7282 outs() << " count x86_THREAD_STATE_COUNT\n";
7283 else
7284 outs() << " count " << count
7285 << " (not x86_THREAD_STATE_COUNT)\n";
7286 struct MachO::x86_thread_state_t ts;
7287 left = end - begin;
7288 if (left >= sizeof(MachO::x86_thread_state_t)) {
7289 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
7290 begin += sizeof(MachO::x86_thread_state_t);
7291 } else {
7292 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
7293 memcpy(&ts, begin, left);
7294 begin += left;
7295 }
7296 if (isLittleEndian != sys::IsLittleEndianHost)
7297 swapStruct(ts);
7298 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
7299 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
7300 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
7301 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
7302 else
7303 outs() << "tsh.count " << ts.tsh.count
7304 << " (not x86_THREAD_STATE64_COUNT\n";
7305 Print_x86_thread_state64_t(ts.uts.ts64);
7306 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007307 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
7308 << ts.tsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00007309 }
7310 } else if (flavor == MachO::x86_FLOAT_STATE) {
7311 outs() << " flavor x86_FLOAT_STATE\n";
7312 if (count == MachO::x86_FLOAT_STATE_COUNT)
7313 outs() << " count x86_FLOAT_STATE_COUNT\n";
7314 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007315 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00007316 struct MachO::x86_float_state_t fs;
7317 left = end - begin;
7318 if (left >= sizeof(MachO::x86_float_state_t)) {
7319 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
7320 begin += sizeof(MachO::x86_float_state_t);
7321 } else {
7322 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
7323 memcpy(&fs, begin, left);
7324 begin += left;
7325 }
7326 if (isLittleEndian != sys::IsLittleEndianHost)
7327 swapStruct(fs);
7328 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
7329 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007330 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00007331 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
7332 else
7333 outs() << "fsh.count " << fs.fsh.count
7334 << " (not x86_FLOAT_STATE64_COUNT\n";
7335 Print_x86_float_state_t(fs.ufs.fs64);
7336 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007337 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
7338 << fs.fsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00007339 }
7340 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
7341 outs() << " flavor x86_EXCEPTION_STATE\n";
7342 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
7343 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
7344 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007345 outs() << " count " << count
Kevin Enderby48ef5342014-12-23 22:56:39 +00007346 << " (not x86_EXCEPTION_STATE_COUNT)\n";
7347 struct MachO::x86_exception_state_t es;
7348 left = end - begin;
7349 if (left >= sizeof(MachO::x86_exception_state_t)) {
7350 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
7351 begin += sizeof(MachO::x86_exception_state_t);
7352 } else {
7353 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
7354 memcpy(&es, begin, left);
7355 begin += left;
7356 }
7357 if (isLittleEndian != sys::IsLittleEndianHost)
7358 swapStruct(es);
7359 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
7360 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007361 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00007362 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
7363 else
7364 outs() << "\t esh.count " << es.esh.count
7365 << " (not x86_EXCEPTION_STATE64_COUNT\n";
7366 Print_x86_exception_state_t(es.ues.es64);
7367 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007368 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
7369 << es.esh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00007370 }
7371 } else {
7372 outs() << " flavor " << flavor << " (unknown)\n";
7373 outs() << " count " << count << "\n";
7374 outs() << " state (unknown)\n";
7375 begin += count * sizeof(uint32_t);
7376 }
7377 }
7378 } else {
7379 while (begin < end) {
7380 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
7381 memcpy((char *)&flavor, begin, sizeof(uint32_t));
7382 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007383 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007384 flavor = 0;
7385 begin = end;
7386 }
7387 if (isLittleEndian != sys::IsLittleEndianHost)
7388 sys::swapByteOrder(flavor);
7389 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
7390 memcpy((char *)&count, begin, sizeof(uint32_t));
7391 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007392 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007393 count = 0;
7394 begin = end;
7395 }
7396 if (isLittleEndian != sys::IsLittleEndianHost)
7397 sys::swapByteOrder(count);
7398 outs() << " flavor " << flavor << "\n";
7399 outs() << " count " << count << "\n";
7400 outs() << " state (Unknown cputype/cpusubtype)\n";
7401 begin += count * sizeof(uint32_t);
7402 }
7403 }
7404}
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007405
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007406static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
7407 if (dl.cmd == MachO::LC_ID_DYLIB)
7408 outs() << " cmd LC_ID_DYLIB\n";
7409 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
7410 outs() << " cmd LC_LOAD_DYLIB\n";
7411 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
7412 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
7413 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
7414 outs() << " cmd LC_REEXPORT_DYLIB\n";
7415 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
7416 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
7417 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
7418 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
7419 else
7420 outs() << " cmd " << dl.cmd << " (unknown)\n";
7421 outs() << " cmdsize " << dl.cmdsize;
7422 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
7423 outs() << " Incorrect size\n";
7424 else
7425 outs() << "\n";
7426 if (dl.dylib.name < dl.cmdsize) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007427 const char *P = (const char *)(Ptr) + dl.dylib.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007428 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
7429 } else {
7430 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
7431 }
7432 outs() << " time stamp " << dl.dylib.timestamp << " ";
7433 time_t t = dl.dylib.timestamp;
7434 outs() << ctime(&t);
7435 outs() << " current version ";
7436 if (dl.dylib.current_version == 0xffffffff)
7437 outs() << "n/a\n";
7438 else
7439 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
7440 << ((dl.dylib.current_version >> 8) & 0xff) << "."
7441 << (dl.dylib.current_version & 0xff) << "\n";
7442 outs() << "compatibility version ";
7443 if (dl.dylib.compatibility_version == 0xffffffff)
7444 outs() << "n/a\n";
7445 else
7446 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
7447 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
7448 << (dl.dylib.compatibility_version & 0xff) << "\n";
7449}
7450
7451static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
7452 uint32_t object_size) {
7453 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
7454 outs() << " cmd LC_FUNCTION_STARTS\n";
7455 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
7456 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
7457 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
7458 outs() << " cmd LC_FUNCTION_STARTS\n";
7459 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
7460 outs() << " cmd LC_DATA_IN_CODE\n";
7461 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
7462 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
7463 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
7464 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
7465 else
7466 outs() << " cmd " << ld.cmd << " (?)\n";
7467 outs() << " cmdsize " << ld.cmdsize;
7468 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
7469 outs() << " Incorrect size\n";
7470 else
7471 outs() << "\n";
7472 outs() << " dataoff " << ld.dataoff;
7473 if (ld.dataoff > object_size)
7474 outs() << " (past end of file)\n";
7475 else
7476 outs() << "\n";
7477 outs() << " datasize " << ld.datasize;
7478 uint64_t big_size = ld.dataoff;
7479 big_size += ld.datasize;
7480 if (big_size > object_size)
7481 outs() << " (past end of file)\n";
7482 else
7483 outs() << "\n";
7484}
7485
Kevin Enderby956366c2014-08-29 22:30:52 +00007486static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t ncmds,
7487 uint32_t filetype, uint32_t cputype,
7488 bool verbose) {
Filipe Cabecinhase71bd0c2015-01-06 17:08:26 +00007489 if (ncmds == 0)
7490 return;
Kevin Enderby956366c2014-08-29 22:30:52 +00007491 StringRef Buf = Obj->getData();
7492 MachOObjectFile::LoadCommandInfo Command = Obj->getFirstLoadCommandInfo();
7493 for (unsigned i = 0;; ++i) {
7494 outs() << "Load command " << i << "\n";
7495 if (Command.C.cmd == MachO::LC_SEGMENT) {
7496 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
7497 const char *sg_segname = SLC.segname;
7498 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
7499 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
7500 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
7501 verbose);
7502 for (unsigned j = 0; j < SLC.nsects; j++) {
Kevin Enderbyc9713382014-12-16 01:14:45 +00007503 MachO::section S = Obj->getSection(Command, j);
Kevin Enderby956366c2014-08-29 22:30:52 +00007504 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
7505 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
7506 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
7507 }
7508 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
7509 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
7510 const char *sg_segname = SLC_64.segname;
7511 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
7512 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
7513 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
7514 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
7515 for (unsigned j = 0; j < SLC_64.nsects; j++) {
7516 MachO::section_64 S_64 = Obj->getSection64(Command, j);
7517 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
7518 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
7519 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
7520 sg_segname, filetype, Buf.size(), verbose);
7521 }
7522 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
7523 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00007524 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00007525 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
7526 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
7527 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00007528 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
7529 Obj->is64Bit());
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007530 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
7531 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
7532 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
7533 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
7534 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
7535 Command.C.cmd == MachO::LC_ID_DYLINKER ||
7536 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
7537 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
7538 PrintDyldLoadCommand(Dyld, Command.Ptr);
7539 } else if (Command.C.cmd == MachO::LC_UUID) {
7540 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
7541 PrintUuidLoadCommand(Uuid);
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00007542 } else if (Command.C.cmd == MachO::LC_RPATH) {
7543 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
7544 PrintRpathLoadCommand(Rpath, Command.Ptr);
Kevin Enderby1ff0ecc2014-12-16 21:48:27 +00007545 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
7546 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007547 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
7548 PrintVersionMinLoadCommand(Vd);
7549 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
7550 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
7551 PrintSourceVersionCommand(Sd);
7552 } else if (Command.C.cmd == MachO::LC_MAIN) {
7553 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
7554 PrintEntryPointCommand(Ep);
Kevin Enderby0804f4672014-12-16 23:25:52 +00007555 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007556 MachO::encryption_info_command Ei =
7557 Obj->getEncryptionInfoCommand(Command);
Kevin Enderby0804f4672014-12-16 23:25:52 +00007558 PrintEncryptionInfoCommand(Ei, Buf.size());
Kevin Enderby57538292014-12-17 01:01:30 +00007559 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007560 MachO::encryption_info_command_64 Ei =
7561 Obj->getEncryptionInfoCommand64(Command);
Kevin Enderby57538292014-12-17 01:01:30 +00007562 PrintEncryptionInfoCommand64(Ei, Buf.size());
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007563 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007564 MachO::linker_option_command Lo =
7565 Obj->getLinkerOptionLoadCommand(Command);
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007566 PrintLinkerOptionCommand(Lo, Command.Ptr);
Kevin Enderbyb4b79312014-12-18 19:24:35 +00007567 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
7568 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
7569 PrintSubFrameworkCommand(Sf, Command.Ptr);
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00007570 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
7571 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
7572 PrintSubUmbrellaCommand(Sf, Command.Ptr);
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007573 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
7574 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
7575 PrintSubLibraryCommand(Sl, Command.Ptr);
Kevin Enderby186eac32014-12-19 21:06:24 +00007576 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
7577 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
7578 PrintSubClientCommand(Sc, Command.Ptr);
Kevin Enderby52e4ce42014-12-19 22:25:22 +00007579 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
7580 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
7581 PrintRoutinesCommand(Rc);
7582 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
7583 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
7584 PrintRoutinesCommand64(Rc);
Kevin Enderby48ef5342014-12-23 22:56:39 +00007585 } else if (Command.C.cmd == MachO::LC_THREAD ||
7586 Command.C.cmd == MachO::LC_UNIXTHREAD) {
7587 MachO::thread_command Tc = Obj->getThreadCommand(Command);
7588 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
Nick Kledzik15558912014-10-16 18:58:20 +00007589 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
7590 Command.C.cmd == MachO::LC_ID_DYLIB ||
7591 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
7592 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
7593 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
7594 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007595 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
7596 PrintDylibCommand(Dl, Command.Ptr);
7597 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
7598 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
7599 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
7600 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
7601 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
7602 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
7603 MachO::linkedit_data_command Ld =
7604 Obj->getLinkeditDataLoadCommand(Command);
7605 PrintLinkEditDataCommand(Ld, Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00007606 } else {
7607 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
7608 << ")\n";
7609 outs() << " cmdsize " << Command.C.cmdsize << "\n";
7610 // TODO: get and print the raw bytes of the load command.
7611 }
7612 // TODO: print all the other kinds of load commands.
7613 if (i == ncmds - 1)
7614 break;
7615 else
7616 Command = Obj->getNextLoadCommandInfo(Command);
7617 }
7618}
7619
7620static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds,
7621 uint32_t &filetype, uint32_t &cputype,
7622 bool verbose) {
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007623 if (Obj->is64Bit()) {
7624 MachO::mach_header_64 H_64;
7625 H_64 = Obj->getHeader64();
7626 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
7627 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00007628 ncmds = H_64.ncmds;
7629 filetype = H_64.filetype;
7630 cputype = H_64.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007631 } else {
7632 MachO::mach_header H;
7633 H = Obj->getHeader();
7634 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
7635 H.sizeofcmds, H.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00007636 ncmds = H.ncmds;
7637 filetype = H.filetype;
7638 cputype = H.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007639 }
7640}
7641
7642void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
7643 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby956366c2014-08-29 22:30:52 +00007644 uint32_t ncmds = 0;
7645 uint32_t filetype = 0;
7646 uint32_t cputype = 0;
Kevin Enderbyf0640752015-03-13 17:56:32 +00007647 getAndPrintMachHeader(file, ncmds, filetype, cputype, !NonVerbose);
7648 PrintLoadCommands(file, ncmds, filetype, cputype, !NonVerbose);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007649}
Nick Kledzikd04bc352014-08-30 00:20:14 +00007650
7651//===----------------------------------------------------------------------===//
7652// export trie dumping
7653//===----------------------------------------------------------------------===//
7654
7655void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00007656 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
7657 uint64_t Flags = Entry.flags();
Nick Kledzikd04bc352014-08-30 00:20:14 +00007658 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
7659 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
7660 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
7661 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
7662 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
7663 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
7664 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
7665 if (ReExport)
7666 outs() << "[re-export] ";
7667 else
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007668 outs() << format("0x%08llX ",
7669 Entry.address()); // FIXME:add in base address
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00007670 outs() << Entry.name();
Nick Kledzikd04bc352014-08-30 00:20:14 +00007671 if (WeakDef || ThreadLocal || Resolver || Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00007672 bool NeedsComma = false;
Nick Kledzik1d1ac4b2014-09-03 01:12:52 +00007673 outs() << " [";
Nick Kledzikd04bc352014-08-30 00:20:14 +00007674 if (WeakDef) {
7675 outs() << "weak_def";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00007676 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00007677 }
7678 if (ThreadLocal) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00007679 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00007680 outs() << ", ";
7681 outs() << "per-thread";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00007682 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00007683 }
7684 if (Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00007685 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00007686 outs() << ", ";
7687 outs() << "absolute";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00007688 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00007689 }
7690 if (Resolver) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00007691 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00007692 outs() << ", ";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00007693 outs() << format("resolver=0x%08llX", Entry.other());
7694 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00007695 }
7696 outs() << "]";
7697 }
7698 if (ReExport) {
7699 StringRef DylibName = "unknown";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00007700 int Ordinal = Entry.other() - 1;
7701 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
7702 if (Entry.otherName().empty())
Nick Kledzikd04bc352014-08-30 00:20:14 +00007703 outs() << " (from " << DylibName << ")";
7704 else
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00007705 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
Nick Kledzikd04bc352014-08-30 00:20:14 +00007706 }
7707 outs() << "\n";
7708 }
7709}
Nick Kledzikac431442014-09-12 21:34:15 +00007710
Nick Kledzikac431442014-09-12 21:34:15 +00007711//===----------------------------------------------------------------------===//
7712// rebase table dumping
7713//===----------------------------------------------------------------------===//
7714
7715namespace {
7716class SegInfo {
7717public:
7718 SegInfo(const object::MachOObjectFile *Obj);
7719
7720 StringRef segmentName(uint32_t SegIndex);
7721 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
7722 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
7723
7724private:
7725 struct SectionInfo {
7726 uint64_t Address;
7727 uint64_t Size;
7728 StringRef SectionName;
7729 StringRef SegmentName;
7730 uint64_t OffsetInSegment;
7731 uint64_t SegmentStartAddress;
7732 uint32_t SegmentIndex;
7733 };
7734 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
7735 SmallVector<SectionInfo, 32> Sections;
7736};
7737}
7738
7739SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
7740 // Build table of sections so segIndex/offset pairs can be translated.
Nick Kledzik56ebef42014-09-16 01:41:51 +00007741 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
Nick Kledzikac431442014-09-12 21:34:15 +00007742 StringRef CurSegName;
7743 uint64_t CurSegAddress;
7744 for (const SectionRef &Section : Obj->sections()) {
7745 SectionInfo Info;
7746 if (error(Section.getName(Info.SectionName)))
7747 return;
Rafael Espindola80291272014-10-08 15:28:58 +00007748 Info.Address = Section.getAddress();
7749 Info.Size = Section.getSize();
Nick Kledzikac431442014-09-12 21:34:15 +00007750 Info.SegmentName =
7751 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
7752 if (!Info.SegmentName.equals(CurSegName)) {
7753 ++CurSegIndex;
7754 CurSegName = Info.SegmentName;
7755 CurSegAddress = Info.Address;
7756 }
7757 Info.SegmentIndex = CurSegIndex - 1;
7758 Info.OffsetInSegment = Info.Address - CurSegAddress;
7759 Info.SegmentStartAddress = CurSegAddress;
7760 Sections.push_back(Info);
7761 }
7762}
7763
7764StringRef SegInfo::segmentName(uint32_t SegIndex) {
7765 for (const SectionInfo &SI : Sections) {
7766 if (SI.SegmentIndex == SegIndex)
7767 return SI.SegmentName;
7768 }
7769 llvm_unreachable("invalid segIndex");
7770}
7771
7772const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
7773 uint64_t OffsetInSeg) {
7774 for (const SectionInfo &SI : Sections) {
7775 if (SI.SegmentIndex != SegIndex)
7776 continue;
7777 if (SI.OffsetInSegment > OffsetInSeg)
7778 continue;
7779 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
7780 continue;
7781 return SI;
7782 }
7783 llvm_unreachable("segIndex and offset not in any section");
7784}
7785
7786StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
7787 return findSection(SegIndex, OffsetInSeg).SectionName;
7788}
7789
7790uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
7791 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
7792 return SI.SegmentStartAddress + OffsetInSeg;
7793}
7794
7795void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
7796 // Build table of sections so names can used in final output.
7797 SegInfo sectionTable(Obj);
7798
7799 outs() << "segment section address type\n";
7800 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
7801 uint32_t SegIndex = Entry.segmentIndex();
7802 uint64_t OffsetInSeg = Entry.segmentOffset();
7803 StringRef SegmentName = sectionTable.segmentName(SegIndex);
7804 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
7805 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
7806
7807 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007808 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
7809 SegmentName.str().c_str(), SectionName.str().c_str(),
7810 Address, Entry.typeName().str().c_str());
Nick Kledzikac431442014-09-12 21:34:15 +00007811 }
7812}
Nick Kledzik56ebef42014-09-16 01:41:51 +00007813
7814static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
7815 StringRef DylibName;
7816 switch (Ordinal) {
7817 case MachO::BIND_SPECIAL_DYLIB_SELF:
7818 return "this-image";
7819 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
7820 return "main-executable";
7821 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
7822 return "flat-namespace";
7823 default:
Nick Kledzikabd29872014-09-16 22:03:13 +00007824 if (Ordinal > 0) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007825 std::error_code EC =
7826 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
Nick Kledzikabd29872014-09-16 22:03:13 +00007827 if (EC)
Nick Kledzik51d2c2b2014-10-14 23:29:38 +00007828 return "<<bad library ordinal>>";
Nick Kledzikabd29872014-09-16 22:03:13 +00007829 return DylibName;
7830 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00007831 }
Nick Kledzikabd29872014-09-16 22:03:13 +00007832 return "<<unknown special ordinal>>";
Nick Kledzik56ebef42014-09-16 01:41:51 +00007833}
7834
7835//===----------------------------------------------------------------------===//
7836// bind table dumping
7837//===----------------------------------------------------------------------===//
7838
7839void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
7840 // Build table of sections so names can used in final output.
7841 SegInfo sectionTable(Obj);
7842
Nick Kledzik5ffacc12014-09-30 00:19:58 +00007843 outs() << "segment section address type "
7844 "addend dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00007845 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
7846 uint32_t SegIndex = Entry.segmentIndex();
7847 uint64_t OffsetInSeg = Entry.segmentOffset();
7848 StringRef SegmentName = sectionTable.segmentName(SegIndex);
7849 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
7850 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
7851
7852 // Table lines look like:
7853 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
Nick Kledzik5ffacc12014-09-30 00:19:58 +00007854 StringRef Attr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00007855 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
Nick Kledzik5ffacc12014-09-30 00:19:58 +00007856 Attr = " (weak_import)";
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007857 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00007858 << left_justify(SectionName, 18) << " "
7859 << format_hex(Address, 10, true) << " "
7860 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007861 << format_decimal(Entry.addend(), 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00007862 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007863 << Entry.symbolName() << Attr << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00007864 }
7865}
7866
7867//===----------------------------------------------------------------------===//
7868// lazy bind table dumping
7869//===----------------------------------------------------------------------===//
7870
7871void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
7872 // Build table of sections so names can used in final output.
7873 SegInfo sectionTable(Obj);
7874
Nick Kledzik5ffacc12014-09-30 00:19:58 +00007875 outs() << "segment section address "
7876 "dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00007877 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
7878 uint32_t SegIndex = Entry.segmentIndex();
7879 uint64_t OffsetInSeg = Entry.segmentOffset();
7880 StringRef SegmentName = sectionTable.segmentName(SegIndex);
7881 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
7882 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
7883
7884 // Table lines look like:
7885 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007886 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00007887 << left_justify(SectionName, 18) << " "
7888 << format_hex(Address, 10, true) << " "
7889 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Nick Kledzik56ebef42014-09-16 01:41:51 +00007890 << Entry.symbolName() << "\n";
7891 }
7892}
7893
Nick Kledzik56ebef42014-09-16 01:41:51 +00007894//===----------------------------------------------------------------------===//
7895// weak bind table dumping
7896//===----------------------------------------------------------------------===//
7897
7898void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
7899 // Build table of sections so names can used in final output.
7900 SegInfo sectionTable(Obj);
7901
Nick Kledzik5ffacc12014-09-30 00:19:58 +00007902 outs() << "segment section address "
7903 "type addend symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00007904 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
7905 // Strong symbols don't have a location to update.
7906 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00007907 outs() << " strong "
Nick Kledzik56ebef42014-09-16 01:41:51 +00007908 << Entry.symbolName() << "\n";
7909 continue;
7910 }
7911 uint32_t SegIndex = Entry.segmentIndex();
7912 uint64_t OffsetInSeg = Entry.segmentOffset();
7913 StringRef SegmentName = sectionTable.segmentName(SegIndex);
7914 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
7915 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
7916
7917 // Table lines look like:
7918 // __DATA __data 0x00001000 pointer 0 _foo
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007919 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00007920 << left_justify(SectionName, 18) << " "
7921 << format_hex(Address, 10, true) << " "
7922 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007923 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
7924 << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00007925 }
7926}
7927
Kevin Enderby6f326ce2014-10-23 19:37:31 +00007928// get_dyld_bind_info_symbolname() is used for disassembly and passed an
7929// address, ReferenceValue, in the Mach-O file and looks in the dyld bind
7930// information for that address. If the address is found its binding symbol
7931// name is returned. If not nullptr is returned.
7932static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
7933 struct DisassembleInfo *info) {
Kevin Enderby078be602014-10-23 19:53:12 +00007934 if (info->bindtable == nullptr) {
7935 info->bindtable = new (BindTable);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00007936 SegInfo sectionTable(info->O);
7937 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
7938 uint32_t SegIndex = Entry.segmentIndex();
7939 uint64_t OffsetInSeg = Entry.segmentOffset();
7940 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
7941 const char *SymbolName = nullptr;
7942 StringRef name = Entry.symbolName();
7943 if (!name.empty())
7944 SymbolName = name.data();
Kevin Enderby078be602014-10-23 19:53:12 +00007945 info->bindtable->push_back(std::make_pair(Address, SymbolName));
Kevin Enderby6f326ce2014-10-23 19:37:31 +00007946 }
7947 }
Kevin Enderby078be602014-10-23 19:53:12 +00007948 for (bind_table_iterator BI = info->bindtable->begin(),
7949 BE = info->bindtable->end();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00007950 BI != BE; ++BI) {
7951 uint64_t Address = BI->first;
7952 if (ReferenceValue == Address) {
7953 const char *SymbolName = BI->second;
7954 return SymbolName;
7955 }
7956 }
7957 return nullptr;
7958}