[ARM] Tighten up VLDRH.32 with low alignments
[llvm-complete.git] / tools / llvm-objdump / MachODump.cpp
bloba3f3a9df4c124f866f9bbbd48f80f50c7b8ec7df
1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the MachO-specific dumper for llvm-objdump.
11 //===----------------------------------------------------------------------===//
13 #include "llvm-objdump.h"
14 #include "llvm-c/Disassembler.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/BinaryFormat/MachO.h"
19 #include "llvm/Config/config.h"
20 #include "llvm/DebugInfo/DIContext.h"
21 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
22 #include "llvm/Demangle/Demangle.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCInstPrinter.h"
28 #include "llvm/MC/MCInstrDesc.h"
29 #include "llvm/MC/MCInstrInfo.h"
30 #include "llvm/MC/MCRegisterInfo.h"
31 #include "llvm/MC/MCSubtargetInfo.h"
32 #include "llvm/Object/MachO.h"
33 #include "llvm/Object/MachOUniversal.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/Endian.h"
38 #include "llvm/Support/Format.h"
39 #include "llvm/Support/FormattedStream.h"
40 #include "llvm/Support/GraphWriter.h"
41 #include "llvm/Support/LEB128.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/TargetRegistry.h"
44 #include "llvm/Support/TargetSelect.h"
45 #include "llvm/Support/ToolOutputFile.h"
46 #include "llvm/Support/WithColor.h"
47 #include "llvm/Support/raw_ostream.h"
48 #include <algorithm>
49 #include <cstring>
50 #include <system_error>
52 #ifdef HAVE_LIBXAR
53 extern "C" {
54 #include <xar/xar.h>
56 #endif
58 using namespace llvm::object;
60 namespace llvm {
62 cl::OptionCategory MachOCat("llvm-objdump MachO Specific Options");
64 extern cl::opt<bool> ArchiveHeaders;
65 extern cl::opt<bool> Disassemble;
66 extern cl::opt<bool> DisassembleAll;
67 extern cl::opt<DIDumpType> DwarfDumpType;
68 extern cl::list<std::string> FilterSections;
69 extern cl::list<std::string> MAttrs;
70 extern cl::opt<std::string> MCPU;
71 extern cl::opt<bool> NoShowRawInsn;
72 extern cl::opt<bool> NoLeadingAddr;
73 extern cl::opt<bool> PrintImmHex;
74 extern cl::opt<bool> PrivateHeaders;
75 extern cl::opt<bool> Relocations;
76 extern cl::opt<bool> SectionHeaders;
77 extern cl::opt<bool> SectionContents;
78 extern cl::opt<bool> SymbolTable;
79 extern cl::opt<std::string> TripleName;
80 extern cl::opt<bool> UnwindInfo;
82 cl::opt<bool>
83 FirstPrivateHeader("private-header",
84 cl::desc("Display only the first format specific file "
85 "header"),
86 cl::cat(MachOCat));
88 cl::opt<bool> ExportsTrie("exports-trie",
89 cl::desc("Display mach-o exported symbols"),
90 cl::cat(MachOCat));
92 cl::opt<bool> Rebase("rebase", cl::desc("Display mach-o rebasing info"),
93 cl::cat(MachOCat));
95 cl::opt<bool> Bind("bind", cl::desc("Display mach-o binding info"),
96 cl::cat(MachOCat));
98 cl::opt<bool> LazyBind("lazy-bind",
99 cl::desc("Display mach-o lazy binding info"),
100 cl::cat(MachOCat));
102 cl::opt<bool> WeakBind("weak-bind",
103 cl::desc("Display mach-o weak binding info"),
104 cl::cat(MachOCat));
106 static cl::opt<bool>
107 UseDbg("g", cl::Grouping,
108 cl::desc("Print line information from debug info if available"),
109 cl::cat(MachOCat));
111 static cl::opt<std::string> DSYMFile("dsym",
112 cl::desc("Use .dSYM file for debug info"),
113 cl::cat(MachOCat));
115 static cl::opt<bool> FullLeadingAddr("full-leading-addr",
116 cl::desc("Print full leading address"),
117 cl::cat(MachOCat));
119 static cl::opt<bool> NoLeadingHeaders("no-leading-headers",
120 cl::desc("Print no leading headers"),
121 cl::cat(MachOCat));
123 cl::opt<bool> UniversalHeaders("universal-headers",
124 cl::desc("Print Mach-O universal headers "
125 "(requires -macho)"),
126 cl::cat(MachOCat));
128 cl::opt<bool>
129 ArchiveMemberOffsets("archive-member-offsets",
130 cl::desc("Print the offset to each archive member for "
131 "Mach-O archives (requires -macho and "
132 "-archive-headers)"),
133 cl::cat(MachOCat));
135 cl::opt<bool> IndirectSymbols("indirect-symbols",
136 cl::desc("Print indirect symbol table for Mach-O "
137 "objects (requires -macho)"),
138 cl::cat(MachOCat));
140 cl::opt<bool>
141 DataInCode("data-in-code",
142 cl::desc("Print the data in code table for Mach-O objects "
143 "(requires -macho)"),
144 cl::cat(MachOCat));
146 cl::opt<bool> LinkOptHints("link-opt-hints",
147 cl::desc("Print the linker optimization hints for "
148 "Mach-O objects (requires -macho)"),
149 cl::cat(MachOCat));
151 cl::opt<bool> InfoPlist("info-plist",
152 cl::desc("Print the info plist section as strings for "
153 "Mach-O objects (requires -macho)"),
154 cl::cat(MachOCat));
156 cl::opt<bool> DylibsUsed("dylibs-used",
157 cl::desc("Print the shared libraries used for linked "
158 "Mach-O files (requires -macho)"),
159 cl::cat(MachOCat));
161 cl::opt<bool>
162 DylibId("dylib-id",
163 cl::desc("Print the shared library's id for the dylib Mach-O "
164 "file (requires -macho)"),
165 cl::cat(MachOCat));
167 cl::opt<bool>
168 NonVerbose("non-verbose",
169 cl::desc("Print the info for Mach-O objects in "
170 "non-verbose or numeric form (requires -macho)"),
171 cl::cat(MachOCat));
173 cl::opt<bool>
174 ObjcMetaData("objc-meta-data",
175 cl::desc("Print the Objective-C runtime meta data for "
176 "Mach-O files (requires -macho)"),
177 cl::cat(MachOCat));
179 cl::opt<std::string> DisSymName(
180 "dis-symname",
181 cl::desc("disassemble just this symbol's instructions (requires -macho)"),
182 cl::cat(MachOCat));
184 static cl::opt<bool> NoSymbolicOperands(
185 "no-symbolic-operands",
186 cl::desc("do not symbolic operands when disassembling (requires -macho)"),
187 cl::cat(MachOCat));
189 static cl::list<std::string>
190 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
191 cl::ZeroOrMore, cl::cat(MachOCat));
193 bool ArchAll = false;
195 static std::string ThumbTripleName;
197 static const Target *GetTarget(const MachOObjectFile *MachOObj,
198 const char **McpuDefault,
199 const Target **ThumbTarget) {
200 // Figure out the target triple.
201 Triple TT(TripleName);
202 if (TripleName.empty()) {
203 TT = MachOObj->getArchTriple(McpuDefault);
204 TripleName = TT.str();
207 if (TT.getArch() == Triple::arm) {
208 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
209 // that support ARM are also capable of Thumb mode.
210 Triple ThumbTriple = TT;
211 std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
212 ThumbTriple.setArchName(ThumbName);
213 ThumbTripleName = ThumbTriple.str();
216 // Get the target specific parser.
217 std::string Error;
218 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
219 if (TheTarget && ThumbTripleName.empty())
220 return TheTarget;
222 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
223 if (*ThumbTarget)
224 return TheTarget;
226 WithColor::error(errs(), "llvm-objdump") << "unable to get target for '";
227 if (!TheTarget)
228 errs() << TripleName;
229 else
230 errs() << ThumbTripleName;
231 errs() << "', see --version and --triple.\n";
232 return nullptr;
235 struct SymbolSorter {
236 bool operator()(const SymbolRef &A, const SymbolRef &B) {
237 Expected<SymbolRef::Type> ATypeOrErr = A.getType();
238 if (!ATypeOrErr)
239 report_error(ATypeOrErr.takeError(), A.getObject()->getFileName());
240 SymbolRef::Type AType = *ATypeOrErr;
241 Expected<SymbolRef::Type> BTypeOrErr = B.getType();
242 if (!BTypeOrErr)
243 report_error(BTypeOrErr.takeError(), B.getObject()->getFileName());
244 SymbolRef::Type BType = *BTypeOrErr;
245 uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue();
246 uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue();
247 return AAddr < BAddr;
251 // Types for the storted data in code table that is built before disassembly
252 // and the predicate function to sort them.
253 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
254 typedef std::vector<DiceTableEntry> DiceTable;
255 typedef DiceTable::iterator dice_table_iterator;
257 #ifdef HAVE_LIBXAR
258 namespace {
259 struct ScopedXarFile {
260 xar_t xar;
261 ScopedXarFile(const char *filename, int32_t flags)
262 : xar(xar_open(filename, flags)) {}
263 ~ScopedXarFile() {
264 if (xar)
265 xar_close(xar);
267 ScopedXarFile(const ScopedXarFile &) = delete;
268 ScopedXarFile &operator=(const ScopedXarFile &) = delete;
269 operator xar_t() { return xar; }
272 struct ScopedXarIter {
273 xar_iter_t iter;
274 ScopedXarIter() : iter(xar_iter_new()) {}
275 ~ScopedXarIter() {
276 if (iter)
277 xar_iter_free(iter);
279 ScopedXarIter(const ScopedXarIter &) = delete;
280 ScopedXarIter &operator=(const ScopedXarIter &) = delete;
281 operator xar_iter_t() { return iter; }
283 } // namespace
284 #endif // defined(HAVE_LIBXAR)
286 // This is used to search for a data in code table entry for the PC being
287 // disassembled. The j parameter has the PC in j.first. A single data in code
288 // table entry can cover many bytes for each of its Kind's. So if the offset,
289 // aka the i.first value, of the data in code table entry plus its Length
290 // covers the PC being searched for this will return true. If not it will
291 // return false.
292 static bool compareDiceTableEntries(const DiceTableEntry &i,
293 const DiceTableEntry &j) {
294 uint16_t Length;
295 i.second.getLength(Length);
297 return j.first >= i.first && j.first < i.first + Length;
300 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
301 unsigned short Kind) {
302 uint32_t Value, Size = 1;
304 switch (Kind) {
305 default:
306 case MachO::DICE_KIND_DATA:
307 if (Length >= 4) {
308 if (!NoShowRawInsn)
309 dumpBytes(makeArrayRef(bytes, 4), outs());
310 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
311 outs() << "\t.long " << Value;
312 Size = 4;
313 } else if (Length >= 2) {
314 if (!NoShowRawInsn)
315 dumpBytes(makeArrayRef(bytes, 2), outs());
316 Value = bytes[1] << 8 | bytes[0];
317 outs() << "\t.short " << Value;
318 Size = 2;
319 } else {
320 if (!NoShowRawInsn)
321 dumpBytes(makeArrayRef(bytes, 2), outs());
322 Value = bytes[0];
323 outs() << "\t.byte " << Value;
324 Size = 1;
326 if (Kind == MachO::DICE_KIND_DATA)
327 outs() << "\t@ KIND_DATA\n";
328 else
329 outs() << "\t@ data in code kind = " << Kind << "\n";
330 break;
331 case MachO::DICE_KIND_JUMP_TABLE8:
332 if (!NoShowRawInsn)
333 dumpBytes(makeArrayRef(bytes, 1), outs());
334 Value = bytes[0];
335 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
336 Size = 1;
337 break;
338 case MachO::DICE_KIND_JUMP_TABLE16:
339 if (!NoShowRawInsn)
340 dumpBytes(makeArrayRef(bytes, 2), outs());
341 Value = bytes[1] << 8 | bytes[0];
342 outs() << "\t.short " << format("%5u", Value & 0xffff)
343 << "\t@ KIND_JUMP_TABLE16\n";
344 Size = 2;
345 break;
346 case MachO::DICE_KIND_JUMP_TABLE32:
347 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
348 if (!NoShowRawInsn)
349 dumpBytes(makeArrayRef(bytes, 4), outs());
350 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
351 outs() << "\t.long " << Value;
352 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
353 outs() << "\t@ KIND_JUMP_TABLE32\n";
354 else
355 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
356 Size = 4;
357 break;
359 return Size;
362 static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
363 std::vector<SectionRef> &Sections,
364 std::vector<SymbolRef> &Symbols,
365 SmallVectorImpl<uint64_t> &FoundFns,
366 uint64_t &BaseSegmentAddress) {
367 const StringRef FileName = MachOObj->getFileName();
368 for (const SymbolRef &Symbol : MachOObj->symbols()) {
369 StringRef SymName = unwrapOrError(Symbol.getName(), FileName);
370 if (!SymName.startswith("ltmp"))
371 Symbols.push_back(Symbol);
374 for (const SectionRef &Section : MachOObj->sections()) {
375 StringRef SectName;
376 Section.getName(SectName);
377 Sections.push_back(Section);
380 bool BaseSegmentAddressSet = false;
381 for (const auto &Command : MachOObj->load_commands()) {
382 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
383 // We found a function starts segment, parse the addresses for later
384 // consumption.
385 MachO::linkedit_data_command LLC =
386 MachOObj->getLinkeditDataLoadCommand(Command);
388 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
389 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
390 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
391 StringRef SegName = SLC.segname;
392 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
393 BaseSegmentAddressSet = true;
394 BaseSegmentAddress = SLC.vmaddr;
396 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
397 MachO::segment_command_64 SLC = MachOObj->getSegment64LoadCommand(Command);
398 StringRef SegName = SLC.segname;
399 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
400 BaseSegmentAddressSet = true;
401 BaseSegmentAddress = SLC.vmaddr;
407 static bool DumpAndSkipDataInCode(uint64_t PC, const uint8_t *bytes,
408 DiceTable &Dices, uint64_t &InstSize) {
409 // Check the data in code table here to see if this is data not an
410 // instruction to be disassembled.
411 DiceTable Dice;
412 Dice.push_back(std::make_pair(PC, DiceRef()));
413 dice_table_iterator DTI =
414 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
415 compareDiceTableEntries);
416 if (DTI != Dices.end()) {
417 uint16_t Length;
418 DTI->second.getLength(Length);
419 uint16_t Kind;
420 DTI->second.getKind(Kind);
421 InstSize = DumpDataInCode(bytes, Length, Kind);
422 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
423 (PC == (DTI->first + Length - 1)) && (Length & 1))
424 InstSize++;
425 return true;
427 return false;
430 static void printRelocationTargetName(const MachOObjectFile *O,
431 const MachO::any_relocation_info &RE,
432 raw_string_ostream &Fmt) {
433 // Target of a scattered relocation is an address. In the interest of
434 // generating pretty output, scan through the symbol table looking for a
435 // symbol that aligns with that address. If we find one, print it.
436 // Otherwise, we just print the hex address of the target.
437 const StringRef FileName = O->getFileName();
438 if (O->isRelocationScattered(RE)) {
439 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
441 for (const SymbolRef &Symbol : O->symbols()) {
442 uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName);
443 if (Addr != Val)
444 continue;
445 Fmt << unwrapOrError(Symbol.getName(), FileName);
446 return;
449 // If we couldn't find a symbol that this relocation refers to, try
450 // to find a section beginning instead.
451 for (const SectionRef &Section : ToolSectionFilter(*O)) {
452 StringRef Name;
453 uint64_t Addr = Section.getAddress();
454 if (Addr != Val)
455 continue;
456 if (std::error_code EC = Section.getName(Name))
457 report_error(errorCodeToError(EC), O->getFileName());
458 Fmt << Name;
459 return;
462 Fmt << format("0x%x", Val);
463 return;
466 StringRef S;
467 bool isExtern = O->getPlainRelocationExternal(RE);
468 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
470 if (O->getAnyRelocationType(RE) == MachO::ARM64_RELOC_ADDEND) {
471 Fmt << format("0x%0" PRIx64, Val);
472 return;
475 if (isExtern) {
476 symbol_iterator SI = O->symbol_begin();
477 advance(SI, Val);
478 S = unwrapOrError(SI->getName(), FileName);
479 } else {
480 section_iterator SI = O->section_begin();
481 // Adjust for the fact that sections are 1-indexed.
482 if (Val == 0) {
483 Fmt << "0 (?,?)";
484 return;
486 uint32_t I = Val - 1;
487 while (I != 0 && SI != O->section_end()) {
488 --I;
489 advance(SI, 1);
491 if (SI == O->section_end())
492 Fmt << Val << " (?,?)";
493 else
494 SI->getName(S);
497 Fmt << S;
500 Error getMachORelocationValueString(const MachOObjectFile *Obj,
501 const RelocationRef &RelRef,
502 SmallVectorImpl<char> &Result) {
503 DataRefImpl Rel = RelRef.getRawDataRefImpl();
504 MachO::any_relocation_info RE = Obj->getRelocation(Rel);
506 unsigned Arch = Obj->getArch();
508 std::string FmtBuf;
509 raw_string_ostream Fmt(FmtBuf);
510 unsigned Type = Obj->getAnyRelocationType(RE);
511 bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
513 // Determine any addends that should be displayed with the relocation.
514 // These require decoding the relocation type, which is triple-specific.
516 // X86_64 has entirely custom relocation types.
517 if (Arch == Triple::x86_64) {
518 switch (Type) {
519 case MachO::X86_64_RELOC_GOT_LOAD:
520 case MachO::X86_64_RELOC_GOT: {
521 printRelocationTargetName(Obj, RE, Fmt);
522 Fmt << "@GOT";
523 if (IsPCRel)
524 Fmt << "PCREL";
525 break;
527 case MachO::X86_64_RELOC_SUBTRACTOR: {
528 DataRefImpl RelNext = Rel;
529 Obj->moveRelocationNext(RelNext);
530 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
532 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
533 // X86_64_RELOC_UNSIGNED.
534 // NOTE: Scattered relocations don't exist on x86_64.
535 unsigned RType = Obj->getAnyRelocationType(RENext);
536 if (RType != MachO::X86_64_RELOC_UNSIGNED)
537 report_error(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after "
538 "X86_64_RELOC_SUBTRACTOR.");
540 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
541 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
542 printRelocationTargetName(Obj, RENext, Fmt);
543 Fmt << "-";
544 printRelocationTargetName(Obj, RE, Fmt);
545 break;
547 case MachO::X86_64_RELOC_TLV:
548 printRelocationTargetName(Obj, RE, Fmt);
549 Fmt << "@TLV";
550 if (IsPCRel)
551 Fmt << "P";
552 break;
553 case MachO::X86_64_RELOC_SIGNED_1:
554 printRelocationTargetName(Obj, RE, Fmt);
555 Fmt << "-1";
556 break;
557 case MachO::X86_64_RELOC_SIGNED_2:
558 printRelocationTargetName(Obj, RE, Fmt);
559 Fmt << "-2";
560 break;
561 case MachO::X86_64_RELOC_SIGNED_4:
562 printRelocationTargetName(Obj, RE, Fmt);
563 Fmt << "-4";
564 break;
565 default:
566 printRelocationTargetName(Obj, RE, Fmt);
567 break;
569 // X86 and ARM share some relocation types in common.
570 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
571 Arch == Triple::ppc) {
572 // Generic relocation types...
573 switch (Type) {
574 case MachO::GENERIC_RELOC_PAIR: // prints no info
575 return Error::success();
576 case MachO::GENERIC_RELOC_SECTDIFF: {
577 DataRefImpl RelNext = Rel;
578 Obj->moveRelocationNext(RelNext);
579 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
581 // X86 sect diff's must be followed by a relocation of type
582 // GENERIC_RELOC_PAIR.
583 unsigned RType = Obj->getAnyRelocationType(RENext);
585 if (RType != MachO::GENERIC_RELOC_PAIR)
586 report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
587 "GENERIC_RELOC_SECTDIFF.");
589 printRelocationTargetName(Obj, RE, Fmt);
590 Fmt << "-";
591 printRelocationTargetName(Obj, RENext, Fmt);
592 break;
596 if (Arch == Triple::x86 || Arch == Triple::ppc) {
597 switch (Type) {
598 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
599 DataRefImpl RelNext = Rel;
600 Obj->moveRelocationNext(RelNext);
601 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
603 // X86 sect diff's must be followed by a relocation of type
604 // GENERIC_RELOC_PAIR.
605 unsigned RType = Obj->getAnyRelocationType(RENext);
606 if (RType != MachO::GENERIC_RELOC_PAIR)
607 report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
608 "GENERIC_RELOC_LOCAL_SECTDIFF.");
610 printRelocationTargetName(Obj, RE, Fmt);
611 Fmt << "-";
612 printRelocationTargetName(Obj, RENext, Fmt);
613 break;
615 case MachO::GENERIC_RELOC_TLV: {
616 printRelocationTargetName(Obj, RE, Fmt);
617 Fmt << "@TLV";
618 if (IsPCRel)
619 Fmt << "P";
620 break;
622 default:
623 printRelocationTargetName(Obj, RE, Fmt);
625 } else { // ARM-specific relocations
626 switch (Type) {
627 case MachO::ARM_RELOC_HALF:
628 case MachO::ARM_RELOC_HALF_SECTDIFF: {
629 // Half relocations steal a bit from the length field to encode
630 // whether this is an upper16 or a lower16 relocation.
631 bool isUpper = (Obj->getAnyRelocationLength(RE) & 0x1) == 1;
633 if (isUpper)
634 Fmt << ":upper16:(";
635 else
636 Fmt << ":lower16:(";
637 printRelocationTargetName(Obj, RE, Fmt);
639 DataRefImpl RelNext = Rel;
640 Obj->moveRelocationNext(RelNext);
641 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
643 // ARM half relocs must be followed by a relocation of type
644 // ARM_RELOC_PAIR.
645 unsigned RType = Obj->getAnyRelocationType(RENext);
646 if (RType != MachO::ARM_RELOC_PAIR)
647 report_error(Obj->getFileName(), "Expected ARM_RELOC_PAIR after "
648 "ARM_RELOC_HALF");
650 // NOTE: The half of the target virtual address is stashed in the
651 // address field of the secondary relocation, but we can't reverse
652 // engineer the constant offset from it without decoding the movw/movt
653 // instruction to find the other half in its immediate field.
655 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
656 // symbol/section pointer of the follow-on relocation.
657 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
658 Fmt << "-";
659 printRelocationTargetName(Obj, RENext, Fmt);
662 Fmt << ")";
663 break;
665 default: {
666 printRelocationTargetName(Obj, RE, Fmt);
670 } else
671 printRelocationTargetName(Obj, RE, Fmt);
673 Fmt.flush();
674 Result.append(FmtBuf.begin(), FmtBuf.end());
675 return Error::success();
678 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
679 uint32_t n, uint32_t count,
680 uint32_t stride, uint64_t addr) {
681 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
682 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
683 if (n > nindirectsyms)
684 outs() << " (entries start past the end of the indirect symbol "
685 "table) (reserved1 field greater than the table size)";
686 else if (n + count > nindirectsyms)
687 outs() << " (entries extends past the end of the indirect symbol "
688 "table)";
689 outs() << "\n";
690 uint32_t cputype = O->getHeader().cputype;
691 if (cputype & MachO::CPU_ARCH_ABI64)
692 outs() << "address index";
693 else
694 outs() << "address index";
695 if (verbose)
696 outs() << " name\n";
697 else
698 outs() << "\n";
699 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
700 if (cputype & MachO::CPU_ARCH_ABI64)
701 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
702 else
703 outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " ";
704 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
705 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
706 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
707 outs() << "LOCAL\n";
708 continue;
710 if (indirect_symbol ==
711 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
712 outs() << "LOCAL ABSOLUTE\n";
713 continue;
715 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
716 outs() << "ABSOLUTE\n";
717 continue;
719 outs() << format("%5u ", indirect_symbol);
720 if (verbose) {
721 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
722 if (indirect_symbol < Symtab.nsyms) {
723 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
724 SymbolRef Symbol = *Sym;
725 outs() << unwrapOrError(Symbol.getName(), O->getFileName());
726 } else {
727 outs() << "?";
730 outs() << "\n";
734 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
735 for (const auto &Load : O->load_commands()) {
736 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
737 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
738 for (unsigned J = 0; J < Seg.nsects; ++J) {
739 MachO::section_64 Sec = O->getSection64(Load, J);
740 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
741 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
742 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
743 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
744 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
745 section_type == MachO::S_SYMBOL_STUBS) {
746 uint32_t stride;
747 if (section_type == MachO::S_SYMBOL_STUBS)
748 stride = Sec.reserved2;
749 else
750 stride = 8;
751 if (stride == 0) {
752 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
753 << Sec.sectname << ") "
754 << "(size of stubs in reserved2 field is zero)\n";
755 continue;
757 uint32_t count = Sec.size / stride;
758 outs() << "Indirect symbols for (" << Sec.segname << ","
759 << Sec.sectname << ") " << count << " entries";
760 uint32_t n = Sec.reserved1;
761 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
764 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
765 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
766 for (unsigned J = 0; J < Seg.nsects; ++J) {
767 MachO::section Sec = O->getSection(Load, J);
768 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
769 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
770 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
771 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
772 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
773 section_type == MachO::S_SYMBOL_STUBS) {
774 uint32_t stride;
775 if (section_type == MachO::S_SYMBOL_STUBS)
776 stride = Sec.reserved2;
777 else
778 stride = 4;
779 if (stride == 0) {
780 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
781 << Sec.sectname << ") "
782 << "(size of stubs in reserved2 field is zero)\n";
783 continue;
785 uint32_t count = Sec.size / stride;
786 outs() << "Indirect symbols for (" << Sec.segname << ","
787 << Sec.sectname << ") " << count << " entries";
788 uint32_t n = Sec.reserved1;
789 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
796 static void PrintRType(const uint64_t cputype, const unsigned r_type) {
797 static char const *generic_r_types[] = {
798 "VANILLA ", "PAIR ", "SECTDIF ", "PBLAPTR ", "LOCSDIF ", "TLV ",
799 " 6 (?) ", " 7 (?) ", " 8 (?) ", " 9 (?) ", " 10 (?) ", " 11 (?) ",
800 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
802 static char const *x86_64_r_types[] = {
803 "UNSIGND ", "SIGNED ", "BRANCH ", "GOT_LD ", "GOT ", "SUB ",
804 "SIGNED1 ", "SIGNED2 ", "SIGNED4 ", "TLV ", " 10 (?) ", " 11 (?) ",
805 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
807 static char const *arm_r_types[] = {
808 "VANILLA ", "PAIR ", "SECTDIFF", "LOCSDIF ", "PBLAPTR ",
809 "BR24 ", "T_BR22 ", "T_BR32 ", "HALF ", "HALFDIF ",
810 " 10 (?) ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
812 static char const *arm64_r_types[] = {
813 "UNSIGND ", "SUB ", "BR26 ", "PAGE21 ", "PAGOF12 ",
814 "GOTLDP ", "GOTLDPOF", "PTRTGOT ", "TLVLDP ", "TLVLDPOF",
815 "ADDEND ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
818 if (r_type > 0xf){
819 outs() << format("%-7u", r_type) << " ";
820 return;
822 switch (cputype) {
823 case MachO::CPU_TYPE_I386:
824 outs() << generic_r_types[r_type];
825 break;
826 case MachO::CPU_TYPE_X86_64:
827 outs() << x86_64_r_types[r_type];
828 break;
829 case MachO::CPU_TYPE_ARM:
830 outs() << arm_r_types[r_type];
831 break;
832 case MachO::CPU_TYPE_ARM64:
833 case MachO::CPU_TYPE_ARM64_32:
834 outs() << arm64_r_types[r_type];
835 break;
836 default:
837 outs() << format("%-7u ", r_type);
841 static void PrintRLength(const uint64_t cputype, const unsigned r_type,
842 const unsigned r_length, const bool previous_arm_half){
843 if (cputype == MachO::CPU_TYPE_ARM &&
844 (r_type == MachO::ARM_RELOC_HALF ||
845 r_type == MachO::ARM_RELOC_HALF_SECTDIFF || previous_arm_half == true)) {
846 if ((r_length & 0x1) == 0)
847 outs() << "lo/";
848 else
849 outs() << "hi/";
850 if ((r_length & 0x1) == 0)
851 outs() << "arm ";
852 else
853 outs() << "thm ";
854 } else {
855 switch (r_length) {
856 case 0:
857 outs() << "byte ";
858 break;
859 case 1:
860 outs() << "word ";
861 break;
862 case 2:
863 outs() << "long ";
864 break;
865 case 3:
866 if (cputype == MachO::CPU_TYPE_X86_64)
867 outs() << "quad ";
868 else
869 outs() << format("?(%2d) ", r_length);
870 break;
871 default:
872 outs() << format("?(%2d) ", r_length);
877 static void PrintRelocationEntries(const MachOObjectFile *O,
878 const relocation_iterator Begin,
879 const relocation_iterator End,
880 const uint64_t cputype,
881 const bool verbose) {
882 const MachO::symtab_command Symtab = O->getSymtabLoadCommand();
883 bool previous_arm_half = false;
884 bool previous_sectdiff = false;
885 uint32_t sectdiff_r_type = 0;
887 for (relocation_iterator Reloc = Begin; Reloc != End; ++Reloc) {
888 const DataRefImpl Rel = Reloc->getRawDataRefImpl();
889 const MachO::any_relocation_info RE = O->getRelocation(Rel);
890 const unsigned r_type = O->getAnyRelocationType(RE);
891 const bool r_scattered = O->isRelocationScattered(RE);
892 const unsigned r_pcrel = O->getAnyRelocationPCRel(RE);
893 const unsigned r_length = O->getAnyRelocationLength(RE);
894 const unsigned r_address = O->getAnyRelocationAddress(RE);
895 const bool r_extern = (r_scattered ? false :
896 O->getPlainRelocationExternal(RE));
897 const uint32_t r_value = (r_scattered ?
898 O->getScatteredRelocationValue(RE) : 0);
899 const unsigned r_symbolnum = (r_scattered ? 0 :
900 O->getPlainRelocationSymbolNum(RE));
902 if (r_scattered && cputype != MachO::CPU_TYPE_X86_64) {
903 if (verbose) {
904 // scattered: address
905 if ((cputype == MachO::CPU_TYPE_I386 &&
906 r_type == MachO::GENERIC_RELOC_PAIR) ||
907 (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR))
908 outs() << " ";
909 else
910 outs() << format("%08x ", (unsigned int)r_address);
912 // scattered: pcrel
913 if (r_pcrel)
914 outs() << "True ";
915 else
916 outs() << "False ";
918 // scattered: length
919 PrintRLength(cputype, r_type, r_length, previous_arm_half);
921 // scattered: extern & type
922 outs() << "n/a ";
923 PrintRType(cputype, r_type);
925 // scattered: scattered & value
926 outs() << format("True 0x%08x", (unsigned int)r_value);
927 if (previous_sectdiff == false) {
928 if ((cputype == MachO::CPU_TYPE_ARM &&
929 r_type == MachO::ARM_RELOC_PAIR))
930 outs() << format(" half = 0x%04x ", (unsigned int)r_address);
931 } else if (cputype == MachO::CPU_TYPE_ARM &&
932 sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF)
933 outs() << format(" other_half = 0x%04x ", (unsigned int)r_address);
934 if ((cputype == MachO::CPU_TYPE_I386 &&
935 (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
936 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) ||
937 (cputype == MachO::CPU_TYPE_ARM &&
938 (sectdiff_r_type == MachO::ARM_RELOC_SECTDIFF ||
939 sectdiff_r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
940 sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF))) {
941 previous_sectdiff = true;
942 sectdiff_r_type = r_type;
943 } else {
944 previous_sectdiff = false;
945 sectdiff_r_type = 0;
947 if (cputype == MachO::CPU_TYPE_ARM &&
948 (r_type == MachO::ARM_RELOC_HALF ||
949 r_type == MachO::ARM_RELOC_HALF_SECTDIFF))
950 previous_arm_half = true;
951 else
952 previous_arm_half = false;
953 outs() << "\n";
955 else {
956 // scattered: address pcrel length extern type scattered value
957 outs() << format("%08x %1d %-2d n/a %-7d 1 0x%08x\n",
958 (unsigned int)r_address, r_pcrel, r_length, r_type,
959 (unsigned int)r_value);
962 else {
963 if (verbose) {
964 // plain: address
965 if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR)
966 outs() << " ";
967 else
968 outs() << format("%08x ", (unsigned int)r_address);
970 // plain: pcrel
971 if (r_pcrel)
972 outs() << "True ";
973 else
974 outs() << "False ";
976 // plain: length
977 PrintRLength(cputype, r_type, r_length, previous_arm_half);
979 if (r_extern) {
980 // plain: extern & type & scattered
981 outs() << "True ";
982 PrintRType(cputype, r_type);
983 outs() << "False ";
985 // plain: symbolnum/value
986 if (r_symbolnum > Symtab.nsyms)
987 outs() << format("?(%d)\n", r_symbolnum);
988 else {
989 SymbolRef Symbol = *O->getSymbolByIndex(r_symbolnum);
990 Expected<StringRef> SymNameNext = Symbol.getName();
991 const char *name = NULL;
992 if (SymNameNext)
993 name = SymNameNext->data();
994 if (name == NULL)
995 outs() << format("?(%d)\n", r_symbolnum);
996 else
997 outs() << name << "\n";
1000 else {
1001 // plain: extern & type & scattered
1002 outs() << "False ";
1003 PrintRType(cputype, r_type);
1004 outs() << "False ";
1006 // plain: symbolnum/value
1007 if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR)
1008 outs() << format("other_half = 0x%04x\n", (unsigned int)r_address);
1009 else if ((cputype == MachO::CPU_TYPE_ARM64 ||
1010 cputype == MachO::CPU_TYPE_ARM64_32) &&
1011 r_type == MachO::ARM64_RELOC_ADDEND)
1012 outs() << format("addend = 0x%06x\n", (unsigned int)r_symbolnum);
1013 else {
1014 outs() << format("%d ", r_symbolnum);
1015 if (r_symbolnum == MachO::R_ABS)
1016 outs() << "R_ABS\n";
1017 else {
1018 // in this case, r_symbolnum is actually a 1-based section number
1019 uint32_t nsects = O->section_end()->getRawDataRefImpl().d.a;
1020 if (r_symbolnum > 0 && r_symbolnum <= nsects) {
1021 object::DataRefImpl DRI;
1022 DRI.d.a = r_symbolnum-1;
1023 StringRef SegName = O->getSectionFinalSegmentName(DRI);
1024 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
1025 outs() << "(" << SegName << "," << *NameOrErr << ")\n";
1026 else
1027 outs() << "(?,?)\n";
1029 else {
1030 outs() << "(?,?)\n";
1035 if (cputype == MachO::CPU_TYPE_ARM &&
1036 (r_type == MachO::ARM_RELOC_HALF ||
1037 r_type == MachO::ARM_RELOC_HALF_SECTDIFF))
1038 previous_arm_half = true;
1039 else
1040 previous_arm_half = false;
1042 else {
1043 // plain: address pcrel length extern type scattered symbolnum/section
1044 outs() << format("%08x %1d %-2d %1d %-7d 0 %d\n",
1045 (unsigned int)r_address, r_pcrel, r_length, r_extern,
1046 r_type, r_symbolnum);
1052 static void PrintRelocations(const MachOObjectFile *O, const bool verbose) {
1053 const uint64_t cputype = O->getHeader().cputype;
1054 const MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
1055 if (Dysymtab.nextrel != 0) {
1056 outs() << "External relocation information " << Dysymtab.nextrel
1057 << " entries";
1058 outs() << "\naddress pcrel length extern type scattered "
1059 "symbolnum/value\n";
1060 PrintRelocationEntries(O, O->extrel_begin(), O->extrel_end(), cputype,
1061 verbose);
1063 if (Dysymtab.nlocrel != 0) {
1064 outs() << format("Local relocation information %u entries",
1065 Dysymtab.nlocrel);
1066 outs() << "\naddress pcrel length extern type scattered "
1067 "symbolnum/value\n";
1068 PrintRelocationEntries(O, O->locrel_begin(), O->locrel_end(), cputype,
1069 verbose);
1071 for (const auto &Load : O->load_commands()) {
1072 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
1073 const MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
1074 for (unsigned J = 0; J < Seg.nsects; ++J) {
1075 const MachO::section_64 Sec = O->getSection64(Load, J);
1076 if (Sec.nreloc != 0) {
1077 DataRefImpl DRI;
1078 DRI.d.a = J;
1079 const StringRef SegName = O->getSectionFinalSegmentName(DRI);
1080 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
1081 outs() << "Relocation information (" << SegName << "," << *NameOrErr
1082 << format(") %u entries", Sec.nreloc);
1083 else
1084 outs() << "Relocation information (" << SegName << ",?) "
1085 << format("%u entries", Sec.nreloc);
1086 outs() << "\naddress pcrel length extern type scattered "
1087 "symbolnum/value\n";
1088 PrintRelocationEntries(O, O->section_rel_begin(DRI),
1089 O->section_rel_end(DRI), cputype, verbose);
1092 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
1093 const MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
1094 for (unsigned J = 0; J < Seg.nsects; ++J) {
1095 const MachO::section Sec = O->getSection(Load, J);
1096 if (Sec.nreloc != 0) {
1097 DataRefImpl DRI;
1098 DRI.d.a = J;
1099 const StringRef SegName = O->getSectionFinalSegmentName(DRI);
1100 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
1101 outs() << "Relocation information (" << SegName << "," << *NameOrErr
1102 << format(") %u entries", Sec.nreloc);
1103 else
1104 outs() << "Relocation information (" << SegName << ",?) "
1105 << format("%u entries", Sec.nreloc);
1106 outs() << "\naddress pcrel length extern type scattered "
1107 "symbolnum/value\n";
1108 PrintRelocationEntries(O, O->section_rel_begin(DRI),
1109 O->section_rel_end(DRI), cputype, verbose);
1116 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
1117 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
1118 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
1119 outs() << "Data in code table (" << nentries << " entries)\n";
1120 outs() << "offset length kind\n";
1121 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
1122 ++DI) {
1123 uint32_t Offset;
1124 DI->getOffset(Offset);
1125 outs() << format("0x%08" PRIx32, Offset) << " ";
1126 uint16_t Length;
1127 DI->getLength(Length);
1128 outs() << format("%6u", Length) << " ";
1129 uint16_t Kind;
1130 DI->getKind(Kind);
1131 if (verbose) {
1132 switch (Kind) {
1133 case MachO::DICE_KIND_DATA:
1134 outs() << "DATA";
1135 break;
1136 case MachO::DICE_KIND_JUMP_TABLE8:
1137 outs() << "JUMP_TABLE8";
1138 break;
1139 case MachO::DICE_KIND_JUMP_TABLE16:
1140 outs() << "JUMP_TABLE16";
1141 break;
1142 case MachO::DICE_KIND_JUMP_TABLE32:
1143 outs() << "JUMP_TABLE32";
1144 break;
1145 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
1146 outs() << "ABS_JUMP_TABLE32";
1147 break;
1148 default:
1149 outs() << format("0x%04" PRIx32, Kind);
1150 break;
1152 } else
1153 outs() << format("0x%04" PRIx32, Kind);
1154 outs() << "\n";
1158 static void PrintLinkOptHints(MachOObjectFile *O) {
1159 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
1160 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
1161 uint32_t nloh = LohLC.datasize;
1162 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
1163 for (uint32_t i = 0; i < nloh;) {
1164 unsigned n;
1165 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
1166 i += n;
1167 outs() << " identifier " << identifier << " ";
1168 if (i >= nloh)
1169 return;
1170 switch (identifier) {
1171 case 1:
1172 outs() << "AdrpAdrp\n";
1173 break;
1174 case 2:
1175 outs() << "AdrpLdr\n";
1176 break;
1177 case 3:
1178 outs() << "AdrpAddLdr\n";
1179 break;
1180 case 4:
1181 outs() << "AdrpLdrGotLdr\n";
1182 break;
1183 case 5:
1184 outs() << "AdrpAddStr\n";
1185 break;
1186 case 6:
1187 outs() << "AdrpLdrGotStr\n";
1188 break;
1189 case 7:
1190 outs() << "AdrpAdd\n";
1191 break;
1192 case 8:
1193 outs() << "AdrpLdrGot\n";
1194 break;
1195 default:
1196 outs() << "Unknown identifier value\n";
1197 break;
1199 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
1200 i += n;
1201 outs() << " narguments " << narguments << "\n";
1202 if (i >= nloh)
1203 return;
1205 for (uint32_t j = 0; j < narguments; j++) {
1206 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
1207 i += n;
1208 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
1209 if (i >= nloh)
1210 return;
1215 static void PrintDylibs(MachOObjectFile *O, bool JustId) {
1216 unsigned Index = 0;
1217 for (const auto &Load : O->load_commands()) {
1218 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
1219 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
1220 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
1221 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
1222 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
1223 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
1224 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
1225 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
1226 if (dl.dylib.name < dl.cmdsize) {
1227 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
1228 if (JustId)
1229 outs() << p << "\n";
1230 else {
1231 outs() << "\t" << p;
1232 outs() << " (compatibility version "
1233 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
1234 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
1235 << (dl.dylib.compatibility_version & 0xff) << ",";
1236 outs() << " current version "
1237 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
1238 << ((dl.dylib.current_version >> 8) & 0xff) << "."
1239 << (dl.dylib.current_version & 0xff);
1240 if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
1241 outs() << ", weak";
1242 if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
1243 outs() << ", reexport";
1244 if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
1245 outs() << ", upward";
1246 if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
1247 outs() << ", lazy";
1248 outs() << ")\n";
1250 } else {
1251 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
1252 if (Load.C.cmd == MachO::LC_ID_DYLIB)
1253 outs() << "LC_ID_DYLIB ";
1254 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
1255 outs() << "LC_LOAD_DYLIB ";
1256 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
1257 outs() << "LC_LOAD_WEAK_DYLIB ";
1258 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
1259 outs() << "LC_LAZY_LOAD_DYLIB ";
1260 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
1261 outs() << "LC_REEXPORT_DYLIB ";
1262 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
1263 outs() << "LC_LOAD_UPWARD_DYLIB ";
1264 else
1265 outs() << "LC_??? ";
1266 outs() << "command " << Index++ << "\n";
1272 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
1274 static void CreateSymbolAddressMap(MachOObjectFile *O,
1275 SymbolAddressMap *AddrMap) {
1276 // Create a map of symbol addresses to symbol names.
1277 const StringRef FileName = O->getFileName();
1278 for (const SymbolRef &Symbol : O->symbols()) {
1279 SymbolRef::Type ST = unwrapOrError(Symbol.getType(), FileName);
1280 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
1281 ST == SymbolRef::ST_Other) {
1282 uint64_t Address = Symbol.getValue();
1283 StringRef SymName = unwrapOrError(Symbol.getName(), FileName);
1284 if (!SymName.startswith(".objc"))
1285 (*AddrMap)[Address] = SymName;
1290 // GuessSymbolName is passed the address of what might be a symbol and a
1291 // pointer to the SymbolAddressMap. It returns the name of a symbol
1292 // with that address or nullptr if no symbol is found with that address.
1293 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
1294 const char *SymbolName = nullptr;
1295 // A DenseMap can't lookup up some values.
1296 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
1297 StringRef name = AddrMap->lookup(value);
1298 if (!name.empty())
1299 SymbolName = name.data();
1301 return SymbolName;
1304 static void DumpCstringChar(const char c) {
1305 char p[2];
1306 p[0] = c;
1307 p[1] = '\0';
1308 outs().write_escaped(p);
1311 static void DumpCstringSection(MachOObjectFile *O, const char *sect,
1312 uint32_t sect_size, uint64_t sect_addr,
1313 bool print_addresses) {
1314 for (uint32_t i = 0; i < sect_size; i++) {
1315 if (print_addresses) {
1316 if (O->is64Bit())
1317 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1318 else
1319 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1321 for (; i < sect_size && sect[i] != '\0'; i++)
1322 DumpCstringChar(sect[i]);
1323 if (i < sect_size && sect[i] == '\0')
1324 outs() << "\n";
1328 static void DumpLiteral4(uint32_t l, float f) {
1329 outs() << format("0x%08" PRIx32, l);
1330 if ((l & 0x7f800000) != 0x7f800000)
1331 outs() << format(" (%.16e)\n", f);
1332 else {
1333 if (l == 0x7f800000)
1334 outs() << " (+Infinity)\n";
1335 else if (l == 0xff800000)
1336 outs() << " (-Infinity)\n";
1337 else if ((l & 0x00400000) == 0x00400000)
1338 outs() << " (non-signaling Not-a-Number)\n";
1339 else
1340 outs() << " (signaling Not-a-Number)\n";
1344 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
1345 uint32_t sect_size, uint64_t sect_addr,
1346 bool print_addresses) {
1347 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
1348 if (print_addresses) {
1349 if (O->is64Bit())
1350 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1351 else
1352 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1354 float f;
1355 memcpy(&f, sect + i, sizeof(float));
1356 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1357 sys::swapByteOrder(f);
1358 uint32_t l;
1359 memcpy(&l, sect + i, sizeof(uint32_t));
1360 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1361 sys::swapByteOrder(l);
1362 DumpLiteral4(l, f);
1366 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
1367 double d) {
1368 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
1369 uint32_t Hi, Lo;
1370 Hi = (O->isLittleEndian()) ? l1 : l0;
1371 Lo = (O->isLittleEndian()) ? l0 : l1;
1373 // Hi is the high word, so this is equivalent to if(isfinite(d))
1374 if ((Hi & 0x7ff00000) != 0x7ff00000)
1375 outs() << format(" (%.16e)\n", d);
1376 else {
1377 if (Hi == 0x7ff00000 && Lo == 0)
1378 outs() << " (+Infinity)\n";
1379 else if (Hi == 0xfff00000 && Lo == 0)
1380 outs() << " (-Infinity)\n";
1381 else if ((Hi & 0x00080000) == 0x00080000)
1382 outs() << " (non-signaling Not-a-Number)\n";
1383 else
1384 outs() << " (signaling Not-a-Number)\n";
1388 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
1389 uint32_t sect_size, uint64_t sect_addr,
1390 bool print_addresses) {
1391 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
1392 if (print_addresses) {
1393 if (O->is64Bit())
1394 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1395 else
1396 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1398 double d;
1399 memcpy(&d, sect + i, sizeof(double));
1400 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1401 sys::swapByteOrder(d);
1402 uint32_t l0, l1;
1403 memcpy(&l0, sect + i, sizeof(uint32_t));
1404 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
1405 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1406 sys::swapByteOrder(l0);
1407 sys::swapByteOrder(l1);
1409 DumpLiteral8(O, l0, l1, d);
1413 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
1414 outs() << format("0x%08" PRIx32, l0) << " ";
1415 outs() << format("0x%08" PRIx32, l1) << " ";
1416 outs() << format("0x%08" PRIx32, l2) << " ";
1417 outs() << format("0x%08" PRIx32, l3) << "\n";
1420 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
1421 uint32_t sect_size, uint64_t sect_addr,
1422 bool print_addresses) {
1423 for (uint32_t i = 0; i < sect_size; i += 16) {
1424 if (print_addresses) {
1425 if (O->is64Bit())
1426 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1427 else
1428 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1430 uint32_t l0, l1, l2, l3;
1431 memcpy(&l0, sect + i, sizeof(uint32_t));
1432 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
1433 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
1434 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
1435 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1436 sys::swapByteOrder(l0);
1437 sys::swapByteOrder(l1);
1438 sys::swapByteOrder(l2);
1439 sys::swapByteOrder(l3);
1441 DumpLiteral16(l0, l1, l2, l3);
1445 static void DumpLiteralPointerSection(MachOObjectFile *O,
1446 const SectionRef &Section,
1447 const char *sect, uint32_t sect_size,
1448 uint64_t sect_addr,
1449 bool print_addresses) {
1450 // Collect the literal sections in this Mach-O file.
1451 std::vector<SectionRef> LiteralSections;
1452 for (const SectionRef &Section : O->sections()) {
1453 DataRefImpl Ref = Section.getRawDataRefImpl();
1454 uint32_t section_type;
1455 if (O->is64Bit()) {
1456 const MachO::section_64 Sec = O->getSection64(Ref);
1457 section_type = Sec.flags & MachO::SECTION_TYPE;
1458 } else {
1459 const MachO::section Sec = O->getSection(Ref);
1460 section_type = Sec.flags & MachO::SECTION_TYPE;
1462 if (section_type == MachO::S_CSTRING_LITERALS ||
1463 section_type == MachO::S_4BYTE_LITERALS ||
1464 section_type == MachO::S_8BYTE_LITERALS ||
1465 section_type == MachO::S_16BYTE_LITERALS)
1466 LiteralSections.push_back(Section);
1469 // Set the size of the literal pointer.
1470 uint32_t lp_size = O->is64Bit() ? 8 : 4;
1472 // Collect the external relocation symbols for the literal pointers.
1473 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
1474 for (const RelocationRef &Reloc : Section.relocations()) {
1475 DataRefImpl Rel;
1476 MachO::any_relocation_info RE;
1477 bool isExtern = false;
1478 Rel = Reloc.getRawDataRefImpl();
1479 RE = O->getRelocation(Rel);
1480 isExtern = O->getPlainRelocationExternal(RE);
1481 if (isExtern) {
1482 uint64_t RelocOffset = Reloc.getOffset();
1483 symbol_iterator RelocSym = Reloc.getSymbol();
1484 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
1487 array_pod_sort(Relocs.begin(), Relocs.end());
1489 // Dump each literal pointer.
1490 for (uint32_t i = 0; i < sect_size; i += lp_size) {
1491 if (print_addresses) {
1492 if (O->is64Bit())
1493 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1494 else
1495 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1497 uint64_t lp;
1498 if (O->is64Bit()) {
1499 memcpy(&lp, sect + i, sizeof(uint64_t));
1500 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1501 sys::swapByteOrder(lp);
1502 } else {
1503 uint32_t li;
1504 memcpy(&li, sect + i, sizeof(uint32_t));
1505 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1506 sys::swapByteOrder(li);
1507 lp = li;
1510 // First look for an external relocation entry for this literal pointer.
1511 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
1512 return P.first == i;
1514 if (Reloc != Relocs.end()) {
1515 symbol_iterator RelocSym = Reloc->second;
1516 StringRef SymName = unwrapOrError(RelocSym->getName(), O->getFileName());
1517 outs() << "external relocation entry for symbol:" << SymName << "\n";
1518 continue;
1521 // For local references see what the section the literal pointer points to.
1522 auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
1523 return lp >= R.getAddress() && lp < R.getAddress() + R.getSize();
1525 if (Sect == LiteralSections.end()) {
1526 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
1527 continue;
1530 uint64_t SectAddress = Sect->getAddress();
1531 uint64_t SectSize = Sect->getSize();
1533 StringRef SectName;
1534 Sect->getName(SectName);
1535 DataRefImpl Ref = Sect->getRawDataRefImpl();
1536 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
1537 outs() << SegmentName << ":" << SectName << ":";
1539 uint32_t section_type;
1540 if (O->is64Bit()) {
1541 const MachO::section_64 Sec = O->getSection64(Ref);
1542 section_type = Sec.flags & MachO::SECTION_TYPE;
1543 } else {
1544 const MachO::section Sec = O->getSection(Ref);
1545 section_type = Sec.flags & MachO::SECTION_TYPE;
1548 StringRef BytesStr = unwrapOrError(Sect->getContents(), O->getFileName());
1550 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
1552 switch (section_type) {
1553 case MachO::S_CSTRING_LITERALS:
1554 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
1555 i++) {
1556 DumpCstringChar(Contents[i]);
1558 outs() << "\n";
1559 break;
1560 case MachO::S_4BYTE_LITERALS:
1561 float f;
1562 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
1563 uint32_t l;
1564 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
1565 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1566 sys::swapByteOrder(f);
1567 sys::swapByteOrder(l);
1569 DumpLiteral4(l, f);
1570 break;
1571 case MachO::S_8BYTE_LITERALS: {
1572 double d;
1573 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
1574 uint32_t l0, l1;
1575 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
1576 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
1577 sizeof(uint32_t));
1578 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1579 sys::swapByteOrder(f);
1580 sys::swapByteOrder(l0);
1581 sys::swapByteOrder(l1);
1583 DumpLiteral8(O, l0, l1, d);
1584 break;
1586 case MachO::S_16BYTE_LITERALS: {
1587 uint32_t l0, l1, l2, l3;
1588 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
1589 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
1590 sizeof(uint32_t));
1591 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
1592 sizeof(uint32_t));
1593 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
1594 sizeof(uint32_t));
1595 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1596 sys::swapByteOrder(l0);
1597 sys::swapByteOrder(l1);
1598 sys::swapByteOrder(l2);
1599 sys::swapByteOrder(l3);
1601 DumpLiteral16(l0, l1, l2, l3);
1602 break;
1608 static void DumpInitTermPointerSection(MachOObjectFile *O,
1609 const SectionRef &Section,
1610 const char *sect,
1611 uint32_t sect_size, uint64_t sect_addr,
1612 SymbolAddressMap *AddrMap,
1613 bool verbose) {
1614 uint32_t stride;
1615 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
1617 // Collect the external relocation symbols for the pointers.
1618 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
1619 for (const RelocationRef &Reloc : Section.relocations()) {
1620 DataRefImpl Rel;
1621 MachO::any_relocation_info RE;
1622 bool isExtern = false;
1623 Rel = Reloc.getRawDataRefImpl();
1624 RE = O->getRelocation(Rel);
1625 isExtern = O->getPlainRelocationExternal(RE);
1626 if (isExtern) {
1627 uint64_t RelocOffset = Reloc.getOffset();
1628 symbol_iterator RelocSym = Reloc.getSymbol();
1629 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
1632 array_pod_sort(Relocs.begin(), Relocs.end());
1634 for (uint32_t i = 0; i < sect_size; i += stride) {
1635 const char *SymbolName = nullptr;
1636 uint64_t p;
1637 if (O->is64Bit()) {
1638 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
1639 uint64_t pointer_value;
1640 memcpy(&pointer_value, sect + i, stride);
1641 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1642 sys::swapByteOrder(pointer_value);
1643 outs() << format("0x%016" PRIx64, pointer_value);
1644 p = pointer_value;
1645 } else {
1646 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
1647 uint32_t pointer_value;
1648 memcpy(&pointer_value, sect + i, stride);
1649 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1650 sys::swapByteOrder(pointer_value);
1651 outs() << format("0x%08" PRIx32, pointer_value);
1652 p = pointer_value;
1654 if (verbose) {
1655 // First look for an external relocation entry for this pointer.
1656 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
1657 return P.first == i;
1659 if (Reloc != Relocs.end()) {
1660 symbol_iterator RelocSym = Reloc->second;
1661 outs() << " " << unwrapOrError(RelocSym->getName(), O->getFileName());
1662 } else {
1663 SymbolName = GuessSymbolName(p, AddrMap);
1664 if (SymbolName)
1665 outs() << " " << SymbolName;
1668 outs() << "\n";
1672 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
1673 uint32_t size, uint64_t addr) {
1674 uint32_t cputype = O->getHeader().cputype;
1675 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
1676 uint32_t j;
1677 for (uint32_t i = 0; i < size; i += j, addr += j) {
1678 if (O->is64Bit())
1679 outs() << format("%016" PRIx64, addr) << "\t";
1680 else
1681 outs() << format("%08" PRIx64, addr) << "\t";
1682 for (j = 0; j < 16 && i + j < size; j++) {
1683 uint8_t byte_word = *(sect + i + j);
1684 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1686 outs() << "\n";
1688 } else {
1689 uint32_t j;
1690 for (uint32_t i = 0; i < size; i += j, addr += j) {
1691 if (O->is64Bit())
1692 outs() << format("%016" PRIx64, addr) << "\t";
1693 else
1694 outs() << format("%08" PRIx64, addr) << "\t";
1695 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
1696 j += sizeof(int32_t)) {
1697 if (i + j + sizeof(int32_t) <= size) {
1698 uint32_t long_word;
1699 memcpy(&long_word, sect + i + j, sizeof(int32_t));
1700 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1701 sys::swapByteOrder(long_word);
1702 outs() << format("%08" PRIx32, long_word) << " ";
1703 } else {
1704 for (uint32_t k = 0; i + j + k < size; k++) {
1705 uint8_t byte_word = *(sect + i + j + k);
1706 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1710 outs() << "\n";
1715 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1716 StringRef DisSegName, StringRef DisSectName);
1717 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1718 uint32_t size, uint32_t addr);
1719 #ifdef HAVE_LIBXAR
1720 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
1721 uint32_t size, bool verbose,
1722 bool PrintXarHeader, bool PrintXarFileHeaders,
1723 std::string XarMemberName);
1724 #endif // defined(HAVE_LIBXAR)
1726 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1727 bool verbose) {
1728 SymbolAddressMap AddrMap;
1729 if (verbose)
1730 CreateSymbolAddressMap(O, &AddrMap);
1732 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1733 StringRef DumpSection = FilterSections[i];
1734 std::pair<StringRef, StringRef> DumpSegSectName;
1735 DumpSegSectName = DumpSection.split(',');
1736 StringRef DumpSegName, DumpSectName;
1737 if (!DumpSegSectName.second.empty()) {
1738 DumpSegName = DumpSegSectName.first;
1739 DumpSectName = DumpSegSectName.second;
1740 } else {
1741 DumpSegName = "";
1742 DumpSectName = DumpSegSectName.first;
1744 for (const SectionRef &Section : O->sections()) {
1745 StringRef SectName;
1746 Section.getName(SectName);
1747 DataRefImpl Ref = Section.getRawDataRefImpl();
1748 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1749 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1750 (SectName == DumpSectName)) {
1752 uint32_t section_flags;
1753 if (O->is64Bit()) {
1754 const MachO::section_64 Sec = O->getSection64(Ref);
1755 section_flags = Sec.flags;
1757 } else {
1758 const MachO::section Sec = O->getSection(Ref);
1759 section_flags = Sec.flags;
1761 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1763 StringRef BytesStr =
1764 unwrapOrError(Section.getContents(), O->getFileName());
1765 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1766 uint32_t sect_size = BytesStr.size();
1767 uint64_t sect_addr = Section.getAddress();
1769 outs() << "Contents of (" << SegName << "," << SectName
1770 << ") section\n";
1772 if (verbose) {
1773 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1774 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1775 DisassembleMachO(Filename, O, SegName, SectName);
1776 continue;
1778 if (SegName == "__TEXT" && SectName == "__info_plist") {
1779 outs() << sect;
1780 continue;
1782 if (SegName == "__OBJC" && SectName == "__protocol") {
1783 DumpProtocolSection(O, sect, sect_size, sect_addr);
1784 continue;
1786 #ifdef HAVE_LIBXAR
1787 if (SegName == "__LLVM" && SectName == "__bundle") {
1788 DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
1789 ArchiveHeaders, "");
1790 continue;
1792 #endif // defined(HAVE_LIBXAR)
1793 switch (section_type) {
1794 case MachO::S_REGULAR:
1795 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1796 break;
1797 case MachO::S_ZEROFILL:
1798 outs() << "zerofill section and has no contents in the file\n";
1799 break;
1800 case MachO::S_CSTRING_LITERALS:
1801 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1802 break;
1803 case MachO::S_4BYTE_LITERALS:
1804 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1805 break;
1806 case MachO::S_8BYTE_LITERALS:
1807 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1808 break;
1809 case MachO::S_16BYTE_LITERALS:
1810 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1811 break;
1812 case MachO::S_LITERAL_POINTERS:
1813 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1814 !NoLeadingAddr);
1815 break;
1816 case MachO::S_MOD_INIT_FUNC_POINTERS:
1817 case MachO::S_MOD_TERM_FUNC_POINTERS:
1818 DumpInitTermPointerSection(O, Section, sect, sect_size, sect_addr,
1819 &AddrMap, verbose);
1820 break;
1821 default:
1822 outs() << "Unknown section type ("
1823 << format("0x%08" PRIx32, section_type) << ")\n";
1824 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1825 break;
1827 } else {
1828 if (section_type == MachO::S_ZEROFILL)
1829 outs() << "zerofill section and has no contents in the file\n";
1830 else
1831 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1838 static void DumpInfoPlistSectionContents(StringRef Filename,
1839 MachOObjectFile *O) {
1840 for (const SectionRef &Section : O->sections()) {
1841 StringRef SectName;
1842 Section.getName(SectName);
1843 DataRefImpl Ref = Section.getRawDataRefImpl();
1844 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1845 if (SegName == "__TEXT" && SectName == "__info_plist") {
1846 if (!NoLeadingHeaders)
1847 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1848 StringRef BytesStr =
1849 unwrapOrError(Section.getContents(), O->getFileName());
1850 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1851 outs() << format("%.*s", BytesStr.size(), sect) << "\n";
1852 return;
1857 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1858 // and if it is and there is a list of architecture flags is specified then
1859 // check to make sure this Mach-O file is one of those architectures or all
1860 // architectures were specified. If not then an error is generated and this
1861 // routine returns false. Else it returns true.
1862 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1863 auto *MachO = dyn_cast<MachOObjectFile>(O);
1865 if (!MachO || ArchAll || ArchFlags.empty())
1866 return true;
1868 MachO::mach_header H;
1869 MachO::mach_header_64 H_64;
1870 Triple T;
1871 const char *McpuDefault, *ArchFlag;
1872 if (MachO->is64Bit()) {
1873 H_64 = MachO->MachOObjectFile::getHeader64();
1874 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
1875 &McpuDefault, &ArchFlag);
1876 } else {
1877 H = MachO->MachOObjectFile::getHeader();
1878 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
1879 &McpuDefault, &ArchFlag);
1881 const std::string ArchFlagName(ArchFlag);
1882 if (none_of(ArchFlags, [&](const std::string &Name) {
1883 return Name == ArchFlagName;
1884 })) {
1885 WithColor::error(errs(), "llvm-objdump")
1886 << Filename << ": no architecture specified.\n";
1887 return false;
1889 return true;
1892 static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1894 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1895 // archive member and or in a slice of a universal file. It prints the
1896 // the file name and header info and then processes it according to the
1897 // command line options.
1898 static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF,
1899 StringRef ArchiveMemberName = StringRef(),
1900 StringRef ArchitectureName = StringRef()) {
1901 // If we are doing some processing here on the Mach-O file print the header
1902 // info. And don't print it otherwise like in the case of printing the
1903 // UniversalHeaders or ArchiveHeaders.
1904 if (Disassemble || Relocations || PrivateHeaders || ExportsTrie || Rebase ||
1905 Bind || SymbolTable || LazyBind || WeakBind || IndirectSymbols ||
1906 DataInCode || LinkOptHints || DylibsUsed || DylibId || ObjcMetaData ||
1907 (!FilterSections.empty())) {
1908 if (!NoLeadingHeaders) {
1909 outs() << Name;
1910 if (!ArchiveMemberName.empty())
1911 outs() << '(' << ArchiveMemberName << ')';
1912 if (!ArchitectureName.empty())
1913 outs() << " (architecture " << ArchitectureName << ")";
1914 outs() << ":\n";
1917 // To use the report_error() form with an ArchiveName and FileName set
1918 // these up based on what is passed for Name and ArchiveMemberName.
1919 StringRef ArchiveName;
1920 StringRef FileName;
1921 if (!ArchiveMemberName.empty()) {
1922 ArchiveName = Name;
1923 FileName = ArchiveMemberName;
1924 } else {
1925 ArchiveName = StringRef();
1926 FileName = Name;
1929 // If we need the symbol table to do the operation then check it here to
1930 // produce a good error message as to where the Mach-O file comes from in
1931 // the error message.
1932 if (Disassemble || IndirectSymbols || !FilterSections.empty() || UnwindInfo)
1933 if (Error Err = MachOOF->checkSymbolTable())
1934 report_error(std::move(Err), ArchiveName, FileName, ArchitectureName);
1936 if (DisassembleAll) {
1937 for (const SectionRef &Section : MachOOF->sections()) {
1938 StringRef SectName;
1939 Section.getName(SectName);
1940 if (SectName.equals("__text")) {
1941 DataRefImpl Ref = Section.getRawDataRefImpl();
1942 StringRef SegName = MachOOF->getSectionFinalSegmentName(Ref);
1943 DisassembleMachO(FileName, MachOOF, SegName, SectName);
1947 else if (Disassemble) {
1948 if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE &&
1949 MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64)
1950 DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text");
1951 else
1952 DisassembleMachO(FileName, MachOOF, "__TEXT", "__text");
1954 if (IndirectSymbols)
1955 PrintIndirectSymbols(MachOOF, !NonVerbose);
1956 if (DataInCode)
1957 PrintDataInCodeTable(MachOOF, !NonVerbose);
1958 if (LinkOptHints)
1959 PrintLinkOptHints(MachOOF);
1960 if (Relocations)
1961 PrintRelocations(MachOOF, !NonVerbose);
1962 if (SectionHeaders)
1963 printSectionHeaders(MachOOF);
1964 if (SectionContents)
1965 printSectionContents(MachOOF);
1966 if (!FilterSections.empty())
1967 DumpSectionContents(FileName, MachOOF, !NonVerbose);
1968 if (InfoPlist)
1969 DumpInfoPlistSectionContents(FileName, MachOOF);
1970 if (DylibsUsed)
1971 PrintDylibs(MachOOF, false);
1972 if (DylibId)
1973 PrintDylibs(MachOOF, true);
1974 if (SymbolTable)
1975 printSymbolTable(MachOOF, ArchiveName, ArchitectureName);
1976 if (UnwindInfo)
1977 printMachOUnwindInfo(MachOOF);
1978 if (PrivateHeaders) {
1979 printMachOFileHeader(MachOOF);
1980 printMachOLoadCommands(MachOOF);
1982 if (FirstPrivateHeader)
1983 printMachOFileHeader(MachOOF);
1984 if (ObjcMetaData)
1985 printObjcMetaData(MachOOF, !NonVerbose);
1986 if (ExportsTrie)
1987 printExportsTrie(MachOOF);
1988 if (Rebase)
1989 printRebaseTable(MachOOF);
1990 if (Bind)
1991 printBindTable(MachOOF);
1992 if (LazyBind)
1993 printLazyBindTable(MachOOF);
1994 if (WeakBind)
1995 printWeakBindTable(MachOOF);
1997 if (DwarfDumpType != DIDT_Null) {
1998 std::unique_ptr<DIContext> DICtx = DWARFContext::create(*MachOOF);
1999 // Dump the complete DWARF structure.
2000 DIDumpOptions DumpOpts;
2001 DumpOpts.DumpType = DwarfDumpType;
2002 DICtx->dump(outs(), DumpOpts);
2006 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
2007 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
2008 outs() << " cputype (" << cputype << ")\n";
2009 outs() << " cpusubtype (" << cpusubtype << ")\n";
2012 // printCPUType() helps print_fat_headers by printing the cputype and
2013 // pusubtype (symbolically for the one's it knows about).
2014 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
2015 switch (cputype) {
2016 case MachO::CPU_TYPE_I386:
2017 switch (cpusubtype) {
2018 case MachO::CPU_SUBTYPE_I386_ALL:
2019 outs() << " cputype CPU_TYPE_I386\n";
2020 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
2021 break;
2022 default:
2023 printUnknownCPUType(cputype, cpusubtype);
2024 break;
2026 break;
2027 case MachO::CPU_TYPE_X86_64:
2028 switch (cpusubtype) {
2029 case MachO::CPU_SUBTYPE_X86_64_ALL:
2030 outs() << " cputype CPU_TYPE_X86_64\n";
2031 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
2032 break;
2033 case MachO::CPU_SUBTYPE_X86_64_H:
2034 outs() << " cputype CPU_TYPE_X86_64\n";
2035 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
2036 break;
2037 default:
2038 printUnknownCPUType(cputype, cpusubtype);
2039 break;
2041 break;
2042 case MachO::CPU_TYPE_ARM:
2043 switch (cpusubtype) {
2044 case MachO::CPU_SUBTYPE_ARM_ALL:
2045 outs() << " cputype CPU_TYPE_ARM\n";
2046 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
2047 break;
2048 case MachO::CPU_SUBTYPE_ARM_V4T:
2049 outs() << " cputype CPU_TYPE_ARM\n";
2050 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
2051 break;
2052 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
2053 outs() << " cputype CPU_TYPE_ARM\n";
2054 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
2055 break;
2056 case MachO::CPU_SUBTYPE_ARM_XSCALE:
2057 outs() << " cputype CPU_TYPE_ARM\n";
2058 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
2059 break;
2060 case MachO::CPU_SUBTYPE_ARM_V6:
2061 outs() << " cputype CPU_TYPE_ARM\n";
2062 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
2063 break;
2064 case MachO::CPU_SUBTYPE_ARM_V6M:
2065 outs() << " cputype CPU_TYPE_ARM\n";
2066 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
2067 break;
2068 case MachO::CPU_SUBTYPE_ARM_V7:
2069 outs() << " cputype CPU_TYPE_ARM\n";
2070 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
2071 break;
2072 case MachO::CPU_SUBTYPE_ARM_V7EM:
2073 outs() << " cputype CPU_TYPE_ARM\n";
2074 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
2075 break;
2076 case MachO::CPU_SUBTYPE_ARM_V7K:
2077 outs() << " cputype CPU_TYPE_ARM\n";
2078 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
2079 break;
2080 case MachO::CPU_SUBTYPE_ARM_V7M:
2081 outs() << " cputype CPU_TYPE_ARM\n";
2082 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
2083 break;
2084 case MachO::CPU_SUBTYPE_ARM_V7S:
2085 outs() << " cputype CPU_TYPE_ARM\n";
2086 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
2087 break;
2088 default:
2089 printUnknownCPUType(cputype, cpusubtype);
2090 break;
2092 break;
2093 case MachO::CPU_TYPE_ARM64:
2094 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
2095 case MachO::CPU_SUBTYPE_ARM64_ALL:
2096 outs() << " cputype CPU_TYPE_ARM64\n";
2097 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
2098 break;
2099 case MachO::CPU_SUBTYPE_ARM64E:
2100 outs() << " cputype CPU_TYPE_ARM64\n";
2101 outs() << " cpusubtype CPU_SUBTYPE_ARM64E\n";
2102 break;
2103 default:
2104 printUnknownCPUType(cputype, cpusubtype);
2105 break;
2107 break;
2108 case MachO::CPU_TYPE_ARM64_32:
2109 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
2110 case MachO::CPU_SUBTYPE_ARM64_32_V8:
2111 outs() << " cputype CPU_TYPE_ARM64_32\n";
2112 outs() << " cpusubtype CPU_SUBTYPE_ARM64_32_V8\n";
2113 break;
2114 default:
2115 printUnknownCPUType(cputype, cpusubtype);
2116 break;
2118 break;
2119 default:
2120 printUnknownCPUType(cputype, cpusubtype);
2121 break;
2125 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
2126 bool verbose) {
2127 outs() << "Fat headers\n";
2128 if (verbose) {
2129 if (UB->getMagic() == MachO::FAT_MAGIC)
2130 outs() << "fat_magic FAT_MAGIC\n";
2131 else // UB->getMagic() == MachO::FAT_MAGIC_64
2132 outs() << "fat_magic FAT_MAGIC_64\n";
2133 } else
2134 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
2136 uint32_t nfat_arch = UB->getNumberOfObjects();
2137 StringRef Buf = UB->getData();
2138 uint64_t size = Buf.size();
2139 uint64_t big_size = sizeof(struct MachO::fat_header) +
2140 nfat_arch * sizeof(struct MachO::fat_arch);
2141 outs() << "nfat_arch " << UB->getNumberOfObjects();
2142 if (nfat_arch == 0)
2143 outs() << " (malformed, contains zero architecture types)\n";
2144 else if (big_size > size)
2145 outs() << " (malformed, architectures past end of file)\n";
2146 else
2147 outs() << "\n";
2149 for (uint32_t i = 0; i < nfat_arch; ++i) {
2150 MachOUniversalBinary::ObjectForArch OFA(UB, i);
2151 uint32_t cputype = OFA.getCPUType();
2152 uint32_t cpusubtype = OFA.getCPUSubType();
2153 outs() << "architecture ";
2154 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
2155 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
2156 uint32_t other_cputype = other_OFA.getCPUType();
2157 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
2158 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
2159 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
2160 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
2161 outs() << "(illegal duplicate architecture) ";
2162 break;
2165 if (verbose) {
2166 outs() << OFA.getArchFlagName() << "\n";
2167 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
2168 } else {
2169 outs() << i << "\n";
2170 outs() << " cputype " << cputype << "\n";
2171 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
2172 << "\n";
2174 if (verbose &&
2175 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
2176 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
2177 else
2178 outs() << " capabilities "
2179 << format("0x%" PRIx32,
2180 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
2181 outs() << " offset " << OFA.getOffset();
2182 if (OFA.getOffset() > size)
2183 outs() << " (past end of file)";
2184 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
2185 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
2186 outs() << "\n";
2187 outs() << " size " << OFA.getSize();
2188 big_size = OFA.getOffset() + OFA.getSize();
2189 if (big_size > size)
2190 outs() << " (past end of file)";
2191 outs() << "\n";
2192 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
2193 << ")\n";
2197 static void printArchiveChild(StringRef Filename, const Archive::Child &C,
2198 bool verbose, bool print_offset,
2199 StringRef ArchitectureName = StringRef()) {
2200 if (print_offset)
2201 outs() << C.getChildOffset() << "\t";
2202 sys::fs::perms Mode =
2203 unwrapOrError(C.getAccessMode(), Filename, C, ArchitectureName);
2204 if (verbose) {
2205 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
2206 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
2207 outs() << "-";
2208 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
2209 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
2210 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
2211 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
2212 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
2213 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
2214 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
2215 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
2216 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
2217 } else {
2218 outs() << format("0%o ", Mode);
2221 outs() << format(
2222 "%3d/%-3d %5" PRId64 " ",
2223 unwrapOrError(C.getUID(), Filename, C, ArchitectureName),
2224 unwrapOrError(C.getGID(), Filename, C, ArchitectureName),
2225 unwrapOrError(C.getRawSize(), Filename, C, ArchitectureName));
2227 StringRef RawLastModified = C.getRawLastModified();
2228 if (verbose) {
2229 unsigned Seconds;
2230 if (RawLastModified.getAsInteger(10, Seconds))
2231 outs() << "(date: \"" << RawLastModified
2232 << "\" contains non-decimal chars) ";
2233 else {
2234 // Since cime(3) returns a 26 character string of the form:
2235 // "Sun Sep 16 01:03:52 1973\n\0"
2236 // just print 24 characters.
2237 time_t t = Seconds;
2238 outs() << format("%.24s ", ctime(&t));
2240 } else {
2241 outs() << RawLastModified << " ";
2244 if (verbose) {
2245 Expected<StringRef> NameOrErr = C.getName();
2246 if (!NameOrErr) {
2247 consumeError(NameOrErr.takeError());
2248 outs() << unwrapOrError(C.getRawName(), Filename, C, ArchitectureName)
2249 << "\n";
2250 } else {
2251 StringRef Name = NameOrErr.get();
2252 outs() << Name << "\n";
2254 } else {
2255 outs() << unwrapOrError(C.getRawName(), Filename, C, ArchitectureName)
2256 << "\n";
2260 static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
2261 bool print_offset,
2262 StringRef ArchitectureName = StringRef()) {
2263 Error Err = Error::success();
2264 for (const auto &C : A->children(Err, false))
2265 printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
2267 if (Err)
2268 report_error(std::move(Err), StringRef(), Filename, ArchitectureName);
2271 static bool ValidateArchFlags() {
2272 // Check for -arch all and verifiy the -arch flags are valid.
2273 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
2274 if (ArchFlags[i] == "all") {
2275 ArchAll = true;
2276 } else {
2277 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
2278 WithColor::error(errs(), "llvm-objdump")
2279 << "unknown architecture named '" + ArchFlags[i] +
2280 "'for the -arch option\n";
2281 return false;
2285 return true;
2288 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
2289 // -arch flags selecting just those slices as specified by them and also parses
2290 // archive files. Then for each individual Mach-O file ProcessMachO() is
2291 // called to process the file based on the command line options.
2292 void parseInputMachO(StringRef Filename) {
2293 if (!ValidateArchFlags())
2294 return;
2296 // Attempt to open the binary.
2297 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
2298 if (!BinaryOrErr) {
2299 if (Error E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
2300 report_error(std::move(E), Filename);
2301 else
2302 outs() << Filename << ": is not an object file\n";
2303 return;
2305 Binary &Bin = *BinaryOrErr.get().getBinary();
2307 if (Archive *A = dyn_cast<Archive>(&Bin)) {
2308 outs() << "Archive : " << Filename << "\n";
2309 if (ArchiveHeaders)
2310 printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
2312 Error Err = Error::success();
2313 for (auto &C : A->children(Err)) {
2314 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2315 if (!ChildOrErr) {
2316 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2317 report_error(std::move(E), Filename, C);
2318 continue;
2320 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
2321 if (!checkMachOAndArchFlags(O, Filename))
2322 return;
2323 ProcessMachO(Filename, O, O->getFileName());
2326 if (Err)
2327 report_error(std::move(Err), Filename);
2328 return;
2330 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
2331 parseInputMachO(UB);
2332 return;
2334 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
2335 if (!checkMachOAndArchFlags(O, Filename))
2336 return;
2337 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O))
2338 ProcessMachO(Filename, MachOOF);
2339 else
2340 WithColor::error(errs(), "llvm-objdump")
2341 << Filename << "': "
2342 << "object is not a Mach-O file type.\n";
2343 return;
2345 llvm_unreachable("Input object can't be invalid at this point");
2348 void parseInputMachO(MachOUniversalBinary *UB) {
2349 if (!ValidateArchFlags())
2350 return;
2352 auto Filename = UB->getFileName();
2354 if (UniversalHeaders)
2355 printMachOUniversalHeaders(UB, !NonVerbose);
2357 // If we have a list of architecture flags specified dump only those.
2358 if (!ArchAll && !ArchFlags.empty()) {
2359 // Look for a slice in the universal binary that matches each ArchFlag.
2360 bool ArchFound;
2361 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
2362 ArchFound = false;
2363 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2364 E = UB->end_objects();
2365 I != E; ++I) {
2366 if (ArchFlags[i] == I->getArchFlagName()) {
2367 ArchFound = true;
2368 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
2369 I->getAsObjectFile();
2370 std::string ArchitectureName = "";
2371 if (ArchFlags.size() > 1)
2372 ArchitectureName = I->getArchFlagName();
2373 if (ObjOrErr) {
2374 ObjectFile &O = *ObjOrErr.get();
2375 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
2376 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
2377 } else if (Error E = isNotObjectErrorInvalidFileType(
2378 ObjOrErr.takeError())) {
2379 report_error(std::move(E), Filename, StringRef(), ArchitectureName);
2380 continue;
2381 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
2382 I->getAsArchive()) {
2383 std::unique_ptr<Archive> &A = *AOrErr;
2384 outs() << "Archive : " << Filename;
2385 if (!ArchitectureName.empty())
2386 outs() << " (architecture " << ArchitectureName << ")";
2387 outs() << "\n";
2388 if (ArchiveHeaders)
2389 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2390 ArchiveMemberOffsets, ArchitectureName);
2391 Error Err = Error::success();
2392 for (auto &C : A->children(Err)) {
2393 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2394 if (!ChildOrErr) {
2395 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2396 report_error(std::move(E), Filename, C, ArchitectureName);
2397 continue;
2399 if (MachOObjectFile *O =
2400 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
2401 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
2403 if (Err)
2404 report_error(std::move(Err), Filename);
2405 } else {
2406 consumeError(AOrErr.takeError());
2407 error("Mach-O universal file: " + Filename + " for " +
2408 "architecture " + StringRef(I->getArchFlagName()) +
2409 " is not a Mach-O file or an archive file");
2413 if (!ArchFound) {
2414 WithColor::error(errs(), "llvm-objdump")
2415 << "file: " + Filename + " does not contain "
2416 << "architecture: " + ArchFlags[i] + "\n";
2417 return;
2420 return;
2422 // No architecture flags were specified so if this contains a slice that
2423 // matches the host architecture dump only that.
2424 if (!ArchAll) {
2425 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2426 E = UB->end_objects();
2427 I != E; ++I) {
2428 if (MachOObjectFile::getHostArch().getArchName() ==
2429 I->getArchFlagName()) {
2430 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
2431 std::string ArchiveName;
2432 ArchiveName.clear();
2433 if (ObjOrErr) {
2434 ObjectFile &O = *ObjOrErr.get();
2435 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
2436 ProcessMachO(Filename, MachOOF);
2437 } else if (Error E =
2438 isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
2439 report_error(std::move(E), Filename);
2440 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
2441 I->getAsArchive()) {
2442 std::unique_ptr<Archive> &A = *AOrErr;
2443 outs() << "Archive : " << Filename << "\n";
2444 if (ArchiveHeaders)
2445 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2446 ArchiveMemberOffsets);
2447 Error Err = Error::success();
2448 for (auto &C : A->children(Err)) {
2449 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2450 if (!ChildOrErr) {
2451 if (Error E =
2452 isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2453 report_error(std::move(E), Filename, C);
2454 continue;
2456 if (MachOObjectFile *O =
2457 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
2458 ProcessMachO(Filename, O, O->getFileName());
2460 if (Err)
2461 report_error(std::move(Err), Filename);
2462 } else {
2463 consumeError(AOrErr.takeError());
2464 error("Mach-O universal file: " + Filename + " for architecture " +
2465 StringRef(I->getArchFlagName()) +
2466 " is not a Mach-O file or an archive file");
2468 return;
2472 // Either all architectures have been specified or none have been specified
2473 // and this does not contain the host architecture so dump all the slices.
2474 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
2475 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2476 E = UB->end_objects();
2477 I != E; ++I) {
2478 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
2479 std::string ArchitectureName = "";
2480 if (moreThanOneArch)
2481 ArchitectureName = I->getArchFlagName();
2482 if (ObjOrErr) {
2483 ObjectFile &Obj = *ObjOrErr.get();
2484 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
2485 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
2486 } else if (Error E =
2487 isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
2488 report_error(std::move(E), StringRef(), Filename, ArchitectureName);
2489 } else if (Expected<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
2490 std::unique_ptr<Archive> &A = *AOrErr;
2491 outs() << "Archive : " << Filename;
2492 if (!ArchitectureName.empty())
2493 outs() << " (architecture " << ArchitectureName << ")";
2494 outs() << "\n";
2495 if (ArchiveHeaders)
2496 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2497 ArchiveMemberOffsets, ArchitectureName);
2498 Error Err = Error::success();
2499 for (auto &C : A->children(Err)) {
2500 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2501 if (!ChildOrErr) {
2502 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2503 report_error(std::move(E), Filename, C, ArchitectureName);
2504 continue;
2506 if (MachOObjectFile *O =
2507 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
2508 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
2509 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
2510 ArchitectureName);
2513 if (Err)
2514 report_error(std::move(Err), Filename);
2515 } else {
2516 consumeError(AOrErr.takeError());
2517 error("Mach-O universal file: " + Filename + " for architecture " +
2518 StringRef(I->getArchFlagName()) +
2519 " is not a Mach-O file or an archive file");
2524 // The block of info used by the Symbolizer call backs.
2525 struct DisassembleInfo {
2526 DisassembleInfo(MachOObjectFile *O, SymbolAddressMap *AddrMap,
2527 std::vector<SectionRef> *Sections, bool verbose)
2528 : verbose(verbose), O(O), AddrMap(AddrMap), Sections(Sections) {}
2529 bool verbose;
2530 MachOObjectFile *O;
2531 SectionRef S;
2532 SymbolAddressMap *AddrMap;
2533 std::vector<SectionRef> *Sections;
2534 const char *class_name = nullptr;
2535 const char *selector_name = nullptr;
2536 std::unique_ptr<char[]> method = nullptr;
2537 char *demangled_name = nullptr;
2538 uint64_t adrp_addr = 0;
2539 uint32_t adrp_inst = 0;
2540 std::unique_ptr<SymbolAddressMap> bindtable;
2541 uint32_t depth = 0;
2544 // SymbolizerGetOpInfo() is the operand information call back function.
2545 // This is called to get the symbolic information for operand(s) of an
2546 // instruction when it is being done. This routine does this from
2547 // the relocation information, symbol table, etc. That block of information
2548 // is a pointer to the struct DisassembleInfo that was passed when the
2549 // disassembler context was created and passed to back to here when
2550 // called back by the disassembler for instruction operands that could have
2551 // relocation information. The address of the instruction containing operand is
2552 // at the Pc parameter. The immediate value the operand has is passed in
2553 // op_info->Value and is at Offset past the start of the instruction and has a
2554 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
2555 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
2556 // names and addends of the symbolic expression to add for the operand. The
2557 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
2558 // information is returned then this function returns 1 else it returns 0.
2559 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
2560 uint64_t Size, int TagType, void *TagBuf) {
2561 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
2562 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
2563 uint64_t value = op_info->Value;
2565 // Make sure all fields returned are zero if we don't set them.
2566 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
2567 op_info->Value = value;
2569 // If the TagType is not the value 1 which it code knows about or if no
2570 // verbose symbolic information is wanted then just return 0, indicating no
2571 // information is being returned.
2572 if (TagType != 1 || !info->verbose)
2573 return 0;
2575 unsigned int Arch = info->O->getArch();
2576 if (Arch == Triple::x86) {
2577 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
2578 return 0;
2579 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2580 // TODO:
2581 // Search the external relocation entries of a fully linked image
2582 // (if any) for an entry that matches this segment offset.
2583 // uint32_t seg_offset = (Pc + Offset);
2584 return 0;
2586 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2587 // for an entry for this section offset.
2588 uint32_t sect_addr = info->S.getAddress();
2589 uint32_t sect_offset = (Pc + Offset) - sect_addr;
2590 bool reloc_found = false;
2591 DataRefImpl Rel;
2592 MachO::any_relocation_info RE;
2593 bool isExtern = false;
2594 SymbolRef Symbol;
2595 bool r_scattered = false;
2596 uint32_t r_value, pair_r_value, r_type;
2597 for (const RelocationRef &Reloc : info->S.relocations()) {
2598 uint64_t RelocOffset = Reloc.getOffset();
2599 if (RelocOffset == sect_offset) {
2600 Rel = Reloc.getRawDataRefImpl();
2601 RE = info->O->getRelocation(Rel);
2602 r_type = info->O->getAnyRelocationType(RE);
2603 r_scattered = info->O->isRelocationScattered(RE);
2604 if (r_scattered) {
2605 r_value = info->O->getScatteredRelocationValue(RE);
2606 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
2607 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
2608 DataRefImpl RelNext = Rel;
2609 info->O->moveRelocationNext(RelNext);
2610 MachO::any_relocation_info RENext;
2611 RENext = info->O->getRelocation(RelNext);
2612 if (info->O->isRelocationScattered(RENext))
2613 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2614 else
2615 return 0;
2617 } else {
2618 isExtern = info->O->getPlainRelocationExternal(RE);
2619 if (isExtern) {
2620 symbol_iterator RelocSym = Reloc.getSymbol();
2621 Symbol = *RelocSym;
2624 reloc_found = true;
2625 break;
2628 if (reloc_found && isExtern) {
2629 op_info->AddSymbol.Present = 1;
2630 op_info->AddSymbol.Name =
2631 unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2632 // For i386 extern relocation entries the value in the instruction is
2633 // the offset from the symbol, and value is already set in op_info->Value.
2634 return 1;
2636 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
2637 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
2638 const char *add = GuessSymbolName(r_value, info->AddrMap);
2639 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2640 uint32_t offset = value - (r_value - pair_r_value);
2641 op_info->AddSymbol.Present = 1;
2642 if (add != nullptr)
2643 op_info->AddSymbol.Name = add;
2644 else
2645 op_info->AddSymbol.Value = r_value;
2646 op_info->SubtractSymbol.Present = 1;
2647 if (sub != nullptr)
2648 op_info->SubtractSymbol.Name = sub;
2649 else
2650 op_info->SubtractSymbol.Value = pair_r_value;
2651 op_info->Value = offset;
2652 return 1;
2654 return 0;
2656 if (Arch == Triple::x86_64) {
2657 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
2658 return 0;
2659 // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
2660 // relocation entries of a linked image (if any) for an entry that matches
2661 // this segment offset.
2662 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2663 uint64_t seg_offset = Pc + Offset;
2664 bool reloc_found = false;
2665 DataRefImpl Rel;
2666 MachO::any_relocation_info RE;
2667 bool isExtern = false;
2668 SymbolRef Symbol;
2669 for (const RelocationRef &Reloc : info->O->external_relocations()) {
2670 uint64_t RelocOffset = Reloc.getOffset();
2671 if (RelocOffset == seg_offset) {
2672 Rel = Reloc.getRawDataRefImpl();
2673 RE = info->O->getRelocation(Rel);
2674 // external relocation entries should always be external.
2675 isExtern = info->O->getPlainRelocationExternal(RE);
2676 if (isExtern) {
2677 symbol_iterator RelocSym = Reloc.getSymbol();
2678 Symbol = *RelocSym;
2680 reloc_found = true;
2681 break;
2684 if (reloc_found && isExtern) {
2685 // The Value passed in will be adjusted by the Pc if the instruction
2686 // adds the Pc. But for x86_64 external relocation entries the Value
2687 // is the offset from the external symbol.
2688 if (info->O->getAnyRelocationPCRel(RE))
2689 op_info->Value -= Pc + Offset + Size;
2690 const char *name =
2691 unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2692 op_info->AddSymbol.Present = 1;
2693 op_info->AddSymbol.Name = name;
2694 return 1;
2696 return 0;
2698 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2699 // for an entry for this section offset.
2700 uint64_t sect_addr = info->S.getAddress();
2701 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2702 bool reloc_found = false;
2703 DataRefImpl Rel;
2704 MachO::any_relocation_info RE;
2705 bool isExtern = false;
2706 SymbolRef Symbol;
2707 for (const RelocationRef &Reloc : info->S.relocations()) {
2708 uint64_t RelocOffset = Reloc.getOffset();
2709 if (RelocOffset == sect_offset) {
2710 Rel = Reloc.getRawDataRefImpl();
2711 RE = info->O->getRelocation(Rel);
2712 // NOTE: Scattered relocations don't exist on x86_64.
2713 isExtern = info->O->getPlainRelocationExternal(RE);
2714 if (isExtern) {
2715 symbol_iterator RelocSym = Reloc.getSymbol();
2716 Symbol = *RelocSym;
2718 reloc_found = true;
2719 break;
2722 if (reloc_found && isExtern) {
2723 // The Value passed in will be adjusted by the Pc if the instruction
2724 // adds the Pc. But for x86_64 external relocation entries the Value
2725 // is the offset from the external symbol.
2726 if (info->O->getAnyRelocationPCRel(RE))
2727 op_info->Value -= Pc + Offset + Size;
2728 const char *name =
2729 unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2730 unsigned Type = info->O->getAnyRelocationType(RE);
2731 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
2732 DataRefImpl RelNext = Rel;
2733 info->O->moveRelocationNext(RelNext);
2734 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2735 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
2736 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
2737 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
2738 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
2739 op_info->SubtractSymbol.Present = 1;
2740 op_info->SubtractSymbol.Name = name;
2741 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
2742 Symbol = *RelocSymNext;
2743 name = unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2746 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2747 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2748 op_info->AddSymbol.Present = 1;
2749 op_info->AddSymbol.Name = name;
2750 return 1;
2752 return 0;
2754 if (Arch == Triple::arm) {
2755 if (Offset != 0 || (Size != 4 && Size != 2))
2756 return 0;
2757 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2758 // TODO:
2759 // Search the external relocation entries of a fully linked image
2760 // (if any) for an entry that matches this segment offset.
2761 // uint32_t seg_offset = (Pc + Offset);
2762 return 0;
2764 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2765 // for an entry for this section offset.
2766 uint32_t sect_addr = info->S.getAddress();
2767 uint32_t sect_offset = (Pc + Offset) - sect_addr;
2768 DataRefImpl Rel;
2769 MachO::any_relocation_info RE;
2770 bool isExtern = false;
2771 SymbolRef Symbol;
2772 bool r_scattered = false;
2773 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
2774 auto Reloc =
2775 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2776 uint64_t RelocOffset = Reloc.getOffset();
2777 return RelocOffset == sect_offset;
2780 if (Reloc == info->S.relocations().end())
2781 return 0;
2783 Rel = Reloc->getRawDataRefImpl();
2784 RE = info->O->getRelocation(Rel);
2785 r_length = info->O->getAnyRelocationLength(RE);
2786 r_scattered = info->O->isRelocationScattered(RE);
2787 if (r_scattered) {
2788 r_value = info->O->getScatteredRelocationValue(RE);
2789 r_type = info->O->getScatteredRelocationType(RE);
2790 } else {
2791 r_type = info->O->getAnyRelocationType(RE);
2792 isExtern = info->O->getPlainRelocationExternal(RE);
2793 if (isExtern) {
2794 symbol_iterator RelocSym = Reloc->getSymbol();
2795 Symbol = *RelocSym;
2798 if (r_type == MachO::ARM_RELOC_HALF ||
2799 r_type == MachO::ARM_RELOC_SECTDIFF ||
2800 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2801 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2802 DataRefImpl RelNext = Rel;
2803 info->O->moveRelocationNext(RelNext);
2804 MachO::any_relocation_info RENext;
2805 RENext = info->O->getRelocation(RelNext);
2806 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
2807 if (info->O->isRelocationScattered(RENext))
2808 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2811 if (isExtern) {
2812 const char *name =
2813 unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2814 op_info->AddSymbol.Present = 1;
2815 op_info->AddSymbol.Name = name;
2816 switch (r_type) {
2817 case MachO::ARM_RELOC_HALF:
2818 if ((r_length & 0x1) == 1) {
2819 op_info->Value = value << 16 | other_half;
2820 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2821 } else {
2822 op_info->Value = other_half << 16 | value;
2823 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2825 break;
2826 default:
2827 break;
2829 return 1;
2831 // If we have a branch that is not an external relocation entry then
2832 // return 0 so the code in tryAddingSymbolicOperand() can use the
2833 // SymbolLookUp call back with the branch target address to look up the
2834 // symbol and possibility add an annotation for a symbol stub.
2835 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2836 r_type == MachO::ARM_THUMB_RELOC_BR22))
2837 return 0;
2839 uint32_t offset = 0;
2840 if (r_type == MachO::ARM_RELOC_HALF ||
2841 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2842 if ((r_length & 0x1) == 1)
2843 value = value << 16 | other_half;
2844 else
2845 value = other_half << 16 | value;
2847 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2848 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2849 offset = value - r_value;
2850 value = r_value;
2853 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2854 if ((r_length & 0x1) == 1)
2855 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2856 else
2857 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2858 const char *add = GuessSymbolName(r_value, info->AddrMap);
2859 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2860 int32_t offset = value - (r_value - pair_r_value);
2861 op_info->AddSymbol.Present = 1;
2862 if (add != nullptr)
2863 op_info->AddSymbol.Name = add;
2864 else
2865 op_info->AddSymbol.Value = r_value;
2866 op_info->SubtractSymbol.Present = 1;
2867 if (sub != nullptr)
2868 op_info->SubtractSymbol.Name = sub;
2869 else
2870 op_info->SubtractSymbol.Value = pair_r_value;
2871 op_info->Value = offset;
2872 return 1;
2875 op_info->AddSymbol.Present = 1;
2876 op_info->Value = offset;
2877 if (r_type == MachO::ARM_RELOC_HALF) {
2878 if ((r_length & 0x1) == 1)
2879 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2880 else
2881 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2883 const char *add = GuessSymbolName(value, info->AddrMap);
2884 if (add != nullptr) {
2885 op_info->AddSymbol.Name = add;
2886 return 1;
2888 op_info->AddSymbol.Value = value;
2889 return 1;
2891 if (Arch == Triple::aarch64) {
2892 if (Offset != 0 || Size != 4)
2893 return 0;
2894 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2895 // TODO:
2896 // Search the external relocation entries of a fully linked image
2897 // (if any) for an entry that matches this segment offset.
2898 // uint64_t seg_offset = (Pc + Offset);
2899 return 0;
2901 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2902 // for an entry for this section offset.
2903 uint64_t sect_addr = info->S.getAddress();
2904 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2905 auto Reloc =
2906 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2907 uint64_t RelocOffset = Reloc.getOffset();
2908 return RelocOffset == sect_offset;
2911 if (Reloc == info->S.relocations().end())
2912 return 0;
2914 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2915 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2916 uint32_t r_type = info->O->getAnyRelocationType(RE);
2917 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2918 DataRefImpl RelNext = Rel;
2919 info->O->moveRelocationNext(RelNext);
2920 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2921 if (value == 0) {
2922 value = info->O->getPlainRelocationSymbolNum(RENext);
2923 op_info->Value = value;
2926 // NOTE: Scattered relocations don't exist on arm64.
2927 if (!info->O->getPlainRelocationExternal(RE))
2928 return 0;
2929 const char *name =
2930 unwrapOrError(Reloc->getSymbol()->getName(), info->O->getFileName())
2931 .data();
2932 op_info->AddSymbol.Present = 1;
2933 op_info->AddSymbol.Name = name;
2935 switch (r_type) {
2936 case MachO::ARM64_RELOC_PAGE21:
2937 /* @page */
2938 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2939 break;
2940 case MachO::ARM64_RELOC_PAGEOFF12:
2941 /* @pageoff */
2942 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2943 break;
2944 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2945 /* @gotpage */
2946 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2947 break;
2948 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2949 /* @gotpageoff */
2950 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2951 break;
2952 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2953 /* @tvlppage is not implemented in llvm-mc */
2954 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2955 break;
2956 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2957 /* @tvlppageoff is not implemented in llvm-mc */
2958 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2959 break;
2960 default:
2961 case MachO::ARM64_RELOC_BRANCH26:
2962 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2963 break;
2965 return 1;
2967 return 0;
2970 // GuessCstringPointer is passed the address of what might be a pointer to a
2971 // literal string in a cstring section. If that address is in a cstring section
2972 // it returns a pointer to that string. Else it returns nullptr.
2973 static const char *GuessCstringPointer(uint64_t ReferenceValue,
2974 struct DisassembleInfo *info) {
2975 for (const auto &Load : info->O->load_commands()) {
2976 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2977 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2978 for (unsigned J = 0; J < Seg.nsects; ++J) {
2979 MachO::section_64 Sec = info->O->getSection64(Load, J);
2980 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2981 if (section_type == MachO::S_CSTRING_LITERALS &&
2982 ReferenceValue >= Sec.addr &&
2983 ReferenceValue < Sec.addr + Sec.size) {
2984 uint64_t sect_offset = ReferenceValue - Sec.addr;
2985 uint64_t object_offset = Sec.offset + sect_offset;
2986 StringRef MachOContents = info->O->getData();
2987 uint64_t object_size = MachOContents.size();
2988 const char *object_addr = (const char *)MachOContents.data();
2989 if (object_offset < object_size) {
2990 const char *name = object_addr + object_offset;
2991 return name;
2992 } else {
2993 return nullptr;
2997 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2998 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2999 for (unsigned J = 0; J < Seg.nsects; ++J) {
3000 MachO::section Sec = info->O->getSection(Load, J);
3001 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
3002 if (section_type == MachO::S_CSTRING_LITERALS &&
3003 ReferenceValue >= Sec.addr &&
3004 ReferenceValue < Sec.addr + Sec.size) {
3005 uint64_t sect_offset = ReferenceValue - Sec.addr;
3006 uint64_t object_offset = Sec.offset + sect_offset;
3007 StringRef MachOContents = info->O->getData();
3008 uint64_t object_size = MachOContents.size();
3009 const char *object_addr = (const char *)MachOContents.data();
3010 if (object_offset < object_size) {
3011 const char *name = object_addr + object_offset;
3012 return name;
3013 } else {
3014 return nullptr;
3020 return nullptr;
3023 // GuessIndirectSymbol returns the name of the indirect symbol for the
3024 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
3025 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
3026 // symbol name being referenced by the stub or pointer.
3027 static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
3028 struct DisassembleInfo *info) {
3029 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
3030 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
3031 for (const auto &Load : info->O->load_commands()) {
3032 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
3033 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
3034 for (unsigned J = 0; J < Seg.nsects; ++J) {
3035 MachO::section_64 Sec = info->O->getSection64(Load, J);
3036 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
3037 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
3038 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
3039 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
3040 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
3041 section_type == MachO::S_SYMBOL_STUBS) &&
3042 ReferenceValue >= Sec.addr &&
3043 ReferenceValue < Sec.addr + Sec.size) {
3044 uint32_t stride;
3045 if (section_type == MachO::S_SYMBOL_STUBS)
3046 stride = Sec.reserved2;
3047 else
3048 stride = 8;
3049 if (stride == 0)
3050 return nullptr;
3051 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
3052 if (index < Dysymtab.nindirectsyms) {
3053 uint32_t indirect_symbol =
3054 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
3055 if (indirect_symbol < Symtab.nsyms) {
3056 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
3057 return unwrapOrError(Sym->getName(), info->O->getFileName())
3058 .data();
3063 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
3064 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
3065 for (unsigned J = 0; J < Seg.nsects; ++J) {
3066 MachO::section Sec = info->O->getSection(Load, J);
3067 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
3068 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
3069 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
3070 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
3071 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
3072 section_type == MachO::S_SYMBOL_STUBS) &&
3073 ReferenceValue >= Sec.addr &&
3074 ReferenceValue < Sec.addr + Sec.size) {
3075 uint32_t stride;
3076 if (section_type == MachO::S_SYMBOL_STUBS)
3077 stride = Sec.reserved2;
3078 else
3079 stride = 4;
3080 if (stride == 0)
3081 return nullptr;
3082 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
3083 if (index < Dysymtab.nindirectsyms) {
3084 uint32_t indirect_symbol =
3085 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
3086 if (indirect_symbol < Symtab.nsyms) {
3087 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
3088 return unwrapOrError(Sym->getName(), info->O->getFileName())
3089 .data();
3096 return nullptr;
3099 // method_reference() is called passing it the ReferenceName that might be
3100 // a reference it to an Objective-C method call. If so then it allocates and
3101 // assembles a method call string with the values last seen and saved in
3102 // the DisassembleInfo's class_name and selector_name fields. This is saved
3103 // into the method field of the info and any previous string is free'ed.
3104 // Then the class_name field in the info is set to nullptr. The method call
3105 // string is set into ReferenceName and ReferenceType is set to
3106 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
3107 // then both ReferenceType and ReferenceName are left unchanged.
3108 static void method_reference(struct DisassembleInfo *info,
3109 uint64_t *ReferenceType,
3110 const char **ReferenceName) {
3111 unsigned int Arch = info->O->getArch();
3112 if (*ReferenceName != nullptr) {
3113 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
3114 if (info->selector_name != nullptr) {
3115 if (info->class_name != nullptr) {
3116 info->method = llvm::make_unique<char[]>(
3117 5 + strlen(info->class_name) + strlen(info->selector_name));
3118 char *method = info->method.get();
3119 if (method != nullptr) {
3120 strcpy(method, "+[");
3121 strcat(method, info->class_name);
3122 strcat(method, " ");
3123 strcat(method, info->selector_name);
3124 strcat(method, "]");
3125 *ReferenceName = method;
3126 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
3128 } else {
3129 info->method =
3130 llvm::make_unique<char[]>(9 + strlen(info->selector_name));
3131 char *method = info->method.get();
3132 if (method != nullptr) {
3133 if (Arch == Triple::x86_64)
3134 strcpy(method, "-[%rdi ");
3135 else if (Arch == Triple::aarch64)
3136 strcpy(method, "-[x0 ");
3137 else
3138 strcpy(method, "-[r? ");
3139 strcat(method, info->selector_name);
3140 strcat(method, "]");
3141 *ReferenceName = method;
3142 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
3145 info->class_name = nullptr;
3147 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
3148 if (info->selector_name != nullptr) {
3149 info->method =
3150 llvm::make_unique<char[]>(17 + strlen(info->selector_name));
3151 char *method = info->method.get();
3152 if (method != nullptr) {
3153 if (Arch == Triple::x86_64)
3154 strcpy(method, "-[[%rdi super] ");
3155 else if (Arch == Triple::aarch64)
3156 strcpy(method, "-[[x0 super] ");
3157 else
3158 strcpy(method, "-[[r? super] ");
3159 strcat(method, info->selector_name);
3160 strcat(method, "]");
3161 *ReferenceName = method;
3162 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
3164 info->class_name = nullptr;
3170 // GuessPointerPointer() is passed the address of what might be a pointer to
3171 // a reference to an Objective-C class, selector, message ref or cfstring.
3172 // If so the value of the pointer is returned and one of the booleans are set
3173 // to true. If not zero is returned and all the booleans are set to false.
3174 static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
3175 struct DisassembleInfo *info,
3176 bool &classref, bool &selref, bool &msgref,
3177 bool &cfstring) {
3178 classref = false;
3179 selref = false;
3180 msgref = false;
3181 cfstring = false;
3182 for (const auto &Load : info->O->load_commands()) {
3183 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
3184 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
3185 for (unsigned J = 0; J < Seg.nsects; ++J) {
3186 MachO::section_64 Sec = info->O->getSection64(Load, J);
3187 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
3188 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
3189 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
3190 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
3191 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
3192 ReferenceValue >= Sec.addr &&
3193 ReferenceValue < Sec.addr + Sec.size) {
3194 uint64_t sect_offset = ReferenceValue - Sec.addr;
3195 uint64_t object_offset = Sec.offset + sect_offset;
3196 StringRef MachOContents = info->O->getData();
3197 uint64_t object_size = MachOContents.size();
3198 const char *object_addr = (const char *)MachOContents.data();
3199 if (object_offset < object_size) {
3200 uint64_t pointer_value;
3201 memcpy(&pointer_value, object_addr + object_offset,
3202 sizeof(uint64_t));
3203 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3204 sys::swapByteOrder(pointer_value);
3205 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
3206 selref = true;
3207 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
3208 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
3209 classref = true;
3210 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
3211 ReferenceValue + 8 < Sec.addr + Sec.size) {
3212 msgref = true;
3213 memcpy(&pointer_value, object_addr + object_offset + 8,
3214 sizeof(uint64_t));
3215 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3216 sys::swapByteOrder(pointer_value);
3217 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
3218 cfstring = true;
3219 return pointer_value;
3220 } else {
3221 return 0;
3226 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
3228 return 0;
3231 // get_pointer_64 returns a pointer to the bytes in the object file at the
3232 // Address from a section in the Mach-O file. And indirectly returns the
3233 // offset into the section, number of bytes left in the section past the offset
3234 // and which section is was being referenced. If the Address is not in a
3235 // section nullptr is returned.
3236 static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
3237 uint32_t &left, SectionRef &S,
3238 DisassembleInfo *info,
3239 bool objc_only = false) {
3240 offset = 0;
3241 left = 0;
3242 S = SectionRef();
3243 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
3244 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
3245 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
3246 if (SectSize == 0)
3247 continue;
3248 if (objc_only) {
3249 StringRef SectName;
3250 ((*(info->Sections))[SectIdx]).getName(SectName);
3251 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
3252 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
3253 if (SegName != "__OBJC" && SectName != "__cstring")
3254 continue;
3256 if (Address >= SectAddress && Address < SectAddress + SectSize) {
3257 S = (*(info->Sections))[SectIdx];
3258 offset = Address - SectAddress;
3259 left = SectSize - offset;
3260 StringRef SectContents = unwrapOrError(
3261 ((*(info->Sections))[SectIdx]).getContents(), info->O->getFileName());
3262 return SectContents.data() + offset;
3265 return nullptr;
3268 static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
3269 uint32_t &left, SectionRef &S,
3270 DisassembleInfo *info,
3271 bool objc_only = false) {
3272 return get_pointer_64(Address, offset, left, S, info, objc_only);
3275 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
3276 // the symbol indirectly through n_value. Based on the relocation information
3277 // for the specified section offset in the specified section reference.
3278 // If no relocation information is found and a non-zero ReferenceValue for the
3279 // symbol is passed, look up that address in the info's AddrMap.
3280 static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
3281 DisassembleInfo *info, uint64_t &n_value,
3282 uint64_t ReferenceValue = 0) {
3283 n_value = 0;
3284 if (!info->verbose)
3285 return nullptr;
3287 // See if there is an external relocation entry at the sect_offset.
3288 bool reloc_found = false;
3289 DataRefImpl Rel;
3290 MachO::any_relocation_info RE;
3291 bool isExtern = false;
3292 SymbolRef Symbol;
3293 for (const RelocationRef &Reloc : S.relocations()) {
3294 uint64_t RelocOffset = Reloc.getOffset();
3295 if (RelocOffset == sect_offset) {
3296 Rel = Reloc.getRawDataRefImpl();
3297 RE = info->O->getRelocation(Rel);
3298 if (info->O->isRelocationScattered(RE))
3299 continue;
3300 isExtern = info->O->getPlainRelocationExternal(RE);
3301 if (isExtern) {
3302 symbol_iterator RelocSym = Reloc.getSymbol();
3303 Symbol = *RelocSym;
3305 reloc_found = true;
3306 break;
3309 // If there is an external relocation entry for a symbol in this section
3310 // at this section_offset then use that symbol's value for the n_value
3311 // and return its name.
3312 const char *SymbolName = nullptr;
3313 if (reloc_found && isExtern) {
3314 n_value = Symbol.getValue();
3315 StringRef Name = unwrapOrError(Symbol.getName(), info->O->getFileName());
3316 if (!Name.empty()) {
3317 SymbolName = Name.data();
3318 return SymbolName;
3322 // TODO: For fully linked images, look through the external relocation
3323 // entries off the dynamic symtab command. For these the r_offset is from the
3324 // start of the first writeable segment in the Mach-O file. So the offset
3325 // to this section from that segment is passed to this routine by the caller,
3326 // as the database_offset. Which is the difference of the section's starting
3327 // address and the first writable segment.
3329 // NOTE: need add passing the database_offset to this routine.
3331 // We did not find an external relocation entry so look up the ReferenceValue
3332 // as an address of a symbol and if found return that symbol's name.
3333 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
3335 return SymbolName;
3338 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
3339 DisassembleInfo *info,
3340 uint32_t ReferenceValue) {
3341 uint64_t n_value64;
3342 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
3345 // These are structs in the Objective-C meta data and read to produce the
3346 // comments for disassembly. While these are part of the ABI they are no
3347 // public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
3348 // .
3350 // The cfstring object in a 64-bit Mach-O file.
3351 struct cfstring64_t {
3352 uint64_t isa; // class64_t * (64-bit pointer)
3353 uint64_t flags; // flag bits
3354 uint64_t characters; // char * (64-bit pointer)
3355 uint64_t length; // number of non-NULL characters in above
3358 // The class object in a 64-bit Mach-O file.
3359 struct class64_t {
3360 uint64_t isa; // class64_t * (64-bit pointer)
3361 uint64_t superclass; // class64_t * (64-bit pointer)
3362 uint64_t cache; // Cache (64-bit pointer)
3363 uint64_t vtable; // IMP * (64-bit pointer)
3364 uint64_t data; // class_ro64_t * (64-bit pointer)
3367 struct class32_t {
3368 uint32_t isa; /* class32_t * (32-bit pointer) */
3369 uint32_t superclass; /* class32_t * (32-bit pointer) */
3370 uint32_t cache; /* Cache (32-bit pointer) */
3371 uint32_t vtable; /* IMP * (32-bit pointer) */
3372 uint32_t data; /* class_ro32_t * (32-bit pointer) */
3375 struct class_ro64_t {
3376 uint32_t flags;
3377 uint32_t instanceStart;
3378 uint32_t instanceSize;
3379 uint32_t reserved;
3380 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
3381 uint64_t name; // const char * (64-bit pointer)
3382 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
3383 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
3384 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
3385 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
3386 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
3389 struct class_ro32_t {
3390 uint32_t flags;
3391 uint32_t instanceStart;
3392 uint32_t instanceSize;
3393 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
3394 uint32_t name; /* const char * (32-bit pointer) */
3395 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
3396 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
3397 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
3398 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
3399 uint32_t baseProperties; /* const struct objc_property_list *
3400 (32-bit pointer) */
3403 /* Values for class_ro{64,32}_t->flags */
3404 #define RO_META (1 << 0)
3405 #define RO_ROOT (1 << 1)
3406 #define RO_HAS_CXX_STRUCTORS (1 << 2)
3408 struct method_list64_t {
3409 uint32_t entsize;
3410 uint32_t count;
3411 /* struct method64_t first; These structures follow inline */
3414 struct method_list32_t {
3415 uint32_t entsize;
3416 uint32_t count;
3417 /* struct method32_t first; These structures follow inline */
3420 struct method64_t {
3421 uint64_t name; /* SEL (64-bit pointer) */
3422 uint64_t types; /* const char * (64-bit pointer) */
3423 uint64_t imp; /* IMP (64-bit pointer) */
3426 struct method32_t {
3427 uint32_t name; /* SEL (32-bit pointer) */
3428 uint32_t types; /* const char * (32-bit pointer) */
3429 uint32_t imp; /* IMP (32-bit pointer) */
3432 struct protocol_list64_t {
3433 uint64_t count; /* uintptr_t (a 64-bit value) */
3434 /* struct protocol64_t * list[0]; These pointers follow inline */
3437 struct protocol_list32_t {
3438 uint32_t count; /* uintptr_t (a 32-bit value) */
3439 /* struct protocol32_t * list[0]; These pointers follow inline */
3442 struct protocol64_t {
3443 uint64_t isa; /* id * (64-bit pointer) */
3444 uint64_t name; /* const char * (64-bit pointer) */
3445 uint64_t protocols; /* struct protocol_list64_t *
3446 (64-bit pointer) */
3447 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
3448 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
3449 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
3450 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
3451 uint64_t instanceProperties; /* struct objc_property_list *
3452 (64-bit pointer) */
3455 struct protocol32_t {
3456 uint32_t isa; /* id * (32-bit pointer) */
3457 uint32_t name; /* const char * (32-bit pointer) */
3458 uint32_t protocols; /* struct protocol_list_t *
3459 (32-bit pointer) */
3460 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
3461 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
3462 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
3463 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
3464 uint32_t instanceProperties; /* struct objc_property_list *
3465 (32-bit pointer) */
3468 struct ivar_list64_t {
3469 uint32_t entsize;
3470 uint32_t count;
3471 /* struct ivar64_t first; These structures follow inline */
3474 struct ivar_list32_t {
3475 uint32_t entsize;
3476 uint32_t count;
3477 /* struct ivar32_t first; These structures follow inline */
3480 struct ivar64_t {
3481 uint64_t offset; /* uintptr_t * (64-bit pointer) */
3482 uint64_t name; /* const char * (64-bit pointer) */
3483 uint64_t type; /* const char * (64-bit pointer) */
3484 uint32_t alignment;
3485 uint32_t size;
3488 struct ivar32_t {
3489 uint32_t offset; /* uintptr_t * (32-bit pointer) */
3490 uint32_t name; /* const char * (32-bit pointer) */
3491 uint32_t type; /* const char * (32-bit pointer) */
3492 uint32_t alignment;
3493 uint32_t size;
3496 struct objc_property_list64 {
3497 uint32_t entsize;
3498 uint32_t count;
3499 /* struct objc_property64 first; These structures follow inline */
3502 struct objc_property_list32 {
3503 uint32_t entsize;
3504 uint32_t count;
3505 /* struct objc_property32 first; These structures follow inline */
3508 struct objc_property64 {
3509 uint64_t name; /* const char * (64-bit pointer) */
3510 uint64_t attributes; /* const char * (64-bit pointer) */
3513 struct objc_property32 {
3514 uint32_t name; /* const char * (32-bit pointer) */
3515 uint32_t attributes; /* const char * (32-bit pointer) */
3518 struct category64_t {
3519 uint64_t name; /* const char * (64-bit pointer) */
3520 uint64_t cls; /* struct class_t * (64-bit pointer) */
3521 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
3522 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
3523 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
3524 uint64_t instanceProperties; /* struct objc_property_list *
3525 (64-bit pointer) */
3528 struct category32_t {
3529 uint32_t name; /* const char * (32-bit pointer) */
3530 uint32_t cls; /* struct class_t * (32-bit pointer) */
3531 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
3532 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
3533 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
3534 uint32_t instanceProperties; /* struct objc_property_list *
3535 (32-bit pointer) */
3538 struct objc_image_info64 {
3539 uint32_t version;
3540 uint32_t flags;
3542 struct objc_image_info32 {
3543 uint32_t version;
3544 uint32_t flags;
3546 struct imageInfo_t {
3547 uint32_t version;
3548 uint32_t flags;
3550 /* masks for objc_image_info.flags */
3551 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
3552 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
3553 #define OBJC_IMAGE_IS_SIMULATED (1 << 5)
3554 #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES (1 << 6)
3556 struct message_ref64 {
3557 uint64_t imp; /* IMP (64-bit pointer) */
3558 uint64_t sel; /* SEL (64-bit pointer) */
3561 struct message_ref32 {
3562 uint32_t imp; /* IMP (32-bit pointer) */
3563 uint32_t sel; /* SEL (32-bit pointer) */
3566 // Objective-C 1 (32-bit only) meta data structs.
3568 struct objc_module_t {
3569 uint32_t version;
3570 uint32_t size;
3571 uint32_t name; /* char * (32-bit pointer) */
3572 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
3575 struct objc_symtab_t {
3576 uint32_t sel_ref_cnt;
3577 uint32_t refs; /* SEL * (32-bit pointer) */
3578 uint16_t cls_def_cnt;
3579 uint16_t cat_def_cnt;
3580 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
3583 struct objc_class_t {
3584 uint32_t isa; /* struct objc_class * (32-bit pointer) */
3585 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
3586 uint32_t name; /* const char * (32-bit pointer) */
3587 int32_t version;
3588 int32_t info;
3589 int32_t instance_size;
3590 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
3591 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
3592 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
3593 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
3596 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
3597 // class is not a metaclass
3598 #define CLS_CLASS 0x1
3599 // class is a metaclass
3600 #define CLS_META 0x2
3602 struct objc_category_t {
3603 uint32_t category_name; /* char * (32-bit pointer) */
3604 uint32_t class_name; /* char * (32-bit pointer) */
3605 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
3606 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
3607 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
3610 struct objc_ivar_t {
3611 uint32_t ivar_name; /* char * (32-bit pointer) */
3612 uint32_t ivar_type; /* char * (32-bit pointer) */
3613 int32_t ivar_offset;
3616 struct objc_ivar_list_t {
3617 int32_t ivar_count;
3618 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
3621 struct objc_method_list_t {
3622 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
3623 int32_t method_count;
3624 // struct objc_method_t method_list[1]; /* variable length structure */
3627 struct objc_method_t {
3628 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
3629 uint32_t method_types; /* char * (32-bit pointer) */
3630 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
3631 (32-bit pointer) */
3634 struct objc_protocol_list_t {
3635 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
3636 int32_t count;
3637 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
3638 // (32-bit pointer) */
3641 struct objc_protocol_t {
3642 uint32_t isa; /* struct objc_class * (32-bit pointer) */
3643 uint32_t protocol_name; /* char * (32-bit pointer) */
3644 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
3645 uint32_t instance_methods; /* struct objc_method_description_list *
3646 (32-bit pointer) */
3647 uint32_t class_methods; /* struct objc_method_description_list *
3648 (32-bit pointer) */
3651 struct objc_method_description_list_t {
3652 int32_t count;
3653 // struct objc_method_description_t list[1];
3656 struct objc_method_description_t {
3657 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
3658 uint32_t types; /* char * (32-bit pointer) */
3661 inline void swapStruct(struct cfstring64_t &cfs) {
3662 sys::swapByteOrder(cfs.isa);
3663 sys::swapByteOrder(cfs.flags);
3664 sys::swapByteOrder(cfs.characters);
3665 sys::swapByteOrder(cfs.length);
3668 inline void swapStruct(struct class64_t &c) {
3669 sys::swapByteOrder(c.isa);
3670 sys::swapByteOrder(c.superclass);
3671 sys::swapByteOrder(c.cache);
3672 sys::swapByteOrder(c.vtable);
3673 sys::swapByteOrder(c.data);
3676 inline void swapStruct(struct class32_t &c) {
3677 sys::swapByteOrder(c.isa);
3678 sys::swapByteOrder(c.superclass);
3679 sys::swapByteOrder(c.cache);
3680 sys::swapByteOrder(c.vtable);
3681 sys::swapByteOrder(c.data);
3684 inline void swapStruct(struct class_ro64_t &cro) {
3685 sys::swapByteOrder(cro.flags);
3686 sys::swapByteOrder(cro.instanceStart);
3687 sys::swapByteOrder(cro.instanceSize);
3688 sys::swapByteOrder(cro.reserved);
3689 sys::swapByteOrder(cro.ivarLayout);
3690 sys::swapByteOrder(cro.name);
3691 sys::swapByteOrder(cro.baseMethods);
3692 sys::swapByteOrder(cro.baseProtocols);
3693 sys::swapByteOrder(cro.ivars);
3694 sys::swapByteOrder(cro.weakIvarLayout);
3695 sys::swapByteOrder(cro.baseProperties);
3698 inline void swapStruct(struct class_ro32_t &cro) {
3699 sys::swapByteOrder(cro.flags);
3700 sys::swapByteOrder(cro.instanceStart);
3701 sys::swapByteOrder(cro.instanceSize);
3702 sys::swapByteOrder(cro.ivarLayout);
3703 sys::swapByteOrder(cro.name);
3704 sys::swapByteOrder(cro.baseMethods);
3705 sys::swapByteOrder(cro.baseProtocols);
3706 sys::swapByteOrder(cro.ivars);
3707 sys::swapByteOrder(cro.weakIvarLayout);
3708 sys::swapByteOrder(cro.baseProperties);
3711 inline void swapStruct(struct method_list64_t &ml) {
3712 sys::swapByteOrder(ml.entsize);
3713 sys::swapByteOrder(ml.count);
3716 inline void swapStruct(struct method_list32_t &ml) {
3717 sys::swapByteOrder(ml.entsize);
3718 sys::swapByteOrder(ml.count);
3721 inline void swapStruct(struct method64_t &m) {
3722 sys::swapByteOrder(m.name);
3723 sys::swapByteOrder(m.types);
3724 sys::swapByteOrder(m.imp);
3727 inline void swapStruct(struct method32_t &m) {
3728 sys::swapByteOrder(m.name);
3729 sys::swapByteOrder(m.types);
3730 sys::swapByteOrder(m.imp);
3733 inline void swapStruct(struct protocol_list64_t &pl) {
3734 sys::swapByteOrder(pl.count);
3737 inline void swapStruct(struct protocol_list32_t &pl) {
3738 sys::swapByteOrder(pl.count);
3741 inline void swapStruct(struct protocol64_t &p) {
3742 sys::swapByteOrder(p.isa);
3743 sys::swapByteOrder(p.name);
3744 sys::swapByteOrder(p.protocols);
3745 sys::swapByteOrder(p.instanceMethods);
3746 sys::swapByteOrder(p.classMethods);
3747 sys::swapByteOrder(p.optionalInstanceMethods);
3748 sys::swapByteOrder(p.optionalClassMethods);
3749 sys::swapByteOrder(p.instanceProperties);
3752 inline void swapStruct(struct protocol32_t &p) {
3753 sys::swapByteOrder(p.isa);
3754 sys::swapByteOrder(p.name);
3755 sys::swapByteOrder(p.protocols);
3756 sys::swapByteOrder(p.instanceMethods);
3757 sys::swapByteOrder(p.classMethods);
3758 sys::swapByteOrder(p.optionalInstanceMethods);
3759 sys::swapByteOrder(p.optionalClassMethods);
3760 sys::swapByteOrder(p.instanceProperties);
3763 inline void swapStruct(struct ivar_list64_t &il) {
3764 sys::swapByteOrder(il.entsize);
3765 sys::swapByteOrder(il.count);
3768 inline void swapStruct(struct ivar_list32_t &il) {
3769 sys::swapByteOrder(il.entsize);
3770 sys::swapByteOrder(il.count);
3773 inline void swapStruct(struct ivar64_t &i) {
3774 sys::swapByteOrder(i.offset);
3775 sys::swapByteOrder(i.name);
3776 sys::swapByteOrder(i.type);
3777 sys::swapByteOrder(i.alignment);
3778 sys::swapByteOrder(i.size);
3781 inline void swapStruct(struct ivar32_t &i) {
3782 sys::swapByteOrder(i.offset);
3783 sys::swapByteOrder(i.name);
3784 sys::swapByteOrder(i.type);
3785 sys::swapByteOrder(i.alignment);
3786 sys::swapByteOrder(i.size);
3789 inline void swapStruct(struct objc_property_list64 &pl) {
3790 sys::swapByteOrder(pl.entsize);
3791 sys::swapByteOrder(pl.count);
3794 inline void swapStruct(struct objc_property_list32 &pl) {
3795 sys::swapByteOrder(pl.entsize);
3796 sys::swapByteOrder(pl.count);
3799 inline void swapStruct(struct objc_property64 &op) {
3800 sys::swapByteOrder(op.name);
3801 sys::swapByteOrder(op.attributes);
3804 inline void swapStruct(struct objc_property32 &op) {
3805 sys::swapByteOrder(op.name);
3806 sys::swapByteOrder(op.attributes);
3809 inline void swapStruct(struct category64_t &c) {
3810 sys::swapByteOrder(c.name);
3811 sys::swapByteOrder(c.cls);
3812 sys::swapByteOrder(c.instanceMethods);
3813 sys::swapByteOrder(c.classMethods);
3814 sys::swapByteOrder(c.protocols);
3815 sys::swapByteOrder(c.instanceProperties);
3818 inline void swapStruct(struct category32_t &c) {
3819 sys::swapByteOrder(c.name);
3820 sys::swapByteOrder(c.cls);
3821 sys::swapByteOrder(c.instanceMethods);
3822 sys::swapByteOrder(c.classMethods);
3823 sys::swapByteOrder(c.protocols);
3824 sys::swapByteOrder(c.instanceProperties);
3827 inline void swapStruct(struct objc_image_info64 &o) {
3828 sys::swapByteOrder(o.version);
3829 sys::swapByteOrder(o.flags);
3832 inline void swapStruct(struct objc_image_info32 &o) {
3833 sys::swapByteOrder(o.version);
3834 sys::swapByteOrder(o.flags);
3837 inline void swapStruct(struct imageInfo_t &o) {
3838 sys::swapByteOrder(o.version);
3839 sys::swapByteOrder(o.flags);
3842 inline void swapStruct(struct message_ref64 &mr) {
3843 sys::swapByteOrder(mr.imp);
3844 sys::swapByteOrder(mr.sel);
3847 inline void swapStruct(struct message_ref32 &mr) {
3848 sys::swapByteOrder(mr.imp);
3849 sys::swapByteOrder(mr.sel);
3852 inline void swapStruct(struct objc_module_t &module) {
3853 sys::swapByteOrder(module.version);
3854 sys::swapByteOrder(module.size);
3855 sys::swapByteOrder(module.name);
3856 sys::swapByteOrder(module.symtab);
3859 inline void swapStruct(struct objc_symtab_t &symtab) {
3860 sys::swapByteOrder(symtab.sel_ref_cnt);
3861 sys::swapByteOrder(symtab.refs);
3862 sys::swapByteOrder(symtab.cls_def_cnt);
3863 sys::swapByteOrder(symtab.cat_def_cnt);
3866 inline void swapStruct(struct objc_class_t &objc_class) {
3867 sys::swapByteOrder(objc_class.isa);
3868 sys::swapByteOrder(objc_class.super_class);
3869 sys::swapByteOrder(objc_class.name);
3870 sys::swapByteOrder(objc_class.version);
3871 sys::swapByteOrder(objc_class.info);
3872 sys::swapByteOrder(objc_class.instance_size);
3873 sys::swapByteOrder(objc_class.ivars);
3874 sys::swapByteOrder(objc_class.methodLists);
3875 sys::swapByteOrder(objc_class.cache);
3876 sys::swapByteOrder(objc_class.protocols);
3879 inline void swapStruct(struct objc_category_t &objc_category) {
3880 sys::swapByteOrder(objc_category.category_name);
3881 sys::swapByteOrder(objc_category.class_name);
3882 sys::swapByteOrder(objc_category.instance_methods);
3883 sys::swapByteOrder(objc_category.class_methods);
3884 sys::swapByteOrder(objc_category.protocols);
3887 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3888 sys::swapByteOrder(objc_ivar_list.ivar_count);
3891 inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3892 sys::swapByteOrder(objc_ivar.ivar_name);
3893 sys::swapByteOrder(objc_ivar.ivar_type);
3894 sys::swapByteOrder(objc_ivar.ivar_offset);
3897 inline void swapStruct(struct objc_method_list_t &method_list) {
3898 sys::swapByteOrder(method_list.obsolete);
3899 sys::swapByteOrder(method_list.method_count);
3902 inline void swapStruct(struct objc_method_t &method) {
3903 sys::swapByteOrder(method.method_name);
3904 sys::swapByteOrder(method.method_types);
3905 sys::swapByteOrder(method.method_imp);
3908 inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3909 sys::swapByteOrder(protocol_list.next);
3910 sys::swapByteOrder(protocol_list.count);
3913 inline void swapStruct(struct objc_protocol_t &protocol) {
3914 sys::swapByteOrder(protocol.isa);
3915 sys::swapByteOrder(protocol.protocol_name);
3916 sys::swapByteOrder(protocol.protocol_list);
3917 sys::swapByteOrder(protocol.instance_methods);
3918 sys::swapByteOrder(protocol.class_methods);
3921 inline void swapStruct(struct objc_method_description_list_t &mdl) {
3922 sys::swapByteOrder(mdl.count);
3925 inline void swapStruct(struct objc_method_description_t &md) {
3926 sys::swapByteOrder(md.name);
3927 sys::swapByteOrder(md.types);
3930 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3931 struct DisassembleInfo *info);
3933 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3934 // to an Objective-C class and returns the class name. It is also passed the
3935 // address of the pointer, so when the pointer is zero as it can be in an .o
3936 // file, that is used to look for an external relocation entry with a symbol
3937 // name.
3938 static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3939 uint64_t ReferenceValue,
3940 struct DisassembleInfo *info) {
3941 const char *r;
3942 uint32_t offset, left;
3943 SectionRef S;
3945 // The pointer_value can be 0 in an object file and have a relocation
3946 // entry for the class symbol at the ReferenceValue (the address of the
3947 // pointer).
3948 if (pointer_value == 0) {
3949 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3950 if (r == nullptr || left < sizeof(uint64_t))
3951 return nullptr;
3952 uint64_t n_value;
3953 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3954 if (symbol_name == nullptr)
3955 return nullptr;
3956 const char *class_name = strrchr(symbol_name, '$');
3957 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3958 return class_name + 2;
3959 else
3960 return nullptr;
3963 // The case were the pointer_value is non-zero and points to a class defined
3964 // in this Mach-O file.
3965 r = get_pointer_64(pointer_value, offset, left, S, info);
3966 if (r == nullptr || left < sizeof(struct class64_t))
3967 return nullptr;
3968 struct class64_t c;
3969 memcpy(&c, r, sizeof(struct class64_t));
3970 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3971 swapStruct(c);
3972 if (c.data == 0)
3973 return nullptr;
3974 r = get_pointer_64(c.data, offset, left, S, info);
3975 if (r == nullptr || left < sizeof(struct class_ro64_t))
3976 return nullptr;
3977 struct class_ro64_t cro;
3978 memcpy(&cro, r, sizeof(struct class_ro64_t));
3979 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3980 swapStruct(cro);
3981 if (cro.name == 0)
3982 return nullptr;
3983 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3984 return name;
3987 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3988 // pointer to a cfstring and returns its name or nullptr.
3989 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3990 struct DisassembleInfo *info) {
3991 const char *r, *name;
3992 uint32_t offset, left;
3993 SectionRef S;
3994 struct cfstring64_t cfs;
3995 uint64_t cfs_characters;
3997 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3998 if (r == nullptr || left < sizeof(struct cfstring64_t))
3999 return nullptr;
4000 memcpy(&cfs, r, sizeof(struct cfstring64_t));
4001 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4002 swapStruct(cfs);
4003 if (cfs.characters == 0) {
4004 uint64_t n_value;
4005 const char *symbol_name = get_symbol_64(
4006 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
4007 if (symbol_name == nullptr)
4008 return nullptr;
4009 cfs_characters = n_value;
4010 } else
4011 cfs_characters = cfs.characters;
4012 name = get_pointer_64(cfs_characters, offset, left, S, info);
4014 return name;
4017 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
4018 // of a pointer to an Objective-C selector reference when the pointer value is
4019 // zero as in a .o file and is likely to have a external relocation entry with
4020 // who's symbol's n_value is the real pointer to the selector name. If that is
4021 // the case the real pointer to the selector name is returned else 0 is
4022 // returned
4023 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
4024 struct DisassembleInfo *info) {
4025 uint32_t offset, left;
4026 SectionRef S;
4028 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
4029 if (r == nullptr || left < sizeof(uint64_t))
4030 return 0;
4031 uint64_t n_value;
4032 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
4033 if (symbol_name == nullptr)
4034 return 0;
4035 return n_value;
4038 static const SectionRef get_section(MachOObjectFile *O, const char *segname,
4039 const char *sectname) {
4040 for (const SectionRef &Section : O->sections()) {
4041 StringRef SectName;
4042 Section.getName(SectName);
4043 DataRefImpl Ref = Section.getRawDataRefImpl();
4044 StringRef SegName = O->getSectionFinalSegmentName(Ref);
4045 if (SegName == segname && SectName == sectname)
4046 return Section;
4048 return SectionRef();
4051 static void
4052 walk_pointer_list_64(const char *listname, const SectionRef S,
4053 MachOObjectFile *O, struct DisassembleInfo *info,
4054 void (*func)(uint64_t, struct DisassembleInfo *info)) {
4055 if (S == SectionRef())
4056 return;
4058 StringRef SectName;
4059 S.getName(SectName);
4060 DataRefImpl Ref = S.getRawDataRefImpl();
4061 StringRef SegName = O->getSectionFinalSegmentName(Ref);
4062 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4064 StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName());
4065 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
4067 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
4068 uint32_t left = S.getSize() - i;
4069 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
4070 uint64_t p = 0;
4071 memcpy(&p, Contents + i, size);
4072 if (i + sizeof(uint64_t) > S.getSize())
4073 outs() << listname << " list pointer extends past end of (" << SegName
4074 << "," << SectName << ") section\n";
4075 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
4077 if (O->isLittleEndian() != sys::IsLittleEndianHost)
4078 sys::swapByteOrder(p);
4080 uint64_t n_value = 0;
4081 const char *name = get_symbol_64(i, S, info, n_value, p);
4082 if (name == nullptr)
4083 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
4085 if (n_value != 0) {
4086 outs() << format("0x%" PRIx64, n_value);
4087 if (p != 0)
4088 outs() << " + " << format("0x%" PRIx64, p);
4089 } else
4090 outs() << format("0x%" PRIx64, p);
4091 if (name != nullptr)
4092 outs() << " " << name;
4093 outs() << "\n";
4095 p += n_value;
4096 if (func)
4097 func(p, info);
4101 static void
4102 walk_pointer_list_32(const char *listname, const SectionRef S,
4103 MachOObjectFile *O, struct DisassembleInfo *info,
4104 void (*func)(uint32_t, struct DisassembleInfo *info)) {
4105 if (S == SectionRef())
4106 return;
4108 StringRef SectName;
4109 S.getName(SectName);
4110 DataRefImpl Ref = S.getRawDataRefImpl();
4111 StringRef SegName = O->getSectionFinalSegmentName(Ref);
4112 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4114 StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName());
4115 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
4117 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
4118 uint32_t left = S.getSize() - i;
4119 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
4120 uint32_t p = 0;
4121 memcpy(&p, Contents + i, size);
4122 if (i + sizeof(uint32_t) > S.getSize())
4123 outs() << listname << " list pointer extends past end of (" << SegName
4124 << "," << SectName << ") section\n";
4125 uint32_t Address = S.getAddress() + i;
4126 outs() << format("%08" PRIx32, Address) << " ";
4128 if (O->isLittleEndian() != sys::IsLittleEndianHost)
4129 sys::swapByteOrder(p);
4130 outs() << format("0x%" PRIx32, p);
4132 const char *name = get_symbol_32(i, S, info, p);
4133 if (name != nullptr)
4134 outs() << " " << name;
4135 outs() << "\n";
4137 if (func)
4138 func(p, info);
4142 static void print_layout_map(const char *layout_map, uint32_t left) {
4143 if (layout_map == nullptr)
4144 return;
4145 outs() << " layout map: ";
4146 do {
4147 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
4148 left--;
4149 layout_map++;
4150 } while (*layout_map != '\0' && left != 0);
4151 outs() << "\n";
4154 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
4155 uint32_t offset, left;
4156 SectionRef S;
4157 const char *layout_map;
4159 if (p == 0)
4160 return;
4161 layout_map = get_pointer_64(p, offset, left, S, info);
4162 print_layout_map(layout_map, left);
4165 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
4166 uint32_t offset, left;
4167 SectionRef S;
4168 const char *layout_map;
4170 if (p == 0)
4171 return;
4172 layout_map = get_pointer_32(p, offset, left, S, info);
4173 print_layout_map(layout_map, left);
4176 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
4177 const char *indent) {
4178 struct method_list64_t ml;
4179 struct method64_t m;
4180 const char *r;
4181 uint32_t offset, xoffset, left, i;
4182 SectionRef S, xS;
4183 const char *name, *sym_name;
4184 uint64_t n_value;
4186 r = get_pointer_64(p, offset, left, S, info);
4187 if (r == nullptr)
4188 return;
4189 memset(&ml, '\0', sizeof(struct method_list64_t));
4190 if (left < sizeof(struct method_list64_t)) {
4191 memcpy(&ml, r, left);
4192 outs() << " (method_list_t entends past the end of the section)\n";
4193 } else
4194 memcpy(&ml, r, sizeof(struct method_list64_t));
4195 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4196 swapStruct(ml);
4197 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
4198 outs() << indent << "\t\t count " << ml.count << "\n";
4200 p += sizeof(struct method_list64_t);
4201 offset += sizeof(struct method_list64_t);
4202 for (i = 0; i < ml.count; i++) {
4203 r = get_pointer_64(p, offset, left, S, info);
4204 if (r == nullptr)
4205 return;
4206 memset(&m, '\0', sizeof(struct method64_t));
4207 if (left < sizeof(struct method64_t)) {
4208 memcpy(&m, r, left);
4209 outs() << indent << " (method_t extends past the end of the section)\n";
4210 } else
4211 memcpy(&m, r, sizeof(struct method64_t));
4212 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4213 swapStruct(m);
4215 outs() << indent << "\t\t name ";
4216 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
4217 info, n_value, m.name);
4218 if (n_value != 0) {
4219 if (info->verbose && sym_name != nullptr)
4220 outs() << sym_name;
4221 else
4222 outs() << format("0x%" PRIx64, n_value);
4223 if (m.name != 0)
4224 outs() << " + " << format("0x%" PRIx64, m.name);
4225 } else
4226 outs() << format("0x%" PRIx64, m.name);
4227 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
4228 if (name != nullptr)
4229 outs() << format(" %.*s", left, name);
4230 outs() << "\n";
4232 outs() << indent << "\t\t types ";
4233 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
4234 info, n_value, m.types);
4235 if (n_value != 0) {
4236 if (info->verbose && sym_name != nullptr)
4237 outs() << sym_name;
4238 else
4239 outs() << format("0x%" PRIx64, n_value);
4240 if (m.types != 0)
4241 outs() << " + " << format("0x%" PRIx64, m.types);
4242 } else
4243 outs() << format("0x%" PRIx64, m.types);
4244 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
4245 if (name != nullptr)
4246 outs() << format(" %.*s", left, name);
4247 outs() << "\n";
4249 outs() << indent << "\t\t imp ";
4250 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
4251 n_value, m.imp);
4252 if (info->verbose && name == nullptr) {
4253 if (n_value != 0) {
4254 outs() << format("0x%" PRIx64, n_value) << " ";
4255 if (m.imp != 0)
4256 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
4257 } else
4258 outs() << format("0x%" PRIx64, m.imp) << " ";
4260 if (name != nullptr)
4261 outs() << name;
4262 outs() << "\n";
4264 p += sizeof(struct method64_t);
4265 offset += sizeof(struct method64_t);
4269 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
4270 const char *indent) {
4271 struct method_list32_t ml;
4272 struct method32_t m;
4273 const char *r, *name;
4274 uint32_t offset, xoffset, left, i;
4275 SectionRef S, xS;
4277 r = get_pointer_32(p, offset, left, S, info);
4278 if (r == nullptr)
4279 return;
4280 memset(&ml, '\0', sizeof(struct method_list32_t));
4281 if (left < sizeof(struct method_list32_t)) {
4282 memcpy(&ml, r, left);
4283 outs() << " (method_list_t entends past the end of the section)\n";
4284 } else
4285 memcpy(&ml, r, sizeof(struct method_list32_t));
4286 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4287 swapStruct(ml);
4288 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
4289 outs() << indent << "\t\t count " << ml.count << "\n";
4291 p += sizeof(struct method_list32_t);
4292 offset += sizeof(struct method_list32_t);
4293 for (i = 0; i < ml.count; i++) {
4294 r = get_pointer_32(p, offset, left, S, info);
4295 if (r == nullptr)
4296 return;
4297 memset(&m, '\0', sizeof(struct method32_t));
4298 if (left < sizeof(struct method32_t)) {
4299 memcpy(&ml, r, left);
4300 outs() << indent << " (method_t entends past the end of the section)\n";
4301 } else
4302 memcpy(&m, r, sizeof(struct method32_t));
4303 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4304 swapStruct(m);
4306 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
4307 name = get_pointer_32(m.name, xoffset, left, xS, info);
4308 if (name != nullptr)
4309 outs() << format(" %.*s", left, name);
4310 outs() << "\n";
4312 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
4313 name = get_pointer_32(m.types, xoffset, left, xS, info);
4314 if (name != nullptr)
4315 outs() << format(" %.*s", left, name);
4316 outs() << "\n";
4318 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
4319 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
4320 m.imp);
4321 if (name != nullptr)
4322 outs() << " " << name;
4323 outs() << "\n";
4325 p += sizeof(struct method32_t);
4326 offset += sizeof(struct method32_t);
4330 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
4331 uint32_t offset, left, xleft;
4332 SectionRef S;
4333 struct objc_method_list_t method_list;
4334 struct objc_method_t method;
4335 const char *r, *methods, *name, *SymbolName;
4336 int32_t i;
4338 r = get_pointer_32(p, offset, left, S, info, true);
4339 if (r == nullptr)
4340 return true;
4342 outs() << "\n";
4343 if (left > sizeof(struct objc_method_list_t)) {
4344 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
4345 } else {
4346 outs() << "\t\t objc_method_list extends past end of the section\n";
4347 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
4348 memcpy(&method_list, r, left);
4350 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4351 swapStruct(method_list);
4353 outs() << "\t\t obsolete "
4354 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
4355 outs() << "\t\t method_count " << method_list.method_count << "\n";
4357 methods = r + sizeof(struct objc_method_list_t);
4358 for (i = 0; i < method_list.method_count; i++) {
4359 if ((i + 1) * sizeof(struct objc_method_t) > left) {
4360 outs() << "\t\t remaining method's extend past the of the section\n";
4361 break;
4363 memcpy(&method, methods + i * sizeof(struct objc_method_t),
4364 sizeof(struct objc_method_t));
4365 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4366 swapStruct(method);
4368 outs() << "\t\t method_name "
4369 << format("0x%08" PRIx32, method.method_name);
4370 if (info->verbose) {
4371 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
4372 if (name != nullptr)
4373 outs() << format(" %.*s", xleft, name);
4374 else
4375 outs() << " (not in an __OBJC section)";
4377 outs() << "\n";
4379 outs() << "\t\t method_types "
4380 << format("0x%08" PRIx32, method.method_types);
4381 if (info->verbose) {
4382 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
4383 if (name != nullptr)
4384 outs() << format(" %.*s", xleft, name);
4385 else
4386 outs() << " (not in an __OBJC section)";
4388 outs() << "\n";
4390 outs() << "\t\t method_imp "
4391 << format("0x%08" PRIx32, method.method_imp) << " ";
4392 if (info->verbose) {
4393 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
4394 if (SymbolName != nullptr)
4395 outs() << SymbolName;
4397 outs() << "\n";
4399 return false;
4402 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
4403 struct protocol_list64_t pl;
4404 uint64_t q, n_value;
4405 struct protocol64_t pc;
4406 const char *r;
4407 uint32_t offset, xoffset, left, i;
4408 SectionRef S, xS;
4409 const char *name, *sym_name;
4411 r = get_pointer_64(p, offset, left, S, info);
4412 if (r == nullptr)
4413 return;
4414 memset(&pl, '\0', sizeof(struct protocol_list64_t));
4415 if (left < sizeof(struct protocol_list64_t)) {
4416 memcpy(&pl, r, left);
4417 outs() << " (protocol_list_t entends past the end of the section)\n";
4418 } else
4419 memcpy(&pl, r, sizeof(struct protocol_list64_t));
4420 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4421 swapStruct(pl);
4422 outs() << " count " << pl.count << "\n";
4424 p += sizeof(struct protocol_list64_t);
4425 offset += sizeof(struct protocol_list64_t);
4426 for (i = 0; i < pl.count; i++) {
4427 r = get_pointer_64(p, offset, left, S, info);
4428 if (r == nullptr)
4429 return;
4430 q = 0;
4431 if (left < sizeof(uint64_t)) {
4432 memcpy(&q, r, left);
4433 outs() << " (protocol_t * entends past the end of the section)\n";
4434 } else
4435 memcpy(&q, r, sizeof(uint64_t));
4436 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4437 sys::swapByteOrder(q);
4439 outs() << "\t\t list[" << i << "] ";
4440 sym_name = get_symbol_64(offset, S, info, n_value, q);
4441 if (n_value != 0) {
4442 if (info->verbose && sym_name != nullptr)
4443 outs() << sym_name;
4444 else
4445 outs() << format("0x%" PRIx64, n_value);
4446 if (q != 0)
4447 outs() << " + " << format("0x%" PRIx64, q);
4448 } else
4449 outs() << format("0x%" PRIx64, q);
4450 outs() << " (struct protocol_t *)\n";
4452 r = get_pointer_64(q + n_value, offset, left, S, info);
4453 if (r == nullptr)
4454 return;
4455 memset(&pc, '\0', sizeof(struct protocol64_t));
4456 if (left < sizeof(struct protocol64_t)) {
4457 memcpy(&pc, r, left);
4458 outs() << " (protocol_t entends past the end of the section)\n";
4459 } else
4460 memcpy(&pc, r, sizeof(struct protocol64_t));
4461 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4462 swapStruct(pc);
4464 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
4466 outs() << "\t\t\t name ";
4467 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
4468 info, n_value, pc.name);
4469 if (n_value != 0) {
4470 if (info->verbose && sym_name != nullptr)
4471 outs() << sym_name;
4472 else
4473 outs() << format("0x%" PRIx64, n_value);
4474 if (pc.name != 0)
4475 outs() << " + " << format("0x%" PRIx64, pc.name);
4476 } else
4477 outs() << format("0x%" PRIx64, pc.name);
4478 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
4479 if (name != nullptr)
4480 outs() << format(" %.*s", left, name);
4481 outs() << "\n";
4483 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
4485 outs() << "\t\t instanceMethods ";
4486 sym_name =
4487 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
4488 S, info, n_value, pc.instanceMethods);
4489 if (n_value != 0) {
4490 if (info->verbose && sym_name != nullptr)
4491 outs() << sym_name;
4492 else
4493 outs() << format("0x%" PRIx64, n_value);
4494 if (pc.instanceMethods != 0)
4495 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
4496 } else
4497 outs() << format("0x%" PRIx64, pc.instanceMethods);
4498 outs() << " (struct method_list_t *)\n";
4499 if (pc.instanceMethods + n_value != 0)
4500 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
4502 outs() << "\t\t classMethods ";
4503 sym_name =
4504 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
4505 info, n_value, pc.classMethods);
4506 if (n_value != 0) {
4507 if (info->verbose && sym_name != nullptr)
4508 outs() << sym_name;
4509 else
4510 outs() << format("0x%" PRIx64, n_value);
4511 if (pc.classMethods != 0)
4512 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
4513 } else
4514 outs() << format("0x%" PRIx64, pc.classMethods);
4515 outs() << " (struct method_list_t *)\n";
4516 if (pc.classMethods + n_value != 0)
4517 print_method_list64_t(pc.classMethods + n_value, info, "\t");
4519 outs() << "\t optionalInstanceMethods "
4520 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
4521 outs() << "\t optionalClassMethods "
4522 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
4523 outs() << "\t instanceProperties "
4524 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
4526 p += sizeof(uint64_t);
4527 offset += sizeof(uint64_t);
4531 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
4532 struct protocol_list32_t pl;
4533 uint32_t q;
4534 struct protocol32_t pc;
4535 const char *r;
4536 uint32_t offset, xoffset, left, i;
4537 SectionRef S, xS;
4538 const char *name;
4540 r = get_pointer_32(p, offset, left, S, info);
4541 if (r == nullptr)
4542 return;
4543 memset(&pl, '\0', sizeof(struct protocol_list32_t));
4544 if (left < sizeof(struct protocol_list32_t)) {
4545 memcpy(&pl, r, left);
4546 outs() << " (protocol_list_t entends past the end of the section)\n";
4547 } else
4548 memcpy(&pl, r, sizeof(struct protocol_list32_t));
4549 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4550 swapStruct(pl);
4551 outs() << " count " << pl.count << "\n";
4553 p += sizeof(struct protocol_list32_t);
4554 offset += sizeof(struct protocol_list32_t);
4555 for (i = 0; i < pl.count; i++) {
4556 r = get_pointer_32(p, offset, left, S, info);
4557 if (r == nullptr)
4558 return;
4559 q = 0;
4560 if (left < sizeof(uint32_t)) {
4561 memcpy(&q, r, left);
4562 outs() << " (protocol_t * entends past the end of the section)\n";
4563 } else
4564 memcpy(&q, r, sizeof(uint32_t));
4565 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4566 sys::swapByteOrder(q);
4567 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
4568 << " (struct protocol_t *)\n";
4569 r = get_pointer_32(q, offset, left, S, info);
4570 if (r == nullptr)
4571 return;
4572 memset(&pc, '\0', sizeof(struct protocol32_t));
4573 if (left < sizeof(struct protocol32_t)) {
4574 memcpy(&pc, r, left);
4575 outs() << " (protocol_t entends past the end of the section)\n";
4576 } else
4577 memcpy(&pc, r, sizeof(struct protocol32_t));
4578 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4579 swapStruct(pc);
4580 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
4581 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
4582 name = get_pointer_32(pc.name, xoffset, left, xS, info);
4583 if (name != nullptr)
4584 outs() << format(" %.*s", left, name);
4585 outs() << "\n";
4586 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
4587 outs() << "\t\t instanceMethods "
4588 << format("0x%" PRIx32, pc.instanceMethods)
4589 << " (struct method_list_t *)\n";
4590 if (pc.instanceMethods != 0)
4591 print_method_list32_t(pc.instanceMethods, info, "\t");
4592 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
4593 << " (struct method_list_t *)\n";
4594 if (pc.classMethods != 0)
4595 print_method_list32_t(pc.classMethods, info, "\t");
4596 outs() << "\t optionalInstanceMethods "
4597 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
4598 outs() << "\t optionalClassMethods "
4599 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
4600 outs() << "\t instanceProperties "
4601 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
4602 p += sizeof(uint32_t);
4603 offset += sizeof(uint32_t);
4607 static void print_indent(uint32_t indent) {
4608 for (uint32_t i = 0; i < indent;) {
4609 if (indent - i >= 8) {
4610 outs() << "\t";
4611 i += 8;
4612 } else {
4613 for (uint32_t j = i; j < indent; j++)
4614 outs() << " ";
4615 return;
4620 static bool print_method_description_list(uint32_t p, uint32_t indent,
4621 struct DisassembleInfo *info) {
4622 uint32_t offset, left, xleft;
4623 SectionRef S;
4624 struct objc_method_description_list_t mdl;
4625 struct objc_method_description_t md;
4626 const char *r, *list, *name;
4627 int32_t i;
4629 r = get_pointer_32(p, offset, left, S, info, true);
4630 if (r == nullptr)
4631 return true;
4633 outs() << "\n";
4634 if (left > sizeof(struct objc_method_description_list_t)) {
4635 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
4636 } else {
4637 print_indent(indent);
4638 outs() << " objc_method_description_list extends past end of the section\n";
4639 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
4640 memcpy(&mdl, r, left);
4642 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4643 swapStruct(mdl);
4645 print_indent(indent);
4646 outs() << " count " << mdl.count << "\n";
4648 list = r + sizeof(struct objc_method_description_list_t);
4649 for (i = 0; i < mdl.count; i++) {
4650 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
4651 print_indent(indent);
4652 outs() << " remaining list entries extend past the of the section\n";
4653 break;
4655 print_indent(indent);
4656 outs() << " list[" << i << "]\n";
4657 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
4658 sizeof(struct objc_method_description_t));
4659 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4660 swapStruct(md);
4662 print_indent(indent);
4663 outs() << " name " << format("0x%08" PRIx32, md.name);
4664 if (info->verbose) {
4665 name = get_pointer_32(md.name, offset, xleft, S, info, true);
4666 if (name != nullptr)
4667 outs() << format(" %.*s", xleft, name);
4668 else
4669 outs() << " (not in an __OBJC section)";
4671 outs() << "\n";
4673 print_indent(indent);
4674 outs() << " types " << format("0x%08" PRIx32, md.types);
4675 if (info->verbose) {
4676 name = get_pointer_32(md.types, offset, xleft, S, info, true);
4677 if (name != nullptr)
4678 outs() << format(" %.*s", xleft, name);
4679 else
4680 outs() << " (not in an __OBJC section)";
4682 outs() << "\n";
4684 return false;
4687 static bool print_protocol_list(uint32_t p, uint32_t indent,
4688 struct DisassembleInfo *info);
4690 static bool print_protocol(uint32_t p, uint32_t indent,
4691 struct DisassembleInfo *info) {
4692 uint32_t offset, left;
4693 SectionRef S;
4694 struct objc_protocol_t protocol;
4695 const char *r, *name;
4697 r = get_pointer_32(p, offset, left, S, info, true);
4698 if (r == nullptr)
4699 return true;
4701 outs() << "\n";
4702 if (left >= sizeof(struct objc_protocol_t)) {
4703 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
4704 } else {
4705 print_indent(indent);
4706 outs() << " Protocol extends past end of the section\n";
4707 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
4708 memcpy(&protocol, r, left);
4710 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4711 swapStruct(protocol);
4713 print_indent(indent);
4714 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
4715 << "\n";
4717 print_indent(indent);
4718 outs() << " protocol_name "
4719 << format("0x%08" PRIx32, protocol.protocol_name);
4720 if (info->verbose) {
4721 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
4722 if (name != nullptr)
4723 outs() << format(" %.*s", left, name);
4724 else
4725 outs() << " (not in an __OBJC section)";
4727 outs() << "\n";
4729 print_indent(indent);
4730 outs() << " protocol_list "
4731 << format("0x%08" PRIx32, protocol.protocol_list);
4732 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
4733 outs() << " (not in an __OBJC section)\n";
4735 print_indent(indent);
4736 outs() << " instance_methods "
4737 << format("0x%08" PRIx32, protocol.instance_methods);
4738 if (print_method_description_list(protocol.instance_methods, indent, info))
4739 outs() << " (not in an __OBJC section)\n";
4741 print_indent(indent);
4742 outs() << " class_methods "
4743 << format("0x%08" PRIx32, protocol.class_methods);
4744 if (print_method_description_list(protocol.class_methods, indent, info))
4745 outs() << " (not in an __OBJC section)\n";
4747 return false;
4750 static bool print_protocol_list(uint32_t p, uint32_t indent,
4751 struct DisassembleInfo *info) {
4752 uint32_t offset, left, l;
4753 SectionRef S;
4754 struct objc_protocol_list_t protocol_list;
4755 const char *r, *list;
4756 int32_t i;
4758 r = get_pointer_32(p, offset, left, S, info, true);
4759 if (r == nullptr)
4760 return true;
4762 outs() << "\n";
4763 if (left > sizeof(struct objc_protocol_list_t)) {
4764 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
4765 } else {
4766 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4767 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
4768 memcpy(&protocol_list, r, left);
4770 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4771 swapStruct(protocol_list);
4773 print_indent(indent);
4774 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
4775 << "\n";
4776 print_indent(indent);
4777 outs() << " count " << protocol_list.count << "\n";
4779 list = r + sizeof(struct objc_protocol_list_t);
4780 for (i = 0; i < protocol_list.count; i++) {
4781 if ((i + 1) * sizeof(uint32_t) > left) {
4782 outs() << "\t\t remaining list entries extend past the of the section\n";
4783 break;
4785 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
4786 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4787 sys::swapByteOrder(l);
4789 print_indent(indent);
4790 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
4791 if (print_protocol(l, indent, info))
4792 outs() << "(not in an __OBJC section)\n";
4794 return false;
4797 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
4798 struct ivar_list64_t il;
4799 struct ivar64_t i;
4800 const char *r;
4801 uint32_t offset, xoffset, left, j;
4802 SectionRef S, xS;
4803 const char *name, *sym_name, *ivar_offset_p;
4804 uint64_t ivar_offset, n_value;
4806 r = get_pointer_64(p, offset, left, S, info);
4807 if (r == nullptr)
4808 return;
4809 memset(&il, '\0', sizeof(struct ivar_list64_t));
4810 if (left < sizeof(struct ivar_list64_t)) {
4811 memcpy(&il, r, left);
4812 outs() << " (ivar_list_t entends past the end of the section)\n";
4813 } else
4814 memcpy(&il, r, sizeof(struct ivar_list64_t));
4815 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4816 swapStruct(il);
4817 outs() << " entsize " << il.entsize << "\n";
4818 outs() << " count " << il.count << "\n";
4820 p += sizeof(struct ivar_list64_t);
4821 offset += sizeof(struct ivar_list64_t);
4822 for (j = 0; j < il.count; j++) {
4823 r = get_pointer_64(p, offset, left, S, info);
4824 if (r == nullptr)
4825 return;
4826 memset(&i, '\0', sizeof(struct ivar64_t));
4827 if (left < sizeof(struct ivar64_t)) {
4828 memcpy(&i, r, left);
4829 outs() << " (ivar_t entends past the end of the section)\n";
4830 } else
4831 memcpy(&i, r, sizeof(struct ivar64_t));
4832 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4833 swapStruct(i);
4835 outs() << "\t\t\t offset ";
4836 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4837 info, n_value, i.offset);
4838 if (n_value != 0) {
4839 if (info->verbose && sym_name != nullptr)
4840 outs() << sym_name;
4841 else
4842 outs() << format("0x%" PRIx64, n_value);
4843 if (i.offset != 0)
4844 outs() << " + " << format("0x%" PRIx64, i.offset);
4845 } else
4846 outs() << format("0x%" PRIx64, i.offset);
4847 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4848 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4849 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4850 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4851 sys::swapByteOrder(ivar_offset);
4852 outs() << " " << ivar_offset << "\n";
4853 } else
4854 outs() << "\n";
4856 outs() << "\t\t\t name ";
4857 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4858 n_value, i.name);
4859 if (n_value != 0) {
4860 if (info->verbose && sym_name != nullptr)
4861 outs() << sym_name;
4862 else
4863 outs() << format("0x%" PRIx64, n_value);
4864 if (i.name != 0)
4865 outs() << " + " << format("0x%" PRIx64, i.name);
4866 } else
4867 outs() << format("0x%" PRIx64, i.name);
4868 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4869 if (name != nullptr)
4870 outs() << format(" %.*s", left, name);
4871 outs() << "\n";
4873 outs() << "\t\t\t type ";
4874 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4875 n_value, i.name);
4876 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4877 if (n_value != 0) {
4878 if (info->verbose && sym_name != nullptr)
4879 outs() << sym_name;
4880 else
4881 outs() << format("0x%" PRIx64, n_value);
4882 if (i.type != 0)
4883 outs() << " + " << format("0x%" PRIx64, i.type);
4884 } else
4885 outs() << format("0x%" PRIx64, i.type);
4886 if (name != nullptr)
4887 outs() << format(" %.*s", left, name);
4888 outs() << "\n";
4890 outs() << "\t\t\talignment " << i.alignment << "\n";
4891 outs() << "\t\t\t size " << i.size << "\n";
4893 p += sizeof(struct ivar64_t);
4894 offset += sizeof(struct ivar64_t);
4898 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4899 struct ivar_list32_t il;
4900 struct ivar32_t i;
4901 const char *r;
4902 uint32_t offset, xoffset, left, j;
4903 SectionRef S, xS;
4904 const char *name, *ivar_offset_p;
4905 uint32_t ivar_offset;
4907 r = get_pointer_32(p, offset, left, S, info);
4908 if (r == nullptr)
4909 return;
4910 memset(&il, '\0', sizeof(struct ivar_list32_t));
4911 if (left < sizeof(struct ivar_list32_t)) {
4912 memcpy(&il, r, left);
4913 outs() << " (ivar_list_t entends past the end of the section)\n";
4914 } else
4915 memcpy(&il, r, sizeof(struct ivar_list32_t));
4916 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4917 swapStruct(il);
4918 outs() << " entsize " << il.entsize << "\n";
4919 outs() << " count " << il.count << "\n";
4921 p += sizeof(struct ivar_list32_t);
4922 offset += sizeof(struct ivar_list32_t);
4923 for (j = 0; j < il.count; j++) {
4924 r = get_pointer_32(p, offset, left, S, info);
4925 if (r == nullptr)
4926 return;
4927 memset(&i, '\0', sizeof(struct ivar32_t));
4928 if (left < sizeof(struct ivar32_t)) {
4929 memcpy(&i, r, left);
4930 outs() << " (ivar_t entends past the end of the section)\n";
4931 } else
4932 memcpy(&i, r, sizeof(struct ivar32_t));
4933 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4934 swapStruct(i);
4936 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4937 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4938 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4939 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4940 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4941 sys::swapByteOrder(ivar_offset);
4942 outs() << " " << ivar_offset << "\n";
4943 } else
4944 outs() << "\n";
4946 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4947 name = get_pointer_32(i.name, xoffset, left, xS, info);
4948 if (name != nullptr)
4949 outs() << format(" %.*s", left, name);
4950 outs() << "\n";
4952 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4953 name = get_pointer_32(i.type, xoffset, left, xS, info);
4954 if (name != nullptr)
4955 outs() << format(" %.*s", left, name);
4956 outs() << "\n";
4958 outs() << "\t\t\talignment " << i.alignment << "\n";
4959 outs() << "\t\t\t size " << i.size << "\n";
4961 p += sizeof(struct ivar32_t);
4962 offset += sizeof(struct ivar32_t);
4966 static void print_objc_property_list64(uint64_t p,
4967 struct DisassembleInfo *info) {
4968 struct objc_property_list64 opl;
4969 struct objc_property64 op;
4970 const char *r;
4971 uint32_t offset, xoffset, left, j;
4972 SectionRef S, xS;
4973 const char *name, *sym_name;
4974 uint64_t n_value;
4976 r = get_pointer_64(p, offset, left, S, info);
4977 if (r == nullptr)
4978 return;
4979 memset(&opl, '\0', sizeof(struct objc_property_list64));
4980 if (left < sizeof(struct objc_property_list64)) {
4981 memcpy(&opl, r, left);
4982 outs() << " (objc_property_list entends past the end of the section)\n";
4983 } else
4984 memcpy(&opl, r, sizeof(struct objc_property_list64));
4985 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4986 swapStruct(opl);
4987 outs() << " entsize " << opl.entsize << "\n";
4988 outs() << " count " << opl.count << "\n";
4990 p += sizeof(struct objc_property_list64);
4991 offset += sizeof(struct objc_property_list64);
4992 for (j = 0; j < opl.count; j++) {
4993 r = get_pointer_64(p, offset, left, S, info);
4994 if (r == nullptr)
4995 return;
4996 memset(&op, '\0', sizeof(struct objc_property64));
4997 if (left < sizeof(struct objc_property64)) {
4998 memcpy(&op, r, left);
4999 outs() << " (objc_property entends past the end of the section)\n";
5000 } else
5001 memcpy(&op, r, sizeof(struct objc_property64));
5002 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5003 swapStruct(op);
5005 outs() << "\t\t\t name ";
5006 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
5007 info, n_value, op.name);
5008 if (n_value != 0) {
5009 if (info->verbose && sym_name != nullptr)
5010 outs() << sym_name;
5011 else
5012 outs() << format("0x%" PRIx64, n_value);
5013 if (op.name != 0)
5014 outs() << " + " << format("0x%" PRIx64, op.name);
5015 } else
5016 outs() << format("0x%" PRIx64, op.name);
5017 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
5018 if (name != nullptr)
5019 outs() << format(" %.*s", left, name);
5020 outs() << "\n";
5022 outs() << "\t\t\tattributes ";
5023 sym_name =
5024 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
5025 info, n_value, op.attributes);
5026 if (n_value != 0) {
5027 if (info->verbose && sym_name != nullptr)
5028 outs() << sym_name;
5029 else
5030 outs() << format("0x%" PRIx64, n_value);
5031 if (op.attributes != 0)
5032 outs() << " + " << format("0x%" PRIx64, op.attributes);
5033 } else
5034 outs() << format("0x%" PRIx64, op.attributes);
5035 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
5036 if (name != nullptr)
5037 outs() << format(" %.*s", left, name);
5038 outs() << "\n";
5040 p += sizeof(struct objc_property64);
5041 offset += sizeof(struct objc_property64);
5045 static void print_objc_property_list32(uint32_t p,
5046 struct DisassembleInfo *info) {
5047 struct objc_property_list32 opl;
5048 struct objc_property32 op;
5049 const char *r;
5050 uint32_t offset, xoffset, left, j;
5051 SectionRef S, xS;
5052 const char *name;
5054 r = get_pointer_32(p, offset, left, S, info);
5055 if (r == nullptr)
5056 return;
5057 memset(&opl, '\0', sizeof(struct objc_property_list32));
5058 if (left < sizeof(struct objc_property_list32)) {
5059 memcpy(&opl, r, left);
5060 outs() << " (objc_property_list entends past the end of the section)\n";
5061 } else
5062 memcpy(&opl, r, sizeof(struct objc_property_list32));
5063 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5064 swapStruct(opl);
5065 outs() << " entsize " << opl.entsize << "\n";
5066 outs() << " count " << opl.count << "\n";
5068 p += sizeof(struct objc_property_list32);
5069 offset += sizeof(struct objc_property_list32);
5070 for (j = 0; j < opl.count; j++) {
5071 r = get_pointer_32(p, offset, left, S, info);
5072 if (r == nullptr)
5073 return;
5074 memset(&op, '\0', sizeof(struct objc_property32));
5075 if (left < sizeof(struct objc_property32)) {
5076 memcpy(&op, r, left);
5077 outs() << " (objc_property entends past the end of the section)\n";
5078 } else
5079 memcpy(&op, r, sizeof(struct objc_property32));
5080 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5081 swapStruct(op);
5083 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
5084 name = get_pointer_32(op.name, xoffset, left, xS, info);
5085 if (name != nullptr)
5086 outs() << format(" %.*s", left, name);
5087 outs() << "\n";
5089 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
5090 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
5091 if (name != nullptr)
5092 outs() << format(" %.*s", left, name);
5093 outs() << "\n";
5095 p += sizeof(struct objc_property32);
5096 offset += sizeof(struct objc_property32);
5100 static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
5101 bool &is_meta_class) {
5102 struct class_ro64_t cro;
5103 const char *r;
5104 uint32_t offset, xoffset, left;
5105 SectionRef S, xS;
5106 const char *name, *sym_name;
5107 uint64_t n_value;
5109 r = get_pointer_64(p, offset, left, S, info);
5110 if (r == nullptr || left < sizeof(struct class_ro64_t))
5111 return false;
5112 memcpy(&cro, r, sizeof(struct class_ro64_t));
5113 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5114 swapStruct(cro);
5115 outs() << " flags " << format("0x%" PRIx32, cro.flags);
5116 if (cro.flags & RO_META)
5117 outs() << " RO_META";
5118 if (cro.flags & RO_ROOT)
5119 outs() << " RO_ROOT";
5120 if (cro.flags & RO_HAS_CXX_STRUCTORS)
5121 outs() << " RO_HAS_CXX_STRUCTORS";
5122 outs() << "\n";
5123 outs() << " instanceStart " << cro.instanceStart << "\n";
5124 outs() << " instanceSize " << cro.instanceSize << "\n";
5125 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
5126 << "\n";
5127 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
5128 << "\n";
5129 print_layout_map64(cro.ivarLayout, info);
5131 outs() << " name ";
5132 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
5133 info, n_value, cro.name);
5134 if (n_value != 0) {
5135 if (info->verbose && sym_name != nullptr)
5136 outs() << sym_name;
5137 else
5138 outs() << format("0x%" PRIx64, n_value);
5139 if (cro.name != 0)
5140 outs() << " + " << format("0x%" PRIx64, cro.name);
5141 } else
5142 outs() << format("0x%" PRIx64, cro.name);
5143 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
5144 if (name != nullptr)
5145 outs() << format(" %.*s", left, name);
5146 outs() << "\n";
5148 outs() << " baseMethods ";
5149 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
5150 S, info, n_value, cro.baseMethods);
5151 if (n_value != 0) {
5152 if (info->verbose && sym_name != nullptr)
5153 outs() << sym_name;
5154 else
5155 outs() << format("0x%" PRIx64, n_value);
5156 if (cro.baseMethods != 0)
5157 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
5158 } else
5159 outs() << format("0x%" PRIx64, cro.baseMethods);
5160 outs() << " (struct method_list_t *)\n";
5161 if (cro.baseMethods + n_value != 0)
5162 print_method_list64_t(cro.baseMethods + n_value, info, "");
5164 outs() << " baseProtocols ";
5165 sym_name =
5166 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
5167 info, n_value, cro.baseProtocols);
5168 if (n_value != 0) {
5169 if (info->verbose && sym_name != nullptr)
5170 outs() << sym_name;
5171 else
5172 outs() << format("0x%" PRIx64, n_value);
5173 if (cro.baseProtocols != 0)
5174 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
5175 } else
5176 outs() << format("0x%" PRIx64, cro.baseProtocols);
5177 outs() << "\n";
5178 if (cro.baseProtocols + n_value != 0)
5179 print_protocol_list64_t(cro.baseProtocols + n_value, info);
5181 outs() << " ivars ";
5182 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
5183 info, n_value, cro.ivars);
5184 if (n_value != 0) {
5185 if (info->verbose && sym_name != nullptr)
5186 outs() << sym_name;
5187 else
5188 outs() << format("0x%" PRIx64, n_value);
5189 if (cro.ivars != 0)
5190 outs() << " + " << format("0x%" PRIx64, cro.ivars);
5191 } else
5192 outs() << format("0x%" PRIx64, cro.ivars);
5193 outs() << "\n";
5194 if (cro.ivars + n_value != 0)
5195 print_ivar_list64_t(cro.ivars + n_value, info);
5197 outs() << " weakIvarLayout ";
5198 sym_name =
5199 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
5200 info, n_value, cro.weakIvarLayout);
5201 if (n_value != 0) {
5202 if (info->verbose && sym_name != nullptr)
5203 outs() << sym_name;
5204 else
5205 outs() << format("0x%" PRIx64, n_value);
5206 if (cro.weakIvarLayout != 0)
5207 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
5208 } else
5209 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
5210 outs() << "\n";
5211 print_layout_map64(cro.weakIvarLayout + n_value, info);
5213 outs() << " baseProperties ";
5214 sym_name =
5215 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
5216 info, n_value, cro.baseProperties);
5217 if (n_value != 0) {
5218 if (info->verbose && sym_name != nullptr)
5219 outs() << sym_name;
5220 else
5221 outs() << format("0x%" PRIx64, n_value);
5222 if (cro.baseProperties != 0)
5223 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
5224 } else
5225 outs() << format("0x%" PRIx64, cro.baseProperties);
5226 outs() << "\n";
5227 if (cro.baseProperties + n_value != 0)
5228 print_objc_property_list64(cro.baseProperties + n_value, info);
5230 is_meta_class = (cro.flags & RO_META) != 0;
5231 return true;
5234 static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
5235 bool &is_meta_class) {
5236 struct class_ro32_t cro;
5237 const char *r;
5238 uint32_t offset, xoffset, left;
5239 SectionRef S, xS;
5240 const char *name;
5242 r = get_pointer_32(p, offset, left, S, info);
5243 if (r == nullptr)
5244 return false;
5245 memset(&cro, '\0', sizeof(struct class_ro32_t));
5246 if (left < sizeof(struct class_ro32_t)) {
5247 memcpy(&cro, r, left);
5248 outs() << " (class_ro_t entends past the end of the section)\n";
5249 } else
5250 memcpy(&cro, r, sizeof(struct class_ro32_t));
5251 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5252 swapStruct(cro);
5253 outs() << " flags " << format("0x%" PRIx32, cro.flags);
5254 if (cro.flags & RO_META)
5255 outs() << " RO_META";
5256 if (cro.flags & RO_ROOT)
5257 outs() << " RO_ROOT";
5258 if (cro.flags & RO_HAS_CXX_STRUCTORS)
5259 outs() << " RO_HAS_CXX_STRUCTORS";
5260 outs() << "\n";
5261 outs() << " instanceStart " << cro.instanceStart << "\n";
5262 outs() << " instanceSize " << cro.instanceSize << "\n";
5263 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
5264 << "\n";
5265 print_layout_map32(cro.ivarLayout, info);
5267 outs() << " name " << format("0x%" PRIx32, cro.name);
5268 name = get_pointer_32(cro.name, xoffset, left, xS, info);
5269 if (name != nullptr)
5270 outs() << format(" %.*s", left, name);
5271 outs() << "\n";
5273 outs() << " baseMethods "
5274 << format("0x%" PRIx32, cro.baseMethods)
5275 << " (struct method_list_t *)\n";
5276 if (cro.baseMethods != 0)
5277 print_method_list32_t(cro.baseMethods, info, "");
5279 outs() << " baseProtocols "
5280 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
5281 if (cro.baseProtocols != 0)
5282 print_protocol_list32_t(cro.baseProtocols, info);
5283 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
5284 << "\n";
5285 if (cro.ivars != 0)
5286 print_ivar_list32_t(cro.ivars, info);
5287 outs() << " weakIvarLayout "
5288 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
5289 print_layout_map32(cro.weakIvarLayout, info);
5290 outs() << " baseProperties "
5291 << format("0x%" PRIx32, cro.baseProperties) << "\n";
5292 if (cro.baseProperties != 0)
5293 print_objc_property_list32(cro.baseProperties, info);
5294 is_meta_class = (cro.flags & RO_META) != 0;
5295 return true;
5298 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
5299 struct class64_t c;
5300 const char *r;
5301 uint32_t offset, left;
5302 SectionRef S;
5303 const char *name;
5304 uint64_t isa_n_value, n_value;
5306 r = get_pointer_64(p, offset, left, S, info);
5307 if (r == nullptr || left < sizeof(struct class64_t))
5308 return;
5309 memcpy(&c, r, sizeof(struct class64_t));
5310 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5311 swapStruct(c);
5313 outs() << " isa " << format("0x%" PRIx64, c.isa);
5314 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
5315 isa_n_value, c.isa);
5316 if (name != nullptr)
5317 outs() << " " << name;
5318 outs() << "\n";
5320 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
5321 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
5322 n_value, c.superclass);
5323 if (name != nullptr)
5324 outs() << " " << name;
5325 else {
5326 name = get_dyld_bind_info_symbolname(S.getAddress() +
5327 offset + offsetof(struct class64_t, superclass), info);
5328 if (name != nullptr)
5329 outs() << " " << name;
5331 outs() << "\n";
5333 outs() << " cache " << format("0x%" PRIx64, c.cache);
5334 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
5335 n_value, c.cache);
5336 if (name != nullptr)
5337 outs() << " " << name;
5338 outs() << "\n";
5340 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
5341 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
5342 n_value, c.vtable);
5343 if (name != nullptr)
5344 outs() << " " << name;
5345 outs() << "\n";
5347 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
5348 n_value, c.data);
5349 outs() << " data ";
5350 if (n_value != 0) {
5351 if (info->verbose && name != nullptr)
5352 outs() << name;
5353 else
5354 outs() << format("0x%" PRIx64, n_value);
5355 if (c.data != 0)
5356 outs() << " + " << format("0x%" PRIx64, c.data);
5357 } else
5358 outs() << format("0x%" PRIx64, c.data);
5359 outs() << " (struct class_ro_t *)";
5361 // This is a Swift class if some of the low bits of the pointer are set.
5362 if ((c.data + n_value) & 0x7)
5363 outs() << " Swift class";
5364 outs() << "\n";
5365 bool is_meta_class;
5366 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
5367 return;
5369 if (!is_meta_class &&
5370 c.isa + isa_n_value != p &&
5371 c.isa + isa_n_value != 0 &&
5372 info->depth < 100) {
5373 info->depth++;
5374 outs() << "Meta Class\n";
5375 print_class64_t(c.isa + isa_n_value, info);
5379 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
5380 struct class32_t c;
5381 const char *r;
5382 uint32_t offset, left;
5383 SectionRef S;
5384 const char *name;
5386 r = get_pointer_32(p, offset, left, S, info);
5387 if (r == nullptr)
5388 return;
5389 memset(&c, '\0', sizeof(struct class32_t));
5390 if (left < sizeof(struct class32_t)) {
5391 memcpy(&c, r, left);
5392 outs() << " (class_t entends past the end of the section)\n";
5393 } else
5394 memcpy(&c, r, sizeof(struct class32_t));
5395 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5396 swapStruct(c);
5398 outs() << " isa " << format("0x%" PRIx32, c.isa);
5399 name =
5400 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
5401 if (name != nullptr)
5402 outs() << " " << name;
5403 outs() << "\n";
5405 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
5406 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
5407 c.superclass);
5408 if (name != nullptr)
5409 outs() << " " << name;
5410 outs() << "\n";
5412 outs() << " cache " << format("0x%" PRIx32, c.cache);
5413 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
5414 c.cache);
5415 if (name != nullptr)
5416 outs() << " " << name;
5417 outs() << "\n";
5419 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
5420 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
5421 c.vtable);
5422 if (name != nullptr)
5423 outs() << " " << name;
5424 outs() << "\n";
5426 name =
5427 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
5428 outs() << " data " << format("0x%" PRIx32, c.data)
5429 << " (struct class_ro_t *)";
5431 // This is a Swift class if some of the low bits of the pointer are set.
5432 if (c.data & 0x3)
5433 outs() << " Swift class";
5434 outs() << "\n";
5435 bool is_meta_class;
5436 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
5437 return;
5439 if (!is_meta_class) {
5440 outs() << "Meta Class\n";
5441 print_class32_t(c.isa, info);
5445 static void print_objc_class_t(struct objc_class_t *objc_class,
5446 struct DisassembleInfo *info) {
5447 uint32_t offset, left, xleft;
5448 const char *name, *p, *ivar_list;
5449 SectionRef S;
5450 int32_t i;
5451 struct objc_ivar_list_t objc_ivar_list;
5452 struct objc_ivar_t ivar;
5454 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
5455 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
5456 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
5457 if (name != nullptr)
5458 outs() << format(" %.*s", left, name);
5459 else
5460 outs() << " (not in an __OBJC section)";
5462 outs() << "\n";
5464 outs() << "\t super_class "
5465 << format("0x%08" PRIx32, objc_class->super_class);
5466 if (info->verbose) {
5467 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
5468 if (name != nullptr)
5469 outs() << format(" %.*s", left, name);
5470 else
5471 outs() << " (not in an __OBJC section)";
5473 outs() << "\n";
5475 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
5476 if (info->verbose) {
5477 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
5478 if (name != nullptr)
5479 outs() << format(" %.*s", left, name);
5480 else
5481 outs() << " (not in an __OBJC section)";
5483 outs() << "\n";
5485 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
5486 << "\n";
5488 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
5489 if (info->verbose) {
5490 if (CLS_GETINFO(objc_class, CLS_CLASS))
5491 outs() << " CLS_CLASS";
5492 else if (CLS_GETINFO(objc_class, CLS_META))
5493 outs() << " CLS_META";
5495 outs() << "\n";
5497 outs() << "\t instance_size "
5498 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
5500 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
5501 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
5502 if (p != nullptr) {
5503 if (left > sizeof(struct objc_ivar_list_t)) {
5504 outs() << "\n";
5505 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
5506 } else {
5507 outs() << " (entends past the end of the section)\n";
5508 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
5509 memcpy(&objc_ivar_list, p, left);
5511 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5512 swapStruct(objc_ivar_list);
5513 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
5514 ivar_list = p + sizeof(struct objc_ivar_list_t);
5515 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
5516 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
5517 outs() << "\t\t remaining ivar's extend past the of the section\n";
5518 break;
5520 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
5521 sizeof(struct objc_ivar_t));
5522 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5523 swapStruct(ivar);
5525 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
5526 if (info->verbose) {
5527 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
5528 if (name != nullptr)
5529 outs() << format(" %.*s", xleft, name);
5530 else
5531 outs() << " (not in an __OBJC section)";
5533 outs() << "\n";
5535 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
5536 if (info->verbose) {
5537 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
5538 if (name != nullptr)
5539 outs() << format(" %.*s", xleft, name);
5540 else
5541 outs() << " (not in an __OBJC section)";
5543 outs() << "\n";
5545 outs() << "\t\t ivar_offset "
5546 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
5548 } else {
5549 outs() << " (not in an __OBJC section)\n";
5552 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
5553 if (print_method_list(objc_class->methodLists, info))
5554 outs() << " (not in an __OBJC section)\n";
5556 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
5557 << "\n";
5559 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
5560 if (print_protocol_list(objc_class->protocols, 16, info))
5561 outs() << " (not in an __OBJC section)\n";
5564 static void print_objc_objc_category_t(struct objc_category_t *objc_category,
5565 struct DisassembleInfo *info) {
5566 uint32_t offset, left;
5567 const char *name;
5568 SectionRef S;
5570 outs() << "\t category name "
5571 << format("0x%08" PRIx32, objc_category->category_name);
5572 if (info->verbose) {
5573 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
5574 true);
5575 if (name != nullptr)
5576 outs() << format(" %.*s", left, name);
5577 else
5578 outs() << " (not in an __OBJC section)";
5580 outs() << "\n";
5582 outs() << "\t\t class name "
5583 << format("0x%08" PRIx32, objc_category->class_name);
5584 if (info->verbose) {
5585 name =
5586 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
5587 if (name != nullptr)
5588 outs() << format(" %.*s", left, name);
5589 else
5590 outs() << " (not in an __OBJC section)";
5592 outs() << "\n";
5594 outs() << "\t instance methods "
5595 << format("0x%08" PRIx32, objc_category->instance_methods);
5596 if (print_method_list(objc_category->instance_methods, info))
5597 outs() << " (not in an __OBJC section)\n";
5599 outs() << "\t class methods "
5600 << format("0x%08" PRIx32, objc_category->class_methods);
5601 if (print_method_list(objc_category->class_methods, info))
5602 outs() << " (not in an __OBJC section)\n";
5605 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
5606 struct category64_t c;
5607 const char *r;
5608 uint32_t offset, xoffset, left;
5609 SectionRef S, xS;
5610 const char *name, *sym_name;
5611 uint64_t n_value;
5613 r = get_pointer_64(p, offset, left, S, info);
5614 if (r == nullptr)
5615 return;
5616 memset(&c, '\0', sizeof(struct category64_t));
5617 if (left < sizeof(struct category64_t)) {
5618 memcpy(&c, r, left);
5619 outs() << " (category_t entends past the end of the section)\n";
5620 } else
5621 memcpy(&c, r, sizeof(struct category64_t));
5622 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5623 swapStruct(c);
5625 outs() << " name ";
5626 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
5627 info, n_value, c.name);
5628 if (n_value != 0) {
5629 if (info->verbose && sym_name != nullptr)
5630 outs() << sym_name;
5631 else
5632 outs() << format("0x%" PRIx64, n_value);
5633 if (c.name != 0)
5634 outs() << " + " << format("0x%" PRIx64, c.name);
5635 } else
5636 outs() << format("0x%" PRIx64, c.name);
5637 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
5638 if (name != nullptr)
5639 outs() << format(" %.*s", left, name);
5640 outs() << "\n";
5642 outs() << " cls ";
5643 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
5644 n_value, c.cls);
5645 if (n_value != 0) {
5646 if (info->verbose && sym_name != nullptr)
5647 outs() << sym_name;
5648 else
5649 outs() << format("0x%" PRIx64, n_value);
5650 if (c.cls != 0)
5651 outs() << " + " << format("0x%" PRIx64, c.cls);
5652 } else
5653 outs() << format("0x%" PRIx64, c.cls);
5654 outs() << "\n";
5655 if (c.cls + n_value != 0)
5656 print_class64_t(c.cls + n_value, info);
5658 outs() << " instanceMethods ";
5659 sym_name =
5660 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
5661 info, n_value, c.instanceMethods);
5662 if (n_value != 0) {
5663 if (info->verbose && sym_name != nullptr)
5664 outs() << sym_name;
5665 else
5666 outs() << format("0x%" PRIx64, n_value);
5667 if (c.instanceMethods != 0)
5668 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
5669 } else
5670 outs() << format("0x%" PRIx64, c.instanceMethods);
5671 outs() << "\n";
5672 if (c.instanceMethods + n_value != 0)
5673 print_method_list64_t(c.instanceMethods + n_value, info, "");
5675 outs() << " classMethods ";
5676 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
5677 S, info, n_value, c.classMethods);
5678 if (n_value != 0) {
5679 if (info->verbose && sym_name != nullptr)
5680 outs() << sym_name;
5681 else
5682 outs() << format("0x%" PRIx64, n_value);
5683 if (c.classMethods != 0)
5684 outs() << " + " << format("0x%" PRIx64, c.classMethods);
5685 } else
5686 outs() << format("0x%" PRIx64, c.classMethods);
5687 outs() << "\n";
5688 if (c.classMethods + n_value != 0)
5689 print_method_list64_t(c.classMethods + n_value, info, "");
5691 outs() << " protocols ";
5692 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
5693 info, n_value, c.protocols);
5694 if (n_value != 0) {
5695 if (info->verbose && sym_name != nullptr)
5696 outs() << sym_name;
5697 else
5698 outs() << format("0x%" PRIx64, n_value);
5699 if (c.protocols != 0)
5700 outs() << " + " << format("0x%" PRIx64, c.protocols);
5701 } else
5702 outs() << format("0x%" PRIx64, c.protocols);
5703 outs() << "\n";
5704 if (c.protocols + n_value != 0)
5705 print_protocol_list64_t(c.protocols + n_value, info);
5707 outs() << "instanceProperties ";
5708 sym_name =
5709 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
5710 S, info, n_value, c.instanceProperties);
5711 if (n_value != 0) {
5712 if (info->verbose && sym_name != nullptr)
5713 outs() << sym_name;
5714 else
5715 outs() << format("0x%" PRIx64, n_value);
5716 if (c.instanceProperties != 0)
5717 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
5718 } else
5719 outs() << format("0x%" PRIx64, c.instanceProperties);
5720 outs() << "\n";
5721 if (c.instanceProperties + n_value != 0)
5722 print_objc_property_list64(c.instanceProperties + n_value, info);
5725 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
5726 struct category32_t c;
5727 const char *r;
5728 uint32_t offset, left;
5729 SectionRef S, xS;
5730 const char *name;
5732 r = get_pointer_32(p, offset, left, S, info);
5733 if (r == nullptr)
5734 return;
5735 memset(&c, '\0', sizeof(struct category32_t));
5736 if (left < sizeof(struct category32_t)) {
5737 memcpy(&c, r, left);
5738 outs() << " (category_t entends past the end of the section)\n";
5739 } else
5740 memcpy(&c, r, sizeof(struct category32_t));
5741 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5742 swapStruct(c);
5744 outs() << " name " << format("0x%" PRIx32, c.name);
5745 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
5746 c.name);
5747 if (name)
5748 outs() << " " << name;
5749 outs() << "\n";
5751 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
5752 if (c.cls != 0)
5753 print_class32_t(c.cls, info);
5754 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
5755 << "\n";
5756 if (c.instanceMethods != 0)
5757 print_method_list32_t(c.instanceMethods, info, "");
5758 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
5759 << "\n";
5760 if (c.classMethods != 0)
5761 print_method_list32_t(c.classMethods, info, "");
5762 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
5763 if (c.protocols != 0)
5764 print_protocol_list32_t(c.protocols, info);
5765 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
5766 << "\n";
5767 if (c.instanceProperties != 0)
5768 print_objc_property_list32(c.instanceProperties, info);
5771 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
5772 uint32_t i, left, offset, xoffset;
5773 uint64_t p, n_value;
5774 struct message_ref64 mr;
5775 const char *name, *sym_name;
5776 const char *r;
5777 SectionRef xS;
5779 if (S == SectionRef())
5780 return;
5782 StringRef SectName;
5783 S.getName(SectName);
5784 DataRefImpl Ref = S.getRawDataRefImpl();
5785 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5786 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5787 offset = 0;
5788 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5789 p = S.getAddress() + i;
5790 r = get_pointer_64(p, offset, left, S, info);
5791 if (r == nullptr)
5792 return;
5793 memset(&mr, '\0', sizeof(struct message_ref64));
5794 if (left < sizeof(struct message_ref64)) {
5795 memcpy(&mr, r, left);
5796 outs() << " (message_ref entends past the end of the section)\n";
5797 } else
5798 memcpy(&mr, r, sizeof(struct message_ref64));
5799 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5800 swapStruct(mr);
5802 outs() << " imp ";
5803 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
5804 n_value, mr.imp);
5805 if (n_value != 0) {
5806 outs() << format("0x%" PRIx64, n_value) << " ";
5807 if (mr.imp != 0)
5808 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
5809 } else
5810 outs() << format("0x%" PRIx64, mr.imp) << " ";
5811 if (name != nullptr)
5812 outs() << " " << name;
5813 outs() << "\n";
5815 outs() << " sel ";
5816 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
5817 info, n_value, mr.sel);
5818 if (n_value != 0) {
5819 if (info->verbose && sym_name != nullptr)
5820 outs() << sym_name;
5821 else
5822 outs() << format("0x%" PRIx64, n_value);
5823 if (mr.sel != 0)
5824 outs() << " + " << format("0x%" PRIx64, mr.sel);
5825 } else
5826 outs() << format("0x%" PRIx64, mr.sel);
5827 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5828 if (name != nullptr)
5829 outs() << format(" %.*s", left, name);
5830 outs() << "\n";
5832 offset += sizeof(struct message_ref64);
5836 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5837 uint32_t i, left, offset, xoffset, p;
5838 struct message_ref32 mr;
5839 const char *name, *r;
5840 SectionRef xS;
5842 if (S == SectionRef())
5843 return;
5845 StringRef SectName;
5846 S.getName(SectName);
5847 DataRefImpl Ref = S.getRawDataRefImpl();
5848 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5849 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5850 offset = 0;
5851 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5852 p = S.getAddress() + i;
5853 r = get_pointer_32(p, offset, left, S, info);
5854 if (r == nullptr)
5855 return;
5856 memset(&mr, '\0', sizeof(struct message_ref32));
5857 if (left < sizeof(struct message_ref32)) {
5858 memcpy(&mr, r, left);
5859 outs() << " (message_ref entends past the end of the section)\n";
5860 } else
5861 memcpy(&mr, r, sizeof(struct message_ref32));
5862 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5863 swapStruct(mr);
5865 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5866 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5867 mr.imp);
5868 if (name != nullptr)
5869 outs() << " " << name;
5870 outs() << "\n";
5872 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5873 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5874 if (name != nullptr)
5875 outs() << " " << name;
5876 outs() << "\n";
5878 offset += sizeof(struct message_ref32);
5882 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5883 uint32_t left, offset, swift_version;
5884 uint64_t p;
5885 struct objc_image_info64 o;
5886 const char *r;
5888 if (S == SectionRef())
5889 return;
5891 StringRef SectName;
5892 S.getName(SectName);
5893 DataRefImpl Ref = S.getRawDataRefImpl();
5894 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5895 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5896 p = S.getAddress();
5897 r = get_pointer_64(p, offset, left, S, info);
5898 if (r == nullptr)
5899 return;
5900 memset(&o, '\0', sizeof(struct objc_image_info64));
5901 if (left < sizeof(struct objc_image_info64)) {
5902 memcpy(&o, r, left);
5903 outs() << " (objc_image_info entends past the end of the section)\n";
5904 } else
5905 memcpy(&o, r, sizeof(struct objc_image_info64));
5906 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5907 swapStruct(o);
5908 outs() << " version " << o.version << "\n";
5909 outs() << " flags " << format("0x%" PRIx32, o.flags);
5910 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5911 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5912 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5913 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5914 if (o.flags & OBJC_IMAGE_IS_SIMULATED)
5915 outs() << " OBJC_IMAGE_IS_SIMULATED";
5916 if (o.flags & OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES)
5917 outs() << " OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES";
5918 swift_version = (o.flags >> 8) & 0xff;
5919 if (swift_version != 0) {
5920 if (swift_version == 1)
5921 outs() << " Swift 1.0";
5922 else if (swift_version == 2)
5923 outs() << " Swift 1.1";
5924 else if(swift_version == 3)
5925 outs() << " Swift 2.0";
5926 else if(swift_version == 4)
5927 outs() << " Swift 3.0";
5928 else if(swift_version == 5)
5929 outs() << " Swift 4.0";
5930 else if(swift_version == 6)
5931 outs() << " Swift 4.1/Swift 4.2";
5932 else if(swift_version == 7)
5933 outs() << " Swift 5 or later";
5934 else
5935 outs() << " unknown future Swift version (" << swift_version << ")";
5937 outs() << "\n";
5940 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5941 uint32_t left, offset, swift_version, p;
5942 struct objc_image_info32 o;
5943 const char *r;
5945 if (S == SectionRef())
5946 return;
5948 StringRef SectName;
5949 S.getName(SectName);
5950 DataRefImpl Ref = S.getRawDataRefImpl();
5951 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5952 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5953 p = S.getAddress();
5954 r = get_pointer_32(p, offset, left, S, info);
5955 if (r == nullptr)
5956 return;
5957 memset(&o, '\0', sizeof(struct objc_image_info32));
5958 if (left < sizeof(struct objc_image_info32)) {
5959 memcpy(&o, r, left);
5960 outs() << " (objc_image_info entends past the end of the section)\n";
5961 } else
5962 memcpy(&o, r, sizeof(struct objc_image_info32));
5963 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5964 swapStruct(o);
5965 outs() << " version " << o.version << "\n";
5966 outs() << " flags " << format("0x%" PRIx32, o.flags);
5967 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5968 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5969 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5970 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5971 swift_version = (o.flags >> 8) & 0xff;
5972 if (swift_version != 0) {
5973 if (swift_version == 1)
5974 outs() << " Swift 1.0";
5975 else if (swift_version == 2)
5976 outs() << " Swift 1.1";
5977 else if(swift_version == 3)
5978 outs() << " Swift 2.0";
5979 else if(swift_version == 4)
5980 outs() << " Swift 3.0";
5981 else if(swift_version == 5)
5982 outs() << " Swift 4.0";
5983 else if(swift_version == 6)
5984 outs() << " Swift 4.1/Swift 4.2";
5985 else if(swift_version == 7)
5986 outs() << " Swift 5 or later";
5987 else
5988 outs() << " unknown future Swift version (" << swift_version << ")";
5990 outs() << "\n";
5993 static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5994 uint32_t left, offset, p;
5995 struct imageInfo_t o;
5996 const char *r;
5998 StringRef SectName;
5999 S.getName(SectName);
6000 DataRefImpl Ref = S.getRawDataRefImpl();
6001 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
6002 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
6003 p = S.getAddress();
6004 r = get_pointer_32(p, offset, left, S, info);
6005 if (r == nullptr)
6006 return;
6007 memset(&o, '\0', sizeof(struct imageInfo_t));
6008 if (left < sizeof(struct imageInfo_t)) {
6009 memcpy(&o, r, left);
6010 outs() << " (imageInfo entends past the end of the section)\n";
6011 } else
6012 memcpy(&o, r, sizeof(struct imageInfo_t));
6013 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
6014 swapStruct(o);
6015 outs() << " version " << o.version << "\n";
6016 outs() << " flags " << format("0x%" PRIx32, o.flags);
6017 if (o.flags & 0x1)
6018 outs() << " F&C";
6019 if (o.flags & 0x2)
6020 outs() << " GC";
6021 if (o.flags & 0x4)
6022 outs() << " GC-only";
6023 else
6024 outs() << " RR";
6025 outs() << "\n";
6028 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
6029 SymbolAddressMap AddrMap;
6030 if (verbose)
6031 CreateSymbolAddressMap(O, &AddrMap);
6033 std::vector<SectionRef> Sections;
6034 for (const SectionRef &Section : O->sections()) {
6035 StringRef SectName;
6036 Section.getName(SectName);
6037 Sections.push_back(Section);
6040 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
6042 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
6043 if (CL == SectionRef())
6044 CL = get_section(O, "__DATA", "__objc_classlist");
6045 if (CL == SectionRef())
6046 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
6047 if (CL == SectionRef())
6048 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
6049 info.S = CL;
6050 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
6052 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
6053 if (CR == SectionRef())
6054 CR = get_section(O, "__DATA", "__objc_classrefs");
6055 if (CR == SectionRef())
6056 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
6057 if (CR == SectionRef())
6058 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
6059 info.S = CR;
6060 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
6062 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
6063 if (SR == SectionRef())
6064 SR = get_section(O, "__DATA", "__objc_superrefs");
6065 if (SR == SectionRef())
6066 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
6067 if (SR == SectionRef())
6068 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
6069 info.S = SR;
6070 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
6072 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
6073 if (CA == SectionRef())
6074 CA = get_section(O, "__DATA", "__objc_catlist");
6075 if (CA == SectionRef())
6076 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
6077 if (CA == SectionRef())
6078 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
6079 info.S = CA;
6080 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
6082 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
6083 if (PL == SectionRef())
6084 PL = get_section(O, "__DATA", "__objc_protolist");
6085 if (PL == SectionRef())
6086 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
6087 if (PL == SectionRef())
6088 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
6089 info.S = PL;
6090 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
6092 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
6093 if (MR == SectionRef())
6094 MR = get_section(O, "__DATA", "__objc_msgrefs");
6095 if (MR == SectionRef())
6096 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
6097 if (MR == SectionRef())
6098 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
6099 info.S = MR;
6100 print_message_refs64(MR, &info);
6102 SectionRef II = get_section(O, "__OBJC2", "__image_info");
6103 if (II == SectionRef())
6104 II = get_section(O, "__DATA", "__objc_imageinfo");
6105 if (II == SectionRef())
6106 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
6107 if (II == SectionRef())
6108 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
6109 info.S = II;
6110 print_image_info64(II, &info);
6113 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
6114 SymbolAddressMap AddrMap;
6115 if (verbose)
6116 CreateSymbolAddressMap(O, &AddrMap);
6118 std::vector<SectionRef> Sections;
6119 for (const SectionRef &Section : O->sections()) {
6120 StringRef SectName;
6121 Section.getName(SectName);
6122 Sections.push_back(Section);
6125 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
6127 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
6128 if (CL == SectionRef())
6129 CL = get_section(O, "__DATA", "__objc_classlist");
6130 if (CL == SectionRef())
6131 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
6132 if (CL == SectionRef())
6133 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
6134 info.S = CL;
6135 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
6137 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
6138 if (CR == SectionRef())
6139 CR = get_section(O, "__DATA", "__objc_classrefs");
6140 if (CR == SectionRef())
6141 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
6142 if (CR == SectionRef())
6143 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
6144 info.S = CR;
6145 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
6147 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
6148 if (SR == SectionRef())
6149 SR = get_section(O, "__DATA", "__objc_superrefs");
6150 if (SR == SectionRef())
6151 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
6152 if (SR == SectionRef())
6153 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
6154 info.S = SR;
6155 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
6157 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
6158 if (CA == SectionRef())
6159 CA = get_section(O, "__DATA", "__objc_catlist");
6160 if (CA == SectionRef())
6161 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
6162 if (CA == SectionRef())
6163 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
6164 info.S = CA;
6165 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
6167 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
6168 if (PL == SectionRef())
6169 PL = get_section(O, "__DATA", "__objc_protolist");
6170 if (PL == SectionRef())
6171 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
6172 if (PL == SectionRef())
6173 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
6174 info.S = PL;
6175 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
6177 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
6178 if (MR == SectionRef())
6179 MR = get_section(O, "__DATA", "__objc_msgrefs");
6180 if (MR == SectionRef())
6181 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
6182 if (MR == SectionRef())
6183 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
6184 info.S = MR;
6185 print_message_refs32(MR, &info);
6187 SectionRef II = get_section(O, "__OBJC2", "__image_info");
6188 if (II == SectionRef())
6189 II = get_section(O, "__DATA", "__objc_imageinfo");
6190 if (II == SectionRef())
6191 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
6192 if (II == SectionRef())
6193 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
6194 info.S = II;
6195 print_image_info32(II, &info);
6198 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
6199 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
6200 const char *r, *name, *defs;
6201 struct objc_module_t module;
6202 SectionRef S, xS;
6203 struct objc_symtab_t symtab;
6204 struct objc_class_t objc_class;
6205 struct objc_category_t objc_category;
6207 outs() << "Objective-C segment\n";
6208 S = get_section(O, "__OBJC", "__module_info");
6209 if (S == SectionRef())
6210 return false;
6212 SymbolAddressMap AddrMap;
6213 if (verbose)
6214 CreateSymbolAddressMap(O, &AddrMap);
6216 std::vector<SectionRef> Sections;
6217 for (const SectionRef &Section : O->sections()) {
6218 StringRef SectName;
6219 Section.getName(SectName);
6220 Sections.push_back(Section);
6223 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
6225 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
6226 p = S.getAddress() + i;
6227 r = get_pointer_32(p, offset, left, S, &info, true);
6228 if (r == nullptr)
6229 return true;
6230 memset(&module, '\0', sizeof(struct objc_module_t));
6231 if (left < sizeof(struct objc_module_t)) {
6232 memcpy(&module, r, left);
6233 outs() << " (module extends past end of __module_info section)\n";
6234 } else
6235 memcpy(&module, r, sizeof(struct objc_module_t));
6236 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6237 swapStruct(module);
6239 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
6240 outs() << " version " << module.version << "\n";
6241 outs() << " size " << module.size << "\n";
6242 outs() << " name ";
6243 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
6244 if (name != nullptr)
6245 outs() << format("%.*s", left, name);
6246 else
6247 outs() << format("0x%08" PRIx32, module.name)
6248 << "(not in an __OBJC section)";
6249 outs() << "\n";
6251 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
6252 if (module.symtab == 0 || r == nullptr) {
6253 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
6254 << " (not in an __OBJC section)\n";
6255 continue;
6257 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
6258 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
6259 defs_left = 0;
6260 defs = nullptr;
6261 if (left < sizeof(struct objc_symtab_t)) {
6262 memcpy(&symtab, r, left);
6263 outs() << "\tsymtab extends past end of an __OBJC section)\n";
6264 } else {
6265 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
6266 if (left > sizeof(struct objc_symtab_t)) {
6267 defs_left = left - sizeof(struct objc_symtab_t);
6268 defs = r + sizeof(struct objc_symtab_t);
6271 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6272 swapStruct(symtab);
6274 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
6275 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
6276 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
6277 if (r == nullptr)
6278 outs() << " (not in an __OBJC section)";
6279 outs() << "\n";
6280 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
6281 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
6282 if (symtab.cls_def_cnt > 0)
6283 outs() << "\tClass Definitions\n";
6284 for (j = 0; j < symtab.cls_def_cnt; j++) {
6285 if ((j + 1) * sizeof(uint32_t) > defs_left) {
6286 outs() << "\t(remaining class defs entries entends past the end of the "
6287 << "section)\n";
6288 break;
6290 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
6291 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6292 sys::swapByteOrder(def);
6294 r = get_pointer_32(def, xoffset, left, xS, &info, true);
6295 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
6296 if (r != nullptr) {
6297 if (left > sizeof(struct objc_class_t)) {
6298 outs() << "\n";
6299 memcpy(&objc_class, r, sizeof(struct objc_class_t));
6300 } else {
6301 outs() << " (entends past the end of the section)\n";
6302 memset(&objc_class, '\0', sizeof(struct objc_class_t));
6303 memcpy(&objc_class, r, left);
6305 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6306 swapStruct(objc_class);
6307 print_objc_class_t(&objc_class, &info);
6308 } else {
6309 outs() << "(not in an __OBJC section)\n";
6312 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
6313 outs() << "\tMeta Class";
6314 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
6315 if (r != nullptr) {
6316 if (left > sizeof(struct objc_class_t)) {
6317 outs() << "\n";
6318 memcpy(&objc_class, r, sizeof(struct objc_class_t));
6319 } else {
6320 outs() << " (entends past the end of the section)\n";
6321 memset(&objc_class, '\0', sizeof(struct objc_class_t));
6322 memcpy(&objc_class, r, left);
6324 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6325 swapStruct(objc_class);
6326 print_objc_class_t(&objc_class, &info);
6327 } else {
6328 outs() << "(not in an __OBJC section)\n";
6332 if (symtab.cat_def_cnt > 0)
6333 outs() << "\tCategory Definitions\n";
6334 for (j = 0; j < symtab.cat_def_cnt; j++) {
6335 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
6336 outs() << "\t(remaining category defs entries entends past the end of "
6337 << "the section)\n";
6338 break;
6340 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
6341 sizeof(uint32_t));
6342 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6343 sys::swapByteOrder(def);
6345 r = get_pointer_32(def, xoffset, left, xS, &info, true);
6346 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
6347 << format("0x%08" PRIx32, def);
6348 if (r != nullptr) {
6349 if (left > sizeof(struct objc_category_t)) {
6350 outs() << "\n";
6351 memcpy(&objc_category, r, sizeof(struct objc_category_t));
6352 } else {
6353 outs() << " (entends past the end of the section)\n";
6354 memset(&objc_category, '\0', sizeof(struct objc_category_t));
6355 memcpy(&objc_category, r, left);
6357 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6358 swapStruct(objc_category);
6359 print_objc_objc_category_t(&objc_category, &info);
6360 } else {
6361 outs() << "(not in an __OBJC section)\n";
6365 const SectionRef II = get_section(O, "__OBJC", "__image_info");
6366 if (II != SectionRef())
6367 print_image_info(II, &info);
6369 return true;
6372 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
6373 uint32_t size, uint32_t addr) {
6374 SymbolAddressMap AddrMap;
6375 CreateSymbolAddressMap(O, &AddrMap);
6377 std::vector<SectionRef> Sections;
6378 for (const SectionRef &Section : O->sections()) {
6379 StringRef SectName;
6380 Section.getName(SectName);
6381 Sections.push_back(Section);
6384 struct DisassembleInfo info(O, &AddrMap, &Sections, true);
6386 const char *p;
6387 struct objc_protocol_t protocol;
6388 uint32_t left, paddr;
6389 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
6390 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
6391 left = size - (p - sect);
6392 if (left < sizeof(struct objc_protocol_t)) {
6393 outs() << "Protocol extends past end of __protocol section\n";
6394 memcpy(&protocol, p, left);
6395 } else
6396 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
6397 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6398 swapStruct(protocol);
6399 paddr = addr + (p - sect);
6400 outs() << "Protocol " << format("0x%" PRIx32, paddr);
6401 if (print_protocol(paddr, 0, &info))
6402 outs() << "(not in an __OBJC section)\n";
6406 #ifdef HAVE_LIBXAR
6407 inline void swapStruct(struct xar_header &xar) {
6408 sys::swapByteOrder(xar.magic);
6409 sys::swapByteOrder(xar.size);
6410 sys::swapByteOrder(xar.version);
6411 sys::swapByteOrder(xar.toc_length_compressed);
6412 sys::swapByteOrder(xar.toc_length_uncompressed);
6413 sys::swapByteOrder(xar.cksum_alg);
6416 static void PrintModeVerbose(uint32_t mode) {
6417 switch(mode & S_IFMT){
6418 case S_IFDIR:
6419 outs() << "d";
6420 break;
6421 case S_IFCHR:
6422 outs() << "c";
6423 break;
6424 case S_IFBLK:
6425 outs() << "b";
6426 break;
6427 case S_IFREG:
6428 outs() << "-";
6429 break;
6430 case S_IFLNK:
6431 outs() << "l";
6432 break;
6433 case S_IFSOCK:
6434 outs() << "s";
6435 break;
6436 default:
6437 outs() << "?";
6438 break;
6441 /* owner permissions */
6442 if(mode & S_IREAD)
6443 outs() << "r";
6444 else
6445 outs() << "-";
6446 if(mode & S_IWRITE)
6447 outs() << "w";
6448 else
6449 outs() << "-";
6450 if(mode & S_ISUID)
6451 outs() << "s";
6452 else if(mode & S_IEXEC)
6453 outs() << "x";
6454 else
6455 outs() << "-";
6457 /* group permissions */
6458 if(mode & (S_IREAD >> 3))
6459 outs() << "r";
6460 else
6461 outs() << "-";
6462 if(mode & (S_IWRITE >> 3))
6463 outs() << "w";
6464 else
6465 outs() << "-";
6466 if(mode & S_ISGID)
6467 outs() << "s";
6468 else if(mode & (S_IEXEC >> 3))
6469 outs() << "x";
6470 else
6471 outs() << "-";
6473 /* other permissions */
6474 if(mode & (S_IREAD >> 6))
6475 outs() << "r";
6476 else
6477 outs() << "-";
6478 if(mode & (S_IWRITE >> 6))
6479 outs() << "w";
6480 else
6481 outs() << "-";
6482 if(mode & S_ISVTX)
6483 outs() << "t";
6484 else if(mode & (S_IEXEC >> 6))
6485 outs() << "x";
6486 else
6487 outs() << "-";
6490 static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
6491 xar_file_t xf;
6492 const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
6493 char *endp;
6494 uint32_t mode_value;
6496 ScopedXarIter xi;
6497 if (!xi) {
6498 WithColor::error(errs(), "llvm-objdump")
6499 << "can't obtain an xar iterator for xar archive " << XarFilename
6500 << "\n";
6501 return;
6504 // Go through the xar's files.
6505 for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
6506 ScopedXarIter xp;
6507 if(!xp){
6508 WithColor::error(errs(), "llvm-objdump")
6509 << "can't obtain an xar iterator for xar archive " << XarFilename
6510 << "\n";
6511 return;
6513 type = nullptr;
6514 mode = nullptr;
6515 user = nullptr;
6516 group = nullptr;
6517 size = nullptr;
6518 mtime = nullptr;
6519 name = nullptr;
6520 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
6521 const char *val = nullptr;
6522 xar_prop_get(xf, key, &val);
6523 #if 0 // Useful for debugging.
6524 outs() << "key: " << key << " value: " << val << "\n";
6525 #endif
6526 if(strcmp(key, "type") == 0)
6527 type = val;
6528 if(strcmp(key, "mode") == 0)
6529 mode = val;
6530 if(strcmp(key, "user") == 0)
6531 user = val;
6532 if(strcmp(key, "group") == 0)
6533 group = val;
6534 if(strcmp(key, "data/size") == 0)
6535 size = val;
6536 if(strcmp(key, "mtime") == 0)
6537 mtime = val;
6538 if(strcmp(key, "name") == 0)
6539 name = val;
6541 if(mode != nullptr){
6542 mode_value = strtoul(mode, &endp, 8);
6543 if(*endp != '\0')
6544 outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
6545 if(strcmp(type, "file") == 0)
6546 mode_value |= S_IFREG;
6547 PrintModeVerbose(mode_value);
6548 outs() << " ";
6550 if(user != nullptr)
6551 outs() << format("%10s/", user);
6552 if(group != nullptr)
6553 outs() << format("%-10s ", group);
6554 if(size != nullptr)
6555 outs() << format("%7s ", size);
6556 if(mtime != nullptr){
6557 for(m = mtime; *m != 'T' && *m != '\0'; m++)
6558 outs() << *m;
6559 if(*m == 'T')
6560 m++;
6561 outs() << " ";
6562 for( ; *m != 'Z' && *m != '\0'; m++)
6563 outs() << *m;
6564 outs() << " ";
6566 if(name != nullptr)
6567 outs() << name;
6568 outs() << "\n";
6572 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
6573 uint32_t size, bool verbose,
6574 bool PrintXarHeader, bool PrintXarFileHeaders,
6575 std::string XarMemberName) {
6576 if(size < sizeof(struct xar_header)) {
6577 outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
6578 "of struct xar_header)\n";
6579 return;
6581 struct xar_header XarHeader;
6582 memcpy(&XarHeader, sect, sizeof(struct xar_header));
6583 if (sys::IsLittleEndianHost)
6584 swapStruct(XarHeader);
6585 if (PrintXarHeader) {
6586 if (!XarMemberName.empty())
6587 outs() << "In xar member " << XarMemberName << ": ";
6588 else
6589 outs() << "For (__LLVM,__bundle) section: ";
6590 outs() << "xar header\n";
6591 if (XarHeader.magic == XAR_HEADER_MAGIC)
6592 outs() << " magic XAR_HEADER_MAGIC\n";
6593 else
6594 outs() << " magic "
6595 << format_hex(XarHeader.magic, 10, true)
6596 << " (not XAR_HEADER_MAGIC)\n";
6597 outs() << " size " << XarHeader.size << "\n";
6598 outs() << " version " << XarHeader.version << "\n";
6599 outs() << " toc_length_compressed " << XarHeader.toc_length_compressed
6600 << "\n";
6601 outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
6602 << "\n";
6603 outs() << " cksum_alg ";
6604 switch (XarHeader.cksum_alg) {
6605 case XAR_CKSUM_NONE:
6606 outs() << "XAR_CKSUM_NONE\n";
6607 break;
6608 case XAR_CKSUM_SHA1:
6609 outs() << "XAR_CKSUM_SHA1\n";
6610 break;
6611 case XAR_CKSUM_MD5:
6612 outs() << "XAR_CKSUM_MD5\n";
6613 break;
6614 #ifdef XAR_CKSUM_SHA256
6615 case XAR_CKSUM_SHA256:
6616 outs() << "XAR_CKSUM_SHA256\n";
6617 break;
6618 #endif
6619 #ifdef XAR_CKSUM_SHA512
6620 case XAR_CKSUM_SHA512:
6621 outs() << "XAR_CKSUM_SHA512\n";
6622 break;
6623 #endif
6624 default:
6625 outs() << XarHeader.cksum_alg << "\n";
6629 SmallString<128> XarFilename;
6630 int FD;
6631 std::error_code XarEC =
6632 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
6633 if (XarEC) {
6634 WithColor::error(errs(), "llvm-objdump") << XarEC.message() << "\n";
6635 return;
6637 ToolOutputFile XarFile(XarFilename, FD);
6638 raw_fd_ostream &XarOut = XarFile.os();
6639 StringRef XarContents(sect, size);
6640 XarOut << XarContents;
6641 XarOut.close();
6642 if (XarOut.has_error())
6643 return;
6645 ScopedXarFile xar(XarFilename.c_str(), READ);
6646 if (!xar) {
6647 WithColor::error(errs(), "llvm-objdump")
6648 << "can't create temporary xar archive " << XarFilename << "\n";
6649 return;
6652 SmallString<128> TocFilename;
6653 std::error_code TocEC =
6654 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
6655 if (TocEC) {
6656 WithColor::error(errs(), "llvm-objdump") << TocEC.message() << "\n";
6657 return;
6659 xar_serialize(xar, TocFilename.c_str());
6661 if (PrintXarFileHeaders) {
6662 if (!XarMemberName.empty())
6663 outs() << "In xar member " << XarMemberName << ": ";
6664 else
6665 outs() << "For (__LLVM,__bundle) section: ";
6666 outs() << "xar archive files:\n";
6667 PrintXarFilesSummary(XarFilename.c_str(), xar);
6670 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
6671 MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
6672 if (std::error_code EC = FileOrErr.getError()) {
6673 WithColor::error(errs(), "llvm-objdump") << EC.message() << "\n";
6674 return;
6676 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
6678 if (!XarMemberName.empty())
6679 outs() << "In xar member " << XarMemberName << ": ";
6680 else
6681 outs() << "For (__LLVM,__bundle) section: ";
6682 outs() << "xar table of contents:\n";
6683 outs() << Buffer->getBuffer() << "\n";
6685 // TODO: Go through the xar's files.
6686 ScopedXarIter xi;
6687 if(!xi){
6688 WithColor::error(errs(), "llvm-objdump")
6689 << "can't obtain an xar iterator for xar archive "
6690 << XarFilename.c_str() << "\n";
6691 return;
6693 for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
6694 const char *key;
6695 const char *member_name, *member_type, *member_size_string;
6696 size_t member_size;
6698 ScopedXarIter xp;
6699 if(!xp){
6700 WithColor::error(errs(), "llvm-objdump")
6701 << "can't obtain an xar iterator for xar archive "
6702 << XarFilename.c_str() << "\n";
6703 return;
6705 member_name = NULL;
6706 member_type = NULL;
6707 member_size_string = NULL;
6708 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
6709 const char *val = nullptr;
6710 xar_prop_get(xf, key, &val);
6711 #if 0 // Useful for debugging.
6712 outs() << "key: " << key << " value: " << val << "\n";
6713 #endif
6714 if (strcmp(key, "name") == 0)
6715 member_name = val;
6716 if (strcmp(key, "type") == 0)
6717 member_type = val;
6718 if (strcmp(key, "data/size") == 0)
6719 member_size_string = val;
6722 * If we find a file with a name, date/size and type properties
6723 * and with the type being "file" see if that is a xar file.
6725 if (member_name != NULL && member_type != NULL &&
6726 strcmp(member_type, "file") == 0 &&
6727 member_size_string != NULL){
6728 // Extract the file into a buffer.
6729 char *endptr;
6730 member_size = strtoul(member_size_string, &endptr, 10);
6731 if (*endptr == '\0' && member_size != 0) {
6732 char *buffer;
6733 if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
6734 #if 0 // Useful for debugging.
6735 outs() << "xar member: " << member_name << " extracted\n";
6736 #endif
6737 // Set the XarMemberName we want to see printed in the header.
6738 std::string OldXarMemberName;
6739 // If XarMemberName is already set this is nested. So
6740 // save the old name and create the nested name.
6741 if (!XarMemberName.empty()) {
6742 OldXarMemberName = XarMemberName;
6743 XarMemberName =
6744 (Twine("[") + XarMemberName + "]" + member_name).str();
6745 } else {
6746 OldXarMemberName = "";
6747 XarMemberName = member_name;
6749 // See if this is could be a xar file (nested).
6750 if (member_size >= sizeof(struct xar_header)) {
6751 #if 0 // Useful for debugging.
6752 outs() << "could be a xar file: " << member_name << "\n";
6753 #endif
6754 memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
6755 if (sys::IsLittleEndianHost)
6756 swapStruct(XarHeader);
6757 if (XarHeader.magic == XAR_HEADER_MAGIC)
6758 DumpBitcodeSection(O, buffer, member_size, verbose,
6759 PrintXarHeader, PrintXarFileHeaders,
6760 XarMemberName);
6762 XarMemberName = OldXarMemberName;
6763 delete buffer;
6769 #endif // defined(HAVE_LIBXAR)
6771 static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
6772 if (O->is64Bit())
6773 printObjc2_64bit_MetaData(O, verbose);
6774 else {
6775 MachO::mach_header H;
6776 H = O->getHeader();
6777 if (H.cputype == MachO::CPU_TYPE_ARM)
6778 printObjc2_32bit_MetaData(O, verbose);
6779 else {
6780 // This is the 32-bit non-arm cputype case. Which is normally
6781 // the first Objective-C ABI. But it may be the case of a
6782 // binary for the iOS simulator which is the second Objective-C
6783 // ABI. In that case printObjc1_32bit_MetaData() will determine that
6784 // and return false.
6785 if (!printObjc1_32bit_MetaData(O, verbose))
6786 printObjc2_32bit_MetaData(O, verbose);
6791 // GuessLiteralPointer returns a string which for the item in the Mach-O file
6792 // for the address passed in as ReferenceValue for printing as a comment with
6793 // the instruction and also returns the corresponding type of that item
6794 // indirectly through ReferenceType.
6796 // If ReferenceValue is an address of literal cstring then a pointer to the
6797 // cstring is returned and ReferenceType is set to
6798 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6800 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6801 // Class ref that name is returned and the ReferenceType is set accordingly.
6803 // Lastly, literals which are Symbol address in a literal pool are looked for
6804 // and if found the symbol name is returned and ReferenceType is set to
6805 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6807 // If there is no item in the Mach-O file for the address passed in as
6808 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
6809 static const char *GuessLiteralPointer(uint64_t ReferenceValue,
6810 uint64_t ReferencePC,
6811 uint64_t *ReferenceType,
6812 struct DisassembleInfo *info) {
6813 // First see if there is an external relocation entry at the ReferencePC.
6814 if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
6815 uint64_t sect_addr = info->S.getAddress();
6816 uint64_t sect_offset = ReferencePC - sect_addr;
6817 bool reloc_found = false;
6818 DataRefImpl Rel;
6819 MachO::any_relocation_info RE;
6820 bool isExtern = false;
6821 SymbolRef Symbol;
6822 for (const RelocationRef &Reloc : info->S.relocations()) {
6823 uint64_t RelocOffset = Reloc.getOffset();
6824 if (RelocOffset == sect_offset) {
6825 Rel = Reloc.getRawDataRefImpl();
6826 RE = info->O->getRelocation(Rel);
6827 if (info->O->isRelocationScattered(RE))
6828 continue;
6829 isExtern = info->O->getPlainRelocationExternal(RE);
6830 if (isExtern) {
6831 symbol_iterator RelocSym = Reloc.getSymbol();
6832 Symbol = *RelocSym;
6834 reloc_found = true;
6835 break;
6838 // If there is an external relocation entry for a symbol in a section
6839 // then used that symbol's value for the value of the reference.
6840 if (reloc_found && isExtern) {
6841 if (info->O->getAnyRelocationPCRel(RE)) {
6842 unsigned Type = info->O->getAnyRelocationType(RE);
6843 if (Type == MachO::X86_64_RELOC_SIGNED) {
6844 ReferenceValue = Symbol.getValue();
6850 // Look for literals such as Objective-C CFStrings refs, Selector refs,
6851 // Message refs and Class refs.
6852 bool classref, selref, msgref, cfstring;
6853 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
6854 selref, msgref, cfstring);
6855 if (classref && pointer_value == 0) {
6856 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6857 // And the pointer_value in that section is typically zero as it will be
6858 // set by dyld as part of the "bind information".
6859 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
6860 if (name != nullptr) {
6861 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6862 const char *class_name = strrchr(name, '$');
6863 if (class_name != nullptr && class_name[1] == '_' &&
6864 class_name[2] != '\0') {
6865 info->class_name = class_name + 2;
6866 return name;
6871 if (classref) {
6872 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6873 const char *name =
6874 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
6875 if (name != nullptr)
6876 info->class_name = name;
6877 else
6878 name = "bad class ref";
6879 return name;
6882 if (cfstring) {
6883 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
6884 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
6885 return name;
6888 if (selref && pointer_value == 0)
6889 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
6891 if (pointer_value != 0)
6892 ReferenceValue = pointer_value;
6894 const char *name = GuessCstringPointer(ReferenceValue, info);
6895 if (name) {
6896 if (pointer_value != 0 && selref) {
6897 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
6898 info->selector_name = name;
6899 } else if (pointer_value != 0 && msgref) {
6900 info->class_name = nullptr;
6901 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
6902 info->selector_name = name;
6903 } else
6904 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
6905 return name;
6908 // Lastly look for an indirect symbol with this ReferenceValue which is in
6909 // a literal pool. If found return that symbol name.
6910 name = GuessIndirectSymbol(ReferenceValue, info);
6911 if (name) {
6912 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
6913 return name;
6916 return nullptr;
6919 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
6920 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
6921 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6922 // is created and returns the symbol name that matches the ReferenceValue or
6923 // nullptr if none. The ReferenceType is passed in for the IN type of
6924 // reference the instruction is making from the values in defined in the header
6925 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
6926 // Out type and the ReferenceName will also be set which is added as a comment
6927 // to the disassembled instruction.
6929 // If the symbol name is a C++ mangled name then the demangled name is
6930 // returned through ReferenceName and ReferenceType is set to
6931 // LLVMDisassembler_ReferenceType_DeMangled_Name .
6933 // When this is called to get a symbol name for a branch target then the
6934 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
6935 // SymbolValue will be looked for in the indirect symbol table to determine if
6936 // it is an address for a symbol stub. If so then the symbol name for that
6937 // stub is returned indirectly through ReferenceName and then ReferenceType is
6938 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
6940 // When this is called with an value loaded via a PC relative load then
6941 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
6942 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
6943 // or an Objective-C meta data reference. If so the output ReferenceType is
6944 // set to correspond to that as well as setting the ReferenceName.
6945 static const char *SymbolizerSymbolLookUp(void *DisInfo,
6946 uint64_t ReferenceValue,
6947 uint64_t *ReferenceType,
6948 uint64_t ReferencePC,
6949 const char **ReferenceName) {
6950 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
6951 // If no verbose symbolic information is wanted then just return nullptr.
6952 if (!info->verbose) {
6953 *ReferenceName = nullptr;
6954 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6955 return nullptr;
6958 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
6960 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
6961 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
6962 if (*ReferenceName != nullptr) {
6963 method_reference(info, ReferenceType, ReferenceName);
6964 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
6965 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
6966 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
6967 if (info->demangled_name != nullptr)
6968 free(info->demangled_name);
6969 int status;
6970 info->demangled_name =
6971 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
6972 if (info->demangled_name != nullptr) {
6973 *ReferenceName = info->demangled_name;
6974 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6975 } else
6976 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6977 } else
6978 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6979 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
6980 *ReferenceName =
6981 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6982 if (*ReferenceName)
6983 method_reference(info, ReferenceType, ReferenceName);
6984 else
6985 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6986 // If this is arm64 and the reference is an adrp instruction save the
6987 // instruction, passed in ReferenceValue and the address of the instruction
6988 // for use later if we see and add immediate instruction.
6989 } else if (info->O->getArch() == Triple::aarch64 &&
6990 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
6991 info->adrp_inst = ReferenceValue;
6992 info->adrp_addr = ReferencePC;
6993 SymbolName = nullptr;
6994 *ReferenceName = nullptr;
6995 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6996 // If this is arm64 and reference is an add immediate instruction and we
6997 // have
6998 // seen an adrp instruction just before it and the adrp's Xd register
6999 // matches
7000 // this add's Xn register reconstruct the value being referenced and look to
7001 // see if it is a literal pointer. Note the add immediate instruction is
7002 // passed in ReferenceValue.
7003 } else if (info->O->getArch() == Triple::aarch64 &&
7004 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
7005 ReferencePC - 4 == info->adrp_addr &&
7006 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
7007 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
7008 uint32_t addxri_inst;
7009 uint64_t adrp_imm, addxri_imm;
7011 adrp_imm =
7012 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
7013 if (info->adrp_inst & 0x0200000)
7014 adrp_imm |= 0xfffffffffc000000LL;
7016 addxri_inst = ReferenceValue;
7017 addxri_imm = (addxri_inst >> 10) & 0xfff;
7018 if (((addxri_inst >> 22) & 0x3) == 1)
7019 addxri_imm <<= 12;
7021 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
7022 (adrp_imm << 12) + addxri_imm;
7024 *ReferenceName =
7025 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
7026 if (*ReferenceName == nullptr)
7027 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7028 // If this is arm64 and the reference is a load register instruction and we
7029 // have seen an adrp instruction just before it and the adrp's Xd register
7030 // matches this add's Xn register reconstruct the value being referenced and
7031 // look to see if it is a literal pointer. Note the load register
7032 // instruction is passed in ReferenceValue.
7033 } else if (info->O->getArch() == Triple::aarch64 &&
7034 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
7035 ReferencePC - 4 == info->adrp_addr &&
7036 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
7037 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
7038 uint32_t ldrxui_inst;
7039 uint64_t adrp_imm, ldrxui_imm;
7041 adrp_imm =
7042 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
7043 if (info->adrp_inst & 0x0200000)
7044 adrp_imm |= 0xfffffffffc000000LL;
7046 ldrxui_inst = ReferenceValue;
7047 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
7049 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
7050 (adrp_imm << 12) + (ldrxui_imm << 3);
7052 *ReferenceName =
7053 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
7054 if (*ReferenceName == nullptr)
7055 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7057 // If this arm64 and is an load register (PC-relative) instruction the
7058 // ReferenceValue is the PC plus the immediate value.
7059 else if (info->O->getArch() == Triple::aarch64 &&
7060 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
7061 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
7062 *ReferenceName =
7063 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
7064 if (*ReferenceName == nullptr)
7065 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7066 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
7067 if (info->demangled_name != nullptr)
7068 free(info->demangled_name);
7069 int status;
7070 info->demangled_name =
7071 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
7072 if (info->demangled_name != nullptr) {
7073 *ReferenceName = info->demangled_name;
7074 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
7077 else {
7078 *ReferenceName = nullptr;
7079 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7082 return SymbolName;
7085 /// Emits the comments that are stored in the CommentStream.
7086 /// Each comment in the CommentStream must end with a newline.
7087 static void emitComments(raw_svector_ostream &CommentStream,
7088 SmallString<128> &CommentsToEmit,
7089 formatted_raw_ostream &FormattedOS,
7090 const MCAsmInfo &MAI) {
7091 // Flush the stream before taking its content.
7092 StringRef Comments = CommentsToEmit.str();
7093 // Get the default information for printing a comment.
7094 StringRef CommentBegin = MAI.getCommentString();
7095 unsigned CommentColumn = MAI.getCommentColumn();
7096 bool IsFirst = true;
7097 while (!Comments.empty()) {
7098 if (!IsFirst)
7099 FormattedOS << '\n';
7100 // Emit a line of comments.
7101 FormattedOS.PadToColumn(CommentColumn);
7102 size_t Position = Comments.find('\n');
7103 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
7104 // Move after the newline character.
7105 Comments = Comments.substr(Position + 1);
7106 IsFirst = false;
7108 FormattedOS.flush();
7110 // Tell the comment stream that the vector changed underneath it.
7111 CommentsToEmit.clear();
7114 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
7115 StringRef DisSegName, StringRef DisSectName) {
7116 const char *McpuDefault = nullptr;
7117 const Target *ThumbTarget = nullptr;
7118 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
7119 if (!TheTarget) {
7120 // GetTarget prints out stuff.
7121 return;
7123 std::string MachOMCPU;
7124 if (MCPU.empty() && McpuDefault)
7125 MachOMCPU = McpuDefault;
7126 else
7127 MachOMCPU = MCPU;
7129 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
7130 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
7131 if (ThumbTarget)
7132 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
7134 // Package up features to be passed to target/subtarget
7135 std::string FeaturesStr;
7136 if (!MAttrs.empty()) {
7137 SubtargetFeatures Features;
7138 for (unsigned i = 0; i != MAttrs.size(); ++i)
7139 Features.AddFeature(MAttrs[i]);
7140 FeaturesStr = Features.getString();
7143 // Set up disassembler.
7144 std::unique_ptr<const MCRegisterInfo> MRI(
7145 TheTarget->createMCRegInfo(TripleName));
7146 std::unique_ptr<const MCAsmInfo> AsmInfo(
7147 TheTarget->createMCAsmInfo(*MRI, TripleName));
7148 std::unique_ptr<const MCSubtargetInfo> STI(
7149 TheTarget->createMCSubtargetInfo(TripleName, MachOMCPU, FeaturesStr));
7150 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
7151 std::unique_ptr<MCDisassembler> DisAsm(
7152 TheTarget->createMCDisassembler(*STI, Ctx));
7153 std::unique_ptr<MCSymbolizer> Symbolizer;
7154 struct DisassembleInfo SymbolizerInfo(nullptr, nullptr, nullptr, false);
7155 std::unique_ptr<MCRelocationInfo> RelInfo(
7156 TheTarget->createMCRelocationInfo(TripleName, Ctx));
7157 if (RelInfo) {
7158 Symbolizer.reset(TheTarget->createMCSymbolizer(
7159 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
7160 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
7161 DisAsm->setSymbolizer(std::move(Symbolizer));
7163 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
7164 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
7165 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
7166 // Set the display preference for hex vs. decimal immediates.
7167 IP->setPrintImmHex(PrintImmHex);
7168 // Comment stream and backing vector.
7169 SmallString<128> CommentsToEmit;
7170 raw_svector_ostream CommentStream(CommentsToEmit);
7171 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
7172 // if it is done then arm64 comments for string literals don't get printed
7173 // and some constant get printed instead and not setting it causes intel
7174 // (32-bit and 64-bit) comments printed with different spacing before the
7175 // comment causing different diffs with the 'C' disassembler library API.
7176 // IP->setCommentStream(CommentStream);
7178 if (!AsmInfo || !STI || !DisAsm || !IP) {
7179 WithColor::error(errs(), "llvm-objdump")
7180 << "couldn't initialize disassembler for target " << TripleName << '\n';
7181 return;
7184 // Set up separate thumb disassembler if needed.
7185 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
7186 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
7187 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
7188 std::unique_ptr<MCDisassembler> ThumbDisAsm;
7189 std::unique_ptr<MCInstPrinter> ThumbIP;
7190 std::unique_ptr<MCContext> ThumbCtx;
7191 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
7192 struct DisassembleInfo ThumbSymbolizerInfo(nullptr, nullptr, nullptr, false);
7193 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
7194 if (ThumbTarget) {
7195 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
7196 ThumbAsmInfo.reset(
7197 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
7198 ThumbSTI.reset(
7199 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MachOMCPU,
7200 FeaturesStr));
7201 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
7202 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
7203 MCContext *PtrThumbCtx = ThumbCtx.get();
7204 ThumbRelInfo.reset(
7205 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
7206 if (ThumbRelInfo) {
7207 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
7208 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
7209 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
7210 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
7212 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
7213 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
7214 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
7215 *ThumbInstrInfo, *ThumbMRI));
7216 // Set the display preference for hex vs. decimal immediates.
7217 ThumbIP->setPrintImmHex(PrintImmHex);
7220 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
7221 WithColor::error(errs(), "llvm-objdump")
7222 << "couldn't initialize disassembler for target " << ThumbTripleName
7223 << '\n';
7224 return;
7227 MachO::mach_header Header = MachOOF->getHeader();
7229 // FIXME: Using the -cfg command line option, this code used to be able to
7230 // annotate relocations with the referenced symbol's name, and if this was
7231 // inside a __[cf]string section, the data it points to. This is now replaced
7232 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
7233 std::vector<SectionRef> Sections;
7234 std::vector<SymbolRef> Symbols;
7235 SmallVector<uint64_t, 8> FoundFns;
7236 uint64_t BaseSegmentAddress = 0;
7238 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
7239 BaseSegmentAddress);
7241 // Sort the symbols by address, just in case they didn't come in that way.
7242 llvm::sort(Symbols, SymbolSorter());
7244 // Build a data in code table that is sorted on by the address of each entry.
7245 uint64_t BaseAddress = 0;
7246 if (Header.filetype == MachO::MH_OBJECT)
7247 BaseAddress = Sections[0].getAddress();
7248 else
7249 BaseAddress = BaseSegmentAddress;
7250 DiceTable Dices;
7251 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
7252 DI != DE; ++DI) {
7253 uint32_t Offset;
7254 DI->getOffset(Offset);
7255 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
7257 array_pod_sort(Dices.begin(), Dices.end());
7259 #ifndef NDEBUG
7260 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
7261 #else
7262 raw_ostream &DebugOut = nulls();
7263 #endif
7265 // Try to find debug info and set up the DIContext for it.
7266 std::unique_ptr<DIContext> diContext;
7267 std::unique_ptr<Binary> DSYMBinary;
7268 std::unique_ptr<MemoryBuffer> DSYMBuf;
7269 if (UseDbg) {
7270 ObjectFile *DbgObj = MachOOF;
7272 // A separate DSym file path was specified, parse it as a macho file,
7273 // get the sections and supply it to the section name parsing machinery.
7274 if (!DSYMFile.empty()) {
7275 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
7276 MemoryBuffer::getFileOrSTDIN(DSYMFile);
7277 if (std::error_code EC = BufOrErr.getError()) {
7278 report_error(errorCodeToError(EC), DSYMFile);
7279 return;
7282 // We need to keep the file alive, because we're replacing DbgObj with it.
7283 DSYMBuf = std::move(BufOrErr.get());
7285 Expected<std::unique_ptr<Binary>> BinaryOrErr =
7286 createBinary(DSYMBuf.get()->getMemBufferRef());
7287 if (!BinaryOrErr) {
7288 report_error(BinaryOrErr.takeError(), DSYMFile);
7289 return;
7292 // We need to keep the Binary elive with the buffer
7293 DSYMBinary = std::move(BinaryOrErr.get());
7295 if (ObjectFile *O = dyn_cast<ObjectFile>(DSYMBinary.get())) {
7296 // this is a Mach-O object file, use it
7297 if (MachOObjectFile *MachDSYM = dyn_cast<MachOObjectFile>(&*O)) {
7298 DbgObj = MachDSYM;
7300 else {
7301 WithColor::error(errs(), "llvm-objdump")
7302 << DSYMFile << " is not a Mach-O file type.\n";
7303 return;
7306 else if (auto UB = dyn_cast<MachOUniversalBinary>(DSYMBinary.get())){
7307 // this is a Universal Binary, find a Mach-O for this architecture
7308 uint32_t CPUType, CPUSubType;
7309 const char *ArchFlag;
7310 if (MachOOF->is64Bit()) {
7311 const MachO::mach_header_64 H_64 = MachOOF->getHeader64();
7312 CPUType = H_64.cputype;
7313 CPUSubType = H_64.cpusubtype;
7314 } else {
7315 const MachO::mach_header H = MachOOF->getHeader();
7316 CPUType = H.cputype;
7317 CPUSubType = H.cpusubtype;
7319 Triple T = MachOObjectFile::getArchTriple(CPUType, CPUSubType, nullptr,
7320 &ArchFlag);
7321 Expected<std::unique_ptr<MachOObjectFile>> MachDSYM =
7322 UB->getObjectForArch(ArchFlag);
7323 if (!MachDSYM) {
7324 report_error(MachDSYM.takeError(), DSYMFile);
7325 return;
7328 // We need to keep the Binary elive with the buffer
7329 DbgObj = &*MachDSYM.get();
7330 DSYMBinary = std::move(*MachDSYM);
7332 else {
7333 WithColor::error(errs(), "llvm-objdump")
7334 << DSYMFile << " is not a Mach-O or Universal file type.\n";
7335 return;
7339 // Setup the DIContext
7340 diContext = DWARFContext::create(*DbgObj);
7343 if (FilterSections.empty())
7344 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
7346 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
7347 StringRef SectName;
7348 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
7349 continue;
7351 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
7353 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
7354 if (SegmentName != DisSegName)
7355 continue;
7357 StringRef BytesStr =
7358 unwrapOrError(Sections[SectIdx].getContents(), Filename);
7359 ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr);
7360 uint64_t SectAddress = Sections[SectIdx].getAddress();
7362 bool symbolTableWorked = false;
7364 // Create a map of symbol addresses to symbol names for use by
7365 // the SymbolizerSymbolLookUp() routine.
7366 SymbolAddressMap AddrMap;
7367 bool DisSymNameFound = false;
7368 for (const SymbolRef &Symbol : MachOOF->symbols()) {
7369 SymbolRef::Type ST =
7370 unwrapOrError(Symbol.getType(), MachOOF->getFileName());
7371 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
7372 ST == SymbolRef::ST_Other) {
7373 uint64_t Address = Symbol.getValue();
7374 StringRef SymName =
7375 unwrapOrError(Symbol.getName(), MachOOF->getFileName());
7376 AddrMap[Address] = SymName;
7377 if (!DisSymName.empty() && DisSymName == SymName)
7378 DisSymNameFound = true;
7381 if (!DisSymName.empty() && !DisSymNameFound) {
7382 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
7383 return;
7385 // Set up the block of info used by the Symbolizer call backs.
7386 SymbolizerInfo.verbose = !NoSymbolicOperands;
7387 SymbolizerInfo.O = MachOOF;
7388 SymbolizerInfo.S = Sections[SectIdx];
7389 SymbolizerInfo.AddrMap = &AddrMap;
7390 SymbolizerInfo.Sections = &Sections;
7391 // Same for the ThumbSymbolizer
7392 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
7393 ThumbSymbolizerInfo.O = MachOOF;
7394 ThumbSymbolizerInfo.S = Sections[SectIdx];
7395 ThumbSymbolizerInfo.AddrMap = &AddrMap;
7396 ThumbSymbolizerInfo.Sections = &Sections;
7398 unsigned int Arch = MachOOF->getArch();
7400 // Skip all symbols if this is a stubs file.
7401 if (Bytes.empty())
7402 return;
7404 // If the section has symbols but no symbol at the start of the section
7405 // these are used to make sure the bytes before the first symbol are
7406 // disassembled.
7407 bool FirstSymbol = true;
7408 bool FirstSymbolAtSectionStart = true;
7410 // Disassemble symbol by symbol.
7411 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
7412 StringRef SymName =
7413 unwrapOrError(Symbols[SymIdx].getName(), MachOOF->getFileName());
7414 SymbolRef::Type ST =
7415 unwrapOrError(Symbols[SymIdx].getType(), MachOOF->getFileName());
7416 if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
7417 continue;
7419 // Make sure the symbol is defined in this section.
7420 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
7421 if (!containsSym) {
7422 if (!DisSymName.empty() && DisSymName == SymName) {
7423 outs() << "-dis-symname: " << DisSymName << " not in the section\n";
7424 return;
7426 continue;
7428 // The __mh_execute_header is special and we need to deal with that fact
7429 // this symbol is before the start of the (__TEXT,__text) section and at the
7430 // address of the start of the __TEXT segment. This is because this symbol
7431 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
7432 // start of the section in a standard MH_EXECUTE filetype.
7433 if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
7434 outs() << "-dis-symname: __mh_execute_header not in any section\n";
7435 return;
7437 // When this code is trying to disassemble a symbol at a time and in the
7438 // case there is only the __mh_execute_header symbol left as in a stripped
7439 // executable, we need to deal with this by ignoring this symbol so the
7440 // whole section is disassembled and this symbol is then not displayed.
7441 if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
7442 SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
7443 SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
7444 continue;
7446 // If we are only disassembling one symbol see if this is that symbol.
7447 if (!DisSymName.empty() && DisSymName != SymName)
7448 continue;
7450 // Start at the address of the symbol relative to the section's address.
7451 uint64_t SectSize = Sections[SectIdx].getSize();
7452 uint64_t Start = Symbols[SymIdx].getValue();
7453 uint64_t SectionAddress = Sections[SectIdx].getAddress();
7454 Start -= SectionAddress;
7456 if (Start > SectSize) {
7457 outs() << "section data ends, " << SymName
7458 << " lies outside valid range\n";
7459 return;
7462 // Stop disassembling either at the beginning of the next symbol or at
7463 // the end of the section.
7464 bool containsNextSym = false;
7465 uint64_t NextSym = 0;
7466 uint64_t NextSymIdx = SymIdx + 1;
7467 while (Symbols.size() > NextSymIdx) {
7468 SymbolRef::Type NextSymType = unwrapOrError(
7469 Symbols[NextSymIdx].getType(), MachOOF->getFileName());
7470 if (NextSymType == SymbolRef::ST_Function) {
7471 containsNextSym =
7472 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
7473 NextSym = Symbols[NextSymIdx].getValue();
7474 NextSym -= SectionAddress;
7475 break;
7477 ++NextSymIdx;
7480 uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
7481 uint64_t Size;
7483 symbolTableWorked = true;
7485 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
7486 bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
7488 // We only need the dedicated Thumb target if there's a real choice
7489 // (i.e. we're not targeting M-class) and the function is Thumb.
7490 bool UseThumbTarget = IsThumb && ThumbTarget;
7492 // If we are not specifying a symbol to start disassembly with and this
7493 // is the first symbol in the section but not at the start of the section
7494 // then move the disassembly index to the start of the section and
7495 // don't print the symbol name just yet. This is so the bytes before the
7496 // first symbol are disassembled.
7497 uint64_t SymbolStart = Start;
7498 if (DisSymName.empty() && FirstSymbol && Start != 0) {
7499 FirstSymbolAtSectionStart = false;
7500 Start = 0;
7502 else
7503 outs() << SymName << ":\n";
7505 DILineInfo lastLine;
7506 for (uint64_t Index = Start; Index < End; Index += Size) {
7507 MCInst Inst;
7509 // If this is the first symbol in the section and it was not at the
7510 // start of the section, see if we are at its Index now and if so print
7511 // the symbol name.
7512 if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart)
7513 outs() << SymName << ":\n";
7515 uint64_t PC = SectAddress + Index;
7516 if (!NoLeadingAddr) {
7517 if (FullLeadingAddr) {
7518 if (MachOOF->is64Bit())
7519 outs() << format("%016" PRIx64, PC);
7520 else
7521 outs() << format("%08" PRIx64, PC);
7522 } else {
7523 outs() << format("%8" PRIx64 ":", PC);
7526 if (!NoShowRawInsn || Arch == Triple::arm)
7527 outs() << "\t";
7529 if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, Size))
7530 continue;
7532 SmallVector<char, 64> AnnotationsBytes;
7533 raw_svector_ostream Annotations(AnnotationsBytes);
7535 bool gotInst;
7536 if (UseThumbTarget)
7537 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
7538 PC, DebugOut, Annotations);
7539 else
7540 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
7541 DebugOut, Annotations);
7542 if (gotInst) {
7543 if (!NoShowRawInsn || Arch == Triple::arm) {
7544 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
7546 formatted_raw_ostream FormattedOS(outs());
7547 StringRef AnnotationsStr = Annotations.str();
7548 if (UseThumbTarget)
7549 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
7550 else
7551 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
7552 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
7554 // Print debug info.
7555 if (diContext) {
7556 DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx});
7557 // Print valid line info if it changed.
7558 if (dli != lastLine && dli.Line != 0)
7559 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
7560 << dli.Column;
7561 lastLine = dli;
7563 outs() << "\n";
7564 } else {
7565 unsigned int Arch = MachOOF->getArch();
7566 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
7567 outs() << format("\t.byte 0x%02x #bad opcode\n",
7568 *(Bytes.data() + Index) & 0xff);
7569 Size = 1; // skip exactly one illegible byte and move on.
7570 } else if (Arch == Triple::aarch64 ||
7571 (Arch == Triple::arm && !IsThumb)) {
7572 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
7573 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
7574 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
7575 (*(Bytes.data() + Index + 3) & 0xff) << 24;
7576 outs() << format("\t.long\t0x%08x\n", opcode);
7577 Size = 4;
7578 } else if (Arch == Triple::arm) {
7579 assert(IsThumb && "ARM mode should have been dealt with above");
7580 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
7581 (*(Bytes.data() + Index + 1) & 0xff) << 8;
7582 outs() << format("\t.short\t0x%04x\n", opcode);
7583 Size = 2;
7584 } else{
7585 WithColor::warning(errs(), "llvm-objdump")
7586 << "invalid instruction encoding\n";
7587 if (Size == 0)
7588 Size = 1; // skip illegible bytes
7592 // Now that we are done disassembled the first symbol set the bool that
7593 // were doing this to false.
7594 FirstSymbol = false;
7596 if (!symbolTableWorked) {
7597 // Reading the symbol table didn't work, disassemble the whole section.
7598 uint64_t SectAddress = Sections[SectIdx].getAddress();
7599 uint64_t SectSize = Sections[SectIdx].getSize();
7600 uint64_t InstSize;
7601 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
7602 MCInst Inst;
7604 uint64_t PC = SectAddress + Index;
7606 if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, InstSize))
7607 continue;
7609 SmallVector<char, 64> AnnotationsBytes;
7610 raw_svector_ostream Annotations(AnnotationsBytes);
7611 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
7612 DebugOut, Annotations)) {
7613 if (!NoLeadingAddr) {
7614 if (FullLeadingAddr) {
7615 if (MachOOF->is64Bit())
7616 outs() << format("%016" PRIx64, PC);
7617 else
7618 outs() << format("%08" PRIx64, PC);
7619 } else {
7620 outs() << format("%8" PRIx64 ":", PC);
7623 if (!NoShowRawInsn || Arch == Triple::arm) {
7624 outs() << "\t";
7625 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
7627 StringRef AnnotationsStr = Annotations.str();
7628 IP->printInst(&Inst, outs(), AnnotationsStr, *STI);
7629 outs() << "\n";
7630 } else {
7631 unsigned int Arch = MachOOF->getArch();
7632 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
7633 outs() << format("\t.byte 0x%02x #bad opcode\n",
7634 *(Bytes.data() + Index) & 0xff);
7635 InstSize = 1; // skip exactly one illegible byte and move on.
7636 } else {
7637 WithColor::warning(errs(), "llvm-objdump")
7638 << "invalid instruction encoding\n";
7639 if (InstSize == 0)
7640 InstSize = 1; // skip illegible bytes
7645 // The TripleName's need to be reset if we are called again for a different
7646 // archtecture.
7647 TripleName = "";
7648 ThumbTripleName = "";
7650 if (SymbolizerInfo.demangled_name != nullptr)
7651 free(SymbolizerInfo.demangled_name);
7652 if (ThumbSymbolizerInfo.demangled_name != nullptr)
7653 free(ThumbSymbolizerInfo.demangled_name);
7657 //===----------------------------------------------------------------------===//
7658 // __compact_unwind section dumping
7659 //===----------------------------------------------------------------------===//
7661 namespace {
7663 template <typename T>
7664 static uint64_t read(StringRef Contents, ptrdiff_t Offset) {
7665 using llvm::support::little;
7666 using llvm::support::unaligned;
7668 if (Offset + sizeof(T) > Contents.size()) {
7669 outs() << "warning: attempt to read past end of buffer\n";
7670 return T();
7673 uint64_t Val =
7674 support::endian::read<T, little, unaligned>(Contents.data() + Offset);
7675 return Val;
7678 template <typename T>
7679 static uint64_t readNext(StringRef Contents, ptrdiff_t &Offset) {
7680 T Val = read<T>(Contents, Offset);
7681 Offset += sizeof(T);
7682 return Val;
7685 struct CompactUnwindEntry {
7686 uint32_t OffsetInSection;
7688 uint64_t FunctionAddr;
7689 uint32_t Length;
7690 uint32_t CompactEncoding;
7691 uint64_t PersonalityAddr;
7692 uint64_t LSDAAddr;
7694 RelocationRef FunctionReloc;
7695 RelocationRef PersonalityReloc;
7696 RelocationRef LSDAReloc;
7698 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
7699 : OffsetInSection(Offset) {
7700 if (Is64)
7701 read<uint64_t>(Contents, Offset);
7702 else
7703 read<uint32_t>(Contents, Offset);
7706 private:
7707 template <typename UIntPtr> void read(StringRef Contents, ptrdiff_t Offset) {
7708 FunctionAddr = readNext<UIntPtr>(Contents, Offset);
7709 Length = readNext<uint32_t>(Contents, Offset);
7710 CompactEncoding = readNext<uint32_t>(Contents, Offset);
7711 PersonalityAddr = readNext<UIntPtr>(Contents, Offset);
7712 LSDAAddr = readNext<UIntPtr>(Contents, Offset);
7717 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
7718 /// and data being relocated, determine the best base Name and Addend to use for
7719 /// display purposes.
7721 /// 1. An Extern relocation will directly reference a symbol (and the data is
7722 /// then already an addend), so use that.
7723 /// 2. Otherwise the data is an offset in the object file's layout; try to find
7724 // a symbol before it in the same section, and use the offset from there.
7725 /// 3. Finally, if all that fails, fall back to an offset from the start of the
7726 /// referenced section.
7727 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
7728 std::map<uint64_t, SymbolRef> &Symbols,
7729 const RelocationRef &Reloc, uint64_t Addr,
7730 StringRef &Name, uint64_t &Addend) {
7731 if (Reloc.getSymbol() != Obj->symbol_end()) {
7732 Name = unwrapOrError(Reloc.getSymbol()->getName(), Obj->getFileName());
7733 Addend = Addr;
7734 return;
7737 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
7738 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
7740 uint64_t SectionAddr = RelocSection.getAddress();
7742 auto Sym = Symbols.upper_bound(Addr);
7743 if (Sym == Symbols.begin()) {
7744 // The first symbol in the object is after this reference, the best we can
7745 // do is section-relative notation.
7746 RelocSection.getName(Name);
7747 Addend = Addr - SectionAddr;
7748 return;
7751 // Go back one so that SymbolAddress <= Addr.
7752 --Sym;
7754 section_iterator SymSection =
7755 unwrapOrError(Sym->second.getSection(), Obj->getFileName());
7756 if (RelocSection == *SymSection) {
7757 // There's a valid symbol in the same section before this reference.
7758 Name = unwrapOrError(Sym->second.getName(), Obj->getFileName());
7759 Addend = Addr - Sym->first;
7760 return;
7763 // There is a symbol before this reference, but it's in a different
7764 // section. Probably not helpful to mention it, so use the section name.
7765 RelocSection.getName(Name);
7766 Addend = Addr - SectionAddr;
7769 static void printUnwindRelocDest(const MachOObjectFile *Obj,
7770 std::map<uint64_t, SymbolRef> &Symbols,
7771 const RelocationRef &Reloc, uint64_t Addr) {
7772 StringRef Name;
7773 uint64_t Addend;
7775 if (!Reloc.getObject())
7776 return;
7778 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
7780 outs() << Name;
7781 if (Addend)
7782 outs() << " + " << format("0x%" PRIx64, Addend);
7785 static void
7786 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
7787 std::map<uint64_t, SymbolRef> &Symbols,
7788 const SectionRef &CompactUnwind) {
7790 if (!Obj->isLittleEndian()) {
7791 outs() << "Skipping big-endian __compact_unwind section\n";
7792 return;
7795 bool Is64 = Obj->is64Bit();
7796 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
7797 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
7799 StringRef Contents =
7800 unwrapOrError(CompactUnwind.getContents(), Obj->getFileName());
7801 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
7803 // First populate the initial raw offsets, encodings and so on from the entry.
7804 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
7805 CompactUnwindEntry Entry(Contents, Offset, Is64);
7806 CompactUnwinds.push_back(Entry);
7809 // Next we need to look at the relocations to find out what objects are
7810 // actually being referred to.
7811 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
7812 uint64_t RelocAddress = Reloc.getOffset();
7814 uint32_t EntryIdx = RelocAddress / EntrySize;
7815 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
7816 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
7818 if (OffsetInEntry == 0)
7819 Entry.FunctionReloc = Reloc;
7820 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
7821 Entry.PersonalityReloc = Reloc;
7822 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
7823 Entry.LSDAReloc = Reloc;
7824 else {
7825 outs() << "Invalid relocation in __compact_unwind section\n";
7826 return;
7830 // Finally, we're ready to print the data we've gathered.
7831 outs() << "Contents of __compact_unwind section:\n";
7832 for (auto &Entry : CompactUnwinds) {
7833 outs() << " Entry at offset "
7834 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
7836 // 1. Start of the region this entry applies to.
7837 outs() << " start: " << format("0x%" PRIx64,
7838 Entry.FunctionAddr) << ' ';
7839 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
7840 outs() << '\n';
7842 // 2. Length of the region this entry applies to.
7843 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
7844 << '\n';
7845 // 3. The 32-bit compact encoding.
7846 outs() << " compact encoding: "
7847 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
7849 // 4. The personality function, if present.
7850 if (Entry.PersonalityReloc.getObject()) {
7851 outs() << " personality function: "
7852 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
7853 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
7854 Entry.PersonalityAddr);
7855 outs() << '\n';
7858 // 5. This entry's language-specific data area.
7859 if (Entry.LSDAReloc.getObject()) {
7860 outs() << " LSDA: " << format("0x%" PRIx64,
7861 Entry.LSDAAddr) << ' ';
7862 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
7863 outs() << '\n';
7868 //===----------------------------------------------------------------------===//
7869 // __unwind_info section dumping
7870 //===----------------------------------------------------------------------===//
7872 static void printRegularSecondLevelUnwindPage(StringRef PageData) {
7873 ptrdiff_t Pos = 0;
7874 uint32_t Kind = readNext<uint32_t>(PageData, Pos);
7875 (void)Kind;
7876 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
7878 uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos);
7879 uint16_t NumEntries = readNext<uint16_t>(PageData, Pos);
7881 Pos = EntriesStart;
7882 for (unsigned i = 0; i < NumEntries; ++i) {
7883 uint32_t FunctionOffset = readNext<uint32_t>(PageData, Pos);
7884 uint32_t Encoding = readNext<uint32_t>(PageData, Pos);
7886 outs() << " [" << i << "]: "
7887 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7888 << ", "
7889 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
7893 static void printCompressedSecondLevelUnwindPage(
7894 StringRef PageData, uint32_t FunctionBase,
7895 const SmallVectorImpl<uint32_t> &CommonEncodings) {
7896 ptrdiff_t Pos = 0;
7897 uint32_t Kind = readNext<uint32_t>(PageData, Pos);
7898 (void)Kind;
7899 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
7901 uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos);
7902 uint16_t NumEntries = readNext<uint16_t>(PageData, Pos);
7904 uint16_t EncodingsStart = readNext<uint16_t>(PageData, Pos);
7905 readNext<uint16_t>(PageData, Pos);
7906 StringRef PageEncodings = PageData.substr(EncodingsStart, StringRef::npos);
7908 Pos = EntriesStart;
7909 for (unsigned i = 0; i < NumEntries; ++i) {
7910 uint32_t Entry = readNext<uint32_t>(PageData, Pos);
7911 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
7912 uint32_t EncodingIdx = Entry >> 24;
7914 uint32_t Encoding;
7915 if (EncodingIdx < CommonEncodings.size())
7916 Encoding = CommonEncodings[EncodingIdx];
7917 else
7918 Encoding = read<uint32_t>(PageEncodings,
7919 sizeof(uint32_t) *
7920 (EncodingIdx - CommonEncodings.size()));
7922 outs() << " [" << i << "]: "
7923 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7924 << ", "
7925 << "encoding[" << EncodingIdx
7926 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
7930 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
7931 std::map<uint64_t, SymbolRef> &Symbols,
7932 const SectionRef &UnwindInfo) {
7934 if (!Obj->isLittleEndian()) {
7935 outs() << "Skipping big-endian __unwind_info section\n";
7936 return;
7939 outs() << "Contents of __unwind_info section:\n";
7941 StringRef Contents =
7942 unwrapOrError(UnwindInfo.getContents(), Obj->getFileName());
7943 ptrdiff_t Pos = 0;
7945 //===----------------------------------
7946 // Section header
7947 //===----------------------------------
7949 uint32_t Version = readNext<uint32_t>(Contents, Pos);
7950 outs() << " Version: "
7951 << format("0x%" PRIx32, Version) << '\n';
7952 if (Version != 1) {
7953 outs() << " Skipping section with unknown version\n";
7954 return;
7957 uint32_t CommonEncodingsStart = readNext<uint32_t>(Contents, Pos);
7958 outs() << " Common encodings array section offset: "
7959 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
7960 uint32_t NumCommonEncodings = readNext<uint32_t>(Contents, Pos);
7961 outs() << " Number of common encodings in array: "
7962 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
7964 uint32_t PersonalitiesStart = readNext<uint32_t>(Contents, Pos);
7965 outs() << " Personality function array section offset: "
7966 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
7967 uint32_t NumPersonalities = readNext<uint32_t>(Contents, Pos);
7968 outs() << " Number of personality functions in array: "
7969 << format("0x%" PRIx32, NumPersonalities) << '\n';
7971 uint32_t IndicesStart = readNext<uint32_t>(Contents, Pos);
7972 outs() << " Index array section offset: "
7973 << format("0x%" PRIx32, IndicesStart) << '\n';
7974 uint32_t NumIndices = readNext<uint32_t>(Contents, Pos);
7975 outs() << " Number of indices in array: "
7976 << format("0x%" PRIx32, NumIndices) << '\n';
7978 //===----------------------------------
7979 // A shared list of common encodings
7980 //===----------------------------------
7982 // These occupy indices in the range [0, N] whenever an encoding is referenced
7983 // from a compressed 2nd level index table. In practice the linker only
7984 // creates ~128 of these, so that indices are available to embed encodings in
7985 // the 2nd level index.
7987 SmallVector<uint32_t, 64> CommonEncodings;
7988 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
7989 Pos = CommonEncodingsStart;
7990 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
7991 uint32_t Encoding = readNext<uint32_t>(Contents, Pos);
7992 CommonEncodings.push_back(Encoding);
7994 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
7995 << '\n';
7998 //===----------------------------------
7999 // Personality functions used in this executable
8000 //===----------------------------------
8002 // There should be only a handful of these (one per source language,
8003 // roughly). Particularly since they only get 2 bits in the compact encoding.
8005 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
8006 Pos = PersonalitiesStart;
8007 for (unsigned i = 0; i < NumPersonalities; ++i) {
8008 uint32_t PersonalityFn = readNext<uint32_t>(Contents, Pos);
8009 outs() << " personality[" << i + 1
8010 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
8013 //===----------------------------------
8014 // The level 1 index entries
8015 //===----------------------------------
8017 // These specify an approximate place to start searching for the more detailed
8018 // information, sorted by PC.
8020 struct IndexEntry {
8021 uint32_t FunctionOffset;
8022 uint32_t SecondLevelPageStart;
8023 uint32_t LSDAStart;
8026 SmallVector<IndexEntry, 4> IndexEntries;
8028 outs() << " Top level indices: (count = " << NumIndices << ")\n";
8029 Pos = IndicesStart;
8030 for (unsigned i = 0; i < NumIndices; ++i) {
8031 IndexEntry Entry;
8033 Entry.FunctionOffset = readNext<uint32_t>(Contents, Pos);
8034 Entry.SecondLevelPageStart = readNext<uint32_t>(Contents, Pos);
8035 Entry.LSDAStart = readNext<uint32_t>(Contents, Pos);
8036 IndexEntries.push_back(Entry);
8038 outs() << " [" << i << "]: "
8039 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
8040 << ", "
8041 << "2nd level page offset="
8042 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
8043 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
8046 //===----------------------------------
8047 // Next come the LSDA tables
8048 //===----------------------------------
8050 // The LSDA layout is rather implicit: it's a contiguous array of entries from
8051 // the first top-level index's LSDAOffset to the last (sentinel).
8053 outs() << " LSDA descriptors:\n";
8054 Pos = IndexEntries[0].LSDAStart;
8055 const uint32_t LSDASize = 2 * sizeof(uint32_t);
8056 int NumLSDAs =
8057 (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) / LSDASize;
8059 for (int i = 0; i < NumLSDAs; ++i) {
8060 uint32_t FunctionOffset = readNext<uint32_t>(Contents, Pos);
8061 uint32_t LSDAOffset = readNext<uint32_t>(Contents, Pos);
8062 outs() << " [" << i << "]: "
8063 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
8064 << ", "
8065 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
8068 //===----------------------------------
8069 // Finally, the 2nd level indices
8070 //===----------------------------------
8072 // Generally these are 4K in size, and have 2 possible forms:
8073 // + Regular stores up to 511 entries with disparate encodings
8074 // + Compressed stores up to 1021 entries if few enough compact encoding
8075 // values are used.
8076 outs() << " Second level indices:\n";
8077 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
8078 // The final sentinel top-level index has no associated 2nd level page
8079 if (IndexEntries[i].SecondLevelPageStart == 0)
8080 break;
8082 outs() << " Second level index[" << i << "]: "
8083 << "offset in section="
8084 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
8085 << ", "
8086 << "base function offset="
8087 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
8089 Pos = IndexEntries[i].SecondLevelPageStart;
8090 if (Pos + sizeof(uint32_t) > Contents.size()) {
8091 outs() << "warning: invalid offset for second level page: " << Pos << '\n';
8092 continue;
8095 uint32_t Kind =
8096 *reinterpret_cast<const support::ulittle32_t *>(Contents.data() + Pos);
8097 if (Kind == 2)
8098 printRegularSecondLevelUnwindPage(Contents.substr(Pos, 4096));
8099 else if (Kind == 3)
8100 printCompressedSecondLevelUnwindPage(Contents.substr(Pos, 4096),
8101 IndexEntries[i].FunctionOffset,
8102 CommonEncodings);
8103 else
8104 outs() << " Skipping 2nd level page with unknown kind " << Kind
8105 << '\n';
8109 void printMachOUnwindInfo(const MachOObjectFile *Obj) {
8110 std::map<uint64_t, SymbolRef> Symbols;
8111 for (const SymbolRef &SymRef : Obj->symbols()) {
8112 // Discard any undefined or absolute symbols. They're not going to take part
8113 // in the convenience lookup for unwind info and just take up resources.
8114 auto SectOrErr = SymRef.getSection();
8115 if (!SectOrErr) {
8116 // TODO: Actually report errors helpfully.
8117 consumeError(SectOrErr.takeError());
8118 continue;
8120 section_iterator Section = *SectOrErr;
8121 if (Section == Obj->section_end())
8122 continue;
8124 uint64_t Addr = SymRef.getValue();
8125 Symbols.insert(std::make_pair(Addr, SymRef));
8128 for (const SectionRef &Section : Obj->sections()) {
8129 StringRef SectName;
8130 Section.getName(SectName);
8131 if (SectName == "__compact_unwind")
8132 printMachOCompactUnwindSection(Obj, Symbols, Section);
8133 else if (SectName == "__unwind_info")
8134 printMachOUnwindInfoSection(Obj, Symbols, Section);
8138 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
8139 uint32_t cpusubtype, uint32_t filetype,
8140 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
8141 bool verbose) {
8142 outs() << "Mach header\n";
8143 outs() << " magic cputype cpusubtype caps filetype ncmds "
8144 "sizeofcmds flags\n";
8145 if (verbose) {
8146 if (magic == MachO::MH_MAGIC)
8147 outs() << " MH_MAGIC";
8148 else if (magic == MachO::MH_MAGIC_64)
8149 outs() << "MH_MAGIC_64";
8150 else
8151 outs() << format(" 0x%08" PRIx32, magic);
8152 switch (cputype) {
8153 case MachO::CPU_TYPE_I386:
8154 outs() << " I386";
8155 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8156 case MachO::CPU_SUBTYPE_I386_ALL:
8157 outs() << " ALL";
8158 break;
8159 default:
8160 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8161 break;
8163 break;
8164 case MachO::CPU_TYPE_X86_64:
8165 outs() << " X86_64";
8166 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8167 case MachO::CPU_SUBTYPE_X86_64_ALL:
8168 outs() << " ALL";
8169 break;
8170 case MachO::CPU_SUBTYPE_X86_64_H:
8171 outs() << " Haswell";
8172 break;
8173 default:
8174 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8175 break;
8177 break;
8178 case MachO::CPU_TYPE_ARM:
8179 outs() << " ARM";
8180 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8181 case MachO::CPU_SUBTYPE_ARM_ALL:
8182 outs() << " ALL";
8183 break;
8184 case MachO::CPU_SUBTYPE_ARM_V4T:
8185 outs() << " V4T";
8186 break;
8187 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
8188 outs() << " V5TEJ";
8189 break;
8190 case MachO::CPU_SUBTYPE_ARM_XSCALE:
8191 outs() << " XSCALE";
8192 break;
8193 case MachO::CPU_SUBTYPE_ARM_V6:
8194 outs() << " V6";
8195 break;
8196 case MachO::CPU_SUBTYPE_ARM_V6M:
8197 outs() << " V6M";
8198 break;
8199 case MachO::CPU_SUBTYPE_ARM_V7:
8200 outs() << " V7";
8201 break;
8202 case MachO::CPU_SUBTYPE_ARM_V7EM:
8203 outs() << " V7EM";
8204 break;
8205 case MachO::CPU_SUBTYPE_ARM_V7K:
8206 outs() << " V7K";
8207 break;
8208 case MachO::CPU_SUBTYPE_ARM_V7M:
8209 outs() << " V7M";
8210 break;
8211 case MachO::CPU_SUBTYPE_ARM_V7S:
8212 outs() << " V7S";
8213 break;
8214 default:
8215 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8216 break;
8218 break;
8219 case MachO::CPU_TYPE_ARM64:
8220 outs() << " ARM64";
8221 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8222 case MachO::CPU_SUBTYPE_ARM64_ALL:
8223 outs() << " ALL";
8224 break;
8225 case MachO::CPU_SUBTYPE_ARM64E:
8226 outs() << " E";
8227 break;
8228 default:
8229 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8230 break;
8232 break;
8233 case MachO::CPU_TYPE_ARM64_32:
8234 outs() << " ARM64_32";
8235 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8236 case MachO::CPU_SUBTYPE_ARM64_32_V8:
8237 outs() << " V8";
8238 break;
8239 default:
8240 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8241 break;
8243 break;
8244 case MachO::CPU_TYPE_POWERPC:
8245 outs() << " PPC";
8246 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8247 case MachO::CPU_SUBTYPE_POWERPC_ALL:
8248 outs() << " ALL";
8249 break;
8250 default:
8251 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8252 break;
8254 break;
8255 case MachO::CPU_TYPE_POWERPC64:
8256 outs() << " PPC64";
8257 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8258 case MachO::CPU_SUBTYPE_POWERPC_ALL:
8259 outs() << " ALL";
8260 break;
8261 default:
8262 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8263 break;
8265 break;
8266 default:
8267 outs() << format(" %7d", cputype);
8268 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8269 break;
8271 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
8272 outs() << " LIB64";
8273 } else {
8274 outs() << format(" 0x%02" PRIx32,
8275 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
8277 switch (filetype) {
8278 case MachO::MH_OBJECT:
8279 outs() << " OBJECT";
8280 break;
8281 case MachO::MH_EXECUTE:
8282 outs() << " EXECUTE";
8283 break;
8284 case MachO::MH_FVMLIB:
8285 outs() << " FVMLIB";
8286 break;
8287 case MachO::MH_CORE:
8288 outs() << " CORE";
8289 break;
8290 case MachO::MH_PRELOAD:
8291 outs() << " PRELOAD";
8292 break;
8293 case MachO::MH_DYLIB:
8294 outs() << " DYLIB";
8295 break;
8296 case MachO::MH_DYLIB_STUB:
8297 outs() << " DYLIB_STUB";
8298 break;
8299 case MachO::MH_DYLINKER:
8300 outs() << " DYLINKER";
8301 break;
8302 case MachO::MH_BUNDLE:
8303 outs() << " BUNDLE";
8304 break;
8305 case MachO::MH_DSYM:
8306 outs() << " DSYM";
8307 break;
8308 case MachO::MH_KEXT_BUNDLE:
8309 outs() << " KEXTBUNDLE";
8310 break;
8311 default:
8312 outs() << format(" %10u", filetype);
8313 break;
8315 outs() << format(" %5u", ncmds);
8316 outs() << format(" %10u", sizeofcmds);
8317 uint32_t f = flags;
8318 if (f & MachO::MH_NOUNDEFS) {
8319 outs() << " NOUNDEFS";
8320 f &= ~MachO::MH_NOUNDEFS;
8322 if (f & MachO::MH_INCRLINK) {
8323 outs() << " INCRLINK";
8324 f &= ~MachO::MH_INCRLINK;
8326 if (f & MachO::MH_DYLDLINK) {
8327 outs() << " DYLDLINK";
8328 f &= ~MachO::MH_DYLDLINK;
8330 if (f & MachO::MH_BINDATLOAD) {
8331 outs() << " BINDATLOAD";
8332 f &= ~MachO::MH_BINDATLOAD;
8334 if (f & MachO::MH_PREBOUND) {
8335 outs() << " PREBOUND";
8336 f &= ~MachO::MH_PREBOUND;
8338 if (f & MachO::MH_SPLIT_SEGS) {
8339 outs() << " SPLIT_SEGS";
8340 f &= ~MachO::MH_SPLIT_SEGS;
8342 if (f & MachO::MH_LAZY_INIT) {
8343 outs() << " LAZY_INIT";
8344 f &= ~MachO::MH_LAZY_INIT;
8346 if (f & MachO::MH_TWOLEVEL) {
8347 outs() << " TWOLEVEL";
8348 f &= ~MachO::MH_TWOLEVEL;
8350 if (f & MachO::MH_FORCE_FLAT) {
8351 outs() << " FORCE_FLAT";
8352 f &= ~MachO::MH_FORCE_FLAT;
8354 if (f & MachO::MH_NOMULTIDEFS) {
8355 outs() << " NOMULTIDEFS";
8356 f &= ~MachO::MH_NOMULTIDEFS;
8358 if (f & MachO::MH_NOFIXPREBINDING) {
8359 outs() << " NOFIXPREBINDING";
8360 f &= ~MachO::MH_NOFIXPREBINDING;
8362 if (f & MachO::MH_PREBINDABLE) {
8363 outs() << " PREBINDABLE";
8364 f &= ~MachO::MH_PREBINDABLE;
8366 if (f & MachO::MH_ALLMODSBOUND) {
8367 outs() << " ALLMODSBOUND";
8368 f &= ~MachO::MH_ALLMODSBOUND;
8370 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
8371 outs() << " SUBSECTIONS_VIA_SYMBOLS";
8372 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
8374 if (f & MachO::MH_CANONICAL) {
8375 outs() << " CANONICAL";
8376 f &= ~MachO::MH_CANONICAL;
8378 if (f & MachO::MH_WEAK_DEFINES) {
8379 outs() << " WEAK_DEFINES";
8380 f &= ~MachO::MH_WEAK_DEFINES;
8382 if (f & MachO::MH_BINDS_TO_WEAK) {
8383 outs() << " BINDS_TO_WEAK";
8384 f &= ~MachO::MH_BINDS_TO_WEAK;
8386 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
8387 outs() << " ALLOW_STACK_EXECUTION";
8388 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
8390 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
8391 outs() << " DEAD_STRIPPABLE_DYLIB";
8392 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
8394 if (f & MachO::MH_PIE) {
8395 outs() << " PIE";
8396 f &= ~MachO::MH_PIE;
8398 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
8399 outs() << " NO_REEXPORTED_DYLIBS";
8400 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
8402 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
8403 outs() << " MH_HAS_TLV_DESCRIPTORS";
8404 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
8406 if (f & MachO::MH_NO_HEAP_EXECUTION) {
8407 outs() << " MH_NO_HEAP_EXECUTION";
8408 f &= ~MachO::MH_NO_HEAP_EXECUTION;
8410 if (f & MachO::MH_APP_EXTENSION_SAFE) {
8411 outs() << " APP_EXTENSION_SAFE";
8412 f &= ~MachO::MH_APP_EXTENSION_SAFE;
8414 if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) {
8415 outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
8416 f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO;
8418 if (f != 0 || flags == 0)
8419 outs() << format(" 0x%08" PRIx32, f);
8420 } else {
8421 outs() << format(" 0x%08" PRIx32, magic);
8422 outs() << format(" %7d", cputype);
8423 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8424 outs() << format(" 0x%02" PRIx32,
8425 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
8426 outs() << format(" %10u", filetype);
8427 outs() << format(" %5u", ncmds);
8428 outs() << format(" %10u", sizeofcmds);
8429 outs() << format(" 0x%08" PRIx32, flags);
8431 outs() << "\n";
8434 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
8435 StringRef SegName, uint64_t vmaddr,
8436 uint64_t vmsize, uint64_t fileoff,
8437 uint64_t filesize, uint32_t maxprot,
8438 uint32_t initprot, uint32_t nsects,
8439 uint32_t flags, uint32_t object_size,
8440 bool verbose) {
8441 uint64_t expected_cmdsize;
8442 if (cmd == MachO::LC_SEGMENT) {
8443 outs() << " cmd LC_SEGMENT\n";
8444 expected_cmdsize = nsects;
8445 expected_cmdsize *= sizeof(struct MachO::section);
8446 expected_cmdsize += sizeof(struct MachO::segment_command);
8447 } else {
8448 outs() << " cmd LC_SEGMENT_64\n";
8449 expected_cmdsize = nsects;
8450 expected_cmdsize *= sizeof(struct MachO::section_64);
8451 expected_cmdsize += sizeof(struct MachO::segment_command_64);
8453 outs() << " cmdsize " << cmdsize;
8454 if (cmdsize != expected_cmdsize)
8455 outs() << " Inconsistent size\n";
8456 else
8457 outs() << "\n";
8458 outs() << " segname " << SegName << "\n";
8459 if (cmd == MachO::LC_SEGMENT_64) {
8460 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
8461 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
8462 } else {
8463 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
8464 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
8466 outs() << " fileoff " << fileoff;
8467 if (fileoff > object_size)
8468 outs() << " (past end of file)\n";
8469 else
8470 outs() << "\n";
8471 outs() << " filesize " << filesize;
8472 if (fileoff + filesize > object_size)
8473 outs() << " (past end of file)\n";
8474 else
8475 outs() << "\n";
8476 if (verbose) {
8477 if ((maxprot &
8478 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
8479 MachO::VM_PROT_EXECUTE)) != 0)
8480 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
8481 else {
8482 outs() << " maxprot ";
8483 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
8484 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
8485 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
8487 if ((initprot &
8488 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
8489 MachO::VM_PROT_EXECUTE)) != 0)
8490 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
8491 else {
8492 outs() << " initprot ";
8493 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
8494 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
8495 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
8497 } else {
8498 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
8499 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
8501 outs() << " nsects " << nsects << "\n";
8502 if (verbose) {
8503 outs() << " flags";
8504 if (flags == 0)
8505 outs() << " (none)\n";
8506 else {
8507 if (flags & MachO::SG_HIGHVM) {
8508 outs() << " HIGHVM";
8509 flags &= ~MachO::SG_HIGHVM;
8511 if (flags & MachO::SG_FVMLIB) {
8512 outs() << " FVMLIB";
8513 flags &= ~MachO::SG_FVMLIB;
8515 if (flags & MachO::SG_NORELOC) {
8516 outs() << " NORELOC";
8517 flags &= ~MachO::SG_NORELOC;
8519 if (flags & MachO::SG_PROTECTED_VERSION_1) {
8520 outs() << " PROTECTED_VERSION_1";
8521 flags &= ~MachO::SG_PROTECTED_VERSION_1;
8523 if (flags)
8524 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
8525 else
8526 outs() << "\n";
8528 } else {
8529 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
8533 static void PrintSection(const char *sectname, const char *segname,
8534 uint64_t addr, uint64_t size, uint32_t offset,
8535 uint32_t align, uint32_t reloff, uint32_t nreloc,
8536 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
8537 uint32_t cmd, const char *sg_segname,
8538 uint32_t filetype, uint32_t object_size,
8539 bool verbose) {
8540 outs() << "Section\n";
8541 outs() << " sectname " << format("%.16s\n", sectname);
8542 outs() << " segname " << format("%.16s", segname);
8543 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
8544 outs() << " (does not match segment)\n";
8545 else
8546 outs() << "\n";
8547 if (cmd == MachO::LC_SEGMENT_64) {
8548 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
8549 outs() << " size " << format("0x%016" PRIx64, size);
8550 } else {
8551 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
8552 outs() << " size " << format("0x%08" PRIx64, size);
8554 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
8555 outs() << " (past end of file)\n";
8556 else
8557 outs() << "\n";
8558 outs() << " offset " << offset;
8559 if (offset > object_size)
8560 outs() << " (past end of file)\n";
8561 else
8562 outs() << "\n";
8563 uint32_t align_shifted = 1 << align;
8564 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
8565 outs() << " reloff " << reloff;
8566 if (reloff > object_size)
8567 outs() << " (past end of file)\n";
8568 else
8569 outs() << "\n";
8570 outs() << " nreloc " << nreloc;
8571 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
8572 outs() << " (past end of file)\n";
8573 else
8574 outs() << "\n";
8575 uint32_t section_type = flags & MachO::SECTION_TYPE;
8576 if (verbose) {
8577 outs() << " type";
8578 if (section_type == MachO::S_REGULAR)
8579 outs() << " S_REGULAR\n";
8580 else if (section_type == MachO::S_ZEROFILL)
8581 outs() << " S_ZEROFILL\n";
8582 else if (section_type == MachO::S_CSTRING_LITERALS)
8583 outs() << " S_CSTRING_LITERALS\n";
8584 else if (section_type == MachO::S_4BYTE_LITERALS)
8585 outs() << " S_4BYTE_LITERALS\n";
8586 else if (section_type == MachO::S_8BYTE_LITERALS)
8587 outs() << " S_8BYTE_LITERALS\n";
8588 else if (section_type == MachO::S_16BYTE_LITERALS)
8589 outs() << " S_16BYTE_LITERALS\n";
8590 else if (section_type == MachO::S_LITERAL_POINTERS)
8591 outs() << " S_LITERAL_POINTERS\n";
8592 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
8593 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
8594 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
8595 outs() << " S_LAZY_SYMBOL_POINTERS\n";
8596 else if (section_type == MachO::S_SYMBOL_STUBS)
8597 outs() << " S_SYMBOL_STUBS\n";
8598 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
8599 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
8600 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
8601 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
8602 else if (section_type == MachO::S_COALESCED)
8603 outs() << " S_COALESCED\n";
8604 else if (section_type == MachO::S_INTERPOSING)
8605 outs() << " S_INTERPOSING\n";
8606 else if (section_type == MachO::S_DTRACE_DOF)
8607 outs() << " S_DTRACE_DOF\n";
8608 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
8609 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
8610 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
8611 outs() << " S_THREAD_LOCAL_REGULAR\n";
8612 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
8613 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
8614 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
8615 outs() << " S_THREAD_LOCAL_VARIABLES\n";
8616 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
8617 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
8618 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
8619 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
8620 else
8621 outs() << format("0x%08" PRIx32, section_type) << "\n";
8622 outs() << "attributes";
8623 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
8624 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
8625 outs() << " PURE_INSTRUCTIONS";
8626 if (section_attributes & MachO::S_ATTR_NO_TOC)
8627 outs() << " NO_TOC";
8628 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
8629 outs() << " STRIP_STATIC_SYMS";
8630 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
8631 outs() << " NO_DEAD_STRIP";
8632 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
8633 outs() << " LIVE_SUPPORT";
8634 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
8635 outs() << " SELF_MODIFYING_CODE";
8636 if (section_attributes & MachO::S_ATTR_DEBUG)
8637 outs() << " DEBUG";
8638 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
8639 outs() << " SOME_INSTRUCTIONS";
8640 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
8641 outs() << " EXT_RELOC";
8642 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
8643 outs() << " LOC_RELOC";
8644 if (section_attributes == 0)
8645 outs() << " (none)";
8646 outs() << "\n";
8647 } else
8648 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
8649 outs() << " reserved1 " << reserved1;
8650 if (section_type == MachO::S_SYMBOL_STUBS ||
8651 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
8652 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
8653 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
8654 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
8655 outs() << " (index into indirect symbol table)\n";
8656 else
8657 outs() << "\n";
8658 outs() << " reserved2 " << reserved2;
8659 if (section_type == MachO::S_SYMBOL_STUBS)
8660 outs() << " (size of stubs)\n";
8661 else
8662 outs() << "\n";
8665 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
8666 uint32_t object_size) {
8667 outs() << " cmd LC_SYMTAB\n";
8668 outs() << " cmdsize " << st.cmdsize;
8669 if (st.cmdsize != sizeof(struct MachO::symtab_command))
8670 outs() << " Incorrect size\n";
8671 else
8672 outs() << "\n";
8673 outs() << " symoff " << st.symoff;
8674 if (st.symoff > object_size)
8675 outs() << " (past end of file)\n";
8676 else
8677 outs() << "\n";
8678 outs() << " nsyms " << st.nsyms;
8679 uint64_t big_size;
8680 if (Is64Bit) {
8681 big_size = st.nsyms;
8682 big_size *= sizeof(struct MachO::nlist_64);
8683 big_size += st.symoff;
8684 if (big_size > object_size)
8685 outs() << " (past end of file)\n";
8686 else
8687 outs() << "\n";
8688 } else {
8689 big_size = st.nsyms;
8690 big_size *= sizeof(struct MachO::nlist);
8691 big_size += st.symoff;
8692 if (big_size > object_size)
8693 outs() << " (past end of file)\n";
8694 else
8695 outs() << "\n";
8697 outs() << " stroff " << st.stroff;
8698 if (st.stroff > object_size)
8699 outs() << " (past end of file)\n";
8700 else
8701 outs() << "\n";
8702 outs() << " strsize " << st.strsize;
8703 big_size = st.stroff;
8704 big_size += st.strsize;
8705 if (big_size > object_size)
8706 outs() << " (past end of file)\n";
8707 else
8708 outs() << "\n";
8711 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
8712 uint32_t nsyms, uint32_t object_size,
8713 bool Is64Bit) {
8714 outs() << " cmd LC_DYSYMTAB\n";
8715 outs() << " cmdsize " << dyst.cmdsize;
8716 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
8717 outs() << " Incorrect size\n";
8718 else
8719 outs() << "\n";
8720 outs() << " ilocalsym " << dyst.ilocalsym;
8721 if (dyst.ilocalsym > nsyms)
8722 outs() << " (greater than the number of symbols)\n";
8723 else
8724 outs() << "\n";
8725 outs() << " nlocalsym " << dyst.nlocalsym;
8726 uint64_t big_size;
8727 big_size = dyst.ilocalsym;
8728 big_size += dyst.nlocalsym;
8729 if (big_size > nsyms)
8730 outs() << " (past the end of the symbol table)\n";
8731 else
8732 outs() << "\n";
8733 outs() << " iextdefsym " << dyst.iextdefsym;
8734 if (dyst.iextdefsym > nsyms)
8735 outs() << " (greater than the number of symbols)\n";
8736 else
8737 outs() << "\n";
8738 outs() << " nextdefsym " << dyst.nextdefsym;
8739 big_size = dyst.iextdefsym;
8740 big_size += dyst.nextdefsym;
8741 if (big_size > nsyms)
8742 outs() << " (past the end of the symbol table)\n";
8743 else
8744 outs() << "\n";
8745 outs() << " iundefsym " << dyst.iundefsym;
8746 if (dyst.iundefsym > nsyms)
8747 outs() << " (greater than the number of symbols)\n";
8748 else
8749 outs() << "\n";
8750 outs() << " nundefsym " << dyst.nundefsym;
8751 big_size = dyst.iundefsym;
8752 big_size += dyst.nundefsym;
8753 if (big_size > nsyms)
8754 outs() << " (past the end of the symbol table)\n";
8755 else
8756 outs() << "\n";
8757 outs() << " tocoff " << dyst.tocoff;
8758 if (dyst.tocoff > object_size)
8759 outs() << " (past end of file)\n";
8760 else
8761 outs() << "\n";
8762 outs() << " ntoc " << dyst.ntoc;
8763 big_size = dyst.ntoc;
8764 big_size *= sizeof(struct MachO::dylib_table_of_contents);
8765 big_size += dyst.tocoff;
8766 if (big_size > object_size)
8767 outs() << " (past end of file)\n";
8768 else
8769 outs() << "\n";
8770 outs() << " modtaboff " << dyst.modtaboff;
8771 if (dyst.modtaboff > object_size)
8772 outs() << " (past end of file)\n";
8773 else
8774 outs() << "\n";
8775 outs() << " nmodtab " << dyst.nmodtab;
8776 uint64_t modtabend;
8777 if (Is64Bit) {
8778 modtabend = dyst.nmodtab;
8779 modtabend *= sizeof(struct MachO::dylib_module_64);
8780 modtabend += dyst.modtaboff;
8781 } else {
8782 modtabend = dyst.nmodtab;
8783 modtabend *= sizeof(struct MachO::dylib_module);
8784 modtabend += dyst.modtaboff;
8786 if (modtabend > object_size)
8787 outs() << " (past end of file)\n";
8788 else
8789 outs() << "\n";
8790 outs() << " extrefsymoff " << dyst.extrefsymoff;
8791 if (dyst.extrefsymoff > object_size)
8792 outs() << " (past end of file)\n";
8793 else
8794 outs() << "\n";
8795 outs() << " nextrefsyms " << dyst.nextrefsyms;
8796 big_size = dyst.nextrefsyms;
8797 big_size *= sizeof(struct MachO::dylib_reference);
8798 big_size += dyst.extrefsymoff;
8799 if (big_size > object_size)
8800 outs() << " (past end of file)\n";
8801 else
8802 outs() << "\n";
8803 outs() << " indirectsymoff " << dyst.indirectsymoff;
8804 if (dyst.indirectsymoff > object_size)
8805 outs() << " (past end of file)\n";
8806 else
8807 outs() << "\n";
8808 outs() << " nindirectsyms " << dyst.nindirectsyms;
8809 big_size = dyst.nindirectsyms;
8810 big_size *= sizeof(uint32_t);
8811 big_size += dyst.indirectsymoff;
8812 if (big_size > object_size)
8813 outs() << " (past end of file)\n";
8814 else
8815 outs() << "\n";
8816 outs() << " extreloff " << dyst.extreloff;
8817 if (dyst.extreloff > object_size)
8818 outs() << " (past end of file)\n";
8819 else
8820 outs() << "\n";
8821 outs() << " nextrel " << dyst.nextrel;
8822 big_size = dyst.nextrel;
8823 big_size *= sizeof(struct MachO::relocation_info);
8824 big_size += dyst.extreloff;
8825 if (big_size > object_size)
8826 outs() << " (past end of file)\n";
8827 else
8828 outs() << "\n";
8829 outs() << " locreloff " << dyst.locreloff;
8830 if (dyst.locreloff > object_size)
8831 outs() << " (past end of file)\n";
8832 else
8833 outs() << "\n";
8834 outs() << " nlocrel " << dyst.nlocrel;
8835 big_size = dyst.nlocrel;
8836 big_size *= sizeof(struct MachO::relocation_info);
8837 big_size += dyst.locreloff;
8838 if (big_size > object_size)
8839 outs() << " (past end of file)\n";
8840 else
8841 outs() << "\n";
8844 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
8845 uint32_t object_size) {
8846 if (dc.cmd == MachO::LC_DYLD_INFO)
8847 outs() << " cmd LC_DYLD_INFO\n";
8848 else
8849 outs() << " cmd LC_DYLD_INFO_ONLY\n";
8850 outs() << " cmdsize " << dc.cmdsize;
8851 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
8852 outs() << " Incorrect size\n";
8853 else
8854 outs() << "\n";
8855 outs() << " rebase_off " << dc.rebase_off;
8856 if (dc.rebase_off > object_size)
8857 outs() << " (past end of file)\n";
8858 else
8859 outs() << "\n";
8860 outs() << " rebase_size " << dc.rebase_size;
8861 uint64_t big_size;
8862 big_size = dc.rebase_off;
8863 big_size += dc.rebase_size;
8864 if (big_size > object_size)
8865 outs() << " (past end of file)\n";
8866 else
8867 outs() << "\n";
8868 outs() << " bind_off " << dc.bind_off;
8869 if (dc.bind_off > object_size)
8870 outs() << " (past end of file)\n";
8871 else
8872 outs() << "\n";
8873 outs() << " bind_size " << dc.bind_size;
8874 big_size = dc.bind_off;
8875 big_size += dc.bind_size;
8876 if (big_size > object_size)
8877 outs() << " (past end of file)\n";
8878 else
8879 outs() << "\n";
8880 outs() << " weak_bind_off " << dc.weak_bind_off;
8881 if (dc.weak_bind_off > object_size)
8882 outs() << " (past end of file)\n";
8883 else
8884 outs() << "\n";
8885 outs() << " weak_bind_size " << dc.weak_bind_size;
8886 big_size = dc.weak_bind_off;
8887 big_size += dc.weak_bind_size;
8888 if (big_size > object_size)
8889 outs() << " (past end of file)\n";
8890 else
8891 outs() << "\n";
8892 outs() << " lazy_bind_off " << dc.lazy_bind_off;
8893 if (dc.lazy_bind_off > object_size)
8894 outs() << " (past end of file)\n";
8895 else
8896 outs() << "\n";
8897 outs() << " lazy_bind_size " << dc.lazy_bind_size;
8898 big_size = dc.lazy_bind_off;
8899 big_size += dc.lazy_bind_size;
8900 if (big_size > object_size)
8901 outs() << " (past end of file)\n";
8902 else
8903 outs() << "\n";
8904 outs() << " export_off " << dc.export_off;
8905 if (dc.export_off > object_size)
8906 outs() << " (past end of file)\n";
8907 else
8908 outs() << "\n";
8909 outs() << " export_size " << dc.export_size;
8910 big_size = dc.export_off;
8911 big_size += dc.export_size;
8912 if (big_size > object_size)
8913 outs() << " (past end of file)\n";
8914 else
8915 outs() << "\n";
8918 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
8919 const char *Ptr) {
8920 if (dyld.cmd == MachO::LC_ID_DYLINKER)
8921 outs() << " cmd LC_ID_DYLINKER\n";
8922 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
8923 outs() << " cmd LC_LOAD_DYLINKER\n";
8924 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
8925 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
8926 else
8927 outs() << " cmd ?(" << dyld.cmd << ")\n";
8928 outs() << " cmdsize " << dyld.cmdsize;
8929 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
8930 outs() << " Incorrect size\n";
8931 else
8932 outs() << "\n";
8933 if (dyld.name >= dyld.cmdsize)
8934 outs() << " name ?(bad offset " << dyld.name << ")\n";
8935 else {
8936 const char *P = (const char *)(Ptr) + dyld.name;
8937 outs() << " name " << P << " (offset " << dyld.name << ")\n";
8941 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
8942 outs() << " cmd LC_UUID\n";
8943 outs() << " cmdsize " << uuid.cmdsize;
8944 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
8945 outs() << " Incorrect size\n";
8946 else
8947 outs() << "\n";
8948 outs() << " uuid ";
8949 for (int i = 0; i < 16; ++i) {
8950 outs() << format("%02" PRIX32, uuid.uuid[i]);
8951 if (i == 3 || i == 5 || i == 7 || i == 9)
8952 outs() << "-";
8954 outs() << "\n";
8957 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
8958 outs() << " cmd LC_RPATH\n";
8959 outs() << " cmdsize " << rpath.cmdsize;
8960 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
8961 outs() << " Incorrect size\n";
8962 else
8963 outs() << "\n";
8964 if (rpath.path >= rpath.cmdsize)
8965 outs() << " path ?(bad offset " << rpath.path << ")\n";
8966 else {
8967 const char *P = (const char *)(Ptr) + rpath.path;
8968 outs() << " path " << P << " (offset " << rpath.path << ")\n";
8972 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
8973 StringRef LoadCmdName;
8974 switch (vd.cmd) {
8975 case MachO::LC_VERSION_MIN_MACOSX:
8976 LoadCmdName = "LC_VERSION_MIN_MACOSX";
8977 break;
8978 case MachO::LC_VERSION_MIN_IPHONEOS:
8979 LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
8980 break;
8981 case MachO::LC_VERSION_MIN_TVOS:
8982 LoadCmdName = "LC_VERSION_MIN_TVOS";
8983 break;
8984 case MachO::LC_VERSION_MIN_WATCHOS:
8985 LoadCmdName = "LC_VERSION_MIN_WATCHOS";
8986 break;
8987 default:
8988 llvm_unreachable("Unknown version min load command");
8991 outs() << " cmd " << LoadCmdName << '\n';
8992 outs() << " cmdsize " << vd.cmdsize;
8993 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
8994 outs() << " Incorrect size\n";
8995 else
8996 outs() << "\n";
8997 outs() << " version "
8998 << MachOObjectFile::getVersionMinMajor(vd, false) << "."
8999 << MachOObjectFile::getVersionMinMinor(vd, false);
9000 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
9001 if (Update != 0)
9002 outs() << "." << Update;
9003 outs() << "\n";
9004 if (vd.sdk == 0)
9005 outs() << " sdk n/a";
9006 else {
9007 outs() << " sdk "
9008 << MachOObjectFile::getVersionMinMajor(vd, true) << "."
9009 << MachOObjectFile::getVersionMinMinor(vd, true);
9011 Update = MachOObjectFile::getVersionMinUpdate(vd, true);
9012 if (Update != 0)
9013 outs() << "." << Update;
9014 outs() << "\n";
9017 static void PrintNoteLoadCommand(MachO::note_command Nt) {
9018 outs() << " cmd LC_NOTE\n";
9019 outs() << " cmdsize " << Nt.cmdsize;
9020 if (Nt.cmdsize != sizeof(struct MachO::note_command))
9021 outs() << " Incorrect size\n";
9022 else
9023 outs() << "\n";
9024 const char *d = Nt.data_owner;
9025 outs() << "data_owner " << format("%.16s\n", d);
9026 outs() << " offset " << Nt.offset << "\n";
9027 outs() << " size " << Nt.size << "\n";
9030 static void PrintBuildToolVersion(MachO::build_tool_version bv) {
9031 outs() << " tool " << MachOObjectFile::getBuildTool(bv.tool) << "\n";
9032 outs() << " version " << MachOObjectFile::getVersionString(bv.version)
9033 << "\n";
9036 static void PrintBuildVersionLoadCommand(const MachOObjectFile *obj,
9037 MachO::build_version_command bd) {
9038 outs() << " cmd LC_BUILD_VERSION\n";
9039 outs() << " cmdsize " << bd.cmdsize;
9040 if (bd.cmdsize !=
9041 sizeof(struct MachO::build_version_command) +
9042 bd.ntools * sizeof(struct MachO::build_tool_version))
9043 outs() << " Incorrect size\n";
9044 else
9045 outs() << "\n";
9046 outs() << " platform " << MachOObjectFile::getBuildPlatform(bd.platform)
9047 << "\n";
9048 if (bd.sdk)
9049 outs() << " sdk " << MachOObjectFile::getVersionString(bd.sdk)
9050 << "\n";
9051 else
9052 outs() << " sdk n/a\n";
9053 outs() << " minos " << MachOObjectFile::getVersionString(bd.minos)
9054 << "\n";
9055 outs() << " ntools " << bd.ntools << "\n";
9056 for (unsigned i = 0; i < bd.ntools; ++i) {
9057 MachO::build_tool_version bv = obj->getBuildToolVersion(i);
9058 PrintBuildToolVersion(bv);
9062 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
9063 outs() << " cmd LC_SOURCE_VERSION\n";
9064 outs() << " cmdsize " << sd.cmdsize;
9065 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
9066 outs() << " Incorrect size\n";
9067 else
9068 outs() << "\n";
9069 uint64_t a = (sd.version >> 40) & 0xffffff;
9070 uint64_t b = (sd.version >> 30) & 0x3ff;
9071 uint64_t c = (sd.version >> 20) & 0x3ff;
9072 uint64_t d = (sd.version >> 10) & 0x3ff;
9073 uint64_t e = sd.version & 0x3ff;
9074 outs() << " version " << a << "." << b;
9075 if (e != 0)
9076 outs() << "." << c << "." << d << "." << e;
9077 else if (d != 0)
9078 outs() << "." << c << "." << d;
9079 else if (c != 0)
9080 outs() << "." << c;
9081 outs() << "\n";
9084 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
9085 outs() << " cmd LC_MAIN\n";
9086 outs() << " cmdsize " << ep.cmdsize;
9087 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
9088 outs() << " Incorrect size\n";
9089 else
9090 outs() << "\n";
9091 outs() << " entryoff " << ep.entryoff << "\n";
9092 outs() << " stacksize " << ep.stacksize << "\n";
9095 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
9096 uint32_t object_size) {
9097 outs() << " cmd LC_ENCRYPTION_INFO\n";
9098 outs() << " cmdsize " << ec.cmdsize;
9099 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
9100 outs() << " Incorrect size\n";
9101 else
9102 outs() << "\n";
9103 outs() << " cryptoff " << ec.cryptoff;
9104 if (ec.cryptoff > object_size)
9105 outs() << " (past end of file)\n";
9106 else
9107 outs() << "\n";
9108 outs() << " cryptsize " << ec.cryptsize;
9109 if (ec.cryptsize > object_size)
9110 outs() << " (past end of file)\n";
9111 else
9112 outs() << "\n";
9113 outs() << " cryptid " << ec.cryptid << "\n";
9116 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
9117 uint32_t object_size) {
9118 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
9119 outs() << " cmdsize " << ec.cmdsize;
9120 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
9121 outs() << " Incorrect size\n";
9122 else
9123 outs() << "\n";
9124 outs() << " cryptoff " << ec.cryptoff;
9125 if (ec.cryptoff > object_size)
9126 outs() << " (past end of file)\n";
9127 else
9128 outs() << "\n";
9129 outs() << " cryptsize " << ec.cryptsize;
9130 if (ec.cryptsize > object_size)
9131 outs() << " (past end of file)\n";
9132 else
9133 outs() << "\n";
9134 outs() << " cryptid " << ec.cryptid << "\n";
9135 outs() << " pad " << ec.pad << "\n";
9138 static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
9139 const char *Ptr) {
9140 outs() << " cmd LC_LINKER_OPTION\n";
9141 outs() << " cmdsize " << lo.cmdsize;
9142 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
9143 outs() << " Incorrect size\n";
9144 else
9145 outs() << "\n";
9146 outs() << " count " << lo.count << "\n";
9147 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
9148 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
9149 uint32_t i = 0;
9150 while (left > 0) {
9151 while (*string == '\0' && left > 0) {
9152 string++;
9153 left--;
9155 if (left > 0) {
9156 i++;
9157 outs() << " string #" << i << " " << format("%.*s\n", left, string);
9158 uint32_t NullPos = StringRef(string, left).find('\0');
9159 uint32_t len = std::min(NullPos, left) + 1;
9160 string += len;
9161 left -= len;
9164 if (lo.count != i)
9165 outs() << " count " << lo.count << " does not match number of strings "
9166 << i << "\n";
9169 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
9170 const char *Ptr) {
9171 outs() << " cmd LC_SUB_FRAMEWORK\n";
9172 outs() << " cmdsize " << sub.cmdsize;
9173 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
9174 outs() << " Incorrect size\n";
9175 else
9176 outs() << "\n";
9177 if (sub.umbrella < sub.cmdsize) {
9178 const char *P = Ptr + sub.umbrella;
9179 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
9180 } else {
9181 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
9185 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
9186 const char *Ptr) {
9187 outs() << " cmd LC_SUB_UMBRELLA\n";
9188 outs() << " cmdsize " << sub.cmdsize;
9189 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
9190 outs() << " Incorrect size\n";
9191 else
9192 outs() << "\n";
9193 if (sub.sub_umbrella < sub.cmdsize) {
9194 const char *P = Ptr + sub.sub_umbrella;
9195 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
9196 } else {
9197 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
9201 static void PrintSubLibraryCommand(MachO::sub_library_command sub,
9202 const char *Ptr) {
9203 outs() << " cmd LC_SUB_LIBRARY\n";
9204 outs() << " cmdsize " << sub.cmdsize;
9205 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
9206 outs() << " Incorrect size\n";
9207 else
9208 outs() << "\n";
9209 if (sub.sub_library < sub.cmdsize) {
9210 const char *P = Ptr + sub.sub_library;
9211 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
9212 } else {
9213 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
9217 static void PrintSubClientCommand(MachO::sub_client_command sub,
9218 const char *Ptr) {
9219 outs() << " cmd LC_SUB_CLIENT\n";
9220 outs() << " cmdsize " << sub.cmdsize;
9221 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
9222 outs() << " Incorrect size\n";
9223 else
9224 outs() << "\n";
9225 if (sub.client < sub.cmdsize) {
9226 const char *P = Ptr + sub.client;
9227 outs() << " client " << P << " (offset " << sub.client << ")\n";
9228 } else {
9229 outs() << " client ?(bad offset " << sub.client << ")\n";
9233 static void PrintRoutinesCommand(MachO::routines_command r) {
9234 outs() << " cmd LC_ROUTINES\n";
9235 outs() << " cmdsize " << r.cmdsize;
9236 if (r.cmdsize != sizeof(struct MachO::routines_command))
9237 outs() << " Incorrect size\n";
9238 else
9239 outs() << "\n";
9240 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
9241 outs() << " init_module " << r.init_module << "\n";
9242 outs() << " reserved1 " << r.reserved1 << "\n";
9243 outs() << " reserved2 " << r.reserved2 << "\n";
9244 outs() << " reserved3 " << r.reserved3 << "\n";
9245 outs() << " reserved4 " << r.reserved4 << "\n";
9246 outs() << " reserved5 " << r.reserved5 << "\n";
9247 outs() << " reserved6 " << r.reserved6 << "\n";
9250 static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
9251 outs() << " cmd LC_ROUTINES_64\n";
9252 outs() << " cmdsize " << r.cmdsize;
9253 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
9254 outs() << " Incorrect size\n";
9255 else
9256 outs() << "\n";
9257 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
9258 outs() << " init_module " << r.init_module << "\n";
9259 outs() << " reserved1 " << r.reserved1 << "\n";
9260 outs() << " reserved2 " << r.reserved2 << "\n";
9261 outs() << " reserved3 " << r.reserved3 << "\n";
9262 outs() << " reserved4 " << r.reserved4 << "\n";
9263 outs() << " reserved5 " << r.reserved5 << "\n";
9264 outs() << " reserved6 " << r.reserved6 << "\n";
9267 static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t &cpu32) {
9268 outs() << "\t eax " << format("0x%08" PRIx32, cpu32.eax);
9269 outs() << " ebx " << format("0x%08" PRIx32, cpu32.ebx);
9270 outs() << " ecx " << format("0x%08" PRIx32, cpu32.ecx);
9271 outs() << " edx " << format("0x%08" PRIx32, cpu32.edx) << "\n";
9272 outs() << "\t edi " << format("0x%08" PRIx32, cpu32.edi);
9273 outs() << " esi " << format("0x%08" PRIx32, cpu32.esi);
9274 outs() << " ebp " << format("0x%08" PRIx32, cpu32.ebp);
9275 outs() << " esp " << format("0x%08" PRIx32, cpu32.esp) << "\n";
9276 outs() << "\t ss " << format("0x%08" PRIx32, cpu32.ss);
9277 outs() << " eflags " << format("0x%08" PRIx32, cpu32.eflags);
9278 outs() << " eip " << format("0x%08" PRIx32, cpu32.eip);
9279 outs() << " cs " << format("0x%08" PRIx32, cpu32.cs) << "\n";
9280 outs() << "\t ds " << format("0x%08" PRIx32, cpu32.ds);
9281 outs() << " es " << format("0x%08" PRIx32, cpu32.es);
9282 outs() << " fs " << format("0x%08" PRIx32, cpu32.fs);
9283 outs() << " gs " << format("0x%08" PRIx32, cpu32.gs) << "\n";
9286 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
9287 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
9288 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
9289 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
9290 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
9291 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
9292 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
9293 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
9294 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
9295 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
9296 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
9297 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
9298 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
9299 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
9300 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
9301 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
9302 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
9303 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
9304 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
9305 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
9306 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
9307 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
9310 static void Print_mmst_reg(MachO::mmst_reg_t &r) {
9311 uint32_t f;
9312 outs() << "\t mmst_reg ";
9313 for (f = 0; f < 10; f++)
9314 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
9315 outs() << "\n";
9316 outs() << "\t mmst_rsrv ";
9317 for (f = 0; f < 6; f++)
9318 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
9319 outs() << "\n";
9322 static void Print_xmm_reg(MachO::xmm_reg_t &r) {
9323 uint32_t f;
9324 outs() << "\t xmm_reg ";
9325 for (f = 0; f < 16; f++)
9326 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
9327 outs() << "\n";
9330 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
9331 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
9332 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
9333 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
9334 outs() << " denorm " << fpu.fpu_fcw.denorm;
9335 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
9336 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
9337 outs() << " undfl " << fpu.fpu_fcw.undfl;
9338 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
9339 outs() << "\t\t pc ";
9340 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
9341 outs() << "FP_PREC_24B ";
9342 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
9343 outs() << "FP_PREC_53B ";
9344 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
9345 outs() << "FP_PREC_64B ";
9346 else
9347 outs() << fpu.fpu_fcw.pc << " ";
9348 outs() << "rc ";
9349 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
9350 outs() << "FP_RND_NEAR ";
9351 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
9352 outs() << "FP_RND_DOWN ";
9353 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
9354 outs() << "FP_RND_UP ";
9355 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
9356 outs() << "FP_CHOP ";
9357 outs() << "\n";
9358 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
9359 outs() << " denorm " << fpu.fpu_fsw.denorm;
9360 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
9361 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
9362 outs() << " undfl " << fpu.fpu_fsw.undfl;
9363 outs() << " precis " << fpu.fpu_fsw.precis;
9364 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
9365 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
9366 outs() << " c0 " << fpu.fpu_fsw.c0;
9367 outs() << " c1 " << fpu.fpu_fsw.c1;
9368 outs() << " c2 " << fpu.fpu_fsw.c2;
9369 outs() << " tos " << fpu.fpu_fsw.tos;
9370 outs() << " c3 " << fpu.fpu_fsw.c3;
9371 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
9372 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
9373 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
9374 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
9375 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
9376 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
9377 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
9378 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
9379 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
9380 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
9381 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
9382 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
9383 outs() << "\n";
9384 outs() << "\t fpu_stmm0:\n";
9385 Print_mmst_reg(fpu.fpu_stmm0);
9386 outs() << "\t fpu_stmm1:\n";
9387 Print_mmst_reg(fpu.fpu_stmm1);
9388 outs() << "\t fpu_stmm2:\n";
9389 Print_mmst_reg(fpu.fpu_stmm2);
9390 outs() << "\t fpu_stmm3:\n";
9391 Print_mmst_reg(fpu.fpu_stmm3);
9392 outs() << "\t fpu_stmm4:\n";
9393 Print_mmst_reg(fpu.fpu_stmm4);
9394 outs() << "\t fpu_stmm5:\n";
9395 Print_mmst_reg(fpu.fpu_stmm5);
9396 outs() << "\t fpu_stmm6:\n";
9397 Print_mmst_reg(fpu.fpu_stmm6);
9398 outs() << "\t fpu_stmm7:\n";
9399 Print_mmst_reg(fpu.fpu_stmm7);
9400 outs() << "\t fpu_xmm0:\n";
9401 Print_xmm_reg(fpu.fpu_xmm0);
9402 outs() << "\t fpu_xmm1:\n";
9403 Print_xmm_reg(fpu.fpu_xmm1);
9404 outs() << "\t fpu_xmm2:\n";
9405 Print_xmm_reg(fpu.fpu_xmm2);
9406 outs() << "\t fpu_xmm3:\n";
9407 Print_xmm_reg(fpu.fpu_xmm3);
9408 outs() << "\t fpu_xmm4:\n";
9409 Print_xmm_reg(fpu.fpu_xmm4);
9410 outs() << "\t fpu_xmm5:\n";
9411 Print_xmm_reg(fpu.fpu_xmm5);
9412 outs() << "\t fpu_xmm6:\n";
9413 Print_xmm_reg(fpu.fpu_xmm6);
9414 outs() << "\t fpu_xmm7:\n";
9415 Print_xmm_reg(fpu.fpu_xmm7);
9416 outs() << "\t fpu_xmm8:\n";
9417 Print_xmm_reg(fpu.fpu_xmm8);
9418 outs() << "\t fpu_xmm9:\n";
9419 Print_xmm_reg(fpu.fpu_xmm9);
9420 outs() << "\t fpu_xmm10:\n";
9421 Print_xmm_reg(fpu.fpu_xmm10);
9422 outs() << "\t fpu_xmm11:\n";
9423 Print_xmm_reg(fpu.fpu_xmm11);
9424 outs() << "\t fpu_xmm12:\n";
9425 Print_xmm_reg(fpu.fpu_xmm12);
9426 outs() << "\t fpu_xmm13:\n";
9427 Print_xmm_reg(fpu.fpu_xmm13);
9428 outs() << "\t fpu_xmm14:\n";
9429 Print_xmm_reg(fpu.fpu_xmm14);
9430 outs() << "\t fpu_xmm15:\n";
9431 Print_xmm_reg(fpu.fpu_xmm15);
9432 outs() << "\t fpu_rsrv4:\n";
9433 for (uint32_t f = 0; f < 6; f++) {
9434 outs() << "\t ";
9435 for (uint32_t g = 0; g < 16; g++)
9436 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
9437 outs() << "\n";
9439 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
9440 outs() << "\n";
9443 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
9444 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
9445 outs() << " err " << format("0x%08" PRIx32, exc64.err);
9446 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
9449 static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) {
9450 outs() << "\t r0 " << format("0x%08" PRIx32, cpu32.r[0]);
9451 outs() << " r1 " << format("0x%08" PRIx32, cpu32.r[1]);
9452 outs() << " r2 " << format("0x%08" PRIx32, cpu32.r[2]);
9453 outs() << " r3 " << format("0x%08" PRIx32, cpu32.r[3]) << "\n";
9454 outs() << "\t r4 " << format("0x%08" PRIx32, cpu32.r[4]);
9455 outs() << " r5 " << format("0x%08" PRIx32, cpu32.r[5]);
9456 outs() << " r6 " << format("0x%08" PRIx32, cpu32.r[6]);
9457 outs() << " r7 " << format("0x%08" PRIx32, cpu32.r[7]) << "\n";
9458 outs() << "\t r8 " << format("0x%08" PRIx32, cpu32.r[8]);
9459 outs() << " r9 " << format("0x%08" PRIx32, cpu32.r[9]);
9460 outs() << " r10 " << format("0x%08" PRIx32, cpu32.r[10]);
9461 outs() << " r11 " << format("0x%08" PRIx32, cpu32.r[11]) << "\n";
9462 outs() << "\t r12 " << format("0x%08" PRIx32, cpu32.r[12]);
9463 outs() << " sp " << format("0x%08" PRIx32, cpu32.sp);
9464 outs() << " lr " << format("0x%08" PRIx32, cpu32.lr);
9465 outs() << " pc " << format("0x%08" PRIx32, cpu32.pc) << "\n";
9466 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n";
9469 static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) {
9470 outs() << "\t x0 " << format("0x%016" PRIx64, cpu64.x[0]);
9471 outs() << " x1 " << format("0x%016" PRIx64, cpu64.x[1]);
9472 outs() << " x2 " << format("0x%016" PRIx64, cpu64.x[2]) << "\n";
9473 outs() << "\t x3 " << format("0x%016" PRIx64, cpu64.x[3]);
9474 outs() << " x4 " << format("0x%016" PRIx64, cpu64.x[4]);
9475 outs() << " x5 " << format("0x%016" PRIx64, cpu64.x[5]) << "\n";
9476 outs() << "\t x6 " << format("0x%016" PRIx64, cpu64.x[6]);
9477 outs() << " x7 " << format("0x%016" PRIx64, cpu64.x[7]);
9478 outs() << " x8 " << format("0x%016" PRIx64, cpu64.x[8]) << "\n";
9479 outs() << "\t x9 " << format("0x%016" PRIx64, cpu64.x[9]);
9480 outs() << " x10 " << format("0x%016" PRIx64, cpu64.x[10]);
9481 outs() << " x11 " << format("0x%016" PRIx64, cpu64.x[11]) << "\n";
9482 outs() << "\t x12 " << format("0x%016" PRIx64, cpu64.x[12]);
9483 outs() << " x13 " << format("0x%016" PRIx64, cpu64.x[13]);
9484 outs() << " x14 " << format("0x%016" PRIx64, cpu64.x[14]) << "\n";
9485 outs() << "\t x15 " << format("0x%016" PRIx64, cpu64.x[15]);
9486 outs() << " x16 " << format("0x%016" PRIx64, cpu64.x[16]);
9487 outs() << " x17 " << format("0x%016" PRIx64, cpu64.x[17]) << "\n";
9488 outs() << "\t x18 " << format("0x%016" PRIx64, cpu64.x[18]);
9489 outs() << " x19 " << format("0x%016" PRIx64, cpu64.x[19]);
9490 outs() << " x20 " << format("0x%016" PRIx64, cpu64.x[20]) << "\n";
9491 outs() << "\t x21 " << format("0x%016" PRIx64, cpu64.x[21]);
9492 outs() << " x22 " << format("0x%016" PRIx64, cpu64.x[22]);
9493 outs() << " x23 " << format("0x%016" PRIx64, cpu64.x[23]) << "\n";
9494 outs() << "\t x24 " << format("0x%016" PRIx64, cpu64.x[24]);
9495 outs() << " x25 " << format("0x%016" PRIx64, cpu64.x[25]);
9496 outs() << " x26 " << format("0x%016" PRIx64, cpu64.x[26]) << "\n";
9497 outs() << "\t x27 " << format("0x%016" PRIx64, cpu64.x[27]);
9498 outs() << " x28 " << format("0x%016" PRIx64, cpu64.x[28]);
9499 outs() << " fp " << format("0x%016" PRIx64, cpu64.fp) << "\n";
9500 outs() << "\t lr " << format("0x%016" PRIx64, cpu64.lr);
9501 outs() << " sp " << format("0x%016" PRIx64, cpu64.sp);
9502 outs() << " pc " << format("0x%016" PRIx64, cpu64.pc) << "\n";
9503 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu64.cpsr) << "\n";
9506 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
9507 bool isLittleEndian, uint32_t cputype) {
9508 if (t.cmd == MachO::LC_THREAD)
9509 outs() << " cmd LC_THREAD\n";
9510 else if (t.cmd == MachO::LC_UNIXTHREAD)
9511 outs() << " cmd LC_UNIXTHREAD\n";
9512 else
9513 outs() << " cmd " << t.cmd << " (unknown)\n";
9514 outs() << " cmdsize " << t.cmdsize;
9515 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
9516 outs() << " Incorrect size\n";
9517 else
9518 outs() << "\n";
9520 const char *begin = Ptr + sizeof(struct MachO::thread_command);
9521 const char *end = Ptr + t.cmdsize;
9522 uint32_t flavor, count, left;
9523 if (cputype == MachO::CPU_TYPE_I386) {
9524 while (begin < end) {
9525 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9526 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9527 begin += sizeof(uint32_t);
9528 } else {
9529 flavor = 0;
9530 begin = end;
9532 if (isLittleEndian != sys::IsLittleEndianHost)
9533 sys::swapByteOrder(flavor);
9534 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9535 memcpy((char *)&count, begin, sizeof(uint32_t));
9536 begin += sizeof(uint32_t);
9537 } else {
9538 count = 0;
9539 begin = end;
9541 if (isLittleEndian != sys::IsLittleEndianHost)
9542 sys::swapByteOrder(count);
9543 if (flavor == MachO::x86_THREAD_STATE32) {
9544 outs() << " flavor i386_THREAD_STATE\n";
9545 if (count == MachO::x86_THREAD_STATE32_COUNT)
9546 outs() << " count i386_THREAD_STATE_COUNT\n";
9547 else
9548 outs() << " count " << count
9549 << " (not x86_THREAD_STATE32_COUNT)\n";
9550 MachO::x86_thread_state32_t cpu32;
9551 left = end - begin;
9552 if (left >= sizeof(MachO::x86_thread_state32_t)) {
9553 memcpy(&cpu32, begin, sizeof(MachO::x86_thread_state32_t));
9554 begin += sizeof(MachO::x86_thread_state32_t);
9555 } else {
9556 memset(&cpu32, '\0', sizeof(MachO::x86_thread_state32_t));
9557 memcpy(&cpu32, begin, left);
9558 begin += left;
9560 if (isLittleEndian != sys::IsLittleEndianHost)
9561 swapStruct(cpu32);
9562 Print_x86_thread_state32_t(cpu32);
9563 } else if (flavor == MachO::x86_THREAD_STATE) {
9564 outs() << " flavor x86_THREAD_STATE\n";
9565 if (count == MachO::x86_THREAD_STATE_COUNT)
9566 outs() << " count x86_THREAD_STATE_COUNT\n";
9567 else
9568 outs() << " count " << count
9569 << " (not x86_THREAD_STATE_COUNT)\n";
9570 struct MachO::x86_thread_state_t ts;
9571 left = end - begin;
9572 if (left >= sizeof(MachO::x86_thread_state_t)) {
9573 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
9574 begin += sizeof(MachO::x86_thread_state_t);
9575 } else {
9576 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
9577 memcpy(&ts, begin, left);
9578 begin += left;
9580 if (isLittleEndian != sys::IsLittleEndianHost)
9581 swapStruct(ts);
9582 if (ts.tsh.flavor == MachO::x86_THREAD_STATE32) {
9583 outs() << "\t tsh.flavor x86_THREAD_STATE32 ";
9584 if (ts.tsh.count == MachO::x86_THREAD_STATE32_COUNT)
9585 outs() << "tsh.count x86_THREAD_STATE32_COUNT\n";
9586 else
9587 outs() << "tsh.count " << ts.tsh.count
9588 << " (not x86_THREAD_STATE32_COUNT\n";
9589 Print_x86_thread_state32_t(ts.uts.ts32);
9590 } else {
9591 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
9592 << ts.tsh.count << "\n";
9594 } else {
9595 outs() << " flavor " << flavor << " (unknown)\n";
9596 outs() << " count " << count << "\n";
9597 outs() << " state (unknown)\n";
9598 begin += count * sizeof(uint32_t);
9601 } else if (cputype == MachO::CPU_TYPE_X86_64) {
9602 while (begin < end) {
9603 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9604 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9605 begin += sizeof(uint32_t);
9606 } else {
9607 flavor = 0;
9608 begin = end;
9610 if (isLittleEndian != sys::IsLittleEndianHost)
9611 sys::swapByteOrder(flavor);
9612 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9613 memcpy((char *)&count, begin, sizeof(uint32_t));
9614 begin += sizeof(uint32_t);
9615 } else {
9616 count = 0;
9617 begin = end;
9619 if (isLittleEndian != sys::IsLittleEndianHost)
9620 sys::swapByteOrder(count);
9621 if (flavor == MachO::x86_THREAD_STATE64) {
9622 outs() << " flavor x86_THREAD_STATE64\n";
9623 if (count == MachO::x86_THREAD_STATE64_COUNT)
9624 outs() << " count x86_THREAD_STATE64_COUNT\n";
9625 else
9626 outs() << " count " << count
9627 << " (not x86_THREAD_STATE64_COUNT)\n";
9628 MachO::x86_thread_state64_t cpu64;
9629 left = end - begin;
9630 if (left >= sizeof(MachO::x86_thread_state64_t)) {
9631 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
9632 begin += sizeof(MachO::x86_thread_state64_t);
9633 } else {
9634 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
9635 memcpy(&cpu64, begin, left);
9636 begin += left;
9638 if (isLittleEndian != sys::IsLittleEndianHost)
9639 swapStruct(cpu64);
9640 Print_x86_thread_state64_t(cpu64);
9641 } else if (flavor == MachO::x86_THREAD_STATE) {
9642 outs() << " flavor x86_THREAD_STATE\n";
9643 if (count == MachO::x86_THREAD_STATE_COUNT)
9644 outs() << " count x86_THREAD_STATE_COUNT\n";
9645 else
9646 outs() << " count " << count
9647 << " (not x86_THREAD_STATE_COUNT)\n";
9648 struct MachO::x86_thread_state_t ts;
9649 left = end - begin;
9650 if (left >= sizeof(MachO::x86_thread_state_t)) {
9651 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
9652 begin += sizeof(MachO::x86_thread_state_t);
9653 } else {
9654 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
9655 memcpy(&ts, begin, left);
9656 begin += left;
9658 if (isLittleEndian != sys::IsLittleEndianHost)
9659 swapStruct(ts);
9660 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
9661 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
9662 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
9663 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
9664 else
9665 outs() << "tsh.count " << ts.tsh.count
9666 << " (not x86_THREAD_STATE64_COUNT\n";
9667 Print_x86_thread_state64_t(ts.uts.ts64);
9668 } else {
9669 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
9670 << ts.tsh.count << "\n";
9672 } else if (flavor == MachO::x86_FLOAT_STATE) {
9673 outs() << " flavor x86_FLOAT_STATE\n";
9674 if (count == MachO::x86_FLOAT_STATE_COUNT)
9675 outs() << " count x86_FLOAT_STATE_COUNT\n";
9676 else
9677 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
9678 struct MachO::x86_float_state_t fs;
9679 left = end - begin;
9680 if (left >= sizeof(MachO::x86_float_state_t)) {
9681 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
9682 begin += sizeof(MachO::x86_float_state_t);
9683 } else {
9684 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
9685 memcpy(&fs, begin, left);
9686 begin += left;
9688 if (isLittleEndian != sys::IsLittleEndianHost)
9689 swapStruct(fs);
9690 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
9691 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
9692 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
9693 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
9694 else
9695 outs() << "fsh.count " << fs.fsh.count
9696 << " (not x86_FLOAT_STATE64_COUNT\n";
9697 Print_x86_float_state_t(fs.ufs.fs64);
9698 } else {
9699 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
9700 << fs.fsh.count << "\n";
9702 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
9703 outs() << " flavor x86_EXCEPTION_STATE\n";
9704 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
9705 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
9706 else
9707 outs() << " count " << count
9708 << " (not x86_EXCEPTION_STATE_COUNT)\n";
9709 struct MachO::x86_exception_state_t es;
9710 left = end - begin;
9711 if (left >= sizeof(MachO::x86_exception_state_t)) {
9712 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
9713 begin += sizeof(MachO::x86_exception_state_t);
9714 } else {
9715 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
9716 memcpy(&es, begin, left);
9717 begin += left;
9719 if (isLittleEndian != sys::IsLittleEndianHost)
9720 swapStruct(es);
9721 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
9722 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
9723 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
9724 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
9725 else
9726 outs() << "\t esh.count " << es.esh.count
9727 << " (not x86_EXCEPTION_STATE64_COUNT\n";
9728 Print_x86_exception_state_t(es.ues.es64);
9729 } else {
9730 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
9731 << es.esh.count << "\n";
9733 } else if (flavor == MachO::x86_EXCEPTION_STATE64) {
9734 outs() << " flavor x86_EXCEPTION_STATE64\n";
9735 if (count == MachO::x86_EXCEPTION_STATE64_COUNT)
9736 outs() << " count x86_EXCEPTION_STATE64_COUNT\n";
9737 else
9738 outs() << " count " << count
9739 << " (not x86_EXCEPTION_STATE64_COUNT)\n";
9740 struct MachO::x86_exception_state64_t es64;
9741 left = end - begin;
9742 if (left >= sizeof(MachO::x86_exception_state64_t)) {
9743 memcpy(&es64, begin, sizeof(MachO::x86_exception_state64_t));
9744 begin += sizeof(MachO::x86_exception_state64_t);
9745 } else {
9746 memset(&es64, '\0', sizeof(MachO::x86_exception_state64_t));
9747 memcpy(&es64, begin, left);
9748 begin += left;
9750 if (isLittleEndian != sys::IsLittleEndianHost)
9751 swapStruct(es64);
9752 Print_x86_exception_state_t(es64);
9753 } else {
9754 outs() << " flavor " << flavor << " (unknown)\n";
9755 outs() << " count " << count << "\n";
9756 outs() << " state (unknown)\n";
9757 begin += count * sizeof(uint32_t);
9760 } else if (cputype == MachO::CPU_TYPE_ARM) {
9761 while (begin < end) {
9762 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9763 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9764 begin += sizeof(uint32_t);
9765 } else {
9766 flavor = 0;
9767 begin = end;
9769 if (isLittleEndian != sys::IsLittleEndianHost)
9770 sys::swapByteOrder(flavor);
9771 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9772 memcpy((char *)&count, begin, sizeof(uint32_t));
9773 begin += sizeof(uint32_t);
9774 } else {
9775 count = 0;
9776 begin = end;
9778 if (isLittleEndian != sys::IsLittleEndianHost)
9779 sys::swapByteOrder(count);
9780 if (flavor == MachO::ARM_THREAD_STATE) {
9781 outs() << " flavor ARM_THREAD_STATE\n";
9782 if (count == MachO::ARM_THREAD_STATE_COUNT)
9783 outs() << " count ARM_THREAD_STATE_COUNT\n";
9784 else
9785 outs() << " count " << count
9786 << " (not ARM_THREAD_STATE_COUNT)\n";
9787 MachO::arm_thread_state32_t cpu32;
9788 left = end - begin;
9789 if (left >= sizeof(MachO::arm_thread_state32_t)) {
9790 memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t));
9791 begin += sizeof(MachO::arm_thread_state32_t);
9792 } else {
9793 memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t));
9794 memcpy(&cpu32, begin, left);
9795 begin += left;
9797 if (isLittleEndian != sys::IsLittleEndianHost)
9798 swapStruct(cpu32);
9799 Print_arm_thread_state32_t(cpu32);
9800 } else {
9801 outs() << " flavor " << flavor << " (unknown)\n";
9802 outs() << " count " << count << "\n";
9803 outs() << " state (unknown)\n";
9804 begin += count * sizeof(uint32_t);
9807 } else if (cputype == MachO::CPU_TYPE_ARM64 ||
9808 cputype == MachO::CPU_TYPE_ARM64_32) {
9809 while (begin < end) {
9810 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9811 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9812 begin += sizeof(uint32_t);
9813 } else {
9814 flavor = 0;
9815 begin = end;
9817 if (isLittleEndian != sys::IsLittleEndianHost)
9818 sys::swapByteOrder(flavor);
9819 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9820 memcpy((char *)&count, begin, sizeof(uint32_t));
9821 begin += sizeof(uint32_t);
9822 } else {
9823 count = 0;
9824 begin = end;
9826 if (isLittleEndian != sys::IsLittleEndianHost)
9827 sys::swapByteOrder(count);
9828 if (flavor == MachO::ARM_THREAD_STATE64) {
9829 outs() << " flavor ARM_THREAD_STATE64\n";
9830 if (count == MachO::ARM_THREAD_STATE64_COUNT)
9831 outs() << " count ARM_THREAD_STATE64_COUNT\n";
9832 else
9833 outs() << " count " << count
9834 << " (not ARM_THREAD_STATE64_COUNT)\n";
9835 MachO::arm_thread_state64_t cpu64;
9836 left = end - begin;
9837 if (left >= sizeof(MachO::arm_thread_state64_t)) {
9838 memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t));
9839 begin += sizeof(MachO::arm_thread_state64_t);
9840 } else {
9841 memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t));
9842 memcpy(&cpu64, begin, left);
9843 begin += left;
9845 if (isLittleEndian != sys::IsLittleEndianHost)
9846 swapStruct(cpu64);
9847 Print_arm_thread_state64_t(cpu64);
9848 } else {
9849 outs() << " flavor " << flavor << " (unknown)\n";
9850 outs() << " count " << count << "\n";
9851 outs() << " state (unknown)\n";
9852 begin += count * sizeof(uint32_t);
9855 } else {
9856 while (begin < end) {
9857 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9858 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9859 begin += sizeof(uint32_t);
9860 } else {
9861 flavor = 0;
9862 begin = end;
9864 if (isLittleEndian != sys::IsLittleEndianHost)
9865 sys::swapByteOrder(flavor);
9866 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9867 memcpy((char *)&count, begin, sizeof(uint32_t));
9868 begin += sizeof(uint32_t);
9869 } else {
9870 count = 0;
9871 begin = end;
9873 if (isLittleEndian != sys::IsLittleEndianHost)
9874 sys::swapByteOrder(count);
9875 outs() << " flavor " << flavor << "\n";
9876 outs() << " count " << count << "\n";
9877 outs() << " state (Unknown cputype/cpusubtype)\n";
9878 begin += count * sizeof(uint32_t);
9883 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
9884 if (dl.cmd == MachO::LC_ID_DYLIB)
9885 outs() << " cmd LC_ID_DYLIB\n";
9886 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
9887 outs() << " cmd LC_LOAD_DYLIB\n";
9888 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
9889 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
9890 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
9891 outs() << " cmd LC_REEXPORT_DYLIB\n";
9892 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
9893 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
9894 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
9895 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
9896 else
9897 outs() << " cmd " << dl.cmd << " (unknown)\n";
9898 outs() << " cmdsize " << dl.cmdsize;
9899 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
9900 outs() << " Incorrect size\n";
9901 else
9902 outs() << "\n";
9903 if (dl.dylib.name < dl.cmdsize) {
9904 const char *P = (const char *)(Ptr) + dl.dylib.name;
9905 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
9906 } else {
9907 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
9909 outs() << " time stamp " << dl.dylib.timestamp << " ";
9910 time_t t = dl.dylib.timestamp;
9911 outs() << ctime(&t);
9912 outs() << " current version ";
9913 if (dl.dylib.current_version == 0xffffffff)
9914 outs() << "n/a\n";
9915 else
9916 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
9917 << ((dl.dylib.current_version >> 8) & 0xff) << "."
9918 << (dl.dylib.current_version & 0xff) << "\n";
9919 outs() << "compatibility version ";
9920 if (dl.dylib.compatibility_version == 0xffffffff)
9921 outs() << "n/a\n";
9922 else
9923 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
9924 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
9925 << (dl.dylib.compatibility_version & 0xff) << "\n";
9928 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
9929 uint32_t object_size) {
9930 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
9931 outs() << " cmd LC_CODE_SIGNATURE\n";
9932 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
9933 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
9934 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
9935 outs() << " cmd LC_FUNCTION_STARTS\n";
9936 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
9937 outs() << " cmd LC_DATA_IN_CODE\n";
9938 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
9939 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
9940 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
9941 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
9942 else
9943 outs() << " cmd " << ld.cmd << " (?)\n";
9944 outs() << " cmdsize " << ld.cmdsize;
9945 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
9946 outs() << " Incorrect size\n";
9947 else
9948 outs() << "\n";
9949 outs() << " dataoff " << ld.dataoff;
9950 if (ld.dataoff > object_size)
9951 outs() << " (past end of file)\n";
9952 else
9953 outs() << "\n";
9954 outs() << " datasize " << ld.datasize;
9955 uint64_t big_size = ld.dataoff;
9956 big_size += ld.datasize;
9957 if (big_size > object_size)
9958 outs() << " (past end of file)\n";
9959 else
9960 outs() << "\n";
9963 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
9964 uint32_t cputype, bool verbose) {
9965 StringRef Buf = Obj->getData();
9966 unsigned Index = 0;
9967 for (const auto &Command : Obj->load_commands()) {
9968 outs() << "Load command " << Index++ << "\n";
9969 if (Command.C.cmd == MachO::LC_SEGMENT) {
9970 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
9971 const char *sg_segname = SLC.segname;
9972 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
9973 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
9974 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
9975 verbose);
9976 for (unsigned j = 0; j < SLC.nsects; j++) {
9977 MachO::section S = Obj->getSection(Command, j);
9978 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
9979 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
9980 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
9982 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
9983 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
9984 const char *sg_segname = SLC_64.segname;
9985 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
9986 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
9987 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
9988 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
9989 for (unsigned j = 0; j < SLC_64.nsects; j++) {
9990 MachO::section_64 S_64 = Obj->getSection64(Command, j);
9991 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
9992 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
9993 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
9994 sg_segname, filetype, Buf.size(), verbose);
9996 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
9997 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
9998 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
9999 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
10000 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
10001 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
10002 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
10003 Obj->is64Bit());
10004 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
10005 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
10006 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
10007 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
10008 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
10009 Command.C.cmd == MachO::LC_ID_DYLINKER ||
10010 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
10011 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
10012 PrintDyldLoadCommand(Dyld, Command.Ptr);
10013 } else if (Command.C.cmd == MachO::LC_UUID) {
10014 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
10015 PrintUuidLoadCommand(Uuid);
10016 } else if (Command.C.cmd == MachO::LC_RPATH) {
10017 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
10018 PrintRpathLoadCommand(Rpath, Command.Ptr);
10019 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
10020 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
10021 Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
10022 Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
10023 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
10024 PrintVersionMinLoadCommand(Vd);
10025 } else if (Command.C.cmd == MachO::LC_NOTE) {
10026 MachO::note_command Nt = Obj->getNoteLoadCommand(Command);
10027 PrintNoteLoadCommand(Nt);
10028 } else if (Command.C.cmd == MachO::LC_BUILD_VERSION) {
10029 MachO::build_version_command Bv =
10030 Obj->getBuildVersionLoadCommand(Command);
10031 PrintBuildVersionLoadCommand(Obj, Bv);
10032 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
10033 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
10034 PrintSourceVersionCommand(Sd);
10035 } else if (Command.C.cmd == MachO::LC_MAIN) {
10036 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
10037 PrintEntryPointCommand(Ep);
10038 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
10039 MachO::encryption_info_command Ei =
10040 Obj->getEncryptionInfoCommand(Command);
10041 PrintEncryptionInfoCommand(Ei, Buf.size());
10042 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
10043 MachO::encryption_info_command_64 Ei =
10044 Obj->getEncryptionInfoCommand64(Command);
10045 PrintEncryptionInfoCommand64(Ei, Buf.size());
10046 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
10047 MachO::linker_option_command Lo =
10048 Obj->getLinkerOptionLoadCommand(Command);
10049 PrintLinkerOptionCommand(Lo, Command.Ptr);
10050 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
10051 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
10052 PrintSubFrameworkCommand(Sf, Command.Ptr);
10053 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
10054 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
10055 PrintSubUmbrellaCommand(Sf, Command.Ptr);
10056 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
10057 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
10058 PrintSubLibraryCommand(Sl, Command.Ptr);
10059 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
10060 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
10061 PrintSubClientCommand(Sc, Command.Ptr);
10062 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
10063 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
10064 PrintRoutinesCommand(Rc);
10065 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
10066 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
10067 PrintRoutinesCommand64(Rc);
10068 } else if (Command.C.cmd == MachO::LC_THREAD ||
10069 Command.C.cmd == MachO::LC_UNIXTHREAD) {
10070 MachO::thread_command Tc = Obj->getThreadCommand(Command);
10071 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
10072 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
10073 Command.C.cmd == MachO::LC_ID_DYLIB ||
10074 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
10075 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
10076 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
10077 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
10078 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
10079 PrintDylibCommand(Dl, Command.Ptr);
10080 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
10081 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
10082 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
10083 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
10084 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
10085 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
10086 MachO::linkedit_data_command Ld =
10087 Obj->getLinkeditDataLoadCommand(Command);
10088 PrintLinkEditDataCommand(Ld, Buf.size());
10089 } else {
10090 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
10091 << ")\n";
10092 outs() << " cmdsize " << Command.C.cmdsize << "\n";
10093 // TODO: get and print the raw bytes of the load command.
10095 // TODO: print all the other kinds of load commands.
10099 static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
10100 if (Obj->is64Bit()) {
10101 MachO::mach_header_64 H_64;
10102 H_64 = Obj->getHeader64();
10103 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
10104 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
10105 } else {
10106 MachO::mach_header H;
10107 H = Obj->getHeader();
10108 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
10109 H.sizeofcmds, H.flags, verbose);
10113 void printMachOFileHeader(const object::ObjectFile *Obj) {
10114 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
10115 PrintMachHeader(file, !NonVerbose);
10118 void printMachOLoadCommands(const object::ObjectFile *Obj) {
10119 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
10120 uint32_t filetype = 0;
10121 uint32_t cputype = 0;
10122 if (file->is64Bit()) {
10123 MachO::mach_header_64 H_64;
10124 H_64 = file->getHeader64();
10125 filetype = H_64.filetype;
10126 cputype = H_64.cputype;
10127 } else {
10128 MachO::mach_header H;
10129 H = file->getHeader();
10130 filetype = H.filetype;
10131 cputype = H.cputype;
10133 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
10136 //===----------------------------------------------------------------------===//
10137 // export trie dumping
10138 //===----------------------------------------------------------------------===//
10140 void printMachOExportsTrie(const object::MachOObjectFile *Obj) {
10141 uint64_t BaseSegmentAddress = 0;
10142 for (const auto &Command : Obj->load_commands()) {
10143 if (Command.C.cmd == MachO::LC_SEGMENT) {
10144 MachO::segment_command Seg = Obj->getSegmentLoadCommand(Command);
10145 if (Seg.fileoff == 0 && Seg.filesize != 0) {
10146 BaseSegmentAddress = Seg.vmaddr;
10147 break;
10149 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
10150 MachO::segment_command_64 Seg = Obj->getSegment64LoadCommand(Command);
10151 if (Seg.fileoff == 0 && Seg.filesize != 0) {
10152 BaseSegmentAddress = Seg.vmaddr;
10153 break;
10157 Error Err = Error::success();
10158 for (const object::ExportEntry &Entry : Obj->exports(Err)) {
10159 uint64_t Flags = Entry.flags();
10160 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
10161 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
10162 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
10163 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
10164 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
10165 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
10166 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
10167 if (ReExport)
10168 outs() << "[re-export] ";
10169 else
10170 outs() << format("0x%08llX ",
10171 Entry.address() + BaseSegmentAddress);
10172 outs() << Entry.name();
10173 if (WeakDef || ThreadLocal || Resolver || Abs) {
10174 bool NeedsComma = false;
10175 outs() << " [";
10176 if (WeakDef) {
10177 outs() << "weak_def";
10178 NeedsComma = true;
10180 if (ThreadLocal) {
10181 if (NeedsComma)
10182 outs() << ", ";
10183 outs() << "per-thread";
10184 NeedsComma = true;
10186 if (Abs) {
10187 if (NeedsComma)
10188 outs() << ", ";
10189 outs() << "absolute";
10190 NeedsComma = true;
10192 if (Resolver) {
10193 if (NeedsComma)
10194 outs() << ", ";
10195 outs() << format("resolver=0x%08llX", Entry.other());
10196 NeedsComma = true;
10198 outs() << "]";
10200 if (ReExport) {
10201 StringRef DylibName = "unknown";
10202 int Ordinal = Entry.other() - 1;
10203 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
10204 if (Entry.otherName().empty())
10205 outs() << " (from " << DylibName << ")";
10206 else
10207 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
10209 outs() << "\n";
10211 if (Err)
10212 report_error(std::move(Err), Obj->getFileName());
10215 //===----------------------------------------------------------------------===//
10216 // rebase table dumping
10217 //===----------------------------------------------------------------------===//
10219 void printMachORebaseTable(object::MachOObjectFile *Obj) {
10220 outs() << "segment section address type\n";
10221 Error Err = Error::success();
10222 for (const object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) {
10223 StringRef SegmentName = Entry.segmentName();
10224 StringRef SectionName = Entry.sectionName();
10225 uint64_t Address = Entry.address();
10227 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
10228 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
10229 SegmentName.str().c_str(), SectionName.str().c_str(),
10230 Address, Entry.typeName().str().c_str());
10232 if (Err)
10233 report_error(std::move(Err), Obj->getFileName());
10236 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
10237 StringRef DylibName;
10238 switch (Ordinal) {
10239 case MachO::BIND_SPECIAL_DYLIB_SELF:
10240 return "this-image";
10241 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
10242 return "main-executable";
10243 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
10244 return "flat-namespace";
10245 default:
10246 if (Ordinal > 0) {
10247 std::error_code EC =
10248 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
10249 if (EC)
10250 return "<<bad library ordinal>>";
10251 return DylibName;
10254 return "<<unknown special ordinal>>";
10257 //===----------------------------------------------------------------------===//
10258 // bind table dumping
10259 //===----------------------------------------------------------------------===//
10261 void printMachOBindTable(object::MachOObjectFile *Obj) {
10262 // Build table of sections so names can used in final output.
10263 outs() << "segment section address type "
10264 "addend dylib symbol\n";
10265 Error Err = Error::success();
10266 for (const object::MachOBindEntry &Entry : Obj->bindTable(Err)) {
10267 StringRef SegmentName = Entry.segmentName();
10268 StringRef SectionName = Entry.sectionName();
10269 uint64_t Address = Entry.address();
10271 // Table lines look like:
10272 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
10273 StringRef Attr;
10274 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
10275 Attr = " (weak_import)";
10276 outs() << left_justify(SegmentName, 8) << " "
10277 << left_justify(SectionName, 18) << " "
10278 << format_hex(Address, 10, true) << " "
10279 << left_justify(Entry.typeName(), 8) << " "
10280 << format_decimal(Entry.addend(), 8) << " "
10281 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
10282 << Entry.symbolName() << Attr << "\n";
10284 if (Err)
10285 report_error(std::move(Err), Obj->getFileName());
10288 //===----------------------------------------------------------------------===//
10289 // lazy bind table dumping
10290 //===----------------------------------------------------------------------===//
10292 void printMachOLazyBindTable(object::MachOObjectFile *Obj) {
10293 outs() << "segment section address "
10294 "dylib symbol\n";
10295 Error Err = Error::success();
10296 for (const object::MachOBindEntry &Entry : Obj->lazyBindTable(Err)) {
10297 StringRef SegmentName = Entry.segmentName();
10298 StringRef SectionName = Entry.sectionName();
10299 uint64_t Address = Entry.address();
10301 // Table lines look like:
10302 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
10303 outs() << left_justify(SegmentName, 8) << " "
10304 << left_justify(SectionName, 18) << " "
10305 << format_hex(Address, 10, true) << " "
10306 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
10307 << Entry.symbolName() << "\n";
10309 if (Err)
10310 report_error(std::move(Err), Obj->getFileName());
10313 //===----------------------------------------------------------------------===//
10314 // weak bind table dumping
10315 //===----------------------------------------------------------------------===//
10317 void printMachOWeakBindTable(object::MachOObjectFile *Obj) {
10318 outs() << "segment section address "
10319 "type addend symbol\n";
10320 Error Err = Error::success();
10321 for (const object::MachOBindEntry &Entry : Obj->weakBindTable(Err)) {
10322 // Strong symbols don't have a location to update.
10323 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
10324 outs() << " strong "
10325 << Entry.symbolName() << "\n";
10326 continue;
10328 StringRef SegmentName = Entry.segmentName();
10329 StringRef SectionName = Entry.sectionName();
10330 uint64_t Address = Entry.address();
10332 // Table lines look like:
10333 // __DATA __data 0x00001000 pointer 0 _foo
10334 outs() << left_justify(SegmentName, 8) << " "
10335 << left_justify(SectionName, 18) << " "
10336 << format_hex(Address, 10, true) << " "
10337 << left_justify(Entry.typeName(), 8) << " "
10338 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
10339 << "\n";
10341 if (Err)
10342 report_error(std::move(Err), Obj->getFileName());
10345 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
10346 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
10347 // information for that address. If the address is found its binding symbol
10348 // name is returned. If not nullptr is returned.
10349 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
10350 struct DisassembleInfo *info) {
10351 if (info->bindtable == nullptr) {
10352 info->bindtable = llvm::make_unique<SymbolAddressMap>();
10353 Error Err = Error::success();
10354 for (const object::MachOBindEntry &Entry : info->O->bindTable(Err)) {
10355 uint64_t Address = Entry.address();
10356 StringRef name = Entry.symbolName();
10357 if (!name.empty())
10358 (*info->bindtable)[Address] = name;
10360 if (Err)
10361 report_error(std::move(Err), info->O->getFileName());
10363 auto name = info->bindtable->lookup(ReferenceValue);
10364 return !name.empty() ? name.data() : nullptr;
10367 void printLazyBindTable(ObjectFile *o) {
10368 outs() << "Lazy bind table:\n";
10369 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10370 printMachOLazyBindTable(MachO);
10371 else
10372 WithColor::error()
10373 << "This operation is only currently supported "
10374 "for Mach-O executable files.\n";
10377 void printWeakBindTable(ObjectFile *o) {
10378 outs() << "Weak bind table:\n";
10379 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10380 printMachOWeakBindTable(MachO);
10381 else
10382 WithColor::error()
10383 << "This operation is only currently supported "
10384 "for Mach-O executable files.\n";
10387 void printExportsTrie(const ObjectFile *o) {
10388 outs() << "Exports trie:\n";
10389 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10390 printMachOExportsTrie(MachO);
10391 else
10392 WithColor::error()
10393 << "This operation is only currently supported "
10394 "for Mach-O executable files.\n";
10397 void printRebaseTable(ObjectFile *o) {
10398 outs() << "Rebase table:\n";
10399 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10400 printMachORebaseTable(MachO);
10401 else
10402 WithColor::error()
10403 << "This operation is only currently supported "
10404 "for Mach-O executable files.\n";
10407 void printBindTable(ObjectFile *o) {
10408 outs() << "Bind table:\n";
10409 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10410 printMachOBindTable(MachO);
10411 else
10412 WithColor::error()
10413 << "This operation is only currently supported "
10414 "for Mach-O executable files.\n";
10416 } // namespace llvm