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