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