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)
119 unsigned SymbolNum : 24;
122 unsigned External : 1;
126 template<endianness TargetEndianness>
127 struct ScatteredRelocationEntry {
128 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
129 unsigned Address : 24;
133 unsigned Scattered : 1;
137 template<endianness TargetEndianness>
138 struct SymbolTableEntryBase {
139 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
140 MachOInt32 StringIndex;
142 uint8_t SectionIndex;
146 template<class MachOT>
147 struct SymbolTableEntry;
149 template<endianness TargetEndianness, bool Is64Bits>
150 struct SymbolTableEntry<MachOType<TargetEndianness, Is64Bits> > {
151 LLVM_MACHO_IMPORT_TYPES(MachOType, TargetEndianness, Is64Bits)
152 MachOInt32 StringIndex;
154 uint8_t SectionIndex;
159 template<endianness TargetEndianness>
161 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
166 template<endianness TargetEndianness>
167 struct SymtabLoadCommand {
168 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
171 MachOInt32 SymbolTableOffset;
172 MachOInt32 NumSymbolTableEntries;
173 MachOInt32 StringTableOffset;
174 MachOInt32 StringTableSize;
177 template<class MachOT>
178 struct SegmentLoadCommand;
180 template<endianness TargetEndianness, bool Is64Bits>
181 struct SegmentLoadCommand<MachOType<TargetEndianness, Is64Bits> > {
182 LLVM_MACHO_IMPORT_TYPES(MachOType, TargetEndianness, Is64Bits)
186 MachOIntPtr VMAddress;
188 MachOIntPtr FileOffset;
189 MachOIntPtr FileSize;
190 MachOInt32 MaxVMProtection;
191 MachOInt32 InitialVMProtection;
192 MachOInt32 NumSections;
196 template<endianness TargetEndianness>
197 struct LinkeditDataLoadCommand {
198 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
201 MachOInt32 DataOffset;
205 template<endianness TargetEndianness>
207 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
210 MachOInt32 CPUSubtype;
212 MachOInt32 NumLoadCommands;
213 MachOInt32 SizeOfLoadCommands;
218 class MachOObjectFileBase : public ObjectFile {
220 typedef MachOFormat::SymbolTableEntryBase<support::little>
221 SymbolTableEntryBase;
222 typedef MachOFormat::SymtabLoadCommand<support::little> SymtabLoadCommand;
223 typedef MachOFormat::RelocationEntry<support::little> RelocationEntry;
224 typedef MachOFormat::ScatteredRelocationEntry<support::little>
225 ScatteredRelocationEntry;
226 typedef MachOFormat::SectionBase SectionBase;
227 typedef MachOFormat::LoadCommand<support::little> LoadCommand;
228 typedef MachOFormat::Header<support::little> Header;
229 typedef MachOFormat::LinkeditDataLoadCommand<support::little>
230 LinkeditDataLoadCommand;
232 MachOObjectFileBase(MemoryBuffer *Object, bool Is64Bits, error_code &ec);
234 virtual symbol_iterator begin_symbols() const;
235 virtual symbol_iterator end_symbols() const;
236 virtual symbol_iterator begin_dynamic_symbols() const;
237 virtual symbol_iterator end_dynamic_symbols() const;
238 virtual library_iterator begin_libraries_needed() const;
239 virtual library_iterator end_libraries_needed() const;
240 virtual section_iterator end_sections() const;
242 virtual uint8_t getBytesInAddress() const;
243 virtual StringRef getFileFormatName() const;
244 virtual unsigned getArch() const;
245 virtual StringRef getLoadName() const;
247 // In a MachO file, sections have a segment name. This is used in the .o
248 // files. They have a single segment, but this field specifies which segment
249 // a section should be put in in the final object.
250 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
252 // Names are stored as 16 bytes. These returns the raw 16 bytes without
253 // interpreting them as a C string.
254 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
255 ArrayRef<char>getSectionRawFinalSegmentName(DataRefImpl Sec) const;
257 bool is64Bit() const;
258 const LoadCommand *getLoadCommandInfo(unsigned Index) const;
259 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
260 const Header *getHeader() const;
261 unsigned getHeaderSize() const;
262 StringRef getData(size_t Offset, size_t Size) const;
263 const RelocationEntry *getRelocation(DataRefImpl Rel) const;
264 bool isScattered(const RelocationEntry *RE) const;
265 bool isPCRel(const RelocationEntry *RE) const;
266 unsigned getLength(const RelocationEntry *RE) const;
267 unsigned getType(const RelocationEntry *RE) const;
269 static inline bool classof(const Binary *v) {
274 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
275 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
276 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
277 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
278 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
279 virtual error_code getSymbolSection(DataRefImpl Symb,
280 section_iterator &Res) const;
281 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
282 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
283 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
284 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
285 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
287 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
288 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
289 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
291 virtual error_code getRelocationNext(DataRefImpl Rel,
292 RelocationRef &Res) const;
294 virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
295 virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
296 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
299 std::size_t getSectionIndex(DataRefImpl Sec) const;
301 typedef SmallVector<DataRefImpl, 1> SectionList;
302 SectionList Sections;
304 void moveToNextSymbol(DataRefImpl &DRI) const;
305 void printRelocationTargetName(const RelocationEntry *RE,
306 raw_string_ostream &fmt) const;
307 const SectionBase *getSectionBase(DataRefImpl DRI) const;
308 const SymbolTableEntryBase *getSymbolTableEntryBase(DataRefImpl DRI) const;
312 const SymbolTableEntryBase *getSymbolTableEntryBase(DataRefImpl DRI,
313 const SymtabLoadCommand *SymtabLoadCmd) const;
316 template<class MachOT>
317 struct MachOObjectFileHelperCommon;
319 template<endianness TargetEndianness, bool Is64Bits>
320 struct MachOObjectFileHelperCommon<MachOType<TargetEndianness, Is64Bits> > {
322 MachOFormat::SegmentLoadCommand<MachOType<TargetEndianness, Is64Bits> >
324 typedef MachOFormat::SymbolTableEntry<MachOType<TargetEndianness, Is64Bits> >
326 typedef MachOFormat::Section<MachOType<TargetEndianness, Is64Bits> > Section;
329 template<class MachOT>
330 struct MachOObjectFileHelper;
332 template<endianness TargetEndianness>
333 struct MachOObjectFileHelper<MachOType<TargetEndianness, false> > :
334 public MachOObjectFileHelperCommon<MachOType<TargetEndianness, false> > {
335 static const macho::LoadCommandType SegmentLoadType = macho::LCT_Segment;
338 template<endianness TargetEndianness>
339 struct MachOObjectFileHelper<MachOType<TargetEndianness, true> > :
340 public MachOObjectFileHelperCommon<MachOType<TargetEndianness, true> > {
341 static const macho::LoadCommandType SegmentLoadType = macho::LCT_Segment64;
344 template<class MachOT>
345 class MachOObjectFile : public MachOObjectFileBase {
347 static const endianness TargetEndianness = MachOT::TargetEndianness;
348 static const bool Is64Bits = MachOT::Is64Bits;
350 typedef MachOObjectFileHelper<MachOT> Helper;
351 static const macho::LoadCommandType SegmentLoadType = Helper::SegmentLoadType;
352 typedef typename Helper::SegmentLoadCommand SegmentLoadCommand;
353 typedef typename Helper::SymbolTableEntry SymbolTableEntry;
354 typedef typename Helper::Section Section;
356 MachOObjectFile(MemoryBuffer *Object, error_code &ec);
357 static bool classof(const Binary *v);
359 const Section *getSection(DataRefImpl DRI) const;
360 const SymbolTableEntry *getSymbolTableEntry(DataRefImpl DRI) const;
361 const RelocationEntry *getRelocation(DataRefImpl Rel) const;
363 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
364 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
365 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
366 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
367 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
368 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
369 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
370 virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
371 virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
372 virtual error_code getRelocationSymbol(DataRefImpl Rel, SymbolRef &Res) const;
373 virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
374 virtual error_code getRelocationTypeName(DataRefImpl Rel,
375 SmallVectorImpl<char> &Result) const;
376 virtual error_code getRelocationValueString(DataRefImpl Rel,
377 SmallVectorImpl<char> &Result) const;
378 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
379 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
380 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
382 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
383 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
384 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
385 virtual section_iterator begin_sections() const;
386 void moveToNextSection(DataRefImpl &DRI) const;
389 template<class MachOT>
390 MachOObjectFile<MachOT>::MachOObjectFile(MemoryBuffer *Object,
392 MachOObjectFileBase(Object, Is64Bits, ec) {
394 moveToNextSection(DRI);
395 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
396 while (DRI.d.a < LoadCommandCount) {
397 Sections.push_back(DRI);
399 moveToNextSection(DRI);
403 template<class MachOT>
404 bool MachOObjectFile<MachOT>::classof(const Binary *v) {
405 return v->getType() == getMachOType(true, Is64Bits);
408 template<class MachOT>
409 const typename MachOObjectFile<MachOT>::Section *
410 MachOObjectFile<MachOT>::getSection(DataRefImpl DRI) const {
411 const SectionBase *Addr = getSectionBase(DRI);
412 return reinterpret_cast<const Section*>(Addr);
415 template<class MachOT>
416 const typename MachOObjectFile<MachOT>::SymbolTableEntry *
417 MachOObjectFile<MachOT>::getSymbolTableEntry(DataRefImpl DRI) const {
418 const SymbolTableEntryBase *Base = getSymbolTableEntryBase(DRI);
419 return reinterpret_cast<const SymbolTableEntry*>(Base);
422 template<class MachOT>
423 const typename MachOObjectFile<MachOT>::RelocationEntry *
424 MachOObjectFile<MachOT>::getRelocation(DataRefImpl Rel) const {
425 const Section *Sect = getSection(Sections[Rel.d.b]);
426 uint32_t RelOffset = Sect->RelocationTableOffset;
427 uint64_t Offset = RelOffset + Rel.d.a * sizeof(RelocationEntry);
428 StringRef Data = getData(Offset, sizeof(RelocationEntry));
429 return reinterpret_cast<const RelocationEntry*>(Data.data());
432 template<class MachOT>
434 MachOObjectFile<MachOT>::getSectionAddress(DataRefImpl Sec,
435 uint64_t &Res) const {
436 const Section *Sect = getSection(Sec);
438 return object_error::success;
441 template<class MachOT>
443 MachOObjectFile<MachOT>::getSectionSize(DataRefImpl Sec,
444 uint64_t &Res) const {
445 const Section *Sect = getSection(Sec);
447 return object_error::success;
450 template<class MachOT>
452 MachOObjectFile<MachOT>::getSectionContents(DataRefImpl Sec,
453 StringRef &Res) const {
454 const Section *Sect = getSection(Sec);
455 Res = getData(Sect->Offset, Sect->Size);
456 return object_error::success;
459 template<class MachOT>
461 MachOObjectFile<MachOT>::getSectionAlignment(DataRefImpl Sec,
462 uint64_t &Res) const {
463 const Section *Sect = getSection(Sec);
464 Res = uint64_t(1) << Sect->Align;
465 return object_error::success;
468 template<class MachOT>
470 MachOObjectFile<MachOT>::isSectionText(DataRefImpl Sec, bool &Res) const {
471 const Section *Sect = getSection(Sec);
472 Res = Sect->Flags & macho::SF_PureInstructions;
473 return object_error::success;
476 template<class MachOT>
478 MachOObjectFile<MachOT>::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
479 const Section *Sect = getSection(Sec);
480 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
481 Res = SectionType == MachO::SectionTypeZeroFill ||
482 SectionType == MachO::SectionTypeZeroFillLarge;
483 return object_error::success;
486 template<class MachOT>
488 MachOObjectFile<MachOT>::getSectionRelEnd(DataRefImpl Sec) const {
489 const Section *Sect = getSection(Sec);
490 uint32_t LastReloc = Sect->NumRelocationTableEntries;
493 Ret.d.b = getSectionIndex(Sec);
494 return relocation_iterator(RelocationRef(Ret, this));
497 template<class MachOT>
499 MachOObjectFile<MachOT>::getRelocationAddress(DataRefImpl Rel,
500 uint64_t &Res) const {
501 const Section *Sect = getSection(Sections[Rel.d.b]);
502 uint64_t SectAddress = Sect->Address;
503 const RelocationEntry *RE = getRelocation(Rel);
507 RelAddr = RE->Address & 0xFFFFFF;
509 RelAddr = RE->Address;
511 Res = SectAddress + RelAddr;
512 return object_error::success;
515 template<class MachOT>
517 MachOObjectFile<MachOT>::getRelocationOffset(DataRefImpl Rel,
518 uint64_t &Res) const {
519 const RelocationEntry *RE = getRelocation(Rel);
521 Res = RE->Address & 0xFFFFFF;
524 return object_error::success;
527 template<class MachOT>
529 MachOObjectFile<MachOT>::getRelocationSymbol(DataRefImpl Rel,
530 SymbolRef &Res) const {
531 const RelocationEntry *RE = getRelocation(Rel);
532 uint32_t SymbolIdx = RE->SymbolNum;
533 bool isExtern = RE->External;
536 moveToNextSymbol(Sym);
538 for (unsigned i = 0; i < SymbolIdx; i++) {
540 moveToNextSymbol(Sym);
541 assert(Sym.d.a < getHeader()->NumLoadCommands &&
542 "Relocation symbol index out of range!");
545 Res = SymbolRef(Sym, this);
546 return object_error::success;
549 template<class MachOT>
550 error_code MachOObjectFile<MachOT>::getRelocationType(DataRefImpl Rel,
551 uint64_t &Res) const {
552 const RelocationEntry *RE = getRelocation(Rel);
554 return object_error::success;
557 template<class MachOT>
559 MachOObjectFile<MachOT>::getRelocationTypeName(DataRefImpl Rel,
560 SmallVectorImpl<char> &Result) const {
561 // TODO: Support scattered relocations.
563 const RelocationEntry *RE = getRelocation(Rel);
565 unsigned Arch = getArch();
567 unsigned r_type = getType(RE);
571 static const char *const Table[] = {
572 "GENERIC_RELOC_VANILLA",
573 "GENERIC_RELOC_PAIR",
574 "GENERIC_RELOC_SECTDIFF",
575 "GENERIC_RELOC_PB_LA_PTR",
576 "GENERIC_RELOC_LOCAL_SECTDIFF",
577 "GENERIC_RELOC_TLV" };
585 case Triple::x86_64: {
586 static const char *const Table[] = {
587 "X86_64_RELOC_UNSIGNED",
588 "X86_64_RELOC_SIGNED",
589 "X86_64_RELOC_BRANCH",
590 "X86_64_RELOC_GOT_LOAD",
592 "X86_64_RELOC_SUBTRACTOR",
593 "X86_64_RELOC_SIGNED_1",
594 "X86_64_RELOC_SIGNED_2",
595 "X86_64_RELOC_SIGNED_4",
596 "X86_64_RELOC_TLV" };
605 static const char *const Table[] = {
608 "ARM_RELOC_SECTDIFF",
609 "ARM_RELOC_LOCAL_SECTDIFF",
610 "ARM_RELOC_PB_LA_PTR",
612 "ARM_THUMB_RELOC_BR22",
613 "ARM_THUMB_32BIT_BRANCH",
615 "ARM_RELOC_HALF_SECTDIFF" };
624 static const char *const Table[] = {
633 "PPC_RELOC_SECTDIFF",
634 "PPC_RELOC_PB_LA_PTR",
635 "PPC_RELOC_HI16_SECTDIFF",
636 "PPC_RELOC_LO16_SECTDIFF",
637 "PPC_RELOC_HA16_SECTDIFF",
639 "PPC_RELOC_LO14_SECTDIFF",
640 "PPC_RELOC_LOCAL_SECTDIFF" };
645 case Triple::UnknownArch:
649 Result.append(res.begin(), res.end());
650 return object_error::success;
653 template<class MachOT>
655 MachOObjectFile<MachOT>::getRelocationValueString(DataRefImpl Rel,
656 SmallVectorImpl<char> &Result) const {
657 const RelocationEntry *RE = getRelocation(Rel);
659 unsigned Arch = getArch();
660 bool IsScattered = isScattered(RE);
663 raw_string_ostream fmt(fmtbuf);
665 unsigned Type = getType(RE);
666 bool IsPCRel = isPCRel(RE);
668 // Determine any addends that should be displayed with the relocation.
669 // These require decoding the relocation type, which is triple-specific.
671 // X86_64 has entirely custom relocation types.
672 if (Arch == Triple::x86_64) {
673 bool isPCRel = RE->PCRel;
676 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
677 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
678 printRelocationTargetName(RE, fmt);
680 if (isPCRel) fmt << "PCREL";
683 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
684 DataRefImpl RelNext = Rel;
686 const RelocationEntry *RENext = getRelocation(RelNext);
688 // X86_64_SUBTRACTOR must be followed by a relocation of type
689 // X86_64_RELOC_UNSIGNED.
690 // NOTE: Scattered relocations don't exist on x86_64.
691 unsigned RType = RENext->Type;
693 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
694 "X86_64_RELOC_SUBTRACTOR.");
696 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
697 // X86_64_SUBTRACTOR contains to the subtrahend.
698 printRelocationTargetName(RENext, fmt);
700 printRelocationTargetName(RE, fmt);
703 case macho::RIT_X86_64_TLV:
704 printRelocationTargetName(RE, fmt);
706 if (isPCRel) fmt << "P";
708 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
709 printRelocationTargetName(RE, fmt);
712 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
713 printRelocationTargetName(RE, fmt);
716 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
717 printRelocationTargetName(RE, fmt);
721 printRelocationTargetName(RE, fmt);
724 // X86 and ARM share some relocation types in common.
725 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
726 // Generic relocation types...
728 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
729 return object_error::success;
730 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
731 DataRefImpl RelNext = Rel;
733 const RelocationEntry *RENext = getRelocation(RelNext);
735 // X86 sect diff's must be followed by a relocation of type
736 // GENERIC_RELOC_PAIR.
737 bool isNextScattered = (Arch != Triple::x86_64) &&
738 (RENext->Address & macho::RF_Scattered);
741 RType = (RENext->Address >> 24) & 0xF;
743 RType = RENext->Type;
745 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
746 "GENERIC_RELOC_SECTDIFF.");
748 printRelocationTargetName(RE, fmt);
750 printRelocationTargetName(RENext, fmt);
755 if (Arch == Triple::x86) {
756 // All X86 relocations that need special printing were already
757 // handled in the generic code.
759 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
760 DataRefImpl RelNext = Rel;
762 const RelocationEntry *RENext = getRelocation(RelNext);
764 // X86 sect diff's must be followed by a relocation of type
765 // GENERIC_RELOC_PAIR.
766 bool isNextScattered = (Arch != Triple::x86_64) &&
767 (RENext->Address & macho::RF_Scattered);
770 RType = (RENext->Address >> 24) & 0xF;
772 RType = RENext->Type;
774 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
775 "GENERIC_RELOC_LOCAL_SECTDIFF.");
777 printRelocationTargetName(RE, fmt);
779 printRelocationTargetName(RENext, fmt);
782 case macho::RIT_Generic_TLV: {
783 printRelocationTargetName(RE, fmt);
785 if (IsPCRel) fmt << "P";
789 printRelocationTargetName(RE, fmt);
791 } else { // ARM-specific relocations
793 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
794 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
795 // Half relocations steal a bit from the length field to encode
796 // whether this is an upper16 or a lower16 relocation.
799 isUpper = (RE->Address >> 28) & 1;
801 isUpper = (RE->Length >> 1) & 1;
807 printRelocationTargetName(RE, fmt);
809 DataRefImpl RelNext = Rel;
811 const RelocationEntry *RENext = getRelocation(RelNext);
813 // ARM half relocs must be followed by a relocation of type
815 bool isNextScattered = (Arch != Triple::x86_64) &&
816 (RENext->Address & macho::RF_Scattered);
819 RType = (RENext->Address >> 24) & 0xF;
821 RType = RENext->Type;
824 report_fatal_error("Expected ARM_RELOC_PAIR after "
825 "GENERIC_RELOC_HALF");
827 // NOTE: The half of the target virtual address is stashed in the
828 // address field of the secondary relocation, but we can't reverse
829 // engineer the constant offset from it without decoding the movw/movt
830 // instruction to find the other half in its immediate field.
832 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
833 // symbol/section pointer of the follow-on relocation.
834 if (Type == macho::RIT_ARM_HalfDifference) {
836 printRelocationTargetName(RENext, fmt);
843 printRelocationTargetName(RE, fmt);
848 printRelocationTargetName(RE, fmt);
851 Result.append(fmtbuf.begin(), fmtbuf.end());
852 return object_error::success;
855 template<class MachOT>
857 MachOObjectFile<MachOT>::getRelocationHidden(DataRefImpl Rel,
858 bool &Result) const {
859 const RelocationEntry *RE = getRelocation(Rel);
860 unsigned Arch = getArch();
861 unsigned Type = getType(RE);
865 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
867 if (Arch == Triple::x86 || Arch == Triple::arm) {
868 if (Type == macho::RIT_Pair) Result = true;
869 } else if (Arch == Triple::x86_64) {
870 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
871 // an X864_64_RELOC_SUBTRACTOR.
872 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
873 DataRefImpl RelPrev = Rel;
875 const RelocationEntry *REPrev = getRelocation(RelPrev);
877 unsigned PrevType = REPrev->Type;
879 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
883 return object_error::success;
886 template<class MachOT>
888 MachOObjectFile<MachOT>::getSymbolFileOffset(DataRefImpl Symb,
889 uint64_t &Res) const {
890 const SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
892 if (Entry->SectionIndex) {
893 const Section *Sec = getSection(Sections[Entry->SectionIndex-1]);
894 Res += Sec->Offset - Sec->Address;
897 return object_error::success;
900 template<class MachOT>
902 MachOObjectFile<MachOT>::sectionContainsSymbol(DataRefImpl Sec,
904 bool &Result) const {
906 getSymbolType(Symb, ST);
907 if (ST == SymbolRef::ST_Unknown) {
909 return object_error::success;
912 uint64_t SectBegin, SectEnd;
913 getSectionAddress(Sec, SectBegin);
914 getSectionSize(Sec, SectEnd);
915 SectEnd += SectBegin;
917 const SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
918 uint64_t SymAddr= Entry->Value;
919 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
921 return object_error::success;
924 template<class MachOT>
925 error_code MachOObjectFile<MachOT>::getSymbolAddress(DataRefImpl Symb,
926 uint64_t &Res) const {
927 const SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
929 return object_error::success;
932 template<class MachOT>
933 error_code MachOObjectFile<MachOT>::getSymbolSize(DataRefImpl DRI,
934 uint64_t &Result) const {
935 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
936 uint64_t BeginOffset;
937 uint64_t EndOffset = 0;
938 uint8_t SectionIndex;
940 const SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
941 BeginOffset = Entry->Value;
942 SectionIndex = Entry->SectionIndex;
944 uint32_t flags = SymbolRef::SF_None;
945 getSymbolFlags(DRI, flags);
946 if (flags & SymbolRef::SF_Common)
947 Result = Entry->Value;
949 Result = UnknownAddressOrSize;
950 return object_error::success;
952 // Unfortunately symbols are unsorted so we need to touch all
953 // symbols from load command
955 uint32_t Command = DRI.d.a;
956 while (Command == DRI.d.a) {
957 moveToNextSymbol(DRI);
958 if (DRI.d.a < LoadCommandCount) {
959 Entry = getSymbolTableEntry(DRI);
960 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
961 if (!EndOffset || Entry->Value < EndOffset)
962 EndOffset = Entry->Value;
968 getSectionSize(Sections[SectionIndex-1], Size);
969 getSectionAddress(Sections[SectionIndex-1], EndOffset);
972 Result = EndOffset - BeginOffset;
973 return object_error::success;
976 template<class MachOT>
977 error_code MachOObjectFile<MachOT>::getSectionNext(DataRefImpl Sec,
978 SectionRef &Res) const {
980 moveToNextSection(Sec);
981 Res = SectionRef(Sec, this);
982 return object_error::success;
985 template<class MachOT>
986 section_iterator MachOObjectFile<MachOT>::begin_sections() const {
988 moveToNextSection(DRI);
989 return section_iterator(SectionRef(DRI, this));
992 template<class MachOT>
993 void MachOObjectFile<MachOT>::moveToNextSection(DataRefImpl &DRI) const {
994 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
995 while (DRI.d.a < LoadCommandCount) {
996 const LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
997 if (Command->Type == SegmentLoadType) {
998 const SegmentLoadCommand *SegmentLoadCmd =
999 reinterpret_cast<const SegmentLoadCommand*>(Command);
1000 if (DRI.d.b < SegmentLoadCmd->NumSections)
1009 typedef MachOObjectFile<MachOType<support::little, false> >
1010 MachOObjectFile32Le;
1011 typedef MachOObjectFile<MachOType<support::little, true> >
1012 MachOObjectFile64Le;