]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Arch/X86.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r306956, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / Arch / X86.cpp
1 //===- X86.cpp ------------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "Error.h"
11 #include "InputFiles.h"
12 #include "Symbols.h"
13 #include "SyntheticSections.h"
14 #include "Target.h"
15 #include "llvm/Support/Endian.h"
16
17 using namespace llvm;
18 using namespace llvm::support::endian;
19 using namespace llvm::ELF;
20 using namespace lld;
21 using namespace lld::elf;
22
23 namespace {
24 class X86 final : public TargetInfo {
25 public:
26   X86();
27   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S,
28                      const uint8_t *Loc) const override;
29   int64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
30   void writeGotPltHeader(uint8_t *Buf) const override;
31   uint32_t getDynRel(uint32_t Type) const override;
32   void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override;
33   void writeIgotPlt(uint8_t *Buf, const SymbolBody &S) const override;
34   void writePltHeader(uint8_t *Buf) const override;
35   void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
36                 int32_t Index, unsigned RelOff) const override;
37   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
38
39   RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
40                           RelExpr Expr) const override;
41   void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
42   void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
43   void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
44   void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
45 };
46 } // namespace
47
48 X86::X86() {
49   GotBaseSymOff = -1;
50   CopyRel = R_386_COPY;
51   GotRel = R_386_GLOB_DAT;
52   PltRel = R_386_JUMP_SLOT;
53   IRelativeRel = R_386_IRELATIVE;
54   RelativeRel = R_386_RELATIVE;
55   TlsGotRel = R_386_TLS_TPOFF;
56   TlsModuleIndexRel = R_386_TLS_DTPMOD32;
57   TlsOffsetRel = R_386_TLS_DTPOFF32;
58   GotEntrySize = 4;
59   GotPltEntrySize = 4;
60   PltEntrySize = 16;
61   PltHeaderSize = 16;
62   TlsGdRelaxSkip = 2;
63   TrapInstr = 0xcccccccc; // 0xcc = INT3
64 }
65
66 RelExpr X86::getRelExpr(uint32_t Type, const SymbolBody &S,
67                         const uint8_t *Loc) const {
68   switch (Type) {
69   case R_386_8:
70   case R_386_16:
71   case R_386_32:
72   case R_386_TLS_LDO_32:
73     return R_ABS;
74   case R_386_TLS_GD:
75     return R_TLSGD;
76   case R_386_TLS_LDM:
77     return R_TLSLD;
78   case R_386_PLT32:
79     return R_PLT_PC;
80   case R_386_PC8:
81   case R_386_PC16:
82   case R_386_PC32:
83     return R_PC;
84   case R_386_GOTPC:
85     return R_GOTONLY_PC_FROM_END;
86   case R_386_TLS_IE:
87     return R_GOT;
88   case R_386_GOT32:
89   case R_386_GOT32X:
90     // These relocations can be calculated in two different ways.
91     // Usual calculation is G + A - GOT what means an offset in GOT table
92     // (R_GOT_FROM_END). When instruction pointed by relocation has no base
93     // register, then relocations can be used when PIC code is disabled. In that
94     // case calculation is G + A, it resolves to an address of entry in GOT
95     // (R_GOT) and not an offset.
96     //
97     // To check that instruction has no base register we scan ModR/M byte.
98     // See "Table 2-2. 32-Bit Addressing Forms with the ModR/M Byte"
99     // (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
100     //  64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
101     if ((Loc[-1] & 0xc7) != 0x5)
102       return R_GOT_FROM_END;
103     if (Config->Pic)
104       error(toString(S.File) + ": relocation " + toString(Type) + " against '" +
105             S.getName() +
106             "' without base register can not be used when PIC enabled");
107     return R_GOT;
108   case R_386_TLS_GOTIE:
109     return R_GOT_FROM_END;
110   case R_386_GOTOFF:
111     return R_GOTREL_FROM_END;
112   case R_386_TLS_LE:
113     return R_TLS;
114   case R_386_TLS_LE_32:
115     return R_NEG_TLS;
116   case R_386_NONE:
117     return R_NONE;
118   default:
119     error(toString(S.File) + ": unknown relocation type: " + toString(Type));
120     return R_HINT;
121   }
122 }
123
124 RelExpr X86::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
125                              RelExpr Expr) const {
126   switch (Expr) {
127   default:
128     return Expr;
129   case R_RELAX_TLS_GD_TO_IE:
130     return R_RELAX_TLS_GD_TO_IE_END;
131   case R_RELAX_TLS_GD_TO_LE:
132     return R_RELAX_TLS_GD_TO_LE_NEG;
133   }
134 }
135
136 void X86::writeGotPltHeader(uint8_t *Buf) const {
137   write32le(Buf, InX::Dynamic->getVA());
138 }
139
140 void X86::writeGotPlt(uint8_t *Buf, const SymbolBody &S) const {
141   // Entries in .got.plt initially points back to the corresponding
142   // PLT entries with a fixed offset to skip the first instruction.
143   write32le(Buf, S.getPltVA() + 6);
144 }
145
146 void X86::writeIgotPlt(uint8_t *Buf, const SymbolBody &S) const {
147   // An x86 entry is the address of the ifunc resolver function.
148   write32le(Buf, S.getVA());
149 }
150
151 uint32_t X86::getDynRel(uint32_t Type) const {
152   if (Type == R_386_TLS_LE)
153     return R_386_TLS_TPOFF;
154   if (Type == R_386_TLS_LE_32)
155     return R_386_TLS_TPOFF32;
156   return Type;
157 }
158
159 void X86::writePltHeader(uint8_t *Buf) const {
160   if (Config->Pic) {
161     const uint8_t V[] = {
162         0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl GOTPLT+4(%ebx)
163         0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *GOTPLT+8(%ebx)
164         0x90, 0x90, 0x90, 0x90              // nop
165     };
166     memcpy(Buf, V, sizeof(V));
167
168     uint32_t Ebx = InX::Got->getVA() + InX::Got->getSize();
169     uint32_t GotPlt = InX::GotPlt->getVA() - Ebx;
170     write32le(Buf + 2, GotPlt + 4);
171     write32le(Buf + 8, GotPlt + 8);
172     return;
173   }
174
175   const uint8_t PltData[] = {
176       0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOTPLT+4)
177       0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOTPLT+8)
178       0x90, 0x90, 0x90, 0x90              // nop
179   };
180   memcpy(Buf, PltData, sizeof(PltData));
181   uint32_t GotPlt = InX::GotPlt->getVA();
182   write32le(Buf + 2, GotPlt + 4);
183   write32le(Buf + 8, GotPlt + 8);
184 }
185
186 void X86::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
187                    uint64_t PltEntryAddr, int32_t Index,
188                    unsigned RelOff) const {
189   const uint8_t Inst[] = {
190       0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
191       0x68, 0x00, 0x00, 0x00, 0x00,       // pushl $reloc_offset
192       0xe9, 0x00, 0x00, 0x00, 0x00        // jmp .PLT0@PC
193   };
194   memcpy(Buf, Inst, sizeof(Inst));
195
196   if (Config->Pic) {
197     // jmp *foo@GOT(%ebx)
198     uint32_t Ebx = InX::Got->getVA() + InX::Got->getSize();
199     Buf[1] = 0xa3;
200     write32le(Buf + 2, GotPltEntryAddr - Ebx);
201   } else {
202     // jmp *foo_in_GOT
203     Buf[1] = 0x25;
204     write32le(Buf + 2, GotPltEntryAddr);
205   }
206
207   write32le(Buf + 7, RelOff);
208   write32le(Buf + 12, -Index * PltEntrySize - PltHeaderSize - 16);
209 }
210
211 int64_t X86::getImplicitAddend(const uint8_t *Buf, uint32_t Type) const {
212   switch (Type) {
213   default:
214     return 0;
215   case R_386_8:
216   case R_386_PC8:
217     return SignExtend64<8>(*Buf);
218   case R_386_16:
219   case R_386_PC16:
220     return SignExtend64<16>(read16le(Buf));
221   case R_386_32:
222   case R_386_GOT32:
223   case R_386_GOT32X:
224   case R_386_GOTOFF:
225   case R_386_GOTPC:
226   case R_386_PC32:
227   case R_386_PLT32:
228   case R_386_TLS_LDO_32:
229   case R_386_TLS_LE:
230     return SignExtend64<32>(read32le(Buf));
231   }
232 }
233
234 void X86::relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
235   // R_386_{PC,}{8,16} are not part of the i386 psABI, but they are
236   // being used for some 16-bit programs such as boot loaders, so
237   // we want to support them.
238   switch (Type) {
239   case R_386_8:
240     checkUInt<8>(Loc, Val, Type);
241     *Loc = Val;
242     break;
243   case R_386_PC8:
244     checkInt<8>(Loc, Val, Type);
245     *Loc = Val;
246     break;
247   case R_386_16:
248     checkUInt<16>(Loc, Val, Type);
249     write16le(Loc, Val);
250     break;
251   case R_386_PC16:
252     // R_386_PC16 is normally used with 16 bit code. In that situation
253     // the PC is 16 bits, just like the addend. This means that it can
254     // point from any 16 bit address to any other if the possibility
255     // of wrapping is included.
256     // The only restriction we have to check then is that the destination
257     // address fits in 16 bits. That is impossible to do here. The problem is
258     // that we are passed the final value, which already had the
259     // current location subtracted from it.
260     // We just check that Val fits in 17 bits. This misses some cases, but
261     // should have no false positives.
262     checkInt<17>(Loc, Val, Type);
263     write16le(Loc, Val);
264     break;
265   default:
266     checkInt<32>(Loc, Val, Type);
267     write32le(Loc, Val);
268   }
269 }
270
271 void X86::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
272   // Convert
273   //   leal x@tlsgd(, %ebx, 1),
274   //   call __tls_get_addr@plt
275   // to
276   //   movl %gs:0,%eax
277   //   subl $x@ntpoff,%eax
278   const uint8_t Inst[] = {
279       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
280       0x81, 0xe8, 0x00, 0x00, 0x00, 0x00  // subl 0(%ebx), %eax
281   };
282   memcpy(Loc - 3, Inst, sizeof(Inst));
283   write32le(Loc + 5, Val);
284 }
285
286 void X86::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
287   // Convert
288   //   leal x@tlsgd(, %ebx, 1),
289   //   call __tls_get_addr@plt
290   // to
291   //   movl %gs:0, %eax
292   //   addl x@gotntpoff(%ebx), %eax
293   const uint8_t Inst[] = {
294       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
295       0x03, 0x83, 0x00, 0x00, 0x00, 0x00  // addl 0(%ebx), %eax
296   };
297   memcpy(Loc - 3, Inst, sizeof(Inst));
298   write32le(Loc + 5, Val);
299 }
300
301 // In some conditions, relocations can be optimized to avoid using GOT.
302 // This function does that for Initial Exec to Local Exec case.
303 void X86::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
304   // Ulrich's document section 6.2 says that @gotntpoff can
305   // be used with MOVL or ADDL instructions.
306   // @indntpoff is similar to @gotntpoff, but for use in
307   // position dependent code.
308   uint8_t Reg = (Loc[-1] >> 3) & 7;
309
310   if (Type == R_386_TLS_IE) {
311     if (Loc[-1] == 0xa1) {
312       // "movl foo@indntpoff,%eax" -> "movl $foo,%eax"
313       // This case is different from the generic case below because
314       // this is a 5 byte instruction while below is 6 bytes.
315       Loc[-1] = 0xb8;
316     } else if (Loc[-2] == 0x8b) {
317       // "movl foo@indntpoff,%reg" -> "movl $foo,%reg"
318       Loc[-2] = 0xc7;
319       Loc[-1] = 0xc0 | Reg;
320     } else {
321       // "addl foo@indntpoff,%reg" -> "addl $foo,%reg"
322       Loc[-2] = 0x81;
323       Loc[-1] = 0xc0 | Reg;
324     }
325   } else {
326     assert(Type == R_386_TLS_GOTIE);
327     if (Loc[-2] == 0x8b) {
328       // "movl foo@gottpoff(%rip),%reg" -> "movl $foo,%reg"
329       Loc[-2] = 0xc7;
330       Loc[-1] = 0xc0 | Reg;
331     } else {
332       // "addl foo@gotntpoff(%rip),%reg" -> "leal foo(%reg),%reg"
333       Loc[-2] = 0x8d;
334       Loc[-1] = 0x80 | (Reg << 3) | Reg;
335     }
336   }
337   write32le(Loc, Val);
338 }
339
340 void X86::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
341   if (Type == R_386_TLS_LDO_32) {
342     write32le(Loc, Val);
343     return;
344   }
345
346   // Convert
347   //   leal foo(%reg),%eax
348   //   call ___tls_get_addr
349   // to
350   //   movl %gs:0,%eax
351   //   nop
352   //   leal 0(%esi,1),%esi
353   const uint8_t Inst[] = {
354       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
355       0x90,                               // nop
356       0x8d, 0x74, 0x26, 0x00              // leal 0(%esi,1),%esi
357   };
358   memcpy(Loc - 2, Inst, sizeof(Inst));
359 }
360
361 TargetInfo *elf::getX86TargetInfo() {
362   static X86 Target;
363   return &Target;
364 }