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