1 //===- AArch64Relocator.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_AARCH64_AARCH64RELOCATOR_H_ 10 #define TARGET_AARCH64_AARCH64RELOCATOR_H_ 11 12 #include "mcld/LD/Relocator.h" 13 #include "mcld/Target/GOT.h" 14 #include "mcld/Target/KeyEntryMap.h" 15 #include "AArch64LDBackend.h" 16 17 namespace mcld { 18 19 /** \class AArch64Relocator 20 * \brief AArch64Relocator creates and destroys the AArch64 relocations. 21 * 22 */ 23 class AArch64Relocator : public Relocator { 24 public: 25 typedef KeyEntryMap<ResolveInfo, AArch64GOTEntry> SymGOTMap; 26 typedef KeyEntryMap<ResolveInfo, AArch64PLT1> SymPLTMap; 27 typedef KeyEntryMap<Relocation, Relocation> RelRelMap; 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 AArch64, 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 { Default = 0, SymVal = 1 }; 60 61 public: 62 AArch64Relocator(AArch64GNULDBackend& pParent, const LinkerConfig& pConfig); 63 ~AArch64Relocator(); 64 65 Result applyRelocation(Relocation& pRelocation); 66 67 AArch64GNULDBackend& getTarget() { return m_Target; } 68 69 const AArch64GNULDBackend& getTarget() const { return m_Target; } 70 71 const char* getName(Relocation::Type pType) const; 72 73 Size getSize(Relocation::Type pType) const; 74 75 const SymGOTMap& getSymGOTMap() const { return m_SymGOTMap; } 76 SymGOTMap& getSymGOTMap() { return m_SymGOTMap; } 77 78 const SymPLTMap& getSymPLTMap() const { return m_SymPLTMap; } 79 SymPLTMap& getSymPLTMap() { return m_SymPLTMap; } 80 81 const SymGOTMap& getSymGOTPLTMap() const { return m_SymGOTPLTMap; } 82 SymGOTMap& getSymGOTPLTMap() { return m_SymGOTPLTMap; } 83 84 const RelRelMap& getRelRelMap() const { return m_RelRelMap; } 85 RelRelMap& getRelRelMap() { return m_RelRelMap; } 86 87 /// scanRelocation - determine the empty entries are needed or not and create 88 /// the empty entries if needed. 89 /// For AArch64, following entries are check to create: 90 /// - GOT entry (for .got section) 91 /// - PLT entry (for .plt section) 92 /// - dynamin relocation entries (for .rel.plt and .rel.dyn sections) 93 void scanRelocation(Relocation& pReloc, 94 IRBuilder& pBuilder, 95 Module& pModule, 96 LDSection& pSection, 97 Input& pInput); 98 99 /// getDebugStringOffset - get the offset from the relocation target. This is 100 /// used to get the debug string offset. 101 uint32_t getDebugStringOffset(Relocation& pReloc) const; 102 103 /// applyDebugStringOffset - apply the relocation target to specific offset. 104 /// This is used to set the debug string offset. 105 void applyDebugStringOffset(Relocation& pReloc, uint32_t pOffset); 106 107 private: 108 void scanLocalReloc(Relocation& pReloc, const LDSection& pSection); 109 110 void scanGlobalReloc(Relocation& pReloc, 111 IRBuilder& pBuilder, 112 const LDSection& pSection); 113 114 /// addCopyReloc - add a copy relocation into .rel.dyn for pSym 115 /// @param pSym - A resolved copy symbol that defined in BSS section 116 void addCopyReloc(ResolveInfo& pSym); 117 118 /// defineSymbolforCopyReloc - allocate a space in BSS section and 119 /// and force define the copy of pSym to BSS section 120 /// @return the output LDSymbol of the copy symbol 121 LDSymbol& defineSymbolforCopyReloc(IRBuilder& pLinker, 122 const ResolveInfo& pSym); 123 124 private: 125 AArch64GNULDBackend& m_Target; 126 SymGOTMap m_SymGOTMap; 127 SymPLTMap m_SymPLTMap; 128 SymGOTMap m_SymGOTPLTMap; 129 RelRelMap m_RelRelMap; 130 }; 131 132 } // namespace mcld 133 134 #endif // TARGET_AARCH64_AARCH64RELOCATOR_H_ 135