• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- Relocation.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 MCLD_FRAGMENT_RELOCATION_H
10 #define MCLD_FRAGMENT_RELOCATION_H
11 #ifdef ENABLE_UNITTEST
12 #include <gtest.h>
13 #endif
14 #include <mcld/Config/Config.h>
15 #include <mcld/Fragment/FragmentRef.h>
16 #include <mcld/Support/GCFactoryListTraits.h>
17 
18 #include <llvm/ADT/ilist_node.h>
19 #include <llvm/Support/DataTypes.h>
20 
21 namespace mcld {
22 
23 class ResolveInfo;
24 class Relocator;
25 class LinkerConfig;
26 
27 class Relocation : public llvm::ilist_node<Relocation>
28 {
29 friend class RelocationFactory;
30 friend class GCFactoryListTraits<Relocation>;
31 friend class Chunk<Relocation, MCLD_RELOCATIONS_PER_INPUT>;
32 
33 public:
34   typedef uint64_t Address; // FIXME: use SizeTrait<T>::Address instead
35   typedef uint64_t DWord;   // FIXME: use SizeTrait<T>::Word instead
36   typedef int64_t  SWord;   // FIXME: use SizeTrait<T>::SWord instead
37   typedef uint8_t  Type;
38   typedef uint32_t Size;
39 
40 private:
41   Relocation();
42 
43   Relocation(Type pType,
44              FragmentRef* pTargetRef,
45              Address pAddend,
46              DWord pTargetData);
47 
48   ~Relocation();
49 
50 public:
51   /// Initialize - set up the relocation factory
52   static void SetUp(const LinkerConfig& pConfig);
53 
54   /// Clear - Clean up the relocation factory
55   static void Clear();
56 
57   /// Create - produce an empty relocation entry
58   static Relocation* Create();
59 
60   /// Create - produce a relocation entry
61   /// @param pType    [in] the type of the relocation entry
62   /// @param pFragRef [in] the place to apply the relocation
63   /// @param pAddend  [in] the addend of the relocation entry
64   static Relocation* Create(Type pType, FragmentRef& pFragRef,
65                             Address pAddend = 0);
66 
67   /// Destroy - destroy a relocation entry
68   static void Destroy(Relocation*& pRelocation);
69 
70   /// type - relocation type
type()71   Type type() const
72   { return m_Type; }
73 
74   /// symValue - S value - the symbol address
75   Address symValue() const;
76 
77   /// addend - A value
addend()78   Address addend() const
79   { return m_Addend; }
80 
81   /// place - P value - address of the place being relocated
82   Address place() const;
83 
84   /// size - the size of the relocation in bit
85   Size size(Relocator& pRelocator) const;
86 
87   /// symbol info - binding, type
symInfo()88   const ResolveInfo* symInfo() const { return m_pSymInfo; }
symInfo()89   ResolveInfo*       symInfo()       { return m_pSymInfo; }
90 
91   /// target - the target data to relocate
target()92   const DWord& target() const { return m_TargetData; }
target()93   DWord&       target()       { return m_TargetData; }
94 
95   /// targetRef - the reference of the target data
targetRef()96   const FragmentRef& targetRef() const { return m_TargetAddress; }
targetRef()97   FragmentRef&       targetRef()       { return m_TargetAddress; }
98 
99   void apply(Relocator& pRelocator);
100 
101   /// updateAddend - A relocation with a section symbol must update addend
102   /// before reading its value.
103   void updateAddend();
104 
105   /// ----- modifiers ----- ///
106   void setType(Type pType);
107 
108   void setAddend(Address pAddend);
109 
110   void setSymInfo(ResolveInfo* pSym);
111 
112 private:
113   /// m_Type - the type of the relocation entries
114   Type m_Type;
115 
116   /// m_TargetData - target data of the place being relocated
117   DWord m_TargetData;
118 
119   /// m_pSymInfo - resolved symbol info of relocation target symbol
120   ResolveInfo* m_pSymInfo;
121 
122   /// m_TargetAddress - FragmentRef of the place being relocated
123   FragmentRef m_TargetAddress;
124 
125   /// m_Addend - the addend
126   Address m_Addend;
127 };
128 
129 } // namespace of mcld
130 
131 #endif
132 
133