X86LDBackend.h revision 551ae4ebd3e9d137ea668fb83ae4a55b8cfba451
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  Relocator* getRelocator();
80
81  virtual void initTargetSections(Module& pModule, ObjectBuilder& pBuilder) = 0;
82
83  void initTargetSymbols(IRBuilder& pBuilder, Module& pModule);
84
85  OutputRelocSection& getRelDyn();
86  const OutputRelocSection& getRelDyn() const;
87
88  OutputRelocSection& getRelPLT();
89  const OutputRelocSection& getRelPLT() const;
90
91  LDSymbol* getGOTSymbol() { return m_pGOTSymbol; }
92  const LDSymbol* getGOTSymbol() const { return m_pGOTSymbol; }
93
94  /// getTargetSectionOrder - compute the layout order of X86 target sections
95  unsigned int getTargetSectionOrder(const LDSection& pSectHdr) const;
96
97  /// finalizeTargetSymbols - finalize the symbol value
98  bool finalizeTargetSymbols();
99
100  /// getPointerRel - get pointer relocation type.
101  Relocation::Type getPointerRel()
102  { return m_PointerRel; }
103
104  Relocation::Type getCopyRelType()    const { return m_CopyRel;    }
105  Relocation::Type getPointerRelType() const { return m_PointerRel; }
106
107protected:
108  void defineGOTSymbol(IRBuilder& pBuilder, Fragment&);
109
110  /// getRelEntrySize - the size in BYTE of rel type relocation
111  size_t getRelEntrySize()
112  { return m_RelEntrySize; }
113
114  /// getRelEntrySize - the size in BYTE of rela type relocation
115  size_t getRelaEntrySize()
116  { return m_RelaEntrySize; }
117
118private:
119  /// doCreateProgramHdrs - backend can implement this function to create the
120  /// target-dependent segments
121  void doCreateProgramHdrs(Module& pModule);
122
123  virtual void setGOTSectionSize(IRBuilder& pBuilder) = 0;
124
125  virtual uint64_t emitGOTSectionData(MemoryRegion& pRegion) const = 0;
126
127  virtual uint64_t
128  emitGOTPLTSectionData(MemoryRegion& pRegion,
129                        const ELFFileFormat* FileFormat) const = 0;
130
131  virtual void setRelDynSize() = 0;
132  virtual void setRelPLTSize() = 0;
133
134  void addEhFrameForPLT(Module& pModule);
135  virtual llvm::StringRef createCIERegionForPLT() = 0;
136  virtual llvm::StringRef createFDERegionForPLT() = 0;
137
138protected:
139  Relocator* m_pRelocator;
140  X86PLT* m_pPLT;
141  /// m_RelDyn - dynamic relocation table of .rel.dyn
142  OutputRelocSection* m_pRelDyn;
143  /// m_RelPLT - dynamic relocation table of .rel.plt
144  OutputRelocSection* m_pRelPLT;
145
146  X86ELFDynamic* m_pDynamic;
147  LDSymbol* m_pGOTSymbol;
148
149  size_t m_RelEntrySize;
150  size_t m_RelaEntrySize;
151
152  Relocation::Type m_CopyRel;
153  Relocation::Type m_PointerRel;
154};
155
156//
157//===----------------------------------------------------------------------===//
158/// X86_32GNULDBackend - linker backend of X86-32 target of GNU ELF format
159///
160class X86_32GNULDBackend : public X86GNULDBackend
161{
162public:
163  X86_32GNULDBackend(const LinkerConfig& pConfig, GNUInfo* pInfo);
164
165  ~X86_32GNULDBackend();
166
167  void initTargetSections(Module& pModule, ObjectBuilder& pBuilder);
168
169  X86_32GOT& getGOT();
170
171  const X86_32GOT& getGOT() const;
172
173  X86_32GOTPLT& getGOTPLT();
174
175  const X86_32GOTPLT& getGOTPLT() const;
176
177private:
178  /// initRelocator - create and initialize Relocator.
179  bool initRelocator();
180
181  void setGOTSectionSize(IRBuilder& pBuilder);
182
183  uint64_t emitGOTSectionData(MemoryRegion& pRegion) const;
184
185  uint64_t emitGOTPLTSectionData(MemoryRegion& pRegion,
186                                 const ELFFileFormat* FileFormat) const;
187
188  void setRelDynSize();
189  void setRelPLTSize();
190
191  llvm::StringRef createCIERegionForPLT();
192  llvm::StringRef createFDERegionForPLT();
193
194private:
195  X86_32GOT* m_pGOT;
196  X86_32GOTPLT* m_pGOTPLT;
197};
198
199//
200//===----------------------------------------------------------------------===//
201/// X86_64GNULDBackend - linker backend of X86-64 target of GNU ELF format
202///
203class X86_64GNULDBackend : public X86GNULDBackend
204{
205public:
206  X86_64GNULDBackend(const LinkerConfig& pConfig, GNUInfo* pInfo);
207
208  ~X86_64GNULDBackend();
209
210  void initTargetSections(Module& pModule, ObjectBuilder& pBuilder);
211
212  X86_64GOT& getGOT();
213
214  const X86_64GOT& getGOT() const;
215
216  X86_64GOTPLT& getGOTPLT();
217
218  const X86_64GOTPLT& getGOTPLT() const;
219
220private:
221  /// initRelocator - create and initialize Relocator.
222  bool initRelocator();
223
224  void setGOTSectionSize(IRBuilder& pBuilder);
225
226  uint64_t emitGOTSectionData(MemoryRegion& pRegion) const;
227
228  uint64_t emitGOTPLTSectionData(MemoryRegion& pRegion,
229                                 const ELFFileFormat* FileFormat) const;
230
231  void setRelDynSize();
232  void setRelPLTSize();
233
234  llvm::StringRef createCIERegionForPLT();
235  llvm::StringRef createFDERegionForPLT();
236
237private:
238  X86_64GOT* m_pGOT;
239  X86_64GOTPLT* m_pGOTPLT;
240};
241} // namespace of mcld
242
243#endif
244
245