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