1 //===- MachO.h - MachO object file implementation ---------------*- 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 declares the MachOObjectFile class, which binds the MachOObject
11 // class to the generic ObjectFile wrapper.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_OBJECT_MACHO_H
16 #define LLVM_OBJECT_MACHO_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Object/MachOFormat.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/MachO.h"
25 #include "llvm/Support/raw_ostream.h"
30 using support::endianness;
32 template<endianness E, bool B>
34 static const endianness TargetEndianness = E;
35 static const bool Is64Bits = B;
38 template<endianness TargetEndianness>
39 struct MachODataTypeTypedefHelperCommon {
40 typedef support::detail::packed_endian_specific_integral
41 <uint16_t, TargetEndianness, support::unaligned> MachOInt16;
42 typedef support::detail::packed_endian_specific_integral
43 <uint32_t, TargetEndianness, support::unaligned> MachOInt32;
44 typedef support::detail::packed_endian_specific_integral
45 <uint64_t, TargetEndianness, support::unaligned> MachOInt64;
48 #define LLVM_MACHOB_IMPORT_TYPES(E) \
49 typedef typename MachODataTypeTypedefHelperCommon<E>::MachOInt16 MachOInt16; \
50 typedef typename MachODataTypeTypedefHelperCommon<E>::MachOInt32 MachOInt32; \
51 typedef typename MachODataTypeTypedefHelperCommon<E>::MachOInt64 MachOInt64;
53 template<class MachOT>
54 struct MachODataTypeTypedefHelper;
56 template<endianness TargetEndianness>
57 struct MachODataTypeTypedefHelper<MachOType<TargetEndianness, false> > {
58 typedef MachODataTypeTypedefHelperCommon<TargetEndianness> Base;
59 typedef typename Base::MachOInt32 MachOIntPtr;
62 template<endianness TargetEndianness>
63 struct MachODataTypeTypedefHelper<MachOType<TargetEndianness, true> > {
64 typedef MachODataTypeTypedefHelperCommon<TargetEndianness> Base;
65 typedef typename Base::MachOInt64 MachOIntPtr;
68 #define LLVM_MACHO_IMPORT_TYPES(MachOT, E, B) \
69 LLVM_MACHOB_IMPORT_TYPES(E) \
71 MachODataTypeTypedefHelper <MachOT<E, B> >::MachOIntPtr MachOIntPtr;
73 namespace MachOFormat {
79 template<class MachOT>
82 template<endianness TargetEndianness>
83 struct Section<MachOType<TargetEndianness, false> > {
84 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
91 MachOInt32 RelocationTableOffset;
92 MachOInt32 NumRelocationTableEntries;
98 template<endianness TargetEndianness>
99 struct Section<MachOType<TargetEndianness, true> > {
100 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
102 char SegmentName[16];
107 MachOInt32 RelocationTableOffset;
108 MachOInt32 NumRelocationTableEntries;
110 MachOInt32 Reserved1;
111 MachOInt32 Reserved2;
112 MachOInt32 Reserved3;
115 template<endianness TargetEndianness>
116 struct RelocationEntry {
117 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
122 template<endianness TargetEndianness>
123 struct SymbolTableEntryBase {
124 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
125 MachOInt32 StringIndex;
127 uint8_t SectionIndex;
131 template<class MachOT>
132 struct SymbolTableEntry;
134 template<endianness TargetEndianness, bool Is64Bits>
135 struct SymbolTableEntry<MachOType<TargetEndianness, Is64Bits> > {
136 LLVM_MACHO_IMPORT_TYPES(MachOType, TargetEndianness, Is64Bits)
137 MachOInt32 StringIndex;
139 uint8_t SectionIndex;
144 template<endianness TargetEndianness>
146 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
151 template<endianness TargetEndianness>
152 struct SymtabLoadCommand {
153 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
156 MachOInt32 SymbolTableOffset;
157 MachOInt32 NumSymbolTableEntries;
158 MachOInt32 StringTableOffset;
159 MachOInt32 StringTableSize;
162 template<class MachOT>
163 struct SegmentLoadCommand;
165 template<endianness TargetEndianness, bool Is64Bits>
166 struct SegmentLoadCommand<MachOType<TargetEndianness, Is64Bits> > {
167 LLVM_MACHO_IMPORT_TYPES(MachOType, TargetEndianness, Is64Bits)
171 MachOIntPtr VMAddress;
173 MachOIntPtr FileOffset;
174 MachOIntPtr FileSize;
175 MachOInt32 MaxVMProtection;
176 MachOInt32 InitialVMProtection;
177 MachOInt32 NumSections;
181 template<endianness TargetEndianness>
182 struct LinkeditDataLoadCommand {
183 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
186 MachOInt32 DataOffset;
190 template<endianness TargetEndianness>
192 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
195 MachOInt32 CPUSubtype;
197 MachOInt32 NumLoadCommands;
198 MachOInt32 SizeOfLoadCommands;
203 class MachOObjectFileBase : public ObjectFile {
205 typedef MachOFormat::SymbolTableEntryBase<support::little>
206 SymbolTableEntryBase;
207 typedef MachOFormat::SymtabLoadCommand<support::little> SymtabLoadCommand;
208 typedef MachOFormat::RelocationEntry<support::little> RelocationEntry;
209 typedef MachOFormat::SectionBase SectionBase;
210 typedef MachOFormat::LoadCommand<support::little> LoadCommand;
211 typedef MachOFormat::Header<support::little> Header;
212 typedef MachOFormat::LinkeditDataLoadCommand<support::little>
213 LinkeditDataLoadCommand;
215 MachOObjectFileBase(MemoryBuffer *Object, bool Is64Bits, error_code &ec);
217 virtual symbol_iterator begin_symbols() const;
218 virtual symbol_iterator end_symbols() const;
219 virtual symbol_iterator begin_dynamic_symbols() const;
220 virtual symbol_iterator end_dynamic_symbols() const;
221 virtual library_iterator begin_libraries_needed() const;
222 virtual library_iterator end_libraries_needed() const;
223 virtual section_iterator end_sections() const;
225 virtual uint8_t getBytesInAddress() const;
226 virtual StringRef getFileFormatName() const;
227 virtual unsigned getArch() const;
228 virtual StringRef getLoadName() const;
230 // In a MachO file, sections have a segment name. This is used in the .o
231 // files. They have a single segment, but this field specifies which segment
232 // a section should be put in in the final object.
233 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
235 // Names are stored as 16 bytes. These returns the raw 16 bytes without
236 // interpreting them as a C string.
237 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
238 ArrayRef<char>getSectionRawFinalSegmentName(DataRefImpl Sec) const;
240 bool is64Bit() const;
241 const LoadCommand *getLoadCommandInfo(unsigned Index) const;
242 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
243 const Header *getHeader() const;
244 unsigned getHeaderSize() const;
245 StringRef getData(size_t Offset, size_t Size) const;
247 static inline bool classof(const Binary *v) {
252 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
253 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
254 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
255 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
256 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
257 virtual error_code getSymbolSection(DataRefImpl Symb,
258 section_iterator &Res) const;
259 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
260 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
261 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
262 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
263 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
265 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
266 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
267 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
269 virtual error_code getRelocationNext(DataRefImpl Rel,
270 RelocationRef &Res) const;
272 virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
273 virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
274 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
277 std::size_t getSectionIndex(DataRefImpl Sec) const;
279 typedef SmallVector<DataRefImpl, 1> SectionList;
280 SectionList Sections;
282 void moveToNextSymbol(DataRefImpl &DRI) const;
283 void printRelocationTargetName(const RelocationEntry *RE,
284 raw_string_ostream &fmt) const;
285 const SectionBase *getSectionBase(DataRefImpl DRI) const;
286 const SymbolTableEntryBase *getSymbolTableEntryBase(DataRefImpl DRI) const;
290 const SymbolTableEntryBase *getSymbolTableEntryBase(DataRefImpl DRI,
291 const SymtabLoadCommand *SymtabLoadCmd) const;
294 template<class MachOT>
295 struct MachOObjectFileHelperCommon;
297 template<endianness TargetEndianness, bool Is64Bits>
298 struct MachOObjectFileHelperCommon<MachOType<TargetEndianness, Is64Bits> > {
300 MachOFormat::SegmentLoadCommand<MachOType<TargetEndianness, Is64Bits> >
302 typedef MachOFormat::SymbolTableEntry<MachOType<TargetEndianness, Is64Bits> >
304 typedef MachOFormat::Section<MachOType<TargetEndianness, Is64Bits> > Section;
307 template<class MachOT>
308 struct MachOObjectFileHelper;
310 template<endianness TargetEndianness>
311 struct MachOObjectFileHelper<MachOType<TargetEndianness, false> > :
312 public MachOObjectFileHelperCommon<MachOType<TargetEndianness, false> > {
313 static const macho::LoadCommandType SegmentLoadType = macho::LCT_Segment;
316 template<endianness TargetEndianness>
317 struct MachOObjectFileHelper<MachOType<TargetEndianness, true> > :
318 public MachOObjectFileHelperCommon<MachOType<TargetEndianness, true> > {
319 static const macho::LoadCommandType SegmentLoadType = macho::LCT_Segment64;
322 template<class MachOT>
323 class MachOObjectFile : public MachOObjectFileBase {
325 static const endianness TargetEndianness = MachOT::TargetEndianness;
326 static const bool Is64Bits = MachOT::Is64Bits;
328 typedef MachOObjectFileHelper<MachOT> Helper;
329 static const macho::LoadCommandType SegmentLoadType = Helper::SegmentLoadType;
330 typedef typename Helper::SegmentLoadCommand SegmentLoadCommand;
331 typedef typename Helper::SymbolTableEntry SymbolTableEntry;
332 typedef typename Helper::Section Section;
334 MachOObjectFile(MemoryBuffer *Object, error_code &ec);
335 static bool classof(const Binary *v);
337 const Section *getSection(DataRefImpl DRI) const;
338 const SymbolTableEntry *getSymbolTableEntry(DataRefImpl DRI) const;
339 const RelocationEntry *getRelocation(DataRefImpl Rel) const;
341 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
342 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
343 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
344 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
345 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
346 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
347 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
348 virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
349 virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
350 virtual error_code getRelocationSymbol(DataRefImpl Rel, SymbolRef &Res) const;
351 virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
352 virtual error_code getRelocationTypeName(DataRefImpl Rel,
353 SmallVectorImpl<char> &Result) const;
354 virtual error_code getRelocationValueString(DataRefImpl Rel,
355 SmallVectorImpl<char> &Result) const;
356 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
357 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
358 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
360 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
361 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
362 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
363 virtual section_iterator begin_sections() const;
364 void moveToNextSection(DataRefImpl &DRI) const;
367 template<class MachOT>
368 MachOObjectFile<MachOT>::MachOObjectFile(MemoryBuffer *Object,
370 MachOObjectFileBase(Object, Is64Bits, ec) {
372 moveToNextSection(DRI);
373 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
374 while (DRI.d.a < LoadCommandCount) {
375 Sections.push_back(DRI);
377 moveToNextSection(DRI);
381 template<class MachOT>
382 bool MachOObjectFile<MachOT>::classof(const Binary *v) {
383 return v->getType() == getMachOType(true, Is64Bits);
386 template<class MachOT>
387 const typename MachOObjectFile<MachOT>::Section *
388 MachOObjectFile<MachOT>::getSection(DataRefImpl DRI) const {
389 const SectionBase *Addr = getSectionBase(DRI);
390 return reinterpret_cast<const Section*>(Addr);
393 template<class MachOT>
394 const typename MachOObjectFile<MachOT>::SymbolTableEntry *
395 MachOObjectFile<MachOT>::getSymbolTableEntry(DataRefImpl DRI) const {
396 const SymbolTableEntryBase *Base = getSymbolTableEntryBase(DRI);
397 return reinterpret_cast<const SymbolTableEntry*>(Base);
400 template<class MachOT>
401 const typename MachOObjectFile<MachOT>::RelocationEntry *
402 MachOObjectFile<MachOT>::getRelocation(DataRefImpl Rel) const {
403 const Section *Sect = getSection(Sections[Rel.d.b]);
404 uint32_t RelOffset = Sect->RelocationTableOffset;
405 uint64_t Offset = RelOffset + Rel.d.a * sizeof(RelocationEntry);
406 StringRef Data = getData(Offset, sizeof(RelocationEntry));
407 return reinterpret_cast<const RelocationEntry*>(Data.data());
410 template<class MachOT>
412 MachOObjectFile<MachOT>::getSectionAddress(DataRefImpl Sec,
413 uint64_t &Res) const {
414 const Section *Sect = getSection(Sec);
416 return object_error::success;
419 template<class MachOT>
421 MachOObjectFile<MachOT>::getSectionSize(DataRefImpl Sec,
422 uint64_t &Res) const {
423 const Section *Sect = getSection(Sec);
425 return object_error::success;
428 template<class MachOT>
430 MachOObjectFile<MachOT>::getSectionContents(DataRefImpl Sec,
431 StringRef &Res) const {
432 const Section *Sect = getSection(Sec);
433 Res = getData(Sect->Offset, Sect->Size);
434 return object_error::success;
437 template<class MachOT>
439 MachOObjectFile<MachOT>::getSectionAlignment(DataRefImpl Sec,
440 uint64_t &Res) const {
441 const Section *Sect = getSection(Sec);
442 Res = uint64_t(1) << Sect->Align;
443 return object_error::success;
446 template<class MachOT>
448 MachOObjectFile<MachOT>::isSectionText(DataRefImpl Sec, bool &Res) const {
449 const Section *Sect = getSection(Sec);
450 Res = Sect->Flags & macho::SF_PureInstructions;
451 return object_error::success;
454 template<class MachOT>
456 MachOObjectFile<MachOT>::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
457 const Section *Sect = getSection(Sec);
458 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
459 Res = SectionType == MachO::SectionTypeZeroFill ||
460 SectionType == MachO::SectionTypeZeroFillLarge;
461 return object_error::success;
464 template<class MachOT>
466 MachOObjectFile<MachOT>::getSectionRelEnd(DataRefImpl Sec) const {
467 const Section *Sect = getSection(Sec);
468 uint32_t LastReloc = Sect->NumRelocationTableEntries;
471 Ret.d.b = getSectionIndex(Sec);
472 return relocation_iterator(RelocationRef(Ret, this));
475 template<class MachOT>
477 MachOObjectFile<MachOT>::getRelocationAddress(DataRefImpl Rel,
478 uint64_t &Res) const {
479 const Section *Sect = getSection(Sections[Rel.d.b]);
480 uint64_t SectAddress = Sect->Address;
481 const RelocationEntry *RE = getRelocation(Rel);
482 unsigned Arch = getArch();
483 bool isScattered = (Arch != Triple::x86_64) &&
484 (RE->Word0 & macho::RF_Scattered);
488 RelAddr = RE->Word0 & 0xFFFFFF;
492 Res = SectAddress + RelAddr;
493 return object_error::success;
496 template<class MachOT>
498 MachOObjectFile<MachOT>::getRelocationOffset(DataRefImpl Rel,
499 uint64_t &Res) const {
500 const RelocationEntry *RE = getRelocation(Rel);
502 unsigned Arch = getArch();
503 bool isScattered = (Arch != Triple::x86_64) &&
504 (RE->Word0 & macho::RF_Scattered);
506 Res = RE->Word0 & 0xFFFFFF;
509 return object_error::success;
512 template<class MachOT>
514 MachOObjectFile<MachOT>::getRelocationSymbol(DataRefImpl Rel,
515 SymbolRef &Res) const {
516 const RelocationEntry *RE = getRelocation(Rel);
517 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
518 bool isExtern = (RE->Word1 >> 27) & 1;
521 moveToNextSymbol(Sym);
523 for (unsigned i = 0; i < SymbolIdx; i++) {
525 moveToNextSymbol(Sym);
526 assert(Sym.d.a < getHeader()->NumLoadCommands &&
527 "Relocation symbol index out of range!");
530 Res = SymbolRef(Sym, this);
531 return object_error::success;
534 template<class MachOT>
535 error_code MachOObjectFile<MachOT>::getRelocationType(DataRefImpl Rel,
536 uint64_t &Res) const {
537 const RelocationEntry *RE = getRelocation(Rel);
541 return object_error::success;
544 template<class MachOT>
546 MachOObjectFile<MachOT>::getRelocationTypeName(DataRefImpl Rel,
547 SmallVectorImpl<char> &Result) const {
548 // TODO: Support scattered relocations.
550 const RelocationEntry *RE = getRelocation(Rel);
552 unsigned Arch = getArch();
553 bool isScattered = (Arch != Triple::x86_64) &&
554 (RE->Word0 & macho::RF_Scattered);
558 r_type = (RE->Word0 >> 24) & 0xF;
560 r_type = (RE->Word1 >> 28) & 0xF;
564 static const char *const Table[] = {
565 "GENERIC_RELOC_VANILLA",
566 "GENERIC_RELOC_PAIR",
567 "GENERIC_RELOC_SECTDIFF",
568 "GENERIC_RELOC_PB_LA_PTR",
569 "GENERIC_RELOC_LOCAL_SECTDIFF",
570 "GENERIC_RELOC_TLV" };
578 case Triple::x86_64: {
579 static const char *const Table[] = {
580 "X86_64_RELOC_UNSIGNED",
581 "X86_64_RELOC_SIGNED",
582 "X86_64_RELOC_BRANCH",
583 "X86_64_RELOC_GOT_LOAD",
585 "X86_64_RELOC_SUBTRACTOR",
586 "X86_64_RELOC_SIGNED_1",
587 "X86_64_RELOC_SIGNED_2",
588 "X86_64_RELOC_SIGNED_4",
589 "X86_64_RELOC_TLV" };
598 static const char *const Table[] = {
601 "ARM_RELOC_SECTDIFF",
602 "ARM_RELOC_LOCAL_SECTDIFF",
603 "ARM_RELOC_PB_LA_PTR",
605 "ARM_THUMB_RELOC_BR22",
606 "ARM_THUMB_32BIT_BRANCH",
608 "ARM_RELOC_HALF_SECTDIFF" };
617 static const char *const Table[] = {
626 "PPC_RELOC_SECTDIFF",
627 "PPC_RELOC_PB_LA_PTR",
628 "PPC_RELOC_HI16_SECTDIFF",
629 "PPC_RELOC_LO16_SECTDIFF",
630 "PPC_RELOC_HA16_SECTDIFF",
632 "PPC_RELOC_LO14_SECTDIFF",
633 "PPC_RELOC_LOCAL_SECTDIFF" };
638 case Triple::UnknownArch:
642 Result.append(res.begin(), res.end());
643 return object_error::success;
646 template<class MachOT>
648 MachOObjectFile<MachOT>::getRelocationValueString(DataRefImpl Rel,
649 SmallVectorImpl<char> &Result) const {
650 const RelocationEntry *RE = getRelocation(Rel);
652 unsigned Arch = getArch();
653 bool isScattered = (Arch != Triple::x86_64) &&
654 (RE->Word0 & macho::RF_Scattered);
657 raw_string_ostream fmt(fmtbuf);
661 Type = (RE->Word0 >> 24) & 0xF;
663 Type = (RE->Word1 >> 28) & 0xF;
667 isPCRel = ((RE->Word0 >> 30) & 1);
669 isPCRel = ((RE->Word1 >> 24) & 1);
671 // Determine any addends that should be displayed with the relocation.
672 // These require decoding the relocation type, which is triple-specific.
674 // X86_64 has entirely custom relocation types.
675 if (Arch == Triple::x86_64) {
676 bool isPCRel = ((RE->Word1 >> 24) & 1);
679 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
680 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
681 printRelocationTargetName(RE, fmt);
683 if (isPCRel) fmt << "PCREL";
686 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
687 DataRefImpl RelNext = Rel;
689 const RelocationEntry *RENext = getRelocation(RelNext);
691 // X86_64_SUBTRACTOR must be followed by a relocation of type
692 // X86_64_RELOC_UNSIGNED.
693 // NOTE: Scattered relocations don't exist on x86_64.
694 unsigned RType = (RENext->Word1 >> 28) & 0xF;
696 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
697 "X86_64_RELOC_SUBTRACTOR.");
699 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
700 // X86_64_SUBTRACTOR contains to the subtrahend.
701 printRelocationTargetName(RENext, fmt);
703 printRelocationTargetName(RE, fmt);
706 case macho::RIT_X86_64_TLV:
707 printRelocationTargetName(RE, fmt);
709 if (isPCRel) fmt << "P";
711 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
712 printRelocationTargetName(RE, fmt);
715 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
716 printRelocationTargetName(RE, fmt);
719 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
720 printRelocationTargetName(RE, fmt);
724 printRelocationTargetName(RE, fmt);
727 // X86 and ARM share some relocation types in common.
728 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
729 // Generic relocation types...
731 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
732 return object_error::success;
733 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
734 DataRefImpl RelNext = Rel;
736 const RelocationEntry *RENext = getRelocation(RelNext);
738 // X86 sect diff's must be followed by a relocation of type
739 // GENERIC_RELOC_PAIR.
740 bool isNextScattered = (Arch != Triple::x86_64) &&
741 (RENext->Word0 & macho::RF_Scattered);
744 RType = (RENext->Word0 >> 24) & 0xF;
746 RType = (RENext->Word1 >> 28) & 0xF;
748 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
749 "GENERIC_RELOC_SECTDIFF.");
751 printRelocationTargetName(RE, fmt);
753 printRelocationTargetName(RENext, fmt);
758 if (Arch == Triple::x86) {
759 // All X86 relocations that need special printing were already
760 // handled in the generic code.
762 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
763 DataRefImpl RelNext = Rel;
765 const RelocationEntry *RENext = getRelocation(RelNext);
767 // X86 sect diff's must be followed by a relocation of type
768 // GENERIC_RELOC_PAIR.
769 bool isNextScattered = (Arch != Triple::x86_64) &&
770 (RENext->Word0 & macho::RF_Scattered);
773 RType = (RENext->Word0 >> 24) & 0xF;
775 RType = (RENext->Word1 >> 28) & 0xF;
777 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
778 "GENERIC_RELOC_LOCAL_SECTDIFF.");
780 printRelocationTargetName(RE, fmt);
782 printRelocationTargetName(RENext, fmt);
785 case macho::RIT_Generic_TLV: {
786 printRelocationTargetName(RE, fmt);
788 if (isPCRel) fmt << "P";
792 printRelocationTargetName(RE, fmt);
794 } else { // ARM-specific relocations
796 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
797 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
798 // Half relocations steal a bit from the length field to encode
799 // whether this is an upper16 or a lower16 relocation.
802 isUpper = (RE->Word0 >> 28) & 1;
804 isUpper = (RE->Word1 >> 25) & 1;
810 printRelocationTargetName(RE, fmt);
812 DataRefImpl RelNext = Rel;
814 const RelocationEntry *RENext = getRelocation(RelNext);
816 // ARM half relocs must be followed by a relocation of type
818 bool isNextScattered = (Arch != Triple::x86_64) &&
819 (RENext->Word0 & macho::RF_Scattered);
822 RType = (RENext->Word0 >> 24) & 0xF;
824 RType = (RENext->Word1 >> 28) & 0xF;
827 report_fatal_error("Expected ARM_RELOC_PAIR after "
828 "GENERIC_RELOC_HALF");
830 // NOTE: The half of the target virtual address is stashed in the
831 // address field of the secondary relocation, but we can't reverse
832 // engineer the constant offset from it without decoding the movw/movt
833 // instruction to find the other half in its immediate field.
835 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
836 // symbol/section pointer of the follow-on relocation.
837 if (Type == macho::RIT_ARM_HalfDifference) {
839 printRelocationTargetName(RENext, fmt);
846 printRelocationTargetName(RE, fmt);
851 printRelocationTargetName(RE, fmt);
854 Result.append(fmtbuf.begin(), fmtbuf.end());
855 return object_error::success;
858 template<class MachOT>
860 MachOObjectFile<MachOT>::getRelocationHidden(DataRefImpl Rel,
861 bool &Result) const {
862 const RelocationEntry *RE = getRelocation(Rel);
864 unsigned Arch = getArch();
865 bool isScattered = (Arch != Triple::x86_64) &&
866 (RE->Word0 & macho::RF_Scattered);
869 Type = (RE->Word0 >> 24) & 0xF;
871 Type = (RE->Word1 >> 28) & 0xF;
875 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
877 if (Arch == Triple::x86 || Arch == Triple::arm) {
878 if (Type == macho::RIT_Pair) Result = true;
879 } else if (Arch == Triple::x86_64) {
880 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
881 // an X864_64_RELOC_SUBTRACTOR.
882 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
883 DataRefImpl RelPrev = Rel;
885 const RelocationEntry *REPrev = getRelocation(RelPrev);
887 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
889 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
893 return object_error::success;
896 template<class MachOT>
898 MachOObjectFile<MachOT>::getSymbolFileOffset(DataRefImpl Symb,
899 uint64_t &Res) const {
900 const SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
902 if (Entry->SectionIndex) {
903 const Section *Sec = getSection(Sections[Entry->SectionIndex-1]);
904 Res += Sec->Offset - Sec->Address;
907 return object_error::success;
910 template<class MachOT>
912 MachOObjectFile<MachOT>::sectionContainsSymbol(DataRefImpl Sec,
914 bool &Result) const {
916 getSymbolType(Symb, ST);
917 if (ST == SymbolRef::ST_Unknown) {
919 return object_error::success;
922 uint64_t SectBegin, SectEnd;
923 getSectionAddress(Sec, SectBegin);
924 getSectionSize(Sec, SectEnd);
925 SectEnd += SectBegin;
927 const SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
928 uint64_t SymAddr= Entry->Value;
929 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
931 return object_error::success;
934 template<class MachOT>
935 error_code MachOObjectFile<MachOT>::getSymbolAddress(DataRefImpl Symb,
936 uint64_t &Res) const {
937 const SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
939 return object_error::success;
942 template<class MachOT>
943 error_code MachOObjectFile<MachOT>::getSymbolSize(DataRefImpl DRI,
944 uint64_t &Result) const {
945 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
946 uint64_t BeginOffset;
947 uint64_t EndOffset = 0;
948 uint8_t SectionIndex;
950 const SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
951 BeginOffset = Entry->Value;
952 SectionIndex = Entry->SectionIndex;
954 uint32_t flags = SymbolRef::SF_None;
955 getSymbolFlags(DRI, flags);
956 if (flags & SymbolRef::SF_Common)
957 Result = Entry->Value;
959 Result = UnknownAddressOrSize;
960 return object_error::success;
962 // Unfortunately symbols are unsorted so we need to touch all
963 // symbols from load command
965 uint32_t Command = DRI.d.a;
966 while (Command == DRI.d.a) {
967 moveToNextSymbol(DRI);
968 if (DRI.d.a < LoadCommandCount) {
969 Entry = getSymbolTableEntry(DRI);
970 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
971 if (!EndOffset || Entry->Value < EndOffset)
972 EndOffset = Entry->Value;
978 getSectionSize(Sections[SectionIndex-1], Size);
979 getSectionAddress(Sections[SectionIndex-1], EndOffset);
982 Result = EndOffset - BeginOffset;
983 return object_error::success;
986 template<class MachOT>
987 error_code MachOObjectFile<MachOT>::getSectionNext(DataRefImpl Sec,
988 SectionRef &Res) const {
990 moveToNextSection(Sec);
991 Res = SectionRef(Sec, this);
992 return object_error::success;
995 template<class MachOT>
996 section_iterator MachOObjectFile<MachOT>::begin_sections() const {
998 moveToNextSection(DRI);
999 return section_iterator(SectionRef(DRI, this));
1002 template<class MachOT>
1003 void MachOObjectFile<MachOT>::moveToNextSection(DataRefImpl &DRI) const {
1004 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
1005 while (DRI.d.a < LoadCommandCount) {
1006 const LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
1007 if (Command->Type == SegmentLoadType) {
1008 const SegmentLoadCommand *SegmentLoadCmd =
1009 reinterpret_cast<const SegmentLoadCommand*>(Command);
1010 if (DRI.d.b < SegmentLoadCmd->NumSections)
1019 typedef MachOObjectFile<MachOType<support::little, false> >
1020 MachOObjectFile32Le;
1021 typedef MachOObjectFile<MachOType<support::little, true> >
1022 MachOObjectFile64Le;