]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Arch/X86.cpp
Merge lld trunk r321017 to contrib/llvm/tools/lld.
[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 "InputFiles.h"
11 #include "Symbols.h"
12 #include "SyntheticSections.h"
13 #include "Target.h"
14 #include "lld/Common/ErrorHandler.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(RelType Type, const Symbol &S,
28                      const uint8_t *Loc) const override;
29   int64_t getImplicitAddend(const uint8_t *Buf, RelType Type) const override;
30   void writeGotPltHeader(uint8_t *Buf) const override;
31   RelType getDynRel(RelType Type) const override;
32   void writeGotPlt(uint8_t *Buf, const Symbol &S) const override;
33   void writeIgotPlt(uint8_t *Buf, const Symbol &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, RelType Type, uint64_t Val) const override;
38
39   RelExpr adjustRelaxExpr(RelType Type, const uint8_t *Data,
40                           RelExpr Expr) const override;
41   void relaxTlsGdToIe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
42   void relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
43   void relaxTlsIeToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
44   void relaxTlsLdToLe(uint8_t *Loc, RelType 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 static bool hasBaseReg(uint8_t ModRM) { return (ModRM & 0xc7) != 0x5; }
67
68 RelExpr X86::getRelExpr(RelType Type, const Symbol &S,
69                         const uint8_t *Loc) const {
70   switch (Type) {
71   case R_386_8:
72   case R_386_16:
73   case R_386_32:
74   case R_386_TLS_LDO_32:
75     return R_ABS;
76   case R_386_TLS_GD:
77     return R_TLSGD;
78   case R_386_TLS_LDM:
79     return R_TLSLD;
80   case R_386_PLT32:
81     return R_PLT_PC;
82   case R_386_PC8:
83   case R_386_PC16:
84   case R_386_PC32:
85     return R_PC;
86   case R_386_GOTPC:
87     return R_GOTONLY_PC_FROM_END;
88   case R_386_TLS_IE:
89     return R_GOT;
90   case R_386_GOT32:
91   case R_386_GOT32X:
92     // These relocations are arguably mis-designed because their calculations
93     // depend on the instructions they are applied to. This is bad because we
94     // usually don't care about whether the target section contains valid
95     // machine instructions or not. But this is part of the documented ABI, so
96     // we had to implement as the standard requires.
97     //
98     // x86 does not support PC-relative data access. Therefore, in order to
99     // access GOT contents, a GOT address needs to be known at link-time
100     // (which means non-PIC) or compilers have to emit code to get a GOT
101     // address at runtime (which means code is position-independent but
102     // compilers need to emit extra code for each GOT access.) This decision
103     // is made at compile-time. In the latter case, compilers emit code to
104     // load an GOT address to a register, which is usually %ebx.
105     //
106     // So, there are two ways to refer to symbol foo's GOT entry: foo@GOT or
107     // foo@GOT(%reg).
108     //
109     // foo@GOT is not usable in PIC. If we are creating a PIC output and if we
110     // find such relocation, we should report an error. foo@GOT is resolved to
111     // an *absolute* address of foo's GOT entry, because both GOT address and
112     // foo's offset are known. In other words, it's G + A.
113     //
114     // foo@GOT(%reg) needs to be resolved to a *relative* offset from a GOT to
115     // foo's GOT entry in the table, because GOT address is not known but foo's
116     // offset in the table is known. It's G + A - GOT.
117     //
118     // It's unfortunate that compilers emit the same relocation for these
119     // different use cases. In order to distinguish them, we have to read a
120     // machine instruction.
121     //
122     // The following code implements it. We assume that Loc[0] is the first
123     // byte of a displacement or an immediate field of a valid machine
124     // instruction. That means a ModRM byte is at Loc[-1]. By taking a look at
125     // the byte, we can determine whether the instruction is register-relative
126     // (i.e. it was generated for foo@GOT(%reg)) or absolute (i.e. foo@GOT).
127     return hasBaseReg(Loc[-1]) ? R_GOT_FROM_END : R_GOT;
128   case R_386_TLS_GOTIE:
129     return R_GOT_FROM_END;
130   case R_386_GOTOFF:
131     return R_GOTREL_FROM_END;
132   case R_386_TLS_LE:
133     return R_TLS;
134   case R_386_TLS_LE_32:
135     return R_NEG_TLS;
136   case R_386_NONE:
137     return R_NONE;
138   default:
139     return R_INVALID;
140   }
141 }
142
143 RelExpr X86::adjustRelaxExpr(RelType Type, const uint8_t *Data,
144                              RelExpr Expr) const {
145   switch (Expr) {
146   default:
147     return Expr;
148   case R_RELAX_TLS_GD_TO_IE:
149     return R_RELAX_TLS_GD_TO_IE_END;
150   case R_RELAX_TLS_GD_TO_LE:
151     return R_RELAX_TLS_GD_TO_LE_NEG;
152   }
153 }
154
155 void X86::writeGotPltHeader(uint8_t *Buf) const {
156   write32le(Buf, InX::Dynamic->getVA());
157 }
158
159 void X86::writeGotPlt(uint8_t *Buf, const Symbol &S) const {
160   // Entries in .got.plt initially points back to the corresponding
161   // PLT entries with a fixed offset to skip the first instruction.
162   write32le(Buf, S.getPltVA() + 6);
163 }
164
165 void X86::writeIgotPlt(uint8_t *Buf, const Symbol &S) const {
166   // An x86 entry is the address of the ifunc resolver function.
167   write32le(Buf, S.getVA());
168 }
169
170 RelType X86::getDynRel(RelType Type) const {
171   if (Type == R_386_TLS_LE)
172     return R_386_TLS_TPOFF;
173   if (Type == R_386_TLS_LE_32)
174     return R_386_TLS_TPOFF32;
175   return Type;
176 }
177
178 void X86::writePltHeader(uint8_t *Buf) const {
179   if (Config->Pic) {
180     const uint8_t V[] = {
181         0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl GOTPLT+4(%ebx)
182         0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *GOTPLT+8(%ebx)
183         0x90, 0x90, 0x90, 0x90              // nop
184     };
185     memcpy(Buf, V, sizeof(V));
186
187     uint32_t Ebx = InX::Got->getVA() + InX::Got->getSize();
188     uint32_t GotPlt = InX::GotPlt->getVA() - Ebx;
189     write32le(Buf + 2, GotPlt + 4);
190     write32le(Buf + 8, GotPlt + 8);
191     return;
192   }
193
194   const uint8_t PltData[] = {
195       0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOTPLT+4)
196       0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOTPLT+8)
197       0x90, 0x90, 0x90, 0x90              // nop
198   };
199   memcpy(Buf, PltData, sizeof(PltData));
200   uint32_t GotPlt = InX::GotPlt->getVA();
201   write32le(Buf + 2, GotPlt + 4);
202   write32le(Buf + 8, GotPlt + 8);
203 }
204
205 void X86::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
206                    uint64_t PltEntryAddr, int32_t Index,
207                    unsigned RelOff) const {
208   const uint8_t Inst[] = {
209       0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
210       0x68, 0x00, 0x00, 0x00, 0x00,       // pushl $reloc_offset
211       0xe9, 0x00, 0x00, 0x00, 0x00        // jmp .PLT0@PC
212   };
213   memcpy(Buf, Inst, sizeof(Inst));
214
215   if (Config->Pic) {
216     // jmp *foo@GOT(%ebx)
217     uint32_t Ebx = InX::Got->getVA() + InX::Got->getSize();
218     Buf[1] = 0xa3;
219     write32le(Buf + 2, GotPltEntryAddr - Ebx);
220   } else {
221     // jmp *foo_in_GOT
222     Buf[1] = 0x25;
223     write32le(Buf + 2, GotPltEntryAddr);
224   }
225
226   write32le(Buf + 7, RelOff);
227   write32le(Buf + 12, -Index * PltEntrySize - PltHeaderSize - 16);
228 }
229
230 int64_t X86::getImplicitAddend(const uint8_t *Buf, RelType Type) const {
231   switch (Type) {
232   case R_386_8:
233   case R_386_PC8:
234     return SignExtend64<8>(*Buf);
235   case R_386_16:
236   case R_386_PC16:
237     return SignExtend64<16>(read16le(Buf));
238   case R_386_32:
239   case R_386_GOT32:
240   case R_386_GOT32X:
241   case R_386_GOTOFF:
242   case R_386_GOTPC:
243   case R_386_PC32:
244   case R_386_PLT32:
245   case R_386_TLS_LDO_32:
246   case R_386_TLS_LE:
247     return SignExtend64<32>(read32le(Buf));
248   default:
249     return 0;
250   }
251 }
252
253 void X86::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
254   switch (Type) {
255   case R_386_8:
256     // R_386_{PC,}{8,16} are not part of the i386 psABI, but they are
257     // being used for some 16-bit programs such as boot loaders, so
258     // we want to support them.
259     checkUInt<8>(Loc, Val, Type);
260     *Loc = Val;
261     break;
262   case R_386_PC8:
263     checkInt<8>(Loc, Val, Type);
264     *Loc = Val;
265     break;
266   case R_386_16:
267     checkUInt<16>(Loc, Val, Type);
268     write16le(Loc, Val);
269     break;
270   case R_386_PC16:
271     // R_386_PC16 is normally used with 16 bit code. In that situation
272     // the PC is 16 bits, just like the addend. This means that it can
273     // point from any 16 bit address to any other if the possibility
274     // of wrapping is included.
275     // The only restriction we have to check then is that the destination
276     // address fits in 16 bits. That is impossible to do here. The problem is
277     // that we are passed the final value, which already had the
278     // current location subtracted from it.
279     // We just check that Val fits in 17 bits. This misses some cases, but
280     // should have no false positives.
281     checkInt<17>(Loc, Val, Type);
282     write16le(Loc, Val);
283     break;
284   case R_386_32:
285   case R_386_GLOB_DAT:
286   case R_386_GOT32:
287   case R_386_GOT32X:
288   case R_386_GOTOFF:
289   case R_386_GOTPC:
290   case R_386_PC32:
291   case R_386_PLT32:
292   case R_386_RELATIVE:
293   case R_386_TLS_DTPMOD32:
294   case R_386_TLS_DTPOFF32:
295   case R_386_TLS_GD:
296   case R_386_TLS_GOTIE:
297   case R_386_TLS_IE:
298   case R_386_TLS_LDM:
299   case R_386_TLS_LDO_32:
300   case R_386_TLS_LE:
301   case R_386_TLS_LE_32:
302   case R_386_TLS_TPOFF:
303   case R_386_TLS_TPOFF32:
304     checkInt<32>(Loc, Val, Type);
305     write32le(Loc, Val);
306     break;
307   default:
308     error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
309   }
310 }
311
312 void X86::relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const {
313   // Convert
314   //   leal x@tlsgd(, %ebx, 1),
315   //   call __tls_get_addr@plt
316   // to
317   //   movl %gs:0,%eax
318   //   subl $x@ntpoff,%eax
319   const uint8_t Inst[] = {
320       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
321       0x81, 0xe8, 0x00, 0x00, 0x00, 0x00  // subl 0(%ebx), %eax
322   };
323   memcpy(Loc - 3, Inst, sizeof(Inst));
324   write32le(Loc + 5, Val);
325 }
326
327 void X86::relaxTlsGdToIe(uint8_t *Loc, RelType Type, uint64_t Val) const {
328   // Convert
329   //   leal x@tlsgd(, %ebx, 1),
330   //   call __tls_get_addr@plt
331   // to
332   //   movl %gs:0, %eax
333   //   addl x@gotntpoff(%ebx), %eax
334   const uint8_t Inst[] = {
335       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
336       0x03, 0x83, 0x00, 0x00, 0x00, 0x00  // addl 0(%ebx), %eax
337   };
338   memcpy(Loc - 3, Inst, sizeof(Inst));
339   write32le(Loc + 5, Val);
340 }
341
342 // In some conditions, relocations can be optimized to avoid using GOT.
343 // This function does that for Initial Exec to Local Exec case.
344 void X86::relaxTlsIeToLe(uint8_t *Loc, RelType Type, uint64_t Val) const {
345   // Ulrich's document section 6.2 says that @gotntpoff can
346   // be used with MOVL or ADDL instructions.
347   // @indntpoff is similar to @gotntpoff, but for use in
348   // position dependent code.
349   uint8_t Reg = (Loc[-1] >> 3) & 7;
350
351   if (Type == R_386_TLS_IE) {
352     if (Loc[-1] == 0xa1) {
353       // "movl foo@indntpoff,%eax" -> "movl $foo,%eax"
354       // This case is different from the generic case below because
355       // this is a 5 byte instruction while below is 6 bytes.
356       Loc[-1] = 0xb8;
357     } else if (Loc[-2] == 0x8b) {
358       // "movl foo@indntpoff,%reg" -> "movl $foo,%reg"
359       Loc[-2] = 0xc7;
360       Loc[-1] = 0xc0 | Reg;
361     } else {
362       // "addl foo@indntpoff,%reg" -> "addl $foo,%reg"
363       Loc[-2] = 0x81;
364       Loc[-1] = 0xc0 | Reg;
365     }
366   } else {
367     assert(Type == R_386_TLS_GOTIE);
368     if (Loc[-2] == 0x8b) {
369       // "movl foo@gottpoff(%rip),%reg" -> "movl $foo,%reg"
370       Loc[-2] = 0xc7;
371       Loc[-1] = 0xc0 | Reg;
372     } else {
373       // "addl foo@gotntpoff(%rip),%reg" -> "leal foo(%reg),%reg"
374       Loc[-2] = 0x8d;
375       Loc[-1] = 0x80 | (Reg << 3) | Reg;
376     }
377   }
378   write32le(Loc, Val);
379 }
380
381 void X86::relaxTlsLdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const {
382   if (Type == R_386_TLS_LDO_32) {
383     write32le(Loc, Val);
384     return;
385   }
386
387   // Convert
388   //   leal foo(%reg),%eax
389   //   call ___tls_get_addr
390   // to
391   //   movl %gs:0,%eax
392   //   nop
393   //   leal 0(%esi,1),%esi
394   const uint8_t Inst[] = {
395       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
396       0x90,                               // nop
397       0x8d, 0x74, 0x26, 0x00              // leal 0(%esi,1),%esi
398   };
399   memcpy(Loc - 2, Inst, sizeof(Inst));
400 }
401
402 TargetInfo *elf::getX86TargetInfo() {
403   static X86 Target;
404   return &Target;
405 }