Stub.h revision 22add6ff3426df1a85089fe6a6e1597ee3b6f300
1//===- Stub.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
10#ifndef MCLD_FRAGMENT_STUB_H
11#define MCLD_FRAGMENT_STUB_H
12#ifdef ENABLE_UNITTEST
13#include <gtest.h>
14#endif
15
16#include <llvm/Support/DataTypes.h>
17#include <mcld/Fragment/Fragment.h>
18#include <mcld/Fragment/Relocation.h>
19#include <vector>
20#include <string>
21
22namespace mcld
23{
24
25class Relocation;
26class ResolveInfo;
27
28class Stub: public Fragment
29{
30public:
31  typedef Relocation::DWord DWord;
32  typedef Relocation::SWord SWord;
33  typedef Relocation::Type  Type;
34
35  class Fixup
36  {
37  public:
38    Fixup(DWord pOffset, SWord pAddend, Type pType)
39     : m_Offset(pOffset), m_Addend(pAddend), m_Type(pType)
40    { }
41
42    ~Fixup()
43    { }
44
45    DWord offset() const { return m_Offset; }
46
47    SWord addend() const { return m_Addend; }
48
49    Type  type() const   { return m_Type; }
50
51  private:
52    DWord m_Offset;
53    SWord m_Addend;
54    Type  m_Type;
55  };
56
57public:
58  typedef std::vector<Fixup*> FixupListType;
59  typedef FixupListType::iterator fixup_iterator;
60  typedef FixupListType::const_iterator const_fixup_iterator;
61
62public:
63  Stub();
64
65  virtual ~Stub();
66
67  /// clone - clone function for stub factory to create the corresponding stub
68  Stub* clone() { return doClone(); }
69
70  /// isMyDuty - return true when the pReloc is problematic and the stub is able
71  /// to fix it!
72  virtual bool isMyDuty(const class Relocation& pReloc,
73                        uint64_t pSource,
74                        uint64_t pTargetSymValue) const = 0;
75
76  /// name - name of this stub
77  virtual const std::string& name() const = 0;
78
79  /// getContent - content of the stub
80  virtual const uint8_t* getContent() const = 0;
81
82  /// size - size of the stub
83  virtual size_t size() const = 0;
84
85  /// alignment - alignment of the stub
86  virtual size_t alignment() const = 0;
87
88  /// symInfo - ResolveInfo of this Stub
89  ResolveInfo* symInfo()             { return m_pSymInfo; }
90
91  const ResolveInfo* symInfo() const { return m_pSymInfo; }
92
93  /// symValue - initial value for stub's symbol
94  virtual uint64_t initSymValue() const  { return 0x0; }
95
96  ///  -----  Fixup  -----  ///
97  fixup_iterator       fixup_begin()       { return m_FixupList.begin(); }
98
99  const_fixup_iterator fixup_begin() const { return m_FixupList.begin(); }
100
101  fixup_iterator       fixup_end()         { return m_FixupList.end();   }
102
103  const_fixup_iterator fixup_end()   const { return m_FixupList.end();   }
104
105  /// ----- modifiers ----- ///
106  void setSymInfo(ResolveInfo* pSymInfo);
107
108  // Stub is a kind of Fragment with type of Stub
109  static bool classof(const Fragment *F)
110  { return F->getKind() == Fragment::Stub; }
111
112  static bool classof(const Stub *)
113  { return true; }
114
115protected:
116  /// addFixup - add a fixup for this stub to build a relocation
117  void addFixup(DWord pOffset, SWord pAddend, Type pType);
118
119  /// addFixup - add a fixup from a existing fixup of the prototype
120  void addFixup(const Fixup& pFixup);
121
122private:
123  /// doClone - when adding a backend stub, we should implement this function
124  virtual Stub* doClone() = 0;
125
126private:
127  ResolveInfo* m_pSymInfo;
128  FixupListType m_FixupList;
129};
130
131} // namespace of mcld
132
133#endif
134
135