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 26 using namespace llvm; 27 using namespace object; 28 29 namespace llvm { 30 namespace object { 31 32 struct SymbolTableEntryBase { 33 uint32_t StringIndex; 34 uint8_t Type; 35 uint8_t SectionIndex; 36 uint16_t Flags; 37 }; 38 39 struct SectionBase { 40 char Name[16]; 41 char SegmentName[16]; 42 }; 43 44 template<typename T> 45 static void SwapValue(T &Value) { 46 Value = sys::SwapByteOrder(Value); 47 } 48 49 template<typename T> 50 static void SwapStruct(T &Value); 51 52 template<> 53 void SwapStruct(macho::RelocationEntry &H) { 54 SwapValue(H.Word0); 55 SwapValue(H.Word1); 56 } 57 58 template<> 59 void SwapStruct(macho::LoadCommand &L) { 60 SwapValue(L.Type); 61 SwapValue(L.Size); 62 } 63 64 template<> 65 void SwapStruct(SymbolTableEntryBase &S) { 66 SwapValue(S.StringIndex); 67 SwapValue(S.Flags); 68 } 69 70 template<> 71 void 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 83 template<> 84 void 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 97 template<> 98 void SwapStruct(macho::SymbolTableEntry &S) { 99 SwapValue(S.StringIndex); 100 SwapValue(S.Flags); 101 SwapValue(S.Value); 102 } 103 104 template<> 105 void SwapStruct(macho::Symbol64TableEntry &S) { 106 SwapValue(S.StringIndex); 107 SwapValue(S.Flags); 108 SwapValue(S.Value); 109 } 110 111 template<> 112 void 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 122 template<> 123 void SwapStruct(macho::Header64Ext &E) { 124 SwapValue(E.Reserved); 125 } 126 127 template<> 128 void 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 137 template<> 138 void 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 161 template<> 162 void SwapStruct(macho::LinkeditDataLoadCommand &C) { 163 SwapValue(C.Type); 164 SwapValue(C.Size); 165 SwapValue(C.DataOffset); 166 SwapValue(C.DataSize); 167 } 168 169 template<> 170 void 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 183 template<> 184 void 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 197 template<> 198 void SwapStruct(macho::IndirectSymbolTableEntry &C) { 199 SwapValue(C.Index); 200 } 201 202 template<> 203 void SwapStruct(macho::LinkerOptionsLoadCommand &C) { 204 SwapValue(C.Type); 205 SwapValue(C.Size); 206 SwapValue(C.Count); 207 } 208 209 template<> 210 void SwapStruct(macho::DataInCodeTableEntry &C) { 211 SwapValue(C.Offset); 212 SwapValue(C.Length); 213 SwapValue(C.Kind); 214 } 215 216 template<typename T> 217 T 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 225 static uint32_t 226 getSegmentLoadCommandNumSections(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 236 static const char * 237 getSectionPtr(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 251 static const char *getPtr(const MachOObjectFile *O, size_t Offset) { 252 return O->getData().substr(Offset, 1).data(); 253 } 254 255 static SymbolTableEntryBase 256 getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) { 257 const char *P = reinterpret_cast<const char *>(DRI.p); 258 return getStruct<SymbolTableEntryBase>(O, P); 259 } 260 261 static 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. 270 template<class T> 271 static 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 279 template<class T> 280 static void advanceTo(T &it, size_t Val) { 281 if (error_code ec = advance(it, Val)) 282 report_fatal_error(ec.message()); 283 } 284 285 static unsigned getCPUType(const MachOObjectFile *O) { 286 return O->getHeader().CPUType; 287 } 288 289 static 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 357 static uint32_t getPlainRelocationAddress(const macho::RelocationEntry &RE) { 358 return RE.Word0; 359 } 360 361 static unsigned 362 getScatteredRelocationAddress(const macho::RelocationEntry &RE) { 363 return RE.Word0 & 0xffffff; 364 } 365 366 static 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 373 static bool 374 getScatteredRelocationPCRel(const MachOObjectFile *O, 375 const macho::RelocationEntry &RE) { 376 return (RE.Word0 >> 30) & 1; 377 } 378 379 static 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 386 static unsigned 387 getScatteredRelocationLength(const macho::RelocationEntry &RE) { 388 return (RE.Word0 >> 28) & 3; 389 } 390 391 static 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 398 static unsigned getScatteredRelocationType(const macho::RelocationEntry &RE) { 399 return (RE.Word0 >> 24) & 0xf; 400 } 401 402 static 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 412 MachOObjectFile::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 444 error_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 454 error_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 463 error_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 475 error_code 476 MachOObjectFile::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 498 error_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 511 error_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 557 error_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 581 error_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 607 error_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 642 error_code 643 MachOObjectFile::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 659 error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb, 660 uint64_t &Val) const { 661 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile"); 662 } 663 664 error_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 671 error_code 672 MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const { 673 ArrayRef<char> Raw = getSectionRawName(Sec); 674 Result = parseSegmentOrSectionName(Raw.data()); 675 return object_error::success; 676 } 677 678 error_code 679 MachOObjectFile::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 690 error_code 691 MachOObjectFile::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 703 error_code 704 MachOObjectFile::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 722 error_code 723 MachOObjectFile::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 737 error_code 738 MachOObjectFile::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 744 error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const { 745 // FIXME: Unimplemented. 746 Result = false; 747 return object_error::success; 748 } 749 750 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const { 751 // FIXME: Unimplemented. 752 Result = false; 753 return object_error::success; 754 } 755 756 error_code 757 MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec, 758 bool &Result) const { 759 // FIXME: Unimplemented. 760 Result = true; 761 return object_error::success; 762 } 763 764 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec, 765 bool &Result) const { 766 // FIXME: Unimplemented. 767 Result = false; 768 return object_error::success; 769 } 770 771 error_code 772 MachOObjectFile::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 780 error_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 791 error_code 792 MachOObjectFile::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 813 relocation_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 828 relocation_iterator 829 MachOObjectFile::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 850 error_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 859 error_code 860 MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const { 861 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile"); 862 } 863 864 error_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 871 error_code 872 MachOObjectFile::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 892 error_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 899 error_code 900 MachOObjectFile::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 992 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel, 993 int64_t &Res) const { 994 Res = 0; 995 return object_error::success; 996 } 997 998 error_code 999 MachOObjectFile::getRelocationValueString(DataRefImpl Rel, 1000 SmallVectorImpl<char> &Result) const { 1001 macho::RelocationEntry RE = getRelocation(Rel); 1002 1003 unsigned Arch = this->getArch(); 1004 1005 std::string fmtbuf; 1006 raw_string_ostream fmt(fmtbuf); 1007 unsigned Type = this->getAnyRelocationType(RE); 1008 bool IsPCRel = this->getAnyRelocationPCRel(RE); 1009 1010 // Determine any addends that should be displayed with the relocation. 1011 // These require decoding the relocation type, which is triple-specific. 1012 1013 // X86_64 has entirely custom relocation types. 1014 if (Arch == Triple::x86_64) { 1015 bool isPCRel = getAnyRelocationPCRel(RE); 1016 1017 switch (Type) { 1018 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD 1019 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT 1020 printRelocationTargetName(this, RE, fmt); 1021 fmt << "@GOT"; 1022 if (isPCRel) fmt << "PCREL"; 1023 break; 1024 } 1025 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR 1026 DataRefImpl RelNext = Rel; 1027 RelNext.d.a++; 1028 macho::RelocationEntry RENext = getRelocation(RelNext); 1029 1030 // X86_64_SUBTRACTOR must be followed by a relocation of type 1031 // X86_64_RELOC_UNSIGNED. 1032 // NOTE: Scattered relocations don't exist on x86_64. 1033 unsigned RType = getAnyRelocationType(RENext); 1034 if (RType != 0) 1035 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after " 1036 "X86_64_RELOC_SUBTRACTOR."); 1037 1038 // The X86_64_RELOC_UNSIGNED contains the minuend symbol, 1039 // X86_64_SUBTRACTOR contains to the subtrahend. 1040 printRelocationTargetName(this, RENext, fmt); 1041 fmt << "-"; 1042 printRelocationTargetName(this, RE, fmt); 1043 break; 1044 } 1045 case macho::RIT_X86_64_TLV: 1046 printRelocationTargetName(this, RE, fmt); 1047 fmt << "@TLV"; 1048 if (isPCRel) fmt << "P"; 1049 break; 1050 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1 1051 printRelocationTargetName(this, RE, fmt); 1052 fmt << "-1"; 1053 break; 1054 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2 1055 printRelocationTargetName(this, RE, fmt); 1056 fmt << "-2"; 1057 break; 1058 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4 1059 printRelocationTargetName(this, RE, fmt); 1060 fmt << "-4"; 1061 break; 1062 default: 1063 printRelocationTargetName(this, RE, fmt); 1064 break; 1065 } 1066 // X86 and ARM share some relocation types in common. 1067 } else if (Arch == Triple::x86 || Arch == Triple::arm) { 1068 // Generic relocation types... 1069 switch (Type) { 1070 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info 1071 return object_error::success; 1072 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF 1073 DataRefImpl RelNext = Rel; 1074 RelNext.d.a++; 1075 macho::RelocationEntry RENext = getRelocation(RelNext); 1076 1077 // X86 sect diff's must be followed by a relocation of type 1078 // GENERIC_RELOC_PAIR. 1079 unsigned RType = getAnyRelocationType(RENext); 1080 1081 if (RType != 1) 1082 report_fatal_error("Expected GENERIC_RELOC_PAIR after " 1083 "GENERIC_RELOC_SECTDIFF."); 1084 1085 printRelocationTargetName(this, RE, fmt); 1086 fmt << "-"; 1087 printRelocationTargetName(this, RENext, fmt); 1088 break; 1089 } 1090 } 1091 1092 if (Arch == Triple::x86) { 1093 // All X86 relocations that need special printing were already 1094 // handled in the generic code. 1095 switch (Type) { 1096 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF 1097 DataRefImpl RelNext = Rel; 1098 RelNext.d.a++; 1099 macho::RelocationEntry RENext = getRelocation(RelNext); 1100 1101 // X86 sect diff's must be followed by a relocation of type 1102 // GENERIC_RELOC_PAIR. 1103 unsigned RType = getAnyRelocationType(RENext); 1104 if (RType != 1) 1105 report_fatal_error("Expected GENERIC_RELOC_PAIR after " 1106 "GENERIC_RELOC_LOCAL_SECTDIFF."); 1107 1108 printRelocationTargetName(this, RE, fmt); 1109 fmt << "-"; 1110 printRelocationTargetName(this, RENext, fmt); 1111 break; 1112 } 1113 case macho::RIT_Generic_TLV: { 1114 printRelocationTargetName(this, RE, fmt); 1115 fmt << "@TLV"; 1116 if (IsPCRel) fmt << "P"; 1117 break; 1118 } 1119 default: 1120 printRelocationTargetName(this, RE, fmt); 1121 } 1122 } else { // ARM-specific relocations 1123 switch (Type) { 1124 case macho::RIT_ARM_Half: // ARM_RELOC_HALF 1125 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF 1126 // Half relocations steal a bit from the length field to encode 1127 // whether this is an upper16 or a lower16 relocation. 1128 bool isUpper = getAnyRelocationLength(RE) >> 1; 1129 1130 if (isUpper) 1131 fmt << ":upper16:("; 1132 else 1133 fmt << ":lower16:("; 1134 printRelocationTargetName(this, RE, fmt); 1135 1136 DataRefImpl RelNext = Rel; 1137 RelNext.d.a++; 1138 macho::RelocationEntry RENext = getRelocation(RelNext); 1139 1140 // ARM half relocs must be followed by a relocation of type 1141 // ARM_RELOC_PAIR. 1142 unsigned RType = getAnyRelocationType(RENext); 1143 if (RType != 1) 1144 report_fatal_error("Expected ARM_RELOC_PAIR after " 1145 "GENERIC_RELOC_HALF"); 1146 1147 // NOTE: The half of the target virtual address is stashed in the 1148 // address field of the secondary relocation, but we can't reverse 1149 // engineer the constant offset from it without decoding the movw/movt 1150 // instruction to find the other half in its immediate field. 1151 1152 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the 1153 // symbol/section pointer of the follow-on relocation. 1154 if (Type == macho::RIT_ARM_HalfDifference) { 1155 fmt << "-"; 1156 printRelocationTargetName(this, RENext, fmt); 1157 } 1158 1159 fmt << ")"; 1160 break; 1161 } 1162 default: { 1163 printRelocationTargetName(this, RE, fmt); 1164 } 1165 } 1166 } 1167 } else 1168 printRelocationTargetName(this, RE, fmt); 1169 1170 fmt.flush(); 1171 Result.append(fmtbuf.begin(), fmtbuf.end()); 1172 return object_error::success; 1173 } 1174 1175 error_code 1176 MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const { 1177 unsigned Arch = getArch(); 1178 uint64_t Type; 1179 getRelocationType(Rel, Type); 1180 1181 Result = false; 1182 1183 // On arches that use the generic relocations, GENERIC_RELOC_PAIR 1184 // is always hidden. 1185 if (Arch == Triple::x86 || Arch == Triple::arm) { 1186 if (Type == macho::RIT_Pair) Result = true; 1187 } else if (Arch == Triple::x86_64) { 1188 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows 1189 // an X864_64_RELOC_SUBTRACTOR. 1190 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) { 1191 DataRefImpl RelPrev = Rel; 1192 RelPrev.d.a--; 1193 uint64_t PrevType; 1194 getRelocationType(RelPrev, PrevType); 1195 if (PrevType == macho::RIT_X86_64_Subtractor) 1196 Result = true; 1197 } 1198 } 1199 1200 return object_error::success; 1201 } 1202 1203 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData, 1204 LibraryRef &Res) const { 1205 report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); 1206 } 1207 1208 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData, 1209 StringRef &Res) const { 1210 report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); 1211 } 1212 1213 symbol_iterator MachOObjectFile::begin_symbols() const { 1214 DataRefImpl DRI; 1215 if (!SymtabLoadCmd) 1216 return symbol_iterator(SymbolRef(DRI, this)); 1217 1218 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand(); 1219 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.SymbolTableOffset)); 1220 return symbol_iterator(SymbolRef(DRI, this)); 1221 } 1222 1223 symbol_iterator MachOObjectFile::end_symbols() const { 1224 DataRefImpl DRI; 1225 if (!SymtabLoadCmd) 1226 return symbol_iterator(SymbolRef(DRI, this)); 1227 1228 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand(); 1229 unsigned SymbolTableEntrySize = is64Bit() ? 1230 sizeof(macho::Symbol64TableEntry) : 1231 sizeof(macho::SymbolTableEntry); 1232 unsigned Offset = Symtab.SymbolTableOffset + 1233 Symtab.NumSymbolTableEntries * SymbolTableEntrySize; 1234 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); 1235 return symbol_iterator(SymbolRef(DRI, this)); 1236 } 1237 1238 symbol_iterator MachOObjectFile::begin_dynamic_symbols() const { 1239 // TODO: implement 1240 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile"); 1241 } 1242 1243 symbol_iterator MachOObjectFile::end_dynamic_symbols() const { 1244 // TODO: implement 1245 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile"); 1246 } 1247 1248 section_iterator MachOObjectFile::begin_sections() const { 1249 DataRefImpl DRI; 1250 return section_iterator(SectionRef(DRI, this)); 1251 } 1252 1253 section_iterator MachOObjectFile::end_sections() const { 1254 DataRefImpl DRI; 1255 DRI.d.a = Sections.size(); 1256 return section_iterator(SectionRef(DRI, this)); 1257 } 1258 1259 library_iterator MachOObjectFile::begin_libraries_needed() const { 1260 // TODO: implement 1261 report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); 1262 } 1263 1264 library_iterator MachOObjectFile::end_libraries_needed() const { 1265 // TODO: implement 1266 report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); 1267 } 1268 1269 uint8_t MachOObjectFile::getBytesInAddress() const { 1270 return is64Bit() ? 8 : 4; 1271 } 1272 1273 StringRef MachOObjectFile::getFileFormatName() const { 1274 unsigned CPUType = getCPUType(this); 1275 if (!is64Bit()) { 1276 switch (CPUType) { 1277 case llvm::MachO::CPUTypeI386: 1278 return "Mach-O 32-bit i386"; 1279 case llvm::MachO::CPUTypeARM: 1280 return "Mach-O arm"; 1281 case llvm::MachO::CPUTypePowerPC: 1282 return "Mach-O 32-bit ppc"; 1283 default: 1284 assert((CPUType & llvm::MachO::CPUArchABI64) == 0 && 1285 "64-bit object file when we're not 64-bit?"); 1286 return "Mach-O 32-bit unknown"; 1287 } 1288 } 1289 1290 // Make sure the cpu type has the correct mask. 1291 assert((CPUType & llvm::MachO::CPUArchABI64) 1292 == llvm::MachO::CPUArchABI64 && 1293 "32-bit object file when we're 64-bit?"); 1294 1295 switch (CPUType) { 1296 case llvm::MachO::CPUTypeX86_64: 1297 return "Mach-O 64-bit x86-64"; 1298 case llvm::MachO::CPUTypePowerPC64: 1299 return "Mach-O 64-bit ppc64"; 1300 default: 1301 return "Mach-O 64-bit unknown"; 1302 } 1303 } 1304 1305 unsigned MachOObjectFile::getArch() const { 1306 switch (getCPUType(this)) { 1307 case llvm::MachO::CPUTypeI386: 1308 return Triple::x86; 1309 case llvm::MachO::CPUTypeX86_64: 1310 return Triple::x86_64; 1311 case llvm::MachO::CPUTypeARM: 1312 return Triple::arm; 1313 case llvm::MachO::CPUTypePowerPC: 1314 return Triple::ppc; 1315 case llvm::MachO::CPUTypePowerPC64: 1316 return Triple::ppc64; 1317 default: 1318 return Triple::UnknownArch; 1319 } 1320 } 1321 1322 StringRef MachOObjectFile::getLoadName() const { 1323 // TODO: Implement 1324 report_fatal_error("get_load_name() unimplemented in MachOObjectFile"); 1325 } 1326 1327 relocation_iterator MachOObjectFile::getSectionRelBegin(unsigned Index) const { 1328 DataRefImpl DRI; 1329 DRI.d.a = Index; 1330 return getSectionRelBegin(DRI); 1331 } 1332 1333 relocation_iterator MachOObjectFile::getSectionRelEnd(unsigned Index) const { 1334 DataRefImpl DRI; 1335 DRI.d.a = Index; 1336 return getSectionRelEnd(DRI); 1337 } 1338 1339 StringRef 1340 MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const { 1341 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec); 1342 return parseSegmentOrSectionName(Raw.data()); 1343 } 1344 1345 ArrayRef<char> 1346 MachOObjectFile::getSectionRawName(DataRefImpl Sec) const { 1347 const SectionBase *Base = 1348 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]); 1349 return ArrayRef<char>(Base->Name); 1350 } 1351 1352 ArrayRef<char> 1353 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const { 1354 const SectionBase *Base = 1355 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]); 1356 return ArrayRef<char>(Base->SegmentName); 1357 } 1358 1359 bool 1360 MachOObjectFile::isRelocationScattered(const macho::RelocationEntry &RE) 1361 const { 1362 if (getCPUType(this) == llvm::MachO::CPUTypeX86_64) 1363 return false; 1364 return getPlainRelocationAddress(RE) & macho::RF_Scattered; 1365 } 1366 1367 unsigned MachOObjectFile::getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const { 1368 if (isLittleEndian()) 1369 return RE.Word1 & 0xffffff; 1370 return RE.Word1 >> 8; 1371 } 1372 1373 bool MachOObjectFile::getPlainRelocationExternal(const macho::RelocationEntry &RE) const { 1374 if (isLittleEndian()) 1375 return (RE.Word1 >> 27) & 1; 1376 return (RE.Word1 >> 4) & 1; 1377 } 1378 1379 bool 1380 MachOObjectFile::getScatteredRelocationScattered(const macho::RelocationEntry &RE) const { 1381 return RE.Word0 >> 31; 1382 } 1383 1384 uint32_t 1385 MachOObjectFile::getScatteredRelocationValue(const macho::RelocationEntry &RE) const { 1386 return RE.Word1; 1387 } 1388 1389 unsigned 1390 MachOObjectFile::getAnyRelocationAddress(const macho::RelocationEntry &RE) const { 1391 if (isRelocationScattered(RE)) 1392 return getScatteredRelocationAddress(RE); 1393 return getPlainRelocationAddress(RE); 1394 } 1395 1396 unsigned 1397 MachOObjectFile::getAnyRelocationPCRel(const macho::RelocationEntry &RE) const { 1398 if (isRelocationScattered(RE)) 1399 return getScatteredRelocationPCRel(this, RE); 1400 return getPlainRelocationPCRel(this, RE); 1401 } 1402 1403 unsigned 1404 MachOObjectFile::getAnyRelocationLength(const macho::RelocationEntry &RE) const { 1405 if (isRelocationScattered(RE)) 1406 return getScatteredRelocationLength(RE); 1407 return getPlainRelocationLength(this, RE); 1408 } 1409 1410 unsigned 1411 MachOObjectFile::getAnyRelocationType(const macho::RelocationEntry &RE) const { 1412 if (isRelocationScattered(RE)) 1413 return getScatteredRelocationType(RE); 1414 return getPlainRelocationType(this, RE); 1415 } 1416 1417 SectionRef 1418 MachOObjectFile::getRelocationSection(const macho::RelocationEntry &RE) const { 1419 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE)) 1420 return *end_sections(); 1421 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1; 1422 DataRefImpl DRI; 1423 DRI.d.a = SecNum; 1424 return SectionRef(DRI, this); 1425 } 1426 1427 MachOObjectFile::LoadCommandInfo 1428 MachOObjectFile::getFirstLoadCommandInfo() const { 1429 MachOObjectFile::LoadCommandInfo Load; 1430 1431 unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size; 1432 Load.Ptr = getPtr(this, HeaderSize); 1433 Load.C = getStruct<macho::LoadCommand>(this, Load.Ptr); 1434 return Load; 1435 } 1436 1437 MachOObjectFile::LoadCommandInfo 1438 MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const { 1439 MachOObjectFile::LoadCommandInfo Next; 1440 Next.Ptr = L.Ptr + L.C.Size; 1441 Next.C = getStruct<macho::LoadCommand>(this, Next.Ptr); 1442 return Next; 1443 } 1444 1445 macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const { 1446 return getStruct<macho::Section>(this, Sections[DRI.d.a]); 1447 } 1448 1449 macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const { 1450 return getStruct<macho::Section64>(this, Sections[DRI.d.a]); 1451 } 1452 1453 macho::Section MachOObjectFile::getSection(const LoadCommandInfo &L, 1454 unsigned Index) const { 1455 const char *Sec = getSectionPtr(this, L, Index); 1456 return getStruct<macho::Section>(this, Sec); 1457 } 1458 1459 macho::Section64 MachOObjectFile::getSection64(const LoadCommandInfo &L, 1460 unsigned Index) const { 1461 const char *Sec = getSectionPtr(this, L, Index); 1462 return getStruct<macho::Section64>(this, Sec); 1463 } 1464 1465 macho::SymbolTableEntry 1466 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { 1467 const char *P = reinterpret_cast<const char *>(DRI.p); 1468 return getStruct<macho::SymbolTableEntry>(this, P); 1469 } 1470 1471 macho::Symbol64TableEntry 1472 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const { 1473 const char *P = reinterpret_cast<const char *>(DRI.p); 1474 return getStruct<macho::Symbol64TableEntry>(this, P); 1475 } 1476 1477 macho::LinkeditDataLoadCommand 1478 MachOObjectFile::getLinkeditDataLoadCommand(const MachOObjectFile::LoadCommandInfo &L) const { 1479 return getStruct<macho::LinkeditDataLoadCommand>(this, L.Ptr); 1480 } 1481 1482 macho::SegmentLoadCommand 1483 MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const { 1484 return getStruct<macho::SegmentLoadCommand>(this, L.Ptr); 1485 } 1486 1487 macho::Segment64LoadCommand 1488 MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const { 1489 return getStruct<macho::Segment64LoadCommand>(this, L.Ptr); 1490 } 1491 1492 macho::LinkerOptionsLoadCommand 1493 MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const { 1494 return getStruct<macho::LinkerOptionsLoadCommand>(this, L.Ptr); 1495 } 1496 1497 macho::RelocationEntry 1498 MachOObjectFile::getRelocation(DataRefImpl Rel) const { 1499 const char *P = reinterpret_cast<const char *>(Rel.p); 1500 return getStruct<macho::RelocationEntry>(this, P); 1501 } 1502 1503 macho::Header MachOObjectFile::getHeader() const { 1504 return getStruct<macho::Header>(this, getPtr(this, 0)); 1505 } 1506 1507 macho::Header64Ext MachOObjectFile::getHeader64Ext() const { 1508 return 1509 getStruct<macho::Header64Ext>(this, getPtr(this, sizeof(macho::Header))); 1510 } 1511 1512 macho::IndirectSymbolTableEntry MachOObjectFile::getIndirectSymbolTableEntry( 1513 const macho::DysymtabLoadCommand &DLC, 1514 unsigned Index) const { 1515 uint64_t Offset = DLC.IndirectSymbolTableOffset + 1516 Index * sizeof(macho::IndirectSymbolTableEntry); 1517 return getStruct<macho::IndirectSymbolTableEntry>(this, getPtr(this, Offset)); 1518 } 1519 1520 macho::DataInCodeTableEntry 1521 MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset, 1522 unsigned Index) const { 1523 uint64_t Offset = DataOffset + Index * sizeof(macho::DataInCodeTableEntry); 1524 return getStruct<macho::DataInCodeTableEntry>(this, getPtr(this, Offset)); 1525 } 1526 1527 macho::SymtabLoadCommand MachOObjectFile::getSymtabLoadCommand() const { 1528 return getStruct<macho::SymtabLoadCommand>(this, SymtabLoadCmd); 1529 } 1530 1531 macho::DysymtabLoadCommand MachOObjectFile::getDysymtabLoadCommand() const { 1532 return getStruct<macho::DysymtabLoadCommand>(this, DysymtabLoadCmd); 1533 } 1534 1535 StringRef MachOObjectFile::getStringTableData() const { 1536 macho::SymtabLoadCommand S = getSymtabLoadCommand(); 1537 return getData().substr(S.StringTableOffset, S.StringTableSize); 1538 } 1539 1540 bool MachOObjectFile::is64Bit() const { 1541 return getType() == getMachOType(false, true) || 1542 getType() == getMachOType(true, true); 1543 } 1544 1545 void MachOObjectFile::ReadULEB128s(uint64_t Index, 1546 SmallVectorImpl<uint64_t> &Out) const { 1547 DataExtractor extractor(ObjectFile::getData(), true, 0); 1548 1549 uint32_t offset = Index; 1550 uint64_t data = 0; 1551 while (uint64_t delta = extractor.getULEB128(&offset)) { 1552 data += delta; 1553 Out.push_back(data); 1554 } 1555 } 1556 1557 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) { 1558 StringRef Magic = Buffer->getBuffer().slice(0, 4); 1559 error_code ec; 1560 ObjectFile *Ret; 1561 if (Magic == "\xFE\xED\xFA\xCE") 1562 Ret = new MachOObjectFile(Buffer, false, false, ec); 1563 else if (Magic == "\xCE\xFA\xED\xFE") 1564 Ret = new MachOObjectFile(Buffer, true, false, ec); 1565 else if (Magic == "\xFE\xED\xFA\xCF") 1566 Ret = new MachOObjectFile(Buffer, false, true, ec); 1567 else if (Magic == "\xCF\xFA\xED\xFE") 1568 Ret = new MachOObjectFile(Buffer, true, true, ec); 1569 else 1570 return NULL; 1571 1572 if (ec) 1573 return NULL; 1574 return Ret; 1575 } 1576 1577 } // end namespace object 1578 } // end namespace llvm 1579