X86LDBackend.h revision a790f0a8f3175183bea088389b3e4ae41813e192
1//===- X86LDBackend.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 TARGET_X86_X86LDBACKEND_H
10#define TARGET_X86_X86LDBACKEND_H
11
12#include "X86ELFDynamic.h"
13#include "X86GOT.h"
14#include "X86GOTPLT.h"
15#include "X86PLT.h"
16#include <mcld/LD/LDSection.h>
17#include <mcld/Target/GNULDBackend.h>
18#include <mcld/Target/OutputRelocSection.h>
19
20namespace mcld {
21
22class LinkerConfig;
23class GNUInfo;
24
25//===----------------------------------------------------------------------===//
26/// X86GNULDBackend - linker backend of X86 target of GNU ELF format
27///
28class X86GNULDBackend : public GNULDBackend
29{
30public:
31  X86GNULDBackend(const LinkerConfig& pConfig,
32                  GNUInfo* pInfo,
33                  Relocation::Type pCopyRel);
34
35  ~X86GNULDBackend();
36
37  uint32_t machine() const;
38
39  X86PLT& getPLT();
40
41  const X86PLT& getPLT() const;
42
43  /// preLayout - Backend can do any needed modification before layout
44  void doPreLayout(IRBuilder& pBuilder);
45
46  /// postLayout -Backend can do any needed modification after layout
47  void doPostLayout(Module& pModule, IRBuilder& pBuilder);
48
49  /// dynamic - the dynamic section of the target machine.
50  /// Use co-variant return type to return its own dynamic section.
51  X86ELFDynamic& dynamic();
52
53  /// dynamic - the dynamic section of the target machine.
54  /// Use co-variant return type to return its own dynamic section.
55  const X86ELFDynamic& dynamic() const;
56
57  /// emitSectionData - write out the section data into the memory region.
58  /// When writers get a LDSection whose kind is LDFileFormat::Target, writers
59  /// call back target backend to emit the data.
60  ///
61  /// Backends handle the target-special tables (plt, gp,...) by themselves.
62  /// Backend can put the data of the tables in MCSectionData directly
63  ///  - LDSection.getSectionData can get the section data.
64  /// Or, backend can put the data into special data structure
65  ///  - backend can maintain its own map<LDSection, table> to get the table
66  /// from given LDSection.
67  ///
68  /// @param pSection - the given LDSection
69  /// @param pLayout - for comouting the size of fragment
70  /// @param pRegion - the region to write out data
71  /// @return the size of the table in the file.
72  uint64_t emitSectionData(const LDSection& pSection,
73                           MemoryRegion& pRegion) const;
74
75  /// initRelocator - create and initialize Relocator.
76  virtual bool initRelocator() = 0;
77
78  /// getRelocator - return relocator.
79  const Relocator* getRelocator() const;
80  Relocator* getRelocator();
81
82  virtual void initTargetSections(Module& pModule, ObjectBuilder& pBuilder) = 0;
83
84  void initTargetSymbols(IRBuilder& pBuilder, Module& pModule);
85
86  OutputRelocSection& getRelDyn();
87  const OutputRelocSection& getRelDyn() const;
88
89  OutputRelocSection& getRelPLT();
90  const OutputRelocSection& getRelPLT() const;
91
92  LDSymbol* getGOTSymbol() { return m_pGOTSymbol; }
93  const LDSymbol* getGOTSymbol() const { return m_pGOTSymbol; }
94
95  /// getTargetSectionOrder - compute the layout order of X86 target sections
96  unsigned int getTargetSectionOrder(const LDSection& pSectHdr) const;
97
98  /// finalizeTargetSymbols - finalize the symbol value
99  bool finalizeTargetSymbols();
100
101  /// getPointerRel - get pointer relocation type.
102  Relocation::Type getPointerRel()
103  { return m_PointerRel; }
104
105  Relocation::Type getCopyRelType()    const { return m_CopyRel;    }
106  Relocation::Type getPointerRelType() const { return m_PointerRel; }
107
108protected:
109  void defineGOTSymbol(IRBuilder& pBuilder, Fragment&);
110
111  /// getRelEntrySize - the size in BYTE of rel type relocation
112  size_t getRelEntrySize()
113  { return m_RelEntrySize; }
114
115  /// getRelEntrySize - the size in BYTE of rela type relocation
116  size_t getRelaEntrySize()
117  { return m_RelaEntrySize; }
118
119private:
120  /// doCreateProgramHdrs - backend can implement this function to create the
121  /// target-dependent segments
122  void doCreateProgramHdrs(Module& pModule);
123
124  virtual void setGOTSectionSize(IRBuilder& pBuilder) = 0;
125
126  virtual uint64_t emitGOTSectionData(MemoryRegion& pRegion) const = 0;
127
128  virtual uint64_t
129  emitGOTPLTSectionData(MemoryRegion& pRegion,
130                        const ELFFileFormat* FileFormat) const = 0;
131
132  virtual void setRelDynSize() = 0;
133  virtual void setRelPLTSize() = 0;
134
135  void addEhFrameForPLT(Module& pModule);
136  virtual llvm::StringRef createCIERegionForPLT() = 0;
137  virtual llvm::StringRef createFDERegionForPLT() = 0;
138
139protected:
140  Relocator* m_pRelocator;
141  X86PLT* m_pPLT;
142  /// m_RelDyn - dynamic relocation table of .rel.dyn
143  OutputRelocSection* m_pRelDyn;
144  /// m_RelPLT - dynamic relocation table of .rel.plt
145  OutputRelocSection* m_pRelPLT;
146
147  X86ELFDynamic* m_pDynamic;
148  LDSymbol* m_pGOTSymbol;
149
150  size_t m_RelEntrySize;
151  size_t m_RelaEntrySize;
152
153  Relocation::Type m_CopyRel;
154  Relocation::Type m_PointerRel;
155};
156
157//
158//===----------------------------------------------------------------------===//
159/// X86_32GNULDBackend - linker backend of X86-32 target of GNU ELF format
160///
161class X86_32GNULDBackend : public X86GNULDBackend
162{
163public:
164  X86_32GNULDBackend(const LinkerConfig& pConfig, GNUInfo* pInfo);
165
166  ~X86_32GNULDBackend();
167
168  void initTargetSections(Module& pModule, ObjectBuilder& pBuilder);
169
170  X86_32GOT& getGOT();
171
172  const X86_32GOT& getGOT() const;
173
174  X86_32GOTPLT& getGOTPLT();
175
176  const X86_32GOTPLT& getGOTPLT() const;
177
178private:
179  /// initRelocator - create and initialize Relocator.
180  bool initRelocator();
181
182  void setGOTSectionSize(IRBuilder& pBuilder);
183
184  uint64_t emitGOTSectionData(MemoryRegion& pRegion) const;
185
186  uint64_t emitGOTPLTSectionData(MemoryRegion& pRegion,
187                                 const ELFFileFormat* FileFormat) const;
188
189  void setRelDynSize();
190  void setRelPLTSize();
191
192  llvm::StringRef createCIERegionForPLT();
193  llvm::StringRef createFDERegionForPLT();
194
195private:
196  X86_32GOT* m_pGOT;
197  X86_32GOTPLT* m_pGOTPLT;
198};
199
200//
201//===----------------------------------------------------------------------===//
202/// X86_64GNULDBackend - linker backend of X86-64 target of GNU ELF format
203///
204class X86_64GNULDBackend : public X86GNULDBackend
205{
206public:
207  X86_64GNULDBackend(const LinkerConfig& pConfig, GNUInfo* pInfo);
208
209  ~X86_64GNULDBackend();
210
211  void initTargetSections(Module& pModule, ObjectBuilder& pBuilder);
212
213  X86_64GOT& getGOT();
214
215  const X86_64GOT& getGOT() const;
216
217  X86_64GOTPLT& getGOTPLT();
218
219  const X86_64GOTPLT& getGOTPLT() const;
220
221private:
222  /// initRelocator - create and initialize Relocator.
223  bool initRelocator();
224
225  void setGOTSectionSize(IRBuilder& pBuilder);
226
227  uint64_t emitGOTSectionData(MemoryRegion& pRegion) const;
228
229  uint64_t emitGOTPLTSectionData(MemoryRegion& pRegion,
230                                 const ELFFileFormat* FileFormat) const;
231
232  void setRelDynSize();
233  void setRelPLTSize();
234
235  llvm::StringRef createCIERegionForPLT();
236  llvm::StringRef createFDERegionForPLT();
237
238private:
239  X86_64GOT* m_pGOT;
240  X86_64GOTPLT* m_pGOTPLT;
241};
242} // namespace of mcld
243
244#endif
245
246