blob: b4a05df21e2fbfdc21d77106ea7ef253e5a76082 [file] [log] [blame]
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001//===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the MachO-specific dumper for llvm-objdump.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm-objdump.h"
Kevin Enderby98c9acc2014-09-16 18:00:57 +000015#include "llvm-c/Disassembler.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000016#include "llvm/ADT/STLExtras.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000017#include "llvm/ADT/StringExtras.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000018#include "llvm/ADT/Triple.h"
Kevin Enderby04bf6932014-10-28 23:39:46 +000019#include "llvm/Config/config.h"
Zachary Turner6489d7b2015-04-23 17:37:47 +000020#include "llvm/DebugInfo/DIContext.h"
21#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000022#include "llvm/MC/MCAsmInfo.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000023#include "llvm/MC/MCContext.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000024#include "llvm/MC/MCDisassembler.h"
25#include "llvm/MC/MCInst.h"
26#include "llvm/MC/MCInstPrinter.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000027#include "llvm/MC/MCInstrDesc.h"
28#include "llvm/MC/MCInstrInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000029#include "llvm/MC/MCRegisterInfo.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000030#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000031#include "llvm/Object/MachO.h"
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +000032#include "llvm/Object/MachOUniversal.h"
Rafael Espindola9b709252013-04-13 01:45:40 +000033#include "llvm/Support/Casting.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000034#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/Debug.h"
Tim Northover4bd286a2014-08-01 13:07:19 +000036#include "llvm/Support/Endian.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000037#include "llvm/Support/Format.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000038#include "llvm/Support/FormattedStream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000039#include "llvm/Support/GraphWriter.h"
Kevin Enderby9a509442015-01-27 21:28:24 +000040#include "llvm/Support/LEB128.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000041#include "llvm/Support/MachO.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000042#include "llvm/Support/MemoryBuffer.h"
43#include "llvm/Support/TargetRegistry.h"
44#include "llvm/Support/TargetSelect.h"
45#include "llvm/Support/raw_ostream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000046#include <algorithm>
47#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000048#include <system_error>
Kevin Enderby04bf6932014-10-28 23:39:46 +000049
50#if HAVE_CXXABI_H
51#include <cxxabi.h>
52#endif
53
Benjamin Kramer43a772e2011-09-19 17:56:04 +000054using namespace llvm;
55using namespace object;
56
57static cl::opt<bool>
Kevin Enderbyb28ed012014-10-29 21:28:24 +000058 UseDbg("g",
59 cl::desc("Print line information from debug info if available"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000060
Kevin Enderbyb28ed012014-10-29 21:28:24 +000061static cl::opt<std::string> DSYMFile("dsym",
62 cl::desc("Use .dSYM file for debug info"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000063
Kevin Enderbyb28ed012014-10-29 21:28:24 +000064static cl::opt<bool> FullLeadingAddr("full-leading-addr",
65 cl::desc("Print full leading address"));
Kevin Enderbybf246f52014-09-24 23:08:22 +000066
Kevin Enderbyab5e6c92015-03-17 21:07:39 +000067static cl::opt<bool> NoLeadingAddr("no-leading-addr",
68 cl::desc("Print no leading address"));
69
Kevin Enderby13023a12015-01-15 23:19:11 +000070cl::opt<bool> llvm::UniversalHeaders("universal-headers",
71 cl::desc("Print Mach-O universal headers "
72 "(requires -macho)"));
73
Kevin Enderby131d1772015-01-09 19:22:37 +000074cl::opt<bool>
Kevin Enderby13023a12015-01-15 23:19:11 +000075 llvm::ArchiveHeaders("archive-headers",
76 cl::desc("Print archive headers for Mach-O archives "
77 "(requires -macho)"));
Kevin Enderby131d1772015-01-09 19:22:37 +000078
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000079cl::opt<bool>
Kevin Enderby8972e482015-04-30 20:30:42 +000080 ArchiveMemberOffsets("archive-member-offsets",
81 cl::desc("Print the offset to each archive member for "
82 "Mach-O archives (requires -macho and "
83 "-archive-headers)"));
84
85cl::opt<bool>
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000086 llvm::IndirectSymbols("indirect-symbols",
87 cl::desc("Print indirect symbol table for Mach-O "
88 "objects (requires -macho)"));
89
Kevin Enderby69fe98d2015-01-23 18:52:17 +000090cl::opt<bool>
91 llvm::DataInCode("data-in-code",
92 cl::desc("Print the data in code table for Mach-O objects "
93 "(requires -macho)"));
94
Kevin Enderby9a509442015-01-27 21:28:24 +000095cl::opt<bool>
96 llvm::LinkOptHints("link-opt-hints",
97 cl::desc("Print the linker optimization hints for "
98 "Mach-O objects (requires -macho)"));
99
Kevin Enderbycd66be52015-03-11 22:06:32 +0000100cl::opt<bool>
101 llvm::InfoPlist("info-plist",
102 cl::desc("Print the info plist section as strings for "
103 "Mach-O objects (requires -macho)"));
104
Kevin Enderbyf0640752015-03-13 17:56:32 +0000105cl::opt<bool>
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000106 llvm::DylibsUsed("dylibs-used",
107 cl::desc("Print the shared libraries used for linked "
108 "Mach-O files (requires -macho)"));
109
110cl::opt<bool>
111 llvm::DylibId("dylib-id",
112 cl::desc("Print the shared library's id for the dylib Mach-O "
113 "file (requires -macho)"));
114
115cl::opt<bool>
Kevin Enderbyf0640752015-03-13 17:56:32 +0000116 llvm::NonVerbose("non-verbose",
117 cl::desc("Print the info for Mach-O objects in "
118 "non-verbose or numeric form (requires -macho)"));
119
Kevin Enderby0fc11822015-04-01 20:57:01 +0000120cl::opt<bool>
121 llvm::ObjcMetaData("objc-meta-data",
122 cl::desc("Print the Objective-C runtime meta data for "
123 "Mach-O files (requires -macho)"));
124
Kevin Enderby6a221752015-03-17 17:10:57 +0000125cl::opt<std::string> llvm::DisSymName(
126 "dis-symname",
127 cl::desc("disassemble just this symbol's instructions (requires -macho"));
128
Kevin Enderby8e29ec92015-03-17 22:26:11 +0000129static cl::opt<bool> NoSymbolicOperands(
130 "no-symbolic-operands",
131 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
132
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000133static cl::list<std::string>
134 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
135 cl::ZeroOrMore);
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000136
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000137bool ArchAll = false;
138
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000139static std::string ThumbTripleName;
140
141static const Target *GetTarget(const MachOObjectFile *MachOObj,
142 const char **McpuDefault,
143 const Target **ThumbTarget) {
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000144 // Figure out the target triple.
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000145 if (TripleName.empty()) {
146 llvm::Triple TT("unknown-unknown-unknown");
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000147 llvm::Triple ThumbTriple = Triple();
148 TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000149 TripleName = TT.str();
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000150 ThumbTripleName = ThumbTriple.str();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000151 }
152
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000153 // Get the target specific parser.
154 std::string Error;
155 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000156 if (TheTarget && ThumbTripleName.empty())
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000157 return TheTarget;
158
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000159 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
160 if (*ThumbTarget)
161 return TheTarget;
162
163 errs() << "llvm-objdump: error: unable to get target for '";
164 if (!TheTarget)
165 errs() << TripleName;
166 else
167 errs() << ThumbTripleName;
168 errs() << "', see --version and --triple.\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000169 return nullptr;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000170}
171
Owen Andersond9243c42011-10-17 21:37:35 +0000172struct SymbolSorter {
173 bool operator()(const SymbolRef &A, const SymbolRef &B) {
Rafael Espindoladea00162015-07-03 17:44:18 +0000174 uint64_t AAddr = (A.getType() != SymbolRef::ST_Function) ? 0 : A.getValue();
175 uint64_t BAddr = (B.getType() != SymbolRef::ST_Function) ? 0 : B.getValue();
Owen Andersond9243c42011-10-17 21:37:35 +0000176 return AAddr < BAddr;
177 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000178};
179
Kevin Enderby273ae012013-06-06 17:20:50 +0000180// Types for the storted data in code table that is built before disassembly
181// and the predicate function to sort them.
182typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
183typedef std::vector<DiceTableEntry> DiceTable;
184typedef DiceTable::iterator dice_table_iterator;
185
Kevin Enderby930fdc72014-11-06 19:00:13 +0000186// This is used to search for a data in code table entry for the PC being
187// disassembled. The j parameter has the PC in j.first. A single data in code
188// table entry can cover many bytes for each of its Kind's. So if the offset,
189// aka the i.first value, of the data in code table entry plus its Length
190// covers the PC being searched for this will return true. If not it will
191// return false.
David Majnemerea9b8ee2014-11-04 08:41:48 +0000192static bool compareDiceTableEntries(const DiceTableEntry &i,
193 const DiceTableEntry &j) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000194 uint16_t Length;
195 i.second.getLength(Length);
196
197 return j.first >= i.first && j.first < i.first + Length;
Kevin Enderby273ae012013-06-06 17:20:50 +0000198}
199
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000200static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
Kevin Enderby930fdc72014-11-06 19:00:13 +0000201 unsigned short Kind) {
202 uint32_t Value, Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000203
204 switch (Kind) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000205 default:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000206 case MachO::DICE_KIND_DATA:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000207 if (Length >= 4) {
208 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000209 dumpBytes(makeArrayRef(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000210 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000211 outs() << "\t.long " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000212 Size = 4;
213 } else if (Length >= 2) {
214 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000215 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000216 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000217 outs() << "\t.short " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000218 Size = 2;
219 } else {
220 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000221 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000222 Value = bytes[0];
223 outs() << "\t.byte " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000224 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000225 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000226 if (Kind == MachO::DICE_KIND_DATA)
227 outs() << "\t@ KIND_DATA\n";
228 else
229 outs() << "\t@ data in code kind = " << Kind << "\n";
Kevin Enderby273ae012013-06-06 17:20:50 +0000230 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000231 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000232 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000233 dumpBytes(makeArrayRef(bytes, 1), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000234 Value = bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000235 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
236 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000237 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000238 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000239 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000240 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000241 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000242 outs() << "\t.short " << format("%5u", Value & 0xffff)
243 << "\t@ KIND_JUMP_TABLE16\n";
244 Size = 2;
Kevin Enderby273ae012013-06-06 17:20:50 +0000245 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000246 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000247 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
248 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000249 dumpBytes(makeArrayRef(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000250 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000251 outs() << "\t.long " << Value;
252 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
253 outs() << "\t@ KIND_JUMP_TABLE32\n";
254 else
255 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
256 Size = 4;
Kevin Enderby273ae012013-06-06 17:20:50 +0000257 break;
258 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000259 return Size;
Kevin Enderby273ae012013-06-06 17:20:50 +0000260}
261
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000262static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000263 std::vector<SectionRef> &Sections,
264 std::vector<SymbolRef> &Symbols,
265 SmallVectorImpl<uint64_t> &FoundFns,
266 uint64_t &BaseSegmentAddress) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000267 for (const SymbolRef &Symbol : MachOObj->symbols()) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000268 ErrorOr<StringRef> SymName = Symbol.getName();
269 if (std::error_code EC = SymName.getError())
270 report_fatal_error(EC.message());
271 if (!SymName->startswith("ltmp"))
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000272 Symbols.push_back(Symbol);
273 }
Owen Andersond9243c42011-10-17 21:37:35 +0000274
Alexey Samsonov48803e52014-03-13 14:37:36 +0000275 for (const SectionRef &Section : MachOObj->sections()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000276 StringRef SectName;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000277 Section.getName(SectName);
278 Sections.push_back(Section);
Owen Andersond9243c42011-10-17 21:37:35 +0000279 }
280
Kevin Enderby273ae012013-06-06 17:20:50 +0000281 bool BaseSegmentAddressSet = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000282 for (const auto &Command : MachOObj->load_commands()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000283 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000284 // We found a function starts segment, parse the addresses for later
285 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000286 MachO::linkedit_data_command LLC =
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000287 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000288
Charles Davis8bdfafd2013-09-01 04:28:48 +0000289 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000290 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
291 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000292 StringRef SegName = SLC.segname;
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000293 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
Kevin Enderby273ae012013-06-06 17:20:50 +0000294 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000295 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000296 }
297 }
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000298 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000299}
300
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000301static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
302 uint32_t n, uint32_t count,
303 uint32_t stride, uint64_t addr) {
304 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
305 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
306 if (n > nindirectsyms)
307 outs() << " (entries start past the end of the indirect symbol "
308 "table) (reserved1 field greater than the table size)";
309 else if (n + count > nindirectsyms)
310 outs() << " (entries extends past the end of the indirect symbol "
311 "table)";
312 outs() << "\n";
313 uint32_t cputype = O->getHeader().cputype;
314 if (cputype & MachO::CPU_ARCH_ABI64)
315 outs() << "address index";
316 else
317 outs() << "address index";
318 if (verbose)
319 outs() << " name\n";
320 else
321 outs() << "\n";
322 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
323 if (cputype & MachO::CPU_ARCH_ABI64)
324 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
325 else
326 outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
327 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
328 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
329 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
330 outs() << "LOCAL\n";
331 continue;
332 }
333 if (indirect_symbol ==
334 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
335 outs() << "LOCAL ABSOLUTE\n";
336 continue;
337 }
338 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
339 outs() << "ABSOLUTE\n";
340 continue;
341 }
342 outs() << format("%5u ", indirect_symbol);
Kevin Enderbyf0640752015-03-13 17:56:32 +0000343 if (verbose) {
344 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
345 if (indirect_symbol < Symtab.nsyms) {
346 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
347 SymbolRef Symbol = *Sym;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000348 ErrorOr<StringRef> SymName = Symbol.getName();
349 if (std::error_code EC = SymName.getError())
350 report_fatal_error(EC.message());
351 outs() << *SymName;
Kevin Enderbyf0640752015-03-13 17:56:32 +0000352 } else {
353 outs() << "?";
354 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000355 }
356 outs() << "\n";
357 }
358}
359
360static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000361 for (const auto &Load : O->load_commands()) {
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000362 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
363 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
364 for (unsigned J = 0; J < Seg.nsects; ++J) {
365 MachO::section_64 Sec = O->getSection64(Load, J);
366 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
367 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
368 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
369 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
370 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
371 section_type == MachO::S_SYMBOL_STUBS) {
372 uint32_t stride;
373 if (section_type == MachO::S_SYMBOL_STUBS)
374 stride = Sec.reserved2;
375 else
376 stride = 8;
377 if (stride == 0) {
378 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
379 << Sec.sectname << ") "
380 << "(size of stubs in reserved2 field is zero)\n";
381 continue;
382 }
383 uint32_t count = Sec.size / stride;
384 outs() << "Indirect symbols for (" << Sec.segname << ","
385 << Sec.sectname << ") " << count << " entries";
386 uint32_t n = Sec.reserved1;
387 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
388 }
389 }
390 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
391 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
392 for (unsigned J = 0; J < Seg.nsects; ++J) {
393 MachO::section Sec = O->getSection(Load, J);
394 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
395 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
396 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
397 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
398 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
399 section_type == MachO::S_SYMBOL_STUBS) {
400 uint32_t stride;
401 if (section_type == MachO::S_SYMBOL_STUBS)
402 stride = Sec.reserved2;
403 else
404 stride = 4;
405 if (stride == 0) {
406 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
407 << Sec.sectname << ") "
408 << "(size of stubs in reserved2 field is zero)\n";
409 continue;
410 }
411 uint32_t count = Sec.size / stride;
412 outs() << "Indirect symbols for (" << Sec.segname << ","
413 << Sec.sectname << ") " << count << " entries";
414 uint32_t n = Sec.reserved1;
415 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
416 }
417 }
418 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000419 }
420}
421
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000422static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
423 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
424 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
425 outs() << "Data in code table (" << nentries << " entries)\n";
426 outs() << "offset length kind\n";
427 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
428 ++DI) {
429 uint32_t Offset;
430 DI->getOffset(Offset);
431 outs() << format("0x%08" PRIx32, Offset) << " ";
432 uint16_t Length;
433 DI->getLength(Length);
434 outs() << format("%6u", Length) << " ";
435 uint16_t Kind;
436 DI->getKind(Kind);
437 if (verbose) {
438 switch (Kind) {
439 case MachO::DICE_KIND_DATA:
440 outs() << "DATA";
441 break;
442 case MachO::DICE_KIND_JUMP_TABLE8:
443 outs() << "JUMP_TABLE8";
444 break;
445 case MachO::DICE_KIND_JUMP_TABLE16:
446 outs() << "JUMP_TABLE16";
447 break;
448 case MachO::DICE_KIND_JUMP_TABLE32:
449 outs() << "JUMP_TABLE32";
450 break;
451 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
452 outs() << "ABS_JUMP_TABLE32";
453 break;
454 default:
455 outs() << format("0x%04" PRIx32, Kind);
456 break;
457 }
458 } else
459 outs() << format("0x%04" PRIx32, Kind);
460 outs() << "\n";
461 }
462}
463
Kevin Enderby9a509442015-01-27 21:28:24 +0000464static void PrintLinkOptHints(MachOObjectFile *O) {
465 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
466 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
467 uint32_t nloh = LohLC.datasize;
468 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
469 for (uint32_t i = 0; i < nloh;) {
470 unsigned n;
471 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
472 i += n;
473 outs() << " identifier " << identifier << " ";
474 if (i >= nloh)
475 return;
476 switch (identifier) {
477 case 1:
478 outs() << "AdrpAdrp\n";
479 break;
480 case 2:
481 outs() << "AdrpLdr\n";
482 break;
483 case 3:
484 outs() << "AdrpAddLdr\n";
485 break;
486 case 4:
487 outs() << "AdrpLdrGotLdr\n";
488 break;
489 case 5:
490 outs() << "AdrpAddStr\n";
491 break;
492 case 6:
493 outs() << "AdrpLdrGotStr\n";
494 break;
495 case 7:
496 outs() << "AdrpAdd\n";
497 break;
498 case 8:
499 outs() << "AdrpLdrGot\n";
500 break;
501 default:
502 outs() << "Unknown identifier value\n";
503 break;
504 }
505 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
506 i += n;
507 outs() << " narguments " << narguments << "\n";
508 if (i >= nloh)
509 return;
510
511 for (uint32_t j = 0; j < narguments; j++) {
512 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
513 i += n;
514 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
515 if (i >= nloh)
516 return;
517 }
518 }
519}
520
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000521static void PrintDylibs(MachOObjectFile *O, bool JustId) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000522 unsigned Index = 0;
523 for (const auto &Load : O->load_commands()) {
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000524 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
525 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
526 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
527 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
528 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
529 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
530 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
531 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
532 if (dl.dylib.name < dl.cmdsize) {
533 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
534 if (JustId)
535 outs() << p << "\n";
536 else {
537 outs() << "\t" << p;
538 outs() << " (compatibility version "
539 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
540 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
541 << (dl.dylib.compatibility_version & 0xff) << ",";
542 outs() << " current version "
543 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
544 << ((dl.dylib.current_version >> 8) & 0xff) << "."
545 << (dl.dylib.current_version & 0xff) << ")\n";
546 }
547 } else {
548 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
549 if (Load.C.cmd == MachO::LC_ID_DYLIB)
550 outs() << "LC_ID_DYLIB ";
551 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
552 outs() << "LC_LOAD_DYLIB ";
553 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
554 outs() << "LC_LOAD_WEAK_DYLIB ";
555 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
556 outs() << "LC_LAZY_LOAD_DYLIB ";
557 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
558 outs() << "LC_REEXPORT_DYLIB ";
559 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
560 outs() << "LC_LOAD_UPWARD_DYLIB ";
561 else
562 outs() << "LC_??? ";
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000563 outs() << "command " << Index++ << "\n";
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000564 }
565 }
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000566 }
567}
568
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000569typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
570
571static void CreateSymbolAddressMap(MachOObjectFile *O,
572 SymbolAddressMap *AddrMap) {
573 // Create a map of symbol addresses to symbol names.
574 for (const SymbolRef &Symbol : O->symbols()) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000575 SymbolRef::Type ST = Symbol.getType();
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000576 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
577 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +0000578 uint64_t Address = Symbol.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000579 ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
580 if (std::error_code EC = SymNameOrErr.getError())
581 report_fatal_error(EC.message());
582 StringRef SymName = *SymNameOrErr;
Kevin Enderby846c0002015-04-16 17:19:59 +0000583 if (!SymName.startswith(".objc"))
584 (*AddrMap)[Address] = SymName;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000585 }
586 }
587}
588
589// GuessSymbolName is passed the address of what might be a symbol and a
590// pointer to the SymbolAddressMap. It returns the name of a symbol
591// with that address or nullptr if no symbol is found with that address.
592static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
593 const char *SymbolName = nullptr;
594 // A DenseMap can't lookup up some values.
595 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
596 StringRef name = AddrMap->lookup(value);
597 if (!name.empty())
598 SymbolName = name.data();
599 }
600 return SymbolName;
601}
602
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000603static void DumpCstringChar(const char c) {
604 char p[2];
605 p[0] = c;
606 p[1] = '\0';
607 outs().write_escaped(p);
608}
609
Kevin Enderby10ba0412015-02-04 21:38:42 +0000610static void DumpCstringSection(MachOObjectFile *O, const char *sect,
611 uint32_t sect_size, uint64_t sect_addr,
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000612 bool print_addresses) {
Kevin Enderby10ba0412015-02-04 21:38:42 +0000613 for (uint32_t i = 0; i < sect_size; i++) {
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000614 if (print_addresses) {
615 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000616 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000617 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000618 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000619 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000620 for (; i < sect_size && sect[i] != '\0'; i++)
621 DumpCstringChar(sect[i]);
Kevin Enderby10ba0412015-02-04 21:38:42 +0000622 if (i < sect_size && sect[i] == '\0')
623 outs() << "\n";
624 }
625}
626
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000627static void DumpLiteral4(uint32_t l, float f) {
628 outs() << format("0x%08" PRIx32, l);
629 if ((l & 0x7f800000) != 0x7f800000)
630 outs() << format(" (%.16e)\n", f);
631 else {
632 if (l == 0x7f800000)
633 outs() << " (+Infinity)\n";
634 else if (l == 0xff800000)
635 outs() << " (-Infinity)\n";
636 else if ((l & 0x00400000) == 0x00400000)
637 outs() << " (non-signaling Not-a-Number)\n";
638 else
639 outs() << " (signaling Not-a-Number)\n";
640 }
641}
642
643static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
644 uint32_t sect_size, uint64_t sect_addr,
645 bool print_addresses) {
646 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
647 if (print_addresses) {
648 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000649 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000650 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000651 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000652 }
653 float f;
654 memcpy(&f, sect + i, sizeof(float));
655 if (O->isLittleEndian() != sys::IsLittleEndianHost)
656 sys::swapByteOrder(f);
657 uint32_t l;
658 memcpy(&l, sect + i, sizeof(uint32_t));
659 if (O->isLittleEndian() != sys::IsLittleEndianHost)
660 sys::swapByteOrder(l);
661 DumpLiteral4(l, f);
662 }
663}
664
665static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
666 double d) {
667 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
668 uint32_t Hi, Lo;
669 if (O->isLittleEndian()) {
670 Hi = l1;
671 Lo = l0;
672 } else {
673 Hi = l0;
674 Lo = l1;
675 }
676 // Hi is the high word, so this is equivalent to if(isfinite(d))
677 if ((Hi & 0x7ff00000) != 0x7ff00000)
678 outs() << format(" (%.16e)\n", d);
679 else {
680 if (Hi == 0x7ff00000 && Lo == 0)
681 outs() << " (+Infinity)\n";
682 else if (Hi == 0xfff00000 && Lo == 0)
683 outs() << " (-Infinity)\n";
684 else if ((Hi & 0x00080000) == 0x00080000)
685 outs() << " (non-signaling Not-a-Number)\n";
686 else
687 outs() << " (signaling Not-a-Number)\n";
688 }
689}
690
691static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
692 uint32_t sect_size, uint64_t sect_addr,
693 bool print_addresses) {
694 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
695 if (print_addresses) {
696 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000697 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000698 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000699 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000700 }
701 double d;
702 memcpy(&d, sect + i, sizeof(double));
703 if (O->isLittleEndian() != sys::IsLittleEndianHost)
704 sys::swapByteOrder(d);
705 uint32_t l0, l1;
706 memcpy(&l0, sect + i, sizeof(uint32_t));
707 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
708 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
709 sys::swapByteOrder(l0);
710 sys::swapByteOrder(l1);
711 }
712 DumpLiteral8(O, l0, l1, d);
713 }
714}
715
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000716static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
717 outs() << format("0x%08" PRIx32, l0) << " ";
718 outs() << format("0x%08" PRIx32, l1) << " ";
719 outs() << format("0x%08" PRIx32, l2) << " ";
720 outs() << format("0x%08" PRIx32, l3) << "\n";
721}
722
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000723static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
724 uint32_t sect_size, uint64_t sect_addr,
725 bool print_addresses) {
726 for (uint32_t i = 0; i < sect_size; i += 16) {
727 if (print_addresses) {
728 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000729 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000730 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000731 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000732 }
733 uint32_t l0, l1, l2, l3;
734 memcpy(&l0, sect + i, sizeof(uint32_t));
735 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
736 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
737 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
738 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
739 sys::swapByteOrder(l0);
740 sys::swapByteOrder(l1);
741 sys::swapByteOrder(l2);
742 sys::swapByteOrder(l3);
743 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000744 DumpLiteral16(l0, l1, l2, l3);
745 }
746}
747
748static void DumpLiteralPointerSection(MachOObjectFile *O,
749 const SectionRef &Section,
750 const char *sect, uint32_t sect_size,
751 uint64_t sect_addr,
752 bool print_addresses) {
753 // Collect the literal sections in this Mach-O file.
754 std::vector<SectionRef> LiteralSections;
755 for (const SectionRef &Section : O->sections()) {
756 DataRefImpl Ref = Section.getRawDataRefImpl();
757 uint32_t section_type;
758 if (O->is64Bit()) {
759 const MachO::section_64 Sec = O->getSection64(Ref);
760 section_type = Sec.flags & MachO::SECTION_TYPE;
761 } else {
762 const MachO::section Sec = O->getSection(Ref);
763 section_type = Sec.flags & MachO::SECTION_TYPE;
764 }
765 if (section_type == MachO::S_CSTRING_LITERALS ||
766 section_type == MachO::S_4BYTE_LITERALS ||
767 section_type == MachO::S_8BYTE_LITERALS ||
768 section_type == MachO::S_16BYTE_LITERALS)
769 LiteralSections.push_back(Section);
770 }
771
772 // Set the size of the literal pointer.
773 uint32_t lp_size = O->is64Bit() ? 8 : 4;
774
Eric Christopher572e03a2015-06-19 01:53:21 +0000775 // Collect the external relocation symbols for the literal pointers.
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000776 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
777 for (const RelocationRef &Reloc : Section.relocations()) {
778 DataRefImpl Rel;
779 MachO::any_relocation_info RE;
780 bool isExtern = false;
781 Rel = Reloc.getRawDataRefImpl();
782 RE = O->getRelocation(Rel);
783 isExtern = O->getPlainRelocationExternal(RE);
784 if (isExtern) {
Rafael Espindola96d071c2015-06-29 23:29:12 +0000785 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000786 symbol_iterator RelocSym = Reloc.getSymbol();
787 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
788 }
789 }
790 array_pod_sort(Relocs.begin(), Relocs.end());
791
792 // Dump each literal pointer.
793 for (uint32_t i = 0; i < sect_size; i += lp_size) {
794 if (print_addresses) {
795 if (O->is64Bit())
796 outs() << format("%016" PRIx64, sect_addr + i) << " ";
797 else
798 outs() << format("%08" PRIx64, sect_addr + i) << " ";
799 }
800 uint64_t lp;
801 if (O->is64Bit()) {
802 memcpy(&lp, sect + i, sizeof(uint64_t));
803 if (O->isLittleEndian() != sys::IsLittleEndianHost)
804 sys::swapByteOrder(lp);
805 } else {
806 uint32_t li;
807 memcpy(&li, sect + i, sizeof(uint32_t));
808 if (O->isLittleEndian() != sys::IsLittleEndianHost)
809 sys::swapByteOrder(li);
810 lp = li;
811 }
812
813 // First look for an external relocation entry for this literal pointer.
David Blaikie33dd45d02015-03-23 18:39:02 +0000814 auto Reloc = std::find_if(
815 Relocs.begin(), Relocs.end(),
816 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
817 if (Reloc != Relocs.end()) {
818 symbol_iterator RelocSym = Reloc->second;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000819 ErrorOr<StringRef> SymName = RelocSym->getName();
820 if (std::error_code EC = SymName.getError())
821 report_fatal_error(EC.message());
822 outs() << "external relocation entry for symbol:" << *SymName << "\n";
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000823 continue;
David Blaikie33dd45d02015-03-23 18:39:02 +0000824 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000825
826 // For local references see what the section the literal pointer points to.
David Blaikie33dd45d02015-03-23 18:39:02 +0000827 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
828 [&](const SectionRef &R) {
829 return lp >= R.getAddress() &&
830 lp < R.getAddress() + R.getSize();
831 });
832 if (Sect == LiteralSections.end()) {
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000833 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
David Blaikie33dd45d02015-03-23 18:39:02 +0000834 continue;
835 }
836
837 uint64_t SectAddress = Sect->getAddress();
838 uint64_t SectSize = Sect->getSize();
839
840 StringRef SectName;
841 Sect->getName(SectName);
842 DataRefImpl Ref = Sect->getRawDataRefImpl();
843 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
844 outs() << SegmentName << ":" << SectName << ":";
845
846 uint32_t section_type;
847 if (O->is64Bit()) {
848 const MachO::section_64 Sec = O->getSection64(Ref);
849 section_type = Sec.flags & MachO::SECTION_TYPE;
850 } else {
851 const MachO::section Sec = O->getSection(Ref);
852 section_type = Sec.flags & MachO::SECTION_TYPE;
853 }
854
855 StringRef BytesStr;
856 Sect->getContents(BytesStr);
857 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
858
859 switch (section_type) {
860 case MachO::S_CSTRING_LITERALS:
861 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
862 i++) {
863 DumpCstringChar(Contents[i]);
864 }
865 outs() << "\n";
866 break;
867 case MachO::S_4BYTE_LITERALS:
868 float f;
869 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
870 uint32_t l;
871 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
872 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
873 sys::swapByteOrder(f);
874 sys::swapByteOrder(l);
875 }
876 DumpLiteral4(l, f);
877 break;
878 case MachO::S_8BYTE_LITERALS: {
879 double d;
880 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
881 uint32_t l0, l1;
882 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
883 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
884 sizeof(uint32_t));
885 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
886 sys::swapByteOrder(f);
887 sys::swapByteOrder(l0);
888 sys::swapByteOrder(l1);
889 }
890 DumpLiteral8(O, l0, l1, d);
891 break;
892 }
893 case MachO::S_16BYTE_LITERALS: {
894 uint32_t l0, l1, l2, l3;
895 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
896 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
897 sizeof(uint32_t));
898 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
899 sizeof(uint32_t));
900 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
901 sizeof(uint32_t));
902 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
903 sys::swapByteOrder(l0);
904 sys::swapByteOrder(l1);
905 sys::swapByteOrder(l2);
906 sys::swapByteOrder(l3);
907 }
908 DumpLiteral16(l0, l1, l2, l3);
909 break;
910 }
911 }
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000912 }
913}
914
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000915static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
916 uint32_t sect_size, uint64_t sect_addr,
917 SymbolAddressMap *AddrMap,
918 bool verbose) {
919 uint32_t stride;
920 if (O->is64Bit())
921 stride = sizeof(uint64_t);
922 else
923 stride = sizeof(uint32_t);
924 for (uint32_t i = 0; i < sect_size; i += stride) {
925 const char *SymbolName = nullptr;
926 if (O->is64Bit()) {
927 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
928 uint64_t pointer_value;
929 memcpy(&pointer_value, sect + i, stride);
930 if (O->isLittleEndian() != sys::IsLittleEndianHost)
931 sys::swapByteOrder(pointer_value);
932 outs() << format("0x%016" PRIx64, pointer_value);
933 if (verbose)
934 SymbolName = GuessSymbolName(pointer_value, AddrMap);
935 } else {
936 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
937 uint32_t pointer_value;
938 memcpy(&pointer_value, sect + i, stride);
939 if (O->isLittleEndian() != sys::IsLittleEndianHost)
940 sys::swapByteOrder(pointer_value);
941 outs() << format("0x%08" PRIx32, pointer_value);
942 if (verbose)
943 SymbolName = GuessSymbolName(pointer_value, AddrMap);
944 }
945 if (SymbolName)
946 outs() << " " << SymbolName;
947 outs() << "\n";
948 }
949}
950
951static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
952 uint32_t size, uint64_t addr) {
953 uint32_t cputype = O->getHeader().cputype;
954 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
955 uint32_t j;
956 for (uint32_t i = 0; i < size; i += j, addr += j) {
957 if (O->is64Bit())
958 outs() << format("%016" PRIx64, addr) << "\t";
959 else
Kevin Enderbyf0640752015-03-13 17:56:32 +0000960 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000961 for (j = 0; j < 16 && i + j < size; j++) {
962 uint8_t byte_word = *(sect + i + j);
963 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
964 }
965 outs() << "\n";
966 }
967 } else {
968 uint32_t j;
969 for (uint32_t i = 0; i < size; i += j, addr += j) {
970 if (O->is64Bit())
971 outs() << format("%016" PRIx64, addr) << "\t";
972 else
973 outs() << format("%08" PRIx64, sect) << "\t";
974 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
975 j += sizeof(int32_t)) {
976 if (i + j + sizeof(int32_t) < size) {
977 uint32_t long_word;
978 memcpy(&long_word, sect + i + j, sizeof(int32_t));
979 if (O->isLittleEndian() != sys::IsLittleEndianHost)
980 sys::swapByteOrder(long_word);
981 outs() << format("%08" PRIx32, long_word) << " ";
982 } else {
983 for (uint32_t k = 0; i + j + k < size; k++) {
984 uint8_t byte_word = *(sect + i + j);
985 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
986 }
987 }
988 }
989 outs() << "\n";
990 }
991 }
992}
993
Kevin Enderby95df54c2015-02-04 01:01:38 +0000994static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
995 StringRef DisSegName, StringRef DisSectName);
Kevin Enderby4ad9bde2015-04-16 22:33:20 +0000996static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
997 uint32_t size, uint32_t addr);
Kevin Enderby95df54c2015-02-04 01:01:38 +0000998
999static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1000 bool verbose) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001001 SymbolAddressMap AddrMap;
1002 if (verbose)
1003 CreateSymbolAddressMap(O, &AddrMap);
1004
Colin LeMahieufcc32762015-07-29 19:08:10 +00001005 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1006 StringRef DumpSection = FilterSections[i];
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001007 std::pair<StringRef, StringRef> DumpSegSectName;
1008 DumpSegSectName = DumpSection.split(',');
1009 StringRef DumpSegName, DumpSectName;
1010 if (DumpSegSectName.second.size()) {
1011 DumpSegName = DumpSegSectName.first;
1012 DumpSectName = DumpSegSectName.second;
1013 } else {
1014 DumpSegName = "";
1015 DumpSectName = DumpSegSectName.first;
1016 }
1017 for (const SectionRef &Section : O->sections()) {
1018 StringRef SectName;
1019 Section.getName(SectName);
1020 DataRefImpl Ref = Section.getRawDataRefImpl();
1021 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1022 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1023 (SectName == DumpSectName)) {
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001024
Kevin Enderby95df54c2015-02-04 01:01:38 +00001025 uint32_t section_flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001026 if (O->is64Bit()) {
1027 const MachO::section_64 Sec = O->getSection64(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001028 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001029
1030 } else {
1031 const MachO::section Sec = O->getSection(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001032 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001033 }
Kevin Enderby95df54c2015-02-04 01:01:38 +00001034 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001035
1036 StringRef BytesStr;
1037 Section.getContents(BytesStr);
1038 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1039 uint32_t sect_size = BytesStr.size();
1040 uint64_t sect_addr = Section.getAddress();
1041
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001042 outs() << "Contents of (" << SegName << "," << SectName
1043 << ") section\n";
1044
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001045 if (verbose) {
Kevin Enderby95df54c2015-02-04 01:01:38 +00001046 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1047 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1048 DisassembleMachO(Filename, O, SegName, SectName);
1049 continue;
1050 }
Kevin Enderbycd66be52015-03-11 22:06:32 +00001051 if (SegName == "__TEXT" && SectName == "__info_plist") {
1052 outs() << sect;
1053 continue;
1054 }
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001055 if (SegName == "__OBJC" && SectName == "__protocol") {
1056 DumpProtocolSection(O, sect, sect_size, sect_addr);
1057 continue;
1058 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001059 switch (section_type) {
1060 case MachO::S_REGULAR:
1061 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1062 break;
1063 case MachO::S_ZEROFILL:
1064 outs() << "zerofill section and has no contents in the file\n";
1065 break;
Kevin Enderby10ba0412015-02-04 21:38:42 +00001066 case MachO::S_CSTRING_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001067 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby10ba0412015-02-04 21:38:42 +00001068 break;
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001069 case MachO::S_4BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001070 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001071 break;
1072 case MachO::S_8BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001073 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001074 break;
1075 case MachO::S_16BYTE_LITERALS:
Kevin Enderby0fc11822015-04-01 20:57:01 +00001076 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1077 break;
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001078 case MachO::S_LITERAL_POINTERS:
1079 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001080 !NoLeadingAddr);
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001081 break;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001082 case MachO::S_MOD_INIT_FUNC_POINTERS:
1083 case MachO::S_MOD_TERM_FUNC_POINTERS:
1084 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1085 verbose);
1086 break;
1087 default:
1088 outs() << "Unknown section type ("
1089 << format("0x%08" PRIx32, section_type) << ")\n";
1090 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1091 break;
1092 }
1093 } else {
1094 if (section_type == MachO::S_ZEROFILL)
1095 outs() << "zerofill section and has no contents in the file\n";
1096 else
1097 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1098 }
1099 }
1100 }
1101 }
1102}
1103
Kevin Enderbycd66be52015-03-11 22:06:32 +00001104static void DumpInfoPlistSectionContents(StringRef Filename,
1105 MachOObjectFile *O) {
1106 for (const SectionRef &Section : O->sections()) {
1107 StringRef SectName;
1108 Section.getName(SectName);
1109 DataRefImpl Ref = Section.getRawDataRefImpl();
1110 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1111 if (SegName == "__TEXT" && SectName == "__info_plist") {
1112 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1113 StringRef BytesStr;
1114 Section.getContents(BytesStr);
1115 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1116 outs() << sect;
1117 return;
1118 }
1119 }
1120}
1121
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001122// checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1123// and if it is and there is a list of architecture flags is specified then
1124// check to make sure this Mach-O file is one of those architectures or all
1125// architectures were specified. If not then an error is generated and this
1126// routine returns false. Else it returns true.
1127static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1128 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1129 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1130 bool ArchFound = false;
1131 MachO::mach_header H;
1132 MachO::mach_header_64 H_64;
1133 Triple T;
1134 if (MachO->is64Bit()) {
1135 H_64 = MachO->MachOObjectFile::getHeader64();
1136 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1137 } else {
1138 H = MachO->MachOObjectFile::getHeader();
1139 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1140 }
1141 unsigned i;
1142 for (i = 0; i < ArchFlags.size(); ++i) {
1143 if (ArchFlags[i] == T.getArchName())
1144 ArchFound = true;
1145 break;
1146 }
1147 if (!ArchFound) {
1148 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1149 << "architecture: " + ArchFlags[i] + "\n";
1150 return false;
1151 }
1152 }
1153 return true;
1154}
1155
Kevin Enderby0fc11822015-04-01 20:57:01 +00001156static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1157
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001158// ProcessMachO() is passed a single opened Mach-O file, which may be an
1159// archive member and or in a slice of a universal file. It prints the
1160// the file name and header info and then processes it according to the
1161// command line options.
1162static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1163 StringRef ArchiveMemberName = StringRef(),
1164 StringRef ArchitectureName = StringRef()) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001165 // If we are doing some processing here on the Mach-O file print the header
1166 // info. And don't print it otherwise like in the case of printing the
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001167 // UniversalHeaders or ArchiveHeaders.
Kevin Enderby131d1772015-01-09 19:22:37 +00001168 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001169 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
Colin LeMahieufcc32762015-07-29 19:08:10 +00001170 DylibsUsed || DylibId || ObjcMetaData || (FilterSections.size() != 0)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001171 outs() << Filename;
1172 if (!ArchiveMemberName.empty())
1173 outs() << '(' << ArchiveMemberName << ')';
1174 if (!ArchitectureName.empty())
1175 outs() << " (architecture " << ArchitectureName << ")";
1176 outs() << ":\n";
1177 }
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001178
1179 if (Disassemble)
Kevin Enderby95df54c2015-02-04 01:01:38 +00001180 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001181 if (IndirectSymbols)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001182 PrintIndirectSymbols(MachOOF, !NonVerbose);
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001183 if (DataInCode)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001184 PrintDataInCodeTable(MachOOF, !NonVerbose);
Kevin Enderby9a509442015-01-27 21:28:24 +00001185 if (LinkOptHints)
1186 PrintLinkOptHints(MachOOF);
Kevin Enderby98da6132015-01-20 21:47:46 +00001187 if (Relocations)
1188 PrintRelocations(MachOOF);
1189 if (SectionHeaders)
1190 PrintSectionHeaders(MachOOF);
1191 if (SectionContents)
1192 PrintSectionContents(MachOOF);
Colin LeMahieufcc32762015-07-29 19:08:10 +00001193 if (FilterSections.size() != 0)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001194 DumpSectionContents(Filename, MachOOF, !NonVerbose);
Kevin Enderbycd66be52015-03-11 22:06:32 +00001195 if (InfoPlist)
1196 DumpInfoPlistSectionContents(Filename, MachOOF);
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001197 if (DylibsUsed)
1198 PrintDylibs(MachOOF, false);
1199 if (DylibId)
1200 PrintDylibs(MachOOF, true);
Kevin Enderby98da6132015-01-20 21:47:46 +00001201 if (SymbolTable)
1202 PrintSymbolTable(MachOOF);
1203 if (UnwindInfo)
1204 printMachOUnwindInfo(MachOOF);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001205 if (PrivateHeaders)
1206 printMachOFileHeader(MachOOF);
Kevin Enderby0fc11822015-04-01 20:57:01 +00001207 if (ObjcMetaData)
1208 printObjcMetaData(MachOOF, !NonVerbose);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001209 if (ExportsTrie)
1210 printExportsTrie(MachOOF);
1211 if (Rebase)
1212 printRebaseTable(MachOOF);
1213 if (Bind)
1214 printBindTable(MachOOF);
1215 if (LazyBind)
1216 printLazyBindTable(MachOOF);
1217 if (WeakBind)
1218 printWeakBindTable(MachOOF);
1219}
1220
Kevin Enderby131d1772015-01-09 19:22:37 +00001221// printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1222static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1223 outs() << " cputype (" << cputype << ")\n";
1224 outs() << " cpusubtype (" << cpusubtype << ")\n";
1225}
1226
1227// printCPUType() helps print_fat_headers by printing the cputype and
1228// pusubtype (symbolically for the one's it knows about).
1229static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1230 switch (cputype) {
1231 case MachO::CPU_TYPE_I386:
1232 switch (cpusubtype) {
1233 case MachO::CPU_SUBTYPE_I386_ALL:
1234 outs() << " cputype CPU_TYPE_I386\n";
1235 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1236 break;
1237 default:
1238 printUnknownCPUType(cputype, cpusubtype);
1239 break;
1240 }
1241 break;
1242 case MachO::CPU_TYPE_X86_64:
1243 switch (cpusubtype) {
1244 case MachO::CPU_SUBTYPE_X86_64_ALL:
1245 outs() << " cputype CPU_TYPE_X86_64\n";
1246 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1247 break;
1248 case MachO::CPU_SUBTYPE_X86_64_H:
1249 outs() << " cputype CPU_TYPE_X86_64\n";
1250 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1251 break;
1252 default:
1253 printUnknownCPUType(cputype, cpusubtype);
1254 break;
1255 }
1256 break;
1257 case MachO::CPU_TYPE_ARM:
1258 switch (cpusubtype) {
1259 case MachO::CPU_SUBTYPE_ARM_ALL:
1260 outs() << " cputype CPU_TYPE_ARM\n";
1261 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1262 break;
1263 case MachO::CPU_SUBTYPE_ARM_V4T:
1264 outs() << " cputype CPU_TYPE_ARM\n";
1265 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1266 break;
1267 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1268 outs() << " cputype CPU_TYPE_ARM\n";
1269 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1270 break;
1271 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1272 outs() << " cputype CPU_TYPE_ARM\n";
1273 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1274 break;
1275 case MachO::CPU_SUBTYPE_ARM_V6:
1276 outs() << " cputype CPU_TYPE_ARM\n";
1277 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1278 break;
1279 case MachO::CPU_SUBTYPE_ARM_V6M:
1280 outs() << " cputype CPU_TYPE_ARM\n";
1281 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1282 break;
1283 case MachO::CPU_SUBTYPE_ARM_V7:
1284 outs() << " cputype CPU_TYPE_ARM\n";
1285 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1286 break;
1287 case MachO::CPU_SUBTYPE_ARM_V7EM:
1288 outs() << " cputype CPU_TYPE_ARM\n";
1289 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1290 break;
1291 case MachO::CPU_SUBTYPE_ARM_V7K:
1292 outs() << " cputype CPU_TYPE_ARM\n";
1293 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1294 break;
1295 case MachO::CPU_SUBTYPE_ARM_V7M:
1296 outs() << " cputype CPU_TYPE_ARM\n";
1297 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1298 break;
1299 case MachO::CPU_SUBTYPE_ARM_V7S:
1300 outs() << " cputype CPU_TYPE_ARM\n";
1301 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1302 break;
1303 default:
1304 printUnknownCPUType(cputype, cpusubtype);
1305 break;
1306 }
1307 break;
1308 case MachO::CPU_TYPE_ARM64:
1309 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1310 case MachO::CPU_SUBTYPE_ARM64_ALL:
1311 outs() << " cputype CPU_TYPE_ARM64\n";
1312 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1313 break;
1314 default:
1315 printUnknownCPUType(cputype, cpusubtype);
1316 break;
1317 }
1318 break;
1319 default:
1320 printUnknownCPUType(cputype, cpusubtype);
1321 break;
1322 }
1323}
1324
1325static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1326 bool verbose) {
1327 outs() << "Fat headers\n";
1328 if (verbose)
1329 outs() << "fat_magic FAT_MAGIC\n";
1330 else
1331 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1332
1333 uint32_t nfat_arch = UB->getNumberOfObjects();
1334 StringRef Buf = UB->getData();
1335 uint64_t size = Buf.size();
1336 uint64_t big_size = sizeof(struct MachO::fat_header) +
1337 nfat_arch * sizeof(struct MachO::fat_arch);
1338 outs() << "nfat_arch " << UB->getNumberOfObjects();
1339 if (nfat_arch == 0)
1340 outs() << " (malformed, contains zero architecture types)\n";
1341 else if (big_size > size)
1342 outs() << " (malformed, architectures past end of file)\n";
1343 else
1344 outs() << "\n";
1345
1346 for (uint32_t i = 0; i < nfat_arch; ++i) {
1347 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1348 uint32_t cputype = OFA.getCPUType();
1349 uint32_t cpusubtype = OFA.getCPUSubType();
1350 outs() << "architecture ";
1351 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1352 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1353 uint32_t other_cputype = other_OFA.getCPUType();
1354 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
Kevin Enderby0512bd72015-01-09 21:55:03 +00001355 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
Kevin Enderby131d1772015-01-09 19:22:37 +00001356 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
Kevin Enderby0512bd72015-01-09 21:55:03 +00001357 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001358 outs() << "(illegal duplicate architecture) ";
1359 break;
Kevin Enderby0512bd72015-01-09 21:55:03 +00001360 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001361 }
1362 if (verbose) {
1363 outs() << OFA.getArchTypeName() << "\n";
1364 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1365 } else {
1366 outs() << i << "\n";
1367 outs() << " cputype " << cputype << "\n";
1368 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1369 << "\n";
1370 }
1371 if (verbose &&
1372 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1373 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1374 else
1375 outs() << " capabilities "
1376 << format("0x%" PRIx32,
1377 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1378 outs() << " offset " << OFA.getOffset();
1379 if (OFA.getOffset() > size)
1380 outs() << " (past end of file)";
1381 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1382 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1383 outs() << "\n";
1384 outs() << " size " << OFA.getSize();
1385 big_size = OFA.getOffset() + OFA.getSize();
1386 if (big_size > size)
1387 outs() << " (past end of file)";
1388 outs() << "\n";
1389 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1390 << ")\n";
1391 }
1392}
1393
Rafael Espindola266b4fe2015-10-31 22:25:59 +00001394static void printArchiveChild(const Archive::Child &C, bool verbose,
Kevin Enderby13023a12015-01-15 23:19:11 +00001395 bool print_offset) {
1396 if (print_offset)
1397 outs() << C.getChildOffset() << "\t";
1398 sys::fs::perms Mode = C.getAccessMode();
1399 if (verbose) {
1400 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1401 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1402 outs() << "-";
Davide Italianobb9a6cc2015-09-07 20:47:03 +00001403 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
1404 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
1405 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
1406 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
1407 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
1408 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
1409 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
1410 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
1411 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
Kevin Enderby13023a12015-01-15 23:19:11 +00001412 } else {
1413 outs() << format("0%o ", Mode);
1414 }
1415
1416 unsigned UID = C.getUID();
1417 outs() << format("%3d/", UID);
1418 unsigned GID = C.getGID();
1419 outs() << format("%-3d ", GID);
Kevin Enderbyda9dd052015-10-21 17:13:20 +00001420 uint64_t Size = C.getRawSize();
1421 outs() << format("%5" PRId64, Size) << " ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001422
1423 StringRef RawLastModified = C.getRawLastModified();
1424 if (verbose) {
1425 unsigned Seconds;
1426 if (RawLastModified.getAsInteger(10, Seconds))
1427 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1428 else {
1429 // Since cime(3) returns a 26 character string of the form:
1430 // "Sun Sep 16 01:03:52 1973\n\0"
1431 // just print 24 characters.
1432 time_t t = Seconds;
1433 outs() << format("%.24s ", ctime(&t));
1434 }
1435 } else {
1436 outs() << RawLastModified << " ";
1437 }
1438
1439 if (verbose) {
1440 ErrorOr<StringRef> NameOrErr = C.getName();
1441 if (NameOrErr.getError()) {
1442 StringRef RawName = C.getRawName();
1443 outs() << RawName << "\n";
1444 } else {
1445 StringRef Name = NameOrErr.get();
1446 outs() << Name << "\n";
1447 }
1448 } else {
1449 StringRef RawName = C.getRawName();
1450 outs() << RawName << "\n";
1451 }
1452}
1453
1454static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
Rafael Espindola4a782fb2015-10-31 21:03:29 +00001455 for (Archive::child_iterator I = A->child_begin(false), E = A->child_end();
1456 I != E; ++I) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +00001457 Archive::Child C = *I;
Kevin Enderby13023a12015-01-15 23:19:11 +00001458 printArchiveChild(C, verbose, print_offset);
1459 }
1460}
1461
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001462// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1463// -arch flags selecting just those slices as specified by them and also parses
1464// archive files. Then for each individual Mach-O file ProcessMachO() is
1465// called to process the file based on the command line options.
1466void llvm::ParseInputMachO(StringRef Filename) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001467 // Check for -arch all and verifiy the -arch flags are valid.
1468 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1469 if (ArchFlags[i] == "all") {
1470 ArchAll = true;
1471 } else {
1472 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1473 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1474 "'for the -arch option\n";
1475 return;
1476 }
1477 }
1478 }
1479
1480 // Attempt to open the binary.
1481 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1482 if (std::error_code EC = BinaryOrErr.getError()) {
1483 errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n";
Rafael Espindolade882cd2014-12-03 23:29:34 +00001484 return;
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001485 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001486 Binary &Bin = *BinaryOrErr.get().getBinary();
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001487
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001488 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1489 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001490 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001491 printArchiveHeaders(A, !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001492 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1493 I != E; ++I) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +00001494 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001495 if (ChildOrErr.getError())
1496 continue;
1497 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1498 if (!checkMachOAndArchFlags(O, Filename))
1499 return;
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001500 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001501 }
1502 }
1503 return;
1504 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001505 if (UniversalHeaders) {
1506 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
Kevin Enderbyf0640752015-03-13 17:56:32 +00001507 printMachOUniversalHeaders(UB, !NonVerbose);
Kevin Enderby131d1772015-01-09 19:22:37 +00001508 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001509 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1510 // If we have a list of architecture flags specified dump only those.
1511 if (!ArchAll && ArchFlags.size() != 0) {
1512 // Look for a slice in the universal binary that matches each ArchFlag.
1513 bool ArchFound;
1514 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1515 ArchFound = false;
1516 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1517 E = UB->end_objects();
1518 I != E; ++I) {
1519 if (ArchFlags[i] == I->getArchTypeName()) {
1520 ArchFound = true;
1521 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1522 I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001523 std::string ArchitectureName = "";
1524 if (ArchFlags.size() > 1)
1525 ArchitectureName = I->getArchTypeName();
1526 if (ObjOrErr) {
1527 ObjectFile &O = *ObjOrErr.get();
1528 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001529 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001530 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1531 I->getAsArchive()) {
1532 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001533 outs() << "Archive : " << Filename;
1534 if (!ArchitectureName.empty())
1535 outs() << " (architecture " << ArchitectureName << ")";
1536 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001537 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001538 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001539 for (Archive::child_iterator AI = A->child_begin(),
1540 AE = A->child_end();
1541 AI != AE; ++AI) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +00001542 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001543 if (ChildOrErr.getError())
1544 continue;
1545 if (MachOObjectFile *O =
1546 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001547 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001548 }
1549 }
1550 }
1551 }
1552 if (!ArchFound) {
1553 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1554 << "architecture: " + ArchFlags[i] + "\n";
1555 return;
1556 }
1557 }
1558 return;
1559 }
1560 // No architecture flags were specified so if this contains a slice that
1561 // matches the host architecture dump only that.
1562 if (!ArchAll) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001563 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1564 E = UB->end_objects();
1565 I != E; ++I) {
Kevin Enderby0512bd72015-01-09 21:55:03 +00001566 if (MachOObjectFile::getHostArch().getArchName() ==
1567 I->getArchTypeName()) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001568 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001569 std::string ArchiveName;
1570 ArchiveName.clear();
1571 if (ObjOrErr) {
1572 ObjectFile &O = *ObjOrErr.get();
1573 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001574 ProcessMachO(Filename, MachOOF);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001575 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1576 I->getAsArchive()) {
1577 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001578 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001579 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001580 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001581 for (Archive::child_iterator AI = A->child_begin(),
1582 AE = A->child_end();
1583 AI != AE; ++AI) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +00001584 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001585 if (ChildOrErr.getError())
1586 continue;
1587 if (MachOObjectFile *O =
1588 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001589 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001590 }
1591 }
1592 return;
1593 }
1594 }
1595 }
1596 // Either all architectures have been specified or none have been specified
1597 // and this does not contain the host architecture so dump all the slices.
1598 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1599 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1600 E = UB->end_objects();
1601 I != E; ++I) {
1602 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001603 std::string ArchitectureName = "";
1604 if (moreThanOneArch)
1605 ArchitectureName = I->getArchTypeName();
1606 if (ObjOrErr) {
1607 ObjectFile &Obj = *ObjOrErr.get();
1608 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001609 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001610 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1611 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001612 outs() << "Archive : " << Filename;
1613 if (!ArchitectureName.empty())
1614 outs() << " (architecture " << ArchitectureName << ")";
1615 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001616 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001617 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001618 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1619 AI != AE; ++AI) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +00001620 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001621 if (ChildOrErr.getError())
1622 continue;
1623 if (MachOObjectFile *O =
1624 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1625 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001626 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1627 ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001628 }
1629 }
1630 }
1631 }
1632 return;
1633 }
1634 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1635 if (!checkMachOAndArchFlags(O, Filename))
1636 return;
1637 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001638 ProcessMachO(Filename, MachOOF);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001639 } else
1640 errs() << "llvm-objdump: '" << Filename << "': "
1641 << "Object is not a Mach-O file type.\n";
1642 } else
1643 errs() << "llvm-objdump: '" << Filename << "': "
1644 << "Unrecognized file type.\n";
Rafael Espindola9b709252013-04-13 01:45:40 +00001645}
1646
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001647typedef std::pair<uint64_t, const char *> BindInfoEntry;
1648typedef std::vector<BindInfoEntry> BindTable;
1649typedef BindTable::iterator bind_table_iterator;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001650
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001651// The block of info used by the Symbolizer call backs.
1652struct DisassembleInfo {
1653 bool verbose;
1654 MachOObjectFile *O;
1655 SectionRef S;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001656 SymbolAddressMap *AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001657 std::vector<SectionRef> *Sections;
1658 const char *class_name;
1659 const char *selector_name;
1660 char *method;
Kevin Enderby04bf6932014-10-28 23:39:46 +00001661 char *demangled_name;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001662 uint64_t adrp_addr;
1663 uint32_t adrp_inst;
Kevin Enderby078be602014-10-23 19:53:12 +00001664 BindTable *bindtable;
Kevin Enderbyaac75382015-10-08 16:56:35 +00001665 uint32_t depth;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001666};
1667
1668// SymbolizerGetOpInfo() is the operand information call back function.
1669// This is called to get the symbolic information for operand(s) of an
1670// instruction when it is being done. This routine does this from
1671// the relocation information, symbol table, etc. That block of information
1672// is a pointer to the struct DisassembleInfo that was passed when the
1673// disassembler context was created and passed to back to here when
1674// called back by the disassembler for instruction operands that could have
1675// relocation information. The address of the instruction containing operand is
1676// at the Pc parameter. The immediate value the operand has is passed in
1677// op_info->Value and is at Offset past the start of the instruction and has a
1678// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1679// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1680// names and addends of the symbolic expression to add for the operand. The
1681// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1682// information is returned then this function returns 1 else it returns 0.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00001683static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1684 uint64_t Size, int TagType, void *TagBuf) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001685 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1686 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001687 uint64_t value = op_info->Value;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001688
1689 // Make sure all fields returned are zero if we don't set them.
1690 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1691 op_info->Value = value;
1692
1693 // If the TagType is not the value 1 which it code knows about or if no
1694 // verbose symbolic information is wanted then just return 0, indicating no
1695 // information is being returned.
David Blaikie33dd45d02015-03-23 18:39:02 +00001696 if (TagType != 1 || !info->verbose)
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001697 return 0;
1698
1699 unsigned int Arch = info->O->getArch();
1700 if (Arch == Triple::x86) {
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001701 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1702 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001703 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1704 // TODO:
1705 // Search the external relocation entries of a fully linked image
1706 // (if any) for an entry that matches this segment offset.
1707 // uint32_t seg_offset = (Pc + Offset);
1708 return 0;
1709 }
1710 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1711 // for an entry for this section offset.
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001712 uint32_t sect_addr = info->S.getAddress();
1713 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1714 bool reloc_found = false;
1715 DataRefImpl Rel;
1716 MachO::any_relocation_info RE;
1717 bool isExtern = false;
1718 SymbolRef Symbol;
1719 bool r_scattered = false;
1720 uint32_t r_value, pair_r_value, r_type;
1721 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001722 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001723 if (RelocOffset == sect_offset) {
1724 Rel = Reloc.getRawDataRefImpl();
1725 RE = info->O->getRelocation(Rel);
Kevin Enderby3eb73e12014-11-11 19:16:45 +00001726 r_type = info->O->getAnyRelocationType(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001727 r_scattered = info->O->isRelocationScattered(RE);
1728 if (r_scattered) {
1729 r_value = info->O->getScatteredRelocationValue(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001730 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1731 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1732 DataRefImpl RelNext = Rel;
1733 info->O->moveRelocationNext(RelNext);
1734 MachO::any_relocation_info RENext;
1735 RENext = info->O->getRelocation(RelNext);
1736 if (info->O->isRelocationScattered(RENext))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001737 pair_r_value = info->O->getScatteredRelocationValue(RENext);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001738 else
1739 return 0;
1740 }
1741 } else {
1742 isExtern = info->O->getPlainRelocationExternal(RE);
1743 if (isExtern) {
1744 symbol_iterator RelocSym = Reloc.getSymbol();
1745 Symbol = *RelocSym;
1746 }
1747 }
1748 reloc_found = true;
1749 break;
1750 }
1751 }
1752 if (reloc_found && isExtern) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001753 ErrorOr<StringRef> SymName = Symbol.getName();
1754 if (std::error_code EC = SymName.getError())
1755 report_fatal_error(EC.message());
1756 const char *name = SymName->data();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001757 op_info->AddSymbol.Present = 1;
1758 op_info->AddSymbol.Name = name;
1759 // For i386 extern relocation entries the value in the instruction is
1760 // the offset from the symbol, and value is already set in op_info->Value.
1761 return 1;
1762 }
1763 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1764 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001765 const char *add = GuessSymbolName(r_value, info->AddrMap);
1766 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001767 uint32_t offset = value - (r_value - pair_r_value);
1768 op_info->AddSymbol.Present = 1;
1769 if (add != nullptr)
1770 op_info->AddSymbol.Name = add;
1771 else
1772 op_info->AddSymbol.Value = r_value;
1773 op_info->SubtractSymbol.Present = 1;
1774 if (sub != nullptr)
1775 op_info->SubtractSymbol.Name = sub;
1776 else
1777 op_info->SubtractSymbol.Value = pair_r_value;
1778 op_info->Value = offset;
1779 return 1;
1780 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001781 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001782 }
1783 if (Arch == Triple::x86_64) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001784 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1785 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001786 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1787 // TODO:
1788 // Search the external relocation entries of a fully linked image
1789 // (if any) for an entry that matches this segment offset.
1790 // uint64_t seg_offset = (Pc + Offset);
1791 return 0;
1792 }
1793 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1794 // for an entry for this section offset.
Rafael Espindola80291272014-10-08 15:28:58 +00001795 uint64_t sect_addr = info->S.getAddress();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001796 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1797 bool reloc_found = false;
1798 DataRefImpl Rel;
1799 MachO::any_relocation_info RE;
1800 bool isExtern = false;
1801 SymbolRef Symbol;
1802 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001803 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001804 if (RelocOffset == sect_offset) {
1805 Rel = Reloc.getRawDataRefImpl();
1806 RE = info->O->getRelocation(Rel);
1807 // NOTE: Scattered relocations don't exist on x86_64.
1808 isExtern = info->O->getPlainRelocationExternal(RE);
1809 if (isExtern) {
1810 symbol_iterator RelocSym = Reloc.getSymbol();
1811 Symbol = *RelocSym;
1812 }
1813 reloc_found = true;
1814 break;
1815 }
1816 }
1817 if (reloc_found && isExtern) {
1818 // The Value passed in will be adjusted by the Pc if the instruction
1819 // adds the Pc. But for x86_64 external relocation entries the Value
1820 // is the offset from the external symbol.
1821 if (info->O->getAnyRelocationPCRel(RE))
1822 op_info->Value -= Pc + Offset + Size;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001823 ErrorOr<StringRef> SymName = Symbol.getName();
1824 if (std::error_code EC = SymName.getError())
1825 report_fatal_error(EC.message());
1826 const char *name = SymName->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001827 unsigned Type = info->O->getAnyRelocationType(RE);
1828 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1829 DataRefImpl RelNext = Rel;
1830 info->O->moveRelocationNext(RelNext);
1831 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1832 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1833 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1834 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1835 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1836 op_info->SubtractSymbol.Present = 1;
1837 op_info->SubtractSymbol.Name = name;
1838 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1839 Symbol = *RelocSymNext;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001840 ErrorOr<StringRef> SymNameNext = Symbol.getName();
1841 if (std::error_code EC = SymNameNext.getError())
1842 report_fatal_error(EC.message());
1843 name = SymNameNext->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001844 }
1845 }
1846 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1847 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1848 op_info->AddSymbol.Present = 1;
1849 op_info->AddSymbol.Name = name;
1850 return 1;
1851 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001852 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001853 }
1854 if (Arch == Triple::arm) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001855 if (Offset != 0 || (Size != 4 && Size != 2))
1856 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001857 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1858 // TODO:
1859 // Search the external relocation entries of a fully linked image
1860 // (if any) for an entry that matches this segment offset.
1861 // uint32_t seg_offset = (Pc + Offset);
1862 return 0;
1863 }
1864 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1865 // for an entry for this section offset.
Kevin Enderby930fdc72014-11-06 19:00:13 +00001866 uint32_t sect_addr = info->S.getAddress();
1867 uint32_t sect_offset = (Pc + Offset) - sect_addr;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001868 DataRefImpl Rel;
1869 MachO::any_relocation_info RE;
1870 bool isExtern = false;
1871 SymbolRef Symbol;
1872 bool r_scattered = false;
1873 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
David Blaikie33dd45d02015-03-23 18:39:02 +00001874 auto Reloc =
1875 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1876 [&](const RelocationRef &Reloc) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001877 uint64_t RelocOffset = Reloc.getOffset();
David Blaikie33dd45d02015-03-23 18:39:02 +00001878 return RelocOffset == sect_offset;
1879 });
1880
1881 if (Reloc == info->S.relocations().end())
1882 return 0;
1883
1884 Rel = Reloc->getRawDataRefImpl();
1885 RE = info->O->getRelocation(Rel);
1886 r_length = info->O->getAnyRelocationLength(RE);
1887 r_scattered = info->O->isRelocationScattered(RE);
1888 if (r_scattered) {
1889 r_value = info->O->getScatteredRelocationValue(RE);
1890 r_type = info->O->getScatteredRelocationType(RE);
1891 } else {
1892 r_type = info->O->getAnyRelocationType(RE);
1893 isExtern = info->O->getPlainRelocationExternal(RE);
1894 if (isExtern) {
1895 symbol_iterator RelocSym = Reloc->getSymbol();
1896 Symbol = *RelocSym;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001897 }
1898 }
David Blaikie33dd45d02015-03-23 18:39:02 +00001899 if (r_type == MachO::ARM_RELOC_HALF ||
1900 r_type == MachO::ARM_RELOC_SECTDIFF ||
1901 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
1902 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1903 DataRefImpl RelNext = Rel;
1904 info->O->moveRelocationNext(RelNext);
1905 MachO::any_relocation_info RENext;
1906 RENext = info->O->getRelocation(RelNext);
1907 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
1908 if (info->O->isRelocationScattered(RENext))
1909 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1910 }
1911
1912 if (isExtern) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001913 ErrorOr<StringRef> SymName = Symbol.getName();
1914 if (std::error_code EC = SymName.getError())
1915 report_fatal_error(EC.message());
1916 const char *name = SymName->data();
Kevin Enderby930fdc72014-11-06 19:00:13 +00001917 op_info->AddSymbol.Present = 1;
1918 op_info->AddSymbol.Name = name;
Sylvestre Ledru648cced2015-02-05 17:00:23 +00001919 switch (r_type) {
1920 case MachO::ARM_RELOC_HALF:
1921 if ((r_length & 0x1) == 1) {
1922 op_info->Value = value << 16 | other_half;
1923 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1924 } else {
1925 op_info->Value = other_half << 16 | value;
1926 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Sylvestre Ledrufe0c7ad2015-02-05 16:35:44 +00001927 }
Sylvestre Ledru648cced2015-02-05 17:00:23 +00001928 break;
1929 default:
1930 break;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001931 }
1932 return 1;
1933 }
1934 // If we have a branch that is not an external relocation entry then
1935 // return 0 so the code in tryAddingSymbolicOperand() can use the
1936 // SymbolLookUp call back with the branch target address to look up the
1937 // symbol and possiblity add an annotation for a symbol stub.
David Blaikie33dd45d02015-03-23 18:39:02 +00001938 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
1939 r_type == MachO::ARM_THUMB_RELOC_BR22))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001940 return 0;
1941
1942 uint32_t offset = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001943 if (r_type == MachO::ARM_RELOC_HALF ||
1944 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1945 if ((r_length & 0x1) == 1)
1946 value = value << 16 | other_half;
1947 else
1948 value = other_half << 16 | value;
1949 }
1950 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
1951 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
1952 offset = value - r_value;
1953 value = r_value;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001954 }
1955
David Blaikie33dd45d02015-03-23 18:39:02 +00001956 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001957 if ((r_length & 0x1) == 1)
1958 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1959 else
1960 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001961 const char *add = GuessSymbolName(r_value, info->AddrMap);
1962 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00001963 int32_t offset = value - (r_value - pair_r_value);
1964 op_info->AddSymbol.Present = 1;
1965 if (add != nullptr)
1966 op_info->AddSymbol.Name = add;
1967 else
1968 op_info->AddSymbol.Value = r_value;
1969 op_info->SubtractSymbol.Present = 1;
1970 if (sub != nullptr)
1971 op_info->SubtractSymbol.Name = sub;
1972 else
1973 op_info->SubtractSymbol.Value = pair_r_value;
1974 op_info->Value = offset;
1975 return 1;
1976 }
1977
Kevin Enderby930fdc72014-11-06 19:00:13 +00001978 op_info->AddSymbol.Present = 1;
1979 op_info->Value = offset;
David Blaikie33dd45d02015-03-23 18:39:02 +00001980 if (r_type == MachO::ARM_RELOC_HALF) {
1981 if ((r_length & 0x1) == 1)
1982 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1983 else
1984 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001985 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001986 const char *add = GuessSymbolName(value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00001987 if (add != nullptr) {
1988 op_info->AddSymbol.Name = add;
1989 return 1;
1990 }
1991 op_info->AddSymbol.Value = value;
1992 return 1;
David Blaikie33dd45d02015-03-23 18:39:02 +00001993 }
1994 if (Arch == Triple::aarch64) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001995 if (Offset != 0 || Size != 4)
1996 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001997 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1998 // TODO:
1999 // Search the external relocation entries of a fully linked image
2000 // (if any) for an entry that matches this segment offset.
2001 // uint64_t seg_offset = (Pc + Offset);
2002 return 0;
2003 }
2004 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2005 // for an entry for this section offset.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002006 uint64_t sect_addr = info->S.getAddress();
2007 uint64_t sect_offset = (Pc + Offset) - sect_addr;
David Blaikie33dd45d02015-03-23 18:39:02 +00002008 auto Reloc =
2009 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2010 [&](const RelocationRef &Reloc) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002011 uint64_t RelocOffset = Reloc.getOffset();
David Blaikie33dd45d02015-03-23 18:39:02 +00002012 return RelocOffset == sect_offset;
2013 });
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002014
David Blaikie33dd45d02015-03-23 18:39:02 +00002015 if (Reloc == info->S.relocations().end())
2016 return 0;
2017
2018 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2019 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2020 uint32_t r_type = info->O->getAnyRelocationType(RE);
2021 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2022 DataRefImpl RelNext = Rel;
2023 info->O->moveRelocationNext(RelNext);
2024 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2025 if (value == 0) {
2026 value = info->O->getPlainRelocationSymbolNum(RENext);
2027 op_info->Value = value;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002028 }
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002029 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002030 // NOTE: Scattered relocations don't exist on arm64.
2031 if (!info->O->getPlainRelocationExternal(RE))
2032 return 0;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002033 ErrorOr<StringRef> SymName = Reloc->getSymbol()->getName();
2034 if (std::error_code EC = SymName.getError())
2035 report_fatal_error(EC.message());
2036 const char *name = SymName->data();
David Blaikie33dd45d02015-03-23 18:39:02 +00002037 op_info->AddSymbol.Present = 1;
2038 op_info->AddSymbol.Name = name;
2039
2040 switch (r_type) {
2041 case MachO::ARM64_RELOC_PAGE21:
2042 /* @page */
2043 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2044 break;
2045 case MachO::ARM64_RELOC_PAGEOFF12:
2046 /* @pageoff */
2047 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2048 break;
2049 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2050 /* @gotpage */
2051 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2052 break;
2053 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2054 /* @gotpageoff */
2055 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2056 break;
2057 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2058 /* @tvlppage is not implemented in llvm-mc */
2059 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2060 break;
2061 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2062 /* @tvlppageoff is not implemented in llvm-mc */
2063 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2064 break;
2065 default:
2066 case MachO::ARM64_RELOC_BRANCH26:
2067 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2068 break;
2069 }
2070 return 1;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002071 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002072 return 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002073}
2074
Kevin Enderbybf246f52014-09-24 23:08:22 +00002075// GuessCstringPointer is passed the address of what might be a pointer to a
2076// literal string in a cstring section. If that address is in a cstring section
2077// it returns a pointer to that string. Else it returns nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002078static const char *GuessCstringPointer(uint64_t ReferenceValue,
2079 struct DisassembleInfo *info) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002080 for (const auto &Load : info->O->load_commands()) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00002081 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2082 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2083 for (unsigned J = 0; J < Seg.nsects; ++J) {
2084 MachO::section_64 Sec = info->O->getSection64(Load, J);
2085 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2086 if (section_type == MachO::S_CSTRING_LITERALS &&
2087 ReferenceValue >= Sec.addr &&
2088 ReferenceValue < Sec.addr + Sec.size) {
2089 uint64_t sect_offset = ReferenceValue - Sec.addr;
2090 uint64_t object_offset = Sec.offset + sect_offset;
2091 StringRef MachOContents = info->O->getData();
2092 uint64_t object_size = MachOContents.size();
2093 const char *object_addr = (const char *)MachOContents.data();
2094 if (object_offset < object_size) {
2095 const char *name = object_addr + object_offset;
2096 return name;
2097 } else {
2098 return nullptr;
2099 }
2100 }
2101 }
2102 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2103 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2104 for (unsigned J = 0; J < Seg.nsects; ++J) {
2105 MachO::section Sec = info->O->getSection(Load, J);
2106 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2107 if (section_type == MachO::S_CSTRING_LITERALS &&
2108 ReferenceValue >= Sec.addr &&
2109 ReferenceValue < Sec.addr + Sec.size) {
2110 uint64_t sect_offset = ReferenceValue - Sec.addr;
2111 uint64_t object_offset = Sec.offset + sect_offset;
2112 StringRef MachOContents = info->O->getData();
2113 uint64_t object_size = MachOContents.size();
2114 const char *object_addr = (const char *)MachOContents.data();
2115 if (object_offset < object_size) {
2116 const char *name = object_addr + object_offset;
2117 return name;
2118 } else {
2119 return nullptr;
2120 }
2121 }
2122 }
2123 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00002124 }
2125 return nullptr;
2126}
2127
Kevin Enderby85974882014-09-26 22:20:44 +00002128// GuessIndirectSymbol returns the name of the indirect symbol for the
2129// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2130// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2131// symbol name being referenced by the stub or pointer.
2132static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2133 struct DisassembleInfo *info) {
Kevin Enderby85974882014-09-26 22:20:44 +00002134 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2135 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002136 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby85974882014-09-26 22:20:44 +00002137 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2138 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2139 for (unsigned J = 0; J < Seg.nsects; ++J) {
2140 MachO::section_64 Sec = info->O->getSection64(Load, J);
2141 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2142 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2143 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2144 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2145 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2146 section_type == MachO::S_SYMBOL_STUBS) &&
2147 ReferenceValue >= Sec.addr &&
2148 ReferenceValue < Sec.addr + Sec.size) {
2149 uint32_t stride;
2150 if (section_type == MachO::S_SYMBOL_STUBS)
2151 stride = Sec.reserved2;
2152 else
2153 stride = 8;
2154 if (stride == 0)
2155 return nullptr;
2156 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2157 if (index < Dysymtab.nindirectsyms) {
2158 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002159 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002160 if (indirect_symbol < Symtab.nsyms) {
2161 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2162 SymbolRef Symbol = *Sym;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002163 ErrorOr<StringRef> SymName = Symbol.getName();
2164 if (std::error_code EC = SymName.getError())
2165 report_fatal_error(EC.message());
2166 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002167 return name;
2168 }
2169 }
2170 }
2171 }
2172 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2173 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2174 for (unsigned J = 0; J < Seg.nsects; ++J) {
2175 MachO::section Sec = info->O->getSection(Load, J);
2176 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2177 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2178 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2179 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2180 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2181 section_type == MachO::S_SYMBOL_STUBS) &&
2182 ReferenceValue >= Sec.addr &&
2183 ReferenceValue < Sec.addr + Sec.size) {
2184 uint32_t stride;
2185 if (section_type == MachO::S_SYMBOL_STUBS)
2186 stride = Sec.reserved2;
2187 else
2188 stride = 4;
2189 if (stride == 0)
2190 return nullptr;
2191 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2192 if (index < Dysymtab.nindirectsyms) {
2193 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002194 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002195 if (indirect_symbol < Symtab.nsyms) {
2196 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2197 SymbolRef Symbol = *Sym;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002198 ErrorOr<StringRef> SymName = Symbol.getName();
2199 if (std::error_code EC = SymName.getError())
2200 report_fatal_error(EC.message());
2201 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002202 return name;
2203 }
2204 }
2205 }
2206 }
2207 }
Kevin Enderby85974882014-09-26 22:20:44 +00002208 }
2209 return nullptr;
2210}
2211
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002212// method_reference() is called passing it the ReferenceName that might be
2213// a reference it to an Objective-C method call. If so then it allocates and
2214// assembles a method call string with the values last seen and saved in
2215// the DisassembleInfo's class_name and selector_name fields. This is saved
2216// into the method field of the info and any previous string is free'ed.
2217// Then the class_name field in the info is set to nullptr. The method call
2218// string is set into ReferenceName and ReferenceType is set to
2219// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2220// then both ReferenceType and ReferenceName are left unchanged.
2221static void method_reference(struct DisassembleInfo *info,
2222 uint64_t *ReferenceType,
2223 const char **ReferenceName) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002224 unsigned int Arch = info->O->getArch();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002225 if (*ReferenceName != nullptr) {
2226 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002227 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002228 if (info->method != nullptr)
2229 free(info->method);
2230 if (info->class_name != nullptr) {
2231 info->method = (char *)malloc(5 + strlen(info->class_name) +
2232 strlen(info->selector_name));
2233 if (info->method != nullptr) {
2234 strcpy(info->method, "+[");
2235 strcat(info->method, info->class_name);
2236 strcat(info->method, " ");
2237 strcat(info->method, info->selector_name);
2238 strcat(info->method, "]");
2239 *ReferenceName = info->method;
2240 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2241 }
2242 } else {
2243 info->method = (char *)malloc(9 + strlen(info->selector_name));
2244 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002245 if (Arch == Triple::x86_64)
2246 strcpy(info->method, "-[%rdi ");
2247 else if (Arch == Triple::aarch64)
2248 strcpy(info->method, "-[x0 ");
2249 else
2250 strcpy(info->method, "-[r? ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002251 strcat(info->method, info->selector_name);
2252 strcat(info->method, "]");
2253 *ReferenceName = info->method;
2254 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2255 }
2256 }
2257 info->class_name = nullptr;
2258 }
2259 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002260 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002261 if (info->method != nullptr)
2262 free(info->method);
2263 info->method = (char *)malloc(17 + strlen(info->selector_name));
2264 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002265 if (Arch == Triple::x86_64)
2266 strcpy(info->method, "-[[%rdi super] ");
2267 else if (Arch == Triple::aarch64)
2268 strcpy(info->method, "-[[x0 super] ");
2269 else
2270 strcpy(info->method, "-[[r? super] ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002271 strcat(info->method, info->selector_name);
2272 strcat(info->method, "]");
2273 *ReferenceName = info->method;
2274 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2275 }
2276 info->class_name = nullptr;
2277 }
2278 }
2279 }
2280}
2281
2282// GuessPointerPointer() is passed the address of what might be a pointer to
2283// a reference to an Objective-C class, selector, message ref or cfstring.
2284// If so the value of the pointer is returned and one of the booleans are set
2285// to true. If not zero is returned and all the booleans are set to false.
2286static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2287 struct DisassembleInfo *info,
2288 bool &classref, bool &selref, bool &msgref,
2289 bool &cfstring) {
2290 classref = false;
2291 selref = false;
2292 msgref = false;
2293 cfstring = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002294 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002295 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2296 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2297 for (unsigned J = 0; J < Seg.nsects; ++J) {
2298 MachO::section_64 Sec = info->O->getSection64(Load, J);
2299 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2300 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2301 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2302 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2303 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2304 ReferenceValue >= Sec.addr &&
2305 ReferenceValue < Sec.addr + Sec.size) {
2306 uint64_t sect_offset = ReferenceValue - Sec.addr;
2307 uint64_t object_offset = Sec.offset + sect_offset;
2308 StringRef MachOContents = info->O->getData();
2309 uint64_t object_size = MachOContents.size();
2310 const char *object_addr = (const char *)MachOContents.data();
2311 if (object_offset < object_size) {
2312 uint64_t pointer_value;
2313 memcpy(&pointer_value, object_addr + object_offset,
2314 sizeof(uint64_t));
2315 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2316 sys::swapByteOrder(pointer_value);
2317 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2318 selref = true;
2319 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2320 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2321 classref = true;
2322 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2323 ReferenceValue + 8 < Sec.addr + Sec.size) {
2324 msgref = true;
2325 memcpy(&pointer_value, object_addr + object_offset + 8,
2326 sizeof(uint64_t));
2327 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2328 sys::swapByteOrder(pointer_value);
2329 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2330 cfstring = true;
2331 return pointer_value;
2332 } else {
2333 return 0;
2334 }
2335 }
2336 }
2337 }
2338 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002339 }
2340 return 0;
2341}
2342
2343// get_pointer_64 returns a pointer to the bytes in the object file at the
2344// Address from a section in the Mach-O file. And indirectly returns the
2345// offset into the section, number of bytes left in the section past the offset
2346// and which section is was being referenced. If the Address is not in a
2347// section nullptr is returned.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002348static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2349 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002350 DisassembleInfo *info,
2351 bool objc_only = false) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002352 offset = 0;
2353 left = 0;
2354 S = SectionRef();
2355 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2356 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2357 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
Kevin Enderby46e642f2015-10-08 22:50:55 +00002358 if (SectSize == 0)
2359 continue;
Kevin Enderby846c0002015-04-16 17:19:59 +00002360 if (objc_only) {
2361 StringRef SectName;
2362 ((*(info->Sections))[SectIdx]).getName(SectName);
2363 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2364 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2365 if (SegName != "__OBJC" && SectName != "__cstring")
2366 continue;
2367 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002368 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2369 S = (*(info->Sections))[SectIdx];
2370 offset = Address - SectAddress;
2371 left = SectSize - offset;
2372 StringRef SectContents;
2373 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2374 return SectContents.data() + offset;
2375 }
2376 }
2377 return nullptr;
2378}
2379
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002380static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2381 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002382 DisassembleInfo *info,
2383 bool objc_only = false) {
2384 return get_pointer_64(Address, offset, left, S, info, objc_only);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002385}
2386
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002387// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2388// the symbol indirectly through n_value. Based on the relocation information
2389// for the specified section offset in the specified section reference.
Kevin Enderby0fc11822015-04-01 20:57:01 +00002390// If no relocation information is found and a non-zero ReferenceValue for the
2391// symbol is passed, look up that address in the info's AddrMap.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002392static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2393 DisassembleInfo *info, uint64_t &n_value,
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002394 uint64_t ReferenceValue = 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002395 n_value = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002396 if (!info->verbose)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002397 return nullptr;
2398
2399 // See if there is an external relocation entry at the sect_offset.
2400 bool reloc_found = false;
2401 DataRefImpl Rel;
2402 MachO::any_relocation_info RE;
2403 bool isExtern = false;
2404 SymbolRef Symbol;
2405 for (const RelocationRef &Reloc : S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002406 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002407 if (RelocOffset == sect_offset) {
2408 Rel = Reloc.getRawDataRefImpl();
2409 RE = info->O->getRelocation(Rel);
2410 if (info->O->isRelocationScattered(RE))
2411 continue;
2412 isExtern = info->O->getPlainRelocationExternal(RE);
2413 if (isExtern) {
2414 symbol_iterator RelocSym = Reloc.getSymbol();
2415 Symbol = *RelocSym;
2416 }
2417 reloc_found = true;
2418 break;
2419 }
2420 }
2421 // If there is an external relocation entry for a symbol in this section
2422 // at this section_offset then use that symbol's value for the n_value
2423 // and return its name.
2424 const char *SymbolName = nullptr;
2425 if (reloc_found && isExtern) {
Rafael Espindoladea00162015-07-03 17:44:18 +00002426 n_value = Symbol.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002427 ErrorOr<StringRef> NameOrError = Symbol.getName();
2428 if (std::error_code EC = NameOrError.getError())
2429 report_fatal_error(EC.message());
2430 StringRef Name = *NameOrError;
2431 if (!Name.empty()) {
2432 SymbolName = Name.data();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002433 return SymbolName;
2434 }
2435 }
2436
2437 // TODO: For fully linked images, look through the external relocation
2438 // entries off the dynamic symtab command. For these the r_offset is from the
2439 // start of the first writeable segment in the Mach-O file. So the offset
2440 // to this section from that segment is passed to this routine by the caller,
2441 // as the database_offset. Which is the difference of the section's starting
2442 // address and the first writable segment.
2443 //
2444 // NOTE: need add passing the database_offset to this routine.
2445
Kevin Enderby0fc11822015-04-01 20:57:01 +00002446 // We did not find an external relocation entry so look up the ReferenceValue
2447 // as an address of a symbol and if found return that symbol's name.
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002448 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002449
2450 return SymbolName;
2451}
2452
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002453static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2454 DisassembleInfo *info,
2455 uint32_t ReferenceValue) {
2456 uint64_t n_value64;
2457 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2458}
2459
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002460// These are structs in the Objective-C meta data and read to produce the
2461// comments for disassembly. While these are part of the ABI they are no
2462// public defintions. So the are here not in include/llvm/Support/MachO.h .
2463
2464// The cfstring object in a 64-bit Mach-O file.
2465struct cfstring64_t {
2466 uint64_t isa; // class64_t * (64-bit pointer)
2467 uint64_t flags; // flag bits
2468 uint64_t characters; // char * (64-bit pointer)
2469 uint64_t length; // number of non-NULL characters in above
2470};
2471
2472// The class object in a 64-bit Mach-O file.
2473struct class64_t {
2474 uint64_t isa; // class64_t * (64-bit pointer)
2475 uint64_t superclass; // class64_t * (64-bit pointer)
2476 uint64_t cache; // Cache (64-bit pointer)
2477 uint64_t vtable; // IMP * (64-bit pointer)
2478 uint64_t data; // class_ro64_t * (64-bit pointer)
2479};
2480
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002481struct class32_t {
2482 uint32_t isa; /* class32_t * (32-bit pointer) */
2483 uint32_t superclass; /* class32_t * (32-bit pointer) */
2484 uint32_t cache; /* Cache (32-bit pointer) */
2485 uint32_t vtable; /* IMP * (32-bit pointer) */
2486 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2487};
2488
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002489struct class_ro64_t {
2490 uint32_t flags;
2491 uint32_t instanceStart;
2492 uint32_t instanceSize;
2493 uint32_t reserved;
2494 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2495 uint64_t name; // const char * (64-bit pointer)
2496 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2497 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2498 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2499 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2500 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2501};
2502
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002503struct class_ro32_t {
2504 uint32_t flags;
2505 uint32_t instanceStart;
2506 uint32_t instanceSize;
2507 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2508 uint32_t name; /* const char * (32-bit pointer) */
2509 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2510 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2511 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2512 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2513 uint32_t baseProperties; /* const struct objc_property_list *
2514 (32-bit pointer) */
2515};
2516
2517/* Values for class_ro{64,32}_t->flags */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002518#define RO_META (1 << 0)
2519#define RO_ROOT (1 << 1)
2520#define RO_HAS_CXX_STRUCTORS (1 << 2)
2521
2522struct method_list64_t {
2523 uint32_t entsize;
2524 uint32_t count;
2525 /* struct method64_t first; These structures follow inline */
2526};
2527
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002528struct method_list32_t {
2529 uint32_t entsize;
2530 uint32_t count;
2531 /* struct method32_t first; These structures follow inline */
2532};
2533
Kevin Enderby0fc11822015-04-01 20:57:01 +00002534struct method64_t {
2535 uint64_t name; /* SEL (64-bit pointer) */
2536 uint64_t types; /* const char * (64-bit pointer) */
2537 uint64_t imp; /* IMP (64-bit pointer) */
2538};
2539
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002540struct method32_t {
2541 uint32_t name; /* SEL (32-bit pointer) */
2542 uint32_t types; /* const char * (32-bit pointer) */
2543 uint32_t imp; /* IMP (32-bit pointer) */
2544};
2545
Kevin Enderby0fc11822015-04-01 20:57:01 +00002546struct protocol_list64_t {
2547 uint64_t count; /* uintptr_t (a 64-bit value) */
2548 /* struct protocol64_t * list[0]; These pointers follow inline */
2549};
2550
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002551struct protocol_list32_t {
2552 uint32_t count; /* uintptr_t (a 32-bit value) */
2553 /* struct protocol32_t * list[0]; These pointers follow inline */
2554};
2555
Kevin Enderby0fc11822015-04-01 20:57:01 +00002556struct protocol64_t {
2557 uint64_t isa; /* id * (64-bit pointer) */
2558 uint64_t name; /* const char * (64-bit pointer) */
2559 uint64_t protocols; /* struct protocol_list64_t *
2560 (64-bit pointer) */
2561 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2562 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2563 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2564 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2565 uint64_t instanceProperties; /* struct objc_property_list *
2566 (64-bit pointer) */
2567};
2568
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002569struct protocol32_t {
2570 uint32_t isa; /* id * (32-bit pointer) */
2571 uint32_t name; /* const char * (32-bit pointer) */
2572 uint32_t protocols; /* struct protocol_list_t *
2573 (32-bit pointer) */
2574 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2575 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2576 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2577 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2578 uint32_t instanceProperties; /* struct objc_property_list *
2579 (32-bit pointer) */
2580};
2581
Kevin Enderby0fc11822015-04-01 20:57:01 +00002582struct ivar_list64_t {
2583 uint32_t entsize;
2584 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002585 /* struct ivar64_t first; These structures follow inline */
2586};
2587
2588struct ivar_list32_t {
2589 uint32_t entsize;
2590 uint32_t count;
2591 /* struct ivar32_t first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002592};
2593
2594struct ivar64_t {
2595 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2596 uint64_t name; /* const char * (64-bit pointer) */
2597 uint64_t type; /* const char * (64-bit pointer) */
2598 uint32_t alignment;
2599 uint32_t size;
2600};
2601
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002602struct ivar32_t {
2603 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2604 uint32_t name; /* const char * (32-bit pointer) */
2605 uint32_t type; /* const char * (32-bit pointer) */
2606 uint32_t alignment;
2607 uint32_t size;
2608};
2609
Kevin Enderby0fc11822015-04-01 20:57:01 +00002610struct objc_property_list64 {
2611 uint32_t entsize;
2612 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002613 /* struct objc_property64 first; These structures follow inline */
2614};
2615
2616struct objc_property_list32 {
2617 uint32_t entsize;
2618 uint32_t count;
2619 /* struct objc_property32 first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002620};
2621
2622struct objc_property64 {
2623 uint64_t name; /* const char * (64-bit pointer) */
2624 uint64_t attributes; /* const char * (64-bit pointer) */
2625};
2626
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002627struct objc_property32 {
2628 uint32_t name; /* const char * (32-bit pointer) */
2629 uint32_t attributes; /* const char * (32-bit pointer) */
2630};
2631
Kevin Enderby0fc11822015-04-01 20:57:01 +00002632struct category64_t {
2633 uint64_t name; /* const char * (64-bit pointer) */
2634 uint64_t cls; /* struct class_t * (64-bit pointer) */
2635 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2636 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2637 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2638 uint64_t instanceProperties; /* struct objc_property_list *
2639 (64-bit pointer) */
2640};
2641
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002642struct category32_t {
2643 uint32_t name; /* const char * (32-bit pointer) */
2644 uint32_t cls; /* struct class_t * (32-bit pointer) */
2645 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2646 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2647 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2648 uint32_t instanceProperties; /* struct objc_property_list *
2649 (32-bit pointer) */
2650};
2651
Kevin Enderby0fc11822015-04-01 20:57:01 +00002652struct objc_image_info64 {
2653 uint32_t version;
2654 uint32_t flags;
2655};
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002656struct objc_image_info32 {
2657 uint32_t version;
2658 uint32_t flags;
2659};
Kevin Enderby846c0002015-04-16 17:19:59 +00002660struct imageInfo_t {
2661 uint32_t version;
2662 uint32_t flags;
2663};
Kevin Enderby0fc11822015-04-01 20:57:01 +00002664/* masks for objc_image_info.flags */
2665#define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2666#define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2667
2668struct message_ref64 {
2669 uint64_t imp; /* IMP (64-bit pointer) */
2670 uint64_t sel; /* SEL (64-bit pointer) */
2671};
2672
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002673struct message_ref32 {
2674 uint32_t imp; /* IMP (32-bit pointer) */
2675 uint32_t sel; /* SEL (32-bit pointer) */
2676};
2677
Kevin Enderby846c0002015-04-16 17:19:59 +00002678// Objective-C 1 (32-bit only) meta data structs.
2679
2680struct objc_module_t {
2681 uint32_t version;
2682 uint32_t size;
2683 uint32_t name; /* char * (32-bit pointer) */
2684 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2685};
2686
2687struct objc_symtab_t {
2688 uint32_t sel_ref_cnt;
2689 uint32_t refs; /* SEL * (32-bit pointer) */
2690 uint16_t cls_def_cnt;
2691 uint16_t cat_def_cnt;
2692 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2693};
2694
2695struct objc_class_t {
2696 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2697 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2698 uint32_t name; /* const char * (32-bit pointer) */
2699 int32_t version;
2700 int32_t info;
2701 int32_t instance_size;
2702 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2703 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2704 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2705 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2706};
2707
2708#define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2709// class is not a metaclass
2710#define CLS_CLASS 0x1
2711// class is a metaclass
2712#define CLS_META 0x2
2713
2714struct objc_category_t {
2715 uint32_t category_name; /* char * (32-bit pointer) */
2716 uint32_t class_name; /* char * (32-bit pointer) */
2717 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2718 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2719 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2720};
2721
2722struct objc_ivar_t {
2723 uint32_t ivar_name; /* char * (32-bit pointer) */
2724 uint32_t ivar_type; /* char * (32-bit pointer) */
2725 int32_t ivar_offset;
2726};
2727
2728struct objc_ivar_list_t {
2729 int32_t ivar_count;
2730 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2731};
2732
2733struct objc_method_list_t {
2734 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2735 int32_t method_count;
2736 // struct objc_method_t method_list[1]; /* variable length structure */
2737};
2738
2739struct objc_method_t {
2740 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2741 uint32_t method_types; /* char * (32-bit pointer) */
2742 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2743 (32-bit pointer) */
2744};
2745
2746struct objc_protocol_list_t {
2747 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2748 int32_t count;
2749 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2750 // (32-bit pointer) */
2751};
2752
2753struct objc_protocol_t {
2754 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2755 uint32_t protocol_name; /* char * (32-bit pointer) */
2756 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2757 uint32_t instance_methods; /* struct objc_method_description_list *
2758 (32-bit pointer) */
2759 uint32_t class_methods; /* struct objc_method_description_list *
2760 (32-bit pointer) */
2761};
2762
2763struct objc_method_description_list_t {
2764 int32_t count;
2765 // struct objc_method_description_t list[1];
2766};
2767
2768struct objc_method_description_t {
2769 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2770 uint32_t types; /* char * (32-bit pointer) */
2771};
2772
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002773inline void swapStruct(struct cfstring64_t &cfs) {
2774 sys::swapByteOrder(cfs.isa);
2775 sys::swapByteOrder(cfs.flags);
2776 sys::swapByteOrder(cfs.characters);
2777 sys::swapByteOrder(cfs.length);
2778}
2779
2780inline void swapStruct(struct class64_t &c) {
2781 sys::swapByteOrder(c.isa);
2782 sys::swapByteOrder(c.superclass);
2783 sys::swapByteOrder(c.cache);
2784 sys::swapByteOrder(c.vtable);
2785 sys::swapByteOrder(c.data);
2786}
2787
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002788inline void swapStruct(struct class32_t &c) {
2789 sys::swapByteOrder(c.isa);
2790 sys::swapByteOrder(c.superclass);
2791 sys::swapByteOrder(c.cache);
2792 sys::swapByteOrder(c.vtable);
2793 sys::swapByteOrder(c.data);
2794}
2795
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002796inline void swapStruct(struct class_ro64_t &cro) {
2797 sys::swapByteOrder(cro.flags);
2798 sys::swapByteOrder(cro.instanceStart);
2799 sys::swapByteOrder(cro.instanceSize);
2800 sys::swapByteOrder(cro.reserved);
2801 sys::swapByteOrder(cro.ivarLayout);
2802 sys::swapByteOrder(cro.name);
2803 sys::swapByteOrder(cro.baseMethods);
2804 sys::swapByteOrder(cro.baseProtocols);
2805 sys::swapByteOrder(cro.ivars);
2806 sys::swapByteOrder(cro.weakIvarLayout);
2807 sys::swapByteOrder(cro.baseProperties);
2808}
2809
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002810inline void swapStruct(struct class_ro32_t &cro) {
2811 sys::swapByteOrder(cro.flags);
2812 sys::swapByteOrder(cro.instanceStart);
2813 sys::swapByteOrder(cro.instanceSize);
2814 sys::swapByteOrder(cro.ivarLayout);
2815 sys::swapByteOrder(cro.name);
2816 sys::swapByteOrder(cro.baseMethods);
2817 sys::swapByteOrder(cro.baseProtocols);
2818 sys::swapByteOrder(cro.ivars);
2819 sys::swapByteOrder(cro.weakIvarLayout);
2820 sys::swapByteOrder(cro.baseProperties);
2821}
2822
Kevin Enderby0fc11822015-04-01 20:57:01 +00002823inline void swapStruct(struct method_list64_t &ml) {
2824 sys::swapByteOrder(ml.entsize);
2825 sys::swapByteOrder(ml.count);
2826}
2827
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002828inline void swapStruct(struct method_list32_t &ml) {
2829 sys::swapByteOrder(ml.entsize);
2830 sys::swapByteOrder(ml.count);
2831}
2832
Kevin Enderby0fc11822015-04-01 20:57:01 +00002833inline void swapStruct(struct method64_t &m) {
2834 sys::swapByteOrder(m.name);
2835 sys::swapByteOrder(m.types);
2836 sys::swapByteOrder(m.imp);
2837}
2838
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002839inline void swapStruct(struct method32_t &m) {
2840 sys::swapByteOrder(m.name);
2841 sys::swapByteOrder(m.types);
2842 sys::swapByteOrder(m.imp);
2843}
2844
Kevin Enderby0fc11822015-04-01 20:57:01 +00002845inline void swapStruct(struct protocol_list64_t &pl) {
2846 sys::swapByteOrder(pl.count);
2847}
2848
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002849inline void swapStruct(struct protocol_list32_t &pl) {
2850 sys::swapByteOrder(pl.count);
2851}
2852
Kevin Enderby0fc11822015-04-01 20:57:01 +00002853inline void swapStruct(struct protocol64_t &p) {
2854 sys::swapByteOrder(p.isa);
2855 sys::swapByteOrder(p.name);
2856 sys::swapByteOrder(p.protocols);
2857 sys::swapByteOrder(p.instanceMethods);
2858 sys::swapByteOrder(p.classMethods);
2859 sys::swapByteOrder(p.optionalInstanceMethods);
2860 sys::swapByteOrder(p.optionalClassMethods);
2861 sys::swapByteOrder(p.instanceProperties);
2862}
2863
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002864inline void swapStruct(struct protocol32_t &p) {
2865 sys::swapByteOrder(p.isa);
2866 sys::swapByteOrder(p.name);
2867 sys::swapByteOrder(p.protocols);
2868 sys::swapByteOrder(p.instanceMethods);
2869 sys::swapByteOrder(p.classMethods);
2870 sys::swapByteOrder(p.optionalInstanceMethods);
2871 sys::swapByteOrder(p.optionalClassMethods);
2872 sys::swapByteOrder(p.instanceProperties);
2873}
2874
Kevin Enderby0fc11822015-04-01 20:57:01 +00002875inline void swapStruct(struct ivar_list64_t &il) {
2876 sys::swapByteOrder(il.entsize);
2877 sys::swapByteOrder(il.count);
2878}
2879
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002880inline void swapStruct(struct ivar_list32_t &il) {
2881 sys::swapByteOrder(il.entsize);
2882 sys::swapByteOrder(il.count);
2883}
2884
Kevin Enderby0fc11822015-04-01 20:57:01 +00002885inline void swapStruct(struct ivar64_t &i) {
2886 sys::swapByteOrder(i.offset);
2887 sys::swapByteOrder(i.name);
2888 sys::swapByteOrder(i.type);
2889 sys::swapByteOrder(i.alignment);
2890 sys::swapByteOrder(i.size);
2891}
2892
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002893inline void swapStruct(struct ivar32_t &i) {
2894 sys::swapByteOrder(i.offset);
2895 sys::swapByteOrder(i.name);
2896 sys::swapByteOrder(i.type);
2897 sys::swapByteOrder(i.alignment);
2898 sys::swapByteOrder(i.size);
2899}
2900
Kevin Enderby0fc11822015-04-01 20:57:01 +00002901inline void swapStruct(struct objc_property_list64 &pl) {
2902 sys::swapByteOrder(pl.entsize);
2903 sys::swapByteOrder(pl.count);
2904}
2905
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002906inline void swapStruct(struct objc_property_list32 &pl) {
2907 sys::swapByteOrder(pl.entsize);
2908 sys::swapByteOrder(pl.count);
2909}
2910
Kevin Enderby0fc11822015-04-01 20:57:01 +00002911inline void swapStruct(struct objc_property64 &op) {
2912 sys::swapByteOrder(op.name);
2913 sys::swapByteOrder(op.attributes);
2914}
2915
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002916inline void swapStruct(struct objc_property32 &op) {
2917 sys::swapByteOrder(op.name);
2918 sys::swapByteOrder(op.attributes);
2919}
2920
Kevin Enderby0fc11822015-04-01 20:57:01 +00002921inline void swapStruct(struct category64_t &c) {
2922 sys::swapByteOrder(c.name);
2923 sys::swapByteOrder(c.cls);
2924 sys::swapByteOrder(c.instanceMethods);
2925 sys::swapByteOrder(c.classMethods);
2926 sys::swapByteOrder(c.protocols);
2927 sys::swapByteOrder(c.instanceProperties);
2928}
2929
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002930inline void swapStruct(struct category32_t &c) {
2931 sys::swapByteOrder(c.name);
2932 sys::swapByteOrder(c.cls);
2933 sys::swapByteOrder(c.instanceMethods);
2934 sys::swapByteOrder(c.classMethods);
2935 sys::swapByteOrder(c.protocols);
2936 sys::swapByteOrder(c.instanceProperties);
2937}
2938
Kevin Enderby0fc11822015-04-01 20:57:01 +00002939inline void swapStruct(struct objc_image_info64 &o) {
2940 sys::swapByteOrder(o.version);
2941 sys::swapByteOrder(o.flags);
2942}
2943
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002944inline void swapStruct(struct objc_image_info32 &o) {
2945 sys::swapByteOrder(o.version);
2946 sys::swapByteOrder(o.flags);
2947}
2948
Kevin Enderby846c0002015-04-16 17:19:59 +00002949inline void swapStruct(struct imageInfo_t &o) {
2950 sys::swapByteOrder(o.version);
2951 sys::swapByteOrder(o.flags);
2952}
2953
Kevin Enderby0fc11822015-04-01 20:57:01 +00002954inline void swapStruct(struct message_ref64 &mr) {
2955 sys::swapByteOrder(mr.imp);
2956 sys::swapByteOrder(mr.sel);
2957}
2958
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002959inline void swapStruct(struct message_ref32 &mr) {
2960 sys::swapByteOrder(mr.imp);
2961 sys::swapByteOrder(mr.sel);
2962}
2963
Kevin Enderby846c0002015-04-16 17:19:59 +00002964inline void swapStruct(struct objc_module_t &module) {
2965 sys::swapByteOrder(module.version);
2966 sys::swapByteOrder(module.size);
2967 sys::swapByteOrder(module.name);
2968 sys::swapByteOrder(module.symtab);
Jingyue Wufedecc42015-04-16 18:43:44 +00002969}
Kevin Enderby846c0002015-04-16 17:19:59 +00002970
2971inline void swapStruct(struct objc_symtab_t &symtab) {
2972 sys::swapByteOrder(symtab.sel_ref_cnt);
2973 sys::swapByteOrder(symtab.refs);
2974 sys::swapByteOrder(symtab.cls_def_cnt);
2975 sys::swapByteOrder(symtab.cat_def_cnt);
Jingyue Wufedecc42015-04-16 18:43:44 +00002976}
Kevin Enderby846c0002015-04-16 17:19:59 +00002977
2978inline void swapStruct(struct objc_class_t &objc_class) {
2979 sys::swapByteOrder(objc_class.isa);
2980 sys::swapByteOrder(objc_class.super_class);
2981 sys::swapByteOrder(objc_class.name);
2982 sys::swapByteOrder(objc_class.version);
2983 sys::swapByteOrder(objc_class.info);
2984 sys::swapByteOrder(objc_class.instance_size);
2985 sys::swapByteOrder(objc_class.ivars);
2986 sys::swapByteOrder(objc_class.methodLists);
2987 sys::swapByteOrder(objc_class.cache);
2988 sys::swapByteOrder(objc_class.protocols);
Jingyue Wufedecc42015-04-16 18:43:44 +00002989}
Kevin Enderby846c0002015-04-16 17:19:59 +00002990
2991inline void swapStruct(struct objc_category_t &objc_category) {
2992 sys::swapByteOrder(objc_category.category_name);
2993 sys::swapByteOrder(objc_category.class_name);
2994 sys::swapByteOrder(objc_category.instance_methods);
2995 sys::swapByteOrder(objc_category.class_methods);
2996 sys::swapByteOrder(objc_category.protocols);
2997}
2998
2999inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3000 sys::swapByteOrder(objc_ivar_list.ivar_count);
3001}
3002
3003inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3004 sys::swapByteOrder(objc_ivar.ivar_name);
3005 sys::swapByteOrder(objc_ivar.ivar_type);
3006 sys::swapByteOrder(objc_ivar.ivar_offset);
Jingyue Wufedecc42015-04-16 18:43:44 +00003007}
Kevin Enderby846c0002015-04-16 17:19:59 +00003008
3009inline void swapStruct(struct objc_method_list_t &method_list) {
3010 sys::swapByteOrder(method_list.obsolete);
3011 sys::swapByteOrder(method_list.method_count);
3012}
3013
3014inline void swapStruct(struct objc_method_t &method) {
3015 sys::swapByteOrder(method.method_name);
3016 sys::swapByteOrder(method.method_types);
3017 sys::swapByteOrder(method.method_imp);
3018}
3019
3020inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3021 sys::swapByteOrder(protocol_list.next);
3022 sys::swapByteOrder(protocol_list.count);
3023}
3024
3025inline void swapStruct(struct objc_protocol_t &protocol) {
3026 sys::swapByteOrder(protocol.isa);
3027 sys::swapByteOrder(protocol.protocol_name);
3028 sys::swapByteOrder(protocol.protocol_list);
3029 sys::swapByteOrder(protocol.instance_methods);
3030 sys::swapByteOrder(protocol.class_methods);
3031}
3032
3033inline void swapStruct(struct objc_method_description_list_t &mdl) {
3034 sys::swapByteOrder(mdl.count);
3035}
3036
3037inline void swapStruct(struct objc_method_description_t &md) {
3038 sys::swapByteOrder(md.name);
3039 sys::swapByteOrder(md.types);
3040}
3041
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003042static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3043 struct DisassembleInfo *info);
3044
3045// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3046// to an Objective-C class and returns the class name. It is also passed the
3047// address of the pointer, so when the pointer is zero as it can be in an .o
3048// file, that is used to look for an external relocation entry with a symbol
3049// name.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003050static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3051 uint64_t ReferenceValue,
3052 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003053 const char *r;
3054 uint32_t offset, left;
3055 SectionRef S;
3056
3057 // The pointer_value can be 0 in an object file and have a relocation
3058 // entry for the class symbol at the ReferenceValue (the address of the
3059 // pointer).
3060 if (pointer_value == 0) {
3061 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3062 if (r == nullptr || left < sizeof(uint64_t))
3063 return nullptr;
3064 uint64_t n_value;
3065 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3066 if (symbol_name == nullptr)
3067 return nullptr;
Hans Wennborgdb53e302014-10-23 21:59:17 +00003068 const char *class_name = strrchr(symbol_name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003069 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3070 return class_name + 2;
3071 else
3072 return nullptr;
3073 }
3074
3075 // The case were the pointer_value is non-zero and points to a class defined
3076 // in this Mach-O file.
3077 r = get_pointer_64(pointer_value, offset, left, S, info);
3078 if (r == nullptr || left < sizeof(struct class64_t))
3079 return nullptr;
3080 struct class64_t c;
3081 memcpy(&c, r, sizeof(struct class64_t));
3082 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3083 swapStruct(c);
3084 if (c.data == 0)
3085 return nullptr;
3086 r = get_pointer_64(c.data, offset, left, S, info);
3087 if (r == nullptr || left < sizeof(struct class_ro64_t))
3088 return nullptr;
3089 struct class_ro64_t cro;
3090 memcpy(&cro, r, sizeof(struct class_ro64_t));
3091 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3092 swapStruct(cro);
3093 if (cro.name == 0)
3094 return nullptr;
3095 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3096 return name;
3097}
3098
3099// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3100// pointer to a cfstring and returns its name or nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003101static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3102 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003103 const char *r, *name;
3104 uint32_t offset, left;
3105 SectionRef S;
3106 struct cfstring64_t cfs;
3107 uint64_t cfs_characters;
3108
3109 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3110 if (r == nullptr || left < sizeof(struct cfstring64_t))
3111 return nullptr;
3112 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3113 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3114 swapStruct(cfs);
3115 if (cfs.characters == 0) {
3116 uint64_t n_value;
3117 const char *symbol_name = get_symbol_64(
3118 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3119 if (symbol_name == nullptr)
3120 return nullptr;
3121 cfs_characters = n_value;
3122 } else
3123 cfs_characters = cfs.characters;
3124 name = get_pointer_64(cfs_characters, offset, left, S, info);
3125
3126 return name;
3127}
3128
3129// get_objc2_64bit_selref() is used for disassembly and is passed a the address
3130// of a pointer to an Objective-C selector reference when the pointer value is
3131// zero as in a .o file and is likely to have a external relocation entry with
3132// who's symbol's n_value is the real pointer to the selector name. If that is
3133// the case the real pointer to the selector name is returned else 0 is
3134// returned
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003135static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3136 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003137 uint32_t offset, left;
3138 SectionRef S;
3139
3140 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3141 if (r == nullptr || left < sizeof(uint64_t))
3142 return 0;
3143 uint64_t n_value;
3144 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3145 if (symbol_name == nullptr)
3146 return 0;
3147 return n_value;
3148}
3149
Kevin Enderby0fc11822015-04-01 20:57:01 +00003150static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3151 const char *sectname) {
3152 for (const SectionRef &Section : O->sections()) {
3153 StringRef SectName;
3154 Section.getName(SectName);
3155 DataRefImpl Ref = Section.getRawDataRefImpl();
3156 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3157 if (SegName == segname && SectName == sectname)
3158 return Section;
3159 }
3160 return SectionRef();
3161}
3162
3163static void
3164walk_pointer_list_64(const char *listname, const SectionRef S,
3165 MachOObjectFile *O, struct DisassembleInfo *info,
3166 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3167 if (S == SectionRef())
3168 return;
3169
3170 StringRef SectName;
3171 S.getName(SectName);
3172 DataRefImpl Ref = S.getRawDataRefImpl();
3173 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3174 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3175
3176 StringRef BytesStr;
3177 S.getContents(BytesStr);
3178 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3179
3180 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3181 uint32_t left = S.getSize() - i;
3182 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3183 uint64_t p = 0;
3184 memcpy(&p, Contents + i, size);
3185 if (i + sizeof(uint64_t) > S.getSize())
3186 outs() << listname << " list pointer extends past end of (" << SegName
3187 << "," << SectName << ") section\n";
3188 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3189
3190 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3191 sys::swapByteOrder(p);
3192
3193 uint64_t n_value = 0;
3194 const char *name = get_symbol_64(i, S, info, n_value, p);
3195 if (name == nullptr)
3196 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3197
3198 if (n_value != 0) {
3199 outs() << format("0x%" PRIx64, n_value);
3200 if (p != 0)
3201 outs() << " + " << format("0x%" PRIx64, p);
3202 } else
3203 outs() << format("0x%" PRIx64, p);
3204 if (name != nullptr)
3205 outs() << " " << name;
3206 outs() << "\n";
3207
3208 p += n_value;
3209 if (func)
3210 func(p, info);
3211 }
3212}
3213
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003214static void
3215walk_pointer_list_32(const char *listname, const SectionRef S,
3216 MachOObjectFile *O, struct DisassembleInfo *info,
3217 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3218 if (S == SectionRef())
3219 return;
3220
3221 StringRef SectName;
3222 S.getName(SectName);
3223 DataRefImpl Ref = S.getRawDataRefImpl();
3224 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3225 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3226
3227 StringRef BytesStr;
3228 S.getContents(BytesStr);
3229 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3230
3231 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3232 uint32_t left = S.getSize() - i;
3233 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3234 uint32_t p = 0;
3235 memcpy(&p, Contents + i, size);
3236 if (i + sizeof(uint32_t) > S.getSize())
3237 outs() << listname << " list pointer extends past end of (" << SegName
3238 << "," << SectName << ") section\n";
Kevin Enderbycf261312015-04-06 22:33:43 +00003239 uint32_t Address = S.getAddress() + i;
3240 outs() << format("%08" PRIx32, Address) << " ";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003241
3242 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3243 sys::swapByteOrder(p);
3244 outs() << format("0x%" PRIx32, p);
3245
3246 const char *name = get_symbol_32(i, S, info, p);
3247 if (name != nullptr)
3248 outs() << " " << name;
3249 outs() << "\n";
3250
3251 if (func)
3252 func(p, info);
3253 }
3254}
3255
3256static void print_layout_map(const char *layout_map, uint32_t left) {
Kevin Enderbya59824a2015-10-06 22:27:08 +00003257 if (layout_map == nullptr)
3258 return;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003259 outs() << " layout map: ";
3260 do {
3261 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3262 left--;
3263 layout_map++;
3264 } while (*layout_map != '\0' && left != 0);
3265 outs() << "\n";
3266}
3267
Kevin Enderby0fc11822015-04-01 20:57:01 +00003268static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3269 uint32_t offset, left;
3270 SectionRef S;
3271 const char *layout_map;
3272
3273 if (p == 0)
3274 return;
3275 layout_map = get_pointer_64(p, offset, left, S, info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003276 print_layout_map(layout_map, left);
3277}
3278
3279static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3280 uint32_t offset, left;
3281 SectionRef S;
3282 const char *layout_map;
3283
3284 if (p == 0)
3285 return;
3286 layout_map = get_pointer_32(p, offset, left, S, info);
3287 print_layout_map(layout_map, left);
Kevin Enderby0fc11822015-04-01 20:57:01 +00003288}
3289
3290static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3291 const char *indent) {
3292 struct method_list64_t ml;
3293 struct method64_t m;
3294 const char *r;
3295 uint32_t offset, xoffset, left, i;
3296 SectionRef S, xS;
3297 const char *name, *sym_name;
3298 uint64_t n_value;
3299
3300 r = get_pointer_64(p, offset, left, S, info);
3301 if (r == nullptr)
3302 return;
3303 memset(&ml, '\0', sizeof(struct method_list64_t));
3304 if (left < sizeof(struct method_list64_t)) {
3305 memcpy(&ml, r, left);
3306 outs() << " (method_list_t entends past the end of the section)\n";
3307 } else
3308 memcpy(&ml, r, sizeof(struct method_list64_t));
3309 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3310 swapStruct(ml);
3311 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3312 outs() << indent << "\t\t count " << ml.count << "\n";
3313
3314 p += sizeof(struct method_list64_t);
3315 offset += sizeof(struct method_list64_t);
3316 for (i = 0; i < ml.count; i++) {
3317 r = get_pointer_64(p, offset, left, S, info);
3318 if (r == nullptr)
3319 return;
3320 memset(&m, '\0', sizeof(struct method64_t));
3321 if (left < sizeof(struct method64_t)) {
Kevin Enderbya59824a2015-10-06 22:27:08 +00003322 memcpy(&m, r, left);
3323 outs() << indent << " (method_t extends past the end of the section)\n";
Kevin Enderby0fc11822015-04-01 20:57:01 +00003324 } else
3325 memcpy(&m, r, sizeof(struct method64_t));
3326 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3327 swapStruct(m);
3328
3329 outs() << indent << "\t\t name ";
3330 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3331 info, n_value, m.name);
3332 if (n_value != 0) {
3333 if (info->verbose && sym_name != nullptr)
3334 outs() << sym_name;
3335 else
3336 outs() << format("0x%" PRIx64, n_value);
3337 if (m.name != 0)
3338 outs() << " + " << format("0x%" PRIx64, m.name);
3339 } else
3340 outs() << format("0x%" PRIx64, m.name);
3341 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3342 if (name != nullptr)
3343 outs() << format(" %.*s", left, name);
3344 outs() << "\n";
3345
3346 outs() << indent << "\t\t types ";
3347 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3348 info, n_value, m.types);
3349 if (n_value != 0) {
3350 if (info->verbose && sym_name != nullptr)
3351 outs() << sym_name;
3352 else
3353 outs() << format("0x%" PRIx64, n_value);
3354 if (m.types != 0)
3355 outs() << " + " << format("0x%" PRIx64, m.types);
3356 } else
3357 outs() << format("0x%" PRIx64, m.types);
3358 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3359 if (name != nullptr)
3360 outs() << format(" %.*s", left, name);
3361 outs() << "\n";
3362
3363 outs() << indent << "\t\t imp ";
3364 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3365 n_value, m.imp);
3366 if (info->verbose && name == nullptr) {
3367 if (n_value != 0) {
3368 outs() << format("0x%" PRIx64, n_value) << " ";
3369 if (m.imp != 0)
3370 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3371 } else
3372 outs() << format("0x%" PRIx64, m.imp) << " ";
3373 }
3374 if (name != nullptr)
3375 outs() << name;
3376 outs() << "\n";
3377
3378 p += sizeof(struct method64_t);
3379 offset += sizeof(struct method64_t);
3380 }
3381}
3382
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003383static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3384 const char *indent) {
3385 struct method_list32_t ml;
3386 struct method32_t m;
Kevin Enderby846c0002015-04-16 17:19:59 +00003387 const char *r, *name;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003388 uint32_t offset, xoffset, left, i;
3389 SectionRef S, xS;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003390
3391 r = get_pointer_32(p, offset, left, S, info);
3392 if (r == nullptr)
3393 return;
3394 memset(&ml, '\0', sizeof(struct method_list32_t));
3395 if (left < sizeof(struct method_list32_t)) {
3396 memcpy(&ml, r, left);
3397 outs() << " (method_list_t entends past the end of the section)\n";
3398 } else
3399 memcpy(&ml, r, sizeof(struct method_list32_t));
3400 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3401 swapStruct(ml);
3402 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3403 outs() << indent << "\t\t count " << ml.count << "\n";
3404
3405 p += sizeof(struct method_list32_t);
3406 offset += sizeof(struct method_list32_t);
3407 for (i = 0; i < ml.count; i++) {
3408 r = get_pointer_32(p, offset, left, S, info);
3409 if (r == nullptr)
3410 return;
3411 memset(&m, '\0', sizeof(struct method32_t));
3412 if (left < sizeof(struct method32_t)) {
3413 memcpy(&ml, r, left);
3414 outs() << indent << " (method_t entends past the end of the section)\n";
3415 } else
3416 memcpy(&m, r, sizeof(struct method32_t));
3417 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3418 swapStruct(m);
3419
3420 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3421 name = get_pointer_32(m.name, xoffset, left, xS, info);
3422 if (name != nullptr)
3423 outs() << format(" %.*s", left, name);
3424 outs() << "\n";
3425
3426 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3427 name = get_pointer_32(m.types, xoffset, left, xS, info);
3428 if (name != nullptr)
3429 outs() << format(" %.*s", left, name);
3430 outs() << "\n";
3431
3432 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3433 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3434 m.imp);
3435 if (name != nullptr)
3436 outs() << " " << name;
3437 outs() << "\n";
3438
3439 p += sizeof(struct method32_t);
3440 offset += sizeof(struct method32_t);
3441 }
3442}
3443
Kevin Enderby846c0002015-04-16 17:19:59 +00003444static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3445 uint32_t offset, left, xleft;
3446 SectionRef S;
3447 struct objc_method_list_t method_list;
3448 struct objc_method_t method;
3449 const char *r, *methods, *name, *SymbolName;
3450 int32_t i;
3451
3452 r = get_pointer_32(p, offset, left, S, info, true);
3453 if (r == nullptr)
3454 return true;
3455
3456 outs() << "\n";
3457 if (left > sizeof(struct objc_method_list_t)) {
3458 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3459 } else {
3460 outs() << "\t\t objc_method_list extends past end of the section\n";
3461 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3462 memcpy(&method_list, r, left);
3463 }
3464 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3465 swapStruct(method_list);
3466
3467 outs() << "\t\t obsolete "
3468 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3469 outs() << "\t\t method_count " << method_list.method_count << "\n";
3470
3471 methods = r + sizeof(struct objc_method_list_t);
3472 for (i = 0; i < method_list.method_count; i++) {
3473 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3474 outs() << "\t\t remaining method's extend past the of the section\n";
3475 break;
3476 }
3477 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3478 sizeof(struct objc_method_t));
3479 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3480 swapStruct(method);
3481
3482 outs() << "\t\t method_name "
3483 << format("0x%08" PRIx32, method.method_name);
3484 if (info->verbose) {
3485 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3486 if (name != nullptr)
3487 outs() << format(" %.*s", xleft, name);
3488 else
3489 outs() << " (not in an __OBJC section)";
3490 }
3491 outs() << "\n";
3492
3493 outs() << "\t\t method_types "
3494 << format("0x%08" PRIx32, method.method_types);
3495 if (info->verbose) {
3496 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3497 if (name != nullptr)
3498 outs() << format(" %.*s", xleft, name);
3499 else
3500 outs() << " (not in an __OBJC section)";
3501 }
3502 outs() << "\n";
3503
3504 outs() << "\t\t method_imp "
3505 << format("0x%08" PRIx32, method.method_imp) << " ";
3506 if (info->verbose) {
3507 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3508 if (SymbolName != nullptr)
3509 outs() << SymbolName;
3510 }
3511 outs() << "\n";
3512 }
3513 return false;
3514}
3515
Kevin Enderby0fc11822015-04-01 20:57:01 +00003516static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3517 struct protocol_list64_t pl;
3518 uint64_t q, n_value;
3519 struct protocol64_t pc;
3520 const char *r;
3521 uint32_t offset, xoffset, left, i;
3522 SectionRef S, xS;
3523 const char *name, *sym_name;
3524
3525 r = get_pointer_64(p, offset, left, S, info);
3526 if (r == nullptr)
3527 return;
3528 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3529 if (left < sizeof(struct protocol_list64_t)) {
3530 memcpy(&pl, r, left);
3531 outs() << " (protocol_list_t entends past the end of the section)\n";
3532 } else
3533 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3534 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3535 swapStruct(pl);
3536 outs() << " count " << pl.count << "\n";
3537
3538 p += sizeof(struct protocol_list64_t);
3539 offset += sizeof(struct protocol_list64_t);
3540 for (i = 0; i < pl.count; i++) {
3541 r = get_pointer_64(p, offset, left, S, info);
3542 if (r == nullptr)
3543 return;
3544 q = 0;
3545 if (left < sizeof(uint64_t)) {
3546 memcpy(&q, r, left);
3547 outs() << " (protocol_t * entends past the end of the section)\n";
3548 } else
3549 memcpy(&q, r, sizeof(uint64_t));
3550 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3551 sys::swapByteOrder(q);
3552
3553 outs() << "\t\t list[" << i << "] ";
3554 sym_name = get_symbol_64(offset, S, info, n_value, q);
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 (q != 0)
3561 outs() << " + " << format("0x%" PRIx64, q);
3562 } else
3563 outs() << format("0x%" PRIx64, q);
3564 outs() << " (struct protocol_t *)\n";
3565
3566 r = get_pointer_64(q + n_value, offset, left, S, info);
3567 if (r == nullptr)
3568 return;
3569 memset(&pc, '\0', sizeof(struct protocol64_t));
3570 if (left < sizeof(struct protocol64_t)) {
3571 memcpy(&pc, r, left);
3572 outs() << " (protocol_t entends past the end of the section)\n";
3573 } else
3574 memcpy(&pc, r, sizeof(struct protocol64_t));
3575 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3576 swapStruct(pc);
3577
3578 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3579
3580 outs() << "\t\t\t name ";
3581 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3582 info, n_value, pc.name);
3583 if (n_value != 0) {
3584 if (info->verbose && sym_name != nullptr)
3585 outs() << sym_name;
3586 else
3587 outs() << format("0x%" PRIx64, n_value);
3588 if (pc.name != 0)
3589 outs() << " + " << format("0x%" PRIx64, pc.name);
3590 } else
3591 outs() << format("0x%" PRIx64, pc.name);
3592 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3593 if (name != nullptr)
3594 outs() << format(" %.*s", left, name);
3595 outs() << "\n";
3596
3597 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3598
3599 outs() << "\t\t instanceMethods ";
3600 sym_name =
3601 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3602 S, info, n_value, pc.instanceMethods);
3603 if (n_value != 0) {
3604 if (info->verbose && sym_name != nullptr)
3605 outs() << sym_name;
3606 else
3607 outs() << format("0x%" PRIx64, n_value);
3608 if (pc.instanceMethods != 0)
3609 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3610 } else
3611 outs() << format("0x%" PRIx64, pc.instanceMethods);
3612 outs() << " (struct method_list_t *)\n";
3613 if (pc.instanceMethods + n_value != 0)
3614 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3615
3616 outs() << "\t\t classMethods ";
3617 sym_name =
3618 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3619 info, n_value, pc.classMethods);
3620 if (n_value != 0) {
3621 if (info->verbose && sym_name != nullptr)
3622 outs() << sym_name;
3623 else
3624 outs() << format("0x%" PRIx64, n_value);
3625 if (pc.classMethods != 0)
3626 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3627 } else
3628 outs() << format("0x%" PRIx64, pc.classMethods);
3629 outs() << " (struct method_list_t *)\n";
3630 if (pc.classMethods + n_value != 0)
3631 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3632
3633 outs() << "\t optionalInstanceMethods "
3634 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3635 outs() << "\t optionalClassMethods "
3636 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3637 outs() << "\t instanceProperties "
3638 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3639
3640 p += sizeof(uint64_t);
3641 offset += sizeof(uint64_t);
3642 }
3643}
3644
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003645static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3646 struct protocol_list32_t pl;
3647 uint32_t q;
3648 struct protocol32_t pc;
3649 const char *r;
3650 uint32_t offset, xoffset, left, i;
3651 SectionRef S, xS;
3652 const char *name;
3653
3654 r = get_pointer_32(p, offset, left, S, info);
3655 if (r == nullptr)
3656 return;
3657 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3658 if (left < sizeof(struct protocol_list32_t)) {
3659 memcpy(&pl, r, left);
3660 outs() << " (protocol_list_t entends past the end of the section)\n";
3661 } else
3662 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3663 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3664 swapStruct(pl);
3665 outs() << " count " << pl.count << "\n";
3666
3667 p += sizeof(struct protocol_list32_t);
3668 offset += sizeof(struct protocol_list32_t);
3669 for (i = 0; i < pl.count; i++) {
3670 r = get_pointer_32(p, offset, left, S, info);
3671 if (r == nullptr)
3672 return;
3673 q = 0;
3674 if (left < sizeof(uint32_t)) {
3675 memcpy(&q, r, left);
3676 outs() << " (protocol_t * entends past the end of the section)\n";
3677 } else
3678 memcpy(&q, r, sizeof(uint32_t));
3679 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3680 sys::swapByteOrder(q);
3681 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3682 << " (struct protocol_t *)\n";
3683 r = get_pointer_32(q, offset, left, S, info);
3684 if (r == nullptr)
3685 return;
3686 memset(&pc, '\0', sizeof(struct protocol32_t));
3687 if (left < sizeof(struct protocol32_t)) {
3688 memcpy(&pc, r, left);
3689 outs() << " (protocol_t entends past the end of the section)\n";
3690 } else
3691 memcpy(&pc, r, sizeof(struct protocol32_t));
3692 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3693 swapStruct(pc);
3694 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3695 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3696 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3697 if (name != nullptr)
3698 outs() << format(" %.*s", left, name);
3699 outs() << "\n";
3700 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3701 outs() << "\t\t instanceMethods "
3702 << format("0x%" PRIx32, pc.instanceMethods)
3703 << " (struct method_list_t *)\n";
3704 if (pc.instanceMethods != 0)
3705 print_method_list32_t(pc.instanceMethods, info, "\t");
3706 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3707 << " (struct method_list_t *)\n";
3708 if (pc.classMethods != 0)
3709 print_method_list32_t(pc.classMethods, info, "\t");
3710 outs() << "\t optionalInstanceMethods "
3711 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3712 outs() << "\t optionalClassMethods "
3713 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3714 outs() << "\t instanceProperties "
3715 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3716 p += sizeof(uint32_t);
3717 offset += sizeof(uint32_t);
3718 }
3719}
3720
Kevin Enderby846c0002015-04-16 17:19:59 +00003721static void print_indent(uint32_t indent) {
3722 for (uint32_t i = 0; i < indent;) {
3723 if (indent - i >= 8) {
3724 outs() << "\t";
3725 i += 8;
3726 } else {
3727 for (uint32_t j = i; j < indent; j++)
3728 outs() << " ";
3729 return;
3730 }
3731 }
3732}
3733
3734static bool print_method_description_list(uint32_t p, uint32_t indent,
3735 struct DisassembleInfo *info) {
3736 uint32_t offset, left, xleft;
3737 SectionRef S;
3738 struct objc_method_description_list_t mdl;
3739 struct objc_method_description_t md;
3740 const char *r, *list, *name;
3741 int32_t i;
3742
3743 r = get_pointer_32(p, offset, left, S, info, true);
3744 if (r == nullptr)
3745 return true;
3746
3747 outs() << "\n";
3748 if (left > sizeof(struct objc_method_description_list_t)) {
3749 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3750 } else {
3751 print_indent(indent);
3752 outs() << " objc_method_description_list extends past end of the section\n";
3753 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3754 memcpy(&mdl, r, left);
3755 }
3756 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3757 swapStruct(mdl);
3758
3759 print_indent(indent);
3760 outs() << " count " << mdl.count << "\n";
3761
3762 list = r + sizeof(struct objc_method_description_list_t);
3763 for (i = 0; i < mdl.count; i++) {
3764 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3765 print_indent(indent);
3766 outs() << " remaining list entries extend past the of the section\n";
3767 break;
3768 }
3769 print_indent(indent);
3770 outs() << " list[" << i << "]\n";
3771 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3772 sizeof(struct objc_method_description_t));
3773 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3774 swapStruct(md);
3775
3776 print_indent(indent);
3777 outs() << " name " << format("0x%08" PRIx32, md.name);
3778 if (info->verbose) {
3779 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3780 if (name != nullptr)
3781 outs() << format(" %.*s", xleft, name);
3782 else
3783 outs() << " (not in an __OBJC section)";
3784 }
3785 outs() << "\n";
3786
3787 print_indent(indent);
3788 outs() << " types " << format("0x%08" PRIx32, md.types);
3789 if (info->verbose) {
3790 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3791 if (name != nullptr)
3792 outs() << format(" %.*s", xleft, name);
3793 else
3794 outs() << " (not in an __OBJC section)";
3795 }
3796 outs() << "\n";
3797 }
3798 return false;
3799}
3800
3801static bool print_protocol_list(uint32_t p, uint32_t indent,
3802 struct DisassembleInfo *info);
3803
3804static bool print_protocol(uint32_t p, uint32_t indent,
3805 struct DisassembleInfo *info) {
3806 uint32_t offset, left;
3807 SectionRef S;
3808 struct objc_protocol_t protocol;
3809 const char *r, *name;
3810
3811 r = get_pointer_32(p, offset, left, S, info, true);
3812 if (r == nullptr)
3813 return true;
3814
3815 outs() << "\n";
3816 if (left >= sizeof(struct objc_protocol_t)) {
3817 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3818 } else {
3819 print_indent(indent);
3820 outs() << " Protocol extends past end of the section\n";
3821 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3822 memcpy(&protocol, r, left);
3823 }
3824 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3825 swapStruct(protocol);
3826
3827 print_indent(indent);
3828 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3829 << "\n";
3830
3831 print_indent(indent);
3832 outs() << " protocol_name "
3833 << format("0x%08" PRIx32, protocol.protocol_name);
3834 if (info->verbose) {
3835 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3836 if (name != nullptr)
3837 outs() << format(" %.*s", left, name);
3838 else
3839 outs() << " (not in an __OBJC section)";
3840 }
3841 outs() << "\n";
3842
3843 print_indent(indent);
3844 outs() << " protocol_list "
3845 << format("0x%08" PRIx32, protocol.protocol_list);
3846 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3847 outs() << " (not in an __OBJC section)\n";
3848
3849 print_indent(indent);
3850 outs() << " instance_methods "
3851 << format("0x%08" PRIx32, protocol.instance_methods);
3852 if (print_method_description_list(protocol.instance_methods, indent, info))
3853 outs() << " (not in an __OBJC section)\n";
3854
3855 print_indent(indent);
3856 outs() << " class_methods "
3857 << format("0x%08" PRIx32, protocol.class_methods);
3858 if (print_method_description_list(protocol.class_methods, indent, info))
3859 outs() << " (not in an __OBJC section)\n";
3860
3861 return false;
3862}
3863
3864static bool print_protocol_list(uint32_t p, uint32_t indent,
3865 struct DisassembleInfo *info) {
3866 uint32_t offset, left, l;
3867 SectionRef S;
3868 struct objc_protocol_list_t protocol_list;
3869 const char *r, *list;
3870 int32_t i;
3871
3872 r = get_pointer_32(p, offset, left, S, info, true);
3873 if (r == nullptr)
3874 return true;
3875
3876 outs() << "\n";
3877 if (left > sizeof(struct objc_protocol_list_t)) {
3878 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
3879 } else {
3880 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
3881 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
3882 memcpy(&protocol_list, r, left);
3883 }
3884 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3885 swapStruct(protocol_list);
3886
3887 print_indent(indent);
3888 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
3889 << "\n";
3890 print_indent(indent);
3891 outs() << " count " << protocol_list.count << "\n";
3892
3893 list = r + sizeof(struct objc_protocol_list_t);
3894 for (i = 0; i < protocol_list.count; i++) {
3895 if ((i + 1) * sizeof(uint32_t) > left) {
3896 outs() << "\t\t remaining list entries extend past the of the section\n";
3897 break;
3898 }
3899 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
3900 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3901 sys::swapByteOrder(l);
3902
3903 print_indent(indent);
3904 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
3905 if (print_protocol(l, indent, info))
3906 outs() << "(not in an __OBJC section)\n";
3907 }
3908 return false;
3909}
3910
Kevin Enderby0fc11822015-04-01 20:57:01 +00003911static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3912 struct ivar_list64_t il;
3913 struct ivar64_t i;
3914 const char *r;
3915 uint32_t offset, xoffset, left, j;
3916 SectionRef S, xS;
3917 const char *name, *sym_name, *ivar_offset_p;
3918 uint64_t ivar_offset, n_value;
3919
3920 r = get_pointer_64(p, offset, left, S, info);
3921 if (r == nullptr)
3922 return;
3923 memset(&il, '\0', sizeof(struct ivar_list64_t));
3924 if (left < sizeof(struct ivar_list64_t)) {
3925 memcpy(&il, r, left);
3926 outs() << " (ivar_list_t entends past the end of the section)\n";
3927 } else
3928 memcpy(&il, r, sizeof(struct ivar_list64_t));
3929 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3930 swapStruct(il);
3931 outs() << " entsize " << il.entsize << "\n";
3932 outs() << " count " << il.count << "\n";
3933
3934 p += sizeof(struct ivar_list64_t);
3935 offset += sizeof(struct ivar_list64_t);
3936 for (j = 0; j < il.count; j++) {
3937 r = get_pointer_64(p, offset, left, S, info);
3938 if (r == nullptr)
3939 return;
3940 memset(&i, '\0', sizeof(struct ivar64_t));
3941 if (left < sizeof(struct ivar64_t)) {
3942 memcpy(&i, r, left);
3943 outs() << " (ivar_t entends past the end of the section)\n";
3944 } else
3945 memcpy(&i, r, sizeof(struct ivar64_t));
3946 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3947 swapStruct(i);
3948
3949 outs() << "\t\t\t offset ";
3950 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
3951 info, n_value, i.offset);
3952 if (n_value != 0) {
3953 if (info->verbose && sym_name != nullptr)
3954 outs() << sym_name;
3955 else
3956 outs() << format("0x%" PRIx64, n_value);
3957 if (i.offset != 0)
3958 outs() << " + " << format("0x%" PRIx64, i.offset);
3959 } else
3960 outs() << format("0x%" PRIx64, i.offset);
3961 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
3962 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
3963 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
3964 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3965 sys::swapByteOrder(ivar_offset);
3966 outs() << " " << ivar_offset << "\n";
3967 } else
3968 outs() << "\n";
3969
3970 outs() << "\t\t\t name ";
3971 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
3972 n_value, i.name);
3973 if (n_value != 0) {
3974 if (info->verbose && sym_name != nullptr)
3975 outs() << sym_name;
3976 else
3977 outs() << format("0x%" PRIx64, n_value);
3978 if (i.name != 0)
3979 outs() << " + " << format("0x%" PRIx64, i.name);
3980 } else
3981 outs() << format("0x%" PRIx64, i.name);
3982 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
3983 if (name != nullptr)
3984 outs() << format(" %.*s", left, name);
3985 outs() << "\n";
3986
3987 outs() << "\t\t\t type ";
3988 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
3989 n_value, i.name);
3990 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
3991 if (n_value != 0) {
3992 if (info->verbose && sym_name != nullptr)
3993 outs() << sym_name;
3994 else
3995 outs() << format("0x%" PRIx64, n_value);
3996 if (i.type != 0)
3997 outs() << " + " << format("0x%" PRIx64, i.type);
3998 } else
3999 outs() << format("0x%" PRIx64, i.type);
4000 if (name != nullptr)
4001 outs() << format(" %.*s", left, name);
4002 outs() << "\n";
4003
4004 outs() << "\t\t\talignment " << i.alignment << "\n";
4005 outs() << "\t\t\t size " << i.size << "\n";
4006
4007 p += sizeof(struct ivar64_t);
4008 offset += sizeof(struct ivar64_t);
4009 }
4010}
4011
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004012static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4013 struct ivar_list32_t il;
4014 struct ivar32_t i;
4015 const char *r;
4016 uint32_t offset, xoffset, left, j;
4017 SectionRef S, xS;
4018 const char *name, *ivar_offset_p;
4019 uint32_t ivar_offset;
4020
4021 r = get_pointer_32(p, offset, left, S, info);
4022 if (r == nullptr)
4023 return;
4024 memset(&il, '\0', sizeof(struct ivar_list32_t));
4025 if (left < sizeof(struct ivar_list32_t)) {
4026 memcpy(&il, r, left);
4027 outs() << " (ivar_list_t entends past the end of the section)\n";
4028 } else
4029 memcpy(&il, r, sizeof(struct ivar_list32_t));
4030 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4031 swapStruct(il);
4032 outs() << " entsize " << il.entsize << "\n";
4033 outs() << " count " << il.count << "\n";
4034
4035 p += sizeof(struct ivar_list32_t);
4036 offset += sizeof(struct ivar_list32_t);
4037 for (j = 0; j < il.count; j++) {
4038 r = get_pointer_32(p, offset, left, S, info);
4039 if (r == nullptr)
4040 return;
4041 memset(&i, '\0', sizeof(struct ivar32_t));
4042 if (left < sizeof(struct ivar32_t)) {
4043 memcpy(&i, r, left);
4044 outs() << " (ivar_t entends past the end of the section)\n";
4045 } else
4046 memcpy(&i, r, sizeof(struct ivar32_t));
4047 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4048 swapStruct(i);
4049
4050 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4051 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4052 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4053 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4054 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4055 sys::swapByteOrder(ivar_offset);
4056 outs() << " " << ivar_offset << "\n";
4057 } else
4058 outs() << "\n";
4059
4060 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4061 name = get_pointer_32(i.name, xoffset, left, xS, info);
4062 if (name != nullptr)
4063 outs() << format(" %.*s", left, name);
4064 outs() << "\n";
4065
4066 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4067 name = get_pointer_32(i.type, xoffset, left, xS, info);
4068 if (name != nullptr)
4069 outs() << format(" %.*s", left, name);
4070 outs() << "\n";
4071
4072 outs() << "\t\t\talignment " << i.alignment << "\n";
4073 outs() << "\t\t\t size " << i.size << "\n";
4074
4075 p += sizeof(struct ivar32_t);
4076 offset += sizeof(struct ivar32_t);
4077 }
4078}
4079
Kevin Enderby0fc11822015-04-01 20:57:01 +00004080static void print_objc_property_list64(uint64_t p,
4081 struct DisassembleInfo *info) {
4082 struct objc_property_list64 opl;
4083 struct objc_property64 op;
4084 const char *r;
4085 uint32_t offset, xoffset, left, j;
4086 SectionRef S, xS;
4087 const char *name, *sym_name;
4088 uint64_t n_value;
4089
4090 r = get_pointer_64(p, offset, left, S, info);
4091 if (r == nullptr)
4092 return;
4093 memset(&opl, '\0', sizeof(struct objc_property_list64));
4094 if (left < sizeof(struct objc_property_list64)) {
4095 memcpy(&opl, r, left);
4096 outs() << " (objc_property_list entends past the end of the section)\n";
4097 } else
4098 memcpy(&opl, r, sizeof(struct objc_property_list64));
4099 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4100 swapStruct(opl);
4101 outs() << " entsize " << opl.entsize << "\n";
4102 outs() << " count " << opl.count << "\n";
4103
4104 p += sizeof(struct objc_property_list64);
4105 offset += sizeof(struct objc_property_list64);
4106 for (j = 0; j < opl.count; j++) {
4107 r = get_pointer_64(p, offset, left, S, info);
4108 if (r == nullptr)
4109 return;
4110 memset(&op, '\0', sizeof(struct objc_property64));
4111 if (left < sizeof(struct objc_property64)) {
4112 memcpy(&op, r, left);
4113 outs() << " (objc_property entends past the end of the section)\n";
4114 } else
4115 memcpy(&op, r, sizeof(struct objc_property64));
4116 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4117 swapStruct(op);
4118
4119 outs() << "\t\t\t name ";
4120 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4121 info, n_value, op.name);
4122 if (n_value != 0) {
4123 if (info->verbose && sym_name != nullptr)
4124 outs() << sym_name;
4125 else
4126 outs() << format("0x%" PRIx64, n_value);
4127 if (op.name != 0)
4128 outs() << " + " << format("0x%" PRIx64, op.name);
4129 } else
4130 outs() << format("0x%" PRIx64, op.name);
4131 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4132 if (name != nullptr)
4133 outs() << format(" %.*s", left, name);
4134 outs() << "\n";
4135
4136 outs() << "\t\t\tattributes ";
4137 sym_name =
4138 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4139 info, n_value, op.attributes);
4140 if (n_value != 0) {
4141 if (info->verbose && sym_name != nullptr)
4142 outs() << sym_name;
4143 else
4144 outs() << format("0x%" PRIx64, n_value);
4145 if (op.attributes != 0)
4146 outs() << " + " << format("0x%" PRIx64, op.attributes);
4147 } else
4148 outs() << format("0x%" PRIx64, op.attributes);
4149 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4150 if (name != nullptr)
4151 outs() << format(" %.*s", left, name);
4152 outs() << "\n";
4153
4154 p += sizeof(struct objc_property64);
4155 offset += sizeof(struct objc_property64);
4156 }
4157}
4158
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004159static void print_objc_property_list32(uint32_t p,
4160 struct DisassembleInfo *info) {
4161 struct objc_property_list32 opl;
4162 struct objc_property32 op;
4163 const char *r;
4164 uint32_t offset, xoffset, left, j;
4165 SectionRef S, xS;
4166 const char *name;
4167
4168 r = get_pointer_32(p, offset, left, S, info);
4169 if (r == nullptr)
4170 return;
4171 memset(&opl, '\0', sizeof(struct objc_property_list32));
4172 if (left < sizeof(struct objc_property_list32)) {
4173 memcpy(&opl, r, left);
4174 outs() << " (objc_property_list entends past the end of the section)\n";
4175 } else
4176 memcpy(&opl, r, sizeof(struct objc_property_list32));
4177 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4178 swapStruct(opl);
4179 outs() << " entsize " << opl.entsize << "\n";
4180 outs() << " count " << opl.count << "\n";
4181
4182 p += sizeof(struct objc_property_list32);
4183 offset += sizeof(struct objc_property_list32);
4184 for (j = 0; j < opl.count; j++) {
4185 r = get_pointer_32(p, offset, left, S, info);
4186 if (r == nullptr)
4187 return;
4188 memset(&op, '\0', sizeof(struct objc_property32));
4189 if (left < sizeof(struct objc_property32)) {
4190 memcpy(&op, r, left);
4191 outs() << " (objc_property entends past the end of the section)\n";
4192 } else
4193 memcpy(&op, r, sizeof(struct objc_property32));
4194 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4195 swapStruct(op);
4196
4197 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4198 name = get_pointer_32(op.name, xoffset, left, xS, info);
4199 if (name != nullptr)
4200 outs() << format(" %.*s", left, name);
4201 outs() << "\n";
4202
4203 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4204 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4205 if (name != nullptr)
4206 outs() << format(" %.*s", left, name);
4207 outs() << "\n";
4208
4209 p += sizeof(struct objc_property32);
4210 offset += sizeof(struct objc_property32);
4211 }
4212}
4213
Richard Smith81ff44d2015-10-09 22:09:56 +00004214static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
Kevin Enderby0fc11822015-04-01 20:57:01 +00004215 bool &is_meta_class) {
4216 struct class_ro64_t cro;
4217 const char *r;
4218 uint32_t offset, xoffset, left;
4219 SectionRef S, xS;
4220 const char *name, *sym_name;
4221 uint64_t n_value;
4222
4223 r = get_pointer_64(p, offset, left, S, info);
4224 if (r == nullptr || left < sizeof(struct class_ro64_t))
Richard Smith81ff44d2015-10-09 22:09:56 +00004225 return false;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004226 memset(&cro, '\0', sizeof(struct class_ro64_t));
4227 if (left < sizeof(struct class_ro64_t)) {
4228 memcpy(&cro, r, left);
4229 outs() << " (class_ro_t entends past the end of the section)\n";
4230 } else
4231 memcpy(&cro, r, sizeof(struct class_ro64_t));
4232 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4233 swapStruct(cro);
4234 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4235 if (cro.flags & RO_META)
4236 outs() << " RO_META";
4237 if (cro.flags & RO_ROOT)
4238 outs() << " RO_ROOT";
4239 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4240 outs() << " RO_HAS_CXX_STRUCTORS";
4241 outs() << "\n";
4242 outs() << " instanceStart " << cro.instanceStart << "\n";
4243 outs() << " instanceSize " << cro.instanceSize << "\n";
4244 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4245 << "\n";
4246 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4247 << "\n";
4248 print_layout_map64(cro.ivarLayout, info);
4249
4250 outs() << " name ";
4251 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4252 info, n_value, cro.name);
4253 if (n_value != 0) {
4254 if (info->verbose && sym_name != nullptr)
4255 outs() << sym_name;
4256 else
4257 outs() << format("0x%" PRIx64, n_value);
4258 if (cro.name != 0)
4259 outs() << " + " << format("0x%" PRIx64, cro.name);
4260 } else
4261 outs() << format("0x%" PRIx64, cro.name);
4262 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4263 if (name != nullptr)
4264 outs() << format(" %.*s", left, name);
4265 outs() << "\n";
4266
4267 outs() << " baseMethods ";
4268 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4269 S, info, n_value, cro.baseMethods);
4270 if (n_value != 0) {
4271 if (info->verbose && sym_name != nullptr)
4272 outs() << sym_name;
4273 else
4274 outs() << format("0x%" PRIx64, n_value);
4275 if (cro.baseMethods != 0)
4276 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4277 } else
4278 outs() << format("0x%" PRIx64, cro.baseMethods);
4279 outs() << " (struct method_list_t *)\n";
4280 if (cro.baseMethods + n_value != 0)
4281 print_method_list64_t(cro.baseMethods + n_value, info, "");
4282
4283 outs() << " baseProtocols ";
4284 sym_name =
4285 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4286 info, n_value, cro.baseProtocols);
4287 if (n_value != 0) {
4288 if (info->verbose && sym_name != nullptr)
4289 outs() << sym_name;
4290 else
4291 outs() << format("0x%" PRIx64, n_value);
4292 if (cro.baseProtocols != 0)
4293 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4294 } else
4295 outs() << format("0x%" PRIx64, cro.baseProtocols);
4296 outs() << "\n";
4297 if (cro.baseProtocols + n_value != 0)
4298 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4299
4300 outs() << " ivars ";
4301 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004302 info, n_value, cro.ivars);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004303 if (n_value != 0) {
4304 if (info->verbose && sym_name != nullptr)
4305 outs() << sym_name;
4306 else
4307 outs() << format("0x%" PRIx64, n_value);
4308 if (cro.ivars != 0)
4309 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4310 } else
4311 outs() << format("0x%" PRIx64, cro.ivars);
4312 outs() << "\n";
4313 if (cro.ivars + n_value != 0)
4314 print_ivar_list64_t(cro.ivars + n_value, info);
4315
4316 outs() << " weakIvarLayout ";
4317 sym_name =
4318 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4319 info, n_value, cro.weakIvarLayout);
4320 if (n_value != 0) {
4321 if (info->verbose && sym_name != nullptr)
4322 outs() << sym_name;
4323 else
4324 outs() << format("0x%" PRIx64, n_value);
4325 if (cro.weakIvarLayout != 0)
4326 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4327 } else
4328 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4329 outs() << "\n";
4330 print_layout_map64(cro.weakIvarLayout + n_value, info);
4331
4332 outs() << " baseProperties ";
4333 sym_name =
4334 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4335 info, n_value, cro.baseProperties);
4336 if (n_value != 0) {
4337 if (info->verbose && sym_name != nullptr)
4338 outs() << sym_name;
4339 else
4340 outs() << format("0x%" PRIx64, n_value);
4341 if (cro.baseProperties != 0)
4342 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4343 } else
4344 outs() << format("0x%" PRIx64, cro.baseProperties);
4345 outs() << "\n";
4346 if (cro.baseProperties + n_value != 0)
4347 print_objc_property_list64(cro.baseProperties + n_value, info);
4348
Rafael Espindolab9091322015-10-24 23:19:10 +00004349 is_meta_class = (cro.flags & RO_META) != 0;
Richard Smith81ff44d2015-10-09 22:09:56 +00004350 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004351}
4352
Richard Smith81ff44d2015-10-09 22:09:56 +00004353static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004354 bool &is_meta_class) {
4355 struct class_ro32_t cro;
4356 const char *r;
4357 uint32_t offset, xoffset, left;
4358 SectionRef S, xS;
4359 const char *name;
4360
4361 r = get_pointer_32(p, offset, left, S, info);
4362 if (r == nullptr)
Richard Smith81ff44d2015-10-09 22:09:56 +00004363 return false;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004364 memset(&cro, '\0', sizeof(struct class_ro32_t));
4365 if (left < sizeof(struct class_ro32_t)) {
4366 memcpy(&cro, r, left);
4367 outs() << " (class_ro_t entends past the end of the section)\n";
4368 } else
4369 memcpy(&cro, r, sizeof(struct class_ro32_t));
4370 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4371 swapStruct(cro);
4372 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4373 if (cro.flags & RO_META)
4374 outs() << " RO_META";
4375 if (cro.flags & RO_ROOT)
4376 outs() << " RO_ROOT";
4377 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4378 outs() << " RO_HAS_CXX_STRUCTORS";
4379 outs() << "\n";
4380 outs() << " instanceStart " << cro.instanceStart << "\n";
4381 outs() << " instanceSize " << cro.instanceSize << "\n";
4382 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4383 << "\n";
4384 print_layout_map32(cro.ivarLayout, info);
4385
4386 outs() << " name " << format("0x%" PRIx32, cro.name);
4387 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4388 if (name != nullptr)
4389 outs() << format(" %.*s", left, name);
4390 outs() << "\n";
4391
4392 outs() << " baseMethods "
4393 << format("0x%" PRIx32, cro.baseMethods)
4394 << " (struct method_list_t *)\n";
4395 if (cro.baseMethods != 0)
4396 print_method_list32_t(cro.baseMethods, info, "");
4397
4398 outs() << " baseProtocols "
4399 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4400 if (cro.baseProtocols != 0)
4401 print_protocol_list32_t(cro.baseProtocols, info);
4402 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4403 << "\n";
4404 if (cro.ivars != 0)
4405 print_ivar_list32_t(cro.ivars, info);
4406 outs() << " weakIvarLayout "
4407 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4408 print_layout_map32(cro.weakIvarLayout, info);
4409 outs() << " baseProperties "
4410 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4411 if (cro.baseProperties != 0)
4412 print_objc_property_list32(cro.baseProperties, info);
Rafael Espindolab9091322015-10-24 23:19:10 +00004413 is_meta_class = (cro.flags & RO_META) != 0;
Richard Smith81ff44d2015-10-09 22:09:56 +00004414 return true;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004415}
4416
Kevin Enderby0fc11822015-04-01 20:57:01 +00004417static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4418 struct class64_t c;
4419 const char *r;
4420 uint32_t offset, left;
4421 SectionRef S;
4422 const char *name;
4423 uint64_t isa_n_value, n_value;
4424
4425 r = get_pointer_64(p, offset, left, S, info);
4426 if (r == nullptr || left < sizeof(struct class64_t))
4427 return;
4428 memset(&c, '\0', sizeof(struct class64_t));
4429 if (left < sizeof(struct class64_t)) {
4430 memcpy(&c, r, left);
4431 outs() << " (class_t entends past the end of the section)\n";
4432 } else
4433 memcpy(&c, r, sizeof(struct class64_t));
4434 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4435 swapStruct(c);
4436
4437 outs() << " isa " << format("0x%" PRIx64, c.isa);
4438 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4439 isa_n_value, c.isa);
4440 if (name != nullptr)
4441 outs() << " " << name;
4442 outs() << "\n";
4443
4444 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4445 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4446 n_value, c.superclass);
4447 if (name != nullptr)
4448 outs() << " " << name;
4449 outs() << "\n";
4450
4451 outs() << " cache " << format("0x%" PRIx64, c.cache);
4452 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4453 n_value, c.cache);
4454 if (name != nullptr)
4455 outs() << " " << name;
4456 outs() << "\n";
4457
4458 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4459 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4460 n_value, c.vtable);
4461 if (name != nullptr)
4462 outs() << " " << name;
4463 outs() << "\n";
4464
4465 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4466 n_value, c.data);
4467 outs() << " data ";
4468 if (n_value != 0) {
4469 if (info->verbose && name != nullptr)
4470 outs() << name;
4471 else
4472 outs() << format("0x%" PRIx64, n_value);
4473 if (c.data != 0)
4474 outs() << " + " << format("0x%" PRIx64, c.data);
4475 } else
4476 outs() << format("0x%" PRIx64, c.data);
4477 outs() << " (struct class_ro_t *)";
4478
4479 // This is a Swift class if some of the low bits of the pointer are set.
4480 if ((c.data + n_value) & 0x7)
4481 outs() << " Swift class";
4482 outs() << "\n";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004483 bool is_meta_class;
Richard Smith81ff44d2015-10-09 22:09:56 +00004484 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
4485 return;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004486
Kevin Enderbyaac75382015-10-08 16:56:35 +00004487 if (!is_meta_class &&
4488 c.isa + isa_n_value != p &&
4489 c.isa + isa_n_value != 0 &&
4490 info->depth < 100) {
4491 info->depth++;
4492 outs() << "Meta Class\n";
4493 print_class64_t(c.isa + isa_n_value, info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004494 }
4495}
4496
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004497static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4498 struct class32_t c;
4499 const char *r;
4500 uint32_t offset, left;
4501 SectionRef S;
4502 const char *name;
4503
4504 r = get_pointer_32(p, offset, left, S, info);
4505 if (r == nullptr)
4506 return;
4507 memset(&c, '\0', sizeof(struct class32_t));
4508 if (left < sizeof(struct class32_t)) {
4509 memcpy(&c, r, left);
4510 outs() << " (class_t entends past the end of the section)\n";
4511 } else
4512 memcpy(&c, r, sizeof(struct class32_t));
4513 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4514 swapStruct(c);
4515
4516 outs() << " isa " << format("0x%" PRIx32, c.isa);
4517 name =
4518 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4519 if (name != nullptr)
4520 outs() << " " << name;
4521 outs() << "\n";
4522
4523 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4524 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4525 c.superclass);
4526 if (name != nullptr)
4527 outs() << " " << name;
4528 outs() << "\n";
4529
4530 outs() << " cache " << format("0x%" PRIx32, c.cache);
4531 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4532 c.cache);
4533 if (name != nullptr)
4534 outs() << " " << name;
4535 outs() << "\n";
4536
4537 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4538 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4539 c.vtable);
4540 if (name != nullptr)
4541 outs() << " " << name;
4542 outs() << "\n";
4543
4544 name =
4545 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4546 outs() << " data " << format("0x%" PRIx32, c.data)
4547 << " (struct class_ro_t *)";
4548
4549 // This is a Swift class if some of the low bits of the pointer are set.
4550 if (c.data & 0x3)
4551 outs() << " Swift class";
4552 outs() << "\n";
4553 bool is_meta_class;
Richard Smith81ff44d2015-10-09 22:09:56 +00004554 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
4555 return;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004556
Hans Wennborgcc9deb42015-09-29 18:02:48 +00004557 if (!is_meta_class) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004558 outs() << "Meta Class\n";
4559 print_class32_t(c.isa, info);
4560 }
4561}
4562
Kevin Enderby846c0002015-04-16 17:19:59 +00004563static void print_objc_class_t(struct objc_class_t *objc_class,
4564 struct DisassembleInfo *info) {
4565 uint32_t offset, left, xleft;
4566 const char *name, *p, *ivar_list;
4567 SectionRef S;
4568 int32_t i;
4569 struct objc_ivar_list_t objc_ivar_list;
4570 struct objc_ivar_t ivar;
4571
4572 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4573 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4574 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4575 if (name != nullptr)
4576 outs() << format(" %.*s", left, name);
4577 else
4578 outs() << " (not in an __OBJC section)";
4579 }
4580 outs() << "\n";
4581
4582 outs() << "\t super_class "
4583 << format("0x%08" PRIx32, objc_class->super_class);
4584 if (info->verbose) {
4585 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4586 if (name != nullptr)
4587 outs() << format(" %.*s", left, name);
4588 else
4589 outs() << " (not in an __OBJC section)";
4590 }
4591 outs() << "\n";
4592
4593 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4594 if (info->verbose) {
4595 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4596 if (name != nullptr)
4597 outs() << format(" %.*s", left, name);
4598 else
4599 outs() << " (not in an __OBJC section)";
4600 }
4601 outs() << "\n";
4602
4603 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4604 << "\n";
4605
4606 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4607 if (info->verbose) {
4608 if (CLS_GETINFO(objc_class, CLS_CLASS))
4609 outs() << " CLS_CLASS";
4610 else if (CLS_GETINFO(objc_class, CLS_META))
4611 outs() << " CLS_META";
4612 }
4613 outs() << "\n";
4614
4615 outs() << "\t instance_size "
4616 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4617
4618 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4619 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4620 if (p != nullptr) {
4621 if (left > sizeof(struct objc_ivar_list_t)) {
4622 outs() << "\n";
4623 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4624 } else {
4625 outs() << " (entends past the end of the section)\n";
4626 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4627 memcpy(&objc_ivar_list, p, left);
4628 }
4629 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4630 swapStruct(objc_ivar_list);
4631 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4632 ivar_list = p + sizeof(struct objc_ivar_list_t);
4633 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4634 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4635 outs() << "\t\t remaining ivar's extend past the of the section\n";
4636 break;
4637 }
4638 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4639 sizeof(struct objc_ivar_t));
4640 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4641 swapStruct(ivar);
4642
4643 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4644 if (info->verbose) {
4645 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4646 if (name != nullptr)
4647 outs() << format(" %.*s", xleft, name);
4648 else
4649 outs() << " (not in an __OBJC section)";
4650 }
4651 outs() << "\n";
4652
4653 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4654 if (info->verbose) {
4655 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4656 if (name != nullptr)
4657 outs() << format(" %.*s", xleft, name);
4658 else
4659 outs() << " (not in an __OBJC section)";
4660 }
4661 outs() << "\n";
4662
4663 outs() << "\t\t ivar_offset "
4664 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4665 }
4666 } else {
4667 outs() << " (not in an __OBJC section)\n";
4668 }
4669
4670 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4671 if (print_method_list(objc_class->methodLists, info))
4672 outs() << " (not in an __OBJC section)\n";
4673
4674 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4675 << "\n";
4676
4677 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4678 if (print_protocol_list(objc_class->protocols, 16, info))
4679 outs() << " (not in an __OBJC section)\n";
4680}
4681
4682static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4683 struct DisassembleInfo *info) {
4684 uint32_t offset, left;
4685 const char *name;
4686 SectionRef S;
4687
4688 outs() << "\t category name "
4689 << format("0x%08" PRIx32, objc_category->category_name);
4690 if (info->verbose) {
4691 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4692 true);
4693 if (name != nullptr)
4694 outs() << format(" %.*s", left, name);
4695 else
4696 outs() << " (not in an __OBJC section)";
4697 }
4698 outs() << "\n";
4699
4700 outs() << "\t\t class name "
4701 << format("0x%08" PRIx32, objc_category->class_name);
4702 if (info->verbose) {
4703 name =
4704 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4705 if (name != nullptr)
4706 outs() << format(" %.*s", left, name);
4707 else
4708 outs() << " (not in an __OBJC section)";
4709 }
4710 outs() << "\n";
4711
4712 outs() << "\t instance methods "
4713 << format("0x%08" PRIx32, objc_category->instance_methods);
4714 if (print_method_list(objc_category->instance_methods, info))
4715 outs() << " (not in an __OBJC section)\n";
4716
4717 outs() << "\t class methods "
4718 << format("0x%08" PRIx32, objc_category->class_methods);
4719 if (print_method_list(objc_category->class_methods, info))
4720 outs() << " (not in an __OBJC section)\n";
4721}
4722
Kevin Enderby0fc11822015-04-01 20:57:01 +00004723static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4724 struct category64_t c;
4725 const char *r;
4726 uint32_t offset, xoffset, left;
4727 SectionRef S, xS;
4728 const char *name, *sym_name;
4729 uint64_t n_value;
4730
4731 r = get_pointer_64(p, offset, left, S, info);
4732 if (r == nullptr)
4733 return;
4734 memset(&c, '\0', sizeof(struct category64_t));
4735 if (left < sizeof(struct category64_t)) {
4736 memcpy(&c, r, left);
4737 outs() << " (category_t entends past the end of the section)\n";
4738 } else
4739 memcpy(&c, r, sizeof(struct category64_t));
4740 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4741 swapStruct(c);
4742
4743 outs() << " name ";
4744 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4745 info, n_value, c.name);
4746 if (n_value != 0) {
4747 if (info->verbose && sym_name != nullptr)
4748 outs() << sym_name;
4749 else
4750 outs() << format("0x%" PRIx64, n_value);
4751 if (c.name != 0)
4752 outs() << " + " << format("0x%" PRIx64, c.name);
4753 } else
4754 outs() << format("0x%" PRIx64, c.name);
4755 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4756 if (name != nullptr)
4757 outs() << format(" %.*s", left, name);
4758 outs() << "\n";
4759
4760 outs() << " cls ";
4761 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4762 n_value, c.cls);
4763 if (n_value != 0) {
4764 if (info->verbose && sym_name != nullptr)
4765 outs() << sym_name;
4766 else
4767 outs() << format("0x%" PRIx64, n_value);
4768 if (c.cls != 0)
4769 outs() << " + " << format("0x%" PRIx64, c.cls);
4770 } else
4771 outs() << format("0x%" PRIx64, c.cls);
4772 outs() << "\n";
4773 if (c.cls + n_value != 0)
4774 print_class64_t(c.cls + n_value, info);
4775
4776 outs() << " instanceMethods ";
4777 sym_name =
4778 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4779 info, n_value, c.instanceMethods);
4780 if (n_value != 0) {
4781 if (info->verbose && sym_name != nullptr)
4782 outs() << sym_name;
4783 else
4784 outs() << format("0x%" PRIx64, n_value);
4785 if (c.instanceMethods != 0)
4786 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4787 } else
4788 outs() << format("0x%" PRIx64, c.instanceMethods);
4789 outs() << "\n";
4790 if (c.instanceMethods + n_value != 0)
4791 print_method_list64_t(c.instanceMethods + n_value, info, "");
4792
4793 outs() << " classMethods ";
4794 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4795 S, info, n_value, c.classMethods);
4796 if (n_value != 0) {
4797 if (info->verbose && sym_name != nullptr)
4798 outs() << sym_name;
4799 else
4800 outs() << format("0x%" PRIx64, n_value);
4801 if (c.classMethods != 0)
4802 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4803 } else
4804 outs() << format("0x%" PRIx64, c.classMethods);
4805 outs() << "\n";
4806 if (c.classMethods + n_value != 0)
4807 print_method_list64_t(c.classMethods + n_value, info, "");
4808
4809 outs() << " protocols ";
4810 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4811 info, n_value, c.protocols);
4812 if (n_value != 0) {
4813 if (info->verbose && sym_name != nullptr)
4814 outs() << sym_name;
4815 else
4816 outs() << format("0x%" PRIx64, n_value);
4817 if (c.protocols != 0)
4818 outs() << " + " << format("0x%" PRIx64, c.protocols);
4819 } else
4820 outs() << format("0x%" PRIx64, c.protocols);
4821 outs() << "\n";
4822 if (c.protocols + n_value != 0)
4823 print_protocol_list64_t(c.protocols + n_value, info);
4824
4825 outs() << "instanceProperties ";
4826 sym_name =
4827 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4828 S, info, n_value, c.instanceProperties);
4829 if (n_value != 0) {
4830 if (info->verbose && sym_name != nullptr)
4831 outs() << sym_name;
4832 else
4833 outs() << format("0x%" PRIx64, n_value);
4834 if (c.instanceProperties != 0)
4835 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4836 } else
4837 outs() << format("0x%" PRIx64, c.instanceProperties);
4838 outs() << "\n";
4839 if (c.instanceProperties + n_value != 0)
4840 print_objc_property_list64(c.instanceProperties + n_value, info);
4841}
4842
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004843static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4844 struct category32_t c;
4845 const char *r;
4846 uint32_t offset, left;
4847 SectionRef S, xS;
4848 const char *name;
4849
4850 r = get_pointer_32(p, offset, left, S, info);
4851 if (r == nullptr)
4852 return;
4853 memset(&c, '\0', sizeof(struct category32_t));
4854 if (left < sizeof(struct category32_t)) {
4855 memcpy(&c, r, left);
4856 outs() << " (category_t entends past the end of the section)\n";
4857 } else
4858 memcpy(&c, r, sizeof(struct category32_t));
4859 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4860 swapStruct(c);
4861
4862 outs() << " name " << format("0x%" PRIx32, c.name);
4863 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4864 c.name);
Hans Wennborgcc9deb42015-09-29 18:02:48 +00004865 if (name)
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004866 outs() << " " << name;
4867 outs() << "\n";
4868
4869 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
4870 if (c.cls != 0)
4871 print_class32_t(c.cls, info);
4872 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4873 << "\n";
4874 if (c.instanceMethods != 0)
4875 print_method_list32_t(c.instanceMethods, info, "");
4876 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
4877 << "\n";
4878 if (c.classMethods != 0)
4879 print_method_list32_t(c.classMethods, info, "");
4880 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
4881 if (c.protocols != 0)
4882 print_protocol_list32_t(c.protocols, info);
4883 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
4884 << "\n";
4885 if (c.instanceProperties != 0)
4886 print_objc_property_list32(c.instanceProperties, info);
4887}
4888
Kevin Enderby0fc11822015-04-01 20:57:01 +00004889static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
4890 uint32_t i, left, offset, xoffset;
4891 uint64_t p, n_value;
4892 struct message_ref64 mr;
4893 const char *name, *sym_name;
4894 const char *r;
4895 SectionRef xS;
4896
4897 if (S == SectionRef())
4898 return;
4899
4900 StringRef SectName;
4901 S.getName(SectName);
4902 DataRefImpl Ref = S.getRawDataRefImpl();
4903 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4904 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4905 offset = 0;
4906 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4907 p = S.getAddress() + i;
4908 r = get_pointer_64(p, offset, left, S, info);
4909 if (r == nullptr)
4910 return;
4911 memset(&mr, '\0', sizeof(struct message_ref64));
4912 if (left < sizeof(struct message_ref64)) {
4913 memcpy(&mr, r, left);
4914 outs() << " (message_ref entends past the end of the section)\n";
4915 } else
4916 memcpy(&mr, r, sizeof(struct message_ref64));
4917 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4918 swapStruct(mr);
4919
4920 outs() << " imp ";
4921 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
4922 n_value, mr.imp);
4923 if (n_value != 0) {
4924 outs() << format("0x%" PRIx64, n_value) << " ";
4925 if (mr.imp != 0)
4926 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
4927 } else
4928 outs() << format("0x%" PRIx64, mr.imp) << " ";
4929 if (name != nullptr)
4930 outs() << " " << name;
4931 outs() << "\n";
4932
4933 outs() << " sel ";
4934 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
4935 info, n_value, mr.sel);
4936 if (n_value != 0) {
4937 if (info->verbose && sym_name != nullptr)
4938 outs() << sym_name;
4939 else
4940 outs() << format("0x%" PRIx64, n_value);
4941 if (mr.sel != 0)
4942 outs() << " + " << format("0x%" PRIx64, mr.sel);
4943 } else
4944 outs() << format("0x%" PRIx64, mr.sel);
4945 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
4946 if (name != nullptr)
4947 outs() << format(" %.*s", left, name);
4948 outs() << "\n";
4949
4950 offset += sizeof(struct message_ref64);
4951 }
4952}
4953
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004954static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
4955 uint32_t i, left, offset, xoffset, p;
4956 struct message_ref32 mr;
4957 const char *name, *r;
4958 SectionRef xS;
4959
4960 if (S == SectionRef())
4961 return;
4962
4963 StringRef SectName;
4964 S.getName(SectName);
4965 DataRefImpl Ref = S.getRawDataRefImpl();
4966 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4967 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4968 offset = 0;
4969 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4970 p = S.getAddress() + i;
4971 r = get_pointer_32(p, offset, left, S, info);
4972 if (r == nullptr)
4973 return;
4974 memset(&mr, '\0', sizeof(struct message_ref32));
4975 if (left < sizeof(struct message_ref32)) {
4976 memcpy(&mr, r, left);
4977 outs() << " (message_ref entends past the end of the section)\n";
4978 } else
4979 memcpy(&mr, r, sizeof(struct message_ref32));
4980 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4981 swapStruct(mr);
4982
4983 outs() << " imp " << format("0x%" PRIx32, mr.imp);
4984 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
4985 mr.imp);
4986 if (name != nullptr)
4987 outs() << " " << name;
4988 outs() << "\n";
4989
4990 outs() << " sel " << format("0x%" PRIx32, mr.sel);
4991 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
4992 if (name != nullptr)
4993 outs() << " " << name;
4994 outs() << "\n";
4995
4996 offset += sizeof(struct message_ref32);
4997 }
4998}
4999
Kevin Enderby0fc11822015-04-01 20:57:01 +00005000static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5001 uint32_t left, offset, swift_version;
5002 uint64_t p;
5003 struct objc_image_info64 o;
5004 const char *r;
5005
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00005006 if (S == SectionRef())
5007 return;
5008
Kevin Enderby0fc11822015-04-01 20:57:01 +00005009 StringRef SectName;
5010 S.getName(SectName);
5011 DataRefImpl Ref = S.getRawDataRefImpl();
5012 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5013 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5014 p = S.getAddress();
5015 r = get_pointer_64(p, offset, left, S, info);
5016 if (r == nullptr)
5017 return;
5018 memset(&o, '\0', sizeof(struct objc_image_info64));
5019 if (left < sizeof(struct objc_image_info64)) {
5020 memcpy(&o, r, left);
5021 outs() << " (objc_image_info entends past the end of the section)\n";
5022 } else
5023 memcpy(&o, r, sizeof(struct objc_image_info64));
5024 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5025 swapStruct(o);
5026 outs() << " version " << o.version << "\n";
5027 outs() << " flags " << format("0x%" PRIx32, o.flags);
5028 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5029 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5030 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5031 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5032 swift_version = (o.flags >> 8) & 0xff;
5033 if (swift_version != 0) {
5034 if (swift_version == 1)
5035 outs() << " Swift 1.0";
5036 else if (swift_version == 2)
5037 outs() << " Swift 1.1";
5038 else
5039 outs() << " unknown future Swift version (" << swift_version << ")";
5040 }
5041 outs() << "\n";
5042}
5043
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005044static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5045 uint32_t left, offset, swift_version, p;
5046 struct objc_image_info32 o;
5047 const char *r;
5048
5049 StringRef SectName;
5050 S.getName(SectName);
5051 DataRefImpl Ref = S.getRawDataRefImpl();
5052 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5053 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5054 p = S.getAddress();
5055 r = get_pointer_32(p, offset, left, S, info);
5056 if (r == nullptr)
5057 return;
5058 memset(&o, '\0', sizeof(struct objc_image_info32));
5059 if (left < sizeof(struct objc_image_info32)) {
5060 memcpy(&o, r, left);
5061 outs() << " (objc_image_info entends past the end of the section)\n";
5062 } else
5063 memcpy(&o, r, sizeof(struct objc_image_info32));
5064 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5065 swapStruct(o);
5066 outs() << " version " << o.version << "\n";
5067 outs() << " flags " << format("0x%" PRIx32, o.flags);
5068 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5069 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5070 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5071 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5072 swift_version = (o.flags >> 8) & 0xff;
5073 if (swift_version != 0) {
5074 if (swift_version == 1)
5075 outs() << " Swift 1.0";
5076 else if (swift_version == 2)
5077 outs() << " Swift 1.1";
5078 else
5079 outs() << " unknown future Swift version (" << swift_version << ")";
5080 }
5081 outs() << "\n";
5082}
5083
Kevin Enderby846c0002015-04-16 17:19:59 +00005084static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5085 uint32_t left, offset, p;
5086 struct imageInfo_t o;
5087 const char *r;
5088
5089 StringRef SectName;
5090 S.getName(SectName);
5091 DataRefImpl Ref = S.getRawDataRefImpl();
5092 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5093 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5094 p = S.getAddress();
5095 r = get_pointer_32(p, offset, left, S, info);
5096 if (r == nullptr)
5097 return;
5098 memset(&o, '\0', sizeof(struct imageInfo_t));
5099 if (left < sizeof(struct imageInfo_t)) {
5100 memcpy(&o, r, left);
5101 outs() << " (imageInfo entends past the end of the section)\n";
5102 } else
5103 memcpy(&o, r, sizeof(struct imageInfo_t));
5104 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5105 swapStruct(o);
5106 outs() << " version " << o.version << "\n";
5107 outs() << " flags " << format("0x%" PRIx32, o.flags);
5108 if (o.flags & 0x1)
5109 outs() << " F&C";
5110 if (o.flags & 0x2)
5111 outs() << " GC";
5112 if (o.flags & 0x4)
5113 outs() << " GC-only";
5114 else
5115 outs() << " RR";
5116 outs() << "\n";
5117}
5118
Kevin Enderby0fc11822015-04-01 20:57:01 +00005119static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5120 SymbolAddressMap AddrMap;
5121 if (verbose)
5122 CreateSymbolAddressMap(O, &AddrMap);
5123
5124 std::vector<SectionRef> Sections;
5125 for (const SectionRef &Section : O->sections()) {
5126 StringRef SectName;
5127 Section.getName(SectName);
5128 Sections.push_back(Section);
5129 }
5130
5131 struct DisassembleInfo info;
5132 // Set up the block of info used by the Symbolizer call backs.
5133 info.verbose = verbose;
5134 info.O = O;
5135 info.AddrMap = &AddrMap;
5136 info.Sections = &Sections;
5137 info.class_name = nullptr;
5138 info.selector_name = nullptr;
5139 info.method = nullptr;
5140 info.demangled_name = nullptr;
5141 info.bindtable = nullptr;
5142 info.adrp_addr = 0;
5143 info.adrp_inst = 0;
5144
Kevin Enderbyaac75382015-10-08 16:56:35 +00005145 info.depth = 0;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005146 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5147 if (CL != SectionRef()) {
5148 info.S = CL;
5149 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5150 } else {
5151 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5152 info.S = CL;
5153 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5154 }
5155
5156 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5157 if (CR != SectionRef()) {
5158 info.S = CR;
5159 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5160 } else {
5161 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5162 info.S = CR;
5163 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5164 }
5165
5166 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5167 if (SR != SectionRef()) {
5168 info.S = SR;
5169 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5170 } else {
5171 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5172 info.S = SR;
5173 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5174 }
5175
5176 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5177 if (CA != SectionRef()) {
5178 info.S = CA;
5179 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5180 } else {
5181 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5182 info.S = CA;
5183 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5184 }
5185
5186 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5187 if (PL != SectionRef()) {
5188 info.S = PL;
5189 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5190 } else {
5191 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5192 info.S = PL;
5193 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5194 }
5195
5196 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5197 if (MR != SectionRef()) {
5198 info.S = MR;
5199 print_message_refs64(MR, &info);
5200 } else {
5201 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5202 info.S = MR;
5203 print_message_refs64(MR, &info);
5204 }
5205
5206 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5207 if (II != SectionRef()) {
5208 info.S = II;
5209 print_image_info64(II, &info);
5210 } else {
5211 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5212 info.S = II;
5213 print_image_info64(II, &info);
5214 }
Kevin Enderby0bc6ed42015-04-01 21:50:45 +00005215
5216 if (info.bindtable != nullptr)
5217 delete info.bindtable;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005218}
5219
5220static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005221 SymbolAddressMap AddrMap;
5222 if (verbose)
5223 CreateSymbolAddressMap(O, &AddrMap);
5224
5225 std::vector<SectionRef> Sections;
5226 for (const SectionRef &Section : O->sections()) {
5227 StringRef SectName;
5228 Section.getName(SectName);
5229 Sections.push_back(Section);
5230 }
5231
5232 struct DisassembleInfo info;
5233 // Set up the block of info used by the Symbolizer call backs.
5234 info.verbose = verbose;
5235 info.O = O;
5236 info.AddrMap = &AddrMap;
5237 info.Sections = &Sections;
5238 info.class_name = nullptr;
5239 info.selector_name = nullptr;
5240 info.method = nullptr;
5241 info.demangled_name = nullptr;
5242 info.bindtable = nullptr;
5243 info.adrp_addr = 0;
5244 info.adrp_inst = 0;
5245
5246 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5247 if (CL != SectionRef()) {
5248 info.S = CL;
5249 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5250 } else {
5251 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5252 info.S = CL;
5253 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5254 }
5255
5256 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5257 if (CR != SectionRef()) {
5258 info.S = CR;
5259 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5260 } else {
5261 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5262 info.S = CR;
5263 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5264 }
5265
5266 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5267 if (SR != SectionRef()) {
5268 info.S = SR;
5269 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5270 } else {
5271 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5272 info.S = SR;
5273 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5274 }
5275
5276 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5277 if (CA != SectionRef()) {
5278 info.S = CA;
5279 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5280 } else {
5281 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5282 info.S = CA;
5283 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5284 }
5285
5286 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5287 if (PL != SectionRef()) {
5288 info.S = PL;
5289 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5290 } else {
5291 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5292 info.S = PL;
5293 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5294 }
5295
5296 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5297 if (MR != SectionRef()) {
5298 info.S = MR;
5299 print_message_refs32(MR, &info);
5300 } else {
5301 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5302 info.S = MR;
5303 print_message_refs32(MR, &info);
5304 }
5305
5306 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5307 if (II != SectionRef()) {
5308 info.S = II;
5309 print_image_info32(II, &info);
5310 } else {
5311 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5312 info.S = II;
5313 print_image_info32(II, &info);
5314 }
Kevin Enderby0fc11822015-04-01 20:57:01 +00005315}
5316
5317static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby846c0002015-04-16 17:19:59 +00005318 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5319 const char *r, *name, *defs;
5320 struct objc_module_t module;
5321 SectionRef S, xS;
5322 struct objc_symtab_t symtab;
5323 struct objc_class_t objc_class;
5324 struct objc_category_t objc_category;
5325
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005326 outs() << "Objective-C segment\n";
Kevin Enderby846c0002015-04-16 17:19:59 +00005327 S = get_section(O, "__OBJC", "__module_info");
5328 if (S == SectionRef())
5329 return false;
5330
5331 SymbolAddressMap AddrMap;
5332 if (verbose)
5333 CreateSymbolAddressMap(O, &AddrMap);
5334
5335 std::vector<SectionRef> Sections;
5336 for (const SectionRef &Section : O->sections()) {
5337 StringRef SectName;
5338 Section.getName(SectName);
5339 Sections.push_back(Section);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005340 }
Kevin Enderby846c0002015-04-16 17:19:59 +00005341
5342 struct DisassembleInfo info;
5343 // Set up the block of info used by the Symbolizer call backs.
5344 info.verbose = verbose;
5345 info.O = O;
5346 info.AddrMap = &AddrMap;
5347 info.Sections = &Sections;
5348 info.class_name = nullptr;
5349 info.selector_name = nullptr;
5350 info.method = nullptr;
5351 info.demangled_name = nullptr;
5352 info.bindtable = nullptr;
5353 info.adrp_addr = 0;
5354 info.adrp_inst = 0;
5355
5356 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5357 p = S.getAddress() + i;
5358 r = get_pointer_32(p, offset, left, S, &info, true);
5359 if (r == nullptr)
5360 return true;
5361 memset(&module, '\0', sizeof(struct objc_module_t));
5362 if (left < sizeof(struct objc_module_t)) {
5363 memcpy(&module, r, left);
5364 outs() << " (module extends past end of __module_info section)\n";
5365 } else
5366 memcpy(&module, r, sizeof(struct objc_module_t));
5367 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5368 swapStruct(module);
5369
5370 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5371 outs() << " version " << module.version << "\n";
5372 outs() << " size " << module.size << "\n";
5373 outs() << " name ";
5374 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5375 if (name != nullptr)
5376 outs() << format("%.*s", left, name);
5377 else
5378 outs() << format("0x%08" PRIx32, module.name)
5379 << "(not in an __OBJC section)";
5380 outs() << "\n";
5381
5382 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5383 if (module.symtab == 0 || r == nullptr) {
5384 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5385 << " (not in an __OBJC section)\n";
5386 continue;
5387 }
5388 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5389 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5390 defs_left = 0;
5391 defs = nullptr;
5392 if (left < sizeof(struct objc_symtab_t)) {
5393 memcpy(&symtab, r, left);
5394 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5395 } else {
5396 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5397 if (left > sizeof(struct objc_symtab_t)) {
5398 defs_left = left - sizeof(struct objc_symtab_t);
5399 defs = r + sizeof(struct objc_symtab_t);
5400 }
5401 }
5402 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5403 swapStruct(symtab);
5404
5405 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5406 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5407 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5408 if (r == nullptr)
5409 outs() << " (not in an __OBJC section)";
5410 outs() << "\n";
5411 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5412 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5413 if (symtab.cls_def_cnt > 0)
5414 outs() << "\tClass Definitions\n";
5415 for (j = 0; j < symtab.cls_def_cnt; j++) {
5416 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5417 outs() << "\t(remaining class defs entries entends past the end of the "
5418 << "section)\n";
5419 break;
5420 }
5421 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5422 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5423 sys::swapByteOrder(def);
5424
5425 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5426 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5427 if (r != nullptr) {
5428 if (left > sizeof(struct objc_class_t)) {
5429 outs() << "\n";
5430 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5431 } else {
5432 outs() << " (entends past the end of the section)\n";
5433 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5434 memcpy(&objc_class, r, left);
5435 }
5436 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5437 swapStruct(objc_class);
5438 print_objc_class_t(&objc_class, &info);
5439 } else {
5440 outs() << "(not in an __OBJC section)\n";
5441 }
5442
5443 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5444 outs() << "\tMeta Class";
5445 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5446 if (r != nullptr) {
5447 if (left > sizeof(struct objc_class_t)) {
5448 outs() << "\n";
5449 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5450 } else {
5451 outs() << " (entends past the end of the section)\n";
5452 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5453 memcpy(&objc_class, r, left);
5454 }
5455 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5456 swapStruct(objc_class);
5457 print_objc_class_t(&objc_class, &info);
5458 } else {
5459 outs() << "(not in an __OBJC section)\n";
5460 }
5461 }
5462 }
5463 if (symtab.cat_def_cnt > 0)
5464 outs() << "\tCategory Definitions\n";
5465 for (j = 0; j < symtab.cat_def_cnt; j++) {
5466 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5467 outs() << "\t(remaining category defs entries entends past the end of "
5468 << "the section)\n";
5469 break;
5470 }
5471 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5472 sizeof(uint32_t));
5473 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5474 sys::swapByteOrder(def);
5475
5476 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5477 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5478 << format("0x%08" PRIx32, def);
5479 if (r != nullptr) {
5480 if (left > sizeof(struct objc_category_t)) {
5481 outs() << "\n";
5482 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5483 } else {
5484 outs() << " (entends past the end of the section)\n";
5485 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5486 memcpy(&objc_category, r, left);
5487 }
5488 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5489 swapStruct(objc_category);
5490 print_objc_objc_category_t(&objc_category, &info);
5491 } else {
5492 outs() << "(not in an __OBJC section)\n";
5493 }
5494 }
5495 }
5496 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5497 if (II != SectionRef())
5498 print_image_info(II, &info);
5499
5500 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005501}
5502
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00005503static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5504 uint32_t size, uint32_t addr) {
5505 SymbolAddressMap AddrMap;
5506 CreateSymbolAddressMap(O, &AddrMap);
5507
5508 std::vector<SectionRef> Sections;
5509 for (const SectionRef &Section : O->sections()) {
5510 StringRef SectName;
5511 Section.getName(SectName);
5512 Sections.push_back(Section);
5513 }
5514
5515 struct DisassembleInfo info;
5516 // Set up the block of info used by the Symbolizer call backs.
5517 info.verbose = true;
5518 info.O = O;
5519 info.AddrMap = &AddrMap;
5520 info.Sections = &Sections;
5521 info.class_name = nullptr;
5522 info.selector_name = nullptr;
5523 info.method = nullptr;
5524 info.demangled_name = nullptr;
5525 info.bindtable = nullptr;
5526 info.adrp_addr = 0;
5527 info.adrp_inst = 0;
5528
5529 const char *p;
5530 struct objc_protocol_t protocol;
5531 uint32_t left, paddr;
5532 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5533 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5534 left = size - (p - sect);
5535 if (left < sizeof(struct objc_protocol_t)) {
5536 outs() << "Protocol extends past end of __protocol section\n";
5537 memcpy(&protocol, p, left);
5538 } else
5539 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5540 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5541 swapStruct(protocol);
5542 paddr = addr + (p - sect);
5543 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5544 if (print_protocol(paddr, 0, &info))
5545 outs() << "(not in an __OBJC section)\n";
5546 }
5547}
5548
Kevin Enderby0fc11822015-04-01 20:57:01 +00005549static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
5550 if (O->is64Bit())
5551 printObjc2_64bit_MetaData(O, verbose);
5552 else {
5553 MachO::mach_header H;
5554 H = O->getHeader();
5555 if (H.cputype == MachO::CPU_TYPE_ARM)
5556 printObjc2_32bit_MetaData(O, verbose);
5557 else {
5558 // This is the 32-bit non-arm cputype case. Which is normally
5559 // the first Objective-C ABI. But it may be the case of a
5560 // binary for the iOS simulator which is the second Objective-C
5561 // ABI. In that case printObjc1_32bit_MetaData() will determine that
5562 // and return false.
Hans Wennborgcc9deb42015-09-29 18:02:48 +00005563 if (!printObjc1_32bit_MetaData(O, verbose))
Kevin Enderby0fc11822015-04-01 20:57:01 +00005564 printObjc2_32bit_MetaData(O, verbose);
5565 }
5566 }
5567}
5568
Kevin Enderbybf246f52014-09-24 23:08:22 +00005569// GuessLiteralPointer returns a string which for the item in the Mach-O file
5570// for the address passed in as ReferenceValue for printing as a comment with
5571// the instruction and also returns the corresponding type of that item
5572// indirectly through ReferenceType.
5573//
5574// If ReferenceValue is an address of literal cstring then a pointer to the
5575// cstring is returned and ReferenceType is set to
5576// LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
5577//
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005578// If ReferenceValue is an address of an Objective-C CFString, Selector ref or
5579// Class ref that name is returned and the ReferenceType is set accordingly.
5580//
5581// Lastly, literals which are Symbol address in a literal pool are looked for
5582// and if found the symbol name is returned and ReferenceType is set to
5583// LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
5584//
5585// If there is no item in the Mach-O file for the address passed in as
5586// ReferenceValue nullptr is returned and ReferenceType is unchanged.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00005587static const char *GuessLiteralPointer(uint64_t ReferenceValue,
5588 uint64_t ReferencePC,
5589 uint64_t *ReferenceType,
5590 struct DisassembleInfo *info) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005591 // First see if there is an external relocation entry at the ReferencePC.
Kevin Enderbyd90a4172015-10-10 00:05:01 +00005592 if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
5593 uint64_t sect_addr = info->S.getAddress();
5594 uint64_t sect_offset = ReferencePC - sect_addr;
5595 bool reloc_found = false;
5596 DataRefImpl Rel;
5597 MachO::any_relocation_info RE;
5598 bool isExtern = false;
5599 SymbolRef Symbol;
5600 for (const RelocationRef &Reloc : info->S.relocations()) {
5601 uint64_t RelocOffset = Reloc.getOffset();
5602 if (RelocOffset == sect_offset) {
5603 Rel = Reloc.getRawDataRefImpl();
5604 RE = info->O->getRelocation(Rel);
5605 if (info->O->isRelocationScattered(RE))
5606 continue;
5607 isExtern = info->O->getPlainRelocationExternal(RE);
5608 if (isExtern) {
5609 symbol_iterator RelocSym = Reloc.getSymbol();
5610 Symbol = *RelocSym;
5611 }
5612 reloc_found = true;
5613 break;
Kevin Enderbybf246f52014-09-24 23:08:22 +00005614 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005615 }
Kevin Enderbyd90a4172015-10-10 00:05:01 +00005616 // If there is an external relocation entry for a symbol in a section
5617 // then used that symbol's value for the value of the reference.
5618 if (reloc_found && isExtern) {
5619 if (info->O->getAnyRelocationPCRel(RE)) {
5620 unsigned Type = info->O->getAnyRelocationType(RE);
5621 if (Type == MachO::X86_64_RELOC_SIGNED) {
5622 ReferenceValue = Symbol.getValue();
5623 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005624 }
5625 }
5626 }
5627
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005628 // Look for literals such as Objective-C CFStrings refs, Selector refs,
5629 // Message refs and Class refs.
5630 bool classref, selref, msgref, cfstring;
5631 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
5632 selref, msgref, cfstring);
David Blaikie33dd45d02015-03-23 18:39:02 +00005633 if (classref && pointer_value == 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005634 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
5635 // And the pointer_value in that section is typically zero as it will be
5636 // set by dyld as part of the "bind information".
5637 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
5638 if (name != nullptr) {
5639 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
Hans Wennborgdb53e302014-10-23 21:59:17 +00005640 const char *class_name = strrchr(name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005641 if (class_name != nullptr && class_name[1] == '_' &&
5642 class_name[2] != '\0') {
5643 info->class_name = class_name + 2;
5644 return name;
5645 }
5646 }
5647 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005648
David Blaikie33dd45d02015-03-23 18:39:02 +00005649 if (classref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005650 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5651 const char *name =
5652 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
5653 if (name != nullptr)
5654 info->class_name = name;
5655 else
5656 name = "bad class ref";
Kevin Enderbybf246f52014-09-24 23:08:22 +00005657 return name;
5658 }
5659
David Blaikie33dd45d02015-03-23 18:39:02 +00005660 if (cfstring) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005661 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
5662 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
5663 return name;
5664 }
5665
David Blaikie33dd45d02015-03-23 18:39:02 +00005666 if (selref && pointer_value == 0)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005667 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
5668
5669 if (pointer_value != 0)
5670 ReferenceValue = pointer_value;
5671
5672 const char *name = GuessCstringPointer(ReferenceValue, info);
5673 if (name) {
David Blaikie33dd45d02015-03-23 18:39:02 +00005674 if (pointer_value != 0 && selref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005675 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
5676 info->selector_name = name;
David Blaikie33dd45d02015-03-23 18:39:02 +00005677 } else if (pointer_value != 0 && msgref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005678 info->class_name = nullptr;
5679 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
5680 info->selector_name = name;
5681 } else
5682 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
5683 return name;
5684 }
5685
5686 // Lastly look for an indirect symbol with this ReferenceValue which is in
5687 // a literal pool. If found return that symbol name.
5688 name = GuessIndirectSymbol(ReferenceValue, info);
5689 if (name) {
5690 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
5691 return name;
5692 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005693
5694 return nullptr;
5695}
5696
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005697// SymbolizerSymbolLookUp is the symbol lookup function passed when creating
Kevin Enderbybf246f52014-09-24 23:08:22 +00005698// the Symbolizer. It looks up the ReferenceValue using the info passed via the
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005699// pointer to the struct DisassembleInfo that was passed when MCSymbolizer
5700// is created and returns the symbol name that matches the ReferenceValue or
5701// nullptr if none. The ReferenceType is passed in for the IN type of
5702// reference the instruction is making from the values in defined in the header
5703// "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
5704// Out type and the ReferenceName will also be set which is added as a comment
5705// to the disassembled instruction.
5706//
Kevin Enderby04bf6932014-10-28 23:39:46 +00005707#if HAVE_CXXABI_H
5708// If the symbol name is a C++ mangled name then the demangled name is
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005709// returned through ReferenceName and ReferenceType is set to
5710// LLVMDisassembler_ReferenceType_DeMangled_Name .
Kevin Enderby04bf6932014-10-28 23:39:46 +00005711#endif
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005712//
5713// When this is called to get a symbol name for a branch target then the
5714// ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
5715// SymbolValue will be looked for in the indirect symbol table to determine if
5716// it is an address for a symbol stub. If so then the symbol name for that
5717// stub is returned indirectly through ReferenceName and then ReferenceType is
5718// set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
5719//
Kevin Enderbybf246f52014-09-24 23:08:22 +00005720// When this is called with an value loaded via a PC relative load then
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005721// ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
5722// SymbolValue is checked to be an address of literal pointer, symbol pointer,
5723// or an Objective-C meta data reference. If so the output ReferenceType is
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005724// set to correspond to that as well as setting the ReferenceName.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00005725static const char *SymbolizerSymbolLookUp(void *DisInfo,
5726 uint64_t ReferenceValue,
5727 uint64_t *ReferenceType,
5728 uint64_t ReferencePC,
5729 const char **ReferenceName) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005730 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
Kevin Enderbybf246f52014-09-24 23:08:22 +00005731 // If no verbose symbolic information is wanted then just return nullptr.
David Blaikie33dd45d02015-03-23 18:39:02 +00005732 if (!info->verbose) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005733 *ReferenceName = nullptr;
5734 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005735 return nullptr;
5736 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00005737
Kevin Enderbyf6d25852015-01-31 00:37:11 +00005738 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderbybf246f52014-09-24 23:08:22 +00005739
Kevin Enderby85974882014-09-26 22:20:44 +00005740 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
5741 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005742 if (*ReferenceName != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005743 method_reference(info, ReferenceType, ReferenceName);
5744 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
5745 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
5746 } else
Kevin Enderby04bf6932014-10-28 23:39:46 +00005747#if HAVE_CXXABI_H
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005748 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00005749 if (info->demangled_name != nullptr)
5750 free(info->demangled_name);
5751 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005752 info->demangled_name =
5753 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005754 if (info->demangled_name != nullptr) {
5755 *ReferenceName = info->demangled_name;
5756 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5757 } else
5758 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5759 } else
5760#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005761 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5762 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
5763 *ReferenceName =
5764 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
Kevin Enderby85974882014-09-26 22:20:44 +00005765 if (*ReferenceName)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005766 method_reference(info, ReferenceType, ReferenceName);
Kevin Enderby85974882014-09-26 22:20:44 +00005767 else
5768 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005769 // If this is arm64 and the reference is an adrp instruction save the
5770 // instruction, passed in ReferenceValue and the address of the instruction
5771 // for use later if we see and add immediate instruction.
5772 } else if (info->O->getArch() == Triple::aarch64 &&
5773 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
5774 info->adrp_inst = ReferenceValue;
5775 info->adrp_addr = ReferencePC;
5776 SymbolName = nullptr;
5777 *ReferenceName = nullptr;
5778 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5779 // If this is arm64 and reference is an add immediate instruction and we
5780 // have
5781 // seen an adrp instruction just before it and the adrp's Xd register
5782 // matches
5783 // this add's Xn register reconstruct the value being referenced and look to
5784 // see if it is a literal pointer. Note the add immediate instruction is
5785 // passed in ReferenceValue.
5786 } else if (info->O->getArch() == Triple::aarch64 &&
5787 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
5788 ReferencePC - 4 == info->adrp_addr &&
5789 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5790 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5791 uint32_t addxri_inst;
5792 uint64_t adrp_imm, addxri_imm;
5793
5794 adrp_imm =
5795 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5796 if (info->adrp_inst & 0x0200000)
5797 adrp_imm |= 0xfffffffffc000000LL;
5798
5799 addxri_inst = ReferenceValue;
5800 addxri_imm = (addxri_inst >> 10) & 0xfff;
5801 if (((addxri_inst >> 22) & 0x3) == 1)
5802 addxri_imm <<= 12;
5803
5804 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5805 (adrp_imm << 12) + addxri_imm;
5806
5807 *ReferenceName =
5808 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5809 if (*ReferenceName == nullptr)
5810 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5811 // If this is arm64 and the reference is a load register instruction and we
5812 // have seen an adrp instruction just before it and the adrp's Xd register
5813 // matches this add's Xn register reconstruct the value being referenced and
5814 // look to see if it is a literal pointer. Note the load register
5815 // instruction is passed in ReferenceValue.
5816 } else if (info->O->getArch() == Triple::aarch64 &&
5817 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
5818 ReferencePC - 4 == info->adrp_addr &&
5819 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5820 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5821 uint32_t ldrxui_inst;
5822 uint64_t adrp_imm, ldrxui_imm;
5823
5824 adrp_imm =
5825 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5826 if (info->adrp_inst & 0x0200000)
5827 adrp_imm |= 0xfffffffffc000000LL;
5828
5829 ldrxui_inst = ReferenceValue;
5830 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
5831
5832 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5833 (adrp_imm << 12) + (ldrxui_imm << 3);
5834
5835 *ReferenceName =
5836 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5837 if (*ReferenceName == nullptr)
5838 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5839 }
5840 // If this arm64 and is an load register (PC-relative) instruction the
5841 // ReferenceValue is the PC plus the immediate value.
5842 else if (info->O->getArch() == Triple::aarch64 &&
5843 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
5844 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
5845 *ReferenceName =
5846 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5847 if (*ReferenceName == nullptr)
5848 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby85974882014-09-26 22:20:44 +00005849 }
Kevin Enderby04bf6932014-10-28 23:39:46 +00005850#if HAVE_CXXABI_H
5851 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5852 if (info->demangled_name != nullptr)
5853 free(info->demangled_name);
5854 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00005855 info->demangled_name =
5856 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00005857 if (info->demangled_name != nullptr) {
5858 *ReferenceName = info->demangled_name;
5859 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5860 }
5861 }
5862#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00005863 else {
Kevin Enderbybf246f52014-09-24 23:08:22 +00005864 *ReferenceName = nullptr;
5865 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5866 }
5867
5868 return SymbolName;
5869}
5870
Kevin Enderbybf246f52014-09-24 23:08:22 +00005871/// \brief Emits the comments that are stored in the CommentStream.
5872/// Each comment in the CommentStream must end with a newline.
5873static void emitComments(raw_svector_ostream &CommentStream,
5874 SmallString<128> &CommentsToEmit,
5875 formatted_raw_ostream &FormattedOS,
5876 const MCAsmInfo &MAI) {
5877 // Flush the stream before taking its content.
Kevin Enderbybf246f52014-09-24 23:08:22 +00005878 StringRef Comments = CommentsToEmit.str();
5879 // Get the default information for printing a comment.
5880 const char *CommentBegin = MAI.getCommentString();
5881 unsigned CommentColumn = MAI.getCommentColumn();
5882 bool IsFirst = true;
5883 while (!Comments.empty()) {
5884 if (!IsFirst)
5885 FormattedOS << '\n';
5886 // Emit a line of comments.
5887 FormattedOS.PadToColumn(CommentColumn);
5888 size_t Position = Comments.find('\n');
5889 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
5890 // Move after the newline character.
5891 Comments = Comments.substr(Position + 1);
5892 IsFirst = false;
5893 }
5894 FormattedOS.flush();
5895
5896 // Tell the comment stream that the vector changed underneath it.
5897 CommentsToEmit.clear();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005898}
5899
Kevin Enderby95df54c2015-02-04 01:01:38 +00005900static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
5901 StringRef DisSegName, StringRef DisSectName) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005902 const char *McpuDefault = nullptr;
5903 const Target *ThumbTarget = nullptr;
5904 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005905 if (!TheTarget) {
5906 // GetTarget prints out stuff.
5907 return;
5908 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005909 if (MCPU.empty() && McpuDefault)
5910 MCPU = McpuDefault;
5911
Ahmed Charles56440fd2014-03-06 05:51:42 +00005912 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005913 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005914 if (ThumbTarget)
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005915 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005916
Kevin Enderbyc9595622014-08-06 23:24:41 +00005917 // Package up features to be passed to target/subtarget
5918 std::string FeaturesStr;
5919 if (MAttrs.size()) {
5920 SubtargetFeatures Features;
5921 for (unsigned i = 0; i != MAttrs.size(); ++i)
5922 Features.AddFeature(MAttrs[i]);
5923 FeaturesStr = Features.getString();
5924 }
5925
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005926 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00005927 std::unique_ptr<const MCRegisterInfo> MRI(
5928 TheTarget->createMCRegInfo(TripleName));
5929 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +00005930 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +00005931 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +00005932 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Craig Toppere6cb63e2014-04-25 04:24:47 +00005933 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005934 std::unique_ptr<MCDisassembler> DisAsm(
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005935 TheTarget->createMCDisassembler(*STI, Ctx));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005936 std::unique_ptr<MCSymbolizer> Symbolizer;
5937 struct DisassembleInfo SymbolizerInfo;
5938 std::unique_ptr<MCRelocationInfo> RelInfo(
5939 TheTarget->createMCRelocationInfo(TripleName, Ctx));
5940 if (RelInfo) {
5941 Symbolizer.reset(TheTarget->createMCSymbolizer(
5942 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00005943 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00005944 DisAsm->setSymbolizer(std::move(Symbolizer));
5945 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005946 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +00005947 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00005948 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00005949 // Set the display preference for hex vs. decimal immediates.
5950 IP->setPrintImmHex(PrintImmHex);
5951 // Comment stream and backing vector.
5952 SmallString<128> CommentsToEmit;
5953 raw_svector_ostream CommentStream(CommentsToEmit);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00005954 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
5955 // if it is done then arm64 comments for string literals don't get printed
5956 // and some constant get printed instead and not setting it causes intel
5957 // (32-bit and 64-bit) comments printed with different spacing before the
5958 // comment causing different diffs with the 'C' disassembler library API.
5959 // IP->setCommentStream(CommentStream);
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005960
Kevin Enderbyae3c1262014-11-14 21:52:18 +00005961 if (!AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +00005962 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00005963 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005964 return;
5965 }
5966
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005967 // Set up thumb disassembler.
5968 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
5969 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
5970 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005971 std::unique_ptr<MCDisassembler> ThumbDisAsm;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005972 std::unique_ptr<MCInstPrinter> ThumbIP;
5973 std::unique_ptr<MCContext> ThumbCtx;
Kevin Enderby930fdc72014-11-06 19:00:13 +00005974 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
5975 struct DisassembleInfo ThumbSymbolizerInfo;
5976 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005977 if (ThumbTarget) {
5978 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
5979 ThumbAsmInfo.reset(
5980 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
5981 ThumbSTI.reset(
5982 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
5983 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
5984 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
Kevin Enderby930fdc72014-11-06 19:00:13 +00005985 MCContext *PtrThumbCtx = ThumbCtx.get();
5986 ThumbRelInfo.reset(
5987 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
5988 if (ThumbRelInfo) {
5989 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
5990 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00005991 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
Kevin Enderby930fdc72014-11-06 19:00:13 +00005992 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
5993 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00005994 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
5995 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00005996 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
5997 *ThumbInstrInfo, *ThumbMRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00005998 // Set the display preference for hex vs. decimal immediates.
5999 ThumbIP->setPrintImmHex(PrintImmHex);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006000 }
6001
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006002 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006003 errs() << "error: couldn't initialize disassembler for target "
6004 << ThumbTripleName << '\n';
6005 return;
6006 }
6007
Charles Davis8bdfafd2013-09-01 04:28:48 +00006008 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006009
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006010 // FIXME: Using the -cfg command line option, this code used to be able to
6011 // annotate relocations with the referenced symbol's name, and if this was
6012 // inside a __[cf]string section, the data it points to. This is now replaced
6013 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +00006014 std::vector<SectionRef> Sections;
6015 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006016 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +00006017 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006018
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00006019 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
Kevin Enderby273ae012013-06-06 17:20:50 +00006020 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006021
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006022 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +00006023 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006024
Kevin Enderby273ae012013-06-06 17:20:50 +00006025 // Build a data in code table that is sorted on by the address of each entry.
6026 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +00006027 if (Header.filetype == MachO::MH_OBJECT)
Rafael Espindola80291272014-10-08 15:28:58 +00006028 BaseAddress = Sections[0].getAddress();
Kevin Enderby273ae012013-06-06 17:20:50 +00006029 else
6030 BaseAddress = BaseSegmentAddress;
6031 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +00006032 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +00006033 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006034 uint32_t Offset;
6035 DI->getOffset(Offset);
6036 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6037 }
6038 array_pod_sort(Dices.begin(), Dices.end());
6039
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006040#ifndef NDEBUG
6041 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6042#else
6043 raw_ostream &DebugOut = nulls();
6044#endif
6045
Ahmed Charles56440fd2014-03-06 05:51:42 +00006046 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +00006047 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +00006048 // Try to find debug info and set up the DIContext for it.
6049 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +00006050 // A separate DSym file path was specified, parse it as a macho file,
6051 // get the sections and supply it to the section name parsing machinery.
6052 if (!DSYMFile.empty()) {
Rafael Espindola48af1c22014-08-19 18:44:46 +00006053 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006054 MemoryBuffer::getFileOrSTDIN(DSYMFile);
Rafael Espindola48af1c22014-08-19 18:44:46 +00006055 if (std::error_code EC = BufOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006056 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
Benjamin Kramer699128e2011-09-21 01:13:19 +00006057 return;
6058 }
Rafael Espindola48af1c22014-08-19 18:44:46 +00006059 DbgObj =
6060 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6061 .get()
6062 .release();
Benjamin Kramer699128e2011-09-21 01:13:19 +00006063 }
6064
Eric Christopher7370b552012-11-12 21:40:38 +00006065 // Setup the DIContext
Zachary Turner6489d7b2015-04-23 17:37:47 +00006066 diContext.reset(new DWARFContextInMemory(*DbgObj));
Benjamin Kramer699128e2011-09-21 01:13:19 +00006067 }
6068
Colin LeMahieufcc32762015-07-29 19:08:10 +00006069 if (FilterSections.size() == 0)
Kevin Enderby95df54c2015-02-04 01:01:38 +00006070 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006071
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006072 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00006073 StringRef SectName;
Kevin Enderby95df54c2015-02-04 01:01:38 +00006074 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6075 continue;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006076
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006077 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006078
Rafael Espindolab0f76a42013-04-05 15:15:22 +00006079 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
Kevin Enderby95df54c2015-02-04 01:01:38 +00006080 if (SegmentName != DisSegName)
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006081 continue;
6082
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006083 StringRef BytesStr;
6084 Sections[SectIdx].getContents(BytesStr);
Aaron Ballman106fd7b2014-11-12 14:01:17 +00006085 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6086 BytesStr.size());
Rafael Espindola80291272014-10-08 15:28:58 +00006087 uint64_t SectAddress = Sections[SectIdx].getAddress();
Rafael Espindolabd604f22014-11-07 00:52:15 +00006088
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006089 bool symbolTableWorked = false;
6090
Kevin Enderbybf246f52014-09-24 23:08:22 +00006091 // Create a map of symbol addresses to symbol names for use by
6092 // the SymbolizerSymbolLookUp() routine.
6093 SymbolAddressMap AddrMap;
Kevin Enderby6a221752015-03-17 17:10:57 +00006094 bool DisSymNameFound = false;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006095 for (const SymbolRef &Symbol : MachOOF->symbols()) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006096 SymbolRef::Type ST = Symbol.getType();
Kevin Enderbybf246f52014-09-24 23:08:22 +00006097 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6098 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +00006099 uint64_t Address = Symbol.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006100 ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
6101 if (std::error_code EC = SymNameOrErr.getError())
6102 report_fatal_error(EC.message());
6103 StringRef SymName = *SymNameOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006104 AddrMap[Address] = SymName;
Kevin Enderby6a221752015-03-17 17:10:57 +00006105 if (!DisSymName.empty() && DisSymName == SymName)
6106 DisSymNameFound = true;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006107 }
6108 }
David Blaikie33dd45d02015-03-23 18:39:02 +00006109 if (!DisSymName.empty() && !DisSymNameFound) {
Kevin Enderby6a221752015-03-17 17:10:57 +00006110 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6111 return;
6112 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006113 // Set up the block of info used by the Symbolizer call backs.
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006114 SymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006115 SymbolizerInfo.O = MachOOF;
6116 SymbolizerInfo.S = Sections[SectIdx];
Kevin Enderbybf246f52014-09-24 23:08:22 +00006117 SymbolizerInfo.AddrMap = &AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006118 SymbolizerInfo.Sections = &Sections;
6119 SymbolizerInfo.class_name = nullptr;
6120 SymbolizerInfo.selector_name = nullptr;
6121 SymbolizerInfo.method = nullptr;
Kevin Enderby04bf6932014-10-28 23:39:46 +00006122 SymbolizerInfo.demangled_name = nullptr;
Kevin Enderby078be602014-10-23 19:53:12 +00006123 SymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006124 SymbolizerInfo.adrp_addr = 0;
6125 SymbolizerInfo.adrp_inst = 0;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006126 // Same for the ThumbSymbolizer
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006127 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006128 ThumbSymbolizerInfo.O = MachOOF;
6129 ThumbSymbolizerInfo.S = Sections[SectIdx];
6130 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6131 ThumbSymbolizerInfo.Sections = &Sections;
6132 ThumbSymbolizerInfo.class_name = nullptr;
6133 ThumbSymbolizerInfo.selector_name = nullptr;
6134 ThumbSymbolizerInfo.method = nullptr;
6135 ThumbSymbolizerInfo.demangled_name = nullptr;
6136 ThumbSymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006137 ThumbSymbolizerInfo.adrp_addr = 0;
6138 ThumbSymbolizerInfo.adrp_inst = 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006139
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006140 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006141 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006142 ErrorOr<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
6143 if (std::error_code EC = SymNameOrErr.getError())
6144 report_fatal_error(EC.message());
6145 StringRef SymName = *SymNameOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006146
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006147 SymbolRef::Type ST = Symbols[SymIdx].getType();
Owen Andersond9243c42011-10-17 21:37:35 +00006148 if (ST != SymbolRef::ST_Function)
6149 continue;
6150
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006151 // Make sure the symbol is defined in this section.
Rafael Espindola80291272014-10-08 15:28:58 +00006152 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Owen Andersond9243c42011-10-17 21:37:35 +00006153 if (!containsSym)
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006154 continue;
6155
Kevin Enderby6a221752015-03-17 17:10:57 +00006156 // If we are only disassembling one symbol see if this is that symbol.
6157 if (!DisSymName.empty() && DisSymName != SymName)
6158 continue;
6159
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006160 // Start at the address of the symbol relative to the section's address.
Rafael Espindoladea00162015-07-03 17:44:18 +00006161 uint64_t Start = Symbols[SymIdx].getValue();
Rafael Espindola80291272014-10-08 15:28:58 +00006162 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006163 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006164
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006165 // Stop disassembling either at the beginning of the next symbol or at
6166 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +00006167 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +00006168 uint64_t NextSym = 0;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006169 uint64_t NextSymIdx = SymIdx + 1;
Owen Andersond9243c42011-10-17 21:37:35 +00006170 while (Symbols.size() > NextSymIdx) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00006171 SymbolRef::Type NextSymType = Symbols[NextSymIdx].getType();
Owen Andersond9243c42011-10-17 21:37:35 +00006172 if (NextSymType == SymbolRef::ST_Function) {
Rafael Espindola80291272014-10-08 15:28:58 +00006173 containsNextSym =
6174 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Rafael Espindoladea00162015-07-03 17:44:18 +00006175 NextSym = Symbols[NextSymIdx].getValue();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006176 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006177 break;
6178 }
6179 ++NextSymIdx;
6180 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006181
Rafael Espindola80291272014-10-08 15:28:58 +00006182 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006183 uint64_t End = containsNextSym ? NextSym : SectSize;
Owen Andersond9243c42011-10-17 21:37:35 +00006184 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006185
6186 symbolTableWorked = true;
Rafael Espindolabd604f22014-11-07 00:52:15 +00006187
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006188 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6189 bool isThumb =
6190 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
6191
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006192 outs() << SymName << ":\n";
6193 DILineInfo lastLine;
6194 for (uint64_t Index = Start; Index < End; Index += Size) {
6195 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +00006196
Kevin Enderbybf246f52014-09-24 23:08:22 +00006197 uint64_t PC = SectAddress + Index;
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006198 if (!NoLeadingAddr) {
6199 if (FullLeadingAddr) {
6200 if (MachOOF->is64Bit())
6201 outs() << format("%016" PRIx64, PC);
6202 else
6203 outs() << format("%08" PRIx64, PC);
6204 } else {
6205 outs() << format("%8" PRIx64 ":", PC);
6206 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006207 }
6208 if (!NoShowRawInsn)
6209 outs() << "\t";
Kevin Enderby273ae012013-06-06 17:20:50 +00006210
6211 // Check the data in code table here to see if this is data not an
6212 // instruction to be disassembled.
6213 DiceTable Dice;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006214 Dice.push_back(std::make_pair(PC, DiceRef()));
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006215 dice_table_iterator DTI =
6216 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6217 compareDiceTableEntries);
6218 if (DTI != Dices.end()) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006219 uint16_t Length;
6220 DTI->second.getLength(Length);
Kevin Enderby273ae012013-06-06 17:20:50 +00006221 uint16_t Kind;
6222 DTI->second.getKind(Kind);
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00006223 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006224 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6225 (PC == (DTI->first + Length - 1)) && (Length & 1))
6226 Size++;
Kevin Enderby273ae012013-06-06 17:20:50 +00006227 continue;
6228 }
6229
Kevin Enderbybf246f52014-09-24 23:08:22 +00006230 SmallVector<char, 64> AnnotationsBytes;
6231 raw_svector_ostream Annotations(AnnotationsBytes);
6232
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006233 bool gotInst;
6234 if (isThumb)
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006235 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006236 PC, DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006237 else
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006238 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
Kevin Enderbybf246f52014-09-24 23:08:22 +00006239 DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006240 if (gotInst) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006241 if (!NoShowRawInsn) {
Craig Topper0013be12015-09-21 05:32:41 +00006242 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006243 }
6244 formatted_raw_ostream FormattedOS(outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006245 StringRef AnnotationsStr = Annotations.str();
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006246 if (isThumb)
Akira Hatanakab46d0232015-03-27 20:36:02 +00006247 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006248 else
Akira Hatanaka1d079942015-03-28 20:44:05 +00006249 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006250 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
Owen Andersond9243c42011-10-17 21:37:35 +00006251
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006252 // Print debug info.
6253 if (diContext) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006254 DILineInfo dli = diContext->getLineInfoForAddress(PC);
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006255 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +00006256 if (dli != lastLine && dli.Line != 0)
6257 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6258 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006259 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006260 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006261 outs() << "\n";
6262 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006263 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006264 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006265 outs() << format("\t.byte 0x%02x #bad opcode\n",
6266 *(Bytes.data() + Index) & 0xff);
6267 Size = 1; // skip exactly one illegible byte and move on.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006268 } else if (Arch == Triple::aarch64) {
6269 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6270 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6271 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6272 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6273 outs() << format("\t.long\t0x%08x\n", opcode);
6274 Size = 4;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006275 } else {
6276 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6277 if (Size == 0)
6278 Size = 1; // skip illegible bytes
6279 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006280 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006281 }
6282 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006283 if (!symbolTableWorked) {
Rafael Espindola80291272014-10-08 15:28:58 +00006284 // Reading the symbol table didn't work, disassemble the whole section.
6285 uint64_t SectAddress = Sections[SectIdx].getAddress();
6286 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbybadd1002012-05-18 00:13:56 +00006287 uint64_t InstSize;
6288 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +00006289 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +00006290
Kevin Enderbybf246f52014-09-24 23:08:22 +00006291 uint64_t PC = SectAddress + Index;
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006292 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6293 DebugOut, nulls())) {
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006294 if (!NoLeadingAddr) {
6295 if (FullLeadingAddr) {
6296 if (MachOOF->is64Bit())
6297 outs() << format("%016" PRIx64, PC);
6298 else
6299 outs() << format("%08" PRIx64, PC);
6300 } else {
6301 outs() << format("%8" PRIx64 ":", PC);
6302 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006303 }
6304 if (!NoShowRawInsn) {
6305 outs() << "\t";
Craig Topper0013be12015-09-21 05:32:41 +00006306 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006307 }
Akira Hatanaka1d079942015-03-28 20:44:05 +00006308 IP->printInst(&Inst, outs(), "", *STI);
Bill Wendling4e68e062012-07-19 00:17:40 +00006309 outs() << "\n";
6310 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006311 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006312 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006313 outs() << format("\t.byte 0x%02x #bad opcode\n",
6314 *(Bytes.data() + Index) & 0xff);
6315 InstSize = 1; // skip exactly one illegible byte and move on.
6316 } else {
6317 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6318 if (InstSize == 0)
6319 InstSize = 1; // skip illegible bytes
6320 }
Bill Wendling4e68e062012-07-19 00:17:40 +00006321 }
Kevin Enderbybadd1002012-05-18 00:13:56 +00006322 }
6323 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006324 // The TripleName's need to be reset if we are called again for a different
6325 // archtecture.
6326 TripleName = "";
6327 ThumbTripleName = "";
6328
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006329 if (SymbolizerInfo.method != nullptr)
6330 free(SymbolizerInfo.method);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006331 if (SymbolizerInfo.demangled_name != nullptr)
6332 free(SymbolizerInfo.demangled_name);
Kevin Enderby078be602014-10-23 19:53:12 +00006333 if (SymbolizerInfo.bindtable != nullptr)
6334 delete SymbolizerInfo.bindtable;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006335 if (ThumbSymbolizerInfo.method != nullptr)
6336 free(ThumbSymbolizerInfo.method);
6337 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6338 free(ThumbSymbolizerInfo.demangled_name);
6339 if (ThumbSymbolizerInfo.bindtable != nullptr)
6340 delete ThumbSymbolizerInfo.bindtable;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006341 }
6342}
Tim Northover4bd286a2014-08-01 13:07:19 +00006343
Tim Northover39c70bb2014-08-12 11:52:59 +00006344//===----------------------------------------------------------------------===//
6345// __compact_unwind section dumping
6346//===----------------------------------------------------------------------===//
6347
Tim Northover4bd286a2014-08-01 13:07:19 +00006348namespace {
Tim Northover39c70bb2014-08-12 11:52:59 +00006349
6350template <typename T> static uint64_t readNext(const char *&Buf) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006351 using llvm::support::little;
6352 using llvm::support::unaligned;
Tim Northover39c70bb2014-08-12 11:52:59 +00006353
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006354 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6355 Buf += sizeof(T);
6356 return Val;
6357}
Tim Northover39c70bb2014-08-12 11:52:59 +00006358
Tim Northover4bd286a2014-08-01 13:07:19 +00006359struct CompactUnwindEntry {
6360 uint32_t OffsetInSection;
6361
6362 uint64_t FunctionAddr;
6363 uint32_t Length;
6364 uint32_t CompactEncoding;
6365 uint64_t PersonalityAddr;
6366 uint64_t LSDAAddr;
6367
6368 RelocationRef FunctionReloc;
6369 RelocationRef PersonalityReloc;
6370 RelocationRef LSDAReloc;
6371
6372 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006373 : OffsetInSection(Offset) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006374 if (Is64)
6375 read<uint64_t>(Contents.data() + Offset);
6376 else
6377 read<uint32_t>(Contents.data() + Offset);
6378 }
6379
6380private:
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006381 template <typename UIntPtr> void read(const char *Buf) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006382 FunctionAddr = readNext<UIntPtr>(Buf);
6383 Length = readNext<uint32_t>(Buf);
6384 CompactEncoding = readNext<uint32_t>(Buf);
6385 PersonalityAddr = readNext<UIntPtr>(Buf);
6386 LSDAAddr = readNext<UIntPtr>(Buf);
6387 }
6388};
6389}
6390
6391/// Given a relocation from __compact_unwind, consisting of the RelocationRef
6392/// and data being relocated, determine the best base Name and Addend to use for
6393/// display purposes.
6394///
6395/// 1. An Extern relocation will directly reference a symbol (and the data is
6396/// then already an addend), so use that.
6397/// 2. Otherwise the data is an offset in the object file's layout; try to find
6398// a symbol before it in the same section, and use the offset from there.
6399/// 3. Finally, if all that fails, fall back to an offset from the start of the
6400/// referenced section.
6401static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6402 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006403 const RelocationRef &Reloc, uint64_t Addr,
Tim Northover4bd286a2014-08-01 13:07:19 +00006404 StringRef &Name, uint64_t &Addend) {
6405 if (Reloc.getSymbol() != Obj->symbol_end()) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006406 ErrorOr<StringRef> NameOrErr = Reloc.getSymbol()->getName();
6407 if (std::error_code EC = NameOrErr.getError())
6408 report_fatal_error(EC.message());
6409 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00006410 Addend = Addr;
6411 return;
6412 }
6413
6414 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
Keno Fischerc780e8e2015-05-21 21:24:32 +00006415 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
Tim Northover4bd286a2014-08-01 13:07:19 +00006416
Rafael Espindola80291272014-10-08 15:28:58 +00006417 uint64_t SectionAddr = RelocSection.getAddress();
Tim Northover4bd286a2014-08-01 13:07:19 +00006418
6419 auto Sym = Symbols.upper_bound(Addr);
6420 if (Sym == Symbols.begin()) {
6421 // The first symbol in the object is after this reference, the best we can
6422 // do is section-relative notation.
6423 RelocSection.getName(Name);
6424 Addend = Addr - SectionAddr;
6425 return;
6426 }
6427
6428 // Go back one so that SymbolAddress <= Addr.
6429 --Sym;
6430
Rafael Espindola8bab8892015-08-07 23:27:14 +00006431 section_iterator SymSection = *Sym->second.getSection();
Tim Northover4bd286a2014-08-01 13:07:19 +00006432 if (RelocSection == *SymSection) {
6433 // There's a valid symbol in the same section before this reference.
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006434 ErrorOr<StringRef> NameOrErr = Sym->second.getName();
6435 if (std::error_code EC = NameOrErr.getError())
6436 report_fatal_error(EC.message());
6437 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00006438 Addend = Addr - Sym->first;
6439 return;
6440 }
6441
6442 // There is a symbol before this reference, but it's in a different
6443 // section. Probably not helpful to mention it, so use the section name.
6444 RelocSection.getName(Name);
6445 Addend = Addr - SectionAddr;
6446}
6447
6448static void printUnwindRelocDest(const MachOObjectFile *Obj,
6449 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006450 const RelocationRef &Reloc, uint64_t Addr) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006451 StringRef Name;
6452 uint64_t Addend;
6453
Rafael Espindola854038e2015-06-26 14:51:16 +00006454 if (!Reloc.getObject())
Tim Northover0b0add52014-09-09 10:45:06 +00006455 return;
6456
Tim Northover4bd286a2014-08-01 13:07:19 +00006457 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6458
6459 outs() << Name;
6460 if (Addend)
Tim Northover63a25622014-08-11 09:14:06 +00006461 outs() << " + " << format("0x%" PRIx64, Addend);
Tim Northover4bd286a2014-08-01 13:07:19 +00006462}
6463
6464static void
6465printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6466 std::map<uint64_t, SymbolRef> &Symbols,
6467 const SectionRef &CompactUnwind) {
6468
6469 assert(Obj->isLittleEndian() &&
6470 "There should not be a big-endian .o with __compact_unwind");
6471
6472 bool Is64 = Obj->is64Bit();
6473 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6474 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6475
6476 StringRef Contents;
6477 CompactUnwind.getContents(Contents);
6478
6479 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6480
6481 // First populate the initial raw offsets, encodings and so on from the entry.
6482 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6483 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
6484 CompactUnwinds.push_back(Entry);
6485 }
6486
6487 // Next we need to look at the relocations to find out what objects are
6488 // actually being referred to.
6489 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00006490 uint64_t RelocAddress = Reloc.getOffset();
Tim Northover4bd286a2014-08-01 13:07:19 +00006491
6492 uint32_t EntryIdx = RelocAddress / EntrySize;
6493 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
6494 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
6495
6496 if (OffsetInEntry == 0)
6497 Entry.FunctionReloc = Reloc;
6498 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
6499 Entry.PersonalityReloc = Reloc;
6500 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
6501 Entry.LSDAReloc = Reloc;
6502 else
6503 llvm_unreachable("Unexpected relocation in __compact_unwind section");
6504 }
6505
6506 // Finally, we're ready to print the data we've gathered.
6507 outs() << "Contents of __compact_unwind section:\n";
6508 for (auto &Entry : CompactUnwinds) {
Tim Northover06af2602014-08-08 12:08:51 +00006509 outs() << " Entry at offset "
6510 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00006511
6512 // 1. Start of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006513 outs() << " start: " << format("0x%" PRIx64,
6514 Entry.FunctionAddr) << ' ';
6515 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
Tim Northover4bd286a2014-08-01 13:07:19 +00006516 outs() << '\n';
6517
6518 // 2. Length of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006519 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
6520 << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00006521 // 3. The 32-bit compact encoding.
6522 outs() << " compact encoding: "
Tim Northoverb911bf82014-08-08 12:00:09 +00006523 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00006524
6525 // 4. The personality function, if present.
Rafael Espindola854038e2015-06-26 14:51:16 +00006526 if (Entry.PersonalityReloc.getObject()) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006527 outs() << " personality function: "
Tim Northoverb911bf82014-08-08 12:00:09 +00006528 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00006529 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
6530 Entry.PersonalityAddr);
6531 outs() << '\n';
6532 }
6533
6534 // 5. This entry's language-specific data area.
Rafael Espindola854038e2015-06-26 14:51:16 +00006535 if (Entry.LSDAReloc.getObject()) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006536 outs() << " LSDA: " << format("0x%" PRIx64,
6537 Entry.LSDAAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00006538 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
6539 outs() << '\n';
6540 }
6541 }
6542}
6543
Tim Northover39c70bb2014-08-12 11:52:59 +00006544//===----------------------------------------------------------------------===//
6545// __unwind_info section dumping
6546//===----------------------------------------------------------------------===//
6547
6548static void printRegularSecondLevelUnwindPage(const char *PageStart) {
6549 const char *Pos = PageStart;
6550 uint32_t Kind = readNext<uint32_t>(Pos);
6551 (void)Kind;
6552 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
6553
6554 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6555 uint16_t NumEntries = readNext<uint16_t>(Pos);
6556
6557 Pos = PageStart + EntriesStart;
6558 for (unsigned i = 0; i < NumEntries; ++i) {
6559 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6560 uint32_t Encoding = readNext<uint32_t>(Pos);
6561
6562 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006563 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6564 << ", "
6565 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006566 }
6567}
6568
6569static void printCompressedSecondLevelUnwindPage(
6570 const char *PageStart, uint32_t FunctionBase,
6571 const SmallVectorImpl<uint32_t> &CommonEncodings) {
6572 const char *Pos = PageStart;
6573 uint32_t Kind = readNext<uint32_t>(Pos);
6574 (void)Kind;
6575 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
6576
6577 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6578 uint16_t NumEntries = readNext<uint16_t>(Pos);
6579
6580 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
6581 readNext<uint16_t>(Pos);
Aaron Ballman80930af2014-08-14 13:53:19 +00006582 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
6583 PageStart + EncodingsStart);
Tim Northover39c70bb2014-08-12 11:52:59 +00006584
6585 Pos = PageStart + EntriesStart;
6586 for (unsigned i = 0; i < NumEntries; ++i) {
6587 uint32_t Entry = readNext<uint32_t>(Pos);
6588 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
6589 uint32_t EncodingIdx = Entry >> 24;
6590
6591 uint32_t Encoding;
6592 if (EncodingIdx < CommonEncodings.size())
6593 Encoding = CommonEncodings[EncodingIdx];
6594 else
6595 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
6596
6597 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006598 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6599 << ", "
6600 << "encoding[" << EncodingIdx
6601 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006602 }
6603}
6604
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006605static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
6606 std::map<uint64_t, SymbolRef> &Symbols,
6607 const SectionRef &UnwindInfo) {
Tim Northover39c70bb2014-08-12 11:52:59 +00006608
6609 assert(Obj->isLittleEndian() &&
6610 "There should not be a big-endian .o with __unwind_info");
6611
6612 outs() << "Contents of __unwind_info section:\n";
6613
6614 StringRef Contents;
6615 UnwindInfo.getContents(Contents);
6616 const char *Pos = Contents.data();
6617
6618 //===----------------------------------
6619 // Section header
6620 //===----------------------------------
6621
6622 uint32_t Version = readNext<uint32_t>(Pos);
6623 outs() << " Version: "
6624 << format("0x%" PRIx32, Version) << '\n';
6625 assert(Version == 1 && "only understand version 1");
6626
6627 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
6628 outs() << " Common encodings array section offset: "
6629 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
6630 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
6631 outs() << " Number of common encodings in array: "
6632 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
6633
6634 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
6635 outs() << " Personality function array section offset: "
6636 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
6637 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
6638 outs() << " Number of personality functions in array: "
6639 << format("0x%" PRIx32, NumPersonalities) << '\n';
6640
6641 uint32_t IndicesStart = readNext<uint32_t>(Pos);
6642 outs() << " Index array section offset: "
6643 << format("0x%" PRIx32, IndicesStart) << '\n';
6644 uint32_t NumIndices = readNext<uint32_t>(Pos);
6645 outs() << " Number of indices in array: "
6646 << format("0x%" PRIx32, NumIndices) << '\n';
6647
6648 //===----------------------------------
6649 // A shared list of common encodings
6650 //===----------------------------------
6651
6652 // These occupy indices in the range [0, N] whenever an encoding is referenced
6653 // from a compressed 2nd level index table. In practice the linker only
6654 // creates ~128 of these, so that indices are available to embed encodings in
6655 // the 2nd level index.
6656
6657 SmallVector<uint32_t, 64> CommonEncodings;
6658 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
6659 Pos = Contents.data() + CommonEncodingsStart;
6660 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
6661 uint32_t Encoding = readNext<uint32_t>(Pos);
6662 CommonEncodings.push_back(Encoding);
6663
6664 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
6665 << '\n';
6666 }
6667
Tim Northover39c70bb2014-08-12 11:52:59 +00006668 //===----------------------------------
6669 // Personality functions used in this executable
6670 //===----------------------------------
6671
6672 // There should be only a handful of these (one per source language,
6673 // roughly). Particularly since they only get 2 bits in the compact encoding.
6674
6675 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
6676 Pos = Contents.data() + PersonalitiesStart;
6677 for (unsigned i = 0; i < NumPersonalities; ++i) {
6678 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
6679 outs() << " personality[" << i + 1
6680 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
6681 }
6682
6683 //===----------------------------------
6684 // The level 1 index entries
6685 //===----------------------------------
6686
6687 // These specify an approximate place to start searching for the more detailed
6688 // information, sorted by PC.
6689
6690 struct IndexEntry {
6691 uint32_t FunctionOffset;
6692 uint32_t SecondLevelPageStart;
6693 uint32_t LSDAStart;
6694 };
6695
6696 SmallVector<IndexEntry, 4> IndexEntries;
6697
6698 outs() << " Top level indices: (count = " << NumIndices << ")\n";
6699 Pos = Contents.data() + IndicesStart;
6700 for (unsigned i = 0; i < NumIndices; ++i) {
6701 IndexEntry Entry;
6702
6703 Entry.FunctionOffset = readNext<uint32_t>(Pos);
6704 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
6705 Entry.LSDAStart = readNext<uint32_t>(Pos);
6706 IndexEntries.push_back(Entry);
6707
6708 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006709 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
6710 << ", "
Tim Northover39c70bb2014-08-12 11:52:59 +00006711 << "2nd level page offset="
6712 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006713 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006714 }
6715
Tim Northover39c70bb2014-08-12 11:52:59 +00006716 //===----------------------------------
6717 // Next come the LSDA tables
6718 //===----------------------------------
6719
6720 // The LSDA layout is rather implicit: it's a contiguous array of entries from
6721 // the first top-level index's LSDAOffset to the last (sentinel).
6722
6723 outs() << " LSDA descriptors:\n";
6724 Pos = Contents.data() + IndexEntries[0].LSDAStart;
6725 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
6726 (2 * sizeof(uint32_t));
6727 for (int i = 0; i < NumLSDAs; ++i) {
6728 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6729 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
6730 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006731 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6732 << ", "
6733 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00006734 }
6735
6736 //===----------------------------------
6737 // Finally, the 2nd level indices
6738 //===----------------------------------
6739
6740 // Generally these are 4K in size, and have 2 possible forms:
6741 // + Regular stores up to 511 entries with disparate encodings
6742 // + Compressed stores up to 1021 entries if few enough compact encoding
6743 // values are used.
6744 outs() << " Second level indices:\n";
6745 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
6746 // The final sentinel top-level index has no associated 2nd level page
6747 if (IndexEntries[i].SecondLevelPageStart == 0)
6748 break;
6749
6750 outs() << " Second level index[" << i << "]: "
6751 << "offset in section="
6752 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
6753 << ", "
6754 << "base function offset="
6755 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
6756
6757 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
Aaron Ballman80930af2014-08-14 13:53:19 +00006758 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00006759 if (Kind == 2)
6760 printRegularSecondLevelUnwindPage(Pos);
6761 else if (Kind == 3)
6762 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
6763 CommonEncodings);
6764 else
6765 llvm_unreachable("Do not know how to print this kind of 2nd level page");
Tim Northover39c70bb2014-08-12 11:52:59 +00006766 }
6767}
6768
Tim Northover4bd286a2014-08-01 13:07:19 +00006769void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
6770 std::map<uint64_t, SymbolRef> Symbols;
6771 for (const SymbolRef &SymRef : Obj->symbols()) {
6772 // Discard any undefined or absolute symbols. They're not going to take part
6773 // in the convenience lookup for unwind info and just take up resources.
Rafael Espindola8bab8892015-08-07 23:27:14 +00006774 section_iterator Section = *SymRef.getSection();
Tim Northover4bd286a2014-08-01 13:07:19 +00006775 if (Section == Obj->section_end())
6776 continue;
6777
Rafael Espindoladea00162015-07-03 17:44:18 +00006778 uint64_t Addr = SymRef.getValue();
Tim Northover4bd286a2014-08-01 13:07:19 +00006779 Symbols.insert(std::make_pair(Addr, SymRef));
6780 }
6781
6782 for (const SectionRef &Section : Obj->sections()) {
6783 StringRef SectName;
6784 Section.getName(SectName);
6785 if (SectName == "__compact_unwind")
6786 printMachOCompactUnwindSection(Obj, Symbols, Section);
6787 else if (SectName == "__unwind_info")
Tim Northover39c70bb2014-08-12 11:52:59 +00006788 printMachOUnwindInfoSection(Obj, Symbols, Section);
Tim Northover4bd286a2014-08-01 13:07:19 +00006789 else if (SectName == "__eh_frame")
6790 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00006791 }
6792}
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006793
6794static void PrintMachHeader(uint32_t magic, uint32_t cputype,
6795 uint32_t cpusubtype, uint32_t filetype,
6796 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
6797 bool verbose) {
6798 outs() << "Mach header\n";
6799 outs() << " magic cputype cpusubtype caps filetype ncmds "
6800 "sizeofcmds flags\n";
6801 if (verbose) {
6802 if (magic == MachO::MH_MAGIC)
6803 outs() << " MH_MAGIC";
6804 else if (magic == MachO::MH_MAGIC_64)
6805 outs() << "MH_MAGIC_64";
6806 else
6807 outs() << format(" 0x%08" PRIx32, magic);
6808 switch (cputype) {
6809 case MachO::CPU_TYPE_I386:
6810 outs() << " I386";
6811 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6812 case MachO::CPU_SUBTYPE_I386_ALL:
6813 outs() << " ALL";
6814 break;
6815 default:
6816 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6817 break;
6818 }
6819 break;
6820 case MachO::CPU_TYPE_X86_64:
6821 outs() << " X86_64";
Kevin Enderby131d1772015-01-09 19:22:37 +00006822 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6823 case MachO::CPU_SUBTYPE_X86_64_ALL:
6824 outs() << " ALL";
6825 break;
6826 case MachO::CPU_SUBTYPE_X86_64_H:
6827 outs() << " Haswell";
6828 break;
6829 default:
6830 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6831 break;
6832 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006833 break;
6834 case MachO::CPU_TYPE_ARM:
6835 outs() << " ARM";
6836 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6837 case MachO::CPU_SUBTYPE_ARM_ALL:
6838 outs() << " ALL";
6839 break;
6840 case MachO::CPU_SUBTYPE_ARM_V4T:
6841 outs() << " V4T";
6842 break;
6843 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6844 outs() << " V5TEJ";
6845 break;
6846 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6847 outs() << " XSCALE";
6848 break;
6849 case MachO::CPU_SUBTYPE_ARM_V6:
6850 outs() << " V6";
6851 break;
6852 case MachO::CPU_SUBTYPE_ARM_V6M:
6853 outs() << " V6M";
6854 break;
6855 case MachO::CPU_SUBTYPE_ARM_V7:
6856 outs() << " V7";
6857 break;
6858 case MachO::CPU_SUBTYPE_ARM_V7EM:
6859 outs() << " V7EM";
6860 break;
6861 case MachO::CPU_SUBTYPE_ARM_V7K:
6862 outs() << " V7K";
6863 break;
6864 case MachO::CPU_SUBTYPE_ARM_V7M:
6865 outs() << " V7M";
6866 break;
6867 case MachO::CPU_SUBTYPE_ARM_V7S:
6868 outs() << " V7S";
6869 break;
6870 default:
6871 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6872 break;
6873 }
6874 break;
6875 case MachO::CPU_TYPE_ARM64:
6876 outs() << " ARM64";
6877 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6878 case MachO::CPU_SUBTYPE_ARM64_ALL:
6879 outs() << " ALL";
6880 break;
6881 default:
6882 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6883 break;
6884 }
6885 break;
6886 case MachO::CPU_TYPE_POWERPC:
6887 outs() << " PPC";
6888 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6889 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6890 outs() << " ALL";
6891 break;
6892 default:
6893 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6894 break;
6895 }
6896 break;
6897 case MachO::CPU_TYPE_POWERPC64:
6898 outs() << " PPC64";
6899 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6900 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6901 outs() << " ALL";
6902 break;
6903 default:
6904 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6905 break;
6906 }
6907 break;
6908 }
6909 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00006910 outs() << " LIB64";
Kevin Enderbyb76d3862014-08-22 20:35:18 +00006911 } else {
6912 outs() << format(" 0x%02" PRIx32,
6913 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6914 }
6915 switch (filetype) {
6916 case MachO::MH_OBJECT:
6917 outs() << " OBJECT";
6918 break;
6919 case MachO::MH_EXECUTE:
6920 outs() << " EXECUTE";
6921 break;
6922 case MachO::MH_FVMLIB:
6923 outs() << " FVMLIB";
6924 break;
6925 case MachO::MH_CORE:
6926 outs() << " CORE";
6927 break;
6928 case MachO::MH_PRELOAD:
6929 outs() << " PRELOAD";
6930 break;
6931 case MachO::MH_DYLIB:
6932 outs() << " DYLIB";
6933 break;
6934 case MachO::MH_DYLIB_STUB:
6935 outs() << " DYLIB_STUB";
6936 break;
6937 case MachO::MH_DYLINKER:
6938 outs() << " DYLINKER";
6939 break;
6940 case MachO::MH_BUNDLE:
6941 outs() << " BUNDLE";
6942 break;
6943 case MachO::MH_DSYM:
6944 outs() << " DSYM";
6945 break;
6946 case MachO::MH_KEXT_BUNDLE:
6947 outs() << " KEXTBUNDLE";
6948 break;
6949 default:
6950 outs() << format(" %10u", filetype);
6951 break;
6952 }
6953 outs() << format(" %5u", ncmds);
6954 outs() << format(" %10u", sizeofcmds);
6955 uint32_t f = flags;
6956 if (f & MachO::MH_NOUNDEFS) {
6957 outs() << " NOUNDEFS";
6958 f &= ~MachO::MH_NOUNDEFS;
6959 }
6960 if (f & MachO::MH_INCRLINK) {
6961 outs() << " INCRLINK";
6962 f &= ~MachO::MH_INCRLINK;
6963 }
6964 if (f & MachO::MH_DYLDLINK) {
6965 outs() << " DYLDLINK";
6966 f &= ~MachO::MH_DYLDLINK;
6967 }
6968 if (f & MachO::MH_BINDATLOAD) {
6969 outs() << " BINDATLOAD";
6970 f &= ~MachO::MH_BINDATLOAD;
6971 }
6972 if (f & MachO::MH_PREBOUND) {
6973 outs() << " PREBOUND";
6974 f &= ~MachO::MH_PREBOUND;
6975 }
6976 if (f & MachO::MH_SPLIT_SEGS) {
6977 outs() << " SPLIT_SEGS";
6978 f &= ~MachO::MH_SPLIT_SEGS;
6979 }
6980 if (f & MachO::MH_LAZY_INIT) {
6981 outs() << " LAZY_INIT";
6982 f &= ~MachO::MH_LAZY_INIT;
6983 }
6984 if (f & MachO::MH_TWOLEVEL) {
6985 outs() << " TWOLEVEL";
6986 f &= ~MachO::MH_TWOLEVEL;
6987 }
6988 if (f & MachO::MH_FORCE_FLAT) {
6989 outs() << " FORCE_FLAT";
6990 f &= ~MachO::MH_FORCE_FLAT;
6991 }
6992 if (f & MachO::MH_NOMULTIDEFS) {
6993 outs() << " NOMULTIDEFS";
6994 f &= ~MachO::MH_NOMULTIDEFS;
6995 }
6996 if (f & MachO::MH_NOFIXPREBINDING) {
6997 outs() << " NOFIXPREBINDING";
6998 f &= ~MachO::MH_NOFIXPREBINDING;
6999 }
7000 if (f & MachO::MH_PREBINDABLE) {
7001 outs() << " PREBINDABLE";
7002 f &= ~MachO::MH_PREBINDABLE;
7003 }
7004 if (f & MachO::MH_ALLMODSBOUND) {
7005 outs() << " ALLMODSBOUND";
7006 f &= ~MachO::MH_ALLMODSBOUND;
7007 }
7008 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7009 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7010 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7011 }
7012 if (f & MachO::MH_CANONICAL) {
7013 outs() << " CANONICAL";
7014 f &= ~MachO::MH_CANONICAL;
7015 }
7016 if (f & MachO::MH_WEAK_DEFINES) {
7017 outs() << " WEAK_DEFINES";
7018 f &= ~MachO::MH_WEAK_DEFINES;
7019 }
7020 if (f & MachO::MH_BINDS_TO_WEAK) {
7021 outs() << " BINDS_TO_WEAK";
7022 f &= ~MachO::MH_BINDS_TO_WEAK;
7023 }
7024 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7025 outs() << " ALLOW_STACK_EXECUTION";
7026 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7027 }
7028 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7029 outs() << " DEAD_STRIPPABLE_DYLIB";
7030 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7031 }
7032 if (f & MachO::MH_PIE) {
7033 outs() << " PIE";
7034 f &= ~MachO::MH_PIE;
7035 }
7036 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7037 outs() << " NO_REEXPORTED_DYLIBS";
7038 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7039 }
7040 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7041 outs() << " MH_HAS_TLV_DESCRIPTORS";
7042 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7043 }
7044 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7045 outs() << " MH_NO_HEAP_EXECUTION";
7046 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7047 }
7048 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7049 outs() << " APP_EXTENSION_SAFE";
7050 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7051 }
7052 if (f != 0 || flags == 0)
7053 outs() << format(" 0x%08" PRIx32, f);
7054 } else {
7055 outs() << format(" 0x%08" PRIx32, magic);
7056 outs() << format(" %7d", cputype);
7057 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7058 outs() << format(" 0x%02" PRIx32,
7059 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7060 outs() << format(" %10u", filetype);
7061 outs() << format(" %5u", ncmds);
7062 outs() << format(" %10u", sizeofcmds);
7063 outs() << format(" 0x%08" PRIx32, flags);
7064 }
7065 outs() << "\n";
7066}
7067
Kevin Enderby956366c2014-08-29 22:30:52 +00007068static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7069 StringRef SegName, uint64_t vmaddr,
7070 uint64_t vmsize, uint64_t fileoff,
7071 uint64_t filesize, uint32_t maxprot,
7072 uint32_t initprot, uint32_t nsects,
7073 uint32_t flags, uint32_t object_size,
7074 bool verbose) {
7075 uint64_t expected_cmdsize;
7076 if (cmd == MachO::LC_SEGMENT) {
7077 outs() << " cmd LC_SEGMENT\n";
7078 expected_cmdsize = nsects;
7079 expected_cmdsize *= sizeof(struct MachO::section);
7080 expected_cmdsize += sizeof(struct MachO::segment_command);
7081 } else {
7082 outs() << " cmd LC_SEGMENT_64\n";
7083 expected_cmdsize = nsects;
7084 expected_cmdsize *= sizeof(struct MachO::section_64);
7085 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7086 }
7087 outs() << " cmdsize " << cmdsize;
7088 if (cmdsize != expected_cmdsize)
7089 outs() << " Inconsistent size\n";
7090 else
7091 outs() << "\n";
7092 outs() << " segname " << SegName << "\n";
7093 if (cmd == MachO::LC_SEGMENT_64) {
7094 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7095 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7096 } else {
Kevin Enderbyadb7c432014-12-16 18:58:11 +00007097 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7098 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007099 }
7100 outs() << " fileoff " << fileoff;
7101 if (fileoff > object_size)
7102 outs() << " (past end of file)\n";
7103 else
7104 outs() << "\n";
7105 outs() << " filesize " << filesize;
7106 if (fileoff + filesize > object_size)
7107 outs() << " (past end of file)\n";
7108 else
7109 outs() << "\n";
7110 if (verbose) {
7111 if ((maxprot &
7112 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7113 MachO::VM_PROT_EXECUTE)) != 0)
7114 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7115 else {
Davide Italiano37ff06a2015-09-02 16:53:25 +00007116 outs() << " maxprot ";
7117 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
7118 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7119 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00007120 }
7121 if ((initprot &
7122 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7123 MachO::VM_PROT_EXECUTE)) != 0)
7124 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7125 else {
Davide Italiano37ff06a2015-09-02 16:53:25 +00007126 outs() << " initprot ";
7127 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
7128 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7129 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00007130 }
7131 } else {
7132 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7133 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7134 }
7135 outs() << " nsects " << nsects << "\n";
7136 if (verbose) {
7137 outs() << " flags";
7138 if (flags == 0)
7139 outs() << " (none)\n";
7140 else {
7141 if (flags & MachO::SG_HIGHVM) {
7142 outs() << " HIGHVM";
7143 flags &= ~MachO::SG_HIGHVM;
7144 }
7145 if (flags & MachO::SG_FVMLIB) {
7146 outs() << " FVMLIB";
7147 flags &= ~MachO::SG_FVMLIB;
7148 }
7149 if (flags & MachO::SG_NORELOC) {
7150 outs() << " NORELOC";
7151 flags &= ~MachO::SG_NORELOC;
7152 }
7153 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7154 outs() << " PROTECTED_VERSION_1";
7155 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7156 }
7157 if (flags)
7158 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7159 else
7160 outs() << "\n";
7161 }
7162 } else {
7163 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7164 }
7165}
7166
7167static void PrintSection(const char *sectname, const char *segname,
7168 uint64_t addr, uint64_t size, uint32_t offset,
7169 uint32_t align, uint32_t reloff, uint32_t nreloc,
7170 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7171 uint32_t cmd, const char *sg_segname,
7172 uint32_t filetype, uint32_t object_size,
7173 bool verbose) {
7174 outs() << "Section\n";
7175 outs() << " sectname " << format("%.16s\n", sectname);
7176 outs() << " segname " << format("%.16s", segname);
7177 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7178 outs() << " (does not match segment)\n";
7179 else
7180 outs() << "\n";
7181 if (cmd == MachO::LC_SEGMENT_64) {
7182 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7183 outs() << " size " << format("0x%016" PRIx64, size);
7184 } else {
Kevin Enderby75594b62014-12-16 21:00:25 +00007185 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7186 outs() << " size " << format("0x%08" PRIx64, size);
Kevin Enderby956366c2014-08-29 22:30:52 +00007187 }
7188 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7189 outs() << " (past end of file)\n";
7190 else
7191 outs() << "\n";
7192 outs() << " offset " << offset;
7193 if (offset > object_size)
7194 outs() << " (past end of file)\n";
7195 else
7196 outs() << "\n";
7197 uint32_t align_shifted = 1 << align;
7198 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7199 outs() << " reloff " << reloff;
7200 if (reloff > object_size)
7201 outs() << " (past end of file)\n";
7202 else
7203 outs() << "\n";
7204 outs() << " nreloc " << nreloc;
7205 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7206 outs() << " (past end of file)\n";
7207 else
7208 outs() << "\n";
7209 uint32_t section_type = flags & MachO::SECTION_TYPE;
7210 if (verbose) {
7211 outs() << " type";
7212 if (section_type == MachO::S_REGULAR)
7213 outs() << " S_REGULAR\n";
7214 else if (section_type == MachO::S_ZEROFILL)
7215 outs() << " S_ZEROFILL\n";
7216 else if (section_type == MachO::S_CSTRING_LITERALS)
7217 outs() << " S_CSTRING_LITERALS\n";
7218 else if (section_type == MachO::S_4BYTE_LITERALS)
7219 outs() << " S_4BYTE_LITERALS\n";
7220 else if (section_type == MachO::S_8BYTE_LITERALS)
7221 outs() << " S_8BYTE_LITERALS\n";
7222 else if (section_type == MachO::S_16BYTE_LITERALS)
7223 outs() << " S_16BYTE_LITERALS\n";
7224 else if (section_type == MachO::S_LITERAL_POINTERS)
7225 outs() << " S_LITERAL_POINTERS\n";
7226 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7227 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7228 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7229 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7230 else if (section_type == MachO::S_SYMBOL_STUBS)
7231 outs() << " S_SYMBOL_STUBS\n";
7232 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7233 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7234 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7235 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7236 else if (section_type == MachO::S_COALESCED)
7237 outs() << " S_COALESCED\n";
7238 else if (section_type == MachO::S_INTERPOSING)
7239 outs() << " S_INTERPOSING\n";
7240 else if (section_type == MachO::S_DTRACE_DOF)
7241 outs() << " S_DTRACE_DOF\n";
7242 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7243 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7244 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7245 outs() << " S_THREAD_LOCAL_REGULAR\n";
7246 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7247 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7248 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7249 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7250 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7251 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7252 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7253 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7254 else
7255 outs() << format("0x%08" PRIx32, section_type) << "\n";
7256 outs() << "attributes";
7257 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7258 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7259 outs() << " PURE_INSTRUCTIONS";
7260 if (section_attributes & MachO::S_ATTR_NO_TOC)
7261 outs() << " NO_TOC";
7262 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7263 outs() << " STRIP_STATIC_SYMS";
7264 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7265 outs() << " NO_DEAD_STRIP";
7266 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7267 outs() << " LIVE_SUPPORT";
7268 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7269 outs() << " SELF_MODIFYING_CODE";
7270 if (section_attributes & MachO::S_ATTR_DEBUG)
7271 outs() << " DEBUG";
7272 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7273 outs() << " SOME_INSTRUCTIONS";
7274 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7275 outs() << " EXT_RELOC";
7276 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7277 outs() << " LOC_RELOC";
7278 if (section_attributes == 0)
7279 outs() << " (none)";
7280 outs() << "\n";
7281 } else
7282 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7283 outs() << " reserved1 " << reserved1;
7284 if (section_type == MachO::S_SYMBOL_STUBS ||
7285 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7286 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7287 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7288 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7289 outs() << " (index into indirect symbol table)\n";
7290 else
7291 outs() << "\n";
7292 outs() << " reserved2 " << reserved2;
7293 if (section_type == MachO::S_SYMBOL_STUBS)
7294 outs() << " (size of stubs)\n";
7295 else
7296 outs() << "\n";
7297}
7298
David Majnemer73cc6ff2014-11-13 19:48:56 +00007299static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
Kevin Enderby956366c2014-08-29 22:30:52 +00007300 uint32_t object_size) {
7301 outs() << " cmd LC_SYMTAB\n";
7302 outs() << " cmdsize " << st.cmdsize;
7303 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7304 outs() << " Incorrect size\n";
7305 else
7306 outs() << "\n";
7307 outs() << " symoff " << st.symoff;
7308 if (st.symoff > object_size)
7309 outs() << " (past end of file)\n";
7310 else
7311 outs() << "\n";
7312 outs() << " nsyms " << st.nsyms;
7313 uint64_t big_size;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007314 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007315 big_size = st.nsyms;
7316 big_size *= sizeof(struct MachO::nlist_64);
7317 big_size += st.symoff;
7318 if (big_size > object_size)
7319 outs() << " (past end of file)\n";
7320 else
7321 outs() << "\n";
7322 } else {
7323 big_size = st.nsyms;
7324 big_size *= sizeof(struct MachO::nlist);
7325 big_size += st.symoff;
7326 if (big_size > object_size)
7327 outs() << " (past end of file)\n";
7328 else
7329 outs() << "\n";
7330 }
7331 outs() << " stroff " << st.stroff;
7332 if (st.stroff > object_size)
7333 outs() << " (past end of file)\n";
7334 else
7335 outs() << "\n";
7336 outs() << " strsize " << st.strsize;
7337 big_size = st.stroff;
7338 big_size += st.strsize;
7339 if (big_size > object_size)
7340 outs() << " (past end of file)\n";
7341 else
7342 outs() << "\n";
7343}
7344
7345static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7346 uint32_t nsyms, uint32_t object_size,
David Majnemer73cc6ff2014-11-13 19:48:56 +00007347 bool Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007348 outs() << " cmd LC_DYSYMTAB\n";
7349 outs() << " cmdsize " << dyst.cmdsize;
7350 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7351 outs() << " Incorrect size\n";
7352 else
7353 outs() << "\n";
7354 outs() << " ilocalsym " << dyst.ilocalsym;
7355 if (dyst.ilocalsym > nsyms)
7356 outs() << " (greater than the number of symbols)\n";
7357 else
7358 outs() << "\n";
7359 outs() << " nlocalsym " << dyst.nlocalsym;
7360 uint64_t big_size;
7361 big_size = dyst.ilocalsym;
7362 big_size += dyst.nlocalsym;
7363 if (big_size > nsyms)
7364 outs() << " (past the end of the symbol table)\n";
7365 else
7366 outs() << "\n";
7367 outs() << " iextdefsym " << dyst.iextdefsym;
7368 if (dyst.iextdefsym > nsyms)
7369 outs() << " (greater than the number of symbols)\n";
7370 else
7371 outs() << "\n";
7372 outs() << " nextdefsym " << dyst.nextdefsym;
7373 big_size = dyst.iextdefsym;
7374 big_size += dyst.nextdefsym;
7375 if (big_size > nsyms)
7376 outs() << " (past the end of the symbol table)\n";
7377 else
7378 outs() << "\n";
7379 outs() << " iundefsym " << dyst.iundefsym;
7380 if (dyst.iundefsym > nsyms)
7381 outs() << " (greater than the number of symbols)\n";
7382 else
7383 outs() << "\n";
7384 outs() << " nundefsym " << dyst.nundefsym;
7385 big_size = dyst.iundefsym;
7386 big_size += dyst.nundefsym;
7387 if (big_size > nsyms)
7388 outs() << " (past the end of the symbol table)\n";
7389 else
7390 outs() << "\n";
7391 outs() << " tocoff " << dyst.tocoff;
7392 if (dyst.tocoff > object_size)
7393 outs() << " (past end of file)\n";
7394 else
7395 outs() << "\n";
7396 outs() << " ntoc " << dyst.ntoc;
7397 big_size = dyst.ntoc;
7398 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7399 big_size += dyst.tocoff;
7400 if (big_size > object_size)
7401 outs() << " (past end of file)\n";
7402 else
7403 outs() << "\n";
7404 outs() << " modtaboff " << dyst.modtaboff;
7405 if (dyst.modtaboff > object_size)
7406 outs() << " (past end of file)\n";
7407 else
7408 outs() << "\n";
7409 outs() << " nmodtab " << dyst.nmodtab;
7410 uint64_t modtabend;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007411 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007412 modtabend = dyst.nmodtab;
7413 modtabend *= sizeof(struct MachO::dylib_module_64);
7414 modtabend += dyst.modtaboff;
7415 } else {
7416 modtabend = dyst.nmodtab;
7417 modtabend *= sizeof(struct MachO::dylib_module);
7418 modtabend += dyst.modtaboff;
7419 }
7420 if (modtabend > object_size)
7421 outs() << " (past end of file)\n";
7422 else
7423 outs() << "\n";
7424 outs() << " extrefsymoff " << dyst.extrefsymoff;
7425 if (dyst.extrefsymoff > object_size)
7426 outs() << " (past end of file)\n";
7427 else
7428 outs() << "\n";
7429 outs() << " nextrefsyms " << dyst.nextrefsyms;
7430 big_size = dyst.nextrefsyms;
7431 big_size *= sizeof(struct MachO::dylib_reference);
7432 big_size += dyst.extrefsymoff;
7433 if (big_size > object_size)
7434 outs() << " (past end of file)\n";
7435 else
7436 outs() << "\n";
7437 outs() << " indirectsymoff " << dyst.indirectsymoff;
7438 if (dyst.indirectsymoff > object_size)
7439 outs() << " (past end of file)\n";
7440 else
7441 outs() << "\n";
7442 outs() << " nindirectsyms " << dyst.nindirectsyms;
7443 big_size = dyst.nindirectsyms;
7444 big_size *= sizeof(uint32_t);
7445 big_size += dyst.indirectsymoff;
7446 if (big_size > object_size)
7447 outs() << " (past end of file)\n";
7448 else
7449 outs() << "\n";
7450 outs() << " extreloff " << dyst.extreloff;
7451 if (dyst.extreloff > object_size)
7452 outs() << " (past end of file)\n";
7453 else
7454 outs() << "\n";
7455 outs() << " nextrel " << dyst.nextrel;
7456 big_size = dyst.nextrel;
7457 big_size *= sizeof(struct MachO::relocation_info);
7458 big_size += dyst.extreloff;
7459 if (big_size > object_size)
7460 outs() << " (past end of file)\n";
7461 else
7462 outs() << "\n";
7463 outs() << " locreloff " << dyst.locreloff;
7464 if (dyst.locreloff > object_size)
7465 outs() << " (past end of file)\n";
7466 else
7467 outs() << "\n";
7468 outs() << " nlocrel " << dyst.nlocrel;
7469 big_size = dyst.nlocrel;
7470 big_size *= sizeof(struct MachO::relocation_info);
7471 big_size += dyst.locreloff;
7472 if (big_size > object_size)
7473 outs() << " (past end of file)\n";
7474 else
7475 outs() << "\n";
7476}
7477
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007478static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
7479 uint32_t object_size) {
7480 if (dc.cmd == MachO::LC_DYLD_INFO)
7481 outs() << " cmd LC_DYLD_INFO\n";
7482 else
7483 outs() << " cmd LC_DYLD_INFO_ONLY\n";
7484 outs() << " cmdsize " << dc.cmdsize;
7485 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
7486 outs() << " Incorrect size\n";
7487 else
7488 outs() << "\n";
7489 outs() << " rebase_off " << dc.rebase_off;
7490 if (dc.rebase_off > object_size)
7491 outs() << " (past end of file)\n";
7492 else
7493 outs() << "\n";
7494 outs() << " rebase_size " << dc.rebase_size;
7495 uint64_t big_size;
7496 big_size = dc.rebase_off;
7497 big_size += dc.rebase_size;
7498 if (big_size > object_size)
7499 outs() << " (past end of file)\n";
7500 else
7501 outs() << "\n";
7502 outs() << " bind_off " << dc.bind_off;
7503 if (dc.bind_off > object_size)
7504 outs() << " (past end of file)\n";
7505 else
7506 outs() << "\n";
7507 outs() << " bind_size " << dc.bind_size;
7508 big_size = dc.bind_off;
7509 big_size += dc.bind_size;
7510 if (big_size > object_size)
7511 outs() << " (past end of file)\n";
7512 else
7513 outs() << "\n";
7514 outs() << " weak_bind_off " << dc.weak_bind_off;
7515 if (dc.weak_bind_off > object_size)
7516 outs() << " (past end of file)\n";
7517 else
7518 outs() << "\n";
7519 outs() << " weak_bind_size " << dc.weak_bind_size;
7520 big_size = dc.weak_bind_off;
7521 big_size += dc.weak_bind_size;
7522 if (big_size > object_size)
7523 outs() << " (past end of file)\n";
7524 else
7525 outs() << "\n";
7526 outs() << " lazy_bind_off " << dc.lazy_bind_off;
7527 if (dc.lazy_bind_off > object_size)
7528 outs() << " (past end of file)\n";
7529 else
7530 outs() << "\n";
7531 outs() << " lazy_bind_size " << dc.lazy_bind_size;
7532 big_size = dc.lazy_bind_off;
7533 big_size += dc.lazy_bind_size;
7534 if (big_size > object_size)
7535 outs() << " (past end of file)\n";
7536 else
7537 outs() << "\n";
7538 outs() << " export_off " << dc.export_off;
7539 if (dc.export_off > object_size)
7540 outs() << " (past end of file)\n";
7541 else
7542 outs() << "\n";
7543 outs() << " export_size " << dc.export_size;
7544 big_size = dc.export_off;
7545 big_size += dc.export_size;
7546 if (big_size > object_size)
7547 outs() << " (past end of file)\n";
7548 else
7549 outs() << "\n";
7550}
7551
7552static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
7553 const char *Ptr) {
7554 if (dyld.cmd == MachO::LC_ID_DYLINKER)
7555 outs() << " cmd LC_ID_DYLINKER\n";
7556 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
7557 outs() << " cmd LC_LOAD_DYLINKER\n";
7558 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
7559 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
7560 else
7561 outs() << " cmd ?(" << dyld.cmd << ")\n";
7562 outs() << " cmdsize " << dyld.cmdsize;
7563 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
7564 outs() << " Incorrect size\n";
7565 else
7566 outs() << "\n";
7567 if (dyld.name >= dyld.cmdsize)
7568 outs() << " name ?(bad offset " << dyld.name << ")\n";
7569 else {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007570 const char *P = (const char *)(Ptr) + dyld.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007571 outs() << " name " << P << " (offset " << dyld.name << ")\n";
7572 }
7573}
7574
7575static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
7576 outs() << " cmd LC_UUID\n";
7577 outs() << " cmdsize " << uuid.cmdsize;
7578 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
7579 outs() << " Incorrect size\n";
7580 else
7581 outs() << "\n";
7582 outs() << " uuid ";
7583 outs() << format("%02" PRIX32, uuid.uuid[0]);
7584 outs() << format("%02" PRIX32, uuid.uuid[1]);
7585 outs() << format("%02" PRIX32, uuid.uuid[2]);
7586 outs() << format("%02" PRIX32, uuid.uuid[3]);
7587 outs() << "-";
7588 outs() << format("%02" PRIX32, uuid.uuid[4]);
7589 outs() << format("%02" PRIX32, uuid.uuid[5]);
7590 outs() << "-";
7591 outs() << format("%02" PRIX32, uuid.uuid[6]);
7592 outs() << format("%02" PRIX32, uuid.uuid[7]);
7593 outs() << "-";
7594 outs() << format("%02" PRIX32, uuid.uuid[8]);
7595 outs() << format("%02" PRIX32, uuid.uuid[9]);
7596 outs() << "-";
7597 outs() << format("%02" PRIX32, uuid.uuid[10]);
7598 outs() << format("%02" PRIX32, uuid.uuid[11]);
7599 outs() << format("%02" PRIX32, uuid.uuid[12]);
7600 outs() << format("%02" PRIX32, uuid.uuid[13]);
7601 outs() << format("%02" PRIX32, uuid.uuid[14]);
7602 outs() << format("%02" PRIX32, uuid.uuid[15]);
7603 outs() << "\n";
7604}
7605
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007606static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00007607 outs() << " cmd LC_RPATH\n";
7608 outs() << " cmdsize " << rpath.cmdsize;
7609 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
7610 outs() << " Incorrect size\n";
7611 else
7612 outs() << "\n";
7613 if (rpath.path >= rpath.cmdsize)
7614 outs() << " path ?(bad offset " << rpath.path << ")\n";
7615 else {
7616 const char *P = (const char *)(Ptr) + rpath.path;
7617 outs() << " path " << P << " (offset " << rpath.path << ")\n";
7618 }
7619}
7620
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007621static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
Tim Northoverbfbfb122015-11-02 21:26:58 +00007622 StringRef LoadCmdName;
7623 switch (vd.cmd) {
7624 case MachO::LC_VERSION_MIN_MACOSX:
7625 LoadCmdName = "LC_VERSION_MIN_MACOSX";
7626 break;
7627 case MachO::LC_VERSION_MIN_IPHONEOS:
7628 LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
7629 break;
7630 case MachO::LC_VERSION_MIN_TVOS:
7631 LoadCmdName = "LC_VERSION_MIN_TVOS";
7632 break;
7633 case MachO::LC_VERSION_MIN_WATCHOS:
7634 LoadCmdName = "LC_VERSION_MIN_WATCHOS";
7635 break;
7636 default:
7637 llvm_unreachable("Unknown version min load command");
7638 }
7639
7640 outs() << " cmd " << LoadCmdName << '\n';
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007641 outs() << " cmdsize " << vd.cmdsize;
7642 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
7643 outs() << " Incorrect size\n";
7644 else
7645 outs() << "\n";
Davide Italiano56baef32015-08-26 12:26:11 +00007646 outs() << " version "
7647 << MachOObjectFile::getVersionMinMajor(vd, false) << "."
7648 << MachOObjectFile::getVersionMinMinor(vd, false);
7649 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
7650 if (Update != 0)
7651 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007652 outs() << "\n";
7653 if (vd.sdk == 0)
Kevin Enderby57538292014-12-17 01:01:30 +00007654 outs() << " sdk n/a";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007655 else {
Davide Italiano56baef32015-08-26 12:26:11 +00007656 outs() << " sdk "
7657 << MachOObjectFile::getVersionMinMajor(vd, true) << "."
7658 << MachOObjectFile::getVersionMinMinor(vd, true);
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007659 }
Davide Italiano56baef32015-08-26 12:26:11 +00007660 Update = MachOObjectFile::getVersionMinUpdate(vd, true);
7661 if (Update != 0)
7662 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007663 outs() << "\n";
7664}
7665
7666static void PrintSourceVersionCommand(MachO::source_version_command sd) {
7667 outs() << " cmd LC_SOURCE_VERSION\n";
7668 outs() << " cmdsize " << sd.cmdsize;
7669 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
7670 outs() << " Incorrect size\n";
7671 else
7672 outs() << "\n";
7673 uint64_t a = (sd.version >> 40) & 0xffffff;
7674 uint64_t b = (sd.version >> 30) & 0x3ff;
7675 uint64_t c = (sd.version >> 20) & 0x3ff;
7676 uint64_t d = (sd.version >> 10) & 0x3ff;
7677 uint64_t e = sd.version & 0x3ff;
7678 outs() << " version " << a << "." << b;
7679 if (e != 0)
7680 outs() << "." << c << "." << d << "." << e;
7681 else if (d != 0)
7682 outs() << "." << c << "." << d;
7683 else if (c != 0)
7684 outs() << "." << c;
7685 outs() << "\n";
7686}
7687
7688static void PrintEntryPointCommand(MachO::entry_point_command ep) {
7689 outs() << " cmd LC_MAIN\n";
7690 outs() << " cmdsize " << ep.cmdsize;
7691 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
7692 outs() << " Incorrect size\n";
7693 else
7694 outs() << "\n";
7695 outs() << " entryoff " << ep.entryoff << "\n";
7696 outs() << " stacksize " << ep.stacksize << "\n";
7697}
7698
Kevin Enderby0804f4672014-12-16 23:25:52 +00007699static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
7700 uint32_t object_size) {
7701 outs() << " cmd LC_ENCRYPTION_INFO\n";
7702 outs() << " cmdsize " << ec.cmdsize;
7703 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
7704 outs() << " Incorrect size\n";
7705 else
7706 outs() << "\n";
7707 outs() << " cryptoff " << ec.cryptoff;
7708 if (ec.cryptoff > object_size)
7709 outs() << " (past end of file)\n";
7710 else
7711 outs() << "\n";
7712 outs() << " cryptsize " << ec.cryptsize;
7713 if (ec.cryptsize > object_size)
7714 outs() << " (past end of file)\n";
7715 else
7716 outs() << "\n";
7717 outs() << " cryptid " << ec.cryptid << "\n";
7718}
7719
Kevin Enderby57538292014-12-17 01:01:30 +00007720static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007721 uint32_t object_size) {
Kevin Enderby57538292014-12-17 01:01:30 +00007722 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
7723 outs() << " cmdsize " << ec.cmdsize;
7724 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
7725 outs() << " Incorrect size\n";
7726 else
7727 outs() << "\n";
7728 outs() << " cryptoff " << ec.cryptoff;
7729 if (ec.cryptoff > object_size)
7730 outs() << " (past end of file)\n";
7731 else
7732 outs() << "\n";
7733 outs() << " cryptsize " << ec.cryptsize;
7734 if (ec.cryptsize > object_size)
7735 outs() << " (past end of file)\n";
7736 else
7737 outs() << "\n";
7738 outs() << " cryptid " << ec.cryptid << "\n";
7739 outs() << " pad " << ec.pad << "\n";
7740}
7741
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007742static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
7743 const char *Ptr) {
7744 outs() << " cmd LC_LINKER_OPTION\n";
7745 outs() << " cmdsize " << lo.cmdsize;
7746 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
7747 outs() << " Incorrect size\n";
7748 else
7749 outs() << "\n";
7750 outs() << " count " << lo.count << "\n";
7751 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
7752 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
7753 uint32_t i = 0;
7754 while (left > 0) {
7755 while (*string == '\0' && left > 0) {
7756 string++;
7757 left--;
7758 }
7759 if (left > 0) {
7760 i++;
7761 outs() << " string #" << i << " " << format("%.*s\n", left, string);
David Majnemerd4449ed2014-12-20 08:24:43 +00007762 uint32_t NullPos = StringRef(string, left).find('\0');
7763 uint32_t len = std::min(NullPos, left) + 1;
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007764 string += len;
7765 left -= len;
7766 }
7767 }
7768 if (lo.count != i)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007769 outs() << " count " << lo.count << " does not match number of strings "
7770 << i << "\n";
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00007771}
7772
Kevin Enderbyb4b79312014-12-18 19:24:35 +00007773static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
7774 const char *Ptr) {
7775 outs() << " cmd LC_SUB_FRAMEWORK\n";
7776 outs() << " cmdsize " << sub.cmdsize;
7777 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
7778 outs() << " Incorrect size\n";
7779 else
7780 outs() << "\n";
7781 if (sub.umbrella < sub.cmdsize) {
7782 const char *P = Ptr + sub.umbrella;
7783 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
7784 } else {
7785 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
7786 }
7787}
7788
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00007789static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
7790 const char *Ptr) {
7791 outs() << " cmd LC_SUB_UMBRELLA\n";
7792 outs() << " cmdsize " << sub.cmdsize;
7793 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
7794 outs() << " Incorrect size\n";
7795 else
7796 outs() << "\n";
7797 if (sub.sub_umbrella < sub.cmdsize) {
7798 const char *P = Ptr + sub.sub_umbrella;
7799 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
7800 } else {
7801 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
7802 }
7803}
7804
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007805static void PrintSubLibraryCommand(MachO::sub_library_command sub,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007806 const char *Ptr) {
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007807 outs() << " cmd LC_SUB_LIBRARY\n";
7808 outs() << " cmdsize " << sub.cmdsize;
7809 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
7810 outs() << " Incorrect size\n";
7811 else
7812 outs() << "\n";
7813 if (sub.sub_library < sub.cmdsize) {
7814 const char *P = Ptr + sub.sub_library;
7815 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7816 } else {
7817 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7818 }
7819}
7820
Kevin Enderby186eac32014-12-19 21:06:24 +00007821static void PrintSubClientCommand(MachO::sub_client_command sub,
7822 const char *Ptr) {
7823 outs() << " cmd LC_SUB_CLIENT\n";
7824 outs() << " cmdsize " << sub.cmdsize;
7825 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7826 outs() << " Incorrect size\n";
7827 else
7828 outs() << "\n";
7829 if (sub.client < sub.cmdsize) {
7830 const char *P = Ptr + sub.client;
7831 outs() << " client " << P << " (offset " << sub.client << ")\n";
7832 } else {
7833 outs() << " client ?(bad offset " << sub.client << ")\n";
7834 }
7835}
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00007836
Kevin Enderby52e4ce42014-12-19 22:25:22 +00007837static void PrintRoutinesCommand(MachO::routines_command r) {
7838 outs() << " cmd LC_ROUTINES\n";
7839 outs() << " cmdsize " << r.cmdsize;
7840 if (r.cmdsize != sizeof(struct MachO::routines_command))
7841 outs() << " Incorrect size\n";
7842 else
7843 outs() << "\n";
7844 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7845 outs() << " init_module " << r.init_module << "\n";
7846 outs() << " reserved1 " << r.reserved1 << "\n";
7847 outs() << " reserved2 " << r.reserved2 << "\n";
7848 outs() << " reserved3 " << r.reserved3 << "\n";
7849 outs() << " reserved4 " << r.reserved4 << "\n";
7850 outs() << " reserved5 " << r.reserved5 << "\n";
7851 outs() << " reserved6 " << r.reserved6 << "\n";
7852}
7853
7854static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7855 outs() << " cmd LC_ROUTINES_64\n";
7856 outs() << " cmdsize " << r.cmdsize;
7857 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7858 outs() << " Incorrect size\n";
7859 else
7860 outs() << "\n";
7861 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
7862 outs() << " init_module " << r.init_module << "\n";
7863 outs() << " reserved1 " << r.reserved1 << "\n";
7864 outs() << " reserved2 " << r.reserved2 << "\n";
7865 outs() << " reserved3 " << r.reserved3 << "\n";
7866 outs() << " reserved4 " << r.reserved4 << "\n";
7867 outs() << " reserved5 " << r.reserved5 << "\n";
7868 outs() << " reserved6 " << r.reserved6 << "\n";
7869}
7870
Kevin Enderby48ef5342014-12-23 22:56:39 +00007871static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7872 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7873 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7874 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7875 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7876 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7877 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7878 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7879 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7880 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7881 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7882 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7883 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7884 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7885 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7886 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7887 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7888 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7889 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7890 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7891 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7892 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7893}
7894
Kevin Enderby227df342014-12-23 23:43:59 +00007895static void Print_mmst_reg(MachO::mmst_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007896 uint32_t f;
7897 outs() << "\t mmst_reg ";
7898 for (f = 0; f < 10; f++)
7899 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7900 outs() << "\n";
7901 outs() << "\t mmst_rsrv ";
7902 for (f = 0; f < 6; f++)
7903 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7904 outs() << "\n";
7905}
7906
Kevin Enderbyaefb0032014-12-24 00:16:51 +00007907static void Print_xmm_reg(MachO::xmm_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00007908 uint32_t f;
7909 outs() << "\t xmm_reg ";
7910 for (f = 0; f < 16; f++)
7911 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7912 outs() << "\n";
7913}
7914
7915static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7916 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7917 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7918 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7919 outs() << " denorm " << fpu.fpu_fcw.denorm;
7920 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7921 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7922 outs() << " undfl " << fpu.fpu_fcw.undfl;
7923 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7924 outs() << "\t\t pc ";
7925 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7926 outs() << "FP_PREC_24B ";
7927 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
7928 outs() << "FP_PREC_53B ";
7929 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
7930 outs() << "FP_PREC_64B ";
7931 else
7932 outs() << fpu.fpu_fcw.pc << " ";
7933 outs() << "rc ";
7934 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
7935 outs() << "FP_RND_NEAR ";
7936 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
7937 outs() << "FP_RND_DOWN ";
7938 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
7939 outs() << "FP_RND_UP ";
7940 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00007941 outs() << "FP_CHOP ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00007942 outs() << "\n";
7943 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
7944 outs() << " denorm " << fpu.fpu_fsw.denorm;
7945 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
7946 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
7947 outs() << " undfl " << fpu.fpu_fsw.undfl;
7948 outs() << " precis " << fpu.fpu_fsw.precis;
7949 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
7950 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
7951 outs() << " c0 " << fpu.fpu_fsw.c0;
7952 outs() << " c1 " << fpu.fpu_fsw.c1;
7953 outs() << " c2 " << fpu.fpu_fsw.c2;
7954 outs() << " tos " << fpu.fpu_fsw.tos;
7955 outs() << " c3 " << fpu.fpu_fsw.c3;
7956 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
7957 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
7958 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
7959 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
7960 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
7961 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
7962 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
7963 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
7964 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
7965 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
7966 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
7967 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
7968 outs() << "\n";
7969 outs() << "\t fpu_stmm0:\n";
7970 Print_mmst_reg(fpu.fpu_stmm0);
7971 outs() << "\t fpu_stmm1:\n";
7972 Print_mmst_reg(fpu.fpu_stmm1);
7973 outs() << "\t fpu_stmm2:\n";
7974 Print_mmst_reg(fpu.fpu_stmm2);
7975 outs() << "\t fpu_stmm3:\n";
7976 Print_mmst_reg(fpu.fpu_stmm3);
7977 outs() << "\t fpu_stmm4:\n";
7978 Print_mmst_reg(fpu.fpu_stmm4);
7979 outs() << "\t fpu_stmm5:\n";
7980 Print_mmst_reg(fpu.fpu_stmm5);
7981 outs() << "\t fpu_stmm6:\n";
7982 Print_mmst_reg(fpu.fpu_stmm6);
7983 outs() << "\t fpu_stmm7:\n";
7984 Print_mmst_reg(fpu.fpu_stmm7);
7985 outs() << "\t fpu_xmm0:\n";
7986 Print_xmm_reg(fpu.fpu_xmm0);
7987 outs() << "\t fpu_xmm1:\n";
7988 Print_xmm_reg(fpu.fpu_xmm1);
7989 outs() << "\t fpu_xmm2:\n";
7990 Print_xmm_reg(fpu.fpu_xmm2);
7991 outs() << "\t fpu_xmm3:\n";
7992 Print_xmm_reg(fpu.fpu_xmm3);
7993 outs() << "\t fpu_xmm4:\n";
7994 Print_xmm_reg(fpu.fpu_xmm4);
7995 outs() << "\t fpu_xmm5:\n";
7996 Print_xmm_reg(fpu.fpu_xmm5);
7997 outs() << "\t fpu_xmm6:\n";
7998 Print_xmm_reg(fpu.fpu_xmm6);
7999 outs() << "\t fpu_xmm7:\n";
8000 Print_xmm_reg(fpu.fpu_xmm7);
8001 outs() << "\t fpu_xmm8:\n";
8002 Print_xmm_reg(fpu.fpu_xmm8);
8003 outs() << "\t fpu_xmm9:\n";
8004 Print_xmm_reg(fpu.fpu_xmm9);
8005 outs() << "\t fpu_xmm10:\n";
8006 Print_xmm_reg(fpu.fpu_xmm10);
8007 outs() << "\t fpu_xmm11:\n";
8008 Print_xmm_reg(fpu.fpu_xmm11);
8009 outs() << "\t fpu_xmm12:\n";
8010 Print_xmm_reg(fpu.fpu_xmm12);
8011 outs() << "\t fpu_xmm13:\n";
8012 Print_xmm_reg(fpu.fpu_xmm13);
8013 outs() << "\t fpu_xmm14:\n";
8014 Print_xmm_reg(fpu.fpu_xmm14);
8015 outs() << "\t fpu_xmm15:\n";
8016 Print_xmm_reg(fpu.fpu_xmm15);
8017 outs() << "\t fpu_rsrv4:\n";
8018 for (uint32_t f = 0; f < 6; f++) {
8019 outs() << "\t ";
8020 for (uint32_t g = 0; g < 16; g++)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008021 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008022 outs() << "\n";
8023 }
8024 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8025 outs() << "\n";
8026}
8027
8028static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8029 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8030 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8031 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8032}
8033
8034static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8035 bool isLittleEndian, uint32_t cputype) {
8036 if (t.cmd == MachO::LC_THREAD)
8037 outs() << " cmd LC_THREAD\n";
8038 else if (t.cmd == MachO::LC_UNIXTHREAD)
8039 outs() << " cmd LC_UNIXTHREAD\n";
8040 else
8041 outs() << " cmd " << t.cmd << " (unknown)\n";
8042 outs() << " cmdsize " << t.cmdsize;
8043 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8044 outs() << " Incorrect size\n";
8045 else
8046 outs() << "\n";
8047
8048 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8049 const char *end = Ptr + t.cmdsize;
8050 uint32_t flavor, count, left;
8051 if (cputype == MachO::CPU_TYPE_X86_64) {
8052 while (begin < end) {
8053 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8054 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8055 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008056 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008057 flavor = 0;
8058 begin = end;
8059 }
8060 if (isLittleEndian != sys::IsLittleEndianHost)
8061 sys::swapByteOrder(flavor);
8062 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8063 memcpy((char *)&count, begin, sizeof(uint32_t));
8064 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008065 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008066 count = 0;
8067 begin = end;
8068 }
8069 if (isLittleEndian != sys::IsLittleEndianHost)
8070 sys::swapByteOrder(count);
8071 if (flavor == MachO::x86_THREAD_STATE64) {
8072 outs() << " flavor x86_THREAD_STATE64\n";
8073 if (count == MachO::x86_THREAD_STATE64_COUNT)
8074 outs() << " count x86_THREAD_STATE64_COUNT\n";
8075 else
8076 outs() << " count " << count
8077 << " (not x86_THREAD_STATE64_COUNT)\n";
8078 MachO::x86_thread_state64_t cpu64;
8079 left = end - begin;
8080 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8081 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8082 begin += sizeof(MachO::x86_thread_state64_t);
8083 } else {
8084 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8085 memcpy(&cpu64, begin, left);
8086 begin += left;
8087 }
8088 if (isLittleEndian != sys::IsLittleEndianHost)
8089 swapStruct(cpu64);
8090 Print_x86_thread_state64_t(cpu64);
8091 } else if (flavor == MachO::x86_THREAD_STATE) {
8092 outs() << " flavor x86_THREAD_STATE\n";
8093 if (count == MachO::x86_THREAD_STATE_COUNT)
8094 outs() << " count x86_THREAD_STATE_COUNT\n";
8095 else
8096 outs() << " count " << count
8097 << " (not x86_THREAD_STATE_COUNT)\n";
8098 struct MachO::x86_thread_state_t ts;
8099 left = end - begin;
8100 if (left >= sizeof(MachO::x86_thread_state_t)) {
8101 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8102 begin += sizeof(MachO::x86_thread_state_t);
8103 } else {
8104 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8105 memcpy(&ts, begin, left);
8106 begin += left;
8107 }
8108 if (isLittleEndian != sys::IsLittleEndianHost)
8109 swapStruct(ts);
8110 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8111 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8112 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8113 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8114 else
8115 outs() << "tsh.count " << ts.tsh.count
8116 << " (not x86_THREAD_STATE64_COUNT\n";
8117 Print_x86_thread_state64_t(ts.uts.ts64);
8118 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008119 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8120 << ts.tsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008121 }
8122 } else if (flavor == MachO::x86_FLOAT_STATE) {
8123 outs() << " flavor x86_FLOAT_STATE\n";
8124 if (count == MachO::x86_FLOAT_STATE_COUNT)
8125 outs() << " count x86_FLOAT_STATE_COUNT\n";
8126 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008127 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008128 struct MachO::x86_float_state_t fs;
8129 left = end - begin;
8130 if (left >= sizeof(MachO::x86_float_state_t)) {
8131 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8132 begin += sizeof(MachO::x86_float_state_t);
8133 } else {
8134 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8135 memcpy(&fs, begin, left);
8136 begin += left;
8137 }
8138 if (isLittleEndian != sys::IsLittleEndianHost)
8139 swapStruct(fs);
8140 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8141 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008142 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008143 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8144 else
8145 outs() << "fsh.count " << fs.fsh.count
8146 << " (not x86_FLOAT_STATE64_COUNT\n";
8147 Print_x86_float_state_t(fs.ufs.fs64);
8148 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008149 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8150 << fs.fsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008151 }
8152 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8153 outs() << " flavor x86_EXCEPTION_STATE\n";
8154 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8155 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8156 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008157 outs() << " count " << count
Kevin Enderby48ef5342014-12-23 22:56:39 +00008158 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8159 struct MachO::x86_exception_state_t es;
8160 left = end - begin;
8161 if (left >= sizeof(MachO::x86_exception_state_t)) {
8162 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8163 begin += sizeof(MachO::x86_exception_state_t);
8164 } else {
8165 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8166 memcpy(&es, begin, left);
8167 begin += left;
8168 }
8169 if (isLittleEndian != sys::IsLittleEndianHost)
8170 swapStruct(es);
8171 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8172 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008173 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008174 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8175 else
8176 outs() << "\t esh.count " << es.esh.count
8177 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8178 Print_x86_exception_state_t(es.ues.es64);
8179 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008180 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8181 << es.esh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008182 }
8183 } else {
8184 outs() << " flavor " << flavor << " (unknown)\n";
8185 outs() << " count " << count << "\n";
8186 outs() << " state (unknown)\n";
8187 begin += count * sizeof(uint32_t);
8188 }
8189 }
8190 } else {
8191 while (begin < end) {
8192 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8193 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8194 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008195 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008196 flavor = 0;
8197 begin = end;
8198 }
8199 if (isLittleEndian != sys::IsLittleEndianHost)
8200 sys::swapByteOrder(flavor);
8201 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8202 memcpy((char *)&count, begin, sizeof(uint32_t));
8203 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008204 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008205 count = 0;
8206 begin = end;
8207 }
8208 if (isLittleEndian != sys::IsLittleEndianHost)
8209 sys::swapByteOrder(count);
8210 outs() << " flavor " << flavor << "\n";
8211 outs() << " count " << count << "\n";
8212 outs() << " state (Unknown cputype/cpusubtype)\n";
8213 begin += count * sizeof(uint32_t);
8214 }
8215 }
8216}
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008217
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008218static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8219 if (dl.cmd == MachO::LC_ID_DYLIB)
8220 outs() << " cmd LC_ID_DYLIB\n";
8221 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8222 outs() << " cmd LC_LOAD_DYLIB\n";
8223 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8224 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8225 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8226 outs() << " cmd LC_REEXPORT_DYLIB\n";
8227 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8228 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8229 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8230 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8231 else
8232 outs() << " cmd " << dl.cmd << " (unknown)\n";
8233 outs() << " cmdsize " << dl.cmdsize;
8234 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8235 outs() << " Incorrect size\n";
8236 else
8237 outs() << "\n";
8238 if (dl.dylib.name < dl.cmdsize) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008239 const char *P = (const char *)(Ptr) + dl.dylib.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008240 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8241 } else {
8242 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8243 }
8244 outs() << " time stamp " << dl.dylib.timestamp << " ";
8245 time_t t = dl.dylib.timestamp;
8246 outs() << ctime(&t);
8247 outs() << " current version ";
8248 if (dl.dylib.current_version == 0xffffffff)
8249 outs() << "n/a\n";
8250 else
8251 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8252 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8253 << (dl.dylib.current_version & 0xff) << "\n";
8254 outs() << "compatibility version ";
8255 if (dl.dylib.compatibility_version == 0xffffffff)
8256 outs() << "n/a\n";
8257 else
8258 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8259 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8260 << (dl.dylib.compatibility_version & 0xff) << "\n";
8261}
8262
8263static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8264 uint32_t object_size) {
8265 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8266 outs() << " cmd LC_FUNCTION_STARTS\n";
8267 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8268 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8269 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8270 outs() << " cmd LC_FUNCTION_STARTS\n";
8271 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8272 outs() << " cmd LC_DATA_IN_CODE\n";
8273 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8274 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8275 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8276 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8277 else
8278 outs() << " cmd " << ld.cmd << " (?)\n";
8279 outs() << " cmdsize " << ld.cmdsize;
8280 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8281 outs() << " Incorrect size\n";
8282 else
8283 outs() << "\n";
8284 outs() << " dataoff " << ld.dataoff;
8285 if (ld.dataoff > object_size)
8286 outs() << " (past end of file)\n";
8287 else
8288 outs() << "\n";
8289 outs() << " datasize " << ld.datasize;
8290 uint64_t big_size = ld.dataoff;
8291 big_size += ld.datasize;
8292 if (big_size > object_size)
8293 outs() << " (past end of file)\n";
8294 else
8295 outs() << "\n";
8296}
8297
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008298static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8299 uint32_t cputype, bool verbose) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008300 StringRef Buf = Obj->getData();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008301 unsigned Index = 0;
8302 for (const auto &Command : Obj->load_commands()) {
8303 outs() << "Load command " << Index++ << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00008304 if (Command.C.cmd == MachO::LC_SEGMENT) {
8305 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8306 const char *sg_segname = SLC.segname;
8307 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8308 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8309 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8310 verbose);
8311 for (unsigned j = 0; j < SLC.nsects; j++) {
Kevin Enderbyc9713382014-12-16 01:14:45 +00008312 MachO::section S = Obj->getSection(Command, j);
Kevin Enderby956366c2014-08-29 22:30:52 +00008313 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8314 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8315 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8316 }
8317 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8318 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8319 const char *sg_segname = SLC_64.segname;
8320 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8321 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8322 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8323 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8324 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8325 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8326 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8327 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8328 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8329 sg_segname, filetype, Buf.size(), verbose);
8330 }
8331 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8332 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008333 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008334 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8335 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8336 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008337 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8338 Obj->is64Bit());
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008339 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8340 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8341 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8342 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8343 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8344 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8345 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8346 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8347 PrintDyldLoadCommand(Dyld, Command.Ptr);
8348 } else if (Command.C.cmd == MachO::LC_UUID) {
8349 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8350 PrintUuidLoadCommand(Uuid);
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008351 } else if (Command.C.cmd == MachO::LC_RPATH) {
8352 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8353 PrintRpathLoadCommand(Rpath, Command.Ptr);
Kevin Enderby1ff0ecc2014-12-16 21:48:27 +00008354 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
Tim Northoverbfbfb122015-11-02 21:26:58 +00008355 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
8356 Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
8357 Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008358 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8359 PrintVersionMinLoadCommand(Vd);
8360 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8361 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8362 PrintSourceVersionCommand(Sd);
8363 } else if (Command.C.cmd == MachO::LC_MAIN) {
8364 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8365 PrintEntryPointCommand(Ep);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008366 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008367 MachO::encryption_info_command Ei =
8368 Obj->getEncryptionInfoCommand(Command);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008369 PrintEncryptionInfoCommand(Ei, Buf.size());
Kevin Enderby57538292014-12-17 01:01:30 +00008370 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008371 MachO::encryption_info_command_64 Ei =
8372 Obj->getEncryptionInfoCommand64(Command);
Kevin Enderby57538292014-12-17 01:01:30 +00008373 PrintEncryptionInfoCommand64(Ei, Buf.size());
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008374 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008375 MachO::linker_option_command Lo =
8376 Obj->getLinkerOptionLoadCommand(Command);
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008377 PrintLinkerOptionCommand(Lo, Command.Ptr);
Kevin Enderbyb4b79312014-12-18 19:24:35 +00008378 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8379 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8380 PrintSubFrameworkCommand(Sf, Command.Ptr);
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00008381 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8382 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8383 PrintSubUmbrellaCommand(Sf, Command.Ptr);
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008384 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8385 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8386 PrintSubLibraryCommand(Sl, Command.Ptr);
Kevin Enderby186eac32014-12-19 21:06:24 +00008387 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8388 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8389 PrintSubClientCommand(Sc, Command.Ptr);
Kevin Enderby52e4ce42014-12-19 22:25:22 +00008390 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8391 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8392 PrintRoutinesCommand(Rc);
8393 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8394 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8395 PrintRoutinesCommand64(Rc);
Kevin Enderby48ef5342014-12-23 22:56:39 +00008396 } else if (Command.C.cmd == MachO::LC_THREAD ||
8397 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8398 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8399 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
Nick Kledzik15558912014-10-16 18:58:20 +00008400 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8401 Command.C.cmd == MachO::LC_ID_DYLIB ||
8402 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8403 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8404 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8405 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008406 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8407 PrintDylibCommand(Dl, Command.Ptr);
8408 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8409 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8410 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8411 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8412 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8413 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8414 MachO::linkedit_data_command Ld =
8415 Obj->getLinkeditDataLoadCommand(Command);
8416 PrintLinkEditDataCommand(Ld, Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008417 } else {
8418 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8419 << ")\n";
8420 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8421 // TODO: get and print the raw bytes of the load command.
8422 }
8423 // TODO: print all the other kinds of load commands.
Kevin Enderby956366c2014-08-29 22:30:52 +00008424 }
8425}
8426
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008427static void getAndPrintMachHeader(const MachOObjectFile *Obj,
Kevin Enderby956366c2014-08-29 22:30:52 +00008428 uint32_t &filetype, uint32_t &cputype,
8429 bool verbose) {
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008430 if (Obj->is64Bit()) {
8431 MachO::mach_header_64 H_64;
8432 H_64 = Obj->getHeader64();
8433 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8434 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00008435 filetype = H_64.filetype;
8436 cputype = H_64.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008437 } else {
8438 MachO::mach_header H;
8439 H = Obj->getHeader();
8440 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8441 H.sizeofcmds, H.flags, verbose);
Kevin Enderby956366c2014-08-29 22:30:52 +00008442 filetype = H.filetype;
8443 cputype = H.cputype;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008444 }
8445}
8446
8447void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8448 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby956366c2014-08-29 22:30:52 +00008449 uint32_t filetype = 0;
8450 uint32_t cputype = 0;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008451 getAndPrintMachHeader(file, filetype, cputype, !NonVerbose);
8452 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008453}
Nick Kledzikd04bc352014-08-30 00:20:14 +00008454
8455//===----------------------------------------------------------------------===//
8456// export trie dumping
8457//===----------------------------------------------------------------------===//
8458
8459void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008460 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
8461 uint64_t Flags = Entry.flags();
Nick Kledzikd04bc352014-08-30 00:20:14 +00008462 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
8463 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
8464 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8465 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
8466 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8467 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
8468 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
8469 if (ReExport)
8470 outs() << "[re-export] ";
8471 else
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008472 outs() << format("0x%08llX ",
8473 Entry.address()); // FIXME:add in base address
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008474 outs() << Entry.name();
Nick Kledzikd04bc352014-08-30 00:20:14 +00008475 if (WeakDef || ThreadLocal || Resolver || Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008476 bool NeedsComma = false;
Nick Kledzik1d1ac4b2014-09-03 01:12:52 +00008477 outs() << " [";
Nick Kledzikd04bc352014-08-30 00:20:14 +00008478 if (WeakDef) {
8479 outs() << "weak_def";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008480 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008481 }
8482 if (ThreadLocal) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008483 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008484 outs() << ", ";
8485 outs() << "per-thread";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008486 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008487 }
8488 if (Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008489 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008490 outs() << ", ";
8491 outs() << "absolute";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008492 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008493 }
8494 if (Resolver) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008495 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00008496 outs() << ", ";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008497 outs() << format("resolver=0x%08llX", Entry.other());
8498 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00008499 }
8500 outs() << "]";
8501 }
8502 if (ReExport) {
8503 StringRef DylibName = "unknown";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008504 int Ordinal = Entry.other() - 1;
8505 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
8506 if (Entry.otherName().empty())
Nick Kledzikd04bc352014-08-30 00:20:14 +00008507 outs() << " (from " << DylibName << ")";
8508 else
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00008509 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
Nick Kledzikd04bc352014-08-30 00:20:14 +00008510 }
8511 outs() << "\n";
8512 }
8513}
Nick Kledzikac431442014-09-12 21:34:15 +00008514
Nick Kledzikac431442014-09-12 21:34:15 +00008515//===----------------------------------------------------------------------===//
8516// rebase table dumping
8517//===----------------------------------------------------------------------===//
8518
8519namespace {
8520class SegInfo {
8521public:
8522 SegInfo(const object::MachOObjectFile *Obj);
8523
8524 StringRef segmentName(uint32_t SegIndex);
8525 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
8526 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00008527 bool isValidSegIndexAndOffset(uint32_t SegIndex, uint64_t SegOffset);
Nick Kledzikac431442014-09-12 21:34:15 +00008528
8529private:
8530 struct SectionInfo {
8531 uint64_t Address;
8532 uint64_t Size;
8533 StringRef SectionName;
8534 StringRef SegmentName;
8535 uint64_t OffsetInSegment;
8536 uint64_t SegmentStartAddress;
8537 uint32_t SegmentIndex;
8538 };
8539 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
8540 SmallVector<SectionInfo, 32> Sections;
8541};
8542}
8543
8544SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
8545 // Build table of sections so segIndex/offset pairs can be translated.
Nick Kledzik56ebef42014-09-16 01:41:51 +00008546 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
Nick Kledzikac431442014-09-12 21:34:15 +00008547 StringRef CurSegName;
8548 uint64_t CurSegAddress;
8549 for (const SectionRef &Section : Obj->sections()) {
8550 SectionInfo Info;
Davide Italianoccd53fe2015-08-05 07:18:31 +00008551 error(Section.getName(Info.SectionName));
Rafael Espindola80291272014-10-08 15:28:58 +00008552 Info.Address = Section.getAddress();
8553 Info.Size = Section.getSize();
Nick Kledzikac431442014-09-12 21:34:15 +00008554 Info.SegmentName =
8555 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
8556 if (!Info.SegmentName.equals(CurSegName)) {
8557 ++CurSegIndex;
8558 CurSegName = Info.SegmentName;
8559 CurSegAddress = Info.Address;
8560 }
8561 Info.SegmentIndex = CurSegIndex - 1;
8562 Info.OffsetInSegment = Info.Address - CurSegAddress;
8563 Info.SegmentStartAddress = CurSegAddress;
8564 Sections.push_back(Info);
8565 }
8566}
8567
8568StringRef SegInfo::segmentName(uint32_t SegIndex) {
8569 for (const SectionInfo &SI : Sections) {
8570 if (SI.SegmentIndex == SegIndex)
8571 return SI.SegmentName;
8572 }
8573 llvm_unreachable("invalid segIndex");
8574}
8575
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00008576bool SegInfo::isValidSegIndexAndOffset(uint32_t SegIndex,
8577 uint64_t OffsetInSeg) {
8578 for (const SectionInfo &SI : Sections) {
8579 if (SI.SegmentIndex != SegIndex)
8580 continue;
8581 if (SI.OffsetInSegment > OffsetInSeg)
8582 continue;
8583 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8584 continue;
8585 return true;
8586 }
8587 return false;
8588}
8589
Nick Kledzikac431442014-09-12 21:34:15 +00008590const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
8591 uint64_t OffsetInSeg) {
8592 for (const SectionInfo &SI : Sections) {
8593 if (SI.SegmentIndex != SegIndex)
8594 continue;
8595 if (SI.OffsetInSegment > OffsetInSeg)
8596 continue;
8597 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8598 continue;
8599 return SI;
8600 }
8601 llvm_unreachable("segIndex and offset not in any section");
8602}
8603
8604StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
8605 return findSection(SegIndex, OffsetInSeg).SectionName;
8606}
8607
8608uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
8609 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
8610 return SI.SegmentStartAddress + OffsetInSeg;
8611}
8612
8613void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
8614 // Build table of sections so names can used in final output.
8615 SegInfo sectionTable(Obj);
8616
8617 outs() << "segment section address type\n";
8618 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
8619 uint32_t SegIndex = Entry.segmentIndex();
8620 uint64_t OffsetInSeg = Entry.segmentOffset();
8621 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8622 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8623 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8624
8625 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008626 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
8627 SegmentName.str().c_str(), SectionName.str().c_str(),
8628 Address, Entry.typeName().str().c_str());
Nick Kledzikac431442014-09-12 21:34:15 +00008629 }
8630}
Nick Kledzik56ebef42014-09-16 01:41:51 +00008631
8632static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
8633 StringRef DylibName;
8634 switch (Ordinal) {
8635 case MachO::BIND_SPECIAL_DYLIB_SELF:
8636 return "this-image";
8637 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
8638 return "main-executable";
8639 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
8640 return "flat-namespace";
8641 default:
Nick Kledzikabd29872014-09-16 22:03:13 +00008642 if (Ordinal > 0) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008643 std::error_code EC =
8644 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
Nick Kledzikabd29872014-09-16 22:03:13 +00008645 if (EC)
Nick Kledzik51d2c2b2014-10-14 23:29:38 +00008646 return "<<bad library ordinal>>";
Nick Kledzikabd29872014-09-16 22:03:13 +00008647 return DylibName;
8648 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00008649 }
Nick Kledzikabd29872014-09-16 22:03:13 +00008650 return "<<unknown special ordinal>>";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008651}
8652
8653//===----------------------------------------------------------------------===//
8654// bind table dumping
8655//===----------------------------------------------------------------------===//
8656
8657void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
8658 // Build table of sections so names can used in final output.
8659 SegInfo sectionTable(Obj);
8660
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008661 outs() << "segment section address type "
8662 "addend dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008663 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
8664 uint32_t SegIndex = Entry.segmentIndex();
8665 uint64_t OffsetInSeg = Entry.segmentOffset();
8666 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8667 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8668 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8669
8670 // Table lines look like:
8671 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008672 StringRef Attr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00008673 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008674 Attr = " (weak_import)";
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008675 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008676 << left_justify(SectionName, 18) << " "
8677 << format_hex(Address, 10, true) << " "
8678 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008679 << format_decimal(Entry.addend(), 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008680 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008681 << Entry.symbolName() << Attr << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008682 }
8683}
8684
8685//===----------------------------------------------------------------------===//
8686// lazy bind table dumping
8687//===----------------------------------------------------------------------===//
8688
8689void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
8690 // Build table of sections so names can used in final output.
8691 SegInfo sectionTable(Obj);
8692
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008693 outs() << "segment section address "
8694 "dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008695 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
8696 uint32_t SegIndex = Entry.segmentIndex();
8697 uint64_t OffsetInSeg = Entry.segmentOffset();
8698 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8699 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8700 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8701
8702 // Table lines look like:
8703 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008704 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008705 << left_justify(SectionName, 18) << " "
8706 << format_hex(Address, 10, true) << " "
8707 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Nick Kledzik56ebef42014-09-16 01:41:51 +00008708 << Entry.symbolName() << "\n";
8709 }
8710}
8711
Nick Kledzik56ebef42014-09-16 01:41:51 +00008712//===----------------------------------------------------------------------===//
8713// weak bind table dumping
8714//===----------------------------------------------------------------------===//
8715
8716void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
8717 // Build table of sections so names can used in final output.
8718 SegInfo sectionTable(Obj);
8719
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008720 outs() << "segment section address "
8721 "type addend symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008722 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
8723 // Strong symbols don't have a location to update.
8724 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008725 outs() << " strong "
Nick Kledzik56ebef42014-09-16 01:41:51 +00008726 << Entry.symbolName() << "\n";
8727 continue;
8728 }
8729 uint32_t SegIndex = Entry.segmentIndex();
8730 uint64_t OffsetInSeg = Entry.segmentOffset();
8731 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8732 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8733 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8734
8735 // Table lines look like:
8736 // __DATA __data 0x00001000 pointer 0 _foo
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008737 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00008738 << left_justify(SectionName, 18) << " "
8739 << format_hex(Address, 10, true) << " "
8740 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008741 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
8742 << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00008743 }
8744}
8745
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008746// get_dyld_bind_info_symbolname() is used for disassembly and passed an
8747// address, ReferenceValue, in the Mach-O file and looks in the dyld bind
8748// information for that address. If the address is found its binding symbol
8749// name is returned. If not nullptr is returned.
8750static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
8751 struct DisassembleInfo *info) {
Kevin Enderby078be602014-10-23 19:53:12 +00008752 if (info->bindtable == nullptr) {
8753 info->bindtable = new (BindTable);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008754 SegInfo sectionTable(info->O);
8755 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
8756 uint32_t SegIndex = Entry.segmentIndex();
8757 uint64_t OffsetInSeg = Entry.segmentOffset();
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00008758 if (!sectionTable.isValidSegIndexAndOffset(SegIndex, OffsetInSeg))
8759 continue;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008760 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8761 const char *SymbolName = nullptr;
8762 StringRef name = Entry.symbolName();
8763 if (!name.empty())
8764 SymbolName = name.data();
Kevin Enderby078be602014-10-23 19:53:12 +00008765 info->bindtable->push_back(std::make_pair(Address, SymbolName));
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008766 }
8767 }
Kevin Enderby078be602014-10-23 19:53:12 +00008768 for (bind_table_iterator BI = info->bindtable->begin(),
8769 BE = info->bindtable->end();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00008770 BI != BE; ++BI) {
8771 uint64_t Address = BI->first;
8772 if (ReferenceValue == Address) {
8773 const char *SymbolName = BI->second;
8774 return SymbolName;
8775 }
8776 }
8777 return nullptr;
8778}