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