summaryrefslogtreecommitdiff
path: root/lib/Target/X86/X86LDBackend.h
blob: a008d00c561129a227fd888616197a0b6f08d6ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//===- X86LDBackend.h -----------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef X86_LDBACKEND_H
#define X86_LDBACKEND_H

#include "X86ELFDynamic.h"
#include "X86GOT.h"
#include "X86GOTPLT.h"
#include "X86PLT.h"
#include <mcld/LD/LDSection.h>
#include <mcld/Target/GNULDBackend.h>
#include <mcld/Target/OutputRelocSection.h>

namespace mcld {

class LinkerConfig;
class GNUInfo;

//===----------------------------------------------------------------------===//
/// X86GNULDBackend - linker backend of X86 target of GNU ELF format
///
class X86GNULDBackend : public GNULDBackend
{
public:
  /** \enum ReservedEntryType
   *  \brief The reserved entry type of reserved space in ResolveInfo.
   *
   *  This is used for sacnRelocation to record what kinds of entries are
   *  reserved for this resolved symbol
   *
   *  In X86, there are three kinds of entries, GOT, PLT, and dynamic reloction.
   *  GOT may needs a corresponding relocation to relocate itself, so we
   *  separate GOT to two situations: GOT and GOTRel. Besides, for the same
   *  symbol, there might be two kinds of entries reserved for different location.
   *  For example, reference to the same symbol, one may use GOT and the other may
   *  use dynamic relocation.
   *
   *  bit:  3       2      1     0
   *   | PLT | GOTRel | GOT | Rel |
   *
   *  value    Name         - Description
   *
   *  0000     None         - no reserved entry
   *  0001     ReserveRel   - reserve an dynamic relocation entry
   *  0010     ReserveGOT   - reserve an GOT entry
   *  0011     GOTandRel    - For different relocation, we've reserved GOT and
   *                          Rel for different location.
   *  0100     GOTRel       - reserve an GOT entry and the corresponding Dyncamic
   *                          relocation entry which relocate this GOT entry
   *  0101     GOTRelandRel - For different relocation, we've reserved GOTRel
   *                          and relocation entry for different location.
   *  1000     ReservePLT   - reserve an PLT entry and the corresponding GOT,
   *                          Dynamic relocation entries
   *  1001     PLTandRel    - For different relocation, we've reserved PLT and
   *                          Rel for different location.
   */
  enum ReservedEntryType {
    None         = 0,
    ReserveRel   = 1,
    ReserveGOT   = 2,
    GOTandRel    = 3,
    GOTRel       = 4,
    GOTRelandRel = 5,
    ReservePLT   = 8,
    PLTandRel    = 9
  };

public:
  X86GNULDBackend(const LinkerConfig& pConfig,
		  GNUInfo* pInfo,
		  Relocation::Type pCopyRel);

  ~X86GNULDBackend();

  uint32_t machine() const;

  X86PLT& getPLT();

  const X86PLT& getPLT() const;

  /// preLayout - Backend can do any needed modification before layout
  void doPreLayout(IRBuilder& pBuilder);

  /// postLayout -Backend can do any needed modification after layout
  void doPostLayout(Module& pModule, IRBuilder& pBuilder);

  /// dynamic - the dynamic section of the target machine.
  /// Use co-variant return type to return its own dynamic section.
  X86ELFDynamic& dynamic();

  /// dynamic - the dynamic section of the target machine.
  /// Use co-variant return type to return its own dynamic section.
  const X86ELFDynamic& dynamic() const;

  /// emitSectionData - write out the section data into the memory region.
  /// When writers get a LDSection whose kind is LDFileFormat::Target, writers
  /// call back target backend to emit the data.
  ///
  /// Backends handle the target-special tables (plt, gp,...) by themselves.
  /// Backend can put the data of the tables in MCSectionData directly
  ///  - LDSection.getSectionData can get the section data.
  /// Or, backend can put the data into special data structure
  ///  - backend can maintain its own map<LDSection, table> to get the table
  /// from given LDSection.
  ///
  /// @param pSection - the given LDSection
  /// @param pLayout - for comouting the size of fragment
  /// @param pRegion - the region to write out data
  /// @return the size of the table in the file.
  uint64_t emitSectionData(const LDSection& pSection,
                           MemoryRegion& pRegion) const;

  /// initRelocator - create and initialize Relocator.
  virtual bool initRelocator() = 0;

  /// getRelocator - return relocator.
  Relocator* getRelocator();

  virtual void initTargetSections(Module& pModule, ObjectBuilder& pBuilder) = 0;

  void initTargetSymbols(IRBuilder& pBuilder, Module& pModule);

  /// scanRelocation - determine the empty entries are needed or not and create
  /// the empty entries if needed.
  /// For X86, following entries are check to create:
  /// - GOT entry (for .got and .got.plt sections)
  /// - PLT entry (for .plt section)
  /// - dynamin relocation entries (for .rel.plt and .rel.dyn sections)
  void scanRelocation(Relocation& pReloc,
                      IRBuilder& pBuilder,
                      Module& pModule,
                      LDSection& pSection);

  OutputRelocSection& getRelDyn();

  const OutputRelocSection& getRelDyn() const;

  OutputRelocSection& getRelPLT();

  const OutputRelocSection& getRelPLT() const;

  /// getTargetSectionOrder - compute the layout order of X86 target sections
  unsigned int getTargetSectionOrder(const LDSection& pSectHdr) const;

  /// finalizeTargetSymbols - finalize the symbol value
  bool finalizeTargetSymbols();

  /// getPointerRel - get pointer relocation type. 
  Relocation::Type getPointerRel()
  { return m_PointerRel; }

private:
  virtual void scanLocalReloc(Relocation& pReloc,
			      IRBuilder& pBuilder,
			      Module& pModule,
			      LDSection& pSection) = 0;

  virtual void scanGlobalReloc(Relocation& pReloc,
			       IRBuilder& pBuilder,
			       Module& pModule,
			       LDSection& pSection) = 0;

protected:
  /// addCopyReloc - add a copy relocation into .rel.dyn for pSym
  /// @param pSym - A resolved copy symbol that defined in BSS section
  void addCopyReloc(ResolveInfo& pSym);

  /// defineSymbolforCopyReloc - allocate a space in BSS section and
  /// and force define the copy of pSym to BSS section
  /// @return the output LDSymbol of the copy symbol
  LDSymbol& defineSymbolforCopyReloc(IRBuilder& pLinker,
                                     const ResolveInfo& pSym);

  void defineGOTSymbol(IRBuilder& pBuilder, Fragment&);

protected:
  /// getRelEntrySize - the size in BYTE of rel type relocation
  size_t getRelEntrySize()
  { return m_RelEntrySize; }

  /// getRelEntrySize - the size in BYTE of rela type relocation
  size_t getRelaEntrySize()
  { return m_RelaEntrySize; }

private:
  /// doCreateProgramHdrs - backend can implement this function to create the
  /// target-dependent segments
  void doCreateProgramHdrs(Module& pModule);

  virtual void setGOTSectionSize(IRBuilder& pBuilder) = 0;

  virtual uint64_t emitGOTSectionData(MemoryRegion& pRegion) const = 0;

  virtual uint64_t emitGOTPLTSectionData(MemoryRegion& pRegion,
					 const ELFFileFormat* FileFormat) const = 0;

  virtual void setRelDynSize() = 0;
  virtual void setRelPLTSize() = 0;

protected:
  Relocator* m_pRelocator;
  X86PLT* m_pPLT;
  /// m_RelDyn - dynamic relocation table of .rel.dyn
  OutputRelocSection* m_pRelDyn;
  /// m_RelPLT - dynamic relocation table of .rel.plt
  OutputRelocSection* m_pRelPLT;

  X86ELFDynamic* m_pDynamic;
  LDSymbol* m_pGOTSymbol;

  size_t m_RelEntrySize;
  size_t m_RelaEntrySize;

  Relocation::Type m_CopyRel;
  Relocation::Type m_PointerRel;
};

//
//===----------------------------------------------------------------------===//
/// X86_32GNULDBackend - linker backend of X86-32 target of GNU ELF format
///
class X86_32GNULDBackend : public X86GNULDBackend
{
public:
  X86_32GNULDBackend(const LinkerConfig& pConfig, GNUInfo* pInfo);

  ~X86_32GNULDBackend();

  void initTargetSections(Module& pModule, ObjectBuilder& pBuilder);

  X86_32GOT& getGOT();

  const X86_32GOT& getGOT() const;

  X86_32GOTPLT& getGOTPLT();

  const X86_32GOTPLT& getGOTPLT() const;

  X86_32GOTEntry& getTLSModuleID();

private:
  void scanLocalReloc(Relocation& pReloc,
                      IRBuilder& pBuilder,
                      Module& pModule,
                      LDSection& pSection);

  void scanGlobalReloc(Relocation& pReloc,
                       IRBuilder& pBuilder,
                       Module& pModule,
                       LDSection& pSection);

  /// initRelocator - create and initialize Relocator.
  bool initRelocator();

  /// -----  tls optimization  ----- ///
  /// convert R_386_TLS_IE to R_386_TLS_LE
  void convertTLSIEtoLE(Relocation& pReloc, LDSection& pSection);

  void setGOTSectionSize(IRBuilder& pBuilder);

  uint64_t emitGOTSectionData(MemoryRegion& pRegion) const;

  uint64_t emitGOTPLTSectionData(MemoryRegion& pRegion,
				 const ELFFileFormat* FileFormat) const;

  void setRelDynSize();
  void setRelPLTSize();

private:
  X86_32GOT* m_pGOT;
  X86_32GOTPLT* m_pGOTPLT;
};

//
//===----------------------------------------------------------------------===//
/// X86_64GNULDBackend - linker backend of X86-64 target of GNU ELF format
///
class X86_64GNULDBackend : public X86GNULDBackend
{
public:
  X86_64GNULDBackend(const LinkerConfig& pConfig, GNUInfo* pInfo);

  ~X86_64GNULDBackend();

  void initTargetSections(Module& pModule, ObjectBuilder& pBuilder);

  X86_64GOT& getGOT();

  const X86_64GOT& getGOT() const;

  X86_64GOTPLT& getGOTPLT();

  const X86_64GOTPLT& getGOTPLT() const;

private:
  void scanLocalReloc(Relocation& pReloc,
                      IRBuilder& pBuilder,
                      Module& pModule,
                      LDSection& pSection);

  void scanGlobalReloc(Relocation& pReloc,
                       IRBuilder& pBuilder,
                       Module& pModule,
                       LDSection& pSection);

  /// initRelocator - create and initialize Relocator.
  bool initRelocator();

  void setGOTSectionSize(IRBuilder& pBuilder);

  uint64_t emitGOTSectionData(MemoryRegion& pRegion) const;

  uint64_t emitGOTPLTSectionData(MemoryRegion& pRegion,
				 const ELFFileFormat* FileFormat) const;

  void setRelDynSize();
  void setRelPLTSize();

private:
  X86_64GOT* m_pGOT;
  X86_64GOTPLT* m_pGOTPLT;
};
} // namespace of mcld

#endif