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 TARGET_HEXAGON_HEXAGONRELOCATOR_H
10 #define TARGET_HEXAGON_HEXAGONRELOCATOR_H
11 
12 #include <mcld/LD/Relocator.h>
13 #include <mcld/Target/GOT.h>
14 #include <mcld/Target/PLT.h>
15 #include <mcld/Target/KeyEntryMap.h>
16 #include "HexagonLDBackend.h"
17 
18 namespace mcld {
19 
20 class ResolveInfo;
21 class LinkerConfig;
22 
23 /** \class HexagonRelocator
24  *  \brief HexagonRelocator creates and destroys the Hexagon relocations.
25  *
26  */
27 class HexagonRelocator : public Relocator
28 {
29 public:
30   typedef KeyEntryMap<ResolveInfo, PLTEntryBase> SymPLTMap;
31   typedef KeyEntryMap<ResolveInfo, HexagonGOTEntry> SymGOTMap;
32   typedef KeyEntryMap<ResolveInfo, HexagonGOTEntry> SymGOTPLTMap;
33   typedef KeyEntryMap<Relocation, Relocation> RelRelMap;
34 
35 public:
36   /** \enum ReservedEntryType
37    *  \brief The reserved entry type of reserved space in ResolveInfo.
38    *
39    *  This is used for sacnRelocation to record what kinds of entries are
40    *  reserved for this resolved symbol. In Hexagon, there are three kinds
41    *  of entries, GOT, PLT, and dynamic relocation.
42    *
43    *  bit:  3     2     1     0
44    *   |    | PLT | GOT | Rel |
45    *
46    *  value    Name         - Description
47    *
48    *  0000     None         - no reserved entry
49    *  0001     ReserveRel   - reserve an dynamic relocation entry
50    *  0010     ReserveGOT   - reserve an GOT entry
51    *  0100     ReservePLT   - reserve an PLT entry and the corresponding GOT,
52    *
53    */
54   enum ReservedEntryType {
55     None         = 0,
56     ReserveRel   = 1,
57     ReserveGOT   = 2,
58     ReservePLT   = 4,
59   };
60 
61   /** \enum EntryValue
62    *  \brief The value of the entries. The symbol value will be decided at after
63    *  layout, so we mark the entry during scanRelocation and fill up the actual
64    *  value when applying relocations.
65    */
66   enum EntryValue {
67     Default = 0,
68     SymVal  = 1
69   };
70 
71   HexagonRelocator(HexagonLDBackend& pParent, const LinkerConfig& pConfig);
72   ~HexagonRelocator();
73 
74   Result applyRelocation(Relocation& pRelocation);
75 
76   /// scanRelocation - determine the empty entries are needed or not and create
77   /// the empty entries if needed.
78   /// For Hexagon, following entries are check to create:
79   /// - GOT entry (for .got and .got.plt sections)
80   /// - PLT entry (for .plt section)
81   /// - dynamin relocation entries (for .rel.plt and .rel.dyn sections)
82   void scanRelocation(Relocation& pReloc,
83                       IRBuilder& pBuilder,
84                       Module& pModule,
85                       LDSection& pSection,
86                       Input& pInput);
87 
88   // Handle partial linking
89   void partialScanRelocation(Relocation& pReloc,
90                              Module& pModule,
91                              const LDSection& pSection);
92 
getTarget()93   HexagonLDBackend& getTarget()
94   { return m_Target; }
95 
getTarget()96   const HexagonLDBackend& getTarget() const
97   { return m_Target; }
98 
99   const char* getName(Relocation::Type pType) const;
100 
101   Size getSize(Relocation::Type pType) const;
102 
getSymPLTMap()103   const SymPLTMap& getSymPLTMap() const { return m_SymPLTMap; }
getSymPLTMap()104   SymPLTMap&       getSymPLTMap()       { return m_SymPLTMap; }
105 
getSymGOTMap()106   const SymGOTMap& getSymGOTMap() const { return m_SymGOTMap; }
getSymGOTMap()107   SymGOTMap&       getSymGOTMap()       { return m_SymGOTMap; }
108 
getSymGOTPLTMap()109   const SymGOTPLTMap& getSymGOTPLTMap() const { return m_SymGOTPLTMap; }
getSymGOTPLTMap()110   SymGOTPLTMap&       getSymGOTPLTMap()       { return m_SymGOTPLTMap; }
111 
getRelRelMap()112   const RelRelMap& getRelRelMap() const { return m_RelRelMap; }
getRelRelMap()113   RelRelMap&       getRelRelMap()       { return m_RelRelMap; }
114 
115 protected:
116   /// addCopyReloc - add a copy relocation into .rela.dyn for pSym
117   /// @param pSym - A resolved copy symbol that defined in BSS section
118   void addCopyReloc(ResolveInfo& pSym, HexagonLDBackend& pTarget);
119 
120   /// defineSymbolforCopyReloc - allocate a space in BSS section and
121   /// and force define the copy of pSym to BSS section
122   /// @return the output LDSymbol of the copy symbol
123   LDSymbol& defineSymbolforCopyReloc(IRBuilder& pLinker,
124                                      const ResolveInfo& pSym,
125                                      HexagonLDBackend& pTarget);
126 
127 private:
128   virtual void scanLocalReloc(Relocation& pReloc,
129                               IRBuilder& pBuilder,
130                               Module& pModule,
131                               LDSection& pSection);
132 
133   virtual void scanGlobalReloc(Relocation& pReloc,
134                                IRBuilder& pBuilder,
135                                Module& pModule,
136                                LDSection& pSection);
137 
138   HexagonLDBackend& m_Target;
139   SymPLTMap m_SymPLTMap;
140   SymGOTMap m_SymGOTMap;
141   SymGOTPLTMap m_SymGOTPLTMap;
142   RelRelMap m_RelRelMap;
143 };
144 
145 } // namespace of mcld
146 
147 #endif
148 
149