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 { "ThumbDef", 0x8 }, 260 { "ReferencedDynamically", 0x10 }, 261 { "NoDeadStrip", 0x20 }, 262 { "WeakRef", 0x40 }, 263 { "WeakDef", 0x80 }, 264 { "SymbolResolver", 0x100 }, 265 { "AltEntry", 0x200 }, 266 { "ColdFunc", 0x400 }, 267 }; 268 269 static const EnumEntry<unsigned> MachOSymbolTypes[] = { 270 { "Undef", 0x0 }, 271 { "Abs", 0x2 }, 272 { "Indirect", 0xA }, 273 { "PreboundUndef", 0xC }, 274 { "Section", 0xE } 275 }; 276 277 namespace { 278 struct MachOSection { 279 ArrayRef<char> Name; 280 ArrayRef<char> SegmentName; 281 uint64_t Address; 282 uint64_t Size; 283 uint32_t Offset; 284 uint32_t Alignment; 285 uint32_t RelocationTableOffset; 286 uint32_t NumRelocationTableEntries; 287 uint32_t Flags; 288 uint32_t Reserved1; 289 uint32_t Reserved2; 290 uint32_t Reserved3; 291 }; 292 293 struct MachOSegment { 294 std::string CmdName; 295 std::string SegName; 296 uint64_t cmdsize; 297 uint64_t vmaddr; 298 uint64_t vmsize; 299 uint64_t fileoff; 300 uint64_t filesize; 301 uint32_t maxprot; 302 uint32_t initprot; 303 uint32_t nsects; 304 uint32_t flags; 305 }; 306 307 struct MachOSymbol { 308 uint32_t StringIndex; 309 uint8_t Type; 310 uint8_t SectionIndex; 311 uint16_t Flags; 312 uint64_t Value; 313 }; 314 } 315 316 static std::string getMask(uint32_t prot) 317 { 318 // TODO (davide): This always assumes prot is valid. 319 // Catch mistakes and report if needed. 320 std::string Prot; 321 Prot = ""; 322 Prot += (prot & MachO::VM_PROT_READ) ? "r" : "-"; 323 Prot += (prot & MachO::VM_PROT_WRITE) ? "w" : "-"; 324 Prot += (prot & MachO::VM_PROT_EXECUTE) ? "x" : "-"; 325 return Prot; 326 } 327 328 static void getSection(const MachOObjectFile *Obj, 329 DataRefImpl Sec, 330 MachOSection &Section) { 331 if (!Obj->is64Bit()) { 332 MachO::section Sect = Obj->getSection(Sec); 333 Section.Address = Sect.addr; 334 Section.Size = Sect.size; 335 Section.Offset = Sect.offset; 336 Section.Alignment = Sect.align; 337 Section.RelocationTableOffset = Sect.reloff; 338 Section.NumRelocationTableEntries = Sect.nreloc; 339 Section.Flags = Sect.flags; 340 Section.Reserved1 = Sect.reserved1; 341 Section.Reserved2 = Sect.reserved2; 342 return; 343 } 344 MachO::section_64 Sect = Obj->getSection64(Sec); 345 Section.Address = Sect.addr; 346 Section.Size = Sect.size; 347 Section.Offset = Sect.offset; 348 Section.Alignment = Sect.align; 349 Section.RelocationTableOffset = Sect.reloff; 350 Section.NumRelocationTableEntries = Sect.nreloc; 351 Section.Flags = Sect.flags; 352 Section.Reserved1 = Sect.reserved1; 353 Section.Reserved2 = Sect.reserved2; 354 Section.Reserved3 = Sect.reserved3; 355 } 356 357 static void getSegment(const MachOObjectFile *Obj, 358 const MachOObjectFile::LoadCommandInfo &L, 359 MachOSegment &Segment) { 360 if (!Obj->is64Bit()) { 361 MachO::segment_command SC = Obj->getSegmentLoadCommand(L); 362 Segment.CmdName = "LC_SEGMENT"; 363 Segment.SegName = SC.segname; 364 Segment.cmdsize = SC.cmdsize; 365 Segment.vmaddr = SC.vmaddr; 366 Segment.vmsize = SC.vmsize; 367 Segment.fileoff = SC.fileoff; 368 Segment.filesize = SC.filesize; 369 Segment.maxprot = SC.maxprot; 370 Segment.initprot = SC.initprot; 371 Segment.nsects = SC.nsects; 372 Segment.flags = SC.flags; 373 return; 374 } 375 MachO::segment_command_64 SC = Obj->getSegment64LoadCommand(L); 376 Segment.CmdName = "LC_SEGMENT_64"; 377 Segment.SegName = SC.segname; 378 Segment.cmdsize = SC.cmdsize; 379 Segment.vmaddr = SC.vmaddr; 380 Segment.vmsize = SC.vmsize; 381 Segment.fileoff = SC.fileoff; 382 Segment.filesize = SC.filesize; 383 Segment.maxprot = SC.maxprot; 384 Segment.initprot = SC.initprot; 385 Segment.nsects = SC.nsects; 386 Segment.flags = SC.flags; 387 } 388 389 static void getSymbol(const MachOObjectFile *Obj, 390 DataRefImpl DRI, 391 MachOSymbol &Symbol) { 392 if (!Obj->is64Bit()) { 393 MachO::nlist Entry = Obj->getSymbolTableEntry(DRI); 394 Symbol.StringIndex = Entry.n_strx; 395 Symbol.Type = Entry.n_type; 396 Symbol.SectionIndex = Entry.n_sect; 397 Symbol.Flags = Entry.n_desc; 398 Symbol.Value = Entry.n_value; 399 return; 400 } 401 MachO::nlist_64 Entry = Obj->getSymbol64TableEntry(DRI); 402 Symbol.StringIndex = Entry.n_strx; 403 Symbol.Type = Entry.n_type; 404 Symbol.SectionIndex = Entry.n_sect; 405 Symbol.Flags = Entry.n_desc; 406 Symbol.Value = Entry.n_value; 407 } 408 409 void MachODumper::printFileHeaders() { 410 DictScope H(W, "MachHeader"); 411 if (!Obj->is64Bit()) { 412 printFileHeaders(Obj->getHeader()); 413 } else { 414 printFileHeaders(Obj->getHeader64()); 415 W.printHex("Reserved", Obj->getHeader64().reserved); 416 } 417 } 418 419 template<class MachHeader> 420 void MachODumper::printFileHeaders(const MachHeader &Header) { 421 W.printEnum("Magic", Header.magic, makeArrayRef(MachOMagics)); 422 W.printEnum("CpuType", Header.cputype, makeArrayRef(MachOHeaderCpuTypes)); 423 uint32_t subtype = Header.cpusubtype & ~MachO::CPU_SUBTYPE_MASK; 424 switch (Header.cputype) { 425 case MachO::CPU_TYPE_X86: 426 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX86)); 427 break; 428 case MachO::CPU_TYPE_X86_64: 429 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX64)); 430 break; 431 case MachO::CPU_TYPE_ARM: 432 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM)); 433 break; 434 case MachO::CPU_TYPE_POWERPC: 435 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesPPC)); 436 break; 437 case MachO::CPU_TYPE_SPARC: 438 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesSPARC)); 439 break; 440 case MachO::CPU_TYPE_ARM64: 441 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM64)); 442 break; 443 case MachO::CPU_TYPE_POWERPC64: 444 default: 445 W.printHex("CpuSubtype", subtype); 446 } 447 W.printEnum("FileType", Header.filetype, makeArrayRef(MachOHeaderFileTypes)); 448 W.printNumber("NumOfLoadCommands", Header.ncmds); 449 W.printNumber("SizeOfLoadCommands", Header.sizeofcmds); 450 W.printFlags("Flags", Header.flags, makeArrayRef(MachOHeaderFlags)); 451 } 452 453 void MachODumper::printSectionHeaders() { return printSectionHeaders(Obj); } 454 455 void MachODumper::printSectionHeaders(const MachOObjectFile *Obj) { 456 ListScope Group(W, "Sections"); 457 458 int SectionIndex = -1; 459 for (const SectionRef &Section : Obj->sections()) { 460 ++SectionIndex; 461 462 MachOSection MOSection; 463 getSection(Obj, Section.getRawDataRefImpl(), MOSection); 464 DataRefImpl DR = Section.getRawDataRefImpl(); 465 StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName()); 466 ArrayRef<char> RawName = Obj->getSectionRawName(DR); 467 StringRef SegmentName = Obj->getSectionFinalSegmentName(DR); 468 ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR); 469 470 DictScope SectionD(W, "Section"); 471 W.printNumber("Index", SectionIndex); 472 W.printBinary("Name", Name, RawName); 473 W.printBinary("Segment", SegmentName, RawSegmentName); 474 W.printHex("Address", MOSection.Address); 475 W.printHex("Size", MOSection.Size); 476 W.printNumber("Offset", MOSection.Offset); 477 W.printNumber("Alignment", MOSection.Alignment); 478 W.printHex("RelocationOffset", MOSection.RelocationTableOffset); 479 W.printNumber("RelocationCount", MOSection.NumRelocationTableEntries); 480 W.printEnum("Type", MOSection.Flags & 0xFF, 481 makeArrayRef(MachOSectionTypes)); 482 W.printFlags("Attributes", MOSection.Flags >> 8, 483 makeArrayRef(MachOSectionAttributes)); 484 W.printHex("Reserved1", MOSection.Reserved1); 485 W.printHex("Reserved2", MOSection.Reserved2); 486 if (Obj->is64Bit()) 487 W.printHex("Reserved3", MOSection.Reserved3); 488 489 if (opts::SectionRelocations) { 490 ListScope D(W, "Relocations"); 491 for (const RelocationRef &Reloc : Section.relocations()) 492 printRelocation(Reloc); 493 } 494 495 if (opts::SectionSymbols) { 496 ListScope D(W, "Symbols"); 497 for (const SymbolRef &Symbol : Obj->symbols()) { 498 if (!Section.containsSymbol(Symbol)) 499 continue; 500 501 printSymbol(Symbol); 502 } 503 } 504 505 if (opts::SectionData && !Section.isBSS()) 506 W.printBinaryBlock("SectionData", unwrapOrError(Obj->getFileName(), 507 Section.getContents())); 508 } 509 } 510 511 void MachODumper::printRelocations() { 512 ListScope D(W, "Relocations"); 513 514 std::error_code EC; 515 for (const SectionRef &Section : Obj->sections()) { 516 StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName()); 517 bool PrintedGroup = false; 518 for (const RelocationRef &Reloc : Section.relocations()) { 519 if (!PrintedGroup) { 520 W.startLine() << "Section " << Name << " {\n"; 521 W.indent(); 522 PrintedGroup = true; 523 } 524 525 printRelocation(Reloc); 526 } 527 528 if (PrintedGroup) { 529 W.unindent(); 530 W.startLine() << "}\n"; 531 } 532 } 533 } 534 535 void MachODumper::printRelocation(const RelocationRef &Reloc) { 536 return printRelocation(Obj, Reloc); 537 } 538 539 void MachODumper::printRelocation(const MachOObjectFile *Obj, 540 const RelocationRef &Reloc) { 541 uint64_t Offset = Reloc.getOffset(); 542 SmallString<32> RelocName; 543 Reloc.getTypeName(RelocName); 544 545 DataRefImpl DR = Reloc.getRawDataRefImpl(); 546 MachO::any_relocation_info RE = Obj->getRelocation(DR); 547 bool IsScattered = Obj->isRelocationScattered(RE); 548 bool IsExtern = !IsScattered && Obj->getPlainRelocationExternal(RE); 549 550 StringRef TargetName; 551 if (IsExtern) { 552 symbol_iterator Symbol = Reloc.getSymbol(); 553 if (Symbol != Obj->symbol_end()) { 554 Expected<StringRef> TargetNameOrErr = Symbol->getName(); 555 if (!TargetNameOrErr) 556 reportError(TargetNameOrErr.takeError(), Obj->getFileName()); 557 TargetName = *TargetNameOrErr; 558 } 559 } else if (!IsScattered) { 560 section_iterator SecI = Obj->getRelocationSection(DR); 561 if (SecI != Obj->section_end()) 562 TargetName = unwrapOrError(Obj->getFileName(), SecI->getName()); 563 } 564 if (TargetName.empty()) 565 TargetName = "-"; 566 567 if (opts::ExpandRelocs) { 568 DictScope Group(W, "Relocation"); 569 W.printHex("Offset", Offset); 570 W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE)); 571 W.printNumber("Length", Obj->getAnyRelocationLength(RE)); 572 W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE)); 573 if (IsScattered) { 574 W.printHex("Value", Obj->getScatteredRelocationValue(RE)); 575 } else { 576 const char *Kind = IsExtern ? "Symbol" : "Section"; 577 W.printNumber(Kind, TargetName, Obj->getPlainRelocationSymbolNum(RE)); 578 } 579 } else { 580 SmallString<32> SymbolNameOrOffset("0x"); 581 if (IsScattered) { 582 // Scattered relocations don't really have an associated symbol for some 583 // reason, even if one exists in the symtab at the correct address. 584 SymbolNameOrOffset += utohexstr(Obj->getScatteredRelocationValue(RE)); 585 } else { 586 SymbolNameOrOffset = TargetName; 587 } 588 589 raw_ostream& OS = W.startLine(); 590 OS << W.hex(Offset) 591 << " " << Obj->getAnyRelocationPCRel(RE) 592 << " " << Obj->getAnyRelocationLength(RE); 593 if (IsScattered) 594 OS << " n/a"; 595 else 596 OS << " " << Obj->getPlainRelocationExternal(RE); 597 OS << " " << RelocName 598 << " " << IsScattered 599 << " " << SymbolNameOrOffset 600 << "\n"; 601 } 602 } 603 604 void MachODumper::printSymbols() { 605 ListScope Group(W, "Symbols"); 606 607 for (const SymbolRef &Symbol : Obj->symbols()) { 608 printSymbol(Symbol); 609 } 610 } 611 612 void MachODumper::printDynamicSymbols() { 613 ListScope Group(W, "DynamicSymbols"); 614 } 615 616 void MachODumper::printSymbol(const SymbolRef &Symbol) { 617 StringRef SymbolName; 618 Expected<StringRef> SymbolNameOrErr = Symbol.getName(); 619 if (!SymbolNameOrErr) { 620 // TODO: Actually report errors helpfully. 621 consumeError(SymbolNameOrErr.takeError()); 622 } else 623 SymbolName = *SymbolNameOrErr; 624 625 MachOSymbol MOSymbol; 626 getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol); 627 628 StringRef SectionName = ""; 629 // Don't ask a Mach-O STABS symbol for its section unless we know that 630 // STAB symbol's section field refers to a valid section index. Otherwise 631 // the symbol may error trying to load a section that does not exist. 632 // TODO: Add a whitelist of STABS symbol types that contain valid section 633 // indices. 634 if (!(MOSymbol.Type & MachO::N_STAB)) { 635 Expected<section_iterator> SecIOrErr = Symbol.getSection(); 636 if (!SecIOrErr) 637 reportError(SecIOrErr.takeError(), Obj->getFileName()); 638 639 section_iterator SecI = *SecIOrErr; 640 if (SecI != Obj->section_end()) 641 SectionName = unwrapOrError(Obj->getFileName(), SecI->getName()); 642 } 643 644 DictScope D(W, "Symbol"); 645 W.printNumber("Name", SymbolName, MOSymbol.StringIndex); 646 if (MOSymbol.Type & MachO::N_STAB) { 647 W.printHex("Type", "SymDebugTable", MOSymbol.Type); 648 } else { 649 if (MOSymbol.Type & MachO::N_PEXT) 650 W.startLine() << "PrivateExtern\n"; 651 if (MOSymbol.Type & MachO::N_EXT) 652 W.startLine() << "Extern\n"; 653 W.printEnum("Type", uint8_t(MOSymbol.Type & MachO::N_TYPE), 654 makeArrayRef(MachOSymbolTypes)); 655 } 656 W.printHex("Section", SectionName, MOSymbol.SectionIndex); 657 W.printEnum("RefType", static_cast<uint16_t>(MOSymbol.Flags & 0x7), 658 makeArrayRef(MachOSymbolRefTypes)); 659 W.printFlags("Flags", static_cast<uint16_t>(MOSymbol.Flags & ~0x7), 660 makeArrayRef(MachOSymbolFlags)); 661 W.printHex("Value", MOSymbol.Value); 662 } 663 664 void MachODumper::printUnwindInfo() { 665 W.startLine() << "UnwindInfo not implemented.\n"; 666 } 667 668 void MachODumper::printStackMap() const { 669 object::SectionRef StackMapSection; 670 for (auto Sec : Obj->sections()) { 671 StringRef Name; 672 if (Expected<StringRef> NameOrErr = Sec.getName()) 673 Name = *NameOrErr; 674 else 675 consumeError(NameOrErr.takeError()); 676 677 if (Name == "__llvm_stackmaps") { 678 StackMapSection = Sec; 679 break; 680 } 681 } 682 683 if (StackMapSection == object::SectionRef()) 684 return; 685 686 StringRef StackMapContents = 687 unwrapOrError(Obj->getFileName(), StackMapSection.getContents()); 688 ArrayRef<uint8_t> StackMapContentsArray = 689 arrayRefFromStringRef(StackMapContents); 690 691 if (Obj->isLittleEndian()) 692 prettyPrintStackMap( 693 W, StackMapParser<support::little>(StackMapContentsArray)); 694 else 695 prettyPrintStackMap( 696 W, StackMapParser<support::big>(StackMapContentsArray)); 697 } 698 699 void MachODumper::printNeededLibraries() { 700 ListScope D(W, "NeededLibraries"); 701 702 using LibsTy = std::vector<StringRef>; 703 LibsTy Libs; 704 705 for (const auto &Command : Obj->load_commands()) { 706 if (Command.C.cmd == MachO::LC_LOAD_DYLIB || 707 Command.C.cmd == MachO::LC_ID_DYLIB || 708 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB || 709 Command.C.cmd == MachO::LC_REEXPORT_DYLIB || 710 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB || 711 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) { 712 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command); 713 if (Dl.dylib.name < Dl.cmdsize) { 714 auto *P = static_cast<const char*>(Command.Ptr) + Dl.dylib.name; 715 Libs.push_back(P); 716 } 717 } 718 } 719 720 llvm::stable_sort(Libs); 721 722 for (const auto &L : Libs) { 723 W.startLine() << L << "\n"; 724 } 725 } 726 727 void MachODumper::printMachODataInCode() { 728 for (const auto &Load : Obj->load_commands()) { 729 if (Load.C.cmd == MachO::LC_DATA_IN_CODE) { 730 MachO::linkedit_data_command LLC = Obj->getLinkeditDataLoadCommand(Load); 731 DictScope Group(W, "DataInCode"); 732 W.printNumber("Data offset", LLC.dataoff); 733 W.printNumber("Data size", LLC.datasize); 734 ListScope D(W, "Data entries"); 735 unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry); 736 for (unsigned i = 0; i < NumRegions; ++i) { 737 MachO::data_in_code_entry DICE = Obj->getDataInCodeTableEntry( 738 LLC.dataoff, i); 739 DictScope Group(W, "Entry"); 740 W.printNumber("Index", i); 741 W.printNumber("Offset", DICE.offset); 742 W.printNumber("Length", DICE.length); 743 W.printNumber("Kind", DICE.kind); 744 } 745 } 746 } 747 } 748 749 void MachODumper::printMachOVersionMin() { 750 for (const auto &Load : Obj->load_commands()) { 751 StringRef Cmd; 752 switch (Load.C.cmd) { 753 case MachO::LC_VERSION_MIN_MACOSX: 754 Cmd = "LC_VERSION_MIN_MACOSX"; 755 break; 756 case MachO::LC_VERSION_MIN_IPHONEOS: 757 Cmd = "LC_VERSION_MIN_IPHONEOS"; 758 break; 759 case MachO::LC_VERSION_MIN_TVOS: 760 Cmd = "LC_VERSION_MIN_TVOS"; 761 break; 762 case MachO::LC_VERSION_MIN_WATCHOS: 763 Cmd = "LC_VERSION_MIN_WATCHOS"; 764 break; 765 case MachO::LC_BUILD_VERSION: 766 Cmd = "LC_BUILD_VERSION"; 767 break; 768 default: 769 continue; 770 } 771 772 DictScope Group(W, "MinVersion"); 773 // Handle LC_BUILD_VERSION. 774 if (Load.C.cmd == MachO::LC_BUILD_VERSION) { 775 MachO::build_version_command BVC = Obj->getBuildVersionLoadCommand(Load); 776 W.printString("Cmd", Cmd); 777 W.printNumber("Size", BVC.cmdsize); 778 W.printString("Platform", 779 MachOObjectFile::getBuildPlatform(BVC.platform)); 780 W.printString("Version", MachOObjectFile::getVersionString(BVC.minos)); 781 if (BVC.sdk) 782 W.printString("SDK", MachOObjectFile::getVersionString(BVC.sdk)); 783 else 784 W.printString("SDK", StringRef("n/a")); 785 continue; 786 } 787 788 MachO::version_min_command VMC = Obj->getVersionMinLoadCommand(Load); 789 W.printString("Cmd", Cmd); 790 W.printNumber("Size", VMC.cmdsize); 791 SmallString<32> Version; 792 Version = utostr(MachOObjectFile::getVersionMinMajor(VMC, false)) + "." + 793 utostr(MachOObjectFile::getVersionMinMinor(VMC, false)); 794 uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, false); 795 if (Update != 0) 796 Version += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, false)); 797 W.printString("Version", Version); 798 SmallString<32> SDK; 799 if (VMC.sdk == 0) 800 SDK = "n/a"; 801 else { 802 SDK = utostr(MachOObjectFile::getVersionMinMajor(VMC, true)) + "." + 803 utostr(MachOObjectFile::getVersionMinMinor(VMC, true)); 804 uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, true); 805 if (Update != 0) 806 SDK += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, true)); 807 } 808 W.printString("SDK", SDK); 809 } 810 } 811 812 void MachODumper::printMachODysymtab() { 813 for (const auto &Load : Obj->load_commands()) { 814 if (Load.C.cmd == MachO::LC_DYSYMTAB) { 815 MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand(); 816 DictScope Group(W, "Dysymtab"); 817 W.printNumber("ilocalsym", DLC.ilocalsym); 818 W.printNumber("nlocalsym", DLC.nlocalsym); 819 W.printNumber("iextdefsym", DLC.iextdefsym); 820 W.printNumber("nextdefsym", DLC.nextdefsym); 821 W.printNumber("iundefsym", DLC.iundefsym); 822 W.printNumber("nundefsym", DLC.nundefsym); 823 W.printNumber("tocoff", DLC.tocoff); 824 W.printNumber("ntoc", DLC.ntoc); 825 W.printNumber("modtaboff", DLC.modtaboff); 826 W.printNumber("nmodtab", DLC.nmodtab); 827 W.printNumber("extrefsymoff", DLC.extrefsymoff); 828 W.printNumber("nextrefsyms", DLC.nextrefsyms); 829 W.printNumber("indirectsymoff", DLC.indirectsymoff); 830 W.printNumber("nindirectsyms", DLC.nindirectsyms); 831 W.printNumber("extreloff", DLC.extreloff); 832 W.printNumber("nextrel", DLC.nextrel); 833 W.printNumber("locreloff", DLC.locreloff); 834 W.printNumber("nlocrel", DLC.nlocrel); 835 } 836 } 837 } 838 839 void MachODumper::printMachOSegment() { 840 for (const auto &Load : Obj->load_commands()) { 841 if (Load.C.cmd == MachO::LC_SEGMENT || Load.C.cmd == MachO::LC_SEGMENT_64) { 842 MachOSegment MOSegment; 843 getSegment(Obj, Load, MOSegment); 844 DictScope Group(W, "Segment"); 845 W.printString("Cmd", MOSegment.CmdName); 846 W.printString("Name", MOSegment.SegName); 847 W.printNumber("Size", MOSegment.cmdsize); 848 W.printHex("vmaddr", MOSegment.vmaddr); 849 W.printHex("vmsize", MOSegment.vmsize); 850 W.printNumber("fileoff", MOSegment.fileoff); 851 W.printNumber("filesize", MOSegment.filesize); 852 W.printString("maxprot", getMask(MOSegment.maxprot)); 853 W.printString("initprot", getMask(MOSegment.initprot)); 854 W.printNumber("nsects", MOSegment.nsects); 855 W.printHex("flags", MOSegment.flags); 856 } 857 } 858 } 859 860 void MachODumper::printMachOIndirectSymbols() { 861 for (const auto &Load : Obj->load_commands()) { 862 if (Load.C.cmd == MachO::LC_DYSYMTAB) { 863 MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand(); 864 DictScope Group(W, "Indirect Symbols"); 865 W.printNumber("Number", DLC.nindirectsyms); 866 ListScope D(W, "Symbols"); 867 for (unsigned i = 0; i < DLC.nindirectsyms; ++i) { 868 DictScope Group(W, "Entry"); 869 W.printNumber("Entry Index", i); 870 W.printHex("Symbol Index", Obj->getIndirectSymbolTableEntry(DLC, i)); 871 } 872 } 873 } 874 } 875 876 void MachODumper::printMachOLinkerOptions() { 877 for (const auto &Load : Obj->load_commands()) { 878 if (Load.C.cmd == MachO::LC_LINKER_OPTION) { 879 MachO::linker_option_command LOLC = Obj->getLinkerOptionLoadCommand(Load); 880 DictScope Group(W, "Linker Options"); 881 W.printNumber("Size", LOLC.cmdsize); 882 ListScope D(W, "Strings"); 883 uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_option_command); 884 const char *P = Load.Ptr + sizeof(MachO::linker_option_command); 885 StringRef Data(P, DataSize); 886 for (unsigned i = 0; i < LOLC.count; ++i) { 887 std::pair<StringRef,StringRef> Split = Data.split('\0'); 888 W.printString("Value", Split.first); 889 Data = Split.second; 890 } 891 } 892 } 893 } 894