HexagonRelocator.h revision 551ae4ebd3e9d137ea668fb83ae4a55b8cfba451
1//===- HexagonRelocator.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_HEXAGON_HEXAGONRELOCATOR_H 10#define TARGET_HEXAGON_HEXAGONRELOCATOR_H 11#ifdef ENABLE_UNITTEST 12#include <gtest.h> 13#endif 14 15#include <mcld/LD/Relocator.h> 16#include <mcld/Target/GOT.h> 17#include <mcld/Target/PLT.h> 18#include <mcld/Target/KeyEntryMap.h> 19#include "HexagonLDBackend.h" 20 21namespace mcld { 22 23class ResolveInfo; 24class LinkerConfig; 25 26/** \class HexagonRelocator 27 * \brief HexagonRelocator creates and destroys the Hexagon relocations. 28 * 29 */ 30class HexagonRelocator : public Relocator 31{ 32public: 33 typedef KeyEntryMap<ResolveInfo, PLTEntryBase> SymPLTMap; 34 typedef KeyEntryMap<ResolveInfo, HexagonGOTEntry> SymGOTMap; 35 typedef KeyEntryMap<ResolveInfo, HexagonGOTEntry> SymGOTPLTMap; 36 typedef KeyEntryMap<Relocation, Relocation> RelRelMap; 37 38public: 39 /** \enum ReservedEntryType 40 * \brief The reserved entry type of reserved space in ResolveInfo. 41 * 42 * This is used for sacnRelocation to record what kinds of entries are 43 * reserved for this resolved symbol. In Hexagon, there are three kinds 44 * of entries, GOT, PLT, and dynamic relocation. 45 * 46 * bit: 3 2 1 0 47 * | | PLT | GOT | Rel | 48 * 49 * value Name - Description 50 * 51 * 0000 None - no reserved entry 52 * 0001 ReserveRel - reserve an dynamic relocation entry 53 * 0010 ReserveGOT - reserve an GOT entry 54 * 0100 ReservePLT - reserve an PLT entry and the corresponding GOT, 55 * 56 */ 57 enum ReservedEntryType { 58 None = 0, 59 ReserveRel = 1, 60 ReserveGOT = 2, 61 ReservePLT = 4, 62 }; 63 64 /** \enum EntryValue 65 * \brief The value of the entries. The symbol value will be decided at after 66 * layout, so we mark the entry during scanRelocation and fill up the actual 67 * value when applying relocations. 68 */ 69 enum EntryValue { 70 Default = 0, 71 SymVal = 1 72 }; 73 74 HexagonRelocator(HexagonLDBackend& pParent, const LinkerConfig& pConfig); 75 ~HexagonRelocator(); 76 77 Result applyRelocation(Relocation& pRelocation); 78 79 /// scanRelocation - determine the empty entries are needed or not and create 80 /// the empty entries if needed. 81 /// For Hexagon, following entries are check to create: 82 /// - GOT entry (for .got and .got.plt sections) 83 /// - PLT entry (for .plt section) 84 /// - dynamin relocation entries (for .rel.plt and .rel.dyn sections) 85 void scanRelocation(Relocation& pReloc, 86 IRBuilder& pBuilder, 87 Module& pModule, 88 LDSection& pSection, 89 Input& pInput); 90 91 // Handle partial linking 92 void partialScanRelocation(Relocation& pReloc, 93 Module& pModule, 94 const LDSection& pSection); 95 96 HexagonLDBackend& getTarget() 97 { return m_Target; } 98 99 const HexagonLDBackend& getTarget() const 100 { return m_Target; } 101 102 const char* getName(Relocation::Type pType) const; 103 104 Size getSize(Relocation::Type pType) const; 105 106 const SymPLTMap& getSymPLTMap() const { return m_SymPLTMap; } 107 SymPLTMap& getSymPLTMap() { return m_SymPLTMap; } 108 109 const SymGOTMap& getSymGOTMap() const { return m_SymGOTMap; } 110 SymGOTMap& getSymGOTMap() { return m_SymGOTMap; } 111 112 const SymGOTPLTMap& getSymGOTPLTMap() const { return m_SymGOTPLTMap; } 113 SymGOTPLTMap& getSymGOTPLTMap() { return m_SymGOTPLTMap; } 114 115 const RelRelMap& getRelRelMap() const { return m_RelRelMap; } 116 RelRelMap& getRelRelMap() { return m_RelRelMap; } 117 118protected: 119 /// addCopyReloc - add a copy relocation into .rela.dyn for pSym 120 /// @param pSym - A resolved copy symbol that defined in BSS section 121 void addCopyReloc(ResolveInfo& pSym, HexagonLDBackend& pTarget); 122 123 /// defineSymbolforCopyReloc - allocate a space in BSS section and 124 /// and force define the copy of pSym to BSS section 125 /// @return the output LDSymbol of the copy symbol 126 LDSymbol& defineSymbolforCopyReloc(IRBuilder& pLinker, 127 const ResolveInfo& pSym, 128 HexagonLDBackend& pTarget); 129 130private: 131 virtual void scanLocalReloc(Relocation& pReloc, 132 IRBuilder& pBuilder, 133 Module& pModule, 134 LDSection& pSection); 135 136 virtual void scanGlobalReloc(Relocation& pReloc, 137 IRBuilder& pBuilder, 138 Module& pModule, 139 LDSection& pSection); 140 141 HexagonLDBackend& m_Target; 142 SymPLTMap m_SymPLTMap; 143 SymGOTMap m_SymGOTMap; 144 SymGOTPLTMap m_SymGOTPLTMap; 145 RelRelMap m_RelRelMap; 146}; 147 148} // namespace of mcld 149 150#endif 151 152