Relocation.h revision d0fbbb227051be16931a1aa9b4a7722ac039c698
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
21namespace mcld {
22
23class ResolveInfo;
24class Relocator;
25class LinkerConfig;
26
27class Relocation : public llvm::ilist_node<Relocation>
28{
29friend class RelocationFactory;
30friend class GCFactoryListTraits<Relocation>;
31friend class Chunk<Relocation, MCLD_RELOCATIONS_PER_INPUT>;
32
33public:
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
39private:
40  Relocation();
41
42  Relocation(Type pType,
43             FragmentRef* pTargetRef,
44             Address pAddend,
45             DWord pTargetData);
46
47  ~Relocation();
48
49public:
50  /// Initialize - set up the relocation factory
51  static void SetUp(const LinkerConfig& pConfig);
52
53  /// Clear - Clean up the relocation factory
54  static void Clear();
55
56  /// Create - produce an empty relocation entry
57  static Relocation* Create();
58
59  /// Create - produce a relocation entry
60  /// @param pType    [in] the type of the relocation entry
61  /// @param pFragRef [in] the place to apply the relocation
62  /// @param pAddend  [in] the addend of the relocation entry
63  static Relocation* Create(Type pType, FragmentRef& pFragRef,
64                            Address pAddend = 0);
65
66  /// Destroy - destroy a relocation entry
67  static void Destroy(Relocation*& pRelocation);
68
69  /// type - relocation type
70  Type type() const
71  { return m_Type; }
72
73  /// symValue - S value - the symbol address
74  Address symValue() const;
75
76  /// addend - A value
77  Address addend() const
78  { return m_Addend; }
79
80  /// place - P value - address of the place being relocated
81  Address place() const;
82
83  /// symbol info - binding, type
84  const ResolveInfo* symInfo() const { return m_pSymInfo; }
85  ResolveInfo*       symInfo()       { return m_pSymInfo; }
86
87  /// target - the target data to relocate
88  const DWord& target() const { return m_TargetData; }
89  DWord&       target()       { return m_TargetData; }
90
91  /// targetRef - the reference of the target data
92  const FragmentRef& targetRef() const { return m_TargetAddress; }
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  size_t size() const;
109
110private:
111  /// m_Type - the type of the relocation entries
112  Type m_Type;
113
114  /// m_TargetData - target data of the place being relocated
115  DWord m_TargetData;
116
117  /// m_pSymInfo - resolved symbol info of relocation target symbol
118  ResolveInfo* m_pSymInfo;
119
120  /// m_TargetAddress - FragmentRef of the place being relocated
121  FragmentRef m_TargetAddress;
122
123  /// m_Addend - the addend
124  Address m_Addend;
125};
126
127} // namespace of mcld
128
129#endif
130
131