blob: 512b1ffdf38dda053173badfc86780d3f211caa8 [file] [log] [blame]
Rui Ueyama21c0a9c2017-06-16 17:32:43 +00001//===- X86_64.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
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000010#include "InputFiles.h"
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000011#include "Symbols.h"
12#include "SyntheticSections.h"
13#include "Target.h"
Bob Haarmanb8a59c82017-10-25 22:28:38 +000014#include "lld/Common/ErrorHandler.h"
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000015#include "llvm/Object/ELF.h"
16#include "llvm/Support/Endian.h"
17
18using namespace llvm;
19using namespace llvm::object;
20using namespace llvm::support::endian;
21using namespace llvm::ELF;
22using namespace lld;
23using namespace lld::elf;
24
25namespace {
Chandler Carruthc58f2162018-01-22 22:05:25 +000026template <class ELFT> class X86_64 : public TargetInfo {
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000027public:
28 X86_64();
Rui Ueyamaf52496e2017-11-03 21:21:47 +000029 RelExpr getRelExpr(RelType Type, const Symbol &S,
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000030 const uint8_t *Loc) const override;
Rui Ueyama67533a22017-10-11 22:49:24 +000031 bool isPicRel(RelType Type) const override;
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000032 void writeGotPltHeader(uint8_t *Buf) const override;
Rui Ueyamaf52496e2017-11-03 21:21:47 +000033 void writeGotPlt(uint8_t *Buf, const Symbol &S) const override;
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000034 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;
Rui Ueyama67533a22017-10-11 22:49:24 +000037 void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000038
Rui Ueyama67533a22017-10-11 22:49:24 +000039 RelExpr adjustRelaxExpr(RelType Type, const uint8_t *Data,
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000040 RelExpr Expr) const override;
41 void relaxGot(uint8_t *Loc, uint64_t Val) const override;
Rui Ueyama67533a22017-10-11 22:49:24 +000042 void relaxTlsGdToIe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
43 void relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
44 void relaxTlsIeToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
45 void relaxTlsLdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000046
47private:
48 void relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
49 uint8_t ModRm) const;
50};
51} // namespace
52
53template <class ELFT> X86_64<ELFT>::X86_64() {
54 CopyRel = R_X86_64_COPY;
55 GotRel = R_X86_64_GLOB_DAT;
56 PltRel = R_X86_64_JUMP_SLOT;
57 RelativeRel = R_X86_64_RELATIVE;
58 IRelativeRel = R_X86_64_IRELATIVE;
59 TlsGotRel = R_X86_64_TPOFF64;
60 TlsModuleIndexRel = R_X86_64_DTPMOD64;
61 TlsOffsetRel = R_X86_64_DTPOFF64;
62 GotEntrySize = 8;
63 GotPltEntrySize = 8;
64 PltEntrySize = 16;
65 PltHeaderSize = 16;
66 TlsGdRelaxSkip = 2;
Rui Ueyama921d43f2017-06-26 19:45:53 +000067 TrapInstr = 0xcccccccc; // 0xcc = INT3
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000068
69 // Align to the large page size (known as a superpage or huge page).
70 // FreeBSD automatically promotes large, superpage-aligned allocations.
71 DefaultImageBase = 0x200000;
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000072}
73
74template <class ELFT>
Rui Ueyamaf52496e2017-11-03 21:21:47 +000075RelExpr X86_64<ELFT>::getRelExpr(RelType Type, const Symbol &S,
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000076 const uint8_t *Loc) const {
77 switch (Type) {
78 case R_X86_64_8:
79 case R_X86_64_16:
80 case R_X86_64_32:
81 case R_X86_64_32S:
82 case R_X86_64_64:
83 case R_X86_64_DTPOFF32:
84 case R_X86_64_DTPOFF64:
85 return R_ABS;
86 case R_X86_64_TPOFF32:
87 return R_TLS;
88 case R_X86_64_TLSLD:
89 return R_TLSLD_PC;
90 case R_X86_64_TLSGD:
91 return R_TLSGD_PC;
92 case R_X86_64_SIZE32:
93 case R_X86_64_SIZE64:
94 return R_SIZE;
95 case R_X86_64_PLT32:
96 return R_PLT_PC;
97 case R_X86_64_PC32:
98 case R_X86_64_PC64:
99 return R_PC;
100 case R_X86_64_GOT32:
101 case R_X86_64_GOT64:
102 return R_GOT_FROM_END;
103 case R_X86_64_GOTPCREL:
104 case R_X86_64_GOTPCRELX:
105 case R_X86_64_REX_GOTPCRELX:
106 case R_X86_64_GOTTPOFF:
107 return R_GOT_PC;
108 case R_X86_64_NONE:
109 return R_NONE;
110 default:
Rui Ueyamabe855292017-10-12 03:14:06 +0000111 return R_INVALID;
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000112 }
113}
114
115template <class ELFT> void X86_64<ELFT>::writeGotPltHeader(uint8_t *Buf) const {
116 // The first entry holds the value of _DYNAMIC. It is not clear why that is
117 // required, but it is documented in the psabi and the glibc dynamic linker
118 // seems to use it (note that this is relevant for linking ld.so, not any
119 // other program).
120 write64le(Buf, InX::Dynamic->getVA());
121}
122
123template <class ELFT>
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000124void X86_64<ELFT>::writeGotPlt(uint8_t *Buf, const Symbol &S) const {
George Rimarb2051f12017-08-31 10:14:10 +0000125 // See comments in X86::writeGotPlt.
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000126 write32le(Buf, S.getPltVA() + 6);
127}
128
129template <class ELFT> void X86_64<ELFT>::writePltHeader(uint8_t *Buf) const {
130 const uint8_t PltData[] = {
Rui Ueyama17a30772017-12-27 06:54:18 +0000131 0xff, 0x35, 0, 0, 0, 0, // pushq GOTPLT+8(%rip)
132 0xff, 0x25, 0, 0, 0, 0, // jmp *GOTPLT+16(%rip)
133 0x0f, 0x1f, 0x40, 0x00, // nop
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000134 };
135 memcpy(Buf, PltData, sizeof(PltData));
136 uint64_t GotPlt = InX::GotPlt->getVA();
137 uint64_t Plt = InX::Plt->getVA();
138 write32le(Buf + 2, GotPlt - Plt + 2); // GOTPLT+8
139 write32le(Buf + 8, GotPlt - Plt + 4); // GOTPLT+16
140}
141
142template <class ELFT>
143void X86_64<ELFT>::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
144 uint64_t PltEntryAddr, int32_t Index,
145 unsigned RelOff) const {
146 const uint8_t Inst[] = {
Rui Ueyama17a30772017-12-27 06:54:18 +0000147 0xff, 0x25, 0, 0, 0, 0, // jmpq *got(%rip)
148 0x68, 0, 0, 0, 0, // pushq <relocation index>
149 0xe9, 0, 0, 0, 0, // jmpq plt[0]
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000150 };
151 memcpy(Buf, Inst, sizeof(Inst));
152
153 write32le(Buf + 2, GotPltEntryAddr - PltEntryAddr - 6);
154 write32le(Buf + 7, Index);
Rafael Espindola74acdfa2018-03-14 17:41:34 +0000155 write32le(Buf + 12, -getPltEntryOffset(Index) - 16);
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000156}
157
Rui Ueyama67533a22017-10-11 22:49:24 +0000158template <class ELFT> bool X86_64<ELFT>::isPicRel(RelType Type) const {
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000159 return Type != R_X86_64_PC32 && Type != R_X86_64_32 &&
160 Type != R_X86_64_TPOFF32;
161}
162
163template <class ELFT>
Rui Ueyama67533a22017-10-11 22:49:24 +0000164void X86_64<ELFT>::relaxTlsGdToLe(uint8_t *Loc, RelType Type,
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000165 uint64_t Val) const {
166 // Convert
167 // .byte 0x66
168 // leaq x@tlsgd(%rip), %rdi
169 // .word 0x6666
170 // rex64
171 // call __tls_get_addr@plt
172 // to
173 // mov %fs:0x0,%rax
174 // lea x@tpoff,%rax
175 const uint8_t Inst[] = {
176 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
Rui Ueyama17a30772017-12-27 06:54:18 +0000177 0x48, 0x8d, 0x80, 0, 0, 0, 0, // lea x@tpoff,%rax
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000178 };
179 memcpy(Loc - 4, Inst, sizeof(Inst));
180
181 // The original code used a pc relative relocation and so we have to
182 // compensate for the -4 in had in the addend.
183 write32le(Loc + 8, Val + 4);
184}
185
186template <class ELFT>
Rui Ueyama67533a22017-10-11 22:49:24 +0000187void X86_64<ELFT>::relaxTlsGdToIe(uint8_t *Loc, RelType Type,
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000188 uint64_t Val) const {
189 // Convert
190 // .byte 0x66
191 // leaq x@tlsgd(%rip), %rdi
192 // .word 0x6666
193 // rex64
194 // call __tls_get_addr@plt
195 // to
196 // mov %fs:0x0,%rax
197 // addq x@tpoff,%rax
198 const uint8_t Inst[] = {
199 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
Rui Ueyama17a30772017-12-27 06:54:18 +0000200 0x48, 0x03, 0x05, 0, 0, 0, 0, // addq x@tpoff,%rax
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000201 };
202 memcpy(Loc - 4, Inst, sizeof(Inst));
203
204 // Both code sequences are PC relatives, but since we are moving the constant
205 // forward by 8 bytes we have to subtract the value by 8.
206 write32le(Loc + 8, Val - 8);
207}
208
209// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
210// R_X86_64_TPOFF32 so that it does not use GOT.
211template <class ELFT>
Rui Ueyama67533a22017-10-11 22:49:24 +0000212void X86_64<ELFT>::relaxTlsIeToLe(uint8_t *Loc, RelType Type,
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000213 uint64_t Val) const {
214 uint8_t *Inst = Loc - 3;
215 uint8_t Reg = Loc[-1] >> 3;
216 uint8_t *RegSlot = Loc - 1;
217
218 // Note that ADD with RSP or R12 is converted to ADD instead of LEA
219 // because LEA with these registers needs 4 bytes to encode and thus
220 // wouldn't fit the space.
221
222 if (memcmp(Inst, "\x48\x03\x25", 3) == 0) {
223 // "addq foo@gottpoff(%rip),%rsp" -> "addq $foo,%rsp"
224 memcpy(Inst, "\x48\x81\xc4", 3);
225 } else if (memcmp(Inst, "\x4c\x03\x25", 3) == 0) {
226 // "addq foo@gottpoff(%rip),%r12" -> "addq $foo,%r12"
227 memcpy(Inst, "\x49\x81\xc4", 3);
228 } else if (memcmp(Inst, "\x4c\x03", 2) == 0) {
229 // "addq foo@gottpoff(%rip),%r[8-15]" -> "leaq foo(%r[8-15]),%r[8-15]"
230 memcpy(Inst, "\x4d\x8d", 2);
231 *RegSlot = 0x80 | (Reg << 3) | Reg;
232 } else if (memcmp(Inst, "\x48\x03", 2) == 0) {
233 // "addq foo@gottpoff(%rip),%reg -> "leaq foo(%reg),%reg"
234 memcpy(Inst, "\x48\x8d", 2);
235 *RegSlot = 0x80 | (Reg << 3) | Reg;
236 } else if (memcmp(Inst, "\x4c\x8b", 2) == 0) {
237 // "movq foo@gottpoff(%rip),%r[8-15]" -> "movq $foo,%r[8-15]"
238 memcpy(Inst, "\x49\xc7", 2);
239 *RegSlot = 0xc0 | Reg;
240 } else if (memcmp(Inst, "\x48\x8b", 2) == 0) {
241 // "movq foo@gottpoff(%rip),%reg" -> "movq $foo,%reg"
242 memcpy(Inst, "\x48\xc7", 2);
243 *RegSlot = 0xc0 | Reg;
244 } else {
245 error(getErrorLocation(Loc - 3) +
246 "R_X86_64_GOTTPOFF must be used in MOVQ or ADDQ instructions only");
247 }
248
249 // The original code used a PC relative relocation.
250 // Need to compensate for the -4 it had in the addend.
251 write32le(Loc, Val + 4);
252}
253
254template <class ELFT>
Rui Ueyama67533a22017-10-11 22:49:24 +0000255void X86_64<ELFT>::relaxTlsLdToLe(uint8_t *Loc, RelType Type,
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000256 uint64_t Val) const {
257 // Convert
258 // leaq bar@tlsld(%rip), %rdi
259 // callq __tls_get_addr@PLT
260 // leaq bar@dtpoff(%rax), %rcx
261 // to
262 // .word 0x6666
263 // .byte 0x66
264 // mov %fs:0,%rax
265 // leaq bar@tpoff(%rax), %rcx
266 if (Type == R_X86_64_DTPOFF64) {
267 write64le(Loc, Val);
268 return;
269 }
270 if (Type == R_X86_64_DTPOFF32) {
271 write32le(Loc, Val);
272 return;
273 }
274
275 const uint8_t Inst[] = {
Rui Ueyama17a30772017-12-27 06:54:18 +0000276 0x66, 0x66, // .word 0x6666
277 0x66, // .byte 0x66
278 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0,%rax
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000279 };
280 memcpy(Loc - 3, Inst, sizeof(Inst));
281}
282
283template <class ELFT>
Rui Ueyama67533a22017-10-11 22:49:24 +0000284void X86_64<ELFT>::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000285 switch (Type) {
286 case R_X86_64_8:
287 checkUInt<8>(Loc, Val, Type);
288 *Loc = Val;
289 break;
290 case R_X86_64_16:
291 checkUInt<16>(Loc, Val, Type);
292 write16le(Loc, Val);
293 break;
294 case R_X86_64_32:
295 checkUInt<32>(Loc, Val, Type);
296 write32le(Loc, Val);
297 break;
298 case R_X86_64_32S:
299 case R_X86_64_TPOFF32:
300 case R_X86_64_GOT32:
301 case R_X86_64_GOTPCREL:
302 case R_X86_64_GOTPCRELX:
303 case R_X86_64_REX_GOTPCRELX:
304 case R_X86_64_PC32:
305 case R_X86_64_GOTTPOFF:
306 case R_X86_64_PLT32:
307 case R_X86_64_TLSGD:
308 case R_X86_64_TLSLD:
309 case R_X86_64_DTPOFF32:
310 case R_X86_64_SIZE32:
311 checkInt<32>(Loc, Val, Type);
312 write32le(Loc, Val);
313 break;
314 case R_X86_64_64:
315 case R_X86_64_DTPOFF64:
316 case R_X86_64_GLOB_DAT:
317 case R_X86_64_PC64:
318 case R_X86_64_SIZE64:
319 case R_X86_64_GOT64:
320 write64le(Loc, Val);
321 break;
322 default:
Rui Ueyamabe855292017-10-12 03:14:06 +0000323 error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000324 }
325}
326
327template <class ELFT>
Rui Ueyama67533a22017-10-11 22:49:24 +0000328RelExpr X86_64<ELFT>::adjustRelaxExpr(RelType Type, const uint8_t *Data,
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000329 RelExpr RelExpr) const {
330 if (Type != R_X86_64_GOTPCRELX && Type != R_X86_64_REX_GOTPCRELX)
331 return RelExpr;
332 const uint8_t Op = Data[-2];
333 const uint8_t ModRm = Data[-1];
334
335 // FIXME: When PIC is disabled and foo is defined locally in the
336 // lower 32 bit address space, memory operand in mov can be converted into
337 // immediate operand. Otherwise, mov must be changed to lea. We support only
338 // latter relaxation at this moment.
339 if (Op == 0x8b)
340 return R_RELAX_GOT_PC;
341
342 // Relax call and jmp.
343 if (Op == 0xff && (ModRm == 0x15 || ModRm == 0x25))
344 return R_RELAX_GOT_PC;
345
346 // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor.
347 // If PIC then no relaxation is available.
348 // We also don't relax test/binop instructions without REX byte,
349 // they are 32bit operations and not common to have.
350 assert(Type == R_X86_64_REX_GOTPCRELX);
351 return Config->Pic ? RelExpr : R_RELAX_GOT_PC_NOPIC;
352}
353
354// A subset of relaxations can only be applied for no-PIC. This method
355// handles such relaxations. Instructions encoding information was taken from:
356// "Intel 64 and IA-32 Architectures Software Developer's Manual V2"
357// (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
358// 64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
359template <class ELFT>
360void X86_64<ELFT>::relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
361 uint8_t ModRm) const {
362 const uint8_t Rex = Loc[-3];
363 // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg".
364 if (Op == 0x85) {
365 // See "TEST-Logical Compare" (4-428 Vol. 2B),
366 // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension).
367
368 // ModR/M byte has form XX YYY ZZZ, where
369 // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1).
370 // XX has different meanings:
371 // 00: The operand's memory address is in reg1.
372 // 01: The operand's memory address is reg1 + a byte-sized displacement.
373 // 10: The operand's memory address is reg1 + a word-sized displacement.
374 // 11: The operand is reg1 itself.
375 // If an instruction requires only one operand, the unused reg2 field
376 // holds extra opcode bits rather than a register code
377 // 0xC0 == 11 000 000 binary.
378 // 0x38 == 00 111 000 binary.
379 // We transfer reg2 to reg1 here as operand.
380 // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3).
381 Loc[-1] = 0xc0 | (ModRm & 0x38) >> 3; // ModR/M byte.
382
383 // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32
384 // See "TEST-Logical Compare" (4-428 Vol. 2B).
385 Loc[-2] = 0xf7;
386
387 // Move R bit to the B bit in REX byte.
388 // REX byte is encoded as 0100WRXB, where
389 // 0100 is 4bit fixed pattern.
390 // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the
391 // default operand size is used (which is 32-bit for most but not all
392 // instructions).
393 // REX.R This 1-bit value is an extension to the MODRM.reg field.
394 // REX.X This 1-bit value is an extension to the SIB.index field.
395 // REX.B This 1-bit value is an extension to the MODRM.rm field or the
396 // SIB.base field.
397 // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A).
398 Loc[-3] = (Rex & ~0x4) | (Rex & 0x4) >> 2;
399 write32le(Loc, Val);
400 return;
401 }
402
403 // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub
404 // or xor operations.
405
406 // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg".
407 // Logic is close to one for test instruction above, but we also
408 // write opcode extension here, see below for details.
409 Loc[-1] = 0xc0 | (ModRm & 0x38) >> 3 | (Op & 0x3c); // ModR/M byte.
410
411 // Primary opcode is 0x81, opcode extension is one of:
412 // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB,
413 // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP.
414 // This value was wrote to MODRM.reg in a line above.
415 // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15),
416 // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for
417 // descriptions about each operation.
418 Loc[-2] = 0x81;
419 Loc[-3] = (Rex & ~0x4) | (Rex & 0x4) >> 2;
420 write32le(Loc, Val);
421}
422
423template <class ELFT>
424void X86_64<ELFT>::relaxGot(uint8_t *Loc, uint64_t Val) const {
425 const uint8_t Op = Loc[-2];
426 const uint8_t ModRm = Loc[-1];
427
428 // Convert "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg".
429 if (Op == 0x8b) {
430 Loc[-2] = 0x8d;
431 write32le(Loc, Val);
432 return;
433 }
434
435 if (Op != 0xff) {
436 // We are relaxing a rip relative to an absolute, so compensate
437 // for the old -4 addend.
438 assert(!Config->Pic);
439 relaxGotNoPic(Loc, Val + 4, Op, ModRm);
440 return;
441 }
442
443 // Convert call/jmp instructions.
444 if (ModRm == 0x15) {
445 // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call foo".
446 // Instead we convert to "addr32 call foo" where addr32 is an instruction
447 // prefix. That makes result expression to be a single instruction.
448 Loc[-2] = 0x67; // addr32 prefix
449 Loc[-1] = 0xe8; // call
450 write32le(Loc, Val);
451 return;
452 }
453
454 // Convert "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop".
455 // jmp doesn't return, so it is fine to use nop here, it is just a stub.
456 assert(ModRm == 0x25);
457 Loc[-2] = 0xe9; // jmp
458 Loc[3] = 0x90; // nop
459 write32le(Loc - 1, Val + 1);
460}
461
Chandler Carruthc58f2162018-01-22 22:05:25 +0000462namespace {
463template <class ELFT> class Retpoline : public X86_64<ELFT> {
464public:
465 Retpoline();
466 void writeGotPlt(uint8_t *Buf, const Symbol &S) const override;
467 void writePltHeader(uint8_t *Buf) const override;
468 void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
469 int32_t Index, unsigned RelOff) const override;
470};
471
472template <class ELFT> class RetpolineZNow : public X86_64<ELFT> {
473public:
474 RetpolineZNow();
475 void writeGotPlt(uint8_t *Buf, const Symbol &S) const override {}
476 void writePltHeader(uint8_t *Buf) const override;
477 void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
478 int32_t Index, unsigned RelOff) const override;
479};
480} // namespace
481
482template <class ELFT> Retpoline<ELFT>::Retpoline() {
483 TargetInfo::PltHeaderSize = 48;
484 TargetInfo::PltEntrySize = 32;
Rui Ueyamae145bc22017-06-16 20:15:03 +0000485}
486
Chandler Carruthc58f2162018-01-22 22:05:25 +0000487template <class ELFT>
488void Retpoline<ELFT>::writeGotPlt(uint8_t *Buf, const Symbol &S) const {
489 write32le(Buf, S.getPltVA() + 17);
Rui Ueyamae145bc22017-06-16 20:15:03 +0000490}
Chandler Carruthc58f2162018-01-22 22:05:25 +0000491
492template <class ELFT> void Retpoline<ELFT>::writePltHeader(uint8_t *Buf) const {
493 const uint8_t Insn[] = {
494 0xff, 0x35, 0, 0, 0, 0, // 0: pushq GOTPLT+8(%rip)
495 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // 6: mov GOTPLT+16(%rip), %r11
496 0xe8, 0x0e, 0x00, 0x00, 0x00, // d: callq next
497 0xf3, 0x90, // 12: loop: pause
498 0x0f, 0xae, 0xe8, // 14: lfence
499 0xeb, 0xf9, // 17: jmp loop
500 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 19: int3; .align 16
501 0x4c, 0x89, 0x1c, 0x24, // 20: next: mov %r11, (%rsp)
502 0xc3, // 24: ret
503 };
504 memcpy(Buf, Insn, sizeof(Insn));
505
506 uint64_t GotPlt = InX::GotPlt->getVA();
507 uint64_t Plt = InX::Plt->getVA();
508 write32le(Buf + 2, GotPlt - Plt - 6 + 8);
509 write32le(Buf + 9, GotPlt - Plt - 13 + 16);
510}
511
512template <class ELFT>
513void Retpoline<ELFT>::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
514 uint64_t PltEntryAddr, int32_t Index,
515 unsigned RelOff) const {
516 const uint8_t Insn[] = {
517 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // 0: mov foo@GOTPLT(%rip), %r11
518 0xe8, 0, 0, 0, 0, // 7: callq plt+0x20
519 0xe9, 0, 0, 0, 0, // c: jmp plt+0x12
520 0x68, 0, 0, 0, 0, // 11: pushq <relocation index>
521 0xe9, 0, 0, 0, 0, // 16: jmp plt+0
522 };
523 memcpy(Buf, Insn, sizeof(Insn));
524
Rafael Espindola74acdfa2018-03-14 17:41:34 +0000525 uint64_t Off = TargetInfo::getPltEntryOffset(Index);
Chandler Carruthc58f2162018-01-22 22:05:25 +0000526
527 write32le(Buf + 3, GotPltEntryAddr - PltEntryAddr - 7);
528 write32le(Buf + 8, -Off - 12 + 32);
529 write32le(Buf + 13, -Off - 17 + 18);
530 write32le(Buf + 18, Index);
531 write32le(Buf + 23, -Off - 27);
532}
533
534template <class ELFT> RetpolineZNow<ELFT>::RetpolineZNow() {
535 TargetInfo::PltHeaderSize = 32;
536 TargetInfo::PltEntrySize = 16;
537}
538
539template <class ELFT>
540void RetpolineZNow<ELFT>::writePltHeader(uint8_t *Buf) const {
541 const uint8_t Insn[] = {
542 0xe8, 0x0b, 0x00, 0x00, 0x00, // 0: call next
543 0xf3, 0x90, // 5: loop: pause
544 0x0f, 0xae, 0xe8, // 7: lfence
545 0xeb, 0xf9, // a: jmp loop
546 0xcc, 0xcc, 0xcc, 0xcc, // c: int3; .align 16
547 0x4c, 0x89, 0x1c, 0x24, // 10: next: mov %r11, (%rsp)
548 0xc3, // 14: ret
549 };
550 memcpy(Buf, Insn, sizeof(Insn));
551}
552
553template <class ELFT>
554void RetpolineZNow<ELFT>::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
555 uint64_t PltEntryAddr, int32_t Index,
556 unsigned RelOff) const {
557 const uint8_t Insn[] = {
558 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // mov foo@GOTPLT(%rip), %r11
559 0xe9, 0, 0, 0, 0, // jmp plt+0
560 };
561 memcpy(Buf, Insn, sizeof(Insn));
562
563 write32le(Buf + 3, GotPltEntryAddr - PltEntryAddr - 7);
Rafael Espindola74acdfa2018-03-14 17:41:34 +0000564 write32le(Buf + 8, -TargetInfo::getPltEntryOffset(Index) - 12);
Chandler Carruthc58f2162018-01-22 22:05:25 +0000565}
566
567template <class ELFT> TargetInfo *getTargetInfo() {
568 if (Config->ZRetpolineplt) {
569 if (Config->ZNow) {
570 static RetpolineZNow<ELFT> T;
571 return &T;
572 }
573 static Retpoline<ELFT> T;
574 return &T;
575 }
576
577 static X86_64<ELFT> T;
578 return &T;
579}
580
581TargetInfo *elf::getX32TargetInfo() { return getTargetInfo<ELF32LE>(); }
582TargetInfo *elf::getX86_64TargetInfo() { return getTargetInfo<ELF64LE>(); }