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::data_in_code_entry &C) {
224 T getStruct(const MachOObjectFile *O, const char *P) {
226 memcpy(&Cmd, P, sizeof(T));
227 if (O->isLittleEndian() != sys::IsLittleEndianHost)
233 getSegmentLoadCommandNumSections(const MachOObjectFile *O,
234 const MachOObjectFile::LoadCommandInfo &L) {
236 MachO::segment_command_64 S = O->getSegment64LoadCommand(L);
239 MachO::segment_command S = O->getSegmentLoadCommand(L);
244 getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
246 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
248 bool Is64 = O->is64Bit();
249 unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
250 sizeof(MachO::segment_command);
251 unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
252 sizeof(MachO::section);
254 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
255 return reinterpret_cast<const char*>(SectionAddr);
258 static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
259 return O->getData().substr(Offset, 1).data();
263 getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
264 const char *P = reinterpret_cast<const char *>(DRI.p);
265 return getStruct<nlist_base>(O, P);
268 static StringRef parseSegmentOrSectionName(const char *P) {
272 // Not null terminated, so this is a 16 char string.
273 return StringRef(P, 16);
276 // Helper to advance a section or symbol iterator multiple increments at a time.
278 static error_code advance(T &it, size_t Val) {
287 static void advanceTo(T &it, size_t Val) {
288 if (error_code ec = advance(it, Val))
289 report_fatal_error(ec.message());
292 static unsigned getCPUType(const MachOObjectFile *O) {
293 return O->getHeader().cputype;
296 static void printRelocationTargetName(const MachOObjectFile *O,
297 const MachO::any_relocation_info &RE,
298 raw_string_ostream &fmt) {
299 bool IsScattered = O->isRelocationScattered(RE);
301 // Target of a scattered relocation is an address. In the interest of
302 // generating pretty output, scan through the symbol table looking for a
303 // symbol that aligns with that address. If we find one, print it.
304 // Otherwise, we just print the hex address of the target.
306 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
309 for (symbol_iterator SI = O->begin_symbols(), SE = O->end_symbols();
310 SI != SE; SI.increment(ec)) {
311 if (ec) report_fatal_error(ec.message());
316 if ((ec = SI->getAddress(Addr)))
317 report_fatal_error(ec.message());
318 if (Addr != Val) continue;
319 if ((ec = SI->getName(Name)))
320 report_fatal_error(ec.message());
325 // If we couldn't find a symbol that this relocation refers to, try
326 // to find a section beginning instead.
327 for (section_iterator SI = O->begin_sections(), SE = O->end_sections();
328 SI != SE; SI.increment(ec)) {
329 if (ec) report_fatal_error(ec.message());
334 if ((ec = SI->getAddress(Addr)))
335 report_fatal_error(ec.message());
336 if (Addr != Val) continue;
337 if ((ec = SI->getName(Name)))
338 report_fatal_error(ec.message());
343 fmt << format("0x%x", Val);
348 bool isExtern = O->getPlainRelocationExternal(RE);
349 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
352 symbol_iterator SI = O->begin_symbols();
356 section_iterator SI = O->begin_sections();
357 // Adjust for the fact that sections are 1-indexed.
358 advanceTo(SI, Val - 1);
366 getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
371 getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
372 return RE.r_word0 & 0xffffff;
375 static bool getPlainRelocationPCRel(const MachOObjectFile *O,
376 const MachO::any_relocation_info &RE) {
377 if (O->isLittleEndian())
378 return (RE.r_word1 >> 24) & 1;
379 return (RE.r_word1 >> 7) & 1;
383 getScatteredRelocationPCRel(const MachOObjectFile *O,
384 const MachO::any_relocation_info &RE) {
385 return (RE.r_word0 >> 30) & 1;
388 static unsigned getPlainRelocationLength(const MachOObjectFile *O,
389 const MachO::any_relocation_info &RE) {
390 if (O->isLittleEndian())
391 return (RE.r_word1 >> 25) & 3;
392 return (RE.r_word1 >> 5) & 3;
396 getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
397 return (RE.r_word0 >> 28) & 3;
400 static unsigned getPlainRelocationType(const MachOObjectFile *O,
401 const MachO::any_relocation_info &RE) {
402 if (O->isLittleEndian())
403 return RE.r_word1 >> 28;
404 return RE.r_word1 & 0xf;
408 getScatteredRelocationType(const MachO::any_relocation_info &RE) {
409 return (RE.r_word0 >> 24) & 0xf;
412 static uint32_t getSectionFlags(const MachOObjectFile *O,
415 MachO::section_64 Sect = O->getSection64(Sec);
418 MachO::section Sect = O->getSection(Sec);
422 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object,
423 bool IsLittleEndian, bool Is64bits,
425 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
426 SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL), DataInCodeLoadCmd(NULL) {
427 uint32_t LoadCommandCount = this->getHeader().ncmds;
428 MachO::LoadCommandType SegmentLoadType = is64Bit() ?
429 MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
431 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
432 for (unsigned I = 0; ; ++I) {
433 if (Load.C.cmd == MachO::LC_SYMTAB) {
434 assert(!SymtabLoadCmd && "Multiple symbol tables");
435 SymtabLoadCmd = Load.Ptr;
436 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
437 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
438 DysymtabLoadCmd = Load.Ptr;
439 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
440 assert(!DataInCodeLoadCmd && "Multiple data in code tables");
441 DataInCodeLoadCmd = Load.Ptr;
442 } else if (Load.C.cmd == SegmentLoadType) {
443 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
444 for (unsigned J = 0; J < NumSections; ++J) {
445 const char *Sec = getSectionPtr(this, Load, J);
446 Sections.push_back(Sec);
450 if (I == LoadCommandCount - 1)
453 Load = getNextLoadCommandInfo(Load);
457 error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb,
458 SymbolRef &Res) const {
459 unsigned SymbolTableEntrySize = is64Bit() ?
460 sizeof(MachO::nlist_64) :
461 sizeof(MachO::nlist);
462 Symb.p += SymbolTableEntrySize;
463 Res = SymbolRef(Symb, this);
464 return object_error::success;
467 error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
468 StringRef &Res) const {
469 StringRef StringTable = getStringTableData();
470 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
471 const char *Start = &StringTable.data()[Entry.n_strx];
472 Res = StringRef(Start);
473 return object_error::success;
476 error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
477 uint64_t &Res) const {
479 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
482 MachO::nlist Entry = getSymbolTableEntry(Symb);
485 return object_error::success;
489 MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb,
490 uint64_t &Res) const {
491 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
492 getSymbolAddress(Symb, Res);
496 SecRel.d.a = Entry.n_sect-1;
498 MachO::section_64 Sec = getSection64(SecRel);
499 Delta = Sec.offset - Sec.addr;
501 MachO::section Sec = getSection(SecRel);
502 Delta = Sec.offset - Sec.addr;
508 return object_error::success;
511 error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
512 uint32_t &Result) const {
514 this->getSymbolFlags(DRI, flags);
515 if (flags & SymbolRef::SF_Common) {
516 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
517 Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
521 return object_error::success;
524 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
525 uint64_t &Result) const {
526 uint64_t BeginOffset;
527 uint64_t EndOffset = 0;
528 uint8_t SectionIndex;
530 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
532 getSymbolAddress(DRI, Value);
536 SectionIndex = Entry.n_sect;
538 uint32_t flags = SymbolRef::SF_None;
539 this->getSymbolFlags(DRI, flags);
540 if (flags & SymbolRef::SF_Common)
543 Result = UnknownAddressOrSize;
544 return object_error::success;
546 // Unfortunately symbols are unsorted so we need to touch all
547 // symbols from load command
549 for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E;
551 DataRefImpl DRI = I->getRawDataRefImpl();
552 Entry = getSymbolTableEntryBase(this, DRI);
553 getSymbolAddress(DRI, Value);
554 if (Entry.n_sect == SectionIndex && Value > BeginOffset)
555 if (!EndOffset || Value < EndOffset)
561 Sec.d.a = SectionIndex-1;
562 getSectionSize(Sec, Size);
563 getSectionAddress(Sec, EndOffset);
566 Result = EndOffset - BeginOffset;
567 return object_error::success;
570 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
571 SymbolRef::Type &Res) const {
572 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
573 uint8_t n_type = Entry.n_type;
575 Res = SymbolRef::ST_Other;
577 // If this is a STAB debugging symbol, we can do nothing more.
578 if (n_type & MachO::N_STAB) {
579 Res = SymbolRef::ST_Debug;
580 return object_error::success;
583 switch (n_type & MachO::N_TYPE) {
585 Res = SymbolRef::ST_Unknown;
588 Res = SymbolRef::ST_Function;
591 return object_error::success;
594 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
596 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
597 uint8_t NType = Entry.n_type;
600 switch (NType & MachO::N_TYPE) {
607 case MachO::N_SECT: {
608 section_iterator Sec = end_sections();
609 getSymbolSection(Symb, Sec);
610 DataRefImpl Ref = Sec->getRawDataRefImpl();
611 StringRef SectionName;
612 getSectionName(Ref, SectionName);
613 StringRef SegmentName = getSectionFinalSegmentName(Ref);
614 if (SegmentName == "__TEXT" && SectionName == "__text")
625 if (NType & (MachO::N_EXT | MachO::N_PEXT))
626 Char = toupper(static_cast<unsigned char>(Char));
628 return object_error::success;
631 error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
632 uint32_t &Result) const {
633 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
635 uint8_t MachOType = Entry.n_type;
636 uint16_t MachOFlags = Entry.n_desc;
638 // TODO: Correctly set SF_ThreadLocal
639 Result = SymbolRef::SF_None;
641 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
642 Result |= SymbolRef::SF_Undefined;
644 if (MachOType & MachO::N_STAB)
645 Result |= SymbolRef::SF_FormatSpecific;
647 if (MachOType & MachO::N_EXT) {
648 Result |= SymbolRef::SF_Global;
649 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
651 getSymbolAddress(DRI, Value);
653 Result |= SymbolRef::SF_Common;
657 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
658 Result |= SymbolRef::SF_Weak;
660 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
661 Result |= SymbolRef::SF_Absolute;
663 return object_error::success;
667 MachOObjectFile::getSymbolSection(DataRefImpl Symb,
668 section_iterator &Res) const {
669 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
670 uint8_t index = Entry.n_sect;
673 Res = end_sections();
677 Res = section_iterator(SectionRef(DRI, this));
680 return object_error::success;
683 error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
684 uint64_t &Val) const {
685 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
688 error_code MachOObjectFile::getSectionNext(DataRefImpl Sec,
689 SectionRef &Res) const {
691 Res = SectionRef(Sec, this);
692 return object_error::success;
696 MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
697 ArrayRef<char> Raw = getSectionRawName(Sec);
698 Result = parseSegmentOrSectionName(Raw.data());
699 return object_error::success;
703 MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
705 MachO::section_64 Sect = getSection64(Sec);
708 MachO::section Sect = getSection(Sec);
711 return object_error::success;
715 MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
717 MachO::section_64 Sect = getSection64(Sec);
720 MachO::section Sect = getSection(Sec);
724 return object_error::success;
728 MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
733 MachO::section_64 Sect = getSection64(Sec);
734 Offset = Sect.offset;
737 MachO::section Sect = getSection(Sec);
738 Offset = Sect.offset;
742 Res = this->getData().substr(Offset, Size);
743 return object_error::success;
747 MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
750 MachO::section_64 Sect = getSection64(Sec);
753 MachO::section Sect = getSection(Sec);
757 Res = uint64_t(1) << Align;
758 return object_error::success;
762 MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
763 uint32_t Flags = getSectionFlags(this, Sec);
764 Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
765 return object_error::success;
768 error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const {
769 // FIXME: Unimplemented.
771 return object_error::success;
774 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const {
775 // FIXME: Unimplemented.
777 return object_error::success;
781 MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
782 bool &Result) const {
783 // FIXME: Unimplemented.
785 return object_error::success;
788 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
789 bool &Result) const {
790 // FIXME: Unimplemented.
792 return object_error::success;
796 MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
797 uint32_t Flags = getSectionFlags(this, Sec);
798 unsigned SectionType = Flags & MachO::SECTION_TYPE;
799 Res = SectionType == MachO::S_ZEROFILL ||
800 SectionType == MachO::S_GB_ZEROFILL;
801 return object_error::success;
804 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
805 bool &Result) const {
806 // Consider using the code from isSectionText to look for __const sections.
807 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
808 // to use section attributes to distinguish code from data.
810 // FIXME: Unimplemented.
812 return object_error::success;
816 MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
817 bool &Result) const {
819 this->getSymbolType(Symb, ST);
820 if (ST == SymbolRef::ST_Unknown) {
822 return object_error::success;
825 uint64_t SectBegin, SectEnd;
826 getSectionAddress(Sec, SectBegin);
827 getSectionSize(Sec, SectEnd);
828 SectEnd += SectBegin;
831 getSymbolAddress(Symb, SymAddr);
832 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
834 return object_error::success;
837 relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
840 MachO::section_64 Sect = getSection64(Sec);
841 Offset = Sect.reloff;
843 MachO::section Sect = getSection(Sec);
844 Offset = Sect.reloff;
848 Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
849 return relocation_iterator(RelocationRef(Ret, this));
853 MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
857 MachO::section_64 Sect = getSection64(Sec);
858 Offset = Sect.reloff;
861 MachO::section Sect = getSection(Sec);
862 Offset = Sect.reloff;
866 const MachO::any_relocation_info *P =
867 reinterpret_cast<const MachO::any_relocation_info *>(getPtr(this, Offset));
870 Ret.p = reinterpret_cast<uintptr_t>(P + Num);
871 return relocation_iterator(RelocationRef(Ret, this));
874 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
875 RelocationRef &Res) const {
876 const MachO::any_relocation_info *P =
877 reinterpret_cast<const MachO::any_relocation_info *>(Rel.p);
878 Rel.p = reinterpret_cast<uintptr_t>(P + 1);
879 Res = RelocationRef(Rel, this);
880 return object_error::success;
884 MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
885 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile");
888 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
889 uint64_t &Res) const {
890 MachO::any_relocation_info RE = getRelocation(Rel);
891 Res = getAnyRelocationAddress(RE);
892 return object_error::success;
896 MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
897 MachO::any_relocation_info RE = getRelocation(Rel);
898 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
899 bool isExtern = getPlainRelocationExternal(RE);
901 return end_symbols();
903 MachO::symtab_command S = getSymtabLoadCommand();
904 unsigned SymbolTableEntrySize = is64Bit() ?
905 sizeof(MachO::nlist_64) :
906 sizeof(MachO::nlist);
907 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
909 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
910 return symbol_iterator(SymbolRef(Sym, this));
913 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
914 uint64_t &Res) const {
915 MachO::any_relocation_info RE = getRelocation(Rel);
916 Res = getAnyRelocationType(RE);
917 return object_error::success;
921 MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
922 SmallVectorImpl<char> &Result) const {
925 getRelocationType(Rel, RType);
927 unsigned Arch = this->getArch();
931 static const char *const Table[] = {
932 "GENERIC_RELOC_VANILLA",
933 "GENERIC_RELOC_PAIR",
934 "GENERIC_RELOC_SECTDIFF",
935 "GENERIC_RELOC_PB_LA_PTR",
936 "GENERIC_RELOC_LOCAL_SECTDIFF",
937 "GENERIC_RELOC_TLV" };
945 case Triple::x86_64: {
946 static const char *const Table[] = {
947 "X86_64_RELOC_UNSIGNED",
948 "X86_64_RELOC_SIGNED",
949 "X86_64_RELOC_BRANCH",
950 "X86_64_RELOC_GOT_LOAD",
952 "X86_64_RELOC_SUBTRACTOR",
953 "X86_64_RELOC_SIGNED_1",
954 "X86_64_RELOC_SIGNED_2",
955 "X86_64_RELOC_SIGNED_4",
956 "X86_64_RELOC_TLV" };
965 static const char *const Table[] = {
968 "ARM_RELOC_SECTDIFF",
969 "ARM_RELOC_LOCAL_SECTDIFF",
970 "ARM_RELOC_PB_LA_PTR",
972 "ARM_THUMB_RELOC_BR22",
973 "ARM_THUMB_32BIT_BRANCH",
975 "ARM_RELOC_HALF_SECTDIFF" };
984 static const char *const Table[] = {
993 "PPC_RELOC_SECTDIFF",
994 "PPC_RELOC_PB_LA_PTR",
995 "PPC_RELOC_HI16_SECTDIFF",
996 "PPC_RELOC_LO16_SECTDIFF",
997 "PPC_RELOC_HA16_SECTDIFF",
999 "PPC_RELOC_LO14_SECTDIFF",
1000 "PPC_RELOC_LOCAL_SECTDIFF" };
1005 case Triple::UnknownArch:
1009 Result.append(res.begin(), res.end());
1010 return object_error::success;
1014 MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1015 SmallVectorImpl<char> &Result) const {
1016 MachO::any_relocation_info RE = getRelocation(Rel);
1018 unsigned Arch = this->getArch();
1021 raw_string_ostream fmt(fmtbuf);
1022 unsigned Type = this->getAnyRelocationType(RE);
1023 bool IsPCRel = this->getAnyRelocationPCRel(RE);
1025 // Determine any addends that should be displayed with the relocation.
1026 // These require decoding the relocation type, which is triple-specific.
1028 // X86_64 has entirely custom relocation types.
1029 if (Arch == Triple::x86_64) {
1030 bool isPCRel = getAnyRelocationPCRel(RE);
1033 case MachO::X86_64_RELOC_GOT_LOAD:
1034 case MachO::X86_64_RELOC_GOT: {
1035 printRelocationTargetName(this, RE, fmt);
1037 if (isPCRel) fmt << "PCREL";
1040 case MachO::X86_64_RELOC_SUBTRACTOR: {
1041 DataRefImpl RelNext = Rel;
1043 MachO::any_relocation_info RENext = getRelocation(RelNext);
1045 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
1046 // X86_64_RELOC_UNSIGNED.
1047 // NOTE: Scattered relocations don't exist on x86_64.
1048 unsigned RType = getAnyRelocationType(RENext);
1049 if (RType != MachO::X86_64_RELOC_UNSIGNED)
1050 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1051 "X86_64_RELOC_SUBTRACTOR.");
1053 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
1054 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
1055 printRelocationTargetName(this, RENext, fmt);
1057 printRelocationTargetName(this, RE, fmt);
1060 case MachO::X86_64_RELOC_TLV:
1061 printRelocationTargetName(this, RE, fmt);
1063 if (isPCRel) fmt << "P";
1065 case MachO::X86_64_RELOC_SIGNED_1:
1066 printRelocationTargetName(this, RE, fmt);
1069 case MachO::X86_64_RELOC_SIGNED_2:
1070 printRelocationTargetName(this, RE, fmt);
1073 case MachO::X86_64_RELOC_SIGNED_4:
1074 printRelocationTargetName(this, RE, fmt);
1078 printRelocationTargetName(this, RE, fmt);
1081 // X86 and ARM share some relocation types in common.
1082 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
1083 Arch == Triple::ppc) {
1084 // Generic relocation types...
1086 case MachO::GENERIC_RELOC_PAIR: // prints no info
1087 return object_error::success;
1088 case MachO::GENERIC_RELOC_SECTDIFF: {
1089 DataRefImpl RelNext = Rel;
1091 MachO::any_relocation_info RENext = getRelocation(RelNext);
1093 // X86 sect diff's must be followed by a relocation of type
1094 // GENERIC_RELOC_PAIR.
1095 unsigned RType = getAnyRelocationType(RENext);
1097 if (RType != MachO::GENERIC_RELOC_PAIR)
1098 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1099 "GENERIC_RELOC_SECTDIFF.");
1101 printRelocationTargetName(this, RE, fmt);
1103 printRelocationTargetName(this, RENext, fmt);
1108 if (Arch == Triple::x86 || Arch == Triple::ppc) {
1110 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
1111 DataRefImpl RelNext = Rel;
1113 MachO::any_relocation_info RENext = getRelocation(RelNext);
1115 // X86 sect diff's must be followed by a relocation of type
1116 // GENERIC_RELOC_PAIR.
1117 unsigned RType = getAnyRelocationType(RENext);
1118 if (RType != MachO::GENERIC_RELOC_PAIR)
1119 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1120 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1122 printRelocationTargetName(this, RE, fmt);
1124 printRelocationTargetName(this, RENext, fmt);
1127 case MachO::GENERIC_RELOC_TLV: {
1128 printRelocationTargetName(this, RE, fmt);
1130 if (IsPCRel) fmt << "P";
1134 printRelocationTargetName(this, RE, fmt);
1136 } else { // ARM-specific relocations
1138 case MachO::ARM_RELOC_HALF:
1139 case MachO::ARM_RELOC_HALF_SECTDIFF: {
1140 // Half relocations steal a bit from the length field to encode
1141 // whether this is an upper16 or a lower16 relocation.
1142 bool isUpper = getAnyRelocationLength(RE) >> 1;
1145 fmt << ":upper16:(";
1147 fmt << ":lower16:(";
1148 printRelocationTargetName(this, RE, fmt);
1150 DataRefImpl RelNext = Rel;
1152 MachO::any_relocation_info RENext = getRelocation(RelNext);
1154 // ARM half relocs must be followed by a relocation of type
1156 unsigned RType = getAnyRelocationType(RENext);
1157 if (RType != MachO::ARM_RELOC_PAIR)
1158 report_fatal_error("Expected ARM_RELOC_PAIR after "
1161 // NOTE: The half of the target virtual address is stashed in the
1162 // address field of the secondary relocation, but we can't reverse
1163 // engineer the constant offset from it without decoding the movw/movt
1164 // instruction to find the other half in its immediate field.
1166 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1167 // symbol/section pointer of the follow-on relocation.
1168 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1170 printRelocationTargetName(this, RENext, fmt);
1177 printRelocationTargetName(this, RE, fmt);
1182 printRelocationTargetName(this, RE, fmt);
1185 Result.append(fmtbuf.begin(), fmtbuf.end());
1186 return object_error::success;
1190 MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
1191 unsigned Arch = getArch();
1193 getRelocationType(Rel, Type);
1197 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1198 // is always hidden.
1199 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
1200 if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
1201 } else if (Arch == Triple::x86_64) {
1202 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1203 // an X86_64_RELOC_SUBTRACTOR.
1204 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
1205 DataRefImpl RelPrev = Rel;
1208 getRelocationType(RelPrev, PrevType);
1209 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
1214 return object_error::success;
1217 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1218 LibraryRef &Res) const {
1219 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1222 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1223 StringRef &Res) const {
1224 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1227 symbol_iterator MachOObjectFile::begin_symbols() const {
1230 return symbol_iterator(SymbolRef(DRI, this));
1232 MachO::symtab_command Symtab = getSymtabLoadCommand();
1233 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
1234 return symbol_iterator(SymbolRef(DRI, this));
1237 symbol_iterator MachOObjectFile::end_symbols() const {
1240 return symbol_iterator(SymbolRef(DRI, this));
1242 MachO::symtab_command Symtab = getSymtabLoadCommand();
1243 unsigned SymbolTableEntrySize = is64Bit() ?
1244 sizeof(MachO::nlist_64) :
1245 sizeof(MachO::nlist);
1246 unsigned Offset = Symtab.symoff +
1247 Symtab.nsyms * SymbolTableEntrySize;
1248 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1249 return symbol_iterator(SymbolRef(DRI, this));
1252 symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
1254 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1257 symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
1259 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1262 section_iterator MachOObjectFile::begin_sections() const {
1264 return section_iterator(SectionRef(DRI, this));
1267 section_iterator MachOObjectFile::end_sections() const {
1269 DRI.d.a = Sections.size();
1270 return section_iterator(SectionRef(DRI, this));
1273 library_iterator MachOObjectFile::begin_libraries_needed() const {
1275 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1278 library_iterator MachOObjectFile::end_libraries_needed() const {
1280 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1283 uint8_t MachOObjectFile::getBytesInAddress() const {
1284 return is64Bit() ? 8 : 4;
1287 StringRef MachOObjectFile::getFileFormatName() const {
1288 unsigned CPUType = getCPUType(this);
1291 case llvm::MachO::CPU_TYPE_I386:
1292 return "Mach-O 32-bit i386";
1293 case llvm::MachO::CPU_TYPE_ARM:
1294 return "Mach-O arm";
1295 case llvm::MachO::CPU_TYPE_POWERPC:
1296 return "Mach-O 32-bit ppc";
1298 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
1299 "64-bit object file when we're not 64-bit?");
1300 return "Mach-O 32-bit unknown";
1304 // Make sure the cpu type has the correct mask.
1305 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
1306 == llvm::MachO::CPU_ARCH_ABI64 &&
1307 "32-bit object file when we're 64-bit?");
1310 case llvm::MachO::CPU_TYPE_X86_64:
1311 return "Mach-O 64-bit x86-64";
1312 case llvm::MachO::CPU_TYPE_POWERPC64:
1313 return "Mach-O 64-bit ppc64";
1315 return "Mach-O 64-bit unknown";
1319 Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1321 case llvm::MachO::CPU_TYPE_I386:
1323 case llvm::MachO::CPU_TYPE_X86_64:
1324 return Triple::x86_64;
1325 case llvm::MachO::CPU_TYPE_ARM:
1327 case llvm::MachO::CPU_TYPE_POWERPC:
1329 case llvm::MachO::CPU_TYPE_POWERPC64:
1330 return Triple::ppc64;
1332 return Triple::UnknownArch;
1336 unsigned MachOObjectFile::getArch() const {
1337 return getArch(getCPUType(this));
1340 StringRef MachOObjectFile::getLoadName() const {
1342 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
1345 relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
1348 return section_rel_begin(DRI);
1351 relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
1354 return section_rel_end(DRI);
1357 dice_iterator MachOObjectFile::begin_dices() const {
1359 if (!DataInCodeLoadCmd)
1360 return dice_iterator(DiceRef(DRI, this));
1362 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1363 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
1364 return dice_iterator(DiceRef(DRI, this));
1367 dice_iterator MachOObjectFile::end_dices() const {
1369 if (!DataInCodeLoadCmd)
1370 return dice_iterator(DiceRef(DRI, this));
1372 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1373 unsigned Offset = DicLC.dataoff + DicLC.datasize;
1374 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1375 return dice_iterator(DiceRef(DRI, this));
1379 MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1380 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1381 return parseSegmentOrSectionName(Raw.data());
1385 MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
1386 const section_base *Base =
1387 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1388 return ArrayRef<char>(Base->sectname);
1392 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
1393 const section_base *Base =
1394 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1395 return ArrayRef<char>(Base->segname);
1399 MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
1401 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
1403 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
1406 unsigned MachOObjectFile::getPlainRelocationSymbolNum(
1407 const MachO::any_relocation_info &RE) const {
1408 if (isLittleEndian())
1409 return RE.r_word1 & 0xffffff;
1410 return RE.r_word1 >> 8;
1413 bool MachOObjectFile::getPlainRelocationExternal(
1414 const MachO::any_relocation_info &RE) const {
1415 if (isLittleEndian())
1416 return (RE.r_word1 >> 27) & 1;
1417 return (RE.r_word1 >> 4) & 1;
1420 bool MachOObjectFile::getScatteredRelocationScattered(
1421 const MachO::any_relocation_info &RE) const {
1422 return RE.r_word0 >> 31;
1425 uint32_t MachOObjectFile::getScatteredRelocationValue(
1426 const MachO::any_relocation_info &RE) const {
1430 unsigned MachOObjectFile::getAnyRelocationAddress(
1431 const MachO::any_relocation_info &RE) const {
1432 if (isRelocationScattered(RE))
1433 return getScatteredRelocationAddress(RE);
1434 return getPlainRelocationAddress(RE);
1437 unsigned MachOObjectFile::getAnyRelocationPCRel(
1438 const MachO::any_relocation_info &RE) const {
1439 if (isRelocationScattered(RE))
1440 return getScatteredRelocationPCRel(this, RE);
1441 return getPlainRelocationPCRel(this, RE);
1444 unsigned MachOObjectFile::getAnyRelocationLength(
1445 const MachO::any_relocation_info &RE) const {
1446 if (isRelocationScattered(RE))
1447 return getScatteredRelocationLength(RE);
1448 return getPlainRelocationLength(this, RE);
1452 MachOObjectFile::getAnyRelocationType(
1453 const MachO::any_relocation_info &RE) const {
1454 if (isRelocationScattered(RE))
1455 return getScatteredRelocationType(RE);
1456 return getPlainRelocationType(this, RE);
1460 MachOObjectFile::getRelocationSection(
1461 const MachO::any_relocation_info &RE) const {
1462 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
1463 return *end_sections();
1464 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1467 return SectionRef(DRI, this);
1470 MachOObjectFile::LoadCommandInfo
1471 MachOObjectFile::getFirstLoadCommandInfo() const {
1472 MachOObjectFile::LoadCommandInfo Load;
1474 unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
1475 sizeof(MachO::mach_header);
1476 Load.Ptr = getPtr(this, HeaderSize);
1477 Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
1481 MachOObjectFile::LoadCommandInfo
1482 MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1483 MachOObjectFile::LoadCommandInfo Next;
1484 Next.Ptr = L.Ptr + L.C.cmdsize;
1485 Next.C = getStruct<MachO::load_command>(this, Next.Ptr);
1489 MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
1490 return getStruct<MachO::section>(this, Sections[DRI.d.a]);
1493 MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1494 return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
1497 MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
1498 unsigned Index) const {
1499 const char *Sec = getSectionPtr(this, L, Index);
1500 return getStruct<MachO::section>(this, Sec);
1503 MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1504 unsigned Index) const {
1505 const char *Sec = getSectionPtr(this, L, Index);
1506 return getStruct<MachO::section_64>(this, Sec);
1510 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
1511 const char *P = reinterpret_cast<const char *>(DRI.p);
1512 return getStruct<MachO::nlist>(this, P);
1516 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
1517 const char *P = reinterpret_cast<const char *>(DRI.p);
1518 return getStruct<MachO::nlist_64>(this, P);
1521 MachO::linkedit_data_command
1522 MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
1523 return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
1526 MachO::segment_command
1527 MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
1528 return getStruct<MachO::segment_command>(this, L.Ptr);
1531 MachO::segment_command_64
1532 MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
1533 return getStruct<MachO::segment_command_64>(this, L.Ptr);
1536 MachO::linker_options_command
1537 MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
1538 return getStruct<MachO::linker_options_command>(this, L.Ptr);
1541 MachO::any_relocation_info
1542 MachOObjectFile::getRelocation(DataRefImpl Rel) const {
1543 const char *P = reinterpret_cast<const char *>(Rel.p);
1544 return getStruct<MachO::any_relocation_info>(this, P);
1547 MachO::data_in_code_entry
1548 MachOObjectFile::getDice(DataRefImpl Rel) const {
1549 const char *P = reinterpret_cast<const char *>(Rel.p);
1550 return getStruct<MachO::data_in_code_entry>(this, P);
1553 MachO::mach_header MachOObjectFile::getHeader() const {
1554 return getStruct<MachO::mach_header>(this, getPtr(this, 0));
1557 MachO::mach_header_64 MachOObjectFile::getHeader64() const {
1558 return getStruct<MachO::mach_header_64>(this, getPtr(this, 0));
1561 uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
1562 const MachO::dysymtab_command &DLC,
1563 unsigned Index) const {
1564 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
1565 return getStruct<uint32_t>(this, getPtr(this, Offset));
1568 MachO::data_in_code_entry
1569 MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1570 unsigned Index) const {
1571 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
1572 return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
1575 MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
1576 return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
1579 MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
1580 return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
1583 MachO::linkedit_data_command
1584 MachOObjectFile::getDataInCodeLoadCommand() const {
1585 if (DataInCodeLoadCmd)
1586 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
1588 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
1589 MachO::linkedit_data_command Cmd;
1590 Cmd.cmd = MachO::LC_DATA_IN_CODE;
1591 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
1597 StringRef MachOObjectFile::getStringTableData() const {
1598 MachO::symtab_command S = getSymtabLoadCommand();
1599 return getData().substr(S.stroff, S.strsize);
1602 bool MachOObjectFile::is64Bit() const {
1603 return getType() == getMachOType(false, true) ||
1604 getType() == getMachOType(true, true);
1607 void MachOObjectFile::ReadULEB128s(uint64_t Index,
1608 SmallVectorImpl<uint64_t> &Out) const {
1609 DataExtractor extractor(ObjectFile::getData(), true, 0);
1611 uint32_t offset = Index;
1613 while (uint64_t delta = extractor.getULEB128(&offset)) {
1615 Out.push_back(data);
1619 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
1620 StringRef Magic = Buffer->getBuffer().slice(0, 4);
1622 OwningPtr<ObjectFile> Ret;
1623 if (Magic == "\xFE\xED\xFA\xCE")
1624 Ret.reset(new MachOObjectFile(Buffer, false, false, ec));
1625 else if (Magic == "\xCE\xFA\xED\xFE")
1626 Ret.reset(new MachOObjectFile(Buffer, true, false, ec));
1627 else if (Magic == "\xFE\xED\xFA\xCF")
1628 Ret.reset(new MachOObjectFile(Buffer, false, true, ec));
1629 else if (Magic == "\xCF\xFA\xED\xFE")
1630 Ret.reset(new MachOObjectFile(Buffer, true, true, ec));
1641 } // end namespace object
1642 } // end namespace llvm