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