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/MachO.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/raw_ostream.h"
29 using namespace object;
34 struct SymbolTableEntryBase {
47 static void SwapValue(T &Value) {
48 Value = sys::SwapByteOrder(Value);
52 static void SwapStruct(T &Value);
55 void SwapStruct(macho::RelocationEntry &H) {
61 void SwapStruct(macho::LoadCommand &L) {
67 void SwapStruct(SymbolTableEntryBase &S) {
68 SwapValue(S.StringIndex);
73 void SwapStruct(macho::Section &S) {
78 SwapValue(S.RelocationTableOffset);
79 SwapValue(S.NumRelocationTableEntries);
81 SwapValue(S.Reserved1);
82 SwapValue(S.Reserved2);
86 void SwapStruct(macho::Section64 &S) {
91 SwapValue(S.RelocationTableOffset);
92 SwapValue(S.NumRelocationTableEntries);
94 SwapValue(S.Reserved1);
95 SwapValue(S.Reserved2);
96 SwapValue(S.Reserved3);
100 void SwapStruct(macho::SymbolTableEntry &S) {
101 SwapValue(S.StringIndex);
107 void SwapStruct(macho::Symbol64TableEntry &S) {
108 SwapValue(S.StringIndex);
114 void SwapStruct(macho::Header &H) {
116 SwapValue(H.CPUType);
117 SwapValue(H.CPUSubtype);
118 SwapValue(H.FileType);
119 SwapValue(H.NumLoadCommands);
120 SwapValue(H.SizeOfLoadCommands);
125 void SwapStruct(macho::Header64Ext &E) {
126 SwapValue(E.Reserved);
130 void SwapStruct(macho::SymtabLoadCommand &C) {
133 SwapValue(C.SymbolTableOffset);
134 SwapValue(C.NumSymbolTableEntries);
135 SwapValue(C.StringTableOffset);
136 SwapValue(C.StringTableSize);
140 void SwapStruct(macho::DysymtabLoadCommand &C) {
143 SwapValue(C.LocalSymbolsIndex);
144 SwapValue(C.NumLocalSymbols);
145 SwapValue(C.ExternalSymbolsIndex);
146 SwapValue(C.NumExternalSymbols);
147 SwapValue(C.UndefinedSymbolsIndex);
148 SwapValue(C.NumUndefinedSymbols);
149 SwapValue(C.TOCOffset);
150 SwapValue(C.NumTOCEntries);
151 SwapValue(C.ModuleTableOffset);
152 SwapValue(C.NumModuleTableEntries);
153 SwapValue(C.ReferenceSymbolTableOffset);
154 SwapValue(C.NumReferencedSymbolTableEntries);
155 SwapValue(C.IndirectSymbolTableOffset);
156 SwapValue(C.NumIndirectSymbolTableEntries);
157 SwapValue(C.ExternalRelocationTableOffset);
158 SwapValue(C.NumExternalRelocationTableEntries);
159 SwapValue(C.LocalRelocationTableOffset);
160 SwapValue(C.NumLocalRelocationTableEntries);
164 void SwapStruct(macho::LinkeditDataLoadCommand &C) {
167 SwapValue(C.DataOffset);
168 SwapValue(C.DataSize);
172 void SwapStruct(macho::SegmentLoadCommand &C) {
175 SwapValue(C.VMAddress);
177 SwapValue(C.FileOffset);
178 SwapValue(C.FileSize);
179 SwapValue(C.MaxVMProtection);
180 SwapValue(C.InitialVMProtection);
181 SwapValue(C.NumSections);
186 void SwapStruct(macho::Segment64LoadCommand &C) {
189 SwapValue(C.VMAddress);
191 SwapValue(C.FileOffset);
192 SwapValue(C.FileSize);
193 SwapValue(C.MaxVMProtection);
194 SwapValue(C.InitialVMProtection);
195 SwapValue(C.NumSections);
200 void SwapStruct(macho::IndirectSymbolTableEntry &C) {
205 void SwapStruct(macho::LinkerOptionsLoadCommand &C) {
212 void SwapStruct(macho::DataInCodeTableEntry &C) {
219 T getStruct(const MachOObjectFile *O, const char *P) {
221 memcpy(&Cmd, P, sizeof(T));
222 if (O->isLittleEndian() != sys::IsLittleEndianHost)
228 getSegmentLoadCommandNumSections(const MachOObjectFile *O,
229 const MachOObjectFile::LoadCommandInfo &L) {
231 macho::Segment64LoadCommand S = O->getSegment64LoadCommand(L);
232 return S.NumSections;
234 macho::SegmentLoadCommand S = O->getSegmentLoadCommand(L);
235 return S.NumSections;
239 getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
241 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
243 bool Is64 = O->is64Bit();
244 unsigned SegmentLoadSize = Is64 ? sizeof(macho::Segment64LoadCommand) :
245 sizeof(macho::SegmentLoadCommand);
246 unsigned SectionSize = Is64 ? sizeof(macho::Section64) :
247 sizeof(macho::Section);
249 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
250 return reinterpret_cast<const char*>(SectionAddr);
253 static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
254 return O->getData().substr(Offset, 1).data();
257 static SymbolTableEntryBase
258 getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
259 const char *P = reinterpret_cast<const char *>(DRI.p);
260 return getStruct<SymbolTableEntryBase>(O, P);
263 static StringRef parseSegmentOrSectionName(const char *P) {
267 // Not null terminated, so this is a 16 char string.
268 return StringRef(P, 16);
271 // Helper to advance a section or symbol iterator multiple increments at a time.
273 static error_code advance(T &it, size_t Val) {
282 static void advanceTo(T &it, size_t Val) {
283 if (error_code ec = advance(it, Val))
284 report_fatal_error(ec.message());
287 static unsigned getCPUType(const MachOObjectFile *O) {
288 return O->getHeader().CPUType;
291 static void printRelocationTargetName(const MachOObjectFile *O,
292 const macho::RelocationEntry &RE,
293 raw_string_ostream &fmt) {
294 bool IsScattered = O->isRelocationScattered(RE);
296 // Target of a scattered relocation is an address. In the interest of
297 // generating pretty output, scan through the symbol table looking for a
298 // symbol that aligns with that address. If we find one, print it.
299 // Otherwise, we just print the hex address of the target.
301 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
304 for (symbol_iterator SI = O->begin_symbols(), SE = O->end_symbols();
305 SI != SE; SI.increment(ec)) {
306 if (ec) report_fatal_error(ec.message());
311 if ((ec = SI->getAddress(Addr)))
312 report_fatal_error(ec.message());
313 if (Addr != Val) continue;
314 if ((ec = SI->getName(Name)))
315 report_fatal_error(ec.message());
320 // If we couldn't find a symbol that this relocation refers to, try
321 // to find a section beginning instead.
322 for (section_iterator SI = O->begin_sections(), SE = O->end_sections();
323 SI != SE; SI.increment(ec)) {
324 if (ec) report_fatal_error(ec.message());
329 if ((ec = SI->getAddress(Addr)))
330 report_fatal_error(ec.message());
331 if (Addr != Val) continue;
332 if ((ec = SI->getName(Name)))
333 report_fatal_error(ec.message());
338 fmt << format("0x%x", Val);
343 bool isExtern = O->getPlainRelocationExternal(RE);
344 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
347 symbol_iterator SI = O->begin_symbols();
351 section_iterator SI = O->begin_sections();
352 // Adjust for the fact that sections are 1-indexed.
353 advanceTo(SI, Val - 1);
360 static uint32_t getPlainRelocationAddress(const macho::RelocationEntry &RE) {
365 getScatteredRelocationAddress(const macho::RelocationEntry &RE) {
366 return RE.Word0 & 0xffffff;
369 static bool getPlainRelocationPCRel(const MachOObjectFile *O,
370 const macho::RelocationEntry &RE) {
371 if (O->isLittleEndian())
372 return (RE.Word1 >> 24) & 1;
373 return (RE.Word1 >> 7) & 1;
377 getScatteredRelocationPCRel(const MachOObjectFile *O,
378 const macho::RelocationEntry &RE) {
379 return (RE.Word0 >> 30) & 1;
382 static unsigned getPlainRelocationLength(const MachOObjectFile *O,
383 const macho::RelocationEntry &RE) {
384 if (O->isLittleEndian())
385 return (RE.Word1 >> 25) & 3;
386 return (RE.Word1 >> 5) & 3;
390 getScatteredRelocationLength(const macho::RelocationEntry &RE) {
391 return (RE.Word0 >> 28) & 3;
394 static unsigned getPlainRelocationType(const MachOObjectFile *O,
395 const macho::RelocationEntry &RE) {
396 if (O->isLittleEndian())
397 return RE.Word1 >> 28;
398 return RE.Word1 & 0xf;
401 static unsigned getScatteredRelocationType(const macho::RelocationEntry &RE) {
402 return (RE.Word0 >> 24) & 0xf;
405 static uint32_t getSectionFlags(const MachOObjectFile *O,
408 macho::Section64 Sect = O->getSection64(Sec);
411 macho::Section Sect = O->getSection(Sec);
415 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object,
416 bool IsLittleEndian, bool Is64bits,
418 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
419 SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL), DataInCodeLoadCmd(NULL) {
420 uint32_t LoadCommandCount = this->getHeader().NumLoadCommands;
421 macho::LoadCommandType SegmentLoadType = is64Bit() ?
422 macho::LCT_Segment64 : macho::LCT_Segment;
424 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
425 for (unsigned I = 0; ; ++I) {
426 if (Load.C.Type == macho::LCT_Symtab) {
427 assert(!SymtabLoadCmd && "Multiple symbol tables");
428 SymtabLoadCmd = Load.Ptr;
429 } else if (Load.C.Type == macho::LCT_Dysymtab) {
430 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
431 DysymtabLoadCmd = Load.Ptr;
432 } else if (Load.C.Type == macho::LCT_DataInCode) {
433 assert(!DataInCodeLoadCmd && "Multiple data in code tables");
434 DataInCodeLoadCmd = Load.Ptr;
435 } else if (Load.C.Type == SegmentLoadType) {
436 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
437 for (unsigned J = 0; J < NumSections; ++J) {
438 const char *Sec = getSectionPtr(this, Load, J);
439 Sections.push_back(Sec);
443 if (I == LoadCommandCount - 1)
446 Load = getNextLoadCommandInfo(Load);
450 error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb,
451 SymbolRef &Res) const {
452 unsigned SymbolTableEntrySize = is64Bit() ?
453 sizeof(macho::Symbol64TableEntry) :
454 sizeof(macho::SymbolTableEntry);
455 Symb.p += SymbolTableEntrySize;
456 Res = SymbolRef(Symb, this);
457 return object_error::success;
460 error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
461 StringRef &Res) const {
462 StringRef StringTable = getStringTableData();
463 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
464 const char *Start = &StringTable.data()[Entry.StringIndex];
465 Res = StringRef(Start);
466 return object_error::success;
469 error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
470 uint64_t &Res) const {
472 macho::Symbol64TableEntry Entry = getSymbol64TableEntry(Symb);
475 macho::SymbolTableEntry Entry = getSymbolTableEntry(Symb);
478 return object_error::success;
482 MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb,
483 uint64_t &Res) const {
484 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
485 getSymbolAddress(Symb, Res);
486 if (Entry.SectionIndex) {
489 SecRel.d.a = Entry.SectionIndex-1;
491 macho::Section64 Sec = getSection64(SecRel);
492 Delta = Sec.Offset - Sec.Address;
494 macho::Section Sec = getSection(SecRel);
495 Delta = Sec.Offset - Sec.Address;
501 return object_error::success;
504 error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
505 uint32_t &Result) const {
507 this->getSymbolFlags(DRI, flags);
508 if (flags & SymbolRef::SF_Common) {
509 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
510 Result = 1 << MachO::GET_COMM_ALIGN(Entry.Flags);
514 return object_error::success;
517 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
518 uint64_t &Result) const {
519 uint64_t BeginOffset;
520 uint64_t EndOffset = 0;
521 uint8_t SectionIndex;
523 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
525 getSymbolAddress(DRI, Value);
529 SectionIndex = Entry.SectionIndex;
531 uint32_t flags = SymbolRef::SF_None;
532 this->getSymbolFlags(DRI, flags);
533 if (flags & SymbolRef::SF_Common)
536 Result = UnknownAddressOrSize;
537 return object_error::success;
539 // Unfortunately symbols are unsorted so we need to touch all
540 // symbols from load command
542 for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E;
544 DataRefImpl DRI = I->getRawDataRefImpl();
545 Entry = getSymbolTableEntryBase(this, DRI);
546 getSymbolAddress(DRI, Value);
547 if (Entry.SectionIndex == SectionIndex && Value > BeginOffset)
548 if (!EndOffset || Value < EndOffset)
554 Sec.d.a = SectionIndex-1;
555 getSectionSize(Sec, Size);
556 getSectionAddress(Sec, EndOffset);
559 Result = EndOffset - BeginOffset;
560 return object_error::success;
563 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
564 SymbolRef::Type &Res) const {
565 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
566 uint8_t n_type = Entry.Type;
568 Res = SymbolRef::ST_Other;
570 // If this is a STAB debugging symbol, we can do nothing more.
571 if (n_type & MachO::N_STAB) {
572 Res = SymbolRef::ST_Debug;
573 return object_error::success;
576 switch (n_type & MachO::N_TYPE) {
578 Res = SymbolRef::ST_Unknown;
581 Res = SymbolRef::ST_Function;
584 return object_error::success;
587 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
589 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
590 uint8_t Type = Entry.Type;
591 uint16_t Flags = Entry.Flags;
594 switch (Type & macho::STF_TypeMask) {
595 case macho::STT_Undefined:
598 case macho::STT_Absolute:
599 case macho::STT_Section:
607 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
608 Char = toupper(static_cast<unsigned char>(Char));
610 return object_error::success;
613 error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
614 uint32_t &Result) const {
615 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
617 uint8_t MachOType = Entry.Type;
618 uint16_t MachOFlags = Entry.Flags;
620 // TODO: Correctly set SF_ThreadLocal
621 Result = SymbolRef::SF_None;
623 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
624 Result |= SymbolRef::SF_Undefined;
626 if (MachOFlags & macho::STF_StabsEntryMask)
627 Result |= SymbolRef::SF_FormatSpecific;
629 if (MachOType & MachO::N_EXT) {
630 Result |= SymbolRef::SF_Global;
631 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
633 getSymbolAddress(DRI, Value);
635 Result |= SymbolRef::SF_Common;
639 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
640 Result |= SymbolRef::SF_Weak;
642 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
643 Result |= SymbolRef::SF_Absolute;
645 return object_error::success;
649 MachOObjectFile::getSymbolSection(DataRefImpl Symb,
650 section_iterator &Res) const {
651 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
652 uint8_t index = Entry.SectionIndex;
655 Res = end_sections();
659 Res = section_iterator(SectionRef(DRI, this));
662 return object_error::success;
665 error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
666 uint64_t &Val) const {
667 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
670 error_code MachOObjectFile::getSectionNext(DataRefImpl Sec,
671 SectionRef &Res) const {
673 Res = SectionRef(Sec, this);
674 return object_error::success;
678 MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
679 ArrayRef<char> Raw = getSectionRawName(Sec);
680 Result = parseSegmentOrSectionName(Raw.data());
681 return object_error::success;
685 MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
687 macho::Section64 Sect = getSection64(Sec);
690 macho::Section Sect = getSection(Sec);
693 return object_error::success;
697 MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
699 macho::Section64 Sect = getSection64(Sec);
702 macho::Section Sect = getSection(Sec);
706 return object_error::success;
710 MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
715 macho::Section64 Sect = getSection64(Sec);
716 Offset = Sect.Offset;
719 macho::Section Sect =getSection(Sec);
720 Offset = Sect.Offset;
724 Res = this->getData().substr(Offset, Size);
725 return object_error::success;
729 MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
732 macho::Section64 Sect = getSection64(Sec);
735 macho::Section Sect = getSection(Sec);
739 Res = uint64_t(1) << Align;
740 return object_error::success;
744 MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
745 uint32_t Flags = getSectionFlags(this, Sec);
746 Res = Flags & macho::SF_PureInstructions;
747 return object_error::success;
750 error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const {
751 // FIXME: Unimplemented.
753 return object_error::success;
756 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const {
757 // FIXME: Unimplemented.
759 return object_error::success;
763 MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
764 bool &Result) const {
765 // FIXME: Unimplemented.
767 return object_error::success;
770 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
771 bool &Result) const {
772 // FIXME: Unimplemented.
774 return object_error::success;
778 MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
779 uint32_t Flags = getSectionFlags(this, Sec);
780 unsigned SectionType = Flags & MachO::SECTION_TYPE;
781 Res = SectionType == MachO::S_ZEROFILL ||
782 SectionType == MachO::S_GB_ZEROFILL;
783 return object_error::success;
786 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
787 bool &Result) const {
788 // Consider using the code from isSectionText to look for __const sections.
789 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
790 // to use section attributes to distinguish code from data.
792 // FIXME: Unimplemented.
794 return object_error::success;
798 MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
799 bool &Result) const {
801 this->getSymbolType(Symb, ST);
802 if (ST == SymbolRef::ST_Unknown) {
804 return object_error::success;
807 uint64_t SectBegin, SectEnd;
808 getSectionAddress(Sec, SectBegin);
809 getSectionSize(Sec, SectEnd);
810 SectEnd += SectBegin;
813 getSymbolAddress(Symb, SymAddr);
814 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
816 return object_error::success;
819 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
822 macho::Section64 Sect = getSection64(Sec);
823 Offset = Sect.RelocationTableOffset;
825 macho::Section Sect = getSection(Sec);
826 Offset = Sect.RelocationTableOffset;
830 Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
831 return relocation_iterator(RelocationRef(Ret, this));
835 MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
839 macho::Section64 Sect = getSection64(Sec);
840 Offset = Sect.RelocationTableOffset;
841 Num = Sect.NumRelocationTableEntries;
843 macho::Section Sect = getSection(Sec);
844 Offset = Sect.RelocationTableOffset;
845 Num = Sect.NumRelocationTableEntries;
848 const macho::RelocationEntry *P =
849 reinterpret_cast<const macho::RelocationEntry*>(getPtr(this, Offset));
852 Ret.p = reinterpret_cast<uintptr_t>(P + Num);
853 return relocation_iterator(RelocationRef(Ret, this));
856 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
857 RelocationRef &Res) const {
858 const macho::RelocationEntry *P =
859 reinterpret_cast<const macho::RelocationEntry *>(Rel.p);
860 Rel.p = reinterpret_cast<uintptr_t>(P + 1);
861 Res = RelocationRef(Rel, this);
862 return object_error::success;
866 MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
867 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile");
870 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
871 uint64_t &Res) const {
872 macho::RelocationEntry RE = getRelocation(Rel);
873 Res = getAnyRelocationAddress(RE);
874 return object_error::success;
878 MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
879 macho::RelocationEntry RE = getRelocation(Rel);
880 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
881 bool isExtern = getPlainRelocationExternal(RE);
883 return end_symbols();
885 macho::SymtabLoadCommand S = getSymtabLoadCommand();
886 unsigned SymbolTableEntrySize = is64Bit() ?
887 sizeof(macho::Symbol64TableEntry) :
888 sizeof(macho::SymbolTableEntry);
889 uint64_t Offset = S.SymbolTableOffset + SymbolIdx * SymbolTableEntrySize;
891 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
892 return symbol_iterator(SymbolRef(Sym, this));
895 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
896 uint64_t &Res) const {
897 macho::RelocationEntry RE = getRelocation(Rel);
898 Res = getAnyRelocationType(RE);
899 return object_error::success;
903 MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
904 SmallVectorImpl<char> &Result) const {
907 getRelocationType(Rel, RType);
909 unsigned Arch = this->getArch();
913 static const char *const Table[] = {
914 "GENERIC_RELOC_VANILLA",
915 "GENERIC_RELOC_PAIR",
916 "GENERIC_RELOC_SECTDIFF",
917 "GENERIC_RELOC_PB_LA_PTR",
918 "GENERIC_RELOC_LOCAL_SECTDIFF",
919 "GENERIC_RELOC_TLV" };
927 case Triple::x86_64: {
928 static const char *const Table[] = {
929 "X86_64_RELOC_UNSIGNED",
930 "X86_64_RELOC_SIGNED",
931 "X86_64_RELOC_BRANCH",
932 "X86_64_RELOC_GOT_LOAD",
934 "X86_64_RELOC_SUBTRACTOR",
935 "X86_64_RELOC_SIGNED_1",
936 "X86_64_RELOC_SIGNED_2",
937 "X86_64_RELOC_SIGNED_4",
938 "X86_64_RELOC_TLV" };
947 static const char *const Table[] = {
950 "ARM_RELOC_SECTDIFF",
951 "ARM_RELOC_LOCAL_SECTDIFF",
952 "ARM_RELOC_PB_LA_PTR",
954 "ARM_THUMB_RELOC_BR22",
955 "ARM_THUMB_32BIT_BRANCH",
957 "ARM_RELOC_HALF_SECTDIFF" };
966 static const char *const Table[] = {
975 "PPC_RELOC_SECTDIFF",
976 "PPC_RELOC_PB_LA_PTR",
977 "PPC_RELOC_HI16_SECTDIFF",
978 "PPC_RELOC_LO16_SECTDIFF",
979 "PPC_RELOC_HA16_SECTDIFF",
981 "PPC_RELOC_LO14_SECTDIFF",
982 "PPC_RELOC_LOCAL_SECTDIFF" };
987 case Triple::UnknownArch:
991 Result.append(res.begin(), res.end());
992 return object_error::success;
996 MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
997 SmallVectorImpl<char> &Result) const {
998 macho::RelocationEntry RE = getRelocation(Rel);
1000 unsigned Arch = this->getArch();
1003 raw_string_ostream fmt(fmtbuf);
1004 unsigned Type = this->getAnyRelocationType(RE);
1005 bool IsPCRel = this->getAnyRelocationPCRel(RE);
1007 // Determine any addends that should be displayed with the relocation.
1008 // These require decoding the relocation type, which is triple-specific.
1010 // X86_64 has entirely custom relocation types.
1011 if (Arch == Triple::x86_64) {
1012 bool isPCRel = getAnyRelocationPCRel(RE);
1015 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1016 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1017 printRelocationTargetName(this, RE, fmt);
1019 if (isPCRel) fmt << "PCREL";
1022 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
1023 DataRefImpl RelNext = Rel;
1025 macho::RelocationEntry RENext = getRelocation(RelNext);
1027 // X86_64_SUBTRACTOR must be followed by a relocation of type
1028 // X86_64_RELOC_UNSIGNED.
1029 // NOTE: Scattered relocations don't exist on x86_64.
1030 unsigned RType = getAnyRelocationType(RENext);
1032 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1033 "X86_64_RELOC_SUBTRACTOR.");
1035 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1036 // X86_64_SUBTRACTOR contains to the subtrahend.
1037 printRelocationTargetName(this, RENext, fmt);
1039 printRelocationTargetName(this, RE, fmt);
1042 case macho::RIT_X86_64_TLV:
1043 printRelocationTargetName(this, RE, fmt);
1045 if (isPCRel) fmt << "P";
1047 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1048 printRelocationTargetName(this, RE, fmt);
1051 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1052 printRelocationTargetName(this, RE, fmt);
1055 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1056 printRelocationTargetName(this, RE, fmt);
1060 printRelocationTargetName(this, RE, fmt);
1063 // X86 and ARM share some relocation types in common.
1064 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
1065 Arch == Triple::ppc) {
1066 // Generic relocation types...
1068 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1069 return object_error::success;
1070 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1071 DataRefImpl RelNext = Rel;
1073 macho::RelocationEntry RENext = getRelocation(RelNext);
1075 // X86 sect diff's must be followed by a relocation of type
1076 // GENERIC_RELOC_PAIR.
1077 unsigned RType = getAnyRelocationType(RENext);
1080 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1081 "GENERIC_RELOC_SECTDIFF.");
1083 printRelocationTargetName(this, RE, fmt);
1085 printRelocationTargetName(this, RENext, fmt);
1090 if (Arch == Triple::x86 || Arch == Triple::ppc) {
1091 // All X86 relocations that need special printing were already
1092 // handled in the generic code.
1094 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1095 DataRefImpl RelNext = Rel;
1097 macho::RelocationEntry RENext = getRelocation(RelNext);
1099 // X86 sect diff's must be followed by a relocation of type
1100 // GENERIC_RELOC_PAIR.
1101 unsigned RType = getAnyRelocationType(RENext);
1103 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1104 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1106 printRelocationTargetName(this, RE, fmt);
1108 printRelocationTargetName(this, RENext, fmt);
1111 case macho::RIT_Generic_TLV: {
1112 printRelocationTargetName(this, RE, fmt);
1114 if (IsPCRel) fmt << "P";
1118 printRelocationTargetName(this, RE, fmt);
1120 } else { // ARM-specific relocations
1122 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1123 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1124 // Half relocations steal a bit from the length field to encode
1125 // whether this is an upper16 or a lower16 relocation.
1126 bool isUpper = getAnyRelocationLength(RE) >> 1;
1129 fmt << ":upper16:(";
1131 fmt << ":lower16:(";
1132 printRelocationTargetName(this, RE, fmt);
1134 DataRefImpl RelNext = Rel;
1136 macho::RelocationEntry RENext = getRelocation(RelNext);
1138 // ARM half relocs must be followed by a relocation of type
1140 unsigned RType = getAnyRelocationType(RENext);
1142 report_fatal_error("Expected ARM_RELOC_PAIR after "
1143 "GENERIC_RELOC_HALF");
1145 // NOTE: The half of the target virtual address is stashed in the
1146 // address field of the secondary relocation, but we can't reverse
1147 // engineer the constant offset from it without decoding the movw/movt
1148 // instruction to find the other half in its immediate field.
1150 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1151 // symbol/section pointer of the follow-on relocation.
1152 if (Type == macho::RIT_ARM_HalfDifference) {
1154 printRelocationTargetName(this, RENext, fmt);
1161 printRelocationTargetName(this, RE, fmt);
1166 printRelocationTargetName(this, RE, fmt);
1169 Result.append(fmtbuf.begin(), fmtbuf.end());
1170 return object_error::success;
1174 MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
1175 unsigned Arch = getArch();
1177 getRelocationType(Rel, Type);
1181 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1182 // is always hidden.
1183 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
1184 if (Type == macho::RIT_Pair) Result = true;
1185 } else if (Arch == Triple::x86_64) {
1186 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1187 // an X86_64_RELOC_SUBTRACTOR.
1188 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1189 DataRefImpl RelPrev = Rel;
1192 getRelocationType(RelPrev, PrevType);
1193 if (PrevType == macho::RIT_X86_64_Subtractor)
1198 return object_error::success;
1201 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1202 LibraryRef &Res) const {
1203 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1206 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1207 StringRef &Res) const {
1208 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1211 symbol_iterator MachOObjectFile::begin_symbols() const {
1214 return symbol_iterator(SymbolRef(DRI, this));
1216 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1217 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.SymbolTableOffset));
1218 return symbol_iterator(SymbolRef(DRI, this));
1221 symbol_iterator MachOObjectFile::end_symbols() const {
1224 return symbol_iterator(SymbolRef(DRI, this));
1226 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1227 unsigned SymbolTableEntrySize = is64Bit() ?
1228 sizeof(macho::Symbol64TableEntry) :
1229 sizeof(macho::SymbolTableEntry);
1230 unsigned Offset = Symtab.SymbolTableOffset +
1231 Symtab.NumSymbolTableEntries * SymbolTableEntrySize;
1232 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1233 return symbol_iterator(SymbolRef(DRI, this));
1236 symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
1238 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1241 symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
1243 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1246 section_iterator MachOObjectFile::begin_sections() const {
1248 return section_iterator(SectionRef(DRI, this));
1251 section_iterator MachOObjectFile::end_sections() const {
1253 DRI.d.a = Sections.size();
1254 return section_iterator(SectionRef(DRI, this));
1257 library_iterator MachOObjectFile::begin_libraries_needed() const {
1259 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1262 library_iterator MachOObjectFile::end_libraries_needed() const {
1264 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1267 uint8_t MachOObjectFile::getBytesInAddress() const {
1268 return is64Bit() ? 8 : 4;
1271 StringRef MachOObjectFile::getFileFormatName() const {
1272 unsigned CPUType = getCPUType(this);
1275 case llvm::MachO::CPU_TYPE_I386:
1276 return "Mach-O 32-bit i386";
1277 case llvm::MachO::CPU_TYPE_ARM:
1278 return "Mach-O arm";
1279 case llvm::MachO::CPU_TYPE_POWERPC:
1280 return "Mach-O 32-bit ppc";
1282 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
1283 "64-bit object file when we're not 64-bit?");
1284 return "Mach-O 32-bit unknown";
1288 // Make sure the cpu type has the correct mask.
1289 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
1290 == llvm::MachO::CPU_ARCH_ABI64 &&
1291 "32-bit object file when we're 64-bit?");
1294 case llvm::MachO::CPU_TYPE_X86_64:
1295 return "Mach-O 64-bit x86-64";
1296 case llvm::MachO::CPU_TYPE_POWERPC64:
1297 return "Mach-O 64-bit ppc64";
1299 return "Mach-O 64-bit unknown";
1303 Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1305 case llvm::MachO::CPU_TYPE_I386:
1307 case llvm::MachO::CPU_TYPE_X86_64:
1308 return Triple::x86_64;
1309 case llvm::MachO::CPU_TYPE_ARM:
1311 case llvm::MachO::CPU_TYPE_POWERPC:
1313 case llvm::MachO::CPU_TYPE_POWERPC64:
1314 return Triple::ppc64;
1316 return Triple::UnknownArch;
1320 unsigned MachOObjectFile::getArch() const {
1321 return getArch(getCPUType(this));
1324 StringRef MachOObjectFile::getLoadName() const {
1326 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
1329 relocation_iterator MachOObjectFile::getSectionRelBegin(unsigned Index) const {
1332 return getSectionRelBegin(DRI);
1335 relocation_iterator MachOObjectFile::getSectionRelEnd(unsigned Index) const {
1338 return getSectionRelEnd(DRI);
1341 dice_iterator MachOObjectFile::begin_dices() const {
1343 if (!DataInCodeLoadCmd)
1344 return dice_iterator(DiceRef(DRI, this));
1346 macho::LinkeditDataLoadCommand DicLC = getDataInCodeLoadCommand();
1347 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.DataOffset));
1348 return dice_iterator(DiceRef(DRI, this));
1351 dice_iterator MachOObjectFile::end_dices() const {
1353 if (!DataInCodeLoadCmd)
1354 return dice_iterator(DiceRef(DRI, this));
1356 macho::LinkeditDataLoadCommand DicLC = getDataInCodeLoadCommand();
1357 unsigned Offset = DicLC.DataOffset + DicLC.DataSize;
1358 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1359 return dice_iterator(DiceRef(DRI, this));
1363 MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1364 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1365 return parseSegmentOrSectionName(Raw.data());
1369 MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
1370 const SectionBase *Base =
1371 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]);
1372 return ArrayRef<char>(Base->Name);
1376 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
1377 const SectionBase *Base =
1378 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]);
1379 return ArrayRef<char>(Base->SegmentName);
1383 MachOObjectFile::isRelocationScattered(const macho::RelocationEntry &RE)
1385 if (getCPUType(this) == llvm::MachO::CPU_TYPE_X86_64)
1387 return getPlainRelocationAddress(RE) & macho::RF_Scattered;
1390 unsigned MachOObjectFile::getPlainRelocationSymbolNum(
1391 const macho::RelocationEntry &RE) const {
1392 if (isLittleEndian())
1393 return RE.Word1 & 0xffffff;
1394 return RE.Word1 >> 8;
1397 bool MachOObjectFile::getPlainRelocationExternal(
1398 const macho::RelocationEntry &RE) const {
1399 if (isLittleEndian())
1400 return (RE.Word1 >> 27) & 1;
1401 return (RE.Word1 >> 4) & 1;
1404 bool MachOObjectFile::getScatteredRelocationScattered(
1405 const macho::RelocationEntry &RE) const {
1406 return RE.Word0 >> 31;
1409 uint32_t MachOObjectFile::getScatteredRelocationValue(
1410 const macho::RelocationEntry &RE) const {
1414 unsigned MachOObjectFile::getAnyRelocationAddress(
1415 const macho::RelocationEntry &RE) const {
1416 if (isRelocationScattered(RE))
1417 return getScatteredRelocationAddress(RE);
1418 return getPlainRelocationAddress(RE);
1422 MachOObjectFile::getAnyRelocationPCRel(const macho::RelocationEntry &RE) const {
1423 if (isRelocationScattered(RE))
1424 return getScatteredRelocationPCRel(this, RE);
1425 return getPlainRelocationPCRel(this, RE);
1428 unsigned MachOObjectFile::getAnyRelocationLength(
1429 const macho::RelocationEntry &RE) const {
1430 if (isRelocationScattered(RE))
1431 return getScatteredRelocationLength(RE);
1432 return getPlainRelocationLength(this, RE);
1436 MachOObjectFile::getAnyRelocationType(const macho::RelocationEntry &RE) const {
1437 if (isRelocationScattered(RE))
1438 return getScatteredRelocationType(RE);
1439 return getPlainRelocationType(this, RE);
1443 MachOObjectFile::getRelocationSection(const macho::RelocationEntry &RE) const {
1444 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
1445 return *end_sections();
1446 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1449 return SectionRef(DRI, this);
1452 MachOObjectFile::LoadCommandInfo
1453 MachOObjectFile::getFirstLoadCommandInfo() const {
1454 MachOObjectFile::LoadCommandInfo Load;
1456 unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size;
1457 Load.Ptr = getPtr(this, HeaderSize);
1458 Load.C = getStruct<macho::LoadCommand>(this, Load.Ptr);
1462 MachOObjectFile::LoadCommandInfo
1463 MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1464 MachOObjectFile::LoadCommandInfo Next;
1465 Next.Ptr = L.Ptr + L.C.Size;
1466 Next.C = getStruct<macho::LoadCommand>(this, Next.Ptr);
1470 macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const {
1471 return getStruct<macho::Section>(this, Sections[DRI.d.a]);
1474 macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1475 return getStruct<macho::Section64>(this, Sections[DRI.d.a]);
1478 macho::Section MachOObjectFile::getSection(const LoadCommandInfo &L,
1479 unsigned Index) const {
1480 const char *Sec = getSectionPtr(this, L, Index);
1481 return getStruct<macho::Section>(this, Sec);
1484 macho::Section64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1485 unsigned Index) const {
1486 const char *Sec = getSectionPtr(this, L, Index);
1487 return getStruct<macho::Section64>(this, Sec);
1490 macho::SymbolTableEntry
1491 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
1492 const char *P = reinterpret_cast<const char *>(DRI.p);
1493 return getStruct<macho::SymbolTableEntry>(this, P);
1496 macho::Symbol64TableEntry
1497 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
1498 const char *P = reinterpret_cast<const char *>(DRI.p);
1499 return getStruct<macho::Symbol64TableEntry>(this, P);
1502 macho::LinkeditDataLoadCommand MachOObjectFile::getLinkeditDataLoadCommand(
1503 const MachOObjectFile::LoadCommandInfo &L) const {
1504 return getStruct<macho::LinkeditDataLoadCommand>(this, L.Ptr);
1507 macho::SegmentLoadCommand
1508 MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
1509 return getStruct<macho::SegmentLoadCommand>(this, L.Ptr);
1512 macho::Segment64LoadCommand
1513 MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
1514 return getStruct<macho::Segment64LoadCommand>(this, L.Ptr);
1517 macho::LinkerOptionsLoadCommand
1518 MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
1519 return getStruct<macho::LinkerOptionsLoadCommand>(this, L.Ptr);
1522 macho::RelocationEntry
1523 MachOObjectFile::getRelocation(DataRefImpl Rel) const {
1524 const char *P = reinterpret_cast<const char *>(Rel.p);
1525 return getStruct<macho::RelocationEntry>(this, P);
1528 macho::DataInCodeTableEntry
1529 MachOObjectFile::getDice(DataRefImpl Rel) const {
1530 const char *P = reinterpret_cast<const char *>(Rel.p);
1531 return getStruct<macho::DataInCodeTableEntry>(this, P);
1534 macho::Header MachOObjectFile::getHeader() const {
1535 return getStruct<macho::Header>(this, getPtr(this, 0));
1538 macho::Header64Ext MachOObjectFile::getHeader64Ext() const {
1540 getStruct<macho::Header64Ext>(this, getPtr(this, sizeof(macho::Header)));
1543 macho::IndirectSymbolTableEntry MachOObjectFile::getIndirectSymbolTableEntry(
1544 const macho::DysymtabLoadCommand &DLC,
1545 unsigned Index) const {
1546 uint64_t Offset = DLC.IndirectSymbolTableOffset +
1547 Index * sizeof(macho::IndirectSymbolTableEntry);
1548 return getStruct<macho::IndirectSymbolTableEntry>(this, getPtr(this, Offset));
1551 macho::DataInCodeTableEntry
1552 MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1553 unsigned Index) const {
1554 uint64_t Offset = DataOffset + Index * sizeof(macho::DataInCodeTableEntry);
1555 return getStruct<macho::DataInCodeTableEntry>(this, getPtr(this, Offset));
1558 macho::SymtabLoadCommand MachOObjectFile::getSymtabLoadCommand() const {
1559 return getStruct<macho::SymtabLoadCommand>(this, SymtabLoadCmd);
1562 macho::DysymtabLoadCommand MachOObjectFile::getDysymtabLoadCommand() const {
1563 return getStruct<macho::DysymtabLoadCommand>(this, DysymtabLoadCmd);
1566 macho::LinkeditDataLoadCommand
1567 MachOObjectFile::getDataInCodeLoadCommand() const {
1568 if (DataInCodeLoadCmd)
1569 return getStruct<macho::LinkeditDataLoadCommand>(this, DataInCodeLoadCmd);
1571 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
1572 macho::LinkeditDataLoadCommand Cmd;
1573 Cmd.Type = macho::LCT_DataInCode;
1574 Cmd.Size = macho::LinkeditLoadCommandSize;
1580 StringRef MachOObjectFile::getStringTableData() const {
1581 macho::SymtabLoadCommand S = getSymtabLoadCommand();
1582 return getData().substr(S.StringTableOffset, S.StringTableSize);
1585 bool MachOObjectFile::is64Bit() const {
1586 return getType() == getMachOType(false, true) ||
1587 getType() == getMachOType(true, true);
1590 void MachOObjectFile::ReadULEB128s(uint64_t Index,
1591 SmallVectorImpl<uint64_t> &Out) const {
1592 DataExtractor extractor(ObjectFile::getData(), true, 0);
1594 uint32_t offset = Index;
1596 while (uint64_t delta = extractor.getULEB128(&offset)) {
1598 Out.push_back(data);
1602 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
1603 StringRef Magic = Buffer->getBuffer().slice(0, 4);
1605 OwningPtr<ObjectFile> Ret;
1606 if (Magic == "\xFE\xED\xFA\xCE")
1607 Ret.reset(new MachOObjectFile(Buffer, false, false, ec));
1608 else if (Magic == "\xCE\xFA\xED\xFE")
1609 Ret.reset(new MachOObjectFile(Buffer, true, false, ec));
1610 else if (Magic == "\xFE\xED\xFA\xCF")
1611 Ret.reset(new MachOObjectFile(Buffer, false, true, ec));
1612 else if (Magic == "\xCF\xFA\xED\xFE")
1613 Ret.reset(new MachOObjectFile(Buffer, true, true, ec));
1624 } // end namespace object
1625 } // end namespace llvm