HexagonRelocator.h revision f7ac0f19a1c8d0ad14bcf6456ce368b830fea886
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 HEXAGON_RELOCATION_FACTORY_H
10#define HEXAGON_RELOCATION_FACTORY_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/SymbolEntryMap.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 SymbolEntryMap<PLTEntryBase> SymPLTMap;
34  typedef SymbolEntryMap<HexagonGOTEntry> SymGOTMap;
35  typedef SymbolEntryMap<HexagonGOTEntry> SymGOTPLTMap;
36
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
44   *
45   *  In Hexagon, there are three kinds of entries, GOT, PLT, and dynamic
46   *  relocation.
47   *
48   *  GOT may needs a corresponding relocation to relocate itself, so we
49   *  separate GOT to two situations: GOT and GOTRel. Besides, for the same
50   *  symbol, there might be two kinds of entries reserved for different location.
51   *  For example, reference to the same symbol, one may use GOT and the other may
52   *  use dynamic relocation.
53   *
54   *  bit:  3       2      1     0
55   *   | PLT | GOTRel | GOT | Rel |
56   *
57   *  value    Name         - Description
58   *
59   *  0000     None         - no reserved entry
60   *  0001     ReserveRel   - reserve an dynamic relocation entry
61   *  0010     ReserveGOT   - reserve an GOT entry
62   *  0011     GOTandRel    - For different relocation, we've reserved GOT and
63   *                          Rel for different location.
64   *  0100     GOTRel       - reserve an GOT entry and the corresponding Dyncamic
65   *                          relocation entry which relocate this GOT entry
66   *  0101     GOTRelandRel - For different relocation, we've reserved GOTRel
67   *                          and relocation entry for different location.
68   *  1000     ReservePLT   - reserve an PLT entry and the corresponding GOT,
69   *                          Dynamic relocation entries
70   *  1001     PLTandRel    - For different relocation, we've reserved PLT and
71   *                          Rel for different location.
72   */
73  enum ReservedEntryType {
74    None         = 0,
75    ReserveRel   = 1,
76    ReserveGOT   = 2,
77    GOTandRel    = 3,
78    GOTRel       = 4,
79    GOTRelandRel = 5,
80    ReservePLT   = 8,
81    PLTandRel    = 9
82  };
83
84  HexagonRelocator(HexagonLDBackend& pParent, const LinkerConfig& pConfig);
85  ~HexagonRelocator();
86
87  Result applyRelocation(Relocation& pRelocation);
88
89  /// scanRelocation - determine the empty entries are needed or not and create
90  /// the empty entries if needed.
91  /// For Hexagon, following entries are check to create:
92  /// - GOT entry (for .got and .got.plt sections)
93  /// - PLT entry (for .plt section)
94  /// - dynamin relocation entries (for .rel.plt and .rel.dyn sections)
95  void scanRelocation(Relocation& pReloc,
96                      IRBuilder& pBuilder,
97                      Module& pModule,
98                      LDSection& pSection);
99
100  // Handle partial linking
101  void partialScanRelocation(Relocation& pReloc,
102                             Module& pModule,
103                             const LDSection& pSection);
104
105  HexagonLDBackend& getTarget()
106  { return m_Target; }
107
108  const HexagonLDBackend& getTarget() const
109  { return m_Target; }
110
111  const char* getName(Relocation::Type pType) const;
112
113  Size getSize(Relocation::Type pType) const;
114
115  const SymPLTMap& getSymPLTMap() const { return m_SymPLTMap; }
116  SymPLTMap&       getSymPLTMap()       { return m_SymPLTMap; }
117
118  const SymGOTMap& getSymGOTMap() const { return m_SymGOTMap; }
119  SymGOTMap&       getSymGOTMap()       { return m_SymGOTMap; }
120
121  const SymGOTPLTMap& getSymGOTPLTMap() const { return m_SymGOTPLTMap; }
122  SymGOTPLTMap&       getSymGOTPLTMap()       { return m_SymGOTPLTMap; }
123
124protected:
125  /// addCopyReloc - add a copy relocation into .rela.dyn for pSym
126  /// @param pSym - A resolved copy symbol that defined in BSS section
127  void addCopyReloc(ResolveInfo& pSym, HexagonLDBackend& pTarget);
128
129  /// defineSymbolforCopyReloc - allocate a space in BSS section and
130  /// and force define the copy of pSym to BSS section
131  /// @return the output LDSymbol of the copy symbol
132  LDSymbol& defineSymbolforCopyReloc(IRBuilder& pLinker,
133                                     const ResolveInfo& pSym,
134                                     HexagonLDBackend& pTarget);
135
136private:
137  virtual void scanLocalReloc(Relocation& pReloc,
138                              IRBuilder& pBuilder,
139                              Module& pModule,
140                              LDSection& pSection);
141
142  virtual void scanGlobalReloc(Relocation& pReloc,
143                               IRBuilder& pBuilder,
144                               Module& pModule,
145                               LDSection& pSection);
146
147  HexagonLDBackend& m_Target;
148  SymPLTMap m_SymPLTMap;
149  SymGOTMap m_SymGOTMap;
150  SymGOTPLTMap m_SymGOTPLTMap;
151};
152
153} // namespace of mcld
154
155#endif
156
157