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/Support/DataExtractor.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/Host.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/raw_ostream.h"
27 using namespace object;
45 static void SwapValue(T &Value) {
46 Value = sys::SwapByteOrder(Value);
50 static void SwapStruct(T &Value);
53 void SwapStruct(MachO::any_relocation_info &H) {
59 void SwapStruct(MachO::load_command &L) {
65 void SwapStruct(nlist_base &S) {
71 void SwapStruct(MachO::section &S) {
79 SwapValue(S.reserved1);
80 SwapValue(S.reserved2);
84 void SwapStruct(MachO::section_64 &S) {
92 SwapValue(S.reserved1);
93 SwapValue(S.reserved2);
94 SwapValue(S.reserved3);
98 void SwapStruct(MachO::nlist &S) {
101 SwapValue(S.n_value);
105 void SwapStruct(MachO::nlist_64 &S) {
108 SwapValue(S.n_value);
112 void SwapStruct(MachO::mach_header &H) {
114 SwapValue(H.cputype);
115 SwapValue(H.cpusubtype);
116 SwapValue(H.filetype);
118 SwapValue(H.sizeofcmds);
123 void SwapStruct(MachO::mach_header_64 &H) {
125 SwapValue(H.cputype);
126 SwapValue(H.cpusubtype);
127 SwapValue(H.filetype);
129 SwapValue(H.sizeofcmds);
131 SwapValue(H.reserved);
135 void SwapStruct(MachO::symtab_command &C) {
137 SwapValue(C.cmdsize);
141 SwapValue(C.strsize);
145 void SwapStruct(MachO::dysymtab_command &C) {
147 SwapValue(C.cmdsize);
148 SwapValue(C.ilocalsym);
149 SwapValue(C.nlocalsym);
150 SwapValue(C.iextdefsym);
151 SwapValue(C.nextdefsym);
152 SwapValue(C.iundefsym);
153 SwapValue(C.nundefsym);
156 SwapValue(C.modtaboff);
157 SwapValue(C.nmodtab);
158 SwapValue(C.extrefsymoff);
159 SwapValue(C.nextrefsyms);
160 SwapValue(C.indirectsymoff);
161 SwapValue(C.nindirectsyms);
162 SwapValue(C.extreloff);
163 SwapValue(C.nextrel);
164 SwapValue(C.locreloff);
165 SwapValue(C.nlocrel);
169 void SwapStruct(MachO::linkedit_data_command &C) {
171 SwapValue(C.cmdsize);
172 SwapValue(C.dataoff);
173 SwapValue(C.datasize);
177 void SwapStruct(MachO::segment_command &C) {
179 SwapValue(C.cmdsize);
182 SwapValue(C.fileoff);
183 SwapValue(C.filesize);
184 SwapValue(C.maxprot);
185 SwapValue(C.initprot);
191 void SwapStruct(MachO::segment_command_64 &C) {
193 SwapValue(C.cmdsize);
196 SwapValue(C.fileoff);
197 SwapValue(C.filesize);
198 SwapValue(C.maxprot);
199 SwapValue(C.initprot);
205 void SwapStruct(uint32_t &C) {
210 void SwapStruct(MachO::linker_options_command &C) {
212 SwapValue(C.cmdsize);
217 void SwapStruct(MachO::version_min_command&C) {
219 SwapValue(C.cmdsize);
220 SwapValue(C.version);
221 SwapValue(C.reserved);
225 void SwapStruct(MachO::data_in_code_entry &C) {
232 T getStruct(const MachOObjectFile *O, const char *P) {
234 memcpy(&Cmd, P, sizeof(T));
235 if (O->isLittleEndian() != sys::IsLittleEndianHost)
241 getSegmentLoadCommandNumSections(const MachOObjectFile *O,
242 const MachOObjectFile::LoadCommandInfo &L) {
244 MachO::segment_command_64 S = O->getSegment64LoadCommand(L);
247 MachO::segment_command S = O->getSegmentLoadCommand(L);
252 getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
254 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
256 bool Is64 = O->is64Bit();
257 unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
258 sizeof(MachO::segment_command);
259 unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
260 sizeof(MachO::section);
262 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
263 return reinterpret_cast<const char*>(SectionAddr);
266 static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
267 return O->getData().substr(Offset, 1).data();
271 getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
272 const char *P = reinterpret_cast<const char *>(DRI.p);
273 return getStruct<nlist_base>(O, P);
276 static StringRef parseSegmentOrSectionName(const char *P) {
280 // Not null terminated, so this is a 16 char string.
281 return StringRef(P, 16);
284 // Helper to advance a section or symbol iterator multiple increments at a time.
286 static void advance(T &it, size_t Val) {
291 static unsigned getCPUType(const MachOObjectFile *O) {
292 return O->getHeader().cputype;
295 static void printRelocationTargetName(const MachOObjectFile *O,
296 const MachO::any_relocation_info &RE,
297 raw_string_ostream &fmt) {
298 bool IsScattered = O->isRelocationScattered(RE);
300 // Target of a scattered relocation is an address. In the interest of
301 // generating pretty output, scan through the symbol table looking for a
302 // symbol that aligns with that address. If we find one, print it.
303 // Otherwise, we just print the hex address of the target.
305 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
307 for (const SymbolRef &Symbol : O->symbols()) {
312 if ((ec = Symbol.getAddress(Addr)))
313 report_fatal_error(ec.message());
316 if ((ec = Symbol.getName(Name)))
317 report_fatal_error(ec.message());
322 // If we couldn't find a symbol that this relocation refers to, try
323 // to find a section beginning instead.
324 for (const SectionRef &Section : O->sections()) {
329 if ((ec = Section.getAddress(Addr)))
330 report_fatal_error(ec.message());
333 if ((ec = Section.getName(Name)))
334 report_fatal_error(ec.message());
339 fmt << format("0x%x", Val);
344 bool isExtern = O->getPlainRelocationExternal(RE);
345 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
348 symbol_iterator SI = O->symbol_begin();
352 section_iterator SI = O->section_begin();
353 // Adjust for the fact that sections are 1-indexed.
354 advance(SI, Val - 1);
362 getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
367 getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
368 return RE.r_word0 & 0xffffff;
371 static bool getPlainRelocationPCRel(const MachOObjectFile *O,
372 const MachO::any_relocation_info &RE) {
373 if (O->isLittleEndian())
374 return (RE.r_word1 >> 24) & 1;
375 return (RE.r_word1 >> 7) & 1;
379 getScatteredRelocationPCRel(const MachOObjectFile *O,
380 const MachO::any_relocation_info &RE) {
381 return (RE.r_word0 >> 30) & 1;
384 static unsigned getPlainRelocationLength(const MachOObjectFile *O,
385 const MachO::any_relocation_info &RE) {
386 if (O->isLittleEndian())
387 return (RE.r_word1 >> 25) & 3;
388 return (RE.r_word1 >> 5) & 3;
392 getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
393 return (RE.r_word0 >> 28) & 3;
396 static unsigned getPlainRelocationType(const MachOObjectFile *O,
397 const MachO::any_relocation_info &RE) {
398 if (O->isLittleEndian())
399 return RE.r_word1 >> 28;
400 return RE.r_word1 & 0xf;
404 getScatteredRelocationType(const MachO::any_relocation_info &RE) {
405 return (RE.r_word0 >> 24) & 0xf;
408 static uint32_t getSectionFlags(const MachOObjectFile *O,
411 MachO::section_64 Sect = O->getSection64(Sec);
414 MachO::section Sect = O->getSection(Sec);
418 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian,
419 bool Is64bits, error_code &EC,
421 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object, BufferOwned),
422 SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL), DataInCodeLoadCmd(NULL) {
423 uint32_t LoadCommandCount = this->getHeader().ncmds;
424 MachO::LoadCommandType SegmentLoadType = is64Bit() ?
425 MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
427 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
428 for (unsigned I = 0; ; ++I) {
429 if (Load.C.cmd == MachO::LC_SYMTAB) {
430 assert(!SymtabLoadCmd && "Multiple symbol tables");
431 SymtabLoadCmd = Load.Ptr;
432 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
433 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
434 DysymtabLoadCmd = Load.Ptr;
435 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
436 assert(!DataInCodeLoadCmd && "Multiple data in code tables");
437 DataInCodeLoadCmd = Load.Ptr;
438 } else if (Load.C.cmd == SegmentLoadType) {
439 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
440 for (unsigned J = 0; J < NumSections; ++J) {
441 const char *Sec = getSectionPtr(this, Load, J);
442 Sections.push_back(Sec);
446 if (I == LoadCommandCount - 1)
449 Load = getNextLoadCommandInfo(Load);
453 void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
454 unsigned SymbolTableEntrySize = is64Bit() ?
455 sizeof(MachO::nlist_64) :
456 sizeof(MachO::nlist);
457 Symb.p += SymbolTableEntrySize;
460 error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
461 StringRef &Res) const {
462 StringRef StringTable = getStringTableData();
463 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
464 const char *Start = &StringTable.data()[Entry.n_strx];
465 Res = StringRef(Start);
466 return object_error::success;
469 error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
470 uint64_t &Res) const {
472 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
475 MachO::nlist Entry = getSymbolTableEntry(Symb);
478 return object_error::success;
482 MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb,
483 uint64_t &Res) const {
484 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
485 getSymbolAddress(Symb, Res);
489 SecRel.d.a = Entry.n_sect-1;
491 MachO::section_64 Sec = getSection64(SecRel);
492 Delta = Sec.offset - Sec.addr;
494 MachO::section Sec = getSection(SecRel);
495 Delta = Sec.offset - Sec.addr;
501 return object_error::success;
504 error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
505 uint32_t &Result) const {
506 uint32_t flags = getSymbolFlags(DRI);
507 if (flags & SymbolRef::SF_Common) {
508 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
509 Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
513 return object_error::success;
516 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
517 uint64_t &Result) const {
518 uint64_t BeginOffset;
519 uint64_t EndOffset = 0;
520 uint8_t SectionIndex;
522 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
524 getSymbolAddress(DRI, Value);
528 SectionIndex = Entry.n_sect;
530 uint32_t flags = getSymbolFlags(DRI);
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
539 for (const SymbolRef &Symbol : symbols()) {
540 DataRefImpl DRI = Symbol.getRawDataRefImpl();
541 Entry = getSymbolTableEntryBase(this, DRI);
542 getSymbolAddress(DRI, Value);
543 if (Entry.n_sect == SectionIndex && Value > BeginOffset)
544 if (!EndOffset || Value < EndOffset)
550 Sec.d.a = SectionIndex-1;
551 getSectionSize(Sec, Size);
552 getSectionAddress(Sec, EndOffset);
555 Result = EndOffset - BeginOffset;
556 return object_error::success;
559 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
560 SymbolRef::Type &Res) const {
561 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
562 uint8_t n_type = Entry.n_type;
564 Res = SymbolRef::ST_Other;
566 // If this is a STAB debugging symbol, we can do nothing more.
567 if (n_type & MachO::N_STAB) {
568 Res = SymbolRef::ST_Debug;
569 return object_error::success;
572 switch (n_type & MachO::N_TYPE) {
574 Res = SymbolRef::ST_Unknown;
577 Res = SymbolRef::ST_Function;
580 return object_error::success;
583 uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
584 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
586 uint8_t MachOType = Entry.n_type;
587 uint16_t MachOFlags = Entry.n_desc;
589 uint32_t Result = SymbolRef::SF_None;
591 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
592 Result |= SymbolRef::SF_Undefined;
594 if (MachOType & MachO::N_STAB)
595 Result |= SymbolRef::SF_FormatSpecific;
597 if (MachOType & MachO::N_EXT) {
598 Result |= SymbolRef::SF_Global;
599 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
601 getSymbolAddress(DRI, Value);
603 Result |= SymbolRef::SF_Common;
607 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
608 Result |= SymbolRef::SF_Weak;
610 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
611 Result |= SymbolRef::SF_Absolute;
617 MachOObjectFile::getSymbolSection(DataRefImpl Symb,
618 section_iterator &Res) const {
619 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
620 uint8_t index = Entry.n_sect;
627 Res = section_iterator(SectionRef(DRI, this));
630 return object_error::success;
633 error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
634 uint64_t &Val) const {
635 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
638 void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
643 MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
644 ArrayRef<char> Raw = getSectionRawName(Sec);
645 Result = parseSegmentOrSectionName(Raw.data());
646 return object_error::success;
650 MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
652 MachO::section_64 Sect = getSection64(Sec);
655 MachO::section Sect = getSection(Sec);
658 return object_error::success;
662 MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
664 MachO::section_64 Sect = getSection64(Sec);
667 MachO::section Sect = getSection(Sec);
671 return object_error::success;
675 MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
680 MachO::section_64 Sect = getSection64(Sec);
681 Offset = Sect.offset;
684 MachO::section Sect = getSection(Sec);
685 Offset = Sect.offset;
689 Res = this->getData().substr(Offset, Size);
690 return object_error::success;
694 MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
697 MachO::section_64 Sect = getSection64(Sec);
700 MachO::section Sect = getSection(Sec);
704 Res = uint64_t(1) << Align;
705 return object_error::success;
709 MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
710 uint32_t Flags = getSectionFlags(this, Sec);
711 Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
712 return object_error::success;
715 error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const {
716 // FIXME: Unimplemented.
718 return object_error::success;
721 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const {
722 // FIXME: Unimplemented.
724 return object_error::success;
728 MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
729 bool &Result) const {
730 // FIXME: Unimplemented.
732 return object_error::success;
735 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
736 bool &Result) const {
737 // FIXME: Unimplemented.
739 return object_error::success;
743 MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
744 uint32_t Flags = getSectionFlags(this, Sec);
745 unsigned SectionType = Flags & MachO::SECTION_TYPE;
746 Res = SectionType == MachO::S_ZEROFILL ||
747 SectionType == MachO::S_GB_ZEROFILL;
748 return object_error::success;
751 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
752 bool &Result) const {
753 // Consider using the code from isSectionText to look for __const sections.
754 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
755 // to use section attributes to distinguish code from data.
757 // FIXME: Unimplemented.
759 return object_error::success;
763 MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
764 bool &Result) const {
766 this->getSymbolType(Symb, ST);
767 if (ST == SymbolRef::ST_Unknown) {
769 return object_error::success;
772 uint64_t SectBegin, SectEnd;
773 getSectionAddress(Sec, SectBegin);
774 getSectionSize(Sec, SectEnd);
775 SectEnd += SectBegin;
778 getSymbolAddress(Symb, SymAddr);
779 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
781 return object_error::success;
784 relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
787 MachO::section_64 Sect = getSection64(Sec);
788 Offset = Sect.reloff;
790 MachO::section Sect = getSection(Sec);
791 Offset = Sect.reloff;
795 Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
796 return relocation_iterator(RelocationRef(Ret, this));
800 MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
804 MachO::section_64 Sect = getSection64(Sec);
805 Offset = Sect.reloff;
808 MachO::section Sect = getSection(Sec);
809 Offset = Sect.reloff;
813 const MachO::any_relocation_info *P =
814 reinterpret_cast<const MachO::any_relocation_info *>(getPtr(this, Offset));
817 Ret.p = reinterpret_cast<uintptr_t>(P + Num);
818 return relocation_iterator(RelocationRef(Ret, this));
821 bool MachOObjectFile::section_rel_empty(DataRefImpl Sec) const {
823 MachO::section_64 Sect = getSection64(Sec);
824 return Sect.nreloc == 0;
826 MachO::section Sect = getSection(Sec);
827 return Sect.nreloc == 0;
831 void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
832 const MachO::any_relocation_info *P =
833 reinterpret_cast<const MachO::any_relocation_info *>(Rel.p);
834 Rel.p = reinterpret_cast<uintptr_t>(P + 1);
838 MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
839 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile");
842 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
843 uint64_t &Res) const {
844 MachO::any_relocation_info RE = getRelocation(Rel);
845 Res = getAnyRelocationAddress(RE);
846 return object_error::success;
850 MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
851 MachO::any_relocation_info RE = getRelocation(Rel);
852 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
853 bool isExtern = getPlainRelocationExternal(RE);
857 MachO::symtab_command S = getSymtabLoadCommand();
858 unsigned SymbolTableEntrySize = is64Bit() ?
859 sizeof(MachO::nlist_64) :
860 sizeof(MachO::nlist);
861 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
863 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
864 return symbol_iterator(SymbolRef(Sym, this));
867 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
868 uint64_t &Res) const {
869 MachO::any_relocation_info RE = getRelocation(Rel);
870 Res = getAnyRelocationType(RE);
871 return object_error::success;
875 MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
876 SmallVectorImpl<char> &Result) const {
879 getRelocationType(Rel, RType);
881 unsigned Arch = this->getArch();
885 static const char *const Table[] = {
886 "GENERIC_RELOC_VANILLA",
887 "GENERIC_RELOC_PAIR",
888 "GENERIC_RELOC_SECTDIFF",
889 "GENERIC_RELOC_PB_LA_PTR",
890 "GENERIC_RELOC_LOCAL_SECTDIFF",
891 "GENERIC_RELOC_TLV" };
899 case Triple::x86_64: {
900 static const char *const Table[] = {
901 "X86_64_RELOC_UNSIGNED",
902 "X86_64_RELOC_SIGNED",
903 "X86_64_RELOC_BRANCH",
904 "X86_64_RELOC_GOT_LOAD",
906 "X86_64_RELOC_SUBTRACTOR",
907 "X86_64_RELOC_SIGNED_1",
908 "X86_64_RELOC_SIGNED_2",
909 "X86_64_RELOC_SIGNED_4",
910 "X86_64_RELOC_TLV" };
919 static const char *const Table[] = {
922 "ARM_RELOC_SECTDIFF",
923 "ARM_RELOC_LOCAL_SECTDIFF",
924 "ARM_RELOC_PB_LA_PTR",
926 "ARM_THUMB_RELOC_BR22",
927 "ARM_THUMB_32BIT_BRANCH",
929 "ARM_RELOC_HALF_SECTDIFF" };
938 static const char *const Table[] = {
947 "PPC_RELOC_SECTDIFF",
948 "PPC_RELOC_PB_LA_PTR",
949 "PPC_RELOC_HI16_SECTDIFF",
950 "PPC_RELOC_LO16_SECTDIFF",
951 "PPC_RELOC_HA16_SECTDIFF",
953 "PPC_RELOC_LO14_SECTDIFF",
954 "PPC_RELOC_LOCAL_SECTDIFF" };
962 case Triple::UnknownArch:
966 Result.append(res.begin(), res.end());
967 return object_error::success;
971 MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
972 SmallVectorImpl<char> &Result) const {
973 MachO::any_relocation_info RE = getRelocation(Rel);
975 unsigned Arch = this->getArch();
978 raw_string_ostream fmt(fmtbuf);
979 unsigned Type = this->getAnyRelocationType(RE);
980 bool IsPCRel = this->getAnyRelocationPCRel(RE);
982 // Determine any addends that should be displayed with the relocation.
983 // These require decoding the relocation type, which is triple-specific.
985 // X86_64 has entirely custom relocation types.
986 if (Arch == Triple::x86_64) {
987 bool isPCRel = getAnyRelocationPCRel(RE);
990 case MachO::X86_64_RELOC_GOT_LOAD:
991 case MachO::X86_64_RELOC_GOT: {
992 printRelocationTargetName(this, RE, fmt);
994 if (isPCRel) fmt << "PCREL";
997 case MachO::X86_64_RELOC_SUBTRACTOR: {
998 DataRefImpl RelNext = Rel;
1000 MachO::any_relocation_info RENext = getRelocation(RelNext);
1002 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
1003 // X86_64_RELOC_UNSIGNED.
1004 // NOTE: Scattered relocations don't exist on x86_64.
1005 unsigned RType = getAnyRelocationType(RENext);
1006 if (RType != MachO::X86_64_RELOC_UNSIGNED)
1007 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1008 "X86_64_RELOC_SUBTRACTOR.");
1010 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
1011 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
1012 printRelocationTargetName(this, RENext, fmt);
1014 printRelocationTargetName(this, RE, fmt);
1017 case MachO::X86_64_RELOC_TLV:
1018 printRelocationTargetName(this, RE, fmt);
1020 if (isPCRel) fmt << "P";
1022 case MachO::X86_64_RELOC_SIGNED_1:
1023 printRelocationTargetName(this, RE, fmt);
1026 case MachO::X86_64_RELOC_SIGNED_2:
1027 printRelocationTargetName(this, RE, fmt);
1030 case MachO::X86_64_RELOC_SIGNED_4:
1031 printRelocationTargetName(this, RE, fmt);
1035 printRelocationTargetName(this, RE, fmt);
1038 // X86 and ARM share some relocation types in common.
1039 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
1040 Arch == Triple::ppc) {
1041 // Generic relocation types...
1043 case MachO::GENERIC_RELOC_PAIR: // prints no info
1044 return object_error::success;
1045 case MachO::GENERIC_RELOC_SECTDIFF: {
1046 DataRefImpl RelNext = Rel;
1048 MachO::any_relocation_info RENext = getRelocation(RelNext);
1050 // X86 sect diff's must be followed by a relocation of type
1051 // GENERIC_RELOC_PAIR.
1052 unsigned RType = getAnyRelocationType(RENext);
1054 if (RType != MachO::GENERIC_RELOC_PAIR)
1055 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1056 "GENERIC_RELOC_SECTDIFF.");
1058 printRelocationTargetName(this, RE, fmt);
1060 printRelocationTargetName(this, RENext, fmt);
1065 if (Arch == Triple::x86 || Arch == Triple::ppc) {
1067 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
1068 DataRefImpl RelNext = Rel;
1070 MachO::any_relocation_info RENext = getRelocation(RelNext);
1072 // X86 sect diff's must be followed by a relocation of type
1073 // GENERIC_RELOC_PAIR.
1074 unsigned RType = getAnyRelocationType(RENext);
1075 if (RType != MachO::GENERIC_RELOC_PAIR)
1076 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1077 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1079 printRelocationTargetName(this, RE, fmt);
1081 printRelocationTargetName(this, RENext, fmt);
1084 case MachO::GENERIC_RELOC_TLV: {
1085 printRelocationTargetName(this, RE, fmt);
1087 if (IsPCRel) fmt << "P";
1091 printRelocationTargetName(this, RE, fmt);
1093 } else { // ARM-specific relocations
1095 case MachO::ARM_RELOC_HALF:
1096 case MachO::ARM_RELOC_HALF_SECTDIFF: {
1097 // Half relocations steal a bit from the length field to encode
1098 // whether this is an upper16 or a lower16 relocation.
1099 bool isUpper = getAnyRelocationLength(RE) >> 1;
1102 fmt << ":upper16:(";
1104 fmt << ":lower16:(";
1105 printRelocationTargetName(this, RE, fmt);
1107 DataRefImpl RelNext = Rel;
1109 MachO::any_relocation_info RENext = getRelocation(RelNext);
1111 // ARM half relocs must be followed by a relocation of type
1113 unsigned RType = getAnyRelocationType(RENext);
1114 if (RType != MachO::ARM_RELOC_PAIR)
1115 report_fatal_error("Expected ARM_RELOC_PAIR after "
1118 // NOTE: The half of the target virtual address is stashed in the
1119 // address field of the secondary relocation, but we can't reverse
1120 // engineer the constant offset from it without decoding the movw/movt
1121 // instruction to find the other half in its immediate field.
1123 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1124 // symbol/section pointer of the follow-on relocation.
1125 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1127 printRelocationTargetName(this, RENext, fmt);
1134 printRelocationTargetName(this, RE, fmt);
1139 printRelocationTargetName(this, RE, fmt);
1142 Result.append(fmtbuf.begin(), fmtbuf.end());
1143 return object_error::success;
1147 MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
1148 unsigned Arch = getArch();
1150 getRelocationType(Rel, Type);
1154 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1155 // is always hidden.
1156 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
1157 if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
1158 } else if (Arch == Triple::x86_64) {
1159 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1160 // an X86_64_RELOC_SUBTRACTOR.
1161 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
1162 DataRefImpl RelPrev = Rel;
1165 getRelocationType(RelPrev, PrevType);
1166 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
1171 return object_error::success;
1174 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1175 LibraryRef &Res) const {
1176 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1179 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1180 StringRef &Res) const {
1181 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1184 basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const {
1187 return basic_symbol_iterator(SymbolRef(DRI, this));
1189 MachO::symtab_command Symtab = getSymtabLoadCommand();
1190 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
1191 return basic_symbol_iterator(SymbolRef(DRI, this));
1194 basic_symbol_iterator MachOObjectFile::symbol_end_impl() const {
1197 return basic_symbol_iterator(SymbolRef(DRI, this));
1199 MachO::symtab_command Symtab = getSymtabLoadCommand();
1200 unsigned SymbolTableEntrySize = is64Bit() ?
1201 sizeof(MachO::nlist_64) :
1202 sizeof(MachO::nlist);
1203 unsigned Offset = Symtab.symoff +
1204 Symtab.nsyms * SymbolTableEntrySize;
1205 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1206 return basic_symbol_iterator(SymbolRef(DRI, this));
1209 section_iterator MachOObjectFile::section_begin() const {
1211 return section_iterator(SectionRef(DRI, this));
1214 section_iterator MachOObjectFile::section_end() const {
1216 DRI.d.a = Sections.size();
1217 return section_iterator(SectionRef(DRI, this));
1220 library_iterator MachOObjectFile::needed_library_begin() const {
1222 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1225 library_iterator MachOObjectFile::needed_library_end() const {
1227 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1230 uint8_t MachOObjectFile::getBytesInAddress() const {
1231 return is64Bit() ? 8 : 4;
1234 StringRef MachOObjectFile::getFileFormatName() const {
1235 unsigned CPUType = getCPUType(this);
1238 case llvm::MachO::CPU_TYPE_I386:
1239 return "Mach-O 32-bit i386";
1240 case llvm::MachO::CPU_TYPE_ARM:
1241 return "Mach-O arm";
1242 case llvm::MachO::CPU_TYPE_POWERPC:
1243 return "Mach-O 32-bit ppc";
1245 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
1246 "64-bit object file when we're not 64-bit?");
1247 return "Mach-O 32-bit unknown";
1251 // Make sure the cpu type has the correct mask.
1252 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
1253 == llvm::MachO::CPU_ARCH_ABI64 &&
1254 "32-bit object file when we're 64-bit?");
1257 case llvm::MachO::CPU_TYPE_X86_64:
1258 return "Mach-O 64-bit x86-64";
1259 case llvm::MachO::CPU_TYPE_POWERPC64:
1260 return "Mach-O 64-bit ppc64";
1262 return "Mach-O 64-bit unknown";
1266 Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1268 case llvm::MachO::CPU_TYPE_I386:
1270 case llvm::MachO::CPU_TYPE_X86_64:
1271 return Triple::x86_64;
1272 case llvm::MachO::CPU_TYPE_ARM:
1274 case llvm::MachO::CPU_TYPE_POWERPC:
1276 case llvm::MachO::CPU_TYPE_POWERPC64:
1277 return Triple::ppc64;
1279 return Triple::UnknownArch;
1283 unsigned MachOObjectFile::getArch() const {
1284 return getArch(getCPUType(this));
1287 StringRef MachOObjectFile::getLoadName() const {
1289 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
1292 relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
1295 return section_rel_begin(DRI);
1298 relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
1301 return section_rel_end(DRI);
1304 dice_iterator MachOObjectFile::begin_dices() const {
1306 if (!DataInCodeLoadCmd)
1307 return dice_iterator(DiceRef(DRI, this));
1309 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1310 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
1311 return dice_iterator(DiceRef(DRI, this));
1314 dice_iterator MachOObjectFile::end_dices() const {
1316 if (!DataInCodeLoadCmd)
1317 return dice_iterator(DiceRef(DRI, this));
1319 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1320 unsigned Offset = DicLC.dataoff + DicLC.datasize;
1321 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1322 return dice_iterator(DiceRef(DRI, this));
1326 MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1327 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1328 return parseSegmentOrSectionName(Raw.data());
1332 MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
1333 const section_base *Base =
1334 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1335 return ArrayRef<char>(Base->sectname);
1339 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
1340 const section_base *Base =
1341 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1342 return ArrayRef<char>(Base->segname);
1346 MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
1348 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
1350 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
1353 unsigned MachOObjectFile::getPlainRelocationSymbolNum(
1354 const MachO::any_relocation_info &RE) const {
1355 if (isLittleEndian())
1356 return RE.r_word1 & 0xffffff;
1357 return RE.r_word1 >> 8;
1360 bool MachOObjectFile::getPlainRelocationExternal(
1361 const MachO::any_relocation_info &RE) const {
1362 if (isLittleEndian())
1363 return (RE.r_word1 >> 27) & 1;
1364 return (RE.r_word1 >> 4) & 1;
1367 bool MachOObjectFile::getScatteredRelocationScattered(
1368 const MachO::any_relocation_info &RE) const {
1369 return RE.r_word0 >> 31;
1372 uint32_t MachOObjectFile::getScatteredRelocationValue(
1373 const MachO::any_relocation_info &RE) const {
1377 unsigned MachOObjectFile::getAnyRelocationAddress(
1378 const MachO::any_relocation_info &RE) const {
1379 if (isRelocationScattered(RE))
1380 return getScatteredRelocationAddress(RE);
1381 return getPlainRelocationAddress(RE);
1384 unsigned MachOObjectFile::getAnyRelocationPCRel(
1385 const MachO::any_relocation_info &RE) const {
1386 if (isRelocationScattered(RE))
1387 return getScatteredRelocationPCRel(this, RE);
1388 return getPlainRelocationPCRel(this, RE);
1391 unsigned MachOObjectFile::getAnyRelocationLength(
1392 const MachO::any_relocation_info &RE) const {
1393 if (isRelocationScattered(RE))
1394 return getScatteredRelocationLength(RE);
1395 return getPlainRelocationLength(this, RE);
1399 MachOObjectFile::getAnyRelocationType(
1400 const MachO::any_relocation_info &RE) const {
1401 if (isRelocationScattered(RE))
1402 return getScatteredRelocationType(RE);
1403 return getPlainRelocationType(this, RE);
1407 MachOObjectFile::getRelocationSection(
1408 const MachO::any_relocation_info &RE) const {
1409 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
1410 return *section_end();
1411 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1414 return SectionRef(DRI, this);
1417 MachOObjectFile::LoadCommandInfo
1418 MachOObjectFile::getFirstLoadCommandInfo() const {
1419 MachOObjectFile::LoadCommandInfo Load;
1421 unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
1422 sizeof(MachO::mach_header);
1423 Load.Ptr = getPtr(this, HeaderSize);
1424 Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
1428 MachOObjectFile::LoadCommandInfo
1429 MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1430 MachOObjectFile::LoadCommandInfo Next;
1431 Next.Ptr = L.Ptr + L.C.cmdsize;
1432 Next.C = getStruct<MachO::load_command>(this, Next.Ptr);
1436 MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
1437 return getStruct<MachO::section>(this, Sections[DRI.d.a]);
1440 MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1441 return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
1444 MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
1445 unsigned Index) const {
1446 const char *Sec = getSectionPtr(this, L, Index);
1447 return getStruct<MachO::section>(this, Sec);
1450 MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1451 unsigned Index) const {
1452 const char *Sec = getSectionPtr(this, L, Index);
1453 return getStruct<MachO::section_64>(this, Sec);
1457 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
1458 const char *P = reinterpret_cast<const char *>(DRI.p);
1459 return getStruct<MachO::nlist>(this, P);
1463 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
1464 const char *P = reinterpret_cast<const char *>(DRI.p);
1465 return getStruct<MachO::nlist_64>(this, P);
1468 MachO::linkedit_data_command
1469 MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
1470 return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
1473 MachO::segment_command
1474 MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
1475 return getStruct<MachO::segment_command>(this, L.Ptr);
1478 MachO::segment_command_64
1479 MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
1480 return getStruct<MachO::segment_command_64>(this, L.Ptr);
1483 MachO::linker_options_command
1484 MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
1485 return getStruct<MachO::linker_options_command>(this, L.Ptr);
1488 MachO::version_min_command
1489 MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
1490 return getStruct<MachO::version_min_command>(this, L.Ptr);
1493 MachO::any_relocation_info
1494 MachOObjectFile::getRelocation(DataRefImpl Rel) const {
1495 const char *P = reinterpret_cast<const char *>(Rel.p);
1496 return getStruct<MachO::any_relocation_info>(this, P);
1499 MachO::data_in_code_entry
1500 MachOObjectFile::getDice(DataRefImpl Rel) const {
1501 const char *P = reinterpret_cast<const char *>(Rel.p);
1502 return getStruct<MachO::data_in_code_entry>(this, P);
1505 MachO::mach_header MachOObjectFile::getHeader() const {
1506 return getStruct<MachO::mach_header>(this, getPtr(this, 0));
1509 MachO::mach_header_64 MachOObjectFile::getHeader64() const {
1510 return getStruct<MachO::mach_header_64>(this, getPtr(this, 0));
1513 uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
1514 const MachO::dysymtab_command &DLC,
1515 unsigned Index) const {
1516 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
1517 return getStruct<uint32_t>(this, getPtr(this, Offset));
1520 MachO::data_in_code_entry
1521 MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1522 unsigned Index) const {
1523 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
1524 return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
1527 MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
1528 return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
1531 MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
1532 return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
1535 MachO::linkedit_data_command
1536 MachOObjectFile::getDataInCodeLoadCommand() const {
1537 if (DataInCodeLoadCmd)
1538 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
1540 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
1541 MachO::linkedit_data_command Cmd;
1542 Cmd.cmd = MachO::LC_DATA_IN_CODE;
1543 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
1549 StringRef MachOObjectFile::getStringTableData() const {
1550 MachO::symtab_command S = getSymtabLoadCommand();
1551 return getData().substr(S.stroff, S.strsize);
1554 bool MachOObjectFile::is64Bit() const {
1555 return getType() == getMachOType(false, true) ||
1556 getType() == getMachOType(true, true);
1559 void MachOObjectFile::ReadULEB128s(uint64_t Index,
1560 SmallVectorImpl<uint64_t> &Out) const {
1561 DataExtractor extractor(ObjectFile::getData(), true, 0);
1563 uint32_t offset = Index;
1565 while (uint64_t delta = extractor.getULEB128(&offset)) {
1567 Out.push_back(data);
1571 ErrorOr<ObjectFile *> ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer,
1573 StringRef Magic = Buffer->getBuffer().slice(0, 4);
1575 std::unique_ptr<MachOObjectFile> Ret;
1576 if (Magic == "\xFE\xED\xFA\xCE")
1577 Ret.reset(new MachOObjectFile(Buffer, false, false, EC, BufferOwned));
1578 else if (Magic == "\xCE\xFA\xED\xFE")
1579 Ret.reset(new MachOObjectFile(Buffer, true, false, EC, BufferOwned));
1580 else if (Magic == "\xFE\xED\xFA\xCF")
1581 Ret.reset(new MachOObjectFile(Buffer, false, true, EC, BufferOwned));
1582 else if (Magic == "\xCF\xFA\xED\xFE")
1583 Ret.reset(new MachOObjectFile(Buffer, true, true, EC, BufferOwned));
1586 return object_error::parse_failed;
1591 return Ret.release();
1594 } // end namespace object
1595 } // end namespace llvm