[X86] Various type corrections to the code that creates LOCK_OR32mi8/OR32mi8Locked...
[llvm-core.git] / tools / llvm-objdump / MachODump.cpp
blob42b82ae5a9d2558972b5747ba2e3ba248a36365d
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 extern cl::opt<bool> ArchiveHeaders;
63 extern cl::opt<bool> Disassemble;
64 extern cl::opt<bool> DisassembleAll;
65 extern cl::opt<DIDumpType> DwarfDumpType;
66 extern cl::list<std::string> FilterSections;
67 extern cl::list<std::string> MAttrs;
68 extern cl::opt<std::string> MCPU;
69 extern cl::opt<bool> NoShowRawInsn;
70 extern cl::opt<bool> NoLeadingAddr;
71 extern cl::opt<bool> PrintImmHex;
72 extern cl::opt<bool> PrivateHeaders;
73 extern cl::opt<bool> Relocations;
74 extern cl::opt<bool> SectionHeaders;
75 extern cl::opt<bool> SectionContents;
76 extern cl::opt<bool> SymbolTable;
77 extern cl::opt<std::string> TripleName;
78 extern cl::opt<bool> UnwindInfo;
80 cl::opt<bool>
81 FirstPrivateHeader("private-header",
82 cl::desc("Display only the first format specific file "
83 "header"));
85 cl::opt<bool> ExportsTrie("exports-trie",
86 cl::desc("Display mach-o exported symbols"));
88 cl::opt<bool> Rebase("rebase", cl::desc("Display mach-o rebasing info"));
90 cl::opt<bool> Bind("bind", cl::desc("Display mach-o binding info"));
92 cl::opt<bool> LazyBind("lazy-bind",
93 cl::desc("Display mach-o lazy binding info"));
95 cl::opt<bool> WeakBind("weak-bind",
96 cl::desc("Display mach-o weak binding info"));
98 static cl::opt<bool>
99 UseDbg("g", cl::Grouping,
100 cl::desc("Print line information from debug info if available"));
102 static cl::opt<std::string> DSYMFile("dsym",
103 cl::desc("Use .dSYM file for debug info"));
105 static cl::opt<bool> FullLeadingAddr("full-leading-addr",
106 cl::desc("Print full leading address"));
108 static cl::opt<bool> NoLeadingHeaders("no-leading-headers",
109 cl::desc("Print no leading headers"));
111 cl::opt<bool> UniversalHeaders("universal-headers",
112 cl::desc("Print Mach-O universal headers "
113 "(requires -macho)"));
115 cl::opt<bool>
116 ArchiveMemberOffsets("archive-member-offsets",
117 cl::desc("Print the offset to each archive member for "
118 "Mach-O archives (requires -macho and "
119 "-archive-headers)"));
121 cl::opt<bool> IndirectSymbols("indirect-symbols",
122 cl::desc("Print indirect symbol table for Mach-O "
123 "objects (requires -macho)"));
125 cl::opt<bool>
126 DataInCode("data-in-code",
127 cl::desc("Print the data in code table for Mach-O objects "
128 "(requires -macho)"));
130 cl::opt<bool> LinkOptHints("link-opt-hints",
131 cl::desc("Print the linker optimization hints for "
132 "Mach-O objects (requires -macho)"));
134 cl::opt<bool> InfoPlist("info-plist",
135 cl::desc("Print the info plist section as strings for "
136 "Mach-O objects (requires -macho)"));
138 cl::opt<bool> DylibsUsed("dylibs-used",
139 cl::desc("Print the shared libraries used for linked "
140 "Mach-O files (requires -macho)"));
142 cl::opt<bool>
143 DylibId("dylib-id",
144 cl::desc("Print the shared library's id for the dylib Mach-O "
145 "file (requires -macho)"));
147 cl::opt<bool>
148 NonVerbose("non-verbose",
149 cl::desc("Print the info for Mach-O objects in "
150 "non-verbose or numeric form (requires -macho)"));
152 cl::opt<bool>
153 ObjcMetaData("objc-meta-data",
154 cl::desc("Print the Objective-C runtime meta data for "
155 "Mach-O files (requires -macho)"));
157 cl::opt<std::string> DisSymName(
158 "dis-symname",
159 cl::desc("disassemble just this symbol's instructions (requires -macho)"));
161 static cl::opt<bool> NoSymbolicOperands(
162 "no-symbolic-operands",
163 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
165 static cl::list<std::string>
166 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
167 cl::ZeroOrMore);
169 bool ArchAll = false;
171 static std::string ThumbTripleName;
173 static const Target *GetTarget(const MachOObjectFile *MachOObj,
174 const char **McpuDefault,
175 const Target **ThumbTarget) {
176 // Figure out the target triple.
177 Triple TT(TripleName);
178 if (TripleName.empty()) {
179 TT = MachOObj->getArchTriple(McpuDefault);
180 TripleName = TT.str();
183 if (TT.getArch() == Triple::arm) {
184 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
185 // that support ARM are also capable of Thumb mode.
186 Triple ThumbTriple = TT;
187 std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
188 ThumbTriple.setArchName(ThumbName);
189 ThumbTripleName = ThumbTriple.str();
192 // Get the target specific parser.
193 std::string Error;
194 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
195 if (TheTarget && ThumbTripleName.empty())
196 return TheTarget;
198 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
199 if (*ThumbTarget)
200 return TheTarget;
202 WithColor::error(errs(), "llvm-objdump") << "unable to get target for '";
203 if (!TheTarget)
204 errs() << TripleName;
205 else
206 errs() << ThumbTripleName;
207 errs() << "', see --version and --triple.\n";
208 return nullptr;
211 struct SymbolSorter {
212 bool operator()(const SymbolRef &A, const SymbolRef &B) {
213 Expected<SymbolRef::Type> ATypeOrErr = A.getType();
214 if (!ATypeOrErr)
215 report_error(ATypeOrErr.takeError(), A.getObject()->getFileName());
216 SymbolRef::Type AType = *ATypeOrErr;
217 Expected<SymbolRef::Type> BTypeOrErr = B.getType();
218 if (!BTypeOrErr)
219 report_error(BTypeOrErr.takeError(), B.getObject()->getFileName());
220 SymbolRef::Type BType = *BTypeOrErr;
221 uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue();
222 uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue();
223 return AAddr < BAddr;
227 // Types for the storted data in code table that is built before disassembly
228 // and the predicate function to sort them.
229 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
230 typedef std::vector<DiceTableEntry> DiceTable;
231 typedef DiceTable::iterator dice_table_iterator;
233 #ifdef HAVE_LIBXAR
234 namespace {
235 struct ScopedXarFile {
236 xar_t xar;
237 ScopedXarFile(const char *filename, int32_t flags)
238 : xar(xar_open(filename, flags)) {}
239 ~ScopedXarFile() {
240 if (xar)
241 xar_close(xar);
243 ScopedXarFile(const ScopedXarFile &) = delete;
244 ScopedXarFile &operator=(const ScopedXarFile &) = delete;
245 operator xar_t() { return xar; }
248 struct ScopedXarIter {
249 xar_iter_t iter;
250 ScopedXarIter() : iter(xar_iter_new()) {}
251 ~ScopedXarIter() {
252 if (iter)
253 xar_iter_free(iter);
255 ScopedXarIter(const ScopedXarIter &) = delete;
256 ScopedXarIter &operator=(const ScopedXarIter &) = delete;
257 operator xar_iter_t() { return iter; }
259 } // namespace
260 #endif // defined(HAVE_LIBXAR)
262 // This is used to search for a data in code table entry for the PC being
263 // disassembled. The j parameter has the PC in j.first. A single data in code
264 // table entry can cover many bytes for each of its Kind's. So if the offset,
265 // aka the i.first value, of the data in code table entry plus its Length
266 // covers the PC being searched for this will return true. If not it will
267 // return false.
268 static bool compareDiceTableEntries(const DiceTableEntry &i,
269 const DiceTableEntry &j) {
270 uint16_t Length;
271 i.second.getLength(Length);
273 return j.first >= i.first && j.first < i.first + Length;
276 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
277 unsigned short Kind) {
278 uint32_t Value, Size = 1;
280 switch (Kind) {
281 default:
282 case MachO::DICE_KIND_DATA:
283 if (Length >= 4) {
284 if (!NoShowRawInsn)
285 dumpBytes(makeArrayRef(bytes, 4), outs());
286 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
287 outs() << "\t.long " << Value;
288 Size = 4;
289 } else if (Length >= 2) {
290 if (!NoShowRawInsn)
291 dumpBytes(makeArrayRef(bytes, 2), outs());
292 Value = bytes[1] << 8 | bytes[0];
293 outs() << "\t.short " << Value;
294 Size = 2;
295 } else {
296 if (!NoShowRawInsn)
297 dumpBytes(makeArrayRef(bytes, 2), outs());
298 Value = bytes[0];
299 outs() << "\t.byte " << Value;
300 Size = 1;
302 if (Kind == MachO::DICE_KIND_DATA)
303 outs() << "\t@ KIND_DATA\n";
304 else
305 outs() << "\t@ data in code kind = " << Kind << "\n";
306 break;
307 case MachO::DICE_KIND_JUMP_TABLE8:
308 if (!NoShowRawInsn)
309 dumpBytes(makeArrayRef(bytes, 1), outs());
310 Value = bytes[0];
311 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
312 Size = 1;
313 break;
314 case MachO::DICE_KIND_JUMP_TABLE16:
315 if (!NoShowRawInsn)
316 dumpBytes(makeArrayRef(bytes, 2), outs());
317 Value = bytes[1] << 8 | bytes[0];
318 outs() << "\t.short " << format("%5u", Value & 0xffff)
319 << "\t@ KIND_JUMP_TABLE16\n";
320 Size = 2;
321 break;
322 case MachO::DICE_KIND_JUMP_TABLE32:
323 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
324 if (!NoShowRawInsn)
325 dumpBytes(makeArrayRef(bytes, 4), outs());
326 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
327 outs() << "\t.long " << Value;
328 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
329 outs() << "\t@ KIND_JUMP_TABLE32\n";
330 else
331 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
332 Size = 4;
333 break;
335 return Size;
338 static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
339 std::vector<SectionRef> &Sections,
340 std::vector<SymbolRef> &Symbols,
341 SmallVectorImpl<uint64_t> &FoundFns,
342 uint64_t &BaseSegmentAddress) {
343 const StringRef FileName = MachOObj->getFileName();
344 for (const SymbolRef &Symbol : MachOObj->symbols()) {
345 StringRef SymName = unwrapOrError(Symbol.getName(), FileName);
346 if (!SymName.startswith("ltmp"))
347 Symbols.push_back(Symbol);
350 for (const SectionRef &Section : MachOObj->sections()) {
351 StringRef SectName;
352 Section.getName(SectName);
353 Sections.push_back(Section);
356 bool BaseSegmentAddressSet = false;
357 for (const auto &Command : MachOObj->load_commands()) {
358 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
359 // We found a function starts segment, parse the addresses for later
360 // consumption.
361 MachO::linkedit_data_command LLC =
362 MachOObj->getLinkeditDataLoadCommand(Command);
364 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
365 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
366 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
367 StringRef SegName = SLC.segname;
368 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
369 BaseSegmentAddressSet = true;
370 BaseSegmentAddress = SLC.vmaddr;
376 static void printRelocationTargetName(const MachOObjectFile *O,
377 const MachO::any_relocation_info &RE,
378 raw_string_ostream &Fmt) {
379 // Target of a scattered relocation is an address. In the interest of
380 // generating pretty output, scan through the symbol table looking for a
381 // symbol that aligns with that address. If we find one, print it.
382 // Otherwise, we just print the hex address of the target.
383 const StringRef FileName = O->getFileName();
384 if (O->isRelocationScattered(RE)) {
385 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
387 for (const SymbolRef &Symbol : O->symbols()) {
388 uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName);
389 if (Addr != Val)
390 continue;
391 Fmt << unwrapOrError(Symbol.getName(), FileName);
392 return;
395 // If we couldn't find a symbol that this relocation refers to, try
396 // to find a section beginning instead.
397 for (const SectionRef &Section : ToolSectionFilter(*O)) {
398 StringRef Name;
399 uint64_t Addr = Section.getAddress();
400 if (Addr != Val)
401 continue;
402 if (std::error_code EC = Section.getName(Name))
403 report_error(errorCodeToError(EC), O->getFileName());
404 Fmt << Name;
405 return;
408 Fmt << format("0x%x", Val);
409 return;
412 StringRef S;
413 bool isExtern = O->getPlainRelocationExternal(RE);
414 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
416 if (O->getAnyRelocationType(RE) == MachO::ARM64_RELOC_ADDEND) {
417 Fmt << format("0x%0" PRIx64, Val);
418 return;
421 if (isExtern) {
422 symbol_iterator SI = O->symbol_begin();
423 advance(SI, Val);
424 S = unwrapOrError(SI->getName(), FileName);
425 } else {
426 section_iterator SI = O->section_begin();
427 // Adjust for the fact that sections are 1-indexed.
428 if (Val == 0) {
429 Fmt << "0 (?,?)";
430 return;
432 uint32_t I = Val - 1;
433 while (I != 0 && SI != O->section_end()) {
434 --I;
435 advance(SI, 1);
437 if (SI == O->section_end())
438 Fmt << Val << " (?,?)";
439 else
440 SI->getName(S);
443 Fmt << S;
446 Error getMachORelocationValueString(const MachOObjectFile *Obj,
447 const RelocationRef &RelRef,
448 SmallVectorImpl<char> &Result) {
449 DataRefImpl Rel = RelRef.getRawDataRefImpl();
450 MachO::any_relocation_info RE = Obj->getRelocation(Rel);
452 unsigned Arch = Obj->getArch();
454 std::string FmtBuf;
455 raw_string_ostream Fmt(FmtBuf);
456 unsigned Type = Obj->getAnyRelocationType(RE);
457 bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
459 // Determine any addends that should be displayed with the relocation.
460 // These require decoding the relocation type, which is triple-specific.
462 // X86_64 has entirely custom relocation types.
463 if (Arch == Triple::x86_64) {
464 switch (Type) {
465 case MachO::X86_64_RELOC_GOT_LOAD:
466 case MachO::X86_64_RELOC_GOT: {
467 printRelocationTargetName(Obj, RE, Fmt);
468 Fmt << "@GOT";
469 if (IsPCRel)
470 Fmt << "PCREL";
471 break;
473 case MachO::X86_64_RELOC_SUBTRACTOR: {
474 DataRefImpl RelNext = Rel;
475 Obj->moveRelocationNext(RelNext);
476 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
478 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
479 // X86_64_RELOC_UNSIGNED.
480 // NOTE: Scattered relocations don't exist on x86_64.
481 unsigned RType = Obj->getAnyRelocationType(RENext);
482 if (RType != MachO::X86_64_RELOC_UNSIGNED)
483 report_error(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after "
484 "X86_64_RELOC_SUBTRACTOR.");
486 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
487 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
488 printRelocationTargetName(Obj, RENext, Fmt);
489 Fmt << "-";
490 printRelocationTargetName(Obj, RE, Fmt);
491 break;
493 case MachO::X86_64_RELOC_TLV:
494 printRelocationTargetName(Obj, RE, Fmt);
495 Fmt << "@TLV";
496 if (IsPCRel)
497 Fmt << "P";
498 break;
499 case MachO::X86_64_RELOC_SIGNED_1:
500 printRelocationTargetName(Obj, RE, Fmt);
501 Fmt << "-1";
502 break;
503 case MachO::X86_64_RELOC_SIGNED_2:
504 printRelocationTargetName(Obj, RE, Fmt);
505 Fmt << "-2";
506 break;
507 case MachO::X86_64_RELOC_SIGNED_4:
508 printRelocationTargetName(Obj, RE, Fmt);
509 Fmt << "-4";
510 break;
511 default:
512 printRelocationTargetName(Obj, RE, Fmt);
513 break;
515 // X86 and ARM share some relocation types in common.
516 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
517 Arch == Triple::ppc) {
518 // Generic relocation types...
519 switch (Type) {
520 case MachO::GENERIC_RELOC_PAIR: // prints no info
521 return Error::success();
522 case MachO::GENERIC_RELOC_SECTDIFF: {
523 DataRefImpl RelNext = Rel;
524 Obj->moveRelocationNext(RelNext);
525 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
527 // X86 sect diff's must be followed by a relocation of type
528 // GENERIC_RELOC_PAIR.
529 unsigned RType = Obj->getAnyRelocationType(RENext);
531 if (RType != MachO::GENERIC_RELOC_PAIR)
532 report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
533 "GENERIC_RELOC_SECTDIFF.");
535 printRelocationTargetName(Obj, RE, Fmt);
536 Fmt << "-";
537 printRelocationTargetName(Obj, RENext, Fmt);
538 break;
542 if (Arch == Triple::x86 || Arch == Triple::ppc) {
543 switch (Type) {
544 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
545 DataRefImpl RelNext = Rel;
546 Obj->moveRelocationNext(RelNext);
547 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
549 // X86 sect diff's must be followed by a relocation of type
550 // GENERIC_RELOC_PAIR.
551 unsigned RType = Obj->getAnyRelocationType(RENext);
552 if (RType != MachO::GENERIC_RELOC_PAIR)
553 report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
554 "GENERIC_RELOC_LOCAL_SECTDIFF.");
556 printRelocationTargetName(Obj, RE, Fmt);
557 Fmt << "-";
558 printRelocationTargetName(Obj, RENext, Fmt);
559 break;
561 case MachO::GENERIC_RELOC_TLV: {
562 printRelocationTargetName(Obj, RE, Fmt);
563 Fmt << "@TLV";
564 if (IsPCRel)
565 Fmt << "P";
566 break;
568 default:
569 printRelocationTargetName(Obj, RE, Fmt);
571 } else { // ARM-specific relocations
572 switch (Type) {
573 case MachO::ARM_RELOC_HALF:
574 case MachO::ARM_RELOC_HALF_SECTDIFF: {
575 // Half relocations steal a bit from the length field to encode
576 // whether this is an upper16 or a lower16 relocation.
577 bool isUpper = (Obj->getAnyRelocationLength(RE) & 0x1) == 1;
579 if (isUpper)
580 Fmt << ":upper16:(";
581 else
582 Fmt << ":lower16:(";
583 printRelocationTargetName(Obj, RE, Fmt);
585 DataRefImpl RelNext = Rel;
586 Obj->moveRelocationNext(RelNext);
587 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
589 // ARM half relocs must be followed by a relocation of type
590 // ARM_RELOC_PAIR.
591 unsigned RType = Obj->getAnyRelocationType(RENext);
592 if (RType != MachO::ARM_RELOC_PAIR)
593 report_error(Obj->getFileName(), "Expected ARM_RELOC_PAIR after "
594 "ARM_RELOC_HALF");
596 // NOTE: The half of the target virtual address is stashed in the
597 // address field of the secondary relocation, but we can't reverse
598 // engineer the constant offset from it without decoding the movw/movt
599 // instruction to find the other half in its immediate field.
601 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
602 // symbol/section pointer of the follow-on relocation.
603 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
604 Fmt << "-";
605 printRelocationTargetName(Obj, RENext, Fmt);
608 Fmt << ")";
609 break;
611 default: {
612 printRelocationTargetName(Obj, RE, Fmt);
616 } else
617 printRelocationTargetName(Obj, RE, Fmt);
619 Fmt.flush();
620 Result.append(FmtBuf.begin(), FmtBuf.end());
621 return Error::success();
624 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
625 uint32_t n, uint32_t count,
626 uint32_t stride, uint64_t addr) {
627 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
628 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
629 if (n > nindirectsyms)
630 outs() << " (entries start past the end of the indirect symbol "
631 "table) (reserved1 field greater than the table size)";
632 else if (n + count > nindirectsyms)
633 outs() << " (entries extends past the end of the indirect symbol "
634 "table)";
635 outs() << "\n";
636 uint32_t cputype = O->getHeader().cputype;
637 if (cputype & MachO::CPU_ARCH_ABI64)
638 outs() << "address index";
639 else
640 outs() << "address index";
641 if (verbose)
642 outs() << " name\n";
643 else
644 outs() << "\n";
645 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
646 if (cputype & MachO::CPU_ARCH_ABI64)
647 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
648 else
649 outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " ";
650 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
651 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
652 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
653 outs() << "LOCAL\n";
654 continue;
656 if (indirect_symbol ==
657 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
658 outs() << "LOCAL ABSOLUTE\n";
659 continue;
661 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
662 outs() << "ABSOLUTE\n";
663 continue;
665 outs() << format("%5u ", indirect_symbol);
666 if (verbose) {
667 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
668 if (indirect_symbol < Symtab.nsyms) {
669 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
670 SymbolRef Symbol = *Sym;
671 outs() << unwrapOrError(Symbol.getName(), O->getFileName());
672 } else {
673 outs() << "?";
676 outs() << "\n";
680 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
681 for (const auto &Load : O->load_commands()) {
682 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
683 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
684 for (unsigned J = 0; J < Seg.nsects; ++J) {
685 MachO::section_64 Sec = O->getSection64(Load, J);
686 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
687 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
688 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
689 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
690 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
691 section_type == MachO::S_SYMBOL_STUBS) {
692 uint32_t stride;
693 if (section_type == MachO::S_SYMBOL_STUBS)
694 stride = Sec.reserved2;
695 else
696 stride = 8;
697 if (stride == 0) {
698 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
699 << Sec.sectname << ") "
700 << "(size of stubs in reserved2 field is zero)\n";
701 continue;
703 uint32_t count = Sec.size / stride;
704 outs() << "Indirect symbols for (" << Sec.segname << ","
705 << Sec.sectname << ") " << count << " entries";
706 uint32_t n = Sec.reserved1;
707 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
710 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
711 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
712 for (unsigned J = 0; J < Seg.nsects; ++J) {
713 MachO::section Sec = O->getSection(Load, J);
714 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
715 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
716 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
717 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
718 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
719 section_type == MachO::S_SYMBOL_STUBS) {
720 uint32_t stride;
721 if (section_type == MachO::S_SYMBOL_STUBS)
722 stride = Sec.reserved2;
723 else
724 stride = 4;
725 if (stride == 0) {
726 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
727 << Sec.sectname << ") "
728 << "(size of stubs in reserved2 field is zero)\n";
729 continue;
731 uint32_t count = Sec.size / stride;
732 outs() << "Indirect symbols for (" << Sec.segname << ","
733 << Sec.sectname << ") " << count << " entries";
734 uint32_t n = Sec.reserved1;
735 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
742 static void PrintRType(const uint64_t cputype, const unsigned r_type) {
743 static char const *generic_r_types[] = {
744 "VANILLA ", "PAIR ", "SECTDIF ", "PBLAPTR ", "LOCSDIF ", "TLV ",
745 " 6 (?) ", " 7 (?) ", " 8 (?) ", " 9 (?) ", " 10 (?) ", " 11 (?) ",
746 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
748 static char const *x86_64_r_types[] = {
749 "UNSIGND ", "SIGNED ", "BRANCH ", "GOT_LD ", "GOT ", "SUB ",
750 "SIGNED1 ", "SIGNED2 ", "SIGNED4 ", "TLV ", " 10 (?) ", " 11 (?) ",
751 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
753 static char const *arm_r_types[] = {
754 "VANILLA ", "PAIR ", "SECTDIFF", "LOCSDIF ", "PBLAPTR ",
755 "BR24 ", "T_BR22 ", "T_BR32 ", "HALF ", "HALFDIF ",
756 " 10 (?) ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
758 static char const *arm64_r_types[] = {
759 "UNSIGND ", "SUB ", "BR26 ", "PAGE21 ", "PAGOF12 ",
760 "GOTLDP ", "GOTLDPOF", "PTRTGOT ", "TLVLDP ", "TLVLDPOF",
761 "ADDEND ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
764 if (r_type > 0xf){
765 outs() << format("%-7u", r_type) << " ";
766 return;
768 switch (cputype) {
769 case MachO::CPU_TYPE_I386:
770 outs() << generic_r_types[r_type];
771 break;
772 case MachO::CPU_TYPE_X86_64:
773 outs() << x86_64_r_types[r_type];
774 break;
775 case MachO::CPU_TYPE_ARM:
776 outs() << arm_r_types[r_type];
777 break;
778 case MachO::CPU_TYPE_ARM64:
779 outs() << arm64_r_types[r_type];
780 break;
781 default:
782 outs() << format("%-7u ", r_type);
786 static void PrintRLength(const uint64_t cputype, const unsigned r_type,
787 const unsigned r_length, const bool previous_arm_half){
788 if (cputype == MachO::CPU_TYPE_ARM &&
789 (r_type == MachO::ARM_RELOC_HALF ||
790 r_type == MachO::ARM_RELOC_HALF_SECTDIFF || previous_arm_half == true)) {
791 if ((r_length & 0x1) == 0)
792 outs() << "lo/";
793 else
794 outs() << "hi/";
795 if ((r_length & 0x1) == 0)
796 outs() << "arm ";
797 else
798 outs() << "thm ";
799 } else {
800 switch (r_length) {
801 case 0:
802 outs() << "byte ";
803 break;
804 case 1:
805 outs() << "word ";
806 break;
807 case 2:
808 outs() << "long ";
809 break;
810 case 3:
811 if (cputype == MachO::CPU_TYPE_X86_64)
812 outs() << "quad ";
813 else
814 outs() << format("?(%2d) ", r_length);
815 break;
816 default:
817 outs() << format("?(%2d) ", r_length);
822 static void PrintRelocationEntries(const MachOObjectFile *O,
823 const relocation_iterator Begin,
824 const relocation_iterator End,
825 const uint64_t cputype,
826 const bool verbose) {
827 const MachO::symtab_command Symtab = O->getSymtabLoadCommand();
828 bool previous_arm_half = false;
829 bool previous_sectdiff = false;
830 uint32_t sectdiff_r_type = 0;
832 for (relocation_iterator Reloc = Begin; Reloc != End; ++Reloc) {
833 const DataRefImpl Rel = Reloc->getRawDataRefImpl();
834 const MachO::any_relocation_info RE = O->getRelocation(Rel);
835 const unsigned r_type = O->getAnyRelocationType(RE);
836 const bool r_scattered = O->isRelocationScattered(RE);
837 const unsigned r_pcrel = O->getAnyRelocationPCRel(RE);
838 const unsigned r_length = O->getAnyRelocationLength(RE);
839 const unsigned r_address = O->getAnyRelocationAddress(RE);
840 const bool r_extern = (r_scattered ? false :
841 O->getPlainRelocationExternal(RE));
842 const uint32_t r_value = (r_scattered ?
843 O->getScatteredRelocationValue(RE) : 0);
844 const unsigned r_symbolnum = (r_scattered ? 0 :
845 O->getPlainRelocationSymbolNum(RE));
847 if (r_scattered && cputype != MachO::CPU_TYPE_X86_64) {
848 if (verbose) {
849 // scattered: address
850 if ((cputype == MachO::CPU_TYPE_I386 &&
851 r_type == MachO::GENERIC_RELOC_PAIR) ||
852 (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR))
853 outs() << " ";
854 else
855 outs() << format("%08x ", (unsigned int)r_address);
857 // scattered: pcrel
858 if (r_pcrel)
859 outs() << "True ";
860 else
861 outs() << "False ";
863 // scattered: length
864 PrintRLength(cputype, r_type, r_length, previous_arm_half);
866 // scattered: extern & type
867 outs() << "n/a ";
868 PrintRType(cputype, r_type);
870 // scattered: scattered & value
871 outs() << format("True 0x%08x", (unsigned int)r_value);
872 if (previous_sectdiff == false) {
873 if ((cputype == MachO::CPU_TYPE_ARM &&
874 r_type == MachO::ARM_RELOC_PAIR))
875 outs() << format(" half = 0x%04x ", (unsigned int)r_address);
876 } else if (cputype == MachO::CPU_TYPE_ARM &&
877 sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF)
878 outs() << format(" other_half = 0x%04x ", (unsigned int)r_address);
879 if ((cputype == MachO::CPU_TYPE_I386 &&
880 (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
881 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) ||
882 (cputype == MachO::CPU_TYPE_ARM &&
883 (sectdiff_r_type == MachO::ARM_RELOC_SECTDIFF ||
884 sectdiff_r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
885 sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF))) {
886 previous_sectdiff = true;
887 sectdiff_r_type = r_type;
888 } else {
889 previous_sectdiff = false;
890 sectdiff_r_type = 0;
892 if (cputype == MachO::CPU_TYPE_ARM &&
893 (r_type == MachO::ARM_RELOC_HALF ||
894 r_type == MachO::ARM_RELOC_HALF_SECTDIFF))
895 previous_arm_half = true;
896 else
897 previous_arm_half = false;
898 outs() << "\n";
900 else {
901 // scattered: address pcrel length extern type scattered value
902 outs() << format("%08x %1d %-2d n/a %-7d 1 0x%08x\n",
903 (unsigned int)r_address, r_pcrel, r_length, r_type,
904 (unsigned int)r_value);
907 else {
908 if (verbose) {
909 // plain: address
910 if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR)
911 outs() << " ";
912 else
913 outs() << format("%08x ", (unsigned int)r_address);
915 // plain: pcrel
916 if (r_pcrel)
917 outs() << "True ";
918 else
919 outs() << "False ";
921 // plain: length
922 PrintRLength(cputype, r_type, r_length, previous_arm_half);
924 if (r_extern) {
925 // plain: extern & type & scattered
926 outs() << "True ";
927 PrintRType(cputype, r_type);
928 outs() << "False ";
930 // plain: symbolnum/value
931 if (r_symbolnum > Symtab.nsyms)
932 outs() << format("?(%d)\n", r_symbolnum);
933 else {
934 SymbolRef Symbol = *O->getSymbolByIndex(r_symbolnum);
935 Expected<StringRef> SymNameNext = Symbol.getName();
936 const char *name = NULL;
937 if (SymNameNext)
938 name = SymNameNext->data();
939 if (name == NULL)
940 outs() << format("?(%d)\n", r_symbolnum);
941 else
942 outs() << name << "\n";
945 else {
946 // plain: extern & type & scattered
947 outs() << "False ";
948 PrintRType(cputype, r_type);
949 outs() << "False ";
951 // plain: symbolnum/value
952 if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR)
953 outs() << format("other_half = 0x%04x\n", (unsigned int)r_address);
954 else if (cputype == MachO::CPU_TYPE_ARM64 &&
955 r_type == MachO::ARM64_RELOC_ADDEND)
956 outs() << format("addend = 0x%06x\n", (unsigned int)r_symbolnum);
957 else {
958 outs() << format("%d ", r_symbolnum);
959 if (r_symbolnum == MachO::R_ABS)
960 outs() << "R_ABS\n";
961 else {
962 // in this case, r_symbolnum is actually a 1-based section number
963 uint32_t nsects = O->section_end()->getRawDataRefImpl().d.a;
964 if (r_symbolnum > 0 && r_symbolnum <= nsects) {
965 object::DataRefImpl DRI;
966 DRI.d.a = r_symbolnum-1;
967 StringRef SegName = O->getSectionFinalSegmentName(DRI);
968 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
969 outs() << "(" << SegName << "," << *NameOrErr << ")\n";
970 else
971 outs() << "(?,?)\n";
973 else {
974 outs() << "(?,?)\n";
979 if (cputype == MachO::CPU_TYPE_ARM &&
980 (r_type == MachO::ARM_RELOC_HALF ||
981 r_type == MachO::ARM_RELOC_HALF_SECTDIFF))
982 previous_arm_half = true;
983 else
984 previous_arm_half = false;
986 else {
987 // plain: address pcrel length extern type scattered symbolnum/section
988 outs() << format("%08x %1d %-2d %1d %-7d 0 %d\n",
989 (unsigned int)r_address, r_pcrel, r_length, r_extern,
990 r_type, r_symbolnum);
996 static void PrintRelocations(const MachOObjectFile *O, const bool verbose) {
997 const uint64_t cputype = O->getHeader().cputype;
998 const MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
999 if (Dysymtab.nextrel != 0) {
1000 outs() << "External relocation information " << Dysymtab.nextrel
1001 << " entries";
1002 outs() << "\naddress pcrel length extern type scattered "
1003 "symbolnum/value\n";
1004 PrintRelocationEntries(O, O->extrel_begin(), O->extrel_end(), cputype,
1005 verbose);
1007 if (Dysymtab.nlocrel != 0) {
1008 outs() << format("Local relocation information %u entries",
1009 Dysymtab.nlocrel);
1010 outs() << "\naddress pcrel length extern type scattered "
1011 "symbolnum/value\n";
1012 PrintRelocationEntries(O, O->locrel_begin(), O->locrel_end(), cputype,
1013 verbose);
1015 for (const auto &Load : O->load_commands()) {
1016 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
1017 const MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
1018 for (unsigned J = 0; J < Seg.nsects; ++J) {
1019 const MachO::section_64 Sec = O->getSection64(Load, J);
1020 if (Sec.nreloc != 0) {
1021 DataRefImpl DRI;
1022 DRI.d.a = J;
1023 const StringRef SegName = O->getSectionFinalSegmentName(DRI);
1024 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
1025 outs() << "Relocation information (" << SegName << "," << *NameOrErr
1026 << format(") %u entries", Sec.nreloc);
1027 else
1028 outs() << "Relocation information (" << SegName << ",?) "
1029 << format("%u entries", Sec.nreloc);
1030 outs() << "\naddress pcrel length extern type scattered "
1031 "symbolnum/value\n";
1032 PrintRelocationEntries(O, O->section_rel_begin(DRI),
1033 O->section_rel_end(DRI), cputype, verbose);
1036 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
1037 const MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
1038 for (unsigned J = 0; J < Seg.nsects; ++J) {
1039 const MachO::section Sec = O->getSection(Load, J);
1040 if (Sec.nreloc != 0) {
1041 DataRefImpl DRI;
1042 DRI.d.a = J;
1043 const StringRef SegName = O->getSectionFinalSegmentName(DRI);
1044 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
1045 outs() << "Relocation information (" << SegName << "," << *NameOrErr
1046 << format(") %u entries", Sec.nreloc);
1047 else
1048 outs() << "Relocation information (" << SegName << ",?) "
1049 << format("%u entries", Sec.nreloc);
1050 outs() << "\naddress pcrel length extern type scattered "
1051 "symbolnum/value\n";
1052 PrintRelocationEntries(O, O->section_rel_begin(DRI),
1053 O->section_rel_end(DRI), cputype, verbose);
1060 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
1061 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
1062 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
1063 outs() << "Data in code table (" << nentries << " entries)\n";
1064 outs() << "offset length kind\n";
1065 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
1066 ++DI) {
1067 uint32_t Offset;
1068 DI->getOffset(Offset);
1069 outs() << format("0x%08" PRIx32, Offset) << " ";
1070 uint16_t Length;
1071 DI->getLength(Length);
1072 outs() << format("%6u", Length) << " ";
1073 uint16_t Kind;
1074 DI->getKind(Kind);
1075 if (verbose) {
1076 switch (Kind) {
1077 case MachO::DICE_KIND_DATA:
1078 outs() << "DATA";
1079 break;
1080 case MachO::DICE_KIND_JUMP_TABLE8:
1081 outs() << "JUMP_TABLE8";
1082 break;
1083 case MachO::DICE_KIND_JUMP_TABLE16:
1084 outs() << "JUMP_TABLE16";
1085 break;
1086 case MachO::DICE_KIND_JUMP_TABLE32:
1087 outs() << "JUMP_TABLE32";
1088 break;
1089 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
1090 outs() << "ABS_JUMP_TABLE32";
1091 break;
1092 default:
1093 outs() << format("0x%04" PRIx32, Kind);
1094 break;
1096 } else
1097 outs() << format("0x%04" PRIx32, Kind);
1098 outs() << "\n";
1102 static void PrintLinkOptHints(MachOObjectFile *O) {
1103 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
1104 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
1105 uint32_t nloh = LohLC.datasize;
1106 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
1107 for (uint32_t i = 0; i < nloh;) {
1108 unsigned n;
1109 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
1110 i += n;
1111 outs() << " identifier " << identifier << " ";
1112 if (i >= nloh)
1113 return;
1114 switch (identifier) {
1115 case 1:
1116 outs() << "AdrpAdrp\n";
1117 break;
1118 case 2:
1119 outs() << "AdrpLdr\n";
1120 break;
1121 case 3:
1122 outs() << "AdrpAddLdr\n";
1123 break;
1124 case 4:
1125 outs() << "AdrpLdrGotLdr\n";
1126 break;
1127 case 5:
1128 outs() << "AdrpAddStr\n";
1129 break;
1130 case 6:
1131 outs() << "AdrpLdrGotStr\n";
1132 break;
1133 case 7:
1134 outs() << "AdrpAdd\n";
1135 break;
1136 case 8:
1137 outs() << "AdrpLdrGot\n";
1138 break;
1139 default:
1140 outs() << "Unknown identifier value\n";
1141 break;
1143 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
1144 i += n;
1145 outs() << " narguments " << narguments << "\n";
1146 if (i >= nloh)
1147 return;
1149 for (uint32_t j = 0; j < narguments; j++) {
1150 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
1151 i += n;
1152 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
1153 if (i >= nloh)
1154 return;
1159 static void PrintDylibs(MachOObjectFile *O, bool JustId) {
1160 unsigned Index = 0;
1161 for (const auto &Load : O->load_commands()) {
1162 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
1163 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
1164 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
1165 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
1166 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
1167 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
1168 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
1169 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
1170 if (dl.dylib.name < dl.cmdsize) {
1171 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
1172 if (JustId)
1173 outs() << p << "\n";
1174 else {
1175 outs() << "\t" << p;
1176 outs() << " (compatibility version "
1177 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
1178 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
1179 << (dl.dylib.compatibility_version & 0xff) << ",";
1180 outs() << " current version "
1181 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
1182 << ((dl.dylib.current_version >> 8) & 0xff) << "."
1183 << (dl.dylib.current_version & 0xff) << ")\n";
1185 } else {
1186 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
1187 if (Load.C.cmd == MachO::LC_ID_DYLIB)
1188 outs() << "LC_ID_DYLIB ";
1189 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
1190 outs() << "LC_LOAD_DYLIB ";
1191 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
1192 outs() << "LC_LOAD_WEAK_DYLIB ";
1193 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
1194 outs() << "LC_LAZY_LOAD_DYLIB ";
1195 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
1196 outs() << "LC_REEXPORT_DYLIB ";
1197 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
1198 outs() << "LC_LOAD_UPWARD_DYLIB ";
1199 else
1200 outs() << "LC_??? ";
1201 outs() << "command " << Index++ << "\n";
1207 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
1209 static void CreateSymbolAddressMap(MachOObjectFile *O,
1210 SymbolAddressMap *AddrMap) {
1211 // Create a map of symbol addresses to symbol names.
1212 const StringRef FileName = O->getFileName();
1213 for (const SymbolRef &Symbol : O->symbols()) {
1214 SymbolRef::Type ST = unwrapOrError(Symbol.getType(), FileName);
1215 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
1216 ST == SymbolRef::ST_Other) {
1217 uint64_t Address = Symbol.getValue();
1218 StringRef SymName = unwrapOrError(Symbol.getName(), FileName);
1219 if (!SymName.startswith(".objc"))
1220 (*AddrMap)[Address] = SymName;
1225 // GuessSymbolName is passed the address of what might be a symbol and a
1226 // pointer to the SymbolAddressMap. It returns the name of a symbol
1227 // with that address or nullptr if no symbol is found with that address.
1228 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
1229 const char *SymbolName = nullptr;
1230 // A DenseMap can't lookup up some values.
1231 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
1232 StringRef name = AddrMap->lookup(value);
1233 if (!name.empty())
1234 SymbolName = name.data();
1236 return SymbolName;
1239 static void DumpCstringChar(const char c) {
1240 char p[2];
1241 p[0] = c;
1242 p[1] = '\0';
1243 outs().write_escaped(p);
1246 static void DumpCstringSection(MachOObjectFile *O, const char *sect,
1247 uint32_t sect_size, uint64_t sect_addr,
1248 bool print_addresses) {
1249 for (uint32_t i = 0; i < sect_size; i++) {
1250 if (print_addresses) {
1251 if (O->is64Bit())
1252 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1253 else
1254 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1256 for (; i < sect_size && sect[i] != '\0'; i++)
1257 DumpCstringChar(sect[i]);
1258 if (i < sect_size && sect[i] == '\0')
1259 outs() << "\n";
1263 static void DumpLiteral4(uint32_t l, float f) {
1264 outs() << format("0x%08" PRIx32, l);
1265 if ((l & 0x7f800000) != 0x7f800000)
1266 outs() << format(" (%.16e)\n", f);
1267 else {
1268 if (l == 0x7f800000)
1269 outs() << " (+Infinity)\n";
1270 else if (l == 0xff800000)
1271 outs() << " (-Infinity)\n";
1272 else if ((l & 0x00400000) == 0x00400000)
1273 outs() << " (non-signaling Not-a-Number)\n";
1274 else
1275 outs() << " (signaling Not-a-Number)\n";
1279 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
1280 uint32_t sect_size, uint64_t sect_addr,
1281 bool print_addresses) {
1282 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
1283 if (print_addresses) {
1284 if (O->is64Bit())
1285 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1286 else
1287 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1289 float f;
1290 memcpy(&f, sect + i, sizeof(float));
1291 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1292 sys::swapByteOrder(f);
1293 uint32_t l;
1294 memcpy(&l, sect + i, sizeof(uint32_t));
1295 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1296 sys::swapByteOrder(l);
1297 DumpLiteral4(l, f);
1301 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
1302 double d) {
1303 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
1304 uint32_t Hi, Lo;
1305 Hi = (O->isLittleEndian()) ? l1 : l0;
1306 Lo = (O->isLittleEndian()) ? l0 : l1;
1308 // Hi is the high word, so this is equivalent to if(isfinite(d))
1309 if ((Hi & 0x7ff00000) != 0x7ff00000)
1310 outs() << format(" (%.16e)\n", d);
1311 else {
1312 if (Hi == 0x7ff00000 && Lo == 0)
1313 outs() << " (+Infinity)\n";
1314 else if (Hi == 0xfff00000 && Lo == 0)
1315 outs() << " (-Infinity)\n";
1316 else if ((Hi & 0x00080000) == 0x00080000)
1317 outs() << " (non-signaling Not-a-Number)\n";
1318 else
1319 outs() << " (signaling Not-a-Number)\n";
1323 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
1324 uint32_t sect_size, uint64_t sect_addr,
1325 bool print_addresses) {
1326 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
1327 if (print_addresses) {
1328 if (O->is64Bit())
1329 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1330 else
1331 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1333 double d;
1334 memcpy(&d, sect + i, sizeof(double));
1335 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1336 sys::swapByteOrder(d);
1337 uint32_t l0, l1;
1338 memcpy(&l0, sect + i, sizeof(uint32_t));
1339 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
1340 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1341 sys::swapByteOrder(l0);
1342 sys::swapByteOrder(l1);
1344 DumpLiteral8(O, l0, l1, d);
1348 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
1349 outs() << format("0x%08" PRIx32, l0) << " ";
1350 outs() << format("0x%08" PRIx32, l1) << " ";
1351 outs() << format("0x%08" PRIx32, l2) << " ";
1352 outs() << format("0x%08" PRIx32, l3) << "\n";
1355 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
1356 uint32_t sect_size, uint64_t sect_addr,
1357 bool print_addresses) {
1358 for (uint32_t i = 0; i < sect_size; i += 16) {
1359 if (print_addresses) {
1360 if (O->is64Bit())
1361 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1362 else
1363 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1365 uint32_t l0, l1, l2, l3;
1366 memcpy(&l0, sect + i, sizeof(uint32_t));
1367 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
1368 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
1369 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
1370 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1371 sys::swapByteOrder(l0);
1372 sys::swapByteOrder(l1);
1373 sys::swapByteOrder(l2);
1374 sys::swapByteOrder(l3);
1376 DumpLiteral16(l0, l1, l2, l3);
1380 static void DumpLiteralPointerSection(MachOObjectFile *O,
1381 const SectionRef &Section,
1382 const char *sect, uint32_t sect_size,
1383 uint64_t sect_addr,
1384 bool print_addresses) {
1385 // Collect the literal sections in this Mach-O file.
1386 std::vector<SectionRef> LiteralSections;
1387 for (const SectionRef &Section : O->sections()) {
1388 DataRefImpl Ref = Section.getRawDataRefImpl();
1389 uint32_t section_type;
1390 if (O->is64Bit()) {
1391 const MachO::section_64 Sec = O->getSection64(Ref);
1392 section_type = Sec.flags & MachO::SECTION_TYPE;
1393 } else {
1394 const MachO::section Sec = O->getSection(Ref);
1395 section_type = Sec.flags & MachO::SECTION_TYPE;
1397 if (section_type == MachO::S_CSTRING_LITERALS ||
1398 section_type == MachO::S_4BYTE_LITERALS ||
1399 section_type == MachO::S_8BYTE_LITERALS ||
1400 section_type == MachO::S_16BYTE_LITERALS)
1401 LiteralSections.push_back(Section);
1404 // Set the size of the literal pointer.
1405 uint32_t lp_size = O->is64Bit() ? 8 : 4;
1407 // Collect the external relocation symbols for the literal pointers.
1408 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
1409 for (const RelocationRef &Reloc : Section.relocations()) {
1410 DataRefImpl Rel;
1411 MachO::any_relocation_info RE;
1412 bool isExtern = false;
1413 Rel = Reloc.getRawDataRefImpl();
1414 RE = O->getRelocation(Rel);
1415 isExtern = O->getPlainRelocationExternal(RE);
1416 if (isExtern) {
1417 uint64_t RelocOffset = Reloc.getOffset();
1418 symbol_iterator RelocSym = Reloc.getSymbol();
1419 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
1422 array_pod_sort(Relocs.begin(), Relocs.end());
1424 // Dump each literal pointer.
1425 for (uint32_t i = 0; i < sect_size; i += lp_size) {
1426 if (print_addresses) {
1427 if (O->is64Bit())
1428 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1429 else
1430 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1432 uint64_t lp;
1433 if (O->is64Bit()) {
1434 memcpy(&lp, sect + i, sizeof(uint64_t));
1435 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1436 sys::swapByteOrder(lp);
1437 } else {
1438 uint32_t li;
1439 memcpy(&li, sect + i, sizeof(uint32_t));
1440 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1441 sys::swapByteOrder(li);
1442 lp = li;
1445 // First look for an external relocation entry for this literal pointer.
1446 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
1447 return P.first == i;
1449 if (Reloc != Relocs.end()) {
1450 symbol_iterator RelocSym = Reloc->second;
1451 StringRef SymName = unwrapOrError(RelocSym->getName(), O->getFileName());
1452 outs() << "external relocation entry for symbol:" << SymName << "\n";
1453 continue;
1456 // For local references see what the section the literal pointer points to.
1457 auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
1458 return lp >= R.getAddress() && lp < R.getAddress() + R.getSize();
1460 if (Sect == LiteralSections.end()) {
1461 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
1462 continue;
1465 uint64_t SectAddress = Sect->getAddress();
1466 uint64_t SectSize = Sect->getSize();
1468 StringRef SectName;
1469 Sect->getName(SectName);
1470 DataRefImpl Ref = Sect->getRawDataRefImpl();
1471 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
1472 outs() << SegmentName << ":" << SectName << ":";
1474 uint32_t section_type;
1475 if (O->is64Bit()) {
1476 const MachO::section_64 Sec = O->getSection64(Ref);
1477 section_type = Sec.flags & MachO::SECTION_TYPE;
1478 } else {
1479 const MachO::section Sec = O->getSection(Ref);
1480 section_type = Sec.flags & MachO::SECTION_TYPE;
1483 StringRef BytesStr;
1484 Sect->getContents(BytesStr);
1485 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
1487 switch (section_type) {
1488 case MachO::S_CSTRING_LITERALS:
1489 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
1490 i++) {
1491 DumpCstringChar(Contents[i]);
1493 outs() << "\n";
1494 break;
1495 case MachO::S_4BYTE_LITERALS:
1496 float f;
1497 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
1498 uint32_t l;
1499 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
1500 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1501 sys::swapByteOrder(f);
1502 sys::swapByteOrder(l);
1504 DumpLiteral4(l, f);
1505 break;
1506 case MachO::S_8BYTE_LITERALS: {
1507 double d;
1508 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
1509 uint32_t l0, l1;
1510 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
1511 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
1512 sizeof(uint32_t));
1513 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1514 sys::swapByteOrder(f);
1515 sys::swapByteOrder(l0);
1516 sys::swapByteOrder(l1);
1518 DumpLiteral8(O, l0, l1, d);
1519 break;
1521 case MachO::S_16BYTE_LITERALS: {
1522 uint32_t l0, l1, l2, l3;
1523 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
1524 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
1525 sizeof(uint32_t));
1526 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
1527 sizeof(uint32_t));
1528 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
1529 sizeof(uint32_t));
1530 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1531 sys::swapByteOrder(l0);
1532 sys::swapByteOrder(l1);
1533 sys::swapByteOrder(l2);
1534 sys::swapByteOrder(l3);
1536 DumpLiteral16(l0, l1, l2, l3);
1537 break;
1543 static void DumpInitTermPointerSection(MachOObjectFile *O,
1544 const SectionRef &Section,
1545 const char *sect,
1546 uint32_t sect_size, uint64_t sect_addr,
1547 SymbolAddressMap *AddrMap,
1548 bool verbose) {
1549 uint32_t stride;
1550 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
1552 // Collect the external relocation symbols for the pointers.
1553 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
1554 for (const RelocationRef &Reloc : Section.relocations()) {
1555 DataRefImpl Rel;
1556 MachO::any_relocation_info RE;
1557 bool isExtern = false;
1558 Rel = Reloc.getRawDataRefImpl();
1559 RE = O->getRelocation(Rel);
1560 isExtern = O->getPlainRelocationExternal(RE);
1561 if (isExtern) {
1562 uint64_t RelocOffset = Reloc.getOffset();
1563 symbol_iterator RelocSym = Reloc.getSymbol();
1564 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
1567 array_pod_sort(Relocs.begin(), Relocs.end());
1569 for (uint32_t i = 0; i < sect_size; i += stride) {
1570 const char *SymbolName = nullptr;
1571 uint64_t p;
1572 if (O->is64Bit()) {
1573 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
1574 uint64_t pointer_value;
1575 memcpy(&pointer_value, sect + i, stride);
1576 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1577 sys::swapByteOrder(pointer_value);
1578 outs() << format("0x%016" PRIx64, pointer_value);
1579 p = pointer_value;
1580 } else {
1581 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
1582 uint32_t pointer_value;
1583 memcpy(&pointer_value, sect + i, stride);
1584 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1585 sys::swapByteOrder(pointer_value);
1586 outs() << format("0x%08" PRIx32, pointer_value);
1587 p = pointer_value;
1589 if (verbose) {
1590 // First look for an external relocation entry for this pointer.
1591 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
1592 return P.first == i;
1594 if (Reloc != Relocs.end()) {
1595 symbol_iterator RelocSym = Reloc->second;
1596 outs() << " " << unwrapOrError(RelocSym->getName(), O->getFileName());
1597 } else {
1598 SymbolName = GuessSymbolName(p, AddrMap);
1599 if (SymbolName)
1600 outs() << " " << SymbolName;
1603 outs() << "\n";
1607 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
1608 uint32_t size, uint64_t addr) {
1609 uint32_t cputype = O->getHeader().cputype;
1610 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
1611 uint32_t j;
1612 for (uint32_t i = 0; i < size; i += j, addr += j) {
1613 if (O->is64Bit())
1614 outs() << format("%016" PRIx64, addr) << "\t";
1615 else
1616 outs() << format("%08" PRIx64, addr) << "\t";
1617 for (j = 0; j < 16 && i + j < size; j++) {
1618 uint8_t byte_word = *(sect + i + j);
1619 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1621 outs() << "\n";
1623 } else {
1624 uint32_t j;
1625 for (uint32_t i = 0; i < size; i += j, addr += j) {
1626 if (O->is64Bit())
1627 outs() << format("%016" PRIx64, addr) << "\t";
1628 else
1629 outs() << format("%08" PRIx64, addr) << "\t";
1630 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
1631 j += sizeof(int32_t)) {
1632 if (i + j + sizeof(int32_t) <= size) {
1633 uint32_t long_word;
1634 memcpy(&long_word, sect + i + j, sizeof(int32_t));
1635 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1636 sys::swapByteOrder(long_word);
1637 outs() << format("%08" PRIx32, long_word) << " ";
1638 } else {
1639 for (uint32_t k = 0; i + j + k < size; k++) {
1640 uint8_t byte_word = *(sect + i + j + k);
1641 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1645 outs() << "\n";
1650 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1651 StringRef DisSegName, StringRef DisSectName);
1652 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1653 uint32_t size, uint32_t addr);
1654 #ifdef HAVE_LIBXAR
1655 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
1656 uint32_t size, bool verbose,
1657 bool PrintXarHeader, bool PrintXarFileHeaders,
1658 std::string XarMemberName);
1659 #endif // defined(HAVE_LIBXAR)
1661 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1662 bool verbose) {
1663 SymbolAddressMap AddrMap;
1664 if (verbose)
1665 CreateSymbolAddressMap(O, &AddrMap);
1667 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1668 StringRef DumpSection = FilterSections[i];
1669 std::pair<StringRef, StringRef> DumpSegSectName;
1670 DumpSegSectName = DumpSection.split(',');
1671 StringRef DumpSegName, DumpSectName;
1672 if (!DumpSegSectName.second.empty()) {
1673 DumpSegName = DumpSegSectName.first;
1674 DumpSectName = DumpSegSectName.second;
1675 } else {
1676 DumpSegName = "";
1677 DumpSectName = DumpSegSectName.first;
1679 for (const SectionRef &Section : O->sections()) {
1680 StringRef SectName;
1681 Section.getName(SectName);
1682 DataRefImpl Ref = Section.getRawDataRefImpl();
1683 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1684 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1685 (SectName == DumpSectName)) {
1687 uint32_t section_flags;
1688 if (O->is64Bit()) {
1689 const MachO::section_64 Sec = O->getSection64(Ref);
1690 section_flags = Sec.flags;
1692 } else {
1693 const MachO::section Sec = O->getSection(Ref);
1694 section_flags = Sec.flags;
1696 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1698 StringRef BytesStr;
1699 Section.getContents(BytesStr);
1700 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1701 uint32_t sect_size = BytesStr.size();
1702 uint64_t sect_addr = Section.getAddress();
1704 outs() << "Contents of (" << SegName << "," << SectName
1705 << ") section\n";
1707 if (verbose) {
1708 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1709 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1710 DisassembleMachO(Filename, O, SegName, SectName);
1711 continue;
1713 if (SegName == "__TEXT" && SectName == "__info_plist") {
1714 outs() << sect;
1715 continue;
1717 if (SegName == "__OBJC" && SectName == "__protocol") {
1718 DumpProtocolSection(O, sect, sect_size, sect_addr);
1719 continue;
1721 #ifdef HAVE_LIBXAR
1722 if (SegName == "__LLVM" && SectName == "__bundle") {
1723 DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
1724 ArchiveHeaders, "");
1725 continue;
1727 #endif // defined(HAVE_LIBXAR)
1728 switch (section_type) {
1729 case MachO::S_REGULAR:
1730 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1731 break;
1732 case MachO::S_ZEROFILL:
1733 outs() << "zerofill section and has no contents in the file\n";
1734 break;
1735 case MachO::S_CSTRING_LITERALS:
1736 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1737 break;
1738 case MachO::S_4BYTE_LITERALS:
1739 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1740 break;
1741 case MachO::S_8BYTE_LITERALS:
1742 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1743 break;
1744 case MachO::S_16BYTE_LITERALS:
1745 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1746 break;
1747 case MachO::S_LITERAL_POINTERS:
1748 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1749 !NoLeadingAddr);
1750 break;
1751 case MachO::S_MOD_INIT_FUNC_POINTERS:
1752 case MachO::S_MOD_TERM_FUNC_POINTERS:
1753 DumpInitTermPointerSection(O, Section, sect, sect_size, sect_addr,
1754 &AddrMap, verbose);
1755 break;
1756 default:
1757 outs() << "Unknown section type ("
1758 << format("0x%08" PRIx32, section_type) << ")\n";
1759 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1760 break;
1762 } else {
1763 if (section_type == MachO::S_ZEROFILL)
1764 outs() << "zerofill section and has no contents in the file\n";
1765 else
1766 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1773 static void DumpInfoPlistSectionContents(StringRef Filename,
1774 MachOObjectFile *O) {
1775 for (const SectionRef &Section : O->sections()) {
1776 StringRef SectName;
1777 Section.getName(SectName);
1778 DataRefImpl Ref = Section.getRawDataRefImpl();
1779 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1780 if (SegName == "__TEXT" && SectName == "__info_plist") {
1781 if (!NoLeadingHeaders)
1782 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1783 StringRef BytesStr;
1784 Section.getContents(BytesStr);
1785 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1786 outs() << format("%.*s", BytesStr.size(), sect) << "\n";
1787 return;
1792 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1793 // and if it is and there is a list of architecture flags is specified then
1794 // check to make sure this Mach-O file is one of those architectures or all
1795 // architectures were specified. If not then an error is generated and this
1796 // routine returns false. Else it returns true.
1797 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1798 auto *MachO = dyn_cast<MachOObjectFile>(O);
1800 if (!MachO || ArchAll || ArchFlags.empty())
1801 return true;
1803 MachO::mach_header H;
1804 MachO::mach_header_64 H_64;
1805 Triple T;
1806 const char *McpuDefault, *ArchFlag;
1807 if (MachO->is64Bit()) {
1808 H_64 = MachO->MachOObjectFile::getHeader64();
1809 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
1810 &McpuDefault, &ArchFlag);
1811 } else {
1812 H = MachO->MachOObjectFile::getHeader();
1813 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
1814 &McpuDefault, &ArchFlag);
1816 const std::string ArchFlagName(ArchFlag);
1817 if (none_of(ArchFlags, [&](const std::string &Name) {
1818 return Name == ArchFlagName;
1819 })) {
1820 WithColor::error(errs(), "llvm-objdump")
1821 << Filename << ": no architecture specified.\n";
1822 return false;
1824 return true;
1827 static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1829 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1830 // archive member and or in a slice of a universal file. It prints the
1831 // the file name and header info and then processes it according to the
1832 // command line options.
1833 static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF,
1834 StringRef ArchiveMemberName = StringRef(),
1835 StringRef ArchitectureName = StringRef()) {
1836 // If we are doing some processing here on the Mach-O file print the header
1837 // info. And don't print it otherwise like in the case of printing the
1838 // UniversalHeaders or ArchiveHeaders.
1839 if (Disassemble || Relocations || PrivateHeaders || ExportsTrie || Rebase ||
1840 Bind || SymbolTable || LazyBind || WeakBind || IndirectSymbols ||
1841 DataInCode || LinkOptHints || DylibsUsed || DylibId || ObjcMetaData ||
1842 (!FilterSections.empty())) {
1843 if (!NoLeadingHeaders) {
1844 outs() << Name;
1845 if (!ArchiveMemberName.empty())
1846 outs() << '(' << ArchiveMemberName << ')';
1847 if (!ArchitectureName.empty())
1848 outs() << " (architecture " << ArchitectureName << ")";
1849 outs() << ":\n";
1852 // To use the report_error() form with an ArchiveName and FileName set
1853 // these up based on what is passed for Name and ArchiveMemberName.
1854 StringRef ArchiveName;
1855 StringRef FileName;
1856 if (!ArchiveMemberName.empty()) {
1857 ArchiveName = Name;
1858 FileName = ArchiveMemberName;
1859 } else {
1860 ArchiveName = StringRef();
1861 FileName = Name;
1864 // If we need the symbol table to do the operation then check it here to
1865 // produce a good error message as to where the Mach-O file comes from in
1866 // the error message.
1867 if (Disassemble || IndirectSymbols || !FilterSections.empty() || UnwindInfo)
1868 if (Error Err = MachOOF->checkSymbolTable())
1869 report_error(std::move(Err), ArchiveName, FileName, ArchitectureName);
1871 if (DisassembleAll) {
1872 for (const SectionRef &Section : MachOOF->sections()) {
1873 StringRef SectName;
1874 Section.getName(SectName);
1875 if (SectName.equals("__text")) {
1876 DataRefImpl Ref = Section.getRawDataRefImpl();
1877 StringRef SegName = MachOOF->getSectionFinalSegmentName(Ref);
1878 DisassembleMachO(FileName, MachOOF, SegName, SectName);
1882 else if (Disassemble) {
1883 if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE &&
1884 MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64)
1885 DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text");
1886 else
1887 DisassembleMachO(FileName, MachOOF, "__TEXT", "__text");
1889 if (IndirectSymbols)
1890 PrintIndirectSymbols(MachOOF, !NonVerbose);
1891 if (DataInCode)
1892 PrintDataInCodeTable(MachOOF, !NonVerbose);
1893 if (LinkOptHints)
1894 PrintLinkOptHints(MachOOF);
1895 if (Relocations)
1896 PrintRelocations(MachOOF, !NonVerbose);
1897 if (SectionHeaders)
1898 printSectionHeaders(MachOOF);
1899 if (SectionContents)
1900 printSectionContents(MachOOF);
1901 if (!FilterSections.empty())
1902 DumpSectionContents(FileName, MachOOF, !NonVerbose);
1903 if (InfoPlist)
1904 DumpInfoPlistSectionContents(FileName, MachOOF);
1905 if (DylibsUsed)
1906 PrintDylibs(MachOOF, false);
1907 if (DylibId)
1908 PrintDylibs(MachOOF, true);
1909 if (SymbolTable)
1910 printSymbolTable(MachOOF, ArchiveName, ArchitectureName);
1911 if (UnwindInfo)
1912 printMachOUnwindInfo(MachOOF);
1913 if (PrivateHeaders) {
1914 printMachOFileHeader(MachOOF);
1915 printMachOLoadCommands(MachOOF);
1917 if (FirstPrivateHeader)
1918 printMachOFileHeader(MachOOF);
1919 if (ObjcMetaData)
1920 printObjcMetaData(MachOOF, !NonVerbose);
1921 if (ExportsTrie)
1922 printExportsTrie(MachOOF);
1923 if (Rebase)
1924 printRebaseTable(MachOOF);
1925 if (Bind)
1926 printBindTable(MachOOF);
1927 if (LazyBind)
1928 printLazyBindTable(MachOOF);
1929 if (WeakBind)
1930 printWeakBindTable(MachOOF);
1932 if (DwarfDumpType != DIDT_Null) {
1933 std::unique_ptr<DIContext> DICtx = DWARFContext::create(*MachOOF);
1934 // Dump the complete DWARF structure.
1935 DIDumpOptions DumpOpts;
1936 DumpOpts.DumpType = DwarfDumpType;
1937 DICtx->dump(outs(), DumpOpts);
1941 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1942 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1943 outs() << " cputype (" << cputype << ")\n";
1944 outs() << " cpusubtype (" << cpusubtype << ")\n";
1947 // printCPUType() helps print_fat_headers by printing the cputype and
1948 // pusubtype (symbolically for the one's it knows about).
1949 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1950 switch (cputype) {
1951 case MachO::CPU_TYPE_I386:
1952 switch (cpusubtype) {
1953 case MachO::CPU_SUBTYPE_I386_ALL:
1954 outs() << " cputype CPU_TYPE_I386\n";
1955 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1956 break;
1957 default:
1958 printUnknownCPUType(cputype, cpusubtype);
1959 break;
1961 break;
1962 case MachO::CPU_TYPE_X86_64:
1963 switch (cpusubtype) {
1964 case MachO::CPU_SUBTYPE_X86_64_ALL:
1965 outs() << " cputype CPU_TYPE_X86_64\n";
1966 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1967 break;
1968 case MachO::CPU_SUBTYPE_X86_64_H:
1969 outs() << " cputype CPU_TYPE_X86_64\n";
1970 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1971 break;
1972 default:
1973 printUnknownCPUType(cputype, cpusubtype);
1974 break;
1976 break;
1977 case MachO::CPU_TYPE_ARM:
1978 switch (cpusubtype) {
1979 case MachO::CPU_SUBTYPE_ARM_ALL:
1980 outs() << " cputype CPU_TYPE_ARM\n";
1981 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1982 break;
1983 case MachO::CPU_SUBTYPE_ARM_V4T:
1984 outs() << " cputype CPU_TYPE_ARM\n";
1985 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1986 break;
1987 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1988 outs() << " cputype CPU_TYPE_ARM\n";
1989 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1990 break;
1991 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1992 outs() << " cputype CPU_TYPE_ARM\n";
1993 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1994 break;
1995 case MachO::CPU_SUBTYPE_ARM_V6:
1996 outs() << " cputype CPU_TYPE_ARM\n";
1997 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1998 break;
1999 case MachO::CPU_SUBTYPE_ARM_V6M:
2000 outs() << " cputype CPU_TYPE_ARM\n";
2001 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
2002 break;
2003 case MachO::CPU_SUBTYPE_ARM_V7:
2004 outs() << " cputype CPU_TYPE_ARM\n";
2005 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
2006 break;
2007 case MachO::CPU_SUBTYPE_ARM_V7EM:
2008 outs() << " cputype CPU_TYPE_ARM\n";
2009 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
2010 break;
2011 case MachO::CPU_SUBTYPE_ARM_V7K:
2012 outs() << " cputype CPU_TYPE_ARM\n";
2013 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
2014 break;
2015 case MachO::CPU_SUBTYPE_ARM_V7M:
2016 outs() << " cputype CPU_TYPE_ARM\n";
2017 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
2018 break;
2019 case MachO::CPU_SUBTYPE_ARM_V7S:
2020 outs() << " cputype CPU_TYPE_ARM\n";
2021 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
2022 break;
2023 default:
2024 printUnknownCPUType(cputype, cpusubtype);
2025 break;
2027 break;
2028 case MachO::CPU_TYPE_ARM64:
2029 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
2030 case MachO::CPU_SUBTYPE_ARM64_ALL:
2031 outs() << " cputype CPU_TYPE_ARM64\n";
2032 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
2033 break;
2034 case MachO::CPU_SUBTYPE_ARM64E:
2035 outs() << " cputype CPU_TYPE_ARM64\n";
2036 outs() << " cpusubtype CPU_SUBTYPE_ARM64E\n";
2037 break;
2038 default:
2039 printUnknownCPUType(cputype, cpusubtype);
2040 break;
2042 break;
2043 default:
2044 printUnknownCPUType(cputype, cpusubtype);
2045 break;
2049 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
2050 bool verbose) {
2051 outs() << "Fat headers\n";
2052 if (verbose) {
2053 if (UB->getMagic() == MachO::FAT_MAGIC)
2054 outs() << "fat_magic FAT_MAGIC\n";
2055 else // UB->getMagic() == MachO::FAT_MAGIC_64
2056 outs() << "fat_magic FAT_MAGIC_64\n";
2057 } else
2058 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
2060 uint32_t nfat_arch = UB->getNumberOfObjects();
2061 StringRef Buf = UB->getData();
2062 uint64_t size = Buf.size();
2063 uint64_t big_size = sizeof(struct MachO::fat_header) +
2064 nfat_arch * sizeof(struct MachO::fat_arch);
2065 outs() << "nfat_arch " << UB->getNumberOfObjects();
2066 if (nfat_arch == 0)
2067 outs() << " (malformed, contains zero architecture types)\n";
2068 else if (big_size > size)
2069 outs() << " (malformed, architectures past end of file)\n";
2070 else
2071 outs() << "\n";
2073 for (uint32_t i = 0; i < nfat_arch; ++i) {
2074 MachOUniversalBinary::ObjectForArch OFA(UB, i);
2075 uint32_t cputype = OFA.getCPUType();
2076 uint32_t cpusubtype = OFA.getCPUSubType();
2077 outs() << "architecture ";
2078 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
2079 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
2080 uint32_t other_cputype = other_OFA.getCPUType();
2081 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
2082 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
2083 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
2084 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
2085 outs() << "(illegal duplicate architecture) ";
2086 break;
2089 if (verbose) {
2090 outs() << OFA.getArchFlagName() << "\n";
2091 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
2092 } else {
2093 outs() << i << "\n";
2094 outs() << " cputype " << cputype << "\n";
2095 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
2096 << "\n";
2098 if (verbose &&
2099 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
2100 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
2101 else
2102 outs() << " capabilities "
2103 << format("0x%" PRIx32,
2104 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
2105 outs() << " offset " << OFA.getOffset();
2106 if (OFA.getOffset() > size)
2107 outs() << " (past end of file)";
2108 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
2109 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
2110 outs() << "\n";
2111 outs() << " size " << OFA.getSize();
2112 big_size = OFA.getOffset() + OFA.getSize();
2113 if (big_size > size)
2114 outs() << " (past end of file)";
2115 outs() << "\n";
2116 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
2117 << ")\n";
2121 static void printArchiveChild(StringRef Filename, const Archive::Child &C,
2122 bool verbose, bool print_offset,
2123 StringRef ArchitectureName = StringRef()) {
2124 if (print_offset)
2125 outs() << C.getChildOffset() << "\t";
2126 sys::fs::perms Mode =
2127 unwrapOrError(C.getAccessMode(), Filename, C, ArchitectureName);
2128 if (verbose) {
2129 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
2130 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
2131 outs() << "-";
2132 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
2133 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
2134 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
2135 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
2136 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
2137 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
2138 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
2139 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
2140 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
2141 } else {
2142 outs() << format("0%o ", Mode);
2145 outs() << format(
2146 "%3d/%-3d %5" PRId64 " ",
2147 unwrapOrError(C.getUID(), Filename, C, ArchitectureName),
2148 unwrapOrError(C.getGID(), Filename, C, ArchitectureName),
2149 unwrapOrError(C.getRawSize(), Filename, C, ArchitectureName));
2151 StringRef RawLastModified = C.getRawLastModified();
2152 if (verbose) {
2153 unsigned Seconds;
2154 if (RawLastModified.getAsInteger(10, Seconds))
2155 outs() << "(date: \"" << RawLastModified
2156 << "\" contains non-decimal chars) ";
2157 else {
2158 // Since cime(3) returns a 26 character string of the form:
2159 // "Sun Sep 16 01:03:52 1973\n\0"
2160 // just print 24 characters.
2161 time_t t = Seconds;
2162 outs() << format("%.24s ", ctime(&t));
2164 } else {
2165 outs() << RawLastModified << " ";
2168 if (verbose) {
2169 Expected<StringRef> NameOrErr = C.getName();
2170 if (!NameOrErr) {
2171 consumeError(NameOrErr.takeError());
2172 outs() << unwrapOrError(C.getRawName(), Filename, C, ArchitectureName)
2173 << "\n";
2174 } else {
2175 StringRef Name = NameOrErr.get();
2176 outs() << Name << "\n";
2178 } else {
2179 outs() << unwrapOrError(C.getRawName(), Filename, C, ArchitectureName)
2180 << "\n";
2184 static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
2185 bool print_offset,
2186 StringRef ArchitectureName = StringRef()) {
2187 Error Err = Error::success();
2188 for (const auto &C : A->children(Err, false))
2189 printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
2191 if (Err)
2192 report_error(std::move(Err), StringRef(), Filename, ArchitectureName);
2195 static bool ValidateArchFlags() {
2196 // Check for -arch all and verifiy the -arch flags are valid.
2197 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
2198 if (ArchFlags[i] == "all") {
2199 ArchAll = true;
2200 } else {
2201 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
2202 WithColor::error(errs(), "llvm-objdump")
2203 << "unknown architecture named '" + ArchFlags[i] +
2204 "'for the -arch option\n";
2205 return false;
2209 return true;
2212 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
2213 // -arch flags selecting just those slices as specified by them and also parses
2214 // archive files. Then for each individual Mach-O file ProcessMachO() is
2215 // called to process the file based on the command line options.
2216 void parseInputMachO(StringRef Filename) {
2217 if (!ValidateArchFlags())
2218 return;
2220 // Attempt to open the binary.
2221 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
2222 if (!BinaryOrErr) {
2223 if (Error E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
2224 report_error(std::move(E), Filename);
2225 else
2226 outs() << Filename << ": is not an object file\n";
2227 return;
2229 Binary &Bin = *BinaryOrErr.get().getBinary();
2231 if (Archive *A = dyn_cast<Archive>(&Bin)) {
2232 outs() << "Archive : " << Filename << "\n";
2233 if (ArchiveHeaders)
2234 printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
2236 Error Err = Error::success();
2237 for (auto &C : A->children(Err)) {
2238 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2239 if (!ChildOrErr) {
2240 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2241 report_error(std::move(E), Filename, C);
2242 continue;
2244 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
2245 if (!checkMachOAndArchFlags(O, Filename))
2246 return;
2247 ProcessMachO(Filename, O, O->getFileName());
2250 if (Err)
2251 report_error(std::move(Err), Filename);
2252 return;
2254 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
2255 parseInputMachO(UB);
2256 return;
2258 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
2259 if (!checkMachOAndArchFlags(O, Filename))
2260 return;
2261 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O))
2262 ProcessMachO(Filename, MachOOF);
2263 else
2264 WithColor::error(errs(), "llvm-objdump")
2265 << Filename << "': "
2266 << "object is not a Mach-O file type.\n";
2267 return;
2269 llvm_unreachable("Input object can't be invalid at this point");
2272 void parseInputMachO(MachOUniversalBinary *UB) {
2273 if (!ValidateArchFlags())
2274 return;
2276 auto Filename = UB->getFileName();
2278 if (UniversalHeaders)
2279 printMachOUniversalHeaders(UB, !NonVerbose);
2281 // If we have a list of architecture flags specified dump only those.
2282 if (!ArchAll && !ArchFlags.empty()) {
2283 // Look for a slice in the universal binary that matches each ArchFlag.
2284 bool ArchFound;
2285 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
2286 ArchFound = false;
2287 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2288 E = UB->end_objects();
2289 I != E; ++I) {
2290 if (ArchFlags[i] == I->getArchFlagName()) {
2291 ArchFound = true;
2292 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
2293 I->getAsObjectFile();
2294 std::string ArchitectureName = "";
2295 if (ArchFlags.size() > 1)
2296 ArchitectureName = I->getArchFlagName();
2297 if (ObjOrErr) {
2298 ObjectFile &O = *ObjOrErr.get();
2299 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
2300 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
2301 } else if (Error E = isNotObjectErrorInvalidFileType(
2302 ObjOrErr.takeError())) {
2303 report_error(std::move(E), Filename, StringRef(), ArchitectureName);
2304 continue;
2305 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
2306 I->getAsArchive()) {
2307 std::unique_ptr<Archive> &A = *AOrErr;
2308 outs() << "Archive : " << Filename;
2309 if (!ArchitectureName.empty())
2310 outs() << " (architecture " << ArchitectureName << ")";
2311 outs() << "\n";
2312 if (ArchiveHeaders)
2313 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2314 ArchiveMemberOffsets, ArchitectureName);
2315 Error Err = Error::success();
2316 for (auto &C : A->children(Err)) {
2317 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2318 if (!ChildOrErr) {
2319 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2320 report_error(std::move(E), Filename, C, ArchitectureName);
2321 continue;
2323 if (MachOObjectFile *O =
2324 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
2325 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
2327 if (Err)
2328 report_error(std::move(Err), Filename);
2329 } else {
2330 consumeError(AOrErr.takeError());
2331 error("Mach-O universal file: " + Filename + " for " +
2332 "architecture " + StringRef(I->getArchFlagName()) +
2333 " is not a Mach-O file or an archive file");
2337 if (!ArchFound) {
2338 WithColor::error(errs(), "llvm-objdump")
2339 << "file: " + Filename + " does not contain "
2340 << "architecture: " + ArchFlags[i] + "\n";
2341 return;
2344 return;
2346 // No architecture flags were specified so if this contains a slice that
2347 // matches the host architecture dump only that.
2348 if (!ArchAll) {
2349 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2350 E = UB->end_objects();
2351 I != E; ++I) {
2352 if (MachOObjectFile::getHostArch().getArchName() ==
2353 I->getArchFlagName()) {
2354 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
2355 std::string ArchiveName;
2356 ArchiveName.clear();
2357 if (ObjOrErr) {
2358 ObjectFile &O = *ObjOrErr.get();
2359 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
2360 ProcessMachO(Filename, MachOOF);
2361 } else if (Error E =
2362 isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
2363 report_error(std::move(E), Filename);
2364 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
2365 I->getAsArchive()) {
2366 std::unique_ptr<Archive> &A = *AOrErr;
2367 outs() << "Archive : " << Filename << "\n";
2368 if (ArchiveHeaders)
2369 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2370 ArchiveMemberOffsets);
2371 Error Err = Error::success();
2372 for (auto &C : A->children(Err)) {
2373 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2374 if (!ChildOrErr) {
2375 if (Error E =
2376 isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2377 report_error(std::move(E), Filename, C);
2378 continue;
2380 if (MachOObjectFile *O =
2381 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
2382 ProcessMachO(Filename, O, O->getFileName());
2384 if (Err)
2385 report_error(std::move(Err), Filename);
2386 } else {
2387 consumeError(AOrErr.takeError());
2388 error("Mach-O universal file: " + Filename + " for architecture " +
2389 StringRef(I->getArchFlagName()) +
2390 " is not a Mach-O file or an archive file");
2392 return;
2396 // Either all architectures have been specified or none have been specified
2397 // and this does not contain the host architecture so dump all the slices.
2398 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
2399 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2400 E = UB->end_objects();
2401 I != E; ++I) {
2402 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
2403 std::string ArchitectureName = "";
2404 if (moreThanOneArch)
2405 ArchitectureName = I->getArchFlagName();
2406 if (ObjOrErr) {
2407 ObjectFile &Obj = *ObjOrErr.get();
2408 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
2409 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
2410 } else if (Error E =
2411 isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
2412 report_error(std::move(E), StringRef(), Filename, ArchitectureName);
2413 } else if (Expected<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
2414 std::unique_ptr<Archive> &A = *AOrErr;
2415 outs() << "Archive : " << Filename;
2416 if (!ArchitectureName.empty())
2417 outs() << " (architecture " << ArchitectureName << ")";
2418 outs() << "\n";
2419 if (ArchiveHeaders)
2420 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2421 ArchiveMemberOffsets, ArchitectureName);
2422 Error Err = Error::success();
2423 for (auto &C : A->children(Err)) {
2424 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2425 if (!ChildOrErr) {
2426 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2427 report_error(std::move(E), Filename, C, ArchitectureName);
2428 continue;
2430 if (MachOObjectFile *O =
2431 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
2432 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
2433 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
2434 ArchitectureName);
2437 if (Err)
2438 report_error(std::move(Err), Filename);
2439 } else {
2440 consumeError(AOrErr.takeError());
2441 error("Mach-O universal file: " + Filename + " for architecture " +
2442 StringRef(I->getArchFlagName()) +
2443 " is not a Mach-O file or an archive file");
2448 // The block of info used by the Symbolizer call backs.
2449 struct DisassembleInfo {
2450 DisassembleInfo(MachOObjectFile *O, SymbolAddressMap *AddrMap,
2451 std::vector<SectionRef> *Sections, bool verbose)
2452 : verbose(verbose), O(O), AddrMap(AddrMap), Sections(Sections) {}
2453 bool verbose;
2454 MachOObjectFile *O;
2455 SectionRef S;
2456 SymbolAddressMap *AddrMap;
2457 std::vector<SectionRef> *Sections;
2458 const char *class_name = nullptr;
2459 const char *selector_name = nullptr;
2460 std::unique_ptr<char[]> method = nullptr;
2461 char *demangled_name = nullptr;
2462 uint64_t adrp_addr = 0;
2463 uint32_t adrp_inst = 0;
2464 std::unique_ptr<SymbolAddressMap> bindtable;
2465 uint32_t depth = 0;
2468 // SymbolizerGetOpInfo() is the operand information call back function.
2469 // This is called to get the symbolic information for operand(s) of an
2470 // instruction when it is being done. This routine does this from
2471 // the relocation information, symbol table, etc. That block of information
2472 // is a pointer to the struct DisassembleInfo that was passed when the
2473 // disassembler context was created and passed to back to here when
2474 // called back by the disassembler for instruction operands that could have
2475 // relocation information. The address of the instruction containing operand is
2476 // at the Pc parameter. The immediate value the operand has is passed in
2477 // op_info->Value and is at Offset past the start of the instruction and has a
2478 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
2479 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
2480 // names and addends of the symbolic expression to add for the operand. The
2481 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
2482 // information is returned then this function returns 1 else it returns 0.
2483 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
2484 uint64_t Size, int TagType, void *TagBuf) {
2485 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
2486 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
2487 uint64_t value = op_info->Value;
2489 // Make sure all fields returned are zero if we don't set them.
2490 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
2491 op_info->Value = value;
2493 // If the TagType is not the value 1 which it code knows about or if no
2494 // verbose symbolic information is wanted then just return 0, indicating no
2495 // information is being returned.
2496 if (TagType != 1 || !info->verbose)
2497 return 0;
2499 unsigned int Arch = info->O->getArch();
2500 if (Arch == Triple::x86) {
2501 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
2502 return 0;
2503 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2504 // TODO:
2505 // Search the external relocation entries of a fully linked image
2506 // (if any) for an entry that matches this segment offset.
2507 // uint32_t seg_offset = (Pc + Offset);
2508 return 0;
2510 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2511 // for an entry for this section offset.
2512 uint32_t sect_addr = info->S.getAddress();
2513 uint32_t sect_offset = (Pc + Offset) - sect_addr;
2514 bool reloc_found = false;
2515 DataRefImpl Rel;
2516 MachO::any_relocation_info RE;
2517 bool isExtern = false;
2518 SymbolRef Symbol;
2519 bool r_scattered = false;
2520 uint32_t r_value, pair_r_value, r_type;
2521 for (const RelocationRef &Reloc : info->S.relocations()) {
2522 uint64_t RelocOffset = Reloc.getOffset();
2523 if (RelocOffset == sect_offset) {
2524 Rel = Reloc.getRawDataRefImpl();
2525 RE = info->O->getRelocation(Rel);
2526 r_type = info->O->getAnyRelocationType(RE);
2527 r_scattered = info->O->isRelocationScattered(RE);
2528 if (r_scattered) {
2529 r_value = info->O->getScatteredRelocationValue(RE);
2530 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
2531 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
2532 DataRefImpl RelNext = Rel;
2533 info->O->moveRelocationNext(RelNext);
2534 MachO::any_relocation_info RENext;
2535 RENext = info->O->getRelocation(RelNext);
2536 if (info->O->isRelocationScattered(RENext))
2537 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2538 else
2539 return 0;
2541 } else {
2542 isExtern = info->O->getPlainRelocationExternal(RE);
2543 if (isExtern) {
2544 symbol_iterator RelocSym = Reloc.getSymbol();
2545 Symbol = *RelocSym;
2548 reloc_found = true;
2549 break;
2552 if (reloc_found && isExtern) {
2553 op_info->AddSymbol.Present = 1;
2554 op_info->AddSymbol.Name =
2555 unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2556 // For i386 extern relocation entries the value in the instruction is
2557 // the offset from the symbol, and value is already set in op_info->Value.
2558 return 1;
2560 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
2561 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
2562 const char *add = GuessSymbolName(r_value, info->AddrMap);
2563 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2564 uint32_t offset = value - (r_value - pair_r_value);
2565 op_info->AddSymbol.Present = 1;
2566 if (add != nullptr)
2567 op_info->AddSymbol.Name = add;
2568 else
2569 op_info->AddSymbol.Value = r_value;
2570 op_info->SubtractSymbol.Present = 1;
2571 if (sub != nullptr)
2572 op_info->SubtractSymbol.Name = sub;
2573 else
2574 op_info->SubtractSymbol.Value = pair_r_value;
2575 op_info->Value = offset;
2576 return 1;
2578 return 0;
2580 if (Arch == Triple::x86_64) {
2581 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
2582 return 0;
2583 // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
2584 // relocation entries of a linked image (if any) for an entry that matches
2585 // this segment offset.
2586 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2587 uint64_t seg_offset = Pc + Offset;
2588 bool reloc_found = false;
2589 DataRefImpl Rel;
2590 MachO::any_relocation_info RE;
2591 bool isExtern = false;
2592 SymbolRef Symbol;
2593 for (const RelocationRef &Reloc : info->O->external_relocations()) {
2594 uint64_t RelocOffset = Reloc.getOffset();
2595 if (RelocOffset == seg_offset) {
2596 Rel = Reloc.getRawDataRefImpl();
2597 RE = info->O->getRelocation(Rel);
2598 // external relocation entries should always be external.
2599 isExtern = info->O->getPlainRelocationExternal(RE);
2600 if (isExtern) {
2601 symbol_iterator RelocSym = Reloc.getSymbol();
2602 Symbol = *RelocSym;
2604 reloc_found = true;
2605 break;
2608 if (reloc_found && isExtern) {
2609 // The Value passed in will be adjusted by the Pc if the instruction
2610 // adds the Pc. But for x86_64 external relocation entries the Value
2611 // is the offset from the external symbol.
2612 if (info->O->getAnyRelocationPCRel(RE))
2613 op_info->Value -= Pc + Offset + Size;
2614 const char *name =
2615 unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2616 op_info->AddSymbol.Present = 1;
2617 op_info->AddSymbol.Name = name;
2618 return 1;
2620 return 0;
2622 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2623 // for an entry for this section offset.
2624 uint64_t sect_addr = info->S.getAddress();
2625 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2626 bool reloc_found = false;
2627 DataRefImpl Rel;
2628 MachO::any_relocation_info RE;
2629 bool isExtern = false;
2630 SymbolRef Symbol;
2631 for (const RelocationRef &Reloc : info->S.relocations()) {
2632 uint64_t RelocOffset = Reloc.getOffset();
2633 if (RelocOffset == sect_offset) {
2634 Rel = Reloc.getRawDataRefImpl();
2635 RE = info->O->getRelocation(Rel);
2636 // NOTE: Scattered relocations don't exist on x86_64.
2637 isExtern = info->O->getPlainRelocationExternal(RE);
2638 if (isExtern) {
2639 symbol_iterator RelocSym = Reloc.getSymbol();
2640 Symbol = *RelocSym;
2642 reloc_found = true;
2643 break;
2646 if (reloc_found && isExtern) {
2647 // The Value passed in will be adjusted by the Pc if the instruction
2648 // adds the Pc. But for x86_64 external relocation entries the Value
2649 // is the offset from the external symbol.
2650 if (info->O->getAnyRelocationPCRel(RE))
2651 op_info->Value -= Pc + Offset + Size;
2652 const char *name =
2653 unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2654 unsigned Type = info->O->getAnyRelocationType(RE);
2655 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
2656 DataRefImpl RelNext = Rel;
2657 info->O->moveRelocationNext(RelNext);
2658 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2659 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
2660 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
2661 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
2662 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
2663 op_info->SubtractSymbol.Present = 1;
2664 op_info->SubtractSymbol.Name = name;
2665 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
2666 Symbol = *RelocSymNext;
2667 name = unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2670 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2671 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2672 op_info->AddSymbol.Present = 1;
2673 op_info->AddSymbol.Name = name;
2674 return 1;
2676 return 0;
2678 if (Arch == Triple::arm) {
2679 if (Offset != 0 || (Size != 4 && Size != 2))
2680 return 0;
2681 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2682 // TODO:
2683 // Search the external relocation entries of a fully linked image
2684 // (if any) for an entry that matches this segment offset.
2685 // uint32_t seg_offset = (Pc + Offset);
2686 return 0;
2688 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2689 // for an entry for this section offset.
2690 uint32_t sect_addr = info->S.getAddress();
2691 uint32_t sect_offset = (Pc + Offset) - sect_addr;
2692 DataRefImpl Rel;
2693 MachO::any_relocation_info RE;
2694 bool isExtern = false;
2695 SymbolRef Symbol;
2696 bool r_scattered = false;
2697 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
2698 auto Reloc =
2699 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2700 uint64_t RelocOffset = Reloc.getOffset();
2701 return RelocOffset == sect_offset;
2704 if (Reloc == info->S.relocations().end())
2705 return 0;
2707 Rel = Reloc->getRawDataRefImpl();
2708 RE = info->O->getRelocation(Rel);
2709 r_length = info->O->getAnyRelocationLength(RE);
2710 r_scattered = info->O->isRelocationScattered(RE);
2711 if (r_scattered) {
2712 r_value = info->O->getScatteredRelocationValue(RE);
2713 r_type = info->O->getScatteredRelocationType(RE);
2714 } else {
2715 r_type = info->O->getAnyRelocationType(RE);
2716 isExtern = info->O->getPlainRelocationExternal(RE);
2717 if (isExtern) {
2718 symbol_iterator RelocSym = Reloc->getSymbol();
2719 Symbol = *RelocSym;
2722 if (r_type == MachO::ARM_RELOC_HALF ||
2723 r_type == MachO::ARM_RELOC_SECTDIFF ||
2724 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2725 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2726 DataRefImpl RelNext = Rel;
2727 info->O->moveRelocationNext(RelNext);
2728 MachO::any_relocation_info RENext;
2729 RENext = info->O->getRelocation(RelNext);
2730 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
2731 if (info->O->isRelocationScattered(RENext))
2732 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2735 if (isExtern) {
2736 const char *name =
2737 unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2738 op_info->AddSymbol.Present = 1;
2739 op_info->AddSymbol.Name = name;
2740 switch (r_type) {
2741 case MachO::ARM_RELOC_HALF:
2742 if ((r_length & 0x1) == 1) {
2743 op_info->Value = value << 16 | other_half;
2744 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2745 } else {
2746 op_info->Value = other_half << 16 | value;
2747 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2749 break;
2750 default:
2751 break;
2753 return 1;
2755 // If we have a branch that is not an external relocation entry then
2756 // return 0 so the code in tryAddingSymbolicOperand() can use the
2757 // SymbolLookUp call back with the branch target address to look up the
2758 // symbol and possibility add an annotation for a symbol stub.
2759 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2760 r_type == MachO::ARM_THUMB_RELOC_BR22))
2761 return 0;
2763 uint32_t offset = 0;
2764 if (r_type == MachO::ARM_RELOC_HALF ||
2765 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2766 if ((r_length & 0x1) == 1)
2767 value = value << 16 | other_half;
2768 else
2769 value = other_half << 16 | value;
2771 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2772 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2773 offset = value - r_value;
2774 value = r_value;
2777 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2778 if ((r_length & 0x1) == 1)
2779 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2780 else
2781 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2782 const char *add = GuessSymbolName(r_value, info->AddrMap);
2783 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2784 int32_t offset = value - (r_value - pair_r_value);
2785 op_info->AddSymbol.Present = 1;
2786 if (add != nullptr)
2787 op_info->AddSymbol.Name = add;
2788 else
2789 op_info->AddSymbol.Value = r_value;
2790 op_info->SubtractSymbol.Present = 1;
2791 if (sub != nullptr)
2792 op_info->SubtractSymbol.Name = sub;
2793 else
2794 op_info->SubtractSymbol.Value = pair_r_value;
2795 op_info->Value = offset;
2796 return 1;
2799 op_info->AddSymbol.Present = 1;
2800 op_info->Value = offset;
2801 if (r_type == MachO::ARM_RELOC_HALF) {
2802 if ((r_length & 0x1) == 1)
2803 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2804 else
2805 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2807 const char *add = GuessSymbolName(value, info->AddrMap);
2808 if (add != nullptr) {
2809 op_info->AddSymbol.Name = add;
2810 return 1;
2812 op_info->AddSymbol.Value = value;
2813 return 1;
2815 if (Arch == Triple::aarch64) {
2816 if (Offset != 0 || Size != 4)
2817 return 0;
2818 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2819 // TODO:
2820 // Search the external relocation entries of a fully linked image
2821 // (if any) for an entry that matches this segment offset.
2822 // uint64_t seg_offset = (Pc + Offset);
2823 return 0;
2825 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2826 // for an entry for this section offset.
2827 uint64_t sect_addr = info->S.getAddress();
2828 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2829 auto Reloc =
2830 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2831 uint64_t RelocOffset = Reloc.getOffset();
2832 return RelocOffset == sect_offset;
2835 if (Reloc == info->S.relocations().end())
2836 return 0;
2838 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2839 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2840 uint32_t r_type = info->O->getAnyRelocationType(RE);
2841 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2842 DataRefImpl RelNext = Rel;
2843 info->O->moveRelocationNext(RelNext);
2844 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2845 if (value == 0) {
2846 value = info->O->getPlainRelocationSymbolNum(RENext);
2847 op_info->Value = value;
2850 // NOTE: Scattered relocations don't exist on arm64.
2851 if (!info->O->getPlainRelocationExternal(RE))
2852 return 0;
2853 const char *name =
2854 unwrapOrError(Reloc->getSymbol()->getName(), info->O->getFileName())
2855 .data();
2856 op_info->AddSymbol.Present = 1;
2857 op_info->AddSymbol.Name = name;
2859 switch (r_type) {
2860 case MachO::ARM64_RELOC_PAGE21:
2861 /* @page */
2862 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2863 break;
2864 case MachO::ARM64_RELOC_PAGEOFF12:
2865 /* @pageoff */
2866 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2867 break;
2868 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2869 /* @gotpage */
2870 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2871 break;
2872 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2873 /* @gotpageoff */
2874 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2875 break;
2876 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2877 /* @tvlppage is not implemented in llvm-mc */
2878 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2879 break;
2880 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2881 /* @tvlppageoff is not implemented in llvm-mc */
2882 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2883 break;
2884 default:
2885 case MachO::ARM64_RELOC_BRANCH26:
2886 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2887 break;
2889 return 1;
2891 return 0;
2894 // GuessCstringPointer is passed the address of what might be a pointer to a
2895 // literal string in a cstring section. If that address is in a cstring section
2896 // it returns a pointer to that string. Else it returns nullptr.
2897 static const char *GuessCstringPointer(uint64_t ReferenceValue,
2898 struct DisassembleInfo *info) {
2899 for (const auto &Load : info->O->load_commands()) {
2900 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2901 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2902 for (unsigned J = 0; J < Seg.nsects; ++J) {
2903 MachO::section_64 Sec = info->O->getSection64(Load, J);
2904 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2905 if (section_type == MachO::S_CSTRING_LITERALS &&
2906 ReferenceValue >= Sec.addr &&
2907 ReferenceValue < Sec.addr + Sec.size) {
2908 uint64_t sect_offset = ReferenceValue - Sec.addr;
2909 uint64_t object_offset = Sec.offset + sect_offset;
2910 StringRef MachOContents = info->O->getData();
2911 uint64_t object_size = MachOContents.size();
2912 const char *object_addr = (const char *)MachOContents.data();
2913 if (object_offset < object_size) {
2914 const char *name = object_addr + object_offset;
2915 return name;
2916 } else {
2917 return nullptr;
2921 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2922 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2923 for (unsigned J = 0; J < Seg.nsects; ++J) {
2924 MachO::section Sec = info->O->getSection(Load, J);
2925 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2926 if (section_type == MachO::S_CSTRING_LITERALS &&
2927 ReferenceValue >= Sec.addr &&
2928 ReferenceValue < Sec.addr + Sec.size) {
2929 uint64_t sect_offset = ReferenceValue - Sec.addr;
2930 uint64_t object_offset = Sec.offset + sect_offset;
2931 StringRef MachOContents = info->O->getData();
2932 uint64_t object_size = MachOContents.size();
2933 const char *object_addr = (const char *)MachOContents.data();
2934 if (object_offset < object_size) {
2935 const char *name = object_addr + object_offset;
2936 return name;
2937 } else {
2938 return nullptr;
2944 return nullptr;
2947 // GuessIndirectSymbol returns the name of the indirect symbol for the
2948 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2949 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
2950 // symbol name being referenced by the stub or pointer.
2951 static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2952 struct DisassembleInfo *info) {
2953 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2954 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
2955 for (const auto &Load : info->O->load_commands()) {
2956 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2957 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2958 for (unsigned J = 0; J < Seg.nsects; ++J) {
2959 MachO::section_64 Sec = info->O->getSection64(Load, J);
2960 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2961 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2962 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2963 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2964 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2965 section_type == MachO::S_SYMBOL_STUBS) &&
2966 ReferenceValue >= Sec.addr &&
2967 ReferenceValue < Sec.addr + Sec.size) {
2968 uint32_t stride;
2969 if (section_type == MachO::S_SYMBOL_STUBS)
2970 stride = Sec.reserved2;
2971 else
2972 stride = 8;
2973 if (stride == 0)
2974 return nullptr;
2975 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2976 if (index < Dysymtab.nindirectsyms) {
2977 uint32_t indirect_symbol =
2978 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2979 if (indirect_symbol < Symtab.nsyms) {
2980 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2981 return unwrapOrError(Sym->getName(), info->O->getFileName())
2982 .data();
2987 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2988 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2989 for (unsigned J = 0; J < Seg.nsects; ++J) {
2990 MachO::section Sec = info->O->getSection(Load, J);
2991 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2992 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2993 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2994 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2995 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2996 section_type == MachO::S_SYMBOL_STUBS) &&
2997 ReferenceValue >= Sec.addr &&
2998 ReferenceValue < Sec.addr + Sec.size) {
2999 uint32_t stride;
3000 if (section_type == MachO::S_SYMBOL_STUBS)
3001 stride = Sec.reserved2;
3002 else
3003 stride = 4;
3004 if (stride == 0)
3005 return nullptr;
3006 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
3007 if (index < Dysymtab.nindirectsyms) {
3008 uint32_t indirect_symbol =
3009 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
3010 if (indirect_symbol < Symtab.nsyms) {
3011 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
3012 return unwrapOrError(Sym->getName(), info->O->getFileName())
3013 .data();
3020 return nullptr;
3023 // method_reference() is called passing it the ReferenceName that might be
3024 // a reference it to an Objective-C method call. If so then it allocates and
3025 // assembles a method call string with the values last seen and saved in
3026 // the DisassembleInfo's class_name and selector_name fields. This is saved
3027 // into the method field of the info and any previous string is free'ed.
3028 // Then the class_name field in the info is set to nullptr. The method call
3029 // string is set into ReferenceName and ReferenceType is set to
3030 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
3031 // then both ReferenceType and ReferenceName are left unchanged.
3032 static void method_reference(struct DisassembleInfo *info,
3033 uint64_t *ReferenceType,
3034 const char **ReferenceName) {
3035 unsigned int Arch = info->O->getArch();
3036 if (*ReferenceName != nullptr) {
3037 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
3038 if (info->selector_name != nullptr) {
3039 if (info->class_name != nullptr) {
3040 info->method = llvm::make_unique<char[]>(
3041 5 + strlen(info->class_name) + strlen(info->selector_name));
3042 char *method = info->method.get();
3043 if (method != nullptr) {
3044 strcpy(method, "+[");
3045 strcat(method, info->class_name);
3046 strcat(method, " ");
3047 strcat(method, info->selector_name);
3048 strcat(method, "]");
3049 *ReferenceName = method;
3050 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
3052 } else {
3053 info->method =
3054 llvm::make_unique<char[]>(9 + strlen(info->selector_name));
3055 char *method = info->method.get();
3056 if (method != nullptr) {
3057 if (Arch == Triple::x86_64)
3058 strcpy(method, "-[%rdi ");
3059 else if (Arch == Triple::aarch64)
3060 strcpy(method, "-[x0 ");
3061 else
3062 strcpy(method, "-[r? ");
3063 strcat(method, info->selector_name);
3064 strcat(method, "]");
3065 *ReferenceName = method;
3066 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
3069 info->class_name = nullptr;
3071 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
3072 if (info->selector_name != nullptr) {
3073 info->method =
3074 llvm::make_unique<char[]>(17 + strlen(info->selector_name));
3075 char *method = info->method.get();
3076 if (method != nullptr) {
3077 if (Arch == Triple::x86_64)
3078 strcpy(method, "-[[%rdi super] ");
3079 else if (Arch == Triple::aarch64)
3080 strcpy(method, "-[[x0 super] ");
3081 else
3082 strcpy(method, "-[[r? super] ");
3083 strcat(method, info->selector_name);
3084 strcat(method, "]");
3085 *ReferenceName = method;
3086 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
3088 info->class_name = nullptr;
3094 // GuessPointerPointer() is passed the address of what might be a pointer to
3095 // a reference to an Objective-C class, selector, message ref or cfstring.
3096 // If so the value of the pointer is returned and one of the booleans are set
3097 // to true. If not zero is returned and all the booleans are set to false.
3098 static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
3099 struct DisassembleInfo *info,
3100 bool &classref, bool &selref, bool &msgref,
3101 bool &cfstring) {
3102 classref = false;
3103 selref = false;
3104 msgref = false;
3105 cfstring = false;
3106 for (const auto &Load : info->O->load_commands()) {
3107 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
3108 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
3109 for (unsigned J = 0; J < Seg.nsects; ++J) {
3110 MachO::section_64 Sec = info->O->getSection64(Load, J);
3111 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
3112 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
3113 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
3114 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
3115 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
3116 ReferenceValue >= Sec.addr &&
3117 ReferenceValue < Sec.addr + Sec.size) {
3118 uint64_t sect_offset = ReferenceValue - Sec.addr;
3119 uint64_t object_offset = Sec.offset + sect_offset;
3120 StringRef MachOContents = info->O->getData();
3121 uint64_t object_size = MachOContents.size();
3122 const char *object_addr = (const char *)MachOContents.data();
3123 if (object_offset < object_size) {
3124 uint64_t pointer_value;
3125 memcpy(&pointer_value, object_addr + object_offset,
3126 sizeof(uint64_t));
3127 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3128 sys::swapByteOrder(pointer_value);
3129 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
3130 selref = true;
3131 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
3132 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
3133 classref = true;
3134 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
3135 ReferenceValue + 8 < Sec.addr + Sec.size) {
3136 msgref = true;
3137 memcpy(&pointer_value, object_addr + object_offset + 8,
3138 sizeof(uint64_t));
3139 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3140 sys::swapByteOrder(pointer_value);
3141 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
3142 cfstring = true;
3143 return pointer_value;
3144 } else {
3145 return 0;
3150 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
3152 return 0;
3155 // get_pointer_64 returns a pointer to the bytes in the object file at the
3156 // Address from a section in the Mach-O file. And indirectly returns the
3157 // offset into the section, number of bytes left in the section past the offset
3158 // and which section is was being referenced. If the Address is not in a
3159 // section nullptr is returned.
3160 static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
3161 uint32_t &left, SectionRef &S,
3162 DisassembleInfo *info,
3163 bool objc_only = false) {
3164 offset = 0;
3165 left = 0;
3166 S = SectionRef();
3167 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
3168 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
3169 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
3170 if (SectSize == 0)
3171 continue;
3172 if (objc_only) {
3173 StringRef SectName;
3174 ((*(info->Sections))[SectIdx]).getName(SectName);
3175 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
3176 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
3177 if (SegName != "__OBJC" && SectName != "__cstring")
3178 continue;
3180 if (Address >= SectAddress && Address < SectAddress + SectSize) {
3181 S = (*(info->Sections))[SectIdx];
3182 offset = Address - SectAddress;
3183 left = SectSize - offset;
3184 StringRef SectContents;
3185 ((*(info->Sections))[SectIdx]).getContents(SectContents);
3186 return SectContents.data() + offset;
3189 return nullptr;
3192 static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
3193 uint32_t &left, SectionRef &S,
3194 DisassembleInfo *info,
3195 bool objc_only = false) {
3196 return get_pointer_64(Address, offset, left, S, info, objc_only);
3199 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
3200 // the symbol indirectly through n_value. Based on the relocation information
3201 // for the specified section offset in the specified section reference.
3202 // If no relocation information is found and a non-zero ReferenceValue for the
3203 // symbol is passed, look up that address in the info's AddrMap.
3204 static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
3205 DisassembleInfo *info, uint64_t &n_value,
3206 uint64_t ReferenceValue = 0) {
3207 n_value = 0;
3208 if (!info->verbose)
3209 return nullptr;
3211 // See if there is an external relocation entry at the sect_offset.
3212 bool reloc_found = false;
3213 DataRefImpl Rel;
3214 MachO::any_relocation_info RE;
3215 bool isExtern = false;
3216 SymbolRef Symbol;
3217 for (const RelocationRef &Reloc : S.relocations()) {
3218 uint64_t RelocOffset = Reloc.getOffset();
3219 if (RelocOffset == sect_offset) {
3220 Rel = Reloc.getRawDataRefImpl();
3221 RE = info->O->getRelocation(Rel);
3222 if (info->O->isRelocationScattered(RE))
3223 continue;
3224 isExtern = info->O->getPlainRelocationExternal(RE);
3225 if (isExtern) {
3226 symbol_iterator RelocSym = Reloc.getSymbol();
3227 Symbol = *RelocSym;
3229 reloc_found = true;
3230 break;
3233 // If there is an external relocation entry for a symbol in this section
3234 // at this section_offset then use that symbol's value for the n_value
3235 // and return its name.
3236 const char *SymbolName = nullptr;
3237 if (reloc_found && isExtern) {
3238 n_value = Symbol.getValue();
3239 StringRef Name = unwrapOrError(Symbol.getName(), info->O->getFileName());
3240 if (!Name.empty()) {
3241 SymbolName = Name.data();
3242 return SymbolName;
3246 // TODO: For fully linked images, look through the external relocation
3247 // entries off the dynamic symtab command. For these the r_offset is from the
3248 // start of the first writeable segment in the Mach-O file. So the offset
3249 // to this section from that segment is passed to this routine by the caller,
3250 // as the database_offset. Which is the difference of the section's starting
3251 // address and the first writable segment.
3253 // NOTE: need add passing the database_offset to this routine.
3255 // We did not find an external relocation entry so look up the ReferenceValue
3256 // as an address of a symbol and if found return that symbol's name.
3257 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
3259 return SymbolName;
3262 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
3263 DisassembleInfo *info,
3264 uint32_t ReferenceValue) {
3265 uint64_t n_value64;
3266 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
3269 // These are structs in the Objective-C meta data and read to produce the
3270 // comments for disassembly. While these are part of the ABI they are no
3271 // public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
3272 // .
3274 // The cfstring object in a 64-bit Mach-O file.
3275 struct cfstring64_t {
3276 uint64_t isa; // class64_t * (64-bit pointer)
3277 uint64_t flags; // flag bits
3278 uint64_t characters; // char * (64-bit pointer)
3279 uint64_t length; // number of non-NULL characters in above
3282 // The class object in a 64-bit Mach-O file.
3283 struct class64_t {
3284 uint64_t isa; // class64_t * (64-bit pointer)
3285 uint64_t superclass; // class64_t * (64-bit pointer)
3286 uint64_t cache; // Cache (64-bit pointer)
3287 uint64_t vtable; // IMP * (64-bit pointer)
3288 uint64_t data; // class_ro64_t * (64-bit pointer)
3291 struct class32_t {
3292 uint32_t isa; /* class32_t * (32-bit pointer) */
3293 uint32_t superclass; /* class32_t * (32-bit pointer) */
3294 uint32_t cache; /* Cache (32-bit pointer) */
3295 uint32_t vtable; /* IMP * (32-bit pointer) */
3296 uint32_t data; /* class_ro32_t * (32-bit pointer) */
3299 struct class_ro64_t {
3300 uint32_t flags;
3301 uint32_t instanceStart;
3302 uint32_t instanceSize;
3303 uint32_t reserved;
3304 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
3305 uint64_t name; // const char * (64-bit pointer)
3306 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
3307 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
3308 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
3309 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
3310 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
3313 struct class_ro32_t {
3314 uint32_t flags;
3315 uint32_t instanceStart;
3316 uint32_t instanceSize;
3317 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
3318 uint32_t name; /* const char * (32-bit pointer) */
3319 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
3320 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
3321 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
3322 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
3323 uint32_t baseProperties; /* const struct objc_property_list *
3324 (32-bit pointer) */
3327 /* Values for class_ro{64,32}_t->flags */
3328 #define RO_META (1 << 0)
3329 #define RO_ROOT (1 << 1)
3330 #define RO_HAS_CXX_STRUCTORS (1 << 2)
3332 struct method_list64_t {
3333 uint32_t entsize;
3334 uint32_t count;
3335 /* struct method64_t first; These structures follow inline */
3338 struct method_list32_t {
3339 uint32_t entsize;
3340 uint32_t count;
3341 /* struct method32_t first; These structures follow inline */
3344 struct method64_t {
3345 uint64_t name; /* SEL (64-bit pointer) */
3346 uint64_t types; /* const char * (64-bit pointer) */
3347 uint64_t imp; /* IMP (64-bit pointer) */
3350 struct method32_t {
3351 uint32_t name; /* SEL (32-bit pointer) */
3352 uint32_t types; /* const char * (32-bit pointer) */
3353 uint32_t imp; /* IMP (32-bit pointer) */
3356 struct protocol_list64_t {
3357 uint64_t count; /* uintptr_t (a 64-bit value) */
3358 /* struct protocol64_t * list[0]; These pointers follow inline */
3361 struct protocol_list32_t {
3362 uint32_t count; /* uintptr_t (a 32-bit value) */
3363 /* struct protocol32_t * list[0]; These pointers follow inline */
3366 struct protocol64_t {
3367 uint64_t isa; /* id * (64-bit pointer) */
3368 uint64_t name; /* const char * (64-bit pointer) */
3369 uint64_t protocols; /* struct protocol_list64_t *
3370 (64-bit pointer) */
3371 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
3372 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
3373 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
3374 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
3375 uint64_t instanceProperties; /* struct objc_property_list *
3376 (64-bit pointer) */
3379 struct protocol32_t {
3380 uint32_t isa; /* id * (32-bit pointer) */
3381 uint32_t name; /* const char * (32-bit pointer) */
3382 uint32_t protocols; /* struct protocol_list_t *
3383 (32-bit pointer) */
3384 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
3385 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
3386 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
3387 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
3388 uint32_t instanceProperties; /* struct objc_property_list *
3389 (32-bit pointer) */
3392 struct ivar_list64_t {
3393 uint32_t entsize;
3394 uint32_t count;
3395 /* struct ivar64_t first; These structures follow inline */
3398 struct ivar_list32_t {
3399 uint32_t entsize;
3400 uint32_t count;
3401 /* struct ivar32_t first; These structures follow inline */
3404 struct ivar64_t {
3405 uint64_t offset; /* uintptr_t * (64-bit pointer) */
3406 uint64_t name; /* const char * (64-bit pointer) */
3407 uint64_t type; /* const char * (64-bit pointer) */
3408 uint32_t alignment;
3409 uint32_t size;
3412 struct ivar32_t {
3413 uint32_t offset; /* uintptr_t * (32-bit pointer) */
3414 uint32_t name; /* const char * (32-bit pointer) */
3415 uint32_t type; /* const char * (32-bit pointer) */
3416 uint32_t alignment;
3417 uint32_t size;
3420 struct objc_property_list64 {
3421 uint32_t entsize;
3422 uint32_t count;
3423 /* struct objc_property64 first; These structures follow inline */
3426 struct objc_property_list32 {
3427 uint32_t entsize;
3428 uint32_t count;
3429 /* struct objc_property32 first; These structures follow inline */
3432 struct objc_property64 {
3433 uint64_t name; /* const char * (64-bit pointer) */
3434 uint64_t attributes; /* const char * (64-bit pointer) */
3437 struct objc_property32 {
3438 uint32_t name; /* const char * (32-bit pointer) */
3439 uint32_t attributes; /* const char * (32-bit pointer) */
3442 struct category64_t {
3443 uint64_t name; /* const char * (64-bit pointer) */
3444 uint64_t cls; /* struct class_t * (64-bit pointer) */
3445 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
3446 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
3447 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
3448 uint64_t instanceProperties; /* struct objc_property_list *
3449 (64-bit pointer) */
3452 struct category32_t {
3453 uint32_t name; /* const char * (32-bit pointer) */
3454 uint32_t cls; /* struct class_t * (32-bit pointer) */
3455 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
3456 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
3457 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
3458 uint32_t instanceProperties; /* struct objc_property_list *
3459 (32-bit pointer) */
3462 struct objc_image_info64 {
3463 uint32_t version;
3464 uint32_t flags;
3466 struct objc_image_info32 {
3467 uint32_t version;
3468 uint32_t flags;
3470 struct imageInfo_t {
3471 uint32_t version;
3472 uint32_t flags;
3474 /* masks for objc_image_info.flags */
3475 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
3476 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
3477 #define OBJC_IMAGE_IS_SIMULATED (1 << 5)
3478 #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES (1 << 6)
3480 struct message_ref64 {
3481 uint64_t imp; /* IMP (64-bit pointer) */
3482 uint64_t sel; /* SEL (64-bit pointer) */
3485 struct message_ref32 {
3486 uint32_t imp; /* IMP (32-bit pointer) */
3487 uint32_t sel; /* SEL (32-bit pointer) */
3490 // Objective-C 1 (32-bit only) meta data structs.
3492 struct objc_module_t {
3493 uint32_t version;
3494 uint32_t size;
3495 uint32_t name; /* char * (32-bit pointer) */
3496 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
3499 struct objc_symtab_t {
3500 uint32_t sel_ref_cnt;
3501 uint32_t refs; /* SEL * (32-bit pointer) */
3502 uint16_t cls_def_cnt;
3503 uint16_t cat_def_cnt;
3504 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
3507 struct objc_class_t {
3508 uint32_t isa; /* struct objc_class * (32-bit pointer) */
3509 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
3510 uint32_t name; /* const char * (32-bit pointer) */
3511 int32_t version;
3512 int32_t info;
3513 int32_t instance_size;
3514 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
3515 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
3516 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
3517 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
3520 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
3521 // class is not a metaclass
3522 #define CLS_CLASS 0x1
3523 // class is a metaclass
3524 #define CLS_META 0x2
3526 struct objc_category_t {
3527 uint32_t category_name; /* char * (32-bit pointer) */
3528 uint32_t class_name; /* char * (32-bit pointer) */
3529 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
3530 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
3531 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
3534 struct objc_ivar_t {
3535 uint32_t ivar_name; /* char * (32-bit pointer) */
3536 uint32_t ivar_type; /* char * (32-bit pointer) */
3537 int32_t ivar_offset;
3540 struct objc_ivar_list_t {
3541 int32_t ivar_count;
3542 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
3545 struct objc_method_list_t {
3546 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
3547 int32_t method_count;
3548 // struct objc_method_t method_list[1]; /* variable length structure */
3551 struct objc_method_t {
3552 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
3553 uint32_t method_types; /* char * (32-bit pointer) */
3554 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
3555 (32-bit pointer) */
3558 struct objc_protocol_list_t {
3559 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
3560 int32_t count;
3561 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
3562 // (32-bit pointer) */
3565 struct objc_protocol_t {
3566 uint32_t isa; /* struct objc_class * (32-bit pointer) */
3567 uint32_t protocol_name; /* char * (32-bit pointer) */
3568 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
3569 uint32_t instance_methods; /* struct objc_method_description_list *
3570 (32-bit pointer) */
3571 uint32_t class_methods; /* struct objc_method_description_list *
3572 (32-bit pointer) */
3575 struct objc_method_description_list_t {
3576 int32_t count;
3577 // struct objc_method_description_t list[1];
3580 struct objc_method_description_t {
3581 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
3582 uint32_t types; /* char * (32-bit pointer) */
3585 inline void swapStruct(struct cfstring64_t &cfs) {
3586 sys::swapByteOrder(cfs.isa);
3587 sys::swapByteOrder(cfs.flags);
3588 sys::swapByteOrder(cfs.characters);
3589 sys::swapByteOrder(cfs.length);
3592 inline void swapStruct(struct class64_t &c) {
3593 sys::swapByteOrder(c.isa);
3594 sys::swapByteOrder(c.superclass);
3595 sys::swapByteOrder(c.cache);
3596 sys::swapByteOrder(c.vtable);
3597 sys::swapByteOrder(c.data);
3600 inline void swapStruct(struct class32_t &c) {
3601 sys::swapByteOrder(c.isa);
3602 sys::swapByteOrder(c.superclass);
3603 sys::swapByteOrder(c.cache);
3604 sys::swapByteOrder(c.vtable);
3605 sys::swapByteOrder(c.data);
3608 inline void swapStruct(struct class_ro64_t &cro) {
3609 sys::swapByteOrder(cro.flags);
3610 sys::swapByteOrder(cro.instanceStart);
3611 sys::swapByteOrder(cro.instanceSize);
3612 sys::swapByteOrder(cro.reserved);
3613 sys::swapByteOrder(cro.ivarLayout);
3614 sys::swapByteOrder(cro.name);
3615 sys::swapByteOrder(cro.baseMethods);
3616 sys::swapByteOrder(cro.baseProtocols);
3617 sys::swapByteOrder(cro.ivars);
3618 sys::swapByteOrder(cro.weakIvarLayout);
3619 sys::swapByteOrder(cro.baseProperties);
3622 inline void swapStruct(struct class_ro32_t &cro) {
3623 sys::swapByteOrder(cro.flags);
3624 sys::swapByteOrder(cro.instanceStart);
3625 sys::swapByteOrder(cro.instanceSize);
3626 sys::swapByteOrder(cro.ivarLayout);
3627 sys::swapByteOrder(cro.name);
3628 sys::swapByteOrder(cro.baseMethods);
3629 sys::swapByteOrder(cro.baseProtocols);
3630 sys::swapByteOrder(cro.ivars);
3631 sys::swapByteOrder(cro.weakIvarLayout);
3632 sys::swapByteOrder(cro.baseProperties);
3635 inline void swapStruct(struct method_list64_t &ml) {
3636 sys::swapByteOrder(ml.entsize);
3637 sys::swapByteOrder(ml.count);
3640 inline void swapStruct(struct method_list32_t &ml) {
3641 sys::swapByteOrder(ml.entsize);
3642 sys::swapByteOrder(ml.count);
3645 inline void swapStruct(struct method64_t &m) {
3646 sys::swapByteOrder(m.name);
3647 sys::swapByteOrder(m.types);
3648 sys::swapByteOrder(m.imp);
3651 inline void swapStruct(struct method32_t &m) {
3652 sys::swapByteOrder(m.name);
3653 sys::swapByteOrder(m.types);
3654 sys::swapByteOrder(m.imp);
3657 inline void swapStruct(struct protocol_list64_t &pl) {
3658 sys::swapByteOrder(pl.count);
3661 inline void swapStruct(struct protocol_list32_t &pl) {
3662 sys::swapByteOrder(pl.count);
3665 inline void swapStruct(struct protocol64_t &p) {
3666 sys::swapByteOrder(p.isa);
3667 sys::swapByteOrder(p.name);
3668 sys::swapByteOrder(p.protocols);
3669 sys::swapByteOrder(p.instanceMethods);
3670 sys::swapByteOrder(p.classMethods);
3671 sys::swapByteOrder(p.optionalInstanceMethods);
3672 sys::swapByteOrder(p.optionalClassMethods);
3673 sys::swapByteOrder(p.instanceProperties);
3676 inline void swapStruct(struct protocol32_t &p) {
3677 sys::swapByteOrder(p.isa);
3678 sys::swapByteOrder(p.name);
3679 sys::swapByteOrder(p.protocols);
3680 sys::swapByteOrder(p.instanceMethods);
3681 sys::swapByteOrder(p.classMethods);
3682 sys::swapByteOrder(p.optionalInstanceMethods);
3683 sys::swapByteOrder(p.optionalClassMethods);
3684 sys::swapByteOrder(p.instanceProperties);
3687 inline void swapStruct(struct ivar_list64_t &il) {
3688 sys::swapByteOrder(il.entsize);
3689 sys::swapByteOrder(il.count);
3692 inline void swapStruct(struct ivar_list32_t &il) {
3693 sys::swapByteOrder(il.entsize);
3694 sys::swapByteOrder(il.count);
3697 inline void swapStruct(struct ivar64_t &i) {
3698 sys::swapByteOrder(i.offset);
3699 sys::swapByteOrder(i.name);
3700 sys::swapByteOrder(i.type);
3701 sys::swapByteOrder(i.alignment);
3702 sys::swapByteOrder(i.size);
3705 inline void swapStruct(struct ivar32_t &i) {
3706 sys::swapByteOrder(i.offset);
3707 sys::swapByteOrder(i.name);
3708 sys::swapByteOrder(i.type);
3709 sys::swapByteOrder(i.alignment);
3710 sys::swapByteOrder(i.size);
3713 inline void swapStruct(struct objc_property_list64 &pl) {
3714 sys::swapByteOrder(pl.entsize);
3715 sys::swapByteOrder(pl.count);
3718 inline void swapStruct(struct objc_property_list32 &pl) {
3719 sys::swapByteOrder(pl.entsize);
3720 sys::swapByteOrder(pl.count);
3723 inline void swapStruct(struct objc_property64 &op) {
3724 sys::swapByteOrder(op.name);
3725 sys::swapByteOrder(op.attributes);
3728 inline void swapStruct(struct objc_property32 &op) {
3729 sys::swapByteOrder(op.name);
3730 sys::swapByteOrder(op.attributes);
3733 inline void swapStruct(struct category64_t &c) {
3734 sys::swapByteOrder(c.name);
3735 sys::swapByteOrder(c.cls);
3736 sys::swapByteOrder(c.instanceMethods);
3737 sys::swapByteOrder(c.classMethods);
3738 sys::swapByteOrder(c.protocols);
3739 sys::swapByteOrder(c.instanceProperties);
3742 inline void swapStruct(struct category32_t &c) {
3743 sys::swapByteOrder(c.name);
3744 sys::swapByteOrder(c.cls);
3745 sys::swapByteOrder(c.instanceMethods);
3746 sys::swapByteOrder(c.classMethods);
3747 sys::swapByteOrder(c.protocols);
3748 sys::swapByteOrder(c.instanceProperties);
3751 inline void swapStruct(struct objc_image_info64 &o) {
3752 sys::swapByteOrder(o.version);
3753 sys::swapByteOrder(o.flags);
3756 inline void swapStruct(struct objc_image_info32 &o) {
3757 sys::swapByteOrder(o.version);
3758 sys::swapByteOrder(o.flags);
3761 inline void swapStruct(struct imageInfo_t &o) {
3762 sys::swapByteOrder(o.version);
3763 sys::swapByteOrder(o.flags);
3766 inline void swapStruct(struct message_ref64 &mr) {
3767 sys::swapByteOrder(mr.imp);
3768 sys::swapByteOrder(mr.sel);
3771 inline void swapStruct(struct message_ref32 &mr) {
3772 sys::swapByteOrder(mr.imp);
3773 sys::swapByteOrder(mr.sel);
3776 inline void swapStruct(struct objc_module_t &module) {
3777 sys::swapByteOrder(module.version);
3778 sys::swapByteOrder(module.size);
3779 sys::swapByteOrder(module.name);
3780 sys::swapByteOrder(module.symtab);
3783 inline void swapStruct(struct objc_symtab_t &symtab) {
3784 sys::swapByteOrder(symtab.sel_ref_cnt);
3785 sys::swapByteOrder(symtab.refs);
3786 sys::swapByteOrder(symtab.cls_def_cnt);
3787 sys::swapByteOrder(symtab.cat_def_cnt);
3790 inline void swapStruct(struct objc_class_t &objc_class) {
3791 sys::swapByteOrder(objc_class.isa);
3792 sys::swapByteOrder(objc_class.super_class);
3793 sys::swapByteOrder(objc_class.name);
3794 sys::swapByteOrder(objc_class.version);
3795 sys::swapByteOrder(objc_class.info);
3796 sys::swapByteOrder(objc_class.instance_size);
3797 sys::swapByteOrder(objc_class.ivars);
3798 sys::swapByteOrder(objc_class.methodLists);
3799 sys::swapByteOrder(objc_class.cache);
3800 sys::swapByteOrder(objc_class.protocols);
3803 inline void swapStruct(struct objc_category_t &objc_category) {
3804 sys::swapByteOrder(objc_category.category_name);
3805 sys::swapByteOrder(objc_category.class_name);
3806 sys::swapByteOrder(objc_category.instance_methods);
3807 sys::swapByteOrder(objc_category.class_methods);
3808 sys::swapByteOrder(objc_category.protocols);
3811 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3812 sys::swapByteOrder(objc_ivar_list.ivar_count);
3815 inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3816 sys::swapByteOrder(objc_ivar.ivar_name);
3817 sys::swapByteOrder(objc_ivar.ivar_type);
3818 sys::swapByteOrder(objc_ivar.ivar_offset);
3821 inline void swapStruct(struct objc_method_list_t &method_list) {
3822 sys::swapByteOrder(method_list.obsolete);
3823 sys::swapByteOrder(method_list.method_count);
3826 inline void swapStruct(struct objc_method_t &method) {
3827 sys::swapByteOrder(method.method_name);
3828 sys::swapByteOrder(method.method_types);
3829 sys::swapByteOrder(method.method_imp);
3832 inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3833 sys::swapByteOrder(protocol_list.next);
3834 sys::swapByteOrder(protocol_list.count);
3837 inline void swapStruct(struct objc_protocol_t &protocol) {
3838 sys::swapByteOrder(protocol.isa);
3839 sys::swapByteOrder(protocol.protocol_name);
3840 sys::swapByteOrder(protocol.protocol_list);
3841 sys::swapByteOrder(protocol.instance_methods);
3842 sys::swapByteOrder(protocol.class_methods);
3845 inline void swapStruct(struct objc_method_description_list_t &mdl) {
3846 sys::swapByteOrder(mdl.count);
3849 inline void swapStruct(struct objc_method_description_t &md) {
3850 sys::swapByteOrder(md.name);
3851 sys::swapByteOrder(md.types);
3854 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3855 struct DisassembleInfo *info);
3857 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3858 // to an Objective-C class and returns the class name. It is also passed the
3859 // address of the pointer, so when the pointer is zero as it can be in an .o
3860 // file, that is used to look for an external relocation entry with a symbol
3861 // name.
3862 static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3863 uint64_t ReferenceValue,
3864 struct DisassembleInfo *info) {
3865 const char *r;
3866 uint32_t offset, left;
3867 SectionRef S;
3869 // The pointer_value can be 0 in an object file and have a relocation
3870 // entry for the class symbol at the ReferenceValue (the address of the
3871 // pointer).
3872 if (pointer_value == 0) {
3873 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3874 if (r == nullptr || left < sizeof(uint64_t))
3875 return nullptr;
3876 uint64_t n_value;
3877 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3878 if (symbol_name == nullptr)
3879 return nullptr;
3880 const char *class_name = strrchr(symbol_name, '$');
3881 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3882 return class_name + 2;
3883 else
3884 return nullptr;
3887 // The case were the pointer_value is non-zero and points to a class defined
3888 // in this Mach-O file.
3889 r = get_pointer_64(pointer_value, offset, left, S, info);
3890 if (r == nullptr || left < sizeof(struct class64_t))
3891 return nullptr;
3892 struct class64_t c;
3893 memcpy(&c, r, sizeof(struct class64_t));
3894 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3895 swapStruct(c);
3896 if (c.data == 0)
3897 return nullptr;
3898 r = get_pointer_64(c.data, offset, left, S, info);
3899 if (r == nullptr || left < sizeof(struct class_ro64_t))
3900 return nullptr;
3901 struct class_ro64_t cro;
3902 memcpy(&cro, r, sizeof(struct class_ro64_t));
3903 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3904 swapStruct(cro);
3905 if (cro.name == 0)
3906 return nullptr;
3907 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3908 return name;
3911 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3912 // pointer to a cfstring and returns its name or nullptr.
3913 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3914 struct DisassembleInfo *info) {
3915 const char *r, *name;
3916 uint32_t offset, left;
3917 SectionRef S;
3918 struct cfstring64_t cfs;
3919 uint64_t cfs_characters;
3921 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3922 if (r == nullptr || left < sizeof(struct cfstring64_t))
3923 return nullptr;
3924 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3925 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3926 swapStruct(cfs);
3927 if (cfs.characters == 0) {
3928 uint64_t n_value;
3929 const char *symbol_name = get_symbol_64(
3930 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3931 if (symbol_name == nullptr)
3932 return nullptr;
3933 cfs_characters = n_value;
3934 } else
3935 cfs_characters = cfs.characters;
3936 name = get_pointer_64(cfs_characters, offset, left, S, info);
3938 return name;
3941 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
3942 // of a pointer to an Objective-C selector reference when the pointer value is
3943 // zero as in a .o file and is likely to have a external relocation entry with
3944 // who's symbol's n_value is the real pointer to the selector name. If that is
3945 // the case the real pointer to the selector name is returned else 0 is
3946 // returned
3947 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3948 struct DisassembleInfo *info) {
3949 uint32_t offset, left;
3950 SectionRef S;
3952 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3953 if (r == nullptr || left < sizeof(uint64_t))
3954 return 0;
3955 uint64_t n_value;
3956 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3957 if (symbol_name == nullptr)
3958 return 0;
3959 return n_value;
3962 static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3963 const char *sectname) {
3964 for (const SectionRef &Section : O->sections()) {
3965 StringRef SectName;
3966 Section.getName(SectName);
3967 DataRefImpl Ref = Section.getRawDataRefImpl();
3968 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3969 if (SegName == segname && SectName == sectname)
3970 return Section;
3972 return SectionRef();
3975 static void
3976 walk_pointer_list_64(const char *listname, const SectionRef S,
3977 MachOObjectFile *O, struct DisassembleInfo *info,
3978 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3979 if (S == SectionRef())
3980 return;
3982 StringRef SectName;
3983 S.getName(SectName);
3984 DataRefImpl Ref = S.getRawDataRefImpl();
3985 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3986 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3988 StringRef BytesStr;
3989 S.getContents(BytesStr);
3990 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3992 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3993 uint32_t left = S.getSize() - i;
3994 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3995 uint64_t p = 0;
3996 memcpy(&p, Contents + i, size);
3997 if (i + sizeof(uint64_t) > S.getSize())
3998 outs() << listname << " list pointer extends past end of (" << SegName
3999 << "," << SectName << ") section\n";
4000 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
4002 if (O->isLittleEndian() != sys::IsLittleEndianHost)
4003 sys::swapByteOrder(p);
4005 uint64_t n_value = 0;
4006 const char *name = get_symbol_64(i, S, info, n_value, p);
4007 if (name == nullptr)
4008 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
4010 if (n_value != 0) {
4011 outs() << format("0x%" PRIx64, n_value);
4012 if (p != 0)
4013 outs() << " + " << format("0x%" PRIx64, p);
4014 } else
4015 outs() << format("0x%" PRIx64, p);
4016 if (name != nullptr)
4017 outs() << " " << name;
4018 outs() << "\n";
4020 p += n_value;
4021 if (func)
4022 func(p, info);
4026 static void
4027 walk_pointer_list_32(const char *listname, const SectionRef S,
4028 MachOObjectFile *O, struct DisassembleInfo *info,
4029 void (*func)(uint32_t, struct DisassembleInfo *info)) {
4030 if (S == SectionRef())
4031 return;
4033 StringRef SectName;
4034 S.getName(SectName);
4035 DataRefImpl Ref = S.getRawDataRefImpl();
4036 StringRef SegName = O->getSectionFinalSegmentName(Ref);
4037 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4039 StringRef BytesStr;
4040 S.getContents(BytesStr);
4041 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
4043 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
4044 uint32_t left = S.getSize() - i;
4045 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
4046 uint32_t p = 0;
4047 memcpy(&p, Contents + i, size);
4048 if (i + sizeof(uint32_t) > S.getSize())
4049 outs() << listname << " list pointer extends past end of (" << SegName
4050 << "," << SectName << ") section\n";
4051 uint32_t Address = S.getAddress() + i;
4052 outs() << format("%08" PRIx32, Address) << " ";
4054 if (O->isLittleEndian() != sys::IsLittleEndianHost)
4055 sys::swapByteOrder(p);
4056 outs() << format("0x%" PRIx32, p);
4058 const char *name = get_symbol_32(i, S, info, p);
4059 if (name != nullptr)
4060 outs() << " " << name;
4061 outs() << "\n";
4063 if (func)
4064 func(p, info);
4068 static void print_layout_map(const char *layout_map, uint32_t left) {
4069 if (layout_map == nullptr)
4070 return;
4071 outs() << " layout map: ";
4072 do {
4073 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
4074 left--;
4075 layout_map++;
4076 } while (*layout_map != '\0' && left != 0);
4077 outs() << "\n";
4080 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
4081 uint32_t offset, left;
4082 SectionRef S;
4083 const char *layout_map;
4085 if (p == 0)
4086 return;
4087 layout_map = get_pointer_64(p, offset, left, S, info);
4088 print_layout_map(layout_map, left);
4091 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
4092 uint32_t offset, left;
4093 SectionRef S;
4094 const char *layout_map;
4096 if (p == 0)
4097 return;
4098 layout_map = get_pointer_32(p, offset, left, S, info);
4099 print_layout_map(layout_map, left);
4102 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
4103 const char *indent) {
4104 struct method_list64_t ml;
4105 struct method64_t m;
4106 const char *r;
4107 uint32_t offset, xoffset, left, i;
4108 SectionRef S, xS;
4109 const char *name, *sym_name;
4110 uint64_t n_value;
4112 r = get_pointer_64(p, offset, left, S, info);
4113 if (r == nullptr)
4114 return;
4115 memset(&ml, '\0', sizeof(struct method_list64_t));
4116 if (left < sizeof(struct method_list64_t)) {
4117 memcpy(&ml, r, left);
4118 outs() << " (method_list_t entends past the end of the section)\n";
4119 } else
4120 memcpy(&ml, r, sizeof(struct method_list64_t));
4121 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4122 swapStruct(ml);
4123 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
4124 outs() << indent << "\t\t count " << ml.count << "\n";
4126 p += sizeof(struct method_list64_t);
4127 offset += sizeof(struct method_list64_t);
4128 for (i = 0; i < ml.count; i++) {
4129 r = get_pointer_64(p, offset, left, S, info);
4130 if (r == nullptr)
4131 return;
4132 memset(&m, '\0', sizeof(struct method64_t));
4133 if (left < sizeof(struct method64_t)) {
4134 memcpy(&m, r, left);
4135 outs() << indent << " (method_t extends past the end of the section)\n";
4136 } else
4137 memcpy(&m, r, sizeof(struct method64_t));
4138 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4139 swapStruct(m);
4141 outs() << indent << "\t\t name ";
4142 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
4143 info, n_value, m.name);
4144 if (n_value != 0) {
4145 if (info->verbose && sym_name != nullptr)
4146 outs() << sym_name;
4147 else
4148 outs() << format("0x%" PRIx64, n_value);
4149 if (m.name != 0)
4150 outs() << " + " << format("0x%" PRIx64, m.name);
4151 } else
4152 outs() << format("0x%" PRIx64, m.name);
4153 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
4154 if (name != nullptr)
4155 outs() << format(" %.*s", left, name);
4156 outs() << "\n";
4158 outs() << indent << "\t\t types ";
4159 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
4160 info, n_value, m.types);
4161 if (n_value != 0) {
4162 if (info->verbose && sym_name != nullptr)
4163 outs() << sym_name;
4164 else
4165 outs() << format("0x%" PRIx64, n_value);
4166 if (m.types != 0)
4167 outs() << " + " << format("0x%" PRIx64, m.types);
4168 } else
4169 outs() << format("0x%" PRIx64, m.types);
4170 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
4171 if (name != nullptr)
4172 outs() << format(" %.*s", left, name);
4173 outs() << "\n";
4175 outs() << indent << "\t\t imp ";
4176 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
4177 n_value, m.imp);
4178 if (info->verbose && name == nullptr) {
4179 if (n_value != 0) {
4180 outs() << format("0x%" PRIx64, n_value) << " ";
4181 if (m.imp != 0)
4182 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
4183 } else
4184 outs() << format("0x%" PRIx64, m.imp) << " ";
4186 if (name != nullptr)
4187 outs() << name;
4188 outs() << "\n";
4190 p += sizeof(struct method64_t);
4191 offset += sizeof(struct method64_t);
4195 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
4196 const char *indent) {
4197 struct method_list32_t ml;
4198 struct method32_t m;
4199 const char *r, *name;
4200 uint32_t offset, xoffset, left, i;
4201 SectionRef S, xS;
4203 r = get_pointer_32(p, offset, left, S, info);
4204 if (r == nullptr)
4205 return;
4206 memset(&ml, '\0', sizeof(struct method_list32_t));
4207 if (left < sizeof(struct method_list32_t)) {
4208 memcpy(&ml, r, left);
4209 outs() << " (method_list_t entends past the end of the section)\n";
4210 } else
4211 memcpy(&ml, r, sizeof(struct method_list32_t));
4212 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4213 swapStruct(ml);
4214 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
4215 outs() << indent << "\t\t count " << ml.count << "\n";
4217 p += sizeof(struct method_list32_t);
4218 offset += sizeof(struct method_list32_t);
4219 for (i = 0; i < ml.count; i++) {
4220 r = get_pointer_32(p, offset, left, S, info);
4221 if (r == nullptr)
4222 return;
4223 memset(&m, '\0', sizeof(struct method32_t));
4224 if (left < sizeof(struct method32_t)) {
4225 memcpy(&ml, r, left);
4226 outs() << indent << " (method_t entends past the end of the section)\n";
4227 } else
4228 memcpy(&m, r, sizeof(struct method32_t));
4229 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4230 swapStruct(m);
4232 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
4233 name = get_pointer_32(m.name, xoffset, left, xS, info);
4234 if (name != nullptr)
4235 outs() << format(" %.*s", left, name);
4236 outs() << "\n";
4238 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
4239 name = get_pointer_32(m.types, xoffset, left, xS, info);
4240 if (name != nullptr)
4241 outs() << format(" %.*s", left, name);
4242 outs() << "\n";
4244 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
4245 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
4246 m.imp);
4247 if (name != nullptr)
4248 outs() << " " << name;
4249 outs() << "\n";
4251 p += sizeof(struct method32_t);
4252 offset += sizeof(struct method32_t);
4256 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
4257 uint32_t offset, left, xleft;
4258 SectionRef S;
4259 struct objc_method_list_t method_list;
4260 struct objc_method_t method;
4261 const char *r, *methods, *name, *SymbolName;
4262 int32_t i;
4264 r = get_pointer_32(p, offset, left, S, info, true);
4265 if (r == nullptr)
4266 return true;
4268 outs() << "\n";
4269 if (left > sizeof(struct objc_method_list_t)) {
4270 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
4271 } else {
4272 outs() << "\t\t objc_method_list extends past end of the section\n";
4273 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
4274 memcpy(&method_list, r, left);
4276 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4277 swapStruct(method_list);
4279 outs() << "\t\t obsolete "
4280 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
4281 outs() << "\t\t method_count " << method_list.method_count << "\n";
4283 methods = r + sizeof(struct objc_method_list_t);
4284 for (i = 0; i < method_list.method_count; i++) {
4285 if ((i + 1) * sizeof(struct objc_method_t) > left) {
4286 outs() << "\t\t remaining method's extend past the of the section\n";
4287 break;
4289 memcpy(&method, methods + i * sizeof(struct objc_method_t),
4290 sizeof(struct objc_method_t));
4291 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4292 swapStruct(method);
4294 outs() << "\t\t method_name "
4295 << format("0x%08" PRIx32, method.method_name);
4296 if (info->verbose) {
4297 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
4298 if (name != nullptr)
4299 outs() << format(" %.*s", xleft, name);
4300 else
4301 outs() << " (not in an __OBJC section)";
4303 outs() << "\n";
4305 outs() << "\t\t method_types "
4306 << format("0x%08" PRIx32, method.method_types);
4307 if (info->verbose) {
4308 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
4309 if (name != nullptr)
4310 outs() << format(" %.*s", xleft, name);
4311 else
4312 outs() << " (not in an __OBJC section)";
4314 outs() << "\n";
4316 outs() << "\t\t method_imp "
4317 << format("0x%08" PRIx32, method.method_imp) << " ";
4318 if (info->verbose) {
4319 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
4320 if (SymbolName != nullptr)
4321 outs() << SymbolName;
4323 outs() << "\n";
4325 return false;
4328 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
4329 struct protocol_list64_t pl;
4330 uint64_t q, n_value;
4331 struct protocol64_t pc;
4332 const char *r;
4333 uint32_t offset, xoffset, left, i;
4334 SectionRef S, xS;
4335 const char *name, *sym_name;
4337 r = get_pointer_64(p, offset, left, S, info);
4338 if (r == nullptr)
4339 return;
4340 memset(&pl, '\0', sizeof(struct protocol_list64_t));
4341 if (left < sizeof(struct protocol_list64_t)) {
4342 memcpy(&pl, r, left);
4343 outs() << " (protocol_list_t entends past the end of the section)\n";
4344 } else
4345 memcpy(&pl, r, sizeof(struct protocol_list64_t));
4346 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4347 swapStruct(pl);
4348 outs() << " count " << pl.count << "\n";
4350 p += sizeof(struct protocol_list64_t);
4351 offset += sizeof(struct protocol_list64_t);
4352 for (i = 0; i < pl.count; i++) {
4353 r = get_pointer_64(p, offset, left, S, info);
4354 if (r == nullptr)
4355 return;
4356 q = 0;
4357 if (left < sizeof(uint64_t)) {
4358 memcpy(&q, r, left);
4359 outs() << " (protocol_t * entends past the end of the section)\n";
4360 } else
4361 memcpy(&q, r, sizeof(uint64_t));
4362 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4363 sys::swapByteOrder(q);
4365 outs() << "\t\t list[" << i << "] ";
4366 sym_name = get_symbol_64(offset, S, info, n_value, q);
4367 if (n_value != 0) {
4368 if (info->verbose && sym_name != nullptr)
4369 outs() << sym_name;
4370 else
4371 outs() << format("0x%" PRIx64, n_value);
4372 if (q != 0)
4373 outs() << " + " << format("0x%" PRIx64, q);
4374 } else
4375 outs() << format("0x%" PRIx64, q);
4376 outs() << " (struct protocol_t *)\n";
4378 r = get_pointer_64(q + n_value, offset, left, S, info);
4379 if (r == nullptr)
4380 return;
4381 memset(&pc, '\0', sizeof(struct protocol64_t));
4382 if (left < sizeof(struct protocol64_t)) {
4383 memcpy(&pc, r, left);
4384 outs() << " (protocol_t entends past the end of the section)\n";
4385 } else
4386 memcpy(&pc, r, sizeof(struct protocol64_t));
4387 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4388 swapStruct(pc);
4390 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
4392 outs() << "\t\t\t name ";
4393 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
4394 info, n_value, pc.name);
4395 if (n_value != 0) {
4396 if (info->verbose && sym_name != nullptr)
4397 outs() << sym_name;
4398 else
4399 outs() << format("0x%" PRIx64, n_value);
4400 if (pc.name != 0)
4401 outs() << " + " << format("0x%" PRIx64, pc.name);
4402 } else
4403 outs() << format("0x%" PRIx64, pc.name);
4404 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
4405 if (name != nullptr)
4406 outs() << format(" %.*s", left, name);
4407 outs() << "\n";
4409 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
4411 outs() << "\t\t instanceMethods ";
4412 sym_name =
4413 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
4414 S, info, n_value, pc.instanceMethods);
4415 if (n_value != 0) {
4416 if (info->verbose && sym_name != nullptr)
4417 outs() << sym_name;
4418 else
4419 outs() << format("0x%" PRIx64, n_value);
4420 if (pc.instanceMethods != 0)
4421 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
4422 } else
4423 outs() << format("0x%" PRIx64, pc.instanceMethods);
4424 outs() << " (struct method_list_t *)\n";
4425 if (pc.instanceMethods + n_value != 0)
4426 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
4428 outs() << "\t\t classMethods ";
4429 sym_name =
4430 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
4431 info, n_value, pc.classMethods);
4432 if (n_value != 0) {
4433 if (info->verbose && sym_name != nullptr)
4434 outs() << sym_name;
4435 else
4436 outs() << format("0x%" PRIx64, n_value);
4437 if (pc.classMethods != 0)
4438 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
4439 } else
4440 outs() << format("0x%" PRIx64, pc.classMethods);
4441 outs() << " (struct method_list_t *)\n";
4442 if (pc.classMethods + n_value != 0)
4443 print_method_list64_t(pc.classMethods + n_value, info, "\t");
4445 outs() << "\t optionalInstanceMethods "
4446 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
4447 outs() << "\t optionalClassMethods "
4448 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
4449 outs() << "\t instanceProperties "
4450 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
4452 p += sizeof(uint64_t);
4453 offset += sizeof(uint64_t);
4457 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
4458 struct protocol_list32_t pl;
4459 uint32_t q;
4460 struct protocol32_t pc;
4461 const char *r;
4462 uint32_t offset, xoffset, left, i;
4463 SectionRef S, xS;
4464 const char *name;
4466 r = get_pointer_32(p, offset, left, S, info);
4467 if (r == nullptr)
4468 return;
4469 memset(&pl, '\0', sizeof(struct protocol_list32_t));
4470 if (left < sizeof(struct protocol_list32_t)) {
4471 memcpy(&pl, r, left);
4472 outs() << " (protocol_list_t entends past the end of the section)\n";
4473 } else
4474 memcpy(&pl, r, sizeof(struct protocol_list32_t));
4475 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4476 swapStruct(pl);
4477 outs() << " count " << pl.count << "\n";
4479 p += sizeof(struct protocol_list32_t);
4480 offset += sizeof(struct protocol_list32_t);
4481 for (i = 0; i < pl.count; i++) {
4482 r = get_pointer_32(p, offset, left, S, info);
4483 if (r == nullptr)
4484 return;
4485 q = 0;
4486 if (left < sizeof(uint32_t)) {
4487 memcpy(&q, r, left);
4488 outs() << " (protocol_t * entends past the end of the section)\n";
4489 } else
4490 memcpy(&q, r, sizeof(uint32_t));
4491 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4492 sys::swapByteOrder(q);
4493 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
4494 << " (struct protocol_t *)\n";
4495 r = get_pointer_32(q, offset, left, S, info);
4496 if (r == nullptr)
4497 return;
4498 memset(&pc, '\0', sizeof(struct protocol32_t));
4499 if (left < sizeof(struct protocol32_t)) {
4500 memcpy(&pc, r, left);
4501 outs() << " (protocol_t entends past the end of the section)\n";
4502 } else
4503 memcpy(&pc, r, sizeof(struct protocol32_t));
4504 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4505 swapStruct(pc);
4506 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
4507 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
4508 name = get_pointer_32(pc.name, xoffset, left, xS, info);
4509 if (name != nullptr)
4510 outs() << format(" %.*s", left, name);
4511 outs() << "\n";
4512 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
4513 outs() << "\t\t instanceMethods "
4514 << format("0x%" PRIx32, pc.instanceMethods)
4515 << " (struct method_list_t *)\n";
4516 if (pc.instanceMethods != 0)
4517 print_method_list32_t(pc.instanceMethods, info, "\t");
4518 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
4519 << " (struct method_list_t *)\n";
4520 if (pc.classMethods != 0)
4521 print_method_list32_t(pc.classMethods, info, "\t");
4522 outs() << "\t optionalInstanceMethods "
4523 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
4524 outs() << "\t optionalClassMethods "
4525 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
4526 outs() << "\t instanceProperties "
4527 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
4528 p += sizeof(uint32_t);
4529 offset += sizeof(uint32_t);
4533 static void print_indent(uint32_t indent) {
4534 for (uint32_t i = 0; i < indent;) {
4535 if (indent - i >= 8) {
4536 outs() << "\t";
4537 i += 8;
4538 } else {
4539 for (uint32_t j = i; j < indent; j++)
4540 outs() << " ";
4541 return;
4546 static bool print_method_description_list(uint32_t p, uint32_t indent,
4547 struct DisassembleInfo *info) {
4548 uint32_t offset, left, xleft;
4549 SectionRef S;
4550 struct objc_method_description_list_t mdl;
4551 struct objc_method_description_t md;
4552 const char *r, *list, *name;
4553 int32_t i;
4555 r = get_pointer_32(p, offset, left, S, info, true);
4556 if (r == nullptr)
4557 return true;
4559 outs() << "\n";
4560 if (left > sizeof(struct objc_method_description_list_t)) {
4561 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
4562 } else {
4563 print_indent(indent);
4564 outs() << " objc_method_description_list extends past end of the section\n";
4565 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
4566 memcpy(&mdl, r, left);
4568 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4569 swapStruct(mdl);
4571 print_indent(indent);
4572 outs() << " count " << mdl.count << "\n";
4574 list = r + sizeof(struct objc_method_description_list_t);
4575 for (i = 0; i < mdl.count; i++) {
4576 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
4577 print_indent(indent);
4578 outs() << " remaining list entries extend past the of the section\n";
4579 break;
4581 print_indent(indent);
4582 outs() << " list[" << i << "]\n";
4583 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
4584 sizeof(struct objc_method_description_t));
4585 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4586 swapStruct(md);
4588 print_indent(indent);
4589 outs() << " name " << format("0x%08" PRIx32, md.name);
4590 if (info->verbose) {
4591 name = get_pointer_32(md.name, offset, xleft, S, info, true);
4592 if (name != nullptr)
4593 outs() << format(" %.*s", xleft, name);
4594 else
4595 outs() << " (not in an __OBJC section)";
4597 outs() << "\n";
4599 print_indent(indent);
4600 outs() << " types " << format("0x%08" PRIx32, md.types);
4601 if (info->verbose) {
4602 name = get_pointer_32(md.types, offset, xleft, S, info, true);
4603 if (name != nullptr)
4604 outs() << format(" %.*s", xleft, name);
4605 else
4606 outs() << " (not in an __OBJC section)";
4608 outs() << "\n";
4610 return false;
4613 static bool print_protocol_list(uint32_t p, uint32_t indent,
4614 struct DisassembleInfo *info);
4616 static bool print_protocol(uint32_t p, uint32_t indent,
4617 struct DisassembleInfo *info) {
4618 uint32_t offset, left;
4619 SectionRef S;
4620 struct objc_protocol_t protocol;
4621 const char *r, *name;
4623 r = get_pointer_32(p, offset, left, S, info, true);
4624 if (r == nullptr)
4625 return true;
4627 outs() << "\n";
4628 if (left >= sizeof(struct objc_protocol_t)) {
4629 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
4630 } else {
4631 print_indent(indent);
4632 outs() << " Protocol extends past end of the section\n";
4633 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
4634 memcpy(&protocol, r, left);
4636 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4637 swapStruct(protocol);
4639 print_indent(indent);
4640 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
4641 << "\n";
4643 print_indent(indent);
4644 outs() << " protocol_name "
4645 << format("0x%08" PRIx32, protocol.protocol_name);
4646 if (info->verbose) {
4647 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
4648 if (name != nullptr)
4649 outs() << format(" %.*s", left, name);
4650 else
4651 outs() << " (not in an __OBJC section)";
4653 outs() << "\n";
4655 print_indent(indent);
4656 outs() << " protocol_list "
4657 << format("0x%08" PRIx32, protocol.protocol_list);
4658 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
4659 outs() << " (not in an __OBJC section)\n";
4661 print_indent(indent);
4662 outs() << " instance_methods "
4663 << format("0x%08" PRIx32, protocol.instance_methods);
4664 if (print_method_description_list(protocol.instance_methods, indent, info))
4665 outs() << " (not in an __OBJC section)\n";
4667 print_indent(indent);
4668 outs() << " class_methods "
4669 << format("0x%08" PRIx32, protocol.class_methods);
4670 if (print_method_description_list(protocol.class_methods, indent, info))
4671 outs() << " (not in an __OBJC section)\n";
4673 return false;
4676 static bool print_protocol_list(uint32_t p, uint32_t indent,
4677 struct DisassembleInfo *info) {
4678 uint32_t offset, left, l;
4679 SectionRef S;
4680 struct objc_protocol_list_t protocol_list;
4681 const char *r, *list;
4682 int32_t i;
4684 r = get_pointer_32(p, offset, left, S, info, true);
4685 if (r == nullptr)
4686 return true;
4688 outs() << "\n";
4689 if (left > sizeof(struct objc_protocol_list_t)) {
4690 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
4691 } else {
4692 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4693 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
4694 memcpy(&protocol_list, r, left);
4696 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4697 swapStruct(protocol_list);
4699 print_indent(indent);
4700 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
4701 << "\n";
4702 print_indent(indent);
4703 outs() << " count " << protocol_list.count << "\n";
4705 list = r + sizeof(struct objc_protocol_list_t);
4706 for (i = 0; i < protocol_list.count; i++) {
4707 if ((i + 1) * sizeof(uint32_t) > left) {
4708 outs() << "\t\t remaining list entries extend past the of the section\n";
4709 break;
4711 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
4712 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4713 sys::swapByteOrder(l);
4715 print_indent(indent);
4716 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
4717 if (print_protocol(l, indent, info))
4718 outs() << "(not in an __OBJC section)\n";
4720 return false;
4723 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
4724 struct ivar_list64_t il;
4725 struct ivar64_t i;
4726 const char *r;
4727 uint32_t offset, xoffset, left, j;
4728 SectionRef S, xS;
4729 const char *name, *sym_name, *ivar_offset_p;
4730 uint64_t ivar_offset, n_value;
4732 r = get_pointer_64(p, offset, left, S, info);
4733 if (r == nullptr)
4734 return;
4735 memset(&il, '\0', sizeof(struct ivar_list64_t));
4736 if (left < sizeof(struct ivar_list64_t)) {
4737 memcpy(&il, r, left);
4738 outs() << " (ivar_list_t entends past the end of the section)\n";
4739 } else
4740 memcpy(&il, r, sizeof(struct ivar_list64_t));
4741 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4742 swapStruct(il);
4743 outs() << " entsize " << il.entsize << "\n";
4744 outs() << " count " << il.count << "\n";
4746 p += sizeof(struct ivar_list64_t);
4747 offset += sizeof(struct ivar_list64_t);
4748 for (j = 0; j < il.count; j++) {
4749 r = get_pointer_64(p, offset, left, S, info);
4750 if (r == nullptr)
4751 return;
4752 memset(&i, '\0', sizeof(struct ivar64_t));
4753 if (left < sizeof(struct ivar64_t)) {
4754 memcpy(&i, r, left);
4755 outs() << " (ivar_t entends past the end of the section)\n";
4756 } else
4757 memcpy(&i, r, sizeof(struct ivar64_t));
4758 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4759 swapStruct(i);
4761 outs() << "\t\t\t offset ";
4762 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4763 info, n_value, i.offset);
4764 if (n_value != 0) {
4765 if (info->verbose && sym_name != nullptr)
4766 outs() << sym_name;
4767 else
4768 outs() << format("0x%" PRIx64, n_value);
4769 if (i.offset != 0)
4770 outs() << " + " << format("0x%" PRIx64, i.offset);
4771 } else
4772 outs() << format("0x%" PRIx64, i.offset);
4773 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4774 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4775 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4776 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4777 sys::swapByteOrder(ivar_offset);
4778 outs() << " " << ivar_offset << "\n";
4779 } else
4780 outs() << "\n";
4782 outs() << "\t\t\t name ";
4783 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4784 n_value, i.name);
4785 if (n_value != 0) {
4786 if (info->verbose && sym_name != nullptr)
4787 outs() << sym_name;
4788 else
4789 outs() << format("0x%" PRIx64, n_value);
4790 if (i.name != 0)
4791 outs() << " + " << format("0x%" PRIx64, i.name);
4792 } else
4793 outs() << format("0x%" PRIx64, i.name);
4794 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4795 if (name != nullptr)
4796 outs() << format(" %.*s", left, name);
4797 outs() << "\n";
4799 outs() << "\t\t\t type ";
4800 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4801 n_value, i.name);
4802 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4803 if (n_value != 0) {
4804 if (info->verbose && sym_name != nullptr)
4805 outs() << sym_name;
4806 else
4807 outs() << format("0x%" PRIx64, n_value);
4808 if (i.type != 0)
4809 outs() << " + " << format("0x%" PRIx64, i.type);
4810 } else
4811 outs() << format("0x%" PRIx64, i.type);
4812 if (name != nullptr)
4813 outs() << format(" %.*s", left, name);
4814 outs() << "\n";
4816 outs() << "\t\t\talignment " << i.alignment << "\n";
4817 outs() << "\t\t\t size " << i.size << "\n";
4819 p += sizeof(struct ivar64_t);
4820 offset += sizeof(struct ivar64_t);
4824 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4825 struct ivar_list32_t il;
4826 struct ivar32_t i;
4827 const char *r;
4828 uint32_t offset, xoffset, left, j;
4829 SectionRef S, xS;
4830 const char *name, *ivar_offset_p;
4831 uint32_t ivar_offset;
4833 r = get_pointer_32(p, offset, left, S, info);
4834 if (r == nullptr)
4835 return;
4836 memset(&il, '\0', sizeof(struct ivar_list32_t));
4837 if (left < sizeof(struct ivar_list32_t)) {
4838 memcpy(&il, r, left);
4839 outs() << " (ivar_list_t entends past the end of the section)\n";
4840 } else
4841 memcpy(&il, r, sizeof(struct ivar_list32_t));
4842 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4843 swapStruct(il);
4844 outs() << " entsize " << il.entsize << "\n";
4845 outs() << " count " << il.count << "\n";
4847 p += sizeof(struct ivar_list32_t);
4848 offset += sizeof(struct ivar_list32_t);
4849 for (j = 0; j < il.count; j++) {
4850 r = get_pointer_32(p, offset, left, S, info);
4851 if (r == nullptr)
4852 return;
4853 memset(&i, '\0', sizeof(struct ivar32_t));
4854 if (left < sizeof(struct ivar32_t)) {
4855 memcpy(&i, r, left);
4856 outs() << " (ivar_t entends past the end of the section)\n";
4857 } else
4858 memcpy(&i, r, sizeof(struct ivar32_t));
4859 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4860 swapStruct(i);
4862 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4863 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4864 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4865 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4866 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4867 sys::swapByteOrder(ivar_offset);
4868 outs() << " " << ivar_offset << "\n";
4869 } else
4870 outs() << "\n";
4872 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4873 name = get_pointer_32(i.name, xoffset, left, xS, info);
4874 if (name != nullptr)
4875 outs() << format(" %.*s", left, name);
4876 outs() << "\n";
4878 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4879 name = get_pointer_32(i.type, xoffset, left, xS, info);
4880 if (name != nullptr)
4881 outs() << format(" %.*s", left, name);
4882 outs() << "\n";
4884 outs() << "\t\t\talignment " << i.alignment << "\n";
4885 outs() << "\t\t\t size " << i.size << "\n";
4887 p += sizeof(struct ivar32_t);
4888 offset += sizeof(struct ivar32_t);
4892 static void print_objc_property_list64(uint64_t p,
4893 struct DisassembleInfo *info) {
4894 struct objc_property_list64 opl;
4895 struct objc_property64 op;
4896 const char *r;
4897 uint32_t offset, xoffset, left, j;
4898 SectionRef S, xS;
4899 const char *name, *sym_name;
4900 uint64_t n_value;
4902 r = get_pointer_64(p, offset, left, S, info);
4903 if (r == nullptr)
4904 return;
4905 memset(&opl, '\0', sizeof(struct objc_property_list64));
4906 if (left < sizeof(struct objc_property_list64)) {
4907 memcpy(&opl, r, left);
4908 outs() << " (objc_property_list entends past the end of the section)\n";
4909 } else
4910 memcpy(&opl, r, sizeof(struct objc_property_list64));
4911 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4912 swapStruct(opl);
4913 outs() << " entsize " << opl.entsize << "\n";
4914 outs() << " count " << opl.count << "\n";
4916 p += sizeof(struct objc_property_list64);
4917 offset += sizeof(struct objc_property_list64);
4918 for (j = 0; j < opl.count; j++) {
4919 r = get_pointer_64(p, offset, left, S, info);
4920 if (r == nullptr)
4921 return;
4922 memset(&op, '\0', sizeof(struct objc_property64));
4923 if (left < sizeof(struct objc_property64)) {
4924 memcpy(&op, r, left);
4925 outs() << " (objc_property entends past the end of the section)\n";
4926 } else
4927 memcpy(&op, r, sizeof(struct objc_property64));
4928 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4929 swapStruct(op);
4931 outs() << "\t\t\t name ";
4932 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4933 info, n_value, op.name);
4934 if (n_value != 0) {
4935 if (info->verbose && sym_name != nullptr)
4936 outs() << sym_name;
4937 else
4938 outs() << format("0x%" PRIx64, n_value);
4939 if (op.name != 0)
4940 outs() << " + " << format("0x%" PRIx64, op.name);
4941 } else
4942 outs() << format("0x%" PRIx64, op.name);
4943 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4944 if (name != nullptr)
4945 outs() << format(" %.*s", left, name);
4946 outs() << "\n";
4948 outs() << "\t\t\tattributes ";
4949 sym_name =
4950 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4951 info, n_value, op.attributes);
4952 if (n_value != 0) {
4953 if (info->verbose && sym_name != nullptr)
4954 outs() << sym_name;
4955 else
4956 outs() << format("0x%" PRIx64, n_value);
4957 if (op.attributes != 0)
4958 outs() << " + " << format("0x%" PRIx64, op.attributes);
4959 } else
4960 outs() << format("0x%" PRIx64, op.attributes);
4961 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4962 if (name != nullptr)
4963 outs() << format(" %.*s", left, name);
4964 outs() << "\n";
4966 p += sizeof(struct objc_property64);
4967 offset += sizeof(struct objc_property64);
4971 static void print_objc_property_list32(uint32_t p,
4972 struct DisassembleInfo *info) {
4973 struct objc_property_list32 opl;
4974 struct objc_property32 op;
4975 const char *r;
4976 uint32_t offset, xoffset, left, j;
4977 SectionRef S, xS;
4978 const char *name;
4980 r = get_pointer_32(p, offset, left, S, info);
4981 if (r == nullptr)
4982 return;
4983 memset(&opl, '\0', sizeof(struct objc_property_list32));
4984 if (left < sizeof(struct objc_property_list32)) {
4985 memcpy(&opl, r, left);
4986 outs() << " (objc_property_list entends past the end of the section)\n";
4987 } else
4988 memcpy(&opl, r, sizeof(struct objc_property_list32));
4989 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4990 swapStruct(opl);
4991 outs() << " entsize " << opl.entsize << "\n";
4992 outs() << " count " << opl.count << "\n";
4994 p += sizeof(struct objc_property_list32);
4995 offset += sizeof(struct objc_property_list32);
4996 for (j = 0; j < opl.count; j++) {
4997 r = get_pointer_32(p, offset, left, S, info);
4998 if (r == nullptr)
4999 return;
5000 memset(&op, '\0', sizeof(struct objc_property32));
5001 if (left < sizeof(struct objc_property32)) {
5002 memcpy(&op, r, left);
5003 outs() << " (objc_property entends past the end of the section)\n";
5004 } else
5005 memcpy(&op, r, sizeof(struct objc_property32));
5006 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5007 swapStruct(op);
5009 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
5010 name = get_pointer_32(op.name, xoffset, left, xS, info);
5011 if (name != nullptr)
5012 outs() << format(" %.*s", left, name);
5013 outs() << "\n";
5015 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
5016 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
5017 if (name != nullptr)
5018 outs() << format(" %.*s", left, name);
5019 outs() << "\n";
5021 p += sizeof(struct objc_property32);
5022 offset += sizeof(struct objc_property32);
5026 static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
5027 bool &is_meta_class) {
5028 struct class_ro64_t cro;
5029 const char *r;
5030 uint32_t offset, xoffset, left;
5031 SectionRef S, xS;
5032 const char *name, *sym_name;
5033 uint64_t n_value;
5035 r = get_pointer_64(p, offset, left, S, info);
5036 if (r == nullptr || left < sizeof(struct class_ro64_t))
5037 return false;
5038 memcpy(&cro, r, sizeof(struct class_ro64_t));
5039 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5040 swapStruct(cro);
5041 outs() << " flags " << format("0x%" PRIx32, cro.flags);
5042 if (cro.flags & RO_META)
5043 outs() << " RO_META";
5044 if (cro.flags & RO_ROOT)
5045 outs() << " RO_ROOT";
5046 if (cro.flags & RO_HAS_CXX_STRUCTORS)
5047 outs() << " RO_HAS_CXX_STRUCTORS";
5048 outs() << "\n";
5049 outs() << " instanceStart " << cro.instanceStart << "\n";
5050 outs() << " instanceSize " << cro.instanceSize << "\n";
5051 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
5052 << "\n";
5053 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
5054 << "\n";
5055 print_layout_map64(cro.ivarLayout, info);
5057 outs() << " name ";
5058 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
5059 info, n_value, cro.name);
5060 if (n_value != 0) {
5061 if (info->verbose && sym_name != nullptr)
5062 outs() << sym_name;
5063 else
5064 outs() << format("0x%" PRIx64, n_value);
5065 if (cro.name != 0)
5066 outs() << " + " << format("0x%" PRIx64, cro.name);
5067 } else
5068 outs() << format("0x%" PRIx64, cro.name);
5069 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
5070 if (name != nullptr)
5071 outs() << format(" %.*s", left, name);
5072 outs() << "\n";
5074 outs() << " baseMethods ";
5075 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
5076 S, info, n_value, cro.baseMethods);
5077 if (n_value != 0) {
5078 if (info->verbose && sym_name != nullptr)
5079 outs() << sym_name;
5080 else
5081 outs() << format("0x%" PRIx64, n_value);
5082 if (cro.baseMethods != 0)
5083 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
5084 } else
5085 outs() << format("0x%" PRIx64, cro.baseMethods);
5086 outs() << " (struct method_list_t *)\n";
5087 if (cro.baseMethods + n_value != 0)
5088 print_method_list64_t(cro.baseMethods + n_value, info, "");
5090 outs() << " baseProtocols ";
5091 sym_name =
5092 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
5093 info, n_value, cro.baseProtocols);
5094 if (n_value != 0) {
5095 if (info->verbose && sym_name != nullptr)
5096 outs() << sym_name;
5097 else
5098 outs() << format("0x%" PRIx64, n_value);
5099 if (cro.baseProtocols != 0)
5100 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
5101 } else
5102 outs() << format("0x%" PRIx64, cro.baseProtocols);
5103 outs() << "\n";
5104 if (cro.baseProtocols + n_value != 0)
5105 print_protocol_list64_t(cro.baseProtocols + n_value, info);
5107 outs() << " ivars ";
5108 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
5109 info, n_value, cro.ivars);
5110 if (n_value != 0) {
5111 if (info->verbose && sym_name != nullptr)
5112 outs() << sym_name;
5113 else
5114 outs() << format("0x%" PRIx64, n_value);
5115 if (cro.ivars != 0)
5116 outs() << " + " << format("0x%" PRIx64, cro.ivars);
5117 } else
5118 outs() << format("0x%" PRIx64, cro.ivars);
5119 outs() << "\n";
5120 if (cro.ivars + n_value != 0)
5121 print_ivar_list64_t(cro.ivars + n_value, info);
5123 outs() << " weakIvarLayout ";
5124 sym_name =
5125 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
5126 info, n_value, cro.weakIvarLayout);
5127 if (n_value != 0) {
5128 if (info->verbose && sym_name != nullptr)
5129 outs() << sym_name;
5130 else
5131 outs() << format("0x%" PRIx64, n_value);
5132 if (cro.weakIvarLayout != 0)
5133 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
5134 } else
5135 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
5136 outs() << "\n";
5137 print_layout_map64(cro.weakIvarLayout + n_value, info);
5139 outs() << " baseProperties ";
5140 sym_name =
5141 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
5142 info, n_value, cro.baseProperties);
5143 if (n_value != 0) {
5144 if (info->verbose && sym_name != nullptr)
5145 outs() << sym_name;
5146 else
5147 outs() << format("0x%" PRIx64, n_value);
5148 if (cro.baseProperties != 0)
5149 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
5150 } else
5151 outs() << format("0x%" PRIx64, cro.baseProperties);
5152 outs() << "\n";
5153 if (cro.baseProperties + n_value != 0)
5154 print_objc_property_list64(cro.baseProperties + n_value, info);
5156 is_meta_class = (cro.flags & RO_META) != 0;
5157 return true;
5160 static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
5161 bool &is_meta_class) {
5162 struct class_ro32_t cro;
5163 const char *r;
5164 uint32_t offset, xoffset, left;
5165 SectionRef S, xS;
5166 const char *name;
5168 r = get_pointer_32(p, offset, left, S, info);
5169 if (r == nullptr)
5170 return false;
5171 memset(&cro, '\0', sizeof(struct class_ro32_t));
5172 if (left < sizeof(struct class_ro32_t)) {
5173 memcpy(&cro, r, left);
5174 outs() << " (class_ro_t entends past the end of the section)\n";
5175 } else
5176 memcpy(&cro, r, sizeof(struct class_ro32_t));
5177 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5178 swapStruct(cro);
5179 outs() << " flags " << format("0x%" PRIx32, cro.flags);
5180 if (cro.flags & RO_META)
5181 outs() << " RO_META";
5182 if (cro.flags & RO_ROOT)
5183 outs() << " RO_ROOT";
5184 if (cro.flags & RO_HAS_CXX_STRUCTORS)
5185 outs() << " RO_HAS_CXX_STRUCTORS";
5186 outs() << "\n";
5187 outs() << " instanceStart " << cro.instanceStart << "\n";
5188 outs() << " instanceSize " << cro.instanceSize << "\n";
5189 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
5190 << "\n";
5191 print_layout_map32(cro.ivarLayout, info);
5193 outs() << " name " << format("0x%" PRIx32, cro.name);
5194 name = get_pointer_32(cro.name, xoffset, left, xS, info);
5195 if (name != nullptr)
5196 outs() << format(" %.*s", left, name);
5197 outs() << "\n";
5199 outs() << " baseMethods "
5200 << format("0x%" PRIx32, cro.baseMethods)
5201 << " (struct method_list_t *)\n";
5202 if (cro.baseMethods != 0)
5203 print_method_list32_t(cro.baseMethods, info, "");
5205 outs() << " baseProtocols "
5206 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
5207 if (cro.baseProtocols != 0)
5208 print_protocol_list32_t(cro.baseProtocols, info);
5209 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
5210 << "\n";
5211 if (cro.ivars != 0)
5212 print_ivar_list32_t(cro.ivars, info);
5213 outs() << " weakIvarLayout "
5214 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
5215 print_layout_map32(cro.weakIvarLayout, info);
5216 outs() << " baseProperties "
5217 << format("0x%" PRIx32, cro.baseProperties) << "\n";
5218 if (cro.baseProperties != 0)
5219 print_objc_property_list32(cro.baseProperties, info);
5220 is_meta_class = (cro.flags & RO_META) != 0;
5221 return true;
5224 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
5225 struct class64_t c;
5226 const char *r;
5227 uint32_t offset, left;
5228 SectionRef S;
5229 const char *name;
5230 uint64_t isa_n_value, n_value;
5232 r = get_pointer_64(p, offset, left, S, info);
5233 if (r == nullptr || left < sizeof(struct class64_t))
5234 return;
5235 memcpy(&c, r, sizeof(struct class64_t));
5236 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5237 swapStruct(c);
5239 outs() << " isa " << format("0x%" PRIx64, c.isa);
5240 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
5241 isa_n_value, c.isa);
5242 if (name != nullptr)
5243 outs() << " " << name;
5244 outs() << "\n";
5246 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
5247 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
5248 n_value, c.superclass);
5249 if (name != nullptr)
5250 outs() << " " << name;
5251 else {
5252 name = get_dyld_bind_info_symbolname(S.getAddress() +
5253 offset + offsetof(struct class64_t, superclass), info);
5254 if (name != nullptr)
5255 outs() << " " << name;
5257 outs() << "\n";
5259 outs() << " cache " << format("0x%" PRIx64, c.cache);
5260 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
5261 n_value, c.cache);
5262 if (name != nullptr)
5263 outs() << " " << name;
5264 outs() << "\n";
5266 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
5267 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
5268 n_value, c.vtable);
5269 if (name != nullptr)
5270 outs() << " " << name;
5271 outs() << "\n";
5273 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
5274 n_value, c.data);
5275 outs() << " data ";
5276 if (n_value != 0) {
5277 if (info->verbose && name != nullptr)
5278 outs() << name;
5279 else
5280 outs() << format("0x%" PRIx64, n_value);
5281 if (c.data != 0)
5282 outs() << " + " << format("0x%" PRIx64, c.data);
5283 } else
5284 outs() << format("0x%" PRIx64, c.data);
5285 outs() << " (struct class_ro_t *)";
5287 // This is a Swift class if some of the low bits of the pointer are set.
5288 if ((c.data + n_value) & 0x7)
5289 outs() << " Swift class";
5290 outs() << "\n";
5291 bool is_meta_class;
5292 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
5293 return;
5295 if (!is_meta_class &&
5296 c.isa + isa_n_value != p &&
5297 c.isa + isa_n_value != 0 &&
5298 info->depth < 100) {
5299 info->depth++;
5300 outs() << "Meta Class\n";
5301 print_class64_t(c.isa + isa_n_value, info);
5305 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
5306 struct class32_t c;
5307 const char *r;
5308 uint32_t offset, left;
5309 SectionRef S;
5310 const char *name;
5312 r = get_pointer_32(p, offset, left, S, info);
5313 if (r == nullptr)
5314 return;
5315 memset(&c, '\0', sizeof(struct class32_t));
5316 if (left < sizeof(struct class32_t)) {
5317 memcpy(&c, r, left);
5318 outs() << " (class_t entends past the end of the section)\n";
5319 } else
5320 memcpy(&c, r, sizeof(struct class32_t));
5321 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5322 swapStruct(c);
5324 outs() << " isa " << format("0x%" PRIx32, c.isa);
5325 name =
5326 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
5327 if (name != nullptr)
5328 outs() << " " << name;
5329 outs() << "\n";
5331 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
5332 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
5333 c.superclass);
5334 if (name != nullptr)
5335 outs() << " " << name;
5336 outs() << "\n";
5338 outs() << " cache " << format("0x%" PRIx32, c.cache);
5339 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
5340 c.cache);
5341 if (name != nullptr)
5342 outs() << " " << name;
5343 outs() << "\n";
5345 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
5346 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
5347 c.vtable);
5348 if (name != nullptr)
5349 outs() << " " << name;
5350 outs() << "\n";
5352 name =
5353 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
5354 outs() << " data " << format("0x%" PRIx32, c.data)
5355 << " (struct class_ro_t *)";
5357 // This is a Swift class if some of the low bits of the pointer are set.
5358 if (c.data & 0x3)
5359 outs() << " Swift class";
5360 outs() << "\n";
5361 bool is_meta_class;
5362 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
5363 return;
5365 if (!is_meta_class) {
5366 outs() << "Meta Class\n";
5367 print_class32_t(c.isa, info);
5371 static void print_objc_class_t(struct objc_class_t *objc_class,
5372 struct DisassembleInfo *info) {
5373 uint32_t offset, left, xleft;
5374 const char *name, *p, *ivar_list;
5375 SectionRef S;
5376 int32_t i;
5377 struct objc_ivar_list_t objc_ivar_list;
5378 struct objc_ivar_t ivar;
5380 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
5381 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
5382 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
5383 if (name != nullptr)
5384 outs() << format(" %.*s", left, name);
5385 else
5386 outs() << " (not in an __OBJC section)";
5388 outs() << "\n";
5390 outs() << "\t super_class "
5391 << format("0x%08" PRIx32, objc_class->super_class);
5392 if (info->verbose) {
5393 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
5394 if (name != nullptr)
5395 outs() << format(" %.*s", left, name);
5396 else
5397 outs() << " (not in an __OBJC section)";
5399 outs() << "\n";
5401 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
5402 if (info->verbose) {
5403 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
5404 if (name != nullptr)
5405 outs() << format(" %.*s", left, name);
5406 else
5407 outs() << " (not in an __OBJC section)";
5409 outs() << "\n";
5411 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
5412 << "\n";
5414 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
5415 if (info->verbose) {
5416 if (CLS_GETINFO(objc_class, CLS_CLASS))
5417 outs() << " CLS_CLASS";
5418 else if (CLS_GETINFO(objc_class, CLS_META))
5419 outs() << " CLS_META";
5421 outs() << "\n";
5423 outs() << "\t instance_size "
5424 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
5426 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
5427 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
5428 if (p != nullptr) {
5429 if (left > sizeof(struct objc_ivar_list_t)) {
5430 outs() << "\n";
5431 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
5432 } else {
5433 outs() << " (entends past the end of the section)\n";
5434 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
5435 memcpy(&objc_ivar_list, p, left);
5437 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5438 swapStruct(objc_ivar_list);
5439 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
5440 ivar_list = p + sizeof(struct objc_ivar_list_t);
5441 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
5442 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
5443 outs() << "\t\t remaining ivar's extend past the of the section\n";
5444 break;
5446 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
5447 sizeof(struct objc_ivar_t));
5448 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5449 swapStruct(ivar);
5451 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
5452 if (info->verbose) {
5453 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
5454 if (name != nullptr)
5455 outs() << format(" %.*s", xleft, name);
5456 else
5457 outs() << " (not in an __OBJC section)";
5459 outs() << "\n";
5461 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
5462 if (info->verbose) {
5463 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
5464 if (name != nullptr)
5465 outs() << format(" %.*s", xleft, name);
5466 else
5467 outs() << " (not in an __OBJC section)";
5469 outs() << "\n";
5471 outs() << "\t\t ivar_offset "
5472 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
5474 } else {
5475 outs() << " (not in an __OBJC section)\n";
5478 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
5479 if (print_method_list(objc_class->methodLists, info))
5480 outs() << " (not in an __OBJC section)\n";
5482 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
5483 << "\n";
5485 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
5486 if (print_protocol_list(objc_class->protocols, 16, info))
5487 outs() << " (not in an __OBJC section)\n";
5490 static void print_objc_objc_category_t(struct objc_category_t *objc_category,
5491 struct DisassembleInfo *info) {
5492 uint32_t offset, left;
5493 const char *name;
5494 SectionRef S;
5496 outs() << "\t category name "
5497 << format("0x%08" PRIx32, objc_category->category_name);
5498 if (info->verbose) {
5499 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
5500 true);
5501 if (name != nullptr)
5502 outs() << format(" %.*s", left, name);
5503 else
5504 outs() << " (not in an __OBJC section)";
5506 outs() << "\n";
5508 outs() << "\t\t class name "
5509 << format("0x%08" PRIx32, objc_category->class_name);
5510 if (info->verbose) {
5511 name =
5512 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
5513 if (name != nullptr)
5514 outs() << format(" %.*s", left, name);
5515 else
5516 outs() << " (not in an __OBJC section)";
5518 outs() << "\n";
5520 outs() << "\t instance methods "
5521 << format("0x%08" PRIx32, objc_category->instance_methods);
5522 if (print_method_list(objc_category->instance_methods, info))
5523 outs() << " (not in an __OBJC section)\n";
5525 outs() << "\t class methods "
5526 << format("0x%08" PRIx32, objc_category->class_methods);
5527 if (print_method_list(objc_category->class_methods, info))
5528 outs() << " (not in an __OBJC section)\n";
5531 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
5532 struct category64_t c;
5533 const char *r;
5534 uint32_t offset, xoffset, left;
5535 SectionRef S, xS;
5536 const char *name, *sym_name;
5537 uint64_t n_value;
5539 r = get_pointer_64(p, offset, left, S, info);
5540 if (r == nullptr)
5541 return;
5542 memset(&c, '\0', sizeof(struct category64_t));
5543 if (left < sizeof(struct category64_t)) {
5544 memcpy(&c, r, left);
5545 outs() << " (category_t entends past the end of the section)\n";
5546 } else
5547 memcpy(&c, r, sizeof(struct category64_t));
5548 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5549 swapStruct(c);
5551 outs() << " name ";
5552 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
5553 info, n_value, c.name);
5554 if (n_value != 0) {
5555 if (info->verbose && sym_name != nullptr)
5556 outs() << sym_name;
5557 else
5558 outs() << format("0x%" PRIx64, n_value);
5559 if (c.name != 0)
5560 outs() << " + " << format("0x%" PRIx64, c.name);
5561 } else
5562 outs() << format("0x%" PRIx64, c.name);
5563 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
5564 if (name != nullptr)
5565 outs() << format(" %.*s", left, name);
5566 outs() << "\n";
5568 outs() << " cls ";
5569 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
5570 n_value, c.cls);
5571 if (n_value != 0) {
5572 if (info->verbose && sym_name != nullptr)
5573 outs() << sym_name;
5574 else
5575 outs() << format("0x%" PRIx64, n_value);
5576 if (c.cls != 0)
5577 outs() << " + " << format("0x%" PRIx64, c.cls);
5578 } else
5579 outs() << format("0x%" PRIx64, c.cls);
5580 outs() << "\n";
5581 if (c.cls + n_value != 0)
5582 print_class64_t(c.cls + n_value, info);
5584 outs() << " instanceMethods ";
5585 sym_name =
5586 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
5587 info, n_value, c.instanceMethods);
5588 if (n_value != 0) {
5589 if (info->verbose && sym_name != nullptr)
5590 outs() << sym_name;
5591 else
5592 outs() << format("0x%" PRIx64, n_value);
5593 if (c.instanceMethods != 0)
5594 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
5595 } else
5596 outs() << format("0x%" PRIx64, c.instanceMethods);
5597 outs() << "\n";
5598 if (c.instanceMethods + n_value != 0)
5599 print_method_list64_t(c.instanceMethods + n_value, info, "");
5601 outs() << " classMethods ";
5602 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
5603 S, info, n_value, c.classMethods);
5604 if (n_value != 0) {
5605 if (info->verbose && sym_name != nullptr)
5606 outs() << sym_name;
5607 else
5608 outs() << format("0x%" PRIx64, n_value);
5609 if (c.classMethods != 0)
5610 outs() << " + " << format("0x%" PRIx64, c.classMethods);
5611 } else
5612 outs() << format("0x%" PRIx64, c.classMethods);
5613 outs() << "\n";
5614 if (c.classMethods + n_value != 0)
5615 print_method_list64_t(c.classMethods + n_value, info, "");
5617 outs() << " protocols ";
5618 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
5619 info, n_value, c.protocols);
5620 if (n_value != 0) {
5621 if (info->verbose && sym_name != nullptr)
5622 outs() << sym_name;
5623 else
5624 outs() << format("0x%" PRIx64, n_value);
5625 if (c.protocols != 0)
5626 outs() << " + " << format("0x%" PRIx64, c.protocols);
5627 } else
5628 outs() << format("0x%" PRIx64, c.protocols);
5629 outs() << "\n";
5630 if (c.protocols + n_value != 0)
5631 print_protocol_list64_t(c.protocols + n_value, info);
5633 outs() << "instanceProperties ";
5634 sym_name =
5635 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
5636 S, info, n_value, c.instanceProperties);
5637 if (n_value != 0) {
5638 if (info->verbose && sym_name != nullptr)
5639 outs() << sym_name;
5640 else
5641 outs() << format("0x%" PRIx64, n_value);
5642 if (c.instanceProperties != 0)
5643 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
5644 } else
5645 outs() << format("0x%" PRIx64, c.instanceProperties);
5646 outs() << "\n";
5647 if (c.instanceProperties + n_value != 0)
5648 print_objc_property_list64(c.instanceProperties + n_value, info);
5651 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
5652 struct category32_t c;
5653 const char *r;
5654 uint32_t offset, left;
5655 SectionRef S, xS;
5656 const char *name;
5658 r = get_pointer_32(p, offset, left, S, info);
5659 if (r == nullptr)
5660 return;
5661 memset(&c, '\0', sizeof(struct category32_t));
5662 if (left < sizeof(struct category32_t)) {
5663 memcpy(&c, r, left);
5664 outs() << " (category_t entends past the end of the section)\n";
5665 } else
5666 memcpy(&c, r, sizeof(struct category32_t));
5667 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5668 swapStruct(c);
5670 outs() << " name " << format("0x%" PRIx32, c.name);
5671 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
5672 c.name);
5673 if (name)
5674 outs() << " " << name;
5675 outs() << "\n";
5677 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
5678 if (c.cls != 0)
5679 print_class32_t(c.cls, info);
5680 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
5681 << "\n";
5682 if (c.instanceMethods != 0)
5683 print_method_list32_t(c.instanceMethods, info, "");
5684 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
5685 << "\n";
5686 if (c.classMethods != 0)
5687 print_method_list32_t(c.classMethods, info, "");
5688 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
5689 if (c.protocols != 0)
5690 print_protocol_list32_t(c.protocols, info);
5691 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
5692 << "\n";
5693 if (c.instanceProperties != 0)
5694 print_objc_property_list32(c.instanceProperties, info);
5697 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
5698 uint32_t i, left, offset, xoffset;
5699 uint64_t p, n_value;
5700 struct message_ref64 mr;
5701 const char *name, *sym_name;
5702 const char *r;
5703 SectionRef xS;
5705 if (S == SectionRef())
5706 return;
5708 StringRef SectName;
5709 S.getName(SectName);
5710 DataRefImpl Ref = S.getRawDataRefImpl();
5711 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5712 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5713 offset = 0;
5714 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5715 p = S.getAddress() + i;
5716 r = get_pointer_64(p, offset, left, S, info);
5717 if (r == nullptr)
5718 return;
5719 memset(&mr, '\0', sizeof(struct message_ref64));
5720 if (left < sizeof(struct message_ref64)) {
5721 memcpy(&mr, r, left);
5722 outs() << " (message_ref entends past the end of the section)\n";
5723 } else
5724 memcpy(&mr, r, sizeof(struct message_ref64));
5725 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5726 swapStruct(mr);
5728 outs() << " imp ";
5729 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
5730 n_value, mr.imp);
5731 if (n_value != 0) {
5732 outs() << format("0x%" PRIx64, n_value) << " ";
5733 if (mr.imp != 0)
5734 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
5735 } else
5736 outs() << format("0x%" PRIx64, mr.imp) << " ";
5737 if (name != nullptr)
5738 outs() << " " << name;
5739 outs() << "\n";
5741 outs() << " sel ";
5742 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
5743 info, n_value, mr.sel);
5744 if (n_value != 0) {
5745 if (info->verbose && sym_name != nullptr)
5746 outs() << sym_name;
5747 else
5748 outs() << format("0x%" PRIx64, n_value);
5749 if (mr.sel != 0)
5750 outs() << " + " << format("0x%" PRIx64, mr.sel);
5751 } else
5752 outs() << format("0x%" PRIx64, mr.sel);
5753 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5754 if (name != nullptr)
5755 outs() << format(" %.*s", left, name);
5756 outs() << "\n";
5758 offset += sizeof(struct message_ref64);
5762 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5763 uint32_t i, left, offset, xoffset, p;
5764 struct message_ref32 mr;
5765 const char *name, *r;
5766 SectionRef xS;
5768 if (S == SectionRef())
5769 return;
5771 StringRef SectName;
5772 S.getName(SectName);
5773 DataRefImpl Ref = S.getRawDataRefImpl();
5774 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5775 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5776 offset = 0;
5777 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5778 p = S.getAddress() + i;
5779 r = get_pointer_32(p, offset, left, S, info);
5780 if (r == nullptr)
5781 return;
5782 memset(&mr, '\0', sizeof(struct message_ref32));
5783 if (left < sizeof(struct message_ref32)) {
5784 memcpy(&mr, r, left);
5785 outs() << " (message_ref entends past the end of the section)\n";
5786 } else
5787 memcpy(&mr, r, sizeof(struct message_ref32));
5788 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5789 swapStruct(mr);
5791 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5792 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5793 mr.imp);
5794 if (name != nullptr)
5795 outs() << " " << name;
5796 outs() << "\n";
5798 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5799 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5800 if (name != nullptr)
5801 outs() << " " << name;
5802 outs() << "\n";
5804 offset += sizeof(struct message_ref32);
5808 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5809 uint32_t left, offset, swift_version;
5810 uint64_t p;
5811 struct objc_image_info64 o;
5812 const char *r;
5814 if (S == SectionRef())
5815 return;
5817 StringRef SectName;
5818 S.getName(SectName);
5819 DataRefImpl Ref = S.getRawDataRefImpl();
5820 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5821 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5822 p = S.getAddress();
5823 r = get_pointer_64(p, offset, left, S, info);
5824 if (r == nullptr)
5825 return;
5826 memset(&o, '\0', sizeof(struct objc_image_info64));
5827 if (left < sizeof(struct objc_image_info64)) {
5828 memcpy(&o, r, left);
5829 outs() << " (objc_image_info entends past the end of the section)\n";
5830 } else
5831 memcpy(&o, r, sizeof(struct objc_image_info64));
5832 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5833 swapStruct(o);
5834 outs() << " version " << o.version << "\n";
5835 outs() << " flags " << format("0x%" PRIx32, o.flags);
5836 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5837 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5838 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5839 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5840 if (o.flags & OBJC_IMAGE_IS_SIMULATED)
5841 outs() << " OBJC_IMAGE_IS_SIMULATED";
5842 if (o.flags & OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES)
5843 outs() << " OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES";
5844 swift_version = (o.flags >> 8) & 0xff;
5845 if (swift_version != 0) {
5846 if (swift_version == 1)
5847 outs() << " Swift 1.0";
5848 else if (swift_version == 2)
5849 outs() << " Swift 1.1";
5850 else if(swift_version == 3)
5851 outs() << " Swift 2.0";
5852 else if(swift_version == 4)
5853 outs() << " Swift 3.0";
5854 else if(swift_version == 5)
5855 outs() << " Swift 4.0";
5856 else if(swift_version == 6)
5857 outs() << " Swift 4.1/Swift 4.2";
5858 else if(swift_version == 7)
5859 outs() << " Swift 5 or later";
5860 else
5861 outs() << " unknown future Swift version (" << swift_version << ")";
5863 outs() << "\n";
5866 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5867 uint32_t left, offset, swift_version, p;
5868 struct objc_image_info32 o;
5869 const char *r;
5871 if (S == SectionRef())
5872 return;
5874 StringRef SectName;
5875 S.getName(SectName);
5876 DataRefImpl Ref = S.getRawDataRefImpl();
5877 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5878 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5879 p = S.getAddress();
5880 r = get_pointer_32(p, offset, left, S, info);
5881 if (r == nullptr)
5882 return;
5883 memset(&o, '\0', sizeof(struct objc_image_info32));
5884 if (left < sizeof(struct objc_image_info32)) {
5885 memcpy(&o, r, left);
5886 outs() << " (objc_image_info entends past the end of the section)\n";
5887 } else
5888 memcpy(&o, r, sizeof(struct objc_image_info32));
5889 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5890 swapStruct(o);
5891 outs() << " version " << o.version << "\n";
5892 outs() << " flags " << format("0x%" PRIx32, o.flags);
5893 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5894 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5895 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5896 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5897 swift_version = (o.flags >> 8) & 0xff;
5898 if (swift_version != 0) {
5899 if (swift_version == 1)
5900 outs() << " Swift 1.0";
5901 else if (swift_version == 2)
5902 outs() << " Swift 1.1";
5903 else if(swift_version == 3)
5904 outs() << " Swift 2.0";
5905 else if(swift_version == 4)
5906 outs() << " Swift 3.0";
5907 else if(swift_version == 5)
5908 outs() << " Swift 4.0";
5909 else if(swift_version == 6)
5910 outs() << " Swift 4.1/Swift 4.2";
5911 else if(swift_version == 7)
5912 outs() << " Swift 5 or later";
5913 else
5914 outs() << " unknown future Swift version (" << swift_version << ")";
5916 outs() << "\n";
5919 static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5920 uint32_t left, offset, p;
5921 struct imageInfo_t o;
5922 const char *r;
5924 StringRef SectName;
5925 S.getName(SectName);
5926 DataRefImpl Ref = S.getRawDataRefImpl();
5927 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5928 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5929 p = S.getAddress();
5930 r = get_pointer_32(p, offset, left, S, info);
5931 if (r == nullptr)
5932 return;
5933 memset(&o, '\0', sizeof(struct imageInfo_t));
5934 if (left < sizeof(struct imageInfo_t)) {
5935 memcpy(&o, r, left);
5936 outs() << " (imageInfo entends past the end of the section)\n";
5937 } else
5938 memcpy(&o, r, sizeof(struct imageInfo_t));
5939 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5940 swapStruct(o);
5941 outs() << " version " << o.version << "\n";
5942 outs() << " flags " << format("0x%" PRIx32, o.flags);
5943 if (o.flags & 0x1)
5944 outs() << " F&C";
5945 if (o.flags & 0x2)
5946 outs() << " GC";
5947 if (o.flags & 0x4)
5948 outs() << " GC-only";
5949 else
5950 outs() << " RR";
5951 outs() << "\n";
5954 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5955 SymbolAddressMap AddrMap;
5956 if (verbose)
5957 CreateSymbolAddressMap(O, &AddrMap);
5959 std::vector<SectionRef> Sections;
5960 for (const SectionRef &Section : O->sections()) {
5961 StringRef SectName;
5962 Section.getName(SectName);
5963 Sections.push_back(Section);
5966 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
5968 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5969 if (CL == SectionRef())
5970 CL = get_section(O, "__DATA", "__objc_classlist");
5971 if (CL == SectionRef())
5972 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
5973 if (CL == SectionRef())
5974 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
5975 info.S = CL;
5976 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5978 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5979 if (CR == SectionRef())
5980 CR = get_section(O, "__DATA", "__objc_classrefs");
5981 if (CR == SectionRef())
5982 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
5983 if (CR == SectionRef())
5984 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
5985 info.S = CR;
5986 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5988 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5989 if (SR == SectionRef())
5990 SR = get_section(O, "__DATA", "__objc_superrefs");
5991 if (SR == SectionRef())
5992 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
5993 if (SR == SectionRef())
5994 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
5995 info.S = SR;
5996 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5998 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5999 if (CA == SectionRef())
6000 CA = get_section(O, "__DATA", "__objc_catlist");
6001 if (CA == SectionRef())
6002 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
6003 if (CA == SectionRef())
6004 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
6005 info.S = CA;
6006 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
6008 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
6009 if (PL == SectionRef())
6010 PL = get_section(O, "__DATA", "__objc_protolist");
6011 if (PL == SectionRef())
6012 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
6013 if (PL == SectionRef())
6014 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
6015 info.S = PL;
6016 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
6018 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
6019 if (MR == SectionRef())
6020 MR = get_section(O, "__DATA", "__objc_msgrefs");
6021 if (MR == SectionRef())
6022 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
6023 if (MR == SectionRef())
6024 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
6025 info.S = MR;
6026 print_message_refs64(MR, &info);
6028 SectionRef II = get_section(O, "__OBJC2", "__image_info");
6029 if (II == SectionRef())
6030 II = get_section(O, "__DATA", "__objc_imageinfo");
6031 if (II == SectionRef())
6032 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
6033 if (II == SectionRef())
6034 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
6035 info.S = II;
6036 print_image_info64(II, &info);
6039 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
6040 SymbolAddressMap AddrMap;
6041 if (verbose)
6042 CreateSymbolAddressMap(O, &AddrMap);
6044 std::vector<SectionRef> Sections;
6045 for (const SectionRef &Section : O->sections()) {
6046 StringRef SectName;
6047 Section.getName(SectName);
6048 Sections.push_back(Section);
6051 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
6053 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
6054 if (CL == SectionRef())
6055 CL = get_section(O, "__DATA", "__objc_classlist");
6056 if (CL == SectionRef())
6057 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
6058 if (CL == SectionRef())
6059 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
6060 info.S = CL;
6061 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
6063 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
6064 if (CR == SectionRef())
6065 CR = get_section(O, "__DATA", "__objc_classrefs");
6066 if (CR == SectionRef())
6067 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
6068 if (CR == SectionRef())
6069 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
6070 info.S = CR;
6071 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
6073 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
6074 if (SR == SectionRef())
6075 SR = get_section(O, "__DATA", "__objc_superrefs");
6076 if (SR == SectionRef())
6077 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
6078 if (SR == SectionRef())
6079 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
6080 info.S = SR;
6081 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
6083 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
6084 if (CA == SectionRef())
6085 CA = get_section(O, "__DATA", "__objc_catlist");
6086 if (CA == SectionRef())
6087 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
6088 if (CA == SectionRef())
6089 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
6090 info.S = CA;
6091 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
6093 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
6094 if (PL == SectionRef())
6095 PL = get_section(O, "__DATA", "__objc_protolist");
6096 if (PL == SectionRef())
6097 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
6098 if (PL == SectionRef())
6099 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
6100 info.S = PL;
6101 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
6103 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
6104 if (MR == SectionRef())
6105 MR = get_section(O, "__DATA", "__objc_msgrefs");
6106 if (MR == SectionRef())
6107 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
6108 if (MR == SectionRef())
6109 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
6110 info.S = MR;
6111 print_message_refs32(MR, &info);
6113 SectionRef II = get_section(O, "__OBJC2", "__image_info");
6114 if (II == SectionRef())
6115 II = get_section(O, "__DATA", "__objc_imageinfo");
6116 if (II == SectionRef())
6117 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
6118 if (II == SectionRef())
6119 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
6120 info.S = II;
6121 print_image_info32(II, &info);
6124 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
6125 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
6126 const char *r, *name, *defs;
6127 struct objc_module_t module;
6128 SectionRef S, xS;
6129 struct objc_symtab_t symtab;
6130 struct objc_class_t objc_class;
6131 struct objc_category_t objc_category;
6133 outs() << "Objective-C segment\n";
6134 S = get_section(O, "__OBJC", "__module_info");
6135 if (S == SectionRef())
6136 return false;
6138 SymbolAddressMap AddrMap;
6139 if (verbose)
6140 CreateSymbolAddressMap(O, &AddrMap);
6142 std::vector<SectionRef> Sections;
6143 for (const SectionRef &Section : O->sections()) {
6144 StringRef SectName;
6145 Section.getName(SectName);
6146 Sections.push_back(Section);
6149 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
6151 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
6152 p = S.getAddress() + i;
6153 r = get_pointer_32(p, offset, left, S, &info, true);
6154 if (r == nullptr)
6155 return true;
6156 memset(&module, '\0', sizeof(struct objc_module_t));
6157 if (left < sizeof(struct objc_module_t)) {
6158 memcpy(&module, r, left);
6159 outs() << " (module extends past end of __module_info section)\n";
6160 } else
6161 memcpy(&module, r, sizeof(struct objc_module_t));
6162 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6163 swapStruct(module);
6165 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
6166 outs() << " version " << module.version << "\n";
6167 outs() << " size " << module.size << "\n";
6168 outs() << " name ";
6169 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
6170 if (name != nullptr)
6171 outs() << format("%.*s", left, name);
6172 else
6173 outs() << format("0x%08" PRIx32, module.name)
6174 << "(not in an __OBJC section)";
6175 outs() << "\n";
6177 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
6178 if (module.symtab == 0 || r == nullptr) {
6179 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
6180 << " (not in an __OBJC section)\n";
6181 continue;
6183 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
6184 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
6185 defs_left = 0;
6186 defs = nullptr;
6187 if (left < sizeof(struct objc_symtab_t)) {
6188 memcpy(&symtab, r, left);
6189 outs() << "\tsymtab extends past end of an __OBJC section)\n";
6190 } else {
6191 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
6192 if (left > sizeof(struct objc_symtab_t)) {
6193 defs_left = left - sizeof(struct objc_symtab_t);
6194 defs = r + sizeof(struct objc_symtab_t);
6197 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6198 swapStruct(symtab);
6200 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
6201 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
6202 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
6203 if (r == nullptr)
6204 outs() << " (not in an __OBJC section)";
6205 outs() << "\n";
6206 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
6207 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
6208 if (symtab.cls_def_cnt > 0)
6209 outs() << "\tClass Definitions\n";
6210 for (j = 0; j < symtab.cls_def_cnt; j++) {
6211 if ((j + 1) * sizeof(uint32_t) > defs_left) {
6212 outs() << "\t(remaining class defs entries entends past the end of the "
6213 << "section)\n";
6214 break;
6216 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
6217 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6218 sys::swapByteOrder(def);
6220 r = get_pointer_32(def, xoffset, left, xS, &info, true);
6221 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
6222 if (r != nullptr) {
6223 if (left > sizeof(struct objc_class_t)) {
6224 outs() << "\n";
6225 memcpy(&objc_class, r, sizeof(struct objc_class_t));
6226 } else {
6227 outs() << " (entends past the end of the section)\n";
6228 memset(&objc_class, '\0', sizeof(struct objc_class_t));
6229 memcpy(&objc_class, r, left);
6231 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6232 swapStruct(objc_class);
6233 print_objc_class_t(&objc_class, &info);
6234 } else {
6235 outs() << "(not in an __OBJC section)\n";
6238 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
6239 outs() << "\tMeta Class";
6240 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
6241 if (r != nullptr) {
6242 if (left > sizeof(struct objc_class_t)) {
6243 outs() << "\n";
6244 memcpy(&objc_class, r, sizeof(struct objc_class_t));
6245 } else {
6246 outs() << " (entends past the end of the section)\n";
6247 memset(&objc_class, '\0', sizeof(struct objc_class_t));
6248 memcpy(&objc_class, r, left);
6250 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6251 swapStruct(objc_class);
6252 print_objc_class_t(&objc_class, &info);
6253 } else {
6254 outs() << "(not in an __OBJC section)\n";
6258 if (symtab.cat_def_cnt > 0)
6259 outs() << "\tCategory Definitions\n";
6260 for (j = 0; j < symtab.cat_def_cnt; j++) {
6261 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
6262 outs() << "\t(remaining category defs entries entends past the end of "
6263 << "the section)\n";
6264 break;
6266 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
6267 sizeof(uint32_t));
6268 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6269 sys::swapByteOrder(def);
6271 r = get_pointer_32(def, xoffset, left, xS, &info, true);
6272 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
6273 << format("0x%08" PRIx32, def);
6274 if (r != nullptr) {
6275 if (left > sizeof(struct objc_category_t)) {
6276 outs() << "\n";
6277 memcpy(&objc_category, r, sizeof(struct objc_category_t));
6278 } else {
6279 outs() << " (entends past the end of the section)\n";
6280 memset(&objc_category, '\0', sizeof(struct objc_category_t));
6281 memcpy(&objc_category, r, left);
6283 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6284 swapStruct(objc_category);
6285 print_objc_objc_category_t(&objc_category, &info);
6286 } else {
6287 outs() << "(not in an __OBJC section)\n";
6291 const SectionRef II = get_section(O, "__OBJC", "__image_info");
6292 if (II != SectionRef())
6293 print_image_info(II, &info);
6295 return true;
6298 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
6299 uint32_t size, uint32_t addr) {
6300 SymbolAddressMap AddrMap;
6301 CreateSymbolAddressMap(O, &AddrMap);
6303 std::vector<SectionRef> Sections;
6304 for (const SectionRef &Section : O->sections()) {
6305 StringRef SectName;
6306 Section.getName(SectName);
6307 Sections.push_back(Section);
6310 struct DisassembleInfo info(O, &AddrMap, &Sections, true);
6312 const char *p;
6313 struct objc_protocol_t protocol;
6314 uint32_t left, paddr;
6315 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
6316 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
6317 left = size - (p - sect);
6318 if (left < sizeof(struct objc_protocol_t)) {
6319 outs() << "Protocol extends past end of __protocol section\n";
6320 memcpy(&protocol, p, left);
6321 } else
6322 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
6323 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6324 swapStruct(protocol);
6325 paddr = addr + (p - sect);
6326 outs() << "Protocol " << format("0x%" PRIx32, paddr);
6327 if (print_protocol(paddr, 0, &info))
6328 outs() << "(not in an __OBJC section)\n";
6332 #ifdef HAVE_LIBXAR
6333 inline void swapStruct(struct xar_header &xar) {
6334 sys::swapByteOrder(xar.magic);
6335 sys::swapByteOrder(xar.size);
6336 sys::swapByteOrder(xar.version);
6337 sys::swapByteOrder(xar.toc_length_compressed);
6338 sys::swapByteOrder(xar.toc_length_uncompressed);
6339 sys::swapByteOrder(xar.cksum_alg);
6342 static void PrintModeVerbose(uint32_t mode) {
6343 switch(mode & S_IFMT){
6344 case S_IFDIR:
6345 outs() << "d";
6346 break;
6347 case S_IFCHR:
6348 outs() << "c";
6349 break;
6350 case S_IFBLK:
6351 outs() << "b";
6352 break;
6353 case S_IFREG:
6354 outs() << "-";
6355 break;
6356 case S_IFLNK:
6357 outs() << "l";
6358 break;
6359 case S_IFSOCK:
6360 outs() << "s";
6361 break;
6362 default:
6363 outs() << "?";
6364 break;
6367 /* owner permissions */
6368 if(mode & S_IREAD)
6369 outs() << "r";
6370 else
6371 outs() << "-";
6372 if(mode & S_IWRITE)
6373 outs() << "w";
6374 else
6375 outs() << "-";
6376 if(mode & S_ISUID)
6377 outs() << "s";
6378 else if(mode & S_IEXEC)
6379 outs() << "x";
6380 else
6381 outs() << "-";
6383 /* group permissions */
6384 if(mode & (S_IREAD >> 3))
6385 outs() << "r";
6386 else
6387 outs() << "-";
6388 if(mode & (S_IWRITE >> 3))
6389 outs() << "w";
6390 else
6391 outs() << "-";
6392 if(mode & S_ISGID)
6393 outs() << "s";
6394 else if(mode & (S_IEXEC >> 3))
6395 outs() << "x";
6396 else
6397 outs() << "-";
6399 /* other permissions */
6400 if(mode & (S_IREAD >> 6))
6401 outs() << "r";
6402 else
6403 outs() << "-";
6404 if(mode & (S_IWRITE >> 6))
6405 outs() << "w";
6406 else
6407 outs() << "-";
6408 if(mode & S_ISVTX)
6409 outs() << "t";
6410 else if(mode & (S_IEXEC >> 6))
6411 outs() << "x";
6412 else
6413 outs() << "-";
6416 static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
6417 xar_file_t xf;
6418 const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
6419 char *endp;
6420 uint32_t mode_value;
6422 ScopedXarIter xi;
6423 if (!xi) {
6424 WithColor::error(errs(), "llvm-objdump")
6425 << "can't obtain an xar iterator for xar archive " << XarFilename
6426 << "\n";
6427 return;
6430 // Go through the xar's files.
6431 for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
6432 ScopedXarIter xp;
6433 if(!xp){
6434 WithColor::error(errs(), "llvm-objdump")
6435 << "can't obtain an xar iterator for xar archive " << XarFilename
6436 << "\n";
6437 return;
6439 type = nullptr;
6440 mode = nullptr;
6441 user = nullptr;
6442 group = nullptr;
6443 size = nullptr;
6444 mtime = nullptr;
6445 name = nullptr;
6446 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
6447 const char *val = nullptr;
6448 xar_prop_get(xf, key, &val);
6449 #if 0 // Useful for debugging.
6450 outs() << "key: " << key << " value: " << val << "\n";
6451 #endif
6452 if(strcmp(key, "type") == 0)
6453 type = val;
6454 if(strcmp(key, "mode") == 0)
6455 mode = val;
6456 if(strcmp(key, "user") == 0)
6457 user = val;
6458 if(strcmp(key, "group") == 0)
6459 group = val;
6460 if(strcmp(key, "data/size") == 0)
6461 size = val;
6462 if(strcmp(key, "mtime") == 0)
6463 mtime = val;
6464 if(strcmp(key, "name") == 0)
6465 name = val;
6467 if(mode != nullptr){
6468 mode_value = strtoul(mode, &endp, 8);
6469 if(*endp != '\0')
6470 outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
6471 if(strcmp(type, "file") == 0)
6472 mode_value |= S_IFREG;
6473 PrintModeVerbose(mode_value);
6474 outs() << " ";
6476 if(user != nullptr)
6477 outs() << format("%10s/", user);
6478 if(group != nullptr)
6479 outs() << format("%-10s ", group);
6480 if(size != nullptr)
6481 outs() << format("%7s ", size);
6482 if(mtime != nullptr){
6483 for(m = mtime; *m != 'T' && *m != '\0'; m++)
6484 outs() << *m;
6485 if(*m == 'T')
6486 m++;
6487 outs() << " ";
6488 for( ; *m != 'Z' && *m != '\0'; m++)
6489 outs() << *m;
6490 outs() << " ";
6492 if(name != nullptr)
6493 outs() << name;
6494 outs() << "\n";
6498 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
6499 uint32_t size, bool verbose,
6500 bool PrintXarHeader, bool PrintXarFileHeaders,
6501 std::string XarMemberName) {
6502 if(size < sizeof(struct xar_header)) {
6503 outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
6504 "of struct xar_header)\n";
6505 return;
6507 struct xar_header XarHeader;
6508 memcpy(&XarHeader, sect, sizeof(struct xar_header));
6509 if (sys::IsLittleEndianHost)
6510 swapStruct(XarHeader);
6511 if (PrintXarHeader) {
6512 if (!XarMemberName.empty())
6513 outs() << "In xar member " << XarMemberName << ": ";
6514 else
6515 outs() << "For (__LLVM,__bundle) section: ";
6516 outs() << "xar header\n";
6517 if (XarHeader.magic == XAR_HEADER_MAGIC)
6518 outs() << " magic XAR_HEADER_MAGIC\n";
6519 else
6520 outs() << " magic "
6521 << format_hex(XarHeader.magic, 10, true)
6522 << " (not XAR_HEADER_MAGIC)\n";
6523 outs() << " size " << XarHeader.size << "\n";
6524 outs() << " version " << XarHeader.version << "\n";
6525 outs() << " toc_length_compressed " << XarHeader.toc_length_compressed
6526 << "\n";
6527 outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
6528 << "\n";
6529 outs() << " cksum_alg ";
6530 switch (XarHeader.cksum_alg) {
6531 case XAR_CKSUM_NONE:
6532 outs() << "XAR_CKSUM_NONE\n";
6533 break;
6534 case XAR_CKSUM_SHA1:
6535 outs() << "XAR_CKSUM_SHA1\n";
6536 break;
6537 case XAR_CKSUM_MD5:
6538 outs() << "XAR_CKSUM_MD5\n";
6539 break;
6540 #ifdef XAR_CKSUM_SHA256
6541 case XAR_CKSUM_SHA256:
6542 outs() << "XAR_CKSUM_SHA256\n";
6543 break;
6544 #endif
6545 #ifdef XAR_CKSUM_SHA512
6546 case XAR_CKSUM_SHA512:
6547 outs() << "XAR_CKSUM_SHA512\n";
6548 break;
6549 #endif
6550 default:
6551 outs() << XarHeader.cksum_alg << "\n";
6555 SmallString<128> XarFilename;
6556 int FD;
6557 std::error_code XarEC =
6558 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
6559 if (XarEC) {
6560 WithColor::error(errs(), "llvm-objdump") << XarEC.message() << "\n";
6561 return;
6563 ToolOutputFile XarFile(XarFilename, FD);
6564 raw_fd_ostream &XarOut = XarFile.os();
6565 StringRef XarContents(sect, size);
6566 XarOut << XarContents;
6567 XarOut.close();
6568 if (XarOut.has_error())
6569 return;
6571 ScopedXarFile xar(XarFilename.c_str(), READ);
6572 if (!xar) {
6573 WithColor::error(errs(), "llvm-objdump")
6574 << "can't create temporary xar archive " << XarFilename << "\n";
6575 return;
6578 SmallString<128> TocFilename;
6579 std::error_code TocEC =
6580 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
6581 if (TocEC) {
6582 WithColor::error(errs(), "llvm-objdump") << TocEC.message() << "\n";
6583 return;
6585 xar_serialize(xar, TocFilename.c_str());
6587 if (PrintXarFileHeaders) {
6588 if (!XarMemberName.empty())
6589 outs() << "In xar member " << XarMemberName << ": ";
6590 else
6591 outs() << "For (__LLVM,__bundle) section: ";
6592 outs() << "xar archive files:\n";
6593 PrintXarFilesSummary(XarFilename.c_str(), xar);
6596 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
6597 MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
6598 if (std::error_code EC = FileOrErr.getError()) {
6599 WithColor::error(errs(), "llvm-objdump") << EC.message() << "\n";
6600 return;
6602 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
6604 if (!XarMemberName.empty())
6605 outs() << "In xar member " << XarMemberName << ": ";
6606 else
6607 outs() << "For (__LLVM,__bundle) section: ";
6608 outs() << "xar table of contents:\n";
6609 outs() << Buffer->getBuffer() << "\n";
6611 // TODO: Go through the xar's files.
6612 ScopedXarIter xi;
6613 if(!xi){
6614 WithColor::error(errs(), "llvm-objdump")
6615 << "can't obtain an xar iterator for xar archive "
6616 << XarFilename.c_str() << "\n";
6617 return;
6619 for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
6620 const char *key;
6621 const char *member_name, *member_type, *member_size_string;
6622 size_t member_size;
6624 ScopedXarIter xp;
6625 if(!xp){
6626 WithColor::error(errs(), "llvm-objdump")
6627 << "can't obtain an xar iterator for xar archive "
6628 << XarFilename.c_str() << "\n";
6629 return;
6631 member_name = NULL;
6632 member_type = NULL;
6633 member_size_string = NULL;
6634 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
6635 const char *val = nullptr;
6636 xar_prop_get(xf, key, &val);
6637 #if 0 // Useful for debugging.
6638 outs() << "key: " << key << " value: " << val << "\n";
6639 #endif
6640 if (strcmp(key, "name") == 0)
6641 member_name = val;
6642 if (strcmp(key, "type") == 0)
6643 member_type = val;
6644 if (strcmp(key, "data/size") == 0)
6645 member_size_string = val;
6648 * If we find a file with a name, date/size and type properties
6649 * and with the type being "file" see if that is a xar file.
6651 if (member_name != NULL && member_type != NULL &&
6652 strcmp(member_type, "file") == 0 &&
6653 member_size_string != NULL){
6654 // Extract the file into a buffer.
6655 char *endptr;
6656 member_size = strtoul(member_size_string, &endptr, 10);
6657 if (*endptr == '\0' && member_size != 0) {
6658 char *buffer;
6659 if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
6660 #if 0 // Useful for debugging.
6661 outs() << "xar member: " << member_name << " extracted\n";
6662 #endif
6663 // Set the XarMemberName we want to see printed in the header.
6664 std::string OldXarMemberName;
6665 // If XarMemberName is already set this is nested. So
6666 // save the old name and create the nested name.
6667 if (!XarMemberName.empty()) {
6668 OldXarMemberName = XarMemberName;
6669 XarMemberName =
6670 (Twine("[") + XarMemberName + "]" + member_name).str();
6671 } else {
6672 OldXarMemberName = "";
6673 XarMemberName = member_name;
6675 // See if this is could be a xar file (nested).
6676 if (member_size >= sizeof(struct xar_header)) {
6677 #if 0 // Useful for debugging.
6678 outs() << "could be a xar file: " << member_name << "\n";
6679 #endif
6680 memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
6681 if (sys::IsLittleEndianHost)
6682 swapStruct(XarHeader);
6683 if (XarHeader.magic == XAR_HEADER_MAGIC)
6684 DumpBitcodeSection(O, buffer, member_size, verbose,
6685 PrintXarHeader, PrintXarFileHeaders,
6686 XarMemberName);
6688 XarMemberName = OldXarMemberName;
6689 delete buffer;
6695 #endif // defined(HAVE_LIBXAR)
6697 static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
6698 if (O->is64Bit())
6699 printObjc2_64bit_MetaData(O, verbose);
6700 else {
6701 MachO::mach_header H;
6702 H = O->getHeader();
6703 if (H.cputype == MachO::CPU_TYPE_ARM)
6704 printObjc2_32bit_MetaData(O, verbose);
6705 else {
6706 // This is the 32-bit non-arm cputype case. Which is normally
6707 // the first Objective-C ABI. But it may be the case of a
6708 // binary for the iOS simulator which is the second Objective-C
6709 // ABI. In that case printObjc1_32bit_MetaData() will determine that
6710 // and return false.
6711 if (!printObjc1_32bit_MetaData(O, verbose))
6712 printObjc2_32bit_MetaData(O, verbose);
6717 // GuessLiteralPointer returns a string which for the item in the Mach-O file
6718 // for the address passed in as ReferenceValue for printing as a comment with
6719 // the instruction and also returns the corresponding type of that item
6720 // indirectly through ReferenceType.
6722 // If ReferenceValue is an address of literal cstring then a pointer to the
6723 // cstring is returned and ReferenceType is set to
6724 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6726 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6727 // Class ref that name is returned and the ReferenceType is set accordingly.
6729 // Lastly, literals which are Symbol address in a literal pool are looked for
6730 // and if found the symbol name is returned and ReferenceType is set to
6731 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6733 // If there is no item in the Mach-O file for the address passed in as
6734 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
6735 static const char *GuessLiteralPointer(uint64_t ReferenceValue,
6736 uint64_t ReferencePC,
6737 uint64_t *ReferenceType,
6738 struct DisassembleInfo *info) {
6739 // First see if there is an external relocation entry at the ReferencePC.
6740 if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
6741 uint64_t sect_addr = info->S.getAddress();
6742 uint64_t sect_offset = ReferencePC - sect_addr;
6743 bool reloc_found = false;
6744 DataRefImpl Rel;
6745 MachO::any_relocation_info RE;
6746 bool isExtern = false;
6747 SymbolRef Symbol;
6748 for (const RelocationRef &Reloc : info->S.relocations()) {
6749 uint64_t RelocOffset = Reloc.getOffset();
6750 if (RelocOffset == sect_offset) {
6751 Rel = Reloc.getRawDataRefImpl();
6752 RE = info->O->getRelocation(Rel);
6753 if (info->O->isRelocationScattered(RE))
6754 continue;
6755 isExtern = info->O->getPlainRelocationExternal(RE);
6756 if (isExtern) {
6757 symbol_iterator RelocSym = Reloc.getSymbol();
6758 Symbol = *RelocSym;
6760 reloc_found = true;
6761 break;
6764 // If there is an external relocation entry for a symbol in a section
6765 // then used that symbol's value for the value of the reference.
6766 if (reloc_found && isExtern) {
6767 if (info->O->getAnyRelocationPCRel(RE)) {
6768 unsigned Type = info->O->getAnyRelocationType(RE);
6769 if (Type == MachO::X86_64_RELOC_SIGNED) {
6770 ReferenceValue = Symbol.getValue();
6776 // Look for literals such as Objective-C CFStrings refs, Selector refs,
6777 // Message refs and Class refs.
6778 bool classref, selref, msgref, cfstring;
6779 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
6780 selref, msgref, cfstring);
6781 if (classref && pointer_value == 0) {
6782 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6783 // And the pointer_value in that section is typically zero as it will be
6784 // set by dyld as part of the "bind information".
6785 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
6786 if (name != nullptr) {
6787 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6788 const char *class_name = strrchr(name, '$');
6789 if (class_name != nullptr && class_name[1] == '_' &&
6790 class_name[2] != '\0') {
6791 info->class_name = class_name + 2;
6792 return name;
6797 if (classref) {
6798 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6799 const char *name =
6800 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
6801 if (name != nullptr)
6802 info->class_name = name;
6803 else
6804 name = "bad class ref";
6805 return name;
6808 if (cfstring) {
6809 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
6810 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
6811 return name;
6814 if (selref && pointer_value == 0)
6815 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
6817 if (pointer_value != 0)
6818 ReferenceValue = pointer_value;
6820 const char *name = GuessCstringPointer(ReferenceValue, info);
6821 if (name) {
6822 if (pointer_value != 0 && selref) {
6823 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
6824 info->selector_name = name;
6825 } else if (pointer_value != 0 && msgref) {
6826 info->class_name = nullptr;
6827 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
6828 info->selector_name = name;
6829 } else
6830 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
6831 return name;
6834 // Lastly look for an indirect symbol with this ReferenceValue which is in
6835 // a literal pool. If found return that symbol name.
6836 name = GuessIndirectSymbol(ReferenceValue, info);
6837 if (name) {
6838 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
6839 return name;
6842 return nullptr;
6845 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
6846 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
6847 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6848 // is created and returns the symbol name that matches the ReferenceValue or
6849 // nullptr if none. The ReferenceType is passed in for the IN type of
6850 // reference the instruction is making from the values in defined in the header
6851 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
6852 // Out type and the ReferenceName will also be set which is added as a comment
6853 // to the disassembled instruction.
6855 // If the symbol name is a C++ mangled name then the demangled name is
6856 // returned through ReferenceName and ReferenceType is set to
6857 // LLVMDisassembler_ReferenceType_DeMangled_Name .
6859 // When this is called to get a symbol name for a branch target then the
6860 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
6861 // SymbolValue will be looked for in the indirect symbol table to determine if
6862 // it is an address for a symbol stub. If so then the symbol name for that
6863 // stub is returned indirectly through ReferenceName and then ReferenceType is
6864 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
6866 // When this is called with an value loaded via a PC relative load then
6867 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
6868 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
6869 // or an Objective-C meta data reference. If so the output ReferenceType is
6870 // set to correspond to that as well as setting the ReferenceName.
6871 static const char *SymbolizerSymbolLookUp(void *DisInfo,
6872 uint64_t ReferenceValue,
6873 uint64_t *ReferenceType,
6874 uint64_t ReferencePC,
6875 const char **ReferenceName) {
6876 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
6877 // If no verbose symbolic information is wanted then just return nullptr.
6878 if (!info->verbose) {
6879 *ReferenceName = nullptr;
6880 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6881 return nullptr;
6884 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
6886 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
6887 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
6888 if (*ReferenceName != nullptr) {
6889 method_reference(info, ReferenceType, ReferenceName);
6890 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
6891 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
6892 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
6893 if (info->demangled_name != nullptr)
6894 free(info->demangled_name);
6895 int status;
6896 info->demangled_name =
6897 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
6898 if (info->demangled_name != nullptr) {
6899 *ReferenceName = info->demangled_name;
6900 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6901 } else
6902 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6903 } else
6904 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6905 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
6906 *ReferenceName =
6907 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6908 if (*ReferenceName)
6909 method_reference(info, ReferenceType, ReferenceName);
6910 else
6911 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6912 // If this is arm64 and the reference is an adrp instruction save the
6913 // instruction, passed in ReferenceValue and the address of the instruction
6914 // for use later if we see and add immediate instruction.
6915 } else if (info->O->getArch() == Triple::aarch64 &&
6916 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
6917 info->adrp_inst = ReferenceValue;
6918 info->adrp_addr = ReferencePC;
6919 SymbolName = nullptr;
6920 *ReferenceName = nullptr;
6921 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6922 // If this is arm64 and reference is an add immediate instruction and we
6923 // have
6924 // seen an adrp instruction just before it and the adrp's Xd register
6925 // matches
6926 // this add's Xn register reconstruct the value being referenced and look to
6927 // see if it is a literal pointer. Note the add immediate instruction is
6928 // passed in ReferenceValue.
6929 } else if (info->O->getArch() == Triple::aarch64 &&
6930 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
6931 ReferencePC - 4 == info->adrp_addr &&
6932 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6933 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6934 uint32_t addxri_inst;
6935 uint64_t adrp_imm, addxri_imm;
6937 adrp_imm =
6938 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6939 if (info->adrp_inst & 0x0200000)
6940 adrp_imm |= 0xfffffffffc000000LL;
6942 addxri_inst = ReferenceValue;
6943 addxri_imm = (addxri_inst >> 10) & 0xfff;
6944 if (((addxri_inst >> 22) & 0x3) == 1)
6945 addxri_imm <<= 12;
6947 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6948 (adrp_imm << 12) + addxri_imm;
6950 *ReferenceName =
6951 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6952 if (*ReferenceName == nullptr)
6953 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6954 // If this is arm64 and the reference is a load register instruction and we
6955 // have seen an adrp instruction just before it and the adrp's Xd register
6956 // matches this add's Xn register reconstruct the value being referenced and
6957 // look to see if it is a literal pointer. Note the load register
6958 // instruction is passed in ReferenceValue.
6959 } else if (info->O->getArch() == Triple::aarch64 &&
6960 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
6961 ReferencePC - 4 == info->adrp_addr &&
6962 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6963 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6964 uint32_t ldrxui_inst;
6965 uint64_t adrp_imm, ldrxui_imm;
6967 adrp_imm =
6968 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6969 if (info->adrp_inst & 0x0200000)
6970 adrp_imm |= 0xfffffffffc000000LL;
6972 ldrxui_inst = ReferenceValue;
6973 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
6975 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6976 (adrp_imm << 12) + (ldrxui_imm << 3);
6978 *ReferenceName =
6979 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6980 if (*ReferenceName == nullptr)
6981 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6983 // If this arm64 and is an load register (PC-relative) instruction the
6984 // ReferenceValue is the PC plus the immediate value.
6985 else if (info->O->getArch() == Triple::aarch64 &&
6986 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
6987 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
6988 *ReferenceName =
6989 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6990 if (*ReferenceName == nullptr)
6991 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6992 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
6993 if (info->demangled_name != nullptr)
6994 free(info->demangled_name);
6995 int status;
6996 info->demangled_name =
6997 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
6998 if (info->demangled_name != nullptr) {
6999 *ReferenceName = info->demangled_name;
7000 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
7003 else {
7004 *ReferenceName = nullptr;
7005 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7008 return SymbolName;
7011 /// Emits the comments that are stored in the CommentStream.
7012 /// Each comment in the CommentStream must end with a newline.
7013 static void emitComments(raw_svector_ostream &CommentStream,
7014 SmallString<128> &CommentsToEmit,
7015 formatted_raw_ostream &FormattedOS,
7016 const MCAsmInfo &MAI) {
7017 // Flush the stream before taking its content.
7018 StringRef Comments = CommentsToEmit.str();
7019 // Get the default information for printing a comment.
7020 StringRef CommentBegin = MAI.getCommentString();
7021 unsigned CommentColumn = MAI.getCommentColumn();
7022 bool IsFirst = true;
7023 while (!Comments.empty()) {
7024 if (!IsFirst)
7025 FormattedOS << '\n';
7026 // Emit a line of comments.
7027 FormattedOS.PadToColumn(CommentColumn);
7028 size_t Position = Comments.find('\n');
7029 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
7030 // Move after the newline character.
7031 Comments = Comments.substr(Position + 1);
7032 IsFirst = false;
7034 FormattedOS.flush();
7036 // Tell the comment stream that the vector changed underneath it.
7037 CommentsToEmit.clear();
7040 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
7041 StringRef DisSegName, StringRef DisSectName) {
7042 const char *McpuDefault = nullptr;
7043 const Target *ThumbTarget = nullptr;
7044 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
7045 if (!TheTarget) {
7046 // GetTarget prints out stuff.
7047 return;
7049 std::string MachOMCPU;
7050 if (MCPU.empty() && McpuDefault)
7051 MachOMCPU = McpuDefault;
7052 else
7053 MachOMCPU = MCPU;
7055 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
7056 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
7057 if (ThumbTarget)
7058 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
7060 // Package up features to be passed to target/subtarget
7061 std::string FeaturesStr;
7062 if (!MAttrs.empty()) {
7063 SubtargetFeatures Features;
7064 for (unsigned i = 0; i != MAttrs.size(); ++i)
7065 Features.AddFeature(MAttrs[i]);
7066 FeaturesStr = Features.getString();
7069 // Set up disassembler.
7070 std::unique_ptr<const MCRegisterInfo> MRI(
7071 TheTarget->createMCRegInfo(TripleName));
7072 std::unique_ptr<const MCAsmInfo> AsmInfo(
7073 TheTarget->createMCAsmInfo(*MRI, TripleName));
7074 std::unique_ptr<const MCSubtargetInfo> STI(
7075 TheTarget->createMCSubtargetInfo(TripleName, MachOMCPU, FeaturesStr));
7076 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
7077 std::unique_ptr<MCDisassembler> DisAsm(
7078 TheTarget->createMCDisassembler(*STI, Ctx));
7079 std::unique_ptr<MCSymbolizer> Symbolizer;
7080 struct DisassembleInfo SymbolizerInfo(nullptr, nullptr, nullptr, false);
7081 std::unique_ptr<MCRelocationInfo> RelInfo(
7082 TheTarget->createMCRelocationInfo(TripleName, Ctx));
7083 if (RelInfo) {
7084 Symbolizer.reset(TheTarget->createMCSymbolizer(
7085 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
7086 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
7087 DisAsm->setSymbolizer(std::move(Symbolizer));
7089 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
7090 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
7091 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
7092 // Set the display preference for hex vs. decimal immediates.
7093 IP->setPrintImmHex(PrintImmHex);
7094 // Comment stream and backing vector.
7095 SmallString<128> CommentsToEmit;
7096 raw_svector_ostream CommentStream(CommentsToEmit);
7097 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
7098 // if it is done then arm64 comments for string literals don't get printed
7099 // and some constant get printed instead and not setting it causes intel
7100 // (32-bit and 64-bit) comments printed with different spacing before the
7101 // comment causing different diffs with the 'C' disassembler library API.
7102 // IP->setCommentStream(CommentStream);
7104 if (!AsmInfo || !STI || !DisAsm || !IP) {
7105 WithColor::error(errs(), "llvm-objdump")
7106 << "couldn't initialize disassembler for target " << TripleName << '\n';
7107 return;
7110 // Set up separate thumb disassembler if needed.
7111 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
7112 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
7113 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
7114 std::unique_ptr<MCDisassembler> ThumbDisAsm;
7115 std::unique_ptr<MCInstPrinter> ThumbIP;
7116 std::unique_ptr<MCContext> ThumbCtx;
7117 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
7118 struct DisassembleInfo ThumbSymbolizerInfo(nullptr, nullptr, nullptr, false);
7119 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
7120 if (ThumbTarget) {
7121 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
7122 ThumbAsmInfo.reset(
7123 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
7124 ThumbSTI.reset(
7125 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MachOMCPU,
7126 FeaturesStr));
7127 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
7128 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
7129 MCContext *PtrThumbCtx = ThumbCtx.get();
7130 ThumbRelInfo.reset(
7131 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
7132 if (ThumbRelInfo) {
7133 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
7134 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
7135 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
7136 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
7138 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
7139 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
7140 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
7141 *ThumbInstrInfo, *ThumbMRI));
7142 // Set the display preference for hex vs. decimal immediates.
7143 ThumbIP->setPrintImmHex(PrintImmHex);
7146 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
7147 WithColor::error(errs(), "llvm-objdump")
7148 << "couldn't initialize disassembler for target " << ThumbTripleName
7149 << '\n';
7150 return;
7153 MachO::mach_header Header = MachOOF->getHeader();
7155 // FIXME: Using the -cfg command line option, this code used to be able to
7156 // annotate relocations with the referenced symbol's name, and if this was
7157 // inside a __[cf]string section, the data it points to. This is now replaced
7158 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
7159 std::vector<SectionRef> Sections;
7160 std::vector<SymbolRef> Symbols;
7161 SmallVector<uint64_t, 8> FoundFns;
7162 uint64_t BaseSegmentAddress;
7164 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
7165 BaseSegmentAddress);
7167 // Sort the symbols by address, just in case they didn't come in that way.
7168 llvm::sort(Symbols, SymbolSorter());
7170 // Build a data in code table that is sorted on by the address of each entry.
7171 uint64_t BaseAddress = 0;
7172 if (Header.filetype == MachO::MH_OBJECT)
7173 BaseAddress = Sections[0].getAddress();
7174 else
7175 BaseAddress = BaseSegmentAddress;
7176 DiceTable Dices;
7177 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
7178 DI != DE; ++DI) {
7179 uint32_t Offset;
7180 DI->getOffset(Offset);
7181 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
7183 array_pod_sort(Dices.begin(), Dices.end());
7185 #ifndef NDEBUG
7186 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
7187 #else
7188 raw_ostream &DebugOut = nulls();
7189 #endif
7191 std::unique_ptr<DIContext> diContext;
7192 ObjectFile *DbgObj = MachOOF;
7193 std::unique_ptr<MemoryBuffer> DSYMBuf;
7194 // Try to find debug info and set up the DIContext for it.
7195 if (UseDbg) {
7196 // A separate DSym file path was specified, parse it as a macho file,
7197 // get the sections and supply it to the section name parsing machinery.
7198 if (!DSYMFile.empty()) {
7199 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
7200 MemoryBuffer::getFileOrSTDIN(DSYMFile);
7201 if (std::error_code EC = BufOrErr.getError()) {
7202 report_error(errorCodeToError(EC), DSYMFile);
7203 return;
7206 std::unique_ptr<MachOObjectFile> DbgObjCheck = unwrapOrError(
7207 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef()),
7208 DSYMFile.getValue());
7209 DbgObj = DbgObjCheck.release();
7210 // We need to keep the file alive, because we're replacing DbgObj with it.
7211 DSYMBuf = std::move(BufOrErr.get());
7214 // Setup the DIContext
7215 diContext = DWARFContext::create(*DbgObj);
7218 if (FilterSections.empty())
7219 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
7221 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
7222 StringRef SectName;
7223 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
7224 continue;
7226 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
7228 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
7229 if (SegmentName != DisSegName)
7230 continue;
7232 StringRef BytesStr;
7233 Sections[SectIdx].getContents(BytesStr);
7234 ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr);
7235 uint64_t SectAddress = Sections[SectIdx].getAddress();
7237 bool symbolTableWorked = false;
7239 // Create a map of symbol addresses to symbol names for use by
7240 // the SymbolizerSymbolLookUp() routine.
7241 SymbolAddressMap AddrMap;
7242 bool DisSymNameFound = false;
7243 for (const SymbolRef &Symbol : MachOOF->symbols()) {
7244 SymbolRef::Type ST =
7245 unwrapOrError(Symbol.getType(), MachOOF->getFileName());
7246 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
7247 ST == SymbolRef::ST_Other) {
7248 uint64_t Address = Symbol.getValue();
7249 StringRef SymName =
7250 unwrapOrError(Symbol.getName(), MachOOF->getFileName());
7251 AddrMap[Address] = SymName;
7252 if (!DisSymName.empty() && DisSymName == SymName)
7253 DisSymNameFound = true;
7256 if (!DisSymName.empty() && !DisSymNameFound) {
7257 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
7258 return;
7260 // Set up the block of info used by the Symbolizer call backs.
7261 SymbolizerInfo.verbose = !NoSymbolicOperands;
7262 SymbolizerInfo.O = MachOOF;
7263 SymbolizerInfo.S = Sections[SectIdx];
7264 SymbolizerInfo.AddrMap = &AddrMap;
7265 SymbolizerInfo.Sections = &Sections;
7266 // Same for the ThumbSymbolizer
7267 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
7268 ThumbSymbolizerInfo.O = MachOOF;
7269 ThumbSymbolizerInfo.S = Sections[SectIdx];
7270 ThumbSymbolizerInfo.AddrMap = &AddrMap;
7271 ThumbSymbolizerInfo.Sections = &Sections;
7273 unsigned int Arch = MachOOF->getArch();
7275 // Skip all symbols if this is a stubs file.
7276 if (Bytes.empty())
7277 return;
7279 // If the section has symbols but no symbol at the start of the section
7280 // these are used to make sure the bytes before the first symbol are
7281 // disassembled.
7282 bool FirstSymbol = true;
7283 bool FirstSymbolAtSectionStart = true;
7285 // Disassemble symbol by symbol.
7286 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
7287 StringRef SymName =
7288 unwrapOrError(Symbols[SymIdx].getName(), MachOOF->getFileName());
7289 SymbolRef::Type ST =
7290 unwrapOrError(Symbols[SymIdx].getType(), MachOOF->getFileName());
7291 if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
7292 continue;
7294 // Make sure the symbol is defined in this section.
7295 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
7296 if (!containsSym) {
7297 if (!DisSymName.empty() && DisSymName == SymName) {
7298 outs() << "-dis-symname: " << DisSymName << " not in the section\n";
7299 return;
7301 continue;
7303 // The __mh_execute_header is special and we need to deal with that fact
7304 // this symbol is before the start of the (__TEXT,__text) section and at the
7305 // address of the start of the __TEXT segment. This is because this symbol
7306 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
7307 // start of the section in a standard MH_EXECUTE filetype.
7308 if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
7309 outs() << "-dis-symname: __mh_execute_header not in any section\n";
7310 return;
7312 // When this code is trying to disassemble a symbol at a time and in the
7313 // case there is only the __mh_execute_header symbol left as in a stripped
7314 // executable, we need to deal with this by ignoring this symbol so the
7315 // whole section is disassembled and this symbol is then not displayed.
7316 if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
7317 SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
7318 SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
7319 continue;
7321 // If we are only disassembling one symbol see if this is that symbol.
7322 if (!DisSymName.empty() && DisSymName != SymName)
7323 continue;
7325 // Start at the address of the symbol relative to the section's address.
7326 uint64_t SectSize = Sections[SectIdx].getSize();
7327 uint64_t Start = Symbols[SymIdx].getValue();
7328 uint64_t SectionAddress = Sections[SectIdx].getAddress();
7329 Start -= SectionAddress;
7331 if (Start > SectSize) {
7332 outs() << "section data ends, " << SymName
7333 << " lies outside valid range\n";
7334 return;
7337 // Stop disassembling either at the beginning of the next symbol or at
7338 // the end of the section.
7339 bool containsNextSym = false;
7340 uint64_t NextSym = 0;
7341 uint64_t NextSymIdx = SymIdx + 1;
7342 while (Symbols.size() > NextSymIdx) {
7343 SymbolRef::Type NextSymType = unwrapOrError(
7344 Symbols[NextSymIdx].getType(), MachOOF->getFileName());
7345 if (NextSymType == SymbolRef::ST_Function) {
7346 containsNextSym =
7347 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
7348 NextSym = Symbols[NextSymIdx].getValue();
7349 NextSym -= SectionAddress;
7350 break;
7352 ++NextSymIdx;
7355 uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
7356 uint64_t Size;
7358 symbolTableWorked = true;
7360 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
7361 bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
7363 // We only need the dedicated Thumb target if there's a real choice
7364 // (i.e. we're not targeting M-class) and the function is Thumb.
7365 bool UseThumbTarget = IsThumb && ThumbTarget;
7367 // If we are not specifying a symbol to start disassembly with and this
7368 // is the first symbol in the section but not at the start of the section
7369 // then move the disassembly index to the start of the section and
7370 // don't print the symbol name just yet. This is so the bytes before the
7371 // first symbol are disassembled.
7372 uint64_t SymbolStart = Start;
7373 if (DisSymName.empty() && FirstSymbol && Start != 0) {
7374 FirstSymbolAtSectionStart = false;
7375 Start = 0;
7377 else
7378 outs() << SymName << ":\n";
7380 DILineInfo lastLine;
7381 for (uint64_t Index = Start; Index < End; Index += Size) {
7382 MCInst Inst;
7384 // If this is the first symbol in the section and it was not at the
7385 // start of the section, see if we are at its Index now and if so print
7386 // the symbol name.
7387 if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart)
7388 outs() << SymName << ":\n";
7390 uint64_t PC = SectAddress + Index;
7391 if (!NoLeadingAddr) {
7392 if (FullLeadingAddr) {
7393 if (MachOOF->is64Bit())
7394 outs() << format("%016" PRIx64, PC);
7395 else
7396 outs() << format("%08" PRIx64, PC);
7397 } else {
7398 outs() << format("%8" PRIx64 ":", PC);
7401 if (!NoShowRawInsn || Arch == Triple::arm)
7402 outs() << "\t";
7404 // Check the data in code table here to see if this is data not an
7405 // instruction to be disassembled.
7406 DiceTable Dice;
7407 Dice.push_back(std::make_pair(PC, DiceRef()));
7408 dice_table_iterator DTI =
7409 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
7410 compareDiceTableEntries);
7411 if (DTI != Dices.end()) {
7412 uint16_t Length;
7413 DTI->second.getLength(Length);
7414 uint16_t Kind;
7415 DTI->second.getKind(Kind);
7416 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
7417 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
7418 (PC == (DTI->first + Length - 1)) && (Length & 1))
7419 Size++;
7420 continue;
7423 SmallVector<char, 64> AnnotationsBytes;
7424 raw_svector_ostream Annotations(AnnotationsBytes);
7426 bool gotInst;
7427 if (UseThumbTarget)
7428 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
7429 PC, DebugOut, Annotations);
7430 else
7431 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
7432 DebugOut, Annotations);
7433 if (gotInst) {
7434 if (!NoShowRawInsn || Arch == Triple::arm) {
7435 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
7437 formatted_raw_ostream FormattedOS(outs());
7438 StringRef AnnotationsStr = Annotations.str();
7439 if (UseThumbTarget)
7440 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
7441 else
7442 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
7443 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
7445 // Print debug info.
7446 if (diContext) {
7447 DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx});
7448 // Print valid line info if it changed.
7449 if (dli != lastLine && dli.Line != 0)
7450 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
7451 << dli.Column;
7452 lastLine = dli;
7454 outs() << "\n";
7455 } else {
7456 unsigned int Arch = MachOOF->getArch();
7457 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
7458 outs() << format("\t.byte 0x%02x #bad opcode\n",
7459 *(Bytes.data() + Index) & 0xff);
7460 Size = 1; // skip exactly one illegible byte and move on.
7461 } else if (Arch == Triple::aarch64 ||
7462 (Arch == Triple::arm && !IsThumb)) {
7463 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
7464 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
7465 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
7466 (*(Bytes.data() + Index + 3) & 0xff) << 24;
7467 outs() << format("\t.long\t0x%08x\n", opcode);
7468 Size = 4;
7469 } else if (Arch == Triple::arm) {
7470 assert(IsThumb && "ARM mode should have been dealt with above");
7471 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
7472 (*(Bytes.data() + Index + 1) & 0xff) << 8;
7473 outs() << format("\t.short\t0x%04x\n", opcode);
7474 Size = 2;
7475 } else{
7476 WithColor::warning(errs(), "llvm-objdump")
7477 << "invalid instruction encoding\n";
7478 if (Size == 0)
7479 Size = 1; // skip illegible bytes
7483 // Now that we are done disassembled the first symbol set the bool that
7484 // were doing this to false.
7485 FirstSymbol = false;
7487 if (!symbolTableWorked) {
7488 // Reading the symbol table didn't work, disassemble the whole section.
7489 uint64_t SectAddress = Sections[SectIdx].getAddress();
7490 uint64_t SectSize = Sections[SectIdx].getSize();
7491 uint64_t InstSize;
7492 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
7493 MCInst Inst;
7495 uint64_t PC = SectAddress + Index;
7496 SmallVector<char, 64> AnnotationsBytes;
7497 raw_svector_ostream Annotations(AnnotationsBytes);
7498 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
7499 DebugOut, Annotations)) {
7500 if (!NoLeadingAddr) {
7501 if (FullLeadingAddr) {
7502 if (MachOOF->is64Bit())
7503 outs() << format("%016" PRIx64, PC);
7504 else
7505 outs() << format("%08" PRIx64, PC);
7506 } else {
7507 outs() << format("%8" PRIx64 ":", PC);
7510 if (!NoShowRawInsn || Arch == Triple::arm) {
7511 outs() << "\t";
7512 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
7514 StringRef AnnotationsStr = Annotations.str();
7515 IP->printInst(&Inst, outs(), AnnotationsStr, *STI);
7516 outs() << "\n";
7517 } else {
7518 unsigned int Arch = MachOOF->getArch();
7519 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
7520 outs() << format("\t.byte 0x%02x #bad opcode\n",
7521 *(Bytes.data() + Index) & 0xff);
7522 InstSize = 1; // skip exactly one illegible byte and move on.
7523 } else {
7524 WithColor::warning(errs(), "llvm-objdump")
7525 << "invalid instruction encoding\n";
7526 if (InstSize == 0)
7527 InstSize = 1; // skip illegible bytes
7532 // The TripleName's need to be reset if we are called again for a different
7533 // archtecture.
7534 TripleName = "";
7535 ThumbTripleName = "";
7537 if (SymbolizerInfo.demangled_name != nullptr)
7538 free(SymbolizerInfo.demangled_name);
7539 if (ThumbSymbolizerInfo.demangled_name != nullptr)
7540 free(ThumbSymbolizerInfo.demangled_name);
7544 //===----------------------------------------------------------------------===//
7545 // __compact_unwind section dumping
7546 //===----------------------------------------------------------------------===//
7548 namespace {
7550 template <typename T>
7551 static uint64_t read(StringRef Contents, ptrdiff_t Offset) {
7552 using llvm::support::little;
7553 using llvm::support::unaligned;
7555 if (Offset + sizeof(T) > Contents.size()) {
7556 outs() << "warning: attempt to read past end of buffer\n";
7557 return T();
7560 uint64_t Val =
7561 support::endian::read<T, little, unaligned>(Contents.data() + Offset);
7562 return Val;
7565 template <typename T>
7566 static uint64_t readNext(StringRef Contents, ptrdiff_t &Offset) {
7567 T Val = read<T>(Contents, Offset);
7568 Offset += sizeof(T);
7569 return Val;
7572 struct CompactUnwindEntry {
7573 uint32_t OffsetInSection;
7575 uint64_t FunctionAddr;
7576 uint32_t Length;
7577 uint32_t CompactEncoding;
7578 uint64_t PersonalityAddr;
7579 uint64_t LSDAAddr;
7581 RelocationRef FunctionReloc;
7582 RelocationRef PersonalityReloc;
7583 RelocationRef LSDAReloc;
7585 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
7586 : OffsetInSection(Offset) {
7587 if (Is64)
7588 read<uint64_t>(Contents, Offset);
7589 else
7590 read<uint32_t>(Contents, Offset);
7593 private:
7594 template <typename UIntPtr> void read(StringRef Contents, ptrdiff_t Offset) {
7595 FunctionAddr = readNext<UIntPtr>(Contents, Offset);
7596 Length = readNext<uint32_t>(Contents, Offset);
7597 CompactEncoding = readNext<uint32_t>(Contents, Offset);
7598 PersonalityAddr = readNext<UIntPtr>(Contents, Offset);
7599 LSDAAddr = readNext<UIntPtr>(Contents, Offset);
7604 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
7605 /// and data being relocated, determine the best base Name and Addend to use for
7606 /// display purposes.
7608 /// 1. An Extern relocation will directly reference a symbol (and the data is
7609 /// then already an addend), so use that.
7610 /// 2. Otherwise the data is an offset in the object file's layout; try to find
7611 // a symbol before it in the same section, and use the offset from there.
7612 /// 3. Finally, if all that fails, fall back to an offset from the start of the
7613 /// referenced section.
7614 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
7615 std::map<uint64_t, SymbolRef> &Symbols,
7616 const RelocationRef &Reloc, uint64_t Addr,
7617 StringRef &Name, uint64_t &Addend) {
7618 if (Reloc.getSymbol() != Obj->symbol_end()) {
7619 Name = unwrapOrError(Reloc.getSymbol()->getName(), Obj->getFileName());
7620 Addend = Addr;
7621 return;
7624 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
7625 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
7627 uint64_t SectionAddr = RelocSection.getAddress();
7629 auto Sym = Symbols.upper_bound(Addr);
7630 if (Sym == Symbols.begin()) {
7631 // The first symbol in the object is after this reference, the best we can
7632 // do is section-relative notation.
7633 RelocSection.getName(Name);
7634 Addend = Addr - SectionAddr;
7635 return;
7638 // Go back one so that SymbolAddress <= Addr.
7639 --Sym;
7641 section_iterator SymSection =
7642 unwrapOrError(Sym->second.getSection(), Obj->getFileName());
7643 if (RelocSection == *SymSection) {
7644 // There's a valid symbol in the same section before this reference.
7645 Name = unwrapOrError(Sym->second.getName(), Obj->getFileName());
7646 Addend = Addr - Sym->first;
7647 return;
7650 // There is a symbol before this reference, but it's in a different
7651 // section. Probably not helpful to mention it, so use the section name.
7652 RelocSection.getName(Name);
7653 Addend = Addr - SectionAddr;
7656 static void printUnwindRelocDest(const MachOObjectFile *Obj,
7657 std::map<uint64_t, SymbolRef> &Symbols,
7658 const RelocationRef &Reloc, uint64_t Addr) {
7659 StringRef Name;
7660 uint64_t Addend;
7662 if (!Reloc.getObject())
7663 return;
7665 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
7667 outs() << Name;
7668 if (Addend)
7669 outs() << " + " << format("0x%" PRIx64, Addend);
7672 static void
7673 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
7674 std::map<uint64_t, SymbolRef> &Symbols,
7675 const SectionRef &CompactUnwind) {
7677 if (!Obj->isLittleEndian()) {
7678 outs() << "Skipping big-endian __compact_unwind section\n";
7679 return;
7682 bool Is64 = Obj->is64Bit();
7683 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
7684 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
7686 StringRef Contents;
7687 CompactUnwind.getContents(Contents);
7689 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
7691 // First populate the initial raw offsets, encodings and so on from the entry.
7692 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
7693 CompactUnwindEntry Entry(Contents, Offset, Is64);
7694 CompactUnwinds.push_back(Entry);
7697 // Next we need to look at the relocations to find out what objects are
7698 // actually being referred to.
7699 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
7700 uint64_t RelocAddress = Reloc.getOffset();
7702 uint32_t EntryIdx = RelocAddress / EntrySize;
7703 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
7704 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
7706 if (OffsetInEntry == 0)
7707 Entry.FunctionReloc = Reloc;
7708 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
7709 Entry.PersonalityReloc = Reloc;
7710 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
7711 Entry.LSDAReloc = Reloc;
7712 else {
7713 outs() << "Invalid relocation in __compact_unwind section\n";
7714 return;
7718 // Finally, we're ready to print the data we've gathered.
7719 outs() << "Contents of __compact_unwind section:\n";
7720 for (auto &Entry : CompactUnwinds) {
7721 outs() << " Entry at offset "
7722 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
7724 // 1. Start of the region this entry applies to.
7725 outs() << " start: " << format("0x%" PRIx64,
7726 Entry.FunctionAddr) << ' ';
7727 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
7728 outs() << '\n';
7730 // 2. Length of the region this entry applies to.
7731 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
7732 << '\n';
7733 // 3. The 32-bit compact encoding.
7734 outs() << " compact encoding: "
7735 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
7737 // 4. The personality function, if present.
7738 if (Entry.PersonalityReloc.getObject()) {
7739 outs() << " personality function: "
7740 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
7741 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
7742 Entry.PersonalityAddr);
7743 outs() << '\n';
7746 // 5. This entry's language-specific data area.
7747 if (Entry.LSDAReloc.getObject()) {
7748 outs() << " LSDA: " << format("0x%" PRIx64,
7749 Entry.LSDAAddr) << ' ';
7750 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
7751 outs() << '\n';
7756 //===----------------------------------------------------------------------===//
7757 // __unwind_info section dumping
7758 //===----------------------------------------------------------------------===//
7760 static void printRegularSecondLevelUnwindPage(StringRef PageData) {
7761 ptrdiff_t Pos = 0;
7762 uint32_t Kind = readNext<uint32_t>(PageData, Pos);
7763 (void)Kind;
7764 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
7766 uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos);
7767 uint16_t NumEntries = readNext<uint16_t>(PageData, Pos);
7769 Pos = EntriesStart;
7770 for (unsigned i = 0; i < NumEntries; ++i) {
7771 uint32_t FunctionOffset = readNext<uint32_t>(PageData, Pos);
7772 uint32_t Encoding = readNext<uint32_t>(PageData, Pos);
7774 outs() << " [" << i << "]: "
7775 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7776 << ", "
7777 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
7781 static void printCompressedSecondLevelUnwindPage(
7782 StringRef PageData, uint32_t FunctionBase,
7783 const SmallVectorImpl<uint32_t> &CommonEncodings) {
7784 ptrdiff_t Pos = 0;
7785 uint32_t Kind = readNext<uint32_t>(PageData, Pos);
7786 (void)Kind;
7787 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
7789 uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos);
7790 uint16_t NumEntries = readNext<uint16_t>(PageData, Pos);
7792 uint16_t EncodingsStart = readNext<uint16_t>(PageData, Pos);
7793 readNext<uint16_t>(PageData, Pos);
7794 StringRef PageEncodings = PageData.substr(EncodingsStart, StringRef::npos);
7796 Pos = EntriesStart;
7797 for (unsigned i = 0; i < NumEntries; ++i) {
7798 uint32_t Entry = readNext<uint32_t>(PageData, Pos);
7799 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
7800 uint32_t EncodingIdx = Entry >> 24;
7802 uint32_t Encoding;
7803 if (EncodingIdx < CommonEncodings.size())
7804 Encoding = CommonEncodings[EncodingIdx];
7805 else
7806 Encoding = read<uint32_t>(PageEncodings,
7807 sizeof(uint32_t) *
7808 (EncodingIdx - CommonEncodings.size()));
7810 outs() << " [" << i << "]: "
7811 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7812 << ", "
7813 << "encoding[" << EncodingIdx
7814 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
7818 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
7819 std::map<uint64_t, SymbolRef> &Symbols,
7820 const SectionRef &UnwindInfo) {
7822 if (!Obj->isLittleEndian()) {
7823 outs() << "Skipping big-endian __unwind_info section\n";
7824 return;
7827 outs() << "Contents of __unwind_info section:\n";
7829 StringRef Contents;
7830 UnwindInfo.getContents(Contents);
7831 ptrdiff_t Pos = 0;
7833 //===----------------------------------
7834 // Section header
7835 //===----------------------------------
7837 uint32_t Version = readNext<uint32_t>(Contents, Pos);
7838 outs() << " Version: "
7839 << format("0x%" PRIx32, Version) << '\n';
7840 if (Version != 1) {
7841 outs() << " Skipping section with unknown version\n";
7842 return;
7845 uint32_t CommonEncodingsStart = readNext<uint32_t>(Contents, Pos);
7846 outs() << " Common encodings array section offset: "
7847 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
7848 uint32_t NumCommonEncodings = readNext<uint32_t>(Contents, Pos);
7849 outs() << " Number of common encodings in array: "
7850 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
7852 uint32_t PersonalitiesStart = readNext<uint32_t>(Contents, Pos);
7853 outs() << " Personality function array section offset: "
7854 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
7855 uint32_t NumPersonalities = readNext<uint32_t>(Contents, Pos);
7856 outs() << " Number of personality functions in array: "
7857 << format("0x%" PRIx32, NumPersonalities) << '\n';
7859 uint32_t IndicesStart = readNext<uint32_t>(Contents, Pos);
7860 outs() << " Index array section offset: "
7861 << format("0x%" PRIx32, IndicesStart) << '\n';
7862 uint32_t NumIndices = readNext<uint32_t>(Contents, Pos);
7863 outs() << " Number of indices in array: "
7864 << format("0x%" PRIx32, NumIndices) << '\n';
7866 //===----------------------------------
7867 // A shared list of common encodings
7868 //===----------------------------------
7870 // These occupy indices in the range [0, N] whenever an encoding is referenced
7871 // from a compressed 2nd level index table. In practice the linker only
7872 // creates ~128 of these, so that indices are available to embed encodings in
7873 // the 2nd level index.
7875 SmallVector<uint32_t, 64> CommonEncodings;
7876 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
7877 Pos = CommonEncodingsStart;
7878 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
7879 uint32_t Encoding = readNext<uint32_t>(Contents, Pos);
7880 CommonEncodings.push_back(Encoding);
7882 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
7883 << '\n';
7886 //===----------------------------------
7887 // Personality functions used in this executable
7888 //===----------------------------------
7890 // There should be only a handful of these (one per source language,
7891 // roughly). Particularly since they only get 2 bits in the compact encoding.
7893 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
7894 Pos = PersonalitiesStart;
7895 for (unsigned i = 0; i < NumPersonalities; ++i) {
7896 uint32_t PersonalityFn = readNext<uint32_t>(Contents, Pos);
7897 outs() << " personality[" << i + 1
7898 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
7901 //===----------------------------------
7902 // The level 1 index entries
7903 //===----------------------------------
7905 // These specify an approximate place to start searching for the more detailed
7906 // information, sorted by PC.
7908 struct IndexEntry {
7909 uint32_t FunctionOffset;
7910 uint32_t SecondLevelPageStart;
7911 uint32_t LSDAStart;
7914 SmallVector<IndexEntry, 4> IndexEntries;
7916 outs() << " Top level indices: (count = " << NumIndices << ")\n";
7917 Pos = IndicesStart;
7918 for (unsigned i = 0; i < NumIndices; ++i) {
7919 IndexEntry Entry;
7921 Entry.FunctionOffset = readNext<uint32_t>(Contents, Pos);
7922 Entry.SecondLevelPageStart = readNext<uint32_t>(Contents, Pos);
7923 Entry.LSDAStart = readNext<uint32_t>(Contents, Pos);
7924 IndexEntries.push_back(Entry);
7926 outs() << " [" << i << "]: "
7927 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
7928 << ", "
7929 << "2nd level page offset="
7930 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
7931 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
7934 //===----------------------------------
7935 // Next come the LSDA tables
7936 //===----------------------------------
7938 // The LSDA layout is rather implicit: it's a contiguous array of entries from
7939 // the first top-level index's LSDAOffset to the last (sentinel).
7941 outs() << " LSDA descriptors:\n";
7942 Pos = IndexEntries[0].LSDAStart;
7943 const uint32_t LSDASize = 2 * sizeof(uint32_t);
7944 int NumLSDAs =
7945 (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) / LSDASize;
7947 for (int i = 0; i < NumLSDAs; ++i) {
7948 uint32_t FunctionOffset = readNext<uint32_t>(Contents, Pos);
7949 uint32_t LSDAOffset = readNext<uint32_t>(Contents, Pos);
7950 outs() << " [" << i << "]: "
7951 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7952 << ", "
7953 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
7956 //===----------------------------------
7957 // Finally, the 2nd level indices
7958 //===----------------------------------
7960 // Generally these are 4K in size, and have 2 possible forms:
7961 // + Regular stores up to 511 entries with disparate encodings
7962 // + Compressed stores up to 1021 entries if few enough compact encoding
7963 // values are used.
7964 outs() << " Second level indices:\n";
7965 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
7966 // The final sentinel top-level index has no associated 2nd level page
7967 if (IndexEntries[i].SecondLevelPageStart == 0)
7968 break;
7970 outs() << " Second level index[" << i << "]: "
7971 << "offset in section="
7972 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
7973 << ", "
7974 << "base function offset="
7975 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
7977 Pos = IndexEntries[i].SecondLevelPageStart;
7978 if (Pos + sizeof(uint32_t) > Contents.size()) {
7979 outs() << "warning: invalid offset for second level page: " << Pos << '\n';
7980 continue;
7983 uint32_t Kind =
7984 *reinterpret_cast<const support::ulittle32_t *>(Contents.data() + Pos);
7985 if (Kind == 2)
7986 printRegularSecondLevelUnwindPage(Contents.substr(Pos, 4096));
7987 else if (Kind == 3)
7988 printCompressedSecondLevelUnwindPage(Contents.substr(Pos, 4096),
7989 IndexEntries[i].FunctionOffset,
7990 CommonEncodings);
7991 else
7992 outs() << " Skipping 2nd level page with unknown kind " << Kind
7993 << '\n';
7997 void printMachOUnwindInfo(const MachOObjectFile *Obj) {
7998 std::map<uint64_t, SymbolRef> Symbols;
7999 for (const SymbolRef &SymRef : Obj->symbols()) {
8000 // Discard any undefined or absolute symbols. They're not going to take part
8001 // in the convenience lookup for unwind info and just take up resources.
8002 auto SectOrErr = SymRef.getSection();
8003 if (!SectOrErr) {
8004 // TODO: Actually report errors helpfully.
8005 consumeError(SectOrErr.takeError());
8006 continue;
8008 section_iterator Section = *SectOrErr;
8009 if (Section == Obj->section_end())
8010 continue;
8012 uint64_t Addr = SymRef.getValue();
8013 Symbols.insert(std::make_pair(Addr, SymRef));
8016 for (const SectionRef &Section : Obj->sections()) {
8017 StringRef SectName;
8018 Section.getName(SectName);
8019 if (SectName == "__compact_unwind")
8020 printMachOCompactUnwindSection(Obj, Symbols, Section);
8021 else if (SectName == "__unwind_info")
8022 printMachOUnwindInfoSection(Obj, Symbols, Section);
8026 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
8027 uint32_t cpusubtype, uint32_t filetype,
8028 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
8029 bool verbose) {
8030 outs() << "Mach header\n";
8031 outs() << " magic cputype cpusubtype caps filetype ncmds "
8032 "sizeofcmds flags\n";
8033 if (verbose) {
8034 if (magic == MachO::MH_MAGIC)
8035 outs() << " MH_MAGIC";
8036 else if (magic == MachO::MH_MAGIC_64)
8037 outs() << "MH_MAGIC_64";
8038 else
8039 outs() << format(" 0x%08" PRIx32, magic);
8040 switch (cputype) {
8041 case MachO::CPU_TYPE_I386:
8042 outs() << " I386";
8043 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8044 case MachO::CPU_SUBTYPE_I386_ALL:
8045 outs() << " ALL";
8046 break;
8047 default:
8048 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8049 break;
8051 break;
8052 case MachO::CPU_TYPE_X86_64:
8053 outs() << " X86_64";
8054 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8055 case MachO::CPU_SUBTYPE_X86_64_ALL:
8056 outs() << " ALL";
8057 break;
8058 case MachO::CPU_SUBTYPE_X86_64_H:
8059 outs() << " Haswell";
8060 break;
8061 default:
8062 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8063 break;
8065 break;
8066 case MachO::CPU_TYPE_ARM:
8067 outs() << " ARM";
8068 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8069 case MachO::CPU_SUBTYPE_ARM_ALL:
8070 outs() << " ALL";
8071 break;
8072 case MachO::CPU_SUBTYPE_ARM_V4T:
8073 outs() << " V4T";
8074 break;
8075 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
8076 outs() << " V5TEJ";
8077 break;
8078 case MachO::CPU_SUBTYPE_ARM_XSCALE:
8079 outs() << " XSCALE";
8080 break;
8081 case MachO::CPU_SUBTYPE_ARM_V6:
8082 outs() << " V6";
8083 break;
8084 case MachO::CPU_SUBTYPE_ARM_V6M:
8085 outs() << " V6M";
8086 break;
8087 case MachO::CPU_SUBTYPE_ARM_V7:
8088 outs() << " V7";
8089 break;
8090 case MachO::CPU_SUBTYPE_ARM_V7EM:
8091 outs() << " V7EM";
8092 break;
8093 case MachO::CPU_SUBTYPE_ARM_V7K:
8094 outs() << " V7K";
8095 break;
8096 case MachO::CPU_SUBTYPE_ARM_V7M:
8097 outs() << " V7M";
8098 break;
8099 case MachO::CPU_SUBTYPE_ARM_V7S:
8100 outs() << " V7S";
8101 break;
8102 default:
8103 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8104 break;
8106 break;
8107 case MachO::CPU_TYPE_ARM64:
8108 outs() << " ARM64";
8109 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8110 case MachO::CPU_SUBTYPE_ARM64_ALL:
8111 outs() << " ALL";
8112 break;
8113 case MachO::CPU_SUBTYPE_ARM64E:
8114 outs() << " E";
8115 break;
8116 default:
8117 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8118 break;
8120 break;
8121 case MachO::CPU_TYPE_POWERPC:
8122 outs() << " PPC";
8123 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8124 case MachO::CPU_SUBTYPE_POWERPC_ALL:
8125 outs() << " ALL";
8126 break;
8127 default:
8128 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8129 break;
8131 break;
8132 case MachO::CPU_TYPE_POWERPC64:
8133 outs() << " PPC64";
8134 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8135 case MachO::CPU_SUBTYPE_POWERPC_ALL:
8136 outs() << " ALL";
8137 break;
8138 default:
8139 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8140 break;
8142 break;
8143 default:
8144 outs() << format(" %7d", cputype);
8145 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8146 break;
8148 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
8149 outs() << " LIB64";
8150 } else {
8151 outs() << format(" 0x%02" PRIx32,
8152 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
8154 switch (filetype) {
8155 case MachO::MH_OBJECT:
8156 outs() << " OBJECT";
8157 break;
8158 case MachO::MH_EXECUTE:
8159 outs() << " EXECUTE";
8160 break;
8161 case MachO::MH_FVMLIB:
8162 outs() << " FVMLIB";
8163 break;
8164 case MachO::MH_CORE:
8165 outs() << " CORE";
8166 break;
8167 case MachO::MH_PRELOAD:
8168 outs() << " PRELOAD";
8169 break;
8170 case MachO::MH_DYLIB:
8171 outs() << " DYLIB";
8172 break;
8173 case MachO::MH_DYLIB_STUB:
8174 outs() << " DYLIB_STUB";
8175 break;
8176 case MachO::MH_DYLINKER:
8177 outs() << " DYLINKER";
8178 break;
8179 case MachO::MH_BUNDLE:
8180 outs() << " BUNDLE";
8181 break;
8182 case MachO::MH_DSYM:
8183 outs() << " DSYM";
8184 break;
8185 case MachO::MH_KEXT_BUNDLE:
8186 outs() << " KEXTBUNDLE";
8187 break;
8188 default:
8189 outs() << format(" %10u", filetype);
8190 break;
8192 outs() << format(" %5u", ncmds);
8193 outs() << format(" %10u", sizeofcmds);
8194 uint32_t f = flags;
8195 if (f & MachO::MH_NOUNDEFS) {
8196 outs() << " NOUNDEFS";
8197 f &= ~MachO::MH_NOUNDEFS;
8199 if (f & MachO::MH_INCRLINK) {
8200 outs() << " INCRLINK";
8201 f &= ~MachO::MH_INCRLINK;
8203 if (f & MachO::MH_DYLDLINK) {
8204 outs() << " DYLDLINK";
8205 f &= ~MachO::MH_DYLDLINK;
8207 if (f & MachO::MH_BINDATLOAD) {
8208 outs() << " BINDATLOAD";
8209 f &= ~MachO::MH_BINDATLOAD;
8211 if (f & MachO::MH_PREBOUND) {
8212 outs() << " PREBOUND";
8213 f &= ~MachO::MH_PREBOUND;
8215 if (f & MachO::MH_SPLIT_SEGS) {
8216 outs() << " SPLIT_SEGS";
8217 f &= ~MachO::MH_SPLIT_SEGS;
8219 if (f & MachO::MH_LAZY_INIT) {
8220 outs() << " LAZY_INIT";
8221 f &= ~MachO::MH_LAZY_INIT;
8223 if (f & MachO::MH_TWOLEVEL) {
8224 outs() << " TWOLEVEL";
8225 f &= ~MachO::MH_TWOLEVEL;
8227 if (f & MachO::MH_FORCE_FLAT) {
8228 outs() << " FORCE_FLAT";
8229 f &= ~MachO::MH_FORCE_FLAT;
8231 if (f & MachO::MH_NOMULTIDEFS) {
8232 outs() << " NOMULTIDEFS";
8233 f &= ~MachO::MH_NOMULTIDEFS;
8235 if (f & MachO::MH_NOFIXPREBINDING) {
8236 outs() << " NOFIXPREBINDING";
8237 f &= ~MachO::MH_NOFIXPREBINDING;
8239 if (f & MachO::MH_PREBINDABLE) {
8240 outs() << " PREBINDABLE";
8241 f &= ~MachO::MH_PREBINDABLE;
8243 if (f & MachO::MH_ALLMODSBOUND) {
8244 outs() << " ALLMODSBOUND";
8245 f &= ~MachO::MH_ALLMODSBOUND;
8247 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
8248 outs() << " SUBSECTIONS_VIA_SYMBOLS";
8249 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
8251 if (f & MachO::MH_CANONICAL) {
8252 outs() << " CANONICAL";
8253 f &= ~MachO::MH_CANONICAL;
8255 if (f & MachO::MH_WEAK_DEFINES) {
8256 outs() << " WEAK_DEFINES";
8257 f &= ~MachO::MH_WEAK_DEFINES;
8259 if (f & MachO::MH_BINDS_TO_WEAK) {
8260 outs() << " BINDS_TO_WEAK";
8261 f &= ~MachO::MH_BINDS_TO_WEAK;
8263 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
8264 outs() << " ALLOW_STACK_EXECUTION";
8265 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
8267 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
8268 outs() << " DEAD_STRIPPABLE_DYLIB";
8269 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
8271 if (f & MachO::MH_PIE) {
8272 outs() << " PIE";
8273 f &= ~MachO::MH_PIE;
8275 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
8276 outs() << " NO_REEXPORTED_DYLIBS";
8277 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
8279 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
8280 outs() << " MH_HAS_TLV_DESCRIPTORS";
8281 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
8283 if (f & MachO::MH_NO_HEAP_EXECUTION) {
8284 outs() << " MH_NO_HEAP_EXECUTION";
8285 f &= ~MachO::MH_NO_HEAP_EXECUTION;
8287 if (f & MachO::MH_APP_EXTENSION_SAFE) {
8288 outs() << " APP_EXTENSION_SAFE";
8289 f &= ~MachO::MH_APP_EXTENSION_SAFE;
8291 if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) {
8292 outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
8293 f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO;
8295 if (f != 0 || flags == 0)
8296 outs() << format(" 0x%08" PRIx32, f);
8297 } else {
8298 outs() << format(" 0x%08" PRIx32, magic);
8299 outs() << format(" %7d", cputype);
8300 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8301 outs() << format(" 0x%02" PRIx32,
8302 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
8303 outs() << format(" %10u", filetype);
8304 outs() << format(" %5u", ncmds);
8305 outs() << format(" %10u", sizeofcmds);
8306 outs() << format(" 0x%08" PRIx32, flags);
8308 outs() << "\n";
8311 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
8312 StringRef SegName, uint64_t vmaddr,
8313 uint64_t vmsize, uint64_t fileoff,
8314 uint64_t filesize, uint32_t maxprot,
8315 uint32_t initprot, uint32_t nsects,
8316 uint32_t flags, uint32_t object_size,
8317 bool verbose) {
8318 uint64_t expected_cmdsize;
8319 if (cmd == MachO::LC_SEGMENT) {
8320 outs() << " cmd LC_SEGMENT\n";
8321 expected_cmdsize = nsects;
8322 expected_cmdsize *= sizeof(struct MachO::section);
8323 expected_cmdsize += sizeof(struct MachO::segment_command);
8324 } else {
8325 outs() << " cmd LC_SEGMENT_64\n";
8326 expected_cmdsize = nsects;
8327 expected_cmdsize *= sizeof(struct MachO::section_64);
8328 expected_cmdsize += sizeof(struct MachO::segment_command_64);
8330 outs() << " cmdsize " << cmdsize;
8331 if (cmdsize != expected_cmdsize)
8332 outs() << " Inconsistent size\n";
8333 else
8334 outs() << "\n";
8335 outs() << " segname " << SegName << "\n";
8336 if (cmd == MachO::LC_SEGMENT_64) {
8337 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
8338 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
8339 } else {
8340 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
8341 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
8343 outs() << " fileoff " << fileoff;
8344 if (fileoff > object_size)
8345 outs() << " (past end of file)\n";
8346 else
8347 outs() << "\n";
8348 outs() << " filesize " << filesize;
8349 if (fileoff + filesize > object_size)
8350 outs() << " (past end of file)\n";
8351 else
8352 outs() << "\n";
8353 if (verbose) {
8354 if ((maxprot &
8355 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
8356 MachO::VM_PROT_EXECUTE)) != 0)
8357 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
8358 else {
8359 outs() << " maxprot ";
8360 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
8361 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
8362 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
8364 if ((initprot &
8365 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
8366 MachO::VM_PROT_EXECUTE)) != 0)
8367 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
8368 else {
8369 outs() << " initprot ";
8370 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
8371 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
8372 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
8374 } else {
8375 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
8376 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
8378 outs() << " nsects " << nsects << "\n";
8379 if (verbose) {
8380 outs() << " flags";
8381 if (flags == 0)
8382 outs() << " (none)\n";
8383 else {
8384 if (flags & MachO::SG_HIGHVM) {
8385 outs() << " HIGHVM";
8386 flags &= ~MachO::SG_HIGHVM;
8388 if (flags & MachO::SG_FVMLIB) {
8389 outs() << " FVMLIB";
8390 flags &= ~MachO::SG_FVMLIB;
8392 if (flags & MachO::SG_NORELOC) {
8393 outs() << " NORELOC";
8394 flags &= ~MachO::SG_NORELOC;
8396 if (flags & MachO::SG_PROTECTED_VERSION_1) {
8397 outs() << " PROTECTED_VERSION_1";
8398 flags &= ~MachO::SG_PROTECTED_VERSION_1;
8400 if (flags)
8401 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
8402 else
8403 outs() << "\n";
8405 } else {
8406 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
8410 static void PrintSection(const char *sectname, const char *segname,
8411 uint64_t addr, uint64_t size, uint32_t offset,
8412 uint32_t align, uint32_t reloff, uint32_t nreloc,
8413 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
8414 uint32_t cmd, const char *sg_segname,
8415 uint32_t filetype, uint32_t object_size,
8416 bool verbose) {
8417 outs() << "Section\n";
8418 outs() << " sectname " << format("%.16s\n", sectname);
8419 outs() << " segname " << format("%.16s", segname);
8420 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
8421 outs() << " (does not match segment)\n";
8422 else
8423 outs() << "\n";
8424 if (cmd == MachO::LC_SEGMENT_64) {
8425 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
8426 outs() << " size " << format("0x%016" PRIx64, size);
8427 } else {
8428 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
8429 outs() << " size " << format("0x%08" PRIx64, size);
8431 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
8432 outs() << " (past end of file)\n";
8433 else
8434 outs() << "\n";
8435 outs() << " offset " << offset;
8436 if (offset > object_size)
8437 outs() << " (past end of file)\n";
8438 else
8439 outs() << "\n";
8440 uint32_t align_shifted = 1 << align;
8441 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
8442 outs() << " reloff " << reloff;
8443 if (reloff > object_size)
8444 outs() << " (past end of file)\n";
8445 else
8446 outs() << "\n";
8447 outs() << " nreloc " << nreloc;
8448 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
8449 outs() << " (past end of file)\n";
8450 else
8451 outs() << "\n";
8452 uint32_t section_type = flags & MachO::SECTION_TYPE;
8453 if (verbose) {
8454 outs() << " type";
8455 if (section_type == MachO::S_REGULAR)
8456 outs() << " S_REGULAR\n";
8457 else if (section_type == MachO::S_ZEROFILL)
8458 outs() << " S_ZEROFILL\n";
8459 else if (section_type == MachO::S_CSTRING_LITERALS)
8460 outs() << " S_CSTRING_LITERALS\n";
8461 else if (section_type == MachO::S_4BYTE_LITERALS)
8462 outs() << " S_4BYTE_LITERALS\n";
8463 else if (section_type == MachO::S_8BYTE_LITERALS)
8464 outs() << " S_8BYTE_LITERALS\n";
8465 else if (section_type == MachO::S_16BYTE_LITERALS)
8466 outs() << " S_16BYTE_LITERALS\n";
8467 else if (section_type == MachO::S_LITERAL_POINTERS)
8468 outs() << " S_LITERAL_POINTERS\n";
8469 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
8470 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
8471 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
8472 outs() << " S_LAZY_SYMBOL_POINTERS\n";
8473 else if (section_type == MachO::S_SYMBOL_STUBS)
8474 outs() << " S_SYMBOL_STUBS\n";
8475 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
8476 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
8477 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
8478 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
8479 else if (section_type == MachO::S_COALESCED)
8480 outs() << " S_COALESCED\n";
8481 else if (section_type == MachO::S_INTERPOSING)
8482 outs() << " S_INTERPOSING\n";
8483 else if (section_type == MachO::S_DTRACE_DOF)
8484 outs() << " S_DTRACE_DOF\n";
8485 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
8486 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
8487 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
8488 outs() << " S_THREAD_LOCAL_REGULAR\n";
8489 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
8490 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
8491 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
8492 outs() << " S_THREAD_LOCAL_VARIABLES\n";
8493 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
8494 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
8495 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
8496 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
8497 else
8498 outs() << format("0x%08" PRIx32, section_type) << "\n";
8499 outs() << "attributes";
8500 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
8501 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
8502 outs() << " PURE_INSTRUCTIONS";
8503 if (section_attributes & MachO::S_ATTR_NO_TOC)
8504 outs() << " NO_TOC";
8505 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
8506 outs() << " STRIP_STATIC_SYMS";
8507 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
8508 outs() << " NO_DEAD_STRIP";
8509 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
8510 outs() << " LIVE_SUPPORT";
8511 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
8512 outs() << " SELF_MODIFYING_CODE";
8513 if (section_attributes & MachO::S_ATTR_DEBUG)
8514 outs() << " DEBUG";
8515 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
8516 outs() << " SOME_INSTRUCTIONS";
8517 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
8518 outs() << " EXT_RELOC";
8519 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
8520 outs() << " LOC_RELOC";
8521 if (section_attributes == 0)
8522 outs() << " (none)";
8523 outs() << "\n";
8524 } else
8525 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
8526 outs() << " reserved1 " << reserved1;
8527 if (section_type == MachO::S_SYMBOL_STUBS ||
8528 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
8529 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
8530 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
8531 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
8532 outs() << " (index into indirect symbol table)\n";
8533 else
8534 outs() << "\n";
8535 outs() << " reserved2 " << reserved2;
8536 if (section_type == MachO::S_SYMBOL_STUBS)
8537 outs() << " (size of stubs)\n";
8538 else
8539 outs() << "\n";
8542 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
8543 uint32_t object_size) {
8544 outs() << " cmd LC_SYMTAB\n";
8545 outs() << " cmdsize " << st.cmdsize;
8546 if (st.cmdsize != sizeof(struct MachO::symtab_command))
8547 outs() << " Incorrect size\n";
8548 else
8549 outs() << "\n";
8550 outs() << " symoff " << st.symoff;
8551 if (st.symoff > object_size)
8552 outs() << " (past end of file)\n";
8553 else
8554 outs() << "\n";
8555 outs() << " nsyms " << st.nsyms;
8556 uint64_t big_size;
8557 if (Is64Bit) {
8558 big_size = st.nsyms;
8559 big_size *= sizeof(struct MachO::nlist_64);
8560 big_size += st.symoff;
8561 if (big_size > object_size)
8562 outs() << " (past end of file)\n";
8563 else
8564 outs() << "\n";
8565 } else {
8566 big_size = st.nsyms;
8567 big_size *= sizeof(struct MachO::nlist);
8568 big_size += st.symoff;
8569 if (big_size > object_size)
8570 outs() << " (past end of file)\n";
8571 else
8572 outs() << "\n";
8574 outs() << " stroff " << st.stroff;
8575 if (st.stroff > object_size)
8576 outs() << " (past end of file)\n";
8577 else
8578 outs() << "\n";
8579 outs() << " strsize " << st.strsize;
8580 big_size = st.stroff;
8581 big_size += st.strsize;
8582 if (big_size > object_size)
8583 outs() << " (past end of file)\n";
8584 else
8585 outs() << "\n";
8588 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
8589 uint32_t nsyms, uint32_t object_size,
8590 bool Is64Bit) {
8591 outs() << " cmd LC_DYSYMTAB\n";
8592 outs() << " cmdsize " << dyst.cmdsize;
8593 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
8594 outs() << " Incorrect size\n";
8595 else
8596 outs() << "\n";
8597 outs() << " ilocalsym " << dyst.ilocalsym;
8598 if (dyst.ilocalsym > nsyms)
8599 outs() << " (greater than the number of symbols)\n";
8600 else
8601 outs() << "\n";
8602 outs() << " nlocalsym " << dyst.nlocalsym;
8603 uint64_t big_size;
8604 big_size = dyst.ilocalsym;
8605 big_size += dyst.nlocalsym;
8606 if (big_size > nsyms)
8607 outs() << " (past the end of the symbol table)\n";
8608 else
8609 outs() << "\n";
8610 outs() << " iextdefsym " << dyst.iextdefsym;
8611 if (dyst.iextdefsym > nsyms)
8612 outs() << " (greater than the number of symbols)\n";
8613 else
8614 outs() << "\n";
8615 outs() << " nextdefsym " << dyst.nextdefsym;
8616 big_size = dyst.iextdefsym;
8617 big_size += dyst.nextdefsym;
8618 if (big_size > nsyms)
8619 outs() << " (past the end of the symbol table)\n";
8620 else
8621 outs() << "\n";
8622 outs() << " iundefsym " << dyst.iundefsym;
8623 if (dyst.iundefsym > nsyms)
8624 outs() << " (greater than the number of symbols)\n";
8625 else
8626 outs() << "\n";
8627 outs() << " nundefsym " << dyst.nundefsym;
8628 big_size = dyst.iundefsym;
8629 big_size += dyst.nundefsym;
8630 if (big_size > nsyms)
8631 outs() << " (past the end of the symbol table)\n";
8632 else
8633 outs() << "\n";
8634 outs() << " tocoff " << dyst.tocoff;
8635 if (dyst.tocoff > object_size)
8636 outs() << " (past end of file)\n";
8637 else
8638 outs() << "\n";
8639 outs() << " ntoc " << dyst.ntoc;
8640 big_size = dyst.ntoc;
8641 big_size *= sizeof(struct MachO::dylib_table_of_contents);
8642 big_size += dyst.tocoff;
8643 if (big_size > object_size)
8644 outs() << " (past end of file)\n";
8645 else
8646 outs() << "\n";
8647 outs() << " modtaboff " << dyst.modtaboff;
8648 if (dyst.modtaboff > object_size)
8649 outs() << " (past end of file)\n";
8650 else
8651 outs() << "\n";
8652 outs() << " nmodtab " << dyst.nmodtab;
8653 uint64_t modtabend;
8654 if (Is64Bit) {
8655 modtabend = dyst.nmodtab;
8656 modtabend *= sizeof(struct MachO::dylib_module_64);
8657 modtabend += dyst.modtaboff;
8658 } else {
8659 modtabend = dyst.nmodtab;
8660 modtabend *= sizeof(struct MachO::dylib_module);
8661 modtabend += dyst.modtaboff;
8663 if (modtabend > object_size)
8664 outs() << " (past end of file)\n";
8665 else
8666 outs() << "\n";
8667 outs() << " extrefsymoff " << dyst.extrefsymoff;
8668 if (dyst.extrefsymoff > object_size)
8669 outs() << " (past end of file)\n";
8670 else
8671 outs() << "\n";
8672 outs() << " nextrefsyms " << dyst.nextrefsyms;
8673 big_size = dyst.nextrefsyms;
8674 big_size *= sizeof(struct MachO::dylib_reference);
8675 big_size += dyst.extrefsymoff;
8676 if (big_size > object_size)
8677 outs() << " (past end of file)\n";
8678 else
8679 outs() << "\n";
8680 outs() << " indirectsymoff " << dyst.indirectsymoff;
8681 if (dyst.indirectsymoff > object_size)
8682 outs() << " (past end of file)\n";
8683 else
8684 outs() << "\n";
8685 outs() << " nindirectsyms " << dyst.nindirectsyms;
8686 big_size = dyst.nindirectsyms;
8687 big_size *= sizeof(uint32_t);
8688 big_size += dyst.indirectsymoff;
8689 if (big_size > object_size)
8690 outs() << " (past end of file)\n";
8691 else
8692 outs() << "\n";
8693 outs() << " extreloff " << dyst.extreloff;
8694 if (dyst.extreloff > object_size)
8695 outs() << " (past end of file)\n";
8696 else
8697 outs() << "\n";
8698 outs() << " nextrel " << dyst.nextrel;
8699 big_size = dyst.nextrel;
8700 big_size *= sizeof(struct MachO::relocation_info);
8701 big_size += dyst.extreloff;
8702 if (big_size > object_size)
8703 outs() << " (past end of file)\n";
8704 else
8705 outs() << "\n";
8706 outs() << " locreloff " << dyst.locreloff;
8707 if (dyst.locreloff > object_size)
8708 outs() << " (past end of file)\n";
8709 else
8710 outs() << "\n";
8711 outs() << " nlocrel " << dyst.nlocrel;
8712 big_size = dyst.nlocrel;
8713 big_size *= sizeof(struct MachO::relocation_info);
8714 big_size += dyst.locreloff;
8715 if (big_size > object_size)
8716 outs() << " (past end of file)\n";
8717 else
8718 outs() << "\n";
8721 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
8722 uint32_t object_size) {
8723 if (dc.cmd == MachO::LC_DYLD_INFO)
8724 outs() << " cmd LC_DYLD_INFO\n";
8725 else
8726 outs() << " cmd LC_DYLD_INFO_ONLY\n";
8727 outs() << " cmdsize " << dc.cmdsize;
8728 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
8729 outs() << " Incorrect size\n";
8730 else
8731 outs() << "\n";
8732 outs() << " rebase_off " << dc.rebase_off;
8733 if (dc.rebase_off > object_size)
8734 outs() << " (past end of file)\n";
8735 else
8736 outs() << "\n";
8737 outs() << " rebase_size " << dc.rebase_size;
8738 uint64_t big_size;
8739 big_size = dc.rebase_off;
8740 big_size += dc.rebase_size;
8741 if (big_size > object_size)
8742 outs() << " (past end of file)\n";
8743 else
8744 outs() << "\n";
8745 outs() << " bind_off " << dc.bind_off;
8746 if (dc.bind_off > object_size)
8747 outs() << " (past end of file)\n";
8748 else
8749 outs() << "\n";
8750 outs() << " bind_size " << dc.bind_size;
8751 big_size = dc.bind_off;
8752 big_size += dc.bind_size;
8753 if (big_size > object_size)
8754 outs() << " (past end of file)\n";
8755 else
8756 outs() << "\n";
8757 outs() << " weak_bind_off " << dc.weak_bind_off;
8758 if (dc.weak_bind_off > object_size)
8759 outs() << " (past end of file)\n";
8760 else
8761 outs() << "\n";
8762 outs() << " weak_bind_size " << dc.weak_bind_size;
8763 big_size = dc.weak_bind_off;
8764 big_size += dc.weak_bind_size;
8765 if (big_size > object_size)
8766 outs() << " (past end of file)\n";
8767 else
8768 outs() << "\n";
8769 outs() << " lazy_bind_off " << dc.lazy_bind_off;
8770 if (dc.lazy_bind_off > object_size)
8771 outs() << " (past end of file)\n";
8772 else
8773 outs() << "\n";
8774 outs() << " lazy_bind_size " << dc.lazy_bind_size;
8775 big_size = dc.lazy_bind_off;
8776 big_size += dc.lazy_bind_size;
8777 if (big_size > object_size)
8778 outs() << " (past end of file)\n";
8779 else
8780 outs() << "\n";
8781 outs() << " export_off " << dc.export_off;
8782 if (dc.export_off > object_size)
8783 outs() << " (past end of file)\n";
8784 else
8785 outs() << "\n";
8786 outs() << " export_size " << dc.export_size;
8787 big_size = dc.export_off;
8788 big_size += dc.export_size;
8789 if (big_size > object_size)
8790 outs() << " (past end of file)\n";
8791 else
8792 outs() << "\n";
8795 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
8796 const char *Ptr) {
8797 if (dyld.cmd == MachO::LC_ID_DYLINKER)
8798 outs() << " cmd LC_ID_DYLINKER\n";
8799 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
8800 outs() << " cmd LC_LOAD_DYLINKER\n";
8801 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
8802 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
8803 else
8804 outs() << " cmd ?(" << dyld.cmd << ")\n";
8805 outs() << " cmdsize " << dyld.cmdsize;
8806 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
8807 outs() << " Incorrect size\n";
8808 else
8809 outs() << "\n";
8810 if (dyld.name >= dyld.cmdsize)
8811 outs() << " name ?(bad offset " << dyld.name << ")\n";
8812 else {
8813 const char *P = (const char *)(Ptr) + dyld.name;
8814 outs() << " name " << P << " (offset " << dyld.name << ")\n";
8818 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
8819 outs() << " cmd LC_UUID\n";
8820 outs() << " cmdsize " << uuid.cmdsize;
8821 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
8822 outs() << " Incorrect size\n";
8823 else
8824 outs() << "\n";
8825 outs() << " uuid ";
8826 for (int i = 0; i < 16; ++i) {
8827 outs() << format("%02" PRIX32, uuid.uuid[i]);
8828 if (i == 3 || i == 5 || i == 7 || i == 9)
8829 outs() << "-";
8831 outs() << "\n";
8834 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
8835 outs() << " cmd LC_RPATH\n";
8836 outs() << " cmdsize " << rpath.cmdsize;
8837 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
8838 outs() << " Incorrect size\n";
8839 else
8840 outs() << "\n";
8841 if (rpath.path >= rpath.cmdsize)
8842 outs() << " path ?(bad offset " << rpath.path << ")\n";
8843 else {
8844 const char *P = (const char *)(Ptr) + rpath.path;
8845 outs() << " path " << P << " (offset " << rpath.path << ")\n";
8849 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
8850 StringRef LoadCmdName;
8851 switch (vd.cmd) {
8852 case MachO::LC_VERSION_MIN_MACOSX:
8853 LoadCmdName = "LC_VERSION_MIN_MACOSX";
8854 break;
8855 case MachO::LC_VERSION_MIN_IPHONEOS:
8856 LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
8857 break;
8858 case MachO::LC_VERSION_MIN_TVOS:
8859 LoadCmdName = "LC_VERSION_MIN_TVOS";
8860 break;
8861 case MachO::LC_VERSION_MIN_WATCHOS:
8862 LoadCmdName = "LC_VERSION_MIN_WATCHOS";
8863 break;
8864 default:
8865 llvm_unreachable("Unknown version min load command");
8868 outs() << " cmd " << LoadCmdName << '\n';
8869 outs() << " cmdsize " << vd.cmdsize;
8870 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
8871 outs() << " Incorrect size\n";
8872 else
8873 outs() << "\n";
8874 outs() << " version "
8875 << MachOObjectFile::getVersionMinMajor(vd, false) << "."
8876 << MachOObjectFile::getVersionMinMinor(vd, false);
8877 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
8878 if (Update != 0)
8879 outs() << "." << Update;
8880 outs() << "\n";
8881 if (vd.sdk == 0)
8882 outs() << " sdk n/a";
8883 else {
8884 outs() << " sdk "
8885 << MachOObjectFile::getVersionMinMajor(vd, true) << "."
8886 << MachOObjectFile::getVersionMinMinor(vd, true);
8888 Update = MachOObjectFile::getVersionMinUpdate(vd, true);
8889 if (Update != 0)
8890 outs() << "." << Update;
8891 outs() << "\n";
8894 static void PrintNoteLoadCommand(MachO::note_command Nt) {
8895 outs() << " cmd LC_NOTE\n";
8896 outs() << " cmdsize " << Nt.cmdsize;
8897 if (Nt.cmdsize != sizeof(struct MachO::note_command))
8898 outs() << " Incorrect size\n";
8899 else
8900 outs() << "\n";
8901 const char *d = Nt.data_owner;
8902 outs() << "data_owner " << format("%.16s\n", d);
8903 outs() << " offset " << Nt.offset << "\n";
8904 outs() << " size " << Nt.size << "\n";
8907 static void PrintBuildToolVersion(MachO::build_tool_version bv) {
8908 outs() << " tool " << MachOObjectFile::getBuildTool(bv.tool) << "\n";
8909 outs() << " version " << MachOObjectFile::getVersionString(bv.version)
8910 << "\n";
8913 static void PrintBuildVersionLoadCommand(const MachOObjectFile *obj,
8914 MachO::build_version_command bd) {
8915 outs() << " cmd LC_BUILD_VERSION\n";
8916 outs() << " cmdsize " << bd.cmdsize;
8917 if (bd.cmdsize !=
8918 sizeof(struct MachO::build_version_command) +
8919 bd.ntools * sizeof(struct MachO::build_tool_version))
8920 outs() << " Incorrect size\n";
8921 else
8922 outs() << "\n";
8923 outs() << " platform " << MachOObjectFile::getBuildPlatform(bd.platform)
8924 << "\n";
8925 if (bd.sdk)
8926 outs() << " sdk " << MachOObjectFile::getVersionString(bd.sdk)
8927 << "\n";
8928 else
8929 outs() << " sdk n/a\n";
8930 outs() << " minos " << MachOObjectFile::getVersionString(bd.minos)
8931 << "\n";
8932 outs() << " ntools " << bd.ntools << "\n";
8933 for (unsigned i = 0; i < bd.ntools; ++i) {
8934 MachO::build_tool_version bv = obj->getBuildToolVersion(i);
8935 PrintBuildToolVersion(bv);
8939 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
8940 outs() << " cmd LC_SOURCE_VERSION\n";
8941 outs() << " cmdsize " << sd.cmdsize;
8942 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
8943 outs() << " Incorrect size\n";
8944 else
8945 outs() << "\n";
8946 uint64_t a = (sd.version >> 40) & 0xffffff;
8947 uint64_t b = (sd.version >> 30) & 0x3ff;
8948 uint64_t c = (sd.version >> 20) & 0x3ff;
8949 uint64_t d = (sd.version >> 10) & 0x3ff;
8950 uint64_t e = sd.version & 0x3ff;
8951 outs() << " version " << a << "." << b;
8952 if (e != 0)
8953 outs() << "." << c << "." << d << "." << e;
8954 else if (d != 0)
8955 outs() << "." << c << "." << d;
8956 else if (c != 0)
8957 outs() << "." << c;
8958 outs() << "\n";
8961 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
8962 outs() << " cmd LC_MAIN\n";
8963 outs() << " cmdsize " << ep.cmdsize;
8964 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
8965 outs() << " Incorrect size\n";
8966 else
8967 outs() << "\n";
8968 outs() << " entryoff " << ep.entryoff << "\n";
8969 outs() << " stacksize " << ep.stacksize << "\n";
8972 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
8973 uint32_t object_size) {
8974 outs() << " cmd LC_ENCRYPTION_INFO\n";
8975 outs() << " cmdsize " << ec.cmdsize;
8976 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
8977 outs() << " Incorrect size\n";
8978 else
8979 outs() << "\n";
8980 outs() << " cryptoff " << ec.cryptoff;
8981 if (ec.cryptoff > object_size)
8982 outs() << " (past end of file)\n";
8983 else
8984 outs() << "\n";
8985 outs() << " cryptsize " << ec.cryptsize;
8986 if (ec.cryptsize > object_size)
8987 outs() << " (past end of file)\n";
8988 else
8989 outs() << "\n";
8990 outs() << " cryptid " << ec.cryptid << "\n";
8993 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
8994 uint32_t object_size) {
8995 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
8996 outs() << " cmdsize " << ec.cmdsize;
8997 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
8998 outs() << " Incorrect size\n";
8999 else
9000 outs() << "\n";
9001 outs() << " cryptoff " << ec.cryptoff;
9002 if (ec.cryptoff > object_size)
9003 outs() << " (past end of file)\n";
9004 else
9005 outs() << "\n";
9006 outs() << " cryptsize " << ec.cryptsize;
9007 if (ec.cryptsize > object_size)
9008 outs() << " (past end of file)\n";
9009 else
9010 outs() << "\n";
9011 outs() << " cryptid " << ec.cryptid << "\n";
9012 outs() << " pad " << ec.pad << "\n";
9015 static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
9016 const char *Ptr) {
9017 outs() << " cmd LC_LINKER_OPTION\n";
9018 outs() << " cmdsize " << lo.cmdsize;
9019 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
9020 outs() << " Incorrect size\n";
9021 else
9022 outs() << "\n";
9023 outs() << " count " << lo.count << "\n";
9024 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
9025 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
9026 uint32_t i = 0;
9027 while (left > 0) {
9028 while (*string == '\0' && left > 0) {
9029 string++;
9030 left--;
9032 if (left > 0) {
9033 i++;
9034 outs() << " string #" << i << " " << format("%.*s\n", left, string);
9035 uint32_t NullPos = StringRef(string, left).find('\0');
9036 uint32_t len = std::min(NullPos, left) + 1;
9037 string += len;
9038 left -= len;
9041 if (lo.count != i)
9042 outs() << " count " << lo.count << " does not match number of strings "
9043 << i << "\n";
9046 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
9047 const char *Ptr) {
9048 outs() << " cmd LC_SUB_FRAMEWORK\n";
9049 outs() << " cmdsize " << sub.cmdsize;
9050 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
9051 outs() << " Incorrect size\n";
9052 else
9053 outs() << "\n";
9054 if (sub.umbrella < sub.cmdsize) {
9055 const char *P = Ptr + sub.umbrella;
9056 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
9057 } else {
9058 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
9062 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
9063 const char *Ptr) {
9064 outs() << " cmd LC_SUB_UMBRELLA\n";
9065 outs() << " cmdsize " << sub.cmdsize;
9066 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
9067 outs() << " Incorrect size\n";
9068 else
9069 outs() << "\n";
9070 if (sub.sub_umbrella < sub.cmdsize) {
9071 const char *P = Ptr + sub.sub_umbrella;
9072 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
9073 } else {
9074 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
9078 static void PrintSubLibraryCommand(MachO::sub_library_command sub,
9079 const char *Ptr) {
9080 outs() << " cmd LC_SUB_LIBRARY\n";
9081 outs() << " cmdsize " << sub.cmdsize;
9082 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
9083 outs() << " Incorrect size\n";
9084 else
9085 outs() << "\n";
9086 if (sub.sub_library < sub.cmdsize) {
9087 const char *P = Ptr + sub.sub_library;
9088 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
9089 } else {
9090 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
9094 static void PrintSubClientCommand(MachO::sub_client_command sub,
9095 const char *Ptr) {
9096 outs() << " cmd LC_SUB_CLIENT\n";
9097 outs() << " cmdsize " << sub.cmdsize;
9098 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
9099 outs() << " Incorrect size\n";
9100 else
9101 outs() << "\n";
9102 if (sub.client < sub.cmdsize) {
9103 const char *P = Ptr + sub.client;
9104 outs() << " client " << P << " (offset " << sub.client << ")\n";
9105 } else {
9106 outs() << " client ?(bad offset " << sub.client << ")\n";
9110 static void PrintRoutinesCommand(MachO::routines_command r) {
9111 outs() << " cmd LC_ROUTINES\n";
9112 outs() << " cmdsize " << r.cmdsize;
9113 if (r.cmdsize != sizeof(struct MachO::routines_command))
9114 outs() << " Incorrect size\n";
9115 else
9116 outs() << "\n";
9117 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
9118 outs() << " init_module " << r.init_module << "\n";
9119 outs() << " reserved1 " << r.reserved1 << "\n";
9120 outs() << " reserved2 " << r.reserved2 << "\n";
9121 outs() << " reserved3 " << r.reserved3 << "\n";
9122 outs() << " reserved4 " << r.reserved4 << "\n";
9123 outs() << " reserved5 " << r.reserved5 << "\n";
9124 outs() << " reserved6 " << r.reserved6 << "\n";
9127 static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
9128 outs() << " cmd LC_ROUTINES_64\n";
9129 outs() << " cmdsize " << r.cmdsize;
9130 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
9131 outs() << " Incorrect size\n";
9132 else
9133 outs() << "\n";
9134 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
9135 outs() << " init_module " << r.init_module << "\n";
9136 outs() << " reserved1 " << r.reserved1 << "\n";
9137 outs() << " reserved2 " << r.reserved2 << "\n";
9138 outs() << " reserved3 " << r.reserved3 << "\n";
9139 outs() << " reserved4 " << r.reserved4 << "\n";
9140 outs() << " reserved5 " << r.reserved5 << "\n";
9141 outs() << " reserved6 " << r.reserved6 << "\n";
9144 static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t &cpu32) {
9145 outs() << "\t eax " << format("0x%08" PRIx32, cpu32.eax);
9146 outs() << " ebx " << format("0x%08" PRIx32, cpu32.ebx);
9147 outs() << " ecx " << format("0x%08" PRIx32, cpu32.ecx);
9148 outs() << " edx " << format("0x%08" PRIx32, cpu32.edx) << "\n";
9149 outs() << "\t edi " << format("0x%08" PRIx32, cpu32.edi);
9150 outs() << " esi " << format("0x%08" PRIx32, cpu32.esi);
9151 outs() << " ebp " << format("0x%08" PRIx32, cpu32.ebp);
9152 outs() << " esp " << format("0x%08" PRIx32, cpu32.esp) << "\n";
9153 outs() << "\t ss " << format("0x%08" PRIx32, cpu32.ss);
9154 outs() << " eflags " << format("0x%08" PRIx32, cpu32.eflags);
9155 outs() << " eip " << format("0x%08" PRIx32, cpu32.eip);
9156 outs() << " cs " << format("0x%08" PRIx32, cpu32.cs) << "\n";
9157 outs() << "\t ds " << format("0x%08" PRIx32, cpu32.ds);
9158 outs() << " es " << format("0x%08" PRIx32, cpu32.es);
9159 outs() << " fs " << format("0x%08" PRIx32, cpu32.fs);
9160 outs() << " gs " << format("0x%08" PRIx32, cpu32.gs) << "\n";
9163 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
9164 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
9165 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
9166 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
9167 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
9168 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
9169 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
9170 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
9171 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
9172 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
9173 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
9174 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
9175 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
9176 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
9177 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
9178 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
9179 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
9180 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
9181 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
9182 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
9183 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
9184 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
9187 static void Print_mmst_reg(MachO::mmst_reg_t &r) {
9188 uint32_t f;
9189 outs() << "\t mmst_reg ";
9190 for (f = 0; f < 10; f++)
9191 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
9192 outs() << "\n";
9193 outs() << "\t mmst_rsrv ";
9194 for (f = 0; f < 6; f++)
9195 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
9196 outs() << "\n";
9199 static void Print_xmm_reg(MachO::xmm_reg_t &r) {
9200 uint32_t f;
9201 outs() << "\t xmm_reg ";
9202 for (f = 0; f < 16; f++)
9203 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
9204 outs() << "\n";
9207 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
9208 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
9209 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
9210 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
9211 outs() << " denorm " << fpu.fpu_fcw.denorm;
9212 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
9213 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
9214 outs() << " undfl " << fpu.fpu_fcw.undfl;
9215 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
9216 outs() << "\t\t pc ";
9217 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
9218 outs() << "FP_PREC_24B ";
9219 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
9220 outs() << "FP_PREC_53B ";
9221 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
9222 outs() << "FP_PREC_64B ";
9223 else
9224 outs() << fpu.fpu_fcw.pc << " ";
9225 outs() << "rc ";
9226 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
9227 outs() << "FP_RND_NEAR ";
9228 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
9229 outs() << "FP_RND_DOWN ";
9230 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
9231 outs() << "FP_RND_UP ";
9232 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
9233 outs() << "FP_CHOP ";
9234 outs() << "\n";
9235 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
9236 outs() << " denorm " << fpu.fpu_fsw.denorm;
9237 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
9238 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
9239 outs() << " undfl " << fpu.fpu_fsw.undfl;
9240 outs() << " precis " << fpu.fpu_fsw.precis;
9241 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
9242 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
9243 outs() << " c0 " << fpu.fpu_fsw.c0;
9244 outs() << " c1 " << fpu.fpu_fsw.c1;
9245 outs() << " c2 " << fpu.fpu_fsw.c2;
9246 outs() << " tos " << fpu.fpu_fsw.tos;
9247 outs() << " c3 " << fpu.fpu_fsw.c3;
9248 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
9249 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
9250 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
9251 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
9252 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
9253 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
9254 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
9255 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
9256 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
9257 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
9258 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
9259 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
9260 outs() << "\n";
9261 outs() << "\t fpu_stmm0:\n";
9262 Print_mmst_reg(fpu.fpu_stmm0);
9263 outs() << "\t fpu_stmm1:\n";
9264 Print_mmst_reg(fpu.fpu_stmm1);
9265 outs() << "\t fpu_stmm2:\n";
9266 Print_mmst_reg(fpu.fpu_stmm2);
9267 outs() << "\t fpu_stmm3:\n";
9268 Print_mmst_reg(fpu.fpu_stmm3);
9269 outs() << "\t fpu_stmm4:\n";
9270 Print_mmst_reg(fpu.fpu_stmm4);
9271 outs() << "\t fpu_stmm5:\n";
9272 Print_mmst_reg(fpu.fpu_stmm5);
9273 outs() << "\t fpu_stmm6:\n";
9274 Print_mmst_reg(fpu.fpu_stmm6);
9275 outs() << "\t fpu_stmm7:\n";
9276 Print_mmst_reg(fpu.fpu_stmm7);
9277 outs() << "\t fpu_xmm0:\n";
9278 Print_xmm_reg(fpu.fpu_xmm0);
9279 outs() << "\t fpu_xmm1:\n";
9280 Print_xmm_reg(fpu.fpu_xmm1);
9281 outs() << "\t fpu_xmm2:\n";
9282 Print_xmm_reg(fpu.fpu_xmm2);
9283 outs() << "\t fpu_xmm3:\n";
9284 Print_xmm_reg(fpu.fpu_xmm3);
9285 outs() << "\t fpu_xmm4:\n";
9286 Print_xmm_reg(fpu.fpu_xmm4);
9287 outs() << "\t fpu_xmm5:\n";
9288 Print_xmm_reg(fpu.fpu_xmm5);
9289 outs() << "\t fpu_xmm6:\n";
9290 Print_xmm_reg(fpu.fpu_xmm6);
9291 outs() << "\t fpu_xmm7:\n";
9292 Print_xmm_reg(fpu.fpu_xmm7);
9293 outs() << "\t fpu_xmm8:\n";
9294 Print_xmm_reg(fpu.fpu_xmm8);
9295 outs() << "\t fpu_xmm9:\n";
9296 Print_xmm_reg(fpu.fpu_xmm9);
9297 outs() << "\t fpu_xmm10:\n";
9298 Print_xmm_reg(fpu.fpu_xmm10);
9299 outs() << "\t fpu_xmm11:\n";
9300 Print_xmm_reg(fpu.fpu_xmm11);
9301 outs() << "\t fpu_xmm12:\n";
9302 Print_xmm_reg(fpu.fpu_xmm12);
9303 outs() << "\t fpu_xmm13:\n";
9304 Print_xmm_reg(fpu.fpu_xmm13);
9305 outs() << "\t fpu_xmm14:\n";
9306 Print_xmm_reg(fpu.fpu_xmm14);
9307 outs() << "\t fpu_xmm15:\n";
9308 Print_xmm_reg(fpu.fpu_xmm15);
9309 outs() << "\t fpu_rsrv4:\n";
9310 for (uint32_t f = 0; f < 6; f++) {
9311 outs() << "\t ";
9312 for (uint32_t g = 0; g < 16; g++)
9313 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
9314 outs() << "\n";
9316 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
9317 outs() << "\n";
9320 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
9321 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
9322 outs() << " err " << format("0x%08" PRIx32, exc64.err);
9323 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
9326 static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) {
9327 outs() << "\t r0 " << format("0x%08" PRIx32, cpu32.r[0]);
9328 outs() << " r1 " << format("0x%08" PRIx32, cpu32.r[1]);
9329 outs() << " r2 " << format("0x%08" PRIx32, cpu32.r[2]);
9330 outs() << " r3 " << format("0x%08" PRIx32, cpu32.r[3]) << "\n";
9331 outs() << "\t r4 " << format("0x%08" PRIx32, cpu32.r[4]);
9332 outs() << " r5 " << format("0x%08" PRIx32, cpu32.r[5]);
9333 outs() << " r6 " << format("0x%08" PRIx32, cpu32.r[6]);
9334 outs() << " r7 " << format("0x%08" PRIx32, cpu32.r[7]) << "\n";
9335 outs() << "\t r8 " << format("0x%08" PRIx32, cpu32.r[8]);
9336 outs() << " r9 " << format("0x%08" PRIx32, cpu32.r[9]);
9337 outs() << " r10 " << format("0x%08" PRIx32, cpu32.r[10]);
9338 outs() << " r11 " << format("0x%08" PRIx32, cpu32.r[11]) << "\n";
9339 outs() << "\t r12 " << format("0x%08" PRIx32, cpu32.r[12]);
9340 outs() << " sp " << format("0x%08" PRIx32, cpu32.sp);
9341 outs() << " lr " << format("0x%08" PRIx32, cpu32.lr);
9342 outs() << " pc " << format("0x%08" PRIx32, cpu32.pc) << "\n";
9343 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n";
9346 static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) {
9347 outs() << "\t x0 " << format("0x%016" PRIx64, cpu64.x[0]);
9348 outs() << " x1 " << format("0x%016" PRIx64, cpu64.x[1]);
9349 outs() << " x2 " << format("0x%016" PRIx64, cpu64.x[2]) << "\n";
9350 outs() << "\t x3 " << format("0x%016" PRIx64, cpu64.x[3]);
9351 outs() << " x4 " << format("0x%016" PRIx64, cpu64.x[4]);
9352 outs() << " x5 " << format("0x%016" PRIx64, cpu64.x[5]) << "\n";
9353 outs() << "\t x6 " << format("0x%016" PRIx64, cpu64.x[6]);
9354 outs() << " x7 " << format("0x%016" PRIx64, cpu64.x[7]);
9355 outs() << " x8 " << format("0x%016" PRIx64, cpu64.x[8]) << "\n";
9356 outs() << "\t x9 " << format("0x%016" PRIx64, cpu64.x[9]);
9357 outs() << " x10 " << format("0x%016" PRIx64, cpu64.x[10]);
9358 outs() << " x11 " << format("0x%016" PRIx64, cpu64.x[11]) << "\n";
9359 outs() << "\t x12 " << format("0x%016" PRIx64, cpu64.x[12]);
9360 outs() << " x13 " << format("0x%016" PRIx64, cpu64.x[13]);
9361 outs() << " x14 " << format("0x%016" PRIx64, cpu64.x[14]) << "\n";
9362 outs() << "\t x15 " << format("0x%016" PRIx64, cpu64.x[15]);
9363 outs() << " x16 " << format("0x%016" PRIx64, cpu64.x[16]);
9364 outs() << " x17 " << format("0x%016" PRIx64, cpu64.x[17]) << "\n";
9365 outs() << "\t x18 " << format("0x%016" PRIx64, cpu64.x[18]);
9366 outs() << " x19 " << format("0x%016" PRIx64, cpu64.x[19]);
9367 outs() << " x20 " << format("0x%016" PRIx64, cpu64.x[20]) << "\n";
9368 outs() << "\t x21 " << format("0x%016" PRIx64, cpu64.x[21]);
9369 outs() << " x22 " << format("0x%016" PRIx64, cpu64.x[22]);
9370 outs() << " x23 " << format("0x%016" PRIx64, cpu64.x[23]) << "\n";
9371 outs() << "\t x24 " << format("0x%016" PRIx64, cpu64.x[24]);
9372 outs() << " x25 " << format("0x%016" PRIx64, cpu64.x[25]);
9373 outs() << " x26 " << format("0x%016" PRIx64, cpu64.x[26]) << "\n";
9374 outs() << "\t x27 " << format("0x%016" PRIx64, cpu64.x[27]);
9375 outs() << " x28 " << format("0x%016" PRIx64, cpu64.x[28]);
9376 outs() << " fp " << format("0x%016" PRIx64, cpu64.fp) << "\n";
9377 outs() << "\t lr " << format("0x%016" PRIx64, cpu64.lr);
9378 outs() << " sp " << format("0x%016" PRIx64, cpu64.sp);
9379 outs() << " pc " << format("0x%016" PRIx64, cpu64.pc) << "\n";
9380 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu64.cpsr) << "\n";
9383 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
9384 bool isLittleEndian, uint32_t cputype) {
9385 if (t.cmd == MachO::LC_THREAD)
9386 outs() << " cmd LC_THREAD\n";
9387 else if (t.cmd == MachO::LC_UNIXTHREAD)
9388 outs() << " cmd LC_UNIXTHREAD\n";
9389 else
9390 outs() << " cmd " << t.cmd << " (unknown)\n";
9391 outs() << " cmdsize " << t.cmdsize;
9392 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
9393 outs() << " Incorrect size\n";
9394 else
9395 outs() << "\n";
9397 const char *begin = Ptr + sizeof(struct MachO::thread_command);
9398 const char *end = Ptr + t.cmdsize;
9399 uint32_t flavor, count, left;
9400 if (cputype == MachO::CPU_TYPE_I386) {
9401 while (begin < end) {
9402 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9403 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9404 begin += sizeof(uint32_t);
9405 } else {
9406 flavor = 0;
9407 begin = end;
9409 if (isLittleEndian != sys::IsLittleEndianHost)
9410 sys::swapByteOrder(flavor);
9411 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9412 memcpy((char *)&count, begin, sizeof(uint32_t));
9413 begin += sizeof(uint32_t);
9414 } else {
9415 count = 0;
9416 begin = end;
9418 if (isLittleEndian != sys::IsLittleEndianHost)
9419 sys::swapByteOrder(count);
9420 if (flavor == MachO::x86_THREAD_STATE32) {
9421 outs() << " flavor i386_THREAD_STATE\n";
9422 if (count == MachO::x86_THREAD_STATE32_COUNT)
9423 outs() << " count i386_THREAD_STATE_COUNT\n";
9424 else
9425 outs() << " count " << count
9426 << " (not x86_THREAD_STATE32_COUNT)\n";
9427 MachO::x86_thread_state32_t cpu32;
9428 left = end - begin;
9429 if (left >= sizeof(MachO::x86_thread_state32_t)) {
9430 memcpy(&cpu32, begin, sizeof(MachO::x86_thread_state32_t));
9431 begin += sizeof(MachO::x86_thread_state32_t);
9432 } else {
9433 memset(&cpu32, '\0', sizeof(MachO::x86_thread_state32_t));
9434 memcpy(&cpu32, begin, left);
9435 begin += left;
9437 if (isLittleEndian != sys::IsLittleEndianHost)
9438 swapStruct(cpu32);
9439 Print_x86_thread_state32_t(cpu32);
9440 } else if (flavor == MachO::x86_THREAD_STATE) {
9441 outs() << " flavor x86_THREAD_STATE\n";
9442 if (count == MachO::x86_THREAD_STATE_COUNT)
9443 outs() << " count x86_THREAD_STATE_COUNT\n";
9444 else
9445 outs() << " count " << count
9446 << " (not x86_THREAD_STATE_COUNT)\n";
9447 struct MachO::x86_thread_state_t ts;
9448 left = end - begin;
9449 if (left >= sizeof(MachO::x86_thread_state_t)) {
9450 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
9451 begin += sizeof(MachO::x86_thread_state_t);
9452 } else {
9453 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
9454 memcpy(&ts, begin, left);
9455 begin += left;
9457 if (isLittleEndian != sys::IsLittleEndianHost)
9458 swapStruct(ts);
9459 if (ts.tsh.flavor == MachO::x86_THREAD_STATE32) {
9460 outs() << "\t tsh.flavor x86_THREAD_STATE32 ";
9461 if (ts.tsh.count == MachO::x86_THREAD_STATE32_COUNT)
9462 outs() << "tsh.count x86_THREAD_STATE32_COUNT\n";
9463 else
9464 outs() << "tsh.count " << ts.tsh.count
9465 << " (not x86_THREAD_STATE32_COUNT\n";
9466 Print_x86_thread_state32_t(ts.uts.ts32);
9467 } else {
9468 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
9469 << ts.tsh.count << "\n";
9471 } else {
9472 outs() << " flavor " << flavor << " (unknown)\n";
9473 outs() << " count " << count << "\n";
9474 outs() << " state (unknown)\n";
9475 begin += count * sizeof(uint32_t);
9478 } else if (cputype == MachO::CPU_TYPE_X86_64) {
9479 while (begin < end) {
9480 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9481 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9482 begin += sizeof(uint32_t);
9483 } else {
9484 flavor = 0;
9485 begin = end;
9487 if (isLittleEndian != sys::IsLittleEndianHost)
9488 sys::swapByteOrder(flavor);
9489 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9490 memcpy((char *)&count, begin, sizeof(uint32_t));
9491 begin += sizeof(uint32_t);
9492 } else {
9493 count = 0;
9494 begin = end;
9496 if (isLittleEndian != sys::IsLittleEndianHost)
9497 sys::swapByteOrder(count);
9498 if (flavor == MachO::x86_THREAD_STATE64) {
9499 outs() << " flavor x86_THREAD_STATE64\n";
9500 if (count == MachO::x86_THREAD_STATE64_COUNT)
9501 outs() << " count x86_THREAD_STATE64_COUNT\n";
9502 else
9503 outs() << " count " << count
9504 << " (not x86_THREAD_STATE64_COUNT)\n";
9505 MachO::x86_thread_state64_t cpu64;
9506 left = end - begin;
9507 if (left >= sizeof(MachO::x86_thread_state64_t)) {
9508 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
9509 begin += sizeof(MachO::x86_thread_state64_t);
9510 } else {
9511 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
9512 memcpy(&cpu64, begin, left);
9513 begin += left;
9515 if (isLittleEndian != sys::IsLittleEndianHost)
9516 swapStruct(cpu64);
9517 Print_x86_thread_state64_t(cpu64);
9518 } else if (flavor == MachO::x86_THREAD_STATE) {
9519 outs() << " flavor x86_THREAD_STATE\n";
9520 if (count == MachO::x86_THREAD_STATE_COUNT)
9521 outs() << " count x86_THREAD_STATE_COUNT\n";
9522 else
9523 outs() << " count " << count
9524 << " (not x86_THREAD_STATE_COUNT)\n";
9525 struct MachO::x86_thread_state_t ts;
9526 left = end - begin;
9527 if (left >= sizeof(MachO::x86_thread_state_t)) {
9528 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
9529 begin += sizeof(MachO::x86_thread_state_t);
9530 } else {
9531 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
9532 memcpy(&ts, begin, left);
9533 begin += left;
9535 if (isLittleEndian != sys::IsLittleEndianHost)
9536 swapStruct(ts);
9537 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
9538 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
9539 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
9540 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
9541 else
9542 outs() << "tsh.count " << ts.tsh.count
9543 << " (not x86_THREAD_STATE64_COUNT\n";
9544 Print_x86_thread_state64_t(ts.uts.ts64);
9545 } else {
9546 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
9547 << ts.tsh.count << "\n";
9549 } else if (flavor == MachO::x86_FLOAT_STATE) {
9550 outs() << " flavor x86_FLOAT_STATE\n";
9551 if (count == MachO::x86_FLOAT_STATE_COUNT)
9552 outs() << " count x86_FLOAT_STATE_COUNT\n";
9553 else
9554 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
9555 struct MachO::x86_float_state_t fs;
9556 left = end - begin;
9557 if (left >= sizeof(MachO::x86_float_state_t)) {
9558 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
9559 begin += sizeof(MachO::x86_float_state_t);
9560 } else {
9561 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
9562 memcpy(&fs, begin, left);
9563 begin += left;
9565 if (isLittleEndian != sys::IsLittleEndianHost)
9566 swapStruct(fs);
9567 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
9568 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
9569 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
9570 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
9571 else
9572 outs() << "fsh.count " << fs.fsh.count
9573 << " (not x86_FLOAT_STATE64_COUNT\n";
9574 Print_x86_float_state_t(fs.ufs.fs64);
9575 } else {
9576 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
9577 << fs.fsh.count << "\n";
9579 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
9580 outs() << " flavor x86_EXCEPTION_STATE\n";
9581 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
9582 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
9583 else
9584 outs() << " count " << count
9585 << " (not x86_EXCEPTION_STATE_COUNT)\n";
9586 struct MachO::x86_exception_state_t es;
9587 left = end - begin;
9588 if (left >= sizeof(MachO::x86_exception_state_t)) {
9589 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
9590 begin += sizeof(MachO::x86_exception_state_t);
9591 } else {
9592 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
9593 memcpy(&es, begin, left);
9594 begin += left;
9596 if (isLittleEndian != sys::IsLittleEndianHost)
9597 swapStruct(es);
9598 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
9599 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
9600 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
9601 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
9602 else
9603 outs() << "\t esh.count " << es.esh.count
9604 << " (not x86_EXCEPTION_STATE64_COUNT\n";
9605 Print_x86_exception_state_t(es.ues.es64);
9606 } else {
9607 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
9608 << es.esh.count << "\n";
9610 } else if (flavor == MachO::x86_EXCEPTION_STATE64) {
9611 outs() << " flavor x86_EXCEPTION_STATE64\n";
9612 if (count == MachO::x86_EXCEPTION_STATE64_COUNT)
9613 outs() << " count x86_EXCEPTION_STATE64_COUNT\n";
9614 else
9615 outs() << " count " << count
9616 << " (not x86_EXCEPTION_STATE64_COUNT)\n";
9617 struct MachO::x86_exception_state64_t es64;
9618 left = end - begin;
9619 if (left >= sizeof(MachO::x86_exception_state64_t)) {
9620 memcpy(&es64, begin, sizeof(MachO::x86_exception_state64_t));
9621 begin += sizeof(MachO::x86_exception_state64_t);
9622 } else {
9623 memset(&es64, '\0', sizeof(MachO::x86_exception_state64_t));
9624 memcpy(&es64, begin, left);
9625 begin += left;
9627 if (isLittleEndian != sys::IsLittleEndianHost)
9628 swapStruct(es64);
9629 Print_x86_exception_state_t(es64);
9630 } else {
9631 outs() << " flavor " << flavor << " (unknown)\n";
9632 outs() << " count " << count << "\n";
9633 outs() << " state (unknown)\n";
9634 begin += count * sizeof(uint32_t);
9637 } else if (cputype == MachO::CPU_TYPE_ARM) {
9638 while (begin < end) {
9639 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9640 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9641 begin += sizeof(uint32_t);
9642 } else {
9643 flavor = 0;
9644 begin = end;
9646 if (isLittleEndian != sys::IsLittleEndianHost)
9647 sys::swapByteOrder(flavor);
9648 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9649 memcpy((char *)&count, begin, sizeof(uint32_t));
9650 begin += sizeof(uint32_t);
9651 } else {
9652 count = 0;
9653 begin = end;
9655 if (isLittleEndian != sys::IsLittleEndianHost)
9656 sys::swapByteOrder(count);
9657 if (flavor == MachO::ARM_THREAD_STATE) {
9658 outs() << " flavor ARM_THREAD_STATE\n";
9659 if (count == MachO::ARM_THREAD_STATE_COUNT)
9660 outs() << " count ARM_THREAD_STATE_COUNT\n";
9661 else
9662 outs() << " count " << count
9663 << " (not ARM_THREAD_STATE_COUNT)\n";
9664 MachO::arm_thread_state32_t cpu32;
9665 left = end - begin;
9666 if (left >= sizeof(MachO::arm_thread_state32_t)) {
9667 memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t));
9668 begin += sizeof(MachO::arm_thread_state32_t);
9669 } else {
9670 memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t));
9671 memcpy(&cpu32, begin, left);
9672 begin += left;
9674 if (isLittleEndian != sys::IsLittleEndianHost)
9675 swapStruct(cpu32);
9676 Print_arm_thread_state32_t(cpu32);
9677 } else {
9678 outs() << " flavor " << flavor << " (unknown)\n";
9679 outs() << " count " << count << "\n";
9680 outs() << " state (unknown)\n";
9681 begin += count * sizeof(uint32_t);
9684 } else if (cputype == MachO::CPU_TYPE_ARM64) {
9685 while (begin < end) {
9686 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9687 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9688 begin += sizeof(uint32_t);
9689 } else {
9690 flavor = 0;
9691 begin = end;
9693 if (isLittleEndian != sys::IsLittleEndianHost)
9694 sys::swapByteOrder(flavor);
9695 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9696 memcpy((char *)&count, begin, sizeof(uint32_t));
9697 begin += sizeof(uint32_t);
9698 } else {
9699 count = 0;
9700 begin = end;
9702 if (isLittleEndian != sys::IsLittleEndianHost)
9703 sys::swapByteOrder(count);
9704 if (flavor == MachO::ARM_THREAD_STATE64) {
9705 outs() << " flavor ARM_THREAD_STATE64\n";
9706 if (count == MachO::ARM_THREAD_STATE64_COUNT)
9707 outs() << " count ARM_THREAD_STATE64_COUNT\n";
9708 else
9709 outs() << " count " << count
9710 << " (not ARM_THREAD_STATE64_COUNT)\n";
9711 MachO::arm_thread_state64_t cpu64;
9712 left = end - begin;
9713 if (left >= sizeof(MachO::arm_thread_state64_t)) {
9714 memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t));
9715 begin += sizeof(MachO::arm_thread_state64_t);
9716 } else {
9717 memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t));
9718 memcpy(&cpu64, begin, left);
9719 begin += left;
9721 if (isLittleEndian != sys::IsLittleEndianHost)
9722 swapStruct(cpu64);
9723 Print_arm_thread_state64_t(cpu64);
9724 } else {
9725 outs() << " flavor " << flavor << " (unknown)\n";
9726 outs() << " count " << count << "\n";
9727 outs() << " state (unknown)\n";
9728 begin += count * sizeof(uint32_t);
9731 } else {
9732 while (begin < end) {
9733 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9734 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9735 begin += sizeof(uint32_t);
9736 } else {
9737 flavor = 0;
9738 begin = end;
9740 if (isLittleEndian != sys::IsLittleEndianHost)
9741 sys::swapByteOrder(flavor);
9742 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9743 memcpy((char *)&count, begin, sizeof(uint32_t));
9744 begin += sizeof(uint32_t);
9745 } else {
9746 count = 0;
9747 begin = end;
9749 if (isLittleEndian != sys::IsLittleEndianHost)
9750 sys::swapByteOrder(count);
9751 outs() << " flavor " << flavor << "\n";
9752 outs() << " count " << count << "\n";
9753 outs() << " state (Unknown cputype/cpusubtype)\n";
9754 begin += count * sizeof(uint32_t);
9759 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
9760 if (dl.cmd == MachO::LC_ID_DYLIB)
9761 outs() << " cmd LC_ID_DYLIB\n";
9762 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
9763 outs() << " cmd LC_LOAD_DYLIB\n";
9764 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
9765 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
9766 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
9767 outs() << " cmd LC_REEXPORT_DYLIB\n";
9768 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
9769 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
9770 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
9771 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
9772 else
9773 outs() << " cmd " << dl.cmd << " (unknown)\n";
9774 outs() << " cmdsize " << dl.cmdsize;
9775 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
9776 outs() << " Incorrect size\n";
9777 else
9778 outs() << "\n";
9779 if (dl.dylib.name < dl.cmdsize) {
9780 const char *P = (const char *)(Ptr) + dl.dylib.name;
9781 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
9782 } else {
9783 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
9785 outs() << " time stamp " << dl.dylib.timestamp << " ";
9786 time_t t = dl.dylib.timestamp;
9787 outs() << ctime(&t);
9788 outs() << " current version ";
9789 if (dl.dylib.current_version == 0xffffffff)
9790 outs() << "n/a\n";
9791 else
9792 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
9793 << ((dl.dylib.current_version >> 8) & 0xff) << "."
9794 << (dl.dylib.current_version & 0xff) << "\n";
9795 outs() << "compatibility version ";
9796 if (dl.dylib.compatibility_version == 0xffffffff)
9797 outs() << "n/a\n";
9798 else
9799 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
9800 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
9801 << (dl.dylib.compatibility_version & 0xff) << "\n";
9804 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
9805 uint32_t object_size) {
9806 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
9807 outs() << " cmd LC_CODE_SIGNATURE\n";
9808 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
9809 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
9810 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
9811 outs() << " cmd LC_FUNCTION_STARTS\n";
9812 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
9813 outs() << " cmd LC_DATA_IN_CODE\n";
9814 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
9815 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
9816 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
9817 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
9818 else
9819 outs() << " cmd " << ld.cmd << " (?)\n";
9820 outs() << " cmdsize " << ld.cmdsize;
9821 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
9822 outs() << " Incorrect size\n";
9823 else
9824 outs() << "\n";
9825 outs() << " dataoff " << ld.dataoff;
9826 if (ld.dataoff > object_size)
9827 outs() << " (past end of file)\n";
9828 else
9829 outs() << "\n";
9830 outs() << " datasize " << ld.datasize;
9831 uint64_t big_size = ld.dataoff;
9832 big_size += ld.datasize;
9833 if (big_size > object_size)
9834 outs() << " (past end of file)\n";
9835 else
9836 outs() << "\n";
9839 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
9840 uint32_t cputype, bool verbose) {
9841 StringRef Buf = Obj->getData();
9842 unsigned Index = 0;
9843 for (const auto &Command : Obj->load_commands()) {
9844 outs() << "Load command " << Index++ << "\n";
9845 if (Command.C.cmd == MachO::LC_SEGMENT) {
9846 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
9847 const char *sg_segname = SLC.segname;
9848 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
9849 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
9850 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
9851 verbose);
9852 for (unsigned j = 0; j < SLC.nsects; j++) {
9853 MachO::section S = Obj->getSection(Command, j);
9854 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
9855 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
9856 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
9858 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
9859 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
9860 const char *sg_segname = SLC_64.segname;
9861 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
9862 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
9863 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
9864 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
9865 for (unsigned j = 0; j < SLC_64.nsects; j++) {
9866 MachO::section_64 S_64 = Obj->getSection64(Command, j);
9867 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
9868 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
9869 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
9870 sg_segname, filetype, Buf.size(), verbose);
9872 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
9873 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
9874 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
9875 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
9876 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
9877 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
9878 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
9879 Obj->is64Bit());
9880 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
9881 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
9882 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
9883 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
9884 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
9885 Command.C.cmd == MachO::LC_ID_DYLINKER ||
9886 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
9887 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
9888 PrintDyldLoadCommand(Dyld, Command.Ptr);
9889 } else if (Command.C.cmd == MachO::LC_UUID) {
9890 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
9891 PrintUuidLoadCommand(Uuid);
9892 } else if (Command.C.cmd == MachO::LC_RPATH) {
9893 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
9894 PrintRpathLoadCommand(Rpath, Command.Ptr);
9895 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
9896 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
9897 Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
9898 Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
9899 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
9900 PrintVersionMinLoadCommand(Vd);
9901 } else if (Command.C.cmd == MachO::LC_NOTE) {
9902 MachO::note_command Nt = Obj->getNoteLoadCommand(Command);
9903 PrintNoteLoadCommand(Nt);
9904 } else if (Command.C.cmd == MachO::LC_BUILD_VERSION) {
9905 MachO::build_version_command Bv =
9906 Obj->getBuildVersionLoadCommand(Command);
9907 PrintBuildVersionLoadCommand(Obj, Bv);
9908 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
9909 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
9910 PrintSourceVersionCommand(Sd);
9911 } else if (Command.C.cmd == MachO::LC_MAIN) {
9912 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
9913 PrintEntryPointCommand(Ep);
9914 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
9915 MachO::encryption_info_command Ei =
9916 Obj->getEncryptionInfoCommand(Command);
9917 PrintEncryptionInfoCommand(Ei, Buf.size());
9918 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
9919 MachO::encryption_info_command_64 Ei =
9920 Obj->getEncryptionInfoCommand64(Command);
9921 PrintEncryptionInfoCommand64(Ei, Buf.size());
9922 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
9923 MachO::linker_option_command Lo =
9924 Obj->getLinkerOptionLoadCommand(Command);
9925 PrintLinkerOptionCommand(Lo, Command.Ptr);
9926 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
9927 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
9928 PrintSubFrameworkCommand(Sf, Command.Ptr);
9929 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
9930 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
9931 PrintSubUmbrellaCommand(Sf, Command.Ptr);
9932 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
9933 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
9934 PrintSubLibraryCommand(Sl, Command.Ptr);
9935 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
9936 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
9937 PrintSubClientCommand(Sc, Command.Ptr);
9938 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
9939 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
9940 PrintRoutinesCommand(Rc);
9941 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
9942 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
9943 PrintRoutinesCommand64(Rc);
9944 } else if (Command.C.cmd == MachO::LC_THREAD ||
9945 Command.C.cmd == MachO::LC_UNIXTHREAD) {
9946 MachO::thread_command Tc = Obj->getThreadCommand(Command);
9947 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
9948 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
9949 Command.C.cmd == MachO::LC_ID_DYLIB ||
9950 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
9951 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
9952 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
9953 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
9954 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
9955 PrintDylibCommand(Dl, Command.Ptr);
9956 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
9957 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
9958 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
9959 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
9960 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
9961 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
9962 MachO::linkedit_data_command Ld =
9963 Obj->getLinkeditDataLoadCommand(Command);
9964 PrintLinkEditDataCommand(Ld, Buf.size());
9965 } else {
9966 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
9967 << ")\n";
9968 outs() << " cmdsize " << Command.C.cmdsize << "\n";
9969 // TODO: get and print the raw bytes of the load command.
9971 // TODO: print all the other kinds of load commands.
9975 static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
9976 if (Obj->is64Bit()) {
9977 MachO::mach_header_64 H_64;
9978 H_64 = Obj->getHeader64();
9979 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
9980 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
9981 } else {
9982 MachO::mach_header H;
9983 H = Obj->getHeader();
9984 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
9985 H.sizeofcmds, H.flags, verbose);
9989 void printMachOFileHeader(const object::ObjectFile *Obj) {
9990 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
9991 PrintMachHeader(file, !NonVerbose);
9994 void printMachOLoadCommands(const object::ObjectFile *Obj) {
9995 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
9996 uint32_t filetype = 0;
9997 uint32_t cputype = 0;
9998 if (file->is64Bit()) {
9999 MachO::mach_header_64 H_64;
10000 H_64 = file->getHeader64();
10001 filetype = H_64.filetype;
10002 cputype = H_64.cputype;
10003 } else {
10004 MachO::mach_header H;
10005 H = file->getHeader();
10006 filetype = H.filetype;
10007 cputype = H.cputype;
10009 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
10012 //===----------------------------------------------------------------------===//
10013 // export trie dumping
10014 //===----------------------------------------------------------------------===//
10016 void printMachOExportsTrie(const object::MachOObjectFile *Obj) {
10017 uint64_t BaseSegmentAddress = 0;
10018 for (const auto &Command : Obj->load_commands()) {
10019 if (Command.C.cmd == MachO::LC_SEGMENT) {
10020 MachO::segment_command Seg = Obj->getSegmentLoadCommand(Command);
10021 if (Seg.fileoff == 0 && Seg.filesize != 0) {
10022 BaseSegmentAddress = Seg.vmaddr;
10023 break;
10025 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
10026 MachO::segment_command_64 Seg = Obj->getSegment64LoadCommand(Command);
10027 if (Seg.fileoff == 0 && Seg.filesize != 0) {
10028 BaseSegmentAddress = Seg.vmaddr;
10029 break;
10033 Error Err = Error::success();
10034 for (const object::ExportEntry &Entry : Obj->exports(Err)) {
10035 uint64_t Flags = Entry.flags();
10036 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
10037 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
10038 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
10039 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
10040 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
10041 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
10042 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
10043 if (ReExport)
10044 outs() << "[re-export] ";
10045 else
10046 outs() << format("0x%08llX ",
10047 Entry.address() + BaseSegmentAddress);
10048 outs() << Entry.name();
10049 if (WeakDef || ThreadLocal || Resolver || Abs) {
10050 bool NeedsComma = false;
10051 outs() << " [";
10052 if (WeakDef) {
10053 outs() << "weak_def";
10054 NeedsComma = true;
10056 if (ThreadLocal) {
10057 if (NeedsComma)
10058 outs() << ", ";
10059 outs() << "per-thread";
10060 NeedsComma = true;
10062 if (Abs) {
10063 if (NeedsComma)
10064 outs() << ", ";
10065 outs() << "absolute";
10066 NeedsComma = true;
10068 if (Resolver) {
10069 if (NeedsComma)
10070 outs() << ", ";
10071 outs() << format("resolver=0x%08llX", Entry.other());
10072 NeedsComma = true;
10074 outs() << "]";
10076 if (ReExport) {
10077 StringRef DylibName = "unknown";
10078 int Ordinal = Entry.other() - 1;
10079 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
10080 if (Entry.otherName().empty())
10081 outs() << " (from " << DylibName << ")";
10082 else
10083 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
10085 outs() << "\n";
10087 if (Err)
10088 report_error(std::move(Err), Obj->getFileName());
10091 //===----------------------------------------------------------------------===//
10092 // rebase table dumping
10093 //===----------------------------------------------------------------------===//
10095 void printMachORebaseTable(object::MachOObjectFile *Obj) {
10096 outs() << "segment section address type\n";
10097 Error Err = Error::success();
10098 for (const object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) {
10099 StringRef SegmentName = Entry.segmentName();
10100 StringRef SectionName = Entry.sectionName();
10101 uint64_t Address = Entry.address();
10103 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
10104 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
10105 SegmentName.str().c_str(), SectionName.str().c_str(),
10106 Address, Entry.typeName().str().c_str());
10108 if (Err)
10109 report_error(std::move(Err), Obj->getFileName());
10112 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
10113 StringRef DylibName;
10114 switch (Ordinal) {
10115 case MachO::BIND_SPECIAL_DYLIB_SELF:
10116 return "this-image";
10117 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
10118 return "main-executable";
10119 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
10120 return "flat-namespace";
10121 default:
10122 if (Ordinal > 0) {
10123 std::error_code EC =
10124 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
10125 if (EC)
10126 return "<<bad library ordinal>>";
10127 return DylibName;
10130 return "<<unknown special ordinal>>";
10133 //===----------------------------------------------------------------------===//
10134 // bind table dumping
10135 //===----------------------------------------------------------------------===//
10137 void printMachOBindTable(object::MachOObjectFile *Obj) {
10138 // Build table of sections so names can used in final output.
10139 outs() << "segment section address type "
10140 "addend dylib symbol\n";
10141 Error Err = Error::success();
10142 for (const object::MachOBindEntry &Entry : Obj->bindTable(Err)) {
10143 StringRef SegmentName = Entry.segmentName();
10144 StringRef SectionName = Entry.sectionName();
10145 uint64_t Address = Entry.address();
10147 // Table lines look like:
10148 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
10149 StringRef Attr;
10150 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
10151 Attr = " (weak_import)";
10152 outs() << left_justify(SegmentName, 8) << " "
10153 << left_justify(SectionName, 18) << " "
10154 << format_hex(Address, 10, true) << " "
10155 << left_justify(Entry.typeName(), 8) << " "
10156 << format_decimal(Entry.addend(), 8) << " "
10157 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
10158 << Entry.symbolName() << Attr << "\n";
10160 if (Err)
10161 report_error(std::move(Err), Obj->getFileName());
10164 //===----------------------------------------------------------------------===//
10165 // lazy bind table dumping
10166 //===----------------------------------------------------------------------===//
10168 void printMachOLazyBindTable(object::MachOObjectFile *Obj) {
10169 outs() << "segment section address "
10170 "dylib symbol\n";
10171 Error Err = Error::success();
10172 for (const object::MachOBindEntry &Entry : Obj->lazyBindTable(Err)) {
10173 StringRef SegmentName = Entry.segmentName();
10174 StringRef SectionName = Entry.sectionName();
10175 uint64_t Address = Entry.address();
10177 // Table lines look like:
10178 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
10179 outs() << left_justify(SegmentName, 8) << " "
10180 << left_justify(SectionName, 18) << " "
10181 << format_hex(Address, 10, true) << " "
10182 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
10183 << Entry.symbolName() << "\n";
10185 if (Err)
10186 report_error(std::move(Err), Obj->getFileName());
10189 //===----------------------------------------------------------------------===//
10190 // weak bind table dumping
10191 //===----------------------------------------------------------------------===//
10193 void printMachOWeakBindTable(object::MachOObjectFile *Obj) {
10194 outs() << "segment section address "
10195 "type addend symbol\n";
10196 Error Err = Error::success();
10197 for (const object::MachOBindEntry &Entry : Obj->weakBindTable(Err)) {
10198 // Strong symbols don't have a location to update.
10199 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
10200 outs() << " strong "
10201 << Entry.symbolName() << "\n";
10202 continue;
10204 StringRef SegmentName = Entry.segmentName();
10205 StringRef SectionName = Entry.sectionName();
10206 uint64_t Address = Entry.address();
10208 // Table lines look like:
10209 // __DATA __data 0x00001000 pointer 0 _foo
10210 outs() << left_justify(SegmentName, 8) << " "
10211 << left_justify(SectionName, 18) << " "
10212 << format_hex(Address, 10, true) << " "
10213 << left_justify(Entry.typeName(), 8) << " "
10214 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
10215 << "\n";
10217 if (Err)
10218 report_error(std::move(Err), Obj->getFileName());
10221 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
10222 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
10223 // information for that address. If the address is found its binding symbol
10224 // name is returned. If not nullptr is returned.
10225 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
10226 struct DisassembleInfo *info) {
10227 if (info->bindtable == nullptr) {
10228 info->bindtable = llvm::make_unique<SymbolAddressMap>();
10229 Error Err = Error::success();
10230 for (const object::MachOBindEntry &Entry : info->O->bindTable(Err)) {
10231 uint64_t Address = Entry.address();
10232 StringRef name = Entry.symbolName();
10233 if (!name.empty())
10234 (*info->bindtable)[Address] = name;
10236 if (Err)
10237 report_error(std::move(Err), info->O->getFileName());
10239 auto name = info->bindtable->lookup(ReferenceValue);
10240 return !name.empty() ? name.data() : nullptr;
10243 void printLazyBindTable(ObjectFile *o) {
10244 outs() << "Lazy bind table:\n";
10245 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10246 printMachOLazyBindTable(MachO);
10247 else
10248 WithColor::error()
10249 << "This operation is only currently supported "
10250 "for Mach-O executable files.\n";
10253 void printWeakBindTable(ObjectFile *o) {
10254 outs() << "Weak bind table:\n";
10255 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10256 printMachOWeakBindTable(MachO);
10257 else
10258 WithColor::error()
10259 << "This operation is only currently supported "
10260 "for Mach-O executable files.\n";
10263 void printExportsTrie(const ObjectFile *o) {
10264 outs() << "Exports trie:\n";
10265 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10266 printMachOExportsTrie(MachO);
10267 else
10268 WithColor::error()
10269 << "This operation is only currently supported "
10270 "for Mach-O executable files.\n";
10273 void printRebaseTable(ObjectFile *o) {
10274 outs() << "Rebase table:\n";
10275 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10276 printMachORebaseTable(MachO);
10277 else
10278 WithColor::error()
10279 << "This operation is only currently supported "
10280 "for Mach-O executable files.\n";
10283 void printBindTable(ObjectFile *o) {
10284 outs() << "Bind table:\n";
10285 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10286 printMachOBindTable(MachO);
10287 else
10288 WithColor::error()
10289 << "This operation is only currently supported "
10290 "for Mach-O executable files.\n";
10292 } // namespace llvm