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