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;
117 operator uint32_t() const {
118 return (bytes[2] << 24) | (bytes[1] << 16) | bytes[0];
122 template<endianness TargetEndianness>
123 struct RelocationEntry {
124 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
126 MachOInt24 SymbolNum;
129 unsigned getPCRel() const {
132 unsigned getLength() const {
133 return (Bits >> 1) & 0x3;
135 unsigned getExternal() const {
136 return (Bits >> 3) & 0x1;
138 unsigned getType() const {
143 template<endianness TargetEndianness>
144 struct ScatteredRelocationEntry {
145 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
150 unsigned getType() const {
153 unsigned getLength() const {
154 return (Bits >> 4) & 0x3;
156 unsigned getPCRel() const {
157 return (Bits >> 6) & 0x1;
159 unsigned getScattered() const {
164 template<endianness TargetEndianness>
165 struct SymbolTableEntryBase {
166 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
167 MachOInt32 StringIndex;
169 uint8_t SectionIndex;
173 template<class MachOT>
174 struct SymbolTableEntry;
176 template<endianness TargetEndianness, bool Is64Bits>
177 struct SymbolTableEntry<MachOType<TargetEndianness, Is64Bits> > {
178 LLVM_MACHO_IMPORT_TYPES(MachOType, TargetEndianness, Is64Bits)
179 MachOInt32 StringIndex;
181 uint8_t SectionIndex;
186 template<endianness TargetEndianness>
188 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
193 template<endianness TargetEndianness>
194 struct SymtabLoadCommand {
195 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
198 MachOInt32 SymbolTableOffset;
199 MachOInt32 NumSymbolTableEntries;
200 MachOInt32 StringTableOffset;
201 MachOInt32 StringTableSize;
204 template<class MachOT>
205 struct SegmentLoadCommand;
207 template<endianness TargetEndianness, bool Is64Bits>
208 struct SegmentLoadCommand<MachOType<TargetEndianness, Is64Bits> > {
209 LLVM_MACHO_IMPORT_TYPES(MachOType, TargetEndianness, Is64Bits)
213 MachOIntPtr VMAddress;
215 MachOIntPtr FileOffset;
216 MachOIntPtr FileSize;
217 MachOInt32 MaxVMProtection;
218 MachOInt32 InitialVMProtection;
219 MachOInt32 NumSections;
223 template<endianness TargetEndianness>
224 struct LinkeditDataLoadCommand {
225 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
228 MachOInt32 DataOffset;
232 template<endianness TargetEndianness>
234 LLVM_MACHOB_IMPORT_TYPES(TargetEndianness)
237 MachOInt32 CPUSubtype;
239 MachOInt32 NumLoadCommands;
240 MachOInt32 SizeOfLoadCommands;
245 class MachOObjectFileBase : public ObjectFile {
247 typedef MachOFormat::SymbolTableEntryBase<support::little>
248 SymbolTableEntryBase;
249 typedef MachOFormat::SymtabLoadCommand<support::little> SymtabLoadCommand;
250 typedef MachOFormat::RelocationEntry<support::little> RelocationEntry;
251 typedef MachOFormat::ScatteredRelocationEntry<support::little>
252 ScatteredRelocationEntry;
253 typedef MachOFormat::SectionBase SectionBase;
254 typedef MachOFormat::LoadCommand<support::little> LoadCommand;
255 typedef MachOFormat::Header<support::little> Header;
256 typedef MachOFormat::LinkeditDataLoadCommand<support::little>
257 LinkeditDataLoadCommand;
259 MachOObjectFileBase(MemoryBuffer *Object, bool Is64Bits, error_code &ec);
261 virtual symbol_iterator begin_symbols() const;
262 virtual symbol_iterator end_symbols() const;
263 virtual symbol_iterator begin_dynamic_symbols() const;
264 virtual symbol_iterator end_dynamic_symbols() const;
265 virtual library_iterator begin_libraries_needed() const;
266 virtual library_iterator end_libraries_needed() const;
267 virtual section_iterator end_sections() const;
269 virtual uint8_t getBytesInAddress() const;
270 virtual StringRef getFileFormatName() const;
271 virtual unsigned getArch() const;
272 virtual StringRef getLoadName() const;
274 // In a MachO file, sections have a segment name. This is used in the .o
275 // files. They have a single segment, but this field specifies which segment
276 // a section should be put in in the final object.
277 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
279 // Names are stored as 16 bytes. These returns the raw 16 bytes without
280 // interpreting them as a C string.
281 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
282 ArrayRef<char>getSectionRawFinalSegmentName(DataRefImpl Sec) const;
284 bool is64Bit() const;
285 const LoadCommand *getLoadCommandInfo(unsigned Index) const;
286 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
287 const Header *getHeader() const;
288 unsigned getHeaderSize() const;
289 StringRef getData(size_t Offset, size_t Size) const;
290 const RelocationEntry *getRelocation(DataRefImpl Rel) const;
291 bool isScattered(const RelocationEntry *RE) const;
292 bool isPCRel(const RelocationEntry *RE) const;
293 unsigned getLength(const RelocationEntry *RE) const;
294 unsigned getType(const RelocationEntry *RE) const;
296 static inline bool classof(const Binary *v) {
301 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
302 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
303 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
304 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
305 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
306 virtual error_code getSymbolSection(DataRefImpl Symb,
307 section_iterator &Res) const;
308 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
309 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
310 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
311 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
312 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
314 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
315 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
316 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
318 virtual error_code getRelocationNext(DataRefImpl Rel,
319 RelocationRef &Res) const;
321 virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
322 virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
323 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
326 std::size_t getSectionIndex(DataRefImpl Sec) const;
328 typedef SmallVector<DataRefImpl, 1> SectionList;
329 SectionList Sections;
331 void moveToNextSymbol(DataRefImpl &DRI) const;
332 void printRelocationTargetName(const RelocationEntry *RE,
333 raw_string_ostream &fmt) const;
334 const SectionBase *getSectionBase(DataRefImpl DRI) const;
335 const SymbolTableEntryBase *getSymbolTableEntryBase(DataRefImpl DRI) const;
339 const SymbolTableEntryBase *getSymbolTableEntryBase(DataRefImpl DRI,
340 const SymtabLoadCommand *SymtabLoadCmd) const;
343 template<class MachOT>
344 struct MachOObjectFileHelperCommon;
346 template<endianness TargetEndianness, bool Is64Bits>
347 struct MachOObjectFileHelperCommon<MachOType<TargetEndianness, Is64Bits> > {
349 MachOFormat::SegmentLoadCommand<MachOType<TargetEndianness, Is64Bits> >
351 typedef MachOFormat::SymbolTableEntry<MachOType<TargetEndianness, Is64Bits> >
353 typedef MachOFormat::Section<MachOType<TargetEndianness, Is64Bits> > Section;
356 template<class MachOT>
357 struct MachOObjectFileHelper;
359 template<endianness TargetEndianness>
360 struct MachOObjectFileHelper<MachOType<TargetEndianness, false> > :
361 public MachOObjectFileHelperCommon<MachOType<TargetEndianness, false> > {
362 static const macho::LoadCommandType SegmentLoadType = macho::LCT_Segment;
365 template<endianness TargetEndianness>
366 struct MachOObjectFileHelper<MachOType<TargetEndianness, true> > :
367 public MachOObjectFileHelperCommon<MachOType<TargetEndianness, true> > {
368 static const macho::LoadCommandType SegmentLoadType = macho::LCT_Segment64;
371 template<class MachOT>
372 class MachOObjectFile : public MachOObjectFileBase {
374 static const endianness TargetEndianness = MachOT::TargetEndianness;
375 static const bool Is64Bits = MachOT::Is64Bits;
377 typedef MachOObjectFileHelper<MachOT> Helper;
378 static const macho::LoadCommandType SegmentLoadType = Helper::SegmentLoadType;
379 typedef typename Helper::SegmentLoadCommand SegmentLoadCommand;
380 typedef typename Helper::SymbolTableEntry SymbolTableEntry;
381 typedef typename Helper::Section Section;
383 MachOObjectFile(MemoryBuffer *Object, error_code &ec);
384 static bool classof(const Binary *v);
386 const Section *getSection(DataRefImpl DRI) const;
387 const SymbolTableEntry *getSymbolTableEntry(DataRefImpl DRI) const;
388 const RelocationEntry *getRelocation(DataRefImpl Rel) const;
390 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
391 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
392 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
393 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
394 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
395 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
396 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
397 virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
398 virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
399 virtual error_code getRelocationSymbol(DataRefImpl Rel, SymbolRef &Res) const;
400 virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
401 virtual error_code getRelocationTypeName(DataRefImpl Rel,
402 SmallVectorImpl<char> &Result) const;
403 virtual error_code getRelocationValueString(DataRefImpl Rel,
404 SmallVectorImpl<char> &Result) const;
405 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
406 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
407 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
409 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
410 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
411 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
412 virtual section_iterator begin_sections() const;
413 void moveToNextSection(DataRefImpl &DRI) const;
416 template<class MachOT>
417 MachOObjectFile<MachOT>::MachOObjectFile(MemoryBuffer *Object,
419 MachOObjectFileBase(Object, Is64Bits, ec) {
421 moveToNextSection(DRI);
422 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
423 while (DRI.d.a < LoadCommandCount) {
424 Sections.push_back(DRI);
426 moveToNextSection(DRI);
430 template<class MachOT>
431 bool MachOObjectFile<MachOT>::classof(const Binary *v) {
432 return v->getType() == getMachOType(true, Is64Bits);
435 template<class MachOT>
436 const typename MachOObjectFile<MachOT>::Section *
437 MachOObjectFile<MachOT>::getSection(DataRefImpl DRI) const {
438 const SectionBase *Addr = getSectionBase(DRI);
439 return reinterpret_cast<const Section*>(Addr);
442 template<class MachOT>
443 const typename MachOObjectFile<MachOT>::SymbolTableEntry *
444 MachOObjectFile<MachOT>::getSymbolTableEntry(DataRefImpl DRI) const {
445 const SymbolTableEntryBase *Base = getSymbolTableEntryBase(DRI);
446 return reinterpret_cast<const SymbolTableEntry*>(Base);
449 template<class MachOT>
450 const typename MachOObjectFile<MachOT>::RelocationEntry *
451 MachOObjectFile<MachOT>::getRelocation(DataRefImpl Rel) const {
452 const Section *Sect = getSection(Sections[Rel.d.b]);
453 uint32_t RelOffset = Sect->RelocationTableOffset;
454 uint64_t Offset = RelOffset + Rel.d.a * sizeof(RelocationEntry);
455 StringRef Data = getData(Offset, sizeof(RelocationEntry));
456 return reinterpret_cast<const RelocationEntry*>(Data.data());
459 template<class MachOT>
461 MachOObjectFile<MachOT>::getSectionAddress(DataRefImpl Sec,
462 uint64_t &Res) const {
463 const Section *Sect = getSection(Sec);
465 return object_error::success;
468 template<class MachOT>
470 MachOObjectFile<MachOT>::getSectionSize(DataRefImpl Sec,
471 uint64_t &Res) const {
472 const Section *Sect = getSection(Sec);
474 return object_error::success;
477 template<class MachOT>
479 MachOObjectFile<MachOT>::getSectionContents(DataRefImpl Sec,
480 StringRef &Res) const {
481 const Section *Sect = getSection(Sec);
482 Res = getData(Sect->Offset, Sect->Size);
483 return object_error::success;
486 template<class MachOT>
488 MachOObjectFile<MachOT>::getSectionAlignment(DataRefImpl Sec,
489 uint64_t &Res) const {
490 const Section *Sect = getSection(Sec);
491 Res = uint64_t(1) << Sect->Align;
492 return object_error::success;
495 template<class MachOT>
497 MachOObjectFile<MachOT>::isSectionText(DataRefImpl Sec, bool &Res) const {
498 const Section *Sect = getSection(Sec);
499 Res = Sect->Flags & macho::SF_PureInstructions;
500 return object_error::success;
503 template<class MachOT>
505 MachOObjectFile<MachOT>::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
506 const Section *Sect = getSection(Sec);
507 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
508 Res = SectionType == MachO::SectionTypeZeroFill ||
509 SectionType == MachO::SectionTypeZeroFillLarge;
510 return object_error::success;
513 template<class MachOT>
515 MachOObjectFile<MachOT>::getSectionRelEnd(DataRefImpl Sec) const {
516 const Section *Sect = getSection(Sec);
517 uint32_t LastReloc = Sect->NumRelocationTableEntries;
520 Ret.d.b = getSectionIndex(Sec);
521 return relocation_iterator(RelocationRef(Ret, this));
524 template<class MachOT>
526 MachOObjectFile<MachOT>::getRelocationAddress(DataRefImpl Rel,
527 uint64_t &Res) const {
528 const Section *Sect = getSection(Sections[Rel.d.b]);
529 uint64_t SectAddress = Sect->Address;
530 const RelocationEntry *RE = getRelocation(Rel);
534 RelAddr = RE->Address & 0xFFFFFF;
536 RelAddr = RE->Address;
538 Res = SectAddress + RelAddr;
539 return object_error::success;
542 template<class MachOT>
544 MachOObjectFile<MachOT>::getRelocationOffset(DataRefImpl Rel,
545 uint64_t &Res) const {
546 const RelocationEntry *RE = getRelocation(Rel);
548 Res = RE->Address & 0xFFFFFF;
551 return object_error::success;
554 template<class MachOT>
556 MachOObjectFile<MachOT>::getRelocationSymbol(DataRefImpl Rel,
557 SymbolRef &Res) const {
558 const RelocationEntry *RE = getRelocation(Rel);
559 uint32_t SymbolIdx = RE->SymbolNum;
560 bool isExtern = RE->getExternal();
563 moveToNextSymbol(Sym);
565 for (unsigned i = 0; i < SymbolIdx; i++) {
567 moveToNextSymbol(Sym);
568 assert(Sym.d.a < getHeader()->NumLoadCommands &&
569 "Relocation symbol index out of range!");
572 Res = SymbolRef(Sym, this);
573 return object_error::success;
576 template<class MachOT>
577 error_code MachOObjectFile<MachOT>::getRelocationType(DataRefImpl Rel,
578 uint64_t &Res) const {
579 const RelocationEntry *RE = getRelocation(Rel);
581 return object_error::success;
584 template<class MachOT>
586 MachOObjectFile<MachOT>::getRelocationTypeName(DataRefImpl Rel,
587 SmallVectorImpl<char> &Result) const {
588 // TODO: Support scattered relocations.
590 const RelocationEntry *RE = getRelocation(Rel);
592 unsigned Arch = getArch();
594 unsigned r_type = getType(RE);
598 static const char *const Table[] = {
599 "GENERIC_RELOC_VANILLA",
600 "GENERIC_RELOC_PAIR",
601 "GENERIC_RELOC_SECTDIFF",
602 "GENERIC_RELOC_PB_LA_PTR",
603 "GENERIC_RELOC_LOCAL_SECTDIFF",
604 "GENERIC_RELOC_TLV" };
612 case Triple::x86_64: {
613 static const char *const Table[] = {
614 "X86_64_RELOC_UNSIGNED",
615 "X86_64_RELOC_SIGNED",
616 "X86_64_RELOC_BRANCH",
617 "X86_64_RELOC_GOT_LOAD",
619 "X86_64_RELOC_SUBTRACTOR",
620 "X86_64_RELOC_SIGNED_1",
621 "X86_64_RELOC_SIGNED_2",
622 "X86_64_RELOC_SIGNED_4",
623 "X86_64_RELOC_TLV" };
632 static const char *const Table[] = {
635 "ARM_RELOC_SECTDIFF",
636 "ARM_RELOC_LOCAL_SECTDIFF",
637 "ARM_RELOC_PB_LA_PTR",
639 "ARM_THUMB_RELOC_BR22",
640 "ARM_THUMB_32BIT_BRANCH",
642 "ARM_RELOC_HALF_SECTDIFF" };
651 static const char *const Table[] = {
660 "PPC_RELOC_SECTDIFF",
661 "PPC_RELOC_PB_LA_PTR",
662 "PPC_RELOC_HI16_SECTDIFF",
663 "PPC_RELOC_LO16_SECTDIFF",
664 "PPC_RELOC_HA16_SECTDIFF",
666 "PPC_RELOC_LO14_SECTDIFF",
667 "PPC_RELOC_LOCAL_SECTDIFF" };
672 case Triple::UnknownArch:
676 Result.append(res.begin(), res.end());
677 return object_error::success;
680 template<class MachOT>
682 MachOObjectFile<MachOT>::getRelocationValueString(DataRefImpl Rel,
683 SmallVectorImpl<char> &Result) const {
684 const RelocationEntry *RE = getRelocation(Rel);
686 unsigned Arch = getArch();
687 bool IsScattered = isScattered(RE);
690 raw_string_ostream fmt(fmtbuf);
692 unsigned Type = getType(RE);
693 bool IsPCRel = isPCRel(RE);
695 // Determine any addends that should be displayed with the relocation.
696 // These require decoding the relocation type, which is triple-specific.
698 // X86_64 has entirely custom relocation types.
699 if (Arch == Triple::x86_64) {
700 bool isPCRel = RE->getPCRel();
703 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
704 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
705 printRelocationTargetName(RE, fmt);
707 if (isPCRel) fmt << "PCREL";
710 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
711 DataRefImpl RelNext = Rel;
713 const RelocationEntry *RENext = getRelocation(RelNext);
715 // X86_64_SUBTRACTOR must be followed by a relocation of type
716 // X86_64_RELOC_UNSIGNED.
717 // NOTE: Scattered relocations don't exist on x86_64.
718 unsigned RType = RENext->getType();
720 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
721 "X86_64_RELOC_SUBTRACTOR.");
723 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
724 // X86_64_SUBTRACTOR contains to the subtrahend.
725 printRelocationTargetName(RENext, fmt);
727 printRelocationTargetName(RE, fmt);
730 case macho::RIT_X86_64_TLV:
731 printRelocationTargetName(RE, fmt);
733 if (isPCRel) fmt << "P";
735 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
736 printRelocationTargetName(RE, fmt);
739 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
740 printRelocationTargetName(RE, fmt);
743 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
744 printRelocationTargetName(RE, fmt);
748 printRelocationTargetName(RE, fmt);
751 // X86 and ARM share some relocation types in common.
752 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
753 // Generic relocation types...
755 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
756 return object_error::success;
757 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
758 DataRefImpl RelNext = Rel;
760 const RelocationEntry *RENext = getRelocation(RelNext);
762 // X86 sect diff's must be followed by a relocation of type
763 // GENERIC_RELOC_PAIR.
764 bool isNextScattered = (Arch != Triple::x86_64) &&
765 (RENext->Address & macho::RF_Scattered);
768 RType = (RENext->Address >> 24) & 0xF;
770 RType = RENext->getType();
772 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
773 "GENERIC_RELOC_SECTDIFF.");
775 printRelocationTargetName(RE, fmt);
777 printRelocationTargetName(RENext, fmt);
782 if (Arch == Triple::x86) {
783 // All X86 relocations that need special printing were already
784 // handled in the generic code.
786 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
787 DataRefImpl RelNext = Rel;
789 const RelocationEntry *RENext = getRelocation(RelNext);
791 // X86 sect diff's must be followed by a relocation of type
792 // GENERIC_RELOC_PAIR.
793 bool isNextScattered = (Arch != Triple::x86_64) &&
794 (RENext->Address & macho::RF_Scattered);
797 RType = (RENext->Address >> 24) & 0xF;
799 RType = RENext->getType();
801 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
802 "GENERIC_RELOC_LOCAL_SECTDIFF.");
804 printRelocationTargetName(RE, fmt);
806 printRelocationTargetName(RENext, fmt);
809 case macho::RIT_Generic_TLV: {
810 printRelocationTargetName(RE, fmt);
812 if (IsPCRel) fmt << "P";
816 printRelocationTargetName(RE, fmt);
818 } else { // ARM-specific relocations
820 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
821 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
822 // Half relocations steal a bit from the length field to encode
823 // whether this is an upper16 or a lower16 relocation.
826 isUpper = (RE->Address >> 28) & 1;
828 isUpper = (RE->getLength() >> 1) & 1;
834 printRelocationTargetName(RE, fmt);
836 DataRefImpl RelNext = Rel;
838 const RelocationEntry *RENext = getRelocation(RelNext);
840 // ARM half relocs must be followed by a relocation of type
842 bool isNextScattered = (Arch != Triple::x86_64) &&
843 (RENext->Address & macho::RF_Scattered);
846 RType = (RENext->Address >> 24) & 0xF;
848 RType = RENext->getType();
851 report_fatal_error("Expected ARM_RELOC_PAIR after "
852 "GENERIC_RELOC_HALF");
854 // NOTE: The half of the target virtual address is stashed in the
855 // address field of the secondary relocation, but we can't reverse
856 // engineer the constant offset from it without decoding the movw/movt
857 // instruction to find the other half in its immediate field.
859 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
860 // symbol/section pointer of the follow-on relocation.
861 if (Type == macho::RIT_ARM_HalfDifference) {
863 printRelocationTargetName(RENext, fmt);
870 printRelocationTargetName(RE, fmt);
875 printRelocationTargetName(RE, fmt);
878 Result.append(fmtbuf.begin(), fmtbuf.end());
879 return object_error::success;
882 template<class MachOT>
884 MachOObjectFile<MachOT>::getRelocationHidden(DataRefImpl Rel,
885 bool &Result) const {
886 const RelocationEntry *RE = getRelocation(Rel);
887 unsigned Arch = getArch();
888 unsigned Type = getType(RE);
892 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
894 if (Arch == Triple::x86 || Arch == Triple::arm) {
895 if (Type == macho::RIT_Pair) Result = true;
896 } else if (Arch == Triple::x86_64) {
897 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
898 // an X864_64_RELOC_SUBTRACTOR.
899 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
900 DataRefImpl RelPrev = Rel;
902 const RelocationEntry *REPrev = getRelocation(RelPrev);
904 unsigned PrevType = REPrev->getType();
906 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
910 return object_error::success;
913 template<class MachOT>
915 MachOObjectFile<MachOT>::getSymbolFileOffset(DataRefImpl Symb,
916 uint64_t &Res) const {
917 const SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
919 if (Entry->SectionIndex) {
920 const Section *Sec = getSection(Sections[Entry->SectionIndex-1]);
921 Res += Sec->Offset - Sec->Address;
924 return object_error::success;
927 template<class MachOT>
929 MachOObjectFile<MachOT>::sectionContainsSymbol(DataRefImpl Sec,
931 bool &Result) const {
933 getSymbolType(Symb, ST);
934 if (ST == SymbolRef::ST_Unknown) {
936 return object_error::success;
939 uint64_t SectBegin, SectEnd;
940 getSectionAddress(Sec, SectBegin);
941 getSectionSize(Sec, SectEnd);
942 SectEnd += SectBegin;
944 const SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
945 uint64_t SymAddr= Entry->Value;
946 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
948 return object_error::success;
951 template<class MachOT>
952 error_code MachOObjectFile<MachOT>::getSymbolAddress(DataRefImpl Symb,
953 uint64_t &Res) const {
954 const SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
956 return object_error::success;
959 template<class MachOT>
960 error_code MachOObjectFile<MachOT>::getSymbolSize(DataRefImpl DRI,
961 uint64_t &Result) const {
962 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
963 uint64_t BeginOffset;
964 uint64_t EndOffset = 0;
965 uint8_t SectionIndex;
967 const SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
968 BeginOffset = Entry->Value;
969 SectionIndex = Entry->SectionIndex;
971 uint32_t flags = SymbolRef::SF_None;
972 getSymbolFlags(DRI, flags);
973 if (flags & SymbolRef::SF_Common)
974 Result = Entry->Value;
976 Result = UnknownAddressOrSize;
977 return object_error::success;
979 // Unfortunately symbols are unsorted so we need to touch all
980 // symbols from load command
982 uint32_t Command = DRI.d.a;
983 while (Command == DRI.d.a) {
984 moveToNextSymbol(DRI);
985 if (DRI.d.a < LoadCommandCount) {
986 Entry = getSymbolTableEntry(DRI);
987 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
988 if (!EndOffset || Entry->Value < EndOffset)
989 EndOffset = Entry->Value;
995 getSectionSize(Sections[SectionIndex-1], Size);
996 getSectionAddress(Sections[SectionIndex-1], EndOffset);
999 Result = EndOffset - BeginOffset;
1000 return object_error::success;
1003 template<class MachOT>
1004 error_code MachOObjectFile<MachOT>::getSectionNext(DataRefImpl Sec,
1005 SectionRef &Res) const {
1007 moveToNextSection(Sec);
1008 Res = SectionRef(Sec, this);
1009 return object_error::success;
1012 template<class MachOT>
1013 section_iterator MachOObjectFile<MachOT>::begin_sections() const {
1015 moveToNextSection(DRI);
1016 return section_iterator(SectionRef(DRI, this));
1019 template<class MachOT>
1020 void MachOObjectFile<MachOT>::moveToNextSection(DataRefImpl &DRI) const {
1021 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
1022 while (DRI.d.a < LoadCommandCount) {
1023 const LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
1024 if (Command->Type == SegmentLoadType) {
1025 const SegmentLoadCommand *SegmentLoadCmd =
1026 reinterpret_cast<const SegmentLoadCommand*>(Command);
1027 if (DRI.d.b < SegmentLoadCmd->NumSections)
1036 typedef MachOObjectFile<MachOType<support::little, false> >
1037 MachOObjectFile32Le;
1038 typedef MachOObjectFile<MachOType<support::little, true> >
1039 MachOObjectFile64Le;