1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the MachO-specific dumper for llvm-readobj. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm-readobj.h" 15 #include "Error.h" 16 #include "ObjDumper.h" 17 #include "StackMapPrinter.h" 18 #include "StreamWriter.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/ADT/StringExtras.h" 21 #include "llvm/Object/MachO.h" 22 #include "llvm/Support/Casting.h" 23 24 using namespace llvm; 25 using namespace object; 26 27 namespace { 28 29 class MachODumper : public ObjDumper { 30 public: 31 MachODumper(const MachOObjectFile *Obj, StreamWriter& Writer) 32 : ObjDumper(Writer) 33 , Obj(Obj) { } 34 35 void printFileHeaders() override; 36 void printSections() override; 37 void printRelocations() override; 38 void printSymbols() override; 39 void printDynamicSymbols() override; 40 void printUnwindInfo() override; 41 void printStackMap() const override; 42 43 // MachO-specific. 44 void printMachODataInCode() override; 45 void printMachOVersionMin() override; 46 void printMachODysymtab() override; 47 void printMachOSegment() override; 48 void printMachOIndirectSymbols() override; 49 void printMachOLinkerOptions () override; 50 51 private: 52 template<class MachHeader> 53 void printFileHeaders(const MachHeader &Header); 54 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 printSections(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 StreamWriter &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 }; 166 167 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesSPARC[] = { 168 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_SPARC_ALL), 169 }; 170 171 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesPPC[] = { 172 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_ALL), 173 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_601), 174 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_602), 175 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603), 176 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603e), 177 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603ev), 178 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604), 179 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604e), 180 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_620), 181 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_750), 182 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7400), 183 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7450), 184 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_970), 185 }; 186 187 static const EnumEntry<uint32_t> MachOHeaderFlags[] = { 188 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOUNDEFS), 189 LLVM_READOBJ_ENUM_ENT(MachO, MH_INCRLINK), 190 LLVM_READOBJ_ENUM_ENT(MachO, MH_DYLDLINK), 191 LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDATLOAD), 192 LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBOUND), 193 LLVM_READOBJ_ENUM_ENT(MachO, MH_SPLIT_SEGS), 194 LLVM_READOBJ_ENUM_ENT(MachO, MH_LAZY_INIT), 195 LLVM_READOBJ_ENUM_ENT(MachO, MH_TWOLEVEL), 196 LLVM_READOBJ_ENUM_ENT(MachO, MH_FORCE_FLAT), 197 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOMULTIDEFS), 198 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOFIXPREBINDING), 199 LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBINDABLE), 200 LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLMODSBOUND), 201 LLVM_READOBJ_ENUM_ENT(MachO, MH_SUBSECTIONS_VIA_SYMBOLS), 202 LLVM_READOBJ_ENUM_ENT(MachO, MH_CANONICAL), 203 LLVM_READOBJ_ENUM_ENT(MachO, MH_WEAK_DEFINES), 204 LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDS_TO_WEAK), 205 LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLOW_STACK_EXECUTION), 206 LLVM_READOBJ_ENUM_ENT(MachO, MH_ROOT_SAFE), 207 LLVM_READOBJ_ENUM_ENT(MachO, MH_SETUID_SAFE), 208 LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_REEXPORTED_DYLIBS), 209 LLVM_READOBJ_ENUM_ENT(MachO, MH_PIE), 210 LLVM_READOBJ_ENUM_ENT(MachO, MH_DEAD_STRIPPABLE_DYLIB), 211 LLVM_READOBJ_ENUM_ENT(MachO, MH_HAS_TLV_DESCRIPTORS), 212 LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_HEAP_EXECUTION), 213 LLVM_READOBJ_ENUM_ENT(MachO, MH_APP_EXTENSION_SAFE), 214 }; 215 216 static const EnumEntry<unsigned> MachOSectionAttributes[] = { 217 { "LocReloc" , 1 << 0 /*S_ATTR_LOC_RELOC */ }, 218 { "ExtReloc" , 1 << 1 /*S_ATTR_EXT_RELOC */ }, 219 { "SomeInstructions" , 1 << 2 /*S_ATTR_SOME_INSTRUCTIONS */ }, 220 { "Debug" , 1 << 17 /*S_ATTR_DEBUG */ }, 221 { "SelfModifyingCode", 1 << 18 /*S_ATTR_SELF_MODIFYING_CODE*/ }, 222 { "LiveSupport" , 1 << 19 /*S_ATTR_LIVE_SUPPORT */ }, 223 { "NoDeadStrip" , 1 << 20 /*S_ATTR_NO_DEAD_STRIP */ }, 224 { "StripStaticSyms" , 1 << 21 /*S_ATTR_STRIP_STATIC_SYMS */ }, 225 { "NoTOC" , 1 << 22 /*S_ATTR_NO_TOC */ }, 226 { "PureInstructions" , 1 << 23 /*S_ATTR_PURE_INSTRUCTIONS */ }, 227 }; 228 229 static const EnumEntry<unsigned> MachOSymbolRefTypes[] = { 230 { "UndefinedNonLazy", 0 }, 231 { "ReferenceFlagUndefinedLazy", 1 }, 232 { "ReferenceFlagDefined", 2 }, 233 { "ReferenceFlagPrivateDefined", 3 }, 234 { "ReferenceFlagPrivateUndefinedNonLazy", 4 }, 235 { "ReferenceFlagPrivateUndefinedLazy", 5 } 236 }; 237 238 static const EnumEntry<unsigned> MachOSymbolFlags[] = { 239 { "ReferencedDynamically", 0x10 }, 240 { "NoDeadStrip", 0x20 }, 241 { "WeakRef", 0x40 }, 242 { "WeakDef", 0x80 } 243 }; 244 245 static const EnumEntry<unsigned> MachOSymbolTypes[] = { 246 { "Undef", 0x0 }, 247 { "Abs", 0x2 }, 248 { "Indirect", 0xA }, 249 { "PreboundUndef", 0xC }, 250 { "Section", 0xE } 251 }; 252 253 namespace { 254 struct MachOSection { 255 ArrayRef<char> Name; 256 ArrayRef<char> SegmentName; 257 uint64_t Address; 258 uint64_t Size; 259 uint32_t Offset; 260 uint32_t Alignment; 261 uint32_t RelocationTableOffset; 262 uint32_t NumRelocationTableEntries; 263 uint32_t Flags; 264 uint32_t Reserved1; 265 uint32_t Reserved2; 266 uint32_t Reserved3; 267 }; 268 269 struct MachOSegment { 270 std::string CmdName; 271 std::string SegName; 272 uint64_t cmdsize; 273 uint64_t vmaddr; 274 uint64_t vmsize; 275 uint64_t fileoff; 276 uint64_t filesize; 277 uint32_t maxprot; 278 uint32_t initprot; 279 uint32_t nsects; 280 uint32_t flags; 281 }; 282 283 struct MachOSymbol { 284 uint32_t StringIndex; 285 uint8_t Type; 286 uint8_t SectionIndex; 287 uint16_t Flags; 288 uint64_t Value; 289 }; 290 } 291 292 static std::string getMask(uint32_t prot) 293 { 294 // TODO (davide): This always assumes prot is valid. 295 // Catch mistakes and report if needed. 296 std::string Prot; 297 Prot = ""; 298 Prot += (prot & MachO::VM_PROT_READ) ? "r" : "-"; 299 Prot += (prot & MachO::VM_PROT_WRITE) ? "w" : "-"; 300 Prot += (prot & MachO::VM_PROT_EXECUTE) ? "x" : "-"; 301 return Prot; 302 } 303 304 static void getSection(const MachOObjectFile *Obj, 305 DataRefImpl Sec, 306 MachOSection &Section) { 307 if (!Obj->is64Bit()) { 308 MachO::section Sect = Obj->getSection(Sec); 309 Section.Address = Sect.addr; 310 Section.Size = Sect.size; 311 Section.Offset = Sect.offset; 312 Section.Alignment = Sect.align; 313 Section.RelocationTableOffset = Sect.reloff; 314 Section.NumRelocationTableEntries = Sect.nreloc; 315 Section.Flags = Sect.flags; 316 Section.Reserved1 = Sect.reserved1; 317 Section.Reserved2 = Sect.reserved2; 318 return; 319 } 320 MachO::section_64 Sect = Obj->getSection64(Sec); 321 Section.Address = Sect.addr; 322 Section.Size = Sect.size; 323 Section.Offset = Sect.offset; 324 Section.Alignment = Sect.align; 325 Section.RelocationTableOffset = Sect.reloff; 326 Section.NumRelocationTableEntries = Sect.nreloc; 327 Section.Flags = Sect.flags; 328 Section.Reserved1 = Sect.reserved1; 329 Section.Reserved2 = Sect.reserved2; 330 Section.Reserved3 = Sect.reserved3; 331 } 332 333 static void getSegment(const MachOObjectFile *Obj, 334 const MachOObjectFile::LoadCommandInfo &L, 335 MachOSegment &Segment) { 336 if (!Obj->is64Bit()) { 337 MachO::segment_command SC = Obj->getSegmentLoadCommand(L); 338 Segment.CmdName = "LC_SEGMENT"; 339 Segment.SegName = SC.segname; 340 Segment.cmdsize = SC.cmdsize; 341 Segment.vmaddr = SC.vmaddr; 342 Segment.vmsize = SC.vmsize; 343 Segment.fileoff = SC.fileoff; 344 Segment.filesize = SC.filesize; 345 Segment.maxprot = SC.maxprot; 346 Segment.initprot = SC.initprot; 347 Segment.nsects = SC.nsects; 348 Segment.flags = SC.flags; 349 return; 350 } 351 MachO::segment_command_64 SC = Obj->getSegment64LoadCommand(L); 352 Segment.CmdName = "LC_SEGMENT_64"; 353 Segment.SegName = SC.segname; 354 Segment.cmdsize = SC.cmdsize; 355 Segment.vmaddr = SC.vmaddr; 356 Segment.vmsize = SC.vmsize; 357 Segment.fileoff = SC.fileoff; 358 Segment.filesize = SC.filesize; 359 Segment.maxprot = SC.maxprot; 360 Segment.initprot = SC.initprot; 361 Segment.nsects = SC.nsects; 362 Segment.flags = SC.flags; 363 } 364 365 static void getSymbol(const MachOObjectFile *Obj, 366 DataRefImpl DRI, 367 MachOSymbol &Symbol) { 368 if (!Obj->is64Bit()) { 369 MachO::nlist Entry = Obj->getSymbolTableEntry(DRI); 370 Symbol.StringIndex = Entry.n_strx; 371 Symbol.Type = Entry.n_type; 372 Symbol.SectionIndex = Entry.n_sect; 373 Symbol.Flags = Entry.n_desc; 374 Symbol.Value = Entry.n_value; 375 return; 376 } 377 MachO::nlist_64 Entry = Obj->getSymbol64TableEntry(DRI); 378 Symbol.StringIndex = Entry.n_strx; 379 Symbol.Type = Entry.n_type; 380 Symbol.SectionIndex = Entry.n_sect; 381 Symbol.Flags = Entry.n_desc; 382 Symbol.Value = Entry.n_value; 383 } 384 385 void MachODumper::printFileHeaders() { 386 DictScope H(W, "MachHeader"); 387 if (!Obj->is64Bit()) { 388 printFileHeaders(Obj->getHeader()); 389 } else { 390 printFileHeaders(Obj->getHeader64()); 391 W.printHex("Reserved", Obj->getHeader64().reserved); 392 } 393 } 394 395 template<class MachHeader> 396 void MachODumper::printFileHeaders(const MachHeader &Header) { 397 W.printEnum("Magic", Header.magic, makeArrayRef(MachOMagics)); 398 W.printEnum("CpuType", Header.cputype, makeArrayRef(MachOHeaderCpuTypes)); 399 uint32_t subtype = Header.cpusubtype & ~MachO::CPU_SUBTYPE_MASK; 400 switch (Header.cputype) { 401 case MachO::CPU_TYPE_X86: 402 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX86)); 403 break; 404 case MachO::CPU_TYPE_X86_64: 405 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX64)); 406 break; 407 case MachO::CPU_TYPE_ARM: 408 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM)); 409 break; 410 case MachO::CPU_TYPE_POWERPC: 411 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesPPC)); 412 break; 413 case MachO::CPU_TYPE_SPARC: 414 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesSPARC)); 415 break; 416 case MachO::CPU_TYPE_ARM64: 417 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM64)); 418 break; 419 case MachO::CPU_TYPE_POWERPC64: 420 default: 421 W.printHex("CpuSubtype", subtype); 422 } 423 W.printEnum("FileType", Header.filetype, makeArrayRef(MachOHeaderFileTypes)); 424 W.printNumber("NumOfLoadCommands", Header.ncmds); 425 W.printNumber("SizeOfLoadCommands", Header.sizeofcmds); 426 W.printFlags("Flags", Header.flags, makeArrayRef(MachOHeaderFlags)); 427 } 428 429 void MachODumper::printSections() { 430 return printSections(Obj); 431 } 432 433 void MachODumper::printSections(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) { 487 bool IsBSS = Section.isBSS(); 488 if (!IsBSS) { 489 StringRef Data; 490 error(Section.getContents(Data)); 491 492 W.printBinaryBlock("SectionData", Data); 493 } 494 } 495 } 496 } 497 498 void MachODumper::printRelocations() { 499 ListScope D(W, "Relocations"); 500 501 std::error_code EC; 502 for (const SectionRef &Section : Obj->sections()) { 503 StringRef Name; 504 error(Section.getName(Name)); 505 506 bool PrintedGroup = false; 507 for (const RelocationRef &Reloc : Section.relocations()) { 508 if (!PrintedGroup) { 509 W.startLine() << "Section " << Name << " {\n"; 510 W.indent(); 511 PrintedGroup = true; 512 } 513 514 printRelocation(Reloc); 515 } 516 517 if (PrintedGroup) { 518 W.unindent(); 519 W.startLine() << "}\n"; 520 } 521 } 522 } 523 524 void MachODumper::printRelocation(const RelocationRef &Reloc) { 525 return printRelocation(Obj, Reloc); 526 } 527 528 void MachODumper::printRelocation(const MachOObjectFile *Obj, 529 const RelocationRef &Reloc) { 530 uint64_t Offset = Reloc.getOffset(); 531 SmallString<32> RelocName; 532 Reloc.getTypeName(RelocName); 533 534 DataRefImpl DR = Reloc.getRawDataRefImpl(); 535 MachO::any_relocation_info RE = Obj->getRelocation(DR); 536 bool IsScattered = Obj->isRelocationScattered(RE); 537 bool IsExtern = !IsScattered && Obj->getPlainRelocationExternal(RE); 538 539 StringRef TargetName; 540 if (IsExtern) { 541 symbol_iterator Symbol = Reloc.getSymbol(); 542 if (Symbol != Obj->symbol_end()) { 543 ErrorOr<StringRef> TargetNameOrErr = Symbol->getName(); 544 error(TargetNameOrErr.getError()); 545 TargetName = *TargetNameOrErr; 546 } 547 } else if (!IsScattered) { 548 section_iterator SecI = Obj->getRelocationSection(DR); 549 if (SecI != Obj->section_end()) { 550 error(SecI->getName(TargetName)); 551 } 552 } 553 if (TargetName.empty()) 554 TargetName = "-"; 555 556 if (opts::ExpandRelocs) { 557 DictScope Group(W, "Relocation"); 558 W.printHex("Offset", Offset); 559 W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE)); 560 W.printNumber("Length", Obj->getAnyRelocationLength(RE)); 561 W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE)); 562 if (IsScattered) { 563 W.printHex("Value", Obj->getScatteredRelocationValue(RE)); 564 } else { 565 const char *Kind = IsExtern ? "Symbol" : "Section"; 566 W.printNumber(Kind, TargetName, Obj->getPlainRelocationSymbolNum(RE)); 567 } 568 } else { 569 SmallString<32> SymbolNameOrOffset("0x"); 570 if (IsScattered) { 571 // Scattered relocations don't really have an associated symbol for some 572 // reason, even if one exists in the symtab at the correct address. 573 SymbolNameOrOffset += utohexstr(Obj->getScatteredRelocationValue(RE)); 574 } else { 575 SymbolNameOrOffset = TargetName; 576 } 577 578 raw_ostream& OS = W.startLine(); 579 OS << W.hex(Offset) 580 << " " << Obj->getAnyRelocationPCRel(RE) 581 << " " << Obj->getAnyRelocationLength(RE); 582 if (IsScattered) 583 OS << " n/a"; 584 else 585 OS << " " << Obj->getPlainRelocationExternal(RE); 586 OS << " " << RelocName 587 << " " << IsScattered 588 << " " << SymbolNameOrOffset 589 << "\n"; 590 } 591 } 592 593 void MachODumper::printSymbols() { 594 ListScope Group(W, "Symbols"); 595 596 for (const SymbolRef &Symbol : Obj->symbols()) { 597 printSymbol(Symbol); 598 } 599 } 600 601 void MachODumper::printDynamicSymbols() { 602 ListScope Group(W, "DynamicSymbols"); 603 } 604 605 void MachODumper::printSymbol(const SymbolRef &Symbol) { 606 StringRef SymbolName; 607 if (ErrorOr<StringRef> SymbolNameOrErr = Symbol.getName()) 608 SymbolName = *SymbolNameOrErr; 609 610 MachOSymbol MOSymbol; 611 getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol); 612 613 StringRef SectionName = ""; 614 ErrorOr<section_iterator> SecIOrErr = Symbol.getSection(); 615 error(SecIOrErr.getError()); 616 section_iterator SecI = *SecIOrErr; 617 if (SecI != Obj->section_end()) 618 error(SecI->getName(SectionName)); 619 620 DictScope D(W, "Symbol"); 621 W.printNumber("Name", SymbolName, MOSymbol.StringIndex); 622 if (MOSymbol.Type & MachO::N_STAB) { 623 W.printHex("Type", "SymDebugTable", MOSymbol.Type); 624 } else { 625 if (MOSymbol.Type & MachO::N_PEXT) 626 W.startLine() << "PrivateExtern\n"; 627 if (MOSymbol.Type & MachO::N_EXT) 628 W.startLine() << "Extern\n"; 629 W.printEnum("Type", uint8_t(MOSymbol.Type & MachO::N_TYPE), 630 makeArrayRef(MachOSymbolTypes)); 631 } 632 W.printHex("Section", SectionName, MOSymbol.SectionIndex); 633 W.printEnum("RefType", static_cast<uint16_t>(MOSymbol.Flags & 0xF), 634 makeArrayRef(MachOSymbolRefTypes)); 635 W.printFlags("Flags", static_cast<uint16_t>(MOSymbol.Flags & ~0xF), 636 makeArrayRef(MachOSymbolFlags)); 637 W.printHex("Value", MOSymbol.Value); 638 } 639 640 void MachODumper::printUnwindInfo() { 641 W.startLine() << "UnwindInfo not implemented.\n"; 642 } 643 644 void MachODumper::printStackMap() const { 645 object::SectionRef StackMapSection; 646 for (auto Sec : Obj->sections()) { 647 StringRef Name; 648 Sec.getName(Name); 649 if (Name == "__llvm_stackmaps") { 650 StackMapSection = Sec; 651 break; 652 } 653 } 654 655 if (StackMapSection == object::SectionRef()) 656 return; 657 658 StringRef StackMapContents; 659 StackMapSection.getContents(StackMapContents); 660 ArrayRef<uint8_t> StackMapContentsArray( 661 reinterpret_cast<const uint8_t*>(StackMapContents.data()), 662 StackMapContents.size()); 663 664 if (Obj->isLittleEndian()) 665 prettyPrintStackMap( 666 llvm::outs(), 667 StackMapV1Parser<support::little>(StackMapContentsArray)); 668 else 669 prettyPrintStackMap(llvm::outs(), 670 StackMapV1Parser<support::big>(StackMapContentsArray)); 671 } 672 673 void MachODumper::printMachODataInCode() { 674 for (const auto &Load : Obj->load_commands()) { 675 if (Load.C.cmd == MachO::LC_DATA_IN_CODE) { 676 MachO::linkedit_data_command LLC = Obj->getLinkeditDataLoadCommand(Load); 677 DictScope Group(W, "DataInCode"); 678 W.printNumber("Data offset", LLC.dataoff); 679 W.printNumber("Data size", LLC.datasize); 680 ListScope D(W, "Data entries"); 681 unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry); 682 for (unsigned i = 0; i < NumRegions; ++i) { 683 MachO::data_in_code_entry DICE = Obj->getDataInCodeTableEntry( 684 LLC.dataoff, i); 685 DictScope Group(W, "Entry"); 686 W.printNumber("Index", i); 687 W.printNumber("Offset", DICE.offset); 688 W.printNumber("Length", DICE.length); 689 W.printNumber("Kind", DICE.kind); 690 } 691 } 692 } 693 } 694 695 void MachODumper::printMachOVersionMin() { 696 for (const auto &Load : Obj->load_commands()) { 697 StringRef Cmd; 698 switch (Load.C.cmd) { 699 case MachO::LC_VERSION_MIN_MACOSX: 700 Cmd = "LC_VERSION_MIN_MACOSX"; 701 break; 702 case MachO::LC_VERSION_MIN_IPHONEOS: 703 Cmd = "LC_VERSION_MIN_IPHONEOS"; 704 break; 705 case MachO::LC_VERSION_MIN_TVOS: 706 Cmd = "LC_VERSION_MIN_TVOS"; 707 break; 708 case MachO::LC_VERSION_MIN_WATCHOS: 709 Cmd = "LC_VERSION_MIN_WATCHOS"; 710 break; 711 default: 712 continue; 713 } 714 715 MachO::version_min_command VMC = Obj->getVersionMinLoadCommand(Load); 716 DictScope Group(W, "MinVersion"); 717 W.printString("Cmd", Cmd); 718 W.printNumber("Size", VMC.cmdsize); 719 SmallString<32> Version; 720 Version = utostr(MachOObjectFile::getVersionMinMajor(VMC, false)) + "." + 721 utostr(MachOObjectFile::getVersionMinMinor(VMC, false)); 722 uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, false); 723 if (Update != 0) 724 Version += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, false)); 725 W.printString("Version", Version); 726 SmallString<32> SDK; 727 if (VMC.sdk == 0) 728 SDK = "n/a"; 729 else { 730 SDK = utostr(MachOObjectFile::getVersionMinMajor(VMC, true)) + "." + 731 utostr(MachOObjectFile::getVersionMinMinor(VMC, true)); 732 uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, true); 733 if (Update != 0) 734 SDK += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, true)); 735 } 736 W.printString("SDK", SDK); 737 } 738 } 739 740 void MachODumper::printMachODysymtab() { 741 for (const auto &Load : Obj->load_commands()) { 742 if (Load.C.cmd == MachO::LC_DYSYMTAB) { 743 MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand(); 744 DictScope Group(W, "Dysymtab"); 745 W.printNumber("ilocalsym", DLC.ilocalsym); 746 W.printNumber("nlocalsym", DLC.nlocalsym); 747 W.printNumber("iextdefsym", DLC.iextdefsym); 748 W.printNumber("nextdefsym", DLC.nextdefsym); 749 W.printNumber("iundefsym", DLC.iundefsym); 750 W.printNumber("nundefsym", DLC.nundefsym); 751 W.printNumber("tocoff", DLC.tocoff); 752 W.printNumber("ntoc", DLC.ntoc); 753 W.printNumber("modtaboff", DLC.modtaboff); 754 W.printNumber("nmodtab", DLC.nmodtab); 755 W.printNumber("extrefsymoff", DLC.extrefsymoff); 756 W.printNumber("nextrefsyms", DLC.nextrefsyms); 757 W.printNumber("indirectsymoff", DLC.indirectsymoff); 758 W.printNumber("nindirectsyms", DLC.nindirectsyms); 759 W.printNumber("extreloff", DLC.extreloff); 760 W.printNumber("nextrel", DLC.nextrel); 761 W.printNumber("locreloff", DLC.locreloff); 762 W.printNumber("nlocrel", DLC.nlocrel); 763 } 764 } 765 } 766 767 void MachODumper::printMachOSegment() { 768 for (const auto &Load : Obj->load_commands()) { 769 if (Load.C.cmd == MachO::LC_SEGMENT || Load.C.cmd == MachO::LC_SEGMENT_64) { 770 MachOSegment MOSegment; 771 getSegment(Obj, Load, MOSegment); 772 DictScope Group(W, "Segment"); 773 W.printString("Cmd", MOSegment.CmdName); 774 W.printString("Name", MOSegment.SegName); 775 W.printNumber("Size", MOSegment.cmdsize); 776 W.printHex("vmaddr", MOSegment.vmaddr); 777 W.printHex("vmsize", MOSegment.vmsize); 778 W.printNumber("fileoff", MOSegment.fileoff); 779 W.printNumber("filesize", MOSegment.filesize); 780 W.printString("maxprot", getMask(MOSegment.maxprot)); 781 W.printString("initprot", getMask(MOSegment.initprot)); 782 W.printNumber("nsects", MOSegment.nsects); 783 W.printHex("flags", MOSegment.flags); 784 } 785 } 786 } 787 788 void MachODumper::printMachOIndirectSymbols() { 789 for (const auto &Load : Obj->load_commands()) { 790 if (Load.C.cmd == MachO::LC_DYSYMTAB) { 791 MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand(); 792 DictScope Group(W, "Indirect Symbols"); 793 W.printNumber("Number", DLC.nindirectsyms); 794 ListScope D(W, "Symbols"); 795 for (unsigned i = 0; i < DLC.nindirectsyms; ++i) { 796 DictScope Group(W, "Entry"); 797 W.printNumber("Entry Index", i); 798 W.printHex("Symbol Index", Obj->getIndirectSymbolTableEntry(DLC, i)); 799 } 800 } 801 } 802 } 803 804 void MachODumper::printMachOLinkerOptions() { 805 for (const auto &Load : Obj->load_commands()) { 806 if (Load.C.cmd == MachO::LC_LINKER_OPTION) { 807 MachO::linker_option_command LOLC = Obj->getLinkerOptionLoadCommand(Load); 808 DictScope Group(W, "Linker Options"); 809 W.printNumber("Size", LOLC.cmdsize); 810 ListScope D(W, "Strings"); 811 uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_option_command); 812 const char *P = Load.Ptr + sizeof(MachO::linker_option_command); 813 StringRef Data(P, DataSize); 814 for (unsigned i = 0; i < LOLC.count; ++i) { 815 std::pair<StringRef,StringRef> Split = Data.split('\0'); 816 W.printString("Value", Split.first); 817 Data = Split.second; 818 } 819 } 820 } 821 } 822