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