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