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;
275 std::size_t getSectionIndex(DataRefImpl Sec) const;
277 typedef SmallVector<DataRefImpl, 1> SectionList;
278 SectionList Sections;
280 void moveToNextSymbol(DataRefImpl &DRI) const;
281 void printRelocationTargetName(const RelocationEntry *RE,
282 raw_string_ostream &fmt) const;
283 const SectionBase *getSectionBase(DataRefImpl DRI) const;
284 const SymbolTableEntryBase *getSymbolTableEntryBase(DataRefImpl DRI) const;
288 const SymbolTableEntryBase *getSymbolTableEntryBase(DataRefImpl DRI,
289 const SymtabLoadCommand *SymtabLoadCmd) const;
292 template<bool is64Bits>
293 struct MachOObjectFileHelperCommon {
294 typedef MachOFormat::SegmentLoadCommand<MachOType<support::little, is64Bits> >
296 typedef MachOFormat::SymbolTableEntry<MachOType<support::little, is64Bits> >
298 typedef MachOFormat::Section<MachOType<support::little, is64Bits> > Section;
301 template<bool is64Bits>
302 struct MachOObjectFileHelper;
305 struct MachOObjectFileHelper<false> :
306 public MachOObjectFileHelperCommon<false> {
307 static const macho::LoadCommandType SegmentLoadType = macho::LCT_Segment;
311 struct MachOObjectFileHelper<true> :
312 public MachOObjectFileHelperCommon<true> {
313 static const macho::LoadCommandType SegmentLoadType = macho::LCT_Segment64;
316 template<bool is64Bits>
317 class MachOObjectFile : public MachOObjectFileBase {
319 typedef MachOObjectFileHelper<is64Bits> Helper;
320 static const macho::LoadCommandType SegmentLoadType = Helper::SegmentLoadType;
321 typedef typename Helper::SegmentLoadCommand SegmentLoadCommand;
322 typedef typename Helper::SymbolTableEntry SymbolTableEntry;
323 typedef typename Helper::Section Section;
325 MachOObjectFile(MemoryBuffer *Object, error_code &ec);
326 static bool classof(const Binary *v);
328 const Section *getSection(DataRefImpl DRI) const;
329 const SymbolTableEntry *getSymbolTableEntry(DataRefImpl DRI) const;
330 const RelocationEntry *getRelocation(DataRefImpl Rel) const;
332 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
333 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
334 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
335 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
336 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
337 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
338 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
339 virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
340 virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
341 virtual error_code getRelocationSymbol(DataRefImpl Rel, SymbolRef &Res) const;
342 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
344 virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
345 virtual error_code getRelocationTypeName(DataRefImpl Rel,
346 SmallVectorImpl<char> &Result) const;
347 virtual error_code getRelocationValueString(DataRefImpl Rel,
348 SmallVectorImpl<char> &Result) const;
349 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
350 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
351 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
353 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
354 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
355 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
356 virtual section_iterator begin_sections() const;
357 void moveToNextSection(DataRefImpl &DRI) const;
360 template<bool is64Bits>
361 MachOObjectFile<is64Bits>::MachOObjectFile(MemoryBuffer *Object,
363 MachOObjectFileBase(Object, is64Bits, ec) {
365 moveToNextSection(DRI);
366 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
367 while (DRI.d.a < LoadCommandCount) {
368 Sections.push_back(DRI);
370 moveToNextSection(DRI);
374 template<bool is64Bits>
375 bool MachOObjectFile<is64Bits>::classof(const Binary *v) {
376 return v->getType() == getMachOType(true, is64Bits);
379 template<bool is64Bits>
380 const typename MachOObjectFile<is64Bits>::Section *
381 MachOObjectFile<is64Bits>::getSection(DataRefImpl DRI) const {
382 const SectionBase *Addr = getSectionBase(DRI);
383 return reinterpret_cast<const Section*>(Addr);
386 template<bool is64Bits>
387 const typename MachOObjectFile<is64Bits>::SymbolTableEntry *
388 MachOObjectFile<is64Bits>::getSymbolTableEntry(DataRefImpl DRI) const {
389 const SymbolTableEntryBase *Base = getSymbolTableEntryBase(DRI);
390 return reinterpret_cast<const SymbolTableEntry*>(Base);
393 template<bool is64Bits>
394 const typename MachOObjectFile<is64Bits>::RelocationEntry *
395 MachOObjectFile<is64Bits>::getRelocation(DataRefImpl Rel) const {
396 const Section *Sect = getSection(Sections[Rel.d.b]);
397 uint32_t RelOffset = Sect->RelocationTableOffset;
398 uint64_t Offset = RelOffset + Rel.d.a * sizeof(RelocationEntry);
399 StringRef Data = getData(Offset, sizeof(RelocationEntry));
400 return reinterpret_cast<const RelocationEntry*>(Data.data());
403 template<bool is64Bits>
405 MachOObjectFile<is64Bits>::getSectionAddress(DataRefImpl Sec,
406 uint64_t &Res) const {
407 const Section *Sect = getSection(Sec);
409 return object_error::success;
412 template<bool is64Bits>
414 MachOObjectFile<is64Bits>::getSectionSize(DataRefImpl Sec,
415 uint64_t &Res) const {
416 const Section *Sect = getSection(Sec);
418 return object_error::success;
421 template<bool is64Bits>
423 MachOObjectFile<is64Bits>::getSectionContents(DataRefImpl Sec,
424 StringRef &Res) const {
425 const Section *Sect = getSection(Sec);
426 Res = getData(Sect->Offset, Sect->Size);
427 return object_error::success;
430 template<bool is64Bits>
432 MachOObjectFile<is64Bits>::getSectionAlignment(DataRefImpl Sec,
433 uint64_t &Res) const {
434 const Section *Sect = getSection(Sec);
435 Res = uint64_t(1) << Sect->Align;
436 return object_error::success;
439 template<bool is64Bits>
441 MachOObjectFile<is64Bits>::isSectionText(DataRefImpl Sec, bool &Res) const {
442 const Section *Sect = getSection(Sec);
443 Res = Sect->Flags & macho::SF_PureInstructions;
444 return object_error::success;
447 template<bool is64Bits>
449 MachOObjectFile<is64Bits>::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
450 const Section *Sect = getSection(Sec);
451 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
452 Res = SectionType == MachO::SectionTypeZeroFill ||
453 SectionType == MachO::SectionTypeZeroFillLarge;
454 return object_error::success;
457 template<bool is64Bits>
459 MachOObjectFile<is64Bits>::getSectionRelEnd(DataRefImpl Sec) const {
460 const Section *Sect = getSection(Sec);
461 uint32_t LastReloc = Sect->NumRelocationTableEntries;
464 Ret.d.b = getSectionIndex(Sec);
465 return relocation_iterator(RelocationRef(Ret, this));
468 template<bool is64Bits>
470 MachOObjectFile<is64Bits>::getRelocationAddress(DataRefImpl Rel,
471 uint64_t &Res) const {
472 const Section *Sect = getSection(Sections[Rel.d.b]);
473 uint64_t SectAddress = Sect->Address;
474 const RelocationEntry *RE = getRelocation(Rel);
475 unsigned Arch = getArch();
476 bool isScattered = (Arch != Triple::x86_64) &&
477 (RE->Word0 & macho::RF_Scattered);
481 RelAddr = RE->Word0 & 0xFFFFFF;
485 Res = SectAddress + RelAddr;
486 return object_error::success;
489 template<bool is64Bits>
491 MachOObjectFile<is64Bits>::getRelocationOffset(DataRefImpl Rel,
492 uint64_t &Res) const {
493 const RelocationEntry *RE = getRelocation(Rel);
495 unsigned Arch = getArch();
496 bool isScattered = (Arch != Triple::x86_64) &&
497 (RE->Word0 & macho::RF_Scattered);
499 Res = RE->Word0 & 0xFFFFFF;
502 return object_error::success;
505 template<bool is64Bits>
507 MachOObjectFile<is64Bits>::getRelocationSymbol(DataRefImpl Rel,
508 SymbolRef &Res) const {
509 const RelocationEntry *RE = getRelocation(Rel);
510 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
511 bool isExtern = (RE->Word1 >> 27) & 1;
514 moveToNextSymbol(Sym);
516 for (unsigned i = 0; i < SymbolIdx; i++) {
518 moveToNextSymbol(Sym);
519 assert(Sym.d.a < getHeader()->NumLoadCommands &&
520 "Relocation symbol index out of range!");
523 Res = SymbolRef(Sym, this);
524 return object_error::success;
527 template<bool is64Bits>
529 MachOObjectFile<is64Bits>::getRelocationAdditionalInfo(DataRefImpl Rel,
530 int64_t &Res) const {
531 const RelocationEntry *RE = getRelocation(Rel);
532 bool isExtern = (RE->Word1 >> 27) & 1;
535 const uint8_t* sectAddress = base();
536 const Section *Sect = getSection(Sections[Rel.d.b]);
537 sectAddress += Sect->Offset;
538 Res = reinterpret_cast<uintptr_t>(sectAddress);
540 return object_error::success;
543 template<bool is64Bits>
544 error_code MachOObjectFile<is64Bits>::getRelocationType(DataRefImpl Rel,
545 uint64_t &Res) const {
546 const RelocationEntry *RE = getRelocation(Rel);
550 return object_error::success;
553 template<bool is64Bits>
555 MachOObjectFile<is64Bits>::getRelocationTypeName(DataRefImpl Rel,
556 SmallVectorImpl<char> &Result) const {
557 // TODO: Support scattered relocations.
559 const RelocationEntry *RE = getRelocation(Rel);
561 unsigned Arch = getArch();
562 bool isScattered = (Arch != Triple::x86_64) &&
563 (RE->Word0 & macho::RF_Scattered);
567 r_type = (RE->Word0 >> 24) & 0xF;
569 r_type = (RE->Word1 >> 28) & 0xF;
573 static const char *const Table[] = {
574 "GENERIC_RELOC_VANILLA",
575 "GENERIC_RELOC_PAIR",
576 "GENERIC_RELOC_SECTDIFF",
577 "GENERIC_RELOC_PB_LA_PTR",
578 "GENERIC_RELOC_LOCAL_SECTDIFF",
579 "GENERIC_RELOC_TLV" };
587 case Triple::x86_64: {
588 static const char *const Table[] = {
589 "X86_64_RELOC_UNSIGNED",
590 "X86_64_RELOC_SIGNED",
591 "X86_64_RELOC_BRANCH",
592 "X86_64_RELOC_GOT_LOAD",
594 "X86_64_RELOC_SUBTRACTOR",
595 "X86_64_RELOC_SIGNED_1",
596 "X86_64_RELOC_SIGNED_2",
597 "X86_64_RELOC_SIGNED_4",
598 "X86_64_RELOC_TLV" };
607 static const char *const Table[] = {
610 "ARM_RELOC_SECTDIFF",
611 "ARM_RELOC_LOCAL_SECTDIFF",
612 "ARM_RELOC_PB_LA_PTR",
614 "ARM_THUMB_RELOC_BR22",
615 "ARM_THUMB_32BIT_BRANCH",
617 "ARM_RELOC_HALF_SECTDIFF" };
626 static const char *const Table[] = {
635 "PPC_RELOC_SECTDIFF",
636 "PPC_RELOC_PB_LA_PTR",
637 "PPC_RELOC_HI16_SECTDIFF",
638 "PPC_RELOC_LO16_SECTDIFF",
639 "PPC_RELOC_HA16_SECTDIFF",
641 "PPC_RELOC_LO14_SECTDIFF",
642 "PPC_RELOC_LOCAL_SECTDIFF" };
647 case Triple::UnknownArch:
651 Result.append(res.begin(), res.end());
652 return object_error::success;
655 template<bool is64Bits>
657 MachOObjectFile<is64Bits>::getRelocationValueString(DataRefImpl Rel,
658 SmallVectorImpl<char> &Result) const {
659 const RelocationEntry *RE = getRelocation(Rel);
661 unsigned Arch = getArch();
662 bool isScattered = (Arch != Triple::x86_64) &&
663 (RE->Word0 & macho::RF_Scattered);
666 raw_string_ostream fmt(fmtbuf);
670 Type = (RE->Word0 >> 24) & 0xF;
672 Type = (RE->Word1 >> 28) & 0xF;
676 isPCRel = ((RE->Word0 >> 30) & 1);
678 isPCRel = ((RE->Word1 >> 24) & 1);
680 // Determine any addends that should be displayed with the relocation.
681 // These require decoding the relocation type, which is triple-specific.
683 // X86_64 has entirely custom relocation types.
684 if (Arch == Triple::x86_64) {
685 bool isPCRel = ((RE->Word1 >> 24) & 1);
688 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
689 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
690 printRelocationTargetName(RE, fmt);
692 if (isPCRel) fmt << "PCREL";
695 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
696 DataRefImpl RelNext = Rel;
698 const RelocationEntry *RENext = getRelocation(RelNext);
700 // X86_64_SUBTRACTOR must be followed by a relocation of type
701 // X86_64_RELOC_UNSIGNED.
702 // NOTE: Scattered relocations don't exist on x86_64.
703 unsigned RType = (RENext->Word1 >> 28) & 0xF;
705 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
706 "X86_64_RELOC_SUBTRACTOR.");
708 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
709 // X86_64_SUBTRACTOR contains to the subtrahend.
710 printRelocationTargetName(RENext, fmt);
712 printRelocationTargetName(RE, fmt);
715 case macho::RIT_X86_64_TLV:
716 printRelocationTargetName(RE, fmt);
718 if (isPCRel) fmt << "P";
720 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
721 printRelocationTargetName(RE, fmt);
724 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
725 printRelocationTargetName(RE, fmt);
728 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
729 printRelocationTargetName(RE, fmt);
733 printRelocationTargetName(RE, fmt);
736 // X86 and ARM share some relocation types in common.
737 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
738 // Generic relocation types...
740 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
741 return object_error::success;
742 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
743 DataRefImpl RelNext = Rel;
745 const RelocationEntry *RENext = getRelocation(RelNext);
747 // X86 sect diff's must be followed by a relocation of type
748 // GENERIC_RELOC_PAIR.
749 bool isNextScattered = (Arch != Triple::x86_64) &&
750 (RENext->Word0 & macho::RF_Scattered);
753 RType = (RENext->Word0 >> 24) & 0xF;
755 RType = (RENext->Word1 >> 28) & 0xF;
757 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
758 "GENERIC_RELOC_SECTDIFF.");
760 printRelocationTargetName(RE, fmt);
762 printRelocationTargetName(RENext, fmt);
767 if (Arch == Triple::x86) {
768 // All X86 relocations that need special printing were already
769 // handled in the generic code.
771 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
772 DataRefImpl RelNext = Rel;
774 const RelocationEntry *RENext = getRelocation(RelNext);
776 // X86 sect diff's must be followed by a relocation of type
777 // GENERIC_RELOC_PAIR.
778 bool isNextScattered = (Arch != Triple::x86_64) &&
779 (RENext->Word0 & macho::RF_Scattered);
782 RType = (RENext->Word0 >> 24) & 0xF;
784 RType = (RENext->Word1 >> 28) & 0xF;
786 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
787 "GENERIC_RELOC_LOCAL_SECTDIFF.");
789 printRelocationTargetName(RE, fmt);
791 printRelocationTargetName(RENext, fmt);
794 case macho::RIT_Generic_TLV: {
795 printRelocationTargetName(RE, fmt);
797 if (isPCRel) fmt << "P";
801 printRelocationTargetName(RE, fmt);
803 } else { // ARM-specific relocations
805 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
806 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
807 // Half relocations steal a bit from the length field to encode
808 // whether this is an upper16 or a lower16 relocation.
811 isUpper = (RE->Word0 >> 28) & 1;
813 isUpper = (RE->Word1 >> 25) & 1;
819 printRelocationTargetName(RE, fmt);
821 DataRefImpl RelNext = Rel;
823 const RelocationEntry *RENext = getRelocation(RelNext);
825 // ARM half relocs must be followed by a relocation of type
827 bool isNextScattered = (Arch != Triple::x86_64) &&
828 (RENext->Word0 & macho::RF_Scattered);
831 RType = (RENext->Word0 >> 24) & 0xF;
833 RType = (RENext->Word1 >> 28) & 0xF;
836 report_fatal_error("Expected ARM_RELOC_PAIR after "
837 "GENERIC_RELOC_HALF");
839 // NOTE: The half of the target virtual address is stashed in the
840 // address field of the secondary relocation, but we can't reverse
841 // engineer the constant offset from it without decoding the movw/movt
842 // instruction to find the other half in its immediate field.
844 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
845 // symbol/section pointer of the follow-on relocation.
846 if (Type == macho::RIT_ARM_HalfDifference) {
848 printRelocationTargetName(RENext, fmt);
855 printRelocationTargetName(RE, fmt);
860 printRelocationTargetName(RE, fmt);
863 Result.append(fmtbuf.begin(), fmtbuf.end());
864 return object_error::success;
867 template<bool is64Bits>
869 MachOObjectFile<is64Bits>::getRelocationHidden(DataRefImpl Rel,
870 bool &Result) const {
871 const RelocationEntry *RE = getRelocation(Rel);
873 unsigned Arch = getArch();
874 bool isScattered = (Arch != Triple::x86_64) &&
875 (RE->Word0 & macho::RF_Scattered);
878 Type = (RE->Word0 >> 24) & 0xF;
880 Type = (RE->Word1 >> 28) & 0xF;
884 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
886 if (Arch == Triple::x86 || Arch == Triple::arm) {
887 if (Type == macho::RIT_Pair) Result = true;
888 } else if (Arch == Triple::x86_64) {
889 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
890 // an X864_64_RELOC_SUBTRACTOR.
891 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
892 DataRefImpl RelPrev = Rel;
894 const RelocationEntry *REPrev = getRelocation(RelPrev);
896 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
898 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
902 return object_error::success;
905 template<bool is64Bits>
907 MachOObjectFile<is64Bits>::getSymbolFileOffset(DataRefImpl Symb,
908 uint64_t &Res) const {
909 const SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
911 if (Entry->SectionIndex) {
912 const Section *Sec = getSection(Sections[Entry->SectionIndex-1]);
913 Res += Sec->Offset - Sec->Address;
916 return object_error::success;
919 template<bool is64Bits>
921 MachOObjectFile<is64Bits>::sectionContainsSymbol(DataRefImpl Sec,
923 bool &Result) const {
925 getSymbolType(Symb, ST);
926 if (ST == SymbolRef::ST_Unknown) {
928 return object_error::success;
931 uint64_t SectBegin, SectEnd;
932 getSectionAddress(Sec, SectBegin);
933 getSectionSize(Sec, SectEnd);
934 SectEnd += SectBegin;
936 const SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
937 uint64_t SymAddr= Entry->Value;
938 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
940 return object_error::success;
943 template<bool is64Bits>
944 error_code MachOObjectFile<is64Bits>::getSymbolAddress(DataRefImpl Symb,
945 uint64_t &Res) const {
946 const SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
948 return object_error::success;
951 template<bool is64Bits>
952 error_code MachOObjectFile<is64Bits>::getSymbolSize(DataRefImpl DRI,
953 uint64_t &Result) const {
954 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
955 uint64_t BeginOffset;
956 uint64_t EndOffset = 0;
957 uint8_t SectionIndex;
959 const SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
960 BeginOffset = Entry->Value;
961 SectionIndex = Entry->SectionIndex;
963 uint32_t flags = SymbolRef::SF_None;
964 getSymbolFlags(DRI, flags);
965 if (flags & SymbolRef::SF_Common)
966 Result = Entry->Value;
968 Result = UnknownAddressOrSize;
969 return object_error::success;
971 // Unfortunately symbols are unsorted so we need to touch all
972 // symbols from load command
974 uint32_t Command = DRI.d.a;
975 while (Command == DRI.d.a) {
976 moveToNextSymbol(DRI);
977 if (DRI.d.a < LoadCommandCount) {
978 Entry = getSymbolTableEntry(DRI);
979 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
980 if (!EndOffset || Entry->Value < EndOffset)
981 EndOffset = Entry->Value;
987 getSectionSize(Sections[SectionIndex-1], Size);
988 getSectionAddress(Sections[SectionIndex-1], EndOffset);
991 Result = EndOffset - BeginOffset;
992 return object_error::success;
995 template<bool is64Bits>
996 error_code MachOObjectFile<is64Bits>::getSectionNext(DataRefImpl Sec,
997 SectionRef &Res) const {
999 moveToNextSection(Sec);
1000 Res = SectionRef(Sec, this);
1001 return object_error::success;
1004 template<bool is64Bits>
1005 section_iterator MachOObjectFile<is64Bits>::begin_sections() const {
1007 moveToNextSection(DRI);
1008 return section_iterator(SectionRef(DRI, this));
1011 template<bool is64Bits>
1012 void MachOObjectFile<is64Bits>::moveToNextSection(DataRefImpl &DRI) const {
1013 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
1014 while (DRI.d.a < LoadCommandCount) {
1015 const LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
1016 if (Command->Type == SegmentLoadType) {
1017 const SegmentLoadCommand *SegmentLoadCmd =
1018 reinterpret_cast<const SegmentLoadCommand*>(Command);
1019 if (DRI.d.b < SegmentLoadCmd->NumSections)