1 //===- GOT.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 MCLD_GLOBAL_OFFSET_TABLE_H 10 #define MCLD_GLOBAL_OFFSET_TABLE_H 11 #ifdef ENABLE_UNITTEST 12 #include <gtest.h> 13 #endif 14 15 #include <mcld/LD/LDSection.h> 16 #include <mcld/LD/SectionData.h> 17 #include <mcld/Fragment/TargetFragment.h> 18 19 namespace mcld { 20 21 class GOT; 22 class LDSection; 23 class ResolveInfo; 24 25 /** \class GOT 26 * \brief The Global Offset Table 27 */ 28 class GOT 29 { 30 protected: 31 GOT(LDSection& pSection); 32 33 public: 34 typedef SectionData::iterator iterator; 35 typedef SectionData::const_iterator const_iterator; 36 37 template<size_t SIZE> 38 class Entry : public TargetFragment 39 { 40 public: 41 enum { EntrySize = SIZE }; 42 43 public: Entry(uint64_t pValue,SectionData * pParent)44 Entry(uint64_t pValue, SectionData* pParent) 45 : TargetFragment(Fragment::Target, pParent), 46 f_Value(pValue) { 47 } 48 ~Entry()49 virtual ~Entry() {} 50 getValue()51 uint64_t getValue() const 52 { return f_Value; } 53 setValue(uint64_t pValue)54 void setValue(uint64_t pValue) 55 { f_Value = pValue; } 56 57 // Override pure virtual function size()58 size_t size() const 59 { return EntrySize; } 60 61 protected: 62 uint64_t f_Value; 63 }; 64 65 public: 66 virtual ~GOT(); 67 68 // ----- observers -----// addr()69 uint64_t addr() const { return m_Section.addr(); } 70 begin()71 const_iterator begin() const { return m_SectionData->begin(); } begin()72 iterator begin() { return m_SectionData->begin(); } end()73 const_iterator end () const { return m_SectionData->end(); } end()74 iterator end () { return m_SectionData->end(); } 75 empty()76 bool empty() const 77 { return m_SectionData->empty(); } 78 79 // finalizeSectionSize - set LDSection size 80 virtual void finalizeSectionSize(); 81 82 /// reserve - reseve number of pNum of empty entries 83 /// Before layout, we scan all relocations to determine if GOT entries are 84 /// needed. If an entry is needed, the empty entry is reserved for layout 85 /// to adjust the fragment offset. After that, we fill up the entries when 86 /// applying relocations. 87 virtual void reserve(size_t pNum = 1) = 0; 88 89 protected: 90 LDSection& m_Section; 91 SectionData* m_SectionData; 92 }; 93 94 } // namespace of mcld 95 96 #endif 97 98