Relocator.h revision f7ac0f19a1c8d0ad14bcf6456ce368b830fea886
1//===- Relocator.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_RELOCATOR_H
10#define MCLD_RELOCATOR_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14
15#include <mcld/Fragment/Relocation.h>
16
17namespace mcld
18{
19
20class FragmentLinker;
21class TargetLDBackend;
22class IRBuilder;
23class Module;
24class Input;
25
26/** \class Relocator
27 *  \brief Relocator provides the interface of performing relocations
28 */
29class Relocator
30{
31public:
32  typedef Relocation::Type    Type;
33  typedef Relocation::Address Address;
34  typedef Relocation::DWord   DWord;
35  typedef Relocation::SWord   SWord;
36  typedef Relocation::Size    Size;
37
38public:
39  enum Result {
40    OK,
41    BadReloc,
42    Overflow,
43    Unsupport,
44    Unknown
45  };
46
47public:
48  Relocator(const LinkerConfig& pConfig)
49    : m_Config(pConfig)
50  {}
51
52  virtual ~Relocator() = 0;
53
54  /// apply - general apply function
55  virtual Result applyRelocation(Relocation& pRelocation) = 0;
56
57  /// scanRelocation - When read in relocations, backend can do any modification
58  /// to relocation and generate empty entries, such as GOT, dynamic relocation
59  /// entries and other target dependent entries. These entries are generated
60  /// for layout to adjust the ouput offset.
61  /// @param pReloc - a read in relocation entry
62  /// @param pInputSym - the input LDSymbol of relocation target symbol
63  /// @param pSection - the section of relocation applying target
64  virtual void scanRelocation(Relocation& pReloc,
65                              IRBuilder& pBuilder,
66                              Module& pModule,
67                              LDSection& pSection) = 0;
68
69  /// initializeScan - do initialization before scan relocations in pInput
70  /// @return - return true for initialization success
71  virtual bool initializeScan(Input& pInput)
72  { return true; }
73
74  /// finalizeScan - do finalization after scan relocations in pInput
75  /// @return - return true for finalization success
76  virtual bool finalizeScan(Input& pInput)
77  { return true; }
78
79  /// initializeApply - do initialization before apply relocations in pInput
80  /// @return - return true for initialization success
81  virtual bool initializeApply(Input& pInput)
82  { return true; }
83
84  /// finalizeApply - do finalization after apply relocations in pInput
85  /// @return - return true for finalization success
86  virtual bool finalizeApply(Input& pInput)
87  { return true; }
88
89  /// partialScanRelocation - When doing partial linking, backend can do any
90  /// modification to relocation to fix the relocation offset after section
91  /// merge
92  /// @param pReloc - a read in relocation entry
93  /// @param pInputSym - the input LDSymbol of relocation target symbol
94  /// @param pSection - the section of relocation applying target
95  virtual void partialScanRelocation(Relocation& pReloc,
96                                     Module& pModule,
97                                     const LDSection& pSection);
98
99  // ------ observers -----//
100  virtual TargetLDBackend& getTarget() = 0;
101
102  virtual const TargetLDBackend& getTarget() const = 0;
103
104  /// getName - get the name of a relocation
105  virtual const char* getName(Type pType) const = 0;
106
107  /// getSize - get the size of a relocation in bit
108  virtual Size getSize(Type pType) const = 0;
109
110protected:
111  const LinkerConfig& config() const { return m_Config; }
112
113private:
114  const LinkerConfig& m_Config;
115};
116
117} // namespace of mcld
118
119#endif
120
121