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