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->getPlainRelocationSymbolNum(RE);
345 symbol_iterator SI = O->begin_symbols();
349 section_iterator SI = O->begin_sections();
350 // Adjust for the fact that sections are 1-indexed.
351 advanceTo(SI, Val - 1);
358 static uint32_t getPlainRelocationAddress(const macho::RelocationEntry &RE) {
363 getScatteredRelocationAddress(const macho::RelocationEntry &RE) {
364 return RE.Word0 & 0xffffff;
367 static bool getPlainRelocationPCRel(const MachOObjectFile *O,
368 const macho::RelocationEntry &RE) {
369 if (O->isLittleEndian())
370 return (RE.Word1 >> 24) & 1;
371 return (RE.Word1 >> 7) & 1;
375 getScatteredRelocationPCRel(const MachOObjectFile *O,
376 const macho::RelocationEntry &RE) {
377 return (RE.Word0 >> 30) & 1;
380 static unsigned getPlainRelocationLength(const MachOObjectFile *O,
381 const macho::RelocationEntry &RE) {
382 if (O->isLittleEndian())
383 return (RE.Word1 >> 25) & 3;
384 return (RE.Word1 >> 5) & 3;
388 getScatteredRelocationLength(const macho::RelocationEntry &RE) {
389 return (RE.Word0 >> 28) & 3;
392 static unsigned getPlainRelocationType(const MachOObjectFile *O,
393 const macho::RelocationEntry &RE) {
394 if (O->isLittleEndian())
395 return RE.Word1 >> 28;
396 return RE.Word1 & 0xf;
399 static unsigned getScatteredRelocationType(const macho::RelocationEntry &RE) {
400 return (RE.Word0 >> 24) & 0xf;
403 static uint32_t getSectionFlags(const MachOObjectFile *O,
406 macho::Section64 Sect = O->getSection64(Sec);
409 macho::Section Sect = O->getSection(Sec);
413 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object,
414 bool IsLittleEndian, bool Is64bits,
416 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
417 SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL), DataInCodeLoadCmd(NULL) {
418 uint32_t LoadCommandCount = this->getHeader().NumLoadCommands;
419 macho::LoadCommandType SegmentLoadType = is64Bit() ?
420 macho::LCT_Segment64 : macho::LCT_Segment;
422 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
423 for (unsigned I = 0; ; ++I) {
424 if (Load.C.Type == macho::LCT_Symtab) {
425 assert(!SymtabLoadCmd && "Multiple symbol tables");
426 SymtabLoadCmd = Load.Ptr;
427 } else if (Load.C.Type == macho::LCT_Dysymtab) {
428 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
429 DysymtabLoadCmd = Load.Ptr;
430 } else if (Load.C.Type == macho::LCT_DataInCode) {
431 assert(!DataInCodeLoadCmd && "Multiple data in code tables");
432 DataInCodeLoadCmd = Load.Ptr;
433 } else if (Load.C.Type == SegmentLoadType) {
434 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
435 for (unsigned J = 0; J < NumSections; ++J) {
436 const char *Sec = getSectionPtr(this, Load, J);
437 Sections.push_back(Sec);
441 if (I == LoadCommandCount - 1)
444 Load = getNextLoadCommandInfo(Load);
448 error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb,
449 SymbolRef &Res) const {
450 unsigned SymbolTableEntrySize = is64Bit() ?
451 sizeof(macho::Symbol64TableEntry) :
452 sizeof(macho::SymbolTableEntry);
453 Symb.p += SymbolTableEntrySize;
454 Res = SymbolRef(Symb, this);
455 return object_error::success;
458 error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
459 StringRef &Res) const {
460 StringRef StringTable = getStringTableData();
461 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
462 const char *Start = &StringTable.data()[Entry.StringIndex];
463 Res = StringRef(Start);
464 return object_error::success;
467 error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
468 uint64_t &Res) const {
470 macho::Symbol64TableEntry Entry = getSymbol64TableEntry(Symb);
473 macho::SymbolTableEntry Entry = getSymbolTableEntry(Symb);
476 return object_error::success;
480 MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb,
481 uint64_t &Res) const {
482 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
483 getSymbolAddress(Symb, Res);
484 if (Entry.SectionIndex) {
487 SecRel.d.a = Entry.SectionIndex-1;
489 macho::Section64 Sec = getSection64(SecRel);
490 Delta = Sec.Offset - Sec.Address;
492 macho::Section Sec = getSection(SecRel);
493 Delta = Sec.Offset - Sec.Address;
499 return object_error::success;
502 error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
503 uint32_t &Result) const {
505 this->getSymbolFlags(DRI, flags);
506 if (flags & SymbolRef::SF_Common) {
507 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
508 Result = 1 << MachO::GET_COMM_ALIGN(Entry.Flags);
512 return object_error::success;
515 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
516 uint64_t &Result) const {
517 uint64_t BeginOffset;
518 uint64_t EndOffset = 0;
519 uint8_t SectionIndex;
521 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
523 getSymbolAddress(DRI, Value);
527 SectionIndex = Entry.SectionIndex;
529 uint32_t flags = SymbolRef::SF_None;
530 this->getSymbolFlags(DRI, flags);
531 if (flags & SymbolRef::SF_Common)
534 Result = UnknownAddressOrSize;
535 return object_error::success;
537 // Unfortunately symbols are unsorted so we need to touch all
538 // symbols from load command
540 for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E;
542 DataRefImpl DRI = I->getRawDataRefImpl();
543 Entry = getSymbolTableEntryBase(this, DRI);
544 getSymbolAddress(DRI, Value);
545 if (Entry.SectionIndex == SectionIndex && Value > BeginOffset)
546 if (!EndOffset || Value < EndOffset)
552 Sec.d.a = SectionIndex-1;
553 getSectionSize(Sec, Size);
554 getSectionAddress(Sec, EndOffset);
557 Result = EndOffset - BeginOffset;
558 return object_error::success;
561 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
562 SymbolRef::Type &Res) const {
563 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
564 uint8_t n_type = Entry.Type;
566 Res = SymbolRef::ST_Other;
568 // If this is a STAB debugging symbol, we can do nothing more.
569 if (n_type & MachO::NlistMaskStab) {
570 Res = SymbolRef::ST_Debug;
571 return object_error::success;
574 switch (n_type & MachO::NlistMaskType) {
575 case MachO::NListTypeUndefined :
576 Res = SymbolRef::ST_Unknown;
578 case MachO::NListTypeSection :
579 Res = SymbolRef::ST_Function;
582 return object_error::success;
585 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
587 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
588 uint8_t Type = Entry.Type;
589 uint16_t Flags = Entry.Flags;
592 switch (Type & macho::STF_TypeMask) {
593 case macho::STT_Undefined:
596 case macho::STT_Absolute:
597 case macho::STT_Section:
605 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
606 Char = toupper(static_cast<unsigned char>(Char));
608 return object_error::success;
611 error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
612 uint32_t &Result) const {
613 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
615 uint8_t MachOType = Entry.Type;
616 uint16_t MachOFlags = Entry.Flags;
618 // TODO: Correctly set SF_ThreadLocal
619 Result = SymbolRef::SF_None;
621 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
622 Result |= SymbolRef::SF_Undefined;
624 if (MachOFlags & macho::STF_StabsEntryMask)
625 Result |= SymbolRef::SF_FormatSpecific;
627 if (MachOType & MachO::NlistMaskExternal) {
628 Result |= SymbolRef::SF_Global;
629 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined) {
631 getSymbolAddress(DRI, Value);
633 Result |= SymbolRef::SF_Common;
637 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
638 Result |= SymbolRef::SF_Weak;
640 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
641 Result |= SymbolRef::SF_Absolute;
643 return object_error::success;
647 MachOObjectFile::getSymbolSection(DataRefImpl Symb,
648 section_iterator &Res) const {
649 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
650 uint8_t index = Entry.SectionIndex;
653 Res = end_sections();
657 Res = section_iterator(SectionRef(DRI, this));
660 return object_error::success;
663 error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
664 uint64_t &Val) const {
665 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
668 error_code MachOObjectFile::getSectionNext(DataRefImpl Sec,
669 SectionRef &Res) const {
671 Res = SectionRef(Sec, this);
672 return object_error::success;
676 MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
677 ArrayRef<char> Raw = getSectionRawName(Sec);
678 Result = parseSegmentOrSectionName(Raw.data());
679 return object_error::success;
683 MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
685 macho::Section64 Sect = getSection64(Sec);
688 macho::Section Sect = getSection(Sec);
691 return object_error::success;
695 MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
697 macho::Section64 Sect = getSection64(Sec);
700 macho::Section Sect = getSection(Sec);
704 return object_error::success;
708 MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
713 macho::Section64 Sect = getSection64(Sec);
714 Offset = Sect.Offset;
717 macho::Section Sect =getSection(Sec);
718 Offset = Sect.Offset;
722 Res = this->getData().substr(Offset, Size);
723 return object_error::success;
727 MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
730 macho::Section64 Sect = getSection64(Sec);
733 macho::Section Sect = getSection(Sec);
737 Res = uint64_t(1) << Align;
738 return object_error::success;
742 MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
743 uint32_t Flags = getSectionFlags(this, Sec);
744 Res = Flags & macho::SF_PureInstructions;
745 return object_error::success;
748 error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const {
749 // FIXME: Unimplemented.
751 return object_error::success;
754 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const {
755 // FIXME: Unimplemented.
757 return object_error::success;
761 MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
762 bool &Result) const {
763 // FIXME: Unimplemented.
765 return object_error::success;
768 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
769 bool &Result) const {
770 // FIXME: Unimplemented.
772 return object_error::success;
776 MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
777 uint32_t Flags = getSectionFlags(this, Sec);
778 unsigned SectionType = Flags & MachO::SectionFlagMaskSectionType;
779 Res = SectionType == MachO::SectionTypeZeroFill ||
780 SectionType == MachO::SectionTypeZeroFillLarge;
781 return object_error::success;
784 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
785 bool &Result) const {
786 // Consider using the code from isSectionText to look for __const sections.
787 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
788 // to use section attributes to distinguish code from data.
790 // FIXME: Unimplemented.
792 return object_error::success;
796 MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
797 bool &Result) const {
799 this->getSymbolType(Symb, ST);
800 if (ST == SymbolRef::ST_Unknown) {
802 return object_error::success;
805 uint64_t SectBegin, SectEnd;
806 getSectionAddress(Sec, SectBegin);
807 getSectionSize(Sec, SectEnd);
808 SectEnd += SectBegin;
811 getSymbolAddress(Symb, SymAddr);
812 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
814 return object_error::success;
817 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
820 macho::Section64 Sect = getSection64(Sec);
821 Offset = Sect.RelocationTableOffset;
823 macho::Section Sect = getSection(Sec);
824 Offset = Sect.RelocationTableOffset;
828 Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
829 return relocation_iterator(RelocationRef(Ret, this));
833 MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
837 macho::Section64 Sect = getSection64(Sec);
838 Offset = Sect.RelocationTableOffset;
839 Num = Sect.NumRelocationTableEntries;
841 macho::Section Sect = getSection(Sec);
842 Offset = Sect.RelocationTableOffset;
843 Num = Sect.NumRelocationTableEntries;
846 const macho::RelocationEntry *P =
847 reinterpret_cast<const macho::RelocationEntry*>(getPtr(this, Offset));
850 Ret.p = reinterpret_cast<uintptr_t>(P + Num);
851 return relocation_iterator(RelocationRef(Ret, this));
854 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
855 RelocationRef &Res) const {
856 const macho::RelocationEntry *P =
857 reinterpret_cast<const macho::RelocationEntry *>(Rel.p);
858 Rel.p = reinterpret_cast<uintptr_t>(P + 1);
859 Res = RelocationRef(Rel, this);
860 return object_error::success;
864 MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
865 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile");
868 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
869 uint64_t &Res) const {
870 macho::RelocationEntry RE = getRelocation(Rel);
871 Res = getAnyRelocationAddress(RE);
872 return object_error::success;
876 MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
877 macho::RelocationEntry RE = getRelocation(Rel);
878 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
879 bool isExtern = getPlainRelocationExternal(RE);
881 return end_symbols();
883 macho::SymtabLoadCommand S = getSymtabLoadCommand();
884 unsigned SymbolTableEntrySize = is64Bit() ?
885 sizeof(macho::Symbol64TableEntry) :
886 sizeof(macho::SymbolTableEntry);
887 uint64_t Offset = S.SymbolTableOffset + SymbolIdx * SymbolTableEntrySize;
889 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
890 return symbol_iterator(SymbolRef(Sym, this));
893 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
894 uint64_t &Res) const {
895 macho::RelocationEntry RE = getRelocation(Rel);
896 Res = getAnyRelocationType(RE);
897 return object_error::success;
901 MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
902 SmallVectorImpl<char> &Result) const {
905 getRelocationType(Rel, RType);
907 unsigned Arch = this->getArch();
911 static const char *const Table[] = {
912 "GENERIC_RELOC_VANILLA",
913 "GENERIC_RELOC_PAIR",
914 "GENERIC_RELOC_SECTDIFF",
915 "GENERIC_RELOC_PB_LA_PTR",
916 "GENERIC_RELOC_LOCAL_SECTDIFF",
917 "GENERIC_RELOC_TLV" };
925 case Triple::x86_64: {
926 static const char *const Table[] = {
927 "X86_64_RELOC_UNSIGNED",
928 "X86_64_RELOC_SIGNED",
929 "X86_64_RELOC_BRANCH",
930 "X86_64_RELOC_GOT_LOAD",
932 "X86_64_RELOC_SUBTRACTOR",
933 "X86_64_RELOC_SIGNED_1",
934 "X86_64_RELOC_SIGNED_2",
935 "X86_64_RELOC_SIGNED_4",
936 "X86_64_RELOC_TLV" };
945 static const char *const Table[] = {
948 "ARM_RELOC_SECTDIFF",
949 "ARM_RELOC_LOCAL_SECTDIFF",
950 "ARM_RELOC_PB_LA_PTR",
952 "ARM_THUMB_RELOC_BR22",
953 "ARM_THUMB_32BIT_BRANCH",
955 "ARM_RELOC_HALF_SECTDIFF" };
964 static const char *const Table[] = {
973 "PPC_RELOC_SECTDIFF",
974 "PPC_RELOC_PB_LA_PTR",
975 "PPC_RELOC_HI16_SECTDIFF",
976 "PPC_RELOC_LO16_SECTDIFF",
977 "PPC_RELOC_HA16_SECTDIFF",
979 "PPC_RELOC_LO14_SECTDIFF",
980 "PPC_RELOC_LOCAL_SECTDIFF" };
985 case Triple::UnknownArch:
989 Result.append(res.begin(), res.end());
990 return object_error::success;
994 MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
995 SmallVectorImpl<char> &Result) const {
996 macho::RelocationEntry RE = getRelocation(Rel);
998 unsigned Arch = this->getArch();
1001 raw_string_ostream fmt(fmtbuf);
1002 unsigned Type = this->getAnyRelocationType(RE);
1003 bool IsPCRel = this->getAnyRelocationPCRel(RE);
1005 // Determine any addends that should be displayed with the relocation.
1006 // These require decoding the relocation type, which is triple-specific.
1008 // X86_64 has entirely custom relocation types.
1009 if (Arch == Triple::x86_64) {
1010 bool isPCRel = getAnyRelocationPCRel(RE);
1013 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1014 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1015 printRelocationTargetName(this, RE, fmt);
1017 if (isPCRel) fmt << "PCREL";
1020 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
1021 DataRefImpl RelNext = Rel;
1023 macho::RelocationEntry RENext = getRelocation(RelNext);
1025 // X86_64_SUBTRACTOR must be followed by a relocation of type
1026 // X86_64_RELOC_UNSIGNED.
1027 // NOTE: Scattered relocations don't exist on x86_64.
1028 unsigned RType = getAnyRelocationType(RENext);
1030 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1031 "X86_64_RELOC_SUBTRACTOR.");
1033 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1034 // X86_64_SUBTRACTOR contains to the subtrahend.
1035 printRelocationTargetName(this, RENext, fmt);
1037 printRelocationTargetName(this, RE, fmt);
1040 case macho::RIT_X86_64_TLV:
1041 printRelocationTargetName(this, RE, fmt);
1043 if (isPCRel) fmt << "P";
1045 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1046 printRelocationTargetName(this, RE, fmt);
1049 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1050 printRelocationTargetName(this, RE, fmt);
1053 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1054 printRelocationTargetName(this, RE, fmt);
1058 printRelocationTargetName(this, RE, fmt);
1061 // X86 and ARM share some relocation types in common.
1062 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
1063 Arch == Triple::ppc) {
1064 // Generic relocation types...
1066 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1067 return object_error::success;
1068 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1069 DataRefImpl RelNext = Rel;
1071 macho::RelocationEntry RENext = getRelocation(RelNext);
1073 // X86 sect diff's must be followed by a relocation of type
1074 // GENERIC_RELOC_PAIR.
1075 unsigned RType = getAnyRelocationType(RENext);
1078 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1079 "GENERIC_RELOC_SECTDIFF.");
1081 printRelocationTargetName(this, RE, fmt);
1083 printRelocationTargetName(this, RENext, fmt);
1088 if (Arch == Triple::x86 || Arch == Triple::ppc) {
1089 // All X86 relocations that need special printing were already
1090 // handled in the generic code.
1092 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1093 DataRefImpl RelNext = Rel;
1095 macho::RelocationEntry RENext = getRelocation(RelNext);
1097 // X86 sect diff's must be followed by a relocation of type
1098 // GENERIC_RELOC_PAIR.
1099 unsigned RType = getAnyRelocationType(RENext);
1101 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1102 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1104 printRelocationTargetName(this, RE, fmt);
1106 printRelocationTargetName(this, RENext, fmt);
1109 case macho::RIT_Generic_TLV: {
1110 printRelocationTargetName(this, RE, fmt);
1112 if (IsPCRel) fmt << "P";
1116 printRelocationTargetName(this, RE, fmt);
1118 } else { // ARM-specific relocations
1120 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1121 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1122 // Half relocations steal a bit from the length field to encode
1123 // whether this is an upper16 or a lower16 relocation.
1124 bool isUpper = getAnyRelocationLength(RE) >> 1;
1127 fmt << ":upper16:(";
1129 fmt << ":lower16:(";
1130 printRelocationTargetName(this, RE, fmt);
1132 DataRefImpl RelNext = Rel;
1134 macho::RelocationEntry RENext = getRelocation(RelNext);
1136 // ARM half relocs must be followed by a relocation of type
1138 unsigned RType = getAnyRelocationType(RENext);
1140 report_fatal_error("Expected ARM_RELOC_PAIR after "
1141 "GENERIC_RELOC_HALF");
1143 // NOTE: The half of the target virtual address is stashed in the
1144 // address field of the secondary relocation, but we can't reverse
1145 // engineer the constant offset from it without decoding the movw/movt
1146 // instruction to find the other half in its immediate field.
1148 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1149 // symbol/section pointer of the follow-on relocation.
1150 if (Type == macho::RIT_ARM_HalfDifference) {
1152 printRelocationTargetName(this, RENext, fmt);
1159 printRelocationTargetName(this, RE, fmt);
1164 printRelocationTargetName(this, RE, fmt);
1167 Result.append(fmtbuf.begin(), fmtbuf.end());
1168 return object_error::success;
1172 MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
1173 unsigned Arch = getArch();
1175 getRelocationType(Rel, Type);
1179 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1180 // is always hidden.
1181 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
1182 if (Type == macho::RIT_Pair) Result = true;
1183 } else if (Arch == Triple::x86_64) {
1184 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1185 // an X86_64_RELOC_SUBTRACTOR.
1186 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1187 DataRefImpl RelPrev = Rel;
1190 getRelocationType(RelPrev, PrevType);
1191 if (PrevType == macho::RIT_X86_64_Subtractor)
1196 return object_error::success;
1199 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1200 LibraryRef &Res) const {
1201 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1204 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1205 StringRef &Res) const {
1206 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1209 symbol_iterator MachOObjectFile::begin_symbols() const {
1212 return symbol_iterator(SymbolRef(DRI, this));
1214 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1215 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.SymbolTableOffset));
1216 return symbol_iterator(SymbolRef(DRI, this));
1219 symbol_iterator MachOObjectFile::end_symbols() const {
1222 return symbol_iterator(SymbolRef(DRI, this));
1224 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1225 unsigned SymbolTableEntrySize = is64Bit() ?
1226 sizeof(macho::Symbol64TableEntry) :
1227 sizeof(macho::SymbolTableEntry);
1228 unsigned Offset = Symtab.SymbolTableOffset +
1229 Symtab.NumSymbolTableEntries * SymbolTableEntrySize;
1230 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1231 return symbol_iterator(SymbolRef(DRI, this));
1234 symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
1236 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1239 symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
1241 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1244 section_iterator MachOObjectFile::begin_sections() const {
1246 return section_iterator(SectionRef(DRI, this));
1249 section_iterator MachOObjectFile::end_sections() const {
1251 DRI.d.a = Sections.size();
1252 return section_iterator(SectionRef(DRI, this));
1255 library_iterator MachOObjectFile::begin_libraries_needed() const {
1257 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1260 library_iterator MachOObjectFile::end_libraries_needed() const {
1262 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1265 uint8_t MachOObjectFile::getBytesInAddress() const {
1266 return is64Bit() ? 8 : 4;
1269 StringRef MachOObjectFile::getFileFormatName() const {
1270 unsigned CPUType = getCPUType(this);
1273 case llvm::MachO::CPUTypeI386:
1274 return "Mach-O 32-bit i386";
1275 case llvm::MachO::CPUTypeARM:
1276 return "Mach-O arm";
1277 case llvm::MachO::CPUTypePowerPC:
1278 return "Mach-O 32-bit ppc";
1280 assert((CPUType & llvm::MachO::CPUArchABI64) == 0 &&
1281 "64-bit object file when we're not 64-bit?");
1282 return "Mach-O 32-bit unknown";
1286 // Make sure the cpu type has the correct mask.
1287 assert((CPUType & llvm::MachO::CPUArchABI64)
1288 == llvm::MachO::CPUArchABI64 &&
1289 "32-bit object file when we're 64-bit?");
1292 case llvm::MachO::CPUTypeX86_64:
1293 return "Mach-O 64-bit x86-64";
1294 case llvm::MachO::CPUTypePowerPC64:
1295 return "Mach-O 64-bit ppc64";
1297 return "Mach-O 64-bit unknown";
1301 Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1303 case llvm::MachO::CPUTypeI386:
1305 case llvm::MachO::CPUTypeX86_64:
1306 return Triple::x86_64;
1307 case llvm::MachO::CPUTypeARM:
1309 case llvm::MachO::CPUTypePowerPC:
1311 case llvm::MachO::CPUTypePowerPC64:
1312 return Triple::ppc64;
1314 return Triple::UnknownArch;
1318 unsigned MachOObjectFile::getArch() const {
1319 return getArch(getCPUType(this));
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);
1339 dice_iterator MachOObjectFile::begin_dices() const {
1341 if (!DataInCodeLoadCmd)
1342 return dice_iterator(DiceRef(DRI, this));
1344 macho::LinkeditDataLoadCommand DicLC = getDataInCodeLoadCommand();
1345 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.DataOffset));
1346 return dice_iterator(DiceRef(DRI, this));
1349 dice_iterator MachOObjectFile::end_dices() const {
1351 if (!DataInCodeLoadCmd)
1352 return dice_iterator(DiceRef(DRI, this));
1354 macho::LinkeditDataLoadCommand DicLC = getDataInCodeLoadCommand();
1355 unsigned Offset = DicLC.DataOffset + DicLC.DataSize;
1356 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1357 return dice_iterator(DiceRef(DRI, this));
1361 MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1362 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1363 return parseSegmentOrSectionName(Raw.data());
1367 MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
1368 const SectionBase *Base =
1369 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]);
1370 return ArrayRef<char>(Base->Name);
1374 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
1375 const SectionBase *Base =
1376 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]);
1377 return ArrayRef<char>(Base->SegmentName);
1381 MachOObjectFile::isRelocationScattered(const macho::RelocationEntry &RE)
1383 if (getCPUType(this) == llvm::MachO::CPUTypeX86_64)
1385 return getPlainRelocationAddress(RE) & macho::RF_Scattered;
1388 unsigned MachOObjectFile::getPlainRelocationSymbolNum(
1389 const macho::RelocationEntry &RE) const {
1390 if (isLittleEndian())
1391 return RE.Word1 & 0xffffff;
1392 return RE.Word1 >> 8;
1395 bool MachOObjectFile::getPlainRelocationExternal(
1396 const macho::RelocationEntry &RE) const {
1397 if (isLittleEndian())
1398 return (RE.Word1 >> 27) & 1;
1399 return (RE.Word1 >> 4) & 1;
1402 bool MachOObjectFile::getScatteredRelocationScattered(
1403 const macho::RelocationEntry &RE) const {
1404 return RE.Word0 >> 31;
1407 uint32_t MachOObjectFile::getScatteredRelocationValue(
1408 const macho::RelocationEntry &RE) const {
1412 unsigned MachOObjectFile::getAnyRelocationAddress(
1413 const macho::RelocationEntry &RE) const {
1414 if (isRelocationScattered(RE))
1415 return getScatteredRelocationAddress(RE);
1416 return getPlainRelocationAddress(RE);
1420 MachOObjectFile::getAnyRelocationPCRel(const macho::RelocationEntry &RE) const {
1421 if (isRelocationScattered(RE))
1422 return getScatteredRelocationPCRel(this, RE);
1423 return getPlainRelocationPCRel(this, RE);
1426 unsigned MachOObjectFile::getAnyRelocationLength(
1427 const macho::RelocationEntry &RE) const {
1428 if (isRelocationScattered(RE))
1429 return getScatteredRelocationLength(RE);
1430 return getPlainRelocationLength(this, RE);
1434 MachOObjectFile::getAnyRelocationType(const macho::RelocationEntry &RE) const {
1435 if (isRelocationScattered(RE))
1436 return getScatteredRelocationType(RE);
1437 return getPlainRelocationType(this, RE);
1441 MachOObjectFile::getRelocationSection(const macho::RelocationEntry &RE) const {
1442 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
1443 return *end_sections();
1444 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1447 return SectionRef(DRI, this);
1450 MachOObjectFile::LoadCommandInfo
1451 MachOObjectFile::getFirstLoadCommandInfo() const {
1452 MachOObjectFile::LoadCommandInfo Load;
1454 unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size;
1455 Load.Ptr = getPtr(this, HeaderSize);
1456 Load.C = getStruct<macho::LoadCommand>(this, Load.Ptr);
1460 MachOObjectFile::LoadCommandInfo
1461 MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1462 MachOObjectFile::LoadCommandInfo Next;
1463 Next.Ptr = L.Ptr + L.C.Size;
1464 Next.C = getStruct<macho::LoadCommand>(this, Next.Ptr);
1468 macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const {
1469 return getStruct<macho::Section>(this, Sections[DRI.d.a]);
1472 macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1473 return getStruct<macho::Section64>(this, Sections[DRI.d.a]);
1476 macho::Section MachOObjectFile::getSection(const LoadCommandInfo &L,
1477 unsigned Index) const {
1478 const char *Sec = getSectionPtr(this, L, Index);
1479 return getStruct<macho::Section>(this, Sec);
1482 macho::Section64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1483 unsigned Index) const {
1484 const char *Sec = getSectionPtr(this, L, Index);
1485 return getStruct<macho::Section64>(this, Sec);
1488 macho::SymbolTableEntry
1489 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
1490 const char *P = reinterpret_cast<const char *>(DRI.p);
1491 return getStruct<macho::SymbolTableEntry>(this, P);
1494 macho::Symbol64TableEntry
1495 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
1496 const char *P = reinterpret_cast<const char *>(DRI.p);
1497 return getStruct<macho::Symbol64TableEntry>(this, P);
1500 macho::LinkeditDataLoadCommand MachOObjectFile::getLinkeditDataLoadCommand(
1501 const MachOObjectFile::LoadCommandInfo &L) const {
1502 return getStruct<macho::LinkeditDataLoadCommand>(this, L.Ptr);
1505 macho::SegmentLoadCommand
1506 MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
1507 return getStruct<macho::SegmentLoadCommand>(this, L.Ptr);
1510 macho::Segment64LoadCommand
1511 MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
1512 return getStruct<macho::Segment64LoadCommand>(this, L.Ptr);
1515 macho::LinkerOptionsLoadCommand
1516 MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
1517 return getStruct<macho::LinkerOptionsLoadCommand>(this, L.Ptr);
1520 macho::RelocationEntry
1521 MachOObjectFile::getRelocation(DataRefImpl Rel) const {
1522 const char *P = reinterpret_cast<const char *>(Rel.p);
1523 return getStruct<macho::RelocationEntry>(this, P);
1526 macho::DataInCodeTableEntry
1527 MachOObjectFile::getDice(DataRefImpl Rel) const {
1528 const char *P = reinterpret_cast<const char *>(Rel.p);
1529 return getStruct<macho::DataInCodeTableEntry>(this, P);
1532 macho::Header MachOObjectFile::getHeader() const {
1533 return getStruct<macho::Header>(this, getPtr(this, 0));
1536 macho::Header64Ext MachOObjectFile::getHeader64Ext() const {
1538 getStruct<macho::Header64Ext>(this, getPtr(this, sizeof(macho::Header)));
1541 macho::IndirectSymbolTableEntry MachOObjectFile::getIndirectSymbolTableEntry(
1542 const macho::DysymtabLoadCommand &DLC,
1543 unsigned Index) const {
1544 uint64_t Offset = DLC.IndirectSymbolTableOffset +
1545 Index * sizeof(macho::IndirectSymbolTableEntry);
1546 return getStruct<macho::IndirectSymbolTableEntry>(this, getPtr(this, Offset));
1549 macho::DataInCodeTableEntry
1550 MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1551 unsigned Index) const {
1552 uint64_t Offset = DataOffset + Index * sizeof(macho::DataInCodeTableEntry);
1553 return getStruct<macho::DataInCodeTableEntry>(this, getPtr(this, Offset));
1556 macho::SymtabLoadCommand MachOObjectFile::getSymtabLoadCommand() const {
1557 return getStruct<macho::SymtabLoadCommand>(this, SymtabLoadCmd);
1560 macho::DysymtabLoadCommand MachOObjectFile::getDysymtabLoadCommand() const {
1561 return getStruct<macho::DysymtabLoadCommand>(this, DysymtabLoadCmd);
1564 macho::LinkeditDataLoadCommand
1565 MachOObjectFile::getDataInCodeLoadCommand() const {
1566 if (DataInCodeLoadCmd)
1567 return getStruct<macho::LinkeditDataLoadCommand>(this, DataInCodeLoadCmd);
1569 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
1570 macho::LinkeditDataLoadCommand Cmd;
1571 Cmd.Type = macho::LCT_DataInCode;
1572 Cmd.Size = macho::LinkeditLoadCommandSize;
1578 StringRef MachOObjectFile::getStringTableData() const {
1579 macho::SymtabLoadCommand S = getSymtabLoadCommand();
1580 return getData().substr(S.StringTableOffset, S.StringTableSize);
1583 bool MachOObjectFile::is64Bit() const {
1584 return getType() == getMachOType(false, true) ||
1585 getType() == getMachOType(true, true);
1588 void MachOObjectFile::ReadULEB128s(uint64_t Index,
1589 SmallVectorImpl<uint64_t> &Out) const {
1590 DataExtractor extractor(ObjectFile::getData(), true, 0);
1592 uint32_t offset = Index;
1594 while (uint64_t delta = extractor.getULEB128(&offset)) {
1596 Out.push_back(data);
1600 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
1601 StringRef Magic = Buffer->getBuffer().slice(0, 4);
1603 OwningPtr<ObjectFile> Ret;
1604 if (Magic == "\xFE\xED\xFA\xCE")
1605 Ret.reset(new MachOObjectFile(Buffer, false, false, ec));
1606 else if (Magic == "\xCE\xFA\xED\xFE")
1607 Ret.reset(new MachOObjectFile(Buffer, true, false, ec));
1608 else if (Magic == "\xFE\xED\xFA\xCF")
1609 Ret.reset(new MachOObjectFile(Buffer, false, true, ec));
1610 else if (Magic == "\xCF\xFA\xED\xFE")
1611 Ret.reset(new MachOObjectFile(Buffer, true, true, ec));
1622 } // end namespace object
1623 } // end namespace llvm