[yaml2obj][obj2yaml] - Do not create a symbol table by default.
[llvm-complete.git] / tools / llvm-objdump / MachODump.cpp
blobe4684d0f1601e79dedd09afa2cb66b248b710eb7
1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the MachO-specific dumper for llvm-objdump.
11 //===----------------------------------------------------------------------===//
13 #include "llvm-objdump.h"
14 #include "llvm-c/Disassembler.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/BinaryFormat/MachO.h"
19 #include "llvm/Config/config.h"
20 #include "llvm/DebugInfo/DIContext.h"
21 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
22 #include "llvm/Demangle/Demangle.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCInstPrinter.h"
28 #include "llvm/MC/MCInstrDesc.h"
29 #include "llvm/MC/MCInstrInfo.h"
30 #include "llvm/MC/MCRegisterInfo.h"
31 #include "llvm/MC/MCSubtargetInfo.h"
32 #include "llvm/Object/MachO.h"
33 #include "llvm/Object/MachOUniversal.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/Endian.h"
38 #include "llvm/Support/Format.h"
39 #include "llvm/Support/FormattedStream.h"
40 #include "llvm/Support/GraphWriter.h"
41 #include "llvm/Support/LEB128.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/TargetRegistry.h"
44 #include "llvm/Support/TargetSelect.h"
45 #include "llvm/Support/ToolOutputFile.h"
46 #include "llvm/Support/WithColor.h"
47 #include "llvm/Support/raw_ostream.h"
48 #include <algorithm>
49 #include <cstring>
50 #include <system_error>
52 #ifdef HAVE_LIBXAR
53 extern "C" {
54 #include <xar/xar.h>
56 #endif
58 using namespace llvm::object;
60 namespace llvm {
62 cl::OptionCategory MachOCat("llvm-objdump MachO Specific Options");
64 extern cl::opt<bool> ArchiveHeaders;
65 extern cl::opt<bool> Disassemble;
66 extern cl::opt<bool> DisassembleAll;
67 extern cl::opt<DIDumpType> DwarfDumpType;
68 extern cl::list<std::string> FilterSections;
69 extern cl::list<std::string> MAttrs;
70 extern cl::opt<std::string> MCPU;
71 extern cl::opt<bool> NoShowRawInsn;
72 extern cl::opt<bool> NoLeadingAddr;
73 extern cl::opt<bool> PrintImmHex;
74 extern cl::opt<bool> PrivateHeaders;
75 extern cl::opt<bool> Relocations;
76 extern cl::opt<bool> SectionHeaders;
77 extern cl::opt<bool> SectionContents;
78 extern cl::opt<bool> SymbolTable;
79 extern cl::opt<std::string> TripleName;
80 extern cl::opt<bool> UnwindInfo;
82 cl::opt<bool>
83 FirstPrivateHeader("private-header",
84 cl::desc("Display only the first format specific file "
85 "header"),
86 cl::cat(MachOCat));
88 cl::opt<bool> ExportsTrie("exports-trie",
89 cl::desc("Display mach-o exported symbols"),
90 cl::cat(MachOCat));
92 cl::opt<bool> Rebase("rebase", cl::desc("Display mach-o rebasing info"),
93 cl::cat(MachOCat));
95 cl::opt<bool> Bind("bind", cl::desc("Display mach-o binding info"),
96 cl::cat(MachOCat));
98 cl::opt<bool> LazyBind("lazy-bind",
99 cl::desc("Display mach-o lazy binding info"),
100 cl::cat(MachOCat));
102 cl::opt<bool> WeakBind("weak-bind",
103 cl::desc("Display mach-o weak binding info"),
104 cl::cat(MachOCat));
106 static cl::opt<bool>
107 UseDbg("g", cl::Grouping,
108 cl::desc("Print line information from debug info if available"),
109 cl::cat(MachOCat));
111 static cl::opt<std::string> DSYMFile("dsym",
112 cl::desc("Use .dSYM file for debug info"),
113 cl::cat(MachOCat));
115 static cl::opt<bool> FullLeadingAddr("full-leading-addr",
116 cl::desc("Print full leading address"),
117 cl::cat(MachOCat));
119 static cl::opt<bool> NoLeadingHeaders("no-leading-headers",
120 cl::desc("Print no leading headers"),
121 cl::cat(MachOCat));
123 cl::opt<bool> UniversalHeaders("universal-headers",
124 cl::desc("Print Mach-O universal headers "
125 "(requires -macho)"),
126 cl::cat(MachOCat));
128 cl::opt<bool>
129 ArchiveMemberOffsets("archive-member-offsets",
130 cl::desc("Print the offset to each archive member for "
131 "Mach-O archives (requires -macho and "
132 "-archive-headers)"),
133 cl::cat(MachOCat));
135 cl::opt<bool> IndirectSymbols("indirect-symbols",
136 cl::desc("Print indirect symbol table for Mach-O "
137 "objects (requires -macho)"),
138 cl::cat(MachOCat));
140 cl::opt<bool>
141 DataInCode("data-in-code",
142 cl::desc("Print the data in code table for Mach-O objects "
143 "(requires -macho)"),
144 cl::cat(MachOCat));
146 cl::opt<bool> LinkOptHints("link-opt-hints",
147 cl::desc("Print the linker optimization hints for "
148 "Mach-O objects (requires -macho)"),
149 cl::cat(MachOCat));
151 cl::opt<bool> InfoPlist("info-plist",
152 cl::desc("Print the info plist section as strings for "
153 "Mach-O objects (requires -macho)"),
154 cl::cat(MachOCat));
156 cl::opt<bool> DylibsUsed("dylibs-used",
157 cl::desc("Print the shared libraries used for linked "
158 "Mach-O files (requires -macho)"),
159 cl::cat(MachOCat));
161 cl::opt<bool>
162 DylibId("dylib-id",
163 cl::desc("Print the shared library's id for the dylib Mach-O "
164 "file (requires -macho)"),
165 cl::cat(MachOCat));
167 cl::opt<bool>
168 NonVerbose("non-verbose",
169 cl::desc("Print the info for Mach-O objects in "
170 "non-verbose or numeric form (requires -macho)"),
171 cl::cat(MachOCat));
173 cl::opt<bool>
174 ObjcMetaData("objc-meta-data",
175 cl::desc("Print the Objective-C runtime meta data for "
176 "Mach-O files (requires -macho)"),
177 cl::cat(MachOCat));
179 cl::opt<std::string> DisSymName(
180 "dis-symname",
181 cl::desc("disassemble just this symbol's instructions (requires -macho)"),
182 cl::cat(MachOCat));
184 static cl::opt<bool> NoSymbolicOperands(
185 "no-symbolic-operands",
186 cl::desc("do not symbolic operands when disassembling (requires -macho)"),
187 cl::cat(MachOCat));
189 static cl::list<std::string>
190 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
191 cl::ZeroOrMore, cl::cat(MachOCat));
193 bool ArchAll = false;
195 static std::string ThumbTripleName;
197 static const Target *GetTarget(const MachOObjectFile *MachOObj,
198 const char **McpuDefault,
199 const Target **ThumbTarget) {
200 // Figure out the target triple.
201 Triple TT(TripleName);
202 if (TripleName.empty()) {
203 TT = MachOObj->getArchTriple(McpuDefault);
204 TripleName = TT.str();
207 if (TT.getArch() == Triple::arm) {
208 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
209 // that support ARM are also capable of Thumb mode.
210 Triple ThumbTriple = TT;
211 std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
212 ThumbTriple.setArchName(ThumbName);
213 ThumbTripleName = ThumbTriple.str();
216 // Get the target specific parser.
217 std::string Error;
218 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
219 if (TheTarget && ThumbTripleName.empty())
220 return TheTarget;
222 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
223 if (*ThumbTarget)
224 return TheTarget;
226 WithColor::error(errs(), "llvm-objdump") << "unable to get target for '";
227 if (!TheTarget)
228 errs() << TripleName;
229 else
230 errs() << ThumbTripleName;
231 errs() << "', see --version and --triple.\n";
232 return nullptr;
235 struct SymbolSorter {
236 bool operator()(const SymbolRef &A, const SymbolRef &B) {
237 Expected<SymbolRef::Type> ATypeOrErr = A.getType();
238 if (!ATypeOrErr)
239 reportError(ATypeOrErr.takeError(), A.getObject()->getFileName());
240 SymbolRef::Type AType = *ATypeOrErr;
241 Expected<SymbolRef::Type> BTypeOrErr = B.getType();
242 if (!BTypeOrErr)
243 reportError(BTypeOrErr.takeError(), B.getObject()->getFileName());
244 SymbolRef::Type BType = *BTypeOrErr;
245 uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue();
246 uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue();
247 return AAddr < BAddr;
251 // Types for the storted data in code table that is built before disassembly
252 // and the predicate function to sort them.
253 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
254 typedef std::vector<DiceTableEntry> DiceTable;
255 typedef DiceTable::iterator dice_table_iterator;
257 #ifdef HAVE_LIBXAR
258 namespace {
259 struct ScopedXarFile {
260 xar_t xar;
261 ScopedXarFile(const char *filename, int32_t flags)
262 : xar(xar_open(filename, flags)) {}
263 ~ScopedXarFile() {
264 if (xar)
265 xar_close(xar);
267 ScopedXarFile(const ScopedXarFile &) = delete;
268 ScopedXarFile &operator=(const ScopedXarFile &) = delete;
269 operator xar_t() { return xar; }
272 struct ScopedXarIter {
273 xar_iter_t iter;
274 ScopedXarIter() : iter(xar_iter_new()) {}
275 ~ScopedXarIter() {
276 if (iter)
277 xar_iter_free(iter);
279 ScopedXarIter(const ScopedXarIter &) = delete;
280 ScopedXarIter &operator=(const ScopedXarIter &) = delete;
281 operator xar_iter_t() { return iter; }
283 } // namespace
284 #endif // defined(HAVE_LIBXAR)
286 // This is used to search for a data in code table entry for the PC being
287 // disassembled. The j parameter has the PC in j.first. A single data in code
288 // table entry can cover many bytes for each of its Kind's. So if the offset,
289 // aka the i.first value, of the data in code table entry plus its Length
290 // covers the PC being searched for this will return true. If not it will
291 // return false.
292 static bool compareDiceTableEntries(const DiceTableEntry &i,
293 const DiceTableEntry &j) {
294 uint16_t Length;
295 i.second.getLength(Length);
297 return j.first >= i.first && j.first < i.first + Length;
300 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
301 unsigned short Kind) {
302 uint32_t Value, Size = 1;
304 switch (Kind) {
305 default:
306 case MachO::DICE_KIND_DATA:
307 if (Length >= 4) {
308 if (!NoShowRawInsn)
309 dumpBytes(makeArrayRef(bytes, 4), outs());
310 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
311 outs() << "\t.long " << Value;
312 Size = 4;
313 } else if (Length >= 2) {
314 if (!NoShowRawInsn)
315 dumpBytes(makeArrayRef(bytes, 2), outs());
316 Value = bytes[1] << 8 | bytes[0];
317 outs() << "\t.short " << Value;
318 Size = 2;
319 } else {
320 if (!NoShowRawInsn)
321 dumpBytes(makeArrayRef(bytes, 2), outs());
322 Value = bytes[0];
323 outs() << "\t.byte " << Value;
324 Size = 1;
326 if (Kind == MachO::DICE_KIND_DATA)
327 outs() << "\t@ KIND_DATA\n";
328 else
329 outs() << "\t@ data in code kind = " << Kind << "\n";
330 break;
331 case MachO::DICE_KIND_JUMP_TABLE8:
332 if (!NoShowRawInsn)
333 dumpBytes(makeArrayRef(bytes, 1), outs());
334 Value = bytes[0];
335 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
336 Size = 1;
337 break;
338 case MachO::DICE_KIND_JUMP_TABLE16:
339 if (!NoShowRawInsn)
340 dumpBytes(makeArrayRef(bytes, 2), outs());
341 Value = bytes[1] << 8 | bytes[0];
342 outs() << "\t.short " << format("%5u", Value & 0xffff)
343 << "\t@ KIND_JUMP_TABLE16\n";
344 Size = 2;
345 break;
346 case MachO::DICE_KIND_JUMP_TABLE32:
347 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
348 if (!NoShowRawInsn)
349 dumpBytes(makeArrayRef(bytes, 4), outs());
350 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
351 outs() << "\t.long " << Value;
352 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
353 outs() << "\t@ KIND_JUMP_TABLE32\n";
354 else
355 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
356 Size = 4;
357 break;
359 return Size;
362 static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
363 std::vector<SectionRef> &Sections,
364 std::vector<SymbolRef> &Symbols,
365 SmallVectorImpl<uint64_t> &FoundFns,
366 uint64_t &BaseSegmentAddress) {
367 const StringRef FileName = MachOObj->getFileName();
368 for (const SymbolRef &Symbol : MachOObj->symbols()) {
369 StringRef SymName = unwrapOrError(Symbol.getName(), FileName);
370 if (!SymName.startswith("ltmp"))
371 Symbols.push_back(Symbol);
374 for (const SectionRef &Section : MachOObj->sections())
375 Sections.push_back(Section);
377 bool BaseSegmentAddressSet = false;
378 for (const auto &Command : MachOObj->load_commands()) {
379 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
380 // We found a function starts segment, parse the addresses for later
381 // consumption.
382 MachO::linkedit_data_command LLC =
383 MachOObj->getLinkeditDataLoadCommand(Command);
385 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
386 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
387 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
388 StringRef SegName = SLC.segname;
389 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
390 BaseSegmentAddressSet = true;
391 BaseSegmentAddress = SLC.vmaddr;
393 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
394 MachO::segment_command_64 SLC = MachOObj->getSegment64LoadCommand(Command);
395 StringRef SegName = SLC.segname;
396 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
397 BaseSegmentAddressSet = true;
398 BaseSegmentAddress = SLC.vmaddr;
404 static bool DumpAndSkipDataInCode(uint64_t PC, const uint8_t *bytes,
405 DiceTable &Dices, uint64_t &InstSize) {
406 // Check the data in code table here to see if this is data not an
407 // instruction to be disassembled.
408 DiceTable Dice;
409 Dice.push_back(std::make_pair(PC, DiceRef()));
410 dice_table_iterator DTI =
411 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
412 compareDiceTableEntries);
413 if (DTI != Dices.end()) {
414 uint16_t Length;
415 DTI->second.getLength(Length);
416 uint16_t Kind;
417 DTI->second.getKind(Kind);
418 InstSize = DumpDataInCode(bytes, Length, Kind);
419 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
420 (PC == (DTI->first + Length - 1)) && (Length & 1))
421 InstSize++;
422 return true;
424 return false;
427 static void printRelocationTargetName(const MachOObjectFile *O,
428 const MachO::any_relocation_info &RE,
429 raw_string_ostream &Fmt) {
430 // Target of a scattered relocation is an address. In the interest of
431 // generating pretty output, scan through the symbol table looking for a
432 // symbol that aligns with that address. If we find one, print it.
433 // Otherwise, we just print the hex address of the target.
434 const StringRef FileName = O->getFileName();
435 if (O->isRelocationScattered(RE)) {
436 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
438 for (const SymbolRef &Symbol : O->symbols()) {
439 uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName);
440 if (Addr != Val)
441 continue;
442 Fmt << unwrapOrError(Symbol.getName(), FileName);
443 return;
446 // If we couldn't find a symbol that this relocation refers to, try
447 // to find a section beginning instead.
448 for (const SectionRef &Section : ToolSectionFilter(*O)) {
449 uint64_t Addr = Section.getAddress();
450 if (Addr != Val)
451 continue;
452 StringRef NameOrErr = unwrapOrError(Section.getName(), O->getFileName());
453 Fmt << NameOrErr;
454 return;
457 Fmt << format("0x%x", Val);
458 return;
461 StringRef S;
462 bool isExtern = O->getPlainRelocationExternal(RE);
463 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
465 if (O->getAnyRelocationType(RE) == MachO::ARM64_RELOC_ADDEND) {
466 Fmt << format("0x%0" PRIx64, Val);
467 return;
470 if (isExtern) {
471 symbol_iterator SI = O->symbol_begin();
472 advance(SI, Val);
473 S = unwrapOrError(SI->getName(), FileName);
474 } else {
475 section_iterator SI = O->section_begin();
476 // Adjust for the fact that sections are 1-indexed.
477 if (Val == 0) {
478 Fmt << "0 (?,?)";
479 return;
481 uint32_t I = Val - 1;
482 while (I != 0 && SI != O->section_end()) {
483 --I;
484 advance(SI, 1);
486 if (SI == O->section_end()) {
487 Fmt << Val << " (?,?)";
488 } else {
489 if (Expected<StringRef> NameOrErr = SI->getName())
490 S = *NameOrErr;
491 else
492 consumeError(NameOrErr.takeError());
496 Fmt << S;
499 Error getMachORelocationValueString(const MachOObjectFile *Obj,
500 const RelocationRef &RelRef,
501 SmallVectorImpl<char> &Result) {
502 DataRefImpl Rel = RelRef.getRawDataRefImpl();
503 MachO::any_relocation_info RE = Obj->getRelocation(Rel);
505 unsigned Arch = Obj->getArch();
507 std::string FmtBuf;
508 raw_string_ostream Fmt(FmtBuf);
509 unsigned Type = Obj->getAnyRelocationType(RE);
510 bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
512 // Determine any addends that should be displayed with the relocation.
513 // These require decoding the relocation type, which is triple-specific.
515 // X86_64 has entirely custom relocation types.
516 if (Arch == Triple::x86_64) {
517 switch (Type) {
518 case MachO::X86_64_RELOC_GOT_LOAD:
519 case MachO::X86_64_RELOC_GOT: {
520 printRelocationTargetName(Obj, RE, Fmt);
521 Fmt << "@GOT";
522 if (IsPCRel)
523 Fmt << "PCREL";
524 break;
526 case MachO::X86_64_RELOC_SUBTRACTOR: {
527 DataRefImpl RelNext = Rel;
528 Obj->moveRelocationNext(RelNext);
529 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
531 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
532 // X86_64_RELOC_UNSIGNED.
533 // NOTE: Scattered relocations don't exist on x86_64.
534 unsigned RType = Obj->getAnyRelocationType(RENext);
535 if (RType != MachO::X86_64_RELOC_UNSIGNED)
536 reportError(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after "
537 "X86_64_RELOC_SUBTRACTOR.");
539 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
540 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
541 printRelocationTargetName(Obj, RENext, Fmt);
542 Fmt << "-";
543 printRelocationTargetName(Obj, RE, Fmt);
544 break;
546 case MachO::X86_64_RELOC_TLV:
547 printRelocationTargetName(Obj, RE, Fmt);
548 Fmt << "@TLV";
549 if (IsPCRel)
550 Fmt << "P";
551 break;
552 case MachO::X86_64_RELOC_SIGNED_1:
553 printRelocationTargetName(Obj, RE, Fmt);
554 Fmt << "-1";
555 break;
556 case MachO::X86_64_RELOC_SIGNED_2:
557 printRelocationTargetName(Obj, RE, Fmt);
558 Fmt << "-2";
559 break;
560 case MachO::X86_64_RELOC_SIGNED_4:
561 printRelocationTargetName(Obj, RE, Fmt);
562 Fmt << "-4";
563 break;
564 default:
565 printRelocationTargetName(Obj, RE, Fmt);
566 break;
568 // X86 and ARM share some relocation types in common.
569 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
570 Arch == Triple::ppc) {
571 // Generic relocation types...
572 switch (Type) {
573 case MachO::GENERIC_RELOC_PAIR: // prints no info
574 return Error::success();
575 case MachO::GENERIC_RELOC_SECTDIFF: {
576 DataRefImpl RelNext = Rel;
577 Obj->moveRelocationNext(RelNext);
578 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
580 // X86 sect diff's must be followed by a relocation of type
581 // GENERIC_RELOC_PAIR.
582 unsigned RType = Obj->getAnyRelocationType(RENext);
584 if (RType != MachO::GENERIC_RELOC_PAIR)
585 reportError(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
586 "GENERIC_RELOC_SECTDIFF.");
588 printRelocationTargetName(Obj, RE, Fmt);
589 Fmt << "-";
590 printRelocationTargetName(Obj, RENext, Fmt);
591 break;
595 if (Arch == Triple::x86 || Arch == Triple::ppc) {
596 switch (Type) {
597 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
598 DataRefImpl RelNext = Rel;
599 Obj->moveRelocationNext(RelNext);
600 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
602 // X86 sect diff's must be followed by a relocation of type
603 // GENERIC_RELOC_PAIR.
604 unsigned RType = Obj->getAnyRelocationType(RENext);
605 if (RType != MachO::GENERIC_RELOC_PAIR)
606 reportError(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
607 "GENERIC_RELOC_LOCAL_SECTDIFF.");
609 printRelocationTargetName(Obj, RE, Fmt);
610 Fmt << "-";
611 printRelocationTargetName(Obj, RENext, Fmt);
612 break;
614 case MachO::GENERIC_RELOC_TLV: {
615 printRelocationTargetName(Obj, RE, Fmt);
616 Fmt << "@TLV";
617 if (IsPCRel)
618 Fmt << "P";
619 break;
621 default:
622 printRelocationTargetName(Obj, RE, Fmt);
624 } else { // ARM-specific relocations
625 switch (Type) {
626 case MachO::ARM_RELOC_HALF:
627 case MachO::ARM_RELOC_HALF_SECTDIFF: {
628 // Half relocations steal a bit from the length field to encode
629 // whether this is an upper16 or a lower16 relocation.
630 bool isUpper = (Obj->getAnyRelocationLength(RE) & 0x1) == 1;
632 if (isUpper)
633 Fmt << ":upper16:(";
634 else
635 Fmt << ":lower16:(";
636 printRelocationTargetName(Obj, RE, Fmt);
638 DataRefImpl RelNext = Rel;
639 Obj->moveRelocationNext(RelNext);
640 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
642 // ARM half relocs must be followed by a relocation of type
643 // ARM_RELOC_PAIR.
644 unsigned RType = Obj->getAnyRelocationType(RENext);
645 if (RType != MachO::ARM_RELOC_PAIR)
646 reportError(Obj->getFileName(), "Expected ARM_RELOC_PAIR after "
647 "ARM_RELOC_HALF");
649 // NOTE: The half of the target virtual address is stashed in the
650 // address field of the secondary relocation, but we can't reverse
651 // engineer the constant offset from it without decoding the movw/movt
652 // instruction to find the other half in its immediate field.
654 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
655 // symbol/section pointer of the follow-on relocation.
656 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
657 Fmt << "-";
658 printRelocationTargetName(Obj, RENext, Fmt);
661 Fmt << ")";
662 break;
664 default: {
665 printRelocationTargetName(Obj, RE, Fmt);
669 } else
670 printRelocationTargetName(Obj, RE, Fmt);
672 Fmt.flush();
673 Result.append(FmtBuf.begin(), FmtBuf.end());
674 return Error::success();
677 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
678 uint32_t n, uint32_t count,
679 uint32_t stride, uint64_t addr) {
680 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
681 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
682 if (n > nindirectsyms)
683 outs() << " (entries start past the end of the indirect symbol "
684 "table) (reserved1 field greater than the table size)";
685 else if (n + count > nindirectsyms)
686 outs() << " (entries extends past the end of the indirect symbol "
687 "table)";
688 outs() << "\n";
689 uint32_t cputype = O->getHeader().cputype;
690 if (cputype & MachO::CPU_ARCH_ABI64)
691 outs() << "address index";
692 else
693 outs() << "address index";
694 if (verbose)
695 outs() << " name\n";
696 else
697 outs() << "\n";
698 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
699 if (cputype & MachO::CPU_ARCH_ABI64)
700 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
701 else
702 outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " ";
703 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
704 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
705 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
706 outs() << "LOCAL\n";
707 continue;
709 if (indirect_symbol ==
710 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
711 outs() << "LOCAL ABSOLUTE\n";
712 continue;
714 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
715 outs() << "ABSOLUTE\n";
716 continue;
718 outs() << format("%5u ", indirect_symbol);
719 if (verbose) {
720 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
721 if (indirect_symbol < Symtab.nsyms) {
722 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
723 SymbolRef Symbol = *Sym;
724 outs() << unwrapOrError(Symbol.getName(), O->getFileName());
725 } else {
726 outs() << "?";
729 outs() << "\n";
733 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
734 for (const auto &Load : O->load_commands()) {
735 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
736 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
737 for (unsigned J = 0; J < Seg.nsects; ++J) {
738 MachO::section_64 Sec = O->getSection64(Load, J);
739 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
740 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
741 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
742 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
743 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
744 section_type == MachO::S_SYMBOL_STUBS) {
745 uint32_t stride;
746 if (section_type == MachO::S_SYMBOL_STUBS)
747 stride = Sec.reserved2;
748 else
749 stride = 8;
750 if (stride == 0) {
751 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
752 << Sec.sectname << ") "
753 << "(size of stubs in reserved2 field is zero)\n";
754 continue;
756 uint32_t count = Sec.size / stride;
757 outs() << "Indirect symbols for (" << Sec.segname << ","
758 << Sec.sectname << ") " << count << " entries";
759 uint32_t n = Sec.reserved1;
760 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
763 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
764 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
765 for (unsigned J = 0; J < Seg.nsects; ++J) {
766 MachO::section Sec = O->getSection(Load, J);
767 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
768 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
769 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
770 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
771 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
772 section_type == MachO::S_SYMBOL_STUBS) {
773 uint32_t stride;
774 if (section_type == MachO::S_SYMBOL_STUBS)
775 stride = Sec.reserved2;
776 else
777 stride = 4;
778 if (stride == 0) {
779 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
780 << Sec.sectname << ") "
781 << "(size of stubs in reserved2 field is zero)\n";
782 continue;
784 uint32_t count = Sec.size / stride;
785 outs() << "Indirect symbols for (" << Sec.segname << ","
786 << Sec.sectname << ") " << count << " entries";
787 uint32_t n = Sec.reserved1;
788 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
795 static void PrintRType(const uint64_t cputype, const unsigned r_type) {
796 static char const *generic_r_types[] = {
797 "VANILLA ", "PAIR ", "SECTDIF ", "PBLAPTR ", "LOCSDIF ", "TLV ",
798 " 6 (?) ", " 7 (?) ", " 8 (?) ", " 9 (?) ", " 10 (?) ", " 11 (?) ",
799 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
801 static char const *x86_64_r_types[] = {
802 "UNSIGND ", "SIGNED ", "BRANCH ", "GOT_LD ", "GOT ", "SUB ",
803 "SIGNED1 ", "SIGNED2 ", "SIGNED4 ", "TLV ", " 10 (?) ", " 11 (?) ",
804 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
806 static char const *arm_r_types[] = {
807 "VANILLA ", "PAIR ", "SECTDIFF", "LOCSDIF ", "PBLAPTR ",
808 "BR24 ", "T_BR22 ", "T_BR32 ", "HALF ", "HALFDIF ",
809 " 10 (?) ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
811 static char const *arm64_r_types[] = {
812 "UNSIGND ", "SUB ", "BR26 ", "PAGE21 ", "PAGOF12 ",
813 "GOTLDP ", "GOTLDPOF", "PTRTGOT ", "TLVLDP ", "TLVLDPOF",
814 "ADDEND ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
817 if (r_type > 0xf){
818 outs() << format("%-7u", r_type) << " ";
819 return;
821 switch (cputype) {
822 case MachO::CPU_TYPE_I386:
823 outs() << generic_r_types[r_type];
824 break;
825 case MachO::CPU_TYPE_X86_64:
826 outs() << x86_64_r_types[r_type];
827 break;
828 case MachO::CPU_TYPE_ARM:
829 outs() << arm_r_types[r_type];
830 break;
831 case MachO::CPU_TYPE_ARM64:
832 case MachO::CPU_TYPE_ARM64_32:
833 outs() << arm64_r_types[r_type];
834 break;
835 default:
836 outs() << format("%-7u ", r_type);
840 static void PrintRLength(const uint64_t cputype, const unsigned r_type,
841 const unsigned r_length, const bool previous_arm_half){
842 if (cputype == MachO::CPU_TYPE_ARM &&
843 (r_type == MachO::ARM_RELOC_HALF ||
844 r_type == MachO::ARM_RELOC_HALF_SECTDIFF || previous_arm_half == true)) {
845 if ((r_length & 0x1) == 0)
846 outs() << "lo/";
847 else
848 outs() << "hi/";
849 if ((r_length & 0x1) == 0)
850 outs() << "arm ";
851 else
852 outs() << "thm ";
853 } else {
854 switch (r_length) {
855 case 0:
856 outs() << "byte ";
857 break;
858 case 1:
859 outs() << "word ";
860 break;
861 case 2:
862 outs() << "long ";
863 break;
864 case 3:
865 if (cputype == MachO::CPU_TYPE_X86_64)
866 outs() << "quad ";
867 else
868 outs() << format("?(%2d) ", r_length);
869 break;
870 default:
871 outs() << format("?(%2d) ", r_length);
876 static void PrintRelocationEntries(const MachOObjectFile *O,
877 const relocation_iterator Begin,
878 const relocation_iterator End,
879 const uint64_t cputype,
880 const bool verbose) {
881 const MachO::symtab_command Symtab = O->getSymtabLoadCommand();
882 bool previous_arm_half = false;
883 bool previous_sectdiff = false;
884 uint32_t sectdiff_r_type = 0;
886 for (relocation_iterator Reloc = Begin; Reloc != End; ++Reloc) {
887 const DataRefImpl Rel = Reloc->getRawDataRefImpl();
888 const MachO::any_relocation_info RE = O->getRelocation(Rel);
889 const unsigned r_type = O->getAnyRelocationType(RE);
890 const bool r_scattered = O->isRelocationScattered(RE);
891 const unsigned r_pcrel = O->getAnyRelocationPCRel(RE);
892 const unsigned r_length = O->getAnyRelocationLength(RE);
893 const unsigned r_address = O->getAnyRelocationAddress(RE);
894 const bool r_extern = (r_scattered ? false :
895 O->getPlainRelocationExternal(RE));
896 const uint32_t r_value = (r_scattered ?
897 O->getScatteredRelocationValue(RE) : 0);
898 const unsigned r_symbolnum = (r_scattered ? 0 :
899 O->getPlainRelocationSymbolNum(RE));
901 if (r_scattered && cputype != MachO::CPU_TYPE_X86_64) {
902 if (verbose) {
903 // scattered: address
904 if ((cputype == MachO::CPU_TYPE_I386 &&
905 r_type == MachO::GENERIC_RELOC_PAIR) ||
906 (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR))
907 outs() << " ";
908 else
909 outs() << format("%08x ", (unsigned int)r_address);
911 // scattered: pcrel
912 if (r_pcrel)
913 outs() << "True ";
914 else
915 outs() << "False ";
917 // scattered: length
918 PrintRLength(cputype, r_type, r_length, previous_arm_half);
920 // scattered: extern & type
921 outs() << "n/a ";
922 PrintRType(cputype, r_type);
924 // scattered: scattered & value
925 outs() << format("True 0x%08x", (unsigned int)r_value);
926 if (previous_sectdiff == false) {
927 if ((cputype == MachO::CPU_TYPE_ARM &&
928 r_type == MachO::ARM_RELOC_PAIR))
929 outs() << format(" half = 0x%04x ", (unsigned int)r_address);
930 } else if (cputype == MachO::CPU_TYPE_ARM &&
931 sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF)
932 outs() << format(" other_half = 0x%04x ", (unsigned int)r_address);
933 if ((cputype == MachO::CPU_TYPE_I386 &&
934 (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
935 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) ||
936 (cputype == MachO::CPU_TYPE_ARM &&
937 (sectdiff_r_type == MachO::ARM_RELOC_SECTDIFF ||
938 sectdiff_r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
939 sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF))) {
940 previous_sectdiff = true;
941 sectdiff_r_type = r_type;
942 } else {
943 previous_sectdiff = false;
944 sectdiff_r_type = 0;
946 if (cputype == MachO::CPU_TYPE_ARM &&
947 (r_type == MachO::ARM_RELOC_HALF ||
948 r_type == MachO::ARM_RELOC_HALF_SECTDIFF))
949 previous_arm_half = true;
950 else
951 previous_arm_half = false;
952 outs() << "\n";
954 else {
955 // scattered: address pcrel length extern type scattered value
956 outs() << format("%08x %1d %-2d n/a %-7d 1 0x%08x\n",
957 (unsigned int)r_address, r_pcrel, r_length, r_type,
958 (unsigned int)r_value);
961 else {
962 if (verbose) {
963 // plain: address
964 if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR)
965 outs() << " ";
966 else
967 outs() << format("%08x ", (unsigned int)r_address);
969 // plain: pcrel
970 if (r_pcrel)
971 outs() << "True ";
972 else
973 outs() << "False ";
975 // plain: length
976 PrintRLength(cputype, r_type, r_length, previous_arm_half);
978 if (r_extern) {
979 // plain: extern & type & scattered
980 outs() << "True ";
981 PrintRType(cputype, r_type);
982 outs() << "False ";
984 // plain: symbolnum/value
985 if (r_symbolnum > Symtab.nsyms)
986 outs() << format("?(%d)\n", r_symbolnum);
987 else {
988 SymbolRef Symbol = *O->getSymbolByIndex(r_symbolnum);
989 Expected<StringRef> SymNameNext = Symbol.getName();
990 const char *name = NULL;
991 if (SymNameNext)
992 name = SymNameNext->data();
993 if (name == NULL)
994 outs() << format("?(%d)\n", r_symbolnum);
995 else
996 outs() << name << "\n";
999 else {
1000 // plain: extern & type & scattered
1001 outs() << "False ";
1002 PrintRType(cputype, r_type);
1003 outs() << "False ";
1005 // plain: symbolnum/value
1006 if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR)
1007 outs() << format("other_half = 0x%04x\n", (unsigned int)r_address);
1008 else if ((cputype == MachO::CPU_TYPE_ARM64 ||
1009 cputype == MachO::CPU_TYPE_ARM64_32) &&
1010 r_type == MachO::ARM64_RELOC_ADDEND)
1011 outs() << format("addend = 0x%06x\n", (unsigned int)r_symbolnum);
1012 else {
1013 outs() << format("%d ", r_symbolnum);
1014 if (r_symbolnum == MachO::R_ABS)
1015 outs() << "R_ABS\n";
1016 else {
1017 // in this case, r_symbolnum is actually a 1-based section number
1018 uint32_t nsects = O->section_end()->getRawDataRefImpl().d.a;
1019 if (r_symbolnum > 0 && r_symbolnum <= nsects) {
1020 object::DataRefImpl DRI;
1021 DRI.d.a = r_symbolnum-1;
1022 StringRef SegName = O->getSectionFinalSegmentName(DRI);
1023 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
1024 outs() << "(" << SegName << "," << *NameOrErr << ")\n";
1025 else
1026 outs() << "(?,?)\n";
1028 else {
1029 outs() << "(?,?)\n";
1034 if (cputype == MachO::CPU_TYPE_ARM &&
1035 (r_type == MachO::ARM_RELOC_HALF ||
1036 r_type == MachO::ARM_RELOC_HALF_SECTDIFF))
1037 previous_arm_half = true;
1038 else
1039 previous_arm_half = false;
1041 else {
1042 // plain: address pcrel length extern type scattered symbolnum/section
1043 outs() << format("%08x %1d %-2d %1d %-7d 0 %d\n",
1044 (unsigned int)r_address, r_pcrel, r_length, r_extern,
1045 r_type, r_symbolnum);
1051 static void PrintRelocations(const MachOObjectFile *O, const bool verbose) {
1052 const uint64_t cputype = O->getHeader().cputype;
1053 const MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
1054 if (Dysymtab.nextrel != 0) {
1055 outs() << "External relocation information " << Dysymtab.nextrel
1056 << " entries";
1057 outs() << "\naddress pcrel length extern type scattered "
1058 "symbolnum/value\n";
1059 PrintRelocationEntries(O, O->extrel_begin(), O->extrel_end(), cputype,
1060 verbose);
1062 if (Dysymtab.nlocrel != 0) {
1063 outs() << format("Local relocation information %u entries",
1064 Dysymtab.nlocrel);
1065 outs() << "\naddress pcrel length extern type scattered "
1066 "symbolnum/value\n";
1067 PrintRelocationEntries(O, O->locrel_begin(), O->locrel_end(), cputype,
1068 verbose);
1070 for (const auto &Load : O->load_commands()) {
1071 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
1072 const MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
1073 for (unsigned J = 0; J < Seg.nsects; ++J) {
1074 const MachO::section_64 Sec = O->getSection64(Load, J);
1075 if (Sec.nreloc != 0) {
1076 DataRefImpl DRI;
1077 DRI.d.a = J;
1078 const StringRef SegName = O->getSectionFinalSegmentName(DRI);
1079 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
1080 outs() << "Relocation information (" << SegName << "," << *NameOrErr
1081 << format(") %u entries", Sec.nreloc);
1082 else
1083 outs() << "Relocation information (" << SegName << ",?) "
1084 << format("%u entries", Sec.nreloc);
1085 outs() << "\naddress pcrel length extern type scattered "
1086 "symbolnum/value\n";
1087 PrintRelocationEntries(O, O->section_rel_begin(DRI),
1088 O->section_rel_end(DRI), cputype, verbose);
1091 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
1092 const MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
1093 for (unsigned J = 0; J < Seg.nsects; ++J) {
1094 const MachO::section Sec = O->getSection(Load, J);
1095 if (Sec.nreloc != 0) {
1096 DataRefImpl DRI;
1097 DRI.d.a = J;
1098 const StringRef SegName = O->getSectionFinalSegmentName(DRI);
1099 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
1100 outs() << "Relocation information (" << SegName << "," << *NameOrErr
1101 << format(") %u entries", Sec.nreloc);
1102 else
1103 outs() << "Relocation information (" << SegName << ",?) "
1104 << format("%u entries", Sec.nreloc);
1105 outs() << "\naddress pcrel length extern type scattered "
1106 "symbolnum/value\n";
1107 PrintRelocationEntries(O, O->section_rel_begin(DRI),
1108 O->section_rel_end(DRI), cputype, verbose);
1115 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
1116 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
1117 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
1118 outs() << "Data in code table (" << nentries << " entries)\n";
1119 outs() << "offset length kind\n";
1120 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
1121 ++DI) {
1122 uint32_t Offset;
1123 DI->getOffset(Offset);
1124 outs() << format("0x%08" PRIx32, Offset) << " ";
1125 uint16_t Length;
1126 DI->getLength(Length);
1127 outs() << format("%6u", Length) << " ";
1128 uint16_t Kind;
1129 DI->getKind(Kind);
1130 if (verbose) {
1131 switch (Kind) {
1132 case MachO::DICE_KIND_DATA:
1133 outs() << "DATA";
1134 break;
1135 case MachO::DICE_KIND_JUMP_TABLE8:
1136 outs() << "JUMP_TABLE8";
1137 break;
1138 case MachO::DICE_KIND_JUMP_TABLE16:
1139 outs() << "JUMP_TABLE16";
1140 break;
1141 case MachO::DICE_KIND_JUMP_TABLE32:
1142 outs() << "JUMP_TABLE32";
1143 break;
1144 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
1145 outs() << "ABS_JUMP_TABLE32";
1146 break;
1147 default:
1148 outs() << format("0x%04" PRIx32, Kind);
1149 break;
1151 } else
1152 outs() << format("0x%04" PRIx32, Kind);
1153 outs() << "\n";
1157 static void PrintLinkOptHints(MachOObjectFile *O) {
1158 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
1159 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
1160 uint32_t nloh = LohLC.datasize;
1161 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
1162 for (uint32_t i = 0; i < nloh;) {
1163 unsigned n;
1164 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
1165 i += n;
1166 outs() << " identifier " << identifier << " ";
1167 if (i >= nloh)
1168 return;
1169 switch (identifier) {
1170 case 1:
1171 outs() << "AdrpAdrp\n";
1172 break;
1173 case 2:
1174 outs() << "AdrpLdr\n";
1175 break;
1176 case 3:
1177 outs() << "AdrpAddLdr\n";
1178 break;
1179 case 4:
1180 outs() << "AdrpLdrGotLdr\n";
1181 break;
1182 case 5:
1183 outs() << "AdrpAddStr\n";
1184 break;
1185 case 6:
1186 outs() << "AdrpLdrGotStr\n";
1187 break;
1188 case 7:
1189 outs() << "AdrpAdd\n";
1190 break;
1191 case 8:
1192 outs() << "AdrpLdrGot\n";
1193 break;
1194 default:
1195 outs() << "Unknown identifier value\n";
1196 break;
1198 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
1199 i += n;
1200 outs() << " narguments " << narguments << "\n";
1201 if (i >= nloh)
1202 return;
1204 for (uint32_t j = 0; j < narguments; j++) {
1205 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
1206 i += n;
1207 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
1208 if (i >= nloh)
1209 return;
1214 static void PrintDylibs(MachOObjectFile *O, bool JustId) {
1215 unsigned Index = 0;
1216 for (const auto &Load : O->load_commands()) {
1217 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
1218 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
1219 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
1220 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
1221 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
1222 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
1223 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
1224 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
1225 if (dl.dylib.name < dl.cmdsize) {
1226 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
1227 if (JustId)
1228 outs() << p << "\n";
1229 else {
1230 outs() << "\t" << p;
1231 outs() << " (compatibility version "
1232 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
1233 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
1234 << (dl.dylib.compatibility_version & 0xff) << ",";
1235 outs() << " current version "
1236 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
1237 << ((dl.dylib.current_version >> 8) & 0xff) << "."
1238 << (dl.dylib.current_version & 0xff);
1239 if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
1240 outs() << ", weak";
1241 if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
1242 outs() << ", reexport";
1243 if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
1244 outs() << ", upward";
1245 if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
1246 outs() << ", lazy";
1247 outs() << ")\n";
1249 } else {
1250 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
1251 if (Load.C.cmd == MachO::LC_ID_DYLIB)
1252 outs() << "LC_ID_DYLIB ";
1253 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
1254 outs() << "LC_LOAD_DYLIB ";
1255 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
1256 outs() << "LC_LOAD_WEAK_DYLIB ";
1257 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
1258 outs() << "LC_LAZY_LOAD_DYLIB ";
1259 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
1260 outs() << "LC_REEXPORT_DYLIB ";
1261 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
1262 outs() << "LC_LOAD_UPWARD_DYLIB ";
1263 else
1264 outs() << "LC_??? ";
1265 outs() << "command " << Index++ << "\n";
1271 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
1273 static void CreateSymbolAddressMap(MachOObjectFile *O,
1274 SymbolAddressMap *AddrMap) {
1275 // Create a map of symbol addresses to symbol names.
1276 const StringRef FileName = O->getFileName();
1277 for (const SymbolRef &Symbol : O->symbols()) {
1278 SymbolRef::Type ST = unwrapOrError(Symbol.getType(), FileName);
1279 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
1280 ST == SymbolRef::ST_Other) {
1281 uint64_t Address = Symbol.getValue();
1282 StringRef SymName = unwrapOrError(Symbol.getName(), FileName);
1283 if (!SymName.startswith(".objc"))
1284 (*AddrMap)[Address] = SymName;
1289 // GuessSymbolName is passed the address of what might be a symbol and a
1290 // pointer to the SymbolAddressMap. It returns the name of a symbol
1291 // with that address or nullptr if no symbol is found with that address.
1292 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
1293 const char *SymbolName = nullptr;
1294 // A DenseMap can't lookup up some values.
1295 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
1296 StringRef name = AddrMap->lookup(value);
1297 if (!name.empty())
1298 SymbolName = name.data();
1300 return SymbolName;
1303 static void DumpCstringChar(const char c) {
1304 char p[2];
1305 p[0] = c;
1306 p[1] = '\0';
1307 outs().write_escaped(p);
1310 static void DumpCstringSection(MachOObjectFile *O, const char *sect,
1311 uint32_t sect_size, uint64_t sect_addr,
1312 bool print_addresses) {
1313 for (uint32_t i = 0; i < sect_size; i++) {
1314 if (print_addresses) {
1315 if (O->is64Bit())
1316 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1317 else
1318 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1320 for (; i < sect_size && sect[i] != '\0'; i++)
1321 DumpCstringChar(sect[i]);
1322 if (i < sect_size && sect[i] == '\0')
1323 outs() << "\n";
1327 static void DumpLiteral4(uint32_t l, float f) {
1328 outs() << format("0x%08" PRIx32, l);
1329 if ((l & 0x7f800000) != 0x7f800000)
1330 outs() << format(" (%.16e)\n", f);
1331 else {
1332 if (l == 0x7f800000)
1333 outs() << " (+Infinity)\n";
1334 else if (l == 0xff800000)
1335 outs() << " (-Infinity)\n";
1336 else if ((l & 0x00400000) == 0x00400000)
1337 outs() << " (non-signaling Not-a-Number)\n";
1338 else
1339 outs() << " (signaling Not-a-Number)\n";
1343 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
1344 uint32_t sect_size, uint64_t sect_addr,
1345 bool print_addresses) {
1346 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
1347 if (print_addresses) {
1348 if (O->is64Bit())
1349 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1350 else
1351 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1353 float f;
1354 memcpy(&f, sect + i, sizeof(float));
1355 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1356 sys::swapByteOrder(f);
1357 uint32_t l;
1358 memcpy(&l, sect + i, sizeof(uint32_t));
1359 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1360 sys::swapByteOrder(l);
1361 DumpLiteral4(l, f);
1365 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
1366 double d) {
1367 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
1368 uint32_t Hi, Lo;
1369 Hi = (O->isLittleEndian()) ? l1 : l0;
1370 Lo = (O->isLittleEndian()) ? l0 : l1;
1372 // Hi is the high word, so this is equivalent to if(isfinite(d))
1373 if ((Hi & 0x7ff00000) != 0x7ff00000)
1374 outs() << format(" (%.16e)\n", d);
1375 else {
1376 if (Hi == 0x7ff00000 && Lo == 0)
1377 outs() << " (+Infinity)\n";
1378 else if (Hi == 0xfff00000 && Lo == 0)
1379 outs() << " (-Infinity)\n";
1380 else if ((Hi & 0x00080000) == 0x00080000)
1381 outs() << " (non-signaling Not-a-Number)\n";
1382 else
1383 outs() << " (signaling Not-a-Number)\n";
1387 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
1388 uint32_t sect_size, uint64_t sect_addr,
1389 bool print_addresses) {
1390 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
1391 if (print_addresses) {
1392 if (O->is64Bit())
1393 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1394 else
1395 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1397 double d;
1398 memcpy(&d, sect + i, sizeof(double));
1399 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1400 sys::swapByteOrder(d);
1401 uint32_t l0, l1;
1402 memcpy(&l0, sect + i, sizeof(uint32_t));
1403 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
1404 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1405 sys::swapByteOrder(l0);
1406 sys::swapByteOrder(l1);
1408 DumpLiteral8(O, l0, l1, d);
1412 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
1413 outs() << format("0x%08" PRIx32, l0) << " ";
1414 outs() << format("0x%08" PRIx32, l1) << " ";
1415 outs() << format("0x%08" PRIx32, l2) << " ";
1416 outs() << format("0x%08" PRIx32, l3) << "\n";
1419 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
1420 uint32_t sect_size, uint64_t sect_addr,
1421 bool print_addresses) {
1422 for (uint32_t i = 0; i < sect_size; i += 16) {
1423 if (print_addresses) {
1424 if (O->is64Bit())
1425 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1426 else
1427 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1429 uint32_t l0, l1, l2, l3;
1430 memcpy(&l0, sect + i, sizeof(uint32_t));
1431 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
1432 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
1433 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
1434 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1435 sys::swapByteOrder(l0);
1436 sys::swapByteOrder(l1);
1437 sys::swapByteOrder(l2);
1438 sys::swapByteOrder(l3);
1440 DumpLiteral16(l0, l1, l2, l3);
1444 static void DumpLiteralPointerSection(MachOObjectFile *O,
1445 const SectionRef &Section,
1446 const char *sect, uint32_t sect_size,
1447 uint64_t sect_addr,
1448 bool print_addresses) {
1449 // Collect the literal sections in this Mach-O file.
1450 std::vector<SectionRef> LiteralSections;
1451 for (const SectionRef &Section : O->sections()) {
1452 DataRefImpl Ref = Section.getRawDataRefImpl();
1453 uint32_t section_type;
1454 if (O->is64Bit()) {
1455 const MachO::section_64 Sec = O->getSection64(Ref);
1456 section_type = Sec.flags & MachO::SECTION_TYPE;
1457 } else {
1458 const MachO::section Sec = O->getSection(Ref);
1459 section_type = Sec.flags & MachO::SECTION_TYPE;
1461 if (section_type == MachO::S_CSTRING_LITERALS ||
1462 section_type == MachO::S_4BYTE_LITERALS ||
1463 section_type == MachO::S_8BYTE_LITERALS ||
1464 section_type == MachO::S_16BYTE_LITERALS)
1465 LiteralSections.push_back(Section);
1468 // Set the size of the literal pointer.
1469 uint32_t lp_size = O->is64Bit() ? 8 : 4;
1471 // Collect the external relocation symbols for the literal pointers.
1472 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
1473 for (const RelocationRef &Reloc : Section.relocations()) {
1474 DataRefImpl Rel;
1475 MachO::any_relocation_info RE;
1476 bool isExtern = false;
1477 Rel = Reloc.getRawDataRefImpl();
1478 RE = O->getRelocation(Rel);
1479 isExtern = O->getPlainRelocationExternal(RE);
1480 if (isExtern) {
1481 uint64_t RelocOffset = Reloc.getOffset();
1482 symbol_iterator RelocSym = Reloc.getSymbol();
1483 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
1486 array_pod_sort(Relocs.begin(), Relocs.end());
1488 // Dump each literal pointer.
1489 for (uint32_t i = 0; i < sect_size; i += lp_size) {
1490 if (print_addresses) {
1491 if (O->is64Bit())
1492 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1493 else
1494 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1496 uint64_t lp;
1497 if (O->is64Bit()) {
1498 memcpy(&lp, sect + i, sizeof(uint64_t));
1499 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1500 sys::swapByteOrder(lp);
1501 } else {
1502 uint32_t li;
1503 memcpy(&li, sect + i, sizeof(uint32_t));
1504 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1505 sys::swapByteOrder(li);
1506 lp = li;
1509 // First look for an external relocation entry for this literal pointer.
1510 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
1511 return P.first == i;
1513 if (Reloc != Relocs.end()) {
1514 symbol_iterator RelocSym = Reloc->second;
1515 StringRef SymName = unwrapOrError(RelocSym->getName(), O->getFileName());
1516 outs() << "external relocation entry for symbol:" << SymName << "\n";
1517 continue;
1520 // For local references see what the section the literal pointer points to.
1521 auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
1522 return lp >= R.getAddress() && lp < R.getAddress() + R.getSize();
1524 if (Sect == LiteralSections.end()) {
1525 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
1526 continue;
1529 uint64_t SectAddress = Sect->getAddress();
1530 uint64_t SectSize = Sect->getSize();
1532 StringRef SectName;
1533 Expected<StringRef> SectNameOrErr = Sect->getName();
1534 if (SectNameOrErr)
1535 SectName = *SectNameOrErr;
1536 else
1537 consumeError(SectNameOrErr.takeError());
1539 DataRefImpl Ref = Sect->getRawDataRefImpl();
1540 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
1541 outs() << SegmentName << ":" << SectName << ":";
1543 uint32_t section_type;
1544 if (O->is64Bit()) {
1545 const MachO::section_64 Sec = O->getSection64(Ref);
1546 section_type = Sec.flags & MachO::SECTION_TYPE;
1547 } else {
1548 const MachO::section Sec = O->getSection(Ref);
1549 section_type = Sec.flags & MachO::SECTION_TYPE;
1552 StringRef BytesStr = unwrapOrError(Sect->getContents(), O->getFileName());
1554 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
1556 switch (section_type) {
1557 case MachO::S_CSTRING_LITERALS:
1558 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
1559 i++) {
1560 DumpCstringChar(Contents[i]);
1562 outs() << "\n";
1563 break;
1564 case MachO::S_4BYTE_LITERALS:
1565 float f;
1566 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
1567 uint32_t l;
1568 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
1569 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1570 sys::swapByteOrder(f);
1571 sys::swapByteOrder(l);
1573 DumpLiteral4(l, f);
1574 break;
1575 case MachO::S_8BYTE_LITERALS: {
1576 double d;
1577 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
1578 uint32_t l0, l1;
1579 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
1580 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
1581 sizeof(uint32_t));
1582 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1583 sys::swapByteOrder(f);
1584 sys::swapByteOrder(l0);
1585 sys::swapByteOrder(l1);
1587 DumpLiteral8(O, l0, l1, d);
1588 break;
1590 case MachO::S_16BYTE_LITERALS: {
1591 uint32_t l0, l1, l2, l3;
1592 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
1593 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
1594 sizeof(uint32_t));
1595 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
1596 sizeof(uint32_t));
1597 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
1598 sizeof(uint32_t));
1599 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1600 sys::swapByteOrder(l0);
1601 sys::swapByteOrder(l1);
1602 sys::swapByteOrder(l2);
1603 sys::swapByteOrder(l3);
1605 DumpLiteral16(l0, l1, l2, l3);
1606 break;
1612 static void DumpInitTermPointerSection(MachOObjectFile *O,
1613 const SectionRef &Section,
1614 const char *sect,
1615 uint32_t sect_size, uint64_t sect_addr,
1616 SymbolAddressMap *AddrMap,
1617 bool verbose) {
1618 uint32_t stride;
1619 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
1621 // Collect the external relocation symbols for the pointers.
1622 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
1623 for (const RelocationRef &Reloc : Section.relocations()) {
1624 DataRefImpl Rel;
1625 MachO::any_relocation_info RE;
1626 bool isExtern = false;
1627 Rel = Reloc.getRawDataRefImpl();
1628 RE = O->getRelocation(Rel);
1629 isExtern = O->getPlainRelocationExternal(RE);
1630 if (isExtern) {
1631 uint64_t RelocOffset = Reloc.getOffset();
1632 symbol_iterator RelocSym = Reloc.getSymbol();
1633 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
1636 array_pod_sort(Relocs.begin(), Relocs.end());
1638 for (uint32_t i = 0; i < sect_size; i += stride) {
1639 const char *SymbolName = nullptr;
1640 uint64_t p;
1641 if (O->is64Bit()) {
1642 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
1643 uint64_t pointer_value;
1644 memcpy(&pointer_value, sect + i, stride);
1645 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1646 sys::swapByteOrder(pointer_value);
1647 outs() << format("0x%016" PRIx64, pointer_value);
1648 p = pointer_value;
1649 } else {
1650 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
1651 uint32_t pointer_value;
1652 memcpy(&pointer_value, sect + i, stride);
1653 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1654 sys::swapByteOrder(pointer_value);
1655 outs() << format("0x%08" PRIx32, pointer_value);
1656 p = pointer_value;
1658 if (verbose) {
1659 // First look for an external relocation entry for this pointer.
1660 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
1661 return P.first == i;
1663 if (Reloc != Relocs.end()) {
1664 symbol_iterator RelocSym = Reloc->second;
1665 outs() << " " << unwrapOrError(RelocSym->getName(), O->getFileName());
1666 } else {
1667 SymbolName = GuessSymbolName(p, AddrMap);
1668 if (SymbolName)
1669 outs() << " " << SymbolName;
1672 outs() << "\n";
1676 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
1677 uint32_t size, uint64_t addr) {
1678 uint32_t cputype = O->getHeader().cputype;
1679 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
1680 uint32_t j;
1681 for (uint32_t i = 0; i < size; i += j, addr += j) {
1682 if (O->is64Bit())
1683 outs() << format("%016" PRIx64, addr) << "\t";
1684 else
1685 outs() << format("%08" PRIx64, addr) << "\t";
1686 for (j = 0; j < 16 && i + j < size; j++) {
1687 uint8_t byte_word = *(sect + i + j);
1688 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1690 outs() << "\n";
1692 } else {
1693 uint32_t j;
1694 for (uint32_t i = 0; i < size; i += j, addr += j) {
1695 if (O->is64Bit())
1696 outs() << format("%016" PRIx64, addr) << "\t";
1697 else
1698 outs() << format("%08" PRIx64, addr) << "\t";
1699 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
1700 j += sizeof(int32_t)) {
1701 if (i + j + sizeof(int32_t) <= size) {
1702 uint32_t long_word;
1703 memcpy(&long_word, sect + i + j, sizeof(int32_t));
1704 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1705 sys::swapByteOrder(long_word);
1706 outs() << format("%08" PRIx32, long_word) << " ";
1707 } else {
1708 for (uint32_t k = 0; i + j + k < size; k++) {
1709 uint8_t byte_word = *(sect + i + j + k);
1710 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1714 outs() << "\n";
1719 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1720 StringRef DisSegName, StringRef DisSectName);
1721 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1722 uint32_t size, uint32_t addr);
1723 #ifdef HAVE_LIBXAR
1724 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
1725 uint32_t size, bool verbose,
1726 bool PrintXarHeader, bool PrintXarFileHeaders,
1727 std::string XarMemberName);
1728 #endif // defined(HAVE_LIBXAR)
1730 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1731 bool verbose) {
1732 SymbolAddressMap AddrMap;
1733 if (verbose)
1734 CreateSymbolAddressMap(O, &AddrMap);
1736 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1737 StringRef DumpSection = FilterSections[i];
1738 std::pair<StringRef, StringRef> DumpSegSectName;
1739 DumpSegSectName = DumpSection.split(',');
1740 StringRef DumpSegName, DumpSectName;
1741 if (!DumpSegSectName.second.empty()) {
1742 DumpSegName = DumpSegSectName.first;
1743 DumpSectName = DumpSegSectName.second;
1744 } else {
1745 DumpSegName = "";
1746 DumpSectName = DumpSegSectName.first;
1748 for (const SectionRef &Section : O->sections()) {
1749 StringRef SectName;
1750 Expected<StringRef> SecNameOrErr = Section.getName();
1751 if (SecNameOrErr)
1752 SectName = *SecNameOrErr;
1753 else
1754 consumeError(SecNameOrErr.takeError());
1756 DataRefImpl Ref = Section.getRawDataRefImpl();
1757 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1758 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1759 (SectName == DumpSectName)) {
1761 uint32_t section_flags;
1762 if (O->is64Bit()) {
1763 const MachO::section_64 Sec = O->getSection64(Ref);
1764 section_flags = Sec.flags;
1766 } else {
1767 const MachO::section Sec = O->getSection(Ref);
1768 section_flags = Sec.flags;
1770 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1772 StringRef BytesStr =
1773 unwrapOrError(Section.getContents(), O->getFileName());
1774 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1775 uint32_t sect_size = BytesStr.size();
1776 uint64_t sect_addr = Section.getAddress();
1778 outs() << "Contents of (" << SegName << "," << SectName
1779 << ") section\n";
1781 if (verbose) {
1782 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1783 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1784 DisassembleMachO(Filename, O, SegName, SectName);
1785 continue;
1787 if (SegName == "__TEXT" && SectName == "__info_plist") {
1788 outs() << sect;
1789 continue;
1791 if (SegName == "__OBJC" && SectName == "__protocol") {
1792 DumpProtocolSection(O, sect, sect_size, sect_addr);
1793 continue;
1795 #ifdef HAVE_LIBXAR
1796 if (SegName == "__LLVM" && SectName == "__bundle") {
1797 DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
1798 ArchiveHeaders, "");
1799 continue;
1801 #endif // defined(HAVE_LIBXAR)
1802 switch (section_type) {
1803 case MachO::S_REGULAR:
1804 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1805 break;
1806 case MachO::S_ZEROFILL:
1807 outs() << "zerofill section and has no contents in the file\n";
1808 break;
1809 case MachO::S_CSTRING_LITERALS:
1810 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1811 break;
1812 case MachO::S_4BYTE_LITERALS:
1813 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1814 break;
1815 case MachO::S_8BYTE_LITERALS:
1816 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1817 break;
1818 case MachO::S_16BYTE_LITERALS:
1819 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1820 break;
1821 case MachO::S_LITERAL_POINTERS:
1822 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1823 !NoLeadingAddr);
1824 break;
1825 case MachO::S_MOD_INIT_FUNC_POINTERS:
1826 case MachO::S_MOD_TERM_FUNC_POINTERS:
1827 DumpInitTermPointerSection(O, Section, sect, sect_size, sect_addr,
1828 &AddrMap, verbose);
1829 break;
1830 default:
1831 outs() << "Unknown section type ("
1832 << format("0x%08" PRIx32, section_type) << ")\n";
1833 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1834 break;
1836 } else {
1837 if (section_type == MachO::S_ZEROFILL)
1838 outs() << "zerofill section and has no contents in the file\n";
1839 else
1840 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1847 static void DumpInfoPlistSectionContents(StringRef Filename,
1848 MachOObjectFile *O) {
1849 for (const SectionRef &Section : O->sections()) {
1850 StringRef SectName;
1851 Expected<StringRef> SecNameOrErr = Section.getName();
1852 if (SecNameOrErr)
1853 SectName = *SecNameOrErr;
1854 else
1855 consumeError(SecNameOrErr.takeError());
1857 DataRefImpl Ref = Section.getRawDataRefImpl();
1858 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1859 if (SegName == "__TEXT" && SectName == "__info_plist") {
1860 if (!NoLeadingHeaders)
1861 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1862 StringRef BytesStr =
1863 unwrapOrError(Section.getContents(), O->getFileName());
1864 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1865 outs() << format("%.*s", BytesStr.size(), sect) << "\n";
1866 return;
1871 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1872 // and if it is and there is a list of architecture flags is specified then
1873 // check to make sure this Mach-O file is one of those architectures or all
1874 // architectures were specified. If not then an error is generated and this
1875 // routine returns false. Else it returns true.
1876 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1877 auto *MachO = dyn_cast<MachOObjectFile>(O);
1879 if (!MachO || ArchAll || ArchFlags.empty())
1880 return true;
1882 MachO::mach_header H;
1883 MachO::mach_header_64 H_64;
1884 Triple T;
1885 const char *McpuDefault, *ArchFlag;
1886 if (MachO->is64Bit()) {
1887 H_64 = MachO->MachOObjectFile::getHeader64();
1888 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
1889 &McpuDefault, &ArchFlag);
1890 } else {
1891 H = MachO->MachOObjectFile::getHeader();
1892 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
1893 &McpuDefault, &ArchFlag);
1895 const std::string ArchFlagName(ArchFlag);
1896 if (none_of(ArchFlags, [&](const std::string &Name) {
1897 return Name == ArchFlagName;
1898 })) {
1899 WithColor::error(errs(), "llvm-objdump")
1900 << Filename << ": no architecture specified.\n";
1901 return false;
1903 return true;
1906 static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1908 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1909 // archive member and or in a slice of a universal file. It prints the
1910 // the file name and header info and then processes it according to the
1911 // command line options.
1912 static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF,
1913 StringRef ArchiveMemberName = StringRef(),
1914 StringRef ArchitectureName = StringRef()) {
1915 // If we are doing some processing here on the Mach-O file print the header
1916 // info. And don't print it otherwise like in the case of printing the
1917 // UniversalHeaders or ArchiveHeaders.
1918 if (Disassemble || Relocations || PrivateHeaders || ExportsTrie || Rebase ||
1919 Bind || SymbolTable || LazyBind || WeakBind || IndirectSymbols ||
1920 DataInCode || LinkOptHints || DylibsUsed || DylibId || ObjcMetaData ||
1921 (!FilterSections.empty())) {
1922 if (!NoLeadingHeaders) {
1923 outs() << Name;
1924 if (!ArchiveMemberName.empty())
1925 outs() << '(' << ArchiveMemberName << ')';
1926 if (!ArchitectureName.empty())
1927 outs() << " (architecture " << ArchitectureName << ")";
1928 outs() << ":\n";
1931 // To use the report_error() form with an ArchiveName and FileName set
1932 // these up based on what is passed for Name and ArchiveMemberName.
1933 StringRef ArchiveName;
1934 StringRef FileName;
1935 if (!ArchiveMemberName.empty()) {
1936 ArchiveName = Name;
1937 FileName = ArchiveMemberName;
1938 } else {
1939 ArchiveName = StringRef();
1940 FileName = Name;
1943 // If we need the symbol table to do the operation then check it here to
1944 // produce a good error message as to where the Mach-O file comes from in
1945 // the error message.
1946 if (Disassemble || IndirectSymbols || !FilterSections.empty() || UnwindInfo)
1947 if (Error Err = MachOOF->checkSymbolTable())
1948 reportError(std::move(Err), FileName, ArchiveName, ArchitectureName);
1950 if (DisassembleAll) {
1951 for (const SectionRef &Section : MachOOF->sections()) {
1952 StringRef SectName;
1953 if (Expected<StringRef> NameOrErr = Section.getName())
1954 SectName = *NameOrErr;
1955 else
1956 consumeError(NameOrErr.takeError());
1958 if (SectName.equals("__text")) {
1959 DataRefImpl Ref = Section.getRawDataRefImpl();
1960 StringRef SegName = MachOOF->getSectionFinalSegmentName(Ref);
1961 DisassembleMachO(FileName, MachOOF, SegName, SectName);
1965 else if (Disassemble) {
1966 if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE &&
1967 MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64)
1968 DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text");
1969 else
1970 DisassembleMachO(FileName, MachOOF, "__TEXT", "__text");
1972 if (IndirectSymbols)
1973 PrintIndirectSymbols(MachOOF, !NonVerbose);
1974 if (DataInCode)
1975 PrintDataInCodeTable(MachOOF, !NonVerbose);
1976 if (LinkOptHints)
1977 PrintLinkOptHints(MachOOF);
1978 if (Relocations)
1979 PrintRelocations(MachOOF, !NonVerbose);
1980 if (SectionHeaders)
1981 printSectionHeaders(MachOOF);
1982 if (SectionContents)
1983 printSectionContents(MachOOF);
1984 if (!FilterSections.empty())
1985 DumpSectionContents(FileName, MachOOF, !NonVerbose);
1986 if (InfoPlist)
1987 DumpInfoPlistSectionContents(FileName, MachOOF);
1988 if (DylibsUsed)
1989 PrintDylibs(MachOOF, false);
1990 if (DylibId)
1991 PrintDylibs(MachOOF, true);
1992 if (SymbolTable)
1993 printSymbolTable(MachOOF, ArchiveName, ArchitectureName);
1994 if (UnwindInfo)
1995 printMachOUnwindInfo(MachOOF);
1996 if (PrivateHeaders) {
1997 printMachOFileHeader(MachOOF);
1998 printMachOLoadCommands(MachOOF);
2000 if (FirstPrivateHeader)
2001 printMachOFileHeader(MachOOF);
2002 if (ObjcMetaData)
2003 printObjcMetaData(MachOOF, !NonVerbose);
2004 if (ExportsTrie)
2005 printExportsTrie(MachOOF);
2006 if (Rebase)
2007 printRebaseTable(MachOOF);
2008 if (Bind)
2009 printBindTable(MachOOF);
2010 if (LazyBind)
2011 printLazyBindTable(MachOOF);
2012 if (WeakBind)
2013 printWeakBindTable(MachOOF);
2015 if (DwarfDumpType != DIDT_Null) {
2016 std::unique_ptr<DIContext> DICtx = DWARFContext::create(*MachOOF);
2017 // Dump the complete DWARF structure.
2018 DIDumpOptions DumpOpts;
2019 DumpOpts.DumpType = DwarfDumpType;
2020 DICtx->dump(outs(), DumpOpts);
2024 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
2025 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
2026 outs() << " cputype (" << cputype << ")\n";
2027 outs() << " cpusubtype (" << cpusubtype << ")\n";
2030 // printCPUType() helps print_fat_headers by printing the cputype and
2031 // pusubtype (symbolically for the one's it knows about).
2032 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
2033 switch (cputype) {
2034 case MachO::CPU_TYPE_I386:
2035 switch (cpusubtype) {
2036 case MachO::CPU_SUBTYPE_I386_ALL:
2037 outs() << " cputype CPU_TYPE_I386\n";
2038 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
2039 break;
2040 default:
2041 printUnknownCPUType(cputype, cpusubtype);
2042 break;
2044 break;
2045 case MachO::CPU_TYPE_X86_64:
2046 switch (cpusubtype) {
2047 case MachO::CPU_SUBTYPE_X86_64_ALL:
2048 outs() << " cputype CPU_TYPE_X86_64\n";
2049 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
2050 break;
2051 case MachO::CPU_SUBTYPE_X86_64_H:
2052 outs() << " cputype CPU_TYPE_X86_64\n";
2053 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
2054 break;
2055 default:
2056 printUnknownCPUType(cputype, cpusubtype);
2057 break;
2059 break;
2060 case MachO::CPU_TYPE_ARM:
2061 switch (cpusubtype) {
2062 case MachO::CPU_SUBTYPE_ARM_ALL:
2063 outs() << " cputype CPU_TYPE_ARM\n";
2064 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
2065 break;
2066 case MachO::CPU_SUBTYPE_ARM_V4T:
2067 outs() << " cputype CPU_TYPE_ARM\n";
2068 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
2069 break;
2070 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
2071 outs() << " cputype CPU_TYPE_ARM\n";
2072 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
2073 break;
2074 case MachO::CPU_SUBTYPE_ARM_XSCALE:
2075 outs() << " cputype CPU_TYPE_ARM\n";
2076 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
2077 break;
2078 case MachO::CPU_SUBTYPE_ARM_V6:
2079 outs() << " cputype CPU_TYPE_ARM\n";
2080 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
2081 break;
2082 case MachO::CPU_SUBTYPE_ARM_V6M:
2083 outs() << " cputype CPU_TYPE_ARM\n";
2084 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
2085 break;
2086 case MachO::CPU_SUBTYPE_ARM_V7:
2087 outs() << " cputype CPU_TYPE_ARM\n";
2088 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
2089 break;
2090 case MachO::CPU_SUBTYPE_ARM_V7EM:
2091 outs() << " cputype CPU_TYPE_ARM\n";
2092 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
2093 break;
2094 case MachO::CPU_SUBTYPE_ARM_V7K:
2095 outs() << " cputype CPU_TYPE_ARM\n";
2096 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
2097 break;
2098 case MachO::CPU_SUBTYPE_ARM_V7M:
2099 outs() << " cputype CPU_TYPE_ARM\n";
2100 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
2101 break;
2102 case MachO::CPU_SUBTYPE_ARM_V7S:
2103 outs() << " cputype CPU_TYPE_ARM\n";
2104 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
2105 break;
2106 default:
2107 printUnknownCPUType(cputype, cpusubtype);
2108 break;
2110 break;
2111 case MachO::CPU_TYPE_ARM64:
2112 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
2113 case MachO::CPU_SUBTYPE_ARM64_ALL:
2114 outs() << " cputype CPU_TYPE_ARM64\n";
2115 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
2116 break;
2117 case MachO::CPU_SUBTYPE_ARM64E:
2118 outs() << " cputype CPU_TYPE_ARM64\n";
2119 outs() << " cpusubtype CPU_SUBTYPE_ARM64E\n";
2120 break;
2121 default:
2122 printUnknownCPUType(cputype, cpusubtype);
2123 break;
2125 break;
2126 case MachO::CPU_TYPE_ARM64_32:
2127 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
2128 case MachO::CPU_SUBTYPE_ARM64_32_V8:
2129 outs() << " cputype CPU_TYPE_ARM64_32\n";
2130 outs() << " cpusubtype CPU_SUBTYPE_ARM64_32_V8\n";
2131 break;
2132 default:
2133 printUnknownCPUType(cputype, cpusubtype);
2134 break;
2136 break;
2137 default:
2138 printUnknownCPUType(cputype, cpusubtype);
2139 break;
2143 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
2144 bool verbose) {
2145 outs() << "Fat headers\n";
2146 if (verbose) {
2147 if (UB->getMagic() == MachO::FAT_MAGIC)
2148 outs() << "fat_magic FAT_MAGIC\n";
2149 else // UB->getMagic() == MachO::FAT_MAGIC_64
2150 outs() << "fat_magic FAT_MAGIC_64\n";
2151 } else
2152 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
2154 uint32_t nfat_arch = UB->getNumberOfObjects();
2155 StringRef Buf = UB->getData();
2156 uint64_t size = Buf.size();
2157 uint64_t big_size = sizeof(struct MachO::fat_header) +
2158 nfat_arch * sizeof(struct MachO::fat_arch);
2159 outs() << "nfat_arch " << UB->getNumberOfObjects();
2160 if (nfat_arch == 0)
2161 outs() << " (malformed, contains zero architecture types)\n";
2162 else if (big_size > size)
2163 outs() << " (malformed, architectures past end of file)\n";
2164 else
2165 outs() << "\n";
2167 for (uint32_t i = 0; i < nfat_arch; ++i) {
2168 MachOUniversalBinary::ObjectForArch OFA(UB, i);
2169 uint32_t cputype = OFA.getCPUType();
2170 uint32_t cpusubtype = OFA.getCPUSubType();
2171 outs() << "architecture ";
2172 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
2173 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
2174 uint32_t other_cputype = other_OFA.getCPUType();
2175 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
2176 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
2177 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
2178 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
2179 outs() << "(illegal duplicate architecture) ";
2180 break;
2183 if (verbose) {
2184 outs() << OFA.getArchFlagName() << "\n";
2185 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
2186 } else {
2187 outs() << i << "\n";
2188 outs() << " cputype " << cputype << "\n";
2189 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
2190 << "\n";
2192 if (verbose &&
2193 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
2194 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
2195 else
2196 outs() << " capabilities "
2197 << format("0x%" PRIx32,
2198 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
2199 outs() << " offset " << OFA.getOffset();
2200 if (OFA.getOffset() > size)
2201 outs() << " (past end of file)";
2202 if (OFA.getOffset() % (1ull << OFA.getAlign()) != 0)
2203 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
2204 outs() << "\n";
2205 outs() << " size " << OFA.getSize();
2206 big_size = OFA.getOffset() + OFA.getSize();
2207 if (big_size > size)
2208 outs() << " (past end of file)";
2209 outs() << "\n";
2210 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
2211 << ")\n";
2215 static void printArchiveChild(StringRef Filename, const Archive::Child &C,
2216 size_t ChildIndex, bool verbose,
2217 bool print_offset,
2218 StringRef ArchitectureName = StringRef()) {
2219 if (print_offset)
2220 outs() << C.getChildOffset() << "\t";
2221 sys::fs::perms Mode =
2222 unwrapOrError(C.getAccessMode(), getFileNameForError(C, ChildIndex),
2223 Filename, ArchitectureName);
2224 if (verbose) {
2225 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
2226 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
2227 outs() << "-";
2228 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
2229 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
2230 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
2231 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
2232 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
2233 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
2234 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
2235 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
2236 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
2237 } else {
2238 outs() << format("0%o ", Mode);
2241 outs() << format("%3d/%-3d %5" PRId64 " ",
2242 unwrapOrError(C.getUID(), getFileNameForError(C, ChildIndex),
2243 Filename, ArchitectureName),
2244 unwrapOrError(C.getGID(), getFileNameForError(C, ChildIndex),
2245 Filename, ArchitectureName),
2246 unwrapOrError(C.getRawSize(),
2247 getFileNameForError(C, ChildIndex), Filename,
2248 ArchitectureName));
2250 StringRef RawLastModified = C.getRawLastModified();
2251 if (verbose) {
2252 unsigned Seconds;
2253 if (RawLastModified.getAsInteger(10, Seconds))
2254 outs() << "(date: \"" << RawLastModified
2255 << "\" contains non-decimal chars) ";
2256 else {
2257 // Since cime(3) returns a 26 character string of the form:
2258 // "Sun Sep 16 01:03:52 1973\n\0"
2259 // just print 24 characters.
2260 time_t t = Seconds;
2261 outs() << format("%.24s ", ctime(&t));
2263 } else {
2264 outs() << RawLastModified << " ";
2267 if (verbose) {
2268 Expected<StringRef> NameOrErr = C.getName();
2269 if (!NameOrErr) {
2270 consumeError(NameOrErr.takeError());
2271 outs() << unwrapOrError(C.getRawName(),
2272 getFileNameForError(C, ChildIndex), Filename,
2273 ArchitectureName)
2274 << "\n";
2275 } else {
2276 StringRef Name = NameOrErr.get();
2277 outs() << Name << "\n";
2279 } else {
2280 outs() << unwrapOrError(C.getRawName(), getFileNameForError(C, ChildIndex),
2281 Filename, ArchitectureName)
2282 << "\n";
2286 static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
2287 bool print_offset,
2288 StringRef ArchitectureName = StringRef()) {
2289 Error Err = Error::success();
2290 size_t I = 0;
2291 for (const auto &C : A->children(Err, false))
2292 printArchiveChild(Filename, C, I++, verbose, print_offset,
2293 ArchitectureName);
2295 if (Err)
2296 reportError(std::move(Err), Filename, "", ArchitectureName);
2299 static bool ValidateArchFlags() {
2300 // Check for -arch all and verifiy the -arch flags are valid.
2301 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
2302 if (ArchFlags[i] == "all") {
2303 ArchAll = true;
2304 } else {
2305 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
2306 WithColor::error(errs(), "llvm-objdump")
2307 << "unknown architecture named '" + ArchFlags[i] +
2308 "'for the -arch option\n";
2309 return false;
2313 return true;
2316 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
2317 // -arch flags selecting just those slices as specified by them and also parses
2318 // archive files. Then for each individual Mach-O file ProcessMachO() is
2319 // called to process the file based on the command line options.
2320 void parseInputMachO(StringRef Filename) {
2321 if (!ValidateArchFlags())
2322 return;
2324 // Attempt to open the binary.
2325 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
2326 if (!BinaryOrErr) {
2327 if (Error E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
2328 reportError(std::move(E), Filename);
2329 else
2330 outs() << Filename << ": is not an object file\n";
2331 return;
2333 Binary &Bin = *BinaryOrErr.get().getBinary();
2335 if (Archive *A = dyn_cast<Archive>(&Bin)) {
2336 outs() << "Archive : " << Filename << "\n";
2337 if (ArchiveHeaders)
2338 printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
2340 Error Err = Error::success();
2341 unsigned I = -1;
2342 for (auto &C : A->children(Err)) {
2343 ++I;
2344 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2345 if (!ChildOrErr) {
2346 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2347 reportError(std::move(E), getFileNameForError(C, I), Filename);
2348 continue;
2350 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
2351 if (!checkMachOAndArchFlags(O, Filename))
2352 return;
2353 ProcessMachO(Filename, O, O->getFileName());
2356 if (Err)
2357 reportError(std::move(Err), Filename);
2358 return;
2360 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
2361 parseInputMachO(UB);
2362 return;
2364 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
2365 if (!checkMachOAndArchFlags(O, Filename))
2366 return;
2367 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O))
2368 ProcessMachO(Filename, MachOOF);
2369 else
2370 WithColor::error(errs(), "llvm-objdump")
2371 << Filename << "': "
2372 << "object is not a Mach-O file type.\n";
2373 return;
2375 llvm_unreachable("Input object can't be invalid at this point");
2378 void parseInputMachO(MachOUniversalBinary *UB) {
2379 if (!ValidateArchFlags())
2380 return;
2382 auto Filename = UB->getFileName();
2384 if (UniversalHeaders)
2385 printMachOUniversalHeaders(UB, !NonVerbose);
2387 // If we have a list of architecture flags specified dump only those.
2388 if (!ArchAll && !ArchFlags.empty()) {
2389 // Look for a slice in the universal binary that matches each ArchFlag.
2390 bool ArchFound;
2391 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
2392 ArchFound = false;
2393 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2394 E = UB->end_objects();
2395 I != E; ++I) {
2396 if (ArchFlags[i] == I->getArchFlagName()) {
2397 ArchFound = true;
2398 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
2399 I->getAsObjectFile();
2400 std::string ArchitectureName = "";
2401 if (ArchFlags.size() > 1)
2402 ArchitectureName = I->getArchFlagName();
2403 if (ObjOrErr) {
2404 ObjectFile &O = *ObjOrErr.get();
2405 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
2406 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
2407 } else if (Error E = isNotObjectErrorInvalidFileType(
2408 ObjOrErr.takeError())) {
2409 reportError(std::move(E), "", Filename, ArchitectureName);
2410 continue;
2411 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
2412 I->getAsArchive()) {
2413 std::unique_ptr<Archive> &A = *AOrErr;
2414 outs() << "Archive : " << Filename;
2415 if (!ArchitectureName.empty())
2416 outs() << " (architecture " << ArchitectureName << ")";
2417 outs() << "\n";
2418 if (ArchiveHeaders)
2419 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2420 ArchiveMemberOffsets, ArchitectureName);
2421 Error Err = Error::success();
2422 unsigned I = -1;
2423 for (auto &C : A->children(Err)) {
2424 ++I;
2425 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2426 if (!ChildOrErr) {
2427 if (Error E =
2428 isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2429 reportError(std::move(E), getFileNameForError(C, I), Filename,
2430 ArchitectureName);
2431 continue;
2433 if (MachOObjectFile *O =
2434 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
2435 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
2437 if (Err)
2438 reportError(std::move(Err), Filename);
2439 } else {
2440 consumeError(AOrErr.takeError());
2441 reportError(Filename,
2442 "Mach-O universal file for architecture " +
2443 StringRef(I->getArchFlagName()) +
2444 " is not a Mach-O file or an archive file");
2448 if (!ArchFound) {
2449 WithColor::error(errs(), "llvm-objdump")
2450 << "file: " + Filename + " does not contain "
2451 << "architecture: " + ArchFlags[i] + "\n";
2452 return;
2455 return;
2457 // No architecture flags were specified so if this contains a slice that
2458 // matches the host architecture dump only that.
2459 if (!ArchAll) {
2460 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2461 E = UB->end_objects();
2462 I != E; ++I) {
2463 if (MachOObjectFile::getHostArch().getArchName() ==
2464 I->getArchFlagName()) {
2465 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
2466 std::string ArchiveName;
2467 ArchiveName.clear();
2468 if (ObjOrErr) {
2469 ObjectFile &O = *ObjOrErr.get();
2470 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
2471 ProcessMachO(Filename, MachOOF);
2472 } else if (Error E =
2473 isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
2474 reportError(std::move(E), Filename);
2475 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
2476 I->getAsArchive()) {
2477 std::unique_ptr<Archive> &A = *AOrErr;
2478 outs() << "Archive : " << Filename << "\n";
2479 if (ArchiveHeaders)
2480 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2481 ArchiveMemberOffsets);
2482 Error Err = Error::success();
2483 unsigned I = -1;
2484 for (auto &C : A->children(Err)) {
2485 ++I;
2486 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2487 if (!ChildOrErr) {
2488 if (Error E =
2489 isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2490 reportError(std::move(E), getFileNameForError(C, I), Filename);
2491 continue;
2493 if (MachOObjectFile *O =
2494 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
2495 ProcessMachO(Filename, O, O->getFileName());
2497 if (Err)
2498 reportError(std::move(Err), Filename);
2499 } else {
2500 consumeError(AOrErr.takeError());
2501 reportError(Filename, "Mach-O universal file for architecture " +
2502 StringRef(I->getArchFlagName()) +
2503 " is not a Mach-O file or an archive file");
2505 return;
2509 // Either all architectures have been specified or none have been specified
2510 // and this does not contain the host architecture so dump all the slices.
2511 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
2512 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2513 E = UB->end_objects();
2514 I != E; ++I) {
2515 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
2516 std::string ArchitectureName = "";
2517 if (moreThanOneArch)
2518 ArchitectureName = I->getArchFlagName();
2519 if (ObjOrErr) {
2520 ObjectFile &Obj = *ObjOrErr.get();
2521 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
2522 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
2523 } else if (Error E =
2524 isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
2525 reportError(std::move(E), Filename, "", ArchitectureName);
2526 } else if (Expected<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
2527 std::unique_ptr<Archive> &A = *AOrErr;
2528 outs() << "Archive : " << Filename;
2529 if (!ArchitectureName.empty())
2530 outs() << " (architecture " << ArchitectureName << ")";
2531 outs() << "\n";
2532 if (ArchiveHeaders)
2533 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2534 ArchiveMemberOffsets, ArchitectureName);
2535 Error Err = Error::success();
2536 unsigned I = -1;
2537 for (auto &C : A->children(Err)) {
2538 ++I;
2539 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2540 if (!ChildOrErr) {
2541 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2542 reportError(std::move(E), getFileNameForError(C, I), Filename,
2543 ArchitectureName);
2544 continue;
2546 if (MachOObjectFile *O =
2547 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
2548 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
2549 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
2550 ArchitectureName);
2553 if (Err)
2554 reportError(std::move(Err), Filename);
2555 } else {
2556 consumeError(AOrErr.takeError());
2557 reportError(Filename, "Mach-O universal file for architecture " +
2558 StringRef(I->getArchFlagName()) +
2559 " is not a Mach-O file or an archive file");
2564 // The block of info used by the Symbolizer call backs.
2565 struct DisassembleInfo {
2566 DisassembleInfo(MachOObjectFile *O, SymbolAddressMap *AddrMap,
2567 std::vector<SectionRef> *Sections, bool verbose)
2568 : verbose(verbose), O(O), AddrMap(AddrMap), Sections(Sections) {}
2569 bool verbose;
2570 MachOObjectFile *O;
2571 SectionRef S;
2572 SymbolAddressMap *AddrMap;
2573 std::vector<SectionRef> *Sections;
2574 const char *class_name = nullptr;
2575 const char *selector_name = nullptr;
2576 std::unique_ptr<char[]> method = nullptr;
2577 char *demangled_name = nullptr;
2578 uint64_t adrp_addr = 0;
2579 uint32_t adrp_inst = 0;
2580 std::unique_ptr<SymbolAddressMap> bindtable;
2581 uint32_t depth = 0;
2584 // SymbolizerGetOpInfo() is the operand information call back function.
2585 // This is called to get the symbolic information for operand(s) of an
2586 // instruction when it is being done. This routine does this from
2587 // the relocation information, symbol table, etc. That block of information
2588 // is a pointer to the struct DisassembleInfo that was passed when the
2589 // disassembler context was created and passed to back to here when
2590 // called back by the disassembler for instruction operands that could have
2591 // relocation information. The address of the instruction containing operand is
2592 // at the Pc parameter. The immediate value the operand has is passed in
2593 // op_info->Value and is at Offset past the start of the instruction and has a
2594 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
2595 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
2596 // names and addends of the symbolic expression to add for the operand. The
2597 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
2598 // information is returned then this function returns 1 else it returns 0.
2599 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
2600 uint64_t Size, int TagType, void *TagBuf) {
2601 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
2602 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
2603 uint64_t value = op_info->Value;
2605 // Make sure all fields returned are zero if we don't set them.
2606 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
2607 op_info->Value = value;
2609 // If the TagType is not the value 1 which it code knows about or if no
2610 // verbose symbolic information is wanted then just return 0, indicating no
2611 // information is being returned.
2612 if (TagType != 1 || !info->verbose)
2613 return 0;
2615 unsigned int Arch = info->O->getArch();
2616 if (Arch == Triple::x86) {
2617 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
2618 return 0;
2619 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2620 // TODO:
2621 // Search the external relocation entries of a fully linked image
2622 // (if any) for an entry that matches this segment offset.
2623 // uint32_t seg_offset = (Pc + Offset);
2624 return 0;
2626 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2627 // for an entry for this section offset.
2628 uint32_t sect_addr = info->S.getAddress();
2629 uint32_t sect_offset = (Pc + Offset) - sect_addr;
2630 bool reloc_found = false;
2631 DataRefImpl Rel;
2632 MachO::any_relocation_info RE;
2633 bool isExtern = false;
2634 SymbolRef Symbol;
2635 bool r_scattered = false;
2636 uint32_t r_value, pair_r_value, r_type;
2637 for (const RelocationRef &Reloc : info->S.relocations()) {
2638 uint64_t RelocOffset = Reloc.getOffset();
2639 if (RelocOffset == sect_offset) {
2640 Rel = Reloc.getRawDataRefImpl();
2641 RE = info->O->getRelocation(Rel);
2642 r_type = info->O->getAnyRelocationType(RE);
2643 r_scattered = info->O->isRelocationScattered(RE);
2644 if (r_scattered) {
2645 r_value = info->O->getScatteredRelocationValue(RE);
2646 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
2647 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
2648 DataRefImpl RelNext = Rel;
2649 info->O->moveRelocationNext(RelNext);
2650 MachO::any_relocation_info RENext;
2651 RENext = info->O->getRelocation(RelNext);
2652 if (info->O->isRelocationScattered(RENext))
2653 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2654 else
2655 return 0;
2657 } else {
2658 isExtern = info->O->getPlainRelocationExternal(RE);
2659 if (isExtern) {
2660 symbol_iterator RelocSym = Reloc.getSymbol();
2661 Symbol = *RelocSym;
2664 reloc_found = true;
2665 break;
2668 if (reloc_found && isExtern) {
2669 op_info->AddSymbol.Present = 1;
2670 op_info->AddSymbol.Name =
2671 unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2672 // For i386 extern relocation entries the value in the instruction is
2673 // the offset from the symbol, and value is already set in op_info->Value.
2674 return 1;
2676 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
2677 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
2678 const char *add = GuessSymbolName(r_value, info->AddrMap);
2679 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2680 uint32_t offset = value - (r_value - pair_r_value);
2681 op_info->AddSymbol.Present = 1;
2682 if (add != nullptr)
2683 op_info->AddSymbol.Name = add;
2684 else
2685 op_info->AddSymbol.Value = r_value;
2686 op_info->SubtractSymbol.Present = 1;
2687 if (sub != nullptr)
2688 op_info->SubtractSymbol.Name = sub;
2689 else
2690 op_info->SubtractSymbol.Value = pair_r_value;
2691 op_info->Value = offset;
2692 return 1;
2694 return 0;
2696 if (Arch == Triple::x86_64) {
2697 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
2698 return 0;
2699 // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
2700 // relocation entries of a linked image (if any) for an entry that matches
2701 // this segment offset.
2702 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2703 uint64_t seg_offset = Pc + Offset;
2704 bool reloc_found = false;
2705 DataRefImpl Rel;
2706 MachO::any_relocation_info RE;
2707 bool isExtern = false;
2708 SymbolRef Symbol;
2709 for (const RelocationRef &Reloc : info->O->external_relocations()) {
2710 uint64_t RelocOffset = Reloc.getOffset();
2711 if (RelocOffset == seg_offset) {
2712 Rel = Reloc.getRawDataRefImpl();
2713 RE = info->O->getRelocation(Rel);
2714 // external relocation entries should always be external.
2715 isExtern = info->O->getPlainRelocationExternal(RE);
2716 if (isExtern) {
2717 symbol_iterator RelocSym = Reloc.getSymbol();
2718 Symbol = *RelocSym;
2720 reloc_found = true;
2721 break;
2724 if (reloc_found && isExtern) {
2725 // The Value passed in will be adjusted by the Pc if the instruction
2726 // adds the Pc. But for x86_64 external relocation entries the Value
2727 // is the offset from the external symbol.
2728 if (info->O->getAnyRelocationPCRel(RE))
2729 op_info->Value -= Pc + Offset + Size;
2730 const char *name =
2731 unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2732 op_info->AddSymbol.Present = 1;
2733 op_info->AddSymbol.Name = name;
2734 return 1;
2736 return 0;
2738 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2739 // for an entry for this section offset.
2740 uint64_t sect_addr = info->S.getAddress();
2741 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2742 bool reloc_found = false;
2743 DataRefImpl Rel;
2744 MachO::any_relocation_info RE;
2745 bool isExtern = false;
2746 SymbolRef Symbol;
2747 for (const RelocationRef &Reloc : info->S.relocations()) {
2748 uint64_t RelocOffset = Reloc.getOffset();
2749 if (RelocOffset == sect_offset) {
2750 Rel = Reloc.getRawDataRefImpl();
2751 RE = info->O->getRelocation(Rel);
2752 // NOTE: Scattered relocations don't exist on x86_64.
2753 isExtern = info->O->getPlainRelocationExternal(RE);
2754 if (isExtern) {
2755 symbol_iterator RelocSym = Reloc.getSymbol();
2756 Symbol = *RelocSym;
2758 reloc_found = true;
2759 break;
2762 if (reloc_found && isExtern) {
2763 // The Value passed in will be adjusted by the Pc if the instruction
2764 // adds the Pc. But for x86_64 external relocation entries the Value
2765 // is the offset from the external symbol.
2766 if (info->O->getAnyRelocationPCRel(RE))
2767 op_info->Value -= Pc + Offset + Size;
2768 const char *name =
2769 unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2770 unsigned Type = info->O->getAnyRelocationType(RE);
2771 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
2772 DataRefImpl RelNext = Rel;
2773 info->O->moveRelocationNext(RelNext);
2774 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2775 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
2776 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
2777 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
2778 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
2779 op_info->SubtractSymbol.Present = 1;
2780 op_info->SubtractSymbol.Name = name;
2781 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
2782 Symbol = *RelocSymNext;
2783 name = unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2786 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2787 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2788 op_info->AddSymbol.Present = 1;
2789 op_info->AddSymbol.Name = name;
2790 return 1;
2792 return 0;
2794 if (Arch == Triple::arm) {
2795 if (Offset != 0 || (Size != 4 && Size != 2))
2796 return 0;
2797 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2798 // TODO:
2799 // Search the external relocation entries of a fully linked image
2800 // (if any) for an entry that matches this segment offset.
2801 // uint32_t seg_offset = (Pc + Offset);
2802 return 0;
2804 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2805 // for an entry for this section offset.
2806 uint32_t sect_addr = info->S.getAddress();
2807 uint32_t sect_offset = (Pc + Offset) - sect_addr;
2808 DataRefImpl Rel;
2809 MachO::any_relocation_info RE;
2810 bool isExtern = false;
2811 SymbolRef Symbol;
2812 bool r_scattered = false;
2813 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
2814 auto Reloc =
2815 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2816 uint64_t RelocOffset = Reloc.getOffset();
2817 return RelocOffset == sect_offset;
2820 if (Reloc == info->S.relocations().end())
2821 return 0;
2823 Rel = Reloc->getRawDataRefImpl();
2824 RE = info->O->getRelocation(Rel);
2825 r_length = info->O->getAnyRelocationLength(RE);
2826 r_scattered = info->O->isRelocationScattered(RE);
2827 if (r_scattered) {
2828 r_value = info->O->getScatteredRelocationValue(RE);
2829 r_type = info->O->getScatteredRelocationType(RE);
2830 } else {
2831 r_type = info->O->getAnyRelocationType(RE);
2832 isExtern = info->O->getPlainRelocationExternal(RE);
2833 if (isExtern) {
2834 symbol_iterator RelocSym = Reloc->getSymbol();
2835 Symbol = *RelocSym;
2838 if (r_type == MachO::ARM_RELOC_HALF ||
2839 r_type == MachO::ARM_RELOC_SECTDIFF ||
2840 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2841 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2842 DataRefImpl RelNext = Rel;
2843 info->O->moveRelocationNext(RelNext);
2844 MachO::any_relocation_info RENext;
2845 RENext = info->O->getRelocation(RelNext);
2846 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
2847 if (info->O->isRelocationScattered(RENext))
2848 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2851 if (isExtern) {
2852 const char *name =
2853 unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2854 op_info->AddSymbol.Present = 1;
2855 op_info->AddSymbol.Name = name;
2856 switch (r_type) {
2857 case MachO::ARM_RELOC_HALF:
2858 if ((r_length & 0x1) == 1) {
2859 op_info->Value = value << 16 | other_half;
2860 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2861 } else {
2862 op_info->Value = other_half << 16 | value;
2863 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2865 break;
2866 default:
2867 break;
2869 return 1;
2871 // If we have a branch that is not an external relocation entry then
2872 // return 0 so the code in tryAddingSymbolicOperand() can use the
2873 // SymbolLookUp call back with the branch target address to look up the
2874 // symbol and possibility add an annotation for a symbol stub.
2875 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2876 r_type == MachO::ARM_THUMB_RELOC_BR22))
2877 return 0;
2879 uint32_t offset = 0;
2880 if (r_type == MachO::ARM_RELOC_HALF ||
2881 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2882 if ((r_length & 0x1) == 1)
2883 value = value << 16 | other_half;
2884 else
2885 value = other_half << 16 | value;
2887 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2888 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2889 offset = value - r_value;
2890 value = r_value;
2893 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2894 if ((r_length & 0x1) == 1)
2895 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2896 else
2897 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2898 const char *add = GuessSymbolName(r_value, info->AddrMap);
2899 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2900 int32_t offset = value - (r_value - pair_r_value);
2901 op_info->AddSymbol.Present = 1;
2902 if (add != nullptr)
2903 op_info->AddSymbol.Name = add;
2904 else
2905 op_info->AddSymbol.Value = r_value;
2906 op_info->SubtractSymbol.Present = 1;
2907 if (sub != nullptr)
2908 op_info->SubtractSymbol.Name = sub;
2909 else
2910 op_info->SubtractSymbol.Value = pair_r_value;
2911 op_info->Value = offset;
2912 return 1;
2915 op_info->AddSymbol.Present = 1;
2916 op_info->Value = offset;
2917 if (r_type == MachO::ARM_RELOC_HALF) {
2918 if ((r_length & 0x1) == 1)
2919 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2920 else
2921 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2923 const char *add = GuessSymbolName(value, info->AddrMap);
2924 if (add != nullptr) {
2925 op_info->AddSymbol.Name = add;
2926 return 1;
2928 op_info->AddSymbol.Value = value;
2929 return 1;
2931 if (Arch == Triple::aarch64) {
2932 if (Offset != 0 || Size != 4)
2933 return 0;
2934 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2935 // TODO:
2936 // Search the external relocation entries of a fully linked image
2937 // (if any) for an entry that matches this segment offset.
2938 // uint64_t seg_offset = (Pc + Offset);
2939 return 0;
2941 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2942 // for an entry for this section offset.
2943 uint64_t sect_addr = info->S.getAddress();
2944 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2945 auto Reloc =
2946 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2947 uint64_t RelocOffset = Reloc.getOffset();
2948 return RelocOffset == sect_offset;
2951 if (Reloc == info->S.relocations().end())
2952 return 0;
2954 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2955 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2956 uint32_t r_type = info->O->getAnyRelocationType(RE);
2957 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2958 DataRefImpl RelNext = Rel;
2959 info->O->moveRelocationNext(RelNext);
2960 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2961 if (value == 0) {
2962 value = info->O->getPlainRelocationSymbolNum(RENext);
2963 op_info->Value = value;
2966 // NOTE: Scattered relocations don't exist on arm64.
2967 if (!info->O->getPlainRelocationExternal(RE))
2968 return 0;
2969 const char *name =
2970 unwrapOrError(Reloc->getSymbol()->getName(), info->O->getFileName())
2971 .data();
2972 op_info->AddSymbol.Present = 1;
2973 op_info->AddSymbol.Name = name;
2975 switch (r_type) {
2976 case MachO::ARM64_RELOC_PAGE21:
2977 /* @page */
2978 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2979 break;
2980 case MachO::ARM64_RELOC_PAGEOFF12:
2981 /* @pageoff */
2982 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2983 break;
2984 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2985 /* @gotpage */
2986 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2987 break;
2988 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2989 /* @gotpageoff */
2990 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2991 break;
2992 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2993 /* @tvlppage is not implemented in llvm-mc */
2994 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2995 break;
2996 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2997 /* @tvlppageoff is not implemented in llvm-mc */
2998 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2999 break;
3000 default:
3001 case MachO::ARM64_RELOC_BRANCH26:
3002 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
3003 break;
3005 return 1;
3007 return 0;
3010 // GuessCstringPointer is passed the address of what might be a pointer to a
3011 // literal string in a cstring section. If that address is in a cstring section
3012 // it returns a pointer to that string. Else it returns nullptr.
3013 static const char *GuessCstringPointer(uint64_t ReferenceValue,
3014 struct DisassembleInfo *info) {
3015 for (const auto &Load : info->O->load_commands()) {
3016 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
3017 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
3018 for (unsigned J = 0; J < Seg.nsects; ++J) {
3019 MachO::section_64 Sec = info->O->getSection64(Load, J);
3020 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
3021 if (section_type == MachO::S_CSTRING_LITERALS &&
3022 ReferenceValue >= Sec.addr &&
3023 ReferenceValue < Sec.addr + Sec.size) {
3024 uint64_t sect_offset = ReferenceValue - Sec.addr;
3025 uint64_t object_offset = Sec.offset + sect_offset;
3026 StringRef MachOContents = info->O->getData();
3027 uint64_t object_size = MachOContents.size();
3028 const char *object_addr = (const char *)MachOContents.data();
3029 if (object_offset < object_size) {
3030 const char *name = object_addr + object_offset;
3031 return name;
3032 } else {
3033 return nullptr;
3037 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
3038 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
3039 for (unsigned J = 0; J < Seg.nsects; ++J) {
3040 MachO::section Sec = info->O->getSection(Load, J);
3041 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
3042 if (section_type == MachO::S_CSTRING_LITERALS &&
3043 ReferenceValue >= Sec.addr &&
3044 ReferenceValue < Sec.addr + Sec.size) {
3045 uint64_t sect_offset = ReferenceValue - Sec.addr;
3046 uint64_t object_offset = Sec.offset + sect_offset;
3047 StringRef MachOContents = info->O->getData();
3048 uint64_t object_size = MachOContents.size();
3049 const char *object_addr = (const char *)MachOContents.data();
3050 if (object_offset < object_size) {
3051 const char *name = object_addr + object_offset;
3052 return name;
3053 } else {
3054 return nullptr;
3060 return nullptr;
3063 // GuessIndirectSymbol returns the name of the indirect symbol for the
3064 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
3065 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
3066 // symbol name being referenced by the stub or pointer.
3067 static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
3068 struct DisassembleInfo *info) {
3069 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
3070 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
3071 for (const auto &Load : info->O->load_commands()) {
3072 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
3073 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
3074 for (unsigned J = 0; J < Seg.nsects; ++J) {
3075 MachO::section_64 Sec = info->O->getSection64(Load, J);
3076 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
3077 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
3078 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
3079 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
3080 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
3081 section_type == MachO::S_SYMBOL_STUBS) &&
3082 ReferenceValue >= Sec.addr &&
3083 ReferenceValue < Sec.addr + Sec.size) {
3084 uint32_t stride;
3085 if (section_type == MachO::S_SYMBOL_STUBS)
3086 stride = Sec.reserved2;
3087 else
3088 stride = 8;
3089 if (stride == 0)
3090 return nullptr;
3091 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
3092 if (index < Dysymtab.nindirectsyms) {
3093 uint32_t indirect_symbol =
3094 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
3095 if (indirect_symbol < Symtab.nsyms) {
3096 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
3097 return unwrapOrError(Sym->getName(), info->O->getFileName())
3098 .data();
3103 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
3104 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
3105 for (unsigned J = 0; J < Seg.nsects; ++J) {
3106 MachO::section Sec = info->O->getSection(Load, J);
3107 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
3108 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
3109 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
3110 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
3111 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
3112 section_type == MachO::S_SYMBOL_STUBS) &&
3113 ReferenceValue >= Sec.addr &&
3114 ReferenceValue < Sec.addr + Sec.size) {
3115 uint32_t stride;
3116 if (section_type == MachO::S_SYMBOL_STUBS)
3117 stride = Sec.reserved2;
3118 else
3119 stride = 4;
3120 if (stride == 0)
3121 return nullptr;
3122 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
3123 if (index < Dysymtab.nindirectsyms) {
3124 uint32_t indirect_symbol =
3125 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
3126 if (indirect_symbol < Symtab.nsyms) {
3127 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
3128 return unwrapOrError(Sym->getName(), info->O->getFileName())
3129 .data();
3136 return nullptr;
3139 // method_reference() is called passing it the ReferenceName that might be
3140 // a reference it to an Objective-C method call. If so then it allocates and
3141 // assembles a method call string with the values last seen and saved in
3142 // the DisassembleInfo's class_name and selector_name fields. This is saved
3143 // into the method field of the info and any previous string is free'ed.
3144 // Then the class_name field in the info is set to nullptr. The method call
3145 // string is set into ReferenceName and ReferenceType is set to
3146 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
3147 // then both ReferenceType and ReferenceName are left unchanged.
3148 static void method_reference(struct DisassembleInfo *info,
3149 uint64_t *ReferenceType,
3150 const char **ReferenceName) {
3151 unsigned int Arch = info->O->getArch();
3152 if (*ReferenceName != nullptr) {
3153 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
3154 if (info->selector_name != nullptr) {
3155 if (info->class_name != nullptr) {
3156 info->method = std::make_unique<char[]>(
3157 5 + strlen(info->class_name) + strlen(info->selector_name));
3158 char *method = info->method.get();
3159 if (method != nullptr) {
3160 strcpy(method, "+[");
3161 strcat(method, info->class_name);
3162 strcat(method, " ");
3163 strcat(method, info->selector_name);
3164 strcat(method, "]");
3165 *ReferenceName = method;
3166 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
3168 } else {
3169 info->method =
3170 std::make_unique<char[]>(9 + strlen(info->selector_name));
3171 char *method = info->method.get();
3172 if (method != nullptr) {
3173 if (Arch == Triple::x86_64)
3174 strcpy(method, "-[%rdi ");
3175 else if (Arch == Triple::aarch64)
3176 strcpy(method, "-[x0 ");
3177 else
3178 strcpy(method, "-[r? ");
3179 strcat(method, info->selector_name);
3180 strcat(method, "]");
3181 *ReferenceName = method;
3182 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
3185 info->class_name = nullptr;
3187 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
3188 if (info->selector_name != nullptr) {
3189 info->method =
3190 std::make_unique<char[]>(17 + strlen(info->selector_name));
3191 char *method = info->method.get();
3192 if (method != nullptr) {
3193 if (Arch == Triple::x86_64)
3194 strcpy(method, "-[[%rdi super] ");
3195 else if (Arch == Triple::aarch64)
3196 strcpy(method, "-[[x0 super] ");
3197 else
3198 strcpy(method, "-[[r? super] ");
3199 strcat(method, info->selector_name);
3200 strcat(method, "]");
3201 *ReferenceName = method;
3202 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
3204 info->class_name = nullptr;
3210 // GuessPointerPointer() is passed the address of what might be a pointer to
3211 // a reference to an Objective-C class, selector, message ref or cfstring.
3212 // If so the value of the pointer is returned and one of the booleans are set
3213 // to true. If not zero is returned and all the booleans are set to false.
3214 static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
3215 struct DisassembleInfo *info,
3216 bool &classref, bool &selref, bool &msgref,
3217 bool &cfstring) {
3218 classref = false;
3219 selref = false;
3220 msgref = false;
3221 cfstring = false;
3222 for (const auto &Load : info->O->load_commands()) {
3223 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
3224 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
3225 for (unsigned J = 0; J < Seg.nsects; ++J) {
3226 MachO::section_64 Sec = info->O->getSection64(Load, J);
3227 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
3228 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
3229 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
3230 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
3231 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
3232 ReferenceValue >= Sec.addr &&
3233 ReferenceValue < Sec.addr + Sec.size) {
3234 uint64_t sect_offset = ReferenceValue - Sec.addr;
3235 uint64_t object_offset = Sec.offset + sect_offset;
3236 StringRef MachOContents = info->O->getData();
3237 uint64_t object_size = MachOContents.size();
3238 const char *object_addr = (const char *)MachOContents.data();
3239 if (object_offset < object_size) {
3240 uint64_t pointer_value;
3241 memcpy(&pointer_value, object_addr + object_offset,
3242 sizeof(uint64_t));
3243 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3244 sys::swapByteOrder(pointer_value);
3245 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
3246 selref = true;
3247 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
3248 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
3249 classref = true;
3250 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
3251 ReferenceValue + 8 < Sec.addr + Sec.size) {
3252 msgref = true;
3253 memcpy(&pointer_value, object_addr + object_offset + 8,
3254 sizeof(uint64_t));
3255 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3256 sys::swapByteOrder(pointer_value);
3257 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
3258 cfstring = true;
3259 return pointer_value;
3260 } else {
3261 return 0;
3266 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
3268 return 0;
3271 // get_pointer_64 returns a pointer to the bytes in the object file at the
3272 // Address from a section in the Mach-O file. And indirectly returns the
3273 // offset into the section, number of bytes left in the section past the offset
3274 // and which section is was being referenced. If the Address is not in a
3275 // section nullptr is returned.
3276 static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
3277 uint32_t &left, SectionRef &S,
3278 DisassembleInfo *info,
3279 bool objc_only = false) {
3280 offset = 0;
3281 left = 0;
3282 S = SectionRef();
3283 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
3284 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
3285 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
3286 if (SectSize == 0)
3287 continue;
3288 if (objc_only) {
3289 StringRef SectName;
3290 Expected<StringRef> SecNameOrErr =
3291 ((*(info->Sections))[SectIdx]).getName();
3292 if (SecNameOrErr)
3293 SectName = *SecNameOrErr;
3294 else
3295 consumeError(SecNameOrErr.takeError());
3297 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
3298 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
3299 if (SegName != "__OBJC" && SectName != "__cstring")
3300 continue;
3302 if (Address >= SectAddress && Address < SectAddress + SectSize) {
3303 S = (*(info->Sections))[SectIdx];
3304 offset = Address - SectAddress;
3305 left = SectSize - offset;
3306 StringRef SectContents = unwrapOrError(
3307 ((*(info->Sections))[SectIdx]).getContents(), info->O->getFileName());
3308 return SectContents.data() + offset;
3311 return nullptr;
3314 static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
3315 uint32_t &left, SectionRef &S,
3316 DisassembleInfo *info,
3317 bool objc_only = false) {
3318 return get_pointer_64(Address, offset, left, S, info, objc_only);
3321 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
3322 // the symbol indirectly through n_value. Based on the relocation information
3323 // for the specified section offset in the specified section reference.
3324 // If no relocation information is found and a non-zero ReferenceValue for the
3325 // symbol is passed, look up that address in the info's AddrMap.
3326 static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
3327 DisassembleInfo *info, uint64_t &n_value,
3328 uint64_t ReferenceValue = 0) {
3329 n_value = 0;
3330 if (!info->verbose)
3331 return nullptr;
3333 // See if there is an external relocation entry at the sect_offset.
3334 bool reloc_found = false;
3335 DataRefImpl Rel;
3336 MachO::any_relocation_info RE;
3337 bool isExtern = false;
3338 SymbolRef Symbol;
3339 for (const RelocationRef &Reloc : S.relocations()) {
3340 uint64_t RelocOffset = Reloc.getOffset();
3341 if (RelocOffset == sect_offset) {
3342 Rel = Reloc.getRawDataRefImpl();
3343 RE = info->O->getRelocation(Rel);
3344 if (info->O->isRelocationScattered(RE))
3345 continue;
3346 isExtern = info->O->getPlainRelocationExternal(RE);
3347 if (isExtern) {
3348 symbol_iterator RelocSym = Reloc.getSymbol();
3349 Symbol = *RelocSym;
3351 reloc_found = true;
3352 break;
3355 // If there is an external relocation entry for a symbol in this section
3356 // at this section_offset then use that symbol's value for the n_value
3357 // and return its name.
3358 const char *SymbolName = nullptr;
3359 if (reloc_found && isExtern) {
3360 n_value = Symbol.getValue();
3361 StringRef Name = unwrapOrError(Symbol.getName(), info->O->getFileName());
3362 if (!Name.empty()) {
3363 SymbolName = Name.data();
3364 return SymbolName;
3368 // TODO: For fully linked images, look through the external relocation
3369 // entries off the dynamic symtab command. For these the r_offset is from the
3370 // start of the first writeable segment in the Mach-O file. So the offset
3371 // to this section from that segment is passed to this routine by the caller,
3372 // as the database_offset. Which is the difference of the section's starting
3373 // address and the first writable segment.
3375 // NOTE: need add passing the database_offset to this routine.
3377 // We did not find an external relocation entry so look up the ReferenceValue
3378 // as an address of a symbol and if found return that symbol's name.
3379 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
3381 return SymbolName;
3384 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
3385 DisassembleInfo *info,
3386 uint32_t ReferenceValue) {
3387 uint64_t n_value64;
3388 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
3391 // These are structs in the Objective-C meta data and read to produce the
3392 // comments for disassembly. While these are part of the ABI they are no
3393 // public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
3394 // .
3396 // The cfstring object in a 64-bit Mach-O file.
3397 struct cfstring64_t {
3398 uint64_t isa; // class64_t * (64-bit pointer)
3399 uint64_t flags; // flag bits
3400 uint64_t characters; // char * (64-bit pointer)
3401 uint64_t length; // number of non-NULL characters in above
3404 // The class object in a 64-bit Mach-O file.
3405 struct class64_t {
3406 uint64_t isa; // class64_t * (64-bit pointer)
3407 uint64_t superclass; // class64_t * (64-bit pointer)
3408 uint64_t cache; // Cache (64-bit pointer)
3409 uint64_t vtable; // IMP * (64-bit pointer)
3410 uint64_t data; // class_ro64_t * (64-bit pointer)
3413 struct class32_t {
3414 uint32_t isa; /* class32_t * (32-bit pointer) */
3415 uint32_t superclass; /* class32_t * (32-bit pointer) */
3416 uint32_t cache; /* Cache (32-bit pointer) */
3417 uint32_t vtable; /* IMP * (32-bit pointer) */
3418 uint32_t data; /* class_ro32_t * (32-bit pointer) */
3421 struct class_ro64_t {
3422 uint32_t flags;
3423 uint32_t instanceStart;
3424 uint32_t instanceSize;
3425 uint32_t reserved;
3426 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
3427 uint64_t name; // const char * (64-bit pointer)
3428 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
3429 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
3430 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
3431 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
3432 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
3435 struct class_ro32_t {
3436 uint32_t flags;
3437 uint32_t instanceStart;
3438 uint32_t instanceSize;
3439 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
3440 uint32_t name; /* const char * (32-bit pointer) */
3441 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
3442 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
3443 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
3444 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
3445 uint32_t baseProperties; /* const struct objc_property_list *
3446 (32-bit pointer) */
3449 /* Values for class_ro{64,32}_t->flags */
3450 #define RO_META (1 << 0)
3451 #define RO_ROOT (1 << 1)
3452 #define RO_HAS_CXX_STRUCTORS (1 << 2)
3454 struct method_list64_t {
3455 uint32_t entsize;
3456 uint32_t count;
3457 /* struct method64_t first; These structures follow inline */
3460 struct method_list32_t {
3461 uint32_t entsize;
3462 uint32_t count;
3463 /* struct method32_t first; These structures follow inline */
3466 struct method64_t {
3467 uint64_t name; /* SEL (64-bit pointer) */
3468 uint64_t types; /* const char * (64-bit pointer) */
3469 uint64_t imp; /* IMP (64-bit pointer) */
3472 struct method32_t {
3473 uint32_t name; /* SEL (32-bit pointer) */
3474 uint32_t types; /* const char * (32-bit pointer) */
3475 uint32_t imp; /* IMP (32-bit pointer) */
3478 struct protocol_list64_t {
3479 uint64_t count; /* uintptr_t (a 64-bit value) */
3480 /* struct protocol64_t * list[0]; These pointers follow inline */
3483 struct protocol_list32_t {
3484 uint32_t count; /* uintptr_t (a 32-bit value) */
3485 /* struct protocol32_t * list[0]; These pointers follow inline */
3488 struct protocol64_t {
3489 uint64_t isa; /* id * (64-bit pointer) */
3490 uint64_t name; /* const char * (64-bit pointer) */
3491 uint64_t protocols; /* struct protocol_list64_t *
3492 (64-bit pointer) */
3493 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
3494 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
3495 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
3496 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
3497 uint64_t instanceProperties; /* struct objc_property_list *
3498 (64-bit pointer) */
3501 struct protocol32_t {
3502 uint32_t isa; /* id * (32-bit pointer) */
3503 uint32_t name; /* const char * (32-bit pointer) */
3504 uint32_t protocols; /* struct protocol_list_t *
3505 (32-bit pointer) */
3506 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
3507 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
3508 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
3509 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
3510 uint32_t instanceProperties; /* struct objc_property_list *
3511 (32-bit pointer) */
3514 struct ivar_list64_t {
3515 uint32_t entsize;
3516 uint32_t count;
3517 /* struct ivar64_t first; These structures follow inline */
3520 struct ivar_list32_t {
3521 uint32_t entsize;
3522 uint32_t count;
3523 /* struct ivar32_t first; These structures follow inline */
3526 struct ivar64_t {
3527 uint64_t offset; /* uintptr_t * (64-bit pointer) */
3528 uint64_t name; /* const char * (64-bit pointer) */
3529 uint64_t type; /* const char * (64-bit pointer) */
3530 uint32_t alignment;
3531 uint32_t size;
3534 struct ivar32_t {
3535 uint32_t offset; /* uintptr_t * (32-bit pointer) */
3536 uint32_t name; /* const char * (32-bit pointer) */
3537 uint32_t type; /* const char * (32-bit pointer) */
3538 uint32_t alignment;
3539 uint32_t size;
3542 struct objc_property_list64 {
3543 uint32_t entsize;
3544 uint32_t count;
3545 /* struct objc_property64 first; These structures follow inline */
3548 struct objc_property_list32 {
3549 uint32_t entsize;
3550 uint32_t count;
3551 /* struct objc_property32 first; These structures follow inline */
3554 struct objc_property64 {
3555 uint64_t name; /* const char * (64-bit pointer) */
3556 uint64_t attributes; /* const char * (64-bit pointer) */
3559 struct objc_property32 {
3560 uint32_t name; /* const char * (32-bit pointer) */
3561 uint32_t attributes; /* const char * (32-bit pointer) */
3564 struct category64_t {
3565 uint64_t name; /* const char * (64-bit pointer) */
3566 uint64_t cls; /* struct class_t * (64-bit pointer) */
3567 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
3568 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
3569 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
3570 uint64_t instanceProperties; /* struct objc_property_list *
3571 (64-bit pointer) */
3574 struct category32_t {
3575 uint32_t name; /* const char * (32-bit pointer) */
3576 uint32_t cls; /* struct class_t * (32-bit pointer) */
3577 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
3578 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
3579 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
3580 uint32_t instanceProperties; /* struct objc_property_list *
3581 (32-bit pointer) */
3584 struct objc_image_info64 {
3585 uint32_t version;
3586 uint32_t flags;
3588 struct objc_image_info32 {
3589 uint32_t version;
3590 uint32_t flags;
3592 struct imageInfo_t {
3593 uint32_t version;
3594 uint32_t flags;
3596 /* masks for objc_image_info.flags */
3597 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
3598 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
3599 #define OBJC_IMAGE_IS_SIMULATED (1 << 5)
3600 #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES (1 << 6)
3602 struct message_ref64 {
3603 uint64_t imp; /* IMP (64-bit pointer) */
3604 uint64_t sel; /* SEL (64-bit pointer) */
3607 struct message_ref32 {
3608 uint32_t imp; /* IMP (32-bit pointer) */
3609 uint32_t sel; /* SEL (32-bit pointer) */
3612 // Objective-C 1 (32-bit only) meta data structs.
3614 struct objc_module_t {
3615 uint32_t version;
3616 uint32_t size;
3617 uint32_t name; /* char * (32-bit pointer) */
3618 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
3621 struct objc_symtab_t {
3622 uint32_t sel_ref_cnt;
3623 uint32_t refs; /* SEL * (32-bit pointer) */
3624 uint16_t cls_def_cnt;
3625 uint16_t cat_def_cnt;
3626 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
3629 struct objc_class_t {
3630 uint32_t isa; /* struct objc_class * (32-bit pointer) */
3631 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
3632 uint32_t name; /* const char * (32-bit pointer) */
3633 int32_t version;
3634 int32_t info;
3635 int32_t instance_size;
3636 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
3637 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
3638 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
3639 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
3642 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
3643 // class is not a metaclass
3644 #define CLS_CLASS 0x1
3645 // class is a metaclass
3646 #define CLS_META 0x2
3648 struct objc_category_t {
3649 uint32_t category_name; /* char * (32-bit pointer) */
3650 uint32_t class_name; /* char * (32-bit pointer) */
3651 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
3652 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
3653 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
3656 struct objc_ivar_t {
3657 uint32_t ivar_name; /* char * (32-bit pointer) */
3658 uint32_t ivar_type; /* char * (32-bit pointer) */
3659 int32_t ivar_offset;
3662 struct objc_ivar_list_t {
3663 int32_t ivar_count;
3664 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
3667 struct objc_method_list_t {
3668 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
3669 int32_t method_count;
3670 // struct objc_method_t method_list[1]; /* variable length structure */
3673 struct objc_method_t {
3674 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
3675 uint32_t method_types; /* char * (32-bit pointer) */
3676 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
3677 (32-bit pointer) */
3680 struct objc_protocol_list_t {
3681 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
3682 int32_t count;
3683 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
3684 // (32-bit pointer) */
3687 struct objc_protocol_t {
3688 uint32_t isa; /* struct objc_class * (32-bit pointer) */
3689 uint32_t protocol_name; /* char * (32-bit pointer) */
3690 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
3691 uint32_t instance_methods; /* struct objc_method_description_list *
3692 (32-bit pointer) */
3693 uint32_t class_methods; /* struct objc_method_description_list *
3694 (32-bit pointer) */
3697 struct objc_method_description_list_t {
3698 int32_t count;
3699 // struct objc_method_description_t list[1];
3702 struct objc_method_description_t {
3703 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
3704 uint32_t types; /* char * (32-bit pointer) */
3707 inline void swapStruct(struct cfstring64_t &cfs) {
3708 sys::swapByteOrder(cfs.isa);
3709 sys::swapByteOrder(cfs.flags);
3710 sys::swapByteOrder(cfs.characters);
3711 sys::swapByteOrder(cfs.length);
3714 inline void swapStruct(struct class64_t &c) {
3715 sys::swapByteOrder(c.isa);
3716 sys::swapByteOrder(c.superclass);
3717 sys::swapByteOrder(c.cache);
3718 sys::swapByteOrder(c.vtable);
3719 sys::swapByteOrder(c.data);
3722 inline void swapStruct(struct class32_t &c) {
3723 sys::swapByteOrder(c.isa);
3724 sys::swapByteOrder(c.superclass);
3725 sys::swapByteOrder(c.cache);
3726 sys::swapByteOrder(c.vtable);
3727 sys::swapByteOrder(c.data);
3730 inline void swapStruct(struct class_ro64_t &cro) {
3731 sys::swapByteOrder(cro.flags);
3732 sys::swapByteOrder(cro.instanceStart);
3733 sys::swapByteOrder(cro.instanceSize);
3734 sys::swapByteOrder(cro.reserved);
3735 sys::swapByteOrder(cro.ivarLayout);
3736 sys::swapByteOrder(cro.name);
3737 sys::swapByteOrder(cro.baseMethods);
3738 sys::swapByteOrder(cro.baseProtocols);
3739 sys::swapByteOrder(cro.ivars);
3740 sys::swapByteOrder(cro.weakIvarLayout);
3741 sys::swapByteOrder(cro.baseProperties);
3744 inline void swapStruct(struct class_ro32_t &cro) {
3745 sys::swapByteOrder(cro.flags);
3746 sys::swapByteOrder(cro.instanceStart);
3747 sys::swapByteOrder(cro.instanceSize);
3748 sys::swapByteOrder(cro.ivarLayout);
3749 sys::swapByteOrder(cro.name);
3750 sys::swapByteOrder(cro.baseMethods);
3751 sys::swapByteOrder(cro.baseProtocols);
3752 sys::swapByteOrder(cro.ivars);
3753 sys::swapByteOrder(cro.weakIvarLayout);
3754 sys::swapByteOrder(cro.baseProperties);
3757 inline void swapStruct(struct method_list64_t &ml) {
3758 sys::swapByteOrder(ml.entsize);
3759 sys::swapByteOrder(ml.count);
3762 inline void swapStruct(struct method_list32_t &ml) {
3763 sys::swapByteOrder(ml.entsize);
3764 sys::swapByteOrder(ml.count);
3767 inline void swapStruct(struct method64_t &m) {
3768 sys::swapByteOrder(m.name);
3769 sys::swapByteOrder(m.types);
3770 sys::swapByteOrder(m.imp);
3773 inline void swapStruct(struct method32_t &m) {
3774 sys::swapByteOrder(m.name);
3775 sys::swapByteOrder(m.types);
3776 sys::swapByteOrder(m.imp);
3779 inline void swapStruct(struct protocol_list64_t &pl) {
3780 sys::swapByteOrder(pl.count);
3783 inline void swapStruct(struct protocol_list32_t &pl) {
3784 sys::swapByteOrder(pl.count);
3787 inline void swapStruct(struct protocol64_t &p) {
3788 sys::swapByteOrder(p.isa);
3789 sys::swapByteOrder(p.name);
3790 sys::swapByteOrder(p.protocols);
3791 sys::swapByteOrder(p.instanceMethods);
3792 sys::swapByteOrder(p.classMethods);
3793 sys::swapByteOrder(p.optionalInstanceMethods);
3794 sys::swapByteOrder(p.optionalClassMethods);
3795 sys::swapByteOrder(p.instanceProperties);
3798 inline void swapStruct(struct protocol32_t &p) {
3799 sys::swapByteOrder(p.isa);
3800 sys::swapByteOrder(p.name);
3801 sys::swapByteOrder(p.protocols);
3802 sys::swapByteOrder(p.instanceMethods);
3803 sys::swapByteOrder(p.classMethods);
3804 sys::swapByteOrder(p.optionalInstanceMethods);
3805 sys::swapByteOrder(p.optionalClassMethods);
3806 sys::swapByteOrder(p.instanceProperties);
3809 inline void swapStruct(struct ivar_list64_t &il) {
3810 sys::swapByteOrder(il.entsize);
3811 sys::swapByteOrder(il.count);
3814 inline void swapStruct(struct ivar_list32_t &il) {
3815 sys::swapByteOrder(il.entsize);
3816 sys::swapByteOrder(il.count);
3819 inline void swapStruct(struct ivar64_t &i) {
3820 sys::swapByteOrder(i.offset);
3821 sys::swapByteOrder(i.name);
3822 sys::swapByteOrder(i.type);
3823 sys::swapByteOrder(i.alignment);
3824 sys::swapByteOrder(i.size);
3827 inline void swapStruct(struct ivar32_t &i) {
3828 sys::swapByteOrder(i.offset);
3829 sys::swapByteOrder(i.name);
3830 sys::swapByteOrder(i.type);
3831 sys::swapByteOrder(i.alignment);
3832 sys::swapByteOrder(i.size);
3835 inline void swapStruct(struct objc_property_list64 &pl) {
3836 sys::swapByteOrder(pl.entsize);
3837 sys::swapByteOrder(pl.count);
3840 inline void swapStruct(struct objc_property_list32 &pl) {
3841 sys::swapByteOrder(pl.entsize);
3842 sys::swapByteOrder(pl.count);
3845 inline void swapStruct(struct objc_property64 &op) {
3846 sys::swapByteOrder(op.name);
3847 sys::swapByteOrder(op.attributes);
3850 inline void swapStruct(struct objc_property32 &op) {
3851 sys::swapByteOrder(op.name);
3852 sys::swapByteOrder(op.attributes);
3855 inline void swapStruct(struct category64_t &c) {
3856 sys::swapByteOrder(c.name);
3857 sys::swapByteOrder(c.cls);
3858 sys::swapByteOrder(c.instanceMethods);
3859 sys::swapByteOrder(c.classMethods);
3860 sys::swapByteOrder(c.protocols);
3861 sys::swapByteOrder(c.instanceProperties);
3864 inline void swapStruct(struct category32_t &c) {
3865 sys::swapByteOrder(c.name);
3866 sys::swapByteOrder(c.cls);
3867 sys::swapByteOrder(c.instanceMethods);
3868 sys::swapByteOrder(c.classMethods);
3869 sys::swapByteOrder(c.protocols);
3870 sys::swapByteOrder(c.instanceProperties);
3873 inline void swapStruct(struct objc_image_info64 &o) {
3874 sys::swapByteOrder(o.version);
3875 sys::swapByteOrder(o.flags);
3878 inline void swapStruct(struct objc_image_info32 &o) {
3879 sys::swapByteOrder(o.version);
3880 sys::swapByteOrder(o.flags);
3883 inline void swapStruct(struct imageInfo_t &o) {
3884 sys::swapByteOrder(o.version);
3885 sys::swapByteOrder(o.flags);
3888 inline void swapStruct(struct message_ref64 &mr) {
3889 sys::swapByteOrder(mr.imp);
3890 sys::swapByteOrder(mr.sel);
3893 inline void swapStruct(struct message_ref32 &mr) {
3894 sys::swapByteOrder(mr.imp);
3895 sys::swapByteOrder(mr.sel);
3898 inline void swapStruct(struct objc_module_t &module) {
3899 sys::swapByteOrder(module.version);
3900 sys::swapByteOrder(module.size);
3901 sys::swapByteOrder(module.name);
3902 sys::swapByteOrder(module.symtab);
3905 inline void swapStruct(struct objc_symtab_t &symtab) {
3906 sys::swapByteOrder(symtab.sel_ref_cnt);
3907 sys::swapByteOrder(symtab.refs);
3908 sys::swapByteOrder(symtab.cls_def_cnt);
3909 sys::swapByteOrder(symtab.cat_def_cnt);
3912 inline void swapStruct(struct objc_class_t &objc_class) {
3913 sys::swapByteOrder(objc_class.isa);
3914 sys::swapByteOrder(objc_class.super_class);
3915 sys::swapByteOrder(objc_class.name);
3916 sys::swapByteOrder(objc_class.version);
3917 sys::swapByteOrder(objc_class.info);
3918 sys::swapByteOrder(objc_class.instance_size);
3919 sys::swapByteOrder(objc_class.ivars);
3920 sys::swapByteOrder(objc_class.methodLists);
3921 sys::swapByteOrder(objc_class.cache);
3922 sys::swapByteOrder(objc_class.protocols);
3925 inline void swapStruct(struct objc_category_t &objc_category) {
3926 sys::swapByteOrder(objc_category.category_name);
3927 sys::swapByteOrder(objc_category.class_name);
3928 sys::swapByteOrder(objc_category.instance_methods);
3929 sys::swapByteOrder(objc_category.class_methods);
3930 sys::swapByteOrder(objc_category.protocols);
3933 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3934 sys::swapByteOrder(objc_ivar_list.ivar_count);
3937 inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3938 sys::swapByteOrder(objc_ivar.ivar_name);
3939 sys::swapByteOrder(objc_ivar.ivar_type);
3940 sys::swapByteOrder(objc_ivar.ivar_offset);
3943 inline void swapStruct(struct objc_method_list_t &method_list) {
3944 sys::swapByteOrder(method_list.obsolete);
3945 sys::swapByteOrder(method_list.method_count);
3948 inline void swapStruct(struct objc_method_t &method) {
3949 sys::swapByteOrder(method.method_name);
3950 sys::swapByteOrder(method.method_types);
3951 sys::swapByteOrder(method.method_imp);
3954 inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3955 sys::swapByteOrder(protocol_list.next);
3956 sys::swapByteOrder(protocol_list.count);
3959 inline void swapStruct(struct objc_protocol_t &protocol) {
3960 sys::swapByteOrder(protocol.isa);
3961 sys::swapByteOrder(protocol.protocol_name);
3962 sys::swapByteOrder(protocol.protocol_list);
3963 sys::swapByteOrder(protocol.instance_methods);
3964 sys::swapByteOrder(protocol.class_methods);
3967 inline void swapStruct(struct objc_method_description_list_t &mdl) {
3968 sys::swapByteOrder(mdl.count);
3971 inline void swapStruct(struct objc_method_description_t &md) {
3972 sys::swapByteOrder(md.name);
3973 sys::swapByteOrder(md.types);
3976 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3977 struct DisassembleInfo *info);
3979 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3980 // to an Objective-C class and returns the class name. It is also passed the
3981 // address of the pointer, so when the pointer is zero as it can be in an .o
3982 // file, that is used to look for an external relocation entry with a symbol
3983 // name.
3984 static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3985 uint64_t ReferenceValue,
3986 struct DisassembleInfo *info) {
3987 const char *r;
3988 uint32_t offset, left;
3989 SectionRef S;
3991 // The pointer_value can be 0 in an object file and have a relocation
3992 // entry for the class symbol at the ReferenceValue (the address of the
3993 // pointer).
3994 if (pointer_value == 0) {
3995 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3996 if (r == nullptr || left < sizeof(uint64_t))
3997 return nullptr;
3998 uint64_t n_value;
3999 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
4000 if (symbol_name == nullptr)
4001 return nullptr;
4002 const char *class_name = strrchr(symbol_name, '$');
4003 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
4004 return class_name + 2;
4005 else
4006 return nullptr;
4009 // The case were the pointer_value is non-zero and points to a class defined
4010 // in this Mach-O file.
4011 r = get_pointer_64(pointer_value, offset, left, S, info);
4012 if (r == nullptr || left < sizeof(struct class64_t))
4013 return nullptr;
4014 struct class64_t c;
4015 memcpy(&c, r, sizeof(struct class64_t));
4016 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4017 swapStruct(c);
4018 if (c.data == 0)
4019 return nullptr;
4020 r = get_pointer_64(c.data, offset, left, S, info);
4021 if (r == nullptr || left < sizeof(struct class_ro64_t))
4022 return nullptr;
4023 struct class_ro64_t cro;
4024 memcpy(&cro, r, sizeof(struct class_ro64_t));
4025 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4026 swapStruct(cro);
4027 if (cro.name == 0)
4028 return nullptr;
4029 const char *name = get_pointer_64(cro.name, offset, left, S, info);
4030 return name;
4033 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
4034 // pointer to a cfstring and returns its name or nullptr.
4035 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
4036 struct DisassembleInfo *info) {
4037 const char *r, *name;
4038 uint32_t offset, left;
4039 SectionRef S;
4040 struct cfstring64_t cfs;
4041 uint64_t cfs_characters;
4043 r = get_pointer_64(ReferenceValue, offset, left, S, info);
4044 if (r == nullptr || left < sizeof(struct cfstring64_t))
4045 return nullptr;
4046 memcpy(&cfs, r, sizeof(struct cfstring64_t));
4047 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4048 swapStruct(cfs);
4049 if (cfs.characters == 0) {
4050 uint64_t n_value;
4051 const char *symbol_name = get_symbol_64(
4052 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
4053 if (symbol_name == nullptr)
4054 return nullptr;
4055 cfs_characters = n_value;
4056 } else
4057 cfs_characters = cfs.characters;
4058 name = get_pointer_64(cfs_characters, offset, left, S, info);
4060 return name;
4063 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
4064 // of a pointer to an Objective-C selector reference when the pointer value is
4065 // zero as in a .o file and is likely to have a external relocation entry with
4066 // who's symbol's n_value is the real pointer to the selector name. If that is
4067 // the case the real pointer to the selector name is returned else 0 is
4068 // returned
4069 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
4070 struct DisassembleInfo *info) {
4071 uint32_t offset, left;
4072 SectionRef S;
4074 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
4075 if (r == nullptr || left < sizeof(uint64_t))
4076 return 0;
4077 uint64_t n_value;
4078 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
4079 if (symbol_name == nullptr)
4080 return 0;
4081 return n_value;
4084 static const SectionRef get_section(MachOObjectFile *O, const char *segname,
4085 const char *sectname) {
4086 for (const SectionRef &Section : O->sections()) {
4087 StringRef SectName;
4088 Expected<StringRef> SecNameOrErr = Section.getName();
4089 if (SecNameOrErr)
4090 SectName = *SecNameOrErr;
4091 else
4092 consumeError(SecNameOrErr.takeError());
4094 DataRefImpl Ref = Section.getRawDataRefImpl();
4095 StringRef SegName = O->getSectionFinalSegmentName(Ref);
4096 if (SegName == segname && SectName == sectname)
4097 return Section;
4099 return SectionRef();
4102 static void
4103 walk_pointer_list_64(const char *listname, const SectionRef S,
4104 MachOObjectFile *O, struct DisassembleInfo *info,
4105 void (*func)(uint64_t, struct DisassembleInfo *info)) {
4106 if (S == SectionRef())
4107 return;
4109 StringRef SectName;
4110 Expected<StringRef> SecNameOrErr = S.getName();
4111 if (SecNameOrErr)
4112 SectName = *SecNameOrErr;
4113 else
4114 consumeError(SecNameOrErr.takeError());
4116 DataRefImpl Ref = S.getRawDataRefImpl();
4117 StringRef SegName = O->getSectionFinalSegmentName(Ref);
4118 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4120 StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName());
4121 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
4123 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
4124 uint32_t left = S.getSize() - i;
4125 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
4126 uint64_t p = 0;
4127 memcpy(&p, Contents + i, size);
4128 if (i + sizeof(uint64_t) > S.getSize())
4129 outs() << listname << " list pointer extends past end of (" << SegName
4130 << "," << SectName << ") section\n";
4131 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
4133 if (O->isLittleEndian() != sys::IsLittleEndianHost)
4134 sys::swapByteOrder(p);
4136 uint64_t n_value = 0;
4137 const char *name = get_symbol_64(i, S, info, n_value, p);
4138 if (name == nullptr)
4139 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
4141 if (n_value != 0) {
4142 outs() << format("0x%" PRIx64, n_value);
4143 if (p != 0)
4144 outs() << " + " << format("0x%" PRIx64, p);
4145 } else
4146 outs() << format("0x%" PRIx64, p);
4147 if (name != nullptr)
4148 outs() << " " << name;
4149 outs() << "\n";
4151 p += n_value;
4152 if (func)
4153 func(p, info);
4157 static void
4158 walk_pointer_list_32(const char *listname, const SectionRef S,
4159 MachOObjectFile *O, struct DisassembleInfo *info,
4160 void (*func)(uint32_t, struct DisassembleInfo *info)) {
4161 if (S == SectionRef())
4162 return;
4164 StringRef SectName = unwrapOrError(S.getName(), O->getFileName());
4165 DataRefImpl Ref = S.getRawDataRefImpl();
4166 StringRef SegName = O->getSectionFinalSegmentName(Ref);
4167 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4169 StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName());
4170 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
4172 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
4173 uint32_t left = S.getSize() - i;
4174 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
4175 uint32_t p = 0;
4176 memcpy(&p, Contents + i, size);
4177 if (i + sizeof(uint32_t) > S.getSize())
4178 outs() << listname << " list pointer extends past end of (" << SegName
4179 << "," << SectName << ") section\n";
4180 uint32_t Address = S.getAddress() + i;
4181 outs() << format("%08" PRIx32, Address) << " ";
4183 if (O->isLittleEndian() != sys::IsLittleEndianHost)
4184 sys::swapByteOrder(p);
4185 outs() << format("0x%" PRIx32, p);
4187 const char *name = get_symbol_32(i, S, info, p);
4188 if (name != nullptr)
4189 outs() << " " << name;
4190 outs() << "\n";
4192 if (func)
4193 func(p, info);
4197 static void print_layout_map(const char *layout_map, uint32_t left) {
4198 if (layout_map == nullptr)
4199 return;
4200 outs() << " layout map: ";
4201 do {
4202 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
4203 left--;
4204 layout_map++;
4205 } while (*layout_map != '\0' && left != 0);
4206 outs() << "\n";
4209 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
4210 uint32_t offset, left;
4211 SectionRef S;
4212 const char *layout_map;
4214 if (p == 0)
4215 return;
4216 layout_map = get_pointer_64(p, offset, left, S, info);
4217 print_layout_map(layout_map, left);
4220 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
4221 uint32_t offset, left;
4222 SectionRef S;
4223 const char *layout_map;
4225 if (p == 0)
4226 return;
4227 layout_map = get_pointer_32(p, offset, left, S, info);
4228 print_layout_map(layout_map, left);
4231 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
4232 const char *indent) {
4233 struct method_list64_t ml;
4234 struct method64_t m;
4235 const char *r;
4236 uint32_t offset, xoffset, left, i;
4237 SectionRef S, xS;
4238 const char *name, *sym_name;
4239 uint64_t n_value;
4241 r = get_pointer_64(p, offset, left, S, info);
4242 if (r == nullptr)
4243 return;
4244 memset(&ml, '\0', sizeof(struct method_list64_t));
4245 if (left < sizeof(struct method_list64_t)) {
4246 memcpy(&ml, r, left);
4247 outs() << " (method_list_t entends past the end of the section)\n";
4248 } else
4249 memcpy(&ml, r, sizeof(struct method_list64_t));
4250 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4251 swapStruct(ml);
4252 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
4253 outs() << indent << "\t\t count " << ml.count << "\n";
4255 p += sizeof(struct method_list64_t);
4256 offset += sizeof(struct method_list64_t);
4257 for (i = 0; i < ml.count; i++) {
4258 r = get_pointer_64(p, offset, left, S, info);
4259 if (r == nullptr)
4260 return;
4261 memset(&m, '\0', sizeof(struct method64_t));
4262 if (left < sizeof(struct method64_t)) {
4263 memcpy(&m, r, left);
4264 outs() << indent << " (method_t extends past the end of the section)\n";
4265 } else
4266 memcpy(&m, r, sizeof(struct method64_t));
4267 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4268 swapStruct(m);
4270 outs() << indent << "\t\t name ";
4271 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
4272 info, n_value, m.name);
4273 if (n_value != 0) {
4274 if (info->verbose && sym_name != nullptr)
4275 outs() << sym_name;
4276 else
4277 outs() << format("0x%" PRIx64, n_value);
4278 if (m.name != 0)
4279 outs() << " + " << format("0x%" PRIx64, m.name);
4280 } else
4281 outs() << format("0x%" PRIx64, m.name);
4282 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
4283 if (name != nullptr)
4284 outs() << format(" %.*s", left, name);
4285 outs() << "\n";
4287 outs() << indent << "\t\t types ";
4288 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
4289 info, n_value, m.types);
4290 if (n_value != 0) {
4291 if (info->verbose && sym_name != nullptr)
4292 outs() << sym_name;
4293 else
4294 outs() << format("0x%" PRIx64, n_value);
4295 if (m.types != 0)
4296 outs() << " + " << format("0x%" PRIx64, m.types);
4297 } else
4298 outs() << format("0x%" PRIx64, m.types);
4299 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
4300 if (name != nullptr)
4301 outs() << format(" %.*s", left, name);
4302 outs() << "\n";
4304 outs() << indent << "\t\t imp ";
4305 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
4306 n_value, m.imp);
4307 if (info->verbose && name == nullptr) {
4308 if (n_value != 0) {
4309 outs() << format("0x%" PRIx64, n_value) << " ";
4310 if (m.imp != 0)
4311 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
4312 } else
4313 outs() << format("0x%" PRIx64, m.imp) << " ";
4315 if (name != nullptr)
4316 outs() << name;
4317 outs() << "\n";
4319 p += sizeof(struct method64_t);
4320 offset += sizeof(struct method64_t);
4324 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
4325 const char *indent) {
4326 struct method_list32_t ml;
4327 struct method32_t m;
4328 const char *r, *name;
4329 uint32_t offset, xoffset, left, i;
4330 SectionRef S, xS;
4332 r = get_pointer_32(p, offset, left, S, info);
4333 if (r == nullptr)
4334 return;
4335 memset(&ml, '\0', sizeof(struct method_list32_t));
4336 if (left < sizeof(struct method_list32_t)) {
4337 memcpy(&ml, r, left);
4338 outs() << " (method_list_t entends past the end of the section)\n";
4339 } else
4340 memcpy(&ml, r, sizeof(struct method_list32_t));
4341 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4342 swapStruct(ml);
4343 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
4344 outs() << indent << "\t\t count " << ml.count << "\n";
4346 p += sizeof(struct method_list32_t);
4347 offset += sizeof(struct method_list32_t);
4348 for (i = 0; i < ml.count; i++) {
4349 r = get_pointer_32(p, offset, left, S, info);
4350 if (r == nullptr)
4351 return;
4352 memset(&m, '\0', sizeof(struct method32_t));
4353 if (left < sizeof(struct method32_t)) {
4354 memcpy(&ml, r, left);
4355 outs() << indent << " (method_t entends past the end of the section)\n";
4356 } else
4357 memcpy(&m, r, sizeof(struct method32_t));
4358 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4359 swapStruct(m);
4361 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
4362 name = get_pointer_32(m.name, xoffset, left, xS, info);
4363 if (name != nullptr)
4364 outs() << format(" %.*s", left, name);
4365 outs() << "\n";
4367 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
4368 name = get_pointer_32(m.types, xoffset, left, xS, info);
4369 if (name != nullptr)
4370 outs() << format(" %.*s", left, name);
4371 outs() << "\n";
4373 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
4374 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
4375 m.imp);
4376 if (name != nullptr)
4377 outs() << " " << name;
4378 outs() << "\n";
4380 p += sizeof(struct method32_t);
4381 offset += sizeof(struct method32_t);
4385 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
4386 uint32_t offset, left, xleft;
4387 SectionRef S;
4388 struct objc_method_list_t method_list;
4389 struct objc_method_t method;
4390 const char *r, *methods, *name, *SymbolName;
4391 int32_t i;
4393 r = get_pointer_32(p, offset, left, S, info, true);
4394 if (r == nullptr)
4395 return true;
4397 outs() << "\n";
4398 if (left > sizeof(struct objc_method_list_t)) {
4399 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
4400 } else {
4401 outs() << "\t\t objc_method_list extends past end of the section\n";
4402 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
4403 memcpy(&method_list, r, left);
4405 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4406 swapStruct(method_list);
4408 outs() << "\t\t obsolete "
4409 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
4410 outs() << "\t\t method_count " << method_list.method_count << "\n";
4412 methods = r + sizeof(struct objc_method_list_t);
4413 for (i = 0; i < method_list.method_count; i++) {
4414 if ((i + 1) * sizeof(struct objc_method_t) > left) {
4415 outs() << "\t\t remaining method's extend past the of the section\n";
4416 break;
4418 memcpy(&method, methods + i * sizeof(struct objc_method_t),
4419 sizeof(struct objc_method_t));
4420 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4421 swapStruct(method);
4423 outs() << "\t\t method_name "
4424 << format("0x%08" PRIx32, method.method_name);
4425 if (info->verbose) {
4426 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
4427 if (name != nullptr)
4428 outs() << format(" %.*s", xleft, name);
4429 else
4430 outs() << " (not in an __OBJC section)";
4432 outs() << "\n";
4434 outs() << "\t\t method_types "
4435 << format("0x%08" PRIx32, method.method_types);
4436 if (info->verbose) {
4437 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
4438 if (name != nullptr)
4439 outs() << format(" %.*s", xleft, name);
4440 else
4441 outs() << " (not in an __OBJC section)";
4443 outs() << "\n";
4445 outs() << "\t\t method_imp "
4446 << format("0x%08" PRIx32, method.method_imp) << " ";
4447 if (info->verbose) {
4448 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
4449 if (SymbolName != nullptr)
4450 outs() << SymbolName;
4452 outs() << "\n";
4454 return false;
4457 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
4458 struct protocol_list64_t pl;
4459 uint64_t q, n_value;
4460 struct protocol64_t pc;
4461 const char *r;
4462 uint32_t offset, xoffset, left, i;
4463 SectionRef S, xS;
4464 const char *name, *sym_name;
4466 r = get_pointer_64(p, offset, left, S, info);
4467 if (r == nullptr)
4468 return;
4469 memset(&pl, '\0', sizeof(struct protocol_list64_t));
4470 if (left < sizeof(struct protocol_list64_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_list64_t));
4475 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4476 swapStruct(pl);
4477 outs() << " count " << pl.count << "\n";
4479 p += sizeof(struct protocol_list64_t);
4480 offset += sizeof(struct protocol_list64_t);
4481 for (i = 0; i < pl.count; i++) {
4482 r = get_pointer_64(p, offset, left, S, info);
4483 if (r == nullptr)
4484 return;
4485 q = 0;
4486 if (left < sizeof(uint64_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(uint64_t));
4491 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4492 sys::swapByteOrder(q);
4494 outs() << "\t\t list[" << i << "] ";
4495 sym_name = get_symbol_64(offset, S, info, n_value, q);
4496 if (n_value != 0) {
4497 if (info->verbose && sym_name != nullptr)
4498 outs() << sym_name;
4499 else
4500 outs() << format("0x%" PRIx64, n_value);
4501 if (q != 0)
4502 outs() << " + " << format("0x%" PRIx64, q);
4503 } else
4504 outs() << format("0x%" PRIx64, q);
4505 outs() << " (struct protocol_t *)\n";
4507 r = get_pointer_64(q + n_value, offset, left, S, info);
4508 if (r == nullptr)
4509 return;
4510 memset(&pc, '\0', sizeof(struct protocol64_t));
4511 if (left < sizeof(struct protocol64_t)) {
4512 memcpy(&pc, r, left);
4513 outs() << " (protocol_t entends past the end of the section)\n";
4514 } else
4515 memcpy(&pc, r, sizeof(struct protocol64_t));
4516 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4517 swapStruct(pc);
4519 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
4521 outs() << "\t\t\t name ";
4522 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
4523 info, n_value, pc.name);
4524 if (n_value != 0) {
4525 if (info->verbose && sym_name != nullptr)
4526 outs() << sym_name;
4527 else
4528 outs() << format("0x%" PRIx64, n_value);
4529 if (pc.name != 0)
4530 outs() << " + " << format("0x%" PRIx64, pc.name);
4531 } else
4532 outs() << format("0x%" PRIx64, pc.name);
4533 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
4534 if (name != nullptr)
4535 outs() << format(" %.*s", left, name);
4536 outs() << "\n";
4538 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
4540 outs() << "\t\t instanceMethods ";
4541 sym_name =
4542 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
4543 S, info, n_value, pc.instanceMethods);
4544 if (n_value != 0) {
4545 if (info->verbose && sym_name != nullptr)
4546 outs() << sym_name;
4547 else
4548 outs() << format("0x%" PRIx64, n_value);
4549 if (pc.instanceMethods != 0)
4550 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
4551 } else
4552 outs() << format("0x%" PRIx64, pc.instanceMethods);
4553 outs() << " (struct method_list_t *)\n";
4554 if (pc.instanceMethods + n_value != 0)
4555 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
4557 outs() << "\t\t classMethods ";
4558 sym_name =
4559 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
4560 info, n_value, pc.classMethods);
4561 if (n_value != 0) {
4562 if (info->verbose && sym_name != nullptr)
4563 outs() << sym_name;
4564 else
4565 outs() << format("0x%" PRIx64, n_value);
4566 if (pc.classMethods != 0)
4567 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
4568 } else
4569 outs() << format("0x%" PRIx64, pc.classMethods);
4570 outs() << " (struct method_list_t *)\n";
4571 if (pc.classMethods + n_value != 0)
4572 print_method_list64_t(pc.classMethods + n_value, info, "\t");
4574 outs() << "\t optionalInstanceMethods "
4575 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
4576 outs() << "\t optionalClassMethods "
4577 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
4578 outs() << "\t instanceProperties "
4579 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
4581 p += sizeof(uint64_t);
4582 offset += sizeof(uint64_t);
4586 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
4587 struct protocol_list32_t pl;
4588 uint32_t q;
4589 struct protocol32_t pc;
4590 const char *r;
4591 uint32_t offset, xoffset, left, i;
4592 SectionRef S, xS;
4593 const char *name;
4595 r = get_pointer_32(p, offset, left, S, info);
4596 if (r == nullptr)
4597 return;
4598 memset(&pl, '\0', sizeof(struct protocol_list32_t));
4599 if (left < sizeof(struct protocol_list32_t)) {
4600 memcpy(&pl, r, left);
4601 outs() << " (protocol_list_t entends past the end of the section)\n";
4602 } else
4603 memcpy(&pl, r, sizeof(struct protocol_list32_t));
4604 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4605 swapStruct(pl);
4606 outs() << " count " << pl.count << "\n";
4608 p += sizeof(struct protocol_list32_t);
4609 offset += sizeof(struct protocol_list32_t);
4610 for (i = 0; i < pl.count; i++) {
4611 r = get_pointer_32(p, offset, left, S, info);
4612 if (r == nullptr)
4613 return;
4614 q = 0;
4615 if (left < sizeof(uint32_t)) {
4616 memcpy(&q, r, left);
4617 outs() << " (protocol_t * entends past the end of the section)\n";
4618 } else
4619 memcpy(&q, r, sizeof(uint32_t));
4620 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4621 sys::swapByteOrder(q);
4622 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
4623 << " (struct protocol_t *)\n";
4624 r = get_pointer_32(q, offset, left, S, info);
4625 if (r == nullptr)
4626 return;
4627 memset(&pc, '\0', sizeof(struct protocol32_t));
4628 if (left < sizeof(struct protocol32_t)) {
4629 memcpy(&pc, r, left);
4630 outs() << " (protocol_t entends past the end of the section)\n";
4631 } else
4632 memcpy(&pc, r, sizeof(struct protocol32_t));
4633 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4634 swapStruct(pc);
4635 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
4636 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
4637 name = get_pointer_32(pc.name, xoffset, left, xS, info);
4638 if (name != nullptr)
4639 outs() << format(" %.*s", left, name);
4640 outs() << "\n";
4641 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
4642 outs() << "\t\t instanceMethods "
4643 << format("0x%" PRIx32, pc.instanceMethods)
4644 << " (struct method_list_t *)\n";
4645 if (pc.instanceMethods != 0)
4646 print_method_list32_t(pc.instanceMethods, info, "\t");
4647 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
4648 << " (struct method_list_t *)\n";
4649 if (pc.classMethods != 0)
4650 print_method_list32_t(pc.classMethods, info, "\t");
4651 outs() << "\t optionalInstanceMethods "
4652 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
4653 outs() << "\t optionalClassMethods "
4654 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
4655 outs() << "\t instanceProperties "
4656 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
4657 p += sizeof(uint32_t);
4658 offset += sizeof(uint32_t);
4662 static void print_indent(uint32_t indent) {
4663 for (uint32_t i = 0; i < indent;) {
4664 if (indent - i >= 8) {
4665 outs() << "\t";
4666 i += 8;
4667 } else {
4668 for (uint32_t j = i; j < indent; j++)
4669 outs() << " ";
4670 return;
4675 static bool print_method_description_list(uint32_t p, uint32_t indent,
4676 struct DisassembleInfo *info) {
4677 uint32_t offset, left, xleft;
4678 SectionRef S;
4679 struct objc_method_description_list_t mdl;
4680 struct objc_method_description_t md;
4681 const char *r, *list, *name;
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_method_description_list_t)) {
4690 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
4691 } else {
4692 print_indent(indent);
4693 outs() << " objc_method_description_list extends past end of the section\n";
4694 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
4695 memcpy(&mdl, r, left);
4697 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4698 swapStruct(mdl);
4700 print_indent(indent);
4701 outs() << " count " << mdl.count << "\n";
4703 list = r + sizeof(struct objc_method_description_list_t);
4704 for (i = 0; i < mdl.count; i++) {
4705 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
4706 print_indent(indent);
4707 outs() << " remaining list entries extend past the of the section\n";
4708 break;
4710 print_indent(indent);
4711 outs() << " list[" << i << "]\n";
4712 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
4713 sizeof(struct objc_method_description_t));
4714 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4715 swapStruct(md);
4717 print_indent(indent);
4718 outs() << " name " << format("0x%08" PRIx32, md.name);
4719 if (info->verbose) {
4720 name = get_pointer_32(md.name, offset, xleft, S, info, true);
4721 if (name != nullptr)
4722 outs() << format(" %.*s", xleft, name);
4723 else
4724 outs() << " (not in an __OBJC section)";
4726 outs() << "\n";
4728 print_indent(indent);
4729 outs() << " types " << format("0x%08" PRIx32, md.types);
4730 if (info->verbose) {
4731 name = get_pointer_32(md.types, offset, xleft, S, info, true);
4732 if (name != nullptr)
4733 outs() << format(" %.*s", xleft, name);
4734 else
4735 outs() << " (not in an __OBJC section)";
4737 outs() << "\n";
4739 return false;
4742 static bool print_protocol_list(uint32_t p, uint32_t indent,
4743 struct DisassembleInfo *info);
4745 static bool print_protocol(uint32_t p, uint32_t indent,
4746 struct DisassembleInfo *info) {
4747 uint32_t offset, left;
4748 SectionRef S;
4749 struct objc_protocol_t protocol;
4750 const char *r, *name;
4752 r = get_pointer_32(p, offset, left, S, info, true);
4753 if (r == nullptr)
4754 return true;
4756 outs() << "\n";
4757 if (left >= sizeof(struct objc_protocol_t)) {
4758 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
4759 } else {
4760 print_indent(indent);
4761 outs() << " Protocol extends past end of the section\n";
4762 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
4763 memcpy(&protocol, r, left);
4765 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4766 swapStruct(protocol);
4768 print_indent(indent);
4769 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
4770 << "\n";
4772 print_indent(indent);
4773 outs() << " protocol_name "
4774 << format("0x%08" PRIx32, protocol.protocol_name);
4775 if (info->verbose) {
4776 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
4777 if (name != nullptr)
4778 outs() << format(" %.*s", left, name);
4779 else
4780 outs() << " (not in an __OBJC section)";
4782 outs() << "\n";
4784 print_indent(indent);
4785 outs() << " protocol_list "
4786 << format("0x%08" PRIx32, protocol.protocol_list);
4787 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
4788 outs() << " (not in an __OBJC section)\n";
4790 print_indent(indent);
4791 outs() << " instance_methods "
4792 << format("0x%08" PRIx32, protocol.instance_methods);
4793 if (print_method_description_list(protocol.instance_methods, indent, info))
4794 outs() << " (not in an __OBJC section)\n";
4796 print_indent(indent);
4797 outs() << " class_methods "
4798 << format("0x%08" PRIx32, protocol.class_methods);
4799 if (print_method_description_list(protocol.class_methods, indent, info))
4800 outs() << " (not in an __OBJC section)\n";
4802 return false;
4805 static bool print_protocol_list(uint32_t p, uint32_t indent,
4806 struct DisassembleInfo *info) {
4807 uint32_t offset, left, l;
4808 SectionRef S;
4809 struct objc_protocol_list_t protocol_list;
4810 const char *r, *list;
4811 int32_t i;
4813 r = get_pointer_32(p, offset, left, S, info, true);
4814 if (r == nullptr)
4815 return true;
4817 outs() << "\n";
4818 if (left > sizeof(struct objc_protocol_list_t)) {
4819 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
4820 } else {
4821 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4822 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
4823 memcpy(&protocol_list, r, left);
4825 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4826 swapStruct(protocol_list);
4828 print_indent(indent);
4829 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
4830 << "\n";
4831 print_indent(indent);
4832 outs() << " count " << protocol_list.count << "\n";
4834 list = r + sizeof(struct objc_protocol_list_t);
4835 for (i = 0; i < protocol_list.count; i++) {
4836 if ((i + 1) * sizeof(uint32_t) > left) {
4837 outs() << "\t\t remaining list entries extend past the of the section\n";
4838 break;
4840 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
4841 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4842 sys::swapByteOrder(l);
4844 print_indent(indent);
4845 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
4846 if (print_protocol(l, indent, info))
4847 outs() << "(not in an __OBJC section)\n";
4849 return false;
4852 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
4853 struct ivar_list64_t il;
4854 struct ivar64_t i;
4855 const char *r;
4856 uint32_t offset, xoffset, left, j;
4857 SectionRef S, xS;
4858 const char *name, *sym_name, *ivar_offset_p;
4859 uint64_t ivar_offset, n_value;
4861 r = get_pointer_64(p, offset, left, S, info);
4862 if (r == nullptr)
4863 return;
4864 memset(&il, '\0', sizeof(struct ivar_list64_t));
4865 if (left < sizeof(struct ivar_list64_t)) {
4866 memcpy(&il, r, left);
4867 outs() << " (ivar_list_t entends past the end of the section)\n";
4868 } else
4869 memcpy(&il, r, sizeof(struct ivar_list64_t));
4870 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4871 swapStruct(il);
4872 outs() << " entsize " << il.entsize << "\n";
4873 outs() << " count " << il.count << "\n";
4875 p += sizeof(struct ivar_list64_t);
4876 offset += sizeof(struct ivar_list64_t);
4877 for (j = 0; j < il.count; j++) {
4878 r = get_pointer_64(p, offset, left, S, info);
4879 if (r == nullptr)
4880 return;
4881 memset(&i, '\0', sizeof(struct ivar64_t));
4882 if (left < sizeof(struct ivar64_t)) {
4883 memcpy(&i, r, left);
4884 outs() << " (ivar_t entends past the end of the section)\n";
4885 } else
4886 memcpy(&i, r, sizeof(struct ivar64_t));
4887 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4888 swapStruct(i);
4890 outs() << "\t\t\t offset ";
4891 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4892 info, n_value, i.offset);
4893 if (n_value != 0) {
4894 if (info->verbose && sym_name != nullptr)
4895 outs() << sym_name;
4896 else
4897 outs() << format("0x%" PRIx64, n_value);
4898 if (i.offset != 0)
4899 outs() << " + " << format("0x%" PRIx64, i.offset);
4900 } else
4901 outs() << format("0x%" PRIx64, i.offset);
4902 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4903 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4904 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4905 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4906 sys::swapByteOrder(ivar_offset);
4907 outs() << " " << ivar_offset << "\n";
4908 } else
4909 outs() << "\n";
4911 outs() << "\t\t\t name ";
4912 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4913 n_value, i.name);
4914 if (n_value != 0) {
4915 if (info->verbose && sym_name != nullptr)
4916 outs() << sym_name;
4917 else
4918 outs() << format("0x%" PRIx64, n_value);
4919 if (i.name != 0)
4920 outs() << " + " << format("0x%" PRIx64, i.name);
4921 } else
4922 outs() << format("0x%" PRIx64, i.name);
4923 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4924 if (name != nullptr)
4925 outs() << format(" %.*s", left, name);
4926 outs() << "\n";
4928 outs() << "\t\t\t type ";
4929 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4930 n_value, i.name);
4931 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4932 if (n_value != 0) {
4933 if (info->verbose && sym_name != nullptr)
4934 outs() << sym_name;
4935 else
4936 outs() << format("0x%" PRIx64, n_value);
4937 if (i.type != 0)
4938 outs() << " + " << format("0x%" PRIx64, i.type);
4939 } else
4940 outs() << format("0x%" PRIx64, i.type);
4941 if (name != nullptr)
4942 outs() << format(" %.*s", left, name);
4943 outs() << "\n";
4945 outs() << "\t\t\talignment " << i.alignment << "\n";
4946 outs() << "\t\t\t size " << i.size << "\n";
4948 p += sizeof(struct ivar64_t);
4949 offset += sizeof(struct ivar64_t);
4953 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4954 struct ivar_list32_t il;
4955 struct ivar32_t i;
4956 const char *r;
4957 uint32_t offset, xoffset, left, j;
4958 SectionRef S, xS;
4959 const char *name, *ivar_offset_p;
4960 uint32_t ivar_offset;
4962 r = get_pointer_32(p, offset, left, S, info);
4963 if (r == nullptr)
4964 return;
4965 memset(&il, '\0', sizeof(struct ivar_list32_t));
4966 if (left < sizeof(struct ivar_list32_t)) {
4967 memcpy(&il, r, left);
4968 outs() << " (ivar_list_t entends past the end of the section)\n";
4969 } else
4970 memcpy(&il, r, sizeof(struct ivar_list32_t));
4971 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4972 swapStruct(il);
4973 outs() << " entsize " << il.entsize << "\n";
4974 outs() << " count " << il.count << "\n";
4976 p += sizeof(struct ivar_list32_t);
4977 offset += sizeof(struct ivar_list32_t);
4978 for (j = 0; j < il.count; j++) {
4979 r = get_pointer_32(p, offset, left, S, info);
4980 if (r == nullptr)
4981 return;
4982 memset(&i, '\0', sizeof(struct ivar32_t));
4983 if (left < sizeof(struct ivar32_t)) {
4984 memcpy(&i, r, left);
4985 outs() << " (ivar_t entends past the end of the section)\n";
4986 } else
4987 memcpy(&i, r, sizeof(struct ivar32_t));
4988 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4989 swapStruct(i);
4991 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4992 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4993 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4994 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4995 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4996 sys::swapByteOrder(ivar_offset);
4997 outs() << " " << ivar_offset << "\n";
4998 } else
4999 outs() << "\n";
5001 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
5002 name = get_pointer_32(i.name, xoffset, left, xS, info);
5003 if (name != nullptr)
5004 outs() << format(" %.*s", left, name);
5005 outs() << "\n";
5007 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
5008 name = get_pointer_32(i.type, xoffset, left, xS, info);
5009 if (name != nullptr)
5010 outs() << format(" %.*s", left, name);
5011 outs() << "\n";
5013 outs() << "\t\t\talignment " << i.alignment << "\n";
5014 outs() << "\t\t\t size " << i.size << "\n";
5016 p += sizeof(struct ivar32_t);
5017 offset += sizeof(struct ivar32_t);
5021 static void print_objc_property_list64(uint64_t p,
5022 struct DisassembleInfo *info) {
5023 struct objc_property_list64 opl;
5024 struct objc_property64 op;
5025 const char *r;
5026 uint32_t offset, xoffset, left, j;
5027 SectionRef S, xS;
5028 const char *name, *sym_name;
5029 uint64_t n_value;
5031 r = get_pointer_64(p, offset, left, S, info);
5032 if (r == nullptr)
5033 return;
5034 memset(&opl, '\0', sizeof(struct objc_property_list64));
5035 if (left < sizeof(struct objc_property_list64)) {
5036 memcpy(&opl, r, left);
5037 outs() << " (objc_property_list entends past the end of the section)\n";
5038 } else
5039 memcpy(&opl, r, sizeof(struct objc_property_list64));
5040 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5041 swapStruct(opl);
5042 outs() << " entsize " << opl.entsize << "\n";
5043 outs() << " count " << opl.count << "\n";
5045 p += sizeof(struct objc_property_list64);
5046 offset += sizeof(struct objc_property_list64);
5047 for (j = 0; j < opl.count; j++) {
5048 r = get_pointer_64(p, offset, left, S, info);
5049 if (r == nullptr)
5050 return;
5051 memset(&op, '\0', sizeof(struct objc_property64));
5052 if (left < sizeof(struct objc_property64)) {
5053 memcpy(&op, r, left);
5054 outs() << " (objc_property entends past the end of the section)\n";
5055 } else
5056 memcpy(&op, r, sizeof(struct objc_property64));
5057 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5058 swapStruct(op);
5060 outs() << "\t\t\t name ";
5061 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
5062 info, n_value, op.name);
5063 if (n_value != 0) {
5064 if (info->verbose && sym_name != nullptr)
5065 outs() << sym_name;
5066 else
5067 outs() << format("0x%" PRIx64, n_value);
5068 if (op.name != 0)
5069 outs() << " + " << format("0x%" PRIx64, op.name);
5070 } else
5071 outs() << format("0x%" PRIx64, op.name);
5072 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
5073 if (name != nullptr)
5074 outs() << format(" %.*s", left, name);
5075 outs() << "\n";
5077 outs() << "\t\t\tattributes ";
5078 sym_name =
5079 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
5080 info, n_value, op.attributes);
5081 if (n_value != 0) {
5082 if (info->verbose && sym_name != nullptr)
5083 outs() << sym_name;
5084 else
5085 outs() << format("0x%" PRIx64, n_value);
5086 if (op.attributes != 0)
5087 outs() << " + " << format("0x%" PRIx64, op.attributes);
5088 } else
5089 outs() << format("0x%" PRIx64, op.attributes);
5090 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
5091 if (name != nullptr)
5092 outs() << format(" %.*s", left, name);
5093 outs() << "\n";
5095 p += sizeof(struct objc_property64);
5096 offset += sizeof(struct objc_property64);
5100 static void print_objc_property_list32(uint32_t p,
5101 struct DisassembleInfo *info) {
5102 struct objc_property_list32 opl;
5103 struct objc_property32 op;
5104 const char *r;
5105 uint32_t offset, xoffset, left, j;
5106 SectionRef S, xS;
5107 const char *name;
5109 r = get_pointer_32(p, offset, left, S, info);
5110 if (r == nullptr)
5111 return;
5112 memset(&opl, '\0', sizeof(struct objc_property_list32));
5113 if (left < sizeof(struct objc_property_list32)) {
5114 memcpy(&opl, r, left);
5115 outs() << " (objc_property_list entends past the end of the section)\n";
5116 } else
5117 memcpy(&opl, r, sizeof(struct objc_property_list32));
5118 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5119 swapStruct(opl);
5120 outs() << " entsize " << opl.entsize << "\n";
5121 outs() << " count " << opl.count << "\n";
5123 p += sizeof(struct objc_property_list32);
5124 offset += sizeof(struct objc_property_list32);
5125 for (j = 0; j < opl.count; j++) {
5126 r = get_pointer_32(p, offset, left, S, info);
5127 if (r == nullptr)
5128 return;
5129 memset(&op, '\0', sizeof(struct objc_property32));
5130 if (left < sizeof(struct objc_property32)) {
5131 memcpy(&op, r, left);
5132 outs() << " (objc_property entends past the end of the section)\n";
5133 } else
5134 memcpy(&op, r, sizeof(struct objc_property32));
5135 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5136 swapStruct(op);
5138 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
5139 name = get_pointer_32(op.name, xoffset, left, xS, info);
5140 if (name != nullptr)
5141 outs() << format(" %.*s", left, name);
5142 outs() << "\n";
5144 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
5145 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
5146 if (name != nullptr)
5147 outs() << format(" %.*s", left, name);
5148 outs() << "\n";
5150 p += sizeof(struct objc_property32);
5151 offset += sizeof(struct objc_property32);
5155 static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
5156 bool &is_meta_class) {
5157 struct class_ro64_t cro;
5158 const char *r;
5159 uint32_t offset, xoffset, left;
5160 SectionRef S, xS;
5161 const char *name, *sym_name;
5162 uint64_t n_value;
5164 r = get_pointer_64(p, offset, left, S, info);
5165 if (r == nullptr || left < sizeof(struct class_ro64_t))
5166 return false;
5167 memcpy(&cro, r, sizeof(struct class_ro64_t));
5168 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5169 swapStruct(cro);
5170 outs() << " flags " << format("0x%" PRIx32, cro.flags);
5171 if (cro.flags & RO_META)
5172 outs() << " RO_META";
5173 if (cro.flags & RO_ROOT)
5174 outs() << " RO_ROOT";
5175 if (cro.flags & RO_HAS_CXX_STRUCTORS)
5176 outs() << " RO_HAS_CXX_STRUCTORS";
5177 outs() << "\n";
5178 outs() << " instanceStart " << cro.instanceStart << "\n";
5179 outs() << " instanceSize " << cro.instanceSize << "\n";
5180 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
5181 << "\n";
5182 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
5183 << "\n";
5184 print_layout_map64(cro.ivarLayout, info);
5186 outs() << " name ";
5187 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
5188 info, n_value, cro.name);
5189 if (n_value != 0) {
5190 if (info->verbose && sym_name != nullptr)
5191 outs() << sym_name;
5192 else
5193 outs() << format("0x%" PRIx64, n_value);
5194 if (cro.name != 0)
5195 outs() << " + " << format("0x%" PRIx64, cro.name);
5196 } else
5197 outs() << format("0x%" PRIx64, cro.name);
5198 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
5199 if (name != nullptr)
5200 outs() << format(" %.*s", left, name);
5201 outs() << "\n";
5203 outs() << " baseMethods ";
5204 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
5205 S, info, n_value, cro.baseMethods);
5206 if (n_value != 0) {
5207 if (info->verbose && sym_name != nullptr)
5208 outs() << sym_name;
5209 else
5210 outs() << format("0x%" PRIx64, n_value);
5211 if (cro.baseMethods != 0)
5212 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
5213 } else
5214 outs() << format("0x%" PRIx64, cro.baseMethods);
5215 outs() << " (struct method_list_t *)\n";
5216 if (cro.baseMethods + n_value != 0)
5217 print_method_list64_t(cro.baseMethods + n_value, info, "");
5219 outs() << " baseProtocols ";
5220 sym_name =
5221 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
5222 info, n_value, cro.baseProtocols);
5223 if (n_value != 0) {
5224 if (info->verbose && sym_name != nullptr)
5225 outs() << sym_name;
5226 else
5227 outs() << format("0x%" PRIx64, n_value);
5228 if (cro.baseProtocols != 0)
5229 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
5230 } else
5231 outs() << format("0x%" PRIx64, cro.baseProtocols);
5232 outs() << "\n";
5233 if (cro.baseProtocols + n_value != 0)
5234 print_protocol_list64_t(cro.baseProtocols + n_value, info);
5236 outs() << " ivars ";
5237 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
5238 info, n_value, cro.ivars);
5239 if (n_value != 0) {
5240 if (info->verbose && sym_name != nullptr)
5241 outs() << sym_name;
5242 else
5243 outs() << format("0x%" PRIx64, n_value);
5244 if (cro.ivars != 0)
5245 outs() << " + " << format("0x%" PRIx64, cro.ivars);
5246 } else
5247 outs() << format("0x%" PRIx64, cro.ivars);
5248 outs() << "\n";
5249 if (cro.ivars + n_value != 0)
5250 print_ivar_list64_t(cro.ivars + n_value, info);
5252 outs() << " weakIvarLayout ";
5253 sym_name =
5254 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
5255 info, n_value, cro.weakIvarLayout);
5256 if (n_value != 0) {
5257 if (info->verbose && sym_name != nullptr)
5258 outs() << sym_name;
5259 else
5260 outs() << format("0x%" PRIx64, n_value);
5261 if (cro.weakIvarLayout != 0)
5262 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
5263 } else
5264 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
5265 outs() << "\n";
5266 print_layout_map64(cro.weakIvarLayout + n_value, info);
5268 outs() << " baseProperties ";
5269 sym_name =
5270 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
5271 info, n_value, cro.baseProperties);
5272 if (n_value != 0) {
5273 if (info->verbose && sym_name != nullptr)
5274 outs() << sym_name;
5275 else
5276 outs() << format("0x%" PRIx64, n_value);
5277 if (cro.baseProperties != 0)
5278 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
5279 } else
5280 outs() << format("0x%" PRIx64, cro.baseProperties);
5281 outs() << "\n";
5282 if (cro.baseProperties + n_value != 0)
5283 print_objc_property_list64(cro.baseProperties + n_value, info);
5285 is_meta_class = (cro.flags & RO_META) != 0;
5286 return true;
5289 static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
5290 bool &is_meta_class) {
5291 struct class_ro32_t cro;
5292 const char *r;
5293 uint32_t offset, xoffset, left;
5294 SectionRef S, xS;
5295 const char *name;
5297 r = get_pointer_32(p, offset, left, S, info);
5298 if (r == nullptr)
5299 return false;
5300 memset(&cro, '\0', sizeof(struct class_ro32_t));
5301 if (left < sizeof(struct class_ro32_t)) {
5302 memcpy(&cro, r, left);
5303 outs() << " (class_ro_t entends past the end of the section)\n";
5304 } else
5305 memcpy(&cro, r, sizeof(struct class_ro32_t));
5306 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5307 swapStruct(cro);
5308 outs() << " flags " << format("0x%" PRIx32, cro.flags);
5309 if (cro.flags & RO_META)
5310 outs() << " RO_META";
5311 if (cro.flags & RO_ROOT)
5312 outs() << " RO_ROOT";
5313 if (cro.flags & RO_HAS_CXX_STRUCTORS)
5314 outs() << " RO_HAS_CXX_STRUCTORS";
5315 outs() << "\n";
5316 outs() << " instanceStart " << cro.instanceStart << "\n";
5317 outs() << " instanceSize " << cro.instanceSize << "\n";
5318 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
5319 << "\n";
5320 print_layout_map32(cro.ivarLayout, info);
5322 outs() << " name " << format("0x%" PRIx32, cro.name);
5323 name = get_pointer_32(cro.name, xoffset, left, xS, info);
5324 if (name != nullptr)
5325 outs() << format(" %.*s", left, name);
5326 outs() << "\n";
5328 outs() << " baseMethods "
5329 << format("0x%" PRIx32, cro.baseMethods)
5330 << " (struct method_list_t *)\n";
5331 if (cro.baseMethods != 0)
5332 print_method_list32_t(cro.baseMethods, info, "");
5334 outs() << " baseProtocols "
5335 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
5336 if (cro.baseProtocols != 0)
5337 print_protocol_list32_t(cro.baseProtocols, info);
5338 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
5339 << "\n";
5340 if (cro.ivars != 0)
5341 print_ivar_list32_t(cro.ivars, info);
5342 outs() << " weakIvarLayout "
5343 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
5344 print_layout_map32(cro.weakIvarLayout, info);
5345 outs() << " baseProperties "
5346 << format("0x%" PRIx32, cro.baseProperties) << "\n";
5347 if (cro.baseProperties != 0)
5348 print_objc_property_list32(cro.baseProperties, info);
5349 is_meta_class = (cro.flags & RO_META) != 0;
5350 return true;
5353 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
5354 struct class64_t c;
5355 const char *r;
5356 uint32_t offset, left;
5357 SectionRef S;
5358 const char *name;
5359 uint64_t isa_n_value, n_value;
5361 r = get_pointer_64(p, offset, left, S, info);
5362 if (r == nullptr || left < sizeof(struct class64_t))
5363 return;
5364 memcpy(&c, r, sizeof(struct class64_t));
5365 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5366 swapStruct(c);
5368 outs() << " isa " << format("0x%" PRIx64, c.isa);
5369 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
5370 isa_n_value, c.isa);
5371 if (name != nullptr)
5372 outs() << " " << name;
5373 outs() << "\n";
5375 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
5376 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
5377 n_value, c.superclass);
5378 if (name != nullptr)
5379 outs() << " " << name;
5380 else {
5381 name = get_dyld_bind_info_symbolname(S.getAddress() +
5382 offset + offsetof(struct class64_t, superclass), info);
5383 if (name != nullptr)
5384 outs() << " " << name;
5386 outs() << "\n";
5388 outs() << " cache " << format("0x%" PRIx64, c.cache);
5389 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
5390 n_value, c.cache);
5391 if (name != nullptr)
5392 outs() << " " << name;
5393 outs() << "\n";
5395 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
5396 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
5397 n_value, c.vtable);
5398 if (name != nullptr)
5399 outs() << " " << name;
5400 outs() << "\n";
5402 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
5403 n_value, c.data);
5404 outs() << " data ";
5405 if (n_value != 0) {
5406 if (info->verbose && name != nullptr)
5407 outs() << name;
5408 else
5409 outs() << format("0x%" PRIx64, n_value);
5410 if (c.data != 0)
5411 outs() << " + " << format("0x%" PRIx64, c.data);
5412 } else
5413 outs() << format("0x%" PRIx64, c.data);
5414 outs() << " (struct class_ro_t *)";
5416 // This is a Swift class if some of the low bits of the pointer are set.
5417 if ((c.data + n_value) & 0x7)
5418 outs() << " Swift class";
5419 outs() << "\n";
5420 bool is_meta_class;
5421 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
5422 return;
5424 if (!is_meta_class &&
5425 c.isa + isa_n_value != p &&
5426 c.isa + isa_n_value != 0 &&
5427 info->depth < 100) {
5428 info->depth++;
5429 outs() << "Meta Class\n";
5430 print_class64_t(c.isa + isa_n_value, info);
5434 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
5435 struct class32_t c;
5436 const char *r;
5437 uint32_t offset, left;
5438 SectionRef S;
5439 const char *name;
5441 r = get_pointer_32(p, offset, left, S, info);
5442 if (r == nullptr)
5443 return;
5444 memset(&c, '\0', sizeof(struct class32_t));
5445 if (left < sizeof(struct class32_t)) {
5446 memcpy(&c, r, left);
5447 outs() << " (class_t entends past the end of the section)\n";
5448 } else
5449 memcpy(&c, r, sizeof(struct class32_t));
5450 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5451 swapStruct(c);
5453 outs() << " isa " << format("0x%" PRIx32, c.isa);
5454 name =
5455 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
5456 if (name != nullptr)
5457 outs() << " " << name;
5458 outs() << "\n";
5460 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
5461 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
5462 c.superclass);
5463 if (name != nullptr)
5464 outs() << " " << name;
5465 outs() << "\n";
5467 outs() << " cache " << format("0x%" PRIx32, c.cache);
5468 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
5469 c.cache);
5470 if (name != nullptr)
5471 outs() << " " << name;
5472 outs() << "\n";
5474 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
5475 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
5476 c.vtable);
5477 if (name != nullptr)
5478 outs() << " " << name;
5479 outs() << "\n";
5481 name =
5482 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
5483 outs() << " data " << format("0x%" PRIx32, c.data)
5484 << " (struct class_ro_t *)";
5486 // This is a Swift class if some of the low bits of the pointer are set.
5487 if (c.data & 0x3)
5488 outs() << " Swift class";
5489 outs() << "\n";
5490 bool is_meta_class;
5491 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
5492 return;
5494 if (!is_meta_class) {
5495 outs() << "Meta Class\n";
5496 print_class32_t(c.isa, info);
5500 static void print_objc_class_t(struct objc_class_t *objc_class,
5501 struct DisassembleInfo *info) {
5502 uint32_t offset, left, xleft;
5503 const char *name, *p, *ivar_list;
5504 SectionRef S;
5505 int32_t i;
5506 struct objc_ivar_list_t objc_ivar_list;
5507 struct objc_ivar_t ivar;
5509 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
5510 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
5511 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
5512 if (name != nullptr)
5513 outs() << format(" %.*s", left, name);
5514 else
5515 outs() << " (not in an __OBJC section)";
5517 outs() << "\n";
5519 outs() << "\t super_class "
5520 << format("0x%08" PRIx32, objc_class->super_class);
5521 if (info->verbose) {
5522 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
5523 if (name != nullptr)
5524 outs() << format(" %.*s", left, name);
5525 else
5526 outs() << " (not in an __OBJC section)";
5528 outs() << "\n";
5530 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
5531 if (info->verbose) {
5532 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
5533 if (name != nullptr)
5534 outs() << format(" %.*s", left, name);
5535 else
5536 outs() << " (not in an __OBJC section)";
5538 outs() << "\n";
5540 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
5541 << "\n";
5543 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
5544 if (info->verbose) {
5545 if (CLS_GETINFO(objc_class, CLS_CLASS))
5546 outs() << " CLS_CLASS";
5547 else if (CLS_GETINFO(objc_class, CLS_META))
5548 outs() << " CLS_META";
5550 outs() << "\n";
5552 outs() << "\t instance_size "
5553 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
5555 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
5556 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
5557 if (p != nullptr) {
5558 if (left > sizeof(struct objc_ivar_list_t)) {
5559 outs() << "\n";
5560 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
5561 } else {
5562 outs() << " (entends past the end of the section)\n";
5563 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
5564 memcpy(&objc_ivar_list, p, left);
5566 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5567 swapStruct(objc_ivar_list);
5568 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
5569 ivar_list = p + sizeof(struct objc_ivar_list_t);
5570 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
5571 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
5572 outs() << "\t\t remaining ivar's extend past the of the section\n";
5573 break;
5575 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
5576 sizeof(struct objc_ivar_t));
5577 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5578 swapStruct(ivar);
5580 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
5581 if (info->verbose) {
5582 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
5583 if (name != nullptr)
5584 outs() << format(" %.*s", xleft, name);
5585 else
5586 outs() << " (not in an __OBJC section)";
5588 outs() << "\n";
5590 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
5591 if (info->verbose) {
5592 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
5593 if (name != nullptr)
5594 outs() << format(" %.*s", xleft, name);
5595 else
5596 outs() << " (not in an __OBJC section)";
5598 outs() << "\n";
5600 outs() << "\t\t ivar_offset "
5601 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
5603 } else {
5604 outs() << " (not in an __OBJC section)\n";
5607 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
5608 if (print_method_list(objc_class->methodLists, info))
5609 outs() << " (not in an __OBJC section)\n";
5611 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
5612 << "\n";
5614 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
5615 if (print_protocol_list(objc_class->protocols, 16, info))
5616 outs() << " (not in an __OBJC section)\n";
5619 static void print_objc_objc_category_t(struct objc_category_t *objc_category,
5620 struct DisassembleInfo *info) {
5621 uint32_t offset, left;
5622 const char *name;
5623 SectionRef S;
5625 outs() << "\t category name "
5626 << format("0x%08" PRIx32, objc_category->category_name);
5627 if (info->verbose) {
5628 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
5629 true);
5630 if (name != nullptr)
5631 outs() << format(" %.*s", left, name);
5632 else
5633 outs() << " (not in an __OBJC section)";
5635 outs() << "\n";
5637 outs() << "\t\t class name "
5638 << format("0x%08" PRIx32, objc_category->class_name);
5639 if (info->verbose) {
5640 name =
5641 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
5642 if (name != nullptr)
5643 outs() << format(" %.*s", left, name);
5644 else
5645 outs() << " (not in an __OBJC section)";
5647 outs() << "\n";
5649 outs() << "\t instance methods "
5650 << format("0x%08" PRIx32, objc_category->instance_methods);
5651 if (print_method_list(objc_category->instance_methods, info))
5652 outs() << " (not in an __OBJC section)\n";
5654 outs() << "\t class methods "
5655 << format("0x%08" PRIx32, objc_category->class_methods);
5656 if (print_method_list(objc_category->class_methods, info))
5657 outs() << " (not in an __OBJC section)\n";
5660 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
5661 struct category64_t c;
5662 const char *r;
5663 uint32_t offset, xoffset, left;
5664 SectionRef S, xS;
5665 const char *name, *sym_name;
5666 uint64_t n_value;
5668 r = get_pointer_64(p, offset, left, S, info);
5669 if (r == nullptr)
5670 return;
5671 memset(&c, '\0', sizeof(struct category64_t));
5672 if (left < sizeof(struct category64_t)) {
5673 memcpy(&c, r, left);
5674 outs() << " (category_t entends past the end of the section)\n";
5675 } else
5676 memcpy(&c, r, sizeof(struct category64_t));
5677 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5678 swapStruct(c);
5680 outs() << " name ";
5681 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
5682 info, n_value, c.name);
5683 if (n_value != 0) {
5684 if (info->verbose && sym_name != nullptr)
5685 outs() << sym_name;
5686 else
5687 outs() << format("0x%" PRIx64, n_value);
5688 if (c.name != 0)
5689 outs() << " + " << format("0x%" PRIx64, c.name);
5690 } else
5691 outs() << format("0x%" PRIx64, c.name);
5692 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
5693 if (name != nullptr)
5694 outs() << format(" %.*s", left, name);
5695 outs() << "\n";
5697 outs() << " cls ";
5698 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
5699 n_value, c.cls);
5700 if (n_value != 0) {
5701 if (info->verbose && sym_name != nullptr)
5702 outs() << sym_name;
5703 else
5704 outs() << format("0x%" PRIx64, n_value);
5705 if (c.cls != 0)
5706 outs() << " + " << format("0x%" PRIx64, c.cls);
5707 } else
5708 outs() << format("0x%" PRIx64, c.cls);
5709 outs() << "\n";
5710 if (c.cls + n_value != 0)
5711 print_class64_t(c.cls + n_value, info);
5713 outs() << " instanceMethods ";
5714 sym_name =
5715 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
5716 info, n_value, c.instanceMethods);
5717 if (n_value != 0) {
5718 if (info->verbose && sym_name != nullptr)
5719 outs() << sym_name;
5720 else
5721 outs() << format("0x%" PRIx64, n_value);
5722 if (c.instanceMethods != 0)
5723 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
5724 } else
5725 outs() << format("0x%" PRIx64, c.instanceMethods);
5726 outs() << "\n";
5727 if (c.instanceMethods + n_value != 0)
5728 print_method_list64_t(c.instanceMethods + n_value, info, "");
5730 outs() << " classMethods ";
5731 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
5732 S, info, n_value, c.classMethods);
5733 if (n_value != 0) {
5734 if (info->verbose && sym_name != nullptr)
5735 outs() << sym_name;
5736 else
5737 outs() << format("0x%" PRIx64, n_value);
5738 if (c.classMethods != 0)
5739 outs() << " + " << format("0x%" PRIx64, c.classMethods);
5740 } else
5741 outs() << format("0x%" PRIx64, c.classMethods);
5742 outs() << "\n";
5743 if (c.classMethods + n_value != 0)
5744 print_method_list64_t(c.classMethods + n_value, info, "");
5746 outs() << " protocols ";
5747 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
5748 info, n_value, c.protocols);
5749 if (n_value != 0) {
5750 if (info->verbose && sym_name != nullptr)
5751 outs() << sym_name;
5752 else
5753 outs() << format("0x%" PRIx64, n_value);
5754 if (c.protocols != 0)
5755 outs() << " + " << format("0x%" PRIx64, c.protocols);
5756 } else
5757 outs() << format("0x%" PRIx64, c.protocols);
5758 outs() << "\n";
5759 if (c.protocols + n_value != 0)
5760 print_protocol_list64_t(c.protocols + n_value, info);
5762 outs() << "instanceProperties ";
5763 sym_name =
5764 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
5765 S, info, n_value, c.instanceProperties);
5766 if (n_value != 0) {
5767 if (info->verbose && sym_name != nullptr)
5768 outs() << sym_name;
5769 else
5770 outs() << format("0x%" PRIx64, n_value);
5771 if (c.instanceProperties != 0)
5772 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
5773 } else
5774 outs() << format("0x%" PRIx64, c.instanceProperties);
5775 outs() << "\n";
5776 if (c.instanceProperties + n_value != 0)
5777 print_objc_property_list64(c.instanceProperties + n_value, info);
5780 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
5781 struct category32_t c;
5782 const char *r;
5783 uint32_t offset, left;
5784 SectionRef S, xS;
5785 const char *name;
5787 r = get_pointer_32(p, offset, left, S, info);
5788 if (r == nullptr)
5789 return;
5790 memset(&c, '\0', sizeof(struct category32_t));
5791 if (left < sizeof(struct category32_t)) {
5792 memcpy(&c, r, left);
5793 outs() << " (category_t entends past the end of the section)\n";
5794 } else
5795 memcpy(&c, r, sizeof(struct category32_t));
5796 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5797 swapStruct(c);
5799 outs() << " name " << format("0x%" PRIx32, c.name);
5800 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
5801 c.name);
5802 if (name)
5803 outs() << " " << name;
5804 outs() << "\n";
5806 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
5807 if (c.cls != 0)
5808 print_class32_t(c.cls, info);
5809 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
5810 << "\n";
5811 if (c.instanceMethods != 0)
5812 print_method_list32_t(c.instanceMethods, info, "");
5813 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
5814 << "\n";
5815 if (c.classMethods != 0)
5816 print_method_list32_t(c.classMethods, info, "");
5817 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
5818 if (c.protocols != 0)
5819 print_protocol_list32_t(c.protocols, info);
5820 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
5821 << "\n";
5822 if (c.instanceProperties != 0)
5823 print_objc_property_list32(c.instanceProperties, info);
5826 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
5827 uint32_t i, left, offset, xoffset;
5828 uint64_t p, n_value;
5829 struct message_ref64 mr;
5830 const char *name, *sym_name;
5831 const char *r;
5832 SectionRef xS;
5834 if (S == SectionRef())
5835 return;
5837 StringRef SectName;
5838 Expected<StringRef> SecNameOrErr = S.getName();
5839 if (SecNameOrErr)
5840 SectName = *SecNameOrErr;
5841 else
5842 consumeError(SecNameOrErr.takeError());
5844 DataRefImpl Ref = S.getRawDataRefImpl();
5845 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5846 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5847 offset = 0;
5848 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5849 p = S.getAddress() + i;
5850 r = get_pointer_64(p, offset, left, S, info);
5851 if (r == nullptr)
5852 return;
5853 memset(&mr, '\0', sizeof(struct message_ref64));
5854 if (left < sizeof(struct message_ref64)) {
5855 memcpy(&mr, r, left);
5856 outs() << " (message_ref entends past the end of the section)\n";
5857 } else
5858 memcpy(&mr, r, sizeof(struct message_ref64));
5859 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5860 swapStruct(mr);
5862 outs() << " imp ";
5863 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
5864 n_value, mr.imp);
5865 if (n_value != 0) {
5866 outs() << format("0x%" PRIx64, n_value) << " ";
5867 if (mr.imp != 0)
5868 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
5869 } else
5870 outs() << format("0x%" PRIx64, mr.imp) << " ";
5871 if (name != nullptr)
5872 outs() << " " << name;
5873 outs() << "\n";
5875 outs() << " sel ";
5876 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
5877 info, n_value, mr.sel);
5878 if (n_value != 0) {
5879 if (info->verbose && sym_name != nullptr)
5880 outs() << sym_name;
5881 else
5882 outs() << format("0x%" PRIx64, n_value);
5883 if (mr.sel != 0)
5884 outs() << " + " << format("0x%" PRIx64, mr.sel);
5885 } else
5886 outs() << format("0x%" PRIx64, mr.sel);
5887 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5888 if (name != nullptr)
5889 outs() << format(" %.*s", left, name);
5890 outs() << "\n";
5892 offset += sizeof(struct message_ref64);
5896 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5897 uint32_t i, left, offset, xoffset, p;
5898 struct message_ref32 mr;
5899 const char *name, *r;
5900 SectionRef xS;
5902 if (S == SectionRef())
5903 return;
5905 StringRef SectName;
5906 Expected<StringRef> SecNameOrErr = S.getName();
5907 if (SecNameOrErr)
5908 SectName = *SecNameOrErr;
5909 else
5910 consumeError(SecNameOrErr.takeError());
5912 DataRefImpl Ref = S.getRawDataRefImpl();
5913 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5914 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5915 offset = 0;
5916 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5917 p = S.getAddress() + i;
5918 r = get_pointer_32(p, offset, left, S, info);
5919 if (r == nullptr)
5920 return;
5921 memset(&mr, '\0', sizeof(struct message_ref32));
5922 if (left < sizeof(struct message_ref32)) {
5923 memcpy(&mr, r, left);
5924 outs() << " (message_ref entends past the end of the section)\n";
5925 } else
5926 memcpy(&mr, r, sizeof(struct message_ref32));
5927 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5928 swapStruct(mr);
5930 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5931 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5932 mr.imp);
5933 if (name != nullptr)
5934 outs() << " " << name;
5935 outs() << "\n";
5937 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5938 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5939 if (name != nullptr)
5940 outs() << " " << name;
5941 outs() << "\n";
5943 offset += sizeof(struct message_ref32);
5947 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5948 uint32_t left, offset, swift_version;
5949 uint64_t p;
5950 struct objc_image_info64 o;
5951 const char *r;
5953 if (S == SectionRef())
5954 return;
5956 StringRef SectName;
5957 Expected<StringRef> SecNameOrErr = S.getName();
5958 if (SecNameOrErr)
5959 SectName = *SecNameOrErr;
5960 else
5961 consumeError(SecNameOrErr.takeError());
5963 DataRefImpl Ref = S.getRawDataRefImpl();
5964 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5965 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5966 p = S.getAddress();
5967 r = get_pointer_64(p, offset, left, S, info);
5968 if (r == nullptr)
5969 return;
5970 memset(&o, '\0', sizeof(struct objc_image_info64));
5971 if (left < sizeof(struct objc_image_info64)) {
5972 memcpy(&o, r, left);
5973 outs() << " (objc_image_info entends past the end of the section)\n";
5974 } else
5975 memcpy(&o, r, sizeof(struct objc_image_info64));
5976 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5977 swapStruct(o);
5978 outs() << " version " << o.version << "\n";
5979 outs() << " flags " << format("0x%" PRIx32, o.flags);
5980 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5981 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5982 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5983 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5984 if (o.flags & OBJC_IMAGE_IS_SIMULATED)
5985 outs() << " OBJC_IMAGE_IS_SIMULATED";
5986 if (o.flags & OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES)
5987 outs() << " OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES";
5988 swift_version = (o.flags >> 8) & 0xff;
5989 if (swift_version != 0) {
5990 if (swift_version == 1)
5991 outs() << " Swift 1.0";
5992 else if (swift_version == 2)
5993 outs() << " Swift 1.1";
5994 else if(swift_version == 3)
5995 outs() << " Swift 2.0";
5996 else if(swift_version == 4)
5997 outs() << " Swift 3.0";
5998 else if(swift_version == 5)
5999 outs() << " Swift 4.0";
6000 else if(swift_version == 6)
6001 outs() << " Swift 4.1/Swift 4.2";
6002 else if(swift_version == 7)
6003 outs() << " Swift 5 or later";
6004 else
6005 outs() << " unknown future Swift version (" << swift_version << ")";
6007 outs() << "\n";
6010 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
6011 uint32_t left, offset, swift_version, p;
6012 struct objc_image_info32 o;
6013 const char *r;
6015 if (S == SectionRef())
6016 return;
6018 StringRef SectName;
6019 Expected<StringRef> SecNameOrErr = S.getName();
6020 if (SecNameOrErr)
6021 SectName = *SecNameOrErr;
6022 else
6023 consumeError(SecNameOrErr.takeError());
6025 DataRefImpl Ref = S.getRawDataRefImpl();
6026 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
6027 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
6028 p = S.getAddress();
6029 r = get_pointer_32(p, offset, left, S, info);
6030 if (r == nullptr)
6031 return;
6032 memset(&o, '\0', sizeof(struct objc_image_info32));
6033 if (left < sizeof(struct objc_image_info32)) {
6034 memcpy(&o, r, left);
6035 outs() << " (objc_image_info entends past the end of the section)\n";
6036 } else
6037 memcpy(&o, r, sizeof(struct objc_image_info32));
6038 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
6039 swapStruct(o);
6040 outs() << " version " << o.version << "\n";
6041 outs() << " flags " << format("0x%" PRIx32, o.flags);
6042 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
6043 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
6044 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
6045 outs() << " OBJC_IMAGE_SUPPORTS_GC";
6046 swift_version = (o.flags >> 8) & 0xff;
6047 if (swift_version != 0) {
6048 if (swift_version == 1)
6049 outs() << " Swift 1.0";
6050 else if (swift_version == 2)
6051 outs() << " Swift 1.1";
6052 else if(swift_version == 3)
6053 outs() << " Swift 2.0";
6054 else if(swift_version == 4)
6055 outs() << " Swift 3.0";
6056 else if(swift_version == 5)
6057 outs() << " Swift 4.0";
6058 else if(swift_version == 6)
6059 outs() << " Swift 4.1/Swift 4.2";
6060 else if(swift_version == 7)
6061 outs() << " Swift 5 or later";
6062 else
6063 outs() << " unknown future Swift version (" << swift_version << ")";
6065 outs() << "\n";
6068 static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
6069 uint32_t left, offset, p;
6070 struct imageInfo_t o;
6071 const char *r;
6073 StringRef SectName;
6074 Expected<StringRef> SecNameOrErr = S.getName();
6075 if (SecNameOrErr)
6076 SectName = *SecNameOrErr;
6077 else
6078 consumeError(SecNameOrErr.takeError());
6080 DataRefImpl Ref = S.getRawDataRefImpl();
6081 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
6082 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
6083 p = S.getAddress();
6084 r = get_pointer_32(p, offset, left, S, info);
6085 if (r == nullptr)
6086 return;
6087 memset(&o, '\0', sizeof(struct imageInfo_t));
6088 if (left < sizeof(struct imageInfo_t)) {
6089 memcpy(&o, r, left);
6090 outs() << " (imageInfo entends past the end of the section)\n";
6091 } else
6092 memcpy(&o, r, sizeof(struct imageInfo_t));
6093 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
6094 swapStruct(o);
6095 outs() << " version " << o.version << "\n";
6096 outs() << " flags " << format("0x%" PRIx32, o.flags);
6097 if (o.flags & 0x1)
6098 outs() << " F&C";
6099 if (o.flags & 0x2)
6100 outs() << " GC";
6101 if (o.flags & 0x4)
6102 outs() << " GC-only";
6103 else
6104 outs() << " RR";
6105 outs() << "\n";
6108 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
6109 SymbolAddressMap AddrMap;
6110 if (verbose)
6111 CreateSymbolAddressMap(O, &AddrMap);
6113 std::vector<SectionRef> Sections;
6114 for (const SectionRef &Section : O->sections())
6115 Sections.push_back(Section);
6117 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
6119 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
6120 if (CL == SectionRef())
6121 CL = get_section(O, "__DATA", "__objc_classlist");
6122 if (CL == SectionRef())
6123 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
6124 if (CL == SectionRef())
6125 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
6126 info.S = CL;
6127 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
6129 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
6130 if (CR == SectionRef())
6131 CR = get_section(O, "__DATA", "__objc_classrefs");
6132 if (CR == SectionRef())
6133 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
6134 if (CR == SectionRef())
6135 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
6136 info.S = CR;
6137 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
6139 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
6140 if (SR == SectionRef())
6141 SR = get_section(O, "__DATA", "__objc_superrefs");
6142 if (SR == SectionRef())
6143 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
6144 if (SR == SectionRef())
6145 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
6146 info.S = SR;
6147 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
6149 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
6150 if (CA == SectionRef())
6151 CA = get_section(O, "__DATA", "__objc_catlist");
6152 if (CA == SectionRef())
6153 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
6154 if (CA == SectionRef())
6155 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
6156 info.S = CA;
6157 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
6159 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
6160 if (PL == SectionRef())
6161 PL = get_section(O, "__DATA", "__objc_protolist");
6162 if (PL == SectionRef())
6163 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
6164 if (PL == SectionRef())
6165 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
6166 info.S = PL;
6167 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
6169 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
6170 if (MR == SectionRef())
6171 MR = get_section(O, "__DATA", "__objc_msgrefs");
6172 if (MR == SectionRef())
6173 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
6174 if (MR == SectionRef())
6175 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
6176 info.S = MR;
6177 print_message_refs64(MR, &info);
6179 SectionRef II = get_section(O, "__OBJC2", "__image_info");
6180 if (II == SectionRef())
6181 II = get_section(O, "__DATA", "__objc_imageinfo");
6182 if (II == SectionRef())
6183 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
6184 if (II == SectionRef())
6185 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
6186 info.S = II;
6187 print_image_info64(II, &info);
6190 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
6191 SymbolAddressMap AddrMap;
6192 if (verbose)
6193 CreateSymbolAddressMap(O, &AddrMap);
6195 std::vector<SectionRef> Sections;
6196 for (const SectionRef &Section : O->sections())
6197 Sections.push_back(Section);
6199 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
6201 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
6202 if (CL == SectionRef())
6203 CL = get_section(O, "__DATA", "__objc_classlist");
6204 if (CL == SectionRef())
6205 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
6206 if (CL == SectionRef())
6207 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
6208 info.S = CL;
6209 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
6211 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
6212 if (CR == SectionRef())
6213 CR = get_section(O, "__DATA", "__objc_classrefs");
6214 if (CR == SectionRef())
6215 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
6216 if (CR == SectionRef())
6217 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
6218 info.S = CR;
6219 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
6221 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
6222 if (SR == SectionRef())
6223 SR = get_section(O, "__DATA", "__objc_superrefs");
6224 if (SR == SectionRef())
6225 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
6226 if (SR == SectionRef())
6227 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
6228 info.S = SR;
6229 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
6231 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
6232 if (CA == SectionRef())
6233 CA = get_section(O, "__DATA", "__objc_catlist");
6234 if (CA == SectionRef())
6235 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
6236 if (CA == SectionRef())
6237 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
6238 info.S = CA;
6239 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
6241 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
6242 if (PL == SectionRef())
6243 PL = get_section(O, "__DATA", "__objc_protolist");
6244 if (PL == SectionRef())
6245 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
6246 if (PL == SectionRef())
6247 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
6248 info.S = PL;
6249 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
6251 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
6252 if (MR == SectionRef())
6253 MR = get_section(O, "__DATA", "__objc_msgrefs");
6254 if (MR == SectionRef())
6255 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
6256 if (MR == SectionRef())
6257 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
6258 info.S = MR;
6259 print_message_refs32(MR, &info);
6261 SectionRef II = get_section(O, "__OBJC2", "__image_info");
6262 if (II == SectionRef())
6263 II = get_section(O, "__DATA", "__objc_imageinfo");
6264 if (II == SectionRef())
6265 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
6266 if (II == SectionRef())
6267 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
6268 info.S = II;
6269 print_image_info32(II, &info);
6272 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
6273 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
6274 const char *r, *name, *defs;
6275 struct objc_module_t module;
6276 SectionRef S, xS;
6277 struct objc_symtab_t symtab;
6278 struct objc_class_t objc_class;
6279 struct objc_category_t objc_category;
6281 outs() << "Objective-C segment\n";
6282 S = get_section(O, "__OBJC", "__module_info");
6283 if (S == SectionRef())
6284 return false;
6286 SymbolAddressMap AddrMap;
6287 if (verbose)
6288 CreateSymbolAddressMap(O, &AddrMap);
6290 std::vector<SectionRef> Sections;
6291 for (const SectionRef &Section : O->sections())
6292 Sections.push_back(Section);
6294 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
6296 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
6297 p = S.getAddress() + i;
6298 r = get_pointer_32(p, offset, left, S, &info, true);
6299 if (r == nullptr)
6300 return true;
6301 memset(&module, '\0', sizeof(struct objc_module_t));
6302 if (left < sizeof(struct objc_module_t)) {
6303 memcpy(&module, r, left);
6304 outs() << " (module extends past end of __module_info section)\n";
6305 } else
6306 memcpy(&module, r, sizeof(struct objc_module_t));
6307 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6308 swapStruct(module);
6310 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
6311 outs() << " version " << module.version << "\n";
6312 outs() << " size " << module.size << "\n";
6313 outs() << " name ";
6314 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
6315 if (name != nullptr)
6316 outs() << format("%.*s", left, name);
6317 else
6318 outs() << format("0x%08" PRIx32, module.name)
6319 << "(not in an __OBJC section)";
6320 outs() << "\n";
6322 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
6323 if (module.symtab == 0 || r == nullptr) {
6324 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
6325 << " (not in an __OBJC section)\n";
6326 continue;
6328 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
6329 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
6330 defs_left = 0;
6331 defs = nullptr;
6332 if (left < sizeof(struct objc_symtab_t)) {
6333 memcpy(&symtab, r, left);
6334 outs() << "\tsymtab extends past end of an __OBJC section)\n";
6335 } else {
6336 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
6337 if (left > sizeof(struct objc_symtab_t)) {
6338 defs_left = left - sizeof(struct objc_symtab_t);
6339 defs = r + sizeof(struct objc_symtab_t);
6342 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6343 swapStruct(symtab);
6345 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
6346 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
6347 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
6348 if (r == nullptr)
6349 outs() << " (not in an __OBJC section)";
6350 outs() << "\n";
6351 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
6352 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
6353 if (symtab.cls_def_cnt > 0)
6354 outs() << "\tClass Definitions\n";
6355 for (j = 0; j < symtab.cls_def_cnt; j++) {
6356 if ((j + 1) * sizeof(uint32_t) > defs_left) {
6357 outs() << "\t(remaining class defs entries entends past the end of the "
6358 << "section)\n";
6359 break;
6361 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
6362 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6363 sys::swapByteOrder(def);
6365 r = get_pointer_32(def, xoffset, left, xS, &info, true);
6366 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
6367 if (r != nullptr) {
6368 if (left > sizeof(struct objc_class_t)) {
6369 outs() << "\n";
6370 memcpy(&objc_class, r, sizeof(struct objc_class_t));
6371 } else {
6372 outs() << " (entends past the end of the section)\n";
6373 memset(&objc_class, '\0', sizeof(struct objc_class_t));
6374 memcpy(&objc_class, r, left);
6376 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6377 swapStruct(objc_class);
6378 print_objc_class_t(&objc_class, &info);
6379 } else {
6380 outs() << "(not in an __OBJC section)\n";
6383 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
6384 outs() << "\tMeta Class";
6385 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
6386 if (r != nullptr) {
6387 if (left > sizeof(struct objc_class_t)) {
6388 outs() << "\n";
6389 memcpy(&objc_class, r, sizeof(struct objc_class_t));
6390 } else {
6391 outs() << " (entends past the end of the section)\n";
6392 memset(&objc_class, '\0', sizeof(struct objc_class_t));
6393 memcpy(&objc_class, r, left);
6395 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6396 swapStruct(objc_class);
6397 print_objc_class_t(&objc_class, &info);
6398 } else {
6399 outs() << "(not in an __OBJC section)\n";
6403 if (symtab.cat_def_cnt > 0)
6404 outs() << "\tCategory Definitions\n";
6405 for (j = 0; j < symtab.cat_def_cnt; j++) {
6406 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
6407 outs() << "\t(remaining category defs entries entends past the end of "
6408 << "the section)\n";
6409 break;
6411 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
6412 sizeof(uint32_t));
6413 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6414 sys::swapByteOrder(def);
6416 r = get_pointer_32(def, xoffset, left, xS, &info, true);
6417 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
6418 << format("0x%08" PRIx32, def);
6419 if (r != nullptr) {
6420 if (left > sizeof(struct objc_category_t)) {
6421 outs() << "\n";
6422 memcpy(&objc_category, r, sizeof(struct objc_category_t));
6423 } else {
6424 outs() << " (entends past the end of the section)\n";
6425 memset(&objc_category, '\0', sizeof(struct objc_category_t));
6426 memcpy(&objc_category, r, left);
6428 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6429 swapStruct(objc_category);
6430 print_objc_objc_category_t(&objc_category, &info);
6431 } else {
6432 outs() << "(not in an __OBJC section)\n";
6436 const SectionRef II = get_section(O, "__OBJC", "__image_info");
6437 if (II != SectionRef())
6438 print_image_info(II, &info);
6440 return true;
6443 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
6444 uint32_t size, uint32_t addr) {
6445 SymbolAddressMap AddrMap;
6446 CreateSymbolAddressMap(O, &AddrMap);
6448 std::vector<SectionRef> Sections;
6449 for (const SectionRef &Section : O->sections())
6450 Sections.push_back(Section);
6452 struct DisassembleInfo info(O, &AddrMap, &Sections, true);
6454 const char *p;
6455 struct objc_protocol_t protocol;
6456 uint32_t left, paddr;
6457 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
6458 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
6459 left = size - (p - sect);
6460 if (left < sizeof(struct objc_protocol_t)) {
6461 outs() << "Protocol extends past end of __protocol section\n";
6462 memcpy(&protocol, p, left);
6463 } else
6464 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
6465 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6466 swapStruct(protocol);
6467 paddr = addr + (p - sect);
6468 outs() << "Protocol " << format("0x%" PRIx32, paddr);
6469 if (print_protocol(paddr, 0, &info))
6470 outs() << "(not in an __OBJC section)\n";
6474 #ifdef HAVE_LIBXAR
6475 inline void swapStruct(struct xar_header &xar) {
6476 sys::swapByteOrder(xar.magic);
6477 sys::swapByteOrder(xar.size);
6478 sys::swapByteOrder(xar.version);
6479 sys::swapByteOrder(xar.toc_length_compressed);
6480 sys::swapByteOrder(xar.toc_length_uncompressed);
6481 sys::swapByteOrder(xar.cksum_alg);
6484 static void PrintModeVerbose(uint32_t mode) {
6485 switch(mode & S_IFMT){
6486 case S_IFDIR:
6487 outs() << "d";
6488 break;
6489 case S_IFCHR:
6490 outs() << "c";
6491 break;
6492 case S_IFBLK:
6493 outs() << "b";
6494 break;
6495 case S_IFREG:
6496 outs() << "-";
6497 break;
6498 case S_IFLNK:
6499 outs() << "l";
6500 break;
6501 case S_IFSOCK:
6502 outs() << "s";
6503 break;
6504 default:
6505 outs() << "?";
6506 break;
6509 /* owner permissions */
6510 if(mode & S_IREAD)
6511 outs() << "r";
6512 else
6513 outs() << "-";
6514 if(mode & S_IWRITE)
6515 outs() << "w";
6516 else
6517 outs() << "-";
6518 if(mode & S_ISUID)
6519 outs() << "s";
6520 else if(mode & S_IEXEC)
6521 outs() << "x";
6522 else
6523 outs() << "-";
6525 /* group permissions */
6526 if(mode & (S_IREAD >> 3))
6527 outs() << "r";
6528 else
6529 outs() << "-";
6530 if(mode & (S_IWRITE >> 3))
6531 outs() << "w";
6532 else
6533 outs() << "-";
6534 if(mode & S_ISGID)
6535 outs() << "s";
6536 else if(mode & (S_IEXEC >> 3))
6537 outs() << "x";
6538 else
6539 outs() << "-";
6541 /* other permissions */
6542 if(mode & (S_IREAD >> 6))
6543 outs() << "r";
6544 else
6545 outs() << "-";
6546 if(mode & (S_IWRITE >> 6))
6547 outs() << "w";
6548 else
6549 outs() << "-";
6550 if(mode & S_ISVTX)
6551 outs() << "t";
6552 else if(mode & (S_IEXEC >> 6))
6553 outs() << "x";
6554 else
6555 outs() << "-";
6558 static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
6559 xar_file_t xf;
6560 const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
6561 char *endp;
6562 uint32_t mode_value;
6564 ScopedXarIter xi;
6565 if (!xi) {
6566 WithColor::error(errs(), "llvm-objdump")
6567 << "can't obtain an xar iterator for xar archive " << XarFilename
6568 << "\n";
6569 return;
6572 // Go through the xar's files.
6573 for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
6574 ScopedXarIter xp;
6575 if(!xp){
6576 WithColor::error(errs(), "llvm-objdump")
6577 << "can't obtain an xar iterator for xar archive " << XarFilename
6578 << "\n";
6579 return;
6581 type = nullptr;
6582 mode = nullptr;
6583 user = nullptr;
6584 group = nullptr;
6585 size = nullptr;
6586 mtime = nullptr;
6587 name = nullptr;
6588 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
6589 const char *val = nullptr;
6590 xar_prop_get(xf, key, &val);
6591 #if 0 // Useful for debugging.
6592 outs() << "key: " << key << " value: " << val << "\n";
6593 #endif
6594 if(strcmp(key, "type") == 0)
6595 type = val;
6596 if(strcmp(key, "mode") == 0)
6597 mode = val;
6598 if(strcmp(key, "user") == 0)
6599 user = val;
6600 if(strcmp(key, "group") == 0)
6601 group = val;
6602 if(strcmp(key, "data/size") == 0)
6603 size = val;
6604 if(strcmp(key, "mtime") == 0)
6605 mtime = val;
6606 if(strcmp(key, "name") == 0)
6607 name = val;
6609 if(mode != nullptr){
6610 mode_value = strtoul(mode, &endp, 8);
6611 if(*endp != '\0')
6612 outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
6613 if(strcmp(type, "file") == 0)
6614 mode_value |= S_IFREG;
6615 PrintModeVerbose(mode_value);
6616 outs() << " ";
6618 if(user != nullptr)
6619 outs() << format("%10s/", user);
6620 if(group != nullptr)
6621 outs() << format("%-10s ", group);
6622 if(size != nullptr)
6623 outs() << format("%7s ", size);
6624 if(mtime != nullptr){
6625 for(m = mtime; *m != 'T' && *m != '\0'; m++)
6626 outs() << *m;
6627 if(*m == 'T')
6628 m++;
6629 outs() << " ";
6630 for( ; *m != 'Z' && *m != '\0'; m++)
6631 outs() << *m;
6632 outs() << " ";
6634 if(name != nullptr)
6635 outs() << name;
6636 outs() << "\n";
6640 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
6641 uint32_t size, bool verbose,
6642 bool PrintXarHeader, bool PrintXarFileHeaders,
6643 std::string XarMemberName) {
6644 if(size < sizeof(struct xar_header)) {
6645 outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
6646 "of struct xar_header)\n";
6647 return;
6649 struct xar_header XarHeader;
6650 memcpy(&XarHeader, sect, sizeof(struct xar_header));
6651 if (sys::IsLittleEndianHost)
6652 swapStruct(XarHeader);
6653 if (PrintXarHeader) {
6654 if (!XarMemberName.empty())
6655 outs() << "In xar member " << XarMemberName << ": ";
6656 else
6657 outs() << "For (__LLVM,__bundle) section: ";
6658 outs() << "xar header\n";
6659 if (XarHeader.magic == XAR_HEADER_MAGIC)
6660 outs() << " magic XAR_HEADER_MAGIC\n";
6661 else
6662 outs() << " magic "
6663 << format_hex(XarHeader.magic, 10, true)
6664 << " (not XAR_HEADER_MAGIC)\n";
6665 outs() << " size " << XarHeader.size << "\n";
6666 outs() << " version " << XarHeader.version << "\n";
6667 outs() << " toc_length_compressed " << XarHeader.toc_length_compressed
6668 << "\n";
6669 outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
6670 << "\n";
6671 outs() << " cksum_alg ";
6672 switch (XarHeader.cksum_alg) {
6673 case XAR_CKSUM_NONE:
6674 outs() << "XAR_CKSUM_NONE\n";
6675 break;
6676 case XAR_CKSUM_SHA1:
6677 outs() << "XAR_CKSUM_SHA1\n";
6678 break;
6679 case XAR_CKSUM_MD5:
6680 outs() << "XAR_CKSUM_MD5\n";
6681 break;
6682 #ifdef XAR_CKSUM_SHA256
6683 case XAR_CKSUM_SHA256:
6684 outs() << "XAR_CKSUM_SHA256\n";
6685 break;
6686 #endif
6687 #ifdef XAR_CKSUM_SHA512
6688 case XAR_CKSUM_SHA512:
6689 outs() << "XAR_CKSUM_SHA512\n";
6690 break;
6691 #endif
6692 default:
6693 outs() << XarHeader.cksum_alg << "\n";
6697 SmallString<128> XarFilename;
6698 int FD;
6699 std::error_code XarEC =
6700 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
6701 if (XarEC) {
6702 WithColor::error(errs(), "llvm-objdump") << XarEC.message() << "\n";
6703 return;
6705 ToolOutputFile XarFile(XarFilename, FD);
6706 raw_fd_ostream &XarOut = XarFile.os();
6707 StringRef XarContents(sect, size);
6708 XarOut << XarContents;
6709 XarOut.close();
6710 if (XarOut.has_error())
6711 return;
6713 ScopedXarFile xar(XarFilename.c_str(), READ);
6714 if (!xar) {
6715 WithColor::error(errs(), "llvm-objdump")
6716 << "can't create temporary xar archive " << XarFilename << "\n";
6717 return;
6720 SmallString<128> TocFilename;
6721 std::error_code TocEC =
6722 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
6723 if (TocEC) {
6724 WithColor::error(errs(), "llvm-objdump") << TocEC.message() << "\n";
6725 return;
6727 xar_serialize(xar, TocFilename.c_str());
6729 if (PrintXarFileHeaders) {
6730 if (!XarMemberName.empty())
6731 outs() << "In xar member " << XarMemberName << ": ";
6732 else
6733 outs() << "For (__LLVM,__bundle) section: ";
6734 outs() << "xar archive files:\n";
6735 PrintXarFilesSummary(XarFilename.c_str(), xar);
6738 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
6739 MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
6740 if (std::error_code EC = FileOrErr.getError()) {
6741 WithColor::error(errs(), "llvm-objdump") << EC.message() << "\n";
6742 return;
6744 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
6746 if (!XarMemberName.empty())
6747 outs() << "In xar member " << XarMemberName << ": ";
6748 else
6749 outs() << "For (__LLVM,__bundle) section: ";
6750 outs() << "xar table of contents:\n";
6751 outs() << Buffer->getBuffer() << "\n";
6753 // TODO: Go through the xar's files.
6754 ScopedXarIter xi;
6755 if(!xi){
6756 WithColor::error(errs(), "llvm-objdump")
6757 << "can't obtain an xar iterator for xar archive "
6758 << XarFilename.c_str() << "\n";
6759 return;
6761 for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
6762 const char *key;
6763 const char *member_name, *member_type, *member_size_string;
6764 size_t member_size;
6766 ScopedXarIter xp;
6767 if(!xp){
6768 WithColor::error(errs(), "llvm-objdump")
6769 << "can't obtain an xar iterator for xar archive "
6770 << XarFilename.c_str() << "\n";
6771 return;
6773 member_name = NULL;
6774 member_type = NULL;
6775 member_size_string = NULL;
6776 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
6777 const char *val = nullptr;
6778 xar_prop_get(xf, key, &val);
6779 #if 0 // Useful for debugging.
6780 outs() << "key: " << key << " value: " << val << "\n";
6781 #endif
6782 if (strcmp(key, "name") == 0)
6783 member_name = val;
6784 if (strcmp(key, "type") == 0)
6785 member_type = val;
6786 if (strcmp(key, "data/size") == 0)
6787 member_size_string = val;
6790 * If we find a file with a name, date/size and type properties
6791 * and with the type being "file" see if that is a xar file.
6793 if (member_name != NULL && member_type != NULL &&
6794 strcmp(member_type, "file") == 0 &&
6795 member_size_string != NULL){
6796 // Extract the file into a buffer.
6797 char *endptr;
6798 member_size = strtoul(member_size_string, &endptr, 10);
6799 if (*endptr == '\0' && member_size != 0) {
6800 char *buffer;
6801 if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
6802 #if 0 // Useful for debugging.
6803 outs() << "xar member: " << member_name << " extracted\n";
6804 #endif
6805 // Set the XarMemberName we want to see printed in the header.
6806 std::string OldXarMemberName;
6807 // If XarMemberName is already set this is nested. So
6808 // save the old name and create the nested name.
6809 if (!XarMemberName.empty()) {
6810 OldXarMemberName = XarMemberName;
6811 XarMemberName =
6812 (Twine("[") + XarMemberName + "]" + member_name).str();
6813 } else {
6814 OldXarMemberName = "";
6815 XarMemberName = member_name;
6817 // See if this is could be a xar file (nested).
6818 if (member_size >= sizeof(struct xar_header)) {
6819 #if 0 // Useful for debugging.
6820 outs() << "could be a xar file: " << member_name << "\n";
6821 #endif
6822 memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
6823 if (sys::IsLittleEndianHost)
6824 swapStruct(XarHeader);
6825 if (XarHeader.magic == XAR_HEADER_MAGIC)
6826 DumpBitcodeSection(O, buffer, member_size, verbose,
6827 PrintXarHeader, PrintXarFileHeaders,
6828 XarMemberName);
6830 XarMemberName = OldXarMemberName;
6831 delete buffer;
6837 #endif // defined(HAVE_LIBXAR)
6839 static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
6840 if (O->is64Bit())
6841 printObjc2_64bit_MetaData(O, verbose);
6842 else {
6843 MachO::mach_header H;
6844 H = O->getHeader();
6845 if (H.cputype == MachO::CPU_TYPE_ARM)
6846 printObjc2_32bit_MetaData(O, verbose);
6847 else {
6848 // This is the 32-bit non-arm cputype case. Which is normally
6849 // the first Objective-C ABI. But it may be the case of a
6850 // binary for the iOS simulator which is the second Objective-C
6851 // ABI. In that case printObjc1_32bit_MetaData() will determine that
6852 // and return false.
6853 if (!printObjc1_32bit_MetaData(O, verbose))
6854 printObjc2_32bit_MetaData(O, verbose);
6859 // GuessLiteralPointer returns a string which for the item in the Mach-O file
6860 // for the address passed in as ReferenceValue for printing as a comment with
6861 // the instruction and also returns the corresponding type of that item
6862 // indirectly through ReferenceType.
6864 // If ReferenceValue is an address of literal cstring then a pointer to the
6865 // cstring is returned and ReferenceType is set to
6866 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6868 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6869 // Class ref that name is returned and the ReferenceType is set accordingly.
6871 // Lastly, literals which are Symbol address in a literal pool are looked for
6872 // and if found the symbol name is returned and ReferenceType is set to
6873 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6875 // If there is no item in the Mach-O file for the address passed in as
6876 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
6877 static const char *GuessLiteralPointer(uint64_t ReferenceValue,
6878 uint64_t ReferencePC,
6879 uint64_t *ReferenceType,
6880 struct DisassembleInfo *info) {
6881 // First see if there is an external relocation entry at the ReferencePC.
6882 if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
6883 uint64_t sect_addr = info->S.getAddress();
6884 uint64_t sect_offset = ReferencePC - sect_addr;
6885 bool reloc_found = false;
6886 DataRefImpl Rel;
6887 MachO::any_relocation_info RE;
6888 bool isExtern = false;
6889 SymbolRef Symbol;
6890 for (const RelocationRef &Reloc : info->S.relocations()) {
6891 uint64_t RelocOffset = Reloc.getOffset();
6892 if (RelocOffset == sect_offset) {
6893 Rel = Reloc.getRawDataRefImpl();
6894 RE = info->O->getRelocation(Rel);
6895 if (info->O->isRelocationScattered(RE))
6896 continue;
6897 isExtern = info->O->getPlainRelocationExternal(RE);
6898 if (isExtern) {
6899 symbol_iterator RelocSym = Reloc.getSymbol();
6900 Symbol = *RelocSym;
6902 reloc_found = true;
6903 break;
6906 // If there is an external relocation entry for a symbol in a section
6907 // then used that symbol's value for the value of the reference.
6908 if (reloc_found && isExtern) {
6909 if (info->O->getAnyRelocationPCRel(RE)) {
6910 unsigned Type = info->O->getAnyRelocationType(RE);
6911 if (Type == MachO::X86_64_RELOC_SIGNED) {
6912 ReferenceValue = Symbol.getValue();
6918 // Look for literals such as Objective-C CFStrings refs, Selector refs,
6919 // Message refs and Class refs.
6920 bool classref, selref, msgref, cfstring;
6921 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
6922 selref, msgref, cfstring);
6923 if (classref && pointer_value == 0) {
6924 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6925 // And the pointer_value in that section is typically zero as it will be
6926 // set by dyld as part of the "bind information".
6927 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
6928 if (name != nullptr) {
6929 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6930 const char *class_name = strrchr(name, '$');
6931 if (class_name != nullptr && class_name[1] == '_' &&
6932 class_name[2] != '\0') {
6933 info->class_name = class_name + 2;
6934 return name;
6939 if (classref) {
6940 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6941 const char *name =
6942 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
6943 if (name != nullptr)
6944 info->class_name = name;
6945 else
6946 name = "bad class ref";
6947 return name;
6950 if (cfstring) {
6951 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
6952 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
6953 return name;
6956 if (selref && pointer_value == 0)
6957 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
6959 if (pointer_value != 0)
6960 ReferenceValue = pointer_value;
6962 const char *name = GuessCstringPointer(ReferenceValue, info);
6963 if (name) {
6964 if (pointer_value != 0 && selref) {
6965 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
6966 info->selector_name = name;
6967 } else if (pointer_value != 0 && msgref) {
6968 info->class_name = nullptr;
6969 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
6970 info->selector_name = name;
6971 } else
6972 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
6973 return name;
6976 // Lastly look for an indirect symbol with this ReferenceValue which is in
6977 // a literal pool. If found return that symbol name.
6978 name = GuessIndirectSymbol(ReferenceValue, info);
6979 if (name) {
6980 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
6981 return name;
6984 return nullptr;
6987 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
6988 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
6989 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6990 // is created and returns the symbol name that matches the ReferenceValue or
6991 // nullptr if none. The ReferenceType is passed in for the IN type of
6992 // reference the instruction is making from the values in defined in the header
6993 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
6994 // Out type and the ReferenceName will also be set which is added as a comment
6995 // to the disassembled instruction.
6997 // If the symbol name is a C++ mangled name then the demangled name is
6998 // returned through ReferenceName and ReferenceType is set to
6999 // LLVMDisassembler_ReferenceType_DeMangled_Name .
7001 // When this is called to get a symbol name for a branch target then the
7002 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
7003 // SymbolValue will be looked for in the indirect symbol table to determine if
7004 // it is an address for a symbol stub. If so then the symbol name for that
7005 // stub is returned indirectly through ReferenceName and then ReferenceType is
7006 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
7008 // When this is called with an value loaded via a PC relative load then
7009 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
7010 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
7011 // or an Objective-C meta data reference. If so the output ReferenceType is
7012 // set to correspond to that as well as setting the ReferenceName.
7013 static const char *SymbolizerSymbolLookUp(void *DisInfo,
7014 uint64_t ReferenceValue,
7015 uint64_t *ReferenceType,
7016 uint64_t ReferencePC,
7017 const char **ReferenceName) {
7018 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
7019 // If no verbose symbolic information is wanted then just return nullptr.
7020 if (!info->verbose) {
7021 *ReferenceName = nullptr;
7022 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7023 return nullptr;
7026 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
7028 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
7029 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
7030 if (*ReferenceName != nullptr) {
7031 method_reference(info, ReferenceType, ReferenceName);
7032 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
7033 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
7034 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
7035 if (info->demangled_name != nullptr)
7036 free(info->demangled_name);
7037 int status;
7038 info->demangled_name =
7039 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
7040 if (info->demangled_name != nullptr) {
7041 *ReferenceName = info->demangled_name;
7042 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
7043 } else
7044 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7045 } else
7046 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7047 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
7048 *ReferenceName =
7049 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
7050 if (*ReferenceName)
7051 method_reference(info, ReferenceType, ReferenceName);
7052 else
7053 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7054 // If this is arm64 and the reference is an adrp instruction save the
7055 // instruction, passed in ReferenceValue and the address of the instruction
7056 // for use later if we see and add immediate instruction.
7057 } else if (info->O->getArch() == Triple::aarch64 &&
7058 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
7059 info->adrp_inst = ReferenceValue;
7060 info->adrp_addr = ReferencePC;
7061 SymbolName = nullptr;
7062 *ReferenceName = nullptr;
7063 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7064 // If this is arm64 and reference is an add immediate instruction and we
7065 // have
7066 // seen an adrp instruction just before it and the adrp's Xd register
7067 // matches
7068 // this add's Xn register reconstruct the value being referenced and look to
7069 // see if it is a literal pointer. Note the add immediate instruction is
7070 // passed in ReferenceValue.
7071 } else if (info->O->getArch() == Triple::aarch64 &&
7072 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
7073 ReferencePC - 4 == info->adrp_addr &&
7074 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
7075 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
7076 uint32_t addxri_inst;
7077 uint64_t adrp_imm, addxri_imm;
7079 adrp_imm =
7080 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
7081 if (info->adrp_inst & 0x0200000)
7082 adrp_imm |= 0xfffffffffc000000LL;
7084 addxri_inst = ReferenceValue;
7085 addxri_imm = (addxri_inst >> 10) & 0xfff;
7086 if (((addxri_inst >> 22) & 0x3) == 1)
7087 addxri_imm <<= 12;
7089 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
7090 (adrp_imm << 12) + addxri_imm;
7092 *ReferenceName =
7093 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
7094 if (*ReferenceName == nullptr)
7095 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7096 // If this is arm64 and the reference is a load register instruction and we
7097 // have seen an adrp instruction just before it and the adrp's Xd register
7098 // matches this add's Xn register reconstruct the value being referenced and
7099 // look to see if it is a literal pointer. Note the load register
7100 // instruction is passed in ReferenceValue.
7101 } else if (info->O->getArch() == Triple::aarch64 &&
7102 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
7103 ReferencePC - 4 == info->adrp_addr &&
7104 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
7105 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
7106 uint32_t ldrxui_inst;
7107 uint64_t adrp_imm, ldrxui_imm;
7109 adrp_imm =
7110 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
7111 if (info->adrp_inst & 0x0200000)
7112 adrp_imm |= 0xfffffffffc000000LL;
7114 ldrxui_inst = ReferenceValue;
7115 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
7117 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
7118 (adrp_imm << 12) + (ldrxui_imm << 3);
7120 *ReferenceName =
7121 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
7122 if (*ReferenceName == nullptr)
7123 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7125 // If this arm64 and is an load register (PC-relative) instruction the
7126 // ReferenceValue is the PC plus the immediate value.
7127 else if (info->O->getArch() == Triple::aarch64 &&
7128 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
7129 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
7130 *ReferenceName =
7131 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
7132 if (*ReferenceName == nullptr)
7133 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7134 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
7135 if (info->demangled_name != nullptr)
7136 free(info->demangled_name);
7137 int status;
7138 info->demangled_name =
7139 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
7140 if (info->demangled_name != nullptr) {
7141 *ReferenceName = info->demangled_name;
7142 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
7145 else {
7146 *ReferenceName = nullptr;
7147 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7150 return SymbolName;
7153 /// Emits the comments that are stored in the CommentStream.
7154 /// Each comment in the CommentStream must end with a newline.
7155 static void emitComments(raw_svector_ostream &CommentStream,
7156 SmallString<128> &CommentsToEmit,
7157 formatted_raw_ostream &FormattedOS,
7158 const MCAsmInfo &MAI) {
7159 // Flush the stream before taking its content.
7160 StringRef Comments = CommentsToEmit.str();
7161 // Get the default information for printing a comment.
7162 StringRef CommentBegin = MAI.getCommentString();
7163 unsigned CommentColumn = MAI.getCommentColumn();
7164 bool IsFirst = true;
7165 while (!Comments.empty()) {
7166 if (!IsFirst)
7167 FormattedOS << '\n';
7168 // Emit a line of comments.
7169 FormattedOS.PadToColumn(CommentColumn);
7170 size_t Position = Comments.find('\n');
7171 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
7172 // Move after the newline character.
7173 Comments = Comments.substr(Position + 1);
7174 IsFirst = false;
7176 FormattedOS.flush();
7178 // Tell the comment stream that the vector changed underneath it.
7179 CommentsToEmit.clear();
7182 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
7183 StringRef DisSegName, StringRef DisSectName) {
7184 const char *McpuDefault = nullptr;
7185 const Target *ThumbTarget = nullptr;
7186 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
7187 if (!TheTarget) {
7188 // GetTarget prints out stuff.
7189 return;
7191 std::string MachOMCPU;
7192 if (MCPU.empty() && McpuDefault)
7193 MachOMCPU = McpuDefault;
7194 else
7195 MachOMCPU = MCPU;
7197 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
7198 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
7199 if (ThumbTarget)
7200 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
7202 // Package up features to be passed to target/subtarget
7203 std::string FeaturesStr;
7204 if (!MAttrs.empty()) {
7205 SubtargetFeatures Features;
7206 for (unsigned i = 0; i != MAttrs.size(); ++i)
7207 Features.AddFeature(MAttrs[i]);
7208 FeaturesStr = Features.getString();
7211 // Set up disassembler.
7212 std::unique_ptr<const MCRegisterInfo> MRI(
7213 TheTarget->createMCRegInfo(TripleName));
7214 std::unique_ptr<const MCAsmInfo> AsmInfo(
7215 TheTarget->createMCAsmInfo(*MRI, TripleName));
7216 std::unique_ptr<const MCSubtargetInfo> STI(
7217 TheTarget->createMCSubtargetInfo(TripleName, MachOMCPU, FeaturesStr));
7218 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
7219 std::unique_ptr<MCDisassembler> DisAsm(
7220 TheTarget->createMCDisassembler(*STI, Ctx));
7221 std::unique_ptr<MCSymbolizer> Symbolizer;
7222 struct DisassembleInfo SymbolizerInfo(nullptr, nullptr, nullptr, false);
7223 std::unique_ptr<MCRelocationInfo> RelInfo(
7224 TheTarget->createMCRelocationInfo(TripleName, Ctx));
7225 if (RelInfo) {
7226 Symbolizer.reset(TheTarget->createMCSymbolizer(
7227 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
7228 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
7229 DisAsm->setSymbolizer(std::move(Symbolizer));
7231 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
7232 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
7233 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
7234 // Set the display preference for hex vs. decimal immediates.
7235 IP->setPrintImmHex(PrintImmHex);
7236 // Comment stream and backing vector.
7237 SmallString<128> CommentsToEmit;
7238 raw_svector_ostream CommentStream(CommentsToEmit);
7239 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
7240 // if it is done then arm64 comments for string literals don't get printed
7241 // and some constant get printed instead and not setting it causes intel
7242 // (32-bit and 64-bit) comments printed with different spacing before the
7243 // comment causing different diffs with the 'C' disassembler library API.
7244 // IP->setCommentStream(CommentStream);
7246 if (!AsmInfo || !STI || !DisAsm || !IP) {
7247 WithColor::error(errs(), "llvm-objdump")
7248 << "couldn't initialize disassembler for target " << TripleName << '\n';
7249 return;
7252 // Set up separate thumb disassembler if needed.
7253 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
7254 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
7255 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
7256 std::unique_ptr<MCDisassembler> ThumbDisAsm;
7257 std::unique_ptr<MCInstPrinter> ThumbIP;
7258 std::unique_ptr<MCContext> ThumbCtx;
7259 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
7260 struct DisassembleInfo ThumbSymbolizerInfo(nullptr, nullptr, nullptr, false);
7261 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
7262 if (ThumbTarget) {
7263 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
7264 ThumbAsmInfo.reset(
7265 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
7266 ThumbSTI.reset(
7267 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MachOMCPU,
7268 FeaturesStr));
7269 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
7270 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
7271 MCContext *PtrThumbCtx = ThumbCtx.get();
7272 ThumbRelInfo.reset(
7273 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
7274 if (ThumbRelInfo) {
7275 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
7276 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
7277 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
7278 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
7280 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
7281 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
7282 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
7283 *ThumbInstrInfo, *ThumbMRI));
7284 // Set the display preference for hex vs. decimal immediates.
7285 ThumbIP->setPrintImmHex(PrintImmHex);
7288 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
7289 WithColor::error(errs(), "llvm-objdump")
7290 << "couldn't initialize disassembler for target " << ThumbTripleName
7291 << '\n';
7292 return;
7295 MachO::mach_header Header = MachOOF->getHeader();
7297 // FIXME: Using the -cfg command line option, this code used to be able to
7298 // annotate relocations with the referenced symbol's name, and if this was
7299 // inside a __[cf]string section, the data it points to. This is now replaced
7300 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
7301 std::vector<SectionRef> Sections;
7302 std::vector<SymbolRef> Symbols;
7303 SmallVector<uint64_t, 8> FoundFns;
7304 uint64_t BaseSegmentAddress = 0;
7306 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
7307 BaseSegmentAddress);
7309 // Sort the symbols by address, just in case they didn't come in that way.
7310 llvm::sort(Symbols, SymbolSorter());
7312 // Build a data in code table that is sorted on by the address of each entry.
7313 uint64_t BaseAddress = 0;
7314 if (Header.filetype == MachO::MH_OBJECT)
7315 BaseAddress = Sections[0].getAddress();
7316 else
7317 BaseAddress = BaseSegmentAddress;
7318 DiceTable Dices;
7319 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
7320 DI != DE; ++DI) {
7321 uint32_t Offset;
7322 DI->getOffset(Offset);
7323 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
7325 array_pod_sort(Dices.begin(), Dices.end());
7327 #ifndef NDEBUG
7328 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
7329 #else
7330 raw_ostream &DebugOut = nulls();
7331 #endif
7333 // Try to find debug info and set up the DIContext for it.
7334 std::unique_ptr<DIContext> diContext;
7335 std::unique_ptr<Binary> DSYMBinary;
7336 std::unique_ptr<MemoryBuffer> DSYMBuf;
7337 if (UseDbg) {
7338 ObjectFile *DbgObj = MachOOF;
7340 // A separate DSym file path was specified, parse it as a macho file,
7341 // get the sections and supply it to the section name parsing machinery.
7342 if (!DSYMFile.empty()) {
7343 std::string DSYMPath(DSYMFile);
7345 // If DSYMPath is a .dSYM directory, append the Mach-O file.
7346 if (llvm::sys::fs::is_directory(DSYMPath) &&
7347 llvm::sys::path::extension(DSYMPath) == ".dSYM") {
7348 SmallString<128> ShortName(llvm::sys::path::filename(DSYMPath));
7349 llvm::sys::path::replace_extension(ShortName, "");
7350 SmallString<1024> FullPath(DSYMPath);
7351 llvm::sys::path::append(FullPath, "Contents", "Resources", "DWARF",
7352 ShortName);
7353 DSYMPath = FullPath.str();
7356 // Load the file.
7357 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
7358 MemoryBuffer::getFileOrSTDIN(DSYMPath);
7359 if (std::error_code EC = BufOrErr.getError()) {
7360 reportError(errorCodeToError(EC), DSYMPath);
7361 return;
7364 // We need to keep the file alive, because we're replacing DbgObj with it.
7365 DSYMBuf = std::move(BufOrErr.get());
7367 Expected<std::unique_ptr<Binary>> BinaryOrErr =
7368 createBinary(DSYMBuf.get()->getMemBufferRef());
7369 if (!BinaryOrErr) {
7370 reportError(BinaryOrErr.takeError(), DSYMPath);
7371 return;
7374 // We need to keep the Binary alive with the buffer
7375 DSYMBinary = std::move(BinaryOrErr.get());
7376 if (ObjectFile *O = dyn_cast<ObjectFile>(DSYMBinary.get())) {
7377 // this is a Mach-O object file, use it
7378 if (MachOObjectFile *MachDSYM = dyn_cast<MachOObjectFile>(&*O)) {
7379 DbgObj = MachDSYM;
7381 else {
7382 WithColor::error(errs(), "llvm-objdump")
7383 << DSYMPath << " is not a Mach-O file type.\n";
7384 return;
7387 else if (auto UB = dyn_cast<MachOUniversalBinary>(DSYMBinary.get())){
7388 // this is a Universal Binary, find a Mach-O for this architecture
7389 uint32_t CPUType, CPUSubType;
7390 const char *ArchFlag;
7391 if (MachOOF->is64Bit()) {
7392 const MachO::mach_header_64 H_64 = MachOOF->getHeader64();
7393 CPUType = H_64.cputype;
7394 CPUSubType = H_64.cpusubtype;
7395 } else {
7396 const MachO::mach_header H = MachOOF->getHeader();
7397 CPUType = H.cputype;
7398 CPUSubType = H.cpusubtype;
7400 Triple T = MachOObjectFile::getArchTriple(CPUType, CPUSubType, nullptr,
7401 &ArchFlag);
7402 Expected<std::unique_ptr<MachOObjectFile>> MachDSYM =
7403 UB->getMachOObjectForArch(ArchFlag);
7404 if (!MachDSYM) {
7405 reportError(MachDSYM.takeError(), DSYMPath);
7406 return;
7409 // We need to keep the Binary alive with the buffer
7410 DbgObj = &*MachDSYM.get();
7411 DSYMBinary = std::move(*MachDSYM);
7413 else {
7414 WithColor::error(errs(), "llvm-objdump")
7415 << DSYMPath << " is not a Mach-O or Universal file type.\n";
7416 return;
7420 // Setup the DIContext
7421 diContext = DWARFContext::create(*DbgObj);
7424 if (FilterSections.empty())
7425 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
7427 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
7428 Expected<StringRef> SecNameOrErr = Sections[SectIdx].getName();
7429 if (!SecNameOrErr) {
7430 consumeError(SecNameOrErr.takeError());
7431 continue;
7433 if (*SecNameOrErr != DisSectName)
7434 continue;
7436 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
7438 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
7439 if (SegmentName != DisSegName)
7440 continue;
7442 StringRef BytesStr =
7443 unwrapOrError(Sections[SectIdx].getContents(), Filename);
7444 ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr);
7445 uint64_t SectAddress = Sections[SectIdx].getAddress();
7447 bool symbolTableWorked = false;
7449 // Create a map of symbol addresses to symbol names for use by
7450 // the SymbolizerSymbolLookUp() routine.
7451 SymbolAddressMap AddrMap;
7452 bool DisSymNameFound = false;
7453 for (const SymbolRef &Symbol : MachOOF->symbols()) {
7454 SymbolRef::Type ST =
7455 unwrapOrError(Symbol.getType(), MachOOF->getFileName());
7456 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
7457 ST == SymbolRef::ST_Other) {
7458 uint64_t Address = Symbol.getValue();
7459 StringRef SymName =
7460 unwrapOrError(Symbol.getName(), MachOOF->getFileName());
7461 AddrMap[Address] = SymName;
7462 if (!DisSymName.empty() && DisSymName == SymName)
7463 DisSymNameFound = true;
7466 if (!DisSymName.empty() && !DisSymNameFound) {
7467 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
7468 return;
7470 // Set up the block of info used by the Symbolizer call backs.
7471 SymbolizerInfo.verbose = !NoSymbolicOperands;
7472 SymbolizerInfo.O = MachOOF;
7473 SymbolizerInfo.S = Sections[SectIdx];
7474 SymbolizerInfo.AddrMap = &AddrMap;
7475 SymbolizerInfo.Sections = &Sections;
7476 // Same for the ThumbSymbolizer
7477 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
7478 ThumbSymbolizerInfo.O = MachOOF;
7479 ThumbSymbolizerInfo.S = Sections[SectIdx];
7480 ThumbSymbolizerInfo.AddrMap = &AddrMap;
7481 ThumbSymbolizerInfo.Sections = &Sections;
7483 unsigned int Arch = MachOOF->getArch();
7485 // Skip all symbols if this is a stubs file.
7486 if (Bytes.empty())
7487 return;
7489 // If the section has symbols but no symbol at the start of the section
7490 // these are used to make sure the bytes before the first symbol are
7491 // disassembled.
7492 bool FirstSymbol = true;
7493 bool FirstSymbolAtSectionStart = true;
7495 // Disassemble symbol by symbol.
7496 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
7497 StringRef SymName =
7498 unwrapOrError(Symbols[SymIdx].getName(), MachOOF->getFileName());
7499 SymbolRef::Type ST =
7500 unwrapOrError(Symbols[SymIdx].getType(), MachOOF->getFileName());
7501 if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
7502 continue;
7504 // Make sure the symbol is defined in this section.
7505 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
7506 if (!containsSym) {
7507 if (!DisSymName.empty() && DisSymName == SymName) {
7508 outs() << "-dis-symname: " << DisSymName << " not in the section\n";
7509 return;
7511 continue;
7513 // The __mh_execute_header is special and we need to deal with that fact
7514 // this symbol is before the start of the (__TEXT,__text) section and at the
7515 // address of the start of the __TEXT segment. This is because this symbol
7516 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
7517 // start of the section in a standard MH_EXECUTE filetype.
7518 if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
7519 outs() << "-dis-symname: __mh_execute_header not in any section\n";
7520 return;
7522 // When this code is trying to disassemble a symbol at a time and in the
7523 // case there is only the __mh_execute_header symbol left as in a stripped
7524 // executable, we need to deal with this by ignoring this symbol so the
7525 // whole section is disassembled and this symbol is then not displayed.
7526 if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
7527 SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
7528 SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
7529 continue;
7531 // If we are only disassembling one symbol see if this is that symbol.
7532 if (!DisSymName.empty() && DisSymName != SymName)
7533 continue;
7535 // Start at the address of the symbol relative to the section's address.
7536 uint64_t SectSize = Sections[SectIdx].getSize();
7537 uint64_t Start = Symbols[SymIdx].getValue();
7538 uint64_t SectionAddress = Sections[SectIdx].getAddress();
7539 Start -= SectionAddress;
7541 if (Start > SectSize) {
7542 outs() << "section data ends, " << SymName
7543 << " lies outside valid range\n";
7544 return;
7547 // Stop disassembling either at the beginning of the next symbol or at
7548 // the end of the section.
7549 bool containsNextSym = false;
7550 uint64_t NextSym = 0;
7551 uint64_t NextSymIdx = SymIdx + 1;
7552 while (Symbols.size() > NextSymIdx) {
7553 SymbolRef::Type NextSymType = unwrapOrError(
7554 Symbols[NextSymIdx].getType(), MachOOF->getFileName());
7555 if (NextSymType == SymbolRef::ST_Function) {
7556 containsNextSym =
7557 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
7558 NextSym = Symbols[NextSymIdx].getValue();
7559 NextSym -= SectionAddress;
7560 break;
7562 ++NextSymIdx;
7565 uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
7566 uint64_t Size;
7568 symbolTableWorked = true;
7570 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
7571 bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
7573 // We only need the dedicated Thumb target if there's a real choice
7574 // (i.e. we're not targeting M-class) and the function is Thumb.
7575 bool UseThumbTarget = IsThumb && ThumbTarget;
7577 // If we are not specifying a symbol to start disassembly with and this
7578 // is the first symbol in the section but not at the start of the section
7579 // then move the disassembly index to the start of the section and
7580 // don't print the symbol name just yet. This is so the bytes before the
7581 // first symbol are disassembled.
7582 uint64_t SymbolStart = Start;
7583 if (DisSymName.empty() && FirstSymbol && Start != 0) {
7584 FirstSymbolAtSectionStart = false;
7585 Start = 0;
7587 else
7588 outs() << SymName << ":\n";
7590 DILineInfo lastLine;
7591 for (uint64_t Index = Start; Index < End; Index += Size) {
7592 MCInst Inst;
7594 // If this is the first symbol in the section and it was not at the
7595 // start of the section, see if we are at its Index now and if so print
7596 // the symbol name.
7597 if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart)
7598 outs() << SymName << ":\n";
7600 uint64_t PC = SectAddress + Index;
7601 if (!NoLeadingAddr) {
7602 if (FullLeadingAddr) {
7603 if (MachOOF->is64Bit())
7604 outs() << format("%016" PRIx64, PC);
7605 else
7606 outs() << format("%08" PRIx64, PC);
7607 } else {
7608 outs() << format("%8" PRIx64 ":", PC);
7611 if (!NoShowRawInsn || Arch == Triple::arm)
7612 outs() << "\t";
7614 if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, Size))
7615 continue;
7617 SmallVector<char, 64> AnnotationsBytes;
7618 raw_svector_ostream Annotations(AnnotationsBytes);
7620 bool gotInst;
7621 if (UseThumbTarget)
7622 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
7623 PC, DebugOut, Annotations);
7624 else
7625 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
7626 DebugOut, Annotations);
7627 if (gotInst) {
7628 if (!NoShowRawInsn || Arch == Triple::arm) {
7629 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
7631 formatted_raw_ostream FormattedOS(outs());
7632 StringRef AnnotationsStr = Annotations.str();
7633 if (UseThumbTarget)
7634 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
7635 else
7636 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
7637 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
7639 // Print debug info.
7640 if (diContext) {
7641 DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx});
7642 // Print valid line info if it changed.
7643 if (dli != lastLine && dli.Line != 0)
7644 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
7645 << dli.Column;
7646 lastLine = dli;
7648 outs() << "\n";
7649 } else {
7650 unsigned int Arch = MachOOF->getArch();
7651 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
7652 outs() << format("\t.byte 0x%02x #bad opcode\n",
7653 *(Bytes.data() + Index) & 0xff);
7654 Size = 1; // skip exactly one illegible byte and move on.
7655 } else if (Arch == Triple::aarch64 ||
7656 (Arch == Triple::arm && !IsThumb)) {
7657 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
7658 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
7659 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
7660 (*(Bytes.data() + Index + 3) & 0xff) << 24;
7661 outs() << format("\t.long\t0x%08x\n", opcode);
7662 Size = 4;
7663 } else if (Arch == Triple::arm) {
7664 assert(IsThumb && "ARM mode should have been dealt with above");
7665 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
7666 (*(Bytes.data() + Index + 1) & 0xff) << 8;
7667 outs() << format("\t.short\t0x%04x\n", opcode);
7668 Size = 2;
7669 } else{
7670 WithColor::warning(errs(), "llvm-objdump")
7671 << "invalid instruction encoding\n";
7672 if (Size == 0)
7673 Size = 1; // skip illegible bytes
7677 // Now that we are done disassembled the first symbol set the bool that
7678 // were doing this to false.
7679 FirstSymbol = false;
7681 if (!symbolTableWorked) {
7682 // Reading the symbol table didn't work, disassemble the whole section.
7683 uint64_t SectAddress = Sections[SectIdx].getAddress();
7684 uint64_t SectSize = Sections[SectIdx].getSize();
7685 uint64_t InstSize;
7686 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
7687 MCInst Inst;
7689 uint64_t PC = SectAddress + Index;
7691 if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, InstSize))
7692 continue;
7694 SmallVector<char, 64> AnnotationsBytes;
7695 raw_svector_ostream Annotations(AnnotationsBytes);
7696 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
7697 DebugOut, Annotations)) {
7698 if (!NoLeadingAddr) {
7699 if (FullLeadingAddr) {
7700 if (MachOOF->is64Bit())
7701 outs() << format("%016" PRIx64, PC);
7702 else
7703 outs() << format("%08" PRIx64, PC);
7704 } else {
7705 outs() << format("%8" PRIx64 ":", PC);
7708 if (!NoShowRawInsn || Arch == Triple::arm) {
7709 outs() << "\t";
7710 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
7712 StringRef AnnotationsStr = Annotations.str();
7713 IP->printInst(&Inst, outs(), AnnotationsStr, *STI);
7714 outs() << "\n";
7715 } else {
7716 unsigned int Arch = MachOOF->getArch();
7717 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
7718 outs() << format("\t.byte 0x%02x #bad opcode\n",
7719 *(Bytes.data() + Index) & 0xff);
7720 InstSize = 1; // skip exactly one illegible byte and move on.
7721 } else {
7722 WithColor::warning(errs(), "llvm-objdump")
7723 << "invalid instruction encoding\n";
7724 if (InstSize == 0)
7725 InstSize = 1; // skip illegible bytes
7730 // The TripleName's need to be reset if we are called again for a different
7731 // archtecture.
7732 TripleName = "";
7733 ThumbTripleName = "";
7735 if (SymbolizerInfo.demangled_name != nullptr)
7736 free(SymbolizerInfo.demangled_name);
7737 if (ThumbSymbolizerInfo.demangled_name != nullptr)
7738 free(ThumbSymbolizerInfo.demangled_name);
7742 //===----------------------------------------------------------------------===//
7743 // __compact_unwind section dumping
7744 //===----------------------------------------------------------------------===//
7746 namespace {
7748 template <typename T>
7749 static uint64_t read(StringRef Contents, ptrdiff_t Offset) {
7750 using llvm::support::little;
7751 using llvm::support::unaligned;
7753 if (Offset + sizeof(T) > Contents.size()) {
7754 outs() << "warning: attempt to read past end of buffer\n";
7755 return T();
7758 uint64_t Val =
7759 support::endian::read<T, little, unaligned>(Contents.data() + Offset);
7760 return Val;
7763 template <typename T>
7764 static uint64_t readNext(StringRef Contents, ptrdiff_t &Offset) {
7765 T Val = read<T>(Contents, Offset);
7766 Offset += sizeof(T);
7767 return Val;
7770 struct CompactUnwindEntry {
7771 uint32_t OffsetInSection;
7773 uint64_t FunctionAddr;
7774 uint32_t Length;
7775 uint32_t CompactEncoding;
7776 uint64_t PersonalityAddr;
7777 uint64_t LSDAAddr;
7779 RelocationRef FunctionReloc;
7780 RelocationRef PersonalityReloc;
7781 RelocationRef LSDAReloc;
7783 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
7784 : OffsetInSection(Offset) {
7785 if (Is64)
7786 read<uint64_t>(Contents, Offset);
7787 else
7788 read<uint32_t>(Contents, Offset);
7791 private:
7792 template <typename UIntPtr> void read(StringRef Contents, ptrdiff_t Offset) {
7793 FunctionAddr = readNext<UIntPtr>(Contents, Offset);
7794 Length = readNext<uint32_t>(Contents, Offset);
7795 CompactEncoding = readNext<uint32_t>(Contents, Offset);
7796 PersonalityAddr = readNext<UIntPtr>(Contents, Offset);
7797 LSDAAddr = readNext<UIntPtr>(Contents, Offset);
7802 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
7803 /// and data being relocated, determine the best base Name and Addend to use for
7804 /// display purposes.
7806 /// 1. An Extern relocation will directly reference a symbol (and the data is
7807 /// then already an addend), so use that.
7808 /// 2. Otherwise the data is an offset in the object file's layout; try to find
7809 // a symbol before it in the same section, and use the offset from there.
7810 /// 3. Finally, if all that fails, fall back to an offset from the start of the
7811 /// referenced section.
7812 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
7813 std::map<uint64_t, SymbolRef> &Symbols,
7814 const RelocationRef &Reloc, uint64_t Addr,
7815 StringRef &Name, uint64_t &Addend) {
7816 if (Reloc.getSymbol() != Obj->symbol_end()) {
7817 Name = unwrapOrError(Reloc.getSymbol()->getName(), Obj->getFileName());
7818 Addend = Addr;
7819 return;
7822 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
7823 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
7825 uint64_t SectionAddr = RelocSection.getAddress();
7827 auto Sym = Symbols.upper_bound(Addr);
7828 if (Sym == Symbols.begin()) {
7829 // The first symbol in the object is after this reference, the best we can
7830 // do is section-relative notation.
7831 if (Expected<StringRef> NameOrErr = RelocSection.getName())
7832 Name = *NameOrErr;
7833 else
7834 consumeError(NameOrErr.takeError());
7836 Addend = Addr - SectionAddr;
7837 return;
7840 // Go back one so that SymbolAddress <= Addr.
7841 --Sym;
7843 section_iterator SymSection =
7844 unwrapOrError(Sym->second.getSection(), Obj->getFileName());
7845 if (RelocSection == *SymSection) {
7846 // There's a valid symbol in the same section before this reference.
7847 Name = unwrapOrError(Sym->second.getName(), Obj->getFileName());
7848 Addend = Addr - Sym->first;
7849 return;
7852 // There is a symbol before this reference, but it's in a different
7853 // section. Probably not helpful to mention it, so use the section name.
7854 if (Expected<StringRef> NameOrErr = RelocSection.getName())
7855 Name = *NameOrErr;
7856 else
7857 consumeError(NameOrErr.takeError());
7859 Addend = Addr - SectionAddr;
7862 static void printUnwindRelocDest(const MachOObjectFile *Obj,
7863 std::map<uint64_t, SymbolRef> &Symbols,
7864 const RelocationRef &Reloc, uint64_t Addr) {
7865 StringRef Name;
7866 uint64_t Addend;
7868 if (!Reloc.getObject())
7869 return;
7871 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
7873 outs() << Name;
7874 if (Addend)
7875 outs() << " + " << format("0x%" PRIx64, Addend);
7878 static void
7879 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
7880 std::map<uint64_t, SymbolRef> &Symbols,
7881 const SectionRef &CompactUnwind) {
7883 if (!Obj->isLittleEndian()) {
7884 outs() << "Skipping big-endian __compact_unwind section\n";
7885 return;
7888 bool Is64 = Obj->is64Bit();
7889 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
7890 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
7892 StringRef Contents =
7893 unwrapOrError(CompactUnwind.getContents(), Obj->getFileName());
7894 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
7896 // First populate the initial raw offsets, encodings and so on from the entry.
7897 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
7898 CompactUnwindEntry Entry(Contents, Offset, Is64);
7899 CompactUnwinds.push_back(Entry);
7902 // Next we need to look at the relocations to find out what objects are
7903 // actually being referred to.
7904 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
7905 uint64_t RelocAddress = Reloc.getOffset();
7907 uint32_t EntryIdx = RelocAddress / EntrySize;
7908 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
7909 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
7911 if (OffsetInEntry == 0)
7912 Entry.FunctionReloc = Reloc;
7913 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
7914 Entry.PersonalityReloc = Reloc;
7915 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
7916 Entry.LSDAReloc = Reloc;
7917 else {
7918 outs() << "Invalid relocation in __compact_unwind section\n";
7919 return;
7923 // Finally, we're ready to print the data we've gathered.
7924 outs() << "Contents of __compact_unwind section:\n";
7925 for (auto &Entry : CompactUnwinds) {
7926 outs() << " Entry at offset "
7927 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
7929 // 1. Start of the region this entry applies to.
7930 outs() << " start: " << format("0x%" PRIx64,
7931 Entry.FunctionAddr) << ' ';
7932 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
7933 outs() << '\n';
7935 // 2. Length of the region this entry applies to.
7936 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
7937 << '\n';
7938 // 3. The 32-bit compact encoding.
7939 outs() << " compact encoding: "
7940 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
7942 // 4. The personality function, if present.
7943 if (Entry.PersonalityReloc.getObject()) {
7944 outs() << " personality function: "
7945 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
7946 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
7947 Entry.PersonalityAddr);
7948 outs() << '\n';
7951 // 5. This entry's language-specific data area.
7952 if (Entry.LSDAReloc.getObject()) {
7953 outs() << " LSDA: " << format("0x%" PRIx64,
7954 Entry.LSDAAddr) << ' ';
7955 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
7956 outs() << '\n';
7961 //===----------------------------------------------------------------------===//
7962 // __unwind_info section dumping
7963 //===----------------------------------------------------------------------===//
7965 static void printRegularSecondLevelUnwindPage(StringRef PageData) {
7966 ptrdiff_t Pos = 0;
7967 uint32_t Kind = readNext<uint32_t>(PageData, Pos);
7968 (void)Kind;
7969 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
7971 uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos);
7972 uint16_t NumEntries = readNext<uint16_t>(PageData, Pos);
7974 Pos = EntriesStart;
7975 for (unsigned i = 0; i < NumEntries; ++i) {
7976 uint32_t FunctionOffset = readNext<uint32_t>(PageData, Pos);
7977 uint32_t Encoding = readNext<uint32_t>(PageData, Pos);
7979 outs() << " [" << i << "]: "
7980 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7981 << ", "
7982 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
7986 static void printCompressedSecondLevelUnwindPage(
7987 StringRef PageData, uint32_t FunctionBase,
7988 const SmallVectorImpl<uint32_t> &CommonEncodings) {
7989 ptrdiff_t Pos = 0;
7990 uint32_t Kind = readNext<uint32_t>(PageData, Pos);
7991 (void)Kind;
7992 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
7994 uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos);
7995 uint16_t NumEntries = readNext<uint16_t>(PageData, Pos);
7997 uint16_t EncodingsStart = readNext<uint16_t>(PageData, Pos);
7998 readNext<uint16_t>(PageData, Pos);
7999 StringRef PageEncodings = PageData.substr(EncodingsStart, StringRef::npos);
8001 Pos = EntriesStart;
8002 for (unsigned i = 0; i < NumEntries; ++i) {
8003 uint32_t Entry = readNext<uint32_t>(PageData, Pos);
8004 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
8005 uint32_t EncodingIdx = Entry >> 24;
8007 uint32_t Encoding;
8008 if (EncodingIdx < CommonEncodings.size())
8009 Encoding = CommonEncodings[EncodingIdx];
8010 else
8011 Encoding = read<uint32_t>(PageEncodings,
8012 sizeof(uint32_t) *
8013 (EncodingIdx - CommonEncodings.size()));
8015 outs() << " [" << i << "]: "
8016 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
8017 << ", "
8018 << "encoding[" << EncodingIdx
8019 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
8023 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
8024 std::map<uint64_t, SymbolRef> &Symbols,
8025 const SectionRef &UnwindInfo) {
8027 if (!Obj->isLittleEndian()) {
8028 outs() << "Skipping big-endian __unwind_info section\n";
8029 return;
8032 outs() << "Contents of __unwind_info section:\n";
8034 StringRef Contents =
8035 unwrapOrError(UnwindInfo.getContents(), Obj->getFileName());
8036 ptrdiff_t Pos = 0;
8038 //===----------------------------------
8039 // Section header
8040 //===----------------------------------
8042 uint32_t Version = readNext<uint32_t>(Contents, Pos);
8043 outs() << " Version: "
8044 << format("0x%" PRIx32, Version) << '\n';
8045 if (Version != 1) {
8046 outs() << " Skipping section with unknown version\n";
8047 return;
8050 uint32_t CommonEncodingsStart = readNext<uint32_t>(Contents, Pos);
8051 outs() << " Common encodings array section offset: "
8052 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
8053 uint32_t NumCommonEncodings = readNext<uint32_t>(Contents, Pos);
8054 outs() << " Number of common encodings in array: "
8055 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
8057 uint32_t PersonalitiesStart = readNext<uint32_t>(Contents, Pos);
8058 outs() << " Personality function array section offset: "
8059 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
8060 uint32_t NumPersonalities = readNext<uint32_t>(Contents, Pos);
8061 outs() << " Number of personality functions in array: "
8062 << format("0x%" PRIx32, NumPersonalities) << '\n';
8064 uint32_t IndicesStart = readNext<uint32_t>(Contents, Pos);
8065 outs() << " Index array section offset: "
8066 << format("0x%" PRIx32, IndicesStart) << '\n';
8067 uint32_t NumIndices = readNext<uint32_t>(Contents, Pos);
8068 outs() << " Number of indices in array: "
8069 << format("0x%" PRIx32, NumIndices) << '\n';
8071 //===----------------------------------
8072 // A shared list of common encodings
8073 //===----------------------------------
8075 // These occupy indices in the range [0, N] whenever an encoding is referenced
8076 // from a compressed 2nd level index table. In practice the linker only
8077 // creates ~128 of these, so that indices are available to embed encodings in
8078 // the 2nd level index.
8080 SmallVector<uint32_t, 64> CommonEncodings;
8081 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
8082 Pos = CommonEncodingsStart;
8083 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
8084 uint32_t Encoding = readNext<uint32_t>(Contents, Pos);
8085 CommonEncodings.push_back(Encoding);
8087 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
8088 << '\n';
8091 //===----------------------------------
8092 // Personality functions used in this executable
8093 //===----------------------------------
8095 // There should be only a handful of these (one per source language,
8096 // roughly). Particularly since they only get 2 bits in the compact encoding.
8098 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
8099 Pos = PersonalitiesStart;
8100 for (unsigned i = 0; i < NumPersonalities; ++i) {
8101 uint32_t PersonalityFn = readNext<uint32_t>(Contents, Pos);
8102 outs() << " personality[" << i + 1
8103 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
8106 //===----------------------------------
8107 // The level 1 index entries
8108 //===----------------------------------
8110 // These specify an approximate place to start searching for the more detailed
8111 // information, sorted by PC.
8113 struct IndexEntry {
8114 uint32_t FunctionOffset;
8115 uint32_t SecondLevelPageStart;
8116 uint32_t LSDAStart;
8119 SmallVector<IndexEntry, 4> IndexEntries;
8121 outs() << " Top level indices: (count = " << NumIndices << ")\n";
8122 Pos = IndicesStart;
8123 for (unsigned i = 0; i < NumIndices; ++i) {
8124 IndexEntry Entry;
8126 Entry.FunctionOffset = readNext<uint32_t>(Contents, Pos);
8127 Entry.SecondLevelPageStart = readNext<uint32_t>(Contents, Pos);
8128 Entry.LSDAStart = readNext<uint32_t>(Contents, Pos);
8129 IndexEntries.push_back(Entry);
8131 outs() << " [" << i << "]: "
8132 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
8133 << ", "
8134 << "2nd level page offset="
8135 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
8136 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
8139 //===----------------------------------
8140 // Next come the LSDA tables
8141 //===----------------------------------
8143 // The LSDA layout is rather implicit: it's a contiguous array of entries from
8144 // the first top-level index's LSDAOffset to the last (sentinel).
8146 outs() << " LSDA descriptors:\n";
8147 Pos = IndexEntries[0].LSDAStart;
8148 const uint32_t LSDASize = 2 * sizeof(uint32_t);
8149 int NumLSDAs =
8150 (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) / LSDASize;
8152 for (int i = 0; i < NumLSDAs; ++i) {
8153 uint32_t FunctionOffset = readNext<uint32_t>(Contents, Pos);
8154 uint32_t LSDAOffset = readNext<uint32_t>(Contents, Pos);
8155 outs() << " [" << i << "]: "
8156 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
8157 << ", "
8158 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
8161 //===----------------------------------
8162 // Finally, the 2nd level indices
8163 //===----------------------------------
8165 // Generally these are 4K in size, and have 2 possible forms:
8166 // + Regular stores up to 511 entries with disparate encodings
8167 // + Compressed stores up to 1021 entries if few enough compact encoding
8168 // values are used.
8169 outs() << " Second level indices:\n";
8170 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
8171 // The final sentinel top-level index has no associated 2nd level page
8172 if (IndexEntries[i].SecondLevelPageStart == 0)
8173 break;
8175 outs() << " Second level index[" << i << "]: "
8176 << "offset in section="
8177 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
8178 << ", "
8179 << "base function offset="
8180 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
8182 Pos = IndexEntries[i].SecondLevelPageStart;
8183 if (Pos + sizeof(uint32_t) > Contents.size()) {
8184 outs() << "warning: invalid offset for second level page: " << Pos << '\n';
8185 continue;
8188 uint32_t Kind =
8189 *reinterpret_cast<const support::ulittle32_t *>(Contents.data() + Pos);
8190 if (Kind == 2)
8191 printRegularSecondLevelUnwindPage(Contents.substr(Pos, 4096));
8192 else if (Kind == 3)
8193 printCompressedSecondLevelUnwindPage(Contents.substr(Pos, 4096),
8194 IndexEntries[i].FunctionOffset,
8195 CommonEncodings);
8196 else
8197 outs() << " Skipping 2nd level page with unknown kind " << Kind
8198 << '\n';
8202 void printMachOUnwindInfo(const MachOObjectFile *Obj) {
8203 std::map<uint64_t, SymbolRef> Symbols;
8204 for (const SymbolRef &SymRef : Obj->symbols()) {
8205 // Discard any undefined or absolute symbols. They're not going to take part
8206 // in the convenience lookup for unwind info and just take up resources.
8207 auto SectOrErr = SymRef.getSection();
8208 if (!SectOrErr) {
8209 // TODO: Actually report errors helpfully.
8210 consumeError(SectOrErr.takeError());
8211 continue;
8213 section_iterator Section = *SectOrErr;
8214 if (Section == Obj->section_end())
8215 continue;
8217 uint64_t Addr = SymRef.getValue();
8218 Symbols.insert(std::make_pair(Addr, SymRef));
8221 for (const SectionRef &Section : Obj->sections()) {
8222 StringRef SectName;
8223 if (Expected<StringRef> NameOrErr = Section.getName())
8224 SectName = *NameOrErr;
8225 else
8226 consumeError(NameOrErr.takeError());
8228 if (SectName == "__compact_unwind")
8229 printMachOCompactUnwindSection(Obj, Symbols, Section);
8230 else if (SectName == "__unwind_info")
8231 printMachOUnwindInfoSection(Obj, Symbols, Section);
8235 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
8236 uint32_t cpusubtype, uint32_t filetype,
8237 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
8238 bool verbose) {
8239 outs() << "Mach header\n";
8240 outs() << " magic cputype cpusubtype caps filetype ncmds "
8241 "sizeofcmds flags\n";
8242 if (verbose) {
8243 if (magic == MachO::MH_MAGIC)
8244 outs() << " MH_MAGIC";
8245 else if (magic == MachO::MH_MAGIC_64)
8246 outs() << "MH_MAGIC_64";
8247 else
8248 outs() << format(" 0x%08" PRIx32, magic);
8249 switch (cputype) {
8250 case MachO::CPU_TYPE_I386:
8251 outs() << " I386";
8252 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8253 case MachO::CPU_SUBTYPE_I386_ALL:
8254 outs() << " ALL";
8255 break;
8256 default:
8257 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8258 break;
8260 break;
8261 case MachO::CPU_TYPE_X86_64:
8262 outs() << " X86_64";
8263 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8264 case MachO::CPU_SUBTYPE_X86_64_ALL:
8265 outs() << " ALL";
8266 break;
8267 case MachO::CPU_SUBTYPE_X86_64_H:
8268 outs() << " Haswell";
8269 break;
8270 default:
8271 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8272 break;
8274 break;
8275 case MachO::CPU_TYPE_ARM:
8276 outs() << " ARM";
8277 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8278 case MachO::CPU_SUBTYPE_ARM_ALL:
8279 outs() << " ALL";
8280 break;
8281 case MachO::CPU_SUBTYPE_ARM_V4T:
8282 outs() << " V4T";
8283 break;
8284 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
8285 outs() << " V5TEJ";
8286 break;
8287 case MachO::CPU_SUBTYPE_ARM_XSCALE:
8288 outs() << " XSCALE";
8289 break;
8290 case MachO::CPU_SUBTYPE_ARM_V6:
8291 outs() << " V6";
8292 break;
8293 case MachO::CPU_SUBTYPE_ARM_V6M:
8294 outs() << " V6M";
8295 break;
8296 case MachO::CPU_SUBTYPE_ARM_V7:
8297 outs() << " V7";
8298 break;
8299 case MachO::CPU_SUBTYPE_ARM_V7EM:
8300 outs() << " V7EM";
8301 break;
8302 case MachO::CPU_SUBTYPE_ARM_V7K:
8303 outs() << " V7K";
8304 break;
8305 case MachO::CPU_SUBTYPE_ARM_V7M:
8306 outs() << " V7M";
8307 break;
8308 case MachO::CPU_SUBTYPE_ARM_V7S:
8309 outs() << " V7S";
8310 break;
8311 default:
8312 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8313 break;
8315 break;
8316 case MachO::CPU_TYPE_ARM64:
8317 outs() << " ARM64";
8318 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8319 case MachO::CPU_SUBTYPE_ARM64_ALL:
8320 outs() << " ALL";
8321 break;
8322 case MachO::CPU_SUBTYPE_ARM64E:
8323 outs() << " E";
8324 break;
8325 default:
8326 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8327 break;
8329 break;
8330 case MachO::CPU_TYPE_ARM64_32:
8331 outs() << " ARM64_32";
8332 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8333 case MachO::CPU_SUBTYPE_ARM64_32_V8:
8334 outs() << " V8";
8335 break;
8336 default:
8337 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8338 break;
8340 break;
8341 case MachO::CPU_TYPE_POWERPC:
8342 outs() << " PPC";
8343 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8344 case MachO::CPU_SUBTYPE_POWERPC_ALL:
8345 outs() << " ALL";
8346 break;
8347 default:
8348 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8349 break;
8351 break;
8352 case MachO::CPU_TYPE_POWERPC64:
8353 outs() << " PPC64";
8354 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8355 case MachO::CPU_SUBTYPE_POWERPC_ALL:
8356 outs() << " ALL";
8357 break;
8358 default:
8359 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8360 break;
8362 break;
8363 default:
8364 outs() << format(" %7d", cputype);
8365 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8366 break;
8368 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
8369 outs() << " LIB64";
8370 } else {
8371 outs() << format(" 0x%02" PRIx32,
8372 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
8374 switch (filetype) {
8375 case MachO::MH_OBJECT:
8376 outs() << " OBJECT";
8377 break;
8378 case MachO::MH_EXECUTE:
8379 outs() << " EXECUTE";
8380 break;
8381 case MachO::MH_FVMLIB:
8382 outs() << " FVMLIB";
8383 break;
8384 case MachO::MH_CORE:
8385 outs() << " CORE";
8386 break;
8387 case MachO::MH_PRELOAD:
8388 outs() << " PRELOAD";
8389 break;
8390 case MachO::MH_DYLIB:
8391 outs() << " DYLIB";
8392 break;
8393 case MachO::MH_DYLIB_STUB:
8394 outs() << " DYLIB_STUB";
8395 break;
8396 case MachO::MH_DYLINKER:
8397 outs() << " DYLINKER";
8398 break;
8399 case MachO::MH_BUNDLE:
8400 outs() << " BUNDLE";
8401 break;
8402 case MachO::MH_DSYM:
8403 outs() << " DSYM";
8404 break;
8405 case MachO::MH_KEXT_BUNDLE:
8406 outs() << " KEXTBUNDLE";
8407 break;
8408 default:
8409 outs() << format(" %10u", filetype);
8410 break;
8412 outs() << format(" %5u", ncmds);
8413 outs() << format(" %10u", sizeofcmds);
8414 uint32_t f = flags;
8415 if (f & MachO::MH_NOUNDEFS) {
8416 outs() << " NOUNDEFS";
8417 f &= ~MachO::MH_NOUNDEFS;
8419 if (f & MachO::MH_INCRLINK) {
8420 outs() << " INCRLINK";
8421 f &= ~MachO::MH_INCRLINK;
8423 if (f & MachO::MH_DYLDLINK) {
8424 outs() << " DYLDLINK";
8425 f &= ~MachO::MH_DYLDLINK;
8427 if (f & MachO::MH_BINDATLOAD) {
8428 outs() << " BINDATLOAD";
8429 f &= ~MachO::MH_BINDATLOAD;
8431 if (f & MachO::MH_PREBOUND) {
8432 outs() << " PREBOUND";
8433 f &= ~MachO::MH_PREBOUND;
8435 if (f & MachO::MH_SPLIT_SEGS) {
8436 outs() << " SPLIT_SEGS";
8437 f &= ~MachO::MH_SPLIT_SEGS;
8439 if (f & MachO::MH_LAZY_INIT) {
8440 outs() << " LAZY_INIT";
8441 f &= ~MachO::MH_LAZY_INIT;
8443 if (f & MachO::MH_TWOLEVEL) {
8444 outs() << " TWOLEVEL";
8445 f &= ~MachO::MH_TWOLEVEL;
8447 if (f & MachO::MH_FORCE_FLAT) {
8448 outs() << " FORCE_FLAT";
8449 f &= ~MachO::MH_FORCE_FLAT;
8451 if (f & MachO::MH_NOMULTIDEFS) {
8452 outs() << " NOMULTIDEFS";
8453 f &= ~MachO::MH_NOMULTIDEFS;
8455 if (f & MachO::MH_NOFIXPREBINDING) {
8456 outs() << " NOFIXPREBINDING";
8457 f &= ~MachO::MH_NOFIXPREBINDING;
8459 if (f & MachO::MH_PREBINDABLE) {
8460 outs() << " PREBINDABLE";
8461 f &= ~MachO::MH_PREBINDABLE;
8463 if (f & MachO::MH_ALLMODSBOUND) {
8464 outs() << " ALLMODSBOUND";
8465 f &= ~MachO::MH_ALLMODSBOUND;
8467 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
8468 outs() << " SUBSECTIONS_VIA_SYMBOLS";
8469 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
8471 if (f & MachO::MH_CANONICAL) {
8472 outs() << " CANONICAL";
8473 f &= ~MachO::MH_CANONICAL;
8475 if (f & MachO::MH_WEAK_DEFINES) {
8476 outs() << " WEAK_DEFINES";
8477 f &= ~MachO::MH_WEAK_DEFINES;
8479 if (f & MachO::MH_BINDS_TO_WEAK) {
8480 outs() << " BINDS_TO_WEAK";
8481 f &= ~MachO::MH_BINDS_TO_WEAK;
8483 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
8484 outs() << " ALLOW_STACK_EXECUTION";
8485 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
8487 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
8488 outs() << " DEAD_STRIPPABLE_DYLIB";
8489 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
8491 if (f & MachO::MH_PIE) {
8492 outs() << " PIE";
8493 f &= ~MachO::MH_PIE;
8495 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
8496 outs() << " NO_REEXPORTED_DYLIBS";
8497 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
8499 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
8500 outs() << " MH_HAS_TLV_DESCRIPTORS";
8501 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
8503 if (f & MachO::MH_NO_HEAP_EXECUTION) {
8504 outs() << " MH_NO_HEAP_EXECUTION";
8505 f &= ~MachO::MH_NO_HEAP_EXECUTION;
8507 if (f & MachO::MH_APP_EXTENSION_SAFE) {
8508 outs() << " APP_EXTENSION_SAFE";
8509 f &= ~MachO::MH_APP_EXTENSION_SAFE;
8511 if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) {
8512 outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
8513 f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO;
8515 if (f != 0 || flags == 0)
8516 outs() << format(" 0x%08" PRIx32, f);
8517 } else {
8518 outs() << format(" 0x%08" PRIx32, magic);
8519 outs() << format(" %7d", cputype);
8520 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8521 outs() << format(" 0x%02" PRIx32,
8522 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
8523 outs() << format(" %10u", filetype);
8524 outs() << format(" %5u", ncmds);
8525 outs() << format(" %10u", sizeofcmds);
8526 outs() << format(" 0x%08" PRIx32, flags);
8528 outs() << "\n";
8531 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
8532 StringRef SegName, uint64_t vmaddr,
8533 uint64_t vmsize, uint64_t fileoff,
8534 uint64_t filesize, uint32_t maxprot,
8535 uint32_t initprot, uint32_t nsects,
8536 uint32_t flags, uint32_t object_size,
8537 bool verbose) {
8538 uint64_t expected_cmdsize;
8539 if (cmd == MachO::LC_SEGMENT) {
8540 outs() << " cmd LC_SEGMENT\n";
8541 expected_cmdsize = nsects;
8542 expected_cmdsize *= sizeof(struct MachO::section);
8543 expected_cmdsize += sizeof(struct MachO::segment_command);
8544 } else {
8545 outs() << " cmd LC_SEGMENT_64\n";
8546 expected_cmdsize = nsects;
8547 expected_cmdsize *= sizeof(struct MachO::section_64);
8548 expected_cmdsize += sizeof(struct MachO::segment_command_64);
8550 outs() << " cmdsize " << cmdsize;
8551 if (cmdsize != expected_cmdsize)
8552 outs() << " Inconsistent size\n";
8553 else
8554 outs() << "\n";
8555 outs() << " segname " << SegName << "\n";
8556 if (cmd == MachO::LC_SEGMENT_64) {
8557 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
8558 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
8559 } else {
8560 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
8561 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
8563 outs() << " fileoff " << fileoff;
8564 if (fileoff > object_size)
8565 outs() << " (past end of file)\n";
8566 else
8567 outs() << "\n";
8568 outs() << " filesize " << filesize;
8569 if (fileoff + filesize > object_size)
8570 outs() << " (past end of file)\n";
8571 else
8572 outs() << "\n";
8573 if (verbose) {
8574 if ((maxprot &
8575 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
8576 MachO::VM_PROT_EXECUTE)) != 0)
8577 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
8578 else {
8579 outs() << " maxprot ";
8580 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
8581 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
8582 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
8584 if ((initprot &
8585 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
8586 MachO::VM_PROT_EXECUTE)) != 0)
8587 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
8588 else {
8589 outs() << " initprot ";
8590 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
8591 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
8592 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
8594 } else {
8595 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
8596 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
8598 outs() << " nsects " << nsects << "\n";
8599 if (verbose) {
8600 outs() << " flags";
8601 if (flags == 0)
8602 outs() << " (none)\n";
8603 else {
8604 if (flags & MachO::SG_HIGHVM) {
8605 outs() << " HIGHVM";
8606 flags &= ~MachO::SG_HIGHVM;
8608 if (flags & MachO::SG_FVMLIB) {
8609 outs() << " FVMLIB";
8610 flags &= ~MachO::SG_FVMLIB;
8612 if (flags & MachO::SG_NORELOC) {
8613 outs() << " NORELOC";
8614 flags &= ~MachO::SG_NORELOC;
8616 if (flags & MachO::SG_PROTECTED_VERSION_1) {
8617 outs() << " PROTECTED_VERSION_1";
8618 flags &= ~MachO::SG_PROTECTED_VERSION_1;
8620 if (flags)
8621 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
8622 else
8623 outs() << "\n";
8625 } else {
8626 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
8630 static void PrintSection(const char *sectname, const char *segname,
8631 uint64_t addr, uint64_t size, uint32_t offset,
8632 uint32_t align, uint32_t reloff, uint32_t nreloc,
8633 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
8634 uint32_t cmd, const char *sg_segname,
8635 uint32_t filetype, uint32_t object_size,
8636 bool verbose) {
8637 outs() << "Section\n";
8638 outs() << " sectname " << format("%.16s\n", sectname);
8639 outs() << " segname " << format("%.16s", segname);
8640 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
8641 outs() << " (does not match segment)\n";
8642 else
8643 outs() << "\n";
8644 if (cmd == MachO::LC_SEGMENT_64) {
8645 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
8646 outs() << " size " << format("0x%016" PRIx64, size);
8647 } else {
8648 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
8649 outs() << " size " << format("0x%08" PRIx64, size);
8651 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
8652 outs() << " (past end of file)\n";
8653 else
8654 outs() << "\n";
8655 outs() << " offset " << offset;
8656 if (offset > object_size)
8657 outs() << " (past end of file)\n";
8658 else
8659 outs() << "\n";
8660 uint32_t align_shifted = 1 << align;
8661 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
8662 outs() << " reloff " << reloff;
8663 if (reloff > object_size)
8664 outs() << " (past end of file)\n";
8665 else
8666 outs() << "\n";
8667 outs() << " nreloc " << nreloc;
8668 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
8669 outs() << " (past end of file)\n";
8670 else
8671 outs() << "\n";
8672 uint32_t section_type = flags & MachO::SECTION_TYPE;
8673 if (verbose) {
8674 outs() << " type";
8675 if (section_type == MachO::S_REGULAR)
8676 outs() << " S_REGULAR\n";
8677 else if (section_type == MachO::S_ZEROFILL)
8678 outs() << " S_ZEROFILL\n";
8679 else if (section_type == MachO::S_CSTRING_LITERALS)
8680 outs() << " S_CSTRING_LITERALS\n";
8681 else if (section_type == MachO::S_4BYTE_LITERALS)
8682 outs() << " S_4BYTE_LITERALS\n";
8683 else if (section_type == MachO::S_8BYTE_LITERALS)
8684 outs() << " S_8BYTE_LITERALS\n";
8685 else if (section_type == MachO::S_16BYTE_LITERALS)
8686 outs() << " S_16BYTE_LITERALS\n";
8687 else if (section_type == MachO::S_LITERAL_POINTERS)
8688 outs() << " S_LITERAL_POINTERS\n";
8689 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
8690 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
8691 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
8692 outs() << " S_LAZY_SYMBOL_POINTERS\n";
8693 else if (section_type == MachO::S_SYMBOL_STUBS)
8694 outs() << " S_SYMBOL_STUBS\n";
8695 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
8696 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
8697 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
8698 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
8699 else if (section_type == MachO::S_COALESCED)
8700 outs() << " S_COALESCED\n";
8701 else if (section_type == MachO::S_INTERPOSING)
8702 outs() << " S_INTERPOSING\n";
8703 else if (section_type == MachO::S_DTRACE_DOF)
8704 outs() << " S_DTRACE_DOF\n";
8705 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
8706 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
8707 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
8708 outs() << " S_THREAD_LOCAL_REGULAR\n";
8709 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
8710 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
8711 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
8712 outs() << " S_THREAD_LOCAL_VARIABLES\n";
8713 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
8714 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
8715 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
8716 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
8717 else
8718 outs() << format("0x%08" PRIx32, section_type) << "\n";
8719 outs() << "attributes";
8720 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
8721 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
8722 outs() << " PURE_INSTRUCTIONS";
8723 if (section_attributes & MachO::S_ATTR_NO_TOC)
8724 outs() << " NO_TOC";
8725 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
8726 outs() << " STRIP_STATIC_SYMS";
8727 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
8728 outs() << " NO_DEAD_STRIP";
8729 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
8730 outs() << " LIVE_SUPPORT";
8731 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
8732 outs() << " SELF_MODIFYING_CODE";
8733 if (section_attributes & MachO::S_ATTR_DEBUG)
8734 outs() << " DEBUG";
8735 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
8736 outs() << " SOME_INSTRUCTIONS";
8737 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
8738 outs() << " EXT_RELOC";
8739 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
8740 outs() << " LOC_RELOC";
8741 if (section_attributes == 0)
8742 outs() << " (none)";
8743 outs() << "\n";
8744 } else
8745 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
8746 outs() << " reserved1 " << reserved1;
8747 if (section_type == MachO::S_SYMBOL_STUBS ||
8748 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
8749 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
8750 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
8751 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
8752 outs() << " (index into indirect symbol table)\n";
8753 else
8754 outs() << "\n";
8755 outs() << " reserved2 " << reserved2;
8756 if (section_type == MachO::S_SYMBOL_STUBS)
8757 outs() << " (size of stubs)\n";
8758 else
8759 outs() << "\n";
8762 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
8763 uint32_t object_size) {
8764 outs() << " cmd LC_SYMTAB\n";
8765 outs() << " cmdsize " << st.cmdsize;
8766 if (st.cmdsize != sizeof(struct MachO::symtab_command))
8767 outs() << " Incorrect size\n";
8768 else
8769 outs() << "\n";
8770 outs() << " symoff " << st.symoff;
8771 if (st.symoff > object_size)
8772 outs() << " (past end of file)\n";
8773 else
8774 outs() << "\n";
8775 outs() << " nsyms " << st.nsyms;
8776 uint64_t big_size;
8777 if (Is64Bit) {
8778 big_size = st.nsyms;
8779 big_size *= sizeof(struct MachO::nlist_64);
8780 big_size += st.symoff;
8781 if (big_size > object_size)
8782 outs() << " (past end of file)\n";
8783 else
8784 outs() << "\n";
8785 } else {
8786 big_size = st.nsyms;
8787 big_size *= sizeof(struct MachO::nlist);
8788 big_size += st.symoff;
8789 if (big_size > object_size)
8790 outs() << " (past end of file)\n";
8791 else
8792 outs() << "\n";
8794 outs() << " stroff " << st.stroff;
8795 if (st.stroff > object_size)
8796 outs() << " (past end of file)\n";
8797 else
8798 outs() << "\n";
8799 outs() << " strsize " << st.strsize;
8800 big_size = st.stroff;
8801 big_size += st.strsize;
8802 if (big_size > object_size)
8803 outs() << " (past end of file)\n";
8804 else
8805 outs() << "\n";
8808 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
8809 uint32_t nsyms, uint32_t object_size,
8810 bool Is64Bit) {
8811 outs() << " cmd LC_DYSYMTAB\n";
8812 outs() << " cmdsize " << dyst.cmdsize;
8813 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
8814 outs() << " Incorrect size\n";
8815 else
8816 outs() << "\n";
8817 outs() << " ilocalsym " << dyst.ilocalsym;
8818 if (dyst.ilocalsym > nsyms)
8819 outs() << " (greater than the number of symbols)\n";
8820 else
8821 outs() << "\n";
8822 outs() << " nlocalsym " << dyst.nlocalsym;
8823 uint64_t big_size;
8824 big_size = dyst.ilocalsym;
8825 big_size += dyst.nlocalsym;
8826 if (big_size > nsyms)
8827 outs() << " (past the end of the symbol table)\n";
8828 else
8829 outs() << "\n";
8830 outs() << " iextdefsym " << dyst.iextdefsym;
8831 if (dyst.iextdefsym > nsyms)
8832 outs() << " (greater than the number of symbols)\n";
8833 else
8834 outs() << "\n";
8835 outs() << " nextdefsym " << dyst.nextdefsym;
8836 big_size = dyst.iextdefsym;
8837 big_size += dyst.nextdefsym;
8838 if (big_size > nsyms)
8839 outs() << " (past the end of the symbol table)\n";
8840 else
8841 outs() << "\n";
8842 outs() << " iundefsym " << dyst.iundefsym;
8843 if (dyst.iundefsym > nsyms)
8844 outs() << " (greater than the number of symbols)\n";
8845 else
8846 outs() << "\n";
8847 outs() << " nundefsym " << dyst.nundefsym;
8848 big_size = dyst.iundefsym;
8849 big_size += dyst.nundefsym;
8850 if (big_size > nsyms)
8851 outs() << " (past the end of the symbol table)\n";
8852 else
8853 outs() << "\n";
8854 outs() << " tocoff " << dyst.tocoff;
8855 if (dyst.tocoff > object_size)
8856 outs() << " (past end of file)\n";
8857 else
8858 outs() << "\n";
8859 outs() << " ntoc " << dyst.ntoc;
8860 big_size = dyst.ntoc;
8861 big_size *= sizeof(struct MachO::dylib_table_of_contents);
8862 big_size += dyst.tocoff;
8863 if (big_size > object_size)
8864 outs() << " (past end of file)\n";
8865 else
8866 outs() << "\n";
8867 outs() << " modtaboff " << dyst.modtaboff;
8868 if (dyst.modtaboff > object_size)
8869 outs() << " (past end of file)\n";
8870 else
8871 outs() << "\n";
8872 outs() << " nmodtab " << dyst.nmodtab;
8873 uint64_t modtabend;
8874 if (Is64Bit) {
8875 modtabend = dyst.nmodtab;
8876 modtabend *= sizeof(struct MachO::dylib_module_64);
8877 modtabend += dyst.modtaboff;
8878 } else {
8879 modtabend = dyst.nmodtab;
8880 modtabend *= sizeof(struct MachO::dylib_module);
8881 modtabend += dyst.modtaboff;
8883 if (modtabend > object_size)
8884 outs() << " (past end of file)\n";
8885 else
8886 outs() << "\n";
8887 outs() << " extrefsymoff " << dyst.extrefsymoff;
8888 if (dyst.extrefsymoff > object_size)
8889 outs() << " (past end of file)\n";
8890 else
8891 outs() << "\n";
8892 outs() << " nextrefsyms " << dyst.nextrefsyms;
8893 big_size = dyst.nextrefsyms;
8894 big_size *= sizeof(struct MachO::dylib_reference);
8895 big_size += dyst.extrefsymoff;
8896 if (big_size > object_size)
8897 outs() << " (past end of file)\n";
8898 else
8899 outs() << "\n";
8900 outs() << " indirectsymoff " << dyst.indirectsymoff;
8901 if (dyst.indirectsymoff > object_size)
8902 outs() << " (past end of file)\n";
8903 else
8904 outs() << "\n";
8905 outs() << " nindirectsyms " << dyst.nindirectsyms;
8906 big_size = dyst.nindirectsyms;
8907 big_size *= sizeof(uint32_t);
8908 big_size += dyst.indirectsymoff;
8909 if (big_size > object_size)
8910 outs() << " (past end of file)\n";
8911 else
8912 outs() << "\n";
8913 outs() << " extreloff " << dyst.extreloff;
8914 if (dyst.extreloff > object_size)
8915 outs() << " (past end of file)\n";
8916 else
8917 outs() << "\n";
8918 outs() << " nextrel " << dyst.nextrel;
8919 big_size = dyst.nextrel;
8920 big_size *= sizeof(struct MachO::relocation_info);
8921 big_size += dyst.extreloff;
8922 if (big_size > object_size)
8923 outs() << " (past end of file)\n";
8924 else
8925 outs() << "\n";
8926 outs() << " locreloff " << dyst.locreloff;
8927 if (dyst.locreloff > object_size)
8928 outs() << " (past end of file)\n";
8929 else
8930 outs() << "\n";
8931 outs() << " nlocrel " << dyst.nlocrel;
8932 big_size = dyst.nlocrel;
8933 big_size *= sizeof(struct MachO::relocation_info);
8934 big_size += dyst.locreloff;
8935 if (big_size > object_size)
8936 outs() << " (past end of file)\n";
8937 else
8938 outs() << "\n";
8941 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
8942 uint32_t object_size) {
8943 if (dc.cmd == MachO::LC_DYLD_INFO)
8944 outs() << " cmd LC_DYLD_INFO\n";
8945 else
8946 outs() << " cmd LC_DYLD_INFO_ONLY\n";
8947 outs() << " cmdsize " << dc.cmdsize;
8948 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
8949 outs() << " Incorrect size\n";
8950 else
8951 outs() << "\n";
8952 outs() << " rebase_off " << dc.rebase_off;
8953 if (dc.rebase_off > object_size)
8954 outs() << " (past end of file)\n";
8955 else
8956 outs() << "\n";
8957 outs() << " rebase_size " << dc.rebase_size;
8958 uint64_t big_size;
8959 big_size = dc.rebase_off;
8960 big_size += dc.rebase_size;
8961 if (big_size > object_size)
8962 outs() << " (past end of file)\n";
8963 else
8964 outs() << "\n";
8965 outs() << " bind_off " << dc.bind_off;
8966 if (dc.bind_off > object_size)
8967 outs() << " (past end of file)\n";
8968 else
8969 outs() << "\n";
8970 outs() << " bind_size " << dc.bind_size;
8971 big_size = dc.bind_off;
8972 big_size += dc.bind_size;
8973 if (big_size > object_size)
8974 outs() << " (past end of file)\n";
8975 else
8976 outs() << "\n";
8977 outs() << " weak_bind_off " << dc.weak_bind_off;
8978 if (dc.weak_bind_off > object_size)
8979 outs() << " (past end of file)\n";
8980 else
8981 outs() << "\n";
8982 outs() << " weak_bind_size " << dc.weak_bind_size;
8983 big_size = dc.weak_bind_off;
8984 big_size += dc.weak_bind_size;
8985 if (big_size > object_size)
8986 outs() << " (past end of file)\n";
8987 else
8988 outs() << "\n";
8989 outs() << " lazy_bind_off " << dc.lazy_bind_off;
8990 if (dc.lazy_bind_off > object_size)
8991 outs() << " (past end of file)\n";
8992 else
8993 outs() << "\n";
8994 outs() << " lazy_bind_size " << dc.lazy_bind_size;
8995 big_size = dc.lazy_bind_off;
8996 big_size += dc.lazy_bind_size;
8997 if (big_size > object_size)
8998 outs() << " (past end of file)\n";
8999 else
9000 outs() << "\n";
9001 outs() << " export_off " << dc.export_off;
9002 if (dc.export_off > object_size)
9003 outs() << " (past end of file)\n";
9004 else
9005 outs() << "\n";
9006 outs() << " export_size " << dc.export_size;
9007 big_size = dc.export_off;
9008 big_size += dc.export_size;
9009 if (big_size > object_size)
9010 outs() << " (past end of file)\n";
9011 else
9012 outs() << "\n";
9015 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
9016 const char *Ptr) {
9017 if (dyld.cmd == MachO::LC_ID_DYLINKER)
9018 outs() << " cmd LC_ID_DYLINKER\n";
9019 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
9020 outs() << " cmd LC_LOAD_DYLINKER\n";
9021 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
9022 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
9023 else
9024 outs() << " cmd ?(" << dyld.cmd << ")\n";
9025 outs() << " cmdsize " << dyld.cmdsize;
9026 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
9027 outs() << " Incorrect size\n";
9028 else
9029 outs() << "\n";
9030 if (dyld.name >= dyld.cmdsize)
9031 outs() << " name ?(bad offset " << dyld.name << ")\n";
9032 else {
9033 const char *P = (const char *)(Ptr) + dyld.name;
9034 outs() << " name " << P << " (offset " << dyld.name << ")\n";
9038 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
9039 outs() << " cmd LC_UUID\n";
9040 outs() << " cmdsize " << uuid.cmdsize;
9041 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
9042 outs() << " Incorrect size\n";
9043 else
9044 outs() << "\n";
9045 outs() << " uuid ";
9046 for (int i = 0; i < 16; ++i) {
9047 outs() << format("%02" PRIX32, uuid.uuid[i]);
9048 if (i == 3 || i == 5 || i == 7 || i == 9)
9049 outs() << "-";
9051 outs() << "\n";
9054 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
9055 outs() << " cmd LC_RPATH\n";
9056 outs() << " cmdsize " << rpath.cmdsize;
9057 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
9058 outs() << " Incorrect size\n";
9059 else
9060 outs() << "\n";
9061 if (rpath.path >= rpath.cmdsize)
9062 outs() << " path ?(bad offset " << rpath.path << ")\n";
9063 else {
9064 const char *P = (const char *)(Ptr) + rpath.path;
9065 outs() << " path " << P << " (offset " << rpath.path << ")\n";
9069 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
9070 StringRef LoadCmdName;
9071 switch (vd.cmd) {
9072 case MachO::LC_VERSION_MIN_MACOSX:
9073 LoadCmdName = "LC_VERSION_MIN_MACOSX";
9074 break;
9075 case MachO::LC_VERSION_MIN_IPHONEOS:
9076 LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
9077 break;
9078 case MachO::LC_VERSION_MIN_TVOS:
9079 LoadCmdName = "LC_VERSION_MIN_TVOS";
9080 break;
9081 case MachO::LC_VERSION_MIN_WATCHOS:
9082 LoadCmdName = "LC_VERSION_MIN_WATCHOS";
9083 break;
9084 default:
9085 llvm_unreachable("Unknown version min load command");
9088 outs() << " cmd " << LoadCmdName << '\n';
9089 outs() << " cmdsize " << vd.cmdsize;
9090 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
9091 outs() << " Incorrect size\n";
9092 else
9093 outs() << "\n";
9094 outs() << " version "
9095 << MachOObjectFile::getVersionMinMajor(vd, false) << "."
9096 << MachOObjectFile::getVersionMinMinor(vd, false);
9097 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
9098 if (Update != 0)
9099 outs() << "." << Update;
9100 outs() << "\n";
9101 if (vd.sdk == 0)
9102 outs() << " sdk n/a";
9103 else {
9104 outs() << " sdk "
9105 << MachOObjectFile::getVersionMinMajor(vd, true) << "."
9106 << MachOObjectFile::getVersionMinMinor(vd, true);
9108 Update = MachOObjectFile::getVersionMinUpdate(vd, true);
9109 if (Update != 0)
9110 outs() << "." << Update;
9111 outs() << "\n";
9114 static void PrintNoteLoadCommand(MachO::note_command Nt) {
9115 outs() << " cmd LC_NOTE\n";
9116 outs() << " cmdsize " << Nt.cmdsize;
9117 if (Nt.cmdsize != sizeof(struct MachO::note_command))
9118 outs() << " Incorrect size\n";
9119 else
9120 outs() << "\n";
9121 const char *d = Nt.data_owner;
9122 outs() << "data_owner " << format("%.16s\n", d);
9123 outs() << " offset " << Nt.offset << "\n";
9124 outs() << " size " << Nt.size << "\n";
9127 static void PrintBuildToolVersion(MachO::build_tool_version bv) {
9128 outs() << " tool " << MachOObjectFile::getBuildTool(bv.tool) << "\n";
9129 outs() << " version " << MachOObjectFile::getVersionString(bv.version)
9130 << "\n";
9133 static void PrintBuildVersionLoadCommand(const MachOObjectFile *obj,
9134 MachO::build_version_command bd) {
9135 outs() << " cmd LC_BUILD_VERSION\n";
9136 outs() << " cmdsize " << bd.cmdsize;
9137 if (bd.cmdsize !=
9138 sizeof(struct MachO::build_version_command) +
9139 bd.ntools * sizeof(struct MachO::build_tool_version))
9140 outs() << " Incorrect size\n";
9141 else
9142 outs() << "\n";
9143 outs() << " platform " << MachOObjectFile::getBuildPlatform(bd.platform)
9144 << "\n";
9145 if (bd.sdk)
9146 outs() << " sdk " << MachOObjectFile::getVersionString(bd.sdk)
9147 << "\n";
9148 else
9149 outs() << " sdk n/a\n";
9150 outs() << " minos " << MachOObjectFile::getVersionString(bd.minos)
9151 << "\n";
9152 outs() << " ntools " << bd.ntools << "\n";
9153 for (unsigned i = 0; i < bd.ntools; ++i) {
9154 MachO::build_tool_version bv = obj->getBuildToolVersion(i);
9155 PrintBuildToolVersion(bv);
9159 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
9160 outs() << " cmd LC_SOURCE_VERSION\n";
9161 outs() << " cmdsize " << sd.cmdsize;
9162 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
9163 outs() << " Incorrect size\n";
9164 else
9165 outs() << "\n";
9166 uint64_t a = (sd.version >> 40) & 0xffffff;
9167 uint64_t b = (sd.version >> 30) & 0x3ff;
9168 uint64_t c = (sd.version >> 20) & 0x3ff;
9169 uint64_t d = (sd.version >> 10) & 0x3ff;
9170 uint64_t e = sd.version & 0x3ff;
9171 outs() << " version " << a << "." << b;
9172 if (e != 0)
9173 outs() << "." << c << "." << d << "." << e;
9174 else if (d != 0)
9175 outs() << "." << c << "." << d;
9176 else if (c != 0)
9177 outs() << "." << c;
9178 outs() << "\n";
9181 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
9182 outs() << " cmd LC_MAIN\n";
9183 outs() << " cmdsize " << ep.cmdsize;
9184 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
9185 outs() << " Incorrect size\n";
9186 else
9187 outs() << "\n";
9188 outs() << " entryoff " << ep.entryoff << "\n";
9189 outs() << " stacksize " << ep.stacksize << "\n";
9192 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
9193 uint32_t object_size) {
9194 outs() << " cmd LC_ENCRYPTION_INFO\n";
9195 outs() << " cmdsize " << ec.cmdsize;
9196 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
9197 outs() << " Incorrect size\n";
9198 else
9199 outs() << "\n";
9200 outs() << " cryptoff " << ec.cryptoff;
9201 if (ec.cryptoff > object_size)
9202 outs() << " (past end of file)\n";
9203 else
9204 outs() << "\n";
9205 outs() << " cryptsize " << ec.cryptsize;
9206 if (ec.cryptsize > object_size)
9207 outs() << " (past end of file)\n";
9208 else
9209 outs() << "\n";
9210 outs() << " cryptid " << ec.cryptid << "\n";
9213 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
9214 uint32_t object_size) {
9215 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
9216 outs() << " cmdsize " << ec.cmdsize;
9217 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
9218 outs() << " Incorrect size\n";
9219 else
9220 outs() << "\n";
9221 outs() << " cryptoff " << ec.cryptoff;
9222 if (ec.cryptoff > object_size)
9223 outs() << " (past end of file)\n";
9224 else
9225 outs() << "\n";
9226 outs() << " cryptsize " << ec.cryptsize;
9227 if (ec.cryptsize > object_size)
9228 outs() << " (past end of file)\n";
9229 else
9230 outs() << "\n";
9231 outs() << " cryptid " << ec.cryptid << "\n";
9232 outs() << " pad " << ec.pad << "\n";
9235 static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
9236 const char *Ptr) {
9237 outs() << " cmd LC_LINKER_OPTION\n";
9238 outs() << " cmdsize " << lo.cmdsize;
9239 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
9240 outs() << " Incorrect size\n";
9241 else
9242 outs() << "\n";
9243 outs() << " count " << lo.count << "\n";
9244 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
9245 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
9246 uint32_t i = 0;
9247 while (left > 0) {
9248 while (*string == '\0' && left > 0) {
9249 string++;
9250 left--;
9252 if (left > 0) {
9253 i++;
9254 outs() << " string #" << i << " " << format("%.*s\n", left, string);
9255 uint32_t NullPos = StringRef(string, left).find('\0');
9256 uint32_t len = std::min(NullPos, left) + 1;
9257 string += len;
9258 left -= len;
9261 if (lo.count != i)
9262 outs() << " count " << lo.count << " does not match number of strings "
9263 << i << "\n";
9266 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
9267 const char *Ptr) {
9268 outs() << " cmd LC_SUB_FRAMEWORK\n";
9269 outs() << " cmdsize " << sub.cmdsize;
9270 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
9271 outs() << " Incorrect size\n";
9272 else
9273 outs() << "\n";
9274 if (sub.umbrella < sub.cmdsize) {
9275 const char *P = Ptr + sub.umbrella;
9276 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
9277 } else {
9278 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
9282 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
9283 const char *Ptr) {
9284 outs() << " cmd LC_SUB_UMBRELLA\n";
9285 outs() << " cmdsize " << sub.cmdsize;
9286 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
9287 outs() << " Incorrect size\n";
9288 else
9289 outs() << "\n";
9290 if (sub.sub_umbrella < sub.cmdsize) {
9291 const char *P = Ptr + sub.sub_umbrella;
9292 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
9293 } else {
9294 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
9298 static void PrintSubLibraryCommand(MachO::sub_library_command sub,
9299 const char *Ptr) {
9300 outs() << " cmd LC_SUB_LIBRARY\n";
9301 outs() << " cmdsize " << sub.cmdsize;
9302 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
9303 outs() << " Incorrect size\n";
9304 else
9305 outs() << "\n";
9306 if (sub.sub_library < sub.cmdsize) {
9307 const char *P = Ptr + sub.sub_library;
9308 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
9309 } else {
9310 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
9314 static void PrintSubClientCommand(MachO::sub_client_command sub,
9315 const char *Ptr) {
9316 outs() << " cmd LC_SUB_CLIENT\n";
9317 outs() << " cmdsize " << sub.cmdsize;
9318 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
9319 outs() << " Incorrect size\n";
9320 else
9321 outs() << "\n";
9322 if (sub.client < sub.cmdsize) {
9323 const char *P = Ptr + sub.client;
9324 outs() << " client " << P << " (offset " << sub.client << ")\n";
9325 } else {
9326 outs() << " client ?(bad offset " << sub.client << ")\n";
9330 static void PrintRoutinesCommand(MachO::routines_command r) {
9331 outs() << " cmd LC_ROUTINES\n";
9332 outs() << " cmdsize " << r.cmdsize;
9333 if (r.cmdsize != sizeof(struct MachO::routines_command))
9334 outs() << " Incorrect size\n";
9335 else
9336 outs() << "\n";
9337 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
9338 outs() << " init_module " << r.init_module << "\n";
9339 outs() << " reserved1 " << r.reserved1 << "\n";
9340 outs() << " reserved2 " << r.reserved2 << "\n";
9341 outs() << " reserved3 " << r.reserved3 << "\n";
9342 outs() << " reserved4 " << r.reserved4 << "\n";
9343 outs() << " reserved5 " << r.reserved5 << "\n";
9344 outs() << " reserved6 " << r.reserved6 << "\n";
9347 static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
9348 outs() << " cmd LC_ROUTINES_64\n";
9349 outs() << " cmdsize " << r.cmdsize;
9350 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
9351 outs() << " Incorrect size\n";
9352 else
9353 outs() << "\n";
9354 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
9355 outs() << " init_module " << r.init_module << "\n";
9356 outs() << " reserved1 " << r.reserved1 << "\n";
9357 outs() << " reserved2 " << r.reserved2 << "\n";
9358 outs() << " reserved3 " << r.reserved3 << "\n";
9359 outs() << " reserved4 " << r.reserved4 << "\n";
9360 outs() << " reserved5 " << r.reserved5 << "\n";
9361 outs() << " reserved6 " << r.reserved6 << "\n";
9364 static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t &cpu32) {
9365 outs() << "\t eax " << format("0x%08" PRIx32, cpu32.eax);
9366 outs() << " ebx " << format("0x%08" PRIx32, cpu32.ebx);
9367 outs() << " ecx " << format("0x%08" PRIx32, cpu32.ecx);
9368 outs() << " edx " << format("0x%08" PRIx32, cpu32.edx) << "\n";
9369 outs() << "\t edi " << format("0x%08" PRIx32, cpu32.edi);
9370 outs() << " esi " << format("0x%08" PRIx32, cpu32.esi);
9371 outs() << " ebp " << format("0x%08" PRIx32, cpu32.ebp);
9372 outs() << " esp " << format("0x%08" PRIx32, cpu32.esp) << "\n";
9373 outs() << "\t ss " << format("0x%08" PRIx32, cpu32.ss);
9374 outs() << " eflags " << format("0x%08" PRIx32, cpu32.eflags);
9375 outs() << " eip " << format("0x%08" PRIx32, cpu32.eip);
9376 outs() << " cs " << format("0x%08" PRIx32, cpu32.cs) << "\n";
9377 outs() << "\t ds " << format("0x%08" PRIx32, cpu32.ds);
9378 outs() << " es " << format("0x%08" PRIx32, cpu32.es);
9379 outs() << " fs " << format("0x%08" PRIx32, cpu32.fs);
9380 outs() << " gs " << format("0x%08" PRIx32, cpu32.gs) << "\n";
9383 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
9384 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
9385 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
9386 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
9387 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
9388 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
9389 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
9390 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
9391 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
9392 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
9393 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
9394 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
9395 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
9396 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
9397 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
9398 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
9399 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
9400 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
9401 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
9402 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
9403 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
9404 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
9407 static void Print_mmst_reg(MachO::mmst_reg_t &r) {
9408 uint32_t f;
9409 outs() << "\t mmst_reg ";
9410 for (f = 0; f < 10; f++)
9411 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
9412 outs() << "\n";
9413 outs() << "\t mmst_rsrv ";
9414 for (f = 0; f < 6; f++)
9415 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
9416 outs() << "\n";
9419 static void Print_xmm_reg(MachO::xmm_reg_t &r) {
9420 uint32_t f;
9421 outs() << "\t xmm_reg ";
9422 for (f = 0; f < 16; f++)
9423 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
9424 outs() << "\n";
9427 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
9428 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
9429 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
9430 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
9431 outs() << " denorm " << fpu.fpu_fcw.denorm;
9432 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
9433 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
9434 outs() << " undfl " << fpu.fpu_fcw.undfl;
9435 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
9436 outs() << "\t\t pc ";
9437 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
9438 outs() << "FP_PREC_24B ";
9439 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
9440 outs() << "FP_PREC_53B ";
9441 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
9442 outs() << "FP_PREC_64B ";
9443 else
9444 outs() << fpu.fpu_fcw.pc << " ";
9445 outs() << "rc ";
9446 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
9447 outs() << "FP_RND_NEAR ";
9448 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
9449 outs() << "FP_RND_DOWN ";
9450 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
9451 outs() << "FP_RND_UP ";
9452 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
9453 outs() << "FP_CHOP ";
9454 outs() << "\n";
9455 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
9456 outs() << " denorm " << fpu.fpu_fsw.denorm;
9457 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
9458 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
9459 outs() << " undfl " << fpu.fpu_fsw.undfl;
9460 outs() << " precis " << fpu.fpu_fsw.precis;
9461 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
9462 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
9463 outs() << " c0 " << fpu.fpu_fsw.c0;
9464 outs() << " c1 " << fpu.fpu_fsw.c1;
9465 outs() << " c2 " << fpu.fpu_fsw.c2;
9466 outs() << " tos " << fpu.fpu_fsw.tos;
9467 outs() << " c3 " << fpu.fpu_fsw.c3;
9468 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
9469 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
9470 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
9471 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
9472 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
9473 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
9474 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
9475 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
9476 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
9477 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
9478 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
9479 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
9480 outs() << "\n";
9481 outs() << "\t fpu_stmm0:\n";
9482 Print_mmst_reg(fpu.fpu_stmm0);
9483 outs() << "\t fpu_stmm1:\n";
9484 Print_mmst_reg(fpu.fpu_stmm1);
9485 outs() << "\t fpu_stmm2:\n";
9486 Print_mmst_reg(fpu.fpu_stmm2);
9487 outs() << "\t fpu_stmm3:\n";
9488 Print_mmst_reg(fpu.fpu_stmm3);
9489 outs() << "\t fpu_stmm4:\n";
9490 Print_mmst_reg(fpu.fpu_stmm4);
9491 outs() << "\t fpu_stmm5:\n";
9492 Print_mmst_reg(fpu.fpu_stmm5);
9493 outs() << "\t fpu_stmm6:\n";
9494 Print_mmst_reg(fpu.fpu_stmm6);
9495 outs() << "\t fpu_stmm7:\n";
9496 Print_mmst_reg(fpu.fpu_stmm7);
9497 outs() << "\t fpu_xmm0:\n";
9498 Print_xmm_reg(fpu.fpu_xmm0);
9499 outs() << "\t fpu_xmm1:\n";
9500 Print_xmm_reg(fpu.fpu_xmm1);
9501 outs() << "\t fpu_xmm2:\n";
9502 Print_xmm_reg(fpu.fpu_xmm2);
9503 outs() << "\t fpu_xmm3:\n";
9504 Print_xmm_reg(fpu.fpu_xmm3);
9505 outs() << "\t fpu_xmm4:\n";
9506 Print_xmm_reg(fpu.fpu_xmm4);
9507 outs() << "\t fpu_xmm5:\n";
9508 Print_xmm_reg(fpu.fpu_xmm5);
9509 outs() << "\t fpu_xmm6:\n";
9510 Print_xmm_reg(fpu.fpu_xmm6);
9511 outs() << "\t fpu_xmm7:\n";
9512 Print_xmm_reg(fpu.fpu_xmm7);
9513 outs() << "\t fpu_xmm8:\n";
9514 Print_xmm_reg(fpu.fpu_xmm8);
9515 outs() << "\t fpu_xmm9:\n";
9516 Print_xmm_reg(fpu.fpu_xmm9);
9517 outs() << "\t fpu_xmm10:\n";
9518 Print_xmm_reg(fpu.fpu_xmm10);
9519 outs() << "\t fpu_xmm11:\n";
9520 Print_xmm_reg(fpu.fpu_xmm11);
9521 outs() << "\t fpu_xmm12:\n";
9522 Print_xmm_reg(fpu.fpu_xmm12);
9523 outs() << "\t fpu_xmm13:\n";
9524 Print_xmm_reg(fpu.fpu_xmm13);
9525 outs() << "\t fpu_xmm14:\n";
9526 Print_xmm_reg(fpu.fpu_xmm14);
9527 outs() << "\t fpu_xmm15:\n";
9528 Print_xmm_reg(fpu.fpu_xmm15);
9529 outs() << "\t fpu_rsrv4:\n";
9530 for (uint32_t f = 0; f < 6; f++) {
9531 outs() << "\t ";
9532 for (uint32_t g = 0; g < 16; g++)
9533 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
9534 outs() << "\n";
9536 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
9537 outs() << "\n";
9540 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
9541 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
9542 outs() << " err " << format("0x%08" PRIx32, exc64.err);
9543 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
9546 static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) {
9547 outs() << "\t r0 " << format("0x%08" PRIx32, cpu32.r[0]);
9548 outs() << " r1 " << format("0x%08" PRIx32, cpu32.r[1]);
9549 outs() << " r2 " << format("0x%08" PRIx32, cpu32.r[2]);
9550 outs() << " r3 " << format("0x%08" PRIx32, cpu32.r[3]) << "\n";
9551 outs() << "\t r4 " << format("0x%08" PRIx32, cpu32.r[4]);
9552 outs() << " r5 " << format("0x%08" PRIx32, cpu32.r[5]);
9553 outs() << " r6 " << format("0x%08" PRIx32, cpu32.r[6]);
9554 outs() << " r7 " << format("0x%08" PRIx32, cpu32.r[7]) << "\n";
9555 outs() << "\t r8 " << format("0x%08" PRIx32, cpu32.r[8]);
9556 outs() << " r9 " << format("0x%08" PRIx32, cpu32.r[9]);
9557 outs() << " r10 " << format("0x%08" PRIx32, cpu32.r[10]);
9558 outs() << " r11 " << format("0x%08" PRIx32, cpu32.r[11]) << "\n";
9559 outs() << "\t r12 " << format("0x%08" PRIx32, cpu32.r[12]);
9560 outs() << " sp " << format("0x%08" PRIx32, cpu32.sp);
9561 outs() << " lr " << format("0x%08" PRIx32, cpu32.lr);
9562 outs() << " pc " << format("0x%08" PRIx32, cpu32.pc) << "\n";
9563 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n";
9566 static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) {
9567 outs() << "\t x0 " << format("0x%016" PRIx64, cpu64.x[0]);
9568 outs() << " x1 " << format("0x%016" PRIx64, cpu64.x[1]);
9569 outs() << " x2 " << format("0x%016" PRIx64, cpu64.x[2]) << "\n";
9570 outs() << "\t x3 " << format("0x%016" PRIx64, cpu64.x[3]);
9571 outs() << " x4 " << format("0x%016" PRIx64, cpu64.x[4]);
9572 outs() << " x5 " << format("0x%016" PRIx64, cpu64.x[5]) << "\n";
9573 outs() << "\t x6 " << format("0x%016" PRIx64, cpu64.x[6]);
9574 outs() << " x7 " << format("0x%016" PRIx64, cpu64.x[7]);
9575 outs() << " x8 " << format("0x%016" PRIx64, cpu64.x[8]) << "\n";
9576 outs() << "\t x9 " << format("0x%016" PRIx64, cpu64.x[9]);
9577 outs() << " x10 " << format("0x%016" PRIx64, cpu64.x[10]);
9578 outs() << " x11 " << format("0x%016" PRIx64, cpu64.x[11]) << "\n";
9579 outs() << "\t x12 " << format("0x%016" PRIx64, cpu64.x[12]);
9580 outs() << " x13 " << format("0x%016" PRIx64, cpu64.x[13]);
9581 outs() << " x14 " << format("0x%016" PRIx64, cpu64.x[14]) << "\n";
9582 outs() << "\t x15 " << format("0x%016" PRIx64, cpu64.x[15]);
9583 outs() << " x16 " << format("0x%016" PRIx64, cpu64.x[16]);
9584 outs() << " x17 " << format("0x%016" PRIx64, cpu64.x[17]) << "\n";
9585 outs() << "\t x18 " << format("0x%016" PRIx64, cpu64.x[18]);
9586 outs() << " x19 " << format("0x%016" PRIx64, cpu64.x[19]);
9587 outs() << " x20 " << format("0x%016" PRIx64, cpu64.x[20]) << "\n";
9588 outs() << "\t x21 " << format("0x%016" PRIx64, cpu64.x[21]);
9589 outs() << " x22 " << format("0x%016" PRIx64, cpu64.x[22]);
9590 outs() << " x23 " << format("0x%016" PRIx64, cpu64.x[23]) << "\n";
9591 outs() << "\t x24 " << format("0x%016" PRIx64, cpu64.x[24]);
9592 outs() << " x25 " << format("0x%016" PRIx64, cpu64.x[25]);
9593 outs() << " x26 " << format("0x%016" PRIx64, cpu64.x[26]) << "\n";
9594 outs() << "\t x27 " << format("0x%016" PRIx64, cpu64.x[27]);
9595 outs() << " x28 " << format("0x%016" PRIx64, cpu64.x[28]);
9596 outs() << " fp " << format("0x%016" PRIx64, cpu64.fp) << "\n";
9597 outs() << "\t lr " << format("0x%016" PRIx64, cpu64.lr);
9598 outs() << " sp " << format("0x%016" PRIx64, cpu64.sp);
9599 outs() << " pc " << format("0x%016" PRIx64, cpu64.pc) << "\n";
9600 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu64.cpsr) << "\n";
9603 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
9604 bool isLittleEndian, uint32_t cputype) {
9605 if (t.cmd == MachO::LC_THREAD)
9606 outs() << " cmd LC_THREAD\n";
9607 else if (t.cmd == MachO::LC_UNIXTHREAD)
9608 outs() << " cmd LC_UNIXTHREAD\n";
9609 else
9610 outs() << " cmd " << t.cmd << " (unknown)\n";
9611 outs() << " cmdsize " << t.cmdsize;
9612 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
9613 outs() << " Incorrect size\n";
9614 else
9615 outs() << "\n";
9617 const char *begin = Ptr + sizeof(struct MachO::thread_command);
9618 const char *end = Ptr + t.cmdsize;
9619 uint32_t flavor, count, left;
9620 if (cputype == MachO::CPU_TYPE_I386) {
9621 while (begin < end) {
9622 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9623 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9624 begin += sizeof(uint32_t);
9625 } else {
9626 flavor = 0;
9627 begin = end;
9629 if (isLittleEndian != sys::IsLittleEndianHost)
9630 sys::swapByteOrder(flavor);
9631 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9632 memcpy((char *)&count, begin, sizeof(uint32_t));
9633 begin += sizeof(uint32_t);
9634 } else {
9635 count = 0;
9636 begin = end;
9638 if (isLittleEndian != sys::IsLittleEndianHost)
9639 sys::swapByteOrder(count);
9640 if (flavor == MachO::x86_THREAD_STATE32) {
9641 outs() << " flavor i386_THREAD_STATE\n";
9642 if (count == MachO::x86_THREAD_STATE32_COUNT)
9643 outs() << " count i386_THREAD_STATE_COUNT\n";
9644 else
9645 outs() << " count " << count
9646 << " (not x86_THREAD_STATE32_COUNT)\n";
9647 MachO::x86_thread_state32_t cpu32;
9648 left = end - begin;
9649 if (left >= sizeof(MachO::x86_thread_state32_t)) {
9650 memcpy(&cpu32, begin, sizeof(MachO::x86_thread_state32_t));
9651 begin += sizeof(MachO::x86_thread_state32_t);
9652 } else {
9653 memset(&cpu32, '\0', sizeof(MachO::x86_thread_state32_t));
9654 memcpy(&cpu32, begin, left);
9655 begin += left;
9657 if (isLittleEndian != sys::IsLittleEndianHost)
9658 swapStruct(cpu32);
9659 Print_x86_thread_state32_t(cpu32);
9660 } else if (flavor == MachO::x86_THREAD_STATE) {
9661 outs() << " flavor x86_THREAD_STATE\n";
9662 if (count == MachO::x86_THREAD_STATE_COUNT)
9663 outs() << " count x86_THREAD_STATE_COUNT\n";
9664 else
9665 outs() << " count " << count
9666 << " (not x86_THREAD_STATE_COUNT)\n";
9667 struct MachO::x86_thread_state_t ts;
9668 left = end - begin;
9669 if (left >= sizeof(MachO::x86_thread_state_t)) {
9670 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
9671 begin += sizeof(MachO::x86_thread_state_t);
9672 } else {
9673 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
9674 memcpy(&ts, begin, left);
9675 begin += left;
9677 if (isLittleEndian != sys::IsLittleEndianHost)
9678 swapStruct(ts);
9679 if (ts.tsh.flavor == MachO::x86_THREAD_STATE32) {
9680 outs() << "\t tsh.flavor x86_THREAD_STATE32 ";
9681 if (ts.tsh.count == MachO::x86_THREAD_STATE32_COUNT)
9682 outs() << "tsh.count x86_THREAD_STATE32_COUNT\n";
9683 else
9684 outs() << "tsh.count " << ts.tsh.count
9685 << " (not x86_THREAD_STATE32_COUNT\n";
9686 Print_x86_thread_state32_t(ts.uts.ts32);
9687 } else {
9688 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
9689 << ts.tsh.count << "\n";
9691 } else {
9692 outs() << " flavor " << flavor << " (unknown)\n";
9693 outs() << " count " << count << "\n";
9694 outs() << " state (unknown)\n";
9695 begin += count * sizeof(uint32_t);
9698 } else if (cputype == MachO::CPU_TYPE_X86_64) {
9699 while (begin < end) {
9700 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9701 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9702 begin += sizeof(uint32_t);
9703 } else {
9704 flavor = 0;
9705 begin = end;
9707 if (isLittleEndian != sys::IsLittleEndianHost)
9708 sys::swapByteOrder(flavor);
9709 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9710 memcpy((char *)&count, begin, sizeof(uint32_t));
9711 begin += sizeof(uint32_t);
9712 } else {
9713 count = 0;
9714 begin = end;
9716 if (isLittleEndian != sys::IsLittleEndianHost)
9717 sys::swapByteOrder(count);
9718 if (flavor == MachO::x86_THREAD_STATE64) {
9719 outs() << " flavor x86_THREAD_STATE64\n";
9720 if (count == MachO::x86_THREAD_STATE64_COUNT)
9721 outs() << " count x86_THREAD_STATE64_COUNT\n";
9722 else
9723 outs() << " count " << count
9724 << " (not x86_THREAD_STATE64_COUNT)\n";
9725 MachO::x86_thread_state64_t cpu64;
9726 left = end - begin;
9727 if (left >= sizeof(MachO::x86_thread_state64_t)) {
9728 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
9729 begin += sizeof(MachO::x86_thread_state64_t);
9730 } else {
9731 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
9732 memcpy(&cpu64, begin, left);
9733 begin += left;
9735 if (isLittleEndian != sys::IsLittleEndianHost)
9736 swapStruct(cpu64);
9737 Print_x86_thread_state64_t(cpu64);
9738 } else if (flavor == MachO::x86_THREAD_STATE) {
9739 outs() << " flavor x86_THREAD_STATE\n";
9740 if (count == MachO::x86_THREAD_STATE_COUNT)
9741 outs() << " count x86_THREAD_STATE_COUNT\n";
9742 else
9743 outs() << " count " << count
9744 << " (not x86_THREAD_STATE_COUNT)\n";
9745 struct MachO::x86_thread_state_t ts;
9746 left = end - begin;
9747 if (left >= sizeof(MachO::x86_thread_state_t)) {
9748 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
9749 begin += sizeof(MachO::x86_thread_state_t);
9750 } else {
9751 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
9752 memcpy(&ts, begin, left);
9753 begin += left;
9755 if (isLittleEndian != sys::IsLittleEndianHost)
9756 swapStruct(ts);
9757 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
9758 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
9759 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
9760 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
9761 else
9762 outs() << "tsh.count " << ts.tsh.count
9763 << " (not x86_THREAD_STATE64_COUNT\n";
9764 Print_x86_thread_state64_t(ts.uts.ts64);
9765 } else {
9766 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
9767 << ts.tsh.count << "\n";
9769 } else if (flavor == MachO::x86_FLOAT_STATE) {
9770 outs() << " flavor x86_FLOAT_STATE\n";
9771 if (count == MachO::x86_FLOAT_STATE_COUNT)
9772 outs() << " count x86_FLOAT_STATE_COUNT\n";
9773 else
9774 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
9775 struct MachO::x86_float_state_t fs;
9776 left = end - begin;
9777 if (left >= sizeof(MachO::x86_float_state_t)) {
9778 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
9779 begin += sizeof(MachO::x86_float_state_t);
9780 } else {
9781 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
9782 memcpy(&fs, begin, left);
9783 begin += left;
9785 if (isLittleEndian != sys::IsLittleEndianHost)
9786 swapStruct(fs);
9787 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
9788 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
9789 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
9790 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
9791 else
9792 outs() << "fsh.count " << fs.fsh.count
9793 << " (not x86_FLOAT_STATE64_COUNT\n";
9794 Print_x86_float_state_t(fs.ufs.fs64);
9795 } else {
9796 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
9797 << fs.fsh.count << "\n";
9799 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
9800 outs() << " flavor x86_EXCEPTION_STATE\n";
9801 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
9802 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
9803 else
9804 outs() << " count " << count
9805 << " (not x86_EXCEPTION_STATE_COUNT)\n";
9806 struct MachO::x86_exception_state_t es;
9807 left = end - begin;
9808 if (left >= sizeof(MachO::x86_exception_state_t)) {
9809 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
9810 begin += sizeof(MachO::x86_exception_state_t);
9811 } else {
9812 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
9813 memcpy(&es, begin, left);
9814 begin += left;
9816 if (isLittleEndian != sys::IsLittleEndianHost)
9817 swapStruct(es);
9818 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
9819 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
9820 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
9821 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
9822 else
9823 outs() << "\t esh.count " << es.esh.count
9824 << " (not x86_EXCEPTION_STATE64_COUNT\n";
9825 Print_x86_exception_state_t(es.ues.es64);
9826 } else {
9827 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
9828 << es.esh.count << "\n";
9830 } else if (flavor == MachO::x86_EXCEPTION_STATE64) {
9831 outs() << " flavor x86_EXCEPTION_STATE64\n";
9832 if (count == MachO::x86_EXCEPTION_STATE64_COUNT)
9833 outs() << " count x86_EXCEPTION_STATE64_COUNT\n";
9834 else
9835 outs() << " count " << count
9836 << " (not x86_EXCEPTION_STATE64_COUNT)\n";
9837 struct MachO::x86_exception_state64_t es64;
9838 left = end - begin;
9839 if (left >= sizeof(MachO::x86_exception_state64_t)) {
9840 memcpy(&es64, begin, sizeof(MachO::x86_exception_state64_t));
9841 begin += sizeof(MachO::x86_exception_state64_t);
9842 } else {
9843 memset(&es64, '\0', sizeof(MachO::x86_exception_state64_t));
9844 memcpy(&es64, begin, left);
9845 begin += left;
9847 if (isLittleEndian != sys::IsLittleEndianHost)
9848 swapStruct(es64);
9849 Print_x86_exception_state_t(es64);
9850 } else {
9851 outs() << " flavor " << flavor << " (unknown)\n";
9852 outs() << " count " << count << "\n";
9853 outs() << " state (unknown)\n";
9854 begin += count * sizeof(uint32_t);
9857 } else if (cputype == MachO::CPU_TYPE_ARM) {
9858 while (begin < end) {
9859 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9860 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9861 begin += sizeof(uint32_t);
9862 } else {
9863 flavor = 0;
9864 begin = end;
9866 if (isLittleEndian != sys::IsLittleEndianHost)
9867 sys::swapByteOrder(flavor);
9868 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9869 memcpy((char *)&count, begin, sizeof(uint32_t));
9870 begin += sizeof(uint32_t);
9871 } else {
9872 count = 0;
9873 begin = end;
9875 if (isLittleEndian != sys::IsLittleEndianHost)
9876 sys::swapByteOrder(count);
9877 if (flavor == MachO::ARM_THREAD_STATE) {
9878 outs() << " flavor ARM_THREAD_STATE\n";
9879 if (count == MachO::ARM_THREAD_STATE_COUNT)
9880 outs() << " count ARM_THREAD_STATE_COUNT\n";
9881 else
9882 outs() << " count " << count
9883 << " (not ARM_THREAD_STATE_COUNT)\n";
9884 MachO::arm_thread_state32_t cpu32;
9885 left = end - begin;
9886 if (left >= sizeof(MachO::arm_thread_state32_t)) {
9887 memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t));
9888 begin += sizeof(MachO::arm_thread_state32_t);
9889 } else {
9890 memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t));
9891 memcpy(&cpu32, begin, left);
9892 begin += left;
9894 if (isLittleEndian != sys::IsLittleEndianHost)
9895 swapStruct(cpu32);
9896 Print_arm_thread_state32_t(cpu32);
9897 } else {
9898 outs() << " flavor " << flavor << " (unknown)\n";
9899 outs() << " count " << count << "\n";
9900 outs() << " state (unknown)\n";
9901 begin += count * sizeof(uint32_t);
9904 } else if (cputype == MachO::CPU_TYPE_ARM64 ||
9905 cputype == MachO::CPU_TYPE_ARM64_32) {
9906 while (begin < end) {
9907 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9908 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9909 begin += sizeof(uint32_t);
9910 } else {
9911 flavor = 0;
9912 begin = end;
9914 if (isLittleEndian != sys::IsLittleEndianHost)
9915 sys::swapByteOrder(flavor);
9916 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9917 memcpy((char *)&count, begin, sizeof(uint32_t));
9918 begin += sizeof(uint32_t);
9919 } else {
9920 count = 0;
9921 begin = end;
9923 if (isLittleEndian != sys::IsLittleEndianHost)
9924 sys::swapByteOrder(count);
9925 if (flavor == MachO::ARM_THREAD_STATE64) {
9926 outs() << " flavor ARM_THREAD_STATE64\n";
9927 if (count == MachO::ARM_THREAD_STATE64_COUNT)
9928 outs() << " count ARM_THREAD_STATE64_COUNT\n";
9929 else
9930 outs() << " count " << count
9931 << " (not ARM_THREAD_STATE64_COUNT)\n";
9932 MachO::arm_thread_state64_t cpu64;
9933 left = end - begin;
9934 if (left >= sizeof(MachO::arm_thread_state64_t)) {
9935 memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t));
9936 begin += sizeof(MachO::arm_thread_state64_t);
9937 } else {
9938 memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t));
9939 memcpy(&cpu64, begin, left);
9940 begin += left;
9942 if (isLittleEndian != sys::IsLittleEndianHost)
9943 swapStruct(cpu64);
9944 Print_arm_thread_state64_t(cpu64);
9945 } else {
9946 outs() << " flavor " << flavor << " (unknown)\n";
9947 outs() << " count " << count << "\n";
9948 outs() << " state (unknown)\n";
9949 begin += count * sizeof(uint32_t);
9952 } else {
9953 while (begin < end) {
9954 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9955 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9956 begin += sizeof(uint32_t);
9957 } else {
9958 flavor = 0;
9959 begin = end;
9961 if (isLittleEndian != sys::IsLittleEndianHost)
9962 sys::swapByteOrder(flavor);
9963 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9964 memcpy((char *)&count, begin, sizeof(uint32_t));
9965 begin += sizeof(uint32_t);
9966 } else {
9967 count = 0;
9968 begin = end;
9970 if (isLittleEndian != sys::IsLittleEndianHost)
9971 sys::swapByteOrder(count);
9972 outs() << " flavor " << flavor << "\n";
9973 outs() << " count " << count << "\n";
9974 outs() << " state (Unknown cputype/cpusubtype)\n";
9975 begin += count * sizeof(uint32_t);
9980 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
9981 if (dl.cmd == MachO::LC_ID_DYLIB)
9982 outs() << " cmd LC_ID_DYLIB\n";
9983 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
9984 outs() << " cmd LC_LOAD_DYLIB\n";
9985 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
9986 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
9987 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
9988 outs() << " cmd LC_REEXPORT_DYLIB\n";
9989 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
9990 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
9991 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
9992 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
9993 else
9994 outs() << " cmd " << dl.cmd << " (unknown)\n";
9995 outs() << " cmdsize " << dl.cmdsize;
9996 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
9997 outs() << " Incorrect size\n";
9998 else
9999 outs() << "\n";
10000 if (dl.dylib.name < dl.cmdsize) {
10001 const char *P = (const char *)(Ptr) + dl.dylib.name;
10002 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
10003 } else {
10004 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
10006 outs() << " time stamp " << dl.dylib.timestamp << " ";
10007 time_t t = dl.dylib.timestamp;
10008 outs() << ctime(&t);
10009 outs() << " current version ";
10010 if (dl.dylib.current_version == 0xffffffff)
10011 outs() << "n/a\n";
10012 else
10013 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
10014 << ((dl.dylib.current_version >> 8) & 0xff) << "."
10015 << (dl.dylib.current_version & 0xff) << "\n";
10016 outs() << "compatibility version ";
10017 if (dl.dylib.compatibility_version == 0xffffffff)
10018 outs() << "n/a\n";
10019 else
10020 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
10021 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
10022 << (dl.dylib.compatibility_version & 0xff) << "\n";
10025 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
10026 uint32_t object_size) {
10027 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
10028 outs() << " cmd LC_CODE_SIGNATURE\n";
10029 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
10030 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
10031 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
10032 outs() << " cmd LC_FUNCTION_STARTS\n";
10033 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
10034 outs() << " cmd LC_DATA_IN_CODE\n";
10035 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
10036 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
10037 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
10038 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
10039 else
10040 outs() << " cmd " << ld.cmd << " (?)\n";
10041 outs() << " cmdsize " << ld.cmdsize;
10042 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
10043 outs() << " Incorrect size\n";
10044 else
10045 outs() << "\n";
10046 outs() << " dataoff " << ld.dataoff;
10047 if (ld.dataoff > object_size)
10048 outs() << " (past end of file)\n";
10049 else
10050 outs() << "\n";
10051 outs() << " datasize " << ld.datasize;
10052 uint64_t big_size = ld.dataoff;
10053 big_size += ld.datasize;
10054 if (big_size > object_size)
10055 outs() << " (past end of file)\n";
10056 else
10057 outs() << "\n";
10060 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
10061 uint32_t cputype, bool verbose) {
10062 StringRef Buf = Obj->getData();
10063 unsigned Index = 0;
10064 for (const auto &Command : Obj->load_commands()) {
10065 outs() << "Load command " << Index++ << "\n";
10066 if (Command.C.cmd == MachO::LC_SEGMENT) {
10067 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
10068 const char *sg_segname = SLC.segname;
10069 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
10070 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
10071 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
10072 verbose);
10073 for (unsigned j = 0; j < SLC.nsects; j++) {
10074 MachO::section S = Obj->getSection(Command, j);
10075 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
10076 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
10077 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
10079 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
10080 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
10081 const char *sg_segname = SLC_64.segname;
10082 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
10083 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
10084 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
10085 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
10086 for (unsigned j = 0; j < SLC_64.nsects; j++) {
10087 MachO::section_64 S_64 = Obj->getSection64(Command, j);
10088 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
10089 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
10090 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
10091 sg_segname, filetype, Buf.size(), verbose);
10093 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
10094 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
10095 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
10096 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
10097 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
10098 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
10099 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
10100 Obj->is64Bit());
10101 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
10102 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
10103 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
10104 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
10105 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
10106 Command.C.cmd == MachO::LC_ID_DYLINKER ||
10107 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
10108 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
10109 PrintDyldLoadCommand(Dyld, Command.Ptr);
10110 } else if (Command.C.cmd == MachO::LC_UUID) {
10111 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
10112 PrintUuidLoadCommand(Uuid);
10113 } else if (Command.C.cmd == MachO::LC_RPATH) {
10114 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
10115 PrintRpathLoadCommand(Rpath, Command.Ptr);
10116 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
10117 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
10118 Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
10119 Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
10120 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
10121 PrintVersionMinLoadCommand(Vd);
10122 } else if (Command.C.cmd == MachO::LC_NOTE) {
10123 MachO::note_command Nt = Obj->getNoteLoadCommand(Command);
10124 PrintNoteLoadCommand(Nt);
10125 } else if (Command.C.cmd == MachO::LC_BUILD_VERSION) {
10126 MachO::build_version_command Bv =
10127 Obj->getBuildVersionLoadCommand(Command);
10128 PrintBuildVersionLoadCommand(Obj, Bv);
10129 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
10130 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
10131 PrintSourceVersionCommand(Sd);
10132 } else if (Command.C.cmd == MachO::LC_MAIN) {
10133 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
10134 PrintEntryPointCommand(Ep);
10135 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
10136 MachO::encryption_info_command Ei =
10137 Obj->getEncryptionInfoCommand(Command);
10138 PrintEncryptionInfoCommand(Ei, Buf.size());
10139 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
10140 MachO::encryption_info_command_64 Ei =
10141 Obj->getEncryptionInfoCommand64(Command);
10142 PrintEncryptionInfoCommand64(Ei, Buf.size());
10143 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
10144 MachO::linker_option_command Lo =
10145 Obj->getLinkerOptionLoadCommand(Command);
10146 PrintLinkerOptionCommand(Lo, Command.Ptr);
10147 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
10148 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
10149 PrintSubFrameworkCommand(Sf, Command.Ptr);
10150 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
10151 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
10152 PrintSubUmbrellaCommand(Sf, Command.Ptr);
10153 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
10154 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
10155 PrintSubLibraryCommand(Sl, Command.Ptr);
10156 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
10157 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
10158 PrintSubClientCommand(Sc, Command.Ptr);
10159 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
10160 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
10161 PrintRoutinesCommand(Rc);
10162 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
10163 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
10164 PrintRoutinesCommand64(Rc);
10165 } else if (Command.C.cmd == MachO::LC_THREAD ||
10166 Command.C.cmd == MachO::LC_UNIXTHREAD) {
10167 MachO::thread_command Tc = Obj->getThreadCommand(Command);
10168 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
10169 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
10170 Command.C.cmd == MachO::LC_ID_DYLIB ||
10171 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
10172 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
10173 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
10174 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
10175 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
10176 PrintDylibCommand(Dl, Command.Ptr);
10177 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
10178 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
10179 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
10180 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
10181 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
10182 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
10183 MachO::linkedit_data_command Ld =
10184 Obj->getLinkeditDataLoadCommand(Command);
10185 PrintLinkEditDataCommand(Ld, Buf.size());
10186 } else {
10187 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
10188 << ")\n";
10189 outs() << " cmdsize " << Command.C.cmdsize << "\n";
10190 // TODO: get and print the raw bytes of the load command.
10192 // TODO: print all the other kinds of load commands.
10196 static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
10197 if (Obj->is64Bit()) {
10198 MachO::mach_header_64 H_64;
10199 H_64 = Obj->getHeader64();
10200 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
10201 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
10202 } else {
10203 MachO::mach_header H;
10204 H = Obj->getHeader();
10205 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
10206 H.sizeofcmds, H.flags, verbose);
10210 void printMachOFileHeader(const object::ObjectFile *Obj) {
10211 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
10212 PrintMachHeader(file, !NonVerbose);
10215 void printMachOLoadCommands(const object::ObjectFile *Obj) {
10216 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
10217 uint32_t filetype = 0;
10218 uint32_t cputype = 0;
10219 if (file->is64Bit()) {
10220 MachO::mach_header_64 H_64;
10221 H_64 = file->getHeader64();
10222 filetype = H_64.filetype;
10223 cputype = H_64.cputype;
10224 } else {
10225 MachO::mach_header H;
10226 H = file->getHeader();
10227 filetype = H.filetype;
10228 cputype = H.cputype;
10230 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
10233 //===----------------------------------------------------------------------===//
10234 // export trie dumping
10235 //===----------------------------------------------------------------------===//
10237 void printMachOExportsTrie(const object::MachOObjectFile *Obj) {
10238 uint64_t BaseSegmentAddress = 0;
10239 for (const auto &Command : Obj->load_commands()) {
10240 if (Command.C.cmd == MachO::LC_SEGMENT) {
10241 MachO::segment_command Seg = Obj->getSegmentLoadCommand(Command);
10242 if (Seg.fileoff == 0 && Seg.filesize != 0) {
10243 BaseSegmentAddress = Seg.vmaddr;
10244 break;
10246 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
10247 MachO::segment_command_64 Seg = Obj->getSegment64LoadCommand(Command);
10248 if (Seg.fileoff == 0 && Seg.filesize != 0) {
10249 BaseSegmentAddress = Seg.vmaddr;
10250 break;
10254 Error Err = Error::success();
10255 for (const object::ExportEntry &Entry : Obj->exports(Err)) {
10256 uint64_t Flags = Entry.flags();
10257 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
10258 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
10259 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
10260 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
10261 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
10262 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
10263 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
10264 if (ReExport)
10265 outs() << "[re-export] ";
10266 else
10267 outs() << format("0x%08llX ",
10268 Entry.address() + BaseSegmentAddress);
10269 outs() << Entry.name();
10270 if (WeakDef || ThreadLocal || Resolver || Abs) {
10271 bool NeedsComma = false;
10272 outs() << " [";
10273 if (WeakDef) {
10274 outs() << "weak_def";
10275 NeedsComma = true;
10277 if (ThreadLocal) {
10278 if (NeedsComma)
10279 outs() << ", ";
10280 outs() << "per-thread";
10281 NeedsComma = true;
10283 if (Abs) {
10284 if (NeedsComma)
10285 outs() << ", ";
10286 outs() << "absolute";
10287 NeedsComma = true;
10289 if (Resolver) {
10290 if (NeedsComma)
10291 outs() << ", ";
10292 outs() << format("resolver=0x%08llX", Entry.other());
10293 NeedsComma = true;
10295 outs() << "]";
10297 if (ReExport) {
10298 StringRef DylibName = "unknown";
10299 int Ordinal = Entry.other() - 1;
10300 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
10301 if (Entry.otherName().empty())
10302 outs() << " (from " << DylibName << ")";
10303 else
10304 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
10306 outs() << "\n";
10308 if (Err)
10309 reportError(std::move(Err), Obj->getFileName());
10312 //===----------------------------------------------------------------------===//
10313 // rebase table dumping
10314 //===----------------------------------------------------------------------===//
10316 void printMachORebaseTable(object::MachOObjectFile *Obj) {
10317 outs() << "segment section address type\n";
10318 Error Err = Error::success();
10319 for (const object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) {
10320 StringRef SegmentName = Entry.segmentName();
10321 StringRef SectionName = Entry.sectionName();
10322 uint64_t Address = Entry.address();
10324 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
10325 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
10326 SegmentName.str().c_str(), SectionName.str().c_str(),
10327 Address, Entry.typeName().str().c_str());
10329 if (Err)
10330 reportError(std::move(Err), Obj->getFileName());
10333 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
10334 StringRef DylibName;
10335 switch (Ordinal) {
10336 case MachO::BIND_SPECIAL_DYLIB_SELF:
10337 return "this-image";
10338 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
10339 return "main-executable";
10340 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
10341 return "flat-namespace";
10342 default:
10343 if (Ordinal > 0) {
10344 std::error_code EC =
10345 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
10346 if (EC)
10347 return "<<bad library ordinal>>";
10348 return DylibName;
10351 return "<<unknown special ordinal>>";
10354 //===----------------------------------------------------------------------===//
10355 // bind table dumping
10356 //===----------------------------------------------------------------------===//
10358 void printMachOBindTable(object::MachOObjectFile *Obj) {
10359 // Build table of sections so names can used in final output.
10360 outs() << "segment section address type "
10361 "addend dylib symbol\n";
10362 Error Err = Error::success();
10363 for (const object::MachOBindEntry &Entry : Obj->bindTable(Err)) {
10364 StringRef SegmentName = Entry.segmentName();
10365 StringRef SectionName = Entry.sectionName();
10366 uint64_t Address = Entry.address();
10368 // Table lines look like:
10369 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
10370 StringRef Attr;
10371 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
10372 Attr = " (weak_import)";
10373 outs() << left_justify(SegmentName, 8) << " "
10374 << left_justify(SectionName, 18) << " "
10375 << format_hex(Address, 10, true) << " "
10376 << left_justify(Entry.typeName(), 8) << " "
10377 << format_decimal(Entry.addend(), 8) << " "
10378 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
10379 << Entry.symbolName() << Attr << "\n";
10381 if (Err)
10382 reportError(std::move(Err), Obj->getFileName());
10385 //===----------------------------------------------------------------------===//
10386 // lazy bind table dumping
10387 //===----------------------------------------------------------------------===//
10389 void printMachOLazyBindTable(object::MachOObjectFile *Obj) {
10390 outs() << "segment section address "
10391 "dylib symbol\n";
10392 Error Err = Error::success();
10393 for (const object::MachOBindEntry &Entry : Obj->lazyBindTable(Err)) {
10394 StringRef SegmentName = Entry.segmentName();
10395 StringRef SectionName = Entry.sectionName();
10396 uint64_t Address = Entry.address();
10398 // Table lines look like:
10399 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
10400 outs() << left_justify(SegmentName, 8) << " "
10401 << left_justify(SectionName, 18) << " "
10402 << format_hex(Address, 10, true) << " "
10403 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
10404 << Entry.symbolName() << "\n";
10406 if (Err)
10407 reportError(std::move(Err), Obj->getFileName());
10410 //===----------------------------------------------------------------------===//
10411 // weak bind table dumping
10412 //===----------------------------------------------------------------------===//
10414 void printMachOWeakBindTable(object::MachOObjectFile *Obj) {
10415 outs() << "segment section address "
10416 "type addend symbol\n";
10417 Error Err = Error::success();
10418 for (const object::MachOBindEntry &Entry : Obj->weakBindTable(Err)) {
10419 // Strong symbols don't have a location to update.
10420 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
10421 outs() << " strong "
10422 << Entry.symbolName() << "\n";
10423 continue;
10425 StringRef SegmentName = Entry.segmentName();
10426 StringRef SectionName = Entry.sectionName();
10427 uint64_t Address = Entry.address();
10429 // Table lines look like:
10430 // __DATA __data 0x00001000 pointer 0 _foo
10431 outs() << left_justify(SegmentName, 8) << " "
10432 << left_justify(SectionName, 18) << " "
10433 << format_hex(Address, 10, true) << " "
10434 << left_justify(Entry.typeName(), 8) << " "
10435 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
10436 << "\n";
10438 if (Err)
10439 reportError(std::move(Err), Obj->getFileName());
10442 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
10443 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
10444 // information for that address. If the address is found its binding symbol
10445 // name is returned. If not nullptr is returned.
10446 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
10447 struct DisassembleInfo *info) {
10448 if (info->bindtable == nullptr) {
10449 info->bindtable = std::make_unique<SymbolAddressMap>();
10450 Error Err = Error::success();
10451 for (const object::MachOBindEntry &Entry : info->O->bindTable(Err)) {
10452 uint64_t Address = Entry.address();
10453 StringRef name = Entry.symbolName();
10454 if (!name.empty())
10455 (*info->bindtable)[Address] = name;
10457 if (Err)
10458 reportError(std::move(Err), info->O->getFileName());
10460 auto name = info->bindtable->lookup(ReferenceValue);
10461 return !name.empty() ? name.data() : nullptr;
10464 void printLazyBindTable(ObjectFile *o) {
10465 outs() << "Lazy bind table:\n";
10466 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10467 printMachOLazyBindTable(MachO);
10468 else
10469 WithColor::error()
10470 << "This operation is only currently supported "
10471 "for Mach-O executable files.\n";
10474 void printWeakBindTable(ObjectFile *o) {
10475 outs() << "Weak bind table:\n";
10476 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10477 printMachOWeakBindTable(MachO);
10478 else
10479 WithColor::error()
10480 << "This operation is only currently supported "
10481 "for Mach-O executable files.\n";
10484 void printExportsTrie(const ObjectFile *o) {
10485 outs() << "Exports trie:\n";
10486 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10487 printMachOExportsTrie(MachO);
10488 else
10489 WithColor::error()
10490 << "This operation is only currently supported "
10491 "for Mach-O executable files.\n";
10494 void printRebaseTable(ObjectFile *o) {
10495 outs() << "Rebase table:\n";
10496 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10497 printMachORebaseTable(MachO);
10498 else
10499 WithColor::error()
10500 << "This operation is only currently supported "
10501 "for Mach-O executable files.\n";
10504 void printBindTable(ObjectFile *o) {
10505 outs() << "Bind table:\n";
10506 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10507 printMachOBindTable(MachO);
10508 else
10509 WithColor::error()
10510 << "This operation is only currently supported "
10511 "for Mach-O executable files.\n";
10513 } // namespace llvm