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