1 //===- MachODumper.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-readobj. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "ObjDumper.h" 14 #include "StackMapPrinter.h" 15 #include "llvm-readobj.h" 16 #include "llvm/ADT/SmallString.h" 17 #include "llvm/ADT/StringExtras.h" 18 #include "llvm/Object/MachO.h" 19 #include "llvm/Support/BinaryStreamReader.h" 20 #include "llvm/Support/Casting.h" 21 #include "llvm/Support/ScopedPrinter.h" 22 23 using namespace llvm; 24 using namespace object; 25 26 namespace { 27 28 class MachODumper : public ObjDumper { 29 public: 30 MachODumper(const MachOObjectFile *Obj, ScopedPrinter &Writer) 31 : ObjDumper(Writer, Obj->getFileName()), Obj(Obj) {} 32 33 void printFileHeaders() override; 34 void printSectionHeaders() override; 35 void printRelocations() override; 36 void printUnwindInfo() override; 37 void printStackMap() const override; 38 void printCGProfile() override; 39 40 void printNeededLibraries() override; 41 42 bool canCompareSymbols() const override { return true; } 43 bool compareSymbolsByName(object::SymbolRef LHS, 44 object::SymbolRef RHS) const override; 45 bool compareSymbolsByType(object::SymbolRef LHS, 46 object::SymbolRef RHS) const override; 47 // MachO-specific. 48 void printMachODataInCode() override; 49 void printMachOVersionMin() override; 50 void printMachODysymtab() override; 51 void printMachOSegment() override; 52 void printMachOIndirectSymbols() override; 53 void printMachOLinkerOptions () override; 54 55 private: 56 template<class MachHeader> 57 void printFileHeaders(const MachHeader &Header); 58 59 StringRef getSymbolName(const SymbolRef &Symbol) const; 60 uint8_t getSymbolType(const SymbolRef &Symbol) const; 61 62 void printSymbols() override; 63 void printSymbols(std::optional<SymbolComparator> SymComp) override; 64 void printDynamicSymbols() override; 65 void printDynamicSymbols(std::optional<SymbolComparator> SymComp) override; 66 void printSymbol(const SymbolRef &Symbol, ScopedPrinter &W); 67 void printSymbol(const SymbolRef &Symbol); 68 69 void printRelocation(const RelocationRef &Reloc); 70 71 void printRelocation(const MachOObjectFile *Obj, const RelocationRef &Reloc); 72 73 void printSectionHeaders(const MachOObjectFile *Obj); 74 75 const MachOObjectFile *Obj; 76 }; 77 78 } // namespace 79 80 81 namespace llvm { 82 83 std::unique_ptr<ObjDumper> createMachODumper(const object::MachOObjectFile &Obj, 84 ScopedPrinter &Writer) { 85 return std::make_unique<MachODumper>(&Obj, Writer); 86 } 87 88 } // namespace llvm 89 90 const EnumEntry<uint32_t> MachOMagics[] = { 91 { "Magic", MachO::MH_MAGIC }, 92 { "Cigam", MachO::MH_CIGAM }, 93 { "Magic64", MachO::MH_MAGIC_64 }, 94 { "Cigam64", MachO::MH_CIGAM_64 }, 95 { "FatMagic", MachO::FAT_MAGIC }, 96 { "FatCigam", MachO::FAT_CIGAM }, 97 }; 98 99 const EnumEntry<uint32_t> MachOHeaderFileTypes[] = { 100 { "Relocatable", MachO::MH_OBJECT }, 101 { "Executable", MachO::MH_EXECUTE }, 102 { "FixedVMLibrary", MachO::MH_FVMLIB }, 103 { "Core", MachO::MH_CORE }, 104 { "PreloadedExecutable", MachO::MH_PRELOAD }, 105 { "DynamicLibrary", MachO::MH_DYLIB }, 106 { "DynamicLinker", MachO::MH_DYLINKER }, 107 { "Bundle", MachO::MH_BUNDLE }, 108 { "DynamicLibraryStub", MachO::MH_DYLIB_STUB }, 109 { "DWARFSymbol", MachO::MH_DSYM }, 110 { "KextBundle", MachO::MH_KEXT_BUNDLE }, 111 }; 112 113 const EnumEntry<uint32_t> MachOHeaderCpuTypes[] = { 114 { "Any" , static_cast<uint32_t>(MachO::CPU_TYPE_ANY) }, 115 { "X86" , MachO::CPU_TYPE_X86 }, 116 { "X86-64" , MachO::CPU_TYPE_X86_64 }, 117 { "Mc98000" , MachO::CPU_TYPE_MC98000 }, 118 { "Arm" , MachO::CPU_TYPE_ARM }, 119 { "Arm64" , MachO::CPU_TYPE_ARM64 }, 120 { "Sparc" , MachO::CPU_TYPE_SPARC }, 121 { "PowerPC" , MachO::CPU_TYPE_POWERPC }, 122 { "PowerPC64" , MachO::CPU_TYPE_POWERPC64 }, 123 }; 124 125 const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX86[] = { 126 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_I386_ALL), 127 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_386), 128 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486), 129 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486SX), 130 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_586), 131 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTPRO), 132 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M3), 133 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M5), 134 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON), 135 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON_MOBILE), 136 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3), 137 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_M), 138 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_XEON), 139 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_M), 140 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4), 141 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4_M), 142 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM), 143 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM_2), 144 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON), 145 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON_MP), 146 }; 147 148 const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX64[] = { 149 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_ALL), 150 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_ARCH1), 151 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_H), 152 }; 153 154 const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM[] = { 155 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_ALL), 156 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V4T), 157 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6), 158 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5), 159 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5TEJ), 160 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_XSCALE), 161 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7), 162 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7S), 163 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7K), 164 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6M), 165 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7M), 166 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7EM), 167 }; 168 169 const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM64[] = { 170 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64_ALL), 171 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64_V8), 172 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64E), 173 }; 174 175 const EnumEntry<uint32_t> MachOHeaderCpuSubtypesSPARC[] = { 176 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_SPARC_ALL), 177 }; 178 179 const EnumEntry<uint32_t> MachOHeaderCpuSubtypesPPC[] = { 180 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_ALL), 181 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_601), 182 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_602), 183 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603), 184 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603e), 185 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603ev), 186 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604), 187 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604e), 188 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_620), 189 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_750), 190 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7400), 191 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7450), 192 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_970), 193 }; 194 195 const EnumEntry<uint32_t> MachOHeaderFlags[] = { 196 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOUNDEFS), 197 LLVM_READOBJ_ENUM_ENT(MachO, MH_INCRLINK), 198 LLVM_READOBJ_ENUM_ENT(MachO, MH_DYLDLINK), 199 LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDATLOAD), 200 LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBOUND), 201 LLVM_READOBJ_ENUM_ENT(MachO, MH_SPLIT_SEGS), 202 LLVM_READOBJ_ENUM_ENT(MachO, MH_LAZY_INIT), 203 LLVM_READOBJ_ENUM_ENT(MachO, MH_TWOLEVEL), 204 LLVM_READOBJ_ENUM_ENT(MachO, MH_FORCE_FLAT), 205 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOMULTIDEFS), 206 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOFIXPREBINDING), 207 LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBINDABLE), 208 LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLMODSBOUND), 209 LLVM_READOBJ_ENUM_ENT(MachO, MH_SUBSECTIONS_VIA_SYMBOLS), 210 LLVM_READOBJ_ENUM_ENT(MachO, MH_CANONICAL), 211 LLVM_READOBJ_ENUM_ENT(MachO, MH_WEAK_DEFINES), 212 LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDS_TO_WEAK), 213 LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLOW_STACK_EXECUTION), 214 LLVM_READOBJ_ENUM_ENT(MachO, MH_ROOT_SAFE), 215 LLVM_READOBJ_ENUM_ENT(MachO, MH_SETUID_SAFE), 216 LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_REEXPORTED_DYLIBS), 217 LLVM_READOBJ_ENUM_ENT(MachO, MH_PIE), 218 LLVM_READOBJ_ENUM_ENT(MachO, MH_DEAD_STRIPPABLE_DYLIB), 219 LLVM_READOBJ_ENUM_ENT(MachO, MH_HAS_TLV_DESCRIPTORS), 220 LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_HEAP_EXECUTION), 221 LLVM_READOBJ_ENUM_ENT(MachO, MH_APP_EXTENSION_SAFE), 222 }; 223 224 const EnumEntry<unsigned> MachOSectionTypes[] = { 225 { "Regular" , MachO::S_REGULAR }, 226 { "ZeroFill" , MachO::S_ZEROFILL }, 227 { "CStringLiterals" , MachO::S_CSTRING_LITERALS }, 228 { "4ByteLiterals" , MachO::S_4BYTE_LITERALS }, 229 { "8ByteLiterals" , MachO::S_8BYTE_LITERALS }, 230 { "LiteralPointers" , MachO::S_LITERAL_POINTERS }, 231 { "NonLazySymbolPointers" , MachO::S_NON_LAZY_SYMBOL_POINTERS }, 232 { "LazySymbolPointers" , MachO::S_LAZY_SYMBOL_POINTERS }, 233 { "SymbolStubs" , MachO::S_SYMBOL_STUBS }, 234 { "ModInitFuncPointers" , MachO::S_MOD_INIT_FUNC_POINTERS }, 235 { "ModTermFuncPointers" , MachO::S_MOD_TERM_FUNC_POINTERS }, 236 { "Coalesced" , MachO::S_COALESCED }, 237 { "GBZeroFill" , MachO::S_GB_ZEROFILL }, 238 { "Interposing" , MachO::S_INTERPOSING }, 239 { "16ByteLiterals" , MachO::S_16BYTE_LITERALS }, 240 { "DTraceDOF" , MachO::S_DTRACE_DOF }, 241 { "LazyDylibSymbolPointers" , MachO::S_LAZY_DYLIB_SYMBOL_POINTERS }, 242 { "ThreadLocalRegular" , MachO::S_THREAD_LOCAL_REGULAR }, 243 { "ThreadLocalZerofill" , MachO::S_THREAD_LOCAL_ZEROFILL }, 244 { "ThreadLocalVariables" , MachO::S_THREAD_LOCAL_VARIABLES }, 245 { "ThreadLocalVariablePointers" , MachO::S_THREAD_LOCAL_VARIABLE_POINTERS }, 246 { "ThreadLocalInitFunctionPointers", MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS } 247 }; 248 249 const EnumEntry<unsigned> MachOSectionAttributes[] = { 250 { "LocReloc" , 1 << 0 /*S_ATTR_LOC_RELOC */ }, 251 { "ExtReloc" , 1 << 1 /*S_ATTR_EXT_RELOC */ }, 252 { "SomeInstructions" , 1 << 2 /*S_ATTR_SOME_INSTRUCTIONS */ }, 253 { "Debug" , 1 << 17 /*S_ATTR_DEBUG */ }, 254 { "SelfModifyingCode", 1 << 18 /*S_ATTR_SELF_MODIFYING_CODE*/ }, 255 { "LiveSupport" , 1 << 19 /*S_ATTR_LIVE_SUPPORT */ }, 256 { "NoDeadStrip" , 1 << 20 /*S_ATTR_NO_DEAD_STRIP */ }, 257 { "StripStaticSyms" , 1 << 21 /*S_ATTR_STRIP_STATIC_SYMS */ }, 258 { "NoTOC" , 1 << 22 /*S_ATTR_NO_TOC */ }, 259 { "PureInstructions" , 1 << 23 /*S_ATTR_PURE_INSTRUCTIONS */ }, 260 }; 261 262 const EnumEntry<unsigned> MachOSymbolRefTypes[] = { 263 { "UndefinedNonLazy", 0 }, 264 { "ReferenceFlagUndefinedLazy", 1 }, 265 { "ReferenceFlagDefined", 2 }, 266 { "ReferenceFlagPrivateDefined", 3 }, 267 { "ReferenceFlagPrivateUndefinedNonLazy", 4 }, 268 { "ReferenceFlagPrivateUndefinedLazy", 5 } 269 }; 270 271 const EnumEntry<unsigned> MachOSymbolFlags[] = { 272 { "ThumbDef", 0x8 }, 273 { "ReferencedDynamically", 0x10 }, 274 { "NoDeadStrip", 0x20 }, 275 { "WeakRef", 0x40 }, 276 { "WeakDef", 0x80 }, 277 { "SymbolResolver", 0x100 }, 278 { "AltEntry", 0x200 }, 279 { "ColdFunc", 0x400 }, 280 }; 281 282 const EnumEntry<unsigned> MachOSymbolTypes[] = { 283 { "Undef", 0x0 }, 284 { "Abs", 0x2 }, 285 { "Indirect", 0xA }, 286 { "PreboundUndef", 0xC }, 287 { "Section", 0xE } 288 }; 289 290 namespace { 291 struct MachOSection { 292 ArrayRef<char> Name; 293 ArrayRef<char> SegmentName; 294 uint64_t Address; 295 uint64_t Size; 296 uint32_t Offset; 297 uint32_t Alignment; 298 uint32_t RelocationTableOffset; 299 uint32_t NumRelocationTableEntries; 300 uint32_t Flags; 301 uint32_t Reserved1; 302 uint32_t Reserved2; 303 uint32_t Reserved3; 304 }; 305 306 struct MachOSegment { 307 std::string CmdName; 308 std::string SegName; 309 uint64_t cmdsize; 310 uint64_t vmaddr; 311 uint64_t vmsize; 312 uint64_t fileoff; 313 uint64_t filesize; 314 uint32_t maxprot; 315 uint32_t initprot; 316 uint32_t nsects; 317 uint32_t flags; 318 }; 319 320 struct MachOSymbol { 321 uint32_t StringIndex; 322 uint8_t Type; 323 uint8_t SectionIndex; 324 uint16_t Flags; 325 uint64_t Value; 326 }; 327 } 328 329 static std::string getMask(uint32_t prot) 330 { 331 // TODO (davide): This always assumes prot is valid. 332 // Catch mistakes and report if needed. 333 std::string Prot; 334 Prot = ""; 335 Prot += (prot & MachO::VM_PROT_READ) ? "r" : "-"; 336 Prot += (prot & MachO::VM_PROT_WRITE) ? "w" : "-"; 337 Prot += (prot & MachO::VM_PROT_EXECUTE) ? "x" : "-"; 338 return Prot; 339 } 340 341 static void getSection(const MachOObjectFile *Obj, 342 DataRefImpl Sec, 343 MachOSection &Section) { 344 if (!Obj->is64Bit()) { 345 MachO::section Sect = Obj->getSection(Sec); 346 Section.Address = Sect.addr; 347 Section.Size = Sect.size; 348 Section.Offset = Sect.offset; 349 Section.Alignment = Sect.align; 350 Section.RelocationTableOffset = Sect.reloff; 351 Section.NumRelocationTableEntries = Sect.nreloc; 352 Section.Flags = Sect.flags; 353 Section.Reserved1 = Sect.reserved1; 354 Section.Reserved2 = Sect.reserved2; 355 return; 356 } 357 MachO::section_64 Sect = Obj->getSection64(Sec); 358 Section.Address = Sect.addr; 359 Section.Size = Sect.size; 360 Section.Offset = Sect.offset; 361 Section.Alignment = Sect.align; 362 Section.RelocationTableOffset = Sect.reloff; 363 Section.NumRelocationTableEntries = Sect.nreloc; 364 Section.Flags = Sect.flags; 365 Section.Reserved1 = Sect.reserved1; 366 Section.Reserved2 = Sect.reserved2; 367 Section.Reserved3 = Sect.reserved3; 368 } 369 370 static void getSegment(const MachOObjectFile *Obj, 371 const MachOObjectFile::LoadCommandInfo &L, 372 MachOSegment &Segment) { 373 if (!Obj->is64Bit()) { 374 MachO::segment_command SC = Obj->getSegmentLoadCommand(L); 375 Segment.CmdName = "LC_SEGMENT"; 376 Segment.SegName = SC.segname; 377 Segment.cmdsize = SC.cmdsize; 378 Segment.vmaddr = SC.vmaddr; 379 Segment.vmsize = SC.vmsize; 380 Segment.fileoff = SC.fileoff; 381 Segment.filesize = SC.filesize; 382 Segment.maxprot = SC.maxprot; 383 Segment.initprot = SC.initprot; 384 Segment.nsects = SC.nsects; 385 Segment.flags = SC.flags; 386 return; 387 } 388 MachO::segment_command_64 SC = Obj->getSegment64LoadCommand(L); 389 Segment.CmdName = "LC_SEGMENT_64"; 390 Segment.SegName = SC.segname; 391 Segment.cmdsize = SC.cmdsize; 392 Segment.vmaddr = SC.vmaddr; 393 Segment.vmsize = SC.vmsize; 394 Segment.fileoff = SC.fileoff; 395 Segment.filesize = SC.filesize; 396 Segment.maxprot = SC.maxprot; 397 Segment.initprot = SC.initprot; 398 Segment.nsects = SC.nsects; 399 Segment.flags = SC.flags; 400 } 401 402 static void getSymbol(const MachOObjectFile *Obj, 403 DataRefImpl DRI, 404 MachOSymbol &Symbol) { 405 if (!Obj->is64Bit()) { 406 MachO::nlist Entry = Obj->getSymbolTableEntry(DRI); 407 Symbol.StringIndex = Entry.n_strx; 408 Symbol.Type = Entry.n_type; 409 Symbol.SectionIndex = Entry.n_sect; 410 Symbol.Flags = Entry.n_desc; 411 Symbol.Value = Entry.n_value; 412 return; 413 } 414 MachO::nlist_64 Entry = Obj->getSymbol64TableEntry(DRI); 415 Symbol.StringIndex = Entry.n_strx; 416 Symbol.Type = Entry.n_type; 417 Symbol.SectionIndex = Entry.n_sect; 418 Symbol.Flags = Entry.n_desc; 419 Symbol.Value = Entry.n_value; 420 } 421 422 void MachODumper::printFileHeaders() { 423 DictScope H(W, "MachHeader"); 424 if (!Obj->is64Bit()) { 425 printFileHeaders(Obj->getHeader()); 426 } else { 427 printFileHeaders(Obj->getHeader64()); 428 W.printHex("Reserved", Obj->getHeader64().reserved); 429 } 430 } 431 432 template<class MachHeader> 433 void MachODumper::printFileHeaders(const MachHeader &Header) { 434 W.printEnum("Magic", Header.magic, ArrayRef(MachOMagics)); 435 W.printEnum("CpuType", Header.cputype, ArrayRef(MachOHeaderCpuTypes)); 436 uint32_t subtype = Header.cpusubtype & ~MachO::CPU_SUBTYPE_MASK; 437 switch (Header.cputype) { 438 case MachO::CPU_TYPE_X86: 439 W.printEnum("CpuSubType", subtype, ArrayRef(MachOHeaderCpuSubtypesX86)); 440 break; 441 case MachO::CPU_TYPE_X86_64: 442 W.printEnum("CpuSubType", subtype, ArrayRef(MachOHeaderCpuSubtypesX64)); 443 break; 444 case MachO::CPU_TYPE_ARM: 445 W.printEnum("CpuSubType", subtype, ArrayRef(MachOHeaderCpuSubtypesARM)); 446 break; 447 case MachO::CPU_TYPE_POWERPC: 448 W.printEnum("CpuSubType", subtype, ArrayRef(MachOHeaderCpuSubtypesPPC)); 449 break; 450 case MachO::CPU_TYPE_SPARC: 451 W.printEnum("CpuSubType", subtype, ArrayRef(MachOHeaderCpuSubtypesSPARC)); 452 break; 453 case MachO::CPU_TYPE_ARM64: 454 W.printEnum("CpuSubType", subtype, ArrayRef(MachOHeaderCpuSubtypesARM64)); 455 break; 456 case MachO::CPU_TYPE_POWERPC64: 457 default: 458 W.printHex("CpuSubtype", subtype); 459 } 460 W.printEnum("FileType", Header.filetype, ArrayRef(MachOHeaderFileTypes)); 461 W.printNumber("NumOfLoadCommands", Header.ncmds); 462 W.printNumber("SizeOfLoadCommands", Header.sizeofcmds); 463 W.printFlags("Flags", Header.flags, ArrayRef(MachOHeaderFlags)); 464 } 465 466 void MachODumper::printSectionHeaders() { return printSectionHeaders(Obj); } 467 468 void MachODumper::printSectionHeaders(const MachOObjectFile *Obj) { 469 ListScope Group(W, "Sections"); 470 471 int SectionIndex = -1; 472 for (const SectionRef &Section : Obj->sections()) { 473 ++SectionIndex; 474 475 MachOSection MOSection; 476 getSection(Obj, Section.getRawDataRefImpl(), MOSection); 477 DataRefImpl DR = Section.getRawDataRefImpl(); 478 StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName()); 479 ArrayRef<char> RawName = Obj->getSectionRawName(DR); 480 StringRef SegmentName = Obj->getSectionFinalSegmentName(DR); 481 ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR); 482 483 DictScope SectionD(W, "Section"); 484 W.printNumber("Index", SectionIndex); 485 W.printBinary("Name", Name, RawName); 486 W.printBinary("Segment", SegmentName, RawSegmentName); 487 W.printHex("Address", MOSection.Address); 488 W.printHex("Size", MOSection.Size); 489 W.printNumber("Offset", MOSection.Offset); 490 W.printNumber("Alignment", MOSection.Alignment); 491 W.printHex("RelocationOffset", MOSection.RelocationTableOffset); 492 W.printNumber("RelocationCount", MOSection.NumRelocationTableEntries); 493 W.printEnum("Type", MOSection.Flags & 0xFF, ArrayRef(MachOSectionTypes)); 494 W.printFlags("Attributes", MOSection.Flags >> 8, 495 ArrayRef(MachOSectionAttributes)); 496 W.printHex("Reserved1", MOSection.Reserved1); 497 W.printHex("Reserved2", MOSection.Reserved2); 498 if (Obj->is64Bit()) 499 W.printHex("Reserved3", MOSection.Reserved3); 500 501 if (opts::SectionRelocations) { 502 ListScope D(W, "Relocations"); 503 for (const RelocationRef &Reloc : Section.relocations()) 504 printRelocation(Reloc); 505 } 506 507 if (opts::SectionSymbols) { 508 ListScope D(W, "Symbols"); 509 for (const SymbolRef &Symbol : Obj->symbols()) { 510 if (!Section.containsSymbol(Symbol)) 511 continue; 512 513 printSymbol(Symbol); 514 } 515 } 516 517 if (opts::SectionData && !Section.isBSS()) 518 W.printBinaryBlock("SectionData", unwrapOrError(Obj->getFileName(), 519 Section.getContents())); 520 } 521 } 522 523 void MachODumper::printRelocations() { 524 ListScope D(W, "Relocations"); 525 526 std::error_code EC; 527 for (const SectionRef &Section : Obj->sections()) { 528 StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName()); 529 bool PrintedGroup = false; 530 for (const RelocationRef &Reloc : Section.relocations()) { 531 if (!PrintedGroup) { 532 W.startLine() << "Section " << Name << " {\n"; 533 W.indent(); 534 PrintedGroup = true; 535 } 536 537 printRelocation(Reloc); 538 } 539 540 if (PrintedGroup) { 541 W.unindent(); 542 W.startLine() << "}\n"; 543 } 544 } 545 } 546 547 void MachODumper::printRelocation(const RelocationRef &Reloc) { 548 return printRelocation(Obj, Reloc); 549 } 550 551 void MachODumper::printRelocation(const MachOObjectFile *Obj, 552 const RelocationRef &Reloc) { 553 uint64_t Offset = Reloc.getOffset(); 554 SmallString<32> RelocName; 555 Reloc.getTypeName(RelocName); 556 557 DataRefImpl DR = Reloc.getRawDataRefImpl(); 558 MachO::any_relocation_info RE = Obj->getRelocation(DR); 559 bool IsScattered = Obj->isRelocationScattered(RE); 560 bool IsExtern = !IsScattered && Obj->getPlainRelocationExternal(RE); 561 562 StringRef TargetName; 563 if (IsExtern) { 564 symbol_iterator Symbol = Reloc.getSymbol(); 565 if (Symbol != Obj->symbol_end()) { 566 TargetName = getSymbolName(*Symbol); 567 } 568 } else if (!IsScattered) { 569 section_iterator SecI = Obj->getRelocationSection(DR); 570 if (SecI != Obj->section_end()) 571 TargetName = unwrapOrError(Obj->getFileName(), SecI->getName()); 572 } 573 if (TargetName.empty()) 574 TargetName = "-"; 575 576 if (opts::ExpandRelocs) { 577 DictScope Group(W, "Relocation"); 578 W.printHex("Offset", Offset); 579 W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE)); 580 W.printNumber("Length", Obj->getAnyRelocationLength(RE)); 581 W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE)); 582 if (IsScattered) { 583 W.printHex("Value", Obj->getScatteredRelocationValue(RE)); 584 } else { 585 const char *Kind = IsExtern ? "Symbol" : "Section"; 586 W.printNumber(Kind, TargetName, Obj->getPlainRelocationSymbolNum(RE)); 587 } 588 } else { 589 SmallString<32> SymbolNameOrOffset("0x"); 590 if (IsScattered) { 591 // Scattered relocations don't really have an associated symbol for some 592 // reason, even if one exists in the symtab at the correct address. 593 SymbolNameOrOffset += utohexstr(Obj->getScatteredRelocationValue(RE)); 594 } else { 595 SymbolNameOrOffset = TargetName; 596 } 597 598 raw_ostream& OS = W.startLine(); 599 OS << W.hex(Offset) 600 << " " << Obj->getAnyRelocationPCRel(RE) 601 << " " << Obj->getAnyRelocationLength(RE); 602 if (IsScattered) 603 OS << " n/a"; 604 else 605 OS << " " << Obj->getPlainRelocationExternal(RE); 606 OS << " " << RelocName 607 << " " << IsScattered 608 << " " << SymbolNameOrOffset 609 << "\n"; 610 } 611 } 612 613 StringRef MachODumper::getSymbolName(const SymbolRef &Symbol) const { 614 Expected<StringRef> SymbolNameOrErr = Symbol.getName(); 615 if (!SymbolNameOrErr) { 616 reportError(SymbolNameOrErr.takeError(), Obj->getFileName()); 617 } 618 return *SymbolNameOrErr; 619 } 620 621 uint8_t MachODumper::getSymbolType(const SymbolRef &Symbol) const { 622 return Obj->is64Bit() 623 ? Obj->getSymbol64TableEntry(Symbol.getRawDataRefImpl()).n_type 624 : Obj->getSymbolTableEntry(Symbol.getRawDataRefImpl()).n_type; 625 } 626 627 bool MachODumper::compareSymbolsByName(SymbolRef LHS, SymbolRef RHS) const { 628 return getSymbolName(LHS).str().compare(getSymbolName(RHS).str()) < 0; 629 } 630 631 bool MachODumper::compareSymbolsByType(SymbolRef LHS, SymbolRef RHS) const { 632 return getSymbolType(LHS) < getSymbolType(RHS); 633 } 634 635 void MachODumper::printSymbols() { printSymbols(std::nullopt); } 636 637 void MachODumper::printSymbols(std::optional<SymbolComparator> SymComp) { 638 ListScope Group(W, "Symbols"); 639 if (SymComp) { 640 auto SymbolRange = Obj->symbols(); 641 std::vector<SymbolRef> SortedSymbols(SymbolRange.begin(), 642 SymbolRange.end()); 643 llvm::stable_sort(SortedSymbols, *SymComp); 644 for (SymbolRef Symbol : SortedSymbols) 645 printSymbol(Symbol); 646 } else { 647 for (const SymbolRef &Symbol : Obj->symbols()) { 648 printSymbol(Symbol); 649 } 650 } 651 } 652 653 void MachODumper::printDynamicSymbols() { 654 ListScope Group(W, "DynamicSymbols"); 655 } 656 void MachODumper::printDynamicSymbols(std::optional<SymbolComparator> SymComp) { 657 ListScope Group(W, "DynamicSymbols"); 658 } 659 660 void MachODumper::printSymbol(const SymbolRef &Symbol) { 661 printSymbol(Symbol, W); 662 } 663 664 void MachODumper::printSymbol(const SymbolRef &Symbol, ScopedPrinter &W) { 665 StringRef SymbolName = getSymbolName(Symbol); 666 667 MachOSymbol MOSymbol; 668 getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol); 669 670 StringRef SectionName = ""; 671 // Don't ask a Mach-O STABS symbol for its section unless we know that 672 // STAB symbol's section field refers to a valid section index. Otherwise 673 // the symbol may error trying to load a section that does not exist. 674 // TODO: Add a whitelist of STABS symbol types that contain valid section 675 // indices. 676 if (!(MOSymbol.Type & MachO::N_STAB)) { 677 Expected<section_iterator> SecIOrErr = Symbol.getSection(); 678 if (!SecIOrErr) 679 reportError(SecIOrErr.takeError(), Obj->getFileName()); 680 681 section_iterator SecI = *SecIOrErr; 682 if (SecI != Obj->section_end()) 683 SectionName = unwrapOrError(Obj->getFileName(), SecI->getName()); 684 } 685 686 DictScope D(W, "Symbol"); 687 W.printNumber("Name", SymbolName, MOSymbol.StringIndex); 688 if (MOSymbol.Type & MachO::N_STAB) { 689 W.printHex("Type", "SymDebugTable", MOSymbol.Type); 690 } else { 691 if (MOSymbol.Type & MachO::N_PEXT) 692 W.startLine() << "PrivateExtern\n"; 693 if (MOSymbol.Type & MachO::N_EXT) 694 W.startLine() << "Extern\n"; 695 W.printEnum("Type", uint8_t(MOSymbol.Type & MachO::N_TYPE), 696 ArrayRef(MachOSymbolTypes)); 697 } 698 W.printHex("Section", SectionName, MOSymbol.SectionIndex); 699 W.printEnum("RefType", static_cast<uint16_t>(MOSymbol.Flags & 0x7), 700 ArrayRef(MachOSymbolRefTypes)); 701 W.printFlags("Flags", static_cast<uint16_t>(MOSymbol.Flags & ~0x7), 702 ArrayRef(MachOSymbolFlags)); 703 W.printHex("Value", MOSymbol.Value); 704 } 705 706 void MachODumper::printUnwindInfo() { 707 W.startLine() << "UnwindInfo not implemented.\n"; 708 } 709 710 void MachODumper::printStackMap() const { 711 object::SectionRef StackMapSection; 712 for (auto Sec : Obj->sections()) { 713 StringRef Name; 714 if (Expected<StringRef> NameOrErr = Sec.getName()) 715 Name = *NameOrErr; 716 else 717 consumeError(NameOrErr.takeError()); 718 719 if (Name == "__llvm_stackmaps") { 720 StackMapSection = Sec; 721 break; 722 } 723 } 724 725 if (StackMapSection == object::SectionRef()) 726 return; 727 728 StringRef StackMapContents = 729 unwrapOrError(Obj->getFileName(), StackMapSection.getContents()); 730 ArrayRef<uint8_t> StackMapContentsArray = 731 arrayRefFromStringRef(StackMapContents); 732 733 if (Obj->isLittleEndian()) 734 prettyPrintStackMap( 735 W, StackMapParser<support::little>(StackMapContentsArray)); 736 else 737 prettyPrintStackMap( 738 W, StackMapParser<support::big>(StackMapContentsArray)); 739 } 740 741 void MachODumper::printCGProfile() { 742 object::SectionRef CGProfileSection; 743 for (auto Sec : Obj->sections()) { 744 StringRef Name; 745 if (Expected<StringRef> NameOrErr = Sec.getName()) 746 Name = *NameOrErr; 747 else 748 consumeError(NameOrErr.takeError()); 749 750 if (Name == "__cg_profile") { 751 CGProfileSection = Sec; 752 break; 753 } 754 } 755 if (CGProfileSection == object::SectionRef()) 756 return; 757 758 StringRef CGProfileContents = 759 unwrapOrError(Obj->getFileName(), CGProfileSection.getContents()); 760 BinaryStreamReader Reader(CGProfileContents, Obj->isLittleEndian() 761 ? llvm::support::little 762 : llvm::support::big); 763 764 ListScope L(W, "CGProfile"); 765 while (!Reader.empty()) { 766 uint32_t FromIndex, ToIndex; 767 uint64_t Count; 768 if (Error Err = Reader.readInteger(FromIndex)) 769 reportError(std::move(Err), Obj->getFileName()); 770 if (Error Err = Reader.readInteger(ToIndex)) 771 reportError(std::move(Err), Obj->getFileName()); 772 if (Error Err = Reader.readInteger(Count)) 773 reportError(std::move(Err), Obj->getFileName()); 774 DictScope D(W, "CGProfileEntry"); 775 W.printNumber("From", getSymbolName(*Obj->getSymbolByIndex(FromIndex)), 776 FromIndex); 777 W.printNumber("To", getSymbolName(*Obj->getSymbolByIndex(ToIndex)), 778 ToIndex); 779 W.printNumber("Weight", Count); 780 } 781 } 782 783 void MachODumper::printNeededLibraries() { 784 ListScope D(W, "NeededLibraries"); 785 786 using LibsTy = std::vector<StringRef>; 787 LibsTy Libs; 788 789 for (const auto &Command : Obj->load_commands()) { 790 if (Command.C.cmd == MachO::LC_LOAD_DYLIB || 791 Command.C.cmd == MachO::LC_ID_DYLIB || 792 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB || 793 Command.C.cmd == MachO::LC_REEXPORT_DYLIB || 794 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB || 795 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) { 796 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command); 797 if (Dl.dylib.name < Dl.cmdsize) { 798 auto *P = static_cast<const char*>(Command.Ptr) + Dl.dylib.name; 799 Libs.push_back(P); 800 } 801 } 802 } 803 804 llvm::stable_sort(Libs); 805 806 for (const auto &L : Libs) { 807 W.startLine() << L << "\n"; 808 } 809 } 810 811 void MachODumper::printMachODataInCode() { 812 for (const auto &Load : Obj->load_commands()) { 813 if (Load.C.cmd == MachO::LC_DATA_IN_CODE) { 814 MachO::linkedit_data_command LLC = Obj->getLinkeditDataLoadCommand(Load); 815 DictScope Group(W, "DataInCode"); 816 W.printNumber("Data offset", LLC.dataoff); 817 W.printNumber("Data size", LLC.datasize); 818 ListScope D(W, "Data entries"); 819 unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry); 820 for (unsigned i = 0; i < NumRegions; ++i) { 821 MachO::data_in_code_entry DICE = Obj->getDataInCodeTableEntry( 822 LLC.dataoff, i); 823 DictScope Group(W, "Entry"); 824 W.printNumber("Index", i); 825 W.printNumber("Offset", DICE.offset); 826 W.printNumber("Length", DICE.length); 827 W.printNumber("Kind", DICE.kind); 828 } 829 } 830 } 831 } 832 833 void MachODumper::printMachOVersionMin() { 834 for (const auto &Load : Obj->load_commands()) { 835 StringRef Cmd; 836 switch (Load.C.cmd) { 837 case MachO::LC_VERSION_MIN_MACOSX: 838 Cmd = "LC_VERSION_MIN_MACOSX"; 839 break; 840 case MachO::LC_VERSION_MIN_IPHONEOS: 841 Cmd = "LC_VERSION_MIN_IPHONEOS"; 842 break; 843 case MachO::LC_VERSION_MIN_TVOS: 844 Cmd = "LC_VERSION_MIN_TVOS"; 845 break; 846 case MachO::LC_VERSION_MIN_WATCHOS: 847 Cmd = "LC_VERSION_MIN_WATCHOS"; 848 break; 849 case MachO::LC_BUILD_VERSION: 850 Cmd = "LC_BUILD_VERSION"; 851 break; 852 default: 853 continue; 854 } 855 856 DictScope Group(W, "MinVersion"); 857 // Handle LC_BUILD_VERSION. 858 if (Load.C.cmd == MachO::LC_BUILD_VERSION) { 859 MachO::build_version_command BVC = Obj->getBuildVersionLoadCommand(Load); 860 W.printString("Cmd", Cmd); 861 W.printNumber("Size", BVC.cmdsize); 862 W.printString("Platform", 863 MachOObjectFile::getBuildPlatform(BVC.platform)); 864 W.printString("Version", MachOObjectFile::getVersionString(BVC.minos)); 865 if (BVC.sdk) 866 W.printString("SDK", MachOObjectFile::getVersionString(BVC.sdk)); 867 else 868 W.printString("SDK", StringRef("n/a")); 869 continue; 870 } 871 872 MachO::version_min_command VMC = Obj->getVersionMinLoadCommand(Load); 873 W.printString("Cmd", Cmd); 874 W.printNumber("Size", VMC.cmdsize); 875 SmallString<32> Version; 876 Version = utostr(MachOObjectFile::getVersionMinMajor(VMC, false)) + "." + 877 utostr(MachOObjectFile::getVersionMinMinor(VMC, false)); 878 uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, false); 879 if (Update != 0) 880 Version += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, false)); 881 W.printString("Version", Version); 882 SmallString<32> SDK; 883 if (VMC.sdk == 0) 884 SDK = "n/a"; 885 else { 886 SDK = utostr(MachOObjectFile::getVersionMinMajor(VMC, true)) + "." + 887 utostr(MachOObjectFile::getVersionMinMinor(VMC, true)); 888 uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, true); 889 if (Update != 0) 890 SDK += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, true)); 891 } 892 W.printString("SDK", SDK); 893 } 894 } 895 896 void MachODumper::printMachODysymtab() { 897 for (const auto &Load : Obj->load_commands()) { 898 if (Load.C.cmd == MachO::LC_DYSYMTAB) { 899 MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand(); 900 DictScope Group(W, "Dysymtab"); 901 W.printNumber("ilocalsym", DLC.ilocalsym); 902 W.printNumber("nlocalsym", DLC.nlocalsym); 903 W.printNumber("iextdefsym", DLC.iextdefsym); 904 W.printNumber("nextdefsym", DLC.nextdefsym); 905 W.printNumber("iundefsym", DLC.iundefsym); 906 W.printNumber("nundefsym", DLC.nundefsym); 907 W.printNumber("tocoff", DLC.tocoff); 908 W.printNumber("ntoc", DLC.ntoc); 909 W.printNumber("modtaboff", DLC.modtaboff); 910 W.printNumber("nmodtab", DLC.nmodtab); 911 W.printNumber("extrefsymoff", DLC.extrefsymoff); 912 W.printNumber("nextrefsyms", DLC.nextrefsyms); 913 W.printNumber("indirectsymoff", DLC.indirectsymoff); 914 W.printNumber("nindirectsyms", DLC.nindirectsyms); 915 W.printNumber("extreloff", DLC.extreloff); 916 W.printNumber("nextrel", DLC.nextrel); 917 W.printNumber("locreloff", DLC.locreloff); 918 W.printNumber("nlocrel", DLC.nlocrel); 919 } 920 } 921 } 922 923 void MachODumper::printMachOSegment() { 924 for (const auto &Load : Obj->load_commands()) { 925 if (Load.C.cmd == MachO::LC_SEGMENT || Load.C.cmd == MachO::LC_SEGMENT_64) { 926 MachOSegment MOSegment; 927 getSegment(Obj, Load, MOSegment); 928 DictScope Group(W, "Segment"); 929 W.printString("Cmd", MOSegment.CmdName); 930 W.printString("Name", MOSegment.SegName); 931 W.printNumber("Size", MOSegment.cmdsize); 932 W.printHex("vmaddr", MOSegment.vmaddr); 933 W.printHex("vmsize", MOSegment.vmsize); 934 W.printNumber("fileoff", MOSegment.fileoff); 935 W.printNumber("filesize", MOSegment.filesize); 936 W.printString("maxprot", getMask(MOSegment.maxprot)); 937 W.printString("initprot", getMask(MOSegment.initprot)); 938 W.printNumber("nsects", MOSegment.nsects); 939 W.printHex("flags", MOSegment.flags); 940 } 941 } 942 } 943 944 void MachODumper::printMachOIndirectSymbols() { 945 for (const auto &Load : Obj->load_commands()) { 946 if (Load.C.cmd == MachO::LC_DYSYMTAB) { 947 MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand(); 948 DictScope Group(W, "Indirect Symbols"); 949 W.printNumber("Number", DLC.nindirectsyms); 950 ListScope D(W, "Symbols"); 951 for (unsigned i = 0; i < DLC.nindirectsyms; ++i) { 952 DictScope Group(W, "Entry"); 953 W.printNumber("Entry Index", i); 954 W.printHex("Symbol Index", Obj->getIndirectSymbolTableEntry(DLC, i)); 955 } 956 } 957 } 958 } 959 960 void MachODumper::printMachOLinkerOptions() { 961 for (const auto &Load : Obj->load_commands()) { 962 if (Load.C.cmd == MachO::LC_LINKER_OPTION) { 963 MachO::linker_option_command LOLC = Obj->getLinkerOptionLoadCommand(Load); 964 DictScope Group(W, "Linker Options"); 965 W.printNumber("Size", LOLC.cmdsize); 966 ListScope D(W, "Strings"); 967 uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_option_command); 968 const char *P = Load.Ptr + sizeof(MachO::linker_option_command); 969 StringRef Data(P, DataSize); 970 for (unsigned i = 0; i < LOLC.count; ++i) { 971 std::pair<StringRef,StringRef> Split = Data.split('\0'); 972 W.printString("Value", Split.first); 973 Data = Split.second; 974 } 975 } 976 } 977 } 978