Bug Summary

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

Annotated Source Code

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// This is used to search for a data in code table entry for the PC being
206// disassembled. The j parameter has the PC in j.first. A single data in code
207// table entry can cover many bytes for each of its Kind's. So if the offset,
208// aka the i.first value, of the data in code table entry plus its Length
209// covers the PC being searched for this will return true. If not it will
210// return false.
211static bool compareDiceTableEntries(const DiceTableEntry &i,
212 const DiceTableEntry &j) {
213 uint16_t Length;
214 i.second.getLength(Length);
215
216 return j.first >= i.first && j.first < i.first + Length;
217}
218
219static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
220 unsigned short Kind) {
221 uint32_t Value, Size = 1;
222
223 switch (Kind) {
224 default:
225 case MachO::DICE_KIND_DATA:
226 if (Length >= 4) {
227 if (!NoShowRawInsn)
228 dumpBytes(makeArrayRef(bytes, 4), outs());
229 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
230 outs() << "\t.long " << Value;
231 Size = 4;
232 } else if (Length >= 2) {
233 if (!NoShowRawInsn)
234 dumpBytes(makeArrayRef(bytes, 2), outs());
235 Value = bytes[1] << 8 | bytes[0];
236 outs() << "\t.short " << Value;
237 Size = 2;
238 } else {
239 if (!NoShowRawInsn)
240 dumpBytes(makeArrayRef(bytes, 2), outs());
241 Value = bytes[0];
242 outs() << "\t.byte " << Value;
243 Size = 1;
244 }
245 if (Kind == MachO::DICE_KIND_DATA)
246 outs() << "\t@ KIND_DATA\n";
247 else
248 outs() << "\t@ data in code kind = " << Kind << "\n";
249 break;
250 case MachO::DICE_KIND_JUMP_TABLE8:
251 if (!NoShowRawInsn)
252 dumpBytes(makeArrayRef(bytes, 1), outs());
253 Value = bytes[0];
254 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
255 Size = 1;
256 break;
257 case MachO::DICE_KIND_JUMP_TABLE16:
258 if (!NoShowRawInsn)
259 dumpBytes(makeArrayRef(bytes, 2), outs());
260 Value = bytes[1] << 8 | bytes[0];
261 outs() << "\t.short " << format("%5u", Value & 0xffff)
262 << "\t@ KIND_JUMP_TABLE16\n";
263 Size = 2;
264 break;
265 case MachO::DICE_KIND_JUMP_TABLE32:
266 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
267 if (!NoShowRawInsn)
268 dumpBytes(makeArrayRef(bytes, 4), outs());
269 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
270 outs() << "\t.long " << Value;
271 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
272 outs() << "\t@ KIND_JUMP_TABLE32\n";
273 else
274 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
275 Size = 4;
276 break;
277 }
278 return Size;
279}
280
281static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
282 std::vector<SectionRef> &Sections,
283 std::vector<SymbolRef> &Symbols,
284 SmallVectorImpl<uint64_t> &FoundFns,
285 uint64_t &BaseSegmentAddress) {
286 for (const SymbolRef &Symbol : MachOObj->symbols()) {
287 Expected<StringRef> SymName = Symbol.getName();
288 if (!SymName)
289 report_error(MachOObj->getFileName(), SymName.takeError());
290 if (!SymName->startswith("ltmp"))
291 Symbols.push_back(Symbol);
292 }
293
294 for (const SectionRef &Section : MachOObj->sections()) {
295 StringRef SectName;
296 Section.getName(SectName);
297 Sections.push_back(Section);
298 }
299
300 bool BaseSegmentAddressSet = false;
301 for (const auto &Command : MachOObj->load_commands()) {
302 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
303 // We found a function starts segment, parse the addresses for later
304 // consumption.
305 MachO::linkedit_data_command LLC =
306 MachOObj->getLinkeditDataLoadCommand(Command);
307
308 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
309 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
310 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
311 StringRef SegName = SLC.segname;
312 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
313 BaseSegmentAddressSet = true;
314 BaseSegmentAddress = SLC.vmaddr;
315 }
316 }
317 }
318}
319
320static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
321 uint32_t n, uint32_t count,
322 uint32_t stride, uint64_t addr) {
323 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
324 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
325 if (n > nindirectsyms)
326 outs() << " (entries start past the end of the indirect symbol "
327 "table) (reserved1 field greater than the table size)";
328 else if (n + count > nindirectsyms)
329 outs() << " (entries extends past the end of the indirect symbol "
330 "table)";
331 outs() << "\n";
332 uint32_t cputype = O->getHeader().cputype;
333 if (cputype & MachO::CPU_ARCH_ABI64)
334 outs() << "address index";
335 else
336 outs() << "address index";
337 if (verbose)
338 outs() << " name\n";
339 else
340 outs() << "\n";
341 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
342 if (cputype & MachO::CPU_ARCH_ABI64)
343 outs() << format("0x%016" PRIx64"l" "x", addr + j * stride) << " ";
344 else
345 outs() << format("0x%08" PRIx32"x", (uint32_t)addr + j * stride) << " ";
346 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
347 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
348 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
349 outs() << "LOCAL\n";
350 continue;
351 }
352 if (indirect_symbol ==
353 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
354 outs() << "LOCAL ABSOLUTE\n";
355 continue;
356 }
357 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
358 outs() << "ABSOLUTE\n";
359 continue;
360 }
361 outs() << format("%5u ", indirect_symbol);
362 if (verbose) {
363 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
364 if (indirect_symbol < Symtab.nsyms) {
365 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
366 SymbolRef Symbol = *Sym;
367 Expected<StringRef> SymName = Symbol.getName();
368 if (!SymName)
369 report_error(O->getFileName(), SymName.takeError());
370 outs() << *SymName;
371 } else {
372 outs() << "?";
373 }
374 }
375 outs() << "\n";
376 }
377}
378
379static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
380 for (const auto &Load : O->load_commands()) {
381 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
382 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
383 for (unsigned J = 0; J < Seg.nsects; ++J) {
384 MachO::section_64 Sec = O->getSection64(Load, J);
385 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
386 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
387 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
388 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
389 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
390 section_type == MachO::S_SYMBOL_STUBS) {
391 uint32_t stride;
392 if (section_type == MachO::S_SYMBOL_STUBS)
393 stride = Sec.reserved2;
394 else
395 stride = 8;
396 if (stride == 0) {
397 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
398 << Sec.sectname << ") "
399 << "(size of stubs in reserved2 field is zero)\n";
400 continue;
401 }
402 uint32_t count = Sec.size / stride;
403 outs() << "Indirect symbols for (" << Sec.segname << ","
404 << Sec.sectname << ") " << count << " entries";
405 uint32_t n = Sec.reserved1;
406 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
407 }
408 }
409 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
410 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
411 for (unsigned J = 0; J < Seg.nsects; ++J) {
412 MachO::section Sec = O->getSection(Load, J);
413 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
414 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
415 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
416 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
417 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
418 section_type == MachO::S_SYMBOL_STUBS) {
419 uint32_t stride;
420 if (section_type == MachO::S_SYMBOL_STUBS)
421 stride = Sec.reserved2;
422 else
423 stride = 4;
424 if (stride == 0) {
425 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
426 << Sec.sectname << ") "
427 << "(size of stubs in reserved2 field is zero)\n";
428 continue;
429 }
430 uint32_t count = Sec.size / stride;
431 outs() << "Indirect symbols for (" << Sec.segname << ","
432 << Sec.sectname << ") " << count << " entries";
433 uint32_t n = Sec.reserved1;
434 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
435 }
436 }
437 }
438 }
439}
440
441static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
442 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
443 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
444 outs() << "Data in code table (" << nentries << " entries)\n";
445 outs() << "offset length kind\n";
446 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
447 ++DI) {
448 uint32_t Offset;
449 DI->getOffset(Offset);
450 outs() << format("0x%08" PRIx32"x", Offset) << " ";
451 uint16_t Length;
452 DI->getLength(Length);
453 outs() << format("%6u", Length) << " ";
454 uint16_t Kind;
455 DI->getKind(Kind);
456 if (verbose) {
457 switch (Kind) {
458 case MachO::DICE_KIND_DATA:
459 outs() << "DATA";
460 break;
461 case MachO::DICE_KIND_JUMP_TABLE8:
462 outs() << "JUMP_TABLE8";
463 break;
464 case MachO::DICE_KIND_JUMP_TABLE16:
465 outs() << "JUMP_TABLE16";
466 break;
467 case MachO::DICE_KIND_JUMP_TABLE32:
468 outs() << "JUMP_TABLE32";
469 break;
470 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
471 outs() << "ABS_JUMP_TABLE32";
472 break;
473 default:
474 outs() << format("0x%04" PRIx32"x", Kind);
475 break;
476 }
477 } else
478 outs() << format("0x%04" PRIx32"x", Kind);
479 outs() << "\n";
480 }
481}
482
483static void PrintLinkOptHints(MachOObjectFile *O) {
484 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
485 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
486 uint32_t nloh = LohLC.datasize;
487 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
488 for (uint32_t i = 0; i < nloh;) {
489 unsigned n;
490 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
491 i += n;
492 outs() << " identifier " << identifier << " ";
493 if (i >= nloh)
494 return;
495 switch (identifier) {
496 case 1:
497 outs() << "AdrpAdrp\n";
498 break;
499 case 2:
500 outs() << "AdrpLdr\n";
501 break;
502 case 3:
503 outs() << "AdrpAddLdr\n";
504 break;
505 case 4:
506 outs() << "AdrpLdrGotLdr\n";
507 break;
508 case 5:
509 outs() << "AdrpAddStr\n";
510 break;
511 case 6:
512 outs() << "AdrpLdrGotStr\n";
513 break;
514 case 7:
515 outs() << "AdrpAdd\n";
516 break;
517 case 8:
518 outs() << "AdrpLdrGot\n";
519 break;
520 default:
521 outs() << "Unknown identifier value\n";
522 break;
523 }
524 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
525 i += n;
526 outs() << " narguments " << narguments << "\n";
527 if (i >= nloh)
528 return;
529
530 for (uint32_t j = 0; j < narguments; j++) {
531 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
532 i += n;
533 outs() << "\tvalue " << format("0x%" PRIx64"l" "x", value) << "\n";
534 if (i >= nloh)
535 return;
536 }
537 }
538}
539
540static void PrintDylibs(MachOObjectFile *O, bool JustId) {
541 unsigned Index = 0;
542 for (const auto &Load : O->load_commands()) {
543 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
544 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
545 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
546 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
547 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
548 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
549 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
550 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
551 if (dl.dylib.name < dl.cmdsize) {
552 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
553 if (JustId)
554 outs() << p << "\n";
555 else {
556 outs() << "\t" << p;
557 outs() << " (compatibility version "
558 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
559 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
560 << (dl.dylib.compatibility_version & 0xff) << ",";
561 outs() << " current version "
562 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
563 << ((dl.dylib.current_version >> 8) & 0xff) << "."
564 << (dl.dylib.current_version & 0xff) << ")\n";
565 }
566 } else {
567 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
568 if (Load.C.cmd == MachO::LC_ID_DYLIB)
569 outs() << "LC_ID_DYLIB ";
570 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
571 outs() << "LC_LOAD_DYLIB ";
572 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
573 outs() << "LC_LOAD_WEAK_DYLIB ";
574 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
575 outs() << "LC_LAZY_LOAD_DYLIB ";
576 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
577 outs() << "LC_REEXPORT_DYLIB ";
578 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
579 outs() << "LC_LOAD_UPWARD_DYLIB ";
580 else
581 outs() << "LC_??? ";
582 outs() << "command " << Index++ << "\n";
583 }
584 }
585 }
586}
587
588typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
589
590static void CreateSymbolAddressMap(MachOObjectFile *O,
591 SymbolAddressMap *AddrMap) {
592 // Create a map of symbol addresses to symbol names.
593 for (const SymbolRef &Symbol : O->symbols()) {
594 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
595 if (!STOrErr)
596 report_error(O->getFileName(), STOrErr.takeError());
597 SymbolRef::Type ST = *STOrErr;
598 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
599 ST == SymbolRef::ST_Other) {
600 uint64_t Address = Symbol.getValue();
601 Expected<StringRef> SymNameOrErr = Symbol.getName();
602 if (!SymNameOrErr)
603 report_error(O->getFileName(), SymNameOrErr.takeError());
604 StringRef SymName = *SymNameOrErr;
605 if (!SymName.startswith(".objc"))
606 (*AddrMap)[Address] = SymName;
607 }
608 }
609}
610
611// GuessSymbolName is passed the address of what might be a symbol and a
612// pointer to the SymbolAddressMap. It returns the name of a symbol
613// with that address or nullptr if no symbol is found with that address.
614static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
615 const char *SymbolName = nullptr;
616 // A DenseMap can't lookup up some values.
617 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
618 StringRef name = AddrMap->lookup(value);
619 if (!name.empty())
620 SymbolName = name.data();
621 }
622 return SymbolName;
623}
624
625static void DumpCstringChar(const char c) {
626 char p[2];
627 p[0] = c;
628 p[1] = '\0';
629 outs().write_escaped(p);
630}
631
632static void DumpCstringSection(MachOObjectFile *O, const char *sect,
633 uint32_t sect_size, uint64_t sect_addr,
634 bool print_addresses) {
635 for (uint32_t i = 0; i < sect_size; i++) {
636 if (print_addresses) {
637 if (O->is64Bit())
638 outs() << format("%016" PRIx64"l" "x", sect_addr + i) << " ";
639 else
640 outs() << format("%08" PRIx64"l" "x", sect_addr + i) << " ";
641 }
642 for (; i < sect_size && sect[i] != '\0'; i++)
643 DumpCstringChar(sect[i]);
644 if (i < sect_size && sect[i] == '\0')
645 outs() << "\n";
646 }
647}
648
649static void DumpLiteral4(uint32_t l, float f) {
650 outs() << format("0x%08" PRIx32"x", l);
651 if ((l & 0x7f800000) != 0x7f800000)
652 outs() << format(" (%.16e)\n", f);
653 else {
654 if (l == 0x7f800000)
655 outs() << " (+Infinity)\n";
656 else if (l == 0xff800000)
657 outs() << " (-Infinity)\n";
658 else if ((l & 0x00400000) == 0x00400000)
659 outs() << " (non-signaling Not-a-Number)\n";
660 else
661 outs() << " (signaling Not-a-Number)\n";
662 }
663}
664
665static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
666 uint32_t sect_size, uint64_t sect_addr,
667 bool print_addresses) {
668 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
669 if (print_addresses) {
670 if (O->is64Bit())
671 outs() << format("%016" PRIx64"l" "x", sect_addr + i) << " ";
672 else
673 outs() << format("%08" PRIx64"l" "x", sect_addr + i) << " ";
674 }
675 float f;
676 memcpy(&f, sect + i, sizeof(float));
677 if (O->isLittleEndian() != sys::IsLittleEndianHost)
678 sys::swapByteOrder(f);
679 uint32_t l;
680 memcpy(&l, sect + i, sizeof(uint32_t));
681 if (O->isLittleEndian() != sys::IsLittleEndianHost)
682 sys::swapByteOrder(l);
683 DumpLiteral4(l, f);
684 }
685}
686
687static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
688 double d) {
689 outs() << format("0x%08" PRIx32"x", l0) << " " << format("0x%08" PRIx32"x", l1);
690 uint32_t Hi, Lo;
691 Hi = (O->isLittleEndian()) ? l1 : l0;
692 Lo = (O->isLittleEndian()) ? l0 : l1;
693
694 // Hi is the high word, so this is equivalent to if(isfinite(d))
695 if ((Hi & 0x7ff00000) != 0x7ff00000)
696 outs() << format(" (%.16e)\n", d);
697 else {
698 if (Hi == 0x7ff00000 && Lo == 0)
699 outs() << " (+Infinity)\n";
700 else if (Hi == 0xfff00000 && Lo == 0)
701 outs() << " (-Infinity)\n";
702 else if ((Hi & 0x00080000) == 0x00080000)
703 outs() << " (non-signaling Not-a-Number)\n";
704 else
705 outs() << " (signaling Not-a-Number)\n";
706 }
707}
708
709static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
710 uint32_t sect_size, uint64_t sect_addr,
711 bool print_addresses) {
712 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
713 if (print_addresses) {
714 if (O->is64Bit())
715 outs() << format("%016" PRIx64"l" "x", sect_addr + i) << " ";
716 else
717 outs() << format("%08" PRIx64"l" "x", sect_addr + i) << " ";
718 }
719 double d;
720 memcpy(&d, sect + i, sizeof(double));
721 if (O->isLittleEndian() != sys::IsLittleEndianHost)
722 sys::swapByteOrder(d);
723 uint32_t l0, l1;
724 memcpy(&l0, sect + i, sizeof(uint32_t));
725 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
726 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
727 sys::swapByteOrder(l0);
728 sys::swapByteOrder(l1);
729 }
730 DumpLiteral8(O, l0, l1, d);
731 }
732}
733
734static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
735 outs() << format("0x%08" PRIx32"x", l0) << " ";
736 outs() << format("0x%08" PRIx32"x", l1) << " ";
737 outs() << format("0x%08" PRIx32"x", l2) << " ";
738 outs() << format("0x%08" PRIx32"x", l3) << "\n";
739}
740
741static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
742 uint32_t sect_size, uint64_t sect_addr,
743 bool print_addresses) {
744 for (uint32_t i = 0; i < sect_size; i += 16) {
745 if (print_addresses) {
746 if (O->is64Bit())
747 outs() << format("%016" PRIx64"l" "x", sect_addr + i) << " ";
748 else
749 outs() << format("%08" PRIx64"l" "x", sect_addr + i) << " ";
750 }
751 uint32_t l0, l1, l2, l3;
752 memcpy(&l0, sect + i, sizeof(uint32_t));
753 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
754 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
755 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
756 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
757 sys::swapByteOrder(l0);
758 sys::swapByteOrder(l1);
759 sys::swapByteOrder(l2);
760 sys::swapByteOrder(l3);
761 }
762 DumpLiteral16(l0, l1, l2, l3);
763 }
764}
765
766static void DumpLiteralPointerSection(MachOObjectFile *O,
767 const SectionRef &Section,
768 const char *sect, uint32_t sect_size,
769 uint64_t sect_addr,
770 bool print_addresses) {
771 // Collect the literal sections in this Mach-O file.
772 std::vector<SectionRef> LiteralSections;
773 for (const SectionRef &Section : O->sections()) {
774 DataRefImpl Ref = Section.getRawDataRefImpl();
775 uint32_t section_type;
776 if (O->is64Bit()) {
777 const MachO::section_64 Sec = O->getSection64(Ref);
778 section_type = Sec.flags & MachO::SECTION_TYPE;
779 } else {
780 const MachO::section Sec = O->getSection(Ref);
781 section_type = Sec.flags & MachO::SECTION_TYPE;
782 }
783 if (section_type == MachO::S_CSTRING_LITERALS ||
784 section_type == MachO::S_4BYTE_LITERALS ||
785 section_type == MachO::S_8BYTE_LITERALS ||
786 section_type == MachO::S_16BYTE_LITERALS)
787 LiteralSections.push_back(Section);
788 }
789
790 // Set the size of the literal pointer.
791 uint32_t lp_size = O->is64Bit() ? 8 : 4;
792
793 // Collect the external relocation symbols for the literal pointers.
794 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
795 for (const RelocationRef &Reloc : Section.relocations()) {
796 DataRefImpl Rel;
797 MachO::any_relocation_info RE;
798 bool isExtern = false;
799 Rel = Reloc.getRawDataRefImpl();
800 RE = O->getRelocation(Rel);
801 isExtern = O->getPlainRelocationExternal(RE);
802 if (isExtern) {
803 uint64_t RelocOffset = Reloc.getOffset();
804 symbol_iterator RelocSym = Reloc.getSymbol();
805 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
806 }
807 }
808 array_pod_sort(Relocs.begin(), Relocs.end());
809
810 // Dump each literal pointer.
811 for (uint32_t i = 0; i < sect_size; i += lp_size) {
812 if (print_addresses) {
813 if (O->is64Bit())
814 outs() << format("%016" PRIx64"l" "x", sect_addr + i) << " ";
815 else
816 outs() << format("%08" PRIx64"l" "x", sect_addr + i) << " ";
817 }
818 uint64_t lp;
819 if (O->is64Bit()) {
820 memcpy(&lp, sect + i, sizeof(uint64_t));
821 if (O->isLittleEndian() != sys::IsLittleEndianHost)
822 sys::swapByteOrder(lp);
823 } else {
824 uint32_t li;
825 memcpy(&li, sect + i, sizeof(uint32_t));
826 if (O->isLittleEndian() != sys::IsLittleEndianHost)
827 sys::swapByteOrder(li);
828 lp = li;
829 }
830
831 // First look for an external relocation entry for this literal pointer.
832 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
833 return P.first == i;
834 });
835 if (Reloc != Relocs.end()) {
836 symbol_iterator RelocSym = Reloc->second;
837 Expected<StringRef> SymName = RelocSym->getName();
838 if (!SymName)
839 report_error(O->getFileName(), SymName.takeError());
840 outs() << "external relocation entry for symbol:" << *SymName << "\n";
841 continue;
842 }
843
844 // For local references see what the section the literal pointer points to.
845 auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
846 return lp >= R.getAddress() && lp < R.getAddress() + R.getSize();
847 });
848 if (Sect == LiteralSections.end()) {
849 outs() << format("0x%" PRIx64"l" "x", lp) << " (not in a literal section)\n";
850 continue;
851 }
852
853 uint64_t SectAddress = Sect->getAddress();
854 uint64_t SectSize = Sect->getSize();
855
856 StringRef SectName;
857 Sect->getName(SectName);
858 DataRefImpl Ref = Sect->getRawDataRefImpl();
859 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
860 outs() << SegmentName << ":" << SectName << ":";
861
862 uint32_t section_type;
863 if (O->is64Bit()) {
864 const MachO::section_64 Sec = O->getSection64(Ref);
865 section_type = Sec.flags & MachO::SECTION_TYPE;
866 } else {
867 const MachO::section Sec = O->getSection(Ref);
868 section_type = Sec.flags & MachO::SECTION_TYPE;
869 }
870
871 StringRef BytesStr;
872 Sect->getContents(BytesStr);
873 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
874
875 switch (section_type) {
876 case MachO::S_CSTRING_LITERALS:
877 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
878 i++) {
879 DumpCstringChar(Contents[i]);
880 }
881 outs() << "\n";
882 break;
883 case MachO::S_4BYTE_LITERALS:
884 float f;
885 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
886 uint32_t l;
887 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
888 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
889 sys::swapByteOrder(f);
890 sys::swapByteOrder(l);
891 }
892 DumpLiteral4(l, f);
893 break;
894 case MachO::S_8BYTE_LITERALS: {
895 double d;
896 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
897 uint32_t l0, l1;
898 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
899 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
900 sizeof(uint32_t));
901 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
902 sys::swapByteOrder(f);
903 sys::swapByteOrder(l0);
904 sys::swapByteOrder(l1);
905 }
906 DumpLiteral8(O, l0, l1, d);
907 break;
908 }
909 case MachO::S_16BYTE_LITERALS: {
910 uint32_t l0, l1, l2, l3;
911 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
912 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
913 sizeof(uint32_t));
914 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
915 sizeof(uint32_t));
916 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
917 sizeof(uint32_t));
918 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
919 sys::swapByteOrder(l0);
920 sys::swapByteOrder(l1);
921 sys::swapByteOrder(l2);
922 sys::swapByteOrder(l3);
923 }
924 DumpLiteral16(l0, l1, l2, l3);
925 break;
926 }
927 }
928 }
929}
930
931static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
932 uint32_t sect_size, uint64_t sect_addr,
933 SymbolAddressMap *AddrMap,
934 bool verbose) {
935 uint32_t stride;
936 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
937 for (uint32_t i = 0; i < sect_size; i += stride) {
938 const char *SymbolName = nullptr;
939 if (O->is64Bit()) {
940 outs() << format("0x%016" PRIx64"l" "x", sect_addr + i * stride) << " ";
941 uint64_t pointer_value;
942 memcpy(&pointer_value, sect + i, stride);
943 if (O->isLittleEndian() != sys::IsLittleEndianHost)
944 sys::swapByteOrder(pointer_value);
945 outs() << format("0x%016" PRIx64"l" "x", pointer_value);
946 if (verbose)
947 SymbolName = GuessSymbolName(pointer_value, AddrMap);
948 } else {
949 outs() << format("0x%08" PRIx64"l" "x", sect_addr + i * stride) << " ";
950 uint32_t pointer_value;
951 memcpy(&pointer_value, sect + i, stride);
952 if (O->isLittleEndian() != sys::IsLittleEndianHost)
953 sys::swapByteOrder(pointer_value);
954 outs() << format("0x%08" PRIx32"x", pointer_value);
955 if (verbose)
956 SymbolName = GuessSymbolName(pointer_value, AddrMap);
957 }
958 if (SymbolName)
959 outs() << " " << SymbolName;
960 outs() << "\n";
961 }
962}
963
964static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
965 uint32_t size, uint64_t addr) {
966 uint32_t cputype = O->getHeader().cputype;
967 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
968 uint32_t j;
969 for (uint32_t i = 0; i < size; i += j, addr += j) {
970 if (O->is64Bit())
971 outs() << format("%016" PRIx64"l" "x", addr) << "\t";
972 else
973 outs() << format("%08" PRIx64"l" "x", addr) << "\t";
974 for (j = 0; j < 16 && i + j < size; j++) {
975 uint8_t byte_word = *(sect + i + j);
976 outs() << format("%02" PRIx32"x", (uint32_t)byte_word) << " ";
977 }
978 outs() << "\n";
979 }
980 } else {
981 uint32_t j;
982 for (uint32_t i = 0; i < size; i += j, addr += j) {
983 if (O->is64Bit())
984 outs() << format("%016" PRIx64"l" "x", addr) << "\t";
985 else
986 outs() << format("%08" PRIx64"l" "x", addr) << "\t";
987 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
988 j += sizeof(int32_t)) {
989 if (i + j + sizeof(int32_t) <= size) {
990 uint32_t long_word;
991 memcpy(&long_word, sect + i + j, sizeof(int32_t));
992 if (O->isLittleEndian() != sys::IsLittleEndianHost)
993 sys::swapByteOrder(long_word);
994 outs() << format("%08" PRIx32"x", long_word) << " ";
995 } else {
996 for (uint32_t k = 0; i + j + k < size; k++) {
997 uint8_t byte_word = *(sect + i + j + k);
998 outs() << format("%02" PRIx32"x", (uint32_t)byte_word) << " ";
999 }
1000 }
1001 }
1002 outs() << "\n";
1003 }
1004 }
1005}
1006
1007static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1008 StringRef DisSegName, StringRef DisSectName);
1009static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1010 uint32_t size, uint32_t addr);
1011#ifdef HAVE_LIBXAR
1012static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
1013 uint32_t size, bool verbose,
1014 bool PrintXarHeader, bool PrintXarFileHeaders,
1015 std::string XarMemberName);
1016#endif // defined(HAVE_LIBXAR)
1017
1018static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1019 bool verbose) {
1020 SymbolAddressMap AddrMap;
1021 if (verbose)
1022 CreateSymbolAddressMap(O, &AddrMap);
1023
1024 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1025 StringRef DumpSection = FilterSections[i];
1026 std::pair<StringRef, StringRef> DumpSegSectName;
1027 DumpSegSectName = DumpSection.split(',');
1028 StringRef DumpSegName, DumpSectName;
1029 if (DumpSegSectName.second.size()) {
1030 DumpSegName = DumpSegSectName.first;
1031 DumpSectName = DumpSegSectName.second;
1032 } else {
1033 DumpSegName = "";
1034 DumpSectName = DumpSegSectName.first;
1035 }
1036 for (const SectionRef &Section : O->sections()) {
1037 StringRef SectName;
1038 Section.getName(SectName);
1039 DataRefImpl Ref = Section.getRawDataRefImpl();
1040 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1041 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1042 (SectName == DumpSectName)) {
1043
1044 uint32_t section_flags;
1045 if (O->is64Bit()) {
1046 const MachO::section_64 Sec = O->getSection64(Ref);
1047 section_flags = Sec.flags;
1048
1049 } else {
1050 const MachO::section Sec = O->getSection(Ref);
1051 section_flags = Sec.flags;
1052 }
1053 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1054
1055 StringRef BytesStr;
1056 Section.getContents(BytesStr);
1057 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1058 uint32_t sect_size = BytesStr.size();
1059 uint64_t sect_addr = Section.getAddress();
1060
1061 outs() << "Contents of (" << SegName << "," << SectName
1062 << ") section\n";
1063
1064 if (verbose) {
1065 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1066 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1067 DisassembleMachO(Filename, O, SegName, SectName);
1068 continue;
1069 }
1070 if (SegName == "__TEXT" && SectName == "__info_plist") {
1071 outs() << sect;
1072 continue;
1073 }
1074 if (SegName == "__OBJC" && SectName == "__protocol") {
1075 DumpProtocolSection(O, sect, sect_size, sect_addr);
1076 continue;
1077 }
1078#ifdef HAVE_LIBXAR
1079 if (SegName == "__LLVM" && SectName == "__bundle") {
1080 DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
1081 ArchiveHeaders, "");
1082 continue;
1083 }
1084#endif // defined(HAVE_LIBXAR)
1085 switch (section_type) {
1086 case MachO::S_REGULAR:
1087 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1088 break;
1089 case MachO::S_ZEROFILL:
1090 outs() << "zerofill section and has no contents in the file\n";
1091 break;
1092 case MachO::S_CSTRING_LITERALS:
1093 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1094 break;
1095 case MachO::S_4BYTE_LITERALS:
1096 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1097 break;
1098 case MachO::S_8BYTE_LITERALS:
1099 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1100 break;
1101 case MachO::S_16BYTE_LITERALS:
1102 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1103 break;
1104 case MachO::S_LITERAL_POINTERS:
1105 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1106 !NoLeadingAddr);
1107 break;
1108 case MachO::S_MOD_INIT_FUNC_POINTERS:
1109 case MachO::S_MOD_TERM_FUNC_POINTERS:
1110 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1111 verbose);
1112 break;
1113 default:
1114 outs() << "Unknown section type ("
1115 << format("0x%08" PRIx32"x", section_type) << ")\n";
1116 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1117 break;
1118 }
1119 } else {
1120 if (section_type == MachO::S_ZEROFILL)
1121 outs() << "zerofill section and has no contents in the file\n";
1122 else
1123 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1124 }
1125 }
1126 }
1127 }
1128}
1129
1130static void DumpInfoPlistSectionContents(StringRef Filename,
1131 MachOObjectFile *O) {
1132 for (const SectionRef &Section : O->sections()) {
1133 StringRef SectName;
1134 Section.getName(SectName);
1135 DataRefImpl Ref = Section.getRawDataRefImpl();
1136 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1137 if (SegName == "__TEXT" && SectName == "__info_plist") {
1138 if (!NoLeadingHeaders)
1139 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1140 StringRef BytesStr;
1141 Section.getContents(BytesStr);
1142 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1143 outs() << format("%.*s", BytesStr.size(), sect) << "\n";
1144 return;
1145 }
1146 }
1147}
1148
1149// checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1150// and if it is and there is a list of architecture flags is specified then
1151// check to make sure this Mach-O file is one of those architectures or all
1152// architectures were specified. If not then an error is generated and this
1153// routine returns false. Else it returns true.
1154static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1155 auto *MachO = dyn_cast<MachOObjectFile>(O);
1156
1157 if (!MachO || ArchAll || ArchFlags.empty())
1158 return true;
1159
1160 MachO::mach_header H;
1161 MachO::mach_header_64 H_64;
1162 Triple T;
1163 const char *McpuDefault, *ArchFlag;
1164 if (MachO->is64Bit()) {
1165 H_64 = MachO->MachOObjectFile::getHeader64();
1166 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
1167 &McpuDefault, &ArchFlag);
1168 } else {
1169 H = MachO->MachOObjectFile::getHeader();
1170 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
1171 &McpuDefault, &ArchFlag);
1172 }
1173 const std::string ArchFlagName(ArchFlag);
1174 if (none_of(ArchFlags, [&](const std::string &Name) {
1175 return Name == ArchFlagName;
1176 })) {
1177 errs() << "llvm-objdump: " + Filename + ": No architecture specified.\n";
1178 return false;
1179 }
1180 return true;
1181}
1182
1183static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1184
1185// ProcessMachO() is passed a single opened Mach-O file, which may be an
1186// archive member and or in a slice of a universal file. It prints the
1187// the file name and header info and then processes it according to the
1188// command line options.
1189static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF,
1190 StringRef ArchiveMemberName = StringRef(),
1191 StringRef ArchitectureName = StringRef()) {
1192 // If we are doing some processing here on the Mach-O file print the header
1193 // info. And don't print it otherwise like in the case of printing the
1194 // UniversalHeaders or ArchiveHeaders.
1195 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind || SymbolTable ||
1196 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
1197 DylibsUsed || DylibId || ObjcMetaData || (FilterSections.size() != 0)) {
1198 if (!NoLeadingHeaders) {
1199 outs() << Name;
1200 if (!ArchiveMemberName.empty())
1201 outs() << '(' << ArchiveMemberName << ')';
1202 if (!ArchitectureName.empty())
1203 outs() << " (architecture " << ArchitectureName << ")";
1204 outs() << ":\n";
1205 }
1206 }
1207 // To use the report_error() form with an ArchiveName and FileName set
1208 // these up based on what is passed for Name and ArchiveMemberName.
1209 StringRef ArchiveName;
1210 StringRef FileName;
1211 if (!ArchiveMemberName.empty()) {
1212 ArchiveName = Name;
1213 FileName = ArchiveMemberName;
1214 } else {
1215 ArchiveName = StringRef();
1216 FileName = Name;
1217 }
1218
1219 // If we need the symbol table to do the operation then check it here to
1220 // produce a good error message as to where the Mach-O file comes from in
1221 // the error message.
1222 if (Disassemble || IndirectSymbols || FilterSections.size() != 0 ||
1223 UnwindInfo)
1224 if (Error Err = MachOOF->checkSymbolTable())
1225 report_error(ArchiveName, FileName, std::move(Err), ArchitectureName);
1226
1227 if (Disassemble) {
1228 if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE &&
1229 MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64)
1230 DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text");
1231 else
1232 DisassembleMachO(FileName, MachOOF, "__TEXT", "__text");
1233 }
1234 if (IndirectSymbols)
1235 PrintIndirectSymbols(MachOOF, !NonVerbose);
1236 if (DataInCode)
1237 PrintDataInCodeTable(MachOOF, !NonVerbose);
1238 if (LinkOptHints)
1239 PrintLinkOptHints(MachOOF);
1240 if (Relocations)
1241 PrintRelocations(MachOOF);
1242 if (SectionHeaders)
1243 PrintSectionHeaders(MachOOF);
1244 if (SectionContents)
1245 PrintSectionContents(MachOOF);
1246 if (FilterSections.size() != 0)
1247 DumpSectionContents(FileName, MachOOF, !NonVerbose);
1248 if (InfoPlist)
1249 DumpInfoPlistSectionContents(FileName, MachOOF);
1250 if (DylibsUsed)
1251 PrintDylibs(MachOOF, false);
1252 if (DylibId)
1253 PrintDylibs(MachOOF, true);
1254 if (SymbolTable)
1255 PrintSymbolTable(MachOOF, ArchiveName, ArchitectureName);
1256 if (UnwindInfo)
1257 printMachOUnwindInfo(MachOOF);
1258 if (PrivateHeaders) {
1259 printMachOFileHeader(MachOOF);
1260 printMachOLoadCommands(MachOOF);
1261 }
1262 if (FirstPrivateHeader)
1263 printMachOFileHeader(MachOOF);
1264 if (ObjcMetaData)
1265 printObjcMetaData(MachOOF, !NonVerbose);
1266 if (ExportsTrie)
1267 printExportsTrie(MachOOF);
1268 if (Rebase)
1269 printRebaseTable(MachOOF);
1270 if (Bind)
1271 printBindTable(MachOOF);
1272 if (LazyBind)
1273 printLazyBindTable(MachOOF);
1274 if (WeakBind)
1275 printWeakBindTable(MachOOF);
1276
1277 if (DwarfDumpType != DIDT_Null) {
1278 std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(*MachOOF));
1279 // Dump the complete DWARF structure.
1280 DIDumpOptions DumpOpts;
1281 DumpOpts.DumpType = DwarfDumpType;
1282 DumpOpts.DumpEH = true;
1283 DICtx->dump(outs(), DumpOpts);
1284 }
1285}
1286
1287// printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1288static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1289 outs() << " cputype (" << cputype << ")\n";
1290 outs() << " cpusubtype (" << cpusubtype << ")\n";
1291}
1292
1293// printCPUType() helps print_fat_headers by printing the cputype and
1294// pusubtype (symbolically for the one's it knows about).
1295static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1296 switch (cputype) {
1297 case MachO::CPU_TYPE_I386:
1298 switch (cpusubtype) {
1299 case MachO::CPU_SUBTYPE_I386_ALL:
1300 outs() << " cputype CPU_TYPE_I386\n";
1301 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1302 break;
1303 default:
1304 printUnknownCPUType(cputype, cpusubtype);
1305 break;
1306 }
1307 break;
1308 case MachO::CPU_TYPE_X86_64:
1309 switch (cpusubtype) {
1310 case MachO::CPU_SUBTYPE_X86_64_ALL:
1311 outs() << " cputype CPU_TYPE_X86_64\n";
1312 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1313 break;
1314 case MachO::CPU_SUBTYPE_X86_64_H:
1315 outs() << " cputype CPU_TYPE_X86_64\n";
1316 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1317 break;
1318 default:
1319 printUnknownCPUType(cputype, cpusubtype);
1320 break;
1321 }
1322 break;
1323 case MachO::CPU_TYPE_ARM:
1324 switch (cpusubtype) {
1325 case MachO::CPU_SUBTYPE_ARM_ALL:
1326 outs() << " cputype CPU_TYPE_ARM\n";
1327 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1328 break;
1329 case MachO::CPU_SUBTYPE_ARM_V4T:
1330 outs() << " cputype CPU_TYPE_ARM\n";
1331 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1332 break;
1333 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1334 outs() << " cputype CPU_TYPE_ARM\n";
1335 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1336 break;
1337 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1338 outs() << " cputype CPU_TYPE_ARM\n";
1339 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1340 break;
1341 case MachO::CPU_SUBTYPE_ARM_V6:
1342 outs() << " cputype CPU_TYPE_ARM\n";
1343 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1344 break;
1345 case MachO::CPU_SUBTYPE_ARM_V6M:
1346 outs() << " cputype CPU_TYPE_ARM\n";
1347 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1348 break;
1349 case MachO::CPU_SUBTYPE_ARM_V7:
1350 outs() << " cputype CPU_TYPE_ARM\n";
1351 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1352 break;
1353 case MachO::CPU_SUBTYPE_ARM_V7EM:
1354 outs() << " cputype CPU_TYPE_ARM\n";
1355 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1356 break;
1357 case MachO::CPU_SUBTYPE_ARM_V7K:
1358 outs() << " cputype CPU_TYPE_ARM\n";
1359 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1360 break;
1361 case MachO::CPU_SUBTYPE_ARM_V7M:
1362 outs() << " cputype CPU_TYPE_ARM\n";
1363 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1364 break;
1365 case MachO::CPU_SUBTYPE_ARM_V7S:
1366 outs() << " cputype CPU_TYPE_ARM\n";
1367 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1368 break;
1369 default:
1370 printUnknownCPUType(cputype, cpusubtype);
1371 break;
1372 }
1373 break;
1374 case MachO::CPU_TYPE_ARM64:
1375 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1376 case MachO::CPU_SUBTYPE_ARM64_ALL:
1377 outs() << " cputype CPU_TYPE_ARM64\n";
1378 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1379 break;
1380 default:
1381 printUnknownCPUType(cputype, cpusubtype);
1382 break;
1383 }
1384 break;
1385 default:
1386 printUnknownCPUType(cputype, cpusubtype);
1387 break;
1388 }
1389}
1390
1391static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1392 bool verbose) {
1393 outs() << "Fat headers\n";
1394 if (verbose) {
1395 if (UB->getMagic() == MachO::FAT_MAGIC)
1396 outs() << "fat_magic FAT_MAGIC\n";
1397 else // UB->getMagic() == MachO::FAT_MAGIC_64
1398 outs() << "fat_magic FAT_MAGIC_64\n";
1399 } else
1400 outs() << "fat_magic " << format("0x%" PRIx32"x", MachO::FAT_MAGIC) << "\n";
1401
1402 uint32_t nfat_arch = UB->getNumberOfObjects();
1403 StringRef Buf = UB->getData();
1404 uint64_t size = Buf.size();
1405 uint64_t big_size = sizeof(struct MachO::fat_header) +
1406 nfat_arch * sizeof(struct MachO::fat_arch);
1407 outs() << "nfat_arch " << UB->getNumberOfObjects();
1408 if (nfat_arch == 0)
1409 outs() << " (malformed, contains zero architecture types)\n";
1410 else if (big_size > size)
1411 outs() << " (malformed, architectures past end of file)\n";
1412 else
1413 outs() << "\n";
1414
1415 for (uint32_t i = 0; i < nfat_arch; ++i) {
1416 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1417 uint32_t cputype = OFA.getCPUType();
1418 uint32_t cpusubtype = OFA.getCPUSubType();
1419 outs() << "architecture ";
1420 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1421 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1422 uint32_t other_cputype = other_OFA.getCPUType();
1423 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
1424 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
1425 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
1426 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
1427 outs() << "(illegal duplicate architecture) ";
1428 break;
1429 }
1430 }
1431 if (verbose) {
1432 outs() << OFA.getArchFlagName() << "\n";
1433 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1434 } else {
1435 outs() << i << "\n";
1436 outs() << " cputype " << cputype << "\n";
1437 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1438 << "\n";
1439 }
1440 if (verbose &&
1441 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1442 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1443 else
1444 outs() << " capabilities "
1445 << format("0x%" PRIx32"x",
1446 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1447 outs() << " offset " << OFA.getOffset();
1448 if (OFA.getOffset() > size)
1449 outs() << " (past end of file)";
1450 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1451 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1452 outs() << "\n";
1453 outs() << " size " << OFA.getSize();
1454 big_size = OFA.getOffset() + OFA.getSize();
1455 if (big_size > size)
1456 outs() << " (past end of file)";
1457 outs() << "\n";
1458 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1459 << ")\n";
1460 }
1461}
1462
1463static void printArchiveChild(StringRef Filename, const Archive::Child &C,
1464 bool verbose, bool print_offset,
1465 StringRef ArchitectureName = StringRef()) {
1466 if (print_offset)
1467 outs() << C.getChildOffset() << "\t";
1468 Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
1469 if (!ModeOrErr)
1470 report_error(Filename, C, ModeOrErr.takeError(), ArchitectureName);
1471 sys::fs::perms Mode = ModeOrErr.get();
1472 if (verbose) {
1473 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1474 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1475 outs() << "-";
1476 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
1477 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
1478 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
1479 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
1480 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
1481 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
1482 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
1483 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
1484 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
1485 } else {
1486 outs() << format("0%o ", Mode);
1487 }
1488
1489 Expected<unsigned> UIDOrErr = C.getUID();
1490 if (!UIDOrErr)
1491 report_error(Filename, C, UIDOrErr.takeError(), ArchitectureName);
1492 unsigned UID = UIDOrErr.get();
1493 outs() << format("%3d/", UID);
1494 Expected<unsigned> GIDOrErr = C.getGID();
1495 if (!GIDOrErr)
1496 report_error(Filename, C, GIDOrErr.takeError(), ArchitectureName);
1497 unsigned GID = GIDOrErr.get();
1498 outs() << format("%-3d ", GID);
1499 Expected<uint64_t> Size = C.getRawSize();
1500 if (!Size)
1501 report_error(Filename, C, Size.takeError(), ArchitectureName);
1502 outs() << format("%5" PRId64"l" "d", Size.get()) << " ";
1503
1504 StringRef RawLastModified = C.getRawLastModified();
1505 if (verbose) {
1506 unsigned Seconds;
1507 if (RawLastModified.getAsInteger(10, Seconds))
1508 outs() << "(date: \"" << RawLastModified
1509 << "\" contains non-decimal chars) ";
1510 else {
1511 // Since cime(3) returns a 26 character string of the form:
1512 // "Sun Sep 16 01:03:52 1973\n\0"
1513 // just print 24 characters.
1514 time_t t = Seconds;
1515 outs() << format("%.24s ", ctime(&t));
1516 }
1517 } else {
1518 outs() << RawLastModified << " ";
1519 }
1520
1521 if (verbose) {
1522 Expected<StringRef> NameOrErr = C.getName();
1523 if (!NameOrErr) {
1524 consumeError(NameOrErr.takeError());
1525 Expected<StringRef> NameOrErr = C.getRawName();
1526 if (!NameOrErr)
1527 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1528 StringRef RawName = NameOrErr.get();
1529 outs() << RawName << "\n";
1530 } else {
1531 StringRef Name = NameOrErr.get();
1532 outs() << Name << "\n";
1533 }
1534 } else {
1535 Expected<StringRef> NameOrErr = C.getRawName();
1536 if (!NameOrErr)
1537 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1538 StringRef RawName = NameOrErr.get();
1539 outs() << RawName << "\n";
1540 }
1541}
1542
1543static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
1544 bool print_offset,
1545 StringRef ArchitectureName = StringRef()) {
1546 Error Err = Error::success();
1547 ;
1548 for (const auto &C : A->children(Err, false))
1549 printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
1550
1551 if (Err)
1552 report_error(StringRef(), Filename, std::move(Err), ArchitectureName);
1553}
1554
1555// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1556// -arch flags selecting just those slices as specified by them and also parses
1557// archive files. Then for each individual Mach-O file ProcessMachO() is
1558// called to process the file based on the command line options.
1559void llvm::ParseInputMachO(StringRef Filename) {
1560 // Check for -arch all and verifiy the -arch flags are valid.
1561 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1562 if (ArchFlags[i] == "all") {
1563 ArchAll = true;
1564 } else {
1565 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1566 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1567 "'for the -arch option\n";
1568 return;
1569 }
1570 }
1571 }
1572
1573 // Attempt to open the binary.
1574 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1575 if (!BinaryOrErr) {
1576 if (auto E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
1577 report_error(Filename, std::move(E));
1578 else
1579 outs() << Filename << ": is not an object file\n";
1580 return;
1581 }
1582 Binary &Bin = *BinaryOrErr.get().getBinary();
1583
1584 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1585 outs() << "Archive : " << Filename << "\n";
1586 if (ArchiveHeaders)
1587 printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
1588
1589 Error Err = Error::success();
1590 for (auto &C : A->children(Err)) {
1591 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1592 if (!ChildOrErr) {
1593 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1594 report_error(Filename, C, std::move(E));
1595 continue;
1596 }
1597 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1598 if (!checkMachOAndArchFlags(O, Filename))
1599 return;
1600 ProcessMachO(Filename, O, O->getFileName());
1601 }
1602 }
1603 if (Err)
1604 report_error(Filename, std::move(Err));
1605 return;
1606 }
1607 if (UniversalHeaders) {
1608 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
1609 printMachOUniversalHeaders(UB, !NonVerbose);
1610 }
1611 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1612 // If we have a list of architecture flags specified dump only those.
1613 if (!ArchAll && ArchFlags.size() != 0) {
1614 // Look for a slice in the universal binary that matches each ArchFlag.
1615 bool ArchFound;
1616 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1617 ArchFound = false;
1618 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1619 E = UB->end_objects();
1620 I != E; ++I) {
1621 if (ArchFlags[i] == I->getArchFlagName()) {
1622 ArchFound = true;
1623 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
1624 I->getAsObjectFile();
1625 std::string ArchitectureName = "";
1626 if (ArchFlags.size() > 1)
1627 ArchitectureName = I->getArchFlagName();
1628 if (ObjOrErr) {
1629 ObjectFile &O = *ObjOrErr.get();
1630 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1631 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1632 } else if (auto E = isNotObjectErrorInvalidFileType(
1633 ObjOrErr.takeError())) {
1634 report_error(Filename, StringRef(), std::move(E),
1635 ArchitectureName);
1636 continue;
1637 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1638 I->getAsArchive()) {
1639 std::unique_ptr<Archive> &A = *AOrErr;
1640 outs() << "Archive : " << Filename;
1641 if (!ArchitectureName.empty())
1642 outs() << " (architecture " << ArchitectureName << ")";
1643 outs() << "\n";
1644 if (ArchiveHeaders)
1645 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1646 ArchiveMemberOffsets, ArchitectureName);
1647 Error Err = Error::success();
1648 for (auto &C : A->children(Err)) {
1649 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1650 if (!ChildOrErr) {
1651 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1652 report_error(Filename, C, std::move(E), ArchitectureName);
1653 continue;
1654 }
1655 if (MachOObjectFile *O =
1656 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1657 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
1658 }
1659 if (Err)
1660 report_error(Filename, std::move(Err));
1661 } else {
1662 consumeError(AOrErr.takeError());
1663 error("Mach-O universal file: " + Filename + " for " +
1664 "architecture " + StringRef(I->getArchFlagName()) +
1665 " is not a Mach-O file or an archive file");
1666 }
1667 }
1668 }
1669 if (!ArchFound) {
1670 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1671 << "architecture: " + ArchFlags[i] + "\n";
1672 return;
1673 }
1674 }
1675 return;
1676 }
1677 // No architecture flags were specified so if this contains a slice that
1678 // matches the host architecture dump only that.
1679 if (!ArchAll) {
1680 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1681 E = UB->end_objects();
1682 I != E; ++I) {
1683 if (MachOObjectFile::getHostArch().getArchName() ==
1684 I->getArchFlagName()) {
1685 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1686 std::string ArchiveName;
1687 ArchiveName.clear();
1688 if (ObjOrErr) {
1689 ObjectFile &O = *ObjOrErr.get();
1690 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1691 ProcessMachO(Filename, MachOOF);
1692 } else if (auto E = isNotObjectErrorInvalidFileType(
1693 ObjOrErr.takeError())) {
1694 report_error(Filename, std::move(E));
1695 continue;
1696 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1697 I->getAsArchive()) {
1698 std::unique_ptr<Archive> &A = *AOrErr;
1699 outs() << "Archive : " << Filename << "\n";
1700 if (ArchiveHeaders)
1701 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1702 ArchiveMemberOffsets);
1703 Error Err = Error::success();
1704 for (auto &C : A->children(Err)) {
1705 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1706 if (!ChildOrErr) {
1707 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1708 report_error(Filename, C, std::move(E));
1709 continue;
1710 }
1711 if (MachOObjectFile *O =
1712 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1713 ProcessMachO(Filename, O, O->getFileName());
1714 }
1715 if (Err)
1716 report_error(Filename, std::move(Err));
1717 } else {
1718 consumeError(AOrErr.takeError());
1719 error("Mach-O universal file: " + Filename + " for architecture " +
1720 StringRef(I->getArchFlagName()) +
1721 " is not a Mach-O file or an archive file");
1722 }
1723 return;
1724 }
1725 }
1726 }
1727 // Either all architectures have been specified or none have been specified
1728 // and this does not contain the host architecture so dump all the slices.
1729 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1730 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1731 E = UB->end_objects();
1732 I != E; ++I) {
1733 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1734 std::string ArchitectureName = "";
1735 if (moreThanOneArch)
1736 ArchitectureName = I->getArchFlagName();
1737 if (ObjOrErr) {
1738 ObjectFile &Obj = *ObjOrErr.get();
1739 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
1740 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1741 } else if (auto E = isNotObjectErrorInvalidFileType(
1742 ObjOrErr.takeError())) {
1743 report_error(StringRef(), Filename, std::move(E), ArchitectureName);
1744 continue;
1745 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1746 I->getAsArchive()) {
1747 std::unique_ptr<Archive> &A = *AOrErr;
1748 outs() << "Archive : " << Filename;
1749 if (!ArchitectureName.empty())
1750 outs() << " (architecture " << ArchitectureName << ")";
1751 outs() << "\n";
1752 if (ArchiveHeaders)
1753 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1754 ArchiveMemberOffsets, ArchitectureName);
1755 Error Err = Error::success();
1756 for (auto &C : A->children(Err)) {
1757 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1758 if (!ChildOrErr) {
1759 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1760 report_error(Filename, C, std::move(E), ArchitectureName);
1761 continue;
1762 }
1763 if (MachOObjectFile *O =
1764 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1765 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
1766 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1767 ArchitectureName);
1768 }
1769 }
1770 if (Err)
1771 report_error(Filename, std::move(Err));
1772 } else {
1773 consumeError(AOrErr.takeError());
1774 error("Mach-O universal file: " + Filename + " for architecture " +
1775 StringRef(I->getArchFlagName()) +
1776 " is not a Mach-O file or an archive file");
1777 }
1778 }
1779 return;
1780 }
1781 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1782 if (!checkMachOAndArchFlags(O, Filename))
1783 return;
1784 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
1785 ProcessMachO(Filename, MachOOF);
1786 } else
1787 errs() << "llvm-objdump: '" << Filename << "': "
1788 << "Object is not a Mach-O file type.\n";
1789 return;
1790 }
1791 llvm_unreachable("Input object can't be invalid at this point")::llvm::llvm_unreachable_internal("Input object can't be invalid at this point"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/llvm-objdump/MachODump.cpp"
, 1791)
;
1792}
1793
1794// The block of info used by the Symbolizer call backs.
1795struct DisassembleInfo {
1796 bool verbose;
1797 MachOObjectFile *O;
1798 SectionRef S;
1799 SymbolAddressMap *AddrMap;
1800 std::vector<SectionRef> *Sections;
1801 const char *class_name;
1802 const char *selector_name;
1803 char *method;
1804 char *demangled_name;
1805 uint64_t adrp_addr;
1806 uint32_t adrp_inst;
1807 std::unique_ptr<SymbolAddressMap> bindtable;
1808 uint32_t depth;
1809};
1810
1811// SymbolizerGetOpInfo() is the operand information call back function.
1812// This is called to get the symbolic information for operand(s) of an
1813// instruction when it is being done. This routine does this from
1814// the relocation information, symbol table, etc. That block of information
1815// is a pointer to the struct DisassembleInfo that was passed when the
1816// disassembler context was created and passed to back to here when
1817// called back by the disassembler for instruction operands that could have
1818// relocation information. The address of the instruction containing operand is
1819// at the Pc parameter. The immediate value the operand has is passed in
1820// op_info->Value and is at Offset past the start of the instruction and has a
1821// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1822// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1823// names and addends of the symbolic expression to add for the operand. The
1824// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1825// information is returned then this function returns 1 else it returns 0.
1826static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1827 uint64_t Size, int TagType, void *TagBuf) {
1828 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1829 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
1830 uint64_t value = op_info->Value;
1831
1832 // Make sure all fields returned are zero if we don't set them.
1833 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1834 op_info->Value = value;
1835
1836 // If the TagType is not the value 1 which it code knows about or if no
1837 // verbose symbolic information is wanted then just return 0, indicating no
1838 // information is being returned.
1839 if (TagType != 1 || !info->verbose)
1840 return 0;
1841
1842 unsigned int Arch = info->O->getArch();
1843 if (Arch == Triple::x86) {
1844 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1845 return 0;
1846 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1847 // TODO:
1848 // Search the external relocation entries of a fully linked image
1849 // (if any) for an entry that matches this segment offset.
1850 // uint32_t seg_offset = (Pc + Offset);
1851 return 0;
1852 }
1853 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1854 // for an entry for this section offset.
1855 uint32_t sect_addr = info->S.getAddress();
1856 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1857 bool reloc_found = false;
1858 DataRefImpl Rel;
1859 MachO::any_relocation_info RE;
1860 bool isExtern = false;
1861 SymbolRef Symbol;
1862 bool r_scattered = false;
1863 uint32_t r_value, pair_r_value, r_type;
1864 for (const RelocationRef &Reloc : info->S.relocations()) {
1865 uint64_t RelocOffset = Reloc.getOffset();
1866 if (RelocOffset == sect_offset) {
1867 Rel = Reloc.getRawDataRefImpl();
1868 RE = info->O->getRelocation(Rel);
1869 r_type = info->O->getAnyRelocationType(RE);
1870 r_scattered = info->O->isRelocationScattered(RE);
1871 if (r_scattered) {
1872 r_value = info->O->getScatteredRelocationValue(RE);
1873 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1874 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1875 DataRefImpl RelNext = Rel;
1876 info->O->moveRelocationNext(RelNext);
1877 MachO::any_relocation_info RENext;
1878 RENext = info->O->getRelocation(RelNext);
1879 if (info->O->isRelocationScattered(RENext))
1880 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1881 else
1882 return 0;
1883 }
1884 } else {
1885 isExtern = info->O->getPlainRelocationExternal(RE);
1886 if (isExtern) {
1887 symbol_iterator RelocSym = Reloc.getSymbol();
1888 Symbol = *RelocSym;
1889 }
1890 }
1891 reloc_found = true;
1892 break;
1893 }
1894 }
1895 if (reloc_found && isExtern) {
1896 Expected<StringRef> SymName = Symbol.getName();
1897 if (!SymName)
1898 report_error(info->O->getFileName(), SymName.takeError());
1899 const char *name = SymName->data();
1900 op_info->AddSymbol.Present = 1;
1901 op_info->AddSymbol.Name = name;
1902 // For i386 extern relocation entries the value in the instruction is
1903 // the offset from the symbol, and value is already set in op_info->Value.
1904 return 1;
1905 }
1906 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1907 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
1908 const char *add = GuessSymbolName(r_value, info->AddrMap);
1909 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
1910 uint32_t offset = value - (r_value - pair_r_value);
1911 op_info->AddSymbol.Present = 1;
1912 if (add != nullptr)
1913 op_info->AddSymbol.Name = add;
1914 else
1915 op_info->AddSymbol.Value = r_value;
1916 op_info->SubtractSymbol.Present = 1;
1917 if (sub != nullptr)
1918 op_info->SubtractSymbol.Name = sub;
1919 else
1920 op_info->SubtractSymbol.Value = pair_r_value;
1921 op_info->Value = offset;
1922 return 1;
1923 }
1924 return 0;
1925 }
1926 if (Arch == Triple::x86_64) {
1927 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1928 return 0;
1929 // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
1930 // relocation entries of a linked image (if any) for an entry that matches
1931 // this segment offset.
1932 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1933 uint64_t seg_offset = Pc + Offset;
1934 bool reloc_found = false;
1935 DataRefImpl Rel;
1936 MachO::any_relocation_info RE;
1937 bool isExtern = false;
1938 SymbolRef Symbol;
1939 for (const RelocationRef &Reloc : info->O->external_relocations()) {
1940 uint64_t RelocOffset = Reloc.getOffset();
1941 if (RelocOffset == seg_offset) {
1942 Rel = Reloc.getRawDataRefImpl();
1943 RE = info->O->getRelocation(Rel);
1944 // external relocation entries should always be external.
1945 isExtern = info->O->getPlainRelocationExternal(RE);
1946 if (isExtern) {
1947 symbol_iterator RelocSym = Reloc.getSymbol();
1948 Symbol = *RelocSym;
1949 }
1950 reloc_found = true;
1951 break;
1952 }
1953 }
1954 if (reloc_found && isExtern) {
1955 // The Value passed in will be adjusted by the Pc if the instruction
1956 // adds the Pc. But for x86_64 external relocation entries the Value
1957 // is the offset from the external symbol.
1958 if (info->O->getAnyRelocationPCRel(RE))
1959 op_info->Value -= Pc + Offset + Size;
1960 Expected<StringRef> SymName = Symbol.getName();
1961 if (!SymName)
1962 report_error(info->O->getFileName(), SymName.takeError());
1963 const char *name = SymName->data();
1964 op_info->AddSymbol.Present = 1;
1965 op_info->AddSymbol.Name = name;
1966 return 1;
1967 }
1968 return 0;
1969 }
1970 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1971 // for an entry for this section offset.
1972 uint64_t sect_addr = info->S.getAddress();
1973 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1974 bool reloc_found = false;
1975 DataRefImpl Rel;
1976 MachO::any_relocation_info RE;
1977 bool isExtern = false;
1978 SymbolRef Symbol;
1979 for (const RelocationRef &Reloc : info->S.relocations()) {
1980 uint64_t RelocOffset = Reloc.getOffset();
1981 if (RelocOffset == sect_offset) {
1982 Rel = Reloc.getRawDataRefImpl();
1983 RE = info->O->getRelocation(Rel);
1984 // NOTE: Scattered relocations don't exist on x86_64.
1985 isExtern = info->O->getPlainRelocationExternal(RE);
1986 if (isExtern) {
1987 symbol_iterator RelocSym = Reloc.getSymbol();
1988 Symbol = *RelocSym;
1989 }
1990 reloc_found = true;
1991 break;
1992 }
1993 }
1994 if (reloc_found && isExtern) {
1995 // The Value passed in will be adjusted by the Pc if the instruction
1996 // adds the Pc. But for x86_64 external relocation entries the Value
1997 // is the offset from the external symbol.
1998 if (info->O->getAnyRelocationPCRel(RE))
1999 op_info->Value -= Pc + Offset + Size;
2000 Expected<StringRef> SymName = Symbol.getName();
2001 if (!SymName)
2002 report_error(info->O->getFileName(), SymName.takeError());
2003 const char *name = SymName->data();
2004 unsigned Type = info->O->getAnyRelocationType(RE);
2005 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
2006 DataRefImpl RelNext = Rel;
2007 info->O->moveRelocationNext(RelNext);
2008 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2009 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
2010 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
2011 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
2012 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
2013 op_info->SubtractSymbol.Present = 1;
2014 op_info->SubtractSymbol.Name = name;
2015 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
2016 Symbol = *RelocSymNext;
2017 Expected<StringRef> SymNameNext = Symbol.getName();
2018 if (!SymNameNext)
2019 report_error(info->O->getFileName(), SymNameNext.takeError());
2020 name = SymNameNext->data();
2021 }
2022 }
2023 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2024 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2025 op_info->AddSymbol.Present = 1;
2026 op_info->AddSymbol.Name = name;
2027 return 1;
2028 }
2029 return 0;
2030 }
2031 if (Arch == Triple::arm) {
2032 if (Offset != 0 || (Size != 4 && Size != 2))
2033 return 0;
2034 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2035 // TODO:
2036 // Search the external relocation entries of a fully linked image
2037 // (if any) for an entry that matches this segment offset.
2038 // uint32_t seg_offset = (Pc + Offset);
2039 return 0;
2040 }
2041 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2042 // for an entry for this section offset.
2043 uint32_t sect_addr = info->S.getAddress();
2044 uint32_t sect_offset = (Pc + Offset) - sect_addr;
2045 DataRefImpl Rel;
2046 MachO::any_relocation_info RE;
2047 bool isExtern = false;
2048 SymbolRef Symbol;
2049 bool r_scattered = false;
2050 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
2051 auto Reloc =
2052 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2053 uint64_t RelocOffset = Reloc.getOffset();
2054 return RelocOffset == sect_offset;
2055 });
2056
2057 if (Reloc == info->S.relocations().end())
2058 return 0;
2059
2060 Rel = Reloc->getRawDataRefImpl();
2061 RE = info->O->getRelocation(Rel);
2062 r_length = info->O->getAnyRelocationLength(RE);
2063 r_scattered = info->O->isRelocationScattered(RE);
2064 if (r_scattered) {
2065 r_value = info->O->getScatteredRelocationValue(RE);
2066 r_type = info->O->getScatteredRelocationType(RE);
2067 } else {
2068 r_type = info->O->getAnyRelocationType(RE);
2069 isExtern = info->O->getPlainRelocationExternal(RE);
2070 if (isExtern) {
2071 symbol_iterator RelocSym = Reloc->getSymbol();
2072 Symbol = *RelocSym;
2073 }
2074 }
2075 if (r_type == MachO::ARM_RELOC_HALF ||
2076 r_type == MachO::ARM_RELOC_SECTDIFF ||
2077 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2078 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2079 DataRefImpl RelNext = Rel;
2080 info->O->moveRelocationNext(RelNext);
2081 MachO::any_relocation_info RENext;
2082 RENext = info->O->getRelocation(RelNext);
2083 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
2084 if (info->O->isRelocationScattered(RENext))
2085 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2086 }
2087
2088 if (isExtern) {
2089 Expected<StringRef> SymName = Symbol.getName();
2090 if (!SymName)
2091 report_error(info->O->getFileName(), SymName.takeError());
2092 const char *name = SymName->data();
2093 op_info->AddSymbol.Present = 1;
2094 op_info->AddSymbol.Name = name;
2095 switch (r_type) {
2096 case MachO::ARM_RELOC_HALF:
2097 if ((r_length & 0x1) == 1) {
2098 op_info->Value = value << 16 | other_half;
2099 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI161;
2100 } else {
2101 op_info->Value = other_half << 16 | value;
2102 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO162;
2103 }
2104 break;
2105 default:
2106 break;
2107 }
2108 return 1;
2109 }
2110 // If we have a branch that is not an external relocation entry then
2111 // return 0 so the code in tryAddingSymbolicOperand() can use the
2112 // SymbolLookUp call back with the branch target address to look up the
2113 // symbol and possibility add an annotation for a symbol stub.
2114 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2115 r_type == MachO::ARM_THUMB_RELOC_BR22))
2116 return 0;
2117
2118 uint32_t offset = 0;
2119 if (r_type == MachO::ARM_RELOC_HALF ||
2120 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2121 if ((r_length & 0x1) == 1)
2122 value = value << 16 | other_half;
2123 else
2124 value = other_half << 16 | value;
2125 }
2126 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2127 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2128 offset = value - r_value;
2129 value = r_value;
2130 }
2131
2132 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2133 if ((r_length & 0x1) == 1)
2134 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI161;
2135 else
2136 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO162;
2137 const char *add = GuessSymbolName(r_value, info->AddrMap);
2138 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2139 int32_t offset = value - (r_value - pair_r_value);
2140 op_info->AddSymbol.Present = 1;
2141 if (add != nullptr)
2142 op_info->AddSymbol.Name = add;
2143 else
2144 op_info->AddSymbol.Value = r_value;
2145 op_info->SubtractSymbol.Present = 1;
2146 if (sub != nullptr)
2147 op_info->SubtractSymbol.Name = sub;
2148 else
2149 op_info->SubtractSymbol.Value = pair_r_value;
2150 op_info->Value = offset;
2151 return 1;
2152 }
2153
2154 op_info->AddSymbol.Present = 1;
2155 op_info->Value = offset;
2156 if (r_type == MachO::ARM_RELOC_HALF) {
2157 if ((r_length & 0x1) == 1)
2158 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI161;
2159 else
2160 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO162;
2161 }
2162 const char *add = GuessSymbolName(value, info->AddrMap);
2163 if (add != nullptr) {
2164 op_info->AddSymbol.Name = add;
2165 return 1;
2166 }
2167 op_info->AddSymbol.Value = value;
2168 return 1;
2169 }
2170 if (Arch == Triple::aarch64) {
2171 if (Offset != 0 || Size != 4)
2172 return 0;
2173 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2174 // TODO:
2175 // Search the external relocation entries of a fully linked image
2176 // (if any) for an entry that matches this segment offset.
2177 // uint64_t seg_offset = (Pc + Offset);
2178 return 0;
2179 }
2180 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2181 // for an entry for this section offset.
2182 uint64_t sect_addr = info->S.getAddress();
2183 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2184 auto Reloc =
2185 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2186 uint64_t RelocOffset = Reloc.getOffset();
2187 return RelocOffset == sect_offset;
2188 });
2189
2190 if (Reloc == info->S.relocations().end())
2191 return 0;
2192
2193 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2194 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2195 uint32_t r_type = info->O->getAnyRelocationType(RE);
2196 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2197 DataRefImpl RelNext = Rel;
2198 info->O->moveRelocationNext(RelNext);
2199 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2200 if (value == 0) {
2201 value = info->O->getPlainRelocationSymbolNum(RENext);
2202 op_info->Value = value;
2203 }
2204 }
2205 // NOTE: Scattered relocations don't exist on arm64.
2206 if (!info->O->getPlainRelocationExternal(RE))
2207 return 0;
2208 Expected<StringRef> SymName = Reloc->getSymbol()->getName();
2209 if (!SymName)
2210 report_error(info->O->getFileName(), SymName.takeError());
2211 const char *name = SymName->data();
2212 op_info->AddSymbol.Present = 1;
2213 op_info->AddSymbol.Name = name;
2214
2215 switch (r_type) {
2216 case MachO::ARM64_RELOC_PAGE21:
2217 /* @page */
2218 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE1;
2219 break;
2220 case MachO::ARM64_RELOC_PAGEOFF12:
2221 /* @pageoff */
2222 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF2;
2223 break;
2224 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2225 /* @gotpage */
2226 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE3;
2227 break;
2228 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2229 /* @gotpageoff */
2230 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF4;
2231 break;
2232 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2233 /* @tvlppage is not implemented in llvm-mc */
2234 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP5;
2235 break;
2236 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2237 /* @tvlppageoff is not implemented in llvm-mc */
2238 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF6;
2239 break;
2240 default:
2241 case MachO::ARM64_RELOC_BRANCH26:
2242 op_info->VariantKind = LLVMDisassembler_VariantKind_None0;
2243 break;
2244 }
2245 return 1;
2246 }
2247 return 0;
2248}
2249
2250// GuessCstringPointer is passed the address of what might be a pointer to a
2251// literal string in a cstring section. If that address is in a cstring section
2252// it returns a pointer to that string. Else it returns nullptr.
2253static const char *GuessCstringPointer(uint64_t ReferenceValue,
2254 struct DisassembleInfo *info) {
2255 for (const auto &Load : info->O->load_commands()) {
2256 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2257 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2258 for (unsigned J = 0; J < Seg.nsects; ++J) {
2259 MachO::section_64 Sec = info->O->getSection64(Load, J);
2260 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2261 if (section_type == MachO::S_CSTRING_LITERALS &&
2262 ReferenceValue >= Sec.addr &&
2263 ReferenceValue < Sec.addr + Sec.size) {
2264 uint64_t sect_offset = ReferenceValue - Sec.addr;
2265 uint64_t object_offset = Sec.offset + sect_offset;
2266 StringRef MachOContents = info->O->getData();
2267 uint64_t object_size = MachOContents.size();
2268 const char *object_addr = (const char *)MachOContents.data();
2269 if (object_offset < object_size) {
2270 const char *name = object_addr + object_offset;
2271 return name;
2272 } else {
2273 return nullptr;
2274 }
2275 }
2276 }
2277 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2278 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2279 for (unsigned J = 0; J < Seg.nsects; ++J) {
2280 MachO::section Sec = info->O->getSection(Load, J);
2281 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2282 if (section_type == MachO::S_CSTRING_LITERALS &&
2283 ReferenceValue >= Sec.addr &&
2284 ReferenceValue < Sec.addr + Sec.size) {
2285 uint64_t sect_offset = ReferenceValue - Sec.addr;
2286 uint64_t object_offset = Sec.offset + sect_offset;
2287 StringRef MachOContents = info->O->getData();
2288 uint64_t object_size = MachOContents.size();
2289 const char *object_addr = (const char *)MachOContents.data();
2290 if (object_offset < object_size) {
2291 const char *name = object_addr + object_offset;
2292 return name;
2293 } else {
2294 return nullptr;
2295 }
2296 }
2297 }
2298 }
2299 }
2300 return nullptr;
2301}
2302
2303// GuessIndirectSymbol returns the name of the indirect symbol for the
2304// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2305// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2306// symbol name being referenced by the stub or pointer.
2307static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2308 struct DisassembleInfo *info) {
2309 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2310 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
2311 for (const auto &Load : info->O->load_commands()) {
2312 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2313 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2314 for (unsigned J = 0; J < Seg.nsects; ++J) {
2315 MachO::section_64 Sec = info->O->getSection64(Load, J);
2316 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2317 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2318 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2319 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2320 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2321 section_type == MachO::S_SYMBOL_STUBS) &&
2322 ReferenceValue >= Sec.addr &&
2323 ReferenceValue < Sec.addr + Sec.size) {
2324 uint32_t stride;
2325 if (section_type == MachO::S_SYMBOL_STUBS)
2326 stride = Sec.reserved2;
2327 else
2328 stride = 8;
2329 if (stride == 0)
2330 return nullptr;
2331 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2332 if (index < Dysymtab.nindirectsyms) {
2333 uint32_t indirect_symbol =
2334 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2335 if (indirect_symbol < Symtab.nsyms) {
2336 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2337 SymbolRef Symbol = *Sym;
2338 Expected<StringRef> SymName = Symbol.getName();
2339 if (!SymName)
2340 report_error(info->O->getFileName(), SymName.takeError());
2341 const char *name = SymName->data();
2342 return name;
2343 }
2344 }
2345 }
2346 }
2347 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2348 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2349 for (unsigned J = 0; J < Seg.nsects; ++J) {
2350 MachO::section Sec = info->O->getSection(Load, J);
2351 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2352 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2353 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2354 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2355 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2356 section_type == MachO::S_SYMBOL_STUBS) &&
2357 ReferenceValue >= Sec.addr &&
2358 ReferenceValue < Sec.addr + Sec.size) {
2359 uint32_t stride;
2360 if (section_type == MachO::S_SYMBOL_STUBS)
2361 stride = Sec.reserved2;
2362 else
2363 stride = 4;
2364 if (stride == 0)
2365 return nullptr;
2366 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2367 if (index < Dysymtab.nindirectsyms) {
2368 uint32_t indirect_symbol =
2369 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2370 if (indirect_symbol < Symtab.nsyms) {
2371 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2372 SymbolRef Symbol = *Sym;
2373 Expected<StringRef> SymName = Symbol.getName();
2374 if (!SymName)
2375 report_error(info->O->getFileName(), SymName.takeError());
2376 const char *name = SymName->data();
2377 return name;
2378 }
2379 }
2380 }
2381 }
2382 }
2383 }
2384 return nullptr;
2385}
2386
2387// method_reference() is called passing it the ReferenceName that might be
2388// a reference it to an Objective-C method call. If so then it allocates and
2389// assembles a method call string with the values last seen and saved in
2390// the DisassembleInfo's class_name and selector_name fields. This is saved
2391// into the method field of the info and any previous string is free'ed.
2392// Then the class_name field in the info is set to nullptr. The method call
2393// string is set into ReferenceName and ReferenceType is set to
2394// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2395// then both ReferenceType and ReferenceName are left unchanged.
2396static void method_reference(struct DisassembleInfo *info,
2397 uint64_t *ReferenceType,
2398 const char **ReferenceName) {
2399 unsigned int Arch = info->O->getArch();
2400 if (*ReferenceName != nullptr) {
2401 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
2402 if (info->selector_name != nullptr) {
2403 if (info->method != nullptr)
2404 free(info->method);
2405 if (info->class_name != nullptr) {
2406 info->method = (char *)malloc(5 + strlen(info->class_name) +
2407 strlen(info->selector_name));
2408 if (info->method != nullptr) {
2409 strcpy(info->method, "+[");
2410 strcat(info->method, info->class_name);
2411 strcat(info->method, " ");
2412 strcat(info->method, info->selector_name);
2413 strcat(info->method, "]");
2414 *ReferenceName = info->method;
2415 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message5;
2416 }
2417 } else {
2418 info->method = (char *)malloc(9 + strlen(info->selector_name));
2419 if (info->method != nullptr) {
2420 if (Arch == Triple::x86_64)
2421 strcpy(info->method, "-[%rdi ");
2422 else if (Arch == Triple::aarch64)
2423 strcpy(info->method, "-[x0 ");
2424 else
2425 strcpy(info->method, "-[r? ");
2426 strcat(info->method, info->selector_name);
2427 strcat(info->method, "]");
2428 *ReferenceName = info->method;
2429 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message5;
2430 }
2431 }
2432 info->class_name = nullptr;
2433 }
2434 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
2435 if (info->selector_name != nullptr) {
2436 if (info->method != nullptr)
2437 free(info->method);
2438 info->method = (char *)malloc(17 + strlen(info->selector_name));
2439 if (info->method != nullptr) {
2440 if (Arch == Triple::x86_64)
2441 strcpy(info->method, "-[[%rdi super] ");
2442 else if (Arch == Triple::aarch64)
2443 strcpy(info->method, "-[[x0 super] ");
2444 else
2445 strcpy(info->method, "-[[r? super] ");
2446 strcat(info->method, info->selector_name);
2447 strcat(info->method, "]");
2448 *ReferenceName = info->method;
2449 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message5;
2450 }
2451 info->class_name = nullptr;
2452 }
2453 }
2454 }
2455}
2456
2457// GuessPointerPointer() is passed the address of what might be a pointer to
2458// a reference to an Objective-C class, selector, message ref or cfstring.
2459// If so the value of the pointer is returned and one of the booleans are set
2460// to true. If not zero is returned and all the booleans are set to false.
2461static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2462 struct DisassembleInfo *info,
2463 bool &classref, bool &selref, bool &msgref,
2464 bool &cfstring) {
2465 classref = false;
2466 selref = false;
2467 msgref = false;
2468 cfstring = false;
2469 for (const auto &Load : info->O->load_commands()) {
2470 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2471 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2472 for (unsigned J = 0; J < Seg.nsects; ++J) {
2473 MachO::section_64 Sec = info->O->getSection64(Load, J);
2474 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2475 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2476 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2477 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2478 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2479 ReferenceValue >= Sec.addr &&
2480 ReferenceValue < Sec.addr + Sec.size) {
2481 uint64_t sect_offset = ReferenceValue - Sec.addr;
2482 uint64_t object_offset = Sec.offset + sect_offset;
2483 StringRef MachOContents = info->O->getData();
2484 uint64_t object_size = MachOContents.size();
2485 const char *object_addr = (const char *)MachOContents.data();
2486 if (object_offset < object_size) {
2487 uint64_t pointer_value;
2488 memcpy(&pointer_value, object_addr + object_offset,
2489 sizeof(uint64_t));
2490 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2491 sys::swapByteOrder(pointer_value);
2492 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2493 selref = true;
2494 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2495 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2496 classref = true;
2497 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2498 ReferenceValue + 8 < Sec.addr + Sec.size) {
2499 msgref = true;
2500 memcpy(&pointer_value, object_addr + object_offset + 8,
2501 sizeof(uint64_t));
2502 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2503 sys::swapByteOrder(pointer_value);
2504 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2505 cfstring = true;
2506 return pointer_value;
2507 } else {
2508 return 0;
2509 }
2510 }
2511 }
2512 }
2513 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
2514 }
2515 return 0;
2516}
2517
2518// get_pointer_64 returns a pointer to the bytes in the object file at the
2519// Address from a section in the Mach-O file. And indirectly returns the
2520// offset into the section, number of bytes left in the section past the offset
2521// and which section is was being referenced. If the Address is not in a
2522// section nullptr is returned.
2523static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2524 uint32_t &left, SectionRef &S,
2525 DisassembleInfo *info,
2526 bool objc_only = false) {
2527 offset = 0;
2528 left = 0;
2529 S = SectionRef();
2530 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2531 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2532 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
2533 if (SectSize == 0)
2534 continue;
2535 if (objc_only) {
2536 StringRef SectName;
2537 ((*(info->Sections))[SectIdx]).getName(SectName);
2538 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2539 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2540 if (SegName != "__OBJC" && SectName != "__cstring")
2541 continue;
2542 }
2543 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2544 S = (*(info->Sections))[SectIdx];
2545 offset = Address - SectAddress;
2546 left = SectSize - offset;
2547 StringRef SectContents;
2548 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2549 return SectContents.data() + offset;
2550 }
2551 }
2552 return nullptr;
2553}
2554
2555static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2556 uint32_t &left, SectionRef &S,
2557 DisassembleInfo *info,
2558 bool objc_only = false) {
2559 return get_pointer_64(Address, offset, left, S, info, objc_only);
2560}
2561
2562// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2563// the symbol indirectly through n_value. Based on the relocation information
2564// for the specified section offset in the specified section reference.
2565// If no relocation information is found and a non-zero ReferenceValue for the
2566// symbol is passed, look up that address in the info's AddrMap.
2567static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2568 DisassembleInfo *info, uint64_t &n_value,
2569 uint64_t ReferenceValue = 0) {
2570 n_value = 0;
2571 if (!info->verbose)
2572 return nullptr;
2573
2574 // See if there is an external relocation entry at the sect_offset.
2575 bool reloc_found = false;
2576 DataRefImpl Rel;
2577 MachO::any_relocation_info RE;
2578 bool isExtern = false;
2579 SymbolRef Symbol;
2580 for (const RelocationRef &Reloc : S.relocations()) {
2581 uint64_t RelocOffset = Reloc.getOffset();
2582 if (RelocOffset == sect_offset) {
2583 Rel = Reloc.getRawDataRefImpl();
2584 RE = info->O->getRelocation(Rel);
2585 if (info->O->isRelocationScattered(RE))
2586 continue;
2587 isExtern = info->O->getPlainRelocationExternal(RE);
2588 if (isExtern) {
2589 symbol_iterator RelocSym = Reloc.getSymbol();
2590 Symbol = *RelocSym;
2591 }
2592 reloc_found = true;
2593 break;
2594 }
2595 }
2596 // If there is an external relocation entry for a symbol in this section
2597 // at this section_offset then use that symbol's value for the n_value
2598 // and return its name.
2599 const char *SymbolName = nullptr;
2600 if (reloc_found && isExtern) {
2601 n_value = Symbol.getValue();
2602 Expected<StringRef> NameOrError = Symbol.getName();
2603 if (!NameOrError)
2604 report_error(info->O->getFileName(), NameOrError.takeError());
2605 StringRef Name = *NameOrError;
2606 if (!Name.empty()) {
2607 SymbolName = Name.data();
2608 return SymbolName;
2609 }
2610 }
2611
2612 // TODO: For fully linked images, look through the external relocation
2613 // entries off the dynamic symtab command. For these the r_offset is from the
2614 // start of the first writeable segment in the Mach-O file. So the offset
2615 // to this section from that segment is passed to this routine by the caller,
2616 // as the database_offset. Which is the difference of the section's starting
2617 // address and the first writable segment.
2618 //
2619 // NOTE: need add passing the database_offset to this routine.
2620
2621 // We did not find an external relocation entry so look up the ReferenceValue
2622 // as an address of a symbol and if found return that symbol's name.
2623 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
2624
2625 return SymbolName;
2626}
2627
2628static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2629 DisassembleInfo *info,
2630 uint32_t ReferenceValue) {
2631 uint64_t n_value64;
2632 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2633}
2634
2635// These are structs in the Objective-C meta data and read to produce the
2636// comments for disassembly. While these are part of the ABI they are no
2637// public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
2638// .
2639
2640// The cfstring object in a 64-bit Mach-O file.
2641struct cfstring64_t {
2642 uint64_t isa; // class64_t * (64-bit pointer)
2643 uint64_t flags; // flag bits
2644 uint64_t characters; // char * (64-bit pointer)
2645 uint64_t length; // number of non-NULL characters in above
2646};
2647
2648// The class object in a 64-bit Mach-O file.
2649struct class64_t {
2650 uint64_t isa; // class64_t * (64-bit pointer)
2651 uint64_t superclass; // class64_t * (64-bit pointer)
2652 uint64_t cache; // Cache (64-bit pointer)
2653 uint64_t vtable; // IMP * (64-bit pointer)
2654 uint64_t data; // class_ro64_t * (64-bit pointer)
2655};
2656
2657struct class32_t {
2658 uint32_t isa; /* class32_t * (32-bit pointer) */
2659 uint32_t superclass; /* class32_t * (32-bit pointer) */
2660 uint32_t cache; /* Cache (32-bit pointer) */
2661 uint32_t vtable; /* IMP * (32-bit pointer) */
2662 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2663};
2664
2665struct class_ro64_t {
2666 uint32_t flags;
2667 uint32_t instanceStart;
2668 uint32_t instanceSize;
2669 uint32_t reserved;
2670 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2671 uint64_t name; // const char * (64-bit pointer)
2672 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2673 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2674 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2675 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2676 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2677};
2678
2679struct class_ro32_t {
2680 uint32_t flags;
2681 uint32_t instanceStart;
2682 uint32_t instanceSize;
2683 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2684 uint32_t name; /* const char * (32-bit pointer) */
2685 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2686 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2687 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2688 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2689 uint32_t baseProperties; /* const struct objc_property_list *
2690 (32-bit pointer) */
2691};
2692
2693/* Values for class_ro{64,32}_t->flags */
2694#define RO_META(1 << 0) (1 << 0)
2695#define RO_ROOT(1 << 1) (1 << 1)
2696#define RO_HAS_CXX_STRUCTORS(1 << 2) (1 << 2)
2697
2698struct method_list64_t {
2699 uint32_t entsize;
2700 uint32_t count;
2701 /* struct method64_t first; These structures follow inline */
2702};
2703
2704struct method_list32_t {
2705 uint32_t entsize;
2706 uint32_t count;
2707 /* struct method32_t first; These structures follow inline */
2708};
2709
2710struct method64_t {
2711 uint64_t name; /* SEL (64-bit pointer) */
2712 uint64_t types; /* const char * (64-bit pointer) */
2713 uint64_t imp; /* IMP (64-bit pointer) */
2714};
2715
2716struct method32_t {
2717 uint32_t name; /* SEL (32-bit pointer) */
2718 uint32_t types; /* const char * (32-bit pointer) */
2719 uint32_t imp; /* IMP (32-bit pointer) */
2720};
2721
2722struct protocol_list64_t {
2723 uint64_t count; /* uintptr_t (a 64-bit value) */
2724 /* struct protocol64_t * list[0]; These pointers follow inline */
2725};
2726
2727struct protocol_list32_t {
2728 uint32_t count; /* uintptr_t (a 32-bit value) */
2729 /* struct protocol32_t * list[0]; These pointers follow inline */
2730};
2731
2732struct protocol64_t {
2733 uint64_t isa; /* id * (64-bit pointer) */
2734 uint64_t name; /* const char * (64-bit pointer) */
2735 uint64_t protocols; /* struct protocol_list64_t *
2736 (64-bit pointer) */
2737 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2738 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2739 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2740 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2741 uint64_t instanceProperties; /* struct objc_property_list *
2742 (64-bit pointer) */
2743};
2744
2745struct protocol32_t {
2746 uint32_t isa; /* id * (32-bit pointer) */
2747 uint32_t name; /* const char * (32-bit pointer) */
2748 uint32_t protocols; /* struct protocol_list_t *
2749 (32-bit pointer) */
2750 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2751 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2752 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2753 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2754 uint32_t instanceProperties; /* struct objc_property_list *
2755 (32-bit pointer) */
2756};
2757
2758struct ivar_list64_t {
2759 uint32_t entsize;
2760 uint32_t count;
2761 /* struct ivar64_t first; These structures follow inline */
2762};
2763
2764struct ivar_list32_t {
2765 uint32_t entsize;
2766 uint32_t count;
2767 /* struct ivar32_t first; These structures follow inline */
2768};
2769
2770struct ivar64_t {
2771 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2772 uint64_t name; /* const char * (64-bit pointer) */
2773 uint64_t type; /* const char * (64-bit pointer) */
2774 uint32_t alignment;
2775 uint32_t size;
2776};
2777
2778struct ivar32_t {
2779 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2780 uint32_t name; /* const char * (32-bit pointer) */
2781 uint32_t type; /* const char * (32-bit pointer) */
2782 uint32_t alignment;
2783 uint32_t size;
2784};
2785
2786struct objc_property_list64 {
2787 uint32_t entsize;
2788 uint32_t count;
2789 /* struct objc_property64 first; These structures follow inline */
2790};
2791
2792struct objc_property_list32 {
2793 uint32_t entsize;
2794 uint32_t count;
2795 /* struct objc_property32 first; These structures follow inline */
2796};
2797
2798struct objc_property64 {
2799 uint64_t name; /* const char * (64-bit pointer) */
2800 uint64_t attributes; /* const char * (64-bit pointer) */
2801};
2802
2803struct objc_property32 {
2804 uint32_t name; /* const char * (32-bit pointer) */
2805 uint32_t attributes; /* const char * (32-bit pointer) */
2806};
2807
2808struct category64_t {
2809 uint64_t name; /* const char * (64-bit pointer) */
2810 uint64_t cls; /* struct class_t * (64-bit pointer) */
2811 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2812 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2813 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2814 uint64_t instanceProperties; /* struct objc_property_list *
2815 (64-bit pointer) */
2816};
2817
2818struct category32_t {
2819 uint32_t name; /* const char * (32-bit pointer) */
2820 uint32_t cls; /* struct class_t * (32-bit pointer) */
2821 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2822 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2823 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2824 uint32_t instanceProperties; /* struct objc_property_list *
2825 (32-bit pointer) */
2826};
2827
2828struct objc_image_info64 {
2829 uint32_t version;
2830 uint32_t flags;
2831};
2832struct objc_image_info32 {
2833 uint32_t version;
2834 uint32_t flags;
2835};
2836struct imageInfo_t {
2837 uint32_t version;
2838 uint32_t flags;
2839};
2840/* masks for objc_image_info.flags */
2841#define OBJC_IMAGE_IS_REPLACEMENT(1 << 0) (1 << 0)
2842#define OBJC_IMAGE_SUPPORTS_GC(1 << 1) (1 << 1)
2843
2844struct message_ref64 {
2845 uint64_t imp; /* IMP (64-bit pointer) */
2846 uint64_t sel; /* SEL (64-bit pointer) */
2847};
2848
2849struct message_ref32 {
2850 uint32_t imp; /* IMP (32-bit pointer) */
2851 uint32_t sel; /* SEL (32-bit pointer) */
2852};
2853
2854// Objective-C 1 (32-bit only) meta data structs.
2855
2856struct objc_module_t {
2857 uint32_t version;
2858 uint32_t size;
2859 uint32_t name; /* char * (32-bit pointer) */
2860 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2861};
2862
2863struct objc_symtab_t {
2864 uint32_t sel_ref_cnt;
2865 uint32_t refs; /* SEL * (32-bit pointer) */
2866 uint16_t cls_def_cnt;
2867 uint16_t cat_def_cnt;
2868 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2869};
2870
2871struct objc_class_t {
2872 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2873 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2874 uint32_t name; /* const char * (32-bit pointer) */
2875 int32_t version;
2876 int32_t info;
2877 int32_t instance_size;
2878 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2879 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2880 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2881 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2882};
2883
2884#define CLS_GETINFO(cls, infomask)((cls)->info & (infomask)) ((cls)->info & (infomask))
2885// class is not a metaclass
2886#define CLS_CLASS0x1 0x1
2887// class is a metaclass
2888#define CLS_META0x2 0x2
2889
2890struct objc_category_t {
2891 uint32_t category_name; /* char * (32-bit pointer) */
2892 uint32_t class_name; /* char * (32-bit pointer) */
2893 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2894 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2895 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2896};
2897
2898struct objc_ivar_t {
2899 uint32_t ivar_name; /* char * (32-bit pointer) */
2900 uint32_t ivar_type; /* char * (32-bit pointer) */
2901 int32_t ivar_offset;
2902};
2903
2904struct objc_ivar_list_t {
2905 int32_t ivar_count;
2906 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2907};
2908
2909struct objc_method_list_t {
2910 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2911 int32_t method_count;
2912 // struct objc_method_t method_list[1]; /* variable length structure */
2913};
2914
2915struct objc_method_t {
2916 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2917 uint32_t method_types; /* char * (32-bit pointer) */
2918 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2919 (32-bit pointer) */
2920};
2921
2922struct objc_protocol_list_t {
2923 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2924 int32_t count;
2925 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2926 // (32-bit pointer) */
2927};
2928
2929struct objc_protocol_t {
2930 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2931 uint32_t protocol_name; /* char * (32-bit pointer) */
2932 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2933 uint32_t instance_methods; /* struct objc_method_description_list *
2934 (32-bit pointer) */
2935 uint32_t class_methods; /* struct objc_method_description_list *
2936 (32-bit pointer) */
2937};
2938
2939struct objc_method_description_list_t {
2940 int32_t count;
2941 // struct objc_method_description_t list[1];
2942};
2943
2944struct objc_method_description_t {
2945 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2946 uint32_t types; /* char * (32-bit pointer) */
2947};
2948
2949inline void swapStruct(struct cfstring64_t &cfs) {
2950 sys::swapByteOrder(cfs.isa);
2951 sys::swapByteOrder(cfs.flags);
2952 sys::swapByteOrder(cfs.characters);
2953 sys::swapByteOrder(cfs.length);
2954}
2955
2956inline void swapStruct(struct class64_t &c) {
2957 sys::swapByteOrder(c.isa);
2958 sys::swapByteOrder(c.superclass);
2959 sys::swapByteOrder(c.cache);
2960 sys::swapByteOrder(c.vtable);
2961 sys::swapByteOrder(c.data);
2962}
2963
2964inline void swapStruct(struct class32_t &c) {
2965 sys::swapByteOrder(c.isa);
2966 sys::swapByteOrder(c.superclass);
2967 sys::swapByteOrder(c.cache);
2968 sys::swapByteOrder(c.vtable);
2969 sys::swapByteOrder(c.data);
2970}
2971
2972inline void swapStruct(struct class_ro64_t &cro) {
2973 sys::swapByteOrder(cro.flags);
2974 sys::swapByteOrder(cro.instanceStart);
2975 sys::swapByteOrder(cro.instanceSize);
2976 sys::swapByteOrder(cro.reserved);
2977 sys::swapByteOrder(cro.ivarLayout);
2978 sys::swapByteOrder(cro.name);
2979 sys::swapByteOrder(cro.baseMethods);
2980 sys::swapByteOrder(cro.baseProtocols);
2981 sys::swapByteOrder(cro.ivars);
2982 sys::swapByteOrder(cro.weakIvarLayout);
2983 sys::swapByteOrder(cro.baseProperties);
2984}
2985
2986inline void swapStruct(struct class_ro32_t &cro) {
2987 sys::swapByteOrder(cro.flags);
2988 sys::swapByteOrder(cro.instanceStart);
2989 sys::swapByteOrder(cro.instanceSize);
2990 sys::swapByteOrder(cro.ivarLayout);
2991 sys::swapByteOrder(cro.name);
2992 sys::swapByteOrder(cro.baseMethods);
2993 sys::swapByteOrder(cro.baseProtocols);
2994 sys::swapByteOrder(cro.ivars);
2995 sys::swapByteOrder(cro.weakIvarLayout);
2996 sys::swapByteOrder(cro.baseProperties);
2997}
2998
2999inline void swapStruct(struct method_list64_t &ml) {
3000 sys::swapByteOrder(ml.entsize);
3001 sys::swapByteOrder(ml.count);
3002}
3003
3004inline void swapStruct(struct method_list32_t &ml) {
3005 sys::swapByteOrder(ml.entsize);
3006 sys::swapByteOrder(ml.count);
3007}
3008
3009inline void swapStruct(struct method64_t &m) {
3010 sys::swapByteOrder(m.name);
3011 sys::swapByteOrder(m.types);
3012 sys::swapByteOrder(m.imp);
3013}
3014
3015inline void swapStruct(struct method32_t &m) {
3016 sys::swapByteOrder(m.name);
3017 sys::swapByteOrder(m.types);
3018 sys::swapByteOrder(m.imp);
3019}
3020
3021inline void swapStruct(struct protocol_list64_t &pl) {
3022 sys::swapByteOrder(pl.count);
3023}
3024
3025inline void swapStruct(struct protocol_list32_t &pl) {
3026 sys::swapByteOrder(pl.count);
3027}
3028
3029inline void swapStruct(struct protocol64_t &p) {
3030 sys::swapByteOrder(p.isa);
3031 sys::swapByteOrder(p.name);
3032 sys::swapByteOrder(p.protocols);
3033 sys::swapByteOrder(p.instanceMethods);
3034 sys::swapByteOrder(p.classMethods);
3035 sys::swapByteOrder(p.optionalInstanceMethods);
3036 sys::swapByteOrder(p.optionalClassMethods);
3037 sys::swapByteOrder(p.instanceProperties);
3038}
3039
3040inline void swapStruct(struct protocol32_t &p) {
3041 sys::swapByteOrder(p.isa);
3042 sys::swapByteOrder(p.name);
3043 sys::swapByteOrder(p.protocols);
3044 sys::swapByteOrder(p.instanceMethods);
3045 sys::swapByteOrder(p.classMethods);
3046 sys::swapByteOrder(p.optionalInstanceMethods);
3047 sys::swapByteOrder(p.optionalClassMethods);
3048 sys::swapByteOrder(p.instanceProperties);
3049}
3050
3051inline void swapStruct(struct ivar_list64_t &il) {
3052 sys::swapByteOrder(il.entsize);
3053 sys::swapByteOrder(il.count);
3054}
3055
3056inline void swapStruct(struct ivar_list32_t &il) {
3057 sys::swapByteOrder(il.entsize);
3058 sys::swapByteOrder(il.count);
3059}
3060
3061inline void swapStruct(struct ivar64_t &i) {
3062 sys::swapByteOrder(i.offset);
3063 sys::swapByteOrder(i.name);
3064 sys::swapByteOrder(i.type);
3065 sys::swapByteOrder(i.alignment);
3066 sys::swapByteOrder(i.size);
3067}
3068
3069inline void swapStruct(struct ivar32_t &i) {
3070 sys::swapByteOrder(i.offset);
3071 sys::swapByteOrder(i.name);
3072 sys::swapByteOrder(i.type);
3073 sys::swapByteOrder(i.alignment);
3074 sys::swapByteOrder(i.size);
3075}
3076
3077inline void swapStruct(struct objc_property_list64 &pl) {
3078 sys::swapByteOrder(pl.entsize);
3079 sys::swapByteOrder(pl.count);
3080}
3081
3082inline void swapStruct(struct objc_property_list32 &pl) {
3083 sys::swapByteOrder(pl.entsize);
3084 sys::swapByteOrder(pl.count);
3085}
3086
3087inline void swapStruct(struct objc_property64 &op) {
3088 sys::swapByteOrder(op.name);
3089 sys::swapByteOrder(op.attributes);
3090}
3091
3092inline void swapStruct(struct objc_property32 &op) {
3093 sys::swapByteOrder(op.name);
3094 sys::swapByteOrder(op.attributes);
3095}
3096
3097inline void swapStruct(struct category64_t &c) {
3098 sys::swapByteOrder(c.name);
3099 sys::swapByteOrder(c.cls);
3100 sys::swapByteOrder(c.instanceMethods);
3101 sys::swapByteOrder(c.classMethods);
3102 sys::swapByteOrder(c.protocols);
3103 sys::swapByteOrder(c.instanceProperties);
3104}
3105
3106inline void swapStruct(struct category32_t &c) {
3107 sys::swapByteOrder(c.name);
3108 sys::swapByteOrder(c.cls);
3109 sys::swapByteOrder(c.instanceMethods);
3110 sys::swapByteOrder(c.classMethods);
3111 sys::swapByteOrder(c.protocols);
3112 sys::swapByteOrder(c.instanceProperties);
3113}
3114
3115inline void swapStruct(struct objc_image_info64 &o) {
3116 sys::swapByteOrder(o.version);
3117 sys::swapByteOrder(o.flags);
3118}
3119
3120inline void swapStruct(struct objc_image_info32 &o) {
3121 sys::swapByteOrder(o.version);
3122 sys::swapByteOrder(o.flags);
3123}
3124
3125inline void swapStruct(struct imageInfo_t &o) {
3126 sys::swapByteOrder(o.version);
3127 sys::swapByteOrder(o.flags);
3128}
3129
3130inline void swapStruct(struct message_ref64 &mr) {
3131 sys::swapByteOrder(mr.imp);
3132 sys::swapByteOrder(mr.sel);
3133}
3134
3135inline void swapStruct(struct message_ref32 &mr) {
3136 sys::swapByteOrder(mr.imp);
3137 sys::swapByteOrder(mr.sel);
3138}
3139
3140inline void swapStruct(struct objc_module_t &module) {
3141 sys::swapByteOrder(module.version);
3142 sys::swapByteOrder(module.size);
3143 sys::swapByteOrder(module.name);
3144 sys::swapByteOrder(module.symtab);
3145}
3146
3147inline void swapStruct(struct objc_symtab_t &symtab) {
3148 sys::swapByteOrder(symtab.sel_ref_cnt);
3149 sys::swapByteOrder(symtab.refs);
3150 sys::swapByteOrder(symtab.cls_def_cnt);
3151 sys::swapByteOrder(symtab.cat_def_cnt);
3152}
3153
3154inline void swapStruct(struct objc_class_t &objc_class) {
3155 sys::swapByteOrder(objc_class.isa);
3156 sys::swapByteOrder(objc_class.super_class);
3157 sys::swapByteOrder(objc_class.name);
3158 sys::swapByteOrder(objc_class.version);
3159 sys::swapByteOrder(objc_class.info);
3160 sys::swapByteOrder(objc_class.instance_size);
3161 sys::swapByteOrder(objc_class.ivars);
3162 sys::swapByteOrder(objc_class.methodLists);
3163 sys::swapByteOrder(objc_class.cache);
3164 sys::swapByteOrder(objc_class.protocols);
3165}
3166
3167inline void swapStruct(struct objc_category_t &objc_category) {
3168 sys::swapByteOrder(objc_category.category_name);
3169 sys::swapByteOrder(objc_category.class_name);
3170 sys::swapByteOrder(objc_category.instance_methods);
3171 sys::swapByteOrder(objc_category.class_methods);
3172 sys::swapByteOrder(objc_category.protocols);
3173}
3174
3175inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3176 sys::swapByteOrder(objc_ivar_list.ivar_count);
3177}
3178
3179inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3180 sys::swapByteOrder(objc_ivar.ivar_name);
3181 sys::swapByteOrder(objc_ivar.ivar_type);
3182 sys::swapByteOrder(objc_ivar.ivar_offset);
3183}
3184
3185inline void swapStruct(struct objc_method_list_t &method_list) {
3186 sys::swapByteOrder(method_list.obsolete);
3187 sys::swapByteOrder(method_list.method_count);
3188}
3189
3190inline void swapStruct(struct objc_method_t &method) {
3191 sys::swapByteOrder(method.method_name);
3192 sys::swapByteOrder(method.method_types);
3193 sys::swapByteOrder(method.method_imp);
3194}
3195
3196inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3197 sys::swapByteOrder(protocol_list.next);
3198 sys::swapByteOrder(protocol_list.count);
3199}
3200
3201inline void swapStruct(struct objc_protocol_t &protocol) {
3202 sys::swapByteOrder(protocol.isa);
3203 sys::swapByteOrder(protocol.protocol_name);
3204 sys::swapByteOrder(protocol.protocol_list);
3205 sys::swapByteOrder(protocol.instance_methods);
3206 sys::swapByteOrder(protocol.class_methods);
3207}
3208
3209inline void swapStruct(struct objc_method_description_list_t &mdl) {
3210 sys::swapByteOrder(mdl.count);
3211}
3212
3213inline void swapStruct(struct objc_method_description_t &md) {
3214 sys::swapByteOrder(md.name);
3215 sys::swapByteOrder(md.types);
3216}
3217
3218static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3219 struct DisassembleInfo *info);
3220
3221// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3222// to an Objective-C class and returns the class name. It is also passed the
3223// address of the pointer, so when the pointer is zero as it can be in an .o
3224// file, that is used to look for an external relocation entry with a symbol
3225// name.
3226static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3227 uint64_t ReferenceValue,
3228 struct DisassembleInfo *info) {
3229 const char *r;
3230 uint32_t offset, left;
3231 SectionRef S;
3232
3233 // The pointer_value can be 0 in an object file and have a relocation
3234 // entry for the class symbol at the ReferenceValue (the address of the
3235 // pointer).
3236 if (pointer_value == 0) {
3237 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3238 if (r == nullptr || left < sizeof(uint64_t))
3239 return nullptr;
3240 uint64_t n_value;
3241 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3242 if (symbol_name == nullptr)
3243 return nullptr;
3244 const char *class_name = strrchr(symbol_name, '$');
3245 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3246 return class_name + 2;
3247 else
3248 return nullptr;
3249 }
3250
3251 // The case were the pointer_value is non-zero and points to a class defined
3252 // in this Mach-O file.
3253 r = get_pointer_64(pointer_value, offset, left, S, info);
3254 if (r == nullptr || left < sizeof(struct class64_t))
3255 return nullptr;
3256 struct class64_t c;
3257 memcpy(&c, r, sizeof(struct class64_t));
3258 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3259 swapStruct(c);
3260 if (c.data == 0)
3261 return nullptr;
3262 r = get_pointer_64(c.data, offset, left, S, info);
3263 if (r == nullptr || left < sizeof(struct class_ro64_t))
3264 return nullptr;
3265 struct class_ro64_t cro;
3266 memcpy(&cro, r, sizeof(struct class_ro64_t));
3267 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3268 swapStruct(cro);
3269 if (cro.name == 0)
3270 return nullptr;
3271 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3272 return name;
3273}
3274
3275// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3276// pointer to a cfstring and returns its name or nullptr.
3277static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3278 struct DisassembleInfo *info) {
3279 const char *r, *name;
3280 uint32_t offset, left;
3281 SectionRef S;
3282 struct cfstring64_t cfs;
3283 uint64_t cfs_characters;
3284
3285 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3286 if (r == nullptr || left < sizeof(struct cfstring64_t))
3287 return nullptr;
3288 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3289 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3290 swapStruct(cfs);
3291 if (cfs.characters == 0) {
3292 uint64_t n_value;
3293 const char *symbol_name = get_symbol_64(
3294 offset + offsetof(struct cfstring64_t, characters)__builtin_offsetof(struct cfstring64_t, characters), S, info, n_value);
3295 if (symbol_name == nullptr)
3296 return nullptr;
3297 cfs_characters = n_value;
3298 } else
3299 cfs_characters = cfs.characters;
3300 name = get_pointer_64(cfs_characters, offset, left, S, info);
3301
3302 return name;
3303}
3304
3305// get_objc2_64bit_selref() is used for disassembly and is passed a the address
3306// of a pointer to an Objective-C selector reference when the pointer value is
3307// zero as in a .o file and is likely to have a external relocation entry with
3308// who's symbol's n_value is the real pointer to the selector name. If that is
3309// the case the real pointer to the selector name is returned else 0 is
3310// returned
3311static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3312 struct DisassembleInfo *info) {
3313 uint32_t offset, left;
3314 SectionRef S;
3315
3316 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3317 if (r == nullptr || left < sizeof(uint64_t))
3318 return 0;
3319 uint64_t n_value;
3320 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3321 if (symbol_name == nullptr)
3322 return 0;
3323 return n_value;
3324}
3325
3326static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3327 const char *sectname) {
3328 for (const SectionRef &Section : O->sections()) {
3329 StringRef SectName;
3330 Section.getName(SectName);
3331 DataRefImpl Ref = Section.getRawDataRefImpl();
3332 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3333 if (SegName == segname && SectName == sectname)
3334 return Section;
3335 }
3336 return SectionRef();
3337}
3338
3339static void
3340walk_pointer_list_64(const char *listname, const SectionRef S,
3341 MachOObjectFile *O, struct DisassembleInfo *info,
3342 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3343 if (S == SectionRef())
3344 return;
3345
3346 StringRef SectName;
3347 S.getName(SectName);
3348 DataRefImpl Ref = S.getRawDataRefImpl();
3349 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3350 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3351
3352 StringRef BytesStr;
3353 S.getContents(BytesStr);
3354 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3355
3356 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3357 uint32_t left = S.getSize() - i;
3358 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3359 uint64_t p = 0;
3360 memcpy(&p, Contents + i, size);
3361 if (i + sizeof(uint64_t) > S.getSize())
3362 outs() << listname << " list pointer extends past end of (" << SegName
3363 << "," << SectName << ") section\n";
3364 outs() << format("%016" PRIx64"l" "x", S.getAddress() + i) << " ";
3365
3366 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3367 sys::swapByteOrder(p);
3368
3369 uint64_t n_value = 0;
3370 const char *name = get_symbol_64(i, S, info, n_value, p);
3371 if (name == nullptr)
3372 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3373
3374 if (n_value != 0) {
3375 outs() << format("0x%" PRIx64"l" "x", n_value);
3376 if (p != 0)
3377 outs() << " + " << format("0x%" PRIx64"l" "x", p);
3378 } else
3379 outs() << format("0x%" PRIx64"l" "x", p);
3380 if (name != nullptr)
3381 outs() << " " << name;
3382 outs() << "\n";
3383
3384 p += n_value;
3385 if (func)
3386 func(p, info);
3387 }
3388}
3389
3390static void
3391walk_pointer_list_32(const char *listname, const SectionRef S,
3392 MachOObjectFile *O, struct DisassembleInfo *info,
3393 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3394 if (S == SectionRef())
3395 return;
3396
3397 StringRef SectName;
3398 S.getName(SectName);
3399 DataRefImpl Ref = S.getRawDataRefImpl();
3400 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3401 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3402
3403 StringRef BytesStr;
3404 S.getContents(BytesStr);
3405 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3406
3407 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3408 uint32_t left = S.getSize() - i;
3409 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3410 uint32_t p = 0;
3411 memcpy(&p, Contents + i, size);
3412 if (i + sizeof(uint32_t) > S.getSize())
3413 outs() << listname << " list pointer extends past end of (" << SegName
3414 << "," << SectName << ") section\n";
3415 uint32_t Address = S.getAddress() + i;
3416 outs() << format("%08" PRIx32"x", Address) << " ";
3417
3418 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3419 sys::swapByteOrder(p);
3420 outs() << format("0x%" PRIx32"x", p);
3421
3422 const char *name = get_symbol_32(i, S, info, p);
3423 if (name != nullptr)
3424 outs() << " " << name;
3425 outs() << "\n";
3426
3427 if (func)
3428 func(p, info);
3429 }
3430}
3431
3432static void print_layout_map(const char *layout_map, uint32_t left) {
3433 if (layout_map == nullptr)
3434 return;
3435 outs() << " layout map: ";
3436 do {
3437 outs() << format("0x%02" PRIx32"x", (*layout_map) & 0xff) << " ";
3438 left--;
3439 layout_map++;
3440 } while (*layout_map != '\0' && left != 0);
3441 outs() << "\n";
3442}
3443
3444static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3445 uint32_t offset, left;
3446 SectionRef S;
3447 const char *layout_map;
3448
3449 if (p == 0)
3450 return;
3451 layout_map = get_pointer_64(p, offset, left, S, info);
3452 print_layout_map(layout_map, left);
3453}
3454
3455static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3456 uint32_t offset, left;
3457 SectionRef S;
3458 const char *layout_map;
3459
3460 if (p == 0)
3461 return;
3462 layout_map = get_pointer_32(p, offset, left, S, info);
3463 print_layout_map(layout_map, left);
3464}
3465
3466static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3467 const char *indent) {
3468 struct method_list64_t ml;
3469 struct method64_t m;
3470 const char *r;
3471 uint32_t offset, xoffset, left, i;
3472 SectionRef S, xS;
3473 const char *name, *sym_name;
3474 uint64_t n_value;
3475
3476 r = get_pointer_64(p, offset, left, S, info);
3477 if (r == nullptr)
3478 return;
3479 memset(&ml, '\0', sizeof(struct method_list64_t));
3480 if (left < sizeof(struct method_list64_t)) {
3481 memcpy(&ml, r, left);
3482 outs() << " (method_list_t entends past the end of the section)\n";
3483 } else
3484 memcpy(&ml, r, sizeof(struct method_list64_t));
3485 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3486 swapStruct(ml);
3487 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3488 outs() << indent << "\t\t count " << ml.count << "\n";
3489
3490 p += sizeof(struct method_list64_t);
3491 offset += sizeof(struct method_list64_t);
3492 for (i = 0; i < ml.count; i++) {
3493 r = get_pointer_64(p, offset, left, S, info);
3494 if (r == nullptr)
3495 return;
3496 memset(&m, '\0', sizeof(struct method64_t));
3497 if (left < sizeof(struct method64_t)) {
3498 memcpy(&m, r, left);
3499 outs() << indent << " (method_t extends past the end of the section)\n";
3500 } else
3501 memcpy(&m, r, sizeof(struct method64_t));
3502 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3503 swapStruct(m);
3504
3505 outs() << indent << "\t\t name ";
3506 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name)__builtin_offsetof(struct method64_t, name), S,
3507 info, n_value, m.name);
3508 if (n_value != 0) {
3509 if (info->verbose && sym_name != nullptr)
3510 outs() << sym_name;
3511 else
3512 outs() << format("0x%" PRIx64"l" "x", n_value);
3513 if (m.name != 0)
3514 outs() << " + " << format("0x%" PRIx64"l" "x", m.name);
3515 } else
3516 outs() << format("0x%" PRIx64"l" "x", m.name);
3517 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3518 if (name != nullptr)
3519 outs() << format(" %.*s", left, name);
3520 outs() << "\n";
3521
3522 outs() << indent << "\t\t types ";
3523 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types)__builtin_offsetof(struct method64_t, types), S,
3524 info, n_value, m.types);
3525 if (n_value != 0) {
3526 if (info->verbose && sym_name != nullptr)
3527 outs() << sym_name;
3528 else
3529 outs() << format("0x%" PRIx64"l" "x", n_value);
3530 if (m.types != 0)
3531 outs() << " + " << format("0x%" PRIx64"l" "x", m.types);
3532 } else
3533 outs() << format("0x%" PRIx64"l" "x", m.types);
3534 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3535 if (name != nullptr)
3536 outs() << format(" %.*s", left, name);
3537 outs() << "\n";
3538
3539 outs() << indent << "\t\t imp ";
3540 name = get_symbol_64(offset + offsetof(struct method64_t, imp)__builtin_offsetof(struct method64_t, imp), S, info,
3541 n_value, m.imp);
3542 if (info->verbose && name == nullptr) {
3543 if (n_value != 0) {
3544 outs() << format("0x%" PRIx64"l" "x", n_value) << " ";
3545 if (m.imp != 0)
3546 outs() << "+ " << format("0x%" PRIx64"l" "x", m.imp) << " ";
3547 } else
3548 outs() << format("0x%" PRIx64"l" "x", m.imp) << " ";
3549 }
3550 if (name != nullptr)
3551 outs() << name;
3552 outs() << "\n";
3553
3554 p += sizeof(struct method64_t);
3555 offset += sizeof(struct method64_t);
3556 }
3557}
3558
3559static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3560 const char *indent) {
3561 struct method_list32_t ml;
3562 struct method32_t m;
3563 const char *r, *name;
3564 uint32_t offset, xoffset, left, i;
3565 SectionRef S, xS;
3566
3567 r = get_pointer_32(p, offset, left, S, info);
3568 if (r == nullptr)
3569 return;
3570 memset(&ml, '\0', sizeof(struct method_list32_t));
3571 if (left < sizeof(struct method_list32_t)) {
3572 memcpy(&ml, r, left);
3573 outs() << " (method_list_t entends past the end of the section)\n";
3574 } else
3575 memcpy(&ml, r, sizeof(struct method_list32_t));
3576 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3577 swapStruct(ml);
3578 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3579 outs() << indent << "\t\t count " << ml.count << "\n";
3580
3581 p += sizeof(struct method_list32_t);
3582 offset += sizeof(struct method_list32_t);
3583 for (i = 0; i < ml.count; i++) {
3584 r = get_pointer_32(p, offset, left, S, info);
3585 if (r == nullptr)
3586 return;
3587 memset(&m, '\0', sizeof(struct method32_t));
3588 if (left < sizeof(struct method32_t)) {
3589 memcpy(&ml, r, left);
3590 outs() << indent << " (method_t entends past the end of the section)\n";
3591 } else
3592 memcpy(&m, r, sizeof(struct method32_t));
3593 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3594 swapStruct(m);
3595
3596 outs() << indent << "\t\t name " << format("0x%" PRIx32"x", m.name);
3597 name = get_pointer_32(m.name, xoffset, left, xS, info);
3598 if (name != nullptr)
3599 outs() << format(" %.*s", left, name);
3600 outs() << "\n";
3601
3602 outs() << indent << "\t\t types " << format("0x%" PRIx32"x", m.types);
3603 name = get_pointer_32(m.types, xoffset, left, xS, info);
3604 if (name != nullptr)
3605 outs() << format(" %.*s", left, name);
3606 outs() << "\n";
3607
3608 outs() << indent << "\t\t imp " << format("0x%" PRIx32"x", m.imp);
3609 name = get_symbol_32(offset + offsetof(struct method32_t, imp)__builtin_offsetof(struct method32_t, imp), S, info,
3610 m.imp);
3611 if (name != nullptr)
3612 outs() << " " << name;
3613 outs() << "\n";
3614
3615 p += sizeof(struct method32_t);
3616 offset += sizeof(struct method32_t);
3617 }
3618}
3619
3620static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3621 uint32_t offset, left, xleft;
3622 SectionRef S;
3623 struct objc_method_list_t method_list;
3624 struct objc_method_t method;
3625 const char *r, *methods, *name, *SymbolName;
3626 int32_t i;
3627
3628 r = get_pointer_32(p, offset, left, S, info, true);
3629 if (r == nullptr)
3630 return true;
3631
3632 outs() << "\n";
3633 if (left > sizeof(struct objc_method_list_t)) {
3634 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3635 } else {
3636 outs() << "\t\t objc_method_list extends past end of the section\n";
3637 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3638 memcpy(&method_list, r, left);
3639 }
3640 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3641 swapStruct(method_list);
3642
3643 outs() << "\t\t obsolete "
3644 << format("0x%08" PRIx32"x", method_list.obsolete) << "\n";
3645 outs() << "\t\t method_count " << method_list.method_count << "\n";
3646
3647 methods = r + sizeof(struct objc_method_list_t);
3648 for (i = 0; i < method_list.method_count; i++) {
3649 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3650 outs() << "\t\t remaining method's extend past the of the section\n";
3651 break;
3652 }
3653 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3654 sizeof(struct objc_method_t));
3655 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3656 swapStruct(method);
3657
3658 outs() << "\t\t method_name "
3659 << format("0x%08" PRIx32"x", method.method_name);
3660 if (info->verbose) {
3661 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3662 if (name != nullptr)
3663 outs() << format(" %.*s", xleft, name);
3664 else
3665 outs() << " (not in an __OBJC section)";
3666 }
3667 outs() << "\n";
3668
3669 outs() << "\t\t method_types "
3670 << format("0x%08" PRIx32"x", method.method_types);
3671 if (info->verbose) {
3672 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3673 if (name != nullptr)
3674 outs() << format(" %.*s", xleft, name);
3675 else
3676 outs() << " (not in an __OBJC section)";
3677 }
3678 outs() << "\n";
3679
3680 outs() << "\t\t method_imp "
3681 << format("0x%08" PRIx32"x", method.method_imp) << " ";
3682 if (info->verbose) {
3683 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3684 if (SymbolName != nullptr)
3685 outs() << SymbolName;
3686 }
3687 outs() << "\n";
3688 }
3689 return false;
3690}
3691
3692static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3693 struct protocol_list64_t pl;
3694 uint64_t q, n_value;
3695 struct protocol64_t pc;
3696 const char *r;
3697 uint32_t offset, xoffset, left, i;
3698 SectionRef S, xS;
3699 const char *name, *sym_name;
3700
3701 r = get_pointer_64(p, offset, left, S, info);
3702 if (r == nullptr)
3703 return;
3704 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3705 if (left < sizeof(struct protocol_list64_t)) {
3706 memcpy(&pl, r, left);
3707 outs() << " (protocol_list_t entends past the end of the section)\n";
3708 } else
3709 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3710 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3711 swapStruct(pl);
3712 outs() << " count " << pl.count << "\n";
3713
3714 p += sizeof(struct protocol_list64_t);
3715 offset += sizeof(struct protocol_list64_t);
3716 for (i = 0; i < pl.count; i++) {
3717 r = get_pointer_64(p, offset, left, S, info);
3718 if (r == nullptr)
3719 return;
3720 q = 0;
3721 if (left < sizeof(uint64_t)) {
3722 memcpy(&q, r, left);
3723 outs() << " (protocol_t * entends past the end of the section)\n";
3724 } else
3725 memcpy(&q, r, sizeof(uint64_t));
3726 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3727 sys::swapByteOrder(q);
3728
3729 outs() << "\t\t list[" << i << "] ";
3730 sym_name = get_symbol_64(offset, S, info, n_value, q);
3731 if (n_value != 0) {
3732 if (info->verbose && sym_name != nullptr)
3733 outs() << sym_name;
3734 else
3735 outs() << format("0x%" PRIx64"l" "x", n_value);
3736 if (q != 0)
3737 outs() << " + " << format("0x%" PRIx64"l" "x", q);
3738 } else
3739 outs() << format("0x%" PRIx64"l" "x", q);
3740 outs() << " (struct protocol_t *)\n";
3741
3742 r = get_pointer_64(q + n_value, offset, left, S, info);
3743 if (r == nullptr)
3744 return;
3745 memset(&pc, '\0', sizeof(struct protocol64_t));
3746 if (left < sizeof(struct protocol64_t)) {
3747 memcpy(&pc, r, left);
3748 outs() << " (protocol_t entends past the end of the section)\n";
3749 } else
3750 memcpy(&pc, r, sizeof(struct protocol64_t));
3751 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3752 swapStruct(pc);
3753
3754 outs() << "\t\t\t isa " << format("0x%" PRIx64"l" "x", pc.isa) << "\n";
3755
3756 outs() << "\t\t\t name ";
3757 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name)__builtin_offsetof(struct protocol64_t, name), S,
3758 info, n_value, pc.name);
3759 if (n_value != 0) {
3760 if (info->verbose && sym_name != nullptr)
3761 outs() << sym_name;
3762 else
3763 outs() << format("0x%" PRIx64"l" "x", n_value);
3764 if (pc.name != 0)
3765 outs() << " + " << format("0x%" PRIx64"l" "x", pc.name);
3766 } else
3767 outs() << format("0x%" PRIx64"l" "x", pc.name);
3768 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3769 if (name != nullptr)
3770 outs() << format(" %.*s", left, name);
3771 outs() << "\n";
3772
3773 outs() << "\t\t\tprotocols " << format("0x%" PRIx64"l" "x", pc.protocols) << "\n";
3774
3775 outs() << "\t\t instanceMethods ";
3776 sym_name =
3777 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods)__builtin_offsetof(struct protocol64_t, instanceMethods),
3778 S, info, n_value, pc.instanceMethods);
3779 if (n_value != 0) {
3780 if (info->verbose && sym_name != nullptr)
3781 outs() << sym_name;
3782 else
3783 outs() << format("0x%" PRIx64"l" "x", n_value);
3784 if (pc.instanceMethods != 0)
3785 outs() << " + " << format("0x%" PRIx64"l" "x", pc.instanceMethods);
3786 } else
3787 outs() << format("0x%" PRIx64"l" "x", pc.instanceMethods);
3788 outs() << " (struct method_list_t *)\n";
3789 if (pc.instanceMethods + n_value != 0)
3790 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3791
3792 outs() << "\t\t classMethods ";
3793 sym_name =
3794 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods)__builtin_offsetof(struct protocol64_t, classMethods), S,
3795 info, n_value, pc.classMethods);
3796 if (n_value != 0) {
3797 if (info->verbose && sym_name != nullptr)
3798 outs() << sym_name;
3799 else
3800 outs() << format("0x%" PRIx64"l" "x", n_value);
3801 if (pc.classMethods != 0)
3802 outs() << " + " << format("0x%" PRIx64"l" "x", pc.classMethods);
3803 } else
3804 outs() << format("0x%" PRIx64"l" "x", pc.classMethods);
3805 outs() << " (struct method_list_t *)\n";
3806 if (pc.classMethods + n_value != 0)
3807 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3808
3809 outs() << "\t optionalInstanceMethods "
3810 << format("0x%" PRIx64"l" "x", pc.optionalInstanceMethods) << "\n";
3811 outs() << "\t optionalClassMethods "
3812 << format("0x%" PRIx64"l" "x", pc.optionalClassMethods) << "\n";
3813 outs() << "\t instanceProperties "
3814 << format("0x%" PRIx64"l" "x", pc.instanceProperties) << "\n";
3815
3816 p += sizeof(uint64_t);
3817 offset += sizeof(uint64_t);
3818 }
3819}
3820
3821static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3822 struct protocol_list32_t pl;
3823 uint32_t q;
3824 struct protocol32_t pc;
3825 const char *r;
3826 uint32_t offset, xoffset, left, i;
3827 SectionRef S, xS;
3828 const char *name;
3829
3830 r = get_pointer_32(p, offset, left, S, info);
3831 if (r == nullptr)
3832 return;
3833 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3834 if (left < sizeof(struct protocol_list32_t)) {
3835 memcpy(&pl, r, left);
3836 outs() << " (protocol_list_t entends past the end of the section)\n";
3837 } else
3838 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3839 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3840 swapStruct(pl);
3841 outs() << " count " << pl.count << "\n";
3842
3843 p += sizeof(struct protocol_list32_t);
3844 offset += sizeof(struct protocol_list32_t);
3845 for (i = 0; i < pl.count; i++) {
3846 r = get_pointer_32(p, offset, left, S, info);
3847 if (r == nullptr)
3848 return;
3849 q = 0;
3850 if (left < sizeof(uint32_t)) {
3851 memcpy(&q, r, left);
3852 outs() << " (protocol_t * entends past the end of the section)\n";
3853 } else
3854 memcpy(&q, r, sizeof(uint32_t));
3855 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3856 sys::swapByteOrder(q);
3857 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32"x", q)
3858 << " (struct protocol_t *)\n";
3859 r = get_pointer_32(q, offset, left, S, info);
3860 if (r == nullptr)
3861 return;
3862 memset(&pc, '\0', sizeof(struct protocol32_t));
3863 if (left < sizeof(struct protocol32_t)) {
3864 memcpy(&pc, r, left);
3865 outs() << " (protocol_t entends past the end of the section)\n";
3866 } else
3867 memcpy(&pc, r, sizeof(struct protocol32_t));
3868 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3869 swapStruct(pc);
3870 outs() << "\t\t\t isa " << format("0x%" PRIx32"x", pc.isa) << "\n";
3871 outs() << "\t\t\t name " << format("0x%" PRIx32"x", pc.name);
3872 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3873 if (name != nullptr)
3874 outs() << format(" %.*s", left, name);
3875 outs() << "\n";
3876 outs() << "\t\t\tprotocols " << format("0x%" PRIx32"x", pc.protocols) << "\n";
3877 outs() << "\t\t instanceMethods "
3878 << format("0x%" PRIx32"x", pc.instanceMethods)
3879 << " (struct method_list_t *)\n";
3880 if (pc.instanceMethods != 0)
3881 print_method_list32_t(pc.instanceMethods, info, "\t");
3882 outs() << "\t\t classMethods " << format("0x%" PRIx32"x", pc.classMethods)
3883 << " (struct method_list_t *)\n";
3884 if (pc.classMethods != 0)
3885 print_method_list32_t(pc.classMethods, info, "\t");
3886 outs() << "\t optionalInstanceMethods "
3887 << format("0x%" PRIx32"x", pc.optionalInstanceMethods) << "\n";
3888 outs() << "\t optionalClassMethods "
3889 << format("0x%" PRIx32"x", pc.optionalClassMethods) << "\n";
3890 outs() << "\t instanceProperties "
3891 << format("0x%" PRIx32"x", pc.instanceProperties) << "\n";
3892 p += sizeof(uint32_t);
3893 offset += sizeof(uint32_t);
3894 }
3895}
3896
3897static void print_indent(uint32_t indent) {
3898 for (uint32_t i = 0; i < indent;) {
3899 if (indent - i >= 8) {
3900 outs() << "\t";
3901 i += 8;
3902 } else {
3903 for (uint32_t j = i; j < indent; j++)
3904 outs() << " ";
3905 return;
3906 }
3907 }
3908}
3909
3910static bool print_method_description_list(uint32_t p, uint32_t indent,
3911 struct DisassembleInfo *info) {
3912 uint32_t offset, left, xleft;
3913 SectionRef S;
3914 struct objc_method_description_list_t mdl;
3915 struct objc_method_description_t md;
3916 const char *r, *list, *name;
3917 int32_t i;
3918
3919 r = get_pointer_32(p, offset, left, S, info, true);
3920 if (r == nullptr)
3921 return true;
3922
3923 outs() << "\n";
3924 if (left > sizeof(struct objc_method_description_list_t)) {
3925 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3926 } else {
3927 print_indent(indent);
3928 outs() << " objc_method_description_list extends past end of the section\n";
3929 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3930 memcpy(&mdl, r, left);
3931 }
3932 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3933 swapStruct(mdl);
3934
3935 print_indent(indent);
3936 outs() << " count " << mdl.count << "\n";
3937
3938 list = r + sizeof(struct objc_method_description_list_t);
3939 for (i = 0; i < mdl.count; i++) {
3940 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3941 print_indent(indent);
3942 outs() << " remaining list entries extend past the of the section\n";
3943 break;
3944 }
3945 print_indent(indent);
3946 outs() << " list[" << i << "]\n";
3947 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3948 sizeof(struct objc_method_description_t));
3949 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3950 swapStruct(md);
3951
3952 print_indent(indent);
3953 outs() << " name " << format("0x%08" PRIx32"x", md.name);
3954 if (info->verbose) {
3955 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3956 if (name != nullptr)
3957 outs() << format(" %.*s", xleft, name);
3958 else
3959 outs() << " (not in an __OBJC section)";
3960 }
3961 outs() << "\n";
3962
3963 print_indent(indent);
3964 outs() << " types " << format("0x%08" PRIx32"x", md.types);
3965 if (info->verbose) {
3966 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3967 if (name != nullptr)
3968 outs() << format(" %.*s", xleft, name);
3969 else
3970 outs() << " (not in an __OBJC section)";
3971 }
3972 outs() << "\n";
3973 }
3974 return false;
3975}
3976
3977static bool print_protocol_list(uint32_t p, uint32_t indent,
3978 struct DisassembleInfo *info);
3979
3980static bool print_protocol(uint32_t p, uint32_t indent,
3981 struct DisassembleInfo *info) {
3982 uint32_t offset, left;
3983 SectionRef S;
3984 struct objc_protocol_t protocol;
3985 const char *r, *name;
3986
3987 r = get_pointer_32(p, offset, left, S, info, true);
3988 if (r == nullptr)
3989 return true;
3990
3991 outs() << "\n";
3992 if (left >= sizeof(struct objc_protocol_t)) {
3993 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3994 } else {
3995 print_indent(indent);
3996 outs() << " Protocol extends past end of the section\n";
3997 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3998 memcpy(&protocol, r, left);
3999 }
4000 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4001 swapStruct(protocol);
4002
4003 print_indent(indent);
4004 outs() << " isa " << format("0x%08" PRIx32"x", protocol.isa)
4005 << "\n";
4006
4007 print_indent(indent);
4008 outs() << " protocol_name "
4009 << format("0x%08" PRIx32"x", protocol.protocol_name);
4010 if (info->verbose) {
4011 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
4012 if (name != nullptr)
4013 outs() << format(" %.*s", left, name);
4014 else
4015 outs() << " (not in an __OBJC section)";
4016 }
4017 outs() << "\n";
4018
4019 print_indent(indent);
4020 outs() << " protocol_list "
4021 << format("0x%08" PRIx32"x", protocol.protocol_list);
4022 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
4023 outs() << " (not in an __OBJC section)\n";
4024
4025 print_indent(indent);
4026 outs() << " instance_methods "
4027 << format("0x%08" PRIx32"x", protocol.instance_methods);
4028 if (print_method_description_list(protocol.instance_methods, indent, info))
4029 outs() << " (not in an __OBJC section)\n";
4030
4031 print_indent(indent);
4032 outs() << " class_methods "
4033 << format("0x%08" PRIx32"x", protocol.class_methods);
4034 if (print_method_description_list(protocol.class_methods, indent, info))
4035 outs() << " (not in an __OBJC section)\n";
4036
4037 return false;
4038}
4039
4040static bool print_protocol_list(uint32_t p, uint32_t indent,
4041 struct DisassembleInfo *info) {
4042 uint32_t offset, left, l;
4043 SectionRef S;
4044 struct objc_protocol_list_t protocol_list;
4045 const char *r, *list;
4046 int32_t i;
4047
4048 r = get_pointer_32(p, offset, left, S, info, true);
4049 if (r == nullptr)
4050 return true;
4051
4052 outs() << "\n";
4053 if (left > sizeof(struct objc_protocol_list_t)) {
4054 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
4055 } else {
4056 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4057 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
4058 memcpy(&protocol_list, r, left);
4059 }
4060 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4061 swapStruct(protocol_list);
4062
4063 print_indent(indent);
4064 outs() << " next " << format("0x%08" PRIx32"x", protocol_list.next)
4065 << "\n";
4066 print_indent(indent);
4067 outs() << " count " << protocol_list.count << "\n";
4068
4069 list = r + sizeof(struct objc_protocol_list_t);
4070 for (i = 0; i < protocol_list.count; i++) {
4071 if ((i + 1) * sizeof(uint32_t) > left) {
4072 outs() << "\t\t remaining list entries extend past the of the section\n";
4073 break;
4074 }
4075 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
4076 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4077 sys::swapByteOrder(l);
4078
4079 print_indent(indent);
4080 outs() << " list[" << i << "] " << format("0x%08" PRIx32"x", l);
4081 if (print_protocol(l, indent, info))
4082 outs() << "(not in an __OBJC section)\n";
4083 }
4084 return false;
4085}
4086
4087static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
4088 struct ivar_list64_t il;
4089 struct ivar64_t i;
4090 const char *r;
4091 uint32_t offset, xoffset, left, j;
4092 SectionRef S, xS;
4093 const char *name, *sym_name, *ivar_offset_p;
4094 uint64_t ivar_offset, n_value;
4095
4096 r = get_pointer_64(p, offset, left, S, info);
4097 if (r == nullptr)
4098 return;
4099 memset(&il, '\0', sizeof(struct ivar_list64_t));
4100 if (left < sizeof(struct ivar_list64_t)) {
4101 memcpy(&il, r, left);
4102 outs() << " (ivar_list_t entends past the end of the section)\n";
4103 } else
4104 memcpy(&il, r, sizeof(struct ivar_list64_t));
4105 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4106 swapStruct(il);
4107 outs() << " entsize " << il.entsize << "\n";
4108 outs() << " count " << il.count << "\n";
4109
4110 p += sizeof(struct ivar_list64_t);
4111 offset += sizeof(struct ivar_list64_t);
4112 for (j = 0; j < il.count; j++) {
4113 r = get_pointer_64(p, offset, left, S, info);
4114 if (r == nullptr)
4115 return;
4116 memset(&i, '\0', sizeof(struct ivar64_t));
4117 if (left < sizeof(struct ivar64_t)) {
4118 memcpy(&i, r, left);
4119 outs() << " (ivar_t entends past the end of the section)\n";
4120 } else
4121 memcpy(&i, r, sizeof(struct ivar64_t));
4122 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4123 swapStruct(i);
4124
4125 outs() << "\t\t\t offset ";
4126 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset)__builtin_offsetof(struct ivar64_t, offset), S,
4127 info, n_value, i.offset);
4128 if (n_value != 0) {
4129 if (info->verbose && sym_name != nullptr)
4130 outs() << sym_name;
4131 else
4132 outs() << format("0x%" PRIx64"l" "x", n_value);
4133 if (i.offset != 0)
4134 outs() << " + " << format("0x%" PRIx64"l" "x", i.offset);
4135 } else
4136 outs() << format("0x%" PRIx64"l" "x", i.offset);
4137 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4138 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4139 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4140 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4141 sys::swapByteOrder(ivar_offset);
4142 outs() << " " << ivar_offset << "\n";
4143 } else
4144 outs() << "\n";
4145
4146 outs() << "\t\t\t name ";
4147 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name)__builtin_offsetof(struct ivar64_t, name), S, info,
4148 n_value, i.name);
4149 if (n_value != 0) {
4150 if (info->verbose && sym_name != nullptr)
4151 outs() << sym_name;
4152 else
4153 outs() << format("0x%" PRIx64"l" "x", n_value);
4154 if (i.name != 0)
4155 outs() << " + " << format("0x%" PRIx64"l" "x", i.name);
4156 } else
4157 outs() << format("0x%" PRIx64"l" "x", i.name);
4158 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4159 if (name != nullptr)
4160 outs() << format(" %.*s", left, name);
4161 outs() << "\n";
4162
4163 outs() << "\t\t\t type ";
4164 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type)__builtin_offsetof(struct ivar64_t, type), S, info,
4165 n_value, i.name);
4166 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4167 if (n_value != 0) {
4168 if (info->verbose && sym_name != nullptr)
4169 outs() << sym_name;
4170 else
4171 outs() << format("0x%" PRIx64"l" "x", n_value);
4172 if (i.type != 0)
4173 outs() << " + " << format("0x%" PRIx64"l" "x", i.type);
4174 } else
4175 outs() << format("0x%" PRIx64"l" "x", i.type);
4176 if (name != nullptr)
4177 outs() << format(" %.*s", left, name);
4178 outs() << "\n";
4179
4180 outs() << "\t\t\talignment " << i.alignment << "\n";
4181 outs() << "\t\t\t size " << i.size << "\n";
4182
4183 p += sizeof(struct ivar64_t);
4184 offset += sizeof(struct ivar64_t);
4185 }
4186}
4187
4188static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4189 struct ivar_list32_t il;
4190 struct ivar32_t i;
4191 const char *r;
4192 uint32_t offset, xoffset, left, j;
4193 SectionRef S, xS;
4194 const char *name, *ivar_offset_p;
4195 uint32_t ivar_offset;
4196
4197 r = get_pointer_32(p, offset, left, S, info);
4198 if (r == nullptr)
4199 return;
4200 memset(&il, '\0', sizeof(struct ivar_list32_t));
4201 if (left < sizeof(struct ivar_list32_t)) {
4202 memcpy(&il, r, left);
4203 outs() << " (ivar_list_t entends past the end of the section)\n";
4204 } else
4205 memcpy(&il, r, sizeof(struct ivar_list32_t));
4206 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4207 swapStruct(il);
4208 outs() << " entsize " << il.entsize << "\n";
4209 outs() << " count " << il.count << "\n";
4210
4211 p += sizeof(struct ivar_list32_t);
4212 offset += sizeof(struct ivar_list32_t);
4213 for (j = 0; j < il.count; j++) {
4214 r = get_pointer_32(p, offset, left, S, info);
4215 if (r == nullptr)
4216 return;
4217 memset(&i, '\0', sizeof(struct ivar32_t));
4218 if (left < sizeof(struct ivar32_t)) {
4219 memcpy(&i, r, left);
4220 outs() << " (ivar_t entends past the end of the section)\n";
4221 } else
4222 memcpy(&i, r, sizeof(struct ivar32_t));
4223 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4224 swapStruct(i);
4225
4226 outs() << "\t\t\t offset " << format("0x%" PRIx32"x", i.offset);
4227 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4228 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4229 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4230 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4231 sys::swapByteOrder(ivar_offset);
4232 outs() << " " << ivar_offset << "\n";
4233 } else
4234 outs() << "\n";
4235
4236 outs() << "\t\t\t name " << format("0x%" PRIx32"x", i.name);
4237 name = get_pointer_32(i.name, xoffset, left, xS, info);
4238 if (name != nullptr)
4239 outs() << format(" %.*s", left, name);
4240 outs() << "\n";
4241
4242 outs() << "\t\t\t type " << format("0x%" PRIx32"x", i.type);
4243 name = get_pointer_32(i.type, xoffset, left, xS, info);
4244 if (name != nullptr)
4245 outs() << format(" %.*s", left, name);
4246 outs() << "\n";
4247
4248 outs() << "\t\t\talignment " << i.alignment << "\n";
4249 outs() << "\t\t\t size " << i.size << "\n";
4250
4251 p += sizeof(struct ivar32_t);
4252 offset += sizeof(struct ivar32_t);
4253 }
4254}
4255
4256static void print_objc_property_list64(uint64_t p,
4257 struct DisassembleInfo *info) {
4258 struct objc_property_list64 opl;
4259 struct objc_property64 op;
4260 const char *r;
4261 uint32_t offset, xoffset, left, j;
4262 SectionRef S, xS;
4263 const char *name, *sym_name;
4264 uint64_t n_value;
4265
4266 r = get_pointer_64(p, offset, left, S, info);
4267 if (r == nullptr)
4268 return;
4269 memset(&opl, '\0', sizeof(struct objc_property_list64));
4270 if (left < sizeof(struct objc_property_list64)) {
4271 memcpy(&opl, r, left);
4272 outs() << " (objc_property_list entends past the end of the section)\n";
4273 } else
4274 memcpy(&opl, r, sizeof(struct objc_property_list64));
4275 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4276 swapStruct(opl);
4277 outs() << " entsize " << opl.entsize << "\n";
4278 outs() << " count " << opl.count << "\n";
4279
4280 p += sizeof(struct objc_property_list64);
4281 offset += sizeof(struct objc_property_list64);
4282 for (j = 0; j < opl.count; j++) {
4283 r = get_pointer_64(p, offset, left, S, info);
4284 if (r == nullptr)
4285 return;
4286 memset(&op, '\0', sizeof(struct objc_property64));
4287 if (left < sizeof(struct objc_property64)) {
4288 memcpy(&op, r, left);
4289 outs() << " (objc_property entends past the end of the section)\n";
4290 } else
4291 memcpy(&op, r, sizeof(struct objc_property64));
4292 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4293 swapStruct(op);
4294
4295 outs() << "\t\t\t name ";
4296 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name)__builtin_offsetof(struct objc_property64, name), S,
4297 info, n_value, op.name);
4298 if (n_value != 0) {
4299 if (info->verbose && sym_name != nullptr)
4300 outs() << sym_name;
4301 else
4302 outs() << format("0x%" PRIx64"l" "x", n_value);
4303 if (op.name != 0)
4304 outs() << " + " << format("0x%" PRIx64"l" "x", op.name);
4305 } else
4306 outs() << format("0x%" PRIx64"l" "x", op.name);
4307 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4308 if (name != nullptr)
4309 outs() << format(" %.*s", left, name);
4310 outs() << "\n";
4311
4312 outs() << "\t\t\tattributes ";
4313 sym_name =
4314 get_symbol_64(offset + offsetof(struct objc_property64, attributes)__builtin_offsetof(struct objc_property64, attributes), S,
4315 info, n_value, op.attributes);
4316 if (n_value != 0) {
4317 if (info->verbose && sym_name != nullptr)
4318 outs() << sym_name;
4319 else
4320 outs() << format("0x%" PRIx64"l" "x", n_value);
4321 if (op.attributes != 0)
4322 outs() << " + " << format("0x%" PRIx64"l" "x", op.attributes);
4323 } else
4324 outs() << format("0x%" PRIx64"l" "x", op.attributes);
4325 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4326 if (name != nullptr)
4327 outs() << format(" %.*s", left, name);
4328 outs() << "\n";
4329
4330 p += sizeof(struct objc_property64);
4331 offset += sizeof(struct objc_property64);
4332 }
4333}
4334
4335static void print_objc_property_list32(uint32_t p,
4336 struct DisassembleInfo *info) {
4337 struct objc_property_list32 opl;
4338 struct objc_property32 op;
4339 const char *r;
4340 uint32_t offset, xoffset, left, j;
4341 SectionRef S, xS;
4342 const char *name;
4343
4344 r = get_pointer_32(p, offset, left, S, info);
4345 if (r == nullptr)
4346 return;
4347 memset(&opl, '\0', sizeof(struct objc_property_list32));
4348 if (left < sizeof(struct objc_property_list32)) {
4349 memcpy(&opl, r, left);
4350 outs() << " (objc_property_list entends past the end of the section)\n";
4351 } else
4352 memcpy(&opl, r, sizeof(struct objc_property_list32));
4353 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4354 swapStruct(opl);
4355 outs() << " entsize " << opl.entsize << "\n";
4356 outs() << " count " << opl.count << "\n";
4357
4358 p += sizeof(struct objc_property_list32);
4359 offset += sizeof(struct objc_property_list32);
4360 for (j = 0; j < opl.count; j++) {
4361 r = get_pointer_32(p, offset, left, S, info);
4362 if (r == nullptr)
4363 return;
4364 memset(&op, '\0', sizeof(struct objc_property32));
4365 if (left < sizeof(struct objc_property32)) {
4366 memcpy(&op, r, left);
4367 outs() << " (objc_property entends past the end of the section)\n";
4368 } else
4369 memcpy(&op, r, sizeof(struct objc_property32));
4370 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4371 swapStruct(op);
4372
4373 outs() << "\t\t\t name " << format("0x%" PRIx32"x", op.name);
4374 name = get_pointer_32(op.name, xoffset, left, xS, info);
4375 if (name != nullptr)
4376 outs() << format(" %.*s", left, name);
4377 outs() << "\n";
4378
4379 outs() << "\t\t\tattributes " << format("0x%" PRIx32"x", op.attributes);
4380 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4381 if (name != nullptr)
4382 outs() << format(" %.*s", left, name);
4383 outs() << "\n";
4384
4385 p += sizeof(struct objc_property32);
4386 offset += sizeof(struct objc_property32);
4387 }
4388}
4389
4390static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4391 bool &is_meta_class) {
4392 struct class_ro64_t cro;
4393 const char *r;
4394 uint32_t offset, xoffset, left;
4395 SectionRef S, xS;
4396 const char *name, *sym_name;
4397 uint64_t n_value;
4398
4399 r = get_pointer_64(p, offset, left, S, info);
4400 if (r == nullptr || left < sizeof(struct class_ro64_t))
4401 return false;
4402 memcpy(&cro, r, sizeof(struct class_ro64_t));
4403 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4404 swapStruct(cro);
4405 outs() << " flags " << format("0x%" PRIx32"x", cro.flags);
4406 if (cro.flags & RO_META(1 << 0))
4407 outs() << " RO_META";
4408 if (cro.flags & RO_ROOT(1 << 1))
4409 outs() << " RO_ROOT";
4410 if (cro.flags & RO_HAS_CXX_STRUCTORS(1 << 2))
4411 outs() << " RO_HAS_CXX_STRUCTORS";
4412 outs() << "\n";
4413 outs() << " instanceStart " << cro.instanceStart << "\n";
4414 outs() << " instanceSize " << cro.instanceSize << "\n";
4415 outs() << " reserved " << format("0x%" PRIx32"x", cro.reserved)
4416 << "\n";
4417 outs() << " ivarLayout " << format("0x%" PRIx64"l" "x", cro.ivarLayout)
4418 << "\n";
4419 print_layout_map64(cro.ivarLayout, info);
4420
4421 outs() << " name ";
4422 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name)__builtin_offsetof(struct class_ro64_t, name), S,
4423 info, n_value, cro.name);
4424 if (n_value != 0) {
4425 if (info->verbose && sym_name != nullptr)
4426 outs() << sym_name;
4427 else
4428 outs() << format("0x%" PRIx64"l" "x", n_value);
4429 if (cro.name != 0)
4430 outs() << " + " << format("0x%" PRIx64"l" "x", cro.name);
4431 } else
4432 outs() << format("0x%" PRIx64"l" "x", cro.name);
4433 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4434 if (name != nullptr)
4435 outs() << format(" %.*s", left, name);
4436 outs() << "\n";
4437
4438 outs() << " baseMethods ";
4439 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods)__builtin_offsetof(struct class_ro64_t, baseMethods),
4440 S, info, n_value, cro.baseMethods);
4441 if (n_value != 0) {
4442 if (info->verbose && sym_name != nullptr)
4443 outs() << sym_name;
4444 else
4445 outs() << format("0x%" PRIx64"l" "x", n_value);
4446 if (cro.baseMethods != 0)
4447 outs() << " + " << format("0x%" PRIx64"l" "x", cro.baseMethods);
4448 } else
4449 outs() << format("0x%" PRIx64"l" "x", cro.baseMethods);
4450 outs() << " (struct method_list_t *)\n";
4451 if (cro.baseMethods + n_value != 0)
4452 print_method_list64_t(cro.baseMethods + n_value, info, "");
4453
4454 outs() << " baseProtocols ";
4455 sym_name =
4456 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols)__builtin_offsetof(struct class_ro64_t, baseProtocols), S,
4457 info, n_value, cro.baseProtocols);
4458 if (n_value != 0) {
4459 if (info->verbose && sym_name != nullptr)
4460 outs() << sym_name;
4461 else
4462 outs() << format("0x%" PRIx64"l" "x", n_value);
4463 if (cro.baseProtocols != 0)
4464 outs() << " + " << format("0x%" PRIx64"l" "x", cro.baseProtocols);
4465 } else
4466 outs() << format("0x%" PRIx64"l" "x", cro.baseProtocols);
4467 outs() << "\n";
4468 if (cro.baseProtocols + n_value != 0)
4469 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4470
4471 outs() << " ivars ";
4472 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars)__builtin_offsetof(struct class_ro64_t, ivars), S,
4473 info, n_value, cro.ivars);
4474 if (n_value != 0) {
4475 if (info->verbose && sym_name != nullptr)
4476 outs() << sym_name;
4477 else
4478 outs() << format("0x%" PRIx64"l" "x", n_value);
4479 if (cro.ivars != 0)
4480 outs() << " + " << format("0x%" PRIx64"l" "x", cro.ivars);
4481 } else
4482 outs() << format("0x%" PRIx64"l" "x", cro.ivars);
4483 outs() << "\n";
4484 if (cro.ivars + n_value != 0)
4485 print_ivar_list64_t(cro.ivars + n_value, info);
4486
4487 outs() << " weakIvarLayout ";
4488 sym_name =
4489 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout)__builtin_offsetof(struct class_ro64_t, weakIvarLayout), S,
4490 info, n_value, cro.weakIvarLayout);
4491 if (n_value != 0) {
4492 if (info->verbose && sym_name != nullptr)
4493 outs() << sym_name;
4494 else
4495 outs() << format("0x%" PRIx64"l" "x", n_value);
4496 if (cro.weakIvarLayout != 0)
4497 outs() << " + " << format("0x%" PRIx64"l" "x", cro.weakIvarLayout);
4498 } else
4499 outs() << format("0x%" PRIx64"l" "x", cro.weakIvarLayout);
4500 outs() << "\n";
4501 print_layout_map64(cro.weakIvarLayout + n_value, info);
4502
4503 outs() << " baseProperties ";
4504 sym_name =
4505 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties)__builtin_offsetof(struct class_ro64_t, baseProperties), S,
4506 info, n_value, cro.baseProperties);
4507 if (n_value != 0) {
4508 if (info->verbose && sym_name != nullptr)
4509 outs() << sym_name;
4510 else
4511 outs() << format("0x%" PRIx64"l" "x", n_value);
4512 if (cro.baseProperties != 0)
4513 outs() << " + " << format("0x%" PRIx64"l" "x", cro.baseProperties);
4514 } else
4515 outs() << format("0x%" PRIx64"l" "x", cro.baseProperties);
4516 outs() << "\n";
4517 if (cro.baseProperties + n_value != 0)
4518 print_objc_property_list64(cro.baseProperties + n_value, info);
4519
4520 is_meta_class = (cro.flags & RO_META(1 << 0)) != 0;
4521 return true;
4522}
4523
4524static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4525 bool &is_meta_class) {
4526 struct class_ro32_t cro;
4527 const char *r;
4528 uint32_t offset, xoffset, left;
4529 SectionRef S, xS;
4530 const char *name;
4531
4532 r = get_pointer_32(p, offset, left, S, info);
4533 if (r == nullptr)
4534 return false;
4535 memset(&cro, '\0', sizeof(struct class_ro32_t));
4536 if (left < sizeof(struct class_ro32_t)) {
4537 memcpy(&cro, r, left);
4538 outs() << " (class_ro_t entends past the end of the section)\n";
4539 } else
4540 memcpy(&cro, r, sizeof(struct class_ro32_t));
4541 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4542 swapStruct(cro);
4543 outs() << " flags " << format("0x%" PRIx32"x", cro.flags);
4544 if (cro.flags & RO_META(1 << 0))
4545 outs() << " RO_META";
4546 if (cro.flags & RO_ROOT(1 << 1))
4547 outs() << " RO_ROOT";
4548 if (cro.flags & RO_HAS_CXX_STRUCTORS(1 << 2))
4549 outs() << " RO_HAS_CXX_STRUCTORS";
4550 outs() << "\n";
4551 outs() << " instanceStart " << cro.instanceStart << "\n";
4552 outs() << " instanceSize " << cro.instanceSize << "\n";
4553 outs() << " ivarLayout " << format("0x%" PRIx32"x", cro.ivarLayout)
4554 << "\n";
4555 print_layout_map32(cro.ivarLayout, info);
4556
4557 outs() << " name " << format("0x%" PRIx32"x", cro.name);
4558 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4559 if (name != nullptr)
4560 outs() << format(" %.*s", left, name);
4561 outs() << "\n";
4562
4563 outs() << " baseMethods "
4564 << format("0x%" PRIx32"x", cro.baseMethods)
4565 << " (struct method_list_t *)\n";
4566 if (cro.baseMethods != 0)
4567 print_method_list32_t(cro.baseMethods, info, "");
4568
4569 outs() << " baseProtocols "
4570 << format("0x%" PRIx32"x", cro.baseProtocols) << "\n";
4571 if (cro.baseProtocols != 0)
4572 print_protocol_list32_t(cro.baseProtocols, info);
4573 outs() << " ivars " << format("0x%" PRIx32"x", cro.ivars)
4574 << "\n";
4575 if (cro.ivars != 0)
4576 print_ivar_list32_t(cro.ivars, info);
4577 outs() << " weakIvarLayout "
4578 << format("0x%" PRIx32"x", cro.weakIvarLayout) << "\n";
4579 print_layout_map32(cro.weakIvarLayout, info);
4580 outs() << " baseProperties "
4581 << format("0x%" PRIx32"x", cro.baseProperties) << "\n";
4582 if (cro.baseProperties != 0)
4583 print_objc_property_list32(cro.baseProperties, info);
4584 is_meta_class = (cro.flags & RO_META(1 << 0)) != 0;
4585 return true;
4586}
4587
4588static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4589 struct class64_t c;
4590 const char *r;
4591 uint32_t offset, left;
4592 SectionRef S;
4593 const char *name;
4594 uint64_t isa_n_value, n_value;
4595
4596 r = get_pointer_64(p, offset, left, S, info);
4597 if (r == nullptr || left < sizeof(struct class64_t))
4598 return;
4599 memcpy(&c, r, sizeof(struct class64_t));
4600 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4601 swapStruct(c);
4602
4603 outs() << " isa " << format("0x%" PRIx64"l" "x", c.isa);
4604 name = get_symbol_64(offset + offsetof(struct class64_t, isa)__builtin_offsetof(struct class64_t, isa), S, info,
4605 isa_n_value, c.isa);
4606 if (name != nullptr)
4607 outs() << " " << name;
4608 outs() << "\n";
4609
4610 outs() << " superclass " << format("0x%" PRIx64"l" "x", c.superclass);
4611 name = get_symbol_64(offset + offsetof(struct class64_t, superclass)__builtin_offsetof(struct class64_t, superclass), S, info,
4612 n_value, c.superclass);
4613 if (name != nullptr)
4614 outs() << " " << name;
4615 else {
4616 name = get_dyld_bind_info_symbolname(S.getAddress() +
4617 offset + offsetof(struct class64_t, superclass)__builtin_offsetof(struct class64_t, superclass), info);
4618 if (name != nullptr)
4619 outs() << " " << name;
4620 }
4621 outs() << "\n";
4622
4623 outs() << " cache " << format("0x%" PRIx64"l" "x", c.cache);
4624 name = get_symbol_64(offset + offsetof(struct class64_t, cache)__builtin_offsetof(struct class64_t, cache), S, info,
4625 n_value, c.cache);
4626 if (name != nullptr)
4627 outs() << " " << name;
4628 outs() << "\n";
4629
4630 outs() << " vtable " << format("0x%" PRIx64"l" "x", c.vtable);
4631 name = get_symbol_64(offset + offsetof(struct class64_t, vtable)__builtin_offsetof(struct class64_t, vtable), S, info,
4632 n_value, c.vtable);
4633 if (name != nullptr)
4634 outs() << " " << name;
4635 outs() << "\n";
4636
4637 name = get_symbol_64(offset + offsetof(struct class64_t, data)__builtin_offsetof(struct class64_t, data), S, info,
4638 n_value, c.data);
4639 outs() << " data ";
4640 if (n_value != 0) {
4641 if (info->verbose && name != nullptr)
4642 outs() << name;
4643 else
4644 outs() << format("0x%" PRIx64"l" "x", n_value);
4645 if (c.data != 0)
4646 outs() << " + " << format("0x%" PRIx64"l" "x", c.data);
4647 } else
4648 outs() << format("0x%" PRIx64"l" "x", c.data);
4649 outs() << " (struct class_ro_t *)";
4650
4651 // This is a Swift class if some of the low bits of the pointer are set.
4652 if ((c.data + n_value) & 0x7)
4653 outs() << " Swift class";
4654 outs() << "\n";
4655 bool is_meta_class;
4656 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
4657 return;
4658
4659 if (!is_meta_class &&
4660 c.isa + isa_n_value != p &&
4661 c.isa + isa_n_value != 0 &&
4662 info->depth < 100) {
4663 info->depth++;
4664 outs() << "Meta Class\n";
4665 print_class64_t(c.isa + isa_n_value, info);
4666 }
4667}
4668
4669static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4670 struct class32_t c;
4671 const char *r;
4672 uint32_t offset, left;
4673 SectionRef S;
4674 const char *name;
4675
4676 r = get_pointer_32(p, offset, left, S, info);
4677 if (r == nullptr)
4678 return;
4679 memset(&c, '\0', sizeof(struct class32_t));
4680 if (left < sizeof(struct class32_t)) {
4681 memcpy(&c, r, left);
4682 outs() << " (class_t entends past the end of the section)\n";
4683 } else
4684 memcpy(&c, r, sizeof(struct class32_t));
4685 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4686 swapStruct(c);
4687
4688 outs() << " isa " << format("0x%" PRIx32"x", c.isa);
4689 name =
4690 get_symbol_32(offset + offsetof(struct class32_t, isa)__builtin_offsetof(struct class32_t, isa), S, info, c.isa);
4691 if (name != nullptr)
4692 outs() << " " << name;
4693 outs() << "\n";
4694
4695 outs() << " superclass " << format("0x%" PRIx32"x", c.superclass);
4696 name = get_symbol_32(offset + offsetof(struct class32_t, superclass)__builtin_offsetof(struct class32_t, superclass), S, info,
4697 c.superclass);
4698 if (name != nullptr)
4699 outs() << " " << name;
4700 outs() << "\n";
4701
4702 outs() << " cache " << format("0x%" PRIx32"x", c.cache);
4703 name = get_symbol_32(offset + offsetof(struct class32_t, cache)__builtin_offsetof(struct class32_t, cache), S, info,
4704 c.cache);
4705 if (name != nullptr)
4706 outs() << " " << name;
4707 outs() << "\n";
4708
4709 outs() << " vtable " << format("0x%" PRIx32"x", c.vtable);
4710 name = get_symbol_32(offset + offsetof(struct class32_t, vtable)__builtin_offsetof(struct class32_t, vtable), S, info,
4711 c.vtable);
4712 if (name != nullptr)
4713 outs() << " " << name;
4714 outs() << "\n";
4715
4716 name =
4717 get_symbol_32(offset + offsetof(struct class32_t, data)__builtin_offsetof(struct class32_t, data), S, info, c.data);
4718 outs() << " data " << format("0x%" PRIx32"x", c.data)
4719 << " (struct class_ro_t *)";
4720
4721 // This is a Swift class if some of the low bits of the pointer are set.
4722 if (c.data & 0x3)
4723 outs() << " Swift class";
4724 outs() << "\n";
4725 bool is_meta_class;
4726 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
4727 return;
4728
4729 if (!is_meta_class) {
4730 outs() << "Meta Class\n";
4731 print_class32_t(c.isa, info);
4732 }
4733}
4734
4735static void print_objc_class_t(struct objc_class_t *objc_class,
4736 struct DisassembleInfo *info) {
4737 uint32_t offset, left, xleft;
4738 const char *name, *p, *ivar_list;
4739 SectionRef S;
4740 int32_t i;
4741 struct objc_ivar_list_t objc_ivar_list;
4742 struct objc_ivar_t ivar;
4743
4744 outs() << "\t\t isa " << format("0x%08" PRIx32"x", objc_class->isa);
4745 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)((objc_class)->info & (0x2))) {
4746 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4747 if (name != nullptr)
4748 outs() << format(" %.*s", left, name);
4749 else
4750 outs() << " (not in an __OBJC section)";
4751 }
4752 outs() << "\n";
4753
4754 outs() << "\t super_class "
4755 << format("0x%08" PRIx32"x", objc_class->super_class);
4756 if (info->verbose) {
4757 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4758 if (name != nullptr)
4759 outs() << format(" %.*s", left, name);
4760 else
4761 outs() << " (not in an __OBJC section)";
4762 }
4763 outs() << "\n";
4764
4765 outs() << "\t\t name " << format("0x%08" PRIx32"x", objc_class->name);
4766 if (info->verbose) {
4767 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4768 if (name != nullptr)
4769 outs() << format(" %.*s", left, name);
4770 else
4771 outs() << " (not in an __OBJC section)";
4772 }
4773 outs() << "\n";
4774
4775 outs() << "\t\t version " << format("0x%08" PRIx32"x", objc_class->version)
4776 << "\n";
4777
4778 outs() << "\t\t info " << format("0x%08" PRIx32"x", objc_class->info);
4779 if (info->verbose) {
4780 if (CLS_GETINFO(objc_class, CLS_CLASS)((objc_class)->info & (0x1)))
4781 outs() << " CLS_CLASS";
4782 else if (CLS_GETINFO(objc_class, CLS_META)((objc_class)->info & (0x2)))
4783 outs() << " CLS_META";
4784 }
4785 outs() << "\n";
4786
4787 outs() << "\t instance_size "
4788 << format("0x%08" PRIx32"x", objc_class->instance_size) << "\n";
4789
4790 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4791 outs() << "\t\t ivars " << format("0x%08" PRIx32"x", objc_class->ivars);
4792 if (p != nullptr) {
4793 if (left > sizeof(struct objc_ivar_list_t)) {
4794 outs() << "\n";
4795 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4796 } else {
4797 outs() << " (entends past the end of the section)\n";
4798 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4799 memcpy(&objc_ivar_list, p, left);
4800 }
4801 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4802 swapStruct(objc_ivar_list);
4803 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4804 ivar_list = p + sizeof(struct objc_ivar_list_t);
4805 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4806 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4807 outs() << "\t\t remaining ivar's extend past the of the section\n";
4808 break;
4809 }
4810 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4811 sizeof(struct objc_ivar_t));
4812 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4813 swapStruct(ivar);
4814
4815 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32"x", ivar.ivar_name);
4816 if (info->verbose) {
4817 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4818 if (name != nullptr)
4819 outs() << format(" %.*s", xleft, name);
4820 else
4821 outs() << " (not in an __OBJC section)";
4822 }
4823 outs() << "\n";
4824
4825 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32"x", ivar.ivar_type);
4826 if (info->verbose) {
4827 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4828 if (name != nullptr)
4829 outs() << format(" %.*s", xleft, name);
4830 else
4831 outs() << " (not in an __OBJC section)";
4832 }
4833 outs() << "\n";
4834
4835 outs() << "\t\t ivar_offset "
4836 << format("0x%08" PRIx32"x", ivar.ivar_offset) << "\n";
4837 }
4838 } else {
4839 outs() << " (not in an __OBJC section)\n";
4840 }
4841
4842 outs() << "\t\t methods " << format("0x%08" PRIx32"x", objc_class->methodLists);
4843 if (print_method_list(objc_class->methodLists, info))
4844 outs() << " (not in an __OBJC section)\n";
4845
4846 outs() << "\t\t cache " << format("0x%08" PRIx32"x", objc_class->cache)
4847 << "\n";
4848
4849 outs() << "\t\tprotocols " << format("0x%08" PRIx32"x", objc_class->protocols);
4850 if (print_protocol_list(objc_class->protocols, 16, info))
4851 outs() << " (not in an __OBJC section)\n";
4852}
4853
4854static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4855 struct DisassembleInfo *info) {
4856 uint32_t offset, left;
4857 const char *name;
4858 SectionRef S;
4859
4860 outs() << "\t category name "
4861 << format("0x%08" PRIx32"x", objc_category->category_name);
4862 if (info->verbose) {
4863 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4864 true);
4865 if (name != nullptr)
4866 outs() << format(" %.*s", left, name);
4867 else
4868 outs() << " (not in an __OBJC section)";
4869 }
4870 outs() << "\n";
4871
4872 outs() << "\t\t class name "
4873 << format("0x%08" PRIx32"x", objc_category->class_name);
4874 if (info->verbose) {
4875 name =
4876 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4877 if (name != nullptr)
4878 outs() << format(" %.*s", left, name);
4879 else
4880 outs() << " (not in an __OBJC section)";
4881 }
4882 outs() << "\n";
4883
4884 outs() << "\t instance methods "
4885 << format("0x%08" PRIx32"x", objc_category->instance_methods);
4886 if (print_method_list(objc_category->instance_methods, info))
4887 outs() << " (not in an __OBJC section)\n";
4888
4889 outs() << "\t class methods "
4890 << format("0x%08" PRIx32"x", objc_category->class_methods);
4891 if (print_method_list(objc_category->class_methods, info))
4892 outs() << " (not in an __OBJC section)\n";
4893}
4894
4895static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4896 struct category64_t c;
4897 const char *r;
4898 uint32_t offset, xoffset, left;
4899 SectionRef S, xS;
4900 const char *name, *sym_name;
4901 uint64_t n_value;
4902
4903 r = get_pointer_64(p, offset, left, S, info);
4904 if (r == nullptr)
4905 return;
4906 memset(&c, '\0', sizeof(struct category64_t));
4907 if (left < sizeof(struct category64_t)) {
4908 memcpy(&c, r, left);
4909 outs() << " (category_t entends past the end of the section)\n";
4910 } else
4911 memcpy(&c, r, sizeof(struct category64_t));
4912 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4913 swapStruct(c);
4914
4915 outs() << " name ";
4916 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name)__builtin_offsetof(struct category64_t, name), S,
4917 info, n_value, c.name);
4918 if (n_value != 0) {
4919 if (info->verbose && sym_name != nullptr)
4920 outs() << sym_name;
4921 else
4922 outs() << format("0x%" PRIx64"l" "x", n_value);
4923 if (c.name != 0)
4924 outs() << " + " << format("0x%" PRIx64"l" "x", c.name);
4925 } else
4926 outs() << format("0x%" PRIx64"l" "x", c.name);
4927 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4928 if (name != nullptr)
4929 outs() << format(" %.*s", left, name);
4930 outs() << "\n";
4931
4932 outs() << " cls ";
4933 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls)__builtin_offsetof(struct category64_t, cls), S, info,
4934 n_value, c.cls);
4935 if (n_value != 0) {
4936 if (info->verbose && sym_name != nullptr)
4937 outs() << sym_name;
4938 else
4939 outs() << format("0x%" PRIx64"l" "x", n_value);
4940 if (c.cls != 0)
4941 outs() << " + " << format("0x%" PRIx64"l" "x", c.cls);
4942 } else
4943 outs() << format("0x%" PRIx64"l" "x", c.cls);
4944 outs() << "\n";
4945 if (c.cls + n_value != 0)
4946 print_class64_t(c.cls + n_value, info);
4947
4948 outs() << " instanceMethods ";
4949 sym_name =
4950 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods)__builtin_offsetof(struct category64_t, instanceMethods), S,
4951 info, n_value, c.instanceMethods);
4952 if (n_value != 0) {
4953 if (info->verbose && sym_name != nullptr)
4954 outs() << sym_name;
4955 else
4956 outs() << format("0x%" PRIx64"l" "x", n_value);
4957 if (c.instanceMethods != 0)
4958 outs() << " + " << format("0x%" PRIx64"l" "x", c.instanceMethods);
4959 } else
4960 outs() << format("0x%" PRIx64"l" "x", c.instanceMethods);
4961 outs() << "\n";
4962 if (c.instanceMethods + n_value != 0)
4963 print_method_list64_t(c.instanceMethods + n_value, info, "");
4964
4965 outs() << " classMethods ";
4966 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods)__builtin_offsetof(struct category64_t, classMethods),
4967 S, info, n_value, c.classMethods);
4968 if (n_value != 0) {
4969 if (info->verbose && sym_name != nullptr)
4970 outs() << sym_name;
4971 else
4972 outs() << format("0x%" PRIx64"l" "x", n_value);
4973 if (c.classMethods != 0)
4974 outs() << " + " << format("0x%" PRIx64"l" "x", c.classMethods);
4975 } else
4976 outs() << format("0x%" PRIx64"l" "x", c.classMethods);
4977 outs() << "\n";
4978 if (c.classMethods + n_value != 0)
4979 print_method_list64_t(c.classMethods + n_value, info, "");
4980
4981 outs() << " protocols ";
4982 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols)__builtin_offsetof(struct category64_t, protocols), S,
4983 info, n_value, c.protocols);
4984 if (n_value != 0) {
4985 if (info->verbose && sym_name != nullptr)
4986 outs() << sym_name;
4987 else
4988 outs() << format("0x%" PRIx64"l" "x", n_value);
4989 if (c.protocols != 0)
4990 outs() << " + " << format("0x%" PRIx64"l" "x", c.protocols);
4991 } else
4992 outs() << format("0x%" PRIx64"l" "x", c.protocols);
4993 outs() << "\n";
4994 if (c.protocols + n_value != 0)
4995 print_protocol_list64_t(c.protocols + n_value, info);
4996
4997 outs() << "instanceProperties ";
4998 sym_name =
4999 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties)__builtin_offsetof(struct category64_t, instanceProperties),
5000 S, info, n_value, c.instanceProperties);
5001 if (n_value != 0) {
5002 if (info->verbose && sym_name != nullptr)
5003 outs() << sym_name;
5004 else
5005 outs() << format("0x%" PRIx64"l" "x", n_value);
5006 if (c.instanceProperties != 0)
5007 outs() << " + " << format("0x%" PRIx64"l" "x", c.instanceProperties);
5008 } else
5009 outs() << format("0x%" PRIx64"l" "x", c.instanceProperties);
5010 outs() << "\n";
5011 if (c.instanceProperties + n_value != 0)
5012 print_objc_property_list64(c.instanceProperties + n_value, info);
5013}
5014
5015static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
5016 struct category32_t c;
5017 const char *r;
5018 uint32_t offset, left;
5019 SectionRef S, xS;
5020 const char *name;
5021
5022 r = get_pointer_32(p, offset, left, S, info);
5023 if (r == nullptr)
5024 return;
5025 memset(&c, '\0', sizeof(struct category32_t));
5026 if (left < sizeof(struct category32_t)) {
5027 memcpy(&c, r, left);
5028 outs() << " (category_t entends past the end of the section)\n";
5029 } else
5030 memcpy(&c, r, sizeof(struct category32_t));
5031 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5032 swapStruct(c);
5033
5034 outs() << " name " << format("0x%" PRIx32"x", c.name);
5035 name = get_symbol_32(offset + offsetof(struct category32_t, name)__builtin_offsetof(struct category32_t, name), S, info,
5036 c.name);
5037 if (name)
5038 outs() << " " << name;
5039 outs() << "\n";
5040
5041 outs() << " cls " << format("0x%" PRIx32"x", c.cls) << "\n";
5042 if (c.cls != 0)
5043 print_class32_t(c.cls, info);
5044 outs() << " instanceMethods " << format("0x%" PRIx32"x", c.instanceMethods)
5045 << "\n";
5046 if (c.instanceMethods != 0)
5047 print_method_list32_t(c.instanceMethods, info, "");
5048 outs() << " classMethods " << format("0x%" PRIx32"x", c.classMethods)
5049 << "\n";
5050 if (c.classMethods != 0)
5051 print_method_list32_t(c.classMethods, info, "");
5052 outs() << " protocols " << format("0x%" PRIx32"x", c.protocols) << "\n";
5053 if (c.protocols != 0)
5054 print_protocol_list32_t(c.protocols, info);
5055 outs() << "instanceProperties " << format("0x%" PRIx32"x", c.instanceProperties)
5056 << "\n";
5057 if (c.instanceProperties != 0)
5058 print_objc_property_list32(c.instanceProperties, info);
5059}
5060
5061static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
5062 uint32_t i, left, offset, xoffset;
5063 uint64_t p, n_value;
5064 struct message_ref64 mr;
5065 const char *name, *sym_name;
5066 const char *r;
5067 SectionRef xS;
5068
5069 if (S == SectionRef())
5070 return;
5071
5072 StringRef SectName;
5073 S.getName(SectName);
5074 DataRefImpl Ref = S.getRawDataRefImpl();
5075 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5076 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5077 offset = 0;
5078 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5079 p = S.getAddress() + i;
5080 r = get_pointer_64(p, offset, left, S, info);
5081 if (r == nullptr)
5082 return;
5083 memset(&mr, '\0', sizeof(struct message_ref64));
5084 if (left < sizeof(struct message_ref64)) {
5085 memcpy(&mr, r, left);
5086 outs() << " (message_ref entends past the end of the section)\n";
5087 } else
5088 memcpy(&mr, r, sizeof(struct message_ref64));
5089 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5090 swapStruct(mr);
5091
5092 outs() << " imp ";
5093 name = get_symbol_64(offset + offsetof(struct message_ref64, imp)__builtin_offsetof(struct message_ref64, imp), S, info,
5094 n_value, mr.imp);
5095 if (n_value != 0) {
5096 outs() << format("0x%" PRIx64"l" "x", n_value) << " ";
5097 if (mr.imp != 0)
5098 outs() << "+ " << format("0x%" PRIx64"l" "x", mr.imp) << " ";
5099 } else
5100 outs() << format("0x%" PRIx64"l" "x", mr.imp) << " ";
5101 if (name != nullptr)
5102 outs() << " " << name;
5103 outs() << "\n";
5104
5105 outs() << " sel ";
5106 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel)__builtin_offsetof(struct message_ref64, sel), S,
5107 info, n_value, mr.sel);
5108 if (n_value != 0) {
5109 if (info->verbose && sym_name != nullptr)
5110 outs() << sym_name;
5111 else
5112 outs() << format("0x%" PRIx64"l" "x", n_value);
5113 if (mr.sel != 0)
5114 outs() << " + " << format("0x%" PRIx64"l" "x", mr.sel);
5115 } else
5116 outs() << format("0x%" PRIx64"l" "x", mr.sel);
5117 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5118 if (name != nullptr)
5119 outs() << format(" %.*s", left, name);
5120 outs() << "\n";
5121
5122 offset += sizeof(struct message_ref64);
5123 }
5124}
5125
5126static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5127 uint32_t i, left, offset, xoffset, p;
5128 struct message_ref32 mr;
5129 const char *name, *r;
5130 SectionRef xS;
5131
5132 if (S == SectionRef())
5133 return;
5134
5135 StringRef SectName;
5136 S.getName(SectName);
5137 DataRefImpl Ref = S.getRawDataRefImpl();
5138 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5139 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5140 offset = 0;
5141 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5142 p = S.getAddress() + i;
5143 r = get_pointer_32(p, offset, left, S, info);
5144 if (r == nullptr)
5145 return;
5146 memset(&mr, '\0', sizeof(struct message_ref32));
5147 if (left < sizeof(struct message_ref32)) {
5148 memcpy(&mr, r, left);
5149 outs() << " (message_ref entends past the end of the section)\n";
5150 } else
5151 memcpy(&mr, r, sizeof(struct message_ref32));
5152 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5153 swapStruct(mr);
5154
5155 outs() << " imp " << format("0x%" PRIx32"x", mr.imp);
5156 name = get_symbol_32(offset + offsetof(struct message_ref32, imp)__builtin_offsetof(struct message_ref32, imp), S, info,
5157 mr.imp);
5158 if (name != nullptr)
5159 outs() << " " << name;
5160 outs() << "\n";
5161
5162 outs() << " sel " << format("0x%" PRIx32"x", mr.sel);
5163 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5164 if (name != nullptr)
5165 outs() << " " << name;
5166 outs() << "\n";
5167
5168 offset += sizeof(struct message_ref32);
5169 }
5170}
5171
5172static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5173 uint32_t left, offset, swift_version;
5174 uint64_t p;
5175 struct objc_image_info64 o;
5176 const char *r;
5177
5178 if (S == SectionRef())
5179 return;
5180
5181 StringRef SectName;
5182 S.getName(SectName);
5183 DataRefImpl Ref = S.getRawDataRefImpl();
5184 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5185 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5186 p = S.getAddress();
5187 r = get_pointer_64(p, offset, left, S, info);
5188 if (r == nullptr)
5189 return;
5190 memset(&o, '\0', sizeof(struct objc_image_info64));
5191 if (left < sizeof(struct objc_image_info64)) {
5192 memcpy(&o, r, left);
5193 outs() << " (objc_image_info entends past the end of the section)\n";
5194 } else
5195 memcpy(&o, r, sizeof(struct objc_image_info64));
5196 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5197 swapStruct(o);
5198 outs() << " version " << o.version << "\n";
5199 outs() << " flags " << format("0x%" PRIx32"x", o.flags);
5200 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT(1 << 0))
5201 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5202 if (o.flags & OBJC_IMAGE_SUPPORTS_GC(1 << 1))
5203 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5204 swift_version = (o.flags >> 8) & 0xff;
5205 if (swift_version != 0) {
5206 if (swift_version == 1)
5207 outs() << " Swift 1.0";
5208 else if (swift_version == 2)
5209 outs() << " Swift 1.1";
5210 else
5211 outs() << " unknown future Swift version (" << swift_version << ")";
5212 }
5213 outs() << "\n";
5214}
5215
5216static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5217 uint32_t left, offset, swift_version, p;
5218 struct objc_image_info32 o;
5219 const char *r;
5220
5221 if (S == SectionRef())
5222 return;
5223
5224 StringRef SectName;
5225 S.getName(SectName);
5226 DataRefImpl Ref = S.getRawDataRefImpl();
5227 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5228 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5229 p = S.getAddress();
5230 r = get_pointer_32(p, offset, left, S, info);
5231 if (r == nullptr)
5232 return;
5233 memset(&o, '\0', sizeof(struct objc_image_info32));
5234 if (left < sizeof(struct objc_image_info32)) {
5235 memcpy(&o, r, left);
5236 outs() << " (objc_image_info entends past the end of the section)\n";
5237 } else
5238 memcpy(&o, r, sizeof(struct objc_image_info32));
5239 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5240 swapStruct(o);
5241 outs() << " version " << o.version << "\n";
5242 outs() << " flags " << format("0x%" PRIx32"x", o.flags);
5243 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT(1 << 0))
5244 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5245 if (o.flags & OBJC_IMAGE_SUPPORTS_GC(1 << 1))
5246 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5247 swift_version = (o.flags >> 8) & 0xff;
5248 if (swift_version != 0) {
5249 if (swift_version == 1)
5250 outs() << " Swift 1.0";
5251 else if (swift_version == 2)
5252 outs() << " Swift 1.1";
5253 else
5254 outs() << " unknown future Swift version (" << swift_version << ")";
5255 }
5256 outs() << "\n";
5257}
5258
5259static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5260 uint32_t left, offset, p;
5261 struct imageInfo_t o;
5262 const char *r;
5263
5264 StringRef SectName;
5265 S.getName(SectName);
5266 DataRefImpl Ref = S.getRawDataRefImpl();
5267 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5268 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5269 p = S.getAddress();
5270 r = get_pointer_32(p, offset, left, S, info);
5271 if (r == nullptr)
5272 return;
5273 memset(&o, '\0', sizeof(struct imageInfo_t));
5274 if (left < sizeof(struct imageInfo_t)) {
5275 memcpy(&o, r, left);
5276 outs() << " (imageInfo entends past the end of the section)\n";
5277 } else
5278 memcpy(&o, r, sizeof(struct imageInfo_t));
5279 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5280 swapStruct(o);
5281 outs() << " version " << o.version << "\n";
5282 outs() << " flags " << format("0x%" PRIx32"x", o.flags);
5283 if (o.flags & 0x1)
5284 outs() << " F&C";
5285 if (o.flags & 0x2)
5286 outs() << " GC";
5287 if (o.flags & 0x4)
5288 outs() << " GC-only";
5289 else
5290 outs() << " RR";
5291 outs() << "\n";
5292}
5293
5294static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5295 SymbolAddressMap AddrMap;
5296 if (verbose)
5297 CreateSymbolAddressMap(O, &AddrMap);
5298
5299 std::vector<SectionRef> Sections;
5300 for (const SectionRef &Section : O->sections()) {
5301 StringRef SectName;
5302 Section.getName(SectName);
5303 Sections.push_back(Section);
5304 }
5305
5306 struct DisassembleInfo info;
5307 // Set up the block of info used by the Symbolizer call backs.
5308 info.verbose = verbose;
5309 info.O = O;
5310 info.AddrMap = &AddrMap;
5311 info.Sections = &Sections;
5312 info.class_name = nullptr;
5313 info.selector_name = nullptr;
5314 info.method = nullptr;
5315 info.demangled_name = nullptr;
5316 info.bindtable = nullptr;
5317 info.adrp_addr = 0;
5318 info.adrp_inst = 0;
5319
5320 info.depth = 0;
5321 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5322 if (CL == SectionRef())
5323 CL = get_section(O, "__DATA", "__objc_classlist");
5324 if (CL == SectionRef())
5325 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
5326 if (CL == SectionRef())
5327 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
5328 info.S = CL;
5329 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5330
5331 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5332 if (CR == SectionRef())
5333 CR = get_section(O, "__DATA", "__objc_classrefs");
5334 if (CR == SectionRef())
5335 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
5336 if (CR == SectionRef())
5337 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
5338 info.S = CR;
5339 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5340
5341 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5342 if (SR == SectionRef())
5343 SR = get_section(O, "__DATA", "__objc_superrefs");
5344 if (SR == SectionRef())
5345 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
5346 if (SR == SectionRef())
5347 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
5348 info.S = SR;
5349 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5350
5351 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5352 if (CA == SectionRef())
5353 CA = get_section(O, "__DATA", "__objc_catlist");
5354 if (CA == SectionRef())
5355 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
5356 if (CA == SectionRef())
5357 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
5358 info.S = CA;
5359 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5360
5361 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5362 if (PL == SectionRef())
5363 PL = get_section(O, "__DATA", "__objc_protolist");
5364 if (PL == SectionRef())
5365 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
5366 if (PL == SectionRef())
5367 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
5368 info.S = PL;
5369 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5370
5371 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5372 if (MR == SectionRef())
5373 MR = get_section(O, "__DATA", "__objc_msgrefs");
5374 if (MR == SectionRef())
5375 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
5376 if (MR == SectionRef())
5377 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
5378 info.S = MR;
5379 print_message_refs64(MR, &info);
5380
5381 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5382 if (II == SectionRef())
5383 II = get_section(O, "__DATA", "__objc_imageinfo");
5384 if (II == SectionRef())
5385 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
5386 if (II == SectionRef())
5387 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
5388 info.S = II;
5389 print_image_info64(II, &info);
5390}
5391
5392static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5393 SymbolAddressMap AddrMap;
5394 if (verbose)
5395 CreateSymbolAddressMap(O, &AddrMap);
5396
5397 std::vector<SectionRef> Sections;
5398 for (const SectionRef &Section : O->sections()) {
5399 StringRef SectName;
5400 Section.getName(SectName);
5401 Sections.push_back(Section);
5402 }
5403
5404 struct DisassembleInfo info;
5405 // Set up the block of info used by the Symbolizer call backs.
5406 info.verbose = verbose;
5407 info.O = O;
5408 info.AddrMap = &AddrMap;
5409 info.Sections = &Sections;
5410 info.class_name = nullptr;
5411 info.selector_name = nullptr;
5412 info.method = nullptr;
5413 info.demangled_name = nullptr;
5414 info.bindtable = nullptr;
5415 info.adrp_addr = 0;
5416 info.adrp_inst = 0;
5417
5418 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5419 if (CL == SectionRef())
5420 CL = get_section(O, "__DATA", "__objc_classlist");
5421 if (CL == SectionRef())
5422 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
5423 if (CL == SectionRef())
5424 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
5425 info.S = CL;
5426 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5427
5428 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5429 if (CR == SectionRef())
5430 CR = get_section(O, "__DATA", "__objc_classrefs");
5431 if (CR == SectionRef())
5432 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
5433 if (CR == SectionRef())
5434 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
5435 info.S = CR;
5436 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5437
5438 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5439 if (SR == SectionRef())
5440 SR = get_section(O, "__DATA", "__objc_superrefs");
5441 if (SR == SectionRef())
5442 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
5443 if (SR == SectionRef())
5444 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
5445 info.S = SR;
5446 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5447
5448 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5449 if (CA == SectionRef())
5450 CA = get_section(O, "__DATA", "__objc_catlist");
5451 if (CA == SectionRef())
5452 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
5453 if (CA == SectionRef())
5454 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
5455 info.S = CA;
5456 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5457
5458 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5459 if (PL == SectionRef())
5460 PL = get_section(O, "__DATA", "__objc_protolist");
5461 if (PL == SectionRef())
5462 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
5463 if (PL == SectionRef())
5464 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
5465 info.S = PL;
5466 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5467
5468 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5469 if (MR == SectionRef())
5470 MR = get_section(O, "__DATA", "__objc_msgrefs");
5471 if (MR == SectionRef())
5472 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
5473 if (MR == SectionRef())
5474 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
5475 info.S = MR;
5476 print_message_refs32(MR, &info);
5477
5478 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5479 if (II == SectionRef())
5480 II = get_section(O, "__DATA", "__objc_imageinfo");
5481 if (II == SectionRef())
5482 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
5483 if (II == SectionRef())
5484 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
5485 info.S = II;
5486 print_image_info32(II, &info);
5487}
5488
5489static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5490 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5491 const char *r, *name, *defs;
5492 struct objc_module_t module;
5493 SectionRef S, xS;
5494 struct objc_symtab_t symtab;
5495 struct objc_class_t objc_class;
5496 struct objc_category_t objc_category;
5497
5498 outs() << "Objective-C segment\n";
5499 S = get_section(O, "__OBJC", "__module_info");
5500 if (S == SectionRef())
5501 return false;
5502
5503 SymbolAddressMap AddrMap;
5504 if (verbose)
5505 CreateSymbolAddressMap(O, &AddrMap);
5506
5507 std::vector<SectionRef> Sections;
5508 for (const SectionRef &Section : O->sections()) {
5509 StringRef SectName;
5510 Section.getName(SectName);
5511 Sections.push_back(Section);
5512 }
5513
5514 struct DisassembleInfo info;
5515 // Set up the block of info used by the Symbolizer call backs.
5516 info.verbose = verbose;
5517 info.O = O;
5518 info.AddrMap = &AddrMap;
5519 info.Sections = &Sections;
5520 info.class_name = nullptr;
5521 info.selector_name = nullptr;
5522 info.method = nullptr;
5523 info.demangled_name = nullptr;
5524 info.bindtable = nullptr;
5525 info.adrp_addr = 0;
5526 info.adrp_inst = 0;
5527
5528 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5529 p = S.getAddress() + i;
5530 r = get_pointer_32(p, offset, left, S, &info, true);
5531 if (r == nullptr)
5532 return true;
5533 memset(&module, '\0', sizeof(struct objc_module_t));
5534 if (left < sizeof(struct objc_module_t)) {
5535 memcpy(&module, r, left);
5536 outs() << " (module extends past end of __module_info section)\n";
5537 } else
5538 memcpy(&module, r, sizeof(struct objc_module_t));
5539 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5540 swapStruct(module);
5541
5542 outs() << "Module " << format("0x%" PRIx32"x", p) << "\n";
5543 outs() << " version " << module.version << "\n";
5544 outs() << " size " << module.size << "\n";
5545 outs() << " name ";
5546 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5547 if (name != nullptr)
5548 outs() << format("%.*s", left, name);
5549 else
5550 outs() << format("0x%08" PRIx32"x", module.name)
5551 << "(not in an __OBJC section)";
5552 outs() << "\n";
5553
5554 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5555 if (module.symtab == 0 || r == nullptr) {
5556 outs() << " symtab " << format("0x%08" PRIx32"x", module.symtab)
5557 << " (not in an __OBJC section)\n";
5558 continue;
5559 }
5560 outs() << " symtab " << format("0x%08" PRIx32"x", module.symtab) << "\n";
5561 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5562 defs_left = 0;
5563 defs = nullptr;
5564 if (left < sizeof(struct objc_symtab_t)) {
5565 memcpy(&symtab, r, left);
5566 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5567 } else {
5568 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5569 if (left > sizeof(struct objc_symtab_t)) {
5570 defs_left = left - sizeof(struct objc_symtab_t);
5571 defs = r + sizeof(struct objc_symtab_t);
5572 }
5573 }
5574 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5575 swapStruct(symtab);
5576
5577 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5578 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5579 outs() << "\trefs " << format("0x%08" PRIx32"x", symtab.refs);
5580 if (r == nullptr)
5581 outs() << " (not in an __OBJC section)";
5582 outs() << "\n";
5583 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5584 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5585 if (symtab.cls_def_cnt > 0)
5586 outs() << "\tClass Definitions\n";
5587 for (j = 0; j < symtab.cls_def_cnt; j++) {
5588 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5589 outs() << "\t(remaining class defs entries entends past the end of the "
5590 << "section)\n";
5591 break;
5592 }
5593 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5594 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5595 sys::swapByteOrder(def);
5596
5597 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5598 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32"x", def);
5599 if (r != nullptr) {
5600 if (left > sizeof(struct objc_class_t)) {
5601 outs() << "\n";
5602 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5603 } else {
5604 outs() << " (entends past the end of the section)\n";
5605 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5606 memcpy(&objc_class, r, left);
5607 }
5608 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5609 swapStruct(objc_class);
5610 print_objc_class_t(&objc_class, &info);
5611 } else {
5612 outs() << "(not in an __OBJC section)\n";
5613 }
5614
5615 if (CLS_GETINFO(&objc_class, CLS_CLASS)((&objc_class)->info & (0x1))) {
5616 outs() << "\tMeta Class";
5617 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5618 if (r != nullptr) {
5619 if (left > sizeof(struct objc_class_t)) {
5620 outs() << "\n";
5621 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5622 } else {
5623 outs() << " (entends past the end of the section)\n";
5624 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5625 memcpy(&objc_class, r, left);
5626 }
5627 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5628 swapStruct(objc_class);
5629 print_objc_class_t(&objc_class, &info);
5630 } else {
5631 outs() << "(not in an __OBJC section)\n";
5632 }
5633 }
5634 }
5635 if (symtab.cat_def_cnt > 0)
5636 outs() << "\tCategory Definitions\n";
5637 for (j = 0; j < symtab.cat_def_cnt; j++) {
5638 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5639 outs() << "\t(remaining category defs entries entends past the end of "
5640 << "the section)\n";
5641 break;
5642 }
5643 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5644 sizeof(uint32_t));
5645 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5646 sys::swapByteOrder(def);
5647
5648 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5649 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5650 << format("0x%08" PRIx32"x", def);
5651 if (r != nullptr) {
5652 if (left > sizeof(struct objc_category_t)) {
5653 outs() << "\n";
5654 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5655 } else {
5656 outs() << " (entends past the end of the section)\n";
5657 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5658 memcpy(&objc_category, r, left);
5659 }
5660 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5661 swapStruct(objc_category);
5662 print_objc_objc_category_t(&objc_category, &info);
5663 } else {
5664 outs() << "(not in an __OBJC section)\n";
5665 }
5666 }
5667 }
5668 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5669 if (II != SectionRef())
5670 print_image_info(II, &info);
5671
5672 return true;
5673}
5674
5675static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5676 uint32_t size, uint32_t addr) {
5677 SymbolAddressMap AddrMap;
5678 CreateSymbolAddressMap(O, &AddrMap);
5679
5680 std::vector<SectionRef> Sections;
5681 for (const SectionRef &Section : O->sections()) {
5682 StringRef SectName;
5683 Section.getName(SectName);
5684 Sections.push_back(Section);
5685 }
5686
5687 struct DisassembleInfo info;
5688 // Set up the block of info used by the Symbolizer call backs.
5689 info.verbose = true;
5690 info.O = O;
5691 info.AddrMap = &AddrMap;
5692 info.Sections = &Sections;
5693 info.class_name = nullptr;
5694 info.selector_name = nullptr;
5695 info.method = nullptr;
5696 info.demangled_name = nullptr;
5697 info.bindtable = nullptr;
5698 info.adrp_addr = 0;
5699 info.adrp_inst = 0;
5700
5701 const char *p;
5702 struct objc_protocol_t protocol;
5703 uint32_t left, paddr;
5704 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5705 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5706 left = size - (p - sect);
5707 if (left < sizeof(struct objc_protocol_t)) {
5708 outs() << "Protocol extends past end of __protocol section\n";
5709 memcpy(&protocol, p, left);
5710 } else
5711 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5712 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5713 swapStruct(protocol);
5714 paddr = addr + (p - sect);
5715 outs() << "Protocol " << format("0x%" PRIx32"x", paddr);
5716 if (print_protocol(paddr, 0, &info))
5717 outs() << "(not in an __OBJC section)\n";
5718 }
5719}
5720
5721#ifdef HAVE_LIBXAR
5722inline void swapStruct(struct xar_header &xar) {
5723 sys::swapByteOrder(xar.magic);
5724 sys::swapByteOrder(xar.size);
5725 sys::swapByteOrder(xar.version);
5726 sys::swapByteOrder(xar.toc_length_compressed);
5727 sys::swapByteOrder(xar.toc_length_uncompressed);
5728 sys::swapByteOrder(xar.cksum_alg);
5729}
5730
5731static void PrintModeVerbose(uint32_t mode) {
5732 switch(mode & S_IFMT){
5733 case S_IFDIR:
5734 outs() << "d";
5735 break;
5736 case S_IFCHR:
5737 outs() << "c";
5738 break;
5739 case S_IFBLK:
5740 outs() << "b";
5741 break;
5742 case S_IFREG:
5743 outs() << "-";
5744 break;
5745 case S_IFLNK:
5746 outs() << "l";
5747 break;
5748 case S_IFSOCK:
5749 outs() << "s";
5750 break;
5751 default:
5752 outs() << "?";
5753 break;
5754 }
5755
5756 /* owner permissions */
5757 if(mode & S_IREAD)
5758 outs() << "r";
5759 else
5760 outs() << "-";
5761 if(mode & S_IWRITE)
5762 outs() << "w";
5763 else
5764 outs() << "-";
5765 if(mode & S_ISUID)
5766 outs() << "s";
5767 else if(mode & S_IEXEC)
5768 outs() << "x";
5769 else
5770 outs() << "-";
5771
5772 /* group permissions */
5773 if(mode & (S_IREAD >> 3))
5774 outs() << "r";
5775 else
5776 outs() << "-";
5777 if(mode & (S_IWRITE >> 3))
5778 outs() << "w";
5779 else
5780 outs() << "-";
5781 if(mode & S_ISGID)
5782 outs() << "s";
5783 else if(mode & (S_IEXEC >> 3))
5784 outs() << "x";
5785 else
5786 outs() << "-";
5787
5788 /* other permissions */
5789 if(mode & (S_IREAD >> 6))
5790 outs() << "r";
5791 else
5792 outs() << "-";
5793 if(mode & (S_IWRITE >> 6))
5794 outs() << "w";
5795 else
5796 outs() << "-";
5797 if(mode & S_ISVTX)
5798 outs() << "t";
5799 else if(mode & (S_IEXEC >> 6))
5800 outs() << "x";
5801 else
5802 outs() << "-";
5803}
5804
5805static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
5806 xar_iter_t xi;
5807 xar_file_t xf;
5808 xar_iter_t xp;
5809 const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
5810 char *endp;
5811 uint32_t mode_value;
5812
5813 xi = xar_iter_new();
5814 if (!xi) {
5815 errs() << "Can't obtain an xar iterator for xar archive "
5816 << XarFilename << "\n";
5817 return;
5818 }
5819
5820 // Go through the xar's files.
5821 for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
5822 xp = xar_iter_new();
5823 if(!xp){
5824 errs() << "Can't obtain an xar iterator for xar archive "
5825 << XarFilename << "\n";
5826 return;
5827 }
5828 type = nullptr;
5829 mode = nullptr;
5830 user = nullptr;
5831 group = nullptr;
5832 size = nullptr;
5833 mtime = nullptr;
5834 name = nullptr;
5835 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
5836 const char *val = nullptr;
5837 xar_prop_get(xf, key, &val);
5838#if 0 // Useful for debugging.
5839 outs() << "key: " << key << " value: " << val << "\n";
5840#endif
5841 if(strcmp(key, "type") == 0)
5842 type = val;
5843 if(strcmp(key, "mode") == 0)
5844 mode = val;
5845 if(strcmp(key, "user") == 0)
5846 user = val;
5847 if(strcmp(key, "group") == 0)
5848 group = val;
5849 if(strcmp(key, "data/size") == 0)
5850 size = val;
5851 if(strcmp(key, "mtime") == 0)
5852 mtime = val;
5853 if(strcmp(key, "name") == 0)
5854 name = val;
5855 }
5856 if(mode != nullptr){
5857 mode_value = strtoul(mode, &endp, 8);
5858 if(*endp != '\0')
5859 outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
5860 if(strcmp(type, "file") == 0)
5861 mode_value |= S_IFREG;
5862 PrintModeVerbose(mode_value);
5863 outs() << " ";
5864 }
5865 if(user != nullptr)
5866 outs() << format("%10s/", user);
5867 if(group != nullptr)
5868 outs() << format("%-10s ", group);
5869 if(size != nullptr)
5870 outs() << format("%7s ", size);
5871 if(mtime != nullptr){
5872 for(m = mtime; *m != 'T' && *m != '\0'; m++)
5873 outs() << *m;
5874 if(*m == 'T')
5875 m++;
5876 outs() << " ";
5877 for( ; *m != 'Z' && *m != '\0'; m++)
5878 outs() << *m;
5879 outs() << " ";
5880 }
5881 if(name != nullptr)
5882 outs() << name;
5883 outs() << "\n";
5884 }
5885}
5886
5887static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
5888 uint32_t size, bool verbose,
5889 bool PrintXarHeader, bool PrintXarFileHeaders,
5890 std::string XarMemberName) {
5891 if(size < sizeof(struct xar_header)) {
5892 outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
5893 "of struct xar_header)\n";
5894 return;
5895 }
5896 struct xar_header XarHeader;
5897 memcpy(&XarHeader, sect, sizeof(struct xar_header));
5898 if (sys::IsLittleEndianHost)
5899 swapStruct(XarHeader);
5900 if (PrintXarHeader) {
5901 if (!XarMemberName.empty())
5902 outs() << "In xar member " << XarMemberName << ": ";
5903 else
5904 outs() << "For (__LLVM,__bundle) section: ";
5905 outs() << "xar header\n";
5906 if (XarHeader.magic == XAR_HEADER_MAGIC)
5907 outs() << " magic XAR_HEADER_MAGIC\n";
5908 else
5909 outs() << " magic "
5910 << format_hex(XarHeader.magic, 10, true)
5911 << " (not XAR_HEADER_MAGIC)\n";
5912 outs() << " size " << XarHeader.size << "\n";
5913 outs() << " version " << XarHeader.version << "\n";
5914 outs() << " toc_length_compressed " << XarHeader.toc_length_compressed
5915 << "\n";
5916 outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
5917 << "\n";
5918 outs() << " cksum_alg ";
5919 switch (XarHeader.cksum_alg) {
5920 case XAR_CKSUM_NONE:
5921 outs() << "XAR_CKSUM_NONE\n";
5922 break;
5923 case XAR_CKSUM_SHA1:
5924 outs() << "XAR_CKSUM_SHA1\n";
5925 break;
5926 case XAR_CKSUM_MD5:
5927 outs() << "XAR_CKSUM_MD5\n";
5928 break;
5929#ifdef XAR_CKSUM_SHA256
5930 case XAR_CKSUM_SHA256:
5931 outs() << "XAR_CKSUM_SHA256\n";
5932 break;
5933#endif
5934#ifdef XAR_CKSUM_SHA512
5935 case XAR_CKSUM_SHA512:
5936 outs() << "XAR_CKSUM_SHA512\n";
5937 break;
5938#endif
5939 default:
5940 outs() << XarHeader.cksum_alg << "\n";
5941 }
5942 }
5943
5944 SmallString<128> XarFilename;
5945 int FD;
5946 std::error_code XarEC =
5947 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
5948 if (XarEC) {
5949 errs() << XarEC.message() << "\n";
5950 return;
5951 }
5952 tool_output_file XarFile(XarFilename, FD);
5953 raw_fd_ostream &XarOut = XarFile.os();
5954 StringRef XarContents(sect, size);
5955 XarOut << XarContents;
5956 XarOut.close();
5957 if (XarOut.has_error())
5958 return;
5959
5960 xar_t xar = xar_open(XarFilename.c_str(), READ);
5961 if (!xar) {
5962 errs() << "Can't create temporary xar archive " << XarFilename << "\n";
5963 return;
5964 }
5965
5966 SmallString<128> TocFilename;
5967 std::error_code TocEC =
5968 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
5969 if (TocEC) {
5970 errs() << TocEC.message() << "\n";
5971 return;
5972 }
5973 xar_serialize(xar, TocFilename.c_str());
5974
5975 if (PrintXarFileHeaders) {
5976 if (!XarMemberName.empty())
5977 outs() << "In xar member " << XarMemberName << ": ";
5978 else
5979 outs() << "For (__LLVM,__bundle) section: ";
5980 outs() << "xar archive files:\n";
5981 PrintXarFilesSummary(XarFilename.c_str(), xar);
5982 }
5983
5984 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
5985 MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
5986 if (std::error_code EC = FileOrErr.getError()) {
5987 errs() << EC.message() << "\n";
5988 return;
5989 }
5990 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
5991
5992 if (!XarMemberName.empty())
5993 outs() << "In xar member " << XarMemberName << ": ";
5994 else
5995 outs() << "For (__LLVM,__bundle) section: ";
5996 outs() << "xar table of contents:\n";
5997 outs() << Buffer->getBuffer() << "\n";
5998
5999 // TODO: Go through the xar's files.
6000 xar_iter_t xi = xar_iter_new();
6001 if(!xi){
6002 errs() << "Can't obtain an xar iterator for xar archive "
6003 << XarFilename.c_str() << "\n";
6004 xar_close(xar);
6005 return;
6006 }
6007 for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
6008 const char *key;
6009 xar_iter_t xp;
6010 const char *member_name, *member_type, *member_size_string;
6011 size_t member_size;
6012
6013 xp = xar_iter_new();
6014 if(!xp){
6015 errs() << "Can't obtain an xar iterator for xar archive "
6016 << XarFilename.c_str() << "\n";
6017 xar_close(xar);
6018 return;
6019 }
6020 member_name = NULL__null;
6021 member_type = NULL__null;
6022 member_size_string = NULL__null;
6023 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
6024 const char *val = nullptr;
6025 xar_prop_get(xf, key, &val);
6026#if 0 // Useful for debugging.
6027 outs() << "key: " << key << " value: " << val << "\n";
6028#endif
6029 if(strcmp(key, "name") == 0)
6030 member_name = val;
6031 if(strcmp(key, "type") == 0)
6032 member_type = val;
6033 if(strcmp(key, "data/size") == 0)
6034 member_size_string = val;
6035 }
6036 /*
6037 * If we find a file with a name, date/size and type properties
6038 * and with the type being "file" see if that is a xar file.
6039 */
6040 if (member_name != NULL__null && member_type != NULL__null &&
6041 strcmp(member_type, "file") == 0 &&
6042 member_size_string != NULL__null){
6043 // Extract the file into a buffer.
6044 char *endptr;
6045 member_size = strtoul(member_size_string, &endptr, 10);
6046 if (*endptr == '\0' && member_size != 0) {
6047 char *buffer = (char *) ::operator new (member_size);
6048 if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
6049#if 0 // Useful for debugging.
6050 outs() << "xar member: " << member_name << " extracted\n";
6051#endif
6052 // Set the XarMemberName we want to see printed in the header.
6053 std::string OldXarMemberName;
6054 // If XarMemberName is already set this is nested. So
6055 // save the old name and create the nested name.
6056 if (!XarMemberName.empty()) {
6057 OldXarMemberName = XarMemberName;
6058 XarMemberName =
6059 (Twine("[") + XarMemberName + "]" + member_name).str();
6060 } else {
6061 OldXarMemberName = "";
6062 XarMemberName = member_name;
6063 }
6064 // See if this is could be a xar file (nested).
6065 if (member_size >= sizeof(struct xar_header)) {
6066#if 0 // Useful for debugging.
6067 outs() << "could be a xar file: " << member_name << "\n";
6068#endif
6069 memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
6070 if (sys::IsLittleEndianHost)
6071 swapStruct(XarHeader);
6072 if(XarHeader.magic == XAR_HEADER_MAGIC)
6073 DumpBitcodeSection(O, buffer, member_size, verbose,
6074 PrintXarHeader, PrintXarFileHeaders,
6075 XarMemberName);
6076 }
6077 XarMemberName = OldXarMemberName;
6078 }
6079 delete buffer;
6080 }
6081 }
6082 xar_iter_free(xp);
6083 }
6084 xar_close(xar);
6085}
6086#endif // defined(HAVE_LIBXAR)
6087
6088static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
6089 if (O->is64Bit())
6090 printObjc2_64bit_MetaData(O, verbose);
6091 else {
6092 MachO::mach_header H;
6093 H = O->getHeader();
6094 if (H.cputype == MachO::CPU_TYPE_ARM)
6095 printObjc2_32bit_MetaData(O, verbose);
6096 else {
6097 // This is the 32-bit non-arm cputype case. Which is normally
6098 // the first Objective-C ABI. But it may be the case of a
6099 // binary for the iOS simulator which is the second Objective-C
6100 // ABI. In that case printObjc1_32bit_MetaData() will determine that
6101 // and return false.
6102 if (!printObjc1_32bit_MetaData(O, verbose))
6103 printObjc2_32bit_MetaData(O, verbose);
6104 }
6105 }
6106}
6107
6108// GuessLiteralPointer returns a string which for the item in the Mach-O file
6109// for the address passed in as ReferenceValue for printing as a comment with
6110// the instruction and also returns the corresponding type of that item
6111// indirectly through ReferenceType.
6112//
6113// If ReferenceValue is an address of literal cstring then a pointer to the
6114// cstring is returned and ReferenceType is set to
6115// LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6116//
6117// If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6118// Class ref that name is returned and the ReferenceType is set accordingly.
6119//
6120// Lastly, literals which are Symbol address in a literal pool are looked for
6121// and if found the symbol name is returned and ReferenceType is set to
6122// LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6123//
6124// If there is no item in the Mach-O file for the address passed in as
6125// ReferenceValue nullptr is returned and ReferenceType is unchanged.
6126static const char *GuessLiteralPointer(uint64_t ReferenceValue,
6127 uint64_t ReferencePC,
6128 uint64_t *ReferenceType,
6129 struct DisassembleInfo *info) {
6130 // First see if there is an external relocation entry at the ReferencePC.
6131 if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
6132 uint64_t sect_addr = info->S.getAddress();
6133 uint64_t sect_offset = ReferencePC - sect_addr;
6134 bool reloc_found = false;
6135 DataRefImpl Rel;
6136 MachO::any_relocation_info RE;
6137 bool isExtern = false;
6138 SymbolRef Symbol;
6139 for (const RelocationRef &Reloc : info->S.relocations()) {
6140 uint64_t RelocOffset = Reloc.getOffset();
6141 if (RelocOffset == sect_offset) {
6142 Rel = Reloc.getRawDataRefImpl();
6143 RE = info->O->getRelocation(Rel);
6144 if (info->O->isRelocationScattered(RE))
6145 continue;
6146 isExtern = info->O->getPlainRelocationExternal(RE);
6147 if (isExtern) {
6148 symbol_iterator RelocSym = Reloc.getSymbol();
6149 Symbol = *RelocSym;
6150 }
6151 reloc_found = true;
6152 break;
6153 }
6154 }
6155 // If there is an external relocation entry for a symbol in a section
6156 // then used that symbol's value for the value of the reference.
6157 if (reloc_found && isExtern) {
6158 if (info->O->getAnyRelocationPCRel(RE)) {
6159 unsigned Type = info->O->getAnyRelocationType(RE);
6160 if (Type == MachO::X86_64_RELOC_SIGNED) {
6161 ReferenceValue = Symbol.getValue();
6162 }
6163 }
6164 }
6165 }
6166
6167 // Look for literals such as Objective-C CFStrings refs, Selector refs,
6168 // Message refs and Class refs.
6169 bool classref, selref, msgref, cfstring;
6170 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
6171 selref, msgref, cfstring);
6172 if (classref && pointer_value == 0) {
6173 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6174 // And the pointer_value in that section is typically zero as it will be
6175 // set by dyld as part of the "bind information".
6176 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
6177 if (name != nullptr) {
6178 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref8;
6179 const char *class_name = strrchr(name, '$');
6180 if (class_name != nullptr && class_name[1] == '_' &&
6181 class_name[2] != '\0') {
6182 info->class_name = class_name + 2;
6183 return name;
6184 }
6185 }
6186 }
6187
6188 if (classref) {
6189 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref8;
6190 const char *name =
6191 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
6192 if (name != nullptr)
6193 info->class_name = name;
6194 else
6195 name = "bad class ref";
6196 return name;
6197 }
6198
6199 if (cfstring) {
6200 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref4;
6201 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
6202 return name;
6203 }
6204
6205 if (selref && pointer_value == 0)
6206 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
6207
6208 if (pointer_value != 0)
6209 ReferenceValue = pointer_value;
6210
6211 const char *name = GuessCstringPointer(ReferenceValue, info);
6212 if (name) {
6213 if (pointer_value != 0 && selref) {
6214 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref7;
6215 info->selector_name = name;
6216 } else if (pointer_value != 0 && msgref) {
6217 info->class_name = nullptr;
6218 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref6;
6219 info->selector_name = name;
6220 } else
6221 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr3;
6222 return name;
6223 }
6224
6225 // Lastly look for an indirect symbol with this ReferenceValue which is in
6226 // a literal pool. If found return that symbol name.
6227 name = GuessIndirectSymbol(ReferenceValue, info);
6228 if (name) {
6229 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr2;
6230 return name;
6231 }
6232
6233 return nullptr;
6234}
6235
6236// SymbolizerSymbolLookUp is the symbol lookup function passed when creating
6237// the Symbolizer. It looks up the ReferenceValue using the info passed via the
6238// pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6239// is created and returns the symbol name that matches the ReferenceValue or
6240// nullptr if none. The ReferenceType is passed in for the IN type of
6241// reference the instruction is making from the values in defined in the header
6242// "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
6243// Out type and the ReferenceName will also be set which is added as a comment
6244// to the disassembled instruction.
6245//
6246// If the symbol name is a C++ mangled name then the demangled name is
6247// returned through ReferenceName and ReferenceType is set to
6248// LLVMDisassembler_ReferenceType_DeMangled_Name .
6249//
6250// When this is called to get a symbol name for a branch target then the
6251// ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
6252// SymbolValue will be looked for in the indirect symbol table to determine if
6253// it is an address for a symbol stub. If so then the symbol name for that
6254// stub is returned indirectly through ReferenceName and then ReferenceType is
6255// set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
6256//
6257// When this is called with an value loaded via a PC relative load then
6258// ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
6259// SymbolValue is checked to be an address of literal pointer, symbol pointer,
6260// or an Objective-C meta data reference. If so the output ReferenceType is
6261// set to correspond to that as well as setting the ReferenceName.
6262static const char *SymbolizerSymbolLookUp(void *DisInfo,
6263 uint64_t ReferenceValue,
6264 uint64_t *ReferenceType,
6265 uint64_t ReferencePC,
6266 const char **ReferenceName) {
6267 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
6268 // If no verbose symbolic information is wanted then just return nullptr.
6269 if (!info->verbose) {
6270 *ReferenceName = nullptr;
6271 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None0;
6272 return nullptr;
6273 }
6274
6275 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
6276
6277 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch1) {
6278 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
6279 if (*ReferenceName != nullptr) {
6280 method_reference(info, ReferenceType, ReferenceName);
6281 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message5)
6282 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub1;
6283 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
6284 if (info->demangled_name != nullptr)
6285 free(info->demangled_name);
6286 int status;
6287 info->demangled_name =
6288 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
6289 if (info->demangled_name != nullptr) {
6290 *ReferenceName = info->demangled_name;
6291 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name9;
6292 } else
6293 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None0;
6294 } else
6295 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None0;
6296 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load2) {
6297 *ReferenceName =
6298 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6299 if (*ReferenceName)
6300 method_reference(info, ReferenceType, ReferenceName);
6301 else
6302 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None0;
6303 // If this is arm64 and the reference is an adrp instruction save the
6304 // instruction, passed in ReferenceValue and the address of the instruction
6305 // for use later if we see and add immediate instruction.
6306 } else if (info->O->getArch() == Triple::aarch64 &&
6307 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP0x100000001) {
6308 info->adrp_inst = ReferenceValue;
6309 info->adrp_addr = ReferencePC;
6310 SymbolName = nullptr;
6311 *ReferenceName = nullptr;
6312 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None0;
6313 // If this is arm64 and reference is an add immediate instruction and we
6314 // have
6315 // seen an adrp instruction just before it and the adrp's Xd register
6316 // matches
6317 // this add's Xn register reconstruct the value being referenced and look to
6318 // see if it is a literal pointer. Note the add immediate instruction is
6319 // passed in ReferenceValue.
6320 } else if (info->O->getArch() == Triple::aarch64 &&
6321 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri0x100000002 &&
6322 ReferencePC - 4 == info->adrp_addr &&
6323 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6324 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6325 uint32_t addxri_inst;
6326 uint64_t adrp_imm, addxri_imm;
6327
6328 adrp_imm =
6329 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6330 if (info->adrp_inst & 0x0200000)
6331 adrp_imm |= 0xfffffffffc000000LL;
6332
6333 addxri_inst = ReferenceValue;
6334 addxri_imm = (addxri_inst >> 10) & 0xfff;
6335 if (((addxri_inst >> 22) & 0x3) == 1)
6336 addxri_imm <<= 12;
6337
6338 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6339 (adrp_imm << 12) + addxri_imm;
6340
6341 *ReferenceName =
6342 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6343 if (*ReferenceName == nullptr)
6344 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None0;
6345 // If this is arm64 and the reference is a load register instruction and we
6346 // have seen an adrp instruction just before it and the adrp's Xd register
6347 // matches this add's Xn register reconstruct the value being referenced and
6348 // look to see if it is a literal pointer. Note the load register
6349 // instruction is passed in ReferenceValue.
6350 } else if (info->O->getArch() == Triple::aarch64 &&
6351 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui0x100000003 &&
6352 ReferencePC - 4 == info->adrp_addr &&
6353 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6354 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6355 uint32_t ldrxui_inst;
6356 uint64_t adrp_imm, ldrxui_imm;
6357
6358 adrp_imm =
6359 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6360 if (info->adrp_inst & 0x0200000)
6361 adrp_imm |= 0xfffffffffc000000LL;
6362
6363 ldrxui_inst = ReferenceValue;
6364 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
6365
6366 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6367 (adrp_imm << 12) + (ldrxui_imm << 3);
6368
6369 *ReferenceName =
6370 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6371 if (*ReferenceName == nullptr)
6372 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None0;
6373 }
6374 // If this arm64 and is an load register (PC-relative) instruction the
6375 // ReferenceValue is the PC plus the immediate value.
6376 else if (info->O->getArch() == Triple::aarch64 &&
6377 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl0x100000004 ||
6378 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR0x100000005)) {
6379 *ReferenceName =
6380 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6381 if (*ReferenceName == nullptr)
6382 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None0;
6383 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
6384 if (info->demangled_name != nullptr)
6385 free(info->demangled_name);
6386 int status;
6387 info->demangled_name =
6388 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
6389 if (info->demangled_name != nullptr) {
6390 *ReferenceName = info->demangled_name;
6391 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name9;
6392 }
6393 }
6394 else {
6395 *ReferenceName = nullptr;
6396 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None0;
6397 }
6398
6399 return SymbolName;
6400}
6401
6402/// \brief Emits the comments that are stored in the CommentStream.
6403/// Each comment in the CommentStream must end with a newline.
6404static void emitComments(raw_svector_ostream &CommentStream,
6405 SmallString<128> &CommentsToEmit,
6406 formatted_raw_ostream &FormattedOS,
6407 const MCAsmInfo &MAI) {
6408 // Flush the stream before taking its content.
6409 StringRef Comments = CommentsToEmit.str();
6410 // Get the default information for printing a comment.
6411 StringRef CommentBegin = MAI.getCommentString();
6412 unsigned CommentColumn = MAI.getCommentColumn();
6413 bool IsFirst = true;
6414 while (!Comments.empty()) {
6415 if (!IsFirst)
6416 FormattedOS << '\n';
6417 // Emit a line of comments.
6418 FormattedOS.PadToColumn(CommentColumn);
6419 size_t Position = Comments.find('\n');
6420 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
6421 // Move after the newline character.
6422 Comments = Comments.substr(Position + 1);
6423 IsFirst = false;
6424 }
6425 FormattedOS.flush();
6426
6427 // Tell the comment stream that the vector changed underneath it.
6428 CommentsToEmit.clear();
6429}
6430
6431static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
6432 StringRef DisSegName, StringRef DisSectName) {
6433 const char *McpuDefault = nullptr;
6434 const Target *ThumbTarget = nullptr;
6435 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
6436 if (!TheTarget) {
6437 // GetTarget prints out stuff.
6438 return;
6439 }
6440 if (MCPU.empty() && McpuDefault)
6441 MCPU = McpuDefault;
6442
6443 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
6444 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
6445 if (ThumbTarget)
6446 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
6447
6448 // Package up features to be passed to target/subtarget
6449 std::string FeaturesStr;
6450 if (MAttrs.size()) {
6451 SubtargetFeatures Features;
6452 for (unsigned i = 0; i != MAttrs.size(); ++i)
6453 Features.AddFeature(MAttrs[i]);
6454 FeaturesStr = Features.getString();
6455 }
6456
6457 // Set up disassembler.
6458 std::unique_ptr<const MCRegisterInfo> MRI(
6459 TheTarget->createMCRegInfo(TripleName));
6460 std::unique_ptr<const MCAsmInfo> AsmInfo(
6461 TheTarget->createMCAsmInfo(*MRI, TripleName));
6462 std::unique_ptr<const MCSubtargetInfo> STI(
6463 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
6464 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
6465 std::unique_ptr<MCDisassembler> DisAsm(
6466 TheTarget->createMCDisassembler(*STI, Ctx));
6467 std::unique_ptr<MCSymbolizer> Symbolizer;
6468 struct DisassembleInfo SymbolizerInfo;
6469 std::unique_ptr<MCRelocationInfo> RelInfo(
6470 TheTarget->createMCRelocationInfo(TripleName, Ctx));
6471 if (RelInfo) {
6472 Symbolizer.reset(TheTarget->createMCSymbolizer(
6473 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
6474 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
6475 DisAsm->setSymbolizer(std::move(Symbolizer));
6476 }
6477 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
6478 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
6479 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
6480 // Set the display preference for hex vs. decimal immediates.
6481 IP->setPrintImmHex(PrintImmHex);
6482 // Comment stream and backing vector.
6483 SmallString<128> CommentsToEmit;
6484 raw_svector_ostream CommentStream(CommentsToEmit);
6485 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
6486 // if it is done then arm64 comments for string literals don't get printed
6487 // and some constant get printed instead and not setting it causes intel
6488 // (32-bit and 64-bit) comments printed with different spacing before the
6489 // comment causing different diffs with the 'C' disassembler library API.
6490 // IP->setCommentStream(CommentStream);
6491
6492 if (!AsmInfo || !STI || !DisAsm || !IP) {
6493 errs() << "error: couldn't initialize disassembler for target "
6494 << TripleName << '\n';
6495 return;
6496 }
6497
6498 // Set up separate thumb disassembler if needed.
6499 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
6500 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
6501 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
6502 std::unique_ptr<MCDisassembler> ThumbDisAsm;
6503 std::unique_ptr<MCInstPrinter> ThumbIP;
6504 std::unique_ptr<MCContext> ThumbCtx;
6505 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
6506 struct DisassembleInfo ThumbSymbolizerInfo;
6507 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
6508 if (ThumbTarget) {
6509 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
6510 ThumbAsmInfo.reset(
6511 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
6512 ThumbSTI.reset(
6513 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
6514 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
6515 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
6516 MCContext *PtrThumbCtx = ThumbCtx.get();
6517 ThumbRelInfo.reset(
6518 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
6519 if (ThumbRelInfo) {
6520 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6521 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
6522 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
6523 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6524 }
6525 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6526 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
6527 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6528 *ThumbInstrInfo, *ThumbMRI));
6529 // Set the display preference for hex vs. decimal immediates.
6530 ThumbIP->setPrintImmHex(PrintImmHex);
6531 }
6532
6533 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
6534 errs() << "error: couldn't initialize disassembler for target "
6535 << ThumbTripleName << '\n';
6536 return;
6537 }
6538
6539 MachO::mach_header Header = MachOOF->getHeader();
6540
6541 // FIXME: Using the -cfg command line option, this code used to be able to
6542 // annotate relocations with the referenced symbol's name, and if this was
6543 // inside a __[cf]string section, the data it points to. This is now replaced
6544 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
6545 std::vector<SectionRef> Sections;
6546 std::vector<SymbolRef> Symbols;
6547 SmallVector<uint64_t, 8> FoundFns;
6548 uint64_t BaseSegmentAddress;
6549
6550 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
6551 BaseSegmentAddress);
6552
6553 // Sort the symbols by address, just in case they didn't come in that way.
6554 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
6555
6556 // Build a data in code table that is sorted on by the address of each entry.
6557 uint64_t BaseAddress = 0;
6558 if (Header.filetype == MachO::MH_OBJECT)
6559 BaseAddress = Sections[0].getAddress();
6560 else
6561 BaseAddress = BaseSegmentAddress;
6562 DiceTable Dices;
6563 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
6564 DI != DE; ++DI) {
6565 uint32_t Offset;
6566 DI->getOffset(Offset);
6567 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6568 }
6569 array_pod_sort(Dices.begin(), Dices.end());
6570
6571#ifndef NDEBUG
6572 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6573#else
6574 raw_ostream &DebugOut = nulls();
6575#endif
6576
6577 std::unique_ptr<DIContext> diContext;
6578 ObjectFile *DbgObj = MachOOF;
6579 // Try to find debug info and set up the DIContext for it.
6580 if (UseDbg) {
6581 // A separate DSym file path was specified, parse it as a macho file,
6582 // get the sections and supply it to the section name parsing machinery.
6583 if (!DSYMFile.empty()) {
6584 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
6585 MemoryBuffer::getFileOrSTDIN(DSYMFile);
6586 if (std::error_code EC = BufOrErr.getError()) {
6587 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
6588 return;
6589 }
6590 DbgObj =
6591 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6592 .get()
6593 .release();
6594 }
6595
6596 // Setup the DIContext
6597 diContext.reset(new DWARFContextInMemory(*DbgObj));
6598 }
6599
6600 if (FilterSections.size() == 0)
6601 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
6602
6603 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
6604 StringRef SectName;
6605 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6606 continue;
6607
6608 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
6609
6610 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
6611 if (SegmentName != DisSegName)
6612 continue;
6613
6614 StringRef BytesStr;
6615 Sections[SectIdx].getContents(BytesStr);
6616 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6617 BytesStr.size());
6618 uint64_t SectAddress = Sections[SectIdx].getAddress();
6619
6620 bool symbolTableWorked = false;
6621
6622 // Create a map of symbol addresses to symbol names for use by
6623 // the SymbolizerSymbolLookUp() routine.
6624 SymbolAddressMap AddrMap;
6625 bool DisSymNameFound = false;
6626 for (const SymbolRef &Symbol : MachOOF->symbols()) {
6627 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
6628 if (!STOrErr)
6629 report_error(MachOOF->getFileName(), STOrErr.takeError());
6630 SymbolRef::Type ST = *STOrErr;
6631 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6632 ST == SymbolRef::ST_Other) {
6633 uint64_t Address = Symbol.getValue();
6634 Expected<StringRef> SymNameOrErr = Symbol.getName();
6635 if (!SymNameOrErr)
6636 report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
6637 StringRef SymName = *SymNameOrErr;
6638 AddrMap[Address] = SymName;
6639 if (!DisSymName.empty() && DisSymName == SymName)
6640 DisSymNameFound = true;
6641 }
6642 }
6643 if (!DisSymName.empty() && !DisSymNameFound) {
6644 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6645 return;
6646 }
6647 // Set up the block of info used by the Symbolizer call backs.
6648 SymbolizerInfo.verbose = !NoSymbolicOperands;
6649 SymbolizerInfo.O = MachOOF;
6650 SymbolizerInfo.S = Sections[SectIdx];
6651 SymbolizerInfo.AddrMap = &AddrMap;
6652 SymbolizerInfo.Sections = &Sections;
6653 SymbolizerInfo.class_name = nullptr;
6654 SymbolizerInfo.selector_name = nullptr;
6655 SymbolizerInfo.method = nullptr;
6656 SymbolizerInfo.demangled_name = nullptr;
6657 SymbolizerInfo.bindtable = nullptr;
6658 SymbolizerInfo.adrp_addr = 0;
6659 SymbolizerInfo.adrp_inst = 0;
6660 // Same for the ThumbSymbolizer
6661 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
6662 ThumbSymbolizerInfo.O = MachOOF;
6663 ThumbSymbolizerInfo.S = Sections[SectIdx];
6664 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6665 ThumbSymbolizerInfo.Sections = &Sections;
6666 ThumbSymbolizerInfo.class_name = nullptr;
6667 ThumbSymbolizerInfo.selector_name = nullptr;
6668 ThumbSymbolizerInfo.method = nullptr;
6669 ThumbSymbolizerInfo.demangled_name = nullptr;
6670 ThumbSymbolizerInfo.bindtable = nullptr;
6671 ThumbSymbolizerInfo.adrp_addr = 0;
6672 ThumbSymbolizerInfo.adrp_inst = 0;
6673
6674 unsigned int Arch = MachOOF->getArch();
6675
6676 // Skip all symbols if this is a stubs file.
6677 if (Bytes.size() == 0)
6678 return;
6679
6680 // If the section has symbols but no symbol at the start of the section
6681 // these are used to make sure the bytes before the first symbol are
6682 // disassembled.
6683 bool FirstSymbol = true;
6684 bool FirstSymbolAtSectionStart = true;
6685
6686 // Disassemble symbol by symbol.
6687 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
6688 Expected<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
6689 if (!SymNameOrErr)
6690 report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
6691 StringRef SymName = *SymNameOrErr;
6692
6693 Expected<SymbolRef::Type> STOrErr = Symbols[SymIdx].getType();
6694 if (!STOrErr)
6695 report_error(MachOOF->getFileName(), STOrErr.takeError());
6696 SymbolRef::Type ST = *STOrErr;
6697 if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
6698 continue;
6699
6700 // Make sure the symbol is defined in this section.
6701 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
6702 if (!containsSym) {
6703 if (!DisSymName.empty() && DisSymName == SymName) {
6704 outs() << "-dis-symname: " << DisSymName << " not in the section\n";
6705 return;
6706 }
6707 continue;
6708 }
6709 // The __mh_execute_header is special and we need to deal with that fact
6710 // this symbol is before the start of the (__TEXT,__text) section and at the
6711 // address of the start of the __TEXT segment. This is because this symbol
6712 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
6713 // start of the section in a standard MH_EXECUTE filetype.
6714 if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
6715 outs() << "-dis-symname: __mh_execute_header not in any section\n";
6716 return;
6717 }
6718 // When this code is trying to disassemble a symbol at a time and in the
6719 // case there is only the __mh_execute_header symbol left as in a stripped
6720 // executable, we need to deal with this by ignoring this symbol so the
6721 // whole section is disassembled and this symbol is then not displayed.
6722 if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
6723 SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
6724 SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
6725 continue;
6726
6727 // If we are only disassembling one symbol see if this is that symbol.
6728 if (!DisSymName.empty() && DisSymName != SymName)
6729 continue;
6730
6731 // Start at the address of the symbol relative to the section's address.
6732 uint64_t SectSize = Sections[SectIdx].getSize();
6733 uint64_t Start = Symbols[SymIdx].getValue();
6734 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6735 Start -= SectionAddress;
6736
6737 if (Start > SectSize) {
6738 outs() << "section data ends, " << SymName
6739 << " lies outside valid range\n";
6740 return;
6741 }
6742
6743 // Stop disassembling either at the beginning of the next symbol or at
6744 // the end of the section.
6745 bool containsNextSym = false;
6746 uint64_t NextSym = 0;
6747 uint64_t NextSymIdx = SymIdx + 1;
6748 while (Symbols.size() > NextSymIdx) {
6749 Expected<SymbolRef::Type> STOrErr = Symbols[NextSymIdx].getType();
6750 if (!STOrErr)
6751 report_error(MachOOF->getFileName(), STOrErr.takeError());
6752 SymbolRef::Type NextSymType = *STOrErr;
6753 if (NextSymType == SymbolRef::ST_Function) {
6754 containsNextSym =
6755 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
6756 NextSym = Symbols[NextSymIdx].getValue();
6757 NextSym -= SectionAddress;
6758 break;
6759 }
6760 ++NextSymIdx;
6761 }
6762
6763 uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
6764 uint64_t Size;
6765
6766 symbolTableWorked = true;
6767
6768 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6769 bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
6770
6771 // We only need the dedicated Thumb target if there's a real choice
6772 // (i.e. we're not targeting M-class) and the function is Thumb.
6773 bool UseThumbTarget = IsThumb && ThumbTarget;
6774
6775 // If we are not specifying a symbol to start disassembly with and this
6776 // is the first symbol in the section but not at the start of the section
6777 // then move the disassembly index to the start of the section and
6778 // don't print the symbol name just yet. This is so the bytes before the
6779 // first symbol are disassembled.
6780 uint64_t SymbolStart = Start;
6781 if (DisSymName.empty() && FirstSymbol && Start != 0) {
6782 FirstSymbolAtSectionStart = false;
6783 Start = 0;
6784 }
6785 else
6786 outs() << SymName << ":\n";
6787
6788 DILineInfo lastLine;
6789 for (uint64_t Index = Start; Index < End; Index += Size) {
6790 MCInst Inst;
6791
6792 // If this is the first symbol in the section and it was not at the
6793 // start of the section, see if we are at its Index now and if so print
6794 // the symbol name.
6795 if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart)
6796 outs() << SymName << ":\n";
6797
6798 uint64_t PC = SectAddress + Index;
6799 if (!NoLeadingAddr) {
6800 if (FullLeadingAddr) {
6801 if (MachOOF->is64Bit())
6802 outs() << format("%016" PRIx64"l" "x", PC);
6803 else
6804 outs() << format("%08" PRIx64"l" "x", PC);
6805 } else {
6806 outs() << format("%8" PRIx64"l" "x" ":", PC);
6807 }
6808 }
6809 if (!NoShowRawInsn || Arch == Triple::arm)
6810 outs() << "\t";
6811
6812 // Check the data in code table here to see if this is data not an
6813 // instruction to be disassembled.
6814 DiceTable Dice;
6815 Dice.push_back(std::make_pair(PC, DiceRef()));
6816 dice_table_iterator DTI =
6817 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6818 compareDiceTableEntries);
6819 if (DTI != Dices.end()) {
6820 uint16_t Length;
6821 DTI->second.getLength(Length);
6822 uint16_t Kind;
6823 DTI->second.getKind(Kind);
6824 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
6825 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6826 (PC == (DTI->first + Length - 1)) && (Length & 1))
6827 Size++;
6828 continue;
6829 }
6830
6831 SmallVector<char, 64> AnnotationsBytes;
6832 raw_svector_ostream Annotations(AnnotationsBytes);
6833
6834 bool gotInst;
6835 if (UseThumbTarget)
6836 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
6837 PC, DebugOut, Annotations);
6838 else
6839 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
6840 DebugOut, Annotations);
6841 if (gotInst) {
6842 if (!NoShowRawInsn || Arch == Triple::arm) {
6843 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
6844 }
6845 formatted_raw_ostream FormattedOS(outs());
6846 StringRef AnnotationsStr = Annotations.str();
6847 if (UseThumbTarget)
6848 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
6849 else
6850 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
6851 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
6852
6853 // Print debug info.
6854 if (diContext) {
6855 DILineInfo dli = diContext->getLineInfoForAddress(PC);
6856 // Print valid line info if it changed.
6857 if (dli != lastLine && dli.Line != 0)
6858 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6859 << dli.Column;
6860 lastLine = dli;
6861 }
6862 outs() << "\n";
6863 } else {
6864 unsigned int Arch = MachOOF->getArch();
6865 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6866 outs() << format("\t.byte 0x%02x #bad opcode\n",
6867 *(Bytes.data() + Index) & 0xff);
6868 Size = 1; // skip exactly one illegible byte and move on.
6869 } else if (Arch == Triple::aarch64 ||
6870 (Arch == Triple::arm && !IsThumb)) {
6871 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6872 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6873 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6874 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6875 outs() << format("\t.long\t0x%08x\n", opcode);
6876 Size = 4;
6877 } else if (Arch == Triple::arm) {
6878 assert(IsThumb && "ARM mode should have been dealt with above")((IsThumb && "ARM mode should have been dealt with above"
) ? static_cast<void> (0) : __assert_fail ("IsThumb && \"ARM mode should have been dealt with above\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/llvm-objdump/MachODump.cpp"
, 6878, __PRETTY_FUNCTION__))
;
6879 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6880 (*(Bytes.data() + Index + 1) & 0xff) << 8;
6881 outs() << format("\t.short\t0x%04x\n", opcode);
6882 Size = 2;
6883 } else{
6884 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6885 if (Size == 0)
6886 Size = 1; // skip illegible bytes
6887 }
6888 }
6889 }
6890 // Now that we are done disassembled the first symbol set the bool that
6891 // were doing this to false.
6892 FirstSymbol = false;
6893 }
6894 if (!symbolTableWorked) {
6895 // Reading the symbol table didn't work, disassemble the whole section.
6896 uint64_t SectAddress = Sections[SectIdx].getAddress();
6897 uint64_t SectSize = Sections[SectIdx].getSize();
6898 uint64_t InstSize;
6899 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
6900 MCInst Inst;
6901
6902 uint64_t PC = SectAddress + Index;
6903 SmallVector<char, 64> AnnotationsBytes;
6904 raw_svector_ostream Annotations(AnnotationsBytes);
6905 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6906 DebugOut, Annotations)) {
6907 if (!NoLeadingAddr) {
6908 if (FullLeadingAddr) {
6909 if (MachOOF->is64Bit())
6910 outs() << format("%016" PRIx64"l" "x", PC);
6911 else
6912 outs() << format("%08" PRIx64"l" "x", PC);
6913 } else {
6914 outs() << format("%8" PRIx64"l" "x" ":", PC);
6915 }
6916 }
6917 if (!NoShowRawInsn || Arch == Triple::arm) {
6918 outs() << "\t";
6919 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
6920 }
6921 StringRef AnnotationsStr = Annotations.str();
6922 IP->printInst(&Inst, outs(), AnnotationsStr, *STI);
6923 outs() << "\n";
6924 } else {
6925 unsigned int Arch = MachOOF->getArch();
6926 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6927 outs() << format("\t.byte 0x%02x #bad opcode\n",
6928 *(Bytes.data() + Index) & 0xff);
6929 InstSize = 1; // skip exactly one illegible byte and move on.
6930 } else {
6931 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6932 if (InstSize == 0)
6933 InstSize = 1; // skip illegible bytes
6934 }
6935 }
6936 }
6937 }
6938 // The TripleName's need to be reset if we are called again for a different
6939 // archtecture.
6940 TripleName = "";
6941 ThumbTripleName = "";
6942
6943 if (SymbolizerInfo.method != nullptr)
6944 free(SymbolizerInfo.method);
6945 if (SymbolizerInfo.demangled_name != nullptr)
6946 free(SymbolizerInfo.demangled_name);
6947 if (ThumbSymbolizerInfo.method != nullptr)
6948 free(ThumbSymbolizerInfo.method);
6949 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6950 free(ThumbSymbolizerInfo.demangled_name);
6951 }
6952}
6953
6954//===----------------------------------------------------------------------===//
6955// __compact_unwind section dumping
6956//===----------------------------------------------------------------------===//
6957
6958namespace {
6959
6960template <typename T> static uint64_t readNext(const char *&Buf) {
6961 using llvm::support::little;
6962 using llvm::support::unaligned;
6963
6964 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6965 Buf += sizeof(T);
6966 return Val;
6967}
6968
6969struct CompactUnwindEntry {
6970 uint32_t OffsetInSection;
6971
6972 uint64_t FunctionAddr;
6973 uint32_t Length;
6974 uint32_t CompactEncoding;
6975 uint64_t PersonalityAddr;
6976 uint64_t LSDAAddr;
6977
6978 RelocationRef FunctionReloc;
6979 RelocationRef PersonalityReloc;
6980 RelocationRef LSDAReloc;
6981
6982 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
6983 : OffsetInSection(Offset) {
6984 if (Is64)
6985 read<uint64_t>(Contents.data() + Offset);
6986 else
6987 read<uint32_t>(Contents.data() + Offset);
6988 }
6989
6990private:
6991 template <typename UIntPtr> void read(const char *Buf) {
6992 FunctionAddr = readNext<UIntPtr>(Buf);
6993 Length = readNext<uint32_t>(Buf);
6994 CompactEncoding = readNext<uint32_t>(Buf);
6995 PersonalityAddr = readNext<UIntPtr>(Buf);
6996 LSDAAddr = readNext<UIntPtr>(Buf);
6997 }
6998};
6999}
7000
7001/// Given a relocation from __compact_unwind, consisting of the RelocationRef
7002/// and data being relocated, determine the best base Name and Addend to use for
7003/// display purposes.
7004///
7005/// 1. An Extern relocation will directly reference a symbol (and the data is
7006/// then already an addend), so use that.
7007/// 2. Otherwise the data is an offset in the object file's layout; try to find
7008// a symbol before it in the same section, and use the offset from there.
7009/// 3. Finally, if all that fails, fall back to an offset from the start of the
7010/// referenced section.
7011static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
7012 std::map<uint64_t, SymbolRef> &Symbols,
7013 const RelocationRef &Reloc, uint64_t Addr,
7014 StringRef &Name, uint64_t &Addend) {
7015 if (Reloc.getSymbol() != Obj->symbol_end()) {
7016 Expected<StringRef> NameOrErr = Reloc.getSymbol()->getName();
7017 if (!NameOrErr)
7018 report_error(Obj->getFileName(), NameOrErr.takeError());
7019 Name = *NameOrErr;
7020 Addend = Addr;
7021 return;
7022 }
7023
7024 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
7025 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
7026
7027 uint64_t SectionAddr = RelocSection.getAddress();
7028
7029 auto Sym = Symbols.upper_bound(Addr);
7030 if (Sym == Symbols.begin()) {
7031 // The first symbol in the object is after this reference, the best we can
7032 // do is section-relative notation.
7033 RelocSection.getName(Name);
7034 Addend = Addr - SectionAddr;
7035 return;
7036 }
7037
7038 // Go back one so that SymbolAddress <= Addr.
7039 --Sym;
7040
7041 auto SectOrErr = Sym->second.getSection();
7042 if (!SectOrErr)
7043 report_error(Obj->getFileName(), SectOrErr.takeError());
7044 section_iterator SymSection = *SectOrErr;
7045 if (RelocSection == *SymSection) {
7046 // There's a valid symbol in the same section before this reference.
7047 Expected<StringRef> NameOrErr = Sym->second.getName();
7048 if (!NameOrErr)
7049 report_error(Obj->getFileName(), NameOrErr.takeError());
7050 Name = *NameOrErr;
7051 Addend = Addr - Sym->first;
7052 return;
7053 }
7054
7055 // There is a symbol before this reference, but it's in a different
7056 // section. Probably not helpful to mention it, so use the section name.
7057 RelocSection.getName(Name);
7058 Addend = Addr - SectionAddr;
7059}
7060
7061static void printUnwindRelocDest(const MachOObjectFile *Obj,
7062 std::map<uint64_t, SymbolRef> &Symbols,
7063 const RelocationRef &Reloc, uint64_t Addr) {
7064 StringRef Name;
7065 uint64_t Addend;
7066
7067 if (!Reloc.getObject())
7068 return;
7069
7070 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
7071
7072 outs() << Name;
7073 if (Addend)
7074 outs() << " + " << format("0x%" PRIx64"l" "x", Addend);
7075}
7076
7077static void
7078printMachOCompactUnwindSection(const MachOObjectFile *Obj,
7079 std::map<uint64_t, SymbolRef> &Symbols,
7080 const SectionRef &CompactUnwind) {
7081
7082 if (!Obj->isLittleEndian()) {
7083 outs() << "Skipping big-endian __compact_unwind section\n";
7084 return;
7085 }
7086
7087 bool Is64 = Obj->is64Bit();
7088 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
7089 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
7090
7091 StringRef Contents;
7092 CompactUnwind.getContents(Contents);
7093
7094 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
7095
7096 // First populate the initial raw offsets, encodings and so on from the entry.
7097 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
7098 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
7099 CompactUnwinds.push_back(Entry);
7100 }
7101
7102 // Next we need to look at the relocations to find out what objects are
7103 // actually being referred to.
7104 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
7105 uint64_t RelocAddress = Reloc.getOffset();
7106
7107 uint32_t EntryIdx = RelocAddress / EntrySize;
7108 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
7109 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
7110
7111 if (OffsetInEntry == 0)
7112 Entry.FunctionReloc = Reloc;
7113 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
7114 Entry.PersonalityReloc = Reloc;
7115 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
7116 Entry.LSDAReloc = Reloc;
7117 else {
7118 outs() << "Invalid relocation in __compact_unwind section\n";
7119 return;
7120 }
7121 }
7122
7123 // Finally, we're ready to print the data we've gathered.
7124 outs() << "Contents of __compact_unwind section:\n";
7125 for (auto &Entry : CompactUnwinds) {
7126 outs() << " Entry at offset "
7127 << format("0x%" PRIx32"x", Entry.OffsetInSection) << ":\n";
7128
7129 // 1. Start of the region this entry applies to.
7130 outs() << " start: " << format("0x%" PRIx64"l" "x",
7131 Entry.FunctionAddr) << ' ';
7132 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
7133 outs() << '\n';
7134
7135 // 2. Length of the region this entry applies to.
7136 outs() << " length: " << format("0x%" PRIx32"x", Entry.Length)
7137 << '\n';
7138 // 3. The 32-bit compact encoding.
7139 outs() << " compact encoding: "
7140 << format("0x%08" PRIx32"x", Entry.CompactEncoding) << '\n';
7141
7142 // 4. The personality function, if present.
7143 if (Entry.PersonalityReloc.getObject()) {
7144 outs() << " personality function: "
7145 << format("0x%" PRIx64"l" "x", Entry.PersonalityAddr) << ' ';
7146 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
7147 Entry.PersonalityAddr);
7148 outs() << '\n';
7149 }
7150
7151 // 5. This entry's language-specific data area.
7152 if (Entry.LSDAReloc.getObject()) {
7153 outs() << " LSDA: " << format("0x%" PRIx64"l" "x",
7154 Entry.LSDAAddr) << ' ';
7155 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
7156 outs() << '\n';
7157 }
7158 }
7159}
7160
7161//===----------------------------------------------------------------------===//
7162// __unwind_info section dumping
7163//===----------------------------------------------------------------------===//
7164
7165static void printRegularSecondLevelUnwindPage(const char *PageStart) {
7166 const char *Pos = PageStart;
7167 uint32_t Kind = readNext<uint32_t>(Pos);
7168 (void)Kind;
7169 assert(Kind == 2 && "kind for a regular 2nd level index should be 2")((Kind == 2 && "kind for a regular 2nd level index should be 2"
) ? static_cast<void> (0) : __assert_fail ("Kind == 2 && \"kind for a regular 2nd level index should be 2\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/llvm-objdump/MachODump.cpp"
, 7169, __PRETTY_FUNCTION__))
;
7170
7171 uint16_t EntriesStart = readNext<uint16_t>(Pos);
7172 uint16_t NumEntries = readNext<uint16_t>(Pos);
7173
7174 Pos = PageStart + EntriesStart;
7175 for (unsigned i = 0; i < NumEntries; ++i) {
7176 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7177 uint32_t Encoding = readNext<uint32_t>(Pos);
7178
7179 outs() << " [" << i << "]: "
7180 << "function offset=" << format("0x%08" PRIx32"x", FunctionOffset)
7181 << ", "
7182 << "encoding=" << format("0x%08" PRIx32"x", Encoding) << '\n';
7183 }
7184}
7185
7186static void printCompressedSecondLevelUnwindPage(
7187 const char *PageStart, uint32_t FunctionBase,
7188 const SmallVectorImpl<uint32_t> &CommonEncodings) {
7189 const char *Pos = PageStart;
7190 uint32_t Kind = readNext<uint32_t>(Pos);
7191 (void)Kind;
7192 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3")((Kind == 3 && "kind for a compressed 2nd level index should be 3"
) ? static_cast<void> (0) : __assert_fail ("Kind == 3 && \"kind for a compressed 2nd level index should be 3\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/llvm-objdump/MachODump.cpp"
, 7192, __PRETTY_FUNCTION__))
;
7193
7194 uint16_t EntriesStart = readNext<uint16_t>(Pos);
7195 uint16_t NumEntries = readNext<uint16_t>(Pos);
7196
7197 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
7198 readNext<uint16_t>(Pos);
7199 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
7200 PageStart + EncodingsStart);
7201
7202 Pos = PageStart + EntriesStart;
7203 for (unsigned i = 0; i < NumEntries; ++i) {
7204 uint32_t Entry = readNext<uint32_t>(Pos);
7205 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
7206 uint32_t EncodingIdx = Entry >> 24;
7207
7208 uint32_t Encoding;
7209 if (EncodingIdx < CommonEncodings.size())
7210 Encoding = CommonEncodings[EncodingIdx];
7211 else
7212 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
7213
7214 outs() << " [" << i << "]: "
7215 << "function offset=" << format("0x%08" PRIx32"x", FunctionOffset)
7216 << ", "
7217 << "encoding[" << EncodingIdx
7218 << "]=" << format("0x%08" PRIx32"x", Encoding) << '\n';
7219 }
7220}
7221
7222static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
7223 std::map<uint64_t, SymbolRef> &Symbols,
7224 const SectionRef &UnwindInfo) {
7225
7226 if (!Obj->isLittleEndian()) {
7227 outs() << "Skipping big-endian __unwind_info section\n";
7228 return;
7229 }
7230
7231 outs() << "Contents of __unwind_info section:\n";
7232
7233 StringRef Contents;
7234 UnwindInfo.getContents(Contents);
7235 const char *Pos = Contents.data();
7236
7237 //===----------------------------------
7238 // Section header
7239 //===----------------------------------
7240
7241 uint32_t Version = readNext<uint32_t>(Pos);
7242 outs() << " Version: "
7243 << format("0x%" PRIx32"x", Version) << '\n';
7244 if (Version != 1) {
7245 outs() << " Skipping section with unknown version\n";
7246 return;
7247 }
7248
7249 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
7250 outs() << " Common encodings array section offset: "
7251 << format("0x%" PRIx32"x", CommonEncodingsStart) << '\n';
7252 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
7253 outs() << " Number of common encodings in array: "
7254 << format("0x%" PRIx32"x", NumCommonEncodings) << '\n';
7255
7256 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
7257 outs() << " Personality function array section offset: "
7258 << format("0x%" PRIx32"x", PersonalitiesStart) << '\n';
7259 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
7260 outs() << " Number of personality functions in array: "
7261 << format("0x%" PRIx32"x", NumPersonalities) << '\n';
7262
7263 uint32_t IndicesStart = readNext<uint32_t>(Pos);
7264 outs() << " Index array section offset: "
7265 << format("0x%" PRIx32"x", IndicesStart) << '\n';
7266 uint32_t NumIndices = readNext<uint32_t>(Pos);
7267 outs() << " Number of indices in array: "
7268 << format("0x%" PRIx32"x", NumIndices) << '\n';
7269
7270 //===----------------------------------
7271 // A shared list of common encodings
7272 //===----------------------------------
7273
7274 // These occupy indices in the range [0, N] whenever an encoding is referenced
7275 // from a compressed 2nd level index table. In practice the linker only
7276 // creates ~128 of these, so that indices are available to embed encodings in
7277 // the 2nd level index.
7278
7279 SmallVector<uint32_t, 64> CommonEncodings;
7280 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
7281 Pos = Contents.data() + CommonEncodingsStart;
7282 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
7283 uint32_t Encoding = readNext<uint32_t>(Pos);
7284 CommonEncodings.push_back(Encoding);
7285
7286 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32"x", Encoding)
7287 << '\n';
7288 }
7289
7290 //===----------------------------------
7291 // Personality functions used in this executable
7292 //===----------------------------------
7293
7294 // There should be only a handful of these (one per source language,
7295 // roughly). Particularly since they only get 2 bits in the compact encoding.
7296
7297 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
7298 Pos = Contents.data() + PersonalitiesStart;
7299 for (unsigned i = 0; i < NumPersonalities; ++i) {
7300 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
7301 outs() << " personality[" << i + 1
7302 << "]: " << format("0x%08" PRIx32"x", PersonalityFn) << '\n';
7303 }
7304
7305 //===----------------------------------
7306 // The level 1 index entries
7307 //===----------------------------------
7308
7309 // These specify an approximate place to start searching for the more detailed
7310 // information, sorted by PC.
7311
7312 struct IndexEntry {
7313 uint32_t FunctionOffset;
7314 uint32_t SecondLevelPageStart;
7315 uint32_t LSDAStart;
7316 };
7317
7318 SmallVector<IndexEntry, 4> IndexEntries;
7319
7320 outs() << " Top level indices: (count = " << NumIndices << ")\n";
7321 Pos = Contents.data() + IndicesStart;
7322 for (unsigned i = 0; i < NumIndices; ++i) {
7323 IndexEntry Entry;
7324
7325 Entry.FunctionOffset = readNext<uint32_t>(Pos);
7326 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
7327 Entry.LSDAStart = readNext<uint32_t>(Pos);
7328 IndexEntries.push_back(Entry);
7329
7330 outs() << " [" << i << "]: "
7331 << "function offset=" << format("0x%08" PRIx32"x", Entry.FunctionOffset)
7332 << ", "
7333 << "2nd level page offset="
7334 << format("0x%08" PRIx32"x", Entry.SecondLevelPageStart) << ", "
7335 << "LSDA offset=" << format("0x%08" PRIx32"x", Entry.LSDAStart) << '\n';
7336 }
7337
7338 //===----------------------------------
7339 // Next come the LSDA tables
7340 //===----------------------------------
7341
7342 // The LSDA layout is rather implicit: it's a contiguous array of entries from
7343 // the first top-level index's LSDAOffset to the last (sentinel).
7344
7345 outs() << " LSDA descriptors:\n";
7346 Pos = Contents.data() + IndexEntries[0].LSDAStart;
7347 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
7348 (2 * sizeof(uint32_t));
7349 for (int i = 0; i < NumLSDAs; ++i) {
7350 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7351 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
7352 outs() << " [" << i << "]: "
7353 << "function offset=" << format("0x%08" PRIx32"x", FunctionOffset)
7354 << ", "
7355 << "LSDA offset=" << format("0x%08" PRIx32"x", LSDAOffset) << '\n';
7356 }
7357
7358 //===----------------------------------
7359 // Finally, the 2nd level indices
7360 //===----------------------------------
7361
7362 // Generally these are 4K in size, and have 2 possible forms:
7363 // + Regular stores up to 511 entries with disparate encodings
7364 // + Compressed stores up to 1021 entries if few enough compact encoding
7365 // values are used.
7366 outs() << " Second level indices:\n";
7367 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
7368 // The final sentinel top-level index has no associated 2nd level page
7369 if (IndexEntries[i].SecondLevelPageStart == 0)
7370 break;
7371
7372 outs() << " Second level index[" << i << "]: "
7373 << "offset in section="
7374 << format("0x%08" PRIx32"x", IndexEntries[i].SecondLevelPageStart)
7375 << ", "
7376 << "base function offset="
7377 << format("0x%08" PRIx32"x", IndexEntries[i].FunctionOffset) << '\n';
7378
7379 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
7380 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
7381 if (Kind == 2)
7382 printRegularSecondLevelUnwindPage(Pos);
7383 else if (Kind == 3)
7384 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
7385 CommonEncodings);
7386 else
7387 outs() << " Skipping 2nd level page with unknown kind " << Kind
7388 << '\n';
7389 }
7390}
7391
7392void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
7393 std::map<uint64_t, SymbolRef> Symbols;
7394 for (const SymbolRef &SymRef : Obj->symbols()) {
7395 // Discard any undefined or absolute symbols. They're not going to take part
7396 // in the convenience lookup for unwind info and just take up resources.
7397 auto SectOrErr = SymRef.getSection();
7398 if (!SectOrErr) {
7399 // TODO: Actually report errors helpfully.
7400 consumeError(SectOrErr.takeError());
7401 continue;
7402 }
7403 section_iterator Section = *SectOrErr;
7404 if (Section == Obj->section_end())
7405 continue;
7406
7407 uint64_t Addr = SymRef.getValue();
7408 Symbols.insert(std::make_pair(Addr, SymRef));
7409 }
7410
7411 for (const SectionRef &Section : Obj->sections()) {
7412 StringRef SectName;
7413 Section.getName(SectName);
7414 if (SectName == "__compact_unwind")
7415 printMachOCompactUnwindSection(Obj, Symbols, Section);
7416 else if (SectName == "__unwind_info")
7417 printMachOUnwindInfoSection(Obj, Symbols, Section);
7418 }
7419}
7420
7421static void PrintMachHeader(uint32_t magic, uint32_t cputype,
7422 uint32_t cpusubtype, uint32_t filetype,
7423 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
7424 bool verbose) {
7425 outs() << "Mach header\n";
7426 outs() << " magic cputype cpusubtype caps filetype ncmds "
7427 "sizeofcmds flags\n";
7428 if (verbose) {
7429 if (magic == MachO::MH_MAGIC)
7430 outs() << " MH_MAGIC";
7431 else if (magic == MachO::MH_MAGIC_64)
7432 outs() << "MH_MAGIC_64";
7433 else
7434 outs() << format(" 0x%08" PRIx32"x", magic);
7435 switch (cputype) {
7436 case MachO::CPU_TYPE_I386:
7437 outs() << " I386";
7438 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7439 case MachO::CPU_SUBTYPE_I386_ALL:
7440 outs() << " ALL";
7441 break;
7442 default:
7443 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7444 break;
7445 }
7446 break;
7447 case MachO::CPU_TYPE_X86_64:
7448 outs() << " X86_64";
7449 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7450 case MachO::CPU_SUBTYPE_X86_64_ALL:
7451 outs() << " ALL";
7452 break;
7453 case MachO::CPU_SUBTYPE_X86_64_H:
7454 outs() << " Haswell";
7455 break;
7456 default:
7457 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7458 break;
7459 }
7460 break;
7461 case MachO::CPU_TYPE_ARM:
7462 outs() << " ARM";
7463 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7464 case MachO::CPU_SUBTYPE_ARM_ALL:
7465 outs() << " ALL";
7466 break;
7467 case MachO::CPU_SUBTYPE_ARM_V4T:
7468 outs() << " V4T";
7469 break;
7470 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
7471 outs() << " V5TEJ";
7472 break;
7473 case MachO::CPU_SUBTYPE_ARM_XSCALE:
7474 outs() << " XSCALE";
7475 break;
7476 case MachO::CPU_SUBTYPE_ARM_V6:
7477 outs() << " V6";
7478 break;
7479 case MachO::CPU_SUBTYPE_ARM_V6M:
7480 outs() << " V6M";
7481 break;
7482 case MachO::CPU_SUBTYPE_ARM_V7:
7483 outs() << " V7";
7484 break;
7485 case MachO::CPU_SUBTYPE_ARM_V7EM:
7486 outs() << " V7EM";
7487 break;
7488 case MachO::CPU_SUBTYPE_ARM_V7K:
7489 outs() << " V7K";
7490 break;
7491 case MachO::CPU_SUBTYPE_ARM_V7M:
7492 outs() << " V7M";
7493 break;
7494 case MachO::CPU_SUBTYPE_ARM_V7S:
7495 outs() << " V7S";
7496 break;
7497 default:
7498 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7499 break;
7500 }
7501 break;
7502 case MachO::CPU_TYPE_ARM64:
7503 outs() << " ARM64";
7504 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7505 case MachO::CPU_SUBTYPE_ARM64_ALL:
7506 outs() << " ALL";
7507 break;
7508 default:
7509 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7510 break;
7511 }
7512 break;
7513 case MachO::CPU_TYPE_POWERPC:
7514 outs() << " PPC";
7515 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7516 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7517 outs() << " ALL";
7518 break;
7519 default:
7520 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7521 break;
7522 }
7523 break;
7524 case MachO::CPU_TYPE_POWERPC64:
7525 outs() << " PPC64";
7526 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7527 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7528 outs() << " ALL";
7529 break;
7530 default:
7531 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7532 break;
7533 }
7534 break;
7535 default:
7536 outs() << format(" %7d", cputype);
7537 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7538 break;
7539 }
7540 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
7541 outs() << " LIB64";
7542 } else {
7543 outs() << format(" 0x%02" PRIx32"x",
7544 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7545 }
7546 switch (filetype) {
7547 case MachO::MH_OBJECT:
7548 outs() << " OBJECT";
7549 break;
7550 case MachO::MH_EXECUTE:
7551 outs() << " EXECUTE";
7552 break;
7553 case MachO::MH_FVMLIB:
7554 outs() << " FVMLIB";
7555 break;
7556 case MachO::MH_CORE:
7557 outs() << " CORE";
7558 break;
7559 case MachO::MH_PRELOAD:
7560 outs() << " PRELOAD";
7561 break;
7562 case MachO::MH_DYLIB:
7563 outs() << " DYLIB";
7564 break;
7565 case MachO::MH_DYLIB_STUB:
7566 outs() << " DYLIB_STUB";
7567 break;
7568 case MachO::MH_DYLINKER:
7569 outs() << " DYLINKER";
7570 break;
7571 case MachO::MH_BUNDLE:
7572 outs() << " BUNDLE";
7573 break;
7574 case MachO::MH_DSYM:
7575 outs() << " DSYM";
7576 break;
7577 case MachO::MH_KEXT_BUNDLE:
7578 outs() << " KEXTBUNDLE";
7579 break;
7580 default:
7581 outs() << format(" %10u", filetype);
7582 break;
7583 }
7584 outs() << format(" %5u", ncmds);
7585 outs() << format(" %10u", sizeofcmds);
7586 uint32_t f = flags;
7587 if (f & MachO::MH_NOUNDEFS) {
7588 outs() << " NOUNDEFS";
7589 f &= ~MachO::MH_NOUNDEFS;
7590 }
7591 if (f & MachO::MH_INCRLINK) {
7592 outs() << " INCRLINK";
7593 f &= ~MachO::MH_INCRLINK;
7594 }
7595 if (f & MachO::MH_DYLDLINK) {
7596 outs() << " DYLDLINK";
7597 f &= ~MachO::MH_DYLDLINK;
7598 }
7599 if (f & MachO::MH_BINDATLOAD) {
7600 outs() << " BINDATLOAD";
7601 f &= ~MachO::MH_BINDATLOAD;
7602 }
7603 if (f & MachO::MH_PREBOUND) {
7604 outs() << " PREBOUND";
7605 f &= ~MachO::MH_PREBOUND;
7606 }
7607 if (f & MachO::MH_SPLIT_SEGS) {
7608 outs() << " SPLIT_SEGS";
7609 f &= ~MachO::MH_SPLIT_SEGS;
7610 }
7611 if (f & MachO::MH_LAZY_INIT) {
7612 outs() << " LAZY_INIT";
7613 f &= ~MachO::MH_LAZY_INIT;
7614 }
7615 if (f & MachO::MH_TWOLEVEL) {
7616 outs() << " TWOLEVEL";
7617 f &= ~MachO::MH_TWOLEVEL;
7618 }
7619 if (f & MachO::MH_FORCE_FLAT) {
7620 outs() << " FORCE_FLAT";
7621 f &= ~MachO::MH_FORCE_FLAT;
7622 }
7623 if (f & MachO::MH_NOMULTIDEFS) {
7624 outs() << " NOMULTIDEFS";
7625 f &= ~MachO::MH_NOMULTIDEFS;
7626 }
7627 if (f & MachO::MH_NOFIXPREBINDING) {
7628 outs() << " NOFIXPREBINDING";
7629 f &= ~MachO::MH_NOFIXPREBINDING;
7630 }
7631 if (f & MachO::MH_PREBINDABLE) {
7632 outs() << " PREBINDABLE";
7633 f &= ~MachO::MH_PREBINDABLE;
7634 }
7635 if (f & MachO::MH_ALLMODSBOUND) {
7636 outs() << " ALLMODSBOUND";
7637 f &= ~MachO::MH_ALLMODSBOUND;
7638 }
7639 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7640 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7641 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7642 }
7643 if (f & MachO::MH_CANONICAL) {
7644 outs() << " CANONICAL";
7645 f &= ~MachO::MH_CANONICAL;
7646 }
7647 if (f & MachO::MH_WEAK_DEFINES) {
7648 outs() << " WEAK_DEFINES";
7649 f &= ~MachO::MH_WEAK_DEFINES;
7650 }
7651 if (f & MachO::MH_BINDS_TO_WEAK) {
7652 outs() << " BINDS_TO_WEAK";
7653 f &= ~MachO::MH_BINDS_TO_WEAK;
7654 }
7655 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7656 outs() << " ALLOW_STACK_EXECUTION";
7657 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7658 }
7659 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7660 outs() << " DEAD_STRIPPABLE_DYLIB";
7661 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7662 }
7663 if (f & MachO::MH_PIE) {
7664 outs() << " PIE";
7665 f &= ~MachO::MH_PIE;
7666 }
7667 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7668 outs() << " NO_REEXPORTED_DYLIBS";
7669 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7670 }
7671 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7672 outs() << " MH_HAS_TLV_DESCRIPTORS";
7673 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7674 }
7675 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7676 outs() << " MH_NO_HEAP_EXECUTION";
7677 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7678 }
7679 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7680 outs() << " APP_EXTENSION_SAFE";
7681 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7682 }
7683 if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) {
7684 outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
7685 f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO;
7686 }
7687 if (f != 0 || flags == 0)
7688 outs() << format(" 0x%08" PRIx32"x", f);
7689 } else {
7690 outs() << format(" 0x%08" PRIx32"x", magic);
7691 outs() << format(" %7d", cputype);
7692 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7693 outs() << format(" 0x%02" PRIx32"x",
7694 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7695 outs() << format(" %10u", filetype);
7696 outs() << format(" %5u", ncmds);
7697 outs() << format(" %10u", sizeofcmds);
7698 outs() << format(" 0x%08" PRIx32"x", flags);
7699 }
7700 outs() << "\n";
7701}
7702
7703static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7704 StringRef SegName, uint64_t vmaddr,
7705 uint64_t vmsize, uint64_t fileoff,
7706 uint64_t filesize, uint32_t maxprot,
7707 uint32_t initprot, uint32_t nsects,
7708 uint32_t flags, uint32_t object_size,
7709 bool verbose) {
7710 uint64_t expected_cmdsize;
7711 if (cmd == MachO::LC_SEGMENT) {
7712 outs() << " cmd LC_SEGMENT\n";
7713 expected_cmdsize = nsects;
7714 expected_cmdsize *= sizeof(struct MachO::section);
7715 expected_cmdsize += sizeof(struct MachO::segment_command);
7716 } else {
7717 outs() << " cmd LC_SEGMENT_64\n";
7718 expected_cmdsize = nsects;
7719 expected_cmdsize *= sizeof(struct MachO::section_64);
7720 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7721 }
7722 outs() << " cmdsize " << cmdsize;
7723 if (cmdsize != expected_cmdsize)
7724 outs() << " Inconsistent size\n";
7725 else
7726 outs() << "\n";
7727 outs() << " segname " << SegName << "\n";
7728 if (cmd == MachO::LC_SEGMENT_64) {
7729 outs() << " vmaddr " << format("0x%016" PRIx64"l" "x", vmaddr) << "\n";
7730 outs() << " vmsize " << format("0x%016" PRIx64"l" "x", vmsize) << "\n";
7731 } else {
7732 outs() << " vmaddr " << format("0x%08" PRIx64"l" "x", vmaddr) << "\n";
7733 outs() << " vmsize " << format("0x%08" PRIx64"l" "x", vmsize) << "\n";
7734 }
7735 outs() << " fileoff " << fileoff;
7736 if (fileoff > object_size)
7737 outs() << " (past end of file)\n";
7738 else
7739 outs() << "\n";
7740 outs() << " filesize " << filesize;
7741 if (fileoff + filesize > object_size)
7742 outs() << " (past end of file)\n";
7743 else
7744 outs() << "\n";
7745 if (verbose) {
7746 if ((maxprot &
7747 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7748 MachO::VM_PROT_EXECUTE)) != 0)
7749 outs() << " maxprot ?" << format("0x%08" PRIx32"x", maxprot) << "\n";
7750 else {
7751 outs() << " maxprot ";
7752 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
7753 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7754 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
7755 }
7756 if ((initprot &
7757 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7758 MachO::VM_PROT_EXECUTE)) != 0)
7759 outs() << " initprot ?" << format("0x%08" PRIx32"x", initprot) << "\n";
7760 else {
7761 outs() << " initprot ";
7762 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
7763 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7764 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
7765 }
7766 } else {
7767 outs() << " maxprot " << format("0x%08" PRIx32"x", maxprot) << "\n";
7768 outs() << " initprot " << format("0x%08" PRIx32"x", initprot) << "\n";
7769 }
7770 outs() << " nsects " << nsects << "\n";
7771 if (verbose) {
7772 outs() << " flags";
7773 if (flags == 0)
7774 outs() << " (none)\n";
7775 else {
7776 if (flags & MachO::SG_HIGHVM) {
7777 outs() << " HIGHVM";
7778 flags &= ~MachO::SG_HIGHVM;
7779 }
7780 if (fl