Relocator.h revision 87f34658dec9097d987d254a990ea7f311bfc95f
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  /// @param pInput - the input file of relocation
65  virtual void scanRelocation(Relocation& pReloc,
66                              IRBuilder& pBuilder,
67                              Module& pModule,
68                              LDSection& pSection,
69                              Input& pInput) = 0;
70
71  /// issueUndefRefError - Provides a basic version for undefined reference dump.
72  /// It will handle the filename and function name automatically.
73  /// @param pReloc - a read in relocation entry
74  /// @param pSection - the section of relocation applying target
75  /// @ param pInput - the input file of relocation
76  virtual void issueUndefRef(Relocation& pReloc,
77                             LDSection& pSection,
78                             Input& pInput);
79
80  /// initializeScan - do initialization before scan relocations in pInput
81  /// @return - return true for initialization success
82  virtual bool initializeScan(Input& pInput)
83  { return true; }
84
85  /// finalizeScan - do finalization after scan relocations in pInput
86  /// @return - return true for finalization success
87  virtual bool finalizeScan(Input& pInput)
88  { return true; }
89
90  /// initializeApply - do initialization before apply relocations in pInput
91  /// @return - return true for initialization success
92  virtual bool initializeApply(Input& pInput)
93  { return true; }
94
95  /// finalizeApply - do finalization after apply relocations in pInput
96  /// @return - return true for finalization success
97  virtual bool finalizeApply(Input& pInput)
98  { return true; }
99
100  /// partialScanRelocation - When doing partial linking, backend can do any
101  /// modification to relocation to fix the relocation offset after section
102  /// merge
103  /// @param pReloc - a read in relocation entry
104  /// @param pInputSym - the input LDSymbol of relocation target symbol
105  /// @param pSection - the section of relocation applying target
106  virtual void partialScanRelocation(Relocation& pReloc,
107                                     Module& pModule,
108                                     const LDSection& pSection);
109
110  // ------ observers -----//
111  virtual TargetLDBackend& getTarget() = 0;
112
113  virtual const TargetLDBackend& getTarget() const = 0;
114
115  /// getName - get the name of a relocation
116  virtual const char* getName(Type pType) const = 0;
117
118  /// getSize - get the size of a relocation in bit
119  virtual Size getSize(Type pType) const = 0;
120
121protected:
122  const LinkerConfig& config() const { return m_Config; }
123
124private:
125  const LinkerConfig& m_Config;
126};
127
128} // namespace of mcld
129
130#endif
131
132