GOT.h revision 22add6ff3426df1a85089fe6a6e1597ee3b6f300
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, size_t pEntrySize);
32
33public:
34  typedef SectionData::iterator iterator;
35  typedef SectionData::const_iterator const_iterator;
36
37  class Entry : public TargetFragment
38  {
39  public:
40    Entry(uint64_t pContent, size_t pEntrySize, SectionData* pParent);
41
42    virtual ~Entry();
43
44    uint64_t getContent() const
45    { return f_Content; }
46
47    void setContent(uint64_t pValue)
48    { f_Content = pValue; }
49
50    // Override pure virtual function
51    size_t size() const
52    { return m_EntrySize; }
53
54  protected:
55    uint64_t f_Content;
56    size_t m_EntrySize;
57  };
58
59public:
60  virtual ~GOT();
61
62  // ----- observers -----//
63  /// entrySize - the number of bytes per entry
64  size_t getEntrySize() const;
65
66  uint64_t addr() const { return m_Section.addr(); }
67
68  const_iterator begin() const { return m_SectionData->begin(); }
69  iterator       begin()       { return m_SectionData->begin(); }
70  const_iterator end  () const { return m_SectionData->end();   }
71  iterator       end  ()       { return m_SectionData->end();   }
72
73  bool empty() const
74  { return m_SectionData->empty(); }
75
76  // finalizeSectionSize - set LDSection size
77  virtual void finalizeSectionSize();
78
79  /// reserve - reseve number of pNum of empty entries
80  /// Before layout, we scan all relocations to determine if GOT entries are
81  /// needed. If an entry is needed, the empty entry is reserved for layout
82  /// to adjust the fragment offset. After that, we fill up the entries when
83  /// applying relocations.
84  virtual void reserve(size_t pNum = 1);
85
86  /// consume - consume and return an empty entry
87  virtual Entry* consume();
88
89protected:
90  LDSection& m_Section;
91  SectionData* m_SectionData;
92  size_t f_EntrySize;
93
94  Entry* m_pLast; ///< the last consumed entry
95};
96
97} // namespace of mcld
98
99#endif
100
101