1 //===- ARMRelocator.h ----------------------------------------------------===// 2 // 3 // The MCLinker Project 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 #ifndef TARGET_ARM_ARMRELOCATOR_H 10 #define TARGET_ARM_ARMRELOCATOR_H 11 12 #include <mcld/LD/Relocator.h> 13 #include <mcld/Target/GOT.h> 14 #include <mcld/Target/KeyEntryMap.h> 15 #include "ARMLDBackend.h" 16 17 namespace mcld { 18 19 /** \class ARMRelocator 20 * \brief ARMRelocator creates and destroys the ARM relocations. 21 * 22 */ 23 class ARMRelocator : public Relocator 24 { 25 public: 26 typedef KeyEntryMap<ResolveInfo, ARMGOTEntry> SymGOTMap; 27 typedef KeyEntryMap<ResolveInfo, ARMPLT1> SymPLTMap; 28 29 /** \enum ReservedEntryType 30 * \brief The reserved entry type of reserved space in ResolveInfo. 31 * 32 * This is used for sacnRelocation to record what kinds of entries are 33 * reserved for this resolved symbol In ARM, there are three kinds of 34 * entries, GOT, PLT, and dynamic reloction. 35 * 36 * bit: 3 2 1 0 37 * | | PLT | GOT | Rel | 38 * 39 * value Name - Description 40 * 41 * 0000 None - no reserved entry 42 * 0001 ReserveRel - reserve an dynamic relocation entry 43 * 0010 ReserveGOT - reserve an GOT entry 44 * 0100 ReservePLT - reserve an PLT entry and the corresponding GOT, 45 * 46 */ 47 enum ReservedEntryType { 48 None = 0, 49 ReserveRel = 1, 50 ReserveGOT = 2, 51 ReservePLT = 4, 52 }; 53 54 /** \enum EntryValue 55 * \brief The value of the entries. The symbol value will be decided at after 56 * layout, so we mark the entry during scanRelocation and fill up the actual 57 * value when applying relocations. 58 */ 59 enum EntryValue { 60 Default = 0, 61 SymVal = 1 62 }; 63 64 public: 65 ARMRelocator(ARMGNULDBackend& pParent, const LinkerConfig& pConfig); 66 ~ARMRelocator(); 67 68 Result applyRelocation(Relocation& pRelocation); 69 getTarget()70 ARMGNULDBackend& getTarget() 71 { return m_Target; } 72 getTarget()73 const ARMGNULDBackend& getTarget() const 74 { return m_Target; } 75 76 const char* getName(Relocation::Type pType) const; 77 78 Size getSize(Relocation::Type pType) const; 79 getSymGOTMap()80 const SymGOTMap& getSymGOTMap() const { return m_SymGOTMap; } getSymGOTMap()81 SymGOTMap& getSymGOTMap() { return m_SymGOTMap; } 82 getSymPLTMap()83 const SymPLTMap& getSymPLTMap() const { return m_SymPLTMap; } getSymPLTMap()84 SymPLTMap& getSymPLTMap() { return m_SymPLTMap; } 85 getSymGOTPLTMap()86 const SymGOTMap& getSymGOTPLTMap() const { return m_SymGOTPLTMap; } getSymGOTPLTMap()87 SymGOTMap& getSymGOTPLTMap() { return m_SymGOTPLTMap; } 88 89 /// scanRelocation - determine the empty entries are needed or not and create 90 /// the empty entries if needed. 91 /// For ARM, following entries are check to create: 92 /// - GOT entry (for .got section) 93 /// - PLT entry (for .plt section) 94 /// - dynamin relocation entries (for .rel.plt and .rel.dyn sections) 95 void scanRelocation(Relocation& pReloc, 96 IRBuilder& pBuilder, 97 Module& pModule, 98 LDSection& pSection, 99 Input& pInput); 100 101 102 /// mayHaveFunctionPointerAccess - check if the given reloc would possibly 103 /// access a function pointer. 104 virtual bool mayHaveFunctionPointerAccess(const Relocation& pReloc) const; 105 106 private: 107 void scanLocalReloc(Relocation& pReloc, const LDSection& pSection); 108 109 void scanGlobalReloc(Relocation& pReloc, 110 IRBuilder& pBuilder, 111 const LDSection& pSection); 112 113 void checkValidReloc(Relocation& pReloc) const; 114 115 /// addCopyReloc - add a copy relocation into .rel.dyn for pSym 116 /// @param pSym - A resolved copy symbol that defined in BSS section 117 void addCopyReloc(ResolveInfo& pSym); 118 119 /// defineSymbolforCopyReloc - allocate a space in BSS section and 120 /// and force define the copy of pSym to BSS section 121 /// @return the output LDSymbol of the copy symbol 122 LDSymbol& defineSymbolforCopyReloc(IRBuilder& pLinker, 123 const ResolveInfo& pSym); 124 125 private: 126 ARMGNULDBackend& m_Target; 127 SymGOTMap m_SymGOTMap; 128 SymPLTMap m_SymPLTMap; 129 SymGOTMap m_SymGOTPLTMap; 130 }; 131 132 } // namespace of mcld 133 134 #endif 135 136