1 //===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===// 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 defines the MachOObjectFile class, which binds the MachOObject 11 // class to the generic ObjectFile wrapper. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Object/MachO.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/Triple.h" 18 #include "llvm/Support/DataExtractor.h" 19 #include "llvm/Support/Format.h" 20 #include "llvm/Support/Host.h" 21 #include "llvm/Support/MemoryBuffer.h" 22 #include "llvm/Support/raw_ostream.h" 23 #include <cctype> 24 #include <cstring> 25 #include <limits> 26 27 using namespace llvm; 28 using namespace object; 29 30 namespace llvm { 31 namespace object { 32 33 struct nlist_base { 34 uint32_t n_strx; 35 uint8_t n_type; 36 uint8_t n_sect; 37 uint16_t n_desc; 38 }; 39 40 struct section_base { 41 char sectname[16]; 42 char segname[16]; 43 }; 44 45 template<typename T> 46 static void SwapValue(T &Value) { 47 Value = sys::SwapByteOrder(Value); 48 } 49 50 template<typename T> 51 static void SwapStruct(T &Value); 52 53 template<> 54 void SwapStruct(MachO::any_relocation_info &H) { 55 SwapValue(H.r_word0); 56 SwapValue(H.r_word1); 57 } 58 59 template<> 60 void SwapStruct(MachO::load_command &L) { 61 SwapValue(L.cmd); 62 SwapValue(L.cmdsize); 63 } 64 65 template<> 66 void SwapStruct(nlist_base &S) { 67 SwapValue(S.n_strx); 68 SwapValue(S.n_desc); 69 } 70 71 template<> 72 void SwapStruct(MachO::section &S) { 73 SwapValue(S.addr); 74 SwapValue(S.size); 75 SwapValue(S.offset); 76 SwapValue(S.align); 77 SwapValue(S.reloff); 78 SwapValue(S.nreloc); 79 SwapValue(S.flags); 80 SwapValue(S.reserved1); 81 SwapValue(S.reserved2); 82 } 83 84 template<> 85 void SwapStruct(MachO::section_64 &S) { 86 SwapValue(S.addr); 87 SwapValue(S.size); 88 SwapValue(S.offset); 89 SwapValue(S.align); 90 SwapValue(S.reloff); 91 SwapValue(S.nreloc); 92 SwapValue(S.flags); 93 SwapValue(S.reserved1); 94 SwapValue(S.reserved2); 95 SwapValue(S.reserved3); 96 } 97 98 template<> 99 void SwapStruct(MachO::nlist &S) { 100 SwapValue(S.n_strx); 101 SwapValue(S.n_desc); 102 SwapValue(S.n_value); 103 } 104 105 template<> 106 void SwapStruct(MachO::nlist_64 &S) { 107 SwapValue(S.n_strx); 108 SwapValue(S.n_desc); 109 SwapValue(S.n_value); 110 } 111 112 template<> 113 void SwapStruct(MachO::mach_header &H) { 114 SwapValue(H.magic); 115 SwapValue(H.cputype); 116 SwapValue(H.cpusubtype); 117 SwapValue(H.filetype); 118 SwapValue(H.ncmds); 119 SwapValue(H.sizeofcmds); 120 SwapValue(H.flags); 121 } 122 123 template<> 124 void SwapStruct(MachO::mach_header_64 &H) { 125 SwapValue(H.magic); 126 SwapValue(H.cputype); 127 SwapValue(H.cpusubtype); 128 SwapValue(H.filetype); 129 SwapValue(H.ncmds); 130 SwapValue(H.sizeofcmds); 131 SwapValue(H.flags); 132 SwapValue(H.reserved); 133 } 134 135 template<> 136 void SwapStruct(MachO::symtab_command &C) { 137 SwapValue(C.cmd); 138 SwapValue(C.cmdsize); 139 SwapValue(C.symoff); 140 SwapValue(C.nsyms); 141 SwapValue(C.stroff); 142 SwapValue(C.strsize); 143 } 144 145 template<> 146 void SwapStruct(MachO::dysymtab_command &C) { 147 SwapValue(C.cmd); 148 SwapValue(C.cmdsize); 149 SwapValue(C.ilocalsym); 150 SwapValue(C.nlocalsym); 151 SwapValue(C.iextdefsym); 152 SwapValue(C.nextdefsym); 153 SwapValue(C.iundefsym); 154 SwapValue(C.nundefsym); 155 SwapValue(C.tocoff); 156 SwapValue(C.ntoc); 157 SwapValue(C.modtaboff); 158 SwapValue(C.nmodtab); 159 SwapValue(C.extrefsymoff); 160 SwapValue(C.nextrefsyms); 161 SwapValue(C.indirectsymoff); 162 SwapValue(C.nindirectsyms); 163 SwapValue(C.extreloff); 164 SwapValue(C.nextrel); 165 SwapValue(C.locreloff); 166 SwapValue(C.nlocrel); 167 } 168 169 template<> 170 void SwapStruct(MachO::linkedit_data_command &C) { 171 SwapValue(C.cmd); 172 SwapValue(C.cmdsize); 173 SwapValue(C.dataoff); 174 SwapValue(C.datasize); 175 } 176 177 template<> 178 void SwapStruct(MachO::segment_command &C) { 179 SwapValue(C.cmd); 180 SwapValue(C.cmdsize); 181 SwapValue(C.vmaddr); 182 SwapValue(C.vmsize); 183 SwapValue(C.fileoff); 184 SwapValue(C.filesize); 185 SwapValue(C.maxprot); 186 SwapValue(C.initprot); 187 SwapValue(C.nsects); 188 SwapValue(C.flags); 189 } 190 191 template<> 192 void SwapStruct(MachO::segment_command_64 &C) { 193 SwapValue(C.cmd); 194 SwapValue(C.cmdsize); 195 SwapValue(C.vmaddr); 196 SwapValue(C.vmsize); 197 SwapValue(C.fileoff); 198 SwapValue(C.filesize); 199 SwapValue(C.maxprot); 200 SwapValue(C.initprot); 201 SwapValue(C.nsects); 202 SwapValue(C.flags); 203 } 204 205 template<> 206 void SwapStruct(uint32_t &C) { 207 SwapValue(C); 208 } 209 210 template<> 211 void SwapStruct(MachO::linker_options_command &C) { 212 SwapValue(C.cmd); 213 SwapValue(C.cmdsize); 214 SwapValue(C.count); 215 } 216 217 template<> 218 void SwapStruct(MachO::version_min_command&C) { 219 SwapValue(C.cmd); 220 SwapValue(C.cmdsize); 221 SwapValue(C.version); 222 SwapValue(C.reserved); 223 } 224 225 template<> 226 void SwapStruct(MachO::data_in_code_entry &C) { 227 SwapValue(C.offset); 228 SwapValue(C.length); 229 SwapValue(C.kind); 230 } 231 232 template<typename T> 233 T getStruct(const MachOObjectFile *O, const char *P) { 234 T Cmd; 235 memcpy(&Cmd, P, sizeof(T)); 236 if (O->isLittleEndian() != sys::IsLittleEndianHost) 237 SwapStruct(Cmd); 238 return Cmd; 239 } 240 241 static uint32_t 242 getSegmentLoadCommandNumSections(const MachOObjectFile *O, 243 const MachOObjectFile::LoadCommandInfo &L) { 244 if (O->is64Bit()) { 245 MachO::segment_command_64 S = O->getSegment64LoadCommand(L); 246 return S.nsects; 247 } 248 MachO::segment_command S = O->getSegmentLoadCommand(L); 249 return S.nsects; 250 } 251 252 static const char * 253 getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L, 254 unsigned Sec) { 255 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr); 256 257 bool Is64 = O->is64Bit(); 258 unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) : 259 sizeof(MachO::segment_command); 260 unsigned SectionSize = Is64 ? sizeof(MachO::section_64) : 261 sizeof(MachO::section); 262 263 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize; 264 return reinterpret_cast<const char*>(SectionAddr); 265 } 266 267 static const char *getPtr(const MachOObjectFile *O, size_t Offset) { 268 return O->getData().substr(Offset, 1).data(); 269 } 270 271 static nlist_base 272 getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) { 273 const char *P = reinterpret_cast<const char *>(DRI.p); 274 return getStruct<nlist_base>(O, P); 275 } 276 277 static StringRef parseSegmentOrSectionName(const char *P) { 278 if (P[15] == 0) 279 // Null terminated. 280 return P; 281 // Not null terminated, so this is a 16 char string. 282 return StringRef(P, 16); 283 } 284 285 // Helper to advance a section or symbol iterator multiple increments at a time. 286 template<class T> 287 static void advance(T &it, size_t Val) { 288 while (Val--) 289 ++it; 290 } 291 292 static unsigned getCPUType(const MachOObjectFile *O) { 293 return O->getHeader().cputype; 294 } 295 296 static void printRelocationTargetName(const MachOObjectFile *O, 297 const MachO::any_relocation_info &RE, 298 raw_string_ostream &fmt) { 299 bool IsScattered = O->isRelocationScattered(RE); 300 301 // Target of a scattered relocation is an address. In the interest of 302 // generating pretty output, scan through the symbol table looking for a 303 // symbol that aligns with that address. If we find one, print it. 304 // Otherwise, we just print the hex address of the target. 305 if (IsScattered) { 306 uint32_t Val = O->getPlainRelocationSymbolNum(RE); 307 308 for (const SymbolRef &Symbol : O->symbols()) { 309 error_code ec; 310 uint64_t Addr; 311 StringRef Name; 312 313 if ((ec = Symbol.getAddress(Addr))) 314 report_fatal_error(ec.message()); 315 if (Addr != Val) 316 continue; 317 if ((ec = Symbol.getName(Name))) 318 report_fatal_error(ec.message()); 319 fmt << Name; 320 return; 321 } 322 323 // If we couldn't find a symbol that this relocation refers to, try 324 // to find a section beginning instead. 325 for (const SectionRef &Section : O->sections()) { 326 error_code ec; 327 uint64_t Addr; 328 StringRef Name; 329 330 if ((ec = Section.getAddress(Addr))) 331 report_fatal_error(ec.message()); 332 if (Addr != Val) 333 continue; 334 if ((ec = Section.getName(Name))) 335 report_fatal_error(ec.message()); 336 fmt << Name; 337 return; 338 } 339 340 fmt << format("0x%x", Val); 341 return; 342 } 343 344 StringRef S; 345 bool isExtern = O->getPlainRelocationExternal(RE); 346 uint64_t Val = O->getPlainRelocationSymbolNum(RE); 347 348 if (isExtern) { 349 symbol_iterator SI = O->symbol_begin(); 350 advance(SI, Val); 351 SI->getName(S); 352 } else { 353 section_iterator SI = O->section_begin(); 354 // Adjust for the fact that sections are 1-indexed. 355 advance(SI, Val - 1); 356 SI->getName(S); 357 } 358 359 fmt << S; 360 } 361 362 static uint32_t 363 getPlainRelocationAddress(const MachO::any_relocation_info &RE) { 364 return RE.r_word0; 365 } 366 367 static unsigned 368 getScatteredRelocationAddress(const MachO::any_relocation_info &RE) { 369 return RE.r_word0 & 0xffffff; 370 } 371 372 static bool getPlainRelocationPCRel(const MachOObjectFile *O, 373 const MachO::any_relocation_info &RE) { 374 if (O->isLittleEndian()) 375 return (RE.r_word1 >> 24) & 1; 376 return (RE.r_word1 >> 7) & 1; 377 } 378 379 static bool 380 getScatteredRelocationPCRel(const MachOObjectFile *O, 381 const MachO::any_relocation_info &RE) { 382 return (RE.r_word0 >> 30) & 1; 383 } 384 385 static unsigned getPlainRelocationLength(const MachOObjectFile *O, 386 const MachO::any_relocation_info &RE) { 387 if (O->isLittleEndian()) 388 return (RE.r_word1 >> 25) & 3; 389 return (RE.r_word1 >> 5) & 3; 390 } 391 392 static unsigned 393 getScatteredRelocationLength(const MachO::any_relocation_info &RE) { 394 return (RE.r_word0 >> 28) & 3; 395 } 396 397 static unsigned getPlainRelocationType(const MachOObjectFile *O, 398 const MachO::any_relocation_info &RE) { 399 if (O->isLittleEndian()) 400 return RE.r_word1 >> 28; 401 return RE.r_word1 & 0xf; 402 } 403 404 static unsigned 405 getScatteredRelocationType(const MachO::any_relocation_info &RE) { 406 return (RE.r_word0 >> 24) & 0xf; 407 } 408 409 static uint32_t getSectionFlags(const MachOObjectFile *O, 410 DataRefImpl Sec) { 411 if (O->is64Bit()) { 412 MachO::section_64 Sect = O->getSection64(Sec); 413 return Sect.flags; 414 } 415 MachO::section Sect = O->getSection(Sec); 416 return Sect.flags; 417 } 418 419 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, 420 bool Is64bits, error_code &EC, 421 bool BufferOwned) 422 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object, BufferOwned), 423 SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL), DataInCodeLoadCmd(NULL) { 424 uint32_t LoadCommandCount = this->getHeader().ncmds; 425 MachO::LoadCommandType SegmentLoadType = is64Bit() ? 426 MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT; 427 428 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo(); 429 for (unsigned I = 0; ; ++I) { 430 if (Load.C.cmd == MachO::LC_SYMTAB) { 431 assert(!SymtabLoadCmd && "Multiple symbol tables"); 432 SymtabLoadCmd = Load.Ptr; 433 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) { 434 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables"); 435 DysymtabLoadCmd = Load.Ptr; 436 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) { 437 assert(!DataInCodeLoadCmd && "Multiple data in code tables"); 438 DataInCodeLoadCmd = Load.Ptr; 439 } else if (Load.C.cmd == SegmentLoadType) { 440 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load); 441 for (unsigned J = 0; J < NumSections; ++J) { 442 const char *Sec = getSectionPtr(this, Load, J); 443 Sections.push_back(Sec); 444 } 445 } 446 447 if (I == LoadCommandCount - 1) 448 break; 449 else 450 Load = getNextLoadCommandInfo(Load); 451 } 452 } 453 454 void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const { 455 unsigned SymbolTableEntrySize = is64Bit() ? 456 sizeof(MachO::nlist_64) : 457 sizeof(MachO::nlist); 458 Symb.p += SymbolTableEntrySize; 459 } 460 461 error_code MachOObjectFile::getSymbolName(DataRefImpl Symb, 462 StringRef &Res) const { 463 StringRef StringTable = getStringTableData(); 464 nlist_base Entry = getSymbolTableEntryBase(this, Symb); 465 const char *Start = &StringTable.data()[Entry.n_strx]; 466 Res = StringRef(Start); 467 return object_error::success; 468 } 469 470 error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb, 471 uint64_t &Res) const { 472 if (is64Bit()) { 473 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb); 474 Res = Entry.n_value; 475 } else { 476 MachO::nlist Entry = getSymbolTableEntry(Symb); 477 Res = Entry.n_value; 478 } 479 return object_error::success; 480 } 481 482 error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI, 483 uint32_t &Result) const { 484 uint32_t flags = getSymbolFlags(DRI); 485 if (flags & SymbolRef::SF_Common) { 486 nlist_base Entry = getSymbolTableEntryBase(this, DRI); 487 Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc); 488 } else { 489 Result = 0; 490 } 491 return object_error::success; 492 } 493 494 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI, 495 uint64_t &Result) const { 496 uint64_t BeginOffset; 497 uint64_t EndOffset = 0; 498 uint8_t SectionIndex; 499 500 nlist_base Entry = getSymbolTableEntryBase(this, DRI); 501 uint64_t Value; 502 getSymbolAddress(DRI, Value); 503 504 BeginOffset = Value; 505 506 SectionIndex = Entry.n_sect; 507 if (!SectionIndex) { 508 uint32_t flags = getSymbolFlags(DRI); 509 if (flags & SymbolRef::SF_Common) 510 Result = Value; 511 else 512 Result = UnknownAddressOrSize; 513 return object_error::success; 514 } 515 // Unfortunately symbols are unsorted so we need to touch all 516 // symbols from load command 517 for (const SymbolRef &Symbol : symbols()) { 518 DataRefImpl DRI = Symbol.getRawDataRefImpl(); 519 Entry = getSymbolTableEntryBase(this, DRI); 520 getSymbolAddress(DRI, Value); 521 if (Entry.n_sect == SectionIndex && Value > BeginOffset) 522 if (!EndOffset || Value < EndOffset) 523 EndOffset = Value; 524 } 525 if (!EndOffset) { 526 uint64_t Size; 527 DataRefImpl Sec; 528 Sec.d.a = SectionIndex-1; 529 getSectionSize(Sec, Size); 530 getSectionAddress(Sec, EndOffset); 531 EndOffset += Size; 532 } 533 Result = EndOffset - BeginOffset; 534 return object_error::success; 535 } 536 537 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb, 538 SymbolRef::Type &Res) const { 539 nlist_base Entry = getSymbolTableEntryBase(this, Symb); 540 uint8_t n_type = Entry.n_type; 541 542 Res = SymbolRef::ST_Other; 543 544 // If this is a STAB debugging symbol, we can do nothing more. 545 if (n_type & MachO::N_STAB) { 546 Res = SymbolRef::ST_Debug; 547 return object_error::success; 548 } 549 550 switch (n_type & MachO::N_TYPE) { 551 case MachO::N_UNDF : 552 Res = SymbolRef::ST_Unknown; 553 break; 554 case MachO::N_SECT : 555 Res = SymbolRef::ST_Function; 556 break; 557 } 558 return object_error::success; 559 } 560 561 uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const { 562 nlist_base Entry = getSymbolTableEntryBase(this, DRI); 563 564 uint8_t MachOType = Entry.n_type; 565 uint16_t MachOFlags = Entry.n_desc; 566 567 uint32_t Result = SymbolRef::SF_None; 568 569 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) 570 Result |= SymbolRef::SF_Undefined; 571 572 if (MachOType & MachO::N_STAB) 573 Result |= SymbolRef::SF_FormatSpecific; 574 575 if (MachOType & MachO::N_EXT) { 576 Result |= SymbolRef::SF_Global; 577 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) { 578 uint64_t Value; 579 getSymbolAddress(DRI, Value); 580 if (Value) 581 Result |= SymbolRef::SF_Common; 582 } 583 } 584 585 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) 586 Result |= SymbolRef::SF_Weak; 587 588 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS) 589 Result |= SymbolRef::SF_Absolute; 590 591 return Result; 592 } 593 594 error_code 595 MachOObjectFile::getSymbolSection(DataRefImpl Symb, 596 section_iterator &Res) const { 597 nlist_base Entry = getSymbolTableEntryBase(this, Symb); 598 uint8_t index = Entry.n_sect; 599 600 if (index == 0) { 601 Res = section_end(); 602 } else { 603 DataRefImpl DRI; 604 DRI.d.a = index - 1; 605 Res = section_iterator(SectionRef(DRI, this)); 606 } 607 608 return object_error::success; 609 } 610 611 void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const { 612 Sec.d.a++; 613 } 614 615 error_code 616 MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const { 617 ArrayRef<char> Raw = getSectionRawName(Sec); 618 Result = parseSegmentOrSectionName(Raw.data()); 619 return object_error::success; 620 } 621 622 error_code 623 MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const { 624 if (is64Bit()) { 625 MachO::section_64 Sect = getSection64(Sec); 626 Res = Sect.addr; 627 } else { 628 MachO::section Sect = getSection(Sec); 629 Res = Sect.addr; 630 } 631 return object_error::success; 632 } 633 634 error_code 635 MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const { 636 if (is64Bit()) { 637 MachO::section_64 Sect = getSection64(Sec); 638 Res = Sect.size; 639 } else { 640 MachO::section Sect = getSection(Sec); 641 Res = Sect.size; 642 } 643 644 return object_error::success; 645 } 646 647 error_code 648 MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const { 649 uint32_t Offset; 650 uint64_t Size; 651 652 if (is64Bit()) { 653 MachO::section_64 Sect = getSection64(Sec); 654 Offset = Sect.offset; 655 Size = Sect.size; 656 } else { 657 MachO::section Sect = getSection(Sec); 658 Offset = Sect.offset; 659 Size = Sect.size; 660 } 661 662 Res = this->getData().substr(Offset, Size); 663 return object_error::success; 664 } 665 666 error_code 667 MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const { 668 uint32_t Align; 669 if (is64Bit()) { 670 MachO::section_64 Sect = getSection64(Sec); 671 Align = Sect.align; 672 } else { 673 MachO::section Sect = getSection(Sec); 674 Align = Sect.align; 675 } 676 677 Res = uint64_t(1) << Align; 678 return object_error::success; 679 } 680 681 error_code 682 MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const { 683 uint32_t Flags = getSectionFlags(this, Sec); 684 Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS; 685 return object_error::success; 686 } 687 688 error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const { 689 // FIXME: Unimplemented. 690 Result = false; 691 return object_error::success; 692 } 693 694 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const { 695 // FIXME: Unimplemented. 696 Result = false; 697 return object_error::success; 698 } 699 700 error_code 701 MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec, 702 bool &Result) const { 703 // FIXME: Unimplemented. 704 Result = true; 705 return object_error::success; 706 } 707 708 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec, 709 bool &Result) const { 710 // FIXME: Unimplemented. 711 Result = false; 712 return object_error::success; 713 } 714 715 error_code 716 MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const { 717 uint32_t Flags = getSectionFlags(this, Sec); 718 unsigned SectionType = Flags & MachO::SECTION_TYPE; 719 Res = SectionType == MachO::S_ZEROFILL || 720 SectionType == MachO::S_GB_ZEROFILL; 721 return object_error::success; 722 } 723 724 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec, 725 bool &Result) const { 726 // Consider using the code from isSectionText to look for __const sections. 727 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS 728 // to use section attributes to distinguish code from data. 729 730 // FIXME: Unimplemented. 731 Result = false; 732 return object_error::success; 733 } 734 735 error_code 736 MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, 737 bool &Result) const { 738 SymbolRef::Type ST; 739 this->getSymbolType(Symb, ST); 740 if (ST == SymbolRef::ST_Unknown) { 741 Result = false; 742 return object_error::success; 743 } 744 745 uint64_t SectBegin, SectEnd; 746 getSectionAddress(Sec, SectBegin); 747 getSectionSize(Sec, SectEnd); 748 SectEnd += SectBegin; 749 750 uint64_t SymAddr; 751 getSymbolAddress(Symb, SymAddr); 752 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd); 753 754 return object_error::success; 755 } 756 757 relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const { 758 DataRefImpl Ret; 759 Ret.d.a = Sec.d.a; 760 Ret.d.b = 0; 761 return relocation_iterator(RelocationRef(Ret, this)); 762 } 763 764 relocation_iterator 765 MachOObjectFile::section_rel_end(DataRefImpl Sec) const { 766 uint32_t Num; 767 if (is64Bit()) { 768 MachO::section_64 Sect = getSection64(Sec); 769 Num = Sect.nreloc; 770 } else { 771 MachO::section Sect = getSection(Sec); 772 Num = Sect.nreloc; 773 } 774 775 DataRefImpl Ret; 776 Ret.d.a = Sec.d.a; 777 Ret.d.b = Num; 778 return relocation_iterator(RelocationRef(Ret, this)); 779 } 780 781 void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const { 782 ++Rel.d.b; 783 } 784 785 error_code 786 MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const { 787 uint64_t Offset; 788 getRelocationOffset(Rel, Offset); 789 790 DataRefImpl Sec; 791 Sec.d.a = Rel.d.a; 792 uint64_t SecAddress; 793 getSectionAddress(Sec, SecAddress); 794 Res = SecAddress + Offset; 795 return object_error::success; 796 } 797 798 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel, 799 uint64_t &Res) const { 800 assert(getHeader().filetype == MachO::MH_OBJECT && 801 "Only implemented for MH_OBJECT"); 802 MachO::any_relocation_info RE = getRelocation(Rel); 803 Res = getAnyRelocationAddress(RE); 804 return object_error::success; 805 } 806 807 symbol_iterator 808 MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const { 809 MachO::any_relocation_info RE = getRelocation(Rel); 810 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE); 811 bool isExtern = getPlainRelocationExternal(RE); 812 if (!isExtern) 813 return symbol_end(); 814 815 MachO::symtab_command S = getSymtabLoadCommand(); 816 unsigned SymbolTableEntrySize = is64Bit() ? 817 sizeof(MachO::nlist_64) : 818 sizeof(MachO::nlist); 819 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize; 820 DataRefImpl Sym; 821 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); 822 return symbol_iterator(SymbolRef(Sym, this)); 823 } 824 825 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel, 826 uint64_t &Res) const { 827 MachO::any_relocation_info RE = getRelocation(Rel); 828 Res = getAnyRelocationType(RE); 829 return object_error::success; 830 } 831 832 error_code 833 MachOObjectFile::getRelocationTypeName(DataRefImpl Rel, 834 SmallVectorImpl<char> &Result) const { 835 StringRef res; 836 uint64_t RType; 837 getRelocationType(Rel, RType); 838 839 unsigned Arch = this->getArch(); 840 841 switch (Arch) { 842 case Triple::x86: { 843 static const char *const Table[] = { 844 "GENERIC_RELOC_VANILLA", 845 "GENERIC_RELOC_PAIR", 846 "GENERIC_RELOC_SECTDIFF", 847 "GENERIC_RELOC_PB_LA_PTR", 848 "GENERIC_RELOC_LOCAL_SECTDIFF", 849 "GENERIC_RELOC_TLV" }; 850 851 if (RType > 5) 852 res = "Unknown"; 853 else 854 res = Table[RType]; 855 break; 856 } 857 case Triple::x86_64: { 858 static const char *const Table[] = { 859 "X86_64_RELOC_UNSIGNED", 860 "X86_64_RELOC_SIGNED", 861 "X86_64_RELOC_BRANCH", 862 "X86_64_RELOC_GOT_LOAD", 863 "X86_64_RELOC_GOT", 864 "X86_64_RELOC_SUBTRACTOR", 865 "X86_64_RELOC_SIGNED_1", 866 "X86_64_RELOC_SIGNED_2", 867 "X86_64_RELOC_SIGNED_4", 868 "X86_64_RELOC_TLV" }; 869 870 if (RType > 9) 871 res = "Unknown"; 872 else 873 res = Table[RType]; 874 break; 875 } 876 case Triple::arm: { 877 static const char *const Table[] = { 878 "ARM_RELOC_VANILLA", 879 "ARM_RELOC_PAIR", 880 "ARM_RELOC_SECTDIFF", 881 "ARM_RELOC_LOCAL_SECTDIFF", 882 "ARM_RELOC_PB_LA_PTR", 883 "ARM_RELOC_BR24", 884 "ARM_THUMB_RELOC_BR22", 885 "ARM_THUMB_32BIT_BRANCH", 886 "ARM_RELOC_HALF", 887 "ARM_RELOC_HALF_SECTDIFF" }; 888 889 if (RType > 9) 890 res = "Unknown"; 891 else 892 res = Table[RType]; 893 break; 894 } 895 case Triple::arm64: 896 case Triple::aarch64: { 897 static const char *const Table[] = { 898 "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR", 899 "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21", 900 "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21", 901 "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT", 902 "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12", 903 "ARM64_RELOC_ADDEND" 904 }; 905 906 if (RType >= array_lengthof(Table)) 907 res = "Unknown"; 908 else 909 res = Table[RType]; 910 break; 911 } 912 case Triple::ppc: { 913 static const char *const Table[] = { 914 "PPC_RELOC_VANILLA", 915 "PPC_RELOC_PAIR", 916 "PPC_RELOC_BR14", 917 "PPC_RELOC_BR24", 918 "PPC_RELOC_HI16", 919 "PPC_RELOC_LO16", 920 "PPC_RELOC_HA16", 921 "PPC_RELOC_LO14", 922 "PPC_RELOC_SECTDIFF", 923 "PPC_RELOC_PB_LA_PTR", 924 "PPC_RELOC_HI16_SECTDIFF", 925 "PPC_RELOC_LO16_SECTDIFF", 926 "PPC_RELOC_HA16_SECTDIFF", 927 "PPC_RELOC_JBSR", 928 "PPC_RELOC_LO14_SECTDIFF", 929 "PPC_RELOC_LOCAL_SECTDIFF" }; 930 931 if (RType > 15) 932 res = "Unknown"; 933 else 934 res = Table[RType]; 935 break; 936 } 937 case Triple::UnknownArch: 938 res = "Unknown"; 939 break; 940 } 941 Result.append(res.begin(), res.end()); 942 return object_error::success; 943 } 944 945 error_code 946 MachOObjectFile::getRelocationValueString(DataRefImpl Rel, 947 SmallVectorImpl<char> &Result) const { 948 MachO::any_relocation_info RE = getRelocation(Rel); 949 950 unsigned Arch = this->getArch(); 951 952 std::string fmtbuf; 953 raw_string_ostream fmt(fmtbuf); 954 unsigned Type = this->getAnyRelocationType(RE); 955 bool IsPCRel = this->getAnyRelocationPCRel(RE); 956 957 // Determine any addends that should be displayed with the relocation. 958 // These require decoding the relocation type, which is triple-specific. 959 960 // X86_64 has entirely custom relocation types. 961 if (Arch == Triple::x86_64) { 962 bool isPCRel = getAnyRelocationPCRel(RE); 963 964 switch (Type) { 965 case MachO::X86_64_RELOC_GOT_LOAD: 966 case MachO::X86_64_RELOC_GOT: { 967 printRelocationTargetName(this, RE, fmt); 968 fmt << "@GOT"; 969 if (isPCRel) fmt << "PCREL"; 970 break; 971 } 972 case MachO::X86_64_RELOC_SUBTRACTOR: { 973 DataRefImpl RelNext = Rel; 974 moveRelocationNext(RelNext); 975 MachO::any_relocation_info RENext = getRelocation(RelNext); 976 977 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type 978 // X86_64_RELOC_UNSIGNED. 979 // NOTE: Scattered relocations don't exist on x86_64. 980 unsigned RType = getAnyRelocationType(RENext); 981 if (RType != MachO::X86_64_RELOC_UNSIGNED) 982 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after " 983 "X86_64_RELOC_SUBTRACTOR."); 984 985 // The X86_64_RELOC_UNSIGNED contains the minuend symbol; 986 // X86_64_RELOC_SUBTRACTOR contains the subtrahend. 987 printRelocationTargetName(this, RENext, fmt); 988 fmt << "-"; 989 printRelocationTargetName(this, RE, fmt); 990 break; 991 } 992 case MachO::X86_64_RELOC_TLV: 993 printRelocationTargetName(this, RE, fmt); 994 fmt << "@TLV"; 995 if (isPCRel) fmt << "P"; 996 break; 997 case MachO::X86_64_RELOC_SIGNED_1: 998 printRelocationTargetName(this, RE, fmt); 999 fmt << "-1"; 1000 break; 1001 case MachO::X86_64_RELOC_SIGNED_2: 1002 printRelocationTargetName(this, RE, fmt); 1003 fmt << "-2"; 1004 break; 1005 case MachO::X86_64_RELOC_SIGNED_4: 1006 printRelocationTargetName(this, RE, fmt); 1007 fmt << "-4"; 1008 break; 1009 default: 1010 printRelocationTargetName(this, RE, fmt); 1011 break; 1012 } 1013 // X86 and ARM share some relocation types in common. 1014 } else if (Arch == Triple::x86 || Arch == Triple::arm || 1015 Arch == Triple::ppc) { 1016 // Generic relocation types... 1017 switch (Type) { 1018 case MachO::GENERIC_RELOC_PAIR: // prints no info 1019 return object_error::success; 1020 case MachO::GENERIC_RELOC_SECTDIFF: { 1021 DataRefImpl RelNext = Rel; 1022 moveRelocationNext(RelNext); 1023 MachO::any_relocation_info RENext = getRelocation(RelNext); 1024 1025 // X86 sect diff's must be followed by a relocation of type 1026 // GENERIC_RELOC_PAIR. 1027 unsigned RType = getAnyRelocationType(RENext); 1028 1029 if (RType != MachO::GENERIC_RELOC_PAIR) 1030 report_fatal_error("Expected GENERIC_RELOC_PAIR after " 1031 "GENERIC_RELOC_SECTDIFF."); 1032 1033 printRelocationTargetName(this, RE, fmt); 1034 fmt << "-"; 1035 printRelocationTargetName(this, RENext, fmt); 1036 break; 1037 } 1038 } 1039 1040 if (Arch == Triple::x86 || Arch == Triple::ppc) { 1041 switch (Type) { 1042 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: { 1043 DataRefImpl RelNext = Rel; 1044 moveRelocationNext(RelNext); 1045 MachO::any_relocation_info RENext = getRelocation(RelNext); 1046 1047 // X86 sect diff's must be followed by a relocation of type 1048 // GENERIC_RELOC_PAIR. 1049 unsigned RType = getAnyRelocationType(RENext); 1050 if (RType != MachO::GENERIC_RELOC_PAIR) 1051 report_fatal_error("Expected GENERIC_RELOC_PAIR after " 1052 "GENERIC_RELOC_LOCAL_SECTDIFF."); 1053 1054 printRelocationTargetName(this, RE, fmt); 1055 fmt << "-"; 1056 printRelocationTargetName(this, RENext, fmt); 1057 break; 1058 } 1059 case MachO::GENERIC_RELOC_TLV: { 1060 printRelocationTargetName(this, RE, fmt); 1061 fmt << "@TLV"; 1062 if (IsPCRel) fmt << "P"; 1063 break; 1064 } 1065 default: 1066 printRelocationTargetName(this, RE, fmt); 1067 } 1068 } else { // ARM-specific relocations 1069 switch (Type) { 1070 case MachO::ARM_RELOC_HALF: 1071 case MachO::ARM_RELOC_HALF_SECTDIFF: { 1072 // Half relocations steal a bit from the length field to encode 1073 // whether this is an upper16 or a lower16 relocation. 1074 bool isUpper = getAnyRelocationLength(RE) >> 1; 1075 1076 if (isUpper) 1077 fmt << ":upper16:("; 1078 else 1079 fmt << ":lower16:("; 1080 printRelocationTargetName(this, RE, fmt); 1081 1082 DataRefImpl RelNext = Rel; 1083 moveRelocationNext(RelNext); 1084 MachO::any_relocation_info RENext = getRelocation(RelNext); 1085 1086 // ARM half relocs must be followed by a relocation of type 1087 // ARM_RELOC_PAIR. 1088 unsigned RType = getAnyRelocationType(RENext); 1089 if (RType != MachO::ARM_RELOC_PAIR) 1090 report_fatal_error("Expected ARM_RELOC_PAIR after " 1091 "ARM_RELOC_HALF"); 1092 1093 // NOTE: The half of the target virtual address is stashed in the 1094 // address field of the secondary relocation, but we can't reverse 1095 // engineer the constant offset from it without decoding the movw/movt 1096 // instruction to find the other half in its immediate field. 1097 1098 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the 1099 // symbol/section pointer of the follow-on relocation. 1100 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) { 1101 fmt << "-"; 1102 printRelocationTargetName(this, RENext, fmt); 1103 } 1104 1105 fmt << ")"; 1106 break; 1107 } 1108 default: { 1109 printRelocationTargetName(this, RE, fmt); 1110 } 1111 } 1112 } 1113 } else 1114 printRelocationTargetName(this, RE, fmt); 1115 1116 fmt.flush(); 1117 Result.append(fmtbuf.begin(), fmtbuf.end()); 1118 return object_error::success; 1119 } 1120 1121 error_code 1122 MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const { 1123 unsigned Arch = getArch(); 1124 uint64_t Type; 1125 getRelocationType(Rel, Type); 1126 1127 Result = false; 1128 1129 // On arches that use the generic relocations, GENERIC_RELOC_PAIR 1130 // is always hidden. 1131 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) { 1132 if (Type == MachO::GENERIC_RELOC_PAIR) Result = true; 1133 } else if (Arch == Triple::x86_64) { 1134 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows 1135 // an X86_64_RELOC_SUBTRACTOR. 1136 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) { 1137 DataRefImpl RelPrev = Rel; 1138 RelPrev.d.a--; 1139 uint64_t PrevType; 1140 getRelocationType(RelPrev, PrevType); 1141 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR) 1142 Result = true; 1143 } 1144 } 1145 1146 return object_error::success; 1147 } 1148 1149 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData, 1150 LibraryRef &Res) const { 1151 report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); 1152 } 1153 1154 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData, 1155 StringRef &Res) const { 1156 report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); 1157 } 1158 1159 basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const { 1160 DataRefImpl DRI; 1161 if (!SymtabLoadCmd) 1162 return basic_symbol_iterator(SymbolRef(DRI, this)); 1163 1164 MachO::symtab_command Symtab = getSymtabLoadCommand(); 1165 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff)); 1166 return basic_symbol_iterator(SymbolRef(DRI, this)); 1167 } 1168 1169 basic_symbol_iterator MachOObjectFile::symbol_end_impl() const { 1170 DataRefImpl DRI; 1171 if (!SymtabLoadCmd) 1172 return basic_symbol_iterator(SymbolRef(DRI, this)); 1173 1174 MachO::symtab_command Symtab = getSymtabLoadCommand(); 1175 unsigned SymbolTableEntrySize = is64Bit() ? 1176 sizeof(MachO::nlist_64) : 1177 sizeof(MachO::nlist); 1178 unsigned Offset = Symtab.symoff + 1179 Symtab.nsyms * SymbolTableEntrySize; 1180 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); 1181 return basic_symbol_iterator(SymbolRef(DRI, this)); 1182 } 1183 1184 section_iterator MachOObjectFile::section_begin() const { 1185 DataRefImpl DRI; 1186 return section_iterator(SectionRef(DRI, this)); 1187 } 1188 1189 section_iterator MachOObjectFile::section_end() const { 1190 DataRefImpl DRI; 1191 DRI.d.a = Sections.size(); 1192 return section_iterator(SectionRef(DRI, this)); 1193 } 1194 1195 library_iterator MachOObjectFile::needed_library_begin() const { 1196 // TODO: implement 1197 report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); 1198 } 1199 1200 library_iterator MachOObjectFile::needed_library_end() const { 1201 // TODO: implement 1202 report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); 1203 } 1204 1205 uint8_t MachOObjectFile::getBytesInAddress() const { 1206 return is64Bit() ? 8 : 4; 1207 } 1208 1209 StringRef MachOObjectFile::getFileFormatName() const { 1210 unsigned CPUType = getCPUType(this); 1211 if (!is64Bit()) { 1212 switch (CPUType) { 1213 case llvm::MachO::CPU_TYPE_I386: 1214 return "Mach-O 32-bit i386"; 1215 case llvm::MachO::CPU_TYPE_ARM: 1216 return "Mach-O arm"; 1217 case llvm::MachO::CPU_TYPE_POWERPC: 1218 return "Mach-O 32-bit ppc"; 1219 default: 1220 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 && 1221 "64-bit object file when we're not 64-bit?"); 1222 return "Mach-O 32-bit unknown"; 1223 } 1224 } 1225 1226 // Make sure the cpu type has the correct mask. 1227 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) 1228 == llvm::MachO::CPU_ARCH_ABI64 && 1229 "32-bit object file when we're 64-bit?"); 1230 1231 switch (CPUType) { 1232 case llvm::MachO::CPU_TYPE_X86_64: 1233 return "Mach-O 64-bit x86-64"; 1234 case llvm::MachO::CPU_TYPE_ARM64: 1235 return "Mach-O arm64"; 1236 case llvm::MachO::CPU_TYPE_POWERPC64: 1237 return "Mach-O 64-bit ppc64"; 1238 default: 1239 return "Mach-O 64-bit unknown"; 1240 } 1241 } 1242 1243 Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) { 1244 switch (CPUType) { 1245 case llvm::MachO::CPU_TYPE_I386: 1246 return Triple::x86; 1247 case llvm::MachO::CPU_TYPE_X86_64: 1248 return Triple::x86_64; 1249 case llvm::MachO::CPU_TYPE_ARM: 1250 return Triple::arm; 1251 case llvm::MachO::CPU_TYPE_ARM64: 1252 return Triple::arm64; 1253 case llvm::MachO::CPU_TYPE_POWERPC: 1254 return Triple::ppc; 1255 case llvm::MachO::CPU_TYPE_POWERPC64: 1256 return Triple::ppc64; 1257 default: 1258 return Triple::UnknownArch; 1259 } 1260 } 1261 1262 unsigned MachOObjectFile::getArch() const { 1263 return getArch(getCPUType(this)); 1264 } 1265 1266 StringRef MachOObjectFile::getLoadName() const { 1267 // TODO: Implement 1268 report_fatal_error("get_load_name() unimplemented in MachOObjectFile"); 1269 } 1270 1271 relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const { 1272 DataRefImpl DRI; 1273 DRI.d.a = Index; 1274 return section_rel_begin(DRI); 1275 } 1276 1277 relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const { 1278 DataRefImpl DRI; 1279 DRI.d.a = Index; 1280 return section_rel_end(DRI); 1281 } 1282 1283 dice_iterator MachOObjectFile::begin_dices() const { 1284 DataRefImpl DRI; 1285 if (!DataInCodeLoadCmd) 1286 return dice_iterator(DiceRef(DRI, this)); 1287 1288 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand(); 1289 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff)); 1290 return dice_iterator(DiceRef(DRI, this)); 1291 } 1292 1293 dice_iterator MachOObjectFile::end_dices() const { 1294 DataRefImpl DRI; 1295 if (!DataInCodeLoadCmd) 1296 return dice_iterator(DiceRef(DRI, this)); 1297 1298 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand(); 1299 unsigned Offset = DicLC.dataoff + DicLC.datasize; 1300 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); 1301 return dice_iterator(DiceRef(DRI, this)); 1302 } 1303 1304 StringRef 1305 MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const { 1306 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec); 1307 return parseSegmentOrSectionName(Raw.data()); 1308 } 1309 1310 ArrayRef<char> 1311 MachOObjectFile::getSectionRawName(DataRefImpl Sec) const { 1312 const section_base *Base = 1313 reinterpret_cast<const section_base *>(Sections[Sec.d.a]); 1314 return ArrayRef<char>(Base->sectname); 1315 } 1316 1317 ArrayRef<char> 1318 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const { 1319 const section_base *Base = 1320 reinterpret_cast<const section_base *>(Sections[Sec.d.a]); 1321 return ArrayRef<char>(Base->segname); 1322 } 1323 1324 bool 1325 MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE) 1326 const { 1327 if (getCPUType(this) == MachO::CPU_TYPE_X86_64) 1328 return false; 1329 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED; 1330 } 1331 1332 unsigned MachOObjectFile::getPlainRelocationSymbolNum( 1333 const MachO::any_relocation_info &RE) const { 1334 if (isLittleEndian()) 1335 return RE.r_word1 & 0xffffff; 1336 return RE.r_word1 >> 8; 1337 } 1338 1339 bool MachOObjectFile::getPlainRelocationExternal( 1340 const MachO::any_relocation_info &RE) const { 1341 if (isLittleEndian()) 1342 return (RE.r_word1 >> 27) & 1; 1343 return (RE.r_word1 >> 4) & 1; 1344 } 1345 1346 bool MachOObjectFile::getScatteredRelocationScattered( 1347 const MachO::any_relocation_info &RE) const { 1348 return RE.r_word0 >> 31; 1349 } 1350 1351 uint32_t MachOObjectFile::getScatteredRelocationValue( 1352 const MachO::any_relocation_info &RE) const { 1353 return RE.r_word1; 1354 } 1355 1356 unsigned MachOObjectFile::getAnyRelocationAddress( 1357 const MachO::any_relocation_info &RE) const { 1358 if (isRelocationScattered(RE)) 1359 return getScatteredRelocationAddress(RE); 1360 return getPlainRelocationAddress(RE); 1361 } 1362 1363 unsigned MachOObjectFile::getAnyRelocationPCRel( 1364 const MachO::any_relocation_info &RE) const { 1365 if (isRelocationScattered(RE)) 1366 return getScatteredRelocationPCRel(this, RE); 1367 return getPlainRelocationPCRel(this, RE); 1368 } 1369 1370 unsigned MachOObjectFile::getAnyRelocationLength( 1371 const MachO::any_relocation_info &RE) const { 1372 if (isRelocationScattered(RE)) 1373 return getScatteredRelocationLength(RE); 1374 return getPlainRelocationLength(this, RE); 1375 } 1376 1377 unsigned 1378 MachOObjectFile::getAnyRelocationType( 1379 const MachO::any_relocation_info &RE) const { 1380 if (isRelocationScattered(RE)) 1381 return getScatteredRelocationType(RE); 1382 return getPlainRelocationType(this, RE); 1383 } 1384 1385 SectionRef 1386 MachOObjectFile::getRelocationSection( 1387 const MachO::any_relocation_info &RE) const { 1388 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE)) 1389 return *section_end(); 1390 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1; 1391 DataRefImpl DRI; 1392 DRI.d.a = SecNum; 1393 return SectionRef(DRI, this); 1394 } 1395 1396 MachOObjectFile::LoadCommandInfo 1397 MachOObjectFile::getFirstLoadCommandInfo() const { 1398 MachOObjectFile::LoadCommandInfo Load; 1399 1400 unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) : 1401 sizeof(MachO::mach_header); 1402 Load.Ptr = getPtr(this, HeaderSize); 1403 Load.C = getStruct<MachO::load_command>(this, Load.Ptr); 1404 return Load; 1405 } 1406 1407 MachOObjectFile::LoadCommandInfo 1408 MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const { 1409 MachOObjectFile::LoadCommandInfo Next; 1410 Next.Ptr = L.Ptr + L.C.cmdsize; 1411 Next.C = getStruct<MachO::load_command>(this, Next.Ptr); 1412 return Next; 1413 } 1414 1415 MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const { 1416 return getStruct<MachO::section>(this, Sections[DRI.d.a]); 1417 } 1418 1419 MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const { 1420 return getStruct<MachO::section_64>(this, Sections[DRI.d.a]); 1421 } 1422 1423 MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L, 1424 unsigned Index) const { 1425 const char *Sec = getSectionPtr(this, L, Index); 1426 return getStruct<MachO::section>(this, Sec); 1427 } 1428 1429 MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L, 1430 unsigned Index) const { 1431 const char *Sec = getSectionPtr(this, L, Index); 1432 return getStruct<MachO::section_64>(this, Sec); 1433 } 1434 1435 MachO::nlist 1436 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { 1437 const char *P = reinterpret_cast<const char *>(DRI.p); 1438 return getStruct<MachO::nlist>(this, P); 1439 } 1440 1441 MachO::nlist_64 1442 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const { 1443 const char *P = reinterpret_cast<const char *>(DRI.p); 1444 return getStruct<MachO::nlist_64>(this, P); 1445 } 1446 1447 MachO::linkedit_data_command 1448 MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const { 1449 return getStruct<MachO::linkedit_data_command>(this, L.Ptr); 1450 } 1451 1452 MachO::segment_command 1453 MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const { 1454 return getStruct<MachO::segment_command>(this, L.Ptr); 1455 } 1456 1457 MachO::segment_command_64 1458 MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const { 1459 return getStruct<MachO::segment_command_64>(this, L.Ptr); 1460 } 1461 1462 MachO::linker_options_command 1463 MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const { 1464 return getStruct<MachO::linker_options_command>(this, L.Ptr); 1465 } 1466 1467 MachO::version_min_command 1468 MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const { 1469 return getStruct<MachO::version_min_command>(this, L.Ptr); 1470 } 1471 1472 MachO::any_relocation_info 1473 MachOObjectFile::getRelocation(DataRefImpl Rel) const { 1474 DataRefImpl Sec; 1475 Sec.d.a = Rel.d.a; 1476 uint32_t Offset; 1477 if (is64Bit()) { 1478 MachO::section_64 Sect = getSection64(Sec); 1479 Offset = Sect.reloff; 1480 } else { 1481 MachO::section Sect = getSection(Sec); 1482 Offset = Sect.reloff; 1483 } 1484 1485 auto P = reinterpret_cast<const MachO::any_relocation_info *>( 1486 getPtr(this, Offset)) + Rel.d.b; 1487 return getStruct<MachO::any_relocation_info>( 1488 this, reinterpret_cast<const char *>(P)); 1489 } 1490 1491 MachO::data_in_code_entry 1492 MachOObjectFile::getDice(DataRefImpl Rel) const { 1493 const char *P = reinterpret_cast<const char *>(Rel.p); 1494 return getStruct<MachO::data_in_code_entry>(this, P); 1495 } 1496 1497 MachO::mach_header MachOObjectFile::getHeader() const { 1498 return getStruct<MachO::mach_header>(this, getPtr(this, 0)); 1499 } 1500 1501 MachO::mach_header_64 MachOObjectFile::getHeader64() const { 1502 return getStruct<MachO::mach_header_64>(this, getPtr(this, 0)); 1503 } 1504 1505 uint32_t MachOObjectFile::getIndirectSymbolTableEntry( 1506 const MachO::dysymtab_command &DLC, 1507 unsigned Index) const { 1508 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t); 1509 return getStruct<uint32_t>(this, getPtr(this, Offset)); 1510 } 1511 1512 MachO::data_in_code_entry 1513 MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset, 1514 unsigned Index) const { 1515 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry); 1516 return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset)); 1517 } 1518 1519 MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const { 1520 return getStruct<MachO::symtab_command>(this, SymtabLoadCmd); 1521 } 1522 1523 MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const { 1524 return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd); 1525 } 1526 1527 MachO::linkedit_data_command 1528 MachOObjectFile::getDataInCodeLoadCommand() const { 1529 if (DataInCodeLoadCmd) 1530 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd); 1531 1532 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields. 1533 MachO::linkedit_data_command Cmd; 1534 Cmd.cmd = MachO::LC_DATA_IN_CODE; 1535 Cmd.cmdsize = sizeof(MachO::linkedit_data_command); 1536 Cmd.dataoff = 0; 1537 Cmd.datasize = 0; 1538 return Cmd; 1539 } 1540 1541 StringRef MachOObjectFile::getStringTableData() const { 1542 MachO::symtab_command S = getSymtabLoadCommand(); 1543 return getData().substr(S.stroff, S.strsize); 1544 } 1545 1546 bool MachOObjectFile::is64Bit() const { 1547 return getType() == getMachOType(false, true) || 1548 getType() == getMachOType(true, true); 1549 } 1550 1551 void MachOObjectFile::ReadULEB128s(uint64_t Index, 1552 SmallVectorImpl<uint64_t> &Out) const { 1553 DataExtractor extractor(ObjectFile::getData(), true, 0); 1554 1555 uint32_t offset = Index; 1556 uint64_t data = 0; 1557 while (uint64_t delta = extractor.getULEB128(&offset)) { 1558 data += delta; 1559 Out.push_back(data); 1560 } 1561 } 1562 1563 ErrorOr<ObjectFile *> ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer, 1564 bool BufferOwned) { 1565 StringRef Magic = Buffer->getBuffer().slice(0, 4); 1566 error_code EC; 1567 std::unique_ptr<MachOObjectFile> Ret; 1568 if (Magic == "\xFE\xED\xFA\xCE") 1569 Ret.reset(new MachOObjectFile(Buffer, false, false, EC, BufferOwned)); 1570 else if (Magic == "\xCE\xFA\xED\xFE") 1571 Ret.reset(new MachOObjectFile(Buffer, true, false, EC, BufferOwned)); 1572 else if (Magic == "\xFE\xED\xFA\xCF") 1573 Ret.reset(new MachOObjectFile(Buffer, false, true, EC, BufferOwned)); 1574 else if (Magic == "\xCF\xFA\xED\xFE") 1575 Ret.reset(new MachOObjectFile(Buffer, true, true, EC, BufferOwned)); 1576 else { 1577 delete Buffer; 1578 return object_error::parse_failed; 1579 } 1580 1581 if (EC) 1582 return EC; 1583 return Ret.release(); 1584 } 1585 1586 } // end namespace object 1587 } // end namespace llvm 1588