GOT.h revision 5460a1f25d9ddecb5c70667267d66d51af177a99
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_GOT_H
10#define MCLD_GOT_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14
15#include <mcld/LD/LDSection.h>
16#include <mcld/MC/MCTargetFragment.h>
17
18namespace mcld
19{
20
21class GOT;
22class ResolveInfo;
23
24/** \class GOTEntry
25 *  \brief The entry of Global Offset Table
26 */
27class GOTEntry : public MCTargetFragment
28{
29public:
30  explicit GOTEntry(uint64_t pContent, size_t pEntrySize,
31                    llvm::MCSectionData* pParent);
32
33  virtual ~GOTEntry();
34
35  uint64_t& getContent()
36  { return f_Content; }
37
38  uint64_t getContent() const
39  { return f_Content; }
40
41  void setContent(uint64_t pValue)
42  { f_Content = pValue; }
43
44  static bool classof(const MCFragment *pFrag)
45  { return pFrag->getKind() == llvm::MCFragment::FT_Target; }
46
47  static bool classof(const GOTEntry* pFrag)
48  { return true; }
49
50  // Override pure virtual function
51  size_t getSize() const
52  { return m_EntrySize; }
53
54protected:
55  uint64_t f_Content;
56  size_t m_EntrySize;
57};
58
59/** \class GOT
60 *  \brief The Global Offset Table
61 */
62class GOT
63{
64protected:
65  GOT(LDSection& pSection,
66      llvm::MCSectionData& pSectionData,
67      size_t pEntrySize);
68
69public:
70  virtual ~GOT();
71
72  /// entrySize - the number of bytes per entry
73  size_t getEntrySize() const;
74
75  const LDSection& getSection() const
76  { return m_Section; }
77
78  llvm::MCSectionData& getSectionData()
79  { return m_SectionData; }
80
81  const llvm::MCSectionData& getSectionData() const
82  { return m_SectionData; }
83
84public:
85  /// reserveEntry - reseve number of pNum of empty entries
86  /// Before layout, we scan all relocations to determine if GOT entries are
87  /// needed. If an entry is needed, the empty entry is reserved for layout
88  /// to adjust the fragment offset. After that, we fill up the entries when
89  /// applying relocations.
90  virtual void reserveEntry(size_t pNum = 1) = 0;
91
92  /// getEntry - get an empty entry or an exitsted filled entry with pSymbol.
93  /// @param pSymbol - the target symbol
94  /// @param pExist - ture if a filled entry with pSymbol existed, otherwise false.
95  virtual GOTEntry* getEntry(const ResolveInfo& pSymbol, bool& pExist) = 0;
96
97protected:
98  LDSection& m_Section;
99  llvm::MCSectionData& m_SectionData;
100  size_t f_EntrySize;
101};
102
103} // namespace of mcld
104
105#endif
106
107