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::Header64Ext &E) {
124 SwapValue(E.Reserved);
128 void SwapStruct(macho::SymtabLoadCommand &C) {
131 SwapValue(C.SymbolTableOffset);
132 SwapValue(C.NumSymbolTableEntries);
133 SwapValue(C.StringTableOffset);
134 SwapValue(C.StringTableSize);
138 void SwapStruct(macho::DysymtabLoadCommand &C) {
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);
162 void SwapStruct(macho::LinkeditDataLoadCommand &C) {
165 SwapValue(C.DataOffset);
166 SwapValue(C.DataSize);
170 void SwapStruct(macho::SegmentLoadCommand &C) {
173 SwapValue(C.VMAddress);
175 SwapValue(C.FileOffset);
176 SwapValue(C.FileSize);
177 SwapValue(C.MaxVMProtection);
178 SwapValue(C.InitialVMProtection);
179 SwapValue(C.NumSections);
184 void SwapStruct(macho::Segment64LoadCommand &C) {
187 SwapValue(C.VMAddress);
189 SwapValue(C.FileOffset);
190 SwapValue(C.FileSize);
191 SwapValue(C.MaxVMProtection);
192 SwapValue(C.InitialVMProtection);
193 SwapValue(C.NumSections);
198 void SwapStruct(macho::IndirectSymbolTableEntry &C) {
203 void SwapStruct(macho::LinkerOptionsLoadCommand &C) {
210 void SwapStruct(macho::DataInCodeTableEntry &C) {
217 T getStruct(const MachOObjectFile *O, const char *P) {
219 memcpy(&Cmd, P, sizeof(T));
220 if (O->isLittleEndian() != sys::IsLittleEndianHost)
226 getSegmentLoadCommandNumSections(const MachOObjectFile *O,
227 const MachOObjectFile::LoadCommandInfo &L) {
229 macho::Segment64LoadCommand S = O->getSegment64LoadCommand(L);
230 return S.NumSections;
232 macho::SegmentLoadCommand S = O->getSegmentLoadCommand(L);
233 return S.NumSections;
237 getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
239 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
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);
247 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
248 return reinterpret_cast<const char*>(SectionAddr);
251 static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
252 return O->getData().substr(Offset, 1).data();
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);
261 static StringRef parseSegmentOrSectionName(const char *P) {
265 // Not null terminated, so this is a 16 char string.
266 return StringRef(P, 16);
269 // Helper to advance a section or symbol iterator multiple increments at a time.
271 static error_code advance(T &it, size_t Val) {
280 static void advanceTo(T &it, size_t Val) {
281 if (error_code ec = advance(it, Val))
282 report_fatal_error(ec.message());
285 static unsigned getCPUType(const MachOObjectFile *O) {
286 return O->getHeader().CPUType;
289 static void printRelocationTargetName(const MachOObjectFile *O,
290 const macho::RelocationEntry &RE,
291 raw_string_ostream &fmt) {
292 bool IsScattered = O->isRelocationScattered(RE);
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.
299 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
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());
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());
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());
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());
336 fmt << format("0x%x", Val);
341 bool isExtern = O->getPlainRelocationExternal(RE);
342 uint64_t Val = O->getAnyRelocationAddress(RE);
345 symbol_iterator SI = O->begin_symbols();
349 section_iterator SI = O->begin_sections();
357 static uint32_t getPlainRelocationAddress(const macho::RelocationEntry &RE) {
362 getScatteredRelocationAddress(const macho::RelocationEntry &RE) {
363 return RE.Word0 & 0xffffff;
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;
374 getScatteredRelocationPCRel(const MachOObjectFile *O,
375 const macho::RelocationEntry &RE) {
376 return (RE.Word0 >> 30) & 1;
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;
387 getScatteredRelocationLength(const macho::RelocationEntry &RE) {
388 return (RE.Word0 >> 28) & 3;
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;
398 static unsigned getScatteredRelocationType(const macho::RelocationEntry &RE) {
399 return (RE.Word0 >> 24) & 0xf;
402 static uint32_t getSectionFlags(const MachOObjectFile *O,
405 macho::Section64 Sect = O->getSection64(Sec);
408 macho::Section Sect = O->getSection(Sec);
412 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object,
413 bool IsLittleEndian, bool Is64bits,
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;
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);
437 if (I == LoadCommandCount - 1)
440 Load = getNextLoadCommandInfo(Load);
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;
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;
463 error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
464 uint64_t &Res) const {
466 macho::Symbol64TableEntry Entry = getSymbol64TableEntry(Symb);
469 macho::SymbolTableEntry Entry = getSymbolTableEntry(Symb);
472 return object_error::success;
476 MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb,
477 uint64_t &Res) const {
478 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
479 getSymbolAddress(Symb, Res);
480 if (Entry.SectionIndex) {
483 SecRel.d.a = Entry.SectionIndex-1;
485 macho::Section64 Sec = getSection64(SecRel);
486 Delta = Sec.Offset - Sec.Address;
488 macho::Section Sec = getSection(SecRel);
489 Delta = Sec.Offset - Sec.Address;
495 return object_error::success;
498 error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
499 uint32_t &Result) const {
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);
508 return object_error::success;
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;
517 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
519 getSymbolAddress(DRI, Value);
523 SectionIndex = Entry.SectionIndex;
525 uint32_t flags = SymbolRef::SF_None;
526 this->getSymbolFlags(DRI, flags);
527 if (flags & SymbolRef::SF_Common)
530 Result = UnknownAddressOrSize;
531 return object_error::success;
533 // Unfortunately symbols are unsorted so we need to touch all
534 // symbols from load command
536 for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E;
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)
548 Sec.d.a = SectionIndex-1;
549 getSectionSize(Sec, Size);
550 getSectionAddress(Sec, EndOffset);
553 Result = EndOffset - BeginOffset;
554 return object_error::success;
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;
562 Res = SymbolRef::ST_Other;
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;
570 switch (n_type & MachO::NlistMaskType) {
571 case MachO::NListTypeUndefined :
572 Res = SymbolRef::ST_Unknown;
574 case MachO::NListTypeSection :
575 Res = SymbolRef::ST_Function;
578 return object_error::success;
581 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
583 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
584 uint8_t Type = Entry.Type;
585 uint16_t Flags = Entry.Flags;
588 switch (Type & macho::STF_TypeMask) {
589 case macho::STT_Undefined:
592 case macho::STT_Absolute:
593 case macho::STT_Section:
601 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
602 Char = toupper(static_cast<unsigned char>(Char));
604 return object_error::success;
607 error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
608 uint32_t &Result) const {
609 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
611 uint8_t MachOType = Entry.Type;
612 uint16_t MachOFlags = Entry.Flags;
614 // TODO: Correctly set SF_ThreadLocal
615 Result = SymbolRef::SF_None;
617 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
618 Result |= SymbolRef::SF_Undefined;
620 if (MachOFlags & macho::STF_StabsEntryMask)
621 Result |= SymbolRef::SF_FormatSpecific;
623 if (MachOType & MachO::NlistMaskExternal) {
624 Result |= SymbolRef::SF_Global;
625 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined) {
627 getSymbolAddress(DRI, Value);
629 Result |= SymbolRef::SF_Common;
633 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
634 Result |= SymbolRef::SF_Weak;
636 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
637 Result |= SymbolRef::SF_Absolute;
639 return object_error::success;
643 MachOObjectFile::getSymbolSection(DataRefImpl Symb,
644 section_iterator &Res) const {
645 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
646 uint8_t index = Entry.SectionIndex;
649 Res = end_sections();
653 Res = section_iterator(SectionRef(DRI, this));
656 return object_error::success;
659 error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
660 uint64_t &Val) const {
661 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
664 error_code MachOObjectFile::getSectionNext(DataRefImpl Sec,
665 SectionRef &Res) const {
667 Res = SectionRef(Sec, this);
668 return object_error::success;
672 MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
673 ArrayRef<char> Raw = getSectionRawName(Sec);
674 Result = parseSegmentOrSectionName(Raw.data());
675 return object_error::success;
679 MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
681 macho::Section64 Sect = getSection64(Sec);
684 macho::Section Sect = getSection(Sec);
687 return object_error::success;
691 MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
693 macho::Section64 Sect = getSection64(Sec);
696 macho::Section Sect = getSection(Sec);
700 return object_error::success;
704 MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
709 macho::Section64 Sect = getSection64(Sec);
710 Offset = Sect.Offset;
713 macho::Section Sect =getSection(Sec);
714 Offset = Sect.Offset;
718 Res = this->getData().substr(Offset, Size);
719 return object_error::success;
723 MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
726 macho::Section64 Sect = getSection64(Sec);
729 macho::Section Sect = getSection(Sec);
733 Res = uint64_t(1) << Align;
734 return object_error::success;
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;
744 error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const {
745 // FIXME: Unimplemented.
747 return object_error::success;
750 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const {
751 // FIXME: Unimplemented.
753 return object_error::success;
757 MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
758 bool &Result) const {
759 // FIXME: Unimplemented.
761 return object_error::success;
764 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
765 bool &Result) const {
766 // FIXME: Unimplemented.
768 return object_error::success;
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;
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.
786 // FIXME: Unimplemented.
788 return object_error::success;
792 MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
793 bool &Result) const {
795 this->getSymbolType(Symb, ST);
796 if (ST == SymbolRef::ST_Unknown) {
798 return object_error::success;
801 uint64_t SectBegin, SectEnd;
802 getSectionAddress(Sec, SectBegin);
803 getSectionSize(Sec, SectEnd);
804 SectEnd += SectBegin;
807 getSymbolAddress(Symb, SymAddr);
808 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
810 return object_error::success;
813 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
816 macho::Section64 Sect = getSection64(Sec);
817 Offset = Sect.RelocationTableOffset;
819 macho::Section Sect = getSection(Sec);
820 Offset = Sect.RelocationTableOffset;
824 Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
825 return relocation_iterator(RelocationRef(Ret, this));
829 MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
833 macho::Section64 Sect = getSection64(Sec);
834 Offset = Sect.RelocationTableOffset;
835 Num = Sect.NumRelocationTableEntries;
837 macho::Section Sect = getSection(Sec);
838 Offset = Sect.RelocationTableOffset;
839 Num = Sect.NumRelocationTableEntries;
842 const macho::RelocationEntry *P =
843 reinterpret_cast<const macho::RelocationEntry*>(getPtr(this, Offset));
846 Ret.p = reinterpret_cast<uintptr_t>(P + Num);
847 return relocation_iterator(RelocationRef(Ret, this));
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;
860 MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
861 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile");
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;
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);
877 Res = *end_symbols();
878 return object_error::success;
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;
887 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
888 Res = SymbolRef(Sym, this);
889 return object_error::success;
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;
900 MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
901 SmallVectorImpl<char> &Result) const {
904 getRelocationType(Rel, RType);
906 unsigned Arch = this->getArch();
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" };
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",
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" };
944 static const char *const Table[] = {
947 "ARM_RELOC_SECTDIFF",
948 "ARM_RELOC_LOCAL_SECTDIFF",
949 "ARM_RELOC_PB_LA_PTR",
951 "ARM_THUMB_RELOC_BR22",
952 "ARM_THUMB_32BIT_BRANCH",
954 "ARM_RELOC_HALF_SECTDIFF" };
963 static const char *const Table[] = {
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",
978 "PPC_RELOC_LO14_SECTDIFF",
979 "PPC_RELOC_LOCAL_SECTDIFF" };
984 case Triple::UnknownArch:
988 Result.append(res.begin(), res.end());
989 return object_error::success;
992 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
993 int64_t &Res) const {
995 return object_error::success;
999 MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1000 SmallVectorImpl<char> &Result) const {
1001 macho::RelocationEntry RE = getRelocation(Rel);
1003 unsigned Arch = this->getArch();
1006 raw_string_ostream fmt(fmtbuf);
1007 unsigned Type = this->getAnyRelocationType(RE);
1008 bool IsPCRel = this->getAnyRelocationPCRel(RE);
1010 // Determine any addends that should be displayed with the relocation.
1011 // These require decoding the relocation type, which is triple-specific.
1013 // X86_64 has entirely custom relocation types.
1014 if (Arch == Triple::x86_64) {
1015 bool isPCRel = getAnyRelocationPCRel(RE);
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);
1022 if (isPCRel) fmt << "PCREL";
1025 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
1026 DataRefImpl RelNext = Rel;
1028 macho::RelocationEntry RENext = getRelocation(RelNext);
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);
1035 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1036 "X86_64_RELOC_SUBTRACTOR.");
1038 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1039 // X86_64_SUBTRACTOR contains to the subtrahend.
1040 printRelocationTargetName(this, RENext, fmt);
1042 printRelocationTargetName(this, RE, fmt);
1045 case macho::RIT_X86_64_TLV:
1046 printRelocationTargetName(this, RE, fmt);
1048 if (isPCRel) fmt << "P";
1050 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1051 printRelocationTargetName(this, RE, fmt);
1054 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1055 printRelocationTargetName(this, RE, fmt);
1058 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1059 printRelocationTargetName(this, RE, fmt);
1063 printRelocationTargetName(this, RE, fmt);
1066 // X86 and ARM share some relocation types in common.
1067 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1068 // Generic relocation types...
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;
1075 macho::RelocationEntry RENext = getRelocation(RelNext);
1077 // X86 sect diff's must be followed by a relocation of type
1078 // GENERIC_RELOC_PAIR.
1079 unsigned RType = getAnyRelocationType(RENext);
1082 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1083 "GENERIC_RELOC_SECTDIFF.");
1085 printRelocationTargetName(this, RE, fmt);
1087 printRelocationTargetName(this, RENext, fmt);
1092 if (Arch == Triple::x86) {
1093 // All X86 relocations that need special printing were already
1094 // handled in the generic code.
1096 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1097 DataRefImpl RelNext = Rel;
1099 macho::RelocationEntry RENext = getRelocation(RelNext);
1101 // X86 sect diff's must be followed by a relocation of type
1102 // GENERIC_RELOC_PAIR.
1103 unsigned RType = getAnyRelocationType(RENext);
1105 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1106 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1108 printRelocationTargetName(this, RE, fmt);
1110 printRelocationTargetName(this, RENext, fmt);
1113 case macho::RIT_Generic_TLV: {
1114 printRelocationTargetName(this, RE, fmt);
1116 if (IsPCRel) fmt << "P";
1120 printRelocationTargetName(this, RE, fmt);
1122 } else { // ARM-specific relocations
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;
1131 fmt << ":upper16:(";
1133 fmt << ":lower16:(";
1134 printRelocationTargetName(this, RE, fmt);
1136 DataRefImpl RelNext = Rel;
1138 macho::RelocationEntry RENext = getRelocation(RelNext);
1140 // ARM half relocs must be followed by a relocation of type
1142 unsigned RType = getAnyRelocationType(RENext);
1144 report_fatal_error("Expected ARM_RELOC_PAIR after "
1145 "GENERIC_RELOC_HALF");
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.
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) {
1156 printRelocationTargetName(this, RENext, fmt);
1163 printRelocationTargetName(this, RE, fmt);
1168 printRelocationTargetName(this, RE, fmt);
1171 Result.append(fmtbuf.begin(), fmtbuf.end());
1172 return object_error::success;
1176 MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
1177 unsigned Arch = getArch();
1179 getRelocationType(Rel, Type);
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;
1194 getRelocationType(RelPrev, PrevType);
1195 if (PrevType == macho::RIT_X86_64_Subtractor)
1200 return object_error::success;
1203 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1204 LibraryRef &Res) const {
1205 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1208 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1209 StringRef &Res) const {
1210 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1213 symbol_iterator MachOObjectFile::begin_symbols() const {
1216 return symbol_iterator(SymbolRef(DRI, this));
1218 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1219 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.SymbolTableOffset));
1220 return symbol_iterator(SymbolRef(DRI, this));
1223 symbol_iterator MachOObjectFile::end_symbols() const {
1226 return symbol_iterator(SymbolRef(DRI, this));
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));
1238 symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
1240 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1243 symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
1245 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1248 section_iterator MachOObjectFile::begin_sections() const {
1250 return section_iterator(SectionRef(DRI, this));
1253 section_iterator MachOObjectFile::end_sections() const {
1255 DRI.d.a = Sections.size();
1256 return section_iterator(SectionRef(DRI, this));
1259 library_iterator MachOObjectFile::begin_libraries_needed() const {
1261 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1264 library_iterator MachOObjectFile::end_libraries_needed() const {
1266 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1269 uint8_t MachOObjectFile::getBytesInAddress() const {
1270 return is64Bit() ? 8 : 4;
1273 StringRef MachOObjectFile::getFileFormatName() const {
1274 unsigned CPUType = getCPUType(this);
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";
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";
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?");
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";
1301 return "Mach-O 64-bit unknown";
1305 unsigned MachOObjectFile::getArch() const {
1306 switch (getCPUType(this)) {
1307 case llvm::MachO::CPUTypeI386:
1309 case llvm::MachO::CPUTypeX86_64:
1310 return Triple::x86_64;
1311 case llvm::MachO::CPUTypeARM:
1313 case llvm::MachO::CPUTypePowerPC:
1315 case llvm::MachO::CPUTypePowerPC64:
1316 return Triple::ppc64;
1318 return Triple::UnknownArch;
1322 StringRef MachOObjectFile::getLoadName() const {
1324 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
1327 relocation_iterator MachOObjectFile::getSectionRelBegin(unsigned Index) const {
1330 return getSectionRelBegin(DRI);
1333 relocation_iterator MachOObjectFile::getSectionRelEnd(unsigned Index) const {
1336 return getSectionRelEnd(DRI);
1340 MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1341 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1342 return parseSegmentOrSectionName(Raw.data());
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);
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);
1360 MachOObjectFile::isRelocationScattered(const macho::RelocationEntry &RE)
1362 if (getCPUType(this) == llvm::MachO::CPUTypeX86_64)
1364 return getPlainRelocationAddress(RE) & macho::RF_Scattered;
1367 unsigned MachOObjectFile::getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const {
1368 if (isLittleEndian())
1369 return RE.Word1 & 0xffffff;
1370 return RE.Word1 >> 8;
1373 bool MachOObjectFile::getPlainRelocationExternal(const macho::RelocationEntry &RE) const {
1374 if (isLittleEndian())
1375 return (RE.Word1 >> 27) & 1;
1376 return (RE.Word1 >> 4) & 1;
1380 MachOObjectFile::getScatteredRelocationScattered(const macho::RelocationEntry &RE) const {
1381 return RE.Word0 >> 31;
1385 MachOObjectFile::getScatteredRelocationValue(const macho::RelocationEntry &RE) const {
1390 MachOObjectFile::getAnyRelocationAddress(const macho::RelocationEntry &RE) const {
1391 if (isRelocationScattered(RE))
1392 return getScatteredRelocationAddress(RE);
1393 return getPlainRelocationAddress(RE);
1397 MachOObjectFile::getAnyRelocationPCRel(const macho::RelocationEntry &RE) const {
1398 if (isRelocationScattered(RE))
1399 return getScatteredRelocationPCRel(this, RE);
1400 return getPlainRelocationPCRel(this, RE);
1404 MachOObjectFile::getAnyRelocationLength(const macho::RelocationEntry &RE) const {
1405 if (isRelocationScattered(RE))
1406 return getScatteredRelocationLength(RE);
1407 return getPlainRelocationLength(this, RE);
1411 MachOObjectFile::getAnyRelocationType(const macho::RelocationEntry &RE) const {
1412 if (isRelocationScattered(RE))
1413 return getScatteredRelocationType(RE);
1414 return getPlainRelocationType(this, RE);
1418 MachOObjectFile::getRelocationSection(const macho::RelocationEntry &RE) const {
1419 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
1420 return *end_sections();
1421 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1424 return SectionRef(DRI, this);
1427 MachOObjectFile::LoadCommandInfo
1428 MachOObjectFile::getFirstLoadCommandInfo() const {
1429 MachOObjectFile::LoadCommandInfo Load;
1431 unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size;
1432 Load.Ptr = getPtr(this, HeaderSize);
1433 Load.C = getStruct<macho::LoadCommand>(this, Load.Ptr);
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);
1445 macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const {
1446 return getStruct<macho::Section>(this, Sections[DRI.d.a]);
1449 macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1450 return getStruct<macho::Section64>(this, Sections[DRI.d.a]);
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);
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);
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);
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);
1477 macho::LinkeditDataLoadCommand
1478 MachOObjectFile::getLinkeditDataLoadCommand(const MachOObjectFile::LoadCommandInfo &L) const {
1479 return getStruct<macho::LinkeditDataLoadCommand>(this, L.Ptr);
1482 macho::SegmentLoadCommand
1483 MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
1484 return getStruct<macho::SegmentLoadCommand>(this, L.Ptr);
1487 macho::Segment64LoadCommand
1488 MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
1489 return getStruct<macho::Segment64LoadCommand>(this, L.Ptr);
1492 macho::LinkerOptionsLoadCommand
1493 MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
1494 return getStruct<macho::LinkerOptionsLoadCommand>(this, L.Ptr);
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);
1503 macho::Header MachOObjectFile::getHeader() const {
1504 return getStruct<macho::Header>(this, getPtr(this, 0));
1507 macho::Header64Ext MachOObjectFile::getHeader64Ext() const {
1509 getStruct<macho::Header64Ext>(this, getPtr(this, sizeof(macho::Header)));
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));
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));
1527 macho::SymtabLoadCommand MachOObjectFile::getSymtabLoadCommand() const {
1528 return getStruct<macho::SymtabLoadCommand>(this, SymtabLoadCmd);
1531 macho::DysymtabLoadCommand MachOObjectFile::getDysymtabLoadCommand() const {
1532 return getStruct<macho::DysymtabLoadCommand>(this, DysymtabLoadCmd);
1535 StringRef MachOObjectFile::getStringTableData() const {
1536 macho::SymtabLoadCommand S = getSymtabLoadCommand();
1537 return getData().substr(S.StringTableOffset, S.StringTableSize);
1540 bool MachOObjectFile::is64Bit() const {
1541 return getType() == getMachOType(false, true) ||
1542 getType() == getMachOType(true, true);
1545 void MachOObjectFile::ReadULEB128s(uint64_t Index,
1546 SmallVectorImpl<uint64_t> &Out) const {
1547 DataExtractor extractor(ObjectFile::getData(), true, 0);
1549 uint32_t offset = Index;
1551 while (uint64_t delta = extractor.getULEB128(&offset)) {
1553 Out.push_back(data);
1557 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
1558 StringRef Magic = Buffer->getBuffer().slice(0, 4);
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);
1577 } // end namespace object
1578 } // end namespace llvm