Bug Summary

File:tools/llvm-objdump/MachODump.cpp
Warning:line 9773, column 9
Value stored to 'NeedsComma' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name MachODump.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn325874/build-llvm/tools/llvm-objdump -I /build/llvm-toolchain-snapshot-7~svn325874/tools/llvm-objdump -I /build/llvm-toolchain-snapshot-7~svn325874/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn325874/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn325874/build-llvm/tools/llvm-objdump -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-02-23-163436-368-1 -x c++ /build/llvm-toolchain-snapshot-7~svn325874/tools/llvm-objdump/MachODump.cpp
1//===-- 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"
15#include "llvm-c/Disassembler.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringExtras.h"
18#include "llvm/ADT/Triple.h"
19#include "llvm/BinaryFormat/MachO.h"
20#include "llvm/Config/config.h"
21#include "llvm/DebugInfo/DIContext.h"
22#include "llvm/DebugInfo/DWARF/DWARFContext.h"
23#include "llvm/Demangle/Demangle.h"
24#include "llvm/MC/MCAsmInfo.h"
25#include "llvm/MC/MCContext.h"
26#include "llvm/MC/MCDisassembler/MCDisassembler.h"
27#include "llvm/MC/MCInst.h"
28#include "llvm/MC/MCInstPrinter.h"
29#include "llvm/MC/MCInstrDesc.h"
30#include "llvm/MC/MCInstrInfo.h"
31#include "llvm/MC/MCRegisterInfo.h"
32#include "llvm/MC/MCSubtargetInfo.h"
33#include "llvm/Object/MachO.h"
34#include "llvm/Object/MachOUniversal.h"
35#include "llvm/Support/Casting.h"
36#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/Debug.h"
38#include "llvm/Support/Endian.h"
39#include "llvm/Support/Format.h"
40#include "llvm/Support/FormattedStream.h"
41#include "llvm/Support/GraphWriter.h"
42#include "llvm/Support/LEB128.h"
43#include "llvm/Support/MemoryBuffer.h"
44#include "llvm/Support/TargetRegistry.h"
45#include "llvm/Support/TargetSelect.h"
46#include "llvm/Support/ToolOutputFile.h"
47#include "llvm/Support/raw_ostream.h"
48#include <algorithm>
49#include <cstring>
50#include <system_error>
51
52#ifdef HAVE_LIBXAR
53extern "C" {
54#include <xar/xar.h>
55}
56#endif
57
58using namespace llvm;
59using namespace object;
60
61static cl::opt<bool>
62 UseDbg("g",
63 cl::desc("Print line information from debug info if available"));
64
65static cl::opt<std::string> DSYMFile("dsym",
66 cl::desc("Use .dSYM file for debug info"));
67
68static cl::opt<bool> FullLeadingAddr("full-leading-addr",
69 cl::desc("Print full leading address"));
70
71static cl::opt<bool> NoLeadingHeaders("no-leading-headers",
72 cl::desc("Print no leading headers"));
73
74cl::opt<bool> llvm::UniversalHeaders("universal-headers",
75 cl::desc("Print Mach-O universal headers "
76 "(requires -macho)"));
77
78cl::opt<bool>
79 llvm::ArchiveHeaders("archive-headers",
80 cl::desc("Print archive headers for Mach-O archives "
81 "(requires -macho)"));
82
83cl::opt<bool>
84 ArchiveMemberOffsets("archive-member-offsets",
85 cl::desc("Print the offset to each archive member for "
86 "Mach-O archives (requires -macho and "
87 "-archive-headers)"));
88
89cl::opt<bool>
90 llvm::IndirectSymbols("indirect-symbols",
91 cl::desc("Print indirect symbol table for Mach-O "
92 "objects (requires -macho)"));
93
94cl::opt<bool>
95 llvm::DataInCode("data-in-code",
96 cl::desc("Print the data in code table for Mach-O objects "
97 "(requires -macho)"));
98
99cl::opt<bool>
100 llvm::LinkOptHints("link-opt-hints",
101 cl::desc("Print the linker optimization hints for "
102 "Mach-O objects (requires -macho)"));
103
104cl::opt<bool>
105 llvm::InfoPlist("info-plist",
106 cl::desc("Print the info plist section as strings for "
107 "Mach-O objects (requires -macho)"));
108
109cl::opt<bool>
110 llvm::DylibsUsed("dylibs-used",
111 cl::desc("Print the shared libraries used for linked "
112 "Mach-O files (requires -macho)"));
113
114cl::opt<bool>
115 llvm::DylibId("dylib-id",
116 cl::desc("Print the shared library's id for the dylib Mach-O "
117 "file (requires -macho)"));
118
119cl::opt<bool>
120 llvm::NonVerbose("non-verbose",
121 cl::desc("Print the info for Mach-O objects in "
122 "non-verbose or numeric form (requires -macho)"));
123
124cl::opt<bool>
125 llvm::ObjcMetaData("objc-meta-data",
126 cl::desc("Print the Objective-C runtime meta data for "
127 "Mach-O files (requires -macho)"));
128
129cl::opt<std::string> llvm::DisSymName(
130 "dis-symname",
131 cl::desc("disassemble just this symbol's instructions (requires -macho)"));
132
133static cl::opt<bool> NoSymbolicOperands(
134 "no-symbolic-operands",
135 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
136
137static cl::list<std::string>
138 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
139 cl::ZeroOrMore);
140
141bool ArchAll = false;
142
143static std::string ThumbTripleName;
144
145static const Target *GetTarget(const MachOObjectFile *MachOObj,
146 const char **McpuDefault,
147 const Target **ThumbTarget) {
148 // Figure out the target triple.
149 llvm::Triple TT(TripleName);
150 if (TripleName.empty()) {
151 TT = MachOObj->getArchTriple(McpuDefault);
152 TripleName = TT.str();
153 }
154
155 if (TT.getArch() == Triple::arm) {
156 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
157 // that support ARM are also capable of Thumb mode.
158 llvm::Triple ThumbTriple = TT;
159 std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
160 ThumbTriple.setArchName(ThumbName);
161 ThumbTripleName = ThumbTriple.str();
162 }
163
164 // Get the target specific parser.
165 std::string Error;
166 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
167 if (TheTarget && ThumbTripleName.empty())
168 return TheTarget;
169
170 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
171 if (*ThumbTarget)
172 return TheTarget;
173
174 errs() << "llvm-objdump: error: unable to get target for '";
175 if (!TheTarget)
176 errs() << TripleName;
177 else
178 errs() << ThumbTripleName;
179 errs() << "', see --version and --triple.\n";
180 return nullptr;
181}
182
183struct SymbolSorter {
184 bool operator()(const SymbolRef &A, const SymbolRef &B) {
185 Expected<SymbolRef::Type> ATypeOrErr = A.getType();
186 if (!ATypeOrErr)
187 report_error(A.getObject()->getFileName(), ATypeOrErr.takeError());
188 SymbolRef::Type AType = *ATypeOrErr;
189 Expected<SymbolRef::Type> BTypeOrErr = B.getType();
190 if (!BTypeOrErr)
191 report_error(B.getObject()->getFileName(), BTypeOrErr.takeError());
192 SymbolRef::Type BType = *BTypeOrErr;
193 uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue();
194 uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue();
195 return AAddr < BAddr;
196 }
197};
198
199// Types for the storted data in code table that is built before disassembly
200// and the predicate function to sort them.
201typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
202typedef std::vector<DiceTableEntry> DiceTable;
203typedef DiceTable::iterator dice_table_iterator;
204
205#ifdef HAVE_LIBXAR
206namespace {
207struct ScopedXarFile {
208 xar_t xar;
209 ScopedXarFile(const char *filename, int32_t flags)
210 : xar(xar_open(filename, flags)) {}
211 ~ScopedXarFile() {
212 if (xar)
213 xar_close(xar);
214 }
215 ScopedXarFile(const ScopedXarFile &) = delete;
216 ScopedXarFile &operator=(const ScopedXarFile &) = delete;
217 operator xar_t() { return xar; }
218};
219
220struct ScopedXarIter {
221 xar_iter_t iter;
222 ScopedXarIter() : iter(xar_iter_new()) {}
223 ~ScopedXarIter() {
224 if (iter)
225 xar_iter_free(iter);
226 }
227 ScopedXarIter(const ScopedXarIter &) = delete;
228 ScopedXarIter &operator=(const ScopedXarIter &) = delete;
229 operator xar_iter_t() { return iter; }
230};
231} // namespace
232#endif // defined(HAVE_LIBXAR)
233
234// This is used to search for a data in code table entry for the PC being
235// disassembled. The j parameter has the PC in j.first. A single data in code
236// table entry can cover many bytes for each of its Kind's. So if the offset,
237// aka the i.first value, of the data in code table entry plus its Length
238// covers the PC being searched for this will return true. If not it will
239// return false.
240static bool compareDiceTableEntries(const DiceTableEntry &i,
241 const DiceTableEntry &j) {
242 uint16_t Length;
243 i.second.getLength(Length);
244
245 return j.first >= i.first && j.first < i.first + Length;
246}
247
248static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
249 unsigned short Kind) {
250 uint32_t Value, Size = 1;
251
252 switch (Kind) {
253 default:
254 case MachO::DICE_KIND_DATA:
255 if (Length >= 4) {
256 if (!NoShowRawInsn)
257 dumpBytes(makeArrayRef(bytes, 4), outs());
258 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
259 outs() << "\t.long " << Value;
260 Size = 4;
261 } else if (Length >= 2) {
262 if (!NoShowRawInsn)
263 dumpBytes(makeArrayRef(bytes, 2), outs());
264 Value = bytes[1] << 8 | bytes[0];
265 outs() << "\t.short " << Value;
266 Size = 2;
267 } else {
268 if (!NoShowRawInsn)
269 dumpBytes(makeArrayRef(bytes, 2), outs());
270 Value = bytes[0];
271 outs() << "\t.byte " << Value;
272 Size = 1;
273 }
274 if (Kind == MachO::DICE_KIND_DATA)
275 outs() << "\t@ KIND_DATA\n";
276 else
277 outs() << "\t@ data in code kind = " << Kind << "\n";
278 break;
279 case MachO::DICE_KIND_JUMP_TABLE8:
280 if (!NoShowRawInsn)
281 dumpBytes(makeArrayRef(bytes, 1), outs());
282 Value = bytes[0];
283 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
284 Size = 1;
285 break;
286 case MachO::DICE_KIND_JUMP_TABLE16:
287 if (!NoShowRawInsn)
288 dumpBytes(makeArrayRef(bytes, 2), outs());
289 Value = bytes[1] << 8 | bytes[0];
290 outs() << "\t.short " << format("%5u", Value & 0xffff)
291 << "\t@ KIND_JUMP_TABLE16\n";
292 Size = 2;
293 break;
294 case MachO::DICE_KIND_JUMP_TABLE32:
295 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
296 if (!NoShowRawInsn)
297 dumpBytes(makeArrayRef(bytes, 4), outs());
298 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
299 outs() << "\t.long " << Value;
300 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
301 outs() << "\t@ KIND_JUMP_TABLE32\n";
302 else
303 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
304 Size = 4;
305 break;
306 }
307 return Size;
308}
309
310static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
311 std::vector<SectionRef> &Sections,
312 std::vector<SymbolRef> &Symbols,
313 SmallVectorImpl<uint64_t> &FoundFns,
314 uint64_t &BaseSegmentAddress) {
315 for (const SymbolRef &Symbol : MachOObj->symbols()) {
316 Expected<StringRef> SymName = Symbol.getName();
317 if (!SymName)
318 report_error(MachOObj->getFileName(), SymName.takeError());
319 if (!SymName->startswith("ltmp"))
320 Symbols.push_back(Symbol);
321 }
322
323 for (const SectionRef &Section : MachOObj->sections()) {
324 StringRef SectName;
325 Section.getName(SectName);
326 Sections.push_back(Section);
327 }
328
329 bool BaseSegmentAddressSet = false;
330 for (const auto &Command : MachOObj->load_commands()) {
331 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
332 // We found a function starts segment, parse the addresses for later
333 // consumption.
334 MachO::linkedit_data_command LLC =
335 MachOObj->getLinkeditDataLoadCommand(Command);
336
337 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
338 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
339 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
340 StringRef SegName = SLC.segname;
341 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
342 BaseSegmentAddressSet = true;
343 BaseSegmentAddress = SLC.vmaddr;
344 }
345 }
346 }
347}
348
349static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
350 uint32_t n, uint32_t count,
351 uint32_t stride, uint64_t addr) {
352 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
353 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
354 if (n > nindirectsyms)
355 outs() << " (entries start past the end of the indirect symbol "
356 "table) (reserved1 field greater than the table size)";
357 else if (n + count > nindirectsyms)
358 outs() << " (entries extends past the end of the indirect symbol "
359 "table)";
360 outs() << "\n";
361 uint32_t cputype = O->getHeader().cputype;
362 if (cputype & MachO::CPU_ARCH_ABI64)
363 outs() << "address index";
364 else
365 outs() << "address index";
366 if (verbose)
367 outs() << " name\n";
368 else
369 outs() << "\n";
370 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
371 if (cputype & MachO::CPU_ARCH_ABI64)
372 outs() << format("0x%016" PRIx64"l" "x", addr + j * stride) << " ";
373 else
374 outs() << format("0x%08" PRIx32"x", (uint32_t)addr + j * stride) << " ";
375 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
376 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
377 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
378 outs() << "LOCAL\n";
379 continue;
380 }
381 if (indirect_symbol ==
382 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
383 outs() << "LOCAL ABSOLUTE\n";
384 continue;
385 }
386 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
387 outs() << "ABSOLUTE\n";
388 continue;
389 }
390 outs() << format("%5u ", indirect_symbol);
391 if (verbose) {
392 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
393 if (indirect_symbol < Symtab.nsyms) {
394 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
395 SymbolRef Symbol = *Sym;
396 Expected<StringRef> SymName = Symbol.getName();
397 if (!SymName)
398 report_error(O->getFileName(), SymName.takeError());
399 outs() << *SymName;
400 } else {
401 outs() << "?";
402 }
403 }
404 outs() << "\n";
405 }
406}
407
408static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
409 for (const auto &Load : O->load_commands()) {
410 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
411 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
412 for (unsigned J = 0; J < Seg.nsects; ++J) {
413 MachO::section_64 Sec = O->getSection64(Load, J);
414 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
415 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
416 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
417 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
418 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
419 section_type == MachO::S_SYMBOL_STUBS) {
420 uint32_t stride;
421 if (section_type == MachO::S_SYMBOL_STUBS)
422 stride = Sec.reserved2;
423 else
424 stride = 8;
425 if (stride == 0) {
426 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
427 << Sec.sectname << ") "
428 << "(size of stubs in reserved2 field is zero)\n";
429 continue;
430 }
431 uint32_t count = Sec.size / stride;
432 outs() << "Indirect symbols for (" << Sec.segname << ","
433 << Sec.sectname << ") " << count << " entries";
434 uint32_t n = Sec.reserved1;
435 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
436 }
437 }
438 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
439 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
440 for (unsigned J = 0; J < Seg.nsects; ++J) {
441 MachO::section Sec = O->getSection(Load, J);
442 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
443 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
444 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
445 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
446 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
447 section_type == MachO::S_SYMBOL_STUBS) {
448 uint32_t stride;
449 if (section_type == MachO::S_SYMBOL_STUBS)
450 stride = Sec.reserved2;
451 else
452 stride = 4;
453 if (stride == 0) {
454 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
455 << Sec.sectname << ") "
456 << "(size of stubs in reserved2 field is zero)\n";
457 continue;
458 }
459 uint32_t count = Sec.size / stride;
460 outs() << "Indirect symbols for (" << Sec.segname << ","
461 << Sec.sectname << ") " << count << " entries";
462 uint32_t n = Sec.reserved1;
463 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
464 }
465 }
466 }
467 }
468}
469
470static void PrintRType(const uint64_t cputype, const unsigned r_type) {
471 static char const *generic_r_types[] = {
472 "VANILLA ", "PAIR ", "SECTDIF ", "PBLAPTR ", "LOCSDIF ", "TLV ",
473 " 6 (?) ", " 7 (?) ", " 8 (?) ", " 9 (?) ", " 10 (?) ", " 11 (?) ",
474 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
475 };
476 static char const *x86_64_r_types[] = {
477 "UNSIGND ", "SIGNED ", "BRANCH ", "GOT_LD ", "GOT ", "SUB ",
478 "SIGNED1 ", "SIGNED2 ", "SIGNED4 ", "TLV ", " 10 (?) ", " 11 (?) ",
479 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
480 };
481 static char const *arm_r_types[] = {
482 "VANILLA ", "PAIR ", "SECTDIFF", "LOCSDIF ", "PBLAPTR ",
483 "BR24 ", "T_BR22 ", "T_BR32 ", "HALF ", "HALFDIF ",
484 " 10 (?) ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
485 };
486 static char const *arm64_r_types[] = {
487 "UNSIGND ", "SUB ", "BR26 ", "PAGE21 ", "PAGOF12 ",
488 "GOTLDP ", "GOTLDPOF", "PTRTGOT ", "TLVLDP ", "TLVLDPOF",
489 "ADDEND ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
490 };
491
492 if (r_type > 0xf){
493 outs() << format("%-7u", r_type) << " ";
494 return;
495 }
496 switch (cputype) {
497 case MachO::CPU_TYPE_I386:
498 outs() << generic_r_types[r_type];
499 break;
500 case MachO::CPU_TYPE_X86_64:
501 outs() << x86_64_r_types[r_type];
502 break;
503 case MachO::CPU_TYPE_ARM:
504 outs() << arm_r_types[r_type];
505 break;
506 case MachO::CPU_TYPE_ARM64:
507 outs() << arm64_r_types[r_type];
508 break;
509 default:
510 outs() << format("%-7u ", r_type);
511 }
512}
513
514static void PrintRLength(const uint64_t cputype, const unsigned r_type,
515 const unsigned r_length, const bool previous_arm_half){
516 if (cputype == MachO::CPU_TYPE_ARM &&
517 (r_type == llvm::MachO::ARM_RELOC_HALF ||
518 r_type == llvm::MachO::ARM_RELOC_HALF_SECTDIFF ||
519 previous_arm_half == true)) {
520 if ((r_length & 0x1) == 0)
521 outs() << "lo/";
522 else
523 outs() << "hi/";
524 if ((r_length & 0x1) == 0)
525 outs() << "arm ";
526 else
527 outs() << "thm ";
528 } else {
529 switch (r_length) {
530 case 0:
531 outs() << "byte ";
532 break;
533 case 1:
534 outs() << "word ";
535 break;
536 case 2:
537 outs() << "long ";
538 break;
539 case 3:
540 if (cputype == MachO::CPU_TYPE_X86_64)
541 outs() << "quad ";
542 else
543 outs() << format("?(%2d) ", r_length);
544 break;
545 default:
546 outs() << format("?(%2d) ", r_length);
547 }
548 }
549}
550
551static void PrintRelocationEntries(const MachOObjectFile *O,
552 const relocation_iterator Begin,
553 const relocation_iterator End,
554 const uint64_t cputype,
555 const bool verbose) {
556 const MachO::symtab_command Symtab = O->getSymtabLoadCommand();
557 bool previous_arm_half = false;
558 bool previous_sectdiff = false;
559 uint32_t sectdiff_r_type = 0;
560
561 for (relocation_iterator Reloc = Begin; Reloc != End; ++Reloc) {
562 const DataRefImpl Rel = Reloc->getRawDataRefImpl();
563 const MachO::any_relocation_info RE = O->getRelocation(Rel);
564 const unsigned r_type = O->getAnyRelocationType(RE);
565 const bool r_scattered = O->isRelocationScattered(RE);
566 const unsigned r_pcrel = O->getAnyRelocationPCRel(RE);
567 const unsigned r_length = O->getAnyRelocationLength(RE);
568 const unsigned r_address = O->getAnyRelocationAddress(RE);
569 const bool r_extern = (r_scattered ? false :
570 O->getPlainRelocationExternal(RE));
571 const uint32_t r_value = (r_scattered ?
572 O->getScatteredRelocationValue(RE) : 0);
573 const unsigned r_symbolnum = (r_scattered ? 0 :
574 O->getPlainRelocationSymbolNum(RE));
575
576 if (r_scattered && cputype != MachO::CPU_TYPE_X86_64) {
577 if (verbose) {
578 // scattered: address
579 if ((cputype == MachO::CPU_TYPE_I386 &&
580 r_type == llvm::MachO::GENERIC_RELOC_PAIR) ||
581 (cputype == MachO::CPU_TYPE_ARM &&
582 r_type == llvm::MachO::ARM_RELOC_PAIR))
583 outs() << " ";
584 else
585 outs() << format("%08x ", (unsigned int)r_address);
586
587 // scattered: pcrel
588 if (r_pcrel)
589 outs() << "True ";
590 else
591 outs() << "False ";
592
593 // scattered: length
594 PrintRLength(cputype, r_type, r_length, previous_arm_half);
595
596 // scattered: extern & type
597 outs() << "n/a ";
598 PrintRType(cputype, r_type);
599
600 // scattered: scattered & value
601 outs() << format("True 0x%08x", (unsigned int)r_value);
602 if (previous_sectdiff == false) {
603 if ((cputype == MachO::CPU_TYPE_ARM &&
604 r_type == llvm::MachO::ARM_RELOC_PAIR))
605 outs() << format(" half = 0x%04x ", (unsigned int)r_address);
606 }
607 else if (cputype == MachO::CPU_TYPE_ARM &&
608 sectdiff_r_type == llvm::MachO::ARM_RELOC_HALF_SECTDIFF)
609 outs() << format(" other_half = 0x%04x ", (unsigned int)r_address);
610 if ((cputype == MachO::CPU_TYPE_I386 &&
611 (r_type == llvm::MachO::GENERIC_RELOC_SECTDIFF ||
612 r_type == llvm::MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) ||
613 (cputype == MachO::CPU_TYPE_ARM &&
614 (sectdiff_r_type == llvm::MachO::ARM_RELOC_SECTDIFF ||
615 sectdiff_r_type == llvm::MachO::ARM_RELOC_LOCAL_SECTDIFF ||
616 sectdiff_r_type == llvm::MachO::ARM_RELOC_HALF_SECTDIFF))) {
617 previous_sectdiff = true;
618 sectdiff_r_type = r_type;
619 }
620 else {
621 previous_sectdiff = false;
622 sectdiff_r_type = 0;
623 }
624 if (cputype == MachO::CPU_TYPE_ARM &&
625 (r_type == llvm::MachO::ARM_RELOC_HALF ||
626 r_type == llvm::MachO::ARM_RELOC_HALF_SECTDIFF))
627 previous_arm_half = true;
628 else
629 previous_arm_half = false;
630 outs() << "\n";
631 }
632 else {
633 // scattered: address pcrel length extern type scattered value
634 outs() << format("%08x %1d %-2d n/a %-7d 1 0x%08x\n",
635 (unsigned int)r_address, r_pcrel, r_length, r_type,
636 (unsigned int)r_value);
637 }
638 }
639 else {
640 if (verbose) {
641 // plain: address
642 if (cputype == MachO::CPU_TYPE_ARM &&
643 r_type == llvm::MachO::ARM_RELOC_PAIR)
644 outs() << " ";
645 else
646 outs() << format("%08x ", (unsigned int)r_address);
647
648 // plain: pcrel
649 if (r_pcrel)
650 outs() << "True ";
651 else
652 outs() << "False ";
653
654 // plain: length
655 PrintRLength(cputype, r_type, r_length, previous_arm_half);
656
657 if (r_extern) {
658 // plain: extern & type & scattered
659 outs() << "True ";
660 PrintRType(cputype, r_type);
661 outs() << "False ";
662
663 // plain: symbolnum/value
664 if (r_symbolnum > Symtab.nsyms)
665 outs() << format("?(%d)\n", r_symbolnum);
666 else {
667 SymbolRef Symbol = *O->getSymbolByIndex(r_symbolnum);
668 Expected<StringRef> SymNameNext = Symbol.getName();
669 const char *name = NULL__null;
670 if (SymNameNext)
671 name = SymNameNext->data();
672 if (name == NULL__null)
673 outs() << format("?(%d)\n", r_symbolnum);
674 else
675 outs() << name << "\n";
676 }
677 }
678 else {
679 // plain: extern & type & scattered
680 outs() << "False ";
681 PrintRType(cputype, r_type);
682 outs() << "False ";
683
684 // plain: symbolnum/value
685 if (cputype == MachO::CPU_TYPE_ARM &&
686 r_type == llvm::MachO::ARM_RELOC_PAIR)
687 outs() << format("other_half = 0x%04x\n", (unsigned int)r_address);
688 else if (cputype == MachO::CPU_TYPE_ARM64 &&
689 r_type == llvm::MachO::ARM64_RELOC_ADDEND)
690 outs() << format("addend = 0x%06x\n", (unsigned int)r_symbolnum);
691 else {
692 outs() << format("%d ", r_symbolnum);
693 if (r_symbolnum == llvm::MachO::R_ABS)
694 outs() << "R_ABS\n";
695 else {
696 // in this case, r_symbolnum is actually a 1-based section number
697 uint32_t nsects = O->section_end()->getRawDataRefImpl().d.a;
698 if (r_symbolnum > 0 && r_symbolnum <= nsects) {
699 llvm::object::DataRefImpl DRI;
700 DRI.d.a = r_symbolnum-1;
701 StringRef SegName = O->getSectionFinalSegmentName(DRI);
702 StringRef SectName;
703 if (O->getSectionName(DRI, SectName))
704 outs() << "(?,?)\n";
705 else
706 outs() << "(" << SegName << "," << SectName << ")\n";
707 }
708 else {
709 outs() << "(?,?)\n";
710 }
711 }
712 }
713 }
714 if (cputype == MachO::CPU_TYPE_ARM &&
715 (r_type == llvm::MachO::ARM_RELOC_HALF ||
716 r_type == llvm::MachO::ARM_RELOC_HALF_SECTDIFF))
717 previous_arm_half = true;
718 else
719 previous_arm_half = false;
720 }
721 else {
722 // plain: address pcrel length extern type scattered symbolnum/section
723 outs() << format("%08x %1d %-2d %1d %-7d 0 %d\n",
724 (unsigned int)r_address, r_pcrel, r_length, r_extern,
725 r_type, r_symbolnum);
726 }
727 }
728 }
729}
730
731static void PrintRelocations(const MachOObjectFile *O, const bool verbose) {
732 const uint64_t cputype = O->getHeader().cputype;
733 const MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
734 if (Dysymtab.nextrel != 0) {
735 outs() << "External relocation information " << Dysymtab.nextrel
736 << " entries";
737 outs() << "\naddress pcrel length extern type scattered "
738 "symbolnum/value\n";
739 PrintRelocationEntries(O, O->extrel_begin(), O->extrel_end(), cputype,
740 verbose);
741 }
742 if (Dysymtab.nlocrel != 0) {
743 outs() << format("Local relocation information %u entries",
744 Dysymtab.nlocrel);
745 outs() << "\naddress pcrel length extern type scattered "
746 "symbolnum/value\n";
747 PrintRelocationEntries(O, O->locrel_begin(), O->locrel_end(), cputype,
748 verbose);
749 }
750 for (const auto &Load : O->load_commands()) {
751 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
752 const MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
753 for (unsigned J = 0; J < Seg.nsects; ++J) {
754 const MachO::section_64 Sec = O->getSection64(Load, J);
755 if (Sec.nreloc != 0) {
756 DataRefImpl DRI;
757 DRI.d.a = J;
758 const StringRef SegName = O->getSectionFinalSegmentName(DRI);
759 StringRef SectName;
760 if (O->getSectionName(DRI, SectName))
761 outs() << "Relocation information (" << SegName << ",?) "
762 << format("%u entries", Sec.nreloc);
763 else
764 outs() << "Relocation information (" << SegName << ","
765 << SectName << format(") %u entries", Sec.nreloc);
766 outs() << "\naddress pcrel length extern type scattered "
767 "symbolnum/value\n";
768 PrintRelocationEntries(O, O->section_rel_begin(DRI),
769 O->section_rel_end(DRI), cputype, verbose);
770 }
771 }
772 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
773 const MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
774 for (unsigned J = 0; J < Seg.nsects; ++J) {
775 const MachO::section Sec = O->getSection(Load, J);
776 if (Sec.nreloc != 0) {
777 DataRefImpl DRI;
778 DRI.d.a = J;
779 const StringRef SegName = O->getSectionFinalSegmentName(DRI);
780 StringRef SectName;
781 if (O->getSectionName(DRI, SectName))
782 outs() << "Relocation information (" << SegName << ",?) "
783 << format("%u entries", Sec.nreloc);
784 else
785 outs() << "Relocation information (" << SegName << ","
786 << SectName << format(") %u entries", Sec.nreloc);
787 outs() << "\naddress pcrel length extern type scattered "
788 "symbolnum/value\n";
789 PrintRelocationEntries(O, O->section_rel_begin(DRI),
790 O->section_rel_end(DRI), cputype, verbose);
791 }
792 }
793 }
794 }
795}
796
797static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
798 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
799 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
800 outs() << "Data in code table (" << nentries << " entries)\n";
801 outs() << "offset length kind\n";
802 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
803 ++DI) {
804 uint32_t Offset;
805 DI->getOffset(Offset);
806 outs() << format("0x%08" PRIx32"x", Offset) << " ";
807 uint16_t Length;
808 DI->getLength(Length);
809 outs() << format("%6u", Length) << " ";
810 uint16_t Kind;
811 DI->getKind(Kind);
812 if (verbose) {
813 switch (Kind) {
814 case MachO::DICE_KIND_DATA:
815 outs() << "DATA";
816 break;
817 case MachO::DICE_KIND_JUMP_TABLE8:
818 outs() << "JUMP_TABLE8";
819 break;
820 case MachO::DICE_KIND_JUMP_TABLE16:
821 outs() << "JUMP_TABLE16";
822 break;
823 case MachO::DICE_KIND_JUMP_TABLE32:
824 outs() << "JUMP_TABLE32";
825 break;
826 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
827 outs() << "ABS_JUMP_TABLE32";
828 break;
829 default:
830 outs() << format("0x%04" PRIx32"x", Kind);
831 break;
832 }
833 } else
834 outs() << format("0x%04" PRIx32"x", Kind);
835 outs() << "\n";
836 }
837}
838
839static void PrintLinkOptHints(MachOObjectFile *O) {
840 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
841 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
842 uint32_t nloh = LohLC.datasize;
843 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
844 for (uint32_t i = 0; i < nloh;) {
845 unsigned n;
846 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
847 i += n;
848 outs() << " identifier " << identifier << " ";
849 if (i >= nloh)
850 return;
851 switch (identifier) {
852 case 1:
853 outs() << "AdrpAdrp\n";
854 break;
855 case 2:
856 outs() << "AdrpLdr\n";
857 break;
858 case 3:
859 outs() << "AdrpAddLdr\n";
860 break;
861 case 4:
862 outs() << "AdrpLdrGotLdr\n";
863 break;
864 case 5:
865 outs() << "AdrpAddStr\n";
866 break;
867 case 6:
868 outs() << "AdrpLdrGotStr\n";
869 break;
870 case 7:
871 outs() << "AdrpAdd\n";
872 break;
873 case 8:
874 outs() << "AdrpLdrGot\n";
875 break;
876 default:
877 outs() << "Unknown identifier value\n";
878 break;
879 }
880 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
881 i += n;
882 outs() << " narguments " << narguments << "\n";
883 if (i >= nloh)
884 return;
885
886 for (uint32_t j = 0; j < narguments; j++) {
887 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
888 i += n;
889 outs() << "\tvalue " << format("0x%" PRIx64"l" "x", value) << "\n";
890 if (i >= nloh)
891 return;
892 }
893 }
894}
895
896static void PrintDylibs(MachOObjectFile *O, bool JustId) {
897 unsigned Index = 0;
898 for (const auto &Load : O->load_commands()) {
899 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
900 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
901 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
902 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
903 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
904 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
905 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
906 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
907 if (dl.dylib.name < dl.cmdsize) {
908 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
909 if (JustId)
910 outs() << p << "\n";
911 else {
912 outs() << "\t" << p;
913 outs() << " (compatibility version "
914 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
915 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
916 << (dl.dylib.compatibility_version & 0xff) << ",";
917 outs() << " current version "
918 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
919 << ((dl.dylib.current_version >> 8) & 0xff) << "."
920 << (dl.dylib.current_version & 0xff) << ")\n";
921 }
922 } else {
923 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
924 if (Load.C.cmd == MachO::LC_ID_DYLIB)
925 outs() << "LC_ID_DYLIB ";
926 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
927 outs() << "LC_LOAD_DYLIB ";
928 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
929 outs() << "LC_LOAD_WEAK_DYLIB ";
930 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
931 outs() << "LC_LAZY_LOAD_DYLIB ";
932 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
933 outs() << "LC_REEXPORT_DYLIB ";
934 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
935 outs() << "LC_LOAD_UPWARD_DYLIB ";
936 else
937 outs() << "LC_??? ";
938 outs() << "command " << Index++ << "\n";
939 }
940 }
941 }
942}
943
944typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
945
946static void CreateSymbolAddressMap(MachOObjectFile *O,
947 SymbolAddressMap *AddrMap) {
948 // Create a map of symbol addresses to symbol names.
949 for (const SymbolRef &Symbol : O->symbols()) {
950 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
951 if (!STOrErr)
952 report_error(O->getFileName(), STOrErr.takeError());
953 SymbolRef::Type ST = *STOrErr;
954 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
955 ST == SymbolRef::ST_Other) {
956 uint64_t Address = Symbol.getValue();
957 Expected<StringRef> SymNameOrErr = Symbol.getName();
958 if (!SymNameOrErr)
959 report_error(O->getFileName(), SymNameOrErr.takeError());
960 StringRef SymName = *SymNameOrErr;
961 if (!SymName.startswith(".objc"))
962 (*AddrMap)[Address] = SymName;
963 }
964 }
965}
966
967// GuessSymbolName is passed the address of what might be a symbol and a
968// pointer to the SymbolAddressMap. It returns the name of a symbol
969// with that address or nullptr if no symbol is found with that address.
970static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
971 const char *SymbolName = nullptr;
972 // A DenseMap can't lookup up some values.
973 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
974 StringRef name = AddrMap->lookup(value);
975 if (!name.empty())
976 SymbolName = name.data();
977 }
978 return SymbolName;
979}
980
981static void DumpCstringChar(const char c) {
982 char p[2];
983 p[0] = c;
984 p[1] = '\0';
985 outs().write_escaped(p);
986}
987
988static void DumpCstringSection(MachOObjectFile *O, const char *sect,
989 uint32_t sect_size, uint64_t sect_addr,
990 bool print_addresses) {
991 for (uint32_t i = 0; i < sect_size; i++) {
992 if (print_addresses) {
993 if (O->is64Bit())
994 outs() << format("%016" PRIx64"l" "x", sect_addr + i) << " ";
995 else
996 outs() << format("%08" PRIx64"l" "x", sect_addr + i) << " ";
997 }
998 for (; i < sect_size && sect[i] != '\0'; i++)
999 DumpCstringChar(sect[i]);
1000 if (i < sect_size && sect[i] == '\0')
1001 outs() << "\n";
1002 }
1003}
1004
1005static void DumpLiteral4(uint32_t l, float f) {
1006 outs() << format("0x%08" PRIx32"x", l);
1007 if ((l & 0x7f800000) != 0x7f800000)
1008 outs() << format(" (%.16e)\n", f);
1009 else {
1010 if (l == 0x7f800000)
1011 outs() << " (+Infinity)\n";
1012 else if (l == 0xff800000)
1013 outs() << " (-Infinity)\n";
1014 else if ((l & 0x00400000) == 0x00400000)
1015 outs() << " (non-signaling Not-a-Number)\n";
1016 else
1017 outs() << " (signaling Not-a-Number)\n";
1018 }
1019}
1020
1021static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
1022 uint32_t sect_size, uint64_t sect_addr,
1023 bool print_addresses) {
1024 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
1025 if (print_addresses) {
1026 if (O->is64Bit())
1027 outs() << format("%016" PRIx64"l" "x", sect_addr + i) << " ";
1028 else
1029 outs() << format("%08" PRIx64"l" "x", sect_addr + i) << " ";
1030 }
1031 float f;
1032 memcpy(&f, sect + i, sizeof(float));
1033 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1034 sys::swapByteOrder(f);
1035 uint32_t l;
1036 memcpy(&l, sect + i, sizeof(uint32_t));
1037 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1038 sys::swapByteOrder(l);
1039 DumpLiteral4(l, f);
1040 }
1041}
1042
1043static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
1044 double d) {
1045 outs() << format("0x%08" PRIx32"x", l0) << " " << format("0x%08" PRIx32"x", l1);
1046 uint32_t Hi, Lo;
1047 Hi = (O->isLittleEndian()) ? l1 : l0;
1048 Lo = (O->isLittleEndian()) ? l0 : l1;
1049
1050 // Hi is the high word, so this is equivalent to if(isfinite(d))
1051 if ((Hi & 0x7ff00000) != 0x7ff00000)
1052 outs() << format(" (%.16e)\n", d);
1053 else {
1054 if (Hi == 0x7ff00000 && Lo == 0)
1055 outs() << " (+Infinity)\n";
1056 else if (Hi == 0xfff00000 && Lo == 0)
1057 outs() << " (-Infinity)\n";
1058 else if ((Hi & 0x00080000) == 0x00080000)
1059 outs() << " (non-signaling Not-a-Number)\n";
1060 else
1061 outs() << " (signaling Not-a-Number)\n";
1062 }
1063}
1064
1065static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
1066 uint32_t sect_size, uint64_t sect_addr,
1067 bool print_addresses) {
1068 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
1069 if (print_addresses) {
1070 if (O->is64Bit())
1071 outs() << format("%016" PRIx64"l" "x", sect_addr + i) << " ";
1072 else
1073 outs() << format("%08" PRIx64"l" "x", sect_addr + i) << " ";
1074 }
1075 double d;
1076 memcpy(&d, sect + i, sizeof(double));
1077 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1078 sys::swapByteOrder(d);
1079 uint32_t l0, l1;
1080 memcpy(&l0, sect + i, sizeof(uint32_t));
1081 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
1082 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1083 sys::swapByteOrder(l0);
1084 sys::swapByteOrder(l1);
1085 }
1086 DumpLiteral8(O, l0, l1, d);
1087 }
1088}
1089
1090static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
1091 outs() << format("0x%08" PRIx32"x", l0) << " ";
1092 outs() << format("0x%08" PRIx32"x", l1) << " ";
1093 outs() << format("0x%08" PRIx32"x", l2) << " ";
1094 outs() << format("0x%08" PRIx32"x", l3) << "\n";
1095}
1096
1097static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
1098 uint32_t sect_size, uint64_t sect_addr,
1099 bool print_addresses) {
1100 for (uint32_t i = 0; i < sect_size; i += 16) {
1101 if (print_addresses) {
1102 if (O->is64Bit())
1103 outs() << format("%016" PRIx64"l" "x", sect_addr + i) << " ";
1104 else
1105 outs() << format("%08" PRIx64"l" "x", sect_addr + i) << " ";
1106 }
1107 uint32_t l0, l1, l2, l3;
1108 memcpy(&l0, sect + i, sizeof(uint32_t));
1109 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
1110 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
1111 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
1112 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1113 sys::swapByteOrder(l0);
1114 sys::swapByteOrder(l1);
1115 sys::swapByteOrder(l2);
1116 sys::swapByteOrder(l3);
1117 }
1118 DumpLiteral16(l0, l1, l2, l3);
1119 }
1120}
1121
1122static void DumpLiteralPointerSection(MachOObjectFile *O,
1123 const SectionRef &Section,
1124 const char *sect, uint32_t sect_size,
1125 uint64_t sect_addr,
1126 bool print_addresses) {
1127 // Collect the literal sections in this Mach-O file.
1128 std::vector<SectionRef> LiteralSections;
1129 for (const SectionRef &Section : O->sections()) {
1130 DataRefImpl Ref = Section.getRawDataRefImpl();
1131 uint32_t section_type;
1132 if (O->is64Bit()) {
1133 const MachO::section_64 Sec = O->getSection64(Ref);
1134 section_type = Sec.flags & MachO::SECTION_TYPE;
1135 } else {
1136 const MachO::section Sec = O->getSection(Ref);
1137 section_type = Sec.flags & MachO::SECTION_TYPE;
1138 }
1139 if (section_type == MachO::S_CSTRING_LITERALS ||
1140 section_type == MachO::S_4BYTE_LITERALS ||
1141 section_type == MachO::S_8BYTE_LITERALS ||
1142 section_type == MachO::S_16BYTE_LITERALS)
1143 LiteralSections.push_back(Section);
1144 }
1145
1146 // Set the size of the literal pointer.
1147 uint32_t lp_size = O->is64Bit() ? 8 : 4;
1148
1149 // Collect the external relocation symbols for the literal pointers.
1150 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
1151 for (const RelocationRef &Reloc : Section.relocations()) {
1152 DataRefImpl Rel;
1153 MachO::any_relocation_info RE;
1154 bool isExtern = false;
1155 Rel = Reloc.getRawDataRefImpl();
1156 RE = O->getRelocation(Rel);
1157 isExtern = O->getPlainRelocationExternal(RE);
1158 if (isExtern) {
1159 uint64_t RelocOffset = Reloc.getOffset();
1160 symbol_iterator RelocSym = Reloc.getSymbol();
1161 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
1162 }
1163 }
1164 array_pod_sort(Relocs.begin(), Relocs.end());
1165
1166 // Dump each literal pointer.
1167 for (uint32_t i = 0; i < sect_size; i += lp_size) {
1168 if (print_addresses) {
1169 if (O->is64Bit())
1170 outs() << format("%016" PRIx64"l" "x", sect_addr + i) << " ";
1171 else
1172 outs() << format("%08" PRIx64"l" "x", sect_addr + i) << " ";
1173 }
1174 uint64_t lp;
1175 if (O->is64Bit()) {
1176 memcpy(&lp, sect + i, sizeof(uint64_t));
1177 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1178 sys::swapByteOrder(lp);
1179 } else {
1180 uint32_t li;
1181 memcpy(&li, sect + i, sizeof(uint32_t));
1182 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1183 sys::swapByteOrder(li);
1184 lp = li;
1185 }
1186
1187 // First look for an external relocation entry for this literal pointer.
1188 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
1189 return P.first == i;
1190 });
1191 if (Reloc != Relocs.end()) {
1192 symbol_iterator RelocSym = Reloc->second;
1193 Expected<StringRef> SymName = RelocSym->getName();
1194 if (!SymName)
1195 report_error(O->getFileName(), SymName.takeError());
1196 outs() << "external relocation entry for symbol:" << *SymName << "\n";
1197 continue;
1198 }
1199
1200 // For local references see what the section the literal pointer points to.
1201 auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
1202 return lp >= R.getAddress() && lp < R.getAddress() + R.getSize();
1203 });
1204 if (Sect == LiteralSections.end()) {
1205 outs() << format("0x%" PRIx64"l" "x", lp) << " (not in a literal section)\n";
1206 continue;
1207 }
1208
1209 uint64_t SectAddress = Sect->getAddress();
1210 uint64_t SectSize = Sect->getSize();
1211
1212 StringRef SectName;
1213 Sect->getName(SectName);
1214 DataRefImpl Ref = Sect->getRawDataRefImpl();
1215 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
1216 outs() << SegmentName << ":" << SectName << ":";
1217
1218 uint32_t section_type;
1219 if (O->is64Bit()) {
1220 const MachO::section_64 Sec = O->getSection64(Ref);
1221 section_type = Sec.flags & MachO::SECTION_TYPE;
1222 } else {
1223 const MachO::section Sec = O->getSection(Ref);
1224 section_type = Sec.flags & MachO::SECTION_TYPE;
1225 }
1226
1227 StringRef BytesStr;
1228 Sect->getContents(BytesStr);
1229 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
1230
1231 switch (section_type) {
1232 case MachO::S_CSTRING_LITERALS:
1233 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
1234 i++) {
1235 DumpCstringChar(Contents[i]);
1236 }
1237 outs() << "\n";
1238 break;
1239 case MachO::S_4BYTE_LITERALS:
1240 float f;
1241 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
1242 uint32_t l;
1243 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
1244 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1245 sys::swapByteOrder(f);
1246 sys::swapByteOrder(l);
1247 }
1248 DumpLiteral4(l, f);
1249 break;
1250 case MachO::S_8BYTE_LITERALS: {
1251 double d;
1252 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
1253 uint32_t l0, l1;
1254 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
1255 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
1256 sizeof(uint32_t));
1257 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1258 sys::swapByteOrder(f);
1259 sys::swapByteOrder(l0);
1260 sys::swapByteOrder(l1);
1261 }
1262 DumpLiteral8(O, l0, l1, d);
1263 break;
1264 }
1265 case MachO::S_16BYTE_LITERALS: {
1266 uint32_t l0, l1, l2, l3;
1267 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
1268 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
1269 sizeof(uint32_t));
1270 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
1271 sizeof(uint32_t));
1272 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
1273 sizeof(uint32_t));
1274 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1275 sys::swapByteOrder(l0);
1276 sys::swapByteOrder(l1);
1277 sys::swapByteOrder(l2);
1278 sys::swapByteOrder(l3);
1279 }
1280 DumpLiteral16(l0, l1, l2, l3);
1281 break;
1282 }
1283 }
1284 }
1285}
1286
1287static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
1288 uint32_t sect_size, uint64_t sect_addr,
1289 SymbolAddressMap *AddrMap,
1290 bool verbose) {
1291 uint32_t stride;
1292 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
1293 for (uint32_t i = 0; i < sect_size; i += stride) {
1294 const char *SymbolName = nullptr;
1295 if (O->is64Bit()) {
1296 outs() << format("0x%016" PRIx64"l" "x", sect_addr + i * stride) << " ";
1297 uint64_t pointer_value;
1298 memcpy(&pointer_value, sect + i, stride);
1299 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1300 sys::swapByteOrder(pointer_value);
1301 outs() << format("0x%016" PRIx64"l" "x", pointer_value);
1302 if (verbose)
1303 SymbolName = GuessSymbolName(pointer_value, AddrMap);
1304 } else {
1305 outs() << format("0x%08" PRIx64"l" "x", sect_addr + i * stride) << " ";
1306 uint32_t pointer_value;
1307 memcpy(&pointer_value, sect + i, stride);
1308 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1309 sys::swapByteOrder(pointer_value);
1310 outs() << format("0x%08" PRIx32"x", pointer_value);
1311 if (verbose)
1312 SymbolName = GuessSymbolName(pointer_value, AddrMap);
1313 }
1314 if (SymbolName)
1315 outs() << " " << SymbolName;
1316 outs() << "\n";
1317 }
1318}
1319
1320static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
1321 uint32_t size, uint64_t addr) {
1322 uint32_t cputype = O->getHeader().cputype;
1323 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
1324 uint32_t j;
1325 for (uint32_t i = 0; i < size; i += j, addr += j) {
1326 if (O->is64Bit())
1327 outs() << format("%016" PRIx64"l" "x", addr) << "\t";
1328 else
1329 outs() << format("%08" PRIx64"l" "x", addr) << "\t";
1330 for (j = 0; j < 16 && i + j < size; j++) {
1331 uint8_t byte_word = *(sect + i + j);
1332 outs() << format("%02" PRIx32"x", (uint32_t)byte_word) << " ";
1333 }
1334 outs() << "\n";
1335 }
1336 } else {
1337 uint32_t j;
1338 for (uint32_t i = 0; i < size; i += j, addr += j) {
1339 if (O->is64Bit())
1340 outs() << format("%016" PRIx64"l" "x", addr) << "\t";
1341 else
1342 outs() << format("%08" PRIx64"l" "x", addr) << "\t";
1343 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
1344 j += sizeof(int32_t)) {
1345 if (i + j + sizeof(int32_t) <= size) {
1346 uint32_t long_word;
1347 memcpy(&long_word, sect + i + j, sizeof(int32_t));
1348 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1349 sys::swapByteOrder(long_word);
1350 outs() << format("%08" PRIx32"x", long_word) << " ";
1351 } else {
1352 for (uint32_t k = 0; i + j + k < size; k++) {
1353 uint8_t byte_word = *(sect + i + j + k);
1354 outs() << format("%02" PRIx32"x", (uint32_t)byte_word) << " ";
1355 }
1356 }
1357 }
1358 outs() << "\n";
1359 }
1360 }
1361}
1362
1363static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1364 StringRef DisSegName, StringRef DisSectName);
1365static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1366 uint32_t size, uint32_t addr);
1367#ifdef HAVE_LIBXAR
1368static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
1369 uint32_t size, bool verbose,
1370 bool PrintXarHeader, bool PrintXarFileHeaders,
1371 std::string XarMemberName);
1372#endif // defined(HAVE_LIBXAR)
1373
1374static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1375 bool verbose) {
1376 SymbolAddressMap AddrMap;
1377 if (verbose)
1378 CreateSymbolAddressMap(O, &AddrMap);
1379
1380 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1381 StringRef DumpSection = FilterSections[i];
1382 std::pair<StringRef, StringRef> DumpSegSectName;
1383 DumpSegSectName = DumpSection.split(',');
1384 StringRef DumpSegName, DumpSectName;
1385 if (DumpSegSectName.second.size()) {
1386 DumpSegName = DumpSegSectName.first;
1387 DumpSectName = DumpSegSectName.second;
1388 } else {
1389 DumpSegName = "";
1390 DumpSectName = DumpSegSectName.first;
1391 }
1392 for (const SectionRef &Section : O->sections()) {
1393 StringRef SectName;
1394 Section.getName(SectName);
1395 DataRefImpl Ref = Section.getRawDataRefImpl();
1396 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1397 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1398 (SectName == DumpSectName)) {
1399
1400 uint32_t section_flags;
1401 if (O->is64Bit()) {
1402 const MachO::section_64 Sec = O->getSection64(Ref);
1403 section_flags = Sec.flags;
1404
1405 } else {
1406 const MachO::section Sec = O->getSection(Ref);
1407 section_flags = Sec.flags;
1408 }
1409 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1410
1411 StringRef BytesStr;
1412 Section.getContents(BytesStr);
1413 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1414 uint32_t sect_size = BytesStr.size();
1415 uint64_t sect_addr = Section.getAddress();
1416
1417 outs() << "Contents of (" << SegName << "," << SectName
1418 << ") section\n";
1419
1420 if (verbose) {
1421 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1422 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1423 DisassembleMachO(Filename, O, SegName, SectName);
1424 continue;
1425 }
1426 if (SegName == "__TEXT" && SectName == "__info_plist") {
1427 outs() << sect;
1428 continue;
1429 }
1430 if (SegName == "__OBJC" && SectName == "__protocol") {
1431 DumpProtocolSection(O, sect, sect_size, sect_addr);
1432 continue;
1433 }
1434#ifdef HAVE_LIBXAR
1435 if (SegName == "__LLVM" && SectName == "__bundle") {
1436 DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
1437 ArchiveHeaders, "");
1438 continue;
1439 }
1440#endif // defined(HAVE_LIBXAR)
1441 switch (section_type) {
1442 case MachO::S_REGULAR:
1443 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1444 break;
1445 case MachO::S_ZEROFILL:
1446 outs() << "zerofill section and has no contents in the file\n";
1447 break;
1448 case MachO::S_CSTRING_LITERALS:
1449 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1450 break;
1451 case MachO::S_4BYTE_LITERALS:
1452 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1453 break;
1454 case MachO::S_8BYTE_LITERALS:
1455 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1456 break;
1457 case MachO::S_16BYTE_LITERALS:
1458 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1459 break;
1460 case MachO::S_LITERAL_POINTERS:
1461 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1462 !NoLeadingAddr);
1463 break;
1464 case MachO::S_MOD_INIT_FUNC_POINTERS:
1465 case MachO::S_MOD_TERM_FUNC_POINTERS:
1466 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1467 verbose);
1468 break;
1469 default:
1470 outs() << "Unknown section type ("
1471 << format("0x%08" PRIx32"x", section_type) << ")\n";
1472 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1473 break;
1474 }
1475 } else {
1476 if (section_type == MachO::S_ZEROFILL)
1477 outs() << "zerofill section and has no contents in the file\n";
1478 else
1479 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1480 }
1481 }
1482 }
1483 }
1484}
1485
1486static void DumpInfoPlistSectionContents(StringRef Filename,
1487 MachOObjectFile *O) {
1488 for (const SectionRef &Section : O->sections()) {
1489 StringRef SectName;
1490 Section.getName(SectName);
1491 DataRefImpl Ref = Section.getRawDataRefImpl();
1492 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1493 if (SegName == "__TEXT" && SectName == "__info_plist") {
1494 if (!NoLeadingHeaders)
1495 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1496 StringRef BytesStr;
1497 Section.getContents(BytesStr);
1498 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1499 outs() << format("%.*s", BytesStr.size(), sect) << "\n";
1500 return;
1501 }
1502 }
1503}
1504
1505// checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1506// and if it is and there is a list of architecture flags is specified then
1507// check to make sure this Mach-O file is one of those architectures or all
1508// architectures were specified. If not then an error is generated and this
1509// routine returns false. Else it returns true.
1510static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1511 auto *MachO = dyn_cast<MachOObjectFile>(O);
1512
1513 if (!MachO || ArchAll || ArchFlags.empty())
1514 return true;
1515
1516 MachO::mach_header H;
1517 MachO::mach_header_64 H_64;
1518 Triple T;
1519 const char *McpuDefault, *ArchFlag;
1520 if (MachO->is64Bit()) {
1521 H_64 = MachO->MachOObjectFile::getHeader64();
1522 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
1523 &McpuDefault, &ArchFlag);
1524 } else {
1525 H = MachO->MachOObjectFile::getHeader();
1526 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
1527 &McpuDefault, &ArchFlag);
1528 }
1529 const std::string ArchFlagName(ArchFlag);
1530 if (none_of(ArchFlags, [&](const std::string &Name) {
1531 return Name == ArchFlagName;
1532 })) {
1533 errs() << "llvm-objdump: " + Filename + ": No architecture specified.\n";
1534 return false;
1535 }
1536 return true;
1537}
1538
1539static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1540
1541// ProcessMachO() is passed a single opened Mach-O file, which may be an
1542// archive member and or in a slice of a universal file. It prints the
1543// the file name and header info and then processes it according to the
1544// command line options.
1545static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF,
1546 StringRef ArchiveMemberName = StringRef(),
1547 StringRef ArchitectureName = StringRef()) {
1548 // If we are doing some processing here on the Mach-O file print the header
1549 // info. And don't print it otherwise like in the case of printing the
1550 // UniversalHeaders or ArchiveHeaders.
1551 if (Disassemble || Relocations || PrivateHeaders || ExportsTrie || Rebase ||
1552 Bind || SymbolTable || LazyBind || WeakBind || IndirectSymbols ||
1553 DataInCode || LinkOptHints || DylibsUsed || DylibId || ObjcMetaData ||
1554 (FilterSections.size() != 0)) {
1555 if (!NoLeadingHeaders) {
1556 outs() << Name;
1557 if (!ArchiveMemberName.empty())
1558 outs() << '(' << ArchiveMemberName << ')';
1559 if (!ArchitectureName.empty())
1560 outs() << " (architecture " << ArchitectureName << ")";
1561 outs() << ":\n";
1562 }
1563 }
1564 // To use the report_error() form with an ArchiveName and FileName set
1565 // these up based on what is passed for Name and ArchiveMemberName.
1566 StringRef ArchiveName;
1567 StringRef FileName;
1568 if (!ArchiveMemberName.empty()) {
1569 ArchiveName = Name;
1570 FileName = ArchiveMemberName;
1571 } else {
1572 ArchiveName = StringRef();
1573 FileName = Name;
1574 }
1575
1576 // If we need the symbol table to do the operation then check it here to
1577 // produce a good error message as to where the Mach-O file comes from in
1578 // the error message.
1579 if (Disassemble || IndirectSymbols || FilterSections.size() != 0 ||
1580 UnwindInfo)
1581 if (Error Err = MachOOF->checkSymbolTable())
1582 report_error(ArchiveName, FileName, std::move(Err), ArchitectureName);
1583
1584 if (Disassemble) {
1585 if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE &&
1586 MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64)
1587 DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text");
1588 else
1589 DisassembleMachO(FileName, MachOOF, "__TEXT", "__text");
1590 }
1591 if (IndirectSymbols)
1592 PrintIndirectSymbols(MachOOF, !NonVerbose);
1593 if (DataInCode)
1594 PrintDataInCodeTable(MachOOF, !NonVerbose);
1595 if (LinkOptHints)
1596 PrintLinkOptHints(MachOOF);
1597 if (Relocations)
1598 PrintRelocations(MachOOF, !NonVerbose);
1599 if (SectionHeaders)
1600 PrintSectionHeaders(MachOOF);
1601 if (SectionContents)
1602 PrintSectionContents(MachOOF);
1603 if (FilterSections.size() != 0)
1604 DumpSectionContents(FileName, MachOOF, !NonVerbose);
1605 if (InfoPlist)
1606 DumpInfoPlistSectionContents(FileName, MachOOF);
1607 if (DylibsUsed)
1608 PrintDylibs(MachOOF, false);
1609 if (DylibId)
1610 PrintDylibs(MachOOF, true);
1611 if (SymbolTable)
1612 PrintSymbolTable(MachOOF, ArchiveName, ArchitectureName);
1613 if (UnwindInfo)
1614 printMachOUnwindInfo(MachOOF);
1615 if (PrivateHeaders) {
1616 printMachOFileHeader(MachOOF);
1617 printMachOLoadCommands(MachOOF);
1618 }
1619 if (FirstPrivateHeader)
1620 printMachOFileHeader(MachOOF);
1621 if (ObjcMetaData)
1622 printObjcMetaData(MachOOF, !NonVerbose);
1623 if (ExportsTrie)
1624 printExportsTrie(MachOOF);
1625 if (Rebase)
1626 printRebaseTable(MachOOF);
1627 if (Bind)
1628 printBindTable(MachOOF);
1629 if (LazyBind)
1630 printLazyBindTable(MachOOF);
1631 if (WeakBind)
1632 printWeakBindTable(MachOOF);
1633
1634 if (DwarfDumpType != DIDT_Null) {
1635 std::unique_ptr<DIContext> DICtx = DWARFContext::create(*MachOOF);
1636 // Dump the complete DWARF structure.
1637 DIDumpOptions DumpOpts;
1638 DumpOpts.DumpType = DwarfDumpType;
1639 DICtx->dump(outs(), DumpOpts);
1640 }
1641}
1642
1643// printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1644static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1645 outs() << " cputype (" << cputype << ")\n";
1646 outs() << " cpusubtype (" << cpusubtype << ")\n";
1647}
1648
1649// printCPUType() helps print_fat_headers by printing the cputype and
1650// pusubtype (symbolically for the one's it knows about).
1651static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1652 switch (cputype) {
1653 case MachO::CPU_TYPE_I386:
1654 switch (cpusubtype) {
1655 case MachO::CPU_SUBTYPE_I386_ALL:
1656 outs() << " cputype CPU_TYPE_I386\n";
1657 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1658 break;
1659 default:
1660 printUnknownCPUType(cputype, cpusubtype);
1661 break;
1662 }
1663 break;
1664 case MachO::CPU_TYPE_X86_64:
1665 switch (cpusubtype) {
1666 case MachO::CPU_SUBTYPE_X86_64_ALL:
1667 outs() << " cputype CPU_TYPE_X86_64\n";
1668 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1669 break;
1670 case MachO::CPU_SUBTYPE_X86_64_H:
1671 outs() << " cputype CPU_TYPE_X86_64\n";
1672 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1673 break;
1674 default:
1675 printUnknownCPUType(cputype, cpusubtype);
1676 break;
1677 }
1678 break;
1679 case MachO::CPU_TYPE_ARM:
1680 switch (cpusubtype) {
1681 case MachO::CPU_SUBTYPE_ARM_ALL:
1682 outs() << " cputype CPU_TYPE_ARM\n";
1683 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1684 break;
1685 case MachO::CPU_SUBTYPE_ARM_V4T:
1686 outs() << " cputype CPU_TYPE_ARM\n";
1687 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1688 break;
1689 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1690 outs() << " cputype CPU_TYPE_ARM\n";
1691 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1692 break;
1693 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1694 outs() << " cputype CPU_TYPE_ARM\n";
1695 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1696 break;
1697 case MachO::CPU_SUBTYPE_ARM_V6:
1698 outs() << " cputype CPU_TYPE_ARM\n";
1699 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1700 break;
1701 case MachO::CPU_SUBTYPE_ARM_V6M:
1702 outs() << " cputype CPU_TYPE_ARM\n";
1703 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1704 break;
1705 case MachO::CPU_SUBTYPE_ARM_V7:
1706 outs() << " cputype CPU_TYPE_ARM\n";
1707 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1708 break;
1709 case MachO::CPU_SUBTYPE_ARM_V7EM:
1710 outs() << " cputype CPU_TYPE_ARM\n";
1711 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1712 break;
1713 case MachO::CPU_SUBTYPE_ARM_V7K:
1714 outs() << " cputype CPU_TYPE_ARM\n";
1715 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1716 break;
1717 case MachO::CPU_SUBTYPE_ARM_V7M:
1718 outs() << " cputype CPU_TYPE_ARM\n";
1719 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1720 break;
1721 case MachO::CPU_SUBTYPE_ARM_V7S:
1722 outs() << " cputype CPU_TYPE_ARM\n";
1723 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1724 break;
1725 default:
1726 printUnknownCPUType(cputype, cpusubtype);
1727 break;
1728 }
1729 break;
1730 case MachO::CPU_TYPE_ARM64:
1731 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1732 case MachO::CPU_SUBTYPE_ARM64_ALL:
1733 outs() << " cputype CPU_TYPE_ARM64\n";
1734 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1735 break;
1736 default:
1737 printUnknownCPUType(cputype, cpusubtype);
1738 break;
1739 }
1740 break;
1741 default:
1742 printUnknownCPUType(cputype, cpusubtype);
1743 break;
1744 }
1745}
1746
1747static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1748 bool verbose) {
1749 outs() << "Fat headers\n";
1750 if (verbose) {
1751 if (UB->getMagic() == MachO::FAT_MAGIC)
1752 outs() << "fat_magic FAT_MAGIC\n";
1753 else // UB->getMagic() == MachO::FAT_MAGIC_64
1754 outs() << "fat_magic FAT_MAGIC_64\n";
1755 } else
1756 outs() << "fat_magic " << format("0x%" PRIx32"x", MachO::FAT_MAGIC) << "\n";
1757
1758 uint32_t nfat_arch = UB->getNumberOfObjects();
1759 StringRef Buf = UB->getData();
1760 uint64_t size = Buf.size();
1761 uint64_t big_size = sizeof(struct MachO::fat_header) +
1762 nfat_arch * sizeof(struct MachO::fat_arch);
1763 outs() << "nfat_arch " << UB->getNumberOfObjects();
1764 if (nfat_arch == 0)
1765 outs() << " (malformed, contains zero architecture types)\n";
1766 else if (big_size > size)
1767 outs() << " (malformed, architectures past end of file)\n";
1768 else
1769 outs() << "\n";
1770
1771 for (uint32_t i = 0; i < nfat_arch; ++i) {
1772 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1773 uint32_t cputype = OFA.getCPUType();
1774 uint32_t cpusubtype = OFA.getCPUSubType();
1775 outs() << "architecture ";
1776 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1777 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1778 uint32_t other_cputype = other_OFA.getCPUType();
1779 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
1780 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
1781 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
1782 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
1783 outs() << "(illegal duplicate architecture) ";
1784 break;
1785 }
1786 }
1787 if (verbose) {
1788 outs() << OFA.getArchFlagName() << "\n";
1789 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1790 } else {
1791 outs() << i << "\n";
1792 outs() << " cputype " << cputype << "\n";
1793 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1794 << "\n";
1795 }
1796 if (verbose &&
1797 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1798 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1799 else
1800 outs() << " capabilities "
1801 << format("0x%" PRIx32"x",
1802 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1803 outs() << " offset " << OFA.getOffset();
1804 if (OFA.getOffset() > size)
1805 outs() << " (past end of file)";
1806 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1807 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1808 outs() << "\n";
1809 outs() << " size " << OFA.getSize();
1810 big_size = OFA.getOffset() + OFA.getSize();
1811 if (big_size > size)
1812 outs() << " (past end of file)";
1813 outs() << "\n";
1814 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1815 << ")\n";
1816 }
1817}
1818
1819static void printArchiveChild(StringRef Filename, const Archive::Child &C,
1820 bool verbose, bool print_offset,
1821 StringRef ArchitectureName = StringRef()) {
1822 if (print_offset)
1823 outs() << C.getChildOffset() << "\t";
1824 Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
1825 if (!ModeOrErr)
1826 report_error(Filename, C, ModeOrErr.takeError(), ArchitectureName);
1827 sys::fs::perms Mode = ModeOrErr.get();
1828 if (verbose) {
1829 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1830 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1831 outs() << "-";
1832 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
1833 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
1834 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
1835 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
1836 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
1837 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
1838 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
1839 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
1840 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
1841 } else {
1842 outs() << format("0%o ", Mode);
1843 }
1844
1845 Expected<unsigned> UIDOrErr = C.getUID();
1846 if (!UIDOrErr)
1847 report_error(Filename, C, UIDOrErr.takeError(), ArchitectureName);
1848 unsigned UID = UIDOrErr.get();
1849 outs() << format("%3d/", UID);
1850 Expected<unsigned> GIDOrErr = C.getGID();
1851 if (!GIDOrErr)
1852 report_error(Filename, C, GIDOrErr.takeError(), ArchitectureName);
1853 unsigned GID = GIDOrErr.get();
1854 outs() << format("%-3d ", GID);
1855 Expected<uint64_t> Size = C.getRawSize();
1856 if (!Size)
1857 report_error(Filename, C, Size.takeError(), ArchitectureName);
1858 outs() << format("%5" PRId64"l" "d", Size.get()) << " ";
1859
1860 StringRef RawLastModified = C.getRawLastModified();
1861 if (verbose) {
1862 unsigned Seconds;
1863 if (RawLastModified.getAsInteger(10, Seconds))
1864 outs() << "(date: \"" << RawLastModified
1865 << "\" contains non-decimal chars) ";
1866 else {
1867 // Since cime(3) returns a 26 character string of the form:
1868 // "Sun Sep 16 01:03:52 1973\n\0"
1869 // just print 24 characters.
1870 time_t t = Seconds;
1871 outs() << format("%.24s ", ctime(&t));
1872 }
1873 } else {
1874 outs() << RawLastModified << " ";
1875 }
1876
1877 if (verbose) {
1878 Expected<StringRef> NameOrErr = C.getName();
1879 if (!NameOrErr) {
1880 consumeError(NameOrErr.takeError());
1881 Expected<StringRef> NameOrErr = C.getRawName();
1882 if (!NameOrErr)
1883 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1884 StringRef RawName = NameOrErr.get();
1885 outs() << RawName << "\n";
1886 } else {
1887 StringRef Name = NameOrErr.get();
1888 outs() << Name << "\n";
1889 }
1890 } else {
1891 Expected<StringRef> NameOrErr = C.getRawName();
1892 if (!NameOrErr)
1893 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1894 StringRef RawName = NameOrErr.get();
1895 outs() << RawName << "\n";
1896 }
1897}
1898
1899static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
1900 bool print_offset,
1901 StringRef ArchitectureName = StringRef()) {
1902 Error Err = Error::success();
1903 ;
1904 for (const auto &C : A->children(Err, false))
1905 printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
1906
1907 if (Err)
1908 report_error(StringRef(), Filename, std::move(Err), ArchitectureName);
1909}
1910
1911// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1912// -arch flags selecting just those slices as specified by them and also parses
1913// archive files. Then for each individual Mach-O file ProcessMachO() is
1914// called to process the file based on the command line options.
1915void llvm::ParseInputMachO(StringRef Filename) {
1916 // Check for -arch all and verifiy the -arch flags are valid.
1917 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1918 if (ArchFlags[i] == "all") {
1919 ArchAll = true;
1920 } else {
1921 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1922 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1923 "'for the -arch option\n";
1924 return;
1925 }
1926 }
1927 }
1928
1929 // Attempt to open the binary.
1930 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1931 if (!BinaryOrErr) {
1932 if (auto E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
1933 report_error(Filename, std::move(E));
1934 else
1935 outs() << Filename << ": is not an object file\n";
1936 return;
1937 }
1938 Binary &Bin = *BinaryOrErr.get().getBinary();
1939
1940 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1941 outs() << "Archive : " << Filename << "\n";
1942 if (ArchiveHeaders)
1943 printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
1944
1945 Error Err = Error::success();
1946 for (auto &C : A->children(Err)) {
1947 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1948 if (!ChildOrErr) {
1949 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1950 report_error(Filename, C, std::move(E));
1951 continue;
1952 }
1953 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1954 if (!checkMachOAndArchFlags(O, Filename))
1955 return;
1956 ProcessMachO(Filename, O, O->getFileName());
1957 }
1958 }
1959 if (Err)
1960 report_error(Filename, std::move(Err));
1961 return;
1962 }
1963 if (UniversalHeaders) {
1964 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
1965 printMachOUniversalHeaders(UB, !NonVerbose);
1966 }
1967 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1968 // If we have a list of architecture flags specified dump only those.
1969 if (!ArchAll && ArchFlags.size() != 0) {
1970 // Look for a slice in the universal binary that matches each ArchFlag.
1971 bool ArchFound;
1972 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1973 ArchFound = false;
1974 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1975 E = UB->end_objects();
1976 I != E; ++I) {
1977 if (ArchFlags[i] == I->getArchFlagName()) {
1978 ArchFound = true;
1979 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
1980 I->getAsObjectFile();
1981 std::string ArchitectureName = "";
1982 if (ArchFlags.size() > 1)
1983 ArchitectureName = I->getArchFlagName();
1984 if (ObjOrErr) {
1985 ObjectFile &O = *ObjOrErr.get();
1986 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1987 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1988 } else if (auto E = isNotObjectErrorInvalidFileType(
1989 ObjOrErr.takeError())) {
1990 report_error(Filename, StringRef(), std::move(E),
1991 ArchitectureName);
1992 continue;
1993 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1994 I->getAsArchive()) {
1995 std::unique_ptr<Archive> &A = *AOrErr;
1996 outs() << "Archive : " << Filename;
1997 if (!ArchitectureName.empty())
1998 outs() << " (architecture " << ArchitectureName << ")";
1999 outs() << "\n";
2000 if (ArchiveHeaders)
2001 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2002 ArchiveMemberOffsets, ArchitectureName);
2003 Error Err = Error::success();
2004 for (auto &C : A->children(Err)) {
2005 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2006 if (!ChildOrErr) {
2007 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2008 report_error(Filename, C, std::move(E), ArchitectureName);
2009 continue;
2010 }
2011 if (MachOObjectFile *O =
2012 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
2013 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
2014 }
2015 if (Err)
2016 report_error(Filename, std::move(Err));
2017 } else {
2018 consumeError(AOrErr.takeError());
2019 error("Mach-O universal file: " + Filename + " for " +
2020 "architecture " + StringRef(I->getArchFlagName()) +
2021 " is not a Mach-O file or an archive file");
2022 }
2023 }
2024 }
2025 if (!ArchFound) {
2026 errs() << "llvm-objdump: file: " + Filename + " does not contain "
2027 << "architecture: " + ArchFlags[i] + "\n";
2028 return;
2029 }
2030 }
2031 return;
2032 }
2033 // No architecture flags were specified so if this contains a slice that
2034 // matches the host architecture dump only that.
2035 if (!ArchAll) {
2036 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2037 E = UB->end_objects();
2038 I != E; ++I) {
2039 if (MachOObjectFile::getHostArch().getArchName() ==
2040 I->getArchFlagName()) {
2041 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
2042 std::string ArchiveName;
2043 ArchiveName.clear();
2044 if (ObjOrErr) {
2045 ObjectFile &O = *ObjOrErr.get();
2046 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
2047 ProcessMachO(Filename, MachOOF);
2048 } else if (auto E = isNotObjectErrorInvalidFileType(
2049 ObjOrErr.takeError())) {
2050 report_error(Filename, std::move(E));
2051 continue;
2052 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
2053 I->getAsArchive()) {
2054 std::unique_ptr<Archive> &A = *AOrErr;
2055 outs() << "Archive : " << Filename << "\n";
2056 if (ArchiveHeaders)
2057 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2058 ArchiveMemberOffsets);
2059 Error Err = Error::success();
2060 for (auto &C : A->children(Err)) {
2061 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2062 if (!ChildOrErr) {
2063 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2064 report_error(Filename, C, std::move(E));
2065 continue;
2066 }
2067 if (MachOObjectFile *O =
2068 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
2069 ProcessMachO(Filename, O, O->getFileName());
2070 }
2071 if (Err)
2072 report_error(Filename, std::move(Err));
2073 } else {
2074 consumeError(AOrErr.takeError());
2075 error("Mach-O universal file: " + Filename + " for architecture " +
2076 StringRef(I->getArchFlagName()) +
2077 " is not a Mach-O file or an archive file");
2078 }
2079 return;
2080 }
2081 }
2082 }
2083 // Either all architectures have been specified or none have been specified
2084 // and this does not contain the host architecture so dump all the slices.
2085 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
2086 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2087 E = UB->end_objects();
2088 I != E; ++I) {
2089 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
2090 std::string ArchitectureName = "";
2091 if (moreThanOneArch)
2092 ArchitectureName = I->getArchFlagName();
2093 if (ObjOrErr) {
2094 ObjectFile &Obj = *ObjOrErr.get();
2095 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
2096 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
2097 } else if (auto E = isNotObjectErrorInvalidFileType(
2098 ObjOrErr.takeError())) {
2099 report_error(StringRef(), Filename, std::move(E), ArchitectureName);
2100 continue;
2101 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
2102 I->getAsArchive()) {
2103 std::unique_ptr<Archive> &A = *AOrErr;
2104 outs() << "Archive : " << Filename;
2105 if (!ArchitectureName.empty())
2106 outs() << " (architecture " << ArchitectureName << ")";
2107 outs() << "\n";
2108 if (ArchiveHeaders)
2109 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2110 ArchiveMemberOffsets, ArchitectureName);
2111 Error Err = Error::success();
2112 for (auto &C : A->children(Err)) {
2113 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2114 if (!ChildOrErr) {
2115 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2116 report_error(Filename, C, std::move(E), ArchitectureName);
2117 continue;
2118 }
2119 if (MachOObjectFile *O =
2120 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
2121 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
2122 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
2123 ArchitectureName);
2124 }
2125 }
2126 if (Err)
2127 report_error(Filename, std::move(Err));
2128 } else {
2129 consumeError(AOrErr.takeError());
2130 error("Mach-O universal file: " + Filename + " for architecture " +
2131 StringRef(I->getArchFlagName()) +
2132 " is not a Mach-O file or an archive file");
2133 }
2134 }
2135 return;
2136 }
2137 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
2138 if (!checkMachOAndArchFlags(O, Filename))
2139 return;
2140 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
2141 ProcessMachO(Filename, MachOOF);
2142 } else
2143 errs() << "llvm-objdump: '" << Filename << "': "
2144 << "Object is not a Mach-O file type.\n";
2145 return;
2146 }
2147 llvm_unreachable("Input object can't be invalid at this point")::llvm::llvm_unreachable_internal("Input object can't be invalid at this point"
, "/build/llvm-toolchain-snapshot-7~svn325874/tools/llvm-objdump/MachODump.cpp"
, 2147)
;
2148}
2149
2150// The block of info used by the Symbolizer call backs.
2151struct DisassembleInfo {
2152 DisassembleInfo(MachOObjectFile *O, SymbolAddressMap *AddrMap,
2153 std::vector<SectionRef> *Sections, bool verbose)
2154 : verbose(verbose), O(O), AddrMap(AddrMap), Sections(Sections) {}
2155 bool verbose;
2156 MachOObjectFile *O;
2157 SectionRef S;
2158 SymbolAddressMap *AddrMap;
2159 std::vector<SectionRef> *Sections;
2160 const char *class_name = nullptr;
2161 const char *selector_name = nullptr;
2162 std::unique_ptr<char[]> method = nullptr;
2163 char *demangled_name = nullptr;
2164 uint64_t adrp_addr = 0;
2165 uint32_t adrp_inst = 0;
2166 std::unique_ptr<SymbolAddressMap> bindtable;
2167 uint32_t depth = 0;
2168};
2169
2170// SymbolizerGetOpInfo() is the operand information call back function.
2171// This is called to get the symbolic information for operand(s) of an
2172// instruction when it is being done. This routine does this from
2173// the relocation information, symbol table, etc. That block of information
2174// is a pointer to the struct DisassembleInfo that was passed when the
2175// disassembler context was created and passed to back to here when
2176// called back by the disassembler for instruction operands that could have
2177// relocation information. The address of the instruction containing operand is
2178// at the Pc parameter. The immediate value the operand has is passed in
2179// op_info->Value and is at Offset past the start of the instruction and has a
2180// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
2181// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
2182// names and addends of the symbolic expression to add for the operand. The
2183// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
2184// information is returned then this function returns 1 else it returns 0.
2185static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
2186 uint64_t Size, int TagType, void *TagBuf) {
2187 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
2188 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
2189 uint64_t value = op_info->Value;
2190
2191 // Make sure all fields returned are zero if we don't set them.
2192 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
2193 op_info->Value = value;
2194
2195 // If the TagType is not the value 1 which it code knows about or if no
2196 // verbose symbolic information is wanted then just return 0, indicating no
2197 // information is being returned.
2198 if (TagType != 1 || !info->verbose)
2199 return 0;
2200
2201 unsigned int Arch = info->O->getArch();
2202 if (Arch == Triple::x86) {
2203 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
2204 return 0;
2205 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2206 // TODO:
2207 // Search the external relocation entries of a fully linked image
2208 // (if any) for an entry that matches this segment offset.
2209 // uint32_t seg_offset = (Pc + Offset);
2210 return 0;
2211 }
2212 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2213 // for an entry for this section offset.
2214 uint32_t sect_addr = info->S.getAddress();
2215 uint32_t sect_offset = (Pc + Offset) - sect_addr;
2216 bool reloc_found = false;
2217 DataRefImpl Rel;
2218 MachO::any_relocation_info RE;
2219 bool isExtern = false;
2220 SymbolRef Symbol;
2221 bool r_scattered = false;
2222 uint32_t r_value, pair_r_value, r_type;
2223 for (const RelocationRef &Reloc : info->S.relocations()) {
2224 uint64_t RelocOffset = Reloc.getOffset();
2225 if (RelocOffset == sect_offset) {
2226 Rel = Reloc.getRawDataRefImpl();
2227 RE = info->O->getRelocation(Rel);
2228 r_type = info->O->getAnyRelocationType(RE);
2229 r_scattered = info->O->isRelocationScattered(RE);
2230 if (r_scattered) {
2231 r_value = info->O->getScatteredRelocationValue(RE);
2232 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
2233 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
2234 DataRefImpl RelNext = Rel;
2235 info->O->moveRelocationNext(RelNext);
2236 MachO::any_relocation_info RENext;
2237 RENext = info->O->getRelocation(RelNext);
2238 if (info->O->isRelocationScattered(RENext))
2239 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2240 else
2241 return 0;
2242 }
2243 } else {
2244 isExtern = info->O->getPlainRelocationExternal(RE);
2245 if (isExtern) {
2246 symbol_iterator RelocSym = Reloc.getSymbol();
2247 Symbol = *RelocSym;
2248 }
2249 }
2250 reloc_found = true;
2251 break;
2252 }
2253 }
2254 if (reloc_found && isExtern) {
2255 Expected<StringRef> SymName = Symbol.getName();
2256 if (!SymName)
2257 report_error(info->O->getFileName(), SymName.takeError());
2258 const char *name = SymName->data();
2259 op_info->AddSymbol.Present = 1;
2260 op_info->AddSymbol.Name = name;
2261 // For i386 extern relocation entries the value in the instruction is
2262 // the offset from the symbol, and value is already set in op_info->Value.
2263 return 1;
2264 }
2265 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
2266 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
2267 const char *add = GuessSymbolName(r_value, info->AddrMap);
2268 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2269 uint32_t offset = value - (r_value - pair_r_value);
2270 op_info->AddSymbol.Present = 1;
2271 if (add != nullptr)
2272 op_info->AddSymbol.Name = add;
2273 else
2274 op_info->AddSymbol.Value = r_value;
2275 op_info->SubtractSymbol.Present = 1;
2276 if (sub != nullptr)
2277 op_info->SubtractSymbol.Name = sub;
2278 else
2279 op_info->SubtractSymbol.Value = pair_r_value;
2280 op_info->Value = offset;
2281 return 1;
2282 }
2283 return 0;
2284 }
2285 if (Arch == Triple::x86_64) {
2286 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
2287 return 0;
2288 // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
2289 // relocation entries of a linked image (if any) for an entry that matches
2290 // this segment offset.
2291 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2292 uint64_t seg_offset = Pc + Offset;
2293 bool reloc_found = false;
2294 DataRefImpl Rel;
2295 MachO::any_relocation_info RE;
2296 bool isExtern = false;
2297 SymbolRef Symbol;
2298 for (const RelocationRef &Reloc : info->O->external_relocations()) {
2299 uint64_t RelocOffset = Reloc.getOffset();
2300 if (RelocOffset == seg_offset) {
2301 Rel = Reloc.getRawDataRefImpl();
2302 RE = info->O->getRelocation(Rel);
2303 // external relocation entries should always be external.
2304 isExtern = info->O->getPlainRelocationExternal(RE);
2305 if (isExtern) {
2306 symbol_iterator RelocSym = Reloc.getSymbol();
2307 Symbol = *RelocSym;
2308 }
2309 reloc_found = true;
2310 break;
2311 }
2312 }
2313 if (reloc_found && isExtern) {
2314 // The Value passed in will be adjusted by the Pc if the instruction
2315 // adds the Pc. But for x86_64 external relocation entries the Value
2316 // is the offset from the external symbol.
2317 if (info->O->getAnyRelocationPCRel(RE))
2318 op_info->Value -= Pc + Offset + Size;
2319 Expected<StringRef> SymName = Symbol.getName();
2320 if (!SymName)
2321 report_error(info->O->getFileName(), SymName.takeError());
2322 const char *name = SymName->data();
2323 op_info->AddSymbol.Present = 1;
2324 op_info->AddSymbol.Name = name;
2325 return 1;
2326 }
2327 return 0;
2328 }
2329 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2330 // for an entry for this section offset.
2331 uint64_t sect_addr = info->S.getAddress();
2332 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2333 bool reloc_found = false;
2334 DataRefImpl Rel;
2335 MachO::any_relocation_info RE;
2336 bool isExtern = false;
2337 SymbolRef Symbol;
2338 for (const RelocationRef &Reloc : info->S.relocations()) {
2339 uint64_t RelocOffset = Reloc.getOffset();
2340 if (RelocOffset == sect_offset) {
2341 Rel = Reloc.getRawDataRefImpl();
2342 RE = info->O->getRelocation(Rel);
2343 // NOTE: Scattered relocations don't exist on x86_64.
2344 isExtern = info->O->getPlainRelocationExternal(RE);
2345 if (isExtern) {
2346 symbol_iterator RelocSym = Reloc.getSymbol();
2347 Symbol = *RelocSym;
2348 }
2349 reloc_found = true;
2350 break;
2351 }
2352 }
2353 if (reloc_found && isExtern) {
2354 // The Value passed in will be adjusted by the Pc if the instruction
2355 // adds the Pc. But for x86_64 external relocation entries the Value
2356 // is the offset from the external symbol.
2357 if (info->O->getAnyRelocationPCRel(RE))
2358 op_info->Value -= Pc + Offset + Size;
2359 Expected<StringRef> SymName = Symbol.getName();
2360 if (!SymName)
2361 report_error(info->O->getFileName(), SymName.takeError());
2362 const char *name = SymName->data();
2363 unsigned Type = info->O->getAnyRelocationType(RE);
2364 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
2365 DataRefImpl RelNext = Rel;
2366 info->O->moveRelocationNext(RelNext);
2367 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2368 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
2369 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
2370 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
2371 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
2372 op_info->SubtractSymbol.Present = 1;
2373 op_info->SubtractSymbol.Name = name;
2374 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
2375 Symbol = *RelocSymNext;
2376 Expected<StringRef> SymNameNext = Symbol.getName();
2377 if (!SymNameNext)
2378 report_error(info->O->getFileName(), SymNameNext.takeError());
2379 name = SymNameNext->data();
2380 }
2381 }
2382 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2383 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2384 op_info->AddSymbol.Present = 1;
2385 op_info->AddSymbol.Name = name;
2386 return 1;
2387 }
2388 return 0;
2389 }
2390 if (Arch == Triple::arm) {
2391 if (Offset != 0 || (Size != 4 && Size != 2))
2392 return 0;
2393 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2394 // TODO:
2395 // Search the external relocation entries of a fully linked image
2396 // (if any) for an entry that matches this segment offset.
2397 // uint32_t seg_offset = (Pc + Offset);
2398 return 0;
2399 }
2400 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2401 // for an entry for this section offset.
2402 uint32_t sect_addr = info->S.getAddress();
2403 uint32_t sect_offset = (Pc + Offset) - sect_addr;
2404 DataRefImpl Rel;
2405 MachO::any_relocation_info RE;
2406 bool isExtern = false;
2407 SymbolRef Symbol;
2408 bool r_scattered = false;
2409 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
2410 auto Reloc =
2411 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2412 uint64_t RelocOffset = Reloc.getOffset();
2413 return RelocOffset == sect_offset;
2414 });
2415
2416 if (Reloc == info->S.relocations().end())
2417 return 0;
2418
2419 Rel = Reloc->getRawDataRefImpl();
2420 RE = info->O->getRelocation(Rel);
2421 r_length = info->O->getAnyRelocationLength(RE);
2422 r_scattered = info->O->isRelocationScattered(RE);
2423 if (r_scattered) {
2424 r_value = info->O->getScatteredRelocationValue(RE);
2425 r_type = info->O->getScatteredRelocationType(RE);
2426 } else {
2427 r_type = info->O->getAnyRelocationType(RE);
2428 isExtern = info->O->getPlainRelocationExternal(RE);
2429 if (isExtern) {
2430 symbol_iterator RelocSym = Reloc->getSymbol();
2431 Symbol = *RelocSym;
2432 }
2433 }
2434 if (r_type == MachO::ARM_RELOC_HALF ||
2435 r_type == MachO::ARM_RELOC_SECTDIFF ||
2436 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2437 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2438 DataRefImpl RelNext = Rel;
2439 info->O->moveRelocationNext(RelNext);
2440 MachO::any_relocation_info RENext;
2441 RENext = info->O->getRelocation(RelNext);
2442 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
2443 if (info->O->isRelocationScattered(RENext))
2444 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2445 }
2446
2447 if (isExtern) {
2448 Expected<StringRef> SymName = Symbol.getName();
2449 if (!SymName)
2450 report_error(info->O->getFileName(), SymName.takeError());
2451 const char *name = SymName->data();
2452 op_info->AddSymbol.Present = 1;
2453 op_info->AddSymbol.Name = name;
2454 switch (r_type) {
2455 case MachO::ARM_RELOC_HALF:
2456 if ((r_length & 0x1) == 1) {
2457 op_info->Value = value << 16 | other_half;
2458 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI161;
2459 } else {
2460 op_info->Value = other_half << 16 | value;
2461 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO162;
2462 }
2463 break;
2464 default:
2465 break;
2466 }
2467 return 1;
2468 }
2469 // If we have a branch that is not an external relocation entry then
2470 // return 0 so the code in tryAddingSymbolicOperand() can use the
2471 // SymbolLookUp call back with the branch target address to look up the
2472 // symbol and possibility add an annotation for a symbol stub.
2473 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2474 r_type == MachO::ARM_THUMB_RELOC_BR22))
2475 return 0;
2476
2477 uint32_t offset = 0;
2478 if (r_type == MachO::ARM_RELOC_HALF ||
2479 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2480 if ((r_length & 0x1) == 1)
2481 value = value << 16 | other_half;
2482 else
2483 value = other_half << 16 | value;
2484 }
2485 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2486 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2487 offset = value - r_value;
2488 value = r_value;
2489 }
2490
2491 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2492 if ((r_length & 0x1) == 1)
2493 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI161;
2494 else
2495 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO162;
2496 const char *add = GuessSymbolName(r_value, info->AddrMap);
2497 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2498 int32_t offset = value - (r_value - pair_r_value);
2499 op_info->AddSymbol.Present = 1;
2500 if (add != nullptr)
2501 op_info->AddSymbol.Name = add;
2502 else
2503 op_info->AddSymbol.Value = r_value;
2504 op_info->SubtractSymbol.Present = 1;
2505 if (sub != nullptr)
2506 op_info->SubtractSymbol.Name = sub;
2507 else
2508 op_info->SubtractSymbol.Value = pair_r_value;
2509 op_info->Value = offset;
2510 return 1;
2511 }
2512
2513 op_info->AddSymbol.Present = 1;
2514 op_info->Value = offset;
2515 if (r_type == MachO::ARM_RELOC_HALF) {
2516 if ((r_length & 0x1) == 1)
2517 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI161;
2518 else
2519 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO162;
2520 }
2521 const char *add = GuessSymbolName(value, info->AddrMap);
2522 if (add != nullptr) {
2523 op_info->AddSymbol.Name = add;
2524 return 1;
2525 }
2526 op_info->AddSymbol.Value = value;
2527 return 1;
2528 }
2529 if (Arch == Triple::aarch64) {
2530 if (Offset != 0 || Size != 4)
2531 return 0;
2532 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2533 // TODO:
2534 // Search the external relocation entries of a fully linked image
2535 // (if any) for an entry that matches this segment offset.
2536 // uint64_t seg_offset = (Pc + Offset);
2537 return 0;
2538 }
2539 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2540 // for an entry for this section offset.
2541 uint64_t sect_addr = info->S.getAddress();
2542 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2543 auto Reloc =
2544 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2545 uint64_t RelocOffset = Reloc.getOffset();
2546 return RelocOffset == sect_offset;
2547 });
2548
2549 if (Reloc == info->S.relocations().end())
2550 return 0;
2551
2552 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2553 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2554 uint32_t r_type = info->O->getAnyRelocationType(RE);
2555 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2556 DataRefImpl RelNext = Rel;
2557 info->O->moveRelocationNext(RelNext);
2558 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2559 if (value == 0) {
2560 value = info->O->getPlainRelocationSymbolNum(RENext);
2561 op_info->Value = value;
2562 }
2563 }
2564 // NOTE: Scattered relocations don't exist on arm64.
2565 if (!info->O->getPlainRelocationExternal(RE))
2566 return 0;
2567 Expected<StringRef> SymName = Reloc->getSymbol()->getName();
2568 if (!SymName)
2569 report_error(info->O->getFileName(), SymName.takeError());
2570 const char *name = SymName->data();
2571 op_info->AddSymbol.Present = 1;
2572 op_info->AddSymbol.Name = name;
2573
2574 switch (r_type) {
2575 case MachO::ARM64_RELOC_PAGE21:
2576 /* @page */
2577 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE1;
2578 break;
2579 case MachO::ARM64_RELOC_PAGEOFF12:
2580 /* @pageoff */
2581 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF2;
2582 break;
2583 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2584 /* @gotpage */
2585 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE3;
2586 break;
2587 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2588 /* @gotpageoff */
2589 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF4;
2590 break;
2591 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2592 /* @tvlppage is not implemented in llvm-mc */
2593 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP5;
2594 break;
2595 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2596 /* @tvlppageoff is not implemented in llvm-mc */
2597 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF6;
2598 break;
2599 default:
2600 case MachO::ARM64_RELOC_BRANCH26:
2601 op_info->VariantKind = LLVMDisassembler_VariantKind_None0;
2602 break;
2603 }
2604 return 1;
2605 }
2606 return 0;
2607}
2608
2609// GuessCstringPointer is passed the address of what might be a pointer to a
2610// literal string in a cstring section. If that address is in a cstring section
2611// it returns a pointer to that string. Else it returns nullptr.
2612static const char *GuessCstringPointer(uint64_t ReferenceValue,
2613 struct DisassembleInfo *info) {
2614 for (const auto &Load : info->O->load_commands()) {
2615 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2616 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2617 for (unsigned J = 0; J < Seg.nsects; ++J) {
2618 MachO::section_64 Sec = info->O->getSection64(Load, J);
2619 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2620 if (section_type == MachO::S_CSTRING_LITERALS &&
2621 ReferenceValue >= Sec.addr &&
2622 ReferenceValue < Sec.addr + Sec.size) {
2623 uint64_t sect_offset = ReferenceValue - Sec.addr;
2624 uint64_t object_offset = Sec.offset + sect_offset;
2625 StringRef MachOContents = info->O->getData();
2626 uint64_t object_size = MachOContents.size();
2627 const char *object_addr = (const char *)MachOContents.data();
2628 if (object_offset < object_size) {
2629 const char *name = object_addr + object_offset;
2630 return name;
2631 } else {
2632 return nullptr;
2633 }
2634 }
2635 }
2636 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2637 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2638 for (unsigned J = 0; J < Seg.nsects; ++J) {
2639 MachO::section Sec = info->O->getSection(Load, J);
2640 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2641 if (section_type == MachO::S_CSTRING_LITERALS &&
2642 ReferenceValue >= Sec.addr &&
2643 ReferenceValue < Sec.addr + Sec.size) {
2644 uint64_t sect_offset = ReferenceValue - Sec.addr;
2645 uint64_t object_offset = Sec.offset + sect_offset;
2646 StringRef MachOContents = info->O->getData();
2647 uint64_t object_size = MachOContents.size();
2648 const char *object_addr = (const char *)MachOContents.data();
2649 if (object_offset < object_size) {
2650 const char *name = object_addr + object_offset;
2651 return name;
2652 } else {
2653 return nullptr;
2654 }
2655 }
2656 }
2657 }
2658 }
2659 return nullptr;
2660}
2661
2662// GuessIndirectSymbol returns the name of the indirect symbol for the
2663// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2664// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2665// symbol name being referenced by the stub or pointer.
2666static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2667 struct DisassembleInfo *info) {
2668 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2669 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
2670 for (const auto &Load : info->O->load_commands()) {
2671 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2672 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2673 for (unsigned J = 0; J < Seg.nsects; ++J) {
2674 MachO::section_64 Sec = info->O->getSection64(Load, J);
2675 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2676 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2677 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2678 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2679 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2680 section_type == MachO::S_SYMBOL_STUBS) &&
2681 ReferenceValue >= Sec.addr &&
2682 ReferenceValue < Sec.addr + Sec.size) {
2683 uint32_t stride;
2684 if (section_type == MachO::S_SYMBOL_STUBS)
2685 stride = Sec.reserved2;
2686 else
2687 stride = 8;
2688 if (stride == 0)
2689 return nullptr;
2690 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2691 if (index < Dysymtab.nindirectsyms) {
2692 uint32_t indirect_symbol =
2693 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2694 if (indirect_symbol < Symtab.nsyms) {
2695 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2696 SymbolRef Symbol = *Sym;
2697 Expected<StringRef> SymName = Symbol.getName();
2698 if (!SymName)
2699 report_error(info->O->getFileName(), SymName.takeError());
2700 const char *name = SymName->data();
2701 return name;
2702 }
2703 }
2704 }
2705 }
2706 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2707 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2708 for (unsigned J = 0; J < Seg.nsects; ++J) {
2709 MachO::section Sec = info->O->getSection(Load, J);
2710 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2711 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2712 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2713 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2714 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2715 section_type == MachO::S_SYMBOL_STUBS) &&
2716 ReferenceValue >= Sec.addr &&
2717 ReferenceValue < Sec.addr + Sec.size) {
2718 uint32_t stride;
2719 if (section_type == MachO::S_SYMBOL_STUBS)
2720 stride = Sec.reserved2;
2721 else
2722 stride = 4;
2723 if (stride == 0)
2724 return nullptr;
2725 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2726 if (index < Dysymtab.nindirectsyms) {
2727 uint32_t indirect_symbol =
2728 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2729 if (indirect_symbol < Symtab.nsyms) {
2730 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2731 SymbolRef Symbol = *Sym;
2732 Expected<StringRef> SymName = Symbol.getName();
2733 if (!SymName)
2734 report_error(info->O->getFileName(), SymName.takeError());
2735 const char *name = SymName->data();
2736 return name;
2737 }
2738 }
2739 }
2740 }
2741 }
2742 }
2743 return nullptr;
2744}
2745
2746// method_reference() is called passing it the ReferenceName that might be
2747// a reference it to an Objective-C method call. If so then it allocates and
2748// assembles a method call string with the values last seen and saved in
2749// the DisassembleInfo's class_name and selector_name fields. This is saved
2750// into the method field of the info and any previous string is free'ed.
2751// Then the class_name field in the info is set to nullptr. The method call
2752// string is set into ReferenceName and ReferenceType is set to
2753// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2754// then both ReferenceType and ReferenceName are left unchanged.
2755static void method_reference(struct DisassembleInfo *info,
2756 uint64_t *ReferenceType,
2757 const char **ReferenceName) {
2758 unsigned int Arch = info->O->getArch();
2759 if (*ReferenceName != nullptr) {
2760 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
2761 if (info->selector_name != nullptr) {
2762 if (info->class_name != nullptr) {
2763 info->method = llvm::make_unique<char[]>(
2764 5 + strlen(info->class_name) + strlen(info->selector_name));
2765 char *method = info->method.get();
2766 if (method != nullptr) {
2767 strcpy(method, "+[");
2768 strcat(method, info->class_name);
2769 strcat(method, " ");
2770 strcat(method, info->selector_name);
2771 strcat(method, "]");
2772 *ReferenceName = method;
2773 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message5;
2774 }
2775 } else {
2776 info->method =
2777 llvm::make_unique<char[]>(9 + strlen(info->selector_name));
2778 char *method = info->method.get();
2779 if (method != nullptr) {
2780 if (Arch == Triple::x86_64)
2781 strcpy(method, "-[%rdi ");
2782 else if (Arch == Triple::aarch64)
2783 strcpy(method, "-[x0 ");
2784 else
2785 strcpy(method, "-[r? ");
2786 strcat(method, info->selector_name);
2787 strcat(method, "]");
2788 *ReferenceName = method;
2789 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message5;
2790 }
2791 }
2792 info->class_name = nullptr;
2793 }
2794 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
2795 if (info->selector_name != nullptr) {
2796 info->method =
2797 llvm::make_unique<char[]>(17 + strlen(info->selector_name));
2798 char *method = info->method.get();
2799 if (method != nullptr) {
2800 if (Arch == Triple::x86_64)
2801 strcpy(method, "-[[%rdi super] ");
2802 else if (Arch == Triple::aarch64)
2803 strcpy(method, "-[[x0 super] ");
2804 else
2805 strcpy(method, "-[[r? super] ");
2806 strcat(method, info->selector_name);
2807 strcat(method, "]");
2808 *ReferenceName = method;
2809 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message5;
2810 }
2811 info->class_name = nullptr;
2812 }
2813 }
2814 }
2815}
2816
2817// GuessPointerPointer() is passed the address of what might be a pointer to
2818// a reference to an Objective-C class, selector, message ref or cfstring.
2819// If so the value of the pointer is returned and one of the booleans are set
2820// to true. If not zero is returned and all the booleans are set to false.
2821static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2822 struct DisassembleInfo *info,
2823 bool &classref, bool &selref, bool &msgref,
2824 bool &cfstring) {
2825 classref = false;
2826 selref = false;
2827 msgref = false;
2828 cfstring = false;
2829 for (const auto &Load : info->O->load_commands()) {
2830 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2831 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2832 for (unsigned J = 0; J < Seg.nsects; ++J) {
2833 MachO::section_64 Sec = info->O->getSection64(Load, J);
2834 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2835 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2836 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2837 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2838 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2839 ReferenceValue >= Sec.addr &&
2840 ReferenceValue < Sec.addr + Sec.size) {
2841 uint64_t sect_offset = ReferenceValue - Sec.addr;
2842 uint64_t object_offset = Sec.offset + sect_offset;
2843 StringRef MachOContents = info->O->getData();
2844 uint64_t object_size = MachOContents.size();
2845 const char *object_addr = (const char *)MachOContents.data();
2846 if (object_offset < object_size) {
2847 uint64_t pointer_value;
2848 memcpy(&pointer_value, object_addr + object_offset,
2849 sizeof(uint64_t));
2850 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2851 sys::swapByteOrder(pointer_value);
2852 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2853 selref = true;
2854 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2855 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2856 classref = true;
2857 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2858 ReferenceValue + 8 < Sec.addr + Sec.size) {
2859 msgref = true;
2860 memcpy(&pointer_value, object_addr + object_offset + 8,
2861 sizeof(uint64_t));
2862 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2863 sys::swapByteOrder(pointer_value);
2864 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2865 cfstring = true;
2866 return pointer_value;
2867 } else {
2868 return 0;
2869 }
2870 }
2871 }
2872 }
2873 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
2874 }
2875 return 0;
2876}
2877
2878// get_pointer_64 returns a pointer to the bytes in the object file at the
2879// Address from a section in the Mach-O file. And indirectly returns the
2880// offset into the section, number of bytes left in the section past the offset
2881// and which section is was being referenced. If the Address is not in a
2882// section nullptr is returned.
2883static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2884 uint32_t &left, SectionRef &S,
2885 DisassembleInfo *info,
2886 bool objc_only = false) {
2887 offset = 0;
2888 left = 0;
2889 S = SectionRef();
2890 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2891 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2892 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
2893 if (SectSize == 0)
2894 continue;
2895 if (objc_only) {
2896 StringRef SectName;
2897 ((*(info->Sections))[SectIdx]).getName(SectName);
2898 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2899 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2900 if (SegName != "__OBJC" && SectName != "__cstring")
2901 continue;
2902 }
2903 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2904 S = (*(info->Sections))[SectIdx];
2905 offset = Address - SectAddress;
2906 left = SectSize - offset;
2907 StringRef SectContents;
2908 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2909 return SectContents.data() + offset;
2910 }
2911 }
2912 return nullptr;
2913}
2914
2915static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2916 uint32_t &left, SectionRef &S,
2917 DisassembleInfo *info,
2918 bool objc_only = false) {
2919 return get_pointer_64(Address, offset, left, S, info, objc_only);
2920}
2921
2922// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2923// the symbol indirectly through n_value. Based on the relocation information
2924// for the specified section offset in the specified section reference.
2925// If no relocation information is found and a non-zero ReferenceValue for the
2926// symbol is passed, look up that address in the info's AddrMap.
2927static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2928 DisassembleInfo *info, uint64_t &n_value,
2929 uint64_t ReferenceValue = 0) {
2930 n_value = 0;
2931 if (!info->verbose)
2932 return nullptr;
2933
2934 // See if there is an external relocation entry at the sect_offset.
2935 bool reloc_found = false;
2936 DataRefImpl Rel;
2937 MachO::any_relocation_info RE;
2938 bool isExtern = false;
2939 SymbolRef Symbol;
2940 for (const RelocationRef &Reloc : S.relocations()) {
2941 uint64_t RelocOffset = Reloc.getOffset();
2942 if (RelocOffset == sect_offset) {
2943 Rel = Reloc.getRawDataRefImpl();
2944 RE = info->O->getRelocation(Rel);
2945 if (info->O->isRelocationScattered(RE))
2946 continue;
2947 isExtern = info->O->getPlainRelocationExternal(RE);
2948 if (isExtern) {
2949 symbol_iterator RelocSym = Reloc.getSymbol();
2950 Symbol = *RelocSym;
2951 }
2952 reloc_found = true;
2953 break;
2954 }
2955 }
2956 // If there is an external relocation entry for a symbol in this section
2957 // at this section_offset then use that symbol's value for the n_value
2958 // and return its name.
2959 const char *SymbolName = nullptr;
2960 if (reloc_found && isExtern) {
2961 n_value = Symbol.getValue();
2962 Expected<StringRef> NameOrError = Symbol.getName();
2963 if (!NameOrError)
2964 report_error(info->O->getFileName(), NameOrError.takeError());
2965 StringRef Name = *NameOrError;
2966 if (!Name.empty()) {
2967 SymbolName = Name.data();
2968 return SymbolName;
2969 }
2970 }
2971
2972 // TODO: For fully linked images, look through the external relocation
2973 // entries off the dynamic symtab command. For these the r_offset is from the
2974 // start of the first writeable segment in the Mach-O file. So the offset
2975 // to this section from that segment is passed to this routine by the caller,
2976 // as the database_offset. Which is the difference of the section's starting
2977 // address and the first writable segment.
2978 //
2979 // NOTE: need add passing the database_offset to this routine.
2980
2981 // We did not find an external relocation entry so look up the ReferenceValue
2982 // as an address of a symbol and if found return that symbol's name.
2983 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
2984
2985 return SymbolName;
2986}
2987
2988static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2989 DisassembleInfo *info,
2990 uint32_t ReferenceValue) {
2991 uint64_t n_value64;
2992 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2993}
2994
2995// These are structs in the Objective-C meta data and read to produce the
2996// comments for disassembly. While these are part of the ABI they are no
2997// public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
2998// .
2999
3000// The cfstring object in a 64-bit Mach-O file.
3001struct cfstring64_t {
3002 uint64_t isa; // class64_t * (64-bit pointer)
3003 uint64_t flags; // flag bits
3004 uint64_t characters; // char * (64-bit pointer)
3005 uint64_t length; // number of non-NULL characters in above
3006};
3007
3008// The class object in a 64-bit Mach-O file.
3009struct class64_t {
3010 uint64_t isa; // class64_t * (64-bit pointer)
3011 uint64_t superclass; // class64_t * (64-bit pointer)
3012 uint64_t cache; // Cache (64-bit pointer)
3013 uint64_t vtable; // IMP * (64-bit pointer)
3014 uint64_t data; // class_ro64_t * (64-bit pointer)
3015};
3016
3017struct class32_t {
3018 uint32_t isa; /* class32_t * (32-bit pointer) */
3019 uint32_t superclass; /* class32_t * (32-bit pointer) */
3020 uint32_t cache; /* Cache (32-bit pointer) */
3021 uint32_t vtable; /* IMP * (32-bit pointer) */
3022 uint32_t data; /* class_ro32_t * (32-bit pointer) */
3023};
3024
3025struct class_ro64_t {
3026 uint32_t flags;
3027 uint32_t instanceStart;
3028 uint32_t instanceSize;
3029 uint32_t reserved;
3030 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
3031 uint64_t name; // const char * (64-bit pointer)
3032 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
3033 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
3034 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
3035 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
3036 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
3037};
3038
3039struct class_ro32_t {
3040 uint32_t flags;
3041 uint32_t instanceStart;
3042 uint32_t instanceSize;
3043 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
3044 uint32_t name; /* const char * (32-bit pointer) */
3045 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
3046 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
3047 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
3048 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
3049 uint32_t baseProperties; /* const struct objc_property_list *
3050 (32-bit pointer) */
3051};
3052
3053/* Values for class_ro{64,32}_t->flags */
3054#define RO_META(1 << 0) (1 << 0)
3055#define RO_ROOT(1 << 1) (1 << 1)
3056#define RO_HAS_CXX_STRUCTORS(1 << 2) (1 << 2)
3057
3058struct method_list64_t {
3059 uint32_t entsize;
3060 uint32_t count;
3061 /* struct method64_t first; These structures follow inline */
3062};
3063
3064struct method_list32_t {
3065 uint32_t entsize;
3066 uint32_t count;
3067 /* struct method32_t first; These structures follow inline */
3068};
3069
3070struct method64_t {
3071 uint64_t name; /* SEL (64-bit pointer) */
3072 uint64_t types; /* const char * (64-bit pointer) */
3073 uint64_t imp; /* IMP (64-bit pointer) */
3074};
3075
3076struct method32_t {
3077 uint32_t name; /* SEL (32-bit pointer) */
3078 uint32_t types; /* const char * (32-bit pointer) */
3079 uint32_t imp; /* IMP (32-bit pointer) */
3080};
3081
3082struct protocol_list64_t {
3083 uint64_t count; /* uintptr_t (a 64-bit value) */
3084 /* struct protocol64_t * list[0]; These pointers follow inline */
3085};
3086
3087struct protocol_list32_t {
3088 uint32_t count; /* uintptr_t (a 32-bit value) */
3089 /* struct protocol32_t * list[0]; These pointers follow inline */
3090};
3091
3092struct protocol64_t {
3093 uint64_t isa; /* id * (64-bit pointer) */
3094 uint64_t name; /* const char * (64-bit pointer) */
3095 uint64_t protocols; /* struct protocol_list64_t *
3096 (64-bit pointer) */
3097 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
3098 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
3099 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
3100 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
3101 uint64_t instanceProperties; /* struct objc_property_list *
3102 (64-bit pointer) */
3103};
3104
3105struct protocol32_t {
3106 uint32_t isa; /* id * (32-bit pointer) */
3107 uint32_t name; /* const char * (32-bit pointer) */
3108 uint32_t protocols; /* struct protocol_list_t *
3109 (32-bit pointer) */
3110 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
3111 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
3112 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
3113 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
3114 uint32_t instanceProperties; /* struct objc_property_list *
3115 (32-bit pointer) */
3116};
3117
3118struct ivar_list64_t {
3119 uint32_t entsize;
3120 uint32_t count;
3121 /* struct ivar64_t first; These structures follow inline */
3122};
3123
3124struct ivar_list32_t {
3125 uint32_t entsize;
3126 uint32_t count;
3127 /* struct ivar32_t first; These structures follow inline */
3128};
3129
3130struct ivar64_t {
3131 uint64_t offset; /* uintptr_t * (64-bit pointer) */
3132 uint64_t name; /* const char * (64-bit pointer) */
3133 uint64_t type; /* const char * (64-bit pointer) */
3134 uint32_t alignment;
3135 uint32_t size;
3136};
3137
3138struct ivar32_t {
3139 uint32_t offset; /* uintptr_t * (32-bit pointer) */
3140 uint32_t name; /* const char * (32-bit pointer) */
3141 uint32_t type; /* const char * (32-bit pointer) */
3142 uint32_t alignment;
3143 uint32_t size;
3144};
3145
3146struct objc_property_list64 {
3147 uint32_t entsize;
3148 uint32_t count;
3149 /* struct objc_property64 first; These structures follow inline */
3150};
3151
3152struct objc_property_list32 {
3153 uint32_t entsize;
3154 uint32_t count;
3155 /* struct objc_property32 first; These structures follow inline */
3156};
3157
3158struct objc_property64 {
3159 uint64_t name; /* const char * (64-bit pointer) */
3160 uint64_t attributes; /* const char * (64-bit pointer) */
3161};
3162
3163struct objc_property32 {
3164 uint32_t name; /* const char * (32-bit pointer) */
3165 uint32_t attributes; /* const char * (32-bit pointer) */
3166};
3167
3168struct category64_t {
3169 uint64_t name; /* const char * (64-bit pointer) */
3170 uint64_t cls; /* struct class_t * (64-bit pointer) */
3171 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
3172 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
3173 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
3174 uint64_t instanceProperties; /* struct objc_property_list *
3175 (64-bit pointer) */
3176};
3177
3178struct category32_t {
3179 uint32_t name; /* const char * (32-bit pointer) */
3180 uint32_t cls; /* struct class_t * (32-bit pointer) */
3181 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
3182 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
3183 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
3184 uint32_t instanceProperties; /* struct objc_property_list *
3185 (32-bit pointer) */
3186};
3187
3188struct objc_image_info64 {
3189 uint32_t version;
3190 uint32_t flags;
3191};
3192struct objc_image_info32 {
3193 uint32_t version;
3194 uint32_t flags;
3195};
3196struct imageInfo_t {
3197 uint32_t version;
3198 uint32_t flags;
3199};
3200/* masks for objc_image_info.flags */
3201#define OBJC_IMAGE_IS_REPLACEMENT(1 << 0) (1 << 0)
3202#define OBJC_IMAGE_SUPPORTS_GC(1 << 1) (1 << 1)
3203
3204struct message_ref64 {
3205 uint64_t imp; /* IMP (64-bit pointer) */
3206 uint64_t sel; /* SEL (64-bit pointer) */
3207};
3208
3209struct message_ref32 {
3210 uint32_t imp; /* IMP (32-bit pointer) */
3211 uint32_t sel; /* SEL (32-bit pointer) */
3212};
3213
3214// Objective-C 1 (32-bit only) meta data structs.
3215
3216struct objc_module_t {
3217 uint32_t version;
3218 uint32_t size;
3219 uint32_t name; /* char * (32-bit pointer) */
3220 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
3221};
3222
3223struct objc_symtab_t {
3224 uint32_t sel_ref_cnt;
3225 uint32_t refs; /* SEL * (32-bit pointer) */
3226 uint16_t cls_def_cnt;
3227 uint16_t cat_def_cnt;
3228 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
3229};
3230
3231struct objc_class_t {
3232 uint32_t isa; /* struct objc_class * (32-bit pointer) */
3233 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
3234 uint32_t name; /* const char * (32-bit pointer) */
3235 int32_t version;
3236 int32_t info;
3237 int32_t instance_size;
3238 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
3239 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
3240 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
3241 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
3242};
3243
3244#define CLS_GETINFO(cls, infomask)((cls)->info & (infomask)) ((cls)->info & (infomask))
3245// class is not a metaclass
3246#define CLS_CLASS0x1 0x1
3247// class is a metaclass
3248#define CLS_META0x2 0x2
3249
3250struct objc_category_t {
3251 uint32_t category_name; /* char * (32-bit pointer) */
3252 uint32_t class_name; /* char * (32-bit pointer) */
3253 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
3254 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
3255 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
3256};
3257
3258struct objc_ivar_t {
3259 uint32_t ivar_name; /* char * (32-bit pointer) */
3260 uint32_t ivar_type; /* char * (32-bit pointer) */
3261 int32_t ivar_offset;
3262};
3263
3264struct objc_ivar_list_t {
3265 int32_t ivar_count;
3266 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
3267};
3268
3269struct objc_method_list_t {
3270 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
3271 int32_t method_count;
3272 // struct objc_method_t method_list[1]; /* variable length structure */
3273};
3274
3275struct objc_method_t {
3276 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
3277 uint32_t method_types; /* char * (32-bit pointer) */
3278 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
3279 (32-bit pointer) */
3280};
3281
3282struct objc_protocol_list_t {
3283 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
3284 int32_t count;
3285 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
3286 // (32-bit pointer) */
3287};
3288
3289struct objc_protocol_t {
3290 uint32_t isa; /* struct objc_class * (32-bit pointer) */
3291 uint32_t protocol_name; /* char * (32-bit pointer) */
3292 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
3293 uint32_t instance_methods; /* struct objc_method_description_list *
3294 (32-bit pointer) */
3295 uint32_t class_methods; /* struct objc_method_description_list *
3296 (32-bit pointer) */
3297};
3298
3299struct objc_method_description_list_t {
3300 int32_t count;
3301 // struct objc_method_description_t list[1];
3302};
3303
3304struct objc_method_description_t {
3305 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
3306 uint32_t types; /* char * (32-bit pointer) */
3307};
3308
3309inline void swapStruct(struct cfstring64_t &cfs) {
3310 sys::swapByteOrder(cfs.isa);
3311 sys::swapByteOrder(cfs.flags);
3312 sys::swapByteOrder(cfs.characters);
3313 sys::swapByteOrder(cfs.length);
3314}
3315
3316inline void swapStruct(struct class64_t &c) {
3317 sys::swapByteOrder(c.isa);
3318 sys::swapByteOrder(c.superclass);
3319 sys::swapByteOrder(c.cache);
3320 sys::swapByteOrder(c.vtable);
3321 sys::swapByteOrder(c.data);
3322}
3323
3324inline void swapStruct(struct class32_t &c) {
3325 sys::swapByteOrder(c.isa);
3326 sys::swapByteOrder(c.superclass);
3327 sys::swapByteOrder(c.cache);
3328 sys::swapByteOrder(c.vtable);
3329 sys::swapByteOrder(c.data);
3330}
3331
3332inline void swapStruct(struct class_ro64_t &cro) {
3333 sys::swapByteOrder(cro.flags);
3334 sys::swapByteOrder(cro.instanceStart);
3335 sys::swapByteOrder(cro.instanceSize);
3336 sys::swapByteOrder(cro.reserved);
3337 sys::swapByteOrder(cro.ivarLayout);
3338 sys::swapByteOrder(cro.name);
3339 sys::swapByteOrder(cro.baseMethods);
3340 sys::swapByteOrder(cro.baseProtocols);
3341 sys::swapByteOrder(cro.ivars);
3342 sys::swapByteOrder(cro.weakIvarLayout);
3343 sys::swapByteOrder(cro.baseProperties);
3344}
3345
3346inline void swapStruct(struct class_ro32_t &cro) {
3347 sys::swapByteOrder(cro.flags);
3348 sys::swapByteOrder(cro.instanceStart);
3349 sys::swapByteOrder(cro.instanceSize);
3350 sys::swapByteOrder(cro.ivarLayout);
3351 sys::swapByteOrder(cro.name);
3352 sys::swapByteOrder(cro.baseMethods);
3353 sys::swapByteOrder(cro.baseProtocols);
3354 sys::swapByteOrder(cro.ivars);
3355 sys::swapByteOrder(cro.weakIvarLayout);
3356 sys::swapByteOrder(cro.baseProperties);
3357}
3358
3359inline void swapStruct(struct method_list64_t &ml) {
3360 sys::swapByteOrder(ml.entsize);
3361 sys::swapByteOrder(ml.count);
3362}
3363
3364inline void swapStruct(struct method_list32_t &ml) {
3365 sys::swapByteOrder(ml.entsize);
3366 sys::swapByteOrder(ml.count);
3367}
3368
3369inline void swapStruct(struct method64_t &m) {
3370 sys::swapByteOrder(m.name);
3371 sys::swapByteOrder(m.types);
3372 sys::swapByteOrder(m.imp);
3373}
3374
3375inline void swapStruct(struct method32_t &m) {
3376 sys::swapByteOrder(m.name);
3377 sys::swapByteOrder(m.types);
3378 sys::swapByteOrder(m.imp);
3379}
3380
3381inline void swapStruct(struct protocol_list64_t &pl) {
3382 sys::swapByteOrder(pl.count);
3383}
3384
3385inline void swapStruct(struct protocol_list32_t &pl) {
3386 sys::swapByteOrder(pl.count);
3387}
3388
3389inline void swapStruct(struct protocol64_t &p) {
3390 sys::swapByteOrder(p.isa);
3391 sys::swapByteOrder(p.name);
3392 sys::swapByteOrder(p.protocols);
3393 sys::swapByteOrder(p.instanceMethods);
3394 sys::swapByteOrder(p.classMethods);
3395 sys::swapByteOrder(p.optionalInstanceMethods);
3396 sys::swapByteOrder(p.optionalClassMethods);
3397 sys::swapByteOrder(p.instanceProperties);
3398}
3399
3400inline void swapStruct(struct protocol32_t &p) {
3401 sys::swapByteOrder(p.isa);
3402 sys::swapByteOrder(p.name);
3403 sys::swapByteOrder(p.protocols);
3404 sys::swapByteOrder(p.instanceMethods);
3405 sys::swapByteOrder(p.classMethods);
3406 sys::swapByteOrder(p.optionalInstanceMethods);
3407 sys::swapByteOrder(p.optionalClassMethods);
3408 sys::swapByteOrder(p.instanceProperties);
3409}
3410
3411inline void swapStruct(struct ivar_list64_t &il) {
3412 sys::swapByteOrder(il.entsize);
3413 sys::swapByteOrder(il.count);
3414}
3415
3416inline void swapStruct(struct ivar_list32_t &il) {
3417 sys::swapByteOrder(il.entsize);
3418 sys::swapByteOrder(il.count);
3419}
3420
3421inline void swapStruct(struct ivar64_t &i) {
3422 sys::swapByteOrder(i.offset);
3423 sys::swapByteOrder(i.name);
3424 sys::swapByteOrder(i.type);
3425 sys::swapByteOrder(i.alignment);
3426 sys::swapByteOrder(i.size);
3427}
3428
3429inline void swapStruct(struct ivar32_t &i) {
3430 sys::swapByteOrder(i.offset);
3431 sys::swapByteOrder(i.name);
3432 sys::swapByteOrder(i.type);
3433 sys::swapByteOrder(i.alignment);
3434 sys::swapByteOrder(i.size);
3435}
3436
3437inline void swapStruct(struct objc_property_list64 &pl) {
3438 sys::swapByteOrder(pl.entsize);
3439 sys::swapByteOrder(pl.count);
3440}
3441
3442inline void swapStruct(struct objc_property_list32 &pl) {
3443 sys::swapByteOrder(pl.entsize);
3444 sys::swapByteOrder(pl.count);
3445}
3446
3447inline void swapStruct(struct objc_property64 &op) {
3448 sys::swapByteOrder(op.name);
3449 sys::swapByteOrder(op.attributes);
3450}
3451
3452inline void swapStruct(struct objc_property32 &op) {
3453 sys::swapByteOrder(op.name);
3454 sys::swapByteOrder(op.attributes);
3455}
3456
3457inline void swapStruct(struct category64_t &c) {
3458 sys::swapByteOrder(c.name);
3459 sys::swapByteOrder(c.cls);
3460 sys::swapByteOrder(c.instanceMethods);
3461 sys::swapByteOrder(c.classMethods);
3462 sys::swapByteOrder(c.protocols);
3463 sys::swapByteOrder(c.instanceProperties);
3464}
3465
3466inline void swapStruct(struct category32_t &c) {
3467 sys::swapByteOrder(c.name);
3468 sys::swapByteOrder(c.cls);
3469 sys::swapByteOrder(c.instanceMethods);
3470 sys::swapByteOrder(c.classMethods);
3471 sys::swapByteOrder(c.protocols);
3472 sys::swapByteOrder(c.instanceProperties);
3473}
3474
3475inline void swapStruct(struct objc_image_info64 &o) {
3476 sys::swapByteOrder(o.version);
3477 sys::swapByteOrder(o.flags);
3478}
3479
3480inline void swapStruct(struct objc_image_info32 &o) {
3481 sys::swapByteOrder(o.version);
3482 sys::swapByteOrder(o.flags);
3483}
3484
3485inline void swapStruct(struct imageInfo_t &o) {
3486 sys::swapByteOrder(o.version);
3487 sys::swapByteOrder(o.flags);
3488}
3489
3490inline void swapStruct(struct message_ref64 &mr) {
3491 sys::swapByteOrder(mr.imp);
3492 sys::swapByteOrder(mr.sel);
3493}
3494
3495inline void swapStruct(struct message_ref32 &mr) {
3496 sys::swapByteOrder(mr.imp);
3497 sys::swapByteOrder(mr.sel);
3498}
3499
3500inline void swapStruct(struct objc_module_t &module) {
3501 sys::swapByteOrder(module.version);
3502 sys::swapByteOrder(module.size);
3503 sys::swapByteOrder(module.name);
3504 sys::swapByteOrder(module.symtab);
3505}
3506
3507inline void swapStruct(struct objc_symtab_t &symtab) {
3508 sys::swapByteOrder(symtab.sel_ref_cnt);
3509 sys::swapByteOrder(symtab.refs);
3510 sys::swapByteOrder(symtab.cls_def_cnt);
3511 sys::swapByteOrder(symtab.cat_def_cnt);
3512}
3513
3514inline void swapStruct(struct objc_class_t &objc_class) {
3515 sys::swapByteOrder(objc_class.isa);
3516 sys::swapByteOrder(objc_class.super_class);
3517 sys::swapByteOrder(objc_class.name);
3518 sys::swapByteOrder(objc_class.version);
3519 sys::swapByteOrder(objc_class.info);
3520 sys::swapByteOrder(objc_class.instance_size);
3521 sys::swapByteOrder(objc_class.ivars);
3522 sys::swapByteOrder(objc_class.methodLists);
3523 sys::swapByteOrder(objc_class.cache);
3524 sys::swapByteOrder(objc_class.protocols);
3525}
3526
3527inline void swapStruct(struct objc_category_t &objc_category) {
3528 sys::swapByteOrder(objc_category.category_name);
3529 sys::swapByteOrder(objc_category.class_name);
3530 sys::swapByteOrder(objc_category.instance_methods);
3531 sys::swapByteOrder(objc_category.class_methods);
3532 sys::swapByteOrder(objc_category.protocols);
3533}
3534
3535inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3536 sys::swapByteOrder(objc_ivar_list.ivar_count);
3537}
3538
3539inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3540 sys::swapByteOrder(objc_ivar.ivar_name);
3541 sys::swapByteOrder(objc_ivar.ivar_type);
3542 sys::swapByteOrder(objc_ivar.ivar_offset);
3543}
3544
3545inline void swapStruct(struct objc_method_list_t &method_list) {
3546 sys::swapByteOrder(method_list.obsolete);
3547 sys::swapByteOrder(method_list.method_count);
3548}
3549
3550inline void swapStruct(struct objc_method_t &method) {
3551 sys::swapByteOrder(method.method_name);
3552 sys::swapByteOrder(method.method_types);
3553 sys::swapByteOrder(method.method_imp);
3554}
3555
3556inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3557 sys::swapByteOrder(protocol_list.next);
3558 sys::swapByteOrder(protocol_list.count);
3559}
3560
3561inline void swapStruct(struct objc_protocol_t &protocol) {
3562 sys::swapByteOrder(protocol.isa);
3563 sys::swapByteOrder(protocol.protocol_name);
3564 sys::swapByteOrder(protocol.protocol_list);
3565 sys::swapByteOrder(protocol.instance_methods);
3566 sys::swapByteOrder(protocol.class_methods);
3567}
3568
3569inline void swapStruct(struct objc_method_description_list_t &mdl) {
3570 sys::swapByteOrder(mdl.count);
3571}
3572
3573inline void swapStruct(struct objc_method_description_t &md) {
3574 sys::swapByteOrder(md.name);
3575 sys::swapByteOrder(md.types);
3576}
3577
3578static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3579 struct DisassembleInfo *info);
3580
3581// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3582// to an Objective-C class and returns the class name. It is also passed the
3583// address of the pointer, so when the pointer is zero as it can be in an .o
3584// file, that is used to look for an external relocation entry with a symbol
3585// name.
3586static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3587 uint64_t ReferenceValue,
3588 struct DisassembleInfo *info) {
3589 const char *r;
3590 uint32_t offset, left;
3591 SectionRef S;
3592
3593 // The pointer_value can be 0 in an object file and have a relocation
3594 // entry for the class symbol at the ReferenceValue (the address of the
3595 // pointer).
3596 if (pointer_value == 0) {
3597 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3598 if (r == nullptr || left < sizeof(uint64_t))
3599 return nullptr;
3600 uint64_t n_value;
3601 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3602 if (symbol_name == nullptr)
3603 return nullptr;
3604 const char *class_name = strrchr(symbol_name, '$');
3605 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3606 return class_name + 2;
3607 else
3608 return nullptr;
3609 }
3610
3611 // The case were the pointer_value is non-zero and points to a class defined
3612 // in this Mach-O file.
3613 r = get_pointer_64(pointer_value, offset, left, S, info);
3614 if (r == nullptr || left < sizeof(struct class64_t))
3615 return nullptr;
3616 struct class64_t c;
3617 memcpy(&c, r, sizeof(struct class64_t));
3618 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3619 swapStruct(c);
3620 if (c.data == 0)
3621 return nullptr;
3622 r = get_pointer_64(c.data, offset, left, S, info);
3623 if (r == nullptr || left < sizeof(struct class_ro64_t))
3624 return nullptr;
3625 struct class_ro64_t cro;
3626 memcpy(&cro, r, sizeof(struct class_ro64_t));
3627 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3628 swapStruct(cro);
3629 if (cro.name == 0)
3630 return nullptr;
3631 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3632 return name;
3633}
3634
3635// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3636// pointer to a cfstring and returns its name or nullptr.
3637static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3638 struct DisassembleInfo *info) {
3639 const char *r, *name;
3640 uint32_t offset, left;
3641 SectionRef S;
3642 struct cfstring64_t cfs;
3643 uint64_t cfs_characters;
3644
3645 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3646 if (r == nullptr || left < sizeof(struct cfstring64_t))
3647 return nullptr;
3648 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3649 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3650 swapStruct(cfs);
3651 if (cfs.characters == 0) {
3652 uint64_t n_value;
3653 const char *symbol_name = get_symbol_64(
3654 offset + offsetof(struct cfstring64_t, characters)__builtin_offsetof(struct cfstring64_t, characters), S, info, n_value);
3655 if (symbol_name == nullptr)
3656 return nullptr;
3657 cfs_characters = n_value;
3658 } else
3659 cfs_characters = cfs.characters;
3660 name = get_pointer_64(cfs_characters, offset, left, S, info);
3661
3662 return name;
3663}
3664
3665// get_objc2_64bit_selref() is used for disassembly and is passed a the address
3666// of a pointer to an Objective-C selector reference when the pointer value is
3667// zero as in a .o file and is likely to have a external relocation entry with
3668// who's symbol's n_value is the real pointer to the selector name. If that is
3669// the case the real pointer to the selector name is returned else 0 is
3670// returned
3671static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3672 struct DisassembleInfo *info) {
3673 uint32_t offset, left;
3674 SectionRef S;
3675
3676 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3677 if (r == nullptr || left < sizeof(uint64_t))
3678 return 0;
3679 uint64_t n_value;
3680 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3681 if (symbol_name == nullptr)
3682 return 0;
3683 return n_value;
3684}
3685
3686static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3687 const char *sectname) {
3688 for (const SectionRef &Section : O->sections()) {
3689 StringRef SectName;
3690 Section.getName(SectName);
3691 DataRefImpl Ref = Section.getRawDataRefImpl();
3692 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3693 if (SegName == segname && SectName == sectname)
3694 return Section;
3695 }
3696 return SectionRef();
3697}
3698
3699static void
3700walk_pointer_list_64(const char *listname, const SectionRef S,
3701 MachOObjectFile *O, struct DisassembleInfo *info,
3702 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3703 if (S == SectionRef())
3704 return;
3705
3706 StringRef SectName;
3707 S.getName(SectName);
3708 DataRefImpl Ref = S.getRawDataRefImpl();
3709 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3710 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3711
3712 StringRef BytesStr;
3713 S.getContents(BytesStr);
3714 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3715
3716 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3717 uint32_t left = S.getSize() - i;
3718 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3719 uint64_t p = 0;
3720 memcpy(&p, Contents + i, size);
3721 if (i + sizeof(uint64_t) > S.getSize())
3722 outs() << listname << " list pointer extends past end of (" << SegName
3723 << "," << SectName << ") section\n";
3724 outs() << format("%016" PRIx64"l" "x", S.getAddress() + i) << " ";
3725
3726 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3727 sys::swapByteOrder(p);
3728
3729 uint64_t n_value = 0;
3730 const char *name = get_symbol_64(i, S, info, n_value, p);
3731 if (name == nullptr)
3732 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3733
3734 if (n_value != 0) {
3735 outs() << format("0x%" PRIx64"l" "x", n_value);
3736 if (p != 0)
3737 outs() << " + " << format("0x%" PRIx64"l" "x", p);
3738 } else
3739 outs() << format("0x%" PRIx64"l" "x", p);
3740 if (name != nullptr)
3741 outs() << " " << name;
3742 outs() << "\n";
3743
3744 p += n_value;
3745 if (func)
3746 func(p, info);
3747 }
3748}
3749
3750static void
3751walk_pointer_list_32(const char *listname, const SectionRef S,
3752 MachOObjectFile *O, struct DisassembleInfo *info,
3753 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3754 if (S == SectionRef())
3755 return;
3756
3757 StringRef SectName;
3758 S.getName(SectName);
3759 DataRefImpl Ref = S.getRawDataRefImpl();
3760 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3761 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3762
3763 StringRef BytesStr;
3764 S.getContents(BytesStr);
3765 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3766
3767 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3768 uint32_t left = S.getSize() - i;
3769 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3770 uint32_t p = 0;
3771 memcpy(&p, Contents + i, size);
3772 if (i + sizeof(uint32_t) > S.getSize())
3773 outs() << listname << " list pointer extends past end of (" << SegName
3774 << "," << SectName << ") section\n";
3775 uint32_t Address = S.getAddress() + i;
3776 outs() << format("%08" PRIx32"x", Address) << " ";
3777
3778 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3779 sys::swapByteOrder(p);
3780 outs() << format("0x%" PRIx32"x", p);
3781
3782 const char *name = get_symbol_32(i, S, info, p);
3783 if (name != nullptr)
3784 outs() << " " << name;
3785 outs() << "\n";
3786
3787 if (func)
3788 func(p, info);
3789 }
3790}
3791
3792static void print_layout_map(const char *layout_map, uint32_t left) {
3793 if (layout_map == nullptr)
3794 return;
3795 outs() << " layout map: ";
3796 do {
3797 outs() << format("0x%02" PRIx32"x", (*layout_map) & 0xff) << " ";
3798 left--;
3799 layout_map++;
3800 } while (*layout_map != '\0' && left != 0);
3801 outs() << "\n";
3802}
3803
3804static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3805 uint32_t offset, left;
3806 SectionRef S;
3807 const char *layout_map;
3808
3809 if (p == 0)
3810 return;
3811 layout_map = get_pointer_64(p, offset, left, S, info);
3812 print_layout_map(layout_map, left);
3813}
3814
3815static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3816 uint32_t offset, left;
3817 SectionRef S;
3818 const char *layout_map;
3819
3820 if (p == 0)
3821 return;
3822 layout_map = get_pointer_32(p, offset, left, S, info);
3823 print_layout_map(layout_map, left);
3824}
3825
3826static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3827 const char *indent) {
3828 struct method_list64_t ml;
3829 struct method64_t m;
3830 const char *r;
3831 uint32_t offset, xoffset, left, i;
3832 SectionRef S, xS;
3833 const char *name, *sym_name;
3834 uint64_t n_value;
3835
3836 r = get_pointer_64(p, offset, left, S, info);
3837 if (r == nullptr)
3838 return;
3839 memset(&ml, '\0', sizeof(struct method_list64_t));
3840 if (left < sizeof(struct method_list64_t)) {
3841 memcpy(&ml, r, left);
3842 outs() << " (method_list_t entends past the end of the section)\n";
3843 } else
3844 memcpy(&ml, r, sizeof(struct method_list64_t));
3845 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3846 swapStruct(ml);
3847 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3848 outs() << indent << "\t\t count " << ml.count << "\n";
3849
3850 p += sizeof(struct method_list64_t);
3851 offset += sizeof(struct method_list64_t);
3852 for (i = 0; i < ml.count; i++) {
3853 r = get_pointer_64(p, offset, left, S, info);
3854 if (r == nullptr)
3855 return;
3856 memset(&m, '\0', sizeof(struct method64_t));
3857 if (left < sizeof(struct method64_t)) {
3858 memcpy(&m, r, left);
3859 outs() << indent << " (method_t extends past the end of the section)\n";
3860 } else
3861 memcpy(&m, r, sizeof(struct method64_t));
3862 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3863 swapStruct(m);
3864
3865 outs() << indent << "\t\t name ";
3866 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name)__builtin_offsetof(struct method64_t, name), S,
3867 info, n_value, m.name);
3868 if (n_value != 0) {
3869 if (info->verbose && sym_name != nullptr)
3870 outs() << sym_name;
3871 else
3872 outs() << format("0x%" PRIx64"l" "x", n_value);
3873 if (m.name != 0)
3874 outs() << " + " << format("0x%" PRIx64"l" "x", m.name);
3875 } else
3876 outs() << format("0x%" PRIx64"l" "x", m.name);
3877 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3878 if (name != nullptr)
3879 outs() << format(" %.*s", left, name);
3880 outs() << "\n";
3881
3882 outs() << indent << "\t\t types ";
3883 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types)__builtin_offsetof(struct method64_t, types), S,
3884 info, n_value, m.types);
3885 if (n_value != 0) {
3886 if (info->verbose && sym_name != nullptr)
3887 outs() << sym_name;
3888 else
3889 outs() << format("0x%" PRIx64"l" "x", n_value);
3890 if (m.types != 0)
3891 outs() << " + " << format("0x%" PRIx64"l" "x", m.types);
3892 } else
3893 outs() << format("0x%" PRIx64"l" "x", m.types);
3894 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3895 if (name != nullptr)
3896 outs() << format(" %.*s", left, name);
3897 outs() << "\n";
3898
3899 outs() << indent << "\t\t imp ";
3900 name = get_symbol_64(offset + offsetof(struct method64_t, imp)__builtin_offsetof(struct method64_t, imp), S, info,
3901 n_value, m.imp);
3902 if (info->verbose && name == nullptr) {
3903 if (n_value != 0) {
3904 outs() << format("0x%" PRIx64"l" "x", n_value) << " ";
3905 if (m.imp != 0)
3906 outs() << "+ " << format("0x%" PRIx64"l" "x", m.imp) << " ";
3907 } else
3908 outs() << format("0x%" PRIx64"l" "x", m.imp) << " ";
3909 }
3910 if (name != nullptr)
3911 outs() << name;
3912 outs() << "\n";
3913
3914 p += sizeof(struct method64_t);
3915 offset += sizeof(struct method64_t);
3916 }
3917}
3918
3919static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3920 const char *indent) {
3921 struct method_list32_t ml;
3922 struct method32_t m;
3923 const char *r, *name;
3924 uint32_t offset, xoffset, left, i;
3925 SectionRef S, xS;
3926
3927 r = get_pointer_32(p, offset, left, S, info);
3928 if (r == nullptr)
3929 return;
3930 memset(&ml, '\0', sizeof(struct method_list32_t));
3931 if (left < sizeof(struct method_list32_t)) {
3932 memcpy(&ml, r, left);
3933 outs() << " (method_list_t entends past the end of the section)\n";
3934 } else
3935 memcpy(&ml, r, sizeof(struct method_list32_t));
3936 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3937 swapStruct(ml);
3938 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3939 outs() << indent << "\t\t count " << ml.count << "\n";
3940
3941 p += sizeof(struct method_list32_t);
3942 offset += sizeof(struct method_list32_t);
3943 for (i = 0; i < ml.count; i++) {
3944 r = get_pointer_32(p, offset, left, S, info);
3945 if (r == nullptr)
3946 return;
3947 memset(&m, '\0', sizeof(struct method32_t));
3948 if (left < sizeof(struct method32_t)) {
3949 memcpy(&ml, r, left);
3950 outs() << indent << " (method_t entends past the end of the section)\n";
3951 } else
3952 memcpy(&m, r, sizeof(struct method32_t));
3953 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3954 swapStruct(m);
3955
3956 outs() << indent << "\t\t name " << format("0x%" PRIx32"x", m.name);
3957 name = get_pointer_32(m.name, xoffset, left, xS, info);
3958 if (name != nullptr)
3959 outs() << format(" %.*s", left, name);
3960 outs() << "\n";
3961
3962 outs() << indent << "\t\t types " << format("0x%" PRIx32"x", m.types);
3963 name = get_pointer_32(m.types, xoffset, left, xS, info);
3964 if (name != nullptr)
3965 outs() << format(" %.*s", left, name);
3966 outs() << "\n";
3967
3968 outs() << indent << "\t\t imp " << format("0x%" PRIx32"x", m.imp);
3969 name = get_symbol_32(offset + offsetof(struct method32_t, imp)__builtin_offsetof(struct method32_t, imp), S, info,
3970 m.imp);
3971 if (name != nullptr)
3972 outs() << " " << name;
3973 outs() << "\n";
3974
3975 p += sizeof(struct method32_t);
3976 offset += sizeof(struct method32_t);
3977 }
3978}
3979
3980static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3981 uint32_t offset, left, xleft;
3982 SectionRef S;
3983 struct objc_method_list_t method_list;
3984 struct objc_method_t method;
3985 const char *r, *methods, *name, *SymbolName;
3986 int32_t i;
3987
3988 r = get_pointer_32(p, offset, left, S, info, true);
3989 if (r == nullptr)
3990 return true;
3991
3992 outs() << "\n";
3993 if (left > sizeof(struct objc_method_list_t)) {
3994 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3995 } else {
3996 outs() << "\t\t objc_method_list extends past end of the section\n";
3997 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3998 memcpy(&method_list, r, left);
3999 }
4000 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4001 swapStruct(method_list);
4002
4003 outs() << "\t\t obsolete "
4004 << format("0x%08" PRIx32"x", method_list.obsolete) << "\n";
4005 outs() << "\t\t method_count " << method_list.method_count << "\n";
4006
4007 methods = r + sizeof(struct objc_method_list_t);
4008 for (i = 0; i < method_list.method_count; i++) {
4009 if ((i + 1) * sizeof(struct objc_method_t) > left) {
4010 outs() << "\t\t remaining method's extend past the of the section\n";
4011 break;
4012 }
4013 memcpy(&method, methods + i * sizeof(struct objc_method_t),
4014 sizeof(struct objc_method_t));
4015 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4016 swapStruct(method);
4017
4018 outs() << "\t\t method_name "
4019 << format("0x%08" PRIx32"x", method.method_name);
4020 if (info->verbose) {
4021 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
4022 if (name != nullptr)
4023 outs() << format(" %.*s", xleft, name);
4024 else
4025 outs() << " (not in an __OBJC section)";
4026 }
4027 outs() << "\n";
4028
4029 outs() << "\t\t method_types "
4030 << format("0x%08" PRIx32"x", method.method_types);
4031 if (info->verbose) {
4032 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
4033 if (name != nullptr)
4034 outs() << format(" %.*s", xleft, name);
4035 else
4036 outs() << " (not in an __OBJC section)";
4037 }
4038 outs() << "\n";
4039
4040 outs() << "\t\t method_imp "
4041 << format("0x%08" PRIx32"x", method.method_imp) << " ";
4042 if (info->verbose) {
4043 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
4044 if (SymbolName != nullptr)
4045 outs() << SymbolName;
4046 }
4047 outs() << "\n";
4048 }
4049 return false;
4050}
4051
4052static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
4053 struct protocol_list64_t pl;
4054 uint64_t q, n_value;
4055 struct protocol64_t pc;
4056 const char *r;
4057 uint32_t offset, xoffset, left, i;
4058 SectionRef S, xS;
4059 const char *name, *sym_name;
4060
4061 r = get_pointer_64(p, offset, left, S, info);
4062 if (r == nullptr)
4063 return;
4064 memset(&pl, '\0', sizeof(struct protocol_list64_t));
4065 if (left < sizeof(struct protocol_list64_t)) {
4066 memcpy(&pl, r, left);
4067 outs() << " (protocol_list_t entends past the end of the section)\n";
4068 } else
4069 memcpy(&pl, r, sizeof(struct protocol_list64_t));
4070 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4071 swapStruct(pl);
4072 outs() << " count " << pl.count << "\n";
4073
4074 p += sizeof(struct protocol_list64_t);
4075 offset += sizeof(struct protocol_list64_t);
4076 for (i = 0; i < pl.count; i++) {
4077 r = get_pointer_64(p, offset, left, S, info);
4078 if (r == nullptr)
4079 return;
4080 q = 0;
4081 if (left < sizeof(uint64_t)) {
4082 memcpy(&q, r, left);
4083 outs() << " (protocol_t * entends past the end of the section)\n";
4084 } else
4085 memcpy(&q, r, sizeof(uint64_t));
4086 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4087 sys::swapByteOrder(q);
4088
4089 outs() << "\t\t list[" << i << "] ";
4090 sym_name = get_symbol_64(offset, S, info, n_value, q);
4091 if (n_value != 0) {
4092 if (info->verbose && sym_name != nullptr)
4093 outs() << sym_name;
4094 else
4095 outs() << format("0x%" PRIx64"l" "x", n_value);
4096 if (q != 0)
4097 outs() << " + " << format("0x%" PRIx64"l" "x", q);
4098 } else
4099 outs() << format("0x%" PRIx64"l" "x", q);
4100 outs() << " (struct protocol_t *)\n";
4101
4102 r = get_pointer_64(q + n_value, offset, left, S, info);
4103 if (r == nullptr)
4104 return;
4105 memset(&pc, '\0', sizeof(struct protocol64_t));
4106 if (left < sizeof(struct protocol64_t)) {
4107 memcpy(&pc, r, left);
4108 outs() << " (protocol_t entends past the end of the section)\n";
4109 } else
4110 memcpy(&pc, r, sizeof(struct protocol64_t));
4111 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4112 swapStruct(pc);
4113
4114 outs() << "\t\t\t isa " << format("0x%" PRIx64"l" "x", pc.isa) << "\n";
4115
4116 outs() << "\t\t\t name ";
4117 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name)__builtin_offsetof(struct protocol64_t, name), S,
4118 info, n_value, pc.name);
4119 if (n_value != 0) {
4120 if (info->verbose && sym_name != nullptr)
4121 outs() << sym_name;
4122 else
4123 outs() << format("0x%" PRIx64"l" "x", n_value);
4124 if (pc.name != 0)
4125 outs() << " + " << format("0x%" PRIx64"l" "x", pc.name);
4126 } else
4127 outs() << format("0x%" PRIx64"l" "x", pc.name);
4128 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
4129 if (name != nullptr)
4130 outs() << format(" %.*s", left, name);
4131 outs() << "\n";
4132
4133 outs() << "\t\t\tprotocols " << format("0x%" PRIx64"l" "x", pc.protocols) << "\n";
4134
4135 outs() << "\t\t instanceMethods ";
4136 sym_name =
4137 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods)__builtin_offsetof(struct protocol64_t, instanceMethods),
4138 S, info, n_value, pc.instanceMethods);
4139 if (n_value != 0) {
4140 if (info->verbose && sym_name != nullptr)
4141 outs() << sym_name;
4142 else
4143 outs() << format("0x%" PRIx64"l" "x", n_value);
4144 if (pc.instanceMethods != 0)
4145 outs() << " + " << format("0x%" PRIx64"l" "x", pc.instanceMethods);
4146 } else
4147 outs() << format("0x%" PRIx64"l" "x", pc.instanceMethods);
4148 outs() << " (struct method_list_t *)\n";
4149 if (pc.instanceMethods + n_value != 0)
4150 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
4151
4152 outs() << "\t\t classMethods ";
4153 sym_name =
4154 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods)__builtin_offsetof(struct protocol64_t, classMethods), S,
4155 info, n_value, pc.classMethods);
4156 if (n_value != 0) {
4157 if (info->verbose && sym_name != nullptr)
4158 outs() << sym_name;
4159 else
4160 outs() << format("0x%" PRIx64"l" "x", n_value);
4161 if (pc.classMethods != 0)
4162 outs() << " + " << format("0x%" PRIx64"l" "x", pc.classMethods);
4163 } else
4164 outs() << format("0x%" PRIx64"l" "x", pc.classMethods);
4165 outs() << " (struct method_list_t *)\n";
4166 if (pc.classMethods + n_value != 0)
4167 print_method_list64_t(pc.classMethods + n_value, info, "\t");
4168
4169 outs() << "\t optionalInstanceMethods "
4170 << format("0x%" PRIx64"l" "x", pc.optionalInstanceMethods) << "\n";
4171 outs() << "\t optionalClassMethods "
4172 << format("0x%" PRIx64"l" "x", pc.optionalClassMethods) << "\n";
4173 outs() << "\t instanceProperties "
4174 << format("0x%" PRIx64"l" "x", pc.instanceProperties) << "\n";
4175
4176 p += sizeof(uint64_t);
4177 offset += sizeof(uint64_t);
4178 }
4179}
4180
4181static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
4182 struct protocol_list32_t pl;
4183 uint32_t q;
4184 struct protocol32_t pc;
4185 const char *r;
4186 uint32_t offset, xoffset, left, i;
4187 SectionRef S, xS;
4188 const char *name;
4189
4190 r = get_pointer_32(p, offset, left, S, info);
4191 if (r == nullptr)
4192 return;
4193 memset(&pl, '\0', sizeof(struct protocol_list32_t));
4194 if (left < sizeof(struct protocol_list32_t)) {
4195 memcpy(&pl, r, left);
4196 outs() << " (protocol_list_t entends past the end of the section)\n";
4197 } else
4198 memcpy(&pl, r, sizeof(struct protocol_list32_t));
4199 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4200 swapStruct(pl);
4201 outs() << " count " << pl.count << "\n";
4202
4203 p += sizeof(struct protocol_list32_t);
4204 offset += sizeof(struct protocol_list32_t);
4205 for (i = 0; i < pl.count; i++) {
4206 r = get_pointer_32(p, offset, left, S, info);
4207 if (r == nullptr)
4208 return;
4209 q = 0;
4210 if (left < sizeof(uint32_t)) {
4211 memcpy(&q, r, left);
4212 outs() << " (protocol_t * entends past the end of the section)\n";
4213 } else
4214 memcpy(&q, r, sizeof(uint32_t));
4215 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4216 sys::swapByteOrder(q);
4217 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32"x", q)
4218 << " (struct protocol_t *)\n";
4219 r = get_pointer_32(q, offset, left, S, info);
4220 if (r == nullptr)
4221 return;
4222 memset(&pc, '\0', sizeof(struct protocol32_t));
4223 if (left < sizeof(struct protocol32_t)) {
4224 memcpy(&pc, r, left);
4225 outs() << " (protocol_t entends past the end of the section)\n";
4226 } else
4227 memcpy(&pc, r, sizeof(struct protocol32_t));
4228 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4229 swapStruct(pc);
4230 outs() << "\t\t\t isa " << format("0x%" PRIx32"x", pc.isa) << "\n";
4231 outs() << "\t\t\t name " << format("0x%" PRIx32"x", pc.name);
4232 name = get_pointer_32(pc.name, xoffset, left, xS, info);
4233 if (name != nullptr)
4234 outs() << format(" %.*s", left, name);
4235 outs() << "\n";
4236 outs() << "\t\t\tprotocols " << format("0x%" PRIx32"x", pc.protocols) << "\n";
4237 outs() << "\t\t instanceMethods "
4238 << format("0x%" PRIx32"x", pc.instanceMethods)
4239 << " (struct method_list_t *)\n";
4240 if (pc.instanceMethods != 0)
4241 print_method_list32_t(pc.instanceMethods, info, "\t");
4242 outs() << "\t\t classMethods " << format("0x%" PRIx32"x", pc.classMethods)
4243 << " (struct method_list_t *)\n";
4244 if (pc.classMethods != 0)
4245 print_method_list32_t(pc.classMethods, info, "\t");
4246 outs() << "\t optionalInstanceMethods "
4247 << format("0x%" PRIx32"x", pc.optionalInstanceMethods) << "\n";
4248 outs() << "\t optionalClassMethods "
4249 << format("0x%" PRIx32"x", pc.optionalClassMethods) << "\n";
4250 outs() << "\t instanceProperties "
4251 << format("0x%" PRIx32"x", pc.instanceProperties) << "\n";
4252 p += sizeof(uint32_t);
4253 offset += sizeof(uint32_t);
4254 }
4255}
4256
4257static void print_indent(uint32_t indent) {
4258 for (uint32_t i = 0; i < indent;) {
4259 if (indent - i >= 8) {
4260 outs() << "\t";
4261 i += 8;
4262 } else {
4263 for (uint32_t j = i; j < indent; j++)
4264 outs() << " ";
4265 return;
4266 }
4267 }
4268}
4269
4270static bool print_method_description_list(uint32_t p, uint32_t indent,
4271 struct DisassembleInfo *info) {
4272 uint32_t offset, left, xleft;
4273 SectionRef S;
4274 struct objc_method_description_list_t mdl;
4275 struct objc_method_description_t md;
4276 const char *r, *list, *name;
4277 int32_t i;
4278
4279 r = get_pointer_32(p, offset, left, S, info, true);
4280 if (r == nullptr)
4281 return true;
4282
4283 outs() << "\n";
4284 if (left > sizeof(struct objc_method_description_list_t)) {
4285 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
4286 } else {
4287 print_indent(indent);
4288 outs() << " objc_method_description_list extends past end of the section\n";
4289 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
4290 memcpy(&mdl, r, left);
4291 }
4292 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4293 swapStruct(mdl);
4294
4295 print_indent(indent);
4296 outs() << " count " << mdl.count << "\n";
4297
4298 list = r + sizeof(struct objc_method_description_list_t);
4299 for (i = 0; i < mdl.count; i++) {
4300 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
4301 print_indent(indent);
4302 outs() << " remaining list entries extend past the of the section\n";
4303 break;
4304 }
4305 print_indent(indent);
4306 outs() << " list[" << i << "]\n";
4307 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
4308 sizeof(struct objc_method_description_t));
4309 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4310 swapStruct(md);
4311
4312 print_indent(indent);
4313 outs() << " name " << format("0x%08" PRIx32"x", md.name);
4314 if (info->verbose) {
4315 name = get_pointer_32(md.name, offset, xleft, S, info, true);
4316 if (name != nullptr)
4317 outs() << format(" %.*s", xleft, name);
4318 else
4319 outs() << " (not in an __OBJC section)";
4320 }
4321 outs() << "\n";
4322
4323 print_indent(indent);
4324 outs() << " types " << format("0x%08" PRIx32"x", md.types);
4325 if (info->verbose) {
4326 name = get_pointer_32(md.types, offset, xleft, S, info, true);
4327 if (name != nullptr)
4328 outs() << format(" %.*s", xleft, name);
4329 else
4330 outs() << " (not in an __OBJC section)";
4331 }
4332 outs() << "\n";
4333 }
4334 return false;
4335}
4336
4337static bool print_protocol_list(uint32_t p, uint32_t indent,
4338 struct DisassembleInfo *info);
4339
4340static bool print_protocol(uint32_t p, uint32_t indent,
4341 struct DisassembleInfo *info) {
4342 uint32_t offset, left;
4343 SectionRef S;
4344 struct objc_protocol_t protocol;
4345 const char *r, *name;
4346
4347 r = get_pointer_32(p, offset, left, S, info, true);
4348 if (r == nullptr)
4349 return true;
4350
4351 outs() << "\n";
4352 if (left >= sizeof(struct objc_protocol_t)) {
4353 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
4354 } else {
4355 print_indent(indent);
4356 outs() << " Protocol extends past end of the section\n";
4357 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
4358 memcpy(&protocol, r, left);
4359 }
4360 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4361 swapStruct(protocol);
4362
4363 print_indent(indent);
4364 outs() << " isa " << format("0x%08" PRIx32"x", protocol.isa)
4365 << "\n";
4366
4367 print_indent(indent);
4368 outs() << " protocol_name "
4369 << format("0x%08" PRIx32"x", protocol.protocol_name);
4370 if (info->verbose) {
4371 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
4372 if (name != nullptr)
4373 outs() << format(" %.*s", left, name);
4374 else
4375 outs() << " (not in an __OBJC section)";
4376 }
4377 outs() << "\n";
4378
4379 print_indent(indent);
4380 outs() << " protocol_list "
4381 << format("0x%08" PRIx32"x", protocol.protocol_list);
4382 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
4383 outs() << " (not in an __OBJC section)\n";
4384
4385 print_indent(indent);
4386 outs() << " instance_methods "
4387 << format("0x%08" PRIx32"x", protocol.instance_methods);
4388 if (print_method_description_list(protocol.instance_methods, indent, info))
4389 outs() << " (not in an __OBJC section)\n";
4390
4391 print_indent(indent);
4392 outs() << " class_methods "
4393 << format("0x%08" PRIx32"x", protocol.class_methods);
4394 if (print_method_description_list(protocol.class_methods, indent, info))
4395 outs() << " (not in an __OBJC section)\n";
4396
4397 return false;
4398}
4399
4400static bool print_protocol_list(uint32_t p, uint32_t indent,
4401 struct DisassembleInfo *info) {
4402 uint32_t offset, left, l;
4403 SectionRef S;
4404 struct objc_protocol_list_t protocol_list;
4405 const char *r, *list;
4406 int32_t i;
4407
4408 r = get_pointer_32(p, offset, left, S, info, true);
4409 if (r == nullptr)
4410 return true;
4411
4412 outs() << "\n";
4413 if (left > sizeof(struct objc_protocol_list_t)) {
4414 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
4415 } else {
4416 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4417 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
4418 memcpy(&protocol_list, r, left);
4419 }
4420 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4421 swapStruct(protocol_list);
4422
4423 print_indent(indent);
4424 outs() << " next " << format("0x%08" PRIx32"x", protocol_list.next)
4425 << "\n";
4426 print_indent(indent);
4427 outs() << " count " << protocol_list.count << "\n";
4428
4429 list = r + sizeof(struct objc_protocol_list_t);
4430 for (i = 0; i < protocol_list.count; i++) {
4431 if ((i + 1) * sizeof(uint32_t) > left) {
4432 outs() << "\t\t remaining list entries extend past the of the section\n";
4433 break;
4434 }
4435 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
4436 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4437 sys::swapByteOrder(l);
4438
4439 print_indent(indent);
4440 outs() << " list[" << i << "] " << format("0x%08" PRIx32"x", l);
4441 if (print_protocol(l, indent, info))
4442 outs() << "(not in an __OBJC section)\n";
4443 }
4444 return false;
4445}
4446
4447static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
4448 struct ivar_list64_t il;
4449 struct ivar64_t i;
4450 const char *r;
4451 uint32_t offset, xoffset, left, j;
4452 SectionRef S, xS;
4453 const char *name, *sym_name, *ivar_offset_p;
4454 uint64_t ivar_offset, n_value;
4455
4456 r = get_pointer_64(p, offset, left, S, info);
4457 if (r == nullptr)
4458 return;
4459 memset(&il, '\0', sizeof(struct ivar_list64_t));
4460 if (left < sizeof(struct ivar_list64_t)) {
4461 memcpy(&il, r, left);
4462 outs() << " (ivar_list_t entends past the end of the section)\n";
4463 } else
4464 memcpy(&il, r, sizeof(struct ivar_list64_t));
4465 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4466 swapStruct(il);
4467 outs() << " entsize " << il.entsize << "\n";
4468 outs() << " count " << il.count << "\n";
4469
4470 p += sizeof(struct ivar_list64_t);
4471 offset += sizeof(struct ivar_list64_t);
4472 for (j = 0; j < il.count; j++) {
4473 r = get_pointer_64(p, offset, left, S, info);
4474 if (r == nullptr)
4475 return;
4476 memset(&i, '\0', sizeof(struct ivar64_t));
4477 if (left < sizeof(struct ivar64_t)) {
4478 memcpy(&i, r, left);
4479 outs() << " (ivar_t entends past the end of the section)\n";
4480 } else
4481 memcpy(&i, r, sizeof(struct ivar64_t));
4482 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4483 swapStruct(i);
4484
4485 outs() << "\t\t\t offset ";
4486 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset)__builtin_offsetof(struct ivar64_t, offset), S,
4487 info, n_value, i.offset);
4488 if (n_value != 0) {
4489 if (info->verbose && sym_name != nullptr)
4490 outs() << sym_name;
4491 else
4492 outs() << format("0x%" PRIx64"l" "x", n_value);
4493 if (i.offset != 0)
4494 outs() << " + " << format("0x%" PRIx64"l" "x", i.offset);
4495 } else
4496 outs() << format("0x%" PRIx64"l" "x", i.offset);
4497 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4498 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4499 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4500 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4501 sys::swapByteOrder(ivar_offset);
4502 outs() << " " << ivar_offset << "\n";
4503 } else
4504 outs() << "\n";
4505
4506 outs() << "\t\t\t name ";
4507 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name)__builtin_offsetof(struct ivar64_t, name), S, info,
4508 n_value, i.name);
4509 if (n_value != 0) {
4510 if (info->verbose && sym_name != nullptr)
4511 outs() << sym_name;
4512 else
4513 outs() << format("0x%" PRIx64"l" "x", n_value);
4514 if (i.name != 0)
4515 outs() << " + " << format("0x%" PRIx64"l" "x", i.name);
4516 } else
4517 outs() << format("0x%" PRIx64"l" "x", i.name);
4518 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4519 if (name != nullptr)
4520 outs() << format(" %.*s", left, name);
4521 outs() << "\n";
4522
4523 outs() << "\t\t\t type ";
4524 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type)__builtin_offsetof(struct ivar64_t, type), S, info,
4525 n_value, i.name);
4526 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4527 if (n_value != 0) {
4528 if (info->verbose && sym_name != nullptr)
4529 outs() << sym_name;
4530 else
4531 outs() << format("0x%" PRIx64"l" "x", n_value);
4532 if (i.type != 0)
4533 outs() << " + " << format("0x%" PRIx64"l" "x", i.type);
4534 } else
4535 outs() << format("0x%" PRIx64"l" "x", i.type);
4536 if (name != nullptr)
4537 outs() << format(" %.*s", left, name);
4538 outs() << "\n";
4539
4540 outs() << "\t\t\talignment " << i.alignment << "\n";
4541 outs() << "\t\t\t size " << i.size << "\n";
4542
4543 p += sizeof(struct ivar64_t);
4544 offset += sizeof(struct ivar64_t);
4545 }
4546}
4547
4548static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4549 struct ivar_list32_t il;
4550 struct ivar32_t i;
4551 const char *r;
4552 uint32_t offset, xoffset, left, j;
4553 SectionRef S, xS;
4554 const char *name, *ivar_offset_p;
4555 uint32_t ivar_offset;
4556
4557 r = get_pointer_32(p, offset, left, S, info);
4558 if (r == nullptr)
4559 return;
4560 memset(&il, '\0', sizeof(struct ivar_list32_t));
4561 if (left < sizeof(struct ivar_list32_t)) {
4562 memcpy(&il, r, left);
4563 outs() << " (ivar_list_t entends past the end of the section)\n";
4564 } else
4565 memcpy(&il, r, sizeof(struct ivar_list32_t));
4566 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4567 swapStruct(il);
4568 outs() << " entsize " << il.entsize << "\n";
4569 outs() << " count " << il.count << "\n";
4570
4571 p += sizeof(struct ivar_list32_t);
4572 offset += sizeof(struct ivar_list32_t);
4573 for (j = 0; j < il.count; j++) {
4574 r = get_pointer_32(p, offset, left, S, info);
4575 if (r == nullptr)
4576 return;
4577 memset(&i, '\0', sizeof(struct ivar32_t));
4578 if (left < sizeof(struct ivar32_t)) {
4579 memcpy(&i, r, left);
4580 outs() << " (ivar_t entends past the end of the section)\n";
4581 } else
4582 memcpy(&i, r, sizeof(struct ivar32_t));
4583 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4584 swapStruct(i);
4585
4586 outs() << "\t\t\t offset " << format("0x%" PRIx32"x", i.offset);
4587 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4588 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4589 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4590 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4591 sys::swapByteOrder(ivar_offset);
4592 outs() << " " << ivar_offset << "\n";
4593 } else
4594 outs() << "\n";
4595
4596 outs() << "\t\t\t name " << format("0x%" PRIx32"x", i.name);
4597 name = get_pointer_32(i.name, xoffset, left, xS, info);
4598 if (name != nullptr)
4599 outs() << format(" %.*s", left, name);
4600 outs() << "\n";
4601
4602 outs() << "\t\t\t type " << format("0x%" PRIx32"x", i.type);
4603 name = get_pointer_32(i.type, xoffset, left, xS, info);
4604 if (name != nullptr)
4605 outs() << format(" %.*s", left, name);
4606 outs() << "\n";
4607
4608 outs() << "\t\t\talignment " << i.alignment << "\n";
4609 outs() << "\t\t\t size " << i.size << "\n";
4610
4611 p += sizeof(struct ivar32_t);
4612 offset += sizeof(struct ivar32_t);
4613 }
4614}
4615
4616static void print_objc_property_list64(uint64_t p,
4617 struct DisassembleInfo *info) {
4618 struct objc_property_list64 opl;
4619 struct objc_property64 op;
4620 const char *r;
4621 uint32_t offset, xoffset, left, j;
4622 SectionRef S, xS;
4623 const char *name, *sym_name;
4624 uint64_t n_value;
4625
4626 r = get_pointer_64(p, offset, left, S, info);
4627 if (r == nullptr)
4628 return;
4629 memset(&opl, '\0', sizeof(struct objc_property_list64));
4630 if (left < sizeof(struct objc_property_list64)) {
4631 memcpy(&opl, r, left);
4632 outs() << " (objc_property_list entends past the end of the section)\n";
4633 } else
4634 memcpy(&opl, r, sizeof(struct objc_property_list64));
4635 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4636 swapStruct(opl);
4637 outs() << " entsize " << opl.entsize << "\n";
4638 outs() << " count " << opl.count << "\n";
4639
4640 p += sizeof(struct objc_property_list64);
4641 offset += sizeof(struct objc_property_list64);
4642 for (j = 0; j < opl.count; j++) {
4643 r = get_pointer_64(p, offset, left, S, info);
4644 if (r == nullptr)
4645 return;
4646 memset(&op, '\0', sizeof(struct objc_property64));
4647 if (left < sizeof(struct objc_property64)) {
4648 memcpy(&op, r, left);
4649 outs() << " (objc_property entends past the end of the section)\n";
4650 } else
4651 memcpy(&op, r, sizeof(struct objc_property64));
4652 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4653 swapStruct(op);
4654
4655 outs() << "\t\t\t name ";
4656 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name)__builtin_offsetof(struct objc_property64, name), S,
4657 info, n_value, op.name);
4658 if (n_value != 0) {
4659 if (info->verbose && sym_name != nullptr)
4660 outs() << sym_name;
4661 else
4662 outs() << format("0x%" PRIx64"l" "x", n_value);
4663 if (op.name != 0)
4664 outs() << " + " << format("0x%" PRIx64"l" "x", op.name);
4665 } else
4666 outs() << format("0x%" PRIx64"l" "x", op.name);
4667 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4668 if (name != nullptr)
4669 outs() << format(" %.*s", left, name);
4670 outs() << "\n";
4671
4672 outs() << "\t\t\tattributes ";
4673 sym_name =
4674 get_symbol_64(offset + offsetof(struct objc_property64, attributes)__builtin_offsetof(struct objc_property64, attributes), S,
4675 info, n_value, op.attributes);
4676 if (n_value != 0) {
4677 if (info->verbose && sym_name != nullptr)
4678 outs() << sym_name;
4679 else
4680 outs() << format("0x%" PRIx64"l" "x", n_value);
4681 if (op.attributes != 0)
4682 outs() << " + " << format("0x%" PRIx64"l" "x", op.attributes);
4683 } else
4684 outs() << format("0x%" PRIx64"l" "x", op.attributes);
4685 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4686 if (name != nullptr)
4687 outs() << format(" %.*s", left, name);
4688 outs() << "\n";
4689
4690 p += sizeof(struct objc_property64);
4691 offset += sizeof(struct objc_property64);
4692 }
4693}
4694
4695static void print_objc_property_list32(uint32_t p,
4696 struct DisassembleInfo *info) {
4697 struct objc_property_list32 opl;
4698 struct objc_property32 op;
4699 const char *r;
4700 uint32_t offset, xoffset, left, j;
4701 SectionRef S, xS;
4702 const char *name;
4703
4704 r = get_pointer_32(p, offset, left, S, info);
4705 if (r == nullptr)
4706 return;
4707 memset(&opl, '\0', sizeof(struct objc_property_list32));
4708 if (left < sizeof(struct objc_property_list32)) {
4709 memcpy(&opl, r, left);
4710 outs() << " (objc_property_list entends past the end of the section)\n";
4711 } else
4712 memcpy(&opl, r, sizeof(struct objc_property_list32));
4713 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4714 swapStruct(opl);
4715 outs() << " entsize " << opl.entsize << "\n";
4716 outs() << " count " << opl.count << "\n";
4717
4718 p += sizeof(struct objc_property_list32);
4719 offset += sizeof(struct objc_property_list32);
4720 for (j = 0; j < opl.count; j++) {
4721 r = get_pointer_32(p, offset, left, S, info);
4722 if (r == nullptr)
4723 return;
4724 memset(&op, '\0', sizeof(struct objc_property32));
4725 if (left < sizeof(struct objc_property32)) {
4726 memcpy(&op, r, left);
4727 outs() << " (objc_property entends past the end of the section)\n";
4728 } else
4729 memcpy(&op, r, sizeof(struct objc_property32));
4730 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4731 swapStruct(op);
4732
4733 outs() << "\t\t\t name " << format("0x%" PRIx32"x", op.name);
4734 name = get_pointer_32(op.name, xoffset, left, xS, info);
4735 if (name != nullptr)
4736 outs() << format(" %.*s", left, name);
4737 outs() << "\n";
4738
4739 outs() << "\t\t\tattributes " << format("0x%" PRIx32"x", op.attributes);
4740 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4741 if (name != nullptr)
4742 outs() << format(" %.*s", left, name);
4743 outs() << "\n";
4744
4745 p += sizeof(struct objc_property32);
4746 offset += sizeof(struct objc_property32);
4747 }
4748}
4749
4750static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4751 bool &is_meta_class) {
4752 struct class_ro64_t cro;
4753 const char *r;
4754 uint32_t offset, xoffset, left;
4755 SectionRef S, xS;
4756 const char *name, *sym_name;
4757 uint64_t n_value;
4758
4759 r = get_pointer_64(p, offset, left, S, info);
4760 if (r == nullptr || left < sizeof(struct class_ro64_t))
4761 return false;
4762 memcpy(&cro, r, sizeof(struct class_ro64_t));
4763 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4764 swapStruct(cro);
4765 outs() << " flags " << format("0x%" PRIx32"x", cro.flags);
4766 if (cro.flags & RO_META(1 << 0))
4767 outs() << " RO_META";
4768 if (cro.flags & RO_ROOT(1 << 1))
4769 outs() << " RO_ROOT";
4770 if (cro.flags & RO_HAS_CXX_STRUCTORS(1 << 2))
4771 outs() << " RO_HAS_CXX_STRUCTORS";
4772 outs() << "\n";
4773 outs() << " instanceStart " << cro.instanceStart << "\n";
4774 outs() << " instanceSize " << cro.instanceSize << "\n";
4775 outs() << " reserved " << format("0x%" PRIx32"x", cro.reserved)
4776 << "\n";
4777 outs() << " ivarLayout " << format("0x%" PRIx64"l" "x", cro.ivarLayout)
4778 << "\n";
4779 print_layout_map64(cro.ivarLayout, info);
4780
4781 outs() << " name ";
4782 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name)__builtin_offsetof(struct class_ro64_t, name), S,
4783 info, n_value, cro.name);
4784 if (n_value != 0) {
4785 if (info->verbose && sym_name != nullptr)
4786 outs() << sym_name;
4787 else
4788 outs() << format("0x%" PRIx64"l" "x", n_value);
4789 if (cro.name != 0)
4790 outs() << " + " << format("0x%" PRIx64"l" "x", cro.name);
4791 } else
4792 outs() << format("0x%" PRIx64"l" "x", cro.name);
4793 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4794 if (name != nullptr)
4795 outs() << format(" %.*s", left, name);
4796 outs() << "\n";
4797
4798 outs() << " baseMethods ";
4799 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods)__builtin_offsetof(struct class_ro64_t, baseMethods),
4800 S, info, n_value, cro.baseMethods);
4801 if (n_value != 0) {
4802 if (info->verbose && sym_name != nullptr)
4803 outs() << sym_name;
4804 else
4805 outs() << format("0x%" PRIx64"l" "x", n_value);
4806 if (cro.baseMethods != 0)
4807 outs() << " + " << format("0x%" PRIx64"l" "x", cro.baseMethods);
4808 } else
4809 outs() << format("0x%" PRIx64"l" "x", cro.baseMethods);
4810 outs() << " (struct method_list_t *)\n";
4811 if (cro.baseMethods + n_value != 0)
4812 print_method_list64_t(cro.baseMethods + n_value, info, "");
4813
4814 outs() << " baseProtocols ";
4815 sym_name =
4816 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols)__builtin_offsetof(struct class_ro64_t, baseProtocols), S,
4817 info, n_value, cro.baseProtocols);
4818 if (n_value != 0) {
4819 if (info->verbose && sym_name != nullptr)
4820 outs() << sym_name;
4821 else
4822 outs() << format("0x%" PRIx64"l" "x", n_value);
4823 if (cro.baseProtocols != 0)
4824 outs() << " + " << format("0x%" PRIx64"l" "x", cro.baseProtocols);
4825 } else
4826 outs() << format("0x%" PRIx64"l" "x", cro.baseProtocols);
4827 outs() << "\n";
4828 if (cro.baseProtocols + n_value != 0)
4829 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4830
4831 outs() << " ivars ";
4832 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars)__builtin_offsetof(struct class_ro64_t, ivars), S,
4833 info, n_value, cro.ivars);
4834 if (n_value != 0) {
4835 if (info->verbose && sym_name != nullptr)
4836 outs() << sym_name;
4837 else
4838 outs() << format("0x%" PRIx64"l" "x", n_value);
4839 if (cro.ivars != 0)
4840 outs() << " + " << format("0x%" PRIx64"l" "x", cro.ivars);
4841 } else
4842 outs() << format("0x%" PRIx64"l" "x", cro.ivars);
4843 outs() << "\n";
4844 if (cro.ivars + n_value != 0)
4845 print_ivar_list64_t(cro.ivars + n_value, info);
4846
4847 outs() << " weakIvarLayout ";
4848 sym_name =
4849 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout)__builtin_offsetof(struct class_ro64_t, weakIvarLayout), S,
4850 info, n_value, cro.weakIvarLayout);
4851 if (n_value != 0) {
4852 if (info->verbose && sym_name != nullptr)
4853 outs() << sym_name;
4854 else
4855 outs() << format("0x%" PRIx64"l" "x", n_value);
4856 if (cro.weakIvarLayout != 0)
4857 outs() << " + " << format("0x%" PRIx64"l" "x", cro.weakIvarLayout);
4858 } else
4859 outs() << format("0x%" PRIx64"l" "x", cro.weakIvarLayout);
4860 outs() << "\n";
4861 print_layout_map64(cro.weakIvarLayout + n_value, info);
4862
4863 outs() << " baseProperties ";
4864 sym_name =
4865 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties)__builtin_offsetof(struct class_ro64_t, baseProperties), S,
4866 info, n_value, cro.baseProperties);
4867 if (n_value != 0) {
4868 if (info->verbose && sym_name != nullptr)
4869 outs() << sym_name;
4870 else
4871 outs() << format("0x%" PRIx64"l" "x", n_value);
4872 if (cro.baseProperties != 0)
4873 outs() << " + " << format("0x%" PRIx64"l" "x", cro.baseProperties);
4874 } else
4875 outs() << format("0x%" PRIx64"l" "x", cro.baseProperties);
4876 outs() << "\n";
4877 if (cro.baseProperties + n_value != 0)
4878 print_objc_property_list64(cro.baseProperties + n_value, info);
4879
4880 is_meta_class = (cro.flags & RO_META(1 << 0)) != 0;
4881 return true;
4882}
4883
4884static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4885 bool &is_meta_class) {
4886 struct class_ro32_t cro;
4887 const char *r;
4888 uint32_t offset, xoffset, left;
4889 SectionRef S, xS;
4890 const char *name;
4891
4892 r = get_pointer_32(p, offset, left, S, info);
4893 if (r == nullptr)
4894 return false;
4895 memset(&cro, '\0', sizeof(struct class_ro32_t));
4896 if (left < sizeof(struct class_ro32_t)) {
4897 memcpy(&cro, r, left);
4898 outs() << " (class_ro_t entends past the end of the section)\n";
4899 } else
4900 memcpy(&cro, r, sizeof(struct class_ro32_t));
4901 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4902 swapStruct(cro);
4903 outs() << " flags " << format("0x%" PRIx32"x", cro.flags);
4904 if (cro.flags & RO_META(1 << 0))
4905 outs() << " RO_META";
4906 if (cro.flags & RO_ROOT(1 << 1))
4907 outs() << " RO_ROOT";
4908 if (cro.flags & RO_HAS_CXX_STRUCTORS(1 << 2))
4909 outs() << " RO_HAS_CXX_STRUCTORS";
4910 outs() << "\n";
4911 outs() << " instanceStart " << cro.instanceStart << "\n";
4912 outs() << " instanceSize " << cro.instanceSize << "\n";
4913 outs() << " ivarLayout " << format("0x%" PRIx32"x", cro.ivarLayout)
4914 << "\n";
4915 print_layout_map32(cro.ivarLayout, info);
4916
4917 outs() << " name " << format("0x%" PRIx32"x", cro.name);
4918 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4919 if (name != nullptr)
4920 outs() << format(" %.*s", left, name);
4921 outs() << "\n";
4922
4923 outs() << " baseMethods "
4924 << format("0x%" PRIx32"x", cro.baseMethods)
4925 << " (struct method_list_t *)\n";
4926 if (cro.baseMethods != 0)
4927 print_method_list32_t(cro.baseMethods, info, "");
4928
4929 outs() << " baseProtocols "
4930 << format("0x%" PRIx32"x", cro.baseProtocols) << "\n";
4931 if (cro.baseProtocols != 0)
4932 print_protocol_list32_t(cro.baseProtocols, info);
4933 outs() << " ivars " << format("0x%" PRIx32"x", cro.ivars)
4934 << "\n";
4935 if (cro.ivars != 0)
4936 print_ivar_list32_t(cro.ivars, info);
4937 outs() << " weakIvarLayout "
4938 << format("0x%" PRIx32"x", cro.weakIvarLayout) << "\n";
4939 print_layout_map32(cro.weakIvarLayout, info);
4940 outs() << " baseProperties "
4941 << format("0x%" PRIx32"x", cro.baseProperties) << "\n";
4942 if (cro.baseProperties != 0)
4943 print_objc_property_list32(cro.baseProperties, info);
4944 is_meta_class = (cro.flags & RO_META(1 << 0)) != 0;
4945 return true;
4946}
4947
4948static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4949 struct class64_t c;
4950 const char *r;
4951 uint32_t offset, left;
4952 SectionRef S;
4953 const char *name;
4954 uint64_t isa_n_value, n_value;
4955
4956 r = get_pointer_64(p, offset, left, S, info);
4957 if (r == nullptr || left < sizeof(struct class64_t))
4958 return;
4959 memcpy(&c, r, sizeof(struct class64_t));
4960 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4961 swapStruct(c);
4962
4963 outs() << " isa " << format("0x%" PRIx64"l" "x", c.isa);
4964 name = get_symbol_64(offset + offsetof(struct class64_t, isa)__builtin_offsetof(struct class64_t, isa), S, info,
4965 isa_n_value, c.isa);
4966 if (name != nullptr)
4967 outs() << " " << name;
4968 outs() << "\n";
4969
4970 outs() << " superclass " << format("0x%" PRIx64"l" "x", c.superclass);
4971 name = get_symbol_64(offset + offsetof(struct class64_t, superclass)__builtin_offsetof(struct class64_t, superclass), S, info,
4972 n_value, c.superclass);
4973 if (name != nullptr)
4974 outs() << " " << name;
4975 else {
4976 name = get_dyld_bind_info_symbolname(S.getAddress() +
4977 offset + offsetof(struct class64_t, superclass)__builtin_offsetof(struct class64_t, superclass), info);
4978 if (name != nullptr)
4979 outs() << " " << name;
4980 }
4981 outs() << "\n";
4982
4983 outs() << " cache " << format("0x%" PRIx64"l" "x", c.cache);
4984 name = get_symbol_64(offset + offsetof(struct class64_t, cache)__builtin_offsetof(struct class64_t, cache), S, info,
4985 n_value, c.cache);
4986 if (name != nullptr)
4987 outs() << " " << name;
4988 outs() << "\n";
4989
4990 outs() << " vtable " << format("0x%" PRIx64"l" "x", c.vtable);
4991 name = get_symbol_64(offset + offsetof(struct class64_t, vtable)__builtin_offsetof(struct class64_t, vtable), S, info,
4992 n_value, c.vtable);
4993 if (name != nullptr)
4994 outs() << " " << name;
4995 outs() << "\n";
4996
4997 name = get_symbol_64(offset + offsetof(struct class64_t, data)__builtin_offsetof(struct class64_t, data), S, info,
4998 n_value, c.data);
4999 outs() << " data ";
5000 if (n_value != 0) {
5001 if (info->verbose && name != nullptr)
5002 outs() << name;
5003 else
5004 outs() << format("0x%" PRIx64"l" "x", n_value);
5005 if (c.data != 0)
5006 outs() << " + " << format("0x%" PRIx64"l" "x", c.data);
5007 } else
5008 outs() << format("0x%" PRIx64"l" "x", c.data);
5009 outs() << " (struct class_ro_t *)";
5010
5011 // This is a Swift class if some of the low bits of the pointer are set.
5012 if ((c.data + n_value) & 0x7)
5013 outs() << " Swift class";
5014 outs() << "\n";
5015 bool is_meta_class;
5016 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
5017 return;
5018
5019 if (!is_meta_class &&
5020 c.isa + isa_n_value != p &&
5021 c.isa + isa_n_value != 0 &&
5022 info->depth < 100) {
5023 info->depth++;
5024 outs() << "Meta Class\n";
5025 print_class64_t(c.isa + isa_n_value, info);
5026 }
5027}
5028
5029static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
5030 struct class32_t c;
5031 const char *r;
5032 uint32_t offset, left;
5033 SectionRef S;
5034 const char *name;
5035
5036 r = get_pointer_32(p, offset, left, S, info);
5037 if (r == nullptr)
5038 return;
5039 memset(&c, '\0', sizeof(struct class32_t));
5040 if (left < sizeof(struct class32_t)) {
5041 memcpy(&c, r, left);
5042 outs() << " (class_t entends past the end of the section)\n";
5043 } else
5044 memcpy(&c, r, sizeof(struct class32_t));
5045 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5046 swapStruct(c);
5047
5048 outs() << " isa " << format("0x%" PRIx32"x", c.isa);
5049 name =
5050 get_symbol_32(offset + offsetof(struct class32_t, isa)__builtin_offsetof(struct class32_t, isa), S, info, c.isa);
5051 if (name != nullptr)
5052 outs() << " " << name;
5053 outs() << "\n";
5054
5055 outs() << " superclass " << format("0x%" PRIx32"x", c.superclass);
5056 name = get_symbol_32(offset + offsetof(struct class32_t, superclass)__builtin_offsetof(struct class32_t, superclass), S, info,
5057 c.superclass);
5058 if (name != nullptr)
5059 outs() << " " << name;
5060 outs() << "\n";
5061
5062 outs() << " cache " << format("0x%" PRIx32"x", c.cache);
5063 name = get_symbol_32(offset + offsetof(struct class32_t, cache)__builtin_offsetof(struct class32_t, cache), S, info,
5064 c.cache);
5065 if (name != nullptr)
5066 outs() << " " << name;
5067 outs() << "\n";
5068
5069 outs() << " vtable " << format("0x%" PRIx32"x", c.vtable);
5070 name = get_symbol_32(offset + offsetof(struct class32_t, vtable)__builtin_offsetof(struct class32_t, vtable), S, info,
5071 c.vtable);
5072 if (name != nullptr)
5073 outs() << " " << name;
5074 outs() << "\n";
5075
5076 name =
5077 get_symbol_32(offset + offsetof(struct class32_t, data)__builtin_offsetof(struct class32_t, data), S, info, c.data);
5078 outs() << " data " << format("0x%" PRIx32"x", c.data)
5079 << " (struct class_ro_t *)";
5080
5081 // This is a Swift class if some of the low bits of the pointer are set.
5082 if (c.data & 0x3)
5083 outs() << " Swift class";
5084 outs() << "\n";
5085 bool is_meta_class;
5086 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
5087 return;
5088
5089 if (!is_meta_class) {
5090 outs() << "Meta Class\n";
5091 print_class32_t(c.isa, info);
5092 }
5093}
5094
5095static void print_objc_class_t(struct objc_class_t *objc_class,
5096 struct DisassembleInfo *info) {
5097 uint32_t offset, left, xleft;
5098 const char *name, *p, *ivar_list;
5099 SectionRef S;
5100 int32_t i;
5101 struct objc_ivar_list_t objc_ivar_list;
5102 struct objc_ivar_t ivar;
5103
5104 outs() << "\t\t isa " << format("0x%08" PRIx32"x", objc_class->isa);
5105 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)((objc_class)->info & (0x2))) {
5106 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
5107 if (name != nullptr)
5108 outs() << format(" %.*s", left, name);
5109 else
5110 outs() << " (not in an __OBJC section)";
5111 }
5112 outs() << "\n";
5113
5114 outs() << "\t super_class "
5115 << format("0x%08" PRIx32"x", objc_class->super_class);
5116 if (info->verbose) {
5117 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
5118 if (name != nullptr)
5119 outs() << format(" %.*s", left, name);
5120 else
5121 outs() << " (not in an __OBJC section)";
5122 }
5123 outs() << "\n";
5124
5125 outs() << "\t\t name " << format("0x%08" PRIx32"x", objc_class->name);
5126 if (info->verbose) {
5127 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
5128 if (name != nullptr)
5129 outs() << format(" %.*s", left, name);
5130 else
5131 outs() << " (not in an __OBJC section)";
5132 }
5133 outs() << "\n";
5134
5135 outs() << "\t\t version " << format("0x%08" PRIx32"x", objc_class->version)
5136 << "\n";
5137
5138 outs() << "\t\t info " << format("0x%08" PRIx32"x", objc_class->info);
5139 if (info->verbose) {
5140 if (CLS_GETINFO(objc_class, CLS_CLASS)((objc_class)->info & (0x1)))
5141 outs() << " CLS_CLASS";
5142 else if (CLS_GETINFO(objc_class, CLS_META)((objc_class)->info & (0x2)))
5143 outs() << " CLS_META";
5144 }
5145 outs() << "\n";
5146
5147 outs() << "\t instance_size "
5148 << format("0x%08" PRIx32"x", objc_class->instance_size) << "\n";
5149
5150 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
5151 outs() << "\t\t ivars " << format("0x%08" PRIx32"x", objc_class->ivars);
5152 if (p != nullptr) {
5153 if (left > sizeof(struct objc_ivar_list_t)) {
5154 outs() << "\n";
5155 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
5156 } else {
5157 outs() << " (entends past the end of the section)\n";
5158 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
5159 memcpy(&objc_ivar_list, p, left);
5160 }
5161 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5162 swapStruct(objc_ivar_list);
5163 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
5164 ivar_list = p + sizeof(struct objc_ivar_list_t);
5165 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
5166 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
5167 outs() << "\t\t remaining ivar's extend past the of the section\n";
5168 break;
5169 }
5170 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
5171 sizeof(struct objc_ivar_t));
5172 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5173 swapStruct(ivar);
5174
5175 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32"x", ivar.ivar_name);
5176 if (info->verbose) {
5177 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
5178 if (name != nullptr)
5179 outs() << format(" %.*s", xleft, name);
5180 else
5181 outs() << " (not in an __OBJC section)";
5182 }
5183 outs() << "\n";
5184
5185 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32"x", ivar.ivar_type);
5186 if (info->verbose) {
5187 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
5188 if (name != nullptr)
5189 outs() << format(" %.*s", xleft, name);
5190 else
5191 outs() << " (not in an __OBJC section)";
5192 }
5193 outs() << "\n";
5194
5195 outs() << "\t\t ivar_offset "
5196 << format("0x%08" PRIx32"x", ivar.ivar_offset) << "\n";
5197 }
5198 } else {
5199 outs() << " (not in an __OBJC section)\n";
5200 }
5201
5202 outs() << "\t\t methods " << format("0x%08" PRIx32"x", objc_class->methodLists);
5203 if (print_method_list(objc_class->methodLists, info))
5204 outs() << " (not in an __OBJC section)\n";
5205
5206 outs() << "\t\t cache " << format("0x%08" PRIx32"x", objc_class->cache)
5207 << "\n";
5208
5209 outs() << "\t\tprotocols " << format("0x%08" PRIx32"x", objc_class->protocols);
5210 if (print_protocol_list(objc_class->protocols, 16, info))
5211 outs() << " (not in an __OBJC section)\n";
5212}
5213
5214static void print_objc_objc_category_t(struct objc_category_t *objc_category,
5215 struct DisassembleInfo *info) {
5216 uint32_t offset, left;
5217 const char *name;
5218 SectionRef S;
5219
5220 outs() << "\t category name "
5221 << format("0x%08" PRIx32"x", objc_category->category_name);
5222 if (info->verbose) {
5223 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
5224 true);
5225 if (name != nullptr)
5226 outs() << format(" %.*s", left, name);
5227 else
5228 outs() << " (not in an __OBJC section)";
5229 }
5230 outs() << "\n";
5231
5232 outs() << "\t\t class name "
5233 << format("0x%08" PRIx32"x", objc_category->class_name);
5234 if (info->verbose) {
5235 name =
5236 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
5237 if (name != nullptr)
5238 outs() << format(" %.*s", left, name);
5239 else
5240 outs() << " (not in an __OBJC section)";
5241 }
5242 outs() << "\n";
5243
5244 outs() << "\t instance methods "
5245 << format("0x%08" PRIx32"x", objc_category->instance_methods);
5246 if (print_method_list(objc_category->instance_methods, info))
5247 outs() << " (not in an __OBJC section)\n";
5248
5249 outs() << "\t class methods "
5250 << format("0x%08" PRIx32"x", objc_category->class_methods);
5251 if (print_method_list(objc_category->class_methods, info))
5252 outs() << " (not in an __OBJC section)\n";
5253}
5254
5255static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
5256 struct category64_t c;
5257 const char *r;
5258 uint32_t offset, xoffset, left;
5259 SectionRef S, xS;
5260 const char *name, *sym_name;
5261 uint64_t n_value;
5262
5263 r = get_pointer_64(p, offset, left, S, info);
5264 if (r == nullptr)
5265 return;
5266 memset(&c, '\0', sizeof(struct category64_t));
5267 if (left < sizeof(struct category64_t)) {
5268 memcpy(&c, r, left);
5269 outs() << " (category_t entends past the end of the section)\n";
5270 } else
5271 memcpy(&c, r, sizeof(struct category64_t));
5272 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5273 swapStruct(c);
5274
5275 outs() << " name ";
5276 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name)__builtin_offsetof(struct category64_t, name), S,
5277 info, n_value, c.name);
5278 if (n_value != 0) {
5279 if (info->verbose && sym_name != nullptr)
5280 outs() << sym_name;
5281 else
5282 outs() << format("0x%" PRIx64"l" "x", n_value);
5283 if (c.name != 0)
5284 outs() << " + " << format("0x%" PRIx64"l" "x", c.name);
5285 } else
5286 outs() << format("0x%" PRIx64"l" "x", c.name);
5287 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
5288 if (name != nullptr)
5289 outs() << format(" %.*s", left, name);
5290 outs() << "\n";
5291
5292 outs() << " cls ";
5293 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls)__builtin_offsetof(struct category64_t, cls), S, info,
5294 n_value, c.cls);
5295 if (n_value != 0) {
5296 if (info->verbose && sym_name != nullptr)
5297 outs() << sym_name;
5298 else
5299 outs() << format("0x%" PRIx64"l" "x", n_value);
5300 if (c.cls != 0)
5301 outs() << " + " << format("0x%" PRIx64"l" "x", c.cls);
5302 } else
5303 outs() << format("0x%" PRIx64"l" "x", c.cls);
5304 outs() << "\n";
5305 if (c.cls + n_value != 0)
5306 print_class64_t(c.cls + n_value, info);
5307
5308 outs() << " instanceMethods ";
5309 sym_name =
5310 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods)__builtin_offsetof(struct category64_t, instanceMethods), S,
5311 info, n_value, c.instanceMethods);
5312 if (n_value != 0) {
5313 if (info->verbose && sym_name != nullptr)
5314 outs() << sym_name;
5315 else
5316 outs() << format("0x%" PRIx64"l" "x", n_value);
5317 if (c.instanceMethods != 0)
5318 outs() << " + " << format("0x%" PRIx64"l" "x", c.instanceMethods);
5319 } else
5320 outs() << format("0x%" PRIx64"l" "x", c.instanceMethods);
5321 outs() << "\n";
5322 if (c.instanceMethods + n_value != 0)
5323 print_method_list64_t(c.instanceMethods + n_value, info, "");
5324
5325 outs() << " classMethods ";
5326 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods)__builtin_offsetof(struct category64_t, classMethods),
5327 S, info, n_value, c.classMethods);
5328 if (n_value != 0) {
5329 if (info->verbose && sym_name != nullptr)
5330 outs() << sym_name;
5331 else
5332 outs() << format("0x%" PRIx64"l" "x", n_value);
5333 if (c.classMethods != 0)
5334 outs() << " + " << format("0x%" PRIx64"l" "x", c.classMethods);
5335 } else
5336 outs() << format("0x%" PRIx64"l" "x", c.classMethods);
5337 outs() << "\n";
5338 if (c.classMethods + n_value != 0)
5339 print_method_list64_t(c.classMethods + n_value, info, "");
5340
5341 outs() << " protocols ";
5342 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols)__builtin_offsetof(struct category64_t, protocols), S,
5343 info, n_value, c.protocols);
5344 if (n_value != 0) {
5345 if (info->verbose && sym_name != nullptr)
5346 outs() << sym_name;
5347 else
5348 outs() << format("0x%" PRIx64"l" "x", n_value);
5349 if (c.protocols != 0)
5350 outs() << " + " << format("0x%" PRIx64"l" "x", c.protocols);
5351 } else
5352 outs() << format("0x%" PRIx64"l" "x", c.protocols);
5353 outs() << "\n";
5354 if (c.protocols + n_value != 0)
5355 print_protocol_list64_t(c.protocols + n_value, info);
5356
5357 outs() << "instanceProperties ";
5358 sym_name =
5359 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties)__builtin_offsetof(struct category64_t, instanceProperties),
5360 S, info, n_value, c.instanceProperties);
5361 if (n_value != 0) {
5362 if (info->verbose && sym_name != nullptr)
5363 outs() << sym_name;
5364 else
5365 outs() << format("0x%" PRIx64"l" "x", n_value);
5366 if (c.instanceProperties != 0)
5367 outs() << " + " << format("0x%" PRIx64"l" "x", c.instanceProperties);
5368 } else
5369 outs() << format("0x%" PRIx64"l" "x", c.instanceProperties);
5370 outs() << "\n";
5371 if (c.instanceProperties + n_value != 0)
5372 print_objc_property_list64(c.instanceProperties + n_value, info);
5373}
5374
5375static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
5376 struct category32_t c;
5377 const char *r;
5378 uint32_t offset, left;
5379 SectionRef S, xS;
5380 const char *name;
5381
5382 r = get_pointer_32(p, offset, left, S, info);
5383 if (r == nullptr)
5384 return;
5385 memset(&c, '\0', sizeof(struct category32_t));
5386 if (left < sizeof(struct category32_t)) {
5387 memcpy(&c, r, left);
5388 outs() << " (category_t entends past the end of the section)\n";
5389 } else
5390 memcpy(&c, r, sizeof(struct category32_t));
5391 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5392 swapStruct(c);
5393
5394 outs() << " name " << format("0x%" PRIx32"x", c.name);
5395 name = get_symbol_32(offset + offsetof(struct category32_t, name)__builtin_offsetof(struct category32_t, name), S, info,
5396 c.name);
5397 if (name)
5398 outs() << " " << name;
5399 outs() << "\n";
5400
5401 outs() << " cls " << format("0x%" PRIx32"x", c.cls) << "\n";
5402 if (c.cls != 0)
5403 print_class32_t(c.cls, info);
5404 outs() << " instanceMethods " << format("0x%" PRIx32"x", c.instanceMethods)
5405 << "\n";
5406 if (c.instanceMethods != 0)
5407 print_method_list32_t(c.instanceMethods, info, "");
5408 outs() << " classMethods " << format("0x%" PRIx32"x", c.classMethods)
5409 << "\n";
5410 if (c.classMethods != 0)
5411 print_method_list32_t(c.classMethods, info, "");
5412 outs() << " protocols " << format("0x%" PRIx32"x", c.protocols) << "\n";
5413 if (c.protocols != 0)
5414 print_protocol_list32_t(c.protocols, info);
5415 outs() << "instanceProperties " << format("0x%" PRIx32"x", c.instanceProperties)
5416 << "\n";
5417 if (c.instanceProperties != 0)
5418 print_objc_property_list32(c.instanceProperties, info);
5419}
5420
5421static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
5422 uint32_t i, left, offset, xoffset;
5423 uint64_t p, n_value;
5424 struct message_ref64 mr;
5425 const char *name, *sym_name;
5426 const char *r;
5427 SectionRef xS;
5428
5429 if (S == SectionRef())
5430 return;
5431
5432 StringRef SectName;
5433 S.getName(SectName);
5434 DataRefImpl Ref = S.getRawDataRefImpl();
5435 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5436 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5437 offset = 0;
5438 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5439 p = S.getAddress() + i;
5440 r = get_pointer_64(p, offset, left, S, info);
5441 if (r == nullptr)
5442 return;
5443 memset(&mr, '\0', sizeof(struct message_ref64));
5444 if (left < sizeof(struct message_ref64)) {
5445 memcpy(&mr, r, left);
5446 outs() << " (message_ref entends past the end of the section)\n";
5447 } else
5448 memcpy(&mr, r, sizeof(struct message_ref64));
5449 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5450 swapStruct(mr);
5451
5452 outs() << " imp ";
5453 name = get_symbol_64(offset + offsetof(struct message_ref64, imp)__builtin_offsetof(struct message_ref64, imp), S, info,
5454 n_value, mr.imp);
5455 if (n_value != 0) {
5456 outs() << format("0x%" PRIx64"l" "x", n_value) << " ";
5457 if (mr.imp != 0)
5458 outs() << "+ " << format("0x%" PRIx64"l" "x", mr.imp) << " ";
5459 } else
5460 outs() << format("0x%" PRIx64"l" "x", mr.imp) << " ";
5461 if (name != nullptr)
5462 outs() << " " << name;
5463 outs() << "\n";
5464
5465 outs() << " sel ";
5466 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel)__builtin_offsetof(struct message_ref64, sel), S,
5467 info, n_value, mr.sel);
5468 if (n_value != 0) {
5469 if (info->verbose && sym_name != nullptr)
5470 outs() << sym_name;
5471 else
5472 outs() << format("0x%" PRIx64"l" "x", n_value);
5473 if (mr.sel != 0)
5474 outs() << " + " << format("0x%" PRIx64"l" "x", mr.sel);
5475 } else
5476 outs() << format("0x%" PRIx64"l" "x", mr.sel);
5477 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5478 if (name != nullptr)
5479 outs() << format(" %.*s", left, name);
5480 outs() << "\n";
5481
5482 offset += sizeof(struct message_ref64);
5483 }
5484}
5485
5486static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5487 uint32_t i, left, offset, xoffset, p;
5488 struct message_ref32 mr;
5489 const char *name, *r;
5490 SectionRef xS;
5491
5492 if (S == SectionRef())
5493 return;
5494
5495 StringRef SectName;
5496 S.getName(SectName);
5497 DataRefImpl Ref = S.getRawDataRefImpl();
5498 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5499 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5500 offset = 0;
5501 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5502 p = S.getAddress() + i;
5503 r = get_pointer_32(p, offset, left, S, info);
5504 if (r == nullptr)
5505 return;
5506 memset(&mr, '\0', sizeof(struct message_ref32));
5507 if (left < sizeof(struct message_ref32)) {
5508 memcpy(&mr, r, left);
5509 outs() << " (message_ref entends past the end of the section)\n";
5510 } else
5511 memcpy(&mr, r, sizeof(struct message_ref32));
5512 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5513 swapStruct(mr);
5514
5515 outs() << " imp " << format("0x%" PRIx32"x", mr.imp);
5516 name = get_symbol_32(offset + offsetof(struct message_ref32, imp)__builtin_offsetof(struct message_ref32, imp), S, info,
5517 mr.imp);
5518 if (name != nullptr)
5519 outs() << " " << name;
5520 outs() << "\n";
5521
5522 outs() << " sel " << format("0x%" PRIx32"x", mr.sel);
5523 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5524 if (name != nullptr)
5525 outs() << " " << name;
5526 outs() << "\n";
5527
5528 offset += sizeof(struct message_ref32);
5529 }
5530}
5531
5532static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5533 uint32_t left, offset, swift_version;
5534 uint64_t p;
5535 struct objc_image_info64 o;
5536 const char *r;
5537
5538 if (S == SectionRef())
5539 return;
5540
5541 StringRef SectName;
5542 S.getName(SectName);
5543 DataRefImpl Ref = S.getRawDataRefImpl();
5544 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5545 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5546 p = S.getAddress();
5547 r = get_pointer_64(p, offset, left, S, info);
5548 if (r == nullptr)
5549 return;
5550 memset(&o, '\0', sizeof(struct objc_image_info64));
5551 if (left < sizeof(struct objc_image_info64)) {
5552 memcpy(&o, r, left);
5553 outs() << " (objc_image_info entends past the end of the section)\n";
5554 } else
5555 memcpy(&o, r, sizeof(struct objc_image_info64));
5556 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5557 swapStruct(o);
5558 outs() << " version " << o.version << "\n";
5559 outs() << " flags " << format("0x%" PRIx32"x", o.flags);
5560 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT(1 << 0))
5561 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5562 if (o.flags & OBJC_IMAGE_SUPPORTS_GC(1 << 1))
5563 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5564 swift_version = (o.flags >> 8) & 0xff;
5565 if (swift_version != 0) {
5566 if (swift_version == 1)
5567 outs() << " Swift 1.0";
5568 else if (swift_version == 2)
5569 outs() << " Swift 1.1";
5570 else if(swift_version == 3)
5571 outs() << " Swift 2.0";
5572 else if(swift_version == 4)
5573 outs() << " Swift 3.0";
5574 else if(swift_version == 5)
5575 outs() << " Swift 4.0";
5576 else if(swift_version == 6)
5577 outs() << " Swift 4.1";
5578 else
5579 outs() << " unknown future Swift version (" << swift_version << ")";
5580 }
5581 outs() << "\n";
5582}
5583
5584static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5585 uint32_t left, offset, swift_version, p;
5586 struct objc_image_info32 o;
5587 const char *r;
5588
5589 if (S == SectionRef())
5590 return;
5591
5592 StringRef SectName;
5593 S.getName(SectName);
5594 DataRefImpl Ref = S.getRawDataRefImpl();
5595 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5596 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5597 p = S.getAddress();
5598 r = get_pointer_32(p, offset, left, S, info);
5599 if (r == nullptr)
5600 return;
5601 memset(&o, '\0', sizeof(struct objc_image_info32));
5602 if (left < sizeof(struct objc_image_info32)) {
5603 memcpy(&o, r, left);
5604 outs() << " (objc_image_info entends past the end of the section)\n";
5605 } else
5606 memcpy(&o, r, sizeof(struct objc_image_info32));
5607 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5608 swapStruct(o);
5609 outs() << " version " << o.version << "\n";
5610 outs() << " flags " << format("0x%" PRIx32"x", o.flags);
5611 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT(1 << 0))
5612 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5613 if (o.flags & OBJC_IMAGE_SUPPORTS_GC(1 << 1))
5614 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5615 swift_version = (o.flags >> 8) & 0xff;
5616 if (swift_version != 0) {
5617 if (swift_version == 1)
5618 outs() << " Swift 1.0";
5619 else if (swift_version == 2)
5620 outs() << " Swift 1.1";
5621 else if(swift_version == 3)
5622 outs() << " Swift 2.0";
5623 else if(swift_version == 4)
5624 outs() << " Swift 3.0";
5625 else if(swift_version == 5)
5626 outs() << " Swift 4.0";
5627 else if(swift_version == 6)
5628 outs() << " Swift 4.1";
5629 else
5630 outs() << " unknown future Swift version (" << swift_version << ")";
5631 }
5632 outs() << "\n";
5633}
5634
5635static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5636 uint32_t left, offset, p;
5637 struct imageInfo_t o;
5638 const char *r;
5639
5640 StringRef SectName;
5641 S.getName(SectName);
5642 DataRefImpl Ref = S.getRawDataRefImpl();
5643 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5644 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5645 p = S.getAddress();
5646 r = get_pointer_32(p, offset, left, S, info);
5647 if (r == nullptr)
5648 return;
5649 memset(&o, '\0', sizeof(struct imageInfo_t));
5650 if (left < sizeof(struct imageInfo_t)) {
5651 memcpy(&o, r, left);
5652 outs() << " (imageInfo entends past the end of the section)\n";
5653 } else
5654 memcpy(&o, r, sizeof(struct imageInfo_t));
5655 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5656 swapStruct(o);
5657 outs() << " version " << o.version << "\n";
5658 outs() << " flags " << format("0x%" PRIx32"x", o.flags);
5659 if (o.flags & 0x1)
5660 outs() << " F&C";
5661 if (o.flags & 0x2)
5662 outs() << " GC";
5663 if (o.flags & 0x4)
5664 outs() << " GC-only";
5665 else
5666 outs() << " RR";
5667 outs() << "\n";
5668}
5669
5670static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5671 SymbolAddressMap AddrMap;
5672 if (verbose)
5673 CreateSymbolAddressMap(O, &AddrMap);
5674
5675 std::vector<SectionRef> Sections;
5676 for (const SectionRef &Section : O->sections()) {
5677 StringRef SectName;
5678 Section.getName(SectName);
5679 Sections.push_back(Section);
5680 }
5681
5682 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
5683
5684 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5685 if (CL == SectionRef())
5686 CL = get_section(O, "__DATA", "__objc_classlist");
5687 if (CL == SectionRef())
5688 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
5689 if (CL == SectionRef())
5690 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
5691 info.S = CL;
5692 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5693
5694 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5695 if (CR == SectionRef())
5696 CR = get_section(O, "__DATA", "__objc_classrefs");
5697 if (CR == SectionRef())
5698 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
5699 if (CR == SectionRef())
5700 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
5701 info.S = CR;
5702 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5703
5704 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5705 if (SR == SectionRef())
5706 SR = get_section(O, "__DATA", "__objc_superrefs");
5707 if (SR == SectionRef())
5708 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
5709 if (SR == SectionRef())
5710 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
5711 info.S = SR;
5712 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5713
5714 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5715 if (CA == SectionRef())
5716 CA = get_section(O, "__DATA", "__objc_catlist");
5717 if (CA == SectionRef())
5718 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
5719 if (CA == SectionRef())
5720 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
5721 info.S = CA;
5722 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5723
5724 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5725 if (PL == SectionRef())
5726 PL = get_section(O, "__DATA", "__objc_protolist");
5727 if (PL == SectionRef())
5728 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
5729 if (PL == SectionRef())
5730 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
5731 info.S = PL;
5732 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5733
5734 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5735 if (MR == SectionRef())
5736 MR = get_section(O, "__DATA", "__objc_msgrefs");
5737 if (MR == SectionRef())
5738 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
5739 if (MR == SectionRef())
5740 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
5741 info.S = MR;
5742 print_message_refs64(MR, &info);
5743
5744 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5745 if (II == SectionRef())
5746 II = get_section(O, "__DATA", "__objc_imageinfo");
5747 if (II == SectionRef())
5748 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
5749 if (II == SectionRef())
5750 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
5751 info.S = II;
5752 print_image_info64(II, &info);
5753}
5754
5755static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5756 SymbolAddressMap AddrMap;
5757 if (verbose)
5758 CreateSymbolAddressMap(O, &AddrMap);
5759
5760 std::vector<SectionRef> Sections;
5761 for (const SectionRef &Section : O->sections()) {
5762 StringRef SectName;
5763 Section.getName(SectName);
5764 Sections.push_back(Section);
5765 }
5766
5767 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
5768
5769 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5770 if (CL == SectionRef())
5771 CL = get_section(O, "__DATA", "__objc_classlist");
5772 if (CL == SectionRef())
5773 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
5774 if (CL == SectionRef())
5775 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
5776 info.S = CL;
5777 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5778
5779 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5780 if (CR == SectionRef())
5781 CR = get_section(O, "__DATA", "__objc_classrefs");
5782 if (CR == SectionRef())
5783 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
5784 if (CR == SectionRef())
5785 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
5786 info.S = CR;
5787 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5788
5789 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5790 if (SR == SectionRef())
5791 SR = get_section(O, "__DATA", "__objc_superrefs");
5792 if (SR == SectionRef())
5793 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
5794 if (SR == SectionRef())
5795 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
5796 info.S = SR;
5797 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5798
5799 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5800 if (CA == SectionRef())
5801 CA = get_section(O, "__DATA", "__objc_catlist");
5802 if (CA == SectionRef())
5803 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
5804 if (CA == SectionRef())
5805 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
5806 info.S = CA;
5807 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5808
5809 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5810 if (PL == SectionRef())
5811 PL = get_section(O, "__DATA", "__objc_protolist");
5812 if (PL == SectionRef())
5813 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
5814 if (PL == SectionRef())
5815 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
5816 info.S = PL;
5817 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5818
5819 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5820 if (MR == SectionRef())
5821 MR = get_section(O, "__DATA", "__objc_msgrefs");
5822 if (MR == SectionRef())
5823 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
5824 if (MR == SectionRef())
5825 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
5826 info.S = MR;
5827 print_message_refs32(MR, &info);
5828
5829 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5830 if (II == SectionRef())
5831 II = get_section(O, "__DATA", "__objc_imageinfo");
5832 if (II == SectionRef())
5833 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
5834 if (II == SectionRef())
5835 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
5836 info.S = II;
5837 print_image_info32(II, &info);
5838}
5839
5840static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5841 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5842 const char *r, *name, *defs;
5843 struct objc_module_t module;
5844 SectionRef S, xS;
5845 struct objc_symtab_t symtab;
5846 struct objc_class_t objc_class;
5847 struct objc_category_t objc_category;
5848
5849 outs() << "Objective-C segment\n";
5850 S = get_section(O, "__OBJC", "__module_info");
5851 if (S == SectionRef())
5852 return false;
5853
5854 SymbolAddressMap AddrMap;
5855 if (verbose)
5856 CreateSymbolAddressMap(O, &AddrMap);
5857
5858 std::vector<SectionRef> Sections;
5859 for (const SectionRef &Section : O->sections()) {
5860 StringRef SectName;
5861 Section.getName(SectName);
5862 Sections.push_back(Section);
5863 }
5864
5865 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
5866
5867 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5868 p = S.getAddress() + i;
5869 r = get_pointer_32(p, offset, left, S, &info, true);
5870 if (r == nullptr)
5871 return true;
5872 memset(&module, '\0', sizeof(struct objc_module_t));
5873 if (left < sizeof(struct objc_module_t)) {
5874 memcpy(&module, r, left);
5875 outs() << " (module extends past end of __module_info section)\n";
5876 } else
5877 memcpy(&module, r, sizeof(struct objc_module_t));
5878 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5879 swapStruct(module);
5880
5881 outs() << "Module " << format("0x%" PRIx32"x", p) << "\n";
5882 outs() << " version " << module.version << "\n";
5883 outs() << " size " << module.size << "\n";
5884 outs() << " name ";
5885 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5886 if (name != nullptr)
5887 outs() << format("%.*s", left, name);
5888 else
5889 outs() << format("0x%08" PRIx32"x", module.name)
5890 << "(not in an __OBJC section)";
5891 outs() << "\n";
5892
5893 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5894 if (module.symtab == 0 || r == nullptr) {
5895 outs() << " symtab " << format("0x%08" PRIx32"x", module.symtab)
5896 << " (not in an __OBJC section)\n";
5897 continue;
5898 }
5899 outs() << " symtab " << format("0x%08" PRIx32"x", module.symtab) << "\n";
5900 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5901 defs_left = 0;
5902 defs = nullptr;
5903 if (left < sizeof(struct objc_symtab_t)) {
5904 memcpy(&symtab, r, left);
5905 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5906 } else {