1 //===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MachOObjectFile class, which binds the MachOObject
11 // class to the generic ObjectFile wrapper.
13 //===----------------------------------------------------------------------===//
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"
27 using namespace object;
32 struct SymbolTableEntryBase {
45 static void SwapValue(T &Value) {
46 Value = sys::SwapByteOrder(Value);
50 static void SwapStruct(T &Value);
53 void SwapStruct(macho::RelocationEntry &H) {
59 void SwapStruct(macho::LoadCommand &L) {
65 void SwapStruct(SymbolTableEntryBase &S) {
66 SwapValue(S.StringIndex);
71 void SwapStruct(macho::Section &S) {
76 SwapValue(S.RelocationTableOffset);
77 SwapValue(S.NumRelocationTableEntries);
79 SwapValue(S.Reserved1);
80 SwapValue(S.Reserved2);
84 void SwapStruct(macho::Section64 &S) {
89 SwapValue(S.RelocationTableOffset);
90 SwapValue(S.NumRelocationTableEntries);
92 SwapValue(S.Reserved1);
93 SwapValue(S.Reserved2);
94 SwapValue(S.Reserved3);
98 void SwapStruct(macho::SymbolTableEntry &S) {
99 SwapValue(S.StringIndex);
105 void SwapStruct(macho::Symbol64TableEntry &S) {
106 SwapValue(S.StringIndex);
112 void SwapStruct(macho::Header &H) {
114 SwapValue(H.CPUType);
115 SwapValue(H.CPUSubtype);
116 SwapValue(H.FileType);
117 SwapValue(H.NumLoadCommands);
118 SwapValue(H.SizeOfLoadCommands);
123 void SwapStruct(macho::SymtabLoadCommand &C) {
126 SwapValue(C.SymbolTableOffset);
127 SwapValue(C.NumSymbolTableEntries);
128 SwapValue(C.StringTableOffset);
129 SwapValue(C.StringTableSize);
133 void SwapStruct(macho::LinkeditDataLoadCommand &C) {
136 SwapValue(C.DataOffset);
137 SwapValue(C.DataSize);
141 void SwapStruct(macho::SegmentLoadCommand &C) {
144 SwapValue(C.VMAddress);
146 SwapValue(C.FileOffset);
147 SwapValue(C.FileSize);
148 SwapValue(C.MaxVMProtection);
149 SwapValue(C.InitialVMProtection);
150 SwapValue(C.NumSections);
155 void SwapStruct(macho::Segment64LoadCommand &C) {
158 SwapValue(C.VMAddress);
160 SwapValue(C.FileOffset);
161 SwapValue(C.FileSize);
162 SwapValue(C.MaxVMProtection);
163 SwapValue(C.InitialVMProtection);
164 SwapValue(C.NumSections);
169 T getStruct(const MachOObjectFile *O, const char *P) {
171 memcpy(&Cmd, P, sizeof(T));
172 if (O->isLittleEndian() != sys::IsLittleEndianHost)
177 static macho::SegmentLoadCommand
178 getSegmentLoadCommand(const MachOObjectFile *O,
179 const MachOObjectFile::LoadCommandInfo &L) {
180 return getStruct<macho::SegmentLoadCommand>(O, L.Ptr);
183 static macho::Segment64LoadCommand
184 getSegment64LoadCommand(const MachOObjectFile *O,
185 const MachOObjectFile::LoadCommandInfo &L) {
186 return getStruct<macho::Segment64LoadCommand>(O, L.Ptr);
190 getSegmentLoadCommandNumSections(const MachOObjectFile *O,
191 const MachOObjectFile::LoadCommandInfo &L) {
193 macho::Segment64LoadCommand S = getSegment64LoadCommand(O, L);
194 return S.NumSections;
196 macho::SegmentLoadCommand S = getSegmentLoadCommand(O, L);
197 return S.NumSections;
200 static const SectionBase *
201 getSectionBase(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
203 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
205 bool Is64 = O->is64Bit();
206 unsigned SegmentLoadSize = Is64 ? sizeof(macho::Segment64LoadCommand) :
207 sizeof(macho::SegmentLoadCommand);
208 unsigned SectionSize = Is64 ? sizeof(macho::Section64) :
209 sizeof(macho::Section);
211 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
212 return reinterpret_cast<const SectionBase*>(SectionAddr);
215 static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
216 return O->getData().substr(Offset, 1).data();
219 static const char *getSymbolTableEntryPtr(const MachOObjectFile *O,
221 macho::SymtabLoadCommand S = O->getSymtabLoadCommand();
223 unsigned Index = DRI.d.b;
225 unsigned SymbolTableEntrySize = O->is64Bit() ?
226 sizeof(macho::Symbol64TableEntry) :
227 sizeof(macho::SymbolTableEntry);
229 uint64_t Offset = S.SymbolTableOffset + Index * SymbolTableEntrySize;
230 return getPtr(O, Offset);
233 static SymbolTableEntryBase
234 getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
235 const char *P = getSymbolTableEntryPtr(O, DRI);
236 return getStruct<SymbolTableEntryBase>(O, P);
239 static StringRef parseSegmentOrSectionName(const char *P) {
243 // Not null terminated, so this is a 16 char string.
244 return StringRef(P, 16);
247 // Helper to advance a section or symbol iterator multiple increments at a time.
249 static error_code advance(T &it, size_t Val) {
258 static void advanceTo(T &it, size_t Val) {
259 if (error_code ec = advance(it, Val))
260 report_fatal_error(ec.message());
263 static unsigned getCPUType(const MachOObjectFile *O) {
264 return O->getHeader().CPUType;
267 static void printRelocationTargetName(const MachOObjectFile *O,
268 const macho::RelocationEntry &RE,
269 raw_string_ostream &fmt) {
270 bool IsScattered = O->isRelocationScattered(RE);
272 // Target of a scattered relocation is an address. In the interest of
273 // generating pretty output, scan through the symbol table looking for a
274 // symbol that aligns with that address. If we find one, print it.
275 // Otherwise, we just print the hex address of the target.
277 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
280 for (symbol_iterator SI = O->begin_symbols(), SE = O->end_symbols();
281 SI != SE; SI.increment(ec)) {
282 if (ec) report_fatal_error(ec.message());
287 if ((ec = SI->getAddress(Addr)))
288 report_fatal_error(ec.message());
289 if (Addr != Val) continue;
290 if ((ec = SI->getName(Name)))
291 report_fatal_error(ec.message());
296 // If we couldn't find a symbol that this relocation refers to, try
297 // to find a section beginning instead.
298 for (section_iterator SI = O->begin_sections(), SE = O->end_sections();
299 SI != SE; SI.increment(ec)) {
300 if (ec) report_fatal_error(ec.message());
305 if ((ec = SI->getAddress(Addr)))
306 report_fatal_error(ec.message());
307 if (Addr != Val) continue;
308 if ((ec = SI->getName(Name)))
309 report_fatal_error(ec.message());
314 fmt << format("0x%x", Val);
319 bool isExtern = O->getPlainRelocationExternal(RE);
320 uint64_t Val = O->getAnyRelocationAddress(RE);
323 symbol_iterator SI = O->begin_symbols();
327 section_iterator SI = O->begin_sections();
335 static uint32_t getPlainRelocationAddress(const macho::RelocationEntry &RE) {
340 getScatteredRelocationAddress(const macho::RelocationEntry &RE) {
341 return RE.Word0 & 0xffffff;
344 static bool getPlainRelocationPCRel(const MachOObjectFile *O,
345 const macho::RelocationEntry &RE) {
346 if (O->isLittleEndian())
347 return (RE.Word1 >> 24) & 1;
348 return (RE.Word1 >> 7) & 1;
352 getScatteredRelocationPCRel(const MachOObjectFile *O,
353 const macho::RelocationEntry &RE) {
354 return (RE.Word0 >> 30) & 1;
357 static unsigned getPlainRelocationLength(const MachOObjectFile *O,
358 const macho::RelocationEntry &RE) {
359 if (O->isLittleEndian())
360 return (RE.Word1 >> 25) & 3;
361 return (RE.Word1 >> 5) & 3;
365 getScatteredRelocationLength(const macho::RelocationEntry &RE) {
366 return (RE.Word0 >> 28) & 3;
369 static unsigned getPlainRelocationType(const MachOObjectFile *O,
370 const macho::RelocationEntry &RE) {
371 if (O->isLittleEndian())
372 return RE.Word1 >> 28;
373 return RE.Word1 & 0xf;
376 static unsigned getScatteredRelocationType(const macho::RelocationEntry &RE) {
377 return (RE.Word0 >> 24) & 0xf;
380 static uint32_t getSectionFlags(const MachOObjectFile *O,
383 macho::Section64 Sect = O->getSection64(Sec);
386 macho::Section Sect = O->getSection(Sec);
390 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object,
391 bool IsLittleEndian, bool Is64bits,
393 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
394 SymtabLoadCmd(NULL) {
395 uint32_t LoadCommandCount = this->getHeader().NumLoadCommands;
396 macho::LoadCommandType SegmentLoadType = is64Bit() ?
397 macho::LCT_Segment64 : macho::LCT_Segment;
399 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
400 for (unsigned I = 0; ; ++I) {
401 if (Load.C.Type == macho::LCT_Symtab) {
402 assert(!SymtabLoadCmd && "Multiple symbol tables");
403 SymtabLoadCmd = Load.Ptr;
406 if (Load.C.Type == SegmentLoadType) {
407 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
408 for (unsigned J = 0; J < NumSections; ++J) {
409 const SectionBase *Sec = getSectionBase(this, Load, J);
410 Sections.push_back(reinterpret_cast<const char*>(Sec));
414 if (I == LoadCommandCount - 1)
417 Load = getNextLoadCommandInfo(Load);
421 error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb,
422 SymbolRef &Res) const {
424 Res = SymbolRef(Symb, this);
425 return object_error::success;
428 error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
429 StringRef &Res) const {
430 macho::SymtabLoadCommand S = getSymtabLoadCommand();
431 const char *StringTable = getPtr(this, S.StringTableOffset);
432 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
433 const char *Start = &StringTable[Entry.StringIndex];
434 Res = StringRef(Start);
435 return object_error::success;
438 error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
439 uint64_t &Res) const {
441 macho::Symbol64TableEntry Entry = getSymbol64TableEntry(Symb);
444 macho::SymbolTableEntry Entry = getSymbolTableEntry(Symb);
447 return object_error::success;
451 MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb,
452 uint64_t &Res) const {
453 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
454 getSymbolAddress(Symb, Res);
455 if (Entry.SectionIndex) {
458 SecRel.d.a = Entry.SectionIndex-1;
460 macho::Section64 Sec = getSection64(SecRel);
461 Delta = Sec.Offset - Sec.Address;
463 macho::Section Sec = getSection(SecRel);
464 Delta = Sec.Offset - Sec.Address;
470 return object_error::success;
473 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
474 uint64_t &Result) const {
475 uint64_t BeginOffset;
476 uint64_t EndOffset = 0;
477 uint8_t SectionIndex;
479 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
481 getSymbolAddress(DRI, Value);
485 SectionIndex = Entry.SectionIndex;
487 uint32_t flags = SymbolRef::SF_None;
488 this->getSymbolFlags(DRI, flags);
489 if (flags & SymbolRef::SF_Common)
492 Result = UnknownAddressOrSize;
493 return object_error::success;
495 // Unfortunately symbols are unsorted so we need to touch all
496 // symbols from load command
497 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
499 while (DRI.d.b <= Symtab.NumSymbolTableEntries) {
500 Entry = getSymbolTableEntryBase(this, DRI);
501 getSymbolAddress(DRI, Value);
502 if (Entry.SectionIndex == SectionIndex && Value > BeginOffset)
503 if (!EndOffset || Value < EndOffset)
510 Sec.d.a = SectionIndex-1;
511 getSectionSize(Sec, Size);
512 getSectionAddress(Sec, EndOffset);
515 Result = EndOffset - BeginOffset;
516 return object_error::success;
519 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
520 SymbolRef::Type &Res) const {
521 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
522 uint8_t n_type = Entry.Type;
524 Res = SymbolRef::ST_Other;
526 // If this is a STAB debugging symbol, we can do nothing more.
527 if (n_type & MachO::NlistMaskStab) {
528 Res = SymbolRef::ST_Debug;
529 return object_error::success;
532 switch (n_type & MachO::NlistMaskType) {
533 case MachO::NListTypeUndefined :
534 Res = SymbolRef::ST_Unknown;
536 case MachO::NListTypeSection :
537 Res = SymbolRef::ST_Function;
540 return object_error::success;
543 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
545 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
546 uint8_t Type = Entry.Type;
547 uint16_t Flags = Entry.Flags;
550 switch (Type & macho::STF_TypeMask) {
551 case macho::STT_Undefined:
554 case macho::STT_Absolute:
555 case macho::STT_Section:
563 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
564 Char = toupper(static_cast<unsigned char>(Char));
566 return object_error::success;
569 error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
570 uint32_t &Result) const {
571 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
573 uint8_t MachOType = Entry.Type;
574 uint16_t MachOFlags = Entry.Flags;
576 // TODO: Correctly set SF_ThreadLocal
577 Result = SymbolRef::SF_None;
579 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
580 Result |= SymbolRef::SF_Undefined;
582 if (MachOFlags & macho::STF_StabsEntryMask)
583 Result |= SymbolRef::SF_FormatSpecific;
585 if (MachOType & MachO::NlistMaskExternal) {
586 Result |= SymbolRef::SF_Global;
587 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
588 Result |= SymbolRef::SF_Common;
591 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
592 Result |= SymbolRef::SF_Weak;
594 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
595 Result |= SymbolRef::SF_Absolute;
597 return object_error::success;
601 MachOObjectFile::getSymbolSection(DataRefImpl Symb,
602 section_iterator &Res) const {
603 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
604 uint8_t index = Entry.SectionIndex;
607 Res = end_sections();
611 Res = section_iterator(SectionRef(DRI, this));
614 return object_error::success;
617 error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
618 uint64_t &Val) const {
619 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
622 error_code MachOObjectFile::getSectionNext(DataRefImpl Sec,
623 SectionRef &Res) const {
625 Res = SectionRef(Sec, this);
626 return object_error::success;
630 MachOObjectFile::getSectionName(DataRefImpl Sec,
631 StringRef &Result) const {
632 ArrayRef<char> Raw = getSectionRawName(Sec);
633 Result = parseSegmentOrSectionName(Raw.data());
634 return object_error::success;
638 MachOObjectFile::getSectionAddress(DataRefImpl Sec,
639 uint64_t &Res) const {
641 macho::Section64 Sect = getSection64(Sec);
644 macho::Section Sect = getSection(Sec);
647 return object_error::success;
651 MachOObjectFile::getSectionSize(DataRefImpl Sec,
652 uint64_t &Res) const {
654 macho::Section64 Sect = getSection64(Sec);
657 macho::Section Sect = getSection(Sec);
661 return object_error::success;
665 MachOObjectFile::getSectionContents(DataRefImpl Sec,
666 StringRef &Res) const {
671 macho::Section64 Sect = getSection64(Sec);
672 Offset = Sect.Offset;
675 macho::Section Sect =getSection(Sec);
676 Offset = Sect.Offset;
680 Res = this->getData().substr(Offset, Size);
681 return object_error::success;
685 MachOObjectFile::getSectionAlignment(DataRefImpl Sec,
686 uint64_t &Res) const {
689 macho::Section64 Sect = getSection64(Sec);
692 macho::Section Sect = getSection(Sec);
696 Res = uint64_t(1) << Align;
697 return object_error::success;
701 MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
702 uint32_t Flags = getSectionFlags(this, Sec);
703 Res = Flags & macho::SF_PureInstructions;
704 return object_error::success;
707 error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
708 bool &Result) const {
709 // FIXME: Unimplemented.
711 return object_error::success;
714 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
715 bool &Result) const {
716 // FIXME: Unimplemented.
718 return object_error::success;
722 MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
723 bool &Result) const {
724 // FIXME: Unimplemented.
726 return object_error::success;
729 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
730 bool &Result) const {
731 // FIXME: Unimplemented.
733 return object_error::success;
737 MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
738 uint32_t Flags = getSectionFlags(this, Sec);
739 unsigned SectionType = Flags & MachO::SectionFlagMaskSectionType;
740 Res = SectionType == MachO::SectionTypeZeroFill ||
741 SectionType == MachO::SectionTypeZeroFillLarge;
742 return object_error::success;
745 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
746 bool &Result) const {
747 // Consider using the code from isSectionText to look for __const sections.
748 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
749 // to use section attributes to distinguish code from data.
751 // FIXME: Unimplemented.
753 return object_error::success;
757 MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
759 bool &Result) const {
761 this->getSymbolType(Symb, ST);
762 if (ST == SymbolRef::ST_Unknown) {
764 return object_error::success;
767 uint64_t SectBegin, SectEnd;
768 getSectionAddress(Sec, SectBegin);
769 getSectionSize(Sec, SectEnd);
770 SectEnd += SectBegin;
773 getSymbolAddress(Symb, SymAddr);
774 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
776 return object_error::success;
779 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
782 return relocation_iterator(RelocationRef(ret, this));
786 MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
789 macho::Section64 Sect = getSection64(Sec);
790 LastReloc = Sect.NumRelocationTableEntries;
792 macho::Section Sect = getSection(Sec);
793 LastReloc = Sect.NumRelocationTableEntries;
799 return relocation_iterator(RelocationRef(Ret, this));
802 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
803 RelocationRef &Res) const {
805 Res = RelocationRef(Rel, this);
806 return object_error::success;
810 MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
811 uint64_t &Res) const {
812 uint64_t SectAddress;
816 macho::Section64 Sect = getSection64(Sec);
817 SectAddress = Sect.Address;
819 macho::Section Sect = getSection(Sec);
820 SectAddress = Sect.Address;
823 macho::RelocationEntry RE = getRelocation(Rel);
824 uint64_t RelAddr = getAnyRelocationAddress(RE);
825 Res = SectAddress + RelAddr;
826 return object_error::success;
829 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
830 uint64_t &Res) const {
831 macho::RelocationEntry RE = getRelocation(Rel);
832 Res = getAnyRelocationAddress(RE);
833 return object_error::success;
837 MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
838 SymbolRef &Res) const {
839 macho::RelocationEntry RE = getRelocation(Rel);
840 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
841 bool isExtern = getPlainRelocationExternal(RE);
847 Res = SymbolRef(Sym, this);
848 return object_error::success;
851 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
852 uint64_t &Res) const {
853 macho::RelocationEntry RE = getRelocation(Rel);
854 Res = getAnyRelocationType(RE);
855 return object_error::success;
859 MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
860 SmallVectorImpl<char> &Result) const {
863 getRelocationType(Rel, RType);
865 unsigned Arch = this->getArch();
869 static const char *const Table[] = {
870 "GENERIC_RELOC_VANILLA",
871 "GENERIC_RELOC_PAIR",
872 "GENERIC_RELOC_SECTDIFF",
873 "GENERIC_RELOC_PB_LA_PTR",
874 "GENERIC_RELOC_LOCAL_SECTDIFF",
875 "GENERIC_RELOC_TLV" };
883 case Triple::x86_64: {
884 static const char *const Table[] = {
885 "X86_64_RELOC_UNSIGNED",
886 "X86_64_RELOC_SIGNED",
887 "X86_64_RELOC_BRANCH",
888 "X86_64_RELOC_GOT_LOAD",
890 "X86_64_RELOC_SUBTRACTOR",
891 "X86_64_RELOC_SIGNED_1",
892 "X86_64_RELOC_SIGNED_2",
893 "X86_64_RELOC_SIGNED_4",
894 "X86_64_RELOC_TLV" };
903 static const char *const Table[] = {
906 "ARM_RELOC_SECTDIFF",
907 "ARM_RELOC_LOCAL_SECTDIFF",
908 "ARM_RELOC_PB_LA_PTR",
910 "ARM_THUMB_RELOC_BR22",
911 "ARM_THUMB_32BIT_BRANCH",
913 "ARM_RELOC_HALF_SECTDIFF" };
922 static const char *const Table[] = {
931 "PPC_RELOC_SECTDIFF",
932 "PPC_RELOC_PB_LA_PTR",
933 "PPC_RELOC_HI16_SECTDIFF",
934 "PPC_RELOC_LO16_SECTDIFF",
935 "PPC_RELOC_HA16_SECTDIFF",
937 "PPC_RELOC_LO14_SECTDIFF",
938 "PPC_RELOC_LOCAL_SECTDIFF" };
943 case Triple::UnknownArch:
947 Result.append(res.begin(), res.end());
948 return object_error::success;
951 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
952 int64_t &Res) const {
954 return object_error::success;
958 MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
959 SmallVectorImpl<char> &Result) const {
960 macho::RelocationEntry RE = getRelocation(Rel);
962 unsigned Arch = this->getArch();
965 raw_string_ostream fmt(fmtbuf);
966 unsigned Type = this->getAnyRelocationType(RE);
967 bool IsPCRel = this->getAnyRelocationPCRel(RE);
969 // Determine any addends that should be displayed with the relocation.
970 // These require decoding the relocation type, which is triple-specific.
972 // X86_64 has entirely custom relocation types.
973 if (Arch == Triple::x86_64) {
974 bool isPCRel = getAnyRelocationPCRel(RE);
977 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
978 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
979 printRelocationTargetName(this, RE, fmt);
981 if (isPCRel) fmt << "PCREL";
984 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
985 DataRefImpl RelNext = Rel;
987 macho::RelocationEntry RENext = getRelocation(RelNext);
989 // X86_64_SUBTRACTOR must be followed by a relocation of type
990 // X86_64_RELOC_UNSIGNED.
991 // NOTE: Scattered relocations don't exist on x86_64.
992 unsigned RType = getAnyRelocationType(RENext);
994 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
995 "X86_64_RELOC_SUBTRACTOR.");
997 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
998 // X86_64_SUBTRACTOR contains to the subtrahend.
999 printRelocationTargetName(this, RENext, fmt);
1001 printRelocationTargetName(this, RE, fmt);
1004 case macho::RIT_X86_64_TLV:
1005 printRelocationTargetName(this, RE, fmt);
1007 if (isPCRel) fmt << "P";
1009 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1010 printRelocationTargetName(this, RE, fmt);
1013 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1014 printRelocationTargetName(this, RE, fmt);
1017 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1018 printRelocationTargetName(this, RE, fmt);
1022 printRelocationTargetName(this, RE, fmt);
1025 // X86 and ARM share some relocation types in common.
1026 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1027 // Generic relocation types...
1029 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1030 return object_error::success;
1031 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1032 DataRefImpl RelNext = Rel;
1034 macho::RelocationEntry RENext = getRelocation(RelNext);
1036 // X86 sect diff's must be followed by a relocation of type
1037 // GENERIC_RELOC_PAIR.
1038 unsigned RType = getAnyRelocationType(RENext);
1041 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1042 "GENERIC_RELOC_SECTDIFF.");
1044 printRelocationTargetName(this, RE, fmt);
1046 printRelocationTargetName(this, RENext, fmt);
1051 if (Arch == Triple::x86) {
1052 // All X86 relocations that need special printing were already
1053 // handled in the generic code.
1055 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1056 DataRefImpl RelNext = Rel;
1058 macho::RelocationEntry RENext = getRelocation(RelNext);
1060 // X86 sect diff's must be followed by a relocation of type
1061 // GENERIC_RELOC_PAIR.
1062 unsigned RType = getAnyRelocationType(RENext);
1064 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1065 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1067 printRelocationTargetName(this, RE, fmt);
1069 printRelocationTargetName(this, RENext, fmt);
1072 case macho::RIT_Generic_TLV: {
1073 printRelocationTargetName(this, RE, fmt);
1075 if (IsPCRel) fmt << "P";
1079 printRelocationTargetName(this, RE, fmt);
1081 } else { // ARM-specific relocations
1083 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1084 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1085 // Half relocations steal a bit from the length field to encode
1086 // whether this is an upper16 or a lower16 relocation.
1087 bool isUpper = getAnyRelocationLength(RE) >> 1;
1090 fmt << ":upper16:(";
1092 fmt << ":lower16:(";
1093 printRelocationTargetName(this, RE, fmt);
1095 DataRefImpl RelNext = Rel;
1097 macho::RelocationEntry RENext = getRelocation(RelNext);
1099 // ARM half relocs must be followed by a relocation of type
1101 unsigned RType = getAnyRelocationType(RENext);
1103 report_fatal_error("Expected ARM_RELOC_PAIR after "
1104 "GENERIC_RELOC_HALF");
1106 // NOTE: The half of the target virtual address is stashed in the
1107 // address field of the secondary relocation, but we can't reverse
1108 // engineer the constant offset from it without decoding the movw/movt
1109 // instruction to find the other half in its immediate field.
1111 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1112 // symbol/section pointer of the follow-on relocation.
1113 if (Type == macho::RIT_ARM_HalfDifference) {
1115 printRelocationTargetName(this, RENext, fmt);
1122 printRelocationTargetName(this, RE, fmt);
1127 printRelocationTargetName(this, RE, fmt);
1130 Result.append(fmtbuf.begin(), fmtbuf.end());
1131 return object_error::success;
1135 MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1136 bool &Result) const {
1137 unsigned Arch = getArch();
1139 getRelocationType(Rel, Type);
1143 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1144 // is always hidden.
1145 if (Arch == Triple::x86 || Arch == Triple::arm) {
1146 if (Type == macho::RIT_Pair) Result = true;
1147 } else if (Arch == Triple::x86_64) {
1148 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1149 // an X864_64_RELOC_SUBTRACTOR.
1150 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1151 DataRefImpl RelPrev = Rel;
1154 getRelocationType(RelPrev, PrevType);
1155 if (PrevType == macho::RIT_X86_64_Subtractor)
1160 return object_error::success;
1163 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1164 LibraryRef &Res) const {
1165 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1168 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1169 StringRef &Res) const {
1170 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1173 symbol_iterator MachOObjectFile::begin_symbols() const {
1174 // DRI.d.a = segment number; DRI.d.b = symbol index.
1176 return symbol_iterator(SymbolRef(DRI, this));
1179 symbol_iterator MachOObjectFile::end_symbols() const {
1181 if (SymtabLoadCmd) {
1182 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1183 DRI.d.b = Symtab.NumSymbolTableEntries;
1185 return symbol_iterator(SymbolRef(DRI, this));
1188 symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
1190 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1193 symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
1195 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1198 section_iterator MachOObjectFile::begin_sections() const {
1200 return section_iterator(SectionRef(DRI, this));
1203 section_iterator MachOObjectFile::end_sections() const {
1205 DRI.d.a = Sections.size();
1206 return section_iterator(SectionRef(DRI, this));
1209 library_iterator MachOObjectFile::begin_libraries_needed() const {
1211 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1214 library_iterator MachOObjectFile::end_libraries_needed() const {
1216 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1219 uint8_t MachOObjectFile::getBytesInAddress() const {
1220 return is64Bit() ? 8 : 4;
1223 StringRef MachOObjectFile::getFileFormatName() const {
1224 unsigned CPUType = getCPUType(this);
1227 case llvm::MachO::CPUTypeI386:
1228 return "Mach-O 32-bit i386";
1229 case llvm::MachO::CPUTypeARM:
1230 return "Mach-O arm";
1231 case llvm::MachO::CPUTypePowerPC:
1232 return "Mach-O 32-bit ppc";
1234 assert((CPUType & llvm::MachO::CPUArchABI64) == 0 &&
1235 "64-bit object file when we're not 64-bit?");
1236 return "Mach-O 32-bit unknown";
1240 // Make sure the cpu type has the correct mask.
1241 assert((CPUType & llvm::MachO::CPUArchABI64)
1242 == llvm::MachO::CPUArchABI64 &&
1243 "32-bit object file when we're 64-bit?");
1246 case llvm::MachO::CPUTypeX86_64:
1247 return "Mach-O 64-bit x86-64";
1248 case llvm::MachO::CPUTypePowerPC64:
1249 return "Mach-O 64-bit ppc64";
1251 return "Mach-O 64-bit unknown";
1255 unsigned MachOObjectFile::getArch() const {
1256 switch (getCPUType(this)) {
1257 case llvm::MachO::CPUTypeI386:
1259 case llvm::MachO::CPUTypeX86_64:
1260 return Triple::x86_64;
1261 case llvm::MachO::CPUTypeARM:
1263 case llvm::MachO::CPUTypePowerPC:
1265 case llvm::MachO::CPUTypePowerPC64:
1266 return Triple::ppc64;
1268 return Triple::UnknownArch;
1272 StringRef MachOObjectFile::getLoadName() const {
1274 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
1278 MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1279 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1280 return parseSegmentOrSectionName(Raw.data());
1284 MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
1285 const SectionBase *Base =
1286 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]);
1287 return ArrayRef<char>(Base->Name);
1291 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
1292 const SectionBase *Base =
1293 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]);
1294 return ArrayRef<char>(Base->SegmentName);
1298 MachOObjectFile::isRelocationScattered(const macho::RelocationEntry &RE)
1300 if (getCPUType(this) == llvm::MachO::CPUTypeX86_64)
1302 return getPlainRelocationAddress(RE) & macho::RF_Scattered;
1305 unsigned MachOObjectFile::getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const {
1306 if (isLittleEndian())
1307 return RE.Word1 & 0xffffff;
1308 return RE.Word1 >> 8;
1311 bool MachOObjectFile::getPlainRelocationExternal(const macho::RelocationEntry &RE) const {
1312 if (isLittleEndian())
1313 return (RE.Word1 >> 27) & 1;
1314 return (RE.Word1 >> 4) & 1;
1318 MachOObjectFile::getScatteredRelocationScattered(const macho::RelocationEntry &RE) const {
1319 return RE.Word0 >> 31;
1323 MachOObjectFile::getScatteredRelocationValue(const macho::RelocationEntry &RE) const {
1328 MachOObjectFile::getAnyRelocationAddress(const macho::RelocationEntry &RE) const {
1329 if (isRelocationScattered(RE))
1330 return getScatteredRelocationAddress(RE);
1331 return getPlainRelocationAddress(RE);
1335 MachOObjectFile::getAnyRelocationPCRel(const macho::RelocationEntry &RE) const {
1336 if (isRelocationScattered(RE))
1337 return getScatteredRelocationPCRel(this, RE);
1338 return getPlainRelocationPCRel(this, RE);
1342 MachOObjectFile::getAnyRelocationLength(const macho::RelocationEntry &RE) const {
1343 if (isRelocationScattered(RE))
1344 return getScatteredRelocationLength(RE);
1345 return getPlainRelocationLength(this, RE);
1349 MachOObjectFile::getAnyRelocationType(const macho::RelocationEntry &RE) const {
1350 if (isRelocationScattered(RE))
1351 return getScatteredRelocationType(RE);
1352 return getPlainRelocationType(this, RE);
1355 MachOObjectFile::LoadCommandInfo
1356 MachOObjectFile::getFirstLoadCommandInfo() const {
1357 MachOObjectFile::LoadCommandInfo Load;
1359 unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size;
1360 Load.Ptr = getPtr(this, HeaderSize);
1361 Load.C = getStruct<macho::LoadCommand>(this, Load.Ptr);
1365 MachOObjectFile::LoadCommandInfo
1366 MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1367 MachOObjectFile::LoadCommandInfo Next;
1368 Next.Ptr = L.Ptr + L.C.Size;
1369 Next.C = getStruct<macho::LoadCommand>(this, Next.Ptr);
1373 macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const {
1374 return getStruct<macho::Section>(this, Sections[DRI.d.a]);
1377 macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1378 return getStruct<macho::Section64>(this, Sections[DRI.d.a]);
1381 macho::SymbolTableEntry
1382 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
1383 const char *P = getSymbolTableEntryPtr(this, DRI);
1384 return getStruct<macho::SymbolTableEntry>(this, P);
1387 macho::Symbol64TableEntry
1388 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
1389 const char *P = getSymbolTableEntryPtr(this, DRI);
1390 return getStruct<macho::Symbol64TableEntry>(this, P);
1393 macho::LinkeditDataLoadCommand
1394 MachOObjectFile::getLinkeditDataLoadCommand(const MachOObjectFile::LoadCommandInfo &L) const {
1395 return getStruct<macho::LinkeditDataLoadCommand>(this, L.Ptr);
1398 macho::RelocationEntry
1399 MachOObjectFile::getRelocation(DataRefImpl Rel) const {
1404 macho::Section64 Sect = getSection64(Sec);
1405 RelOffset = Sect.RelocationTableOffset;
1407 macho::Section Sect = getSection(Sec);
1408 RelOffset = Sect.RelocationTableOffset;
1411 uint64_t Offset = RelOffset + Rel.d.a * sizeof(macho::RelocationEntry);
1412 return getStruct<macho::RelocationEntry>(this, getPtr(this, Offset));
1415 macho::Header MachOObjectFile::getHeader() const {
1416 return getStruct<macho::Header>(this, getPtr(this, 0));
1419 macho::SymtabLoadCommand
1420 MachOObjectFile::getSymtabLoadCommand() const {
1421 return getStruct<macho::SymtabLoadCommand>(this, SymtabLoadCmd);
1424 bool MachOObjectFile::is64Bit() const {
1425 return getType() == getMachOType(false, true) ||
1426 getType() == getMachOType(true, true);
1429 void MachOObjectFile::ReadULEB128s(uint64_t Index,
1430 SmallVectorImpl<uint64_t> &Out) const {
1431 DataExtractor extractor(ObjectFile::getData(), true, 0);
1433 uint32_t offset = Index;
1435 while (uint64_t delta = extractor.getULEB128(&offset)) {
1437 Out.push_back(data);
1441 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
1442 StringRef Magic = Buffer->getBuffer().slice(0, 4);
1445 if (Magic == "\xFE\xED\xFA\xCE")
1446 Ret = new MachOObjectFile(Buffer, false, false, ec);
1447 else if (Magic == "\xCE\xFA\xED\xFE")
1448 Ret = new MachOObjectFile(Buffer, true, false, ec);
1449 else if (Magic == "\xFE\xED\xFA\xCF")
1450 Ret = new MachOObjectFile(Buffer, false, true, ec);
1451 else if (Magic == "\xCF\xFA\xED\xFE")
1452 Ret = new MachOObjectFile(Buffer, true, true, ec);
1461 } // end namespace object
1462 } // end namespace llvm