blob: e3d725f50a2dd4a7cc12cc361e4505603d740756 [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();
Rui Ueyama67533a22017-10-11 22:49:24 +000029 RelExpr getRelExpr(RelType Type, const SymbolBody &S, const InputFile &File,
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;
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;
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() {
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>
Rui Ueyama67533a22017-10-11 22:49:24 +000076RelExpr X86_64<ELFT>::getRelExpr(RelType Type, const SymbolBody &S,
Rafael Espindola3bab9132017-08-04 18:33:16 +000077 const InputFile &File,
Rui Ueyama21c0a9c2017-06-16 17:32:43 +000078 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:
Rafael Espindola3bab9132017-08-04 18:33:16 +0000113 error(toString(&File) + ": unknown relocation type: " + toString(Type));
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000114 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 {
George Rimarb2051f12017-08-31 10:14:10 +0000128 // See comments in X86::writeGotPlt.
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000129 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
Rui Ueyama67533a22017-10-11 22:49:24 +0000161template <class ELFT> bool X86_64<ELFT>::isPicRel(RelType Type) const {
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000162 return Type != R_X86_64_PC32 && Type != R_X86_64_32 &&
163 Type != R_X86_64_TPOFF32;
164}
165
166template <class ELFT>
Rui Ueyama67533a22017-10-11 22:49:24 +0000167void X86_64<ELFT>::relaxTlsGdToLe(uint8_t *Loc, RelType Type,
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000168 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>
Rui Ueyama67533a22017-10-11 22:49:24 +0000190void X86_64<ELFT>::relaxTlsGdToIe(uint8_t *Loc, RelType Type,
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000191 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>
Rui Ueyama67533a22017-10-11 22:49:24 +0000215void X86_64<ELFT>::relaxTlsIeToLe(uint8_t *Loc, RelType Type,
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000216 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>
Rui Ueyama67533a22017-10-11 22:49:24 +0000258void X86_64<ELFT>::relaxTlsLdToLe(uint8_t *Loc, RelType Type,
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000259 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>
Rui Ueyama67533a22017-10-11 22:49:24 +0000287void X86_64<ELFT>::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000288 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>
Rui Ueyama67533a22017-10-11 22:49:24 +0000331RelExpr X86_64<ELFT>::adjustRelaxExpr(RelType Type, const uint8_t *Data,
Rui Ueyama21c0a9c2017-06-16 17:32:43 +0000332 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}