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