summaryrefslogtreecommitdiff
path: root/include/mcld/LD/Relocation.h
blob: 9f7a117ef55d925b2f6917cb69c4cb1b57c24c4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//===- Relocation.h ----------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LD_RELOCATION_H
#define LD_RELOCATION_H
#ifdef ENABLE_UNITTEST
#include <gtest.h>
#endif
#include <llvm/ADT/ilist_node.h>
#include <llvm/Support/DataTypes.h>
#include <mcld/MC/MCFragmentRef.h>
#include <mcld/LD/ResolveInfo.h>
#include <mcld/LD/LDSymbol.h>


namespace mcld
{
class Layout;
class RelocationFactory;
class MCLDInfo;

class Relocation : public llvm::MCFragment
{
friend class RelocationFactory;

public:
  typedef uint64_t Address; // FIXME: use SizeTrait<T>::Address instead
  typedef uint64_t DWord; // FIXME: use SizeTrait<T>::Word instead
  typedef uint8_t Type;

private:
  Relocation(Type pType,
             MCFragmentRef* pTargetRef,
             Address pAddend,
             DWord pTargetData);

public:
  ~Relocation();

  /// type - relocation type
  Type type() const
  { return m_Type; }

  /// symValue - S value - the symbol address
  Address symValue() const;

  /// addend - A value
  Address addend() const
  { return m_Addend; }

  /// place - P value - address of the place being relocated
  Address place(const Layout& pLayout) const;

  /// symbol info - binding, type
  const ResolveInfo* symInfo() const
  { return m_pSymInfo; }

  /// symbol info - binding, type
  ResolveInfo* symInfo()
  { return m_pSymInfo; }

  /// target - the target data to relocate
  DWord& target();

  /// target - the target data to relocate
  const DWord& target() const;

  /// targetRef - the reference of the target data
  MCFragmentRef& targetRef()
  { return m_TargetAddress; }

  /// targetRef - the reference of the target data
  const MCFragmentRef& targetRef() const
  { return m_TargetAddress; }

  void apply(RelocationFactory& pRelocFactory, const MCLDInfo& pLDInfo);

  /// ----- modifiers ----- ///
  void setType(Type pType);

  void setAddend(Address pAddend);

  void setSymInfo(ResolveInfo* pSym);

  // Relocation is a kind of MCFragment with type of FT_Reloc
  static bool classof(const MCFragment *F)
  { return F->getKind() == MCFragment::FT_Reloc;}
  static bool classof(const Relocation *) { return true; }

private:
  /// m_Type - the type of the relocation entries
  Type m_Type;

  /// m_TargetData - target data of the place being relocated
  DWord m_TargetData;

  /// m_pSymInfo - resolved symbol info of relocation target symbol
  ResolveInfo* m_pSymInfo;

  /// m_TargetAddress - MCFragmentRef of the place being relocated
  MCFragmentRef m_TargetAddress;

  /// m_Addend - the addend
  Address m_Addend;
};

} // namespace of mcld

#endif