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::getSymbolSize(DataRefImpl DRI, 499 uint64_t &Result) const { 500 uint64_t BeginOffset; 501 uint64_t EndOffset = 0; 502 uint8_t SectionIndex; 503 504 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI); 505 uint64_t Value; 506 getSymbolAddress(DRI, Value); 507 508 BeginOffset = Value; 509 510 SectionIndex = Entry.SectionIndex; 511 if (!SectionIndex) { 512 uint32_t flags = SymbolRef::SF_None; 513 this->getSymbolFlags(DRI, flags); 514 if (flags & SymbolRef::SF_Common) 515 Result = Value; 516 else 517 Result = UnknownAddressOrSize; 518 return object_error::success; 519 } 520 // Unfortunately symbols are unsorted so we need to touch all 521 // symbols from load command 522 error_code ec; 523 for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E; 524 I.increment(ec)) { 525 DataRefImpl DRI = I->getRawDataRefImpl(); 526 Entry = getSymbolTableEntryBase(this, DRI); 527 getSymbolAddress(DRI, Value); 528 if (Entry.SectionIndex == SectionIndex && Value > BeginOffset) 529 if (!EndOffset || Value < EndOffset) 530 EndOffset = Value; 531 } 532 if (!EndOffset) { 533 uint64_t Size; 534 DataRefImpl Sec; 535 Sec.d.a = SectionIndex-1; 536 getSectionSize(Sec, Size); 537 getSectionAddress(Sec, EndOffset); 538 EndOffset += Size; 539 } 540 Result = EndOffset - BeginOffset; 541 return object_error::success; 542 } 543 544 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb, 545 SymbolRef::Type &Res) const { 546 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb); 547 uint8_t n_type = Entry.Type; 548 549 Res = SymbolRef::ST_Other; 550 551 // If this is a STAB debugging symbol, we can do nothing more. 552 if (n_type & MachO::NlistMaskStab) { 553 Res = SymbolRef::ST_Debug; 554 return object_error::success; 555 } 556 557 switch (n_type & MachO::NlistMaskType) { 558 case MachO::NListTypeUndefined : 559 Res = SymbolRef::ST_Unknown; 560 break; 561 case MachO::NListTypeSection : 562 Res = SymbolRef::ST_Function; 563 break; 564 } 565 return object_error::success; 566 } 567 568 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl Symb, 569 char &Res) const { 570 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb); 571 uint8_t Type = Entry.Type; 572 uint16_t Flags = Entry.Flags; 573 574 char Char; 575 switch (Type & macho::STF_TypeMask) { 576 case macho::STT_Undefined: 577 Char = 'u'; 578 break; 579 case macho::STT_Absolute: 580 case macho::STT_Section: 581 Char = 's'; 582 break; 583 default: 584 Char = '?'; 585 break; 586 } 587 588 if (Flags & (macho::STF_External | macho::STF_PrivateExtern)) 589 Char = toupper(static_cast<unsigned char>(Char)); 590 Res = Char; 591 return object_error::success; 592 } 593 594 error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI, 595 uint32_t &Result) const { 596 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI); 597 598 uint8_t MachOType = Entry.Type; 599 uint16_t MachOFlags = Entry.Flags; 600 601 // TODO: Correctly set SF_ThreadLocal 602 Result = SymbolRef::SF_None; 603 604 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined) 605 Result |= SymbolRef::SF_Undefined; 606 607 if (MachOFlags & macho::STF_StabsEntryMask) 608 Result |= SymbolRef::SF_FormatSpecific; 609 610 if (MachOType & MachO::NlistMaskExternal) { 611 Result |= SymbolRef::SF_Global; 612 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined) 613 Result |= SymbolRef::SF_Common; 614 } 615 616 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef)) 617 Result |= SymbolRef::SF_Weak; 618 619 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute) 620 Result |= SymbolRef::SF_Absolute; 621 622 return object_error::success; 623 } 624 625 error_code 626 MachOObjectFile::getSymbolSection(DataRefImpl Symb, 627 section_iterator &Res) const { 628 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb); 629 uint8_t index = Entry.SectionIndex; 630 631 if (index == 0) { 632 Res = end_sections(); 633 } else { 634 DataRefImpl DRI; 635 DRI.d.a = index - 1; 636 Res = section_iterator(SectionRef(DRI, this)); 637 } 638 639 return object_error::success; 640 } 641 642 error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb, 643 uint64_t &Val) const { 644 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile"); 645 } 646 647 error_code MachOObjectFile::getSectionNext(DataRefImpl Sec, 648 SectionRef &Res) const { 649 Sec.d.a++; 650 Res = SectionRef(Sec, this); 651 return object_error::success; 652 } 653 654 error_code 655 MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const { 656 ArrayRef<char> Raw = getSectionRawName(Sec); 657 Result = parseSegmentOrSectionName(Raw.data()); 658 return object_error::success; 659 } 660 661 error_code 662 MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const { 663 if (is64Bit()) { 664 macho::Section64 Sect = getSection64(Sec); 665 Res = Sect.Address; 666 } else { 667 macho::Section Sect = getSection(Sec); 668 Res = Sect.Address; 669 } 670 return object_error::success; 671 } 672 673 error_code 674 MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const { 675 if (is64Bit()) { 676 macho::Section64 Sect = getSection64(Sec); 677 Res = Sect.Size; 678 } else { 679 macho::Section Sect = getSection(Sec); 680 Res = Sect.Size; 681 } 682 683 return object_error::success; 684 } 685 686 error_code 687 MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const { 688 uint32_t Offset; 689 uint64_t Size; 690 691 if (is64Bit()) { 692 macho::Section64 Sect = getSection64(Sec); 693 Offset = Sect.Offset; 694 Size = Sect.Size; 695 } else { 696 macho::Section Sect =getSection(Sec); 697 Offset = Sect.Offset; 698 Size = Sect.Size; 699 } 700 701 Res = this->getData().substr(Offset, Size); 702 return object_error::success; 703 } 704 705 error_code 706 MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const { 707 uint32_t Align; 708 if (is64Bit()) { 709 macho::Section64 Sect = getSection64(Sec); 710 Align = Sect.Align; 711 } else { 712 macho::Section Sect = getSection(Sec); 713 Align = Sect.Align; 714 } 715 716 Res = uint64_t(1) << Align; 717 return object_error::success; 718 } 719 720 error_code 721 MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const { 722 uint32_t Flags = getSectionFlags(this, Sec); 723 Res = Flags & macho::SF_PureInstructions; 724 return object_error::success; 725 } 726 727 error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const { 728 // FIXME: Unimplemented. 729 Result = false; 730 return object_error::success; 731 } 732 733 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const { 734 // FIXME: Unimplemented. 735 Result = false; 736 return object_error::success; 737 } 738 739 error_code 740 MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec, 741 bool &Result) const { 742 // FIXME: Unimplemented. 743 Result = true; 744 return object_error::success; 745 } 746 747 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec, 748 bool &Result) const { 749 // FIXME: Unimplemented. 750 Result = false; 751 return object_error::success; 752 } 753 754 error_code 755 MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const { 756 uint32_t Flags = getSectionFlags(this, Sec); 757 unsigned SectionType = Flags & MachO::SectionFlagMaskSectionType; 758 Res = SectionType == MachO::SectionTypeZeroFill || 759 SectionType == MachO::SectionTypeZeroFillLarge; 760 return object_error::success; 761 } 762 763 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec, 764 bool &Result) const { 765 // Consider using the code from isSectionText to look for __const sections. 766 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS 767 // to use section attributes to distinguish code from data. 768 769 // FIXME: Unimplemented. 770 Result = false; 771 return object_error::success; 772 } 773 774 error_code 775 MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, 776 bool &Result) const { 777 SymbolRef::Type ST; 778 this->getSymbolType(Symb, ST); 779 if (ST == SymbolRef::ST_Unknown) { 780 Result = false; 781 return object_error::success; 782 } 783 784 uint64_t SectBegin, SectEnd; 785 getSectionAddress(Sec, SectBegin); 786 getSectionSize(Sec, SectEnd); 787 SectEnd += SectBegin; 788 789 uint64_t SymAddr; 790 getSymbolAddress(Symb, SymAddr); 791 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd); 792 793 return object_error::success; 794 } 795 796 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const { 797 uint32_t Offset; 798 if (is64Bit()) { 799 macho::Section64 Sect = getSection64(Sec); 800 Offset = Sect.RelocationTableOffset; 801 } else { 802 macho::Section Sect = getSection(Sec); 803 Offset = Sect.RelocationTableOffset; 804 } 805 806 DataRefImpl Ret; 807 Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); 808 return relocation_iterator(RelocationRef(Ret, this)); 809 } 810 811 relocation_iterator 812 MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const { 813 uint32_t Offset; 814 uint32_t Num; 815 if (is64Bit()) { 816 macho::Section64 Sect = getSection64(Sec); 817 Offset = Sect.RelocationTableOffset; 818 Num = Sect.NumRelocationTableEntries; 819 } else { 820 macho::Section Sect = getSection(Sec); 821 Offset = Sect.RelocationTableOffset; 822 Num = Sect.NumRelocationTableEntries; 823 } 824 825 const macho::RelocationEntry *P = 826 reinterpret_cast<const macho::RelocationEntry*>(getPtr(this, Offset)); 827 828 DataRefImpl Ret; 829 Ret.p = reinterpret_cast<uintptr_t>(P + Num); 830 return relocation_iterator(RelocationRef(Ret, this)); 831 } 832 833 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel, 834 RelocationRef &Res) const { 835 const macho::RelocationEntry *P = 836 reinterpret_cast<const macho::RelocationEntry *>(Rel.p); 837 Rel.p = reinterpret_cast<uintptr_t>(P + 1); 838 Res = RelocationRef(Rel, this); 839 return object_error::success; 840 } 841 842 error_code 843 MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const { 844 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile"); 845 } 846 847 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel, 848 uint64_t &Res) const { 849 macho::RelocationEntry RE = getRelocation(Rel); 850 Res = getAnyRelocationAddress(RE); 851 return object_error::success; 852 } 853 854 error_code 855 MachOObjectFile::getRelocationSymbol(DataRefImpl Rel, SymbolRef &Res) const { 856 macho::RelocationEntry RE = getRelocation(Rel); 857 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE); 858 bool isExtern = getPlainRelocationExternal(RE); 859 if (!isExtern) { 860 Res = *end_symbols(); 861 return object_error::success; 862 } 863 864 macho::SymtabLoadCommand S = getSymtabLoadCommand(); 865 unsigned SymbolTableEntrySize = is64Bit() ? 866 sizeof(macho::Symbol64TableEntry) : 867 sizeof(macho::SymbolTableEntry); 868 uint64_t Offset = S.SymbolTableOffset + SymbolIdx * SymbolTableEntrySize; 869 DataRefImpl Sym; 870 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); 871 Res = SymbolRef(Sym, this); 872 return object_error::success; 873 } 874 875 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel, 876 uint64_t &Res) const { 877 macho::RelocationEntry RE = getRelocation(Rel); 878 Res = getAnyRelocationType(RE); 879 return object_error::success; 880 } 881 882 error_code 883 MachOObjectFile::getRelocationTypeName(DataRefImpl Rel, 884 SmallVectorImpl<char> &Result) const { 885 StringRef res; 886 uint64_t RType; 887 getRelocationType(Rel, RType); 888 889 unsigned Arch = this->getArch(); 890 891 switch (Arch) { 892 case Triple::x86: { 893 static const char *const Table[] = { 894 "GENERIC_RELOC_VANILLA", 895 "GENERIC_RELOC_PAIR", 896 "GENERIC_RELOC_SECTDIFF", 897 "GENERIC_RELOC_PB_LA_PTR", 898 "GENERIC_RELOC_LOCAL_SECTDIFF", 899 "GENERIC_RELOC_TLV" }; 900 901 if (RType > 6) 902 res = "Unknown"; 903 else 904 res = Table[RType]; 905 break; 906 } 907 case Triple::x86_64: { 908 static const char *const Table[] = { 909 "X86_64_RELOC_UNSIGNED", 910 "X86_64_RELOC_SIGNED", 911 "X86_64_RELOC_BRANCH", 912 "X86_64_RELOC_GOT_LOAD", 913 "X86_64_RELOC_GOT", 914 "X86_64_RELOC_SUBTRACTOR", 915 "X86_64_RELOC_SIGNED_1", 916 "X86_64_RELOC_SIGNED_2", 917 "X86_64_RELOC_SIGNED_4", 918 "X86_64_RELOC_TLV" }; 919 920 if (RType > 9) 921 res = "Unknown"; 922 else 923 res = Table[RType]; 924 break; 925 } 926 case Triple::arm: { 927 static const char *const Table[] = { 928 "ARM_RELOC_VANILLA", 929 "ARM_RELOC_PAIR", 930 "ARM_RELOC_SECTDIFF", 931 "ARM_RELOC_LOCAL_SECTDIFF", 932 "ARM_RELOC_PB_LA_PTR", 933 "ARM_RELOC_BR24", 934 "ARM_THUMB_RELOC_BR22", 935 "ARM_THUMB_32BIT_BRANCH", 936 "ARM_RELOC_HALF", 937 "ARM_RELOC_HALF_SECTDIFF" }; 938 939 if (RType > 9) 940 res = "Unknown"; 941 else 942 res = Table[RType]; 943 break; 944 } 945 case Triple::ppc: { 946 static const char *const Table[] = { 947 "PPC_RELOC_VANILLA", 948 "PPC_RELOC_PAIR", 949 "PPC_RELOC_BR14", 950 "PPC_RELOC_BR24", 951 "PPC_RELOC_HI16", 952 "PPC_RELOC_LO16", 953 "PPC_RELOC_HA16", 954 "PPC_RELOC_LO14", 955 "PPC_RELOC_SECTDIFF", 956 "PPC_RELOC_PB_LA_PTR", 957 "PPC_RELOC_HI16_SECTDIFF", 958 "PPC_RELOC_LO16_SECTDIFF", 959 "PPC_RELOC_HA16_SECTDIFF", 960 "PPC_RELOC_JBSR", 961 "PPC_RELOC_LO14_SECTDIFF", 962 "PPC_RELOC_LOCAL_SECTDIFF" }; 963 964 res = Table[RType]; 965 break; 966 } 967 case Triple::UnknownArch: 968 res = "Unknown"; 969 break; 970 } 971 Result.append(res.begin(), res.end()); 972 return object_error::success; 973 } 974 975 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel, 976 int64_t &Res) const { 977 Res = 0; 978 return object_error::success; 979 } 980 981 error_code 982 MachOObjectFile::getRelocationValueString(DataRefImpl Rel, 983 SmallVectorImpl<char> &Result) const { 984 macho::RelocationEntry RE = getRelocation(Rel); 985 986 unsigned Arch = this->getArch(); 987 988 std::string fmtbuf; 989 raw_string_ostream fmt(fmtbuf); 990 unsigned Type = this->getAnyRelocationType(RE); 991 bool IsPCRel = this->getAnyRelocationPCRel(RE); 992 993 // Determine any addends that should be displayed with the relocation. 994 // These require decoding the relocation type, which is triple-specific. 995 996 // X86_64 has entirely custom relocation types. 997 if (Arch == Triple::x86_64) { 998 bool isPCRel = getAnyRelocationPCRel(RE); 999 1000 switch (Type) { 1001 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD 1002 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT 1003 printRelocationTargetName(this, RE, fmt); 1004 fmt << "@GOT"; 1005 if (isPCRel) fmt << "PCREL"; 1006 break; 1007 } 1008 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR 1009 DataRefImpl RelNext = Rel; 1010 RelNext.d.a++; 1011 macho::RelocationEntry RENext = getRelocation(RelNext); 1012 1013 // X86_64_SUBTRACTOR must be followed by a relocation of type 1014 // X86_64_RELOC_UNSIGNED. 1015 // NOTE: Scattered relocations don't exist on x86_64. 1016 unsigned RType = getAnyRelocationType(RENext); 1017 if (RType != 0) 1018 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after " 1019 "X86_64_RELOC_SUBTRACTOR."); 1020 1021 // The X86_64_RELOC_UNSIGNED contains the minuend symbol, 1022 // X86_64_SUBTRACTOR contains to the subtrahend. 1023 printRelocationTargetName(this, RENext, fmt); 1024 fmt << "-"; 1025 printRelocationTargetName(this, RE, fmt); 1026 break; 1027 } 1028 case macho::RIT_X86_64_TLV: 1029 printRelocationTargetName(this, RE, fmt); 1030 fmt << "@TLV"; 1031 if (isPCRel) fmt << "P"; 1032 break; 1033 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1 1034 printRelocationTargetName(this, RE, fmt); 1035 fmt << "-1"; 1036 break; 1037 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2 1038 printRelocationTargetName(this, RE, fmt); 1039 fmt << "-2"; 1040 break; 1041 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4 1042 printRelocationTargetName(this, RE, fmt); 1043 fmt << "-4"; 1044 break; 1045 default: 1046 printRelocationTargetName(this, RE, fmt); 1047 break; 1048 } 1049 // X86 and ARM share some relocation types in common. 1050 } else if (Arch == Triple::x86 || Arch == Triple::arm) { 1051 // Generic relocation types... 1052 switch (Type) { 1053 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info 1054 return object_error::success; 1055 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF 1056 DataRefImpl RelNext = Rel; 1057 RelNext.d.a++; 1058 macho::RelocationEntry RENext = getRelocation(RelNext); 1059 1060 // X86 sect diff's must be followed by a relocation of type 1061 // GENERIC_RELOC_PAIR. 1062 unsigned RType = getAnyRelocationType(RENext); 1063 1064 if (RType != 1) 1065 report_fatal_error("Expected GENERIC_RELOC_PAIR after " 1066 "GENERIC_RELOC_SECTDIFF."); 1067 1068 printRelocationTargetName(this, RE, fmt); 1069 fmt << "-"; 1070 printRelocationTargetName(this, RENext, fmt); 1071 break; 1072 } 1073 } 1074 1075 if (Arch == Triple::x86) { 1076 // All X86 relocations that need special printing were already 1077 // handled in the generic code. 1078 switch (Type) { 1079 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF 1080 DataRefImpl RelNext = Rel; 1081 RelNext.d.a++; 1082 macho::RelocationEntry RENext = getRelocation(RelNext); 1083 1084 // X86 sect diff's must be followed by a relocation of type 1085 // GENERIC_RELOC_PAIR. 1086 unsigned RType = getAnyRelocationType(RENext); 1087 if (RType != 1) 1088 report_fatal_error("Expected GENERIC_RELOC_PAIR after " 1089 "GENERIC_RELOC_LOCAL_SECTDIFF."); 1090 1091 printRelocationTargetName(this, RE, fmt); 1092 fmt << "-"; 1093 printRelocationTargetName(this, RENext, fmt); 1094 break; 1095 } 1096 case macho::RIT_Generic_TLV: { 1097 printRelocationTargetName(this, RE, fmt); 1098 fmt << "@TLV"; 1099 if (IsPCRel) fmt << "P"; 1100 break; 1101 } 1102 default: 1103 printRelocationTargetName(this, RE, fmt); 1104 } 1105 } else { // ARM-specific relocations 1106 switch (Type) { 1107 case macho::RIT_ARM_Half: // ARM_RELOC_HALF 1108 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF 1109 // Half relocations steal a bit from the length field to encode 1110 // whether this is an upper16 or a lower16 relocation. 1111 bool isUpper = getAnyRelocationLength(RE) >> 1; 1112 1113 if (isUpper) 1114 fmt << ":upper16:("; 1115 else 1116 fmt << ":lower16:("; 1117 printRelocationTargetName(this, RE, fmt); 1118 1119 DataRefImpl RelNext = Rel; 1120 RelNext.d.a++; 1121 macho::RelocationEntry RENext = getRelocation(RelNext); 1122 1123 // ARM half relocs must be followed by a relocation of type 1124 // ARM_RELOC_PAIR. 1125 unsigned RType = getAnyRelocationType(RENext); 1126 if (RType != 1) 1127 report_fatal_error("Expected ARM_RELOC_PAIR after " 1128 "GENERIC_RELOC_HALF"); 1129 1130 // NOTE: The half of the target virtual address is stashed in the 1131 // address field of the secondary relocation, but we can't reverse 1132 // engineer the constant offset from it without decoding the movw/movt 1133 // instruction to find the other half in its immediate field. 1134 1135 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the 1136 // symbol/section pointer of the follow-on relocation. 1137 if (Type == macho::RIT_ARM_HalfDifference) { 1138 fmt << "-"; 1139 printRelocationTargetName(this, RENext, fmt); 1140 } 1141 1142 fmt << ")"; 1143 break; 1144 } 1145 default: { 1146 printRelocationTargetName(this, RE, fmt); 1147 } 1148 } 1149 } 1150 } else 1151 printRelocationTargetName(this, RE, fmt); 1152 1153 fmt.flush(); 1154 Result.append(fmtbuf.begin(), fmtbuf.end()); 1155 return object_error::success; 1156 } 1157 1158 error_code 1159 MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const { 1160 unsigned Arch = getArch(); 1161 uint64_t Type; 1162 getRelocationType(Rel, Type); 1163 1164 Result = false; 1165 1166 // On arches that use the generic relocations, GENERIC_RELOC_PAIR 1167 // is always hidden. 1168 if (Arch == Triple::x86 || Arch == Triple::arm) { 1169 if (Type == macho::RIT_Pair) Result = true; 1170 } else if (Arch == Triple::x86_64) { 1171 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows 1172 // an X864_64_RELOC_SUBTRACTOR. 1173 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) { 1174 DataRefImpl RelPrev = Rel; 1175 RelPrev.d.a--; 1176 uint64_t PrevType; 1177 getRelocationType(RelPrev, PrevType); 1178 if (PrevType == macho::RIT_X86_64_Subtractor) 1179 Result = true; 1180 } 1181 } 1182 1183 return object_error::success; 1184 } 1185 1186 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData, 1187 LibraryRef &Res) const { 1188 report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); 1189 } 1190 1191 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData, 1192 StringRef &Res) const { 1193 report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); 1194 } 1195 1196 symbol_iterator MachOObjectFile::begin_symbols() const { 1197 DataRefImpl DRI; 1198 if (!SymtabLoadCmd) 1199 return symbol_iterator(SymbolRef(DRI, this)); 1200 1201 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand(); 1202 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.SymbolTableOffset)); 1203 return symbol_iterator(SymbolRef(DRI, this)); 1204 } 1205 1206 symbol_iterator MachOObjectFile::end_symbols() const { 1207 DataRefImpl DRI; 1208 if (!SymtabLoadCmd) 1209 return symbol_iterator(SymbolRef(DRI, this)); 1210 1211 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand(); 1212 unsigned SymbolTableEntrySize = is64Bit() ? 1213 sizeof(macho::Symbol64TableEntry) : 1214 sizeof(macho::SymbolTableEntry); 1215 unsigned Offset = Symtab.SymbolTableOffset + 1216 Symtab.NumSymbolTableEntries * SymbolTableEntrySize; 1217 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); 1218 return symbol_iterator(SymbolRef(DRI, this)); 1219 } 1220 1221 symbol_iterator MachOObjectFile::begin_dynamic_symbols() const { 1222 // TODO: implement 1223 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile"); 1224 } 1225 1226 symbol_iterator MachOObjectFile::end_dynamic_symbols() const { 1227 // TODO: implement 1228 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile"); 1229 } 1230 1231 section_iterator MachOObjectFile::begin_sections() const { 1232 DataRefImpl DRI; 1233 return section_iterator(SectionRef(DRI, this)); 1234 } 1235 1236 section_iterator MachOObjectFile::end_sections() const { 1237 DataRefImpl DRI; 1238 DRI.d.a = Sections.size(); 1239 return section_iterator(SectionRef(DRI, this)); 1240 } 1241 1242 library_iterator MachOObjectFile::begin_libraries_needed() const { 1243 // TODO: implement 1244 report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); 1245 } 1246 1247 library_iterator MachOObjectFile::end_libraries_needed() const { 1248 // TODO: implement 1249 report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); 1250 } 1251 1252 uint8_t MachOObjectFile::getBytesInAddress() const { 1253 return is64Bit() ? 8 : 4; 1254 } 1255 1256 StringRef MachOObjectFile::getFileFormatName() const { 1257 unsigned CPUType = getCPUType(this); 1258 if (!is64Bit()) { 1259 switch (CPUType) { 1260 case llvm::MachO::CPUTypeI386: 1261 return "Mach-O 32-bit i386"; 1262 case llvm::MachO::CPUTypeARM: 1263 return "Mach-O arm"; 1264 case llvm::MachO::CPUTypePowerPC: 1265 return "Mach-O 32-bit ppc"; 1266 default: 1267 assert((CPUType & llvm::MachO::CPUArchABI64) == 0 && 1268 "64-bit object file when we're not 64-bit?"); 1269 return "Mach-O 32-bit unknown"; 1270 } 1271 } 1272 1273 // Make sure the cpu type has the correct mask. 1274 assert((CPUType & llvm::MachO::CPUArchABI64) 1275 == llvm::MachO::CPUArchABI64 && 1276 "32-bit object file when we're 64-bit?"); 1277 1278 switch (CPUType) { 1279 case llvm::MachO::CPUTypeX86_64: 1280 return "Mach-O 64-bit x86-64"; 1281 case llvm::MachO::CPUTypePowerPC64: 1282 return "Mach-O 64-bit ppc64"; 1283 default: 1284 return "Mach-O 64-bit unknown"; 1285 } 1286 } 1287 1288 unsigned MachOObjectFile::getArch() const { 1289 switch (getCPUType(this)) { 1290 case llvm::MachO::CPUTypeI386: 1291 return Triple::x86; 1292 case llvm::MachO::CPUTypeX86_64: 1293 return Triple::x86_64; 1294 case llvm::MachO::CPUTypeARM: 1295 return Triple::arm; 1296 case llvm::MachO::CPUTypePowerPC: 1297 return Triple::ppc; 1298 case llvm::MachO::CPUTypePowerPC64: 1299 return Triple::ppc64; 1300 default: 1301 return Triple::UnknownArch; 1302 } 1303 } 1304 1305 StringRef MachOObjectFile::getLoadName() const { 1306 // TODO: Implement 1307 report_fatal_error("get_load_name() unimplemented in MachOObjectFile"); 1308 } 1309 1310 relocation_iterator MachOObjectFile::getSectionRelBegin(unsigned Index) const { 1311 DataRefImpl DRI; 1312 DRI.d.a = Index; 1313 return getSectionRelBegin(DRI); 1314 } 1315 1316 relocation_iterator MachOObjectFile::getSectionRelEnd(unsigned Index) const { 1317 DataRefImpl DRI; 1318 DRI.d.a = Index; 1319 return getSectionRelEnd(DRI); 1320 } 1321 1322 StringRef 1323 MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const { 1324 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec); 1325 return parseSegmentOrSectionName(Raw.data()); 1326 } 1327 1328 ArrayRef<char> 1329 MachOObjectFile::getSectionRawName(DataRefImpl Sec) const { 1330 const SectionBase *Base = 1331 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]); 1332 return ArrayRef<char>(Base->Name); 1333 } 1334 1335 ArrayRef<char> 1336 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const { 1337 const SectionBase *Base = 1338 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]); 1339 return ArrayRef<char>(Base->SegmentName); 1340 } 1341 1342 bool 1343 MachOObjectFile::isRelocationScattered(const macho::RelocationEntry &RE) 1344 const { 1345 if (getCPUType(this) == llvm::MachO::CPUTypeX86_64) 1346 return false; 1347 return getPlainRelocationAddress(RE) & macho::RF_Scattered; 1348 } 1349 1350 unsigned MachOObjectFile::getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const { 1351 if (isLittleEndian()) 1352 return RE.Word1 & 0xffffff; 1353 return RE.Word1 >> 8; 1354 } 1355 1356 bool MachOObjectFile::getPlainRelocationExternal(const macho::RelocationEntry &RE) const { 1357 if (isLittleEndian()) 1358 return (RE.Word1 >> 27) & 1; 1359 return (RE.Word1 >> 4) & 1; 1360 } 1361 1362 bool 1363 MachOObjectFile::getScatteredRelocationScattered(const macho::RelocationEntry &RE) const { 1364 return RE.Word0 >> 31; 1365 } 1366 1367 uint32_t 1368 MachOObjectFile::getScatteredRelocationValue(const macho::RelocationEntry &RE) const { 1369 return RE.Word1; 1370 } 1371 1372 unsigned 1373 MachOObjectFile::getAnyRelocationAddress(const macho::RelocationEntry &RE) const { 1374 if (isRelocationScattered(RE)) 1375 return getScatteredRelocationAddress(RE); 1376 return getPlainRelocationAddress(RE); 1377 } 1378 1379 unsigned 1380 MachOObjectFile::getAnyRelocationPCRel(const macho::RelocationEntry &RE) const { 1381 if (isRelocationScattered(RE)) 1382 return getScatteredRelocationPCRel(this, RE); 1383 return getPlainRelocationPCRel(this, RE); 1384 } 1385 1386 unsigned 1387 MachOObjectFile::getAnyRelocationLength(const macho::RelocationEntry &RE) const { 1388 if (isRelocationScattered(RE)) 1389 return getScatteredRelocationLength(RE); 1390 return getPlainRelocationLength(this, RE); 1391 } 1392 1393 unsigned 1394 MachOObjectFile::getAnyRelocationType(const macho::RelocationEntry &RE) const { 1395 if (isRelocationScattered(RE)) 1396 return getScatteredRelocationType(RE); 1397 return getPlainRelocationType(this, RE); 1398 } 1399 1400 MachOObjectFile::LoadCommandInfo 1401 MachOObjectFile::getFirstLoadCommandInfo() const { 1402 MachOObjectFile::LoadCommandInfo Load; 1403 1404 unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size; 1405 Load.Ptr = getPtr(this, HeaderSize); 1406 Load.C = getStruct<macho::LoadCommand>(this, Load.Ptr); 1407 return Load; 1408 } 1409 1410 MachOObjectFile::LoadCommandInfo 1411 MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const { 1412 MachOObjectFile::LoadCommandInfo Next; 1413 Next.Ptr = L.Ptr + L.C.Size; 1414 Next.C = getStruct<macho::LoadCommand>(this, Next.Ptr); 1415 return Next; 1416 } 1417 1418 macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const { 1419 return getStruct<macho::Section>(this, Sections[DRI.d.a]); 1420 } 1421 1422 macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const { 1423 return getStruct<macho::Section64>(this, Sections[DRI.d.a]); 1424 } 1425 1426 macho::Section MachOObjectFile::getSection(const LoadCommandInfo &L, 1427 unsigned Index) const { 1428 const char *Sec = getSectionPtr(this, L, Index); 1429 return getStruct<macho::Section>(this, Sec); 1430 } 1431 1432 macho::Section64 MachOObjectFile::getSection64(const LoadCommandInfo &L, 1433 unsigned Index) const { 1434 const char *Sec = getSectionPtr(this, L, Index); 1435 return getStruct<macho::Section64>(this, Sec); 1436 } 1437 1438 macho::SymbolTableEntry 1439 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { 1440 const char *P = reinterpret_cast<const char *>(DRI.p); 1441 return getStruct<macho::SymbolTableEntry>(this, P); 1442 } 1443 1444 macho::Symbol64TableEntry 1445 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const { 1446 const char *P = reinterpret_cast<const char *>(DRI.p); 1447 return getStruct<macho::Symbol64TableEntry>(this, P); 1448 } 1449 1450 macho::LinkeditDataLoadCommand 1451 MachOObjectFile::getLinkeditDataLoadCommand(const MachOObjectFile::LoadCommandInfo &L) const { 1452 return getStruct<macho::LinkeditDataLoadCommand>(this, L.Ptr); 1453 } 1454 1455 macho::SegmentLoadCommand 1456 MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const { 1457 return getStruct<macho::SegmentLoadCommand>(this, L.Ptr); 1458 } 1459 1460 macho::Segment64LoadCommand 1461 MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const { 1462 return getStruct<macho::Segment64LoadCommand>(this, L.Ptr); 1463 } 1464 1465 macho::LinkerOptionsLoadCommand 1466 MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const { 1467 return getStruct<macho::LinkerOptionsLoadCommand>(this, L.Ptr); 1468 } 1469 1470 macho::RelocationEntry 1471 MachOObjectFile::getRelocation(DataRefImpl Rel) const { 1472 const char *P = reinterpret_cast<const char *>(Rel.p); 1473 return getStruct<macho::RelocationEntry>(this, P); 1474 } 1475 1476 macho::Header MachOObjectFile::getHeader() const { 1477 return getStruct<macho::Header>(this, getPtr(this, 0)); 1478 } 1479 1480 macho::Header64Ext MachOObjectFile::getHeader64Ext() const { 1481 return 1482 getStruct<macho::Header64Ext>(this, getPtr(this, sizeof(macho::Header))); 1483 } 1484 1485 macho::IndirectSymbolTableEntry MachOObjectFile::getIndirectSymbolTableEntry( 1486 const macho::DysymtabLoadCommand &DLC, 1487 unsigned Index) const { 1488 uint64_t Offset = DLC.IndirectSymbolTableOffset + 1489 Index * sizeof(macho::IndirectSymbolTableEntry); 1490 return getStruct<macho::IndirectSymbolTableEntry>(this, getPtr(this, Offset)); 1491 } 1492 1493 macho::DataInCodeTableEntry 1494 MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset, 1495 unsigned Index) const { 1496 uint64_t Offset = DataOffset + Index * sizeof(macho::DataInCodeTableEntry); 1497 return getStruct<macho::DataInCodeTableEntry>(this, getPtr(this, Offset)); 1498 } 1499 1500 macho::SymtabLoadCommand MachOObjectFile::getSymtabLoadCommand() const { 1501 return getStruct<macho::SymtabLoadCommand>(this, SymtabLoadCmd); 1502 } 1503 1504 macho::DysymtabLoadCommand MachOObjectFile::getDysymtabLoadCommand() const { 1505 return getStruct<macho::DysymtabLoadCommand>(this, DysymtabLoadCmd); 1506 } 1507 1508 StringRef MachOObjectFile::getStringTableData() const { 1509 macho::SymtabLoadCommand S = getSymtabLoadCommand(); 1510 return getData().substr(S.StringTableOffset, S.StringTableSize); 1511 } 1512 1513 bool MachOObjectFile::is64Bit() const { 1514 return getType() == getMachOType(false, true) || 1515 getType() == getMachOType(true, true); 1516 } 1517 1518 void MachOObjectFile::ReadULEB128s(uint64_t Index, 1519 SmallVectorImpl<uint64_t> &Out) const { 1520 DataExtractor extractor(ObjectFile::getData(), true, 0); 1521 1522 uint32_t offset = Index; 1523 uint64_t data = 0; 1524 while (uint64_t delta = extractor.getULEB128(&offset)) { 1525 data += delta; 1526 Out.push_back(data); 1527 } 1528 } 1529 1530 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) { 1531 StringRef Magic = Buffer->getBuffer().slice(0, 4); 1532 error_code ec; 1533 ObjectFile *Ret; 1534 if (Magic == "\xFE\xED\xFA\xCE") 1535 Ret = new MachOObjectFile(Buffer, false, false, ec); 1536 else if (Magic == "\xCE\xFA\xED\xFE") 1537 Ret = new MachOObjectFile(Buffer, true, false, ec); 1538 else if (Magic == "\xFE\xED\xFA\xCF") 1539 Ret = new MachOObjectFile(Buffer, false, true, ec); 1540 else if (Magic == "\xCF\xFA\xED\xFE") 1541 Ret = new MachOObjectFile(Buffer, true, true, ec); 1542 else 1543 return NULL; 1544 1545 if (ec) 1546 return NULL; 1547 return Ret; 1548 } 1549 1550 } // end namespace object 1551 } // end namespace llvm 1552