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