blob: 10179f57ee9377b61129d5b23a1922b29c7d5d98 [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
10#include "Error.h"
11#include "InputFiles.h"
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000012#include "Symbols.h"
13#include "SyntheticSections.h"
14#include "Target.h"
15#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 {
26template <class ELFT> class X86_64 final : public TargetInfo {
27public:
28 X86_64();
29 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S,
30 const uint8_t *Loc) const override;
31 bool isPicRel(uint32_t Type) const override;
32 void writeGotPltHeader(uint8_t *Buf) const override;
33 void writeGotPlt(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 relaxGot(uint8_t *Loc, uint64_t Val) 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
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() {
Peter Smith113a59e2017-06-26 10:22:17 +000054 GotBaseSymOff = -1;
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000055 CopyRel = R_X86_64_COPY;
56 GotRel = R_X86_64_GLOB_DAT;
57 PltRel = R_X86_64_JUMP_SLOT;
58 RelativeRel = R_X86_64_RELATIVE;
59 IRelativeRel = R_X86_64_IRELATIVE;
60 TlsGotRel = R_X86_64_TPOFF64;
61 TlsModuleIndexRel = R_X86_64_DTPMOD64;
62 TlsOffsetRel = R_X86_64_DTPOFF64;
63 GotEntrySize = 8;
64 GotPltEntrySize = 8;
65 PltEntrySize = 16;
66 PltHeaderSize = 16;
67 TlsGdRelaxSkip = 2;
Rui Ueyama921d43f2017-06-26 19:45:53 +000068 TrapInstr = 0xcccccccc; // 0xcc = INT3
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000069
70 // Align to the large page size (known as a superpage or huge page).
71 // FreeBSD automatically promotes large, superpage-aligned allocations.
72 DefaultImageBase = 0x200000;
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000073}
74
75template <class ELFT>
76RelExpr X86_64<ELFT>::getRelExpr(uint32_t Type, const SymbolBody &S,
77 const uint8_t *Loc) const {
78 switch (Type) {
79 case R_X86_64_8:
80 case R_X86_64_16:
81 case R_X86_64_32:
82 case R_X86_64_32S:
83 case R_X86_64_64:
84 case R_X86_64_DTPOFF32:
85 case R_X86_64_DTPOFF64:
86 return R_ABS;
87 case R_X86_64_TPOFF32:
88 return R_TLS;
89 case R_X86_64_TLSLD:
90 return R_TLSLD_PC;
91 case R_X86_64_TLSGD:
92 return R_TLSGD_PC;
93 case R_X86_64_SIZE32:
94 case R_X86_64_SIZE64:
95 return R_SIZE;
96 case R_X86_64_PLT32:
97 return R_PLT_PC;
98 case R_X86_64_PC32:
99 case R_X86_64_PC64:
100 return R_PC;
101 case R_X86_64_GOT32:
102 case R_X86_64_GOT64:
103 return R_GOT_FROM_END;
104 case R_X86_64_GOTPCREL:
105 case R_X86_64_GOTPCRELX:
106 case R_X86_64_REX_GOTPCRELX:
107 case R_X86_64_GOTTPOFF:
108 return R_GOT_PC;
109 case R_X86_64_NONE:
110 return R_NONE;
111 default:
112 error(toString(S.File) + ": unknown relocation type: " + toString(Type));
113 return R_HINT;
114 }
115}
116
117template <class ELFT> void X86_64<ELFT>::writeGotPltHeader(uint8_t *Buf) const {
118 // The first entry holds the value of _DYNAMIC. It is not clear why that is
119 // required, but it is documented in the psabi and the glibc dynamic linker
120 // seems to use it (note that this is relevant for linking ld.so, not any
121 // other program).
122 write64le(Buf, InX::Dynamic->getVA());
123}
124
125template <class ELFT>
126void X86_64<ELFT>::writeGotPlt(uint8_t *Buf, const SymbolBody &S) const {
127 // See comments in X86TargetInfo::writeGotPlt.
128 write32le(Buf, S.getPltVA() + 6);
129}
130
131template <class ELFT> void X86_64<ELFT>::writePltHeader(uint8_t *Buf) const {
132 const uint8_t PltData[] = {
133 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOTPLT+8(%rip)
134 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOTPLT+16(%rip)
135 0x0f, 0x1f, 0x40, 0x00 // nop
136 };
137 memcpy(Buf, PltData, sizeof(PltData));
138 uint64_t GotPlt = InX::GotPlt->getVA();
139 uint64_t Plt = InX::Plt->getVA();
140 write32le(Buf + 2, GotPlt - Plt + 2); // GOTPLT+8
141 write32le(Buf + 8, GotPlt - Plt + 4); // GOTPLT+16
142}
143
144template <class ELFT>
145void X86_64<ELFT>::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
146 uint64_t PltEntryAddr, int32_t Index,
147 unsigned RelOff) const {
148 const uint8_t Inst[] = {
149 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
150 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
151 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
152 };
153 memcpy(Buf, Inst, sizeof(Inst));
154
155 write32le(Buf + 2, GotPltEntryAddr - PltEntryAddr - 6);
156 write32le(Buf + 7, Index);
157 write32le(Buf + 12, -Index * PltEntrySize - PltHeaderSize - 16);
158}
159
160template <class ELFT> bool X86_64<ELFT>::isPicRel(uint32_t Type) const {
161 return Type != R_X86_64_PC32 && Type != R_X86_64_32 &&
162 Type != R_X86_64_TPOFF32;
163}
164
165template <class ELFT>
166void X86_64<ELFT>::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
167 uint64_t Val) const {
168 // Convert
169 // .byte 0x66
170 // leaq x@tlsgd(%rip), %rdi
171 // .word 0x6666
172 // rex64
173 // call __tls_get_addr@plt
174 // to
175 // mov %fs:0x0,%rax
176 // lea x@tpoff,%rax
177 const uint8_t Inst[] = {
178 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
179 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
180 };
181 memcpy(Loc - 4, Inst, sizeof(Inst));
182
183 // The original code used a pc relative relocation and so we have to
184 // compensate for the -4 in had in the addend.
185 write32le(Loc + 8, Val + 4);
186}
187
188template <class ELFT>
189void X86_64<ELFT>::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
190 uint64_t Val) const {
191 // Convert
192 // .byte 0x66
193 // leaq x@tlsgd(%rip), %rdi
194 // .word 0x6666
195 // rex64
196 // call __tls_get_addr@plt
197 // to
198 // mov %fs:0x0,%rax
199 // addq x@tpoff,%rax
200 const uint8_t Inst[] = {
201 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
202 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
203 };
204 memcpy(Loc - 4, Inst, sizeof(Inst));
205
206 // Both code sequences are PC relatives, but since we are moving the constant
207 // forward by 8 bytes we have to subtract the value by 8.
208 write32le(Loc + 8, Val - 8);
209}
210
211// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
212// R_X86_64_TPOFF32 so that it does not use GOT.
213template <class ELFT>
214void X86_64<ELFT>::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
215 uint64_t Val) const {
216 uint8_t *Inst = Loc - 3;
217 uint8_t Reg = Loc[-1] >> 3;
218 uint8_t *RegSlot = Loc - 1;
219
220 // Note that ADD with RSP or R12 is converted to ADD instead of LEA
221 // because LEA with these registers needs 4 bytes to encode and thus
222 // wouldn't fit the space.
223
224 if (memcmp(Inst, "\x48\x03\x25", 3) == 0) {
225 // "addq foo@gottpoff(%rip),%rsp" -> "addq $foo,%rsp"
226 memcpy(Inst, "\x48\x81\xc4", 3);
227 } else if (memcmp(Inst, "\x4c\x03\x25", 3) == 0) {
228 // "addq foo@gottpoff(%rip),%r12" -> "addq $foo,%r12"
229 memcpy(Inst, "\x49\x81\xc4", 3);
230 } else if (memcmp(Inst, "\x4c\x03", 2) == 0) {
231 // "addq foo@gottpoff(%rip),%r[8-15]" -> "leaq foo(%r[8-15]),%r[8-15]"
232 memcpy(Inst, "\x4d\x8d", 2);
233 *RegSlot = 0x80 | (Reg << 3) | Reg;
234 } else if (memcmp(Inst, "\x48\x03", 2) == 0) {
235 // "addq foo@gottpoff(%rip),%reg -> "leaq foo(%reg),%reg"
236 memcpy(Inst, "\x48\x8d", 2);
237 *RegSlot = 0x80 | (Reg << 3) | Reg;
238 } else if (memcmp(Inst, "\x4c\x8b", 2) == 0) {
239 // "movq foo@gottpoff(%rip),%r[8-15]" -> "movq $foo,%r[8-15]"
240 memcpy(Inst, "\x49\xc7", 2);
241 *RegSlot = 0xc0 | Reg;
242 } else if (memcmp(Inst, "\x48\x8b", 2) == 0) {
243 // "movq foo@gottpoff(%rip),%reg" -> "movq $foo,%reg"
244 memcpy(Inst, "\x48\xc7", 2);
245 *RegSlot = 0xc0 | Reg;
246 } else {
247 error(getErrorLocation(Loc - 3) +
248 "R_X86_64_GOTTPOFF must be used in MOVQ or ADDQ instructions only");
249 }
250
251 // The original code used a PC relative relocation.
252 // Need to compensate for the -4 it had in the addend.
253 write32le(Loc, Val + 4);
254}
255
256template <class ELFT>
257void X86_64<ELFT>::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
258 uint64_t Val) const {
259 // Convert
260 // leaq bar@tlsld(%rip), %rdi
261 // callq __tls_get_addr@PLT
262 // leaq bar@dtpoff(%rax), %rcx
263 // to
264 // .word 0x6666
265 // .byte 0x66
266 // mov %fs:0,%rax
267 // leaq bar@tpoff(%rax), %rcx
268 if (Type == R_X86_64_DTPOFF64) {
269 write64le(Loc, Val);
270 return;
271 }
272 if (Type == R_X86_64_DTPOFF32) {
273 write32le(Loc, Val);
274 return;
275 }
276
277 const uint8_t Inst[] = {
278 0x66, 0x66, // .word 0x6666
279 0x66, // .byte 0x66
280 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
281 };
282 memcpy(Loc - 3, Inst, sizeof(Inst));
283}
284
285template <class ELFT>
286void X86_64<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
287 uint64_t Val) const {
288 switch (Type) {
289 case R_X86_64_8:
290 checkUInt<8>(Loc, Val, Type);
291 *Loc = Val;
292 break;
293 case R_X86_64_16:
294 checkUInt<16>(Loc, Val, Type);
295 write16le(Loc, Val);
296 break;
297 case R_X86_64_32:
298 checkUInt<32>(Loc, Val, Type);
299 write32le(Loc, Val);
300 break;
301 case R_X86_64_32S:
302 case R_X86_64_TPOFF32:
303 case R_X86_64_GOT32:
304 case R_X86_64_GOTPCREL:
305 case R_X86_64_GOTPCRELX:
306 case R_X86_64_REX_GOTPCRELX:
307 case R_X86_64_PC32:
308 case R_X86_64_GOTTPOFF:
309 case R_X86_64_PLT32:
310 case R_X86_64_TLSGD:
311 case R_X86_64_TLSLD:
312 case R_X86_64_DTPOFF32:
313 case R_X86_64_SIZE32:
314 checkInt<32>(Loc, Val, Type);
315 write32le(Loc, Val);
316 break;
317 case R_X86_64_64:
318 case R_X86_64_DTPOFF64:
319 case R_X86_64_GLOB_DAT:
320 case R_X86_64_PC64:
321 case R_X86_64_SIZE64:
322 case R_X86_64_GOT64:
323 write64le(Loc, Val);
324 break;
325 default:
326 llvm_unreachable("unexpected relocation");
327 }
328}
329
330template <class ELFT>
331RelExpr X86_64<ELFT>::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
332 RelExpr RelExpr) const {
333 if (Type != R_X86_64_GOTPCRELX && Type != R_X86_64_REX_GOTPCRELX)
334 return RelExpr;
335 const uint8_t Op = Data[-2];
336 const uint8_t ModRm = Data[-1];
337
338 // FIXME: When PIC is disabled and foo is defined locally in the
339 // lower 32 bit address space, memory operand in mov can be converted into
340 // immediate operand. Otherwise, mov must be changed to lea. We support only
341 // latter relaxation at this moment.
342 if (Op == 0x8b)
343 return R_RELAX_GOT_PC;
344
345 // Relax call and jmp.
346 if (Op == 0xff && (ModRm == 0x15 || ModRm == 0x25))
347 return R_RELAX_GOT_PC;
348
349 // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor.
350 // If PIC then no relaxation is available.
351 // We also don't relax test/binop instructions without REX byte,
352 // they are 32bit operations and not common to have.
353 assert(Type == R_X86_64_REX_GOTPCRELX);
354 return Config->Pic ? RelExpr : R_RELAX_GOT_PC_NOPIC;
355}
356
357// A subset of relaxations can only be applied for no-PIC. This method
358// handles such relaxations. Instructions encoding information was taken from:
359// "Intel 64 and IA-32 Architectures Software Developer's Manual V2"
360// (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
361// 64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
362template <class ELFT>
363void X86_64<ELFT>::relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
364 uint8_t ModRm) const {
365 const uint8_t Rex = Loc[-3];
366 // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg".
367 if (Op == 0x85) {
368 // See "TEST-Logical Compare" (4-428 Vol. 2B),
369 // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension).
370
371 // ModR/M byte has form XX YYY ZZZ, where
372 // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1).
373 // XX has different meanings:
374 // 00: The operand's memory address is in reg1.
375 // 01: The operand's memory address is reg1 + a byte-sized displacement.
376 // 10: The operand's memory address is reg1 + a word-sized displacement.
377 // 11: The operand is reg1 itself.
378 // If an instruction requires only one operand, the unused reg2 field
379 // holds extra opcode bits rather than a register code
380 // 0xC0 == 11 000 000 binary.
381 // 0x38 == 00 111 000 binary.
382 // We transfer reg2 to reg1 here as operand.
383 // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3).
384 Loc[-1] = 0xc0 | (ModRm & 0x38) >> 3; // ModR/M byte.
385
386 // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32
387 // See "TEST-Logical Compare" (4-428 Vol. 2B).
388 Loc[-2] = 0xf7;
389
390 // Move R bit to the B bit in REX byte.
391 // REX byte is encoded as 0100WRXB, where
392 // 0100 is 4bit fixed pattern.
393 // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the
394 // default operand size is used (which is 32-bit for most but not all
395 // instructions).
396 // REX.R This 1-bit value is an extension to the MODRM.reg field.
397 // REX.X This 1-bit value is an extension to the SIB.index field.
398 // REX.B This 1-bit value is an extension to the MODRM.rm field or the
399 // SIB.base field.
400 // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A).
401 Loc[-3] = (Rex & ~0x4) | (Rex & 0x4) >> 2;
402 write32le(Loc, Val);
403 return;
404 }
405
406 // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub
407 // or xor operations.
408
409 // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg".
410 // Logic is close to one for test instruction above, but we also
411 // write opcode extension here, see below for details.
412 Loc[-1] = 0xc0 | (ModRm & 0x38) >> 3 | (Op & 0x3c); // ModR/M byte.
413
414 // Primary opcode is 0x81, opcode extension is one of:
415 // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB,
416 // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP.
417 // This value was wrote to MODRM.reg in a line above.
418 // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15),
419 // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for
420 // descriptions about each operation.
421 Loc[-2] = 0x81;
422 Loc[-3] = (Rex & ~0x4) | (Rex & 0x4) >> 2;
423 write32le(Loc, Val);
424}
425
426template <class ELFT>
427void X86_64<ELFT>::relaxGot(uint8_t *Loc, uint64_t Val) const {
428 const uint8_t Op = Loc[-2];
429 const uint8_t ModRm = Loc[-1];
430
431 // Convert "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg".
432 if (Op == 0x8b) {
433 Loc[-2] = 0x8d;
434 write32le(Loc, Val);
435 return;
436 }
437
438 if (Op != 0xff) {
439 // We are relaxing a rip relative to an absolute, so compensate
440 // for the old -4 addend.
441 assert(!Config->Pic);
442 relaxGotNoPic(Loc, Val + 4, Op, ModRm);
443 return;
444 }
445
446 // Convert call/jmp instructions.
447 if (ModRm == 0x15) {
448 // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call foo".
449 // Instead we convert to "addr32 call foo" where addr32 is an instruction
450 // prefix. That makes result expression to be a single instruction.
451 Loc[-2] = 0x67; // addr32 prefix
452 Loc[-1] = 0xe8; // call
453 write32le(Loc, Val);
454 return;
455 }
456
457 // Convert "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop".
458 // jmp doesn't return, so it is fine to use nop here, it is just a stub.
459 assert(ModRm == 0x25);
460 Loc[-2] = 0xe9; // jmp
461 Loc[3] = 0x90; // nop
462 write32le(Loc - 1, Val + 1);
463}
464
Rui Ueyamae145bc22017-06-16 20:15:03 +0000465TargetInfo *elf::getX32TargetInfo() {
466 static X86_64<ELF32LE> Target;
467 return &Target;
468}
469
470TargetInfo *elf::getX86_64TargetInfo() {
471 static X86_64<ELF64LE> Target;
472 return &Target;
473}