1 //===- MipsRelocator.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_MIPS_MIPSRELOCATOR_H_ 10 #define TARGET_MIPS_MIPSRELOCATOR_H_ 11 12 #include "mcld/LD/Relocator.h" 13 #include "mcld/Support/GCFactory.h" 14 #include "MipsLDBackend.h" 15 16 #include <llvm/ADT/DenseMapInfo.h> 17 18 namespace mcld { 19 20 class MipsRelocationInfo; 21 22 /** \class MipsRelocator 23 * \brief MipsRelocator creates and destroys the Mips relocations. 24 */ 25 class MipsRelocator : public Relocator { 26 public: 27 enum ReservedEntryType { 28 None = 0, // no reserved entry 29 ReserveRel = 1, // reserve a dynamic relocation entry 30 ReserveGot = 2, // reserve a GOT entry 31 ReservePLT = 4 // reserve a PLT entry 32 }; 33 34 public: 35 MipsRelocator(MipsGNULDBackend& pParent, const LinkerConfig& pConfig); 36 37 /// scanRelocation - determine the empty entries are needed or not and 38 /// create the empty entries if needed. 39 /// For Mips, the GOT, GP, and dynamic relocation entries are check to create. 40 void scanRelocation(Relocation& pReloc, 41 IRBuilder& pBuilder, 42 Module& pModule, 43 LDSection& pSection, 44 Input& pInput); 45 46 /// initializeScan - do initialization before scan relocations in pInput 47 /// @return - return true for initialization success 48 bool initializeScan(Input& pInput); 49 50 /// finalizeScan - do finalization after scan relocations in pInput 51 /// @return - return true for finalization success 52 bool finalizeScan(Input& pInput); 53 54 /// initializeApply - do initialization before apply relocations in pInput 55 /// @return - return true for initialization success 56 bool initializeApply(Input& pInput); 57 58 /// finalizeApply - do finalization after apply relocations in pInput 59 /// @return - return true for finalization success 60 bool finalizeApply(Input& pInput); 61 62 Result applyRelocation(Relocation& pReloc); 63 64 /// getDebugStringOffset - get the offset from the relocation target. This is 65 /// used to get the debug string offset. 66 uint32_t getDebugStringOffset(Relocation& pReloc) const; 67 68 /// applyDebugStringOffset - apply the relocation target to specific offset. 69 /// This is used to set the debug string offset. 70 void applyDebugStringOffset(Relocation& pReloc, uint32_t pOffset); 71 getApplyingInput()72 const Input& getApplyingInput() const { return *m_pApplyingInput; } 73 getTarget()74 MipsGNULDBackend& getTarget() { return m_Target; } 75 getTarget()76 const MipsGNULDBackend& getTarget() const { return m_Target; } 77 78 /// postponeRelocation - save R_MIPS_LO16 paired relocations 79 /// like R_MISP_HI16 and R_MIPS_GOT16 for a future processing. 80 void postponeRelocation(Relocation& pReloc); 81 82 /// applyPostponedRelocations - apply all postponed relocations 83 /// paired with the R_MIPS_LO16 one. 84 void applyPostponedRelocations(MipsRelocationInfo& pLo16Reloc); 85 86 /// isGpDisp - return true if relocation is against _gp_disp symbol. 87 bool isGpDisp(const Relocation& pReloc) const; 88 89 /// getGPAddress - return address of _gp symbol. 90 Address getGPAddress(); 91 92 /// getGP0 - the gp value used to create the relocatable objects 93 /// in the processing input. 94 Address getGP0(); 95 96 /// getLocalGOTEntry - initialize and return a local GOT entry 97 /// for this relocation. 98 Fragment& getLocalGOTEntry(MipsRelocationInfo& pReloc, 99 Relocation::DWord entryValue); 100 101 /// getGlobalGOTEntry - initialize and return a global GOT entry 102 /// for this relocation. 103 Fragment& getGlobalGOTEntry(MipsRelocationInfo& pReloc); 104 105 /// getGOTOffset - return offset of corresponded GOT entry. 106 Address getGOTOffset(MipsRelocationInfo& pReloc); 107 108 /// createDynRel - initialize dynamic relocation for the relocation. 109 void createDynRel(MipsRelocationInfo& pReloc); 110 111 /// getPLTOffset - initialize PLT-related entries for the symbol 112 /// @return - return address of PLT entry 113 uint64_t getPLTAddress(ResolveInfo& rsym); 114 115 /// calcAHL - calculate combined addend used 116 /// by R_MIPS_HI16 and R_MIPS_GOT16 relocations. 117 uint64_t calcAHL(const MipsRelocationInfo& pHiReloc); 118 119 /// isN64ABI - check current ABI 120 bool isN64ABI() const; 121 122 const char* getName(Relocation::Type pType) const; 123 124 Size getSize(Relocation::Type pType) const; 125 126 protected: 127 /// setupRelDynEntry - create dynamic relocation entry. 128 virtual void setupRelDynEntry(FragmentRef& pFragRef, ResolveInfo* pSym) = 0; 129 130 /// isLocalReloc - handle relocation as a local symbol 131 bool isLocalReloc(ResolveInfo& pSym) const; 132 133 private: 134 typedef std::pair<Fragment*, Fragment*> PLTDescriptor; 135 typedef llvm::DenseMap<const ResolveInfo*, PLTDescriptor> SymPLTMap; 136 typedef llvm::DenseSet<Relocation*> RelocationSet; 137 typedef llvm::DenseMap<const ResolveInfo*, RelocationSet> SymRelocSetMap; 138 139 private: 140 MipsGNULDBackend& m_Target; 141 SymPLTMap m_SymPLTMap; 142 Input* m_pApplyingInput; 143 SymRelocSetMap m_PostponedRelocs; 144 MipsRelocationInfo* m_CurrentLo16Reloc; 145 146 private: 147 void scanLocalReloc(MipsRelocationInfo& pReloc, 148 IRBuilder& pBuilder, 149 const LDSection& pSection); 150 151 void scanGlobalReloc(MipsRelocationInfo& pReloc, 152 IRBuilder& pBuilder, 153 const LDSection& pSection); 154 155 /// isPostponed - relocation applying needs to be postponed. 156 bool isPostponed(const Relocation& pReloc) const; 157 158 /// addCopyReloc - add a copy relocation into .rel.dyn for pSym 159 /// @param pSym - A resolved copy symbol that defined in BSS section 160 void addCopyReloc(ResolveInfo& pSym); 161 162 /// defineSymbolforCopyReloc - allocate a space in BSS section and 163 /// and force define the copy of pSym to BSS section 164 /// @return the output LDSymbol of the copy symbol 165 LDSymbol& defineSymbolforCopyReloc(IRBuilder& pBuilder, 166 const ResolveInfo& pSym); 167 168 /// isRel - returns true if REL relocation record format is expected 169 bool isRel() const; 170 }; 171 172 /** \class Mips32Relocator 173 * \brief Mips32Relocator creates and destroys the Mips 32-bit relocations. 174 */ 175 class Mips32Relocator : public MipsRelocator { 176 public: 177 Mips32Relocator(Mips32GNULDBackend& pParent, const LinkerConfig& pConfig); 178 179 private: 180 // MipsRelocator 181 void setupRelDynEntry(FragmentRef& pFragRef, ResolveInfo* pSym); 182 }; 183 184 /** \class Mips64Relocator 185 * \brief Mips64Relocator creates and destroys the Mips 64-bit relocations. 186 */ 187 class Mips64Relocator : public MipsRelocator { 188 public: 189 Mips64Relocator(Mips64GNULDBackend& pParent, const LinkerConfig& pConfig); 190 191 private: 192 // MipsRelocator 193 void setupRelDynEntry(FragmentRef& pFragRef, ResolveInfo* pSym); 194 }; 195 196 } // namespace mcld 197 198 #endif // TARGET_MIPS_MIPSRELOCATOR_H_ 199