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