Import ELF Tool Chain snapshot at revision 3475
[freebsd-src.git] / ld / amd64.c
blob324aa6c804124f6f0a5ff9c2f991c4d2c5517473
1 /*-
2 * Copyright (c) 2012,2013 Kai Wang
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
27 #include "ld.h"
28 #include "ld_arch.h"
29 #include "ld_dynamic.h"
30 #include "ld_input.h"
31 #include "ld_layout.h"
32 #include "ld_output.h"
33 #include "ld_reloc.h"
34 #include "ld_symbols.h"
35 #include "ld_utils.h"
36 #include "amd64.h"
38 ELFTC_VCSID("$Id: amd64.c 3419 2016-02-19 20:07:15Z emaste $");
40 static void _create_plt_reloc(struct ld *ld, struct ld_symbol *lsb,
41 uint64_t offset);
42 static void _create_got_reloc(struct ld *ld, struct ld_symbol *lsb,
43 uint64_t type, uint64_t offset);
44 static void _create_copy_reloc(struct ld *ld, struct ld_symbol *lsb);
45 static void _create_dynamic_reloc(struct ld *ld, struct ld_input_section *is,
46 struct ld_symbol *lsb, uint64_t type, uint64_t offset, int64_t addend);
47 static void _scan_reloc(struct ld *ld, struct ld_input_section *is,
48 struct ld_reloc_entry *lre);
49 static struct ld_input_section *_find_and_create_got_section(struct ld *ld,
50 int create);
51 static struct ld_input_section *_find_and_create_gotplt_section(struct ld *ld,
52 int create);
53 static struct ld_input_section *_find_and_create_plt_section(struct ld *ld,
54 int create);
55 static void _finalize_got_and_plt(struct ld *ld);
56 static uint64_t _get_max_page_size(struct ld *ld);
57 static uint64_t _get_common_page_size(struct ld *ld);
58 static void _adjust_reloc(struct ld *ld, struct ld_input_section *is,
59 struct ld_reloc_entry *lre, struct ld_symbol *lsb, uint8_t *buf);
60 static void _process_reloc(struct ld *ld, struct ld_input_section *is,
61 struct ld_reloc_entry *lre, struct ld_symbol *lsb, uint8_t *buf);
62 static void _reserve_got_entry(struct ld *ld, struct ld_symbol *lsb, int num);
63 static void _reserve_gotplt_entry(struct ld *ld, struct ld_symbol *lsb);
64 static void _reserve_plt_entry(struct ld *ld, struct ld_symbol *lsb);
65 static int _is_absolute_reloc(uint64_t r);
66 static void _warn_pic(struct ld *ld, struct ld_reloc_entry *lre);
67 static void _create_tls_gd_reloc(struct ld *ld, struct ld_symbol *lsb);
68 static void _create_tls_ld_reloc(struct ld *ld, struct ld_symbol *lsb);
69 static void _create_tls_ie_reloc(struct ld *ld, struct ld_symbol *lsb);
70 static enum ld_tls_relax _tls_check_relax(struct ld *ld,
71 struct ld_reloc_entry *lre);
72 static uint64_t _got_offset(struct ld *ld, struct ld_symbol *lsb);
73 static int _tls_verify_gd(uint8_t *buf, uint64_t off);
74 static int _tls_verify_ld(uint8_t *buf, uint64_t off);
75 static void _tls_relax_gd_to_ie(struct ld *ld, struct ld_state *ls,
76 struct ld_output *lo,struct ld_reloc_entry *lre, uint64_t p, uint64_t g,
77 uint8_t *buf);
78 static void _tls_relax_gd_to_le(struct ld *ld, struct ld_state *ls,
79 struct ld_output *lo, struct ld_reloc_entry *lre, struct ld_symbol *lsb,
80 uint8_t *buf);
81 static void _tls_relax_ld_to_le(struct ld *ld, struct ld_state *ls,
82 struct ld_reloc_entry *lre, uint8_t *buf);
83 static void _tls_relax_ie_to_le(struct ld *ld, struct ld_output *lo,
84 struct ld_reloc_entry *lre, struct ld_symbol *lsb, uint8_t *buf);
85 static int32_t _tls_dtpoff(struct ld_output *lo, struct ld_symbol *lsb);
86 static int32_t _tls_tpoff(struct ld_output *lo, struct ld_symbol *lsb);
88 static uint64_t
89 _get_max_page_size(struct ld *ld)
92 (void) ld;
93 return (0x200000);
96 static uint64_t
97 _get_common_page_size(struct ld *ld)
100 (void) ld;
101 return (0x1000);
104 static int
105 _is_absolute_reloc(uint64_t r)
108 if (r == R_X86_64_64 || r == R_X86_64_32 || r == R_X86_64_32S ||
109 r == R_X86_64_16 || r == R_X86_64_8)
110 return (1);
112 return (0);
115 static int
116 _is_relative_reloc(uint64_t r)
119 if (r == R_X86_64_RELATIVE)
120 return (1);
122 return (0);
125 static void
126 _warn_pic(struct ld *ld, struct ld_reloc_entry *lre)
128 struct ld_symbol *lsb;
130 lsb = lre->lre_sym;
132 if (lsb->lsb_bind != STB_LOCAL)
133 ld_warn(ld, "relocation %s against `%s' can not be used"
134 " by runtime linker; recompile with -fPIC",
135 elftc_reloc_type_str(EM_X86_64,
136 lre->lre_type), lsb->lsb_name);
137 else
138 ld_warn(ld, "relocation %s can not be used by runtime linker;"
139 " recompile with -fPIC", elftc_reloc_type_str(EM_X86_64,
140 lre->lre_type));
143 static struct ld_input_section *
144 _find_and_create_got_section(struct ld *ld, int create)
146 struct ld_input_section *is;
148 /* Check if the GOT section is already created. */
149 is = ld_input_find_internal_section(ld, ".got");
150 if (is != NULL)
151 return (is);
153 if (create) {
154 is = ld_input_add_internal_section(ld, ".got");
155 is->is_entsize = 8;
156 is->is_align = 8;
157 is->is_type = SHT_PROGBITS;
158 is->is_flags = SHF_ALLOC | SHF_WRITE;
161 return (is);
164 static struct ld_input_section *
165 _find_and_create_gotplt_section(struct ld *ld, int create)
167 struct ld_input_section *is;
169 /* Check if the GOT (for PLT) section is already created. */
170 is = ld_input_find_internal_section(ld, ".got.plt");
171 if (is != NULL)
172 return (is);
174 if (create) {
175 is = ld_input_add_internal_section(ld, ".got.plt");
176 is->is_entsize = 8;
177 is->is_align = 8;
178 is->is_type = SHT_PROGBITS;
179 is->is_flags = SHF_ALLOC | SHF_WRITE;
181 /* Reserve space for the initial entries. */
182 (void) ld_input_reserve_ibuf(is, 3);
184 /* Create _GLOBAL_OFFSET_TABLE_ symbol. */
185 ld_symbols_add_internal(ld, "_GLOBAL_OFFSET_TABLE_", 0, 0,
186 is->is_index, STB_LOCAL, STT_OBJECT, STV_HIDDEN, is, NULL);
189 return (is);
192 static struct ld_input_section *
193 _find_and_create_plt_section(struct ld *ld, int create)
195 struct ld_input_section *is;
197 /* Check if the PLT section is already created. */
198 is = ld_input_find_internal_section(ld, ".plt");
199 if (is != NULL)
200 return (is);
202 if (create) {
203 is = ld_input_add_internal_section(ld, ".plt");
204 is->is_entsize = 16;
205 is->is_align = 4;
206 is->is_type = SHT_PROGBITS;
207 is->is_flags = SHF_ALLOC | SHF_EXECINSTR;
209 /* Reserve space for the initial entry. */
210 (void) ld_input_reserve_ibuf(is, 1);
213 return (is);
216 static void
217 _reserve_got_entry(struct ld *ld, struct ld_symbol *lsb, int num)
219 struct ld_input_section *is;
221 is = _find_and_create_got_section(ld, 1);
223 /* Check if the entry already has a GOT entry. */
224 if (lsb->lsb_got)
225 return;
227 /* Reserve GOT entries. */
228 lsb->lsb_got_off = ld_input_reserve_ibuf(is, num);
229 lsb->lsb_got = 1;
232 static void
233 _reserve_gotplt_entry(struct ld *ld, struct ld_symbol *lsb)
235 struct ld_input_section *is;
237 is = _find_and_create_gotplt_section(ld, 1);
239 /* Reserve a GOT entry for PLT. */
240 (void) ld_input_reserve_ibuf(is, 1);
243 * Record a R_X86_64_JUMP_SLOT entry for this symbol. Note that
244 * we don't need to record the offset (relative to the GOT section)
245 * here, since the PLT relocations will be sorted later and we
246 * will generate GOT section according to the new order.
248 _create_plt_reloc(ld, lsb, 0);
251 static void
252 _reserve_plt_entry(struct ld *ld, struct ld_symbol *lsb)
254 struct ld_input_section *is;
256 is = _find_and_create_plt_section(ld, 1);
258 (void) ld_input_reserve_ibuf(is, 1);
259 lsb->lsb_plt = 1;
262 static void
263 _create_plt_reloc(struct ld *ld, struct ld_symbol *lsb, uint64_t offset)
266 ld_reloc_create_entry(ld, ".rela.plt", NULL, R_X86_64_JUMP_SLOT,
267 lsb, offset, 0);
269 lsb->lsb_dynrel = 1;
272 static void
273 _create_got_reloc(struct ld *ld, struct ld_symbol *lsb, uint64_t type,
274 uint64_t offset)
276 struct ld_input_section *tis;
278 tis = _find_and_create_got_section(ld, 0);
279 assert(tis != NULL);
281 ld_reloc_create_entry(ld, ".rela.got", tis, type, lsb, offset, 0);
283 if (type != R_X86_64_RELATIVE)
284 lsb->lsb_dynrel = 1;
287 static void
288 _create_copy_reloc(struct ld *ld, struct ld_symbol *lsb)
290 struct ld_input_section *tis;
292 ld_dynamic_reserve_dynbss_entry(ld, lsb);
294 tis = ld_input_find_internal_section(ld, ".dynbss");
295 assert(tis != NULL);
297 ld_reloc_create_entry(ld, ".rela.bss", tis, R_X86_64_COPY, lsb,
298 lsb->lsb_value, 0);
300 lsb->lsb_dynrel = 1;
303 static void
304 _create_dynamic_reloc(struct ld *ld, struct ld_input_section *is,
305 struct ld_symbol *lsb, uint64_t type, uint64_t offset, int64_t addend)
308 if (lsb->lsb_bind == STB_LOCAL) {
309 if (is->is_flags & SHF_WRITE)
310 ld_reloc_create_entry(ld, ".rela.data.rel.local",
311 is, type, lsb, offset, addend);
312 else
313 ld_reloc_create_entry(ld, ".rela.data.rel.ro.local",
314 is, type, lsb, offset, addend);
315 } else {
316 if (is->is_flags & SHF_WRITE)
317 ld_reloc_create_entry(ld, ".rela.data.rel",
318 is, type, lsb, offset, addend);
319 else
320 ld_reloc_create_entry(ld, ".rela.data.rel.ro",
321 is, type, lsb, offset, addend);
324 if (type != R_X86_64_RELATIVE)
325 lsb->lsb_dynrel = 1;
328 static void
329 _finalize_reloc(struct ld *ld, struct ld_input_section *tis,
330 struct ld_reloc_entry *lre)
332 struct ld_symbol *lsb;
334 (void) ld;
335 (void) tis;
337 lsb = ld_symbols_ref(lre->lre_sym);
339 switch (lre->lre_type) {
340 case R_X86_64_RELATIVE:
342 * Update the addend stored in the original relocation to
343 * point to the new location, by adding the updated symbol
344 * value.
346 lre->lre_addend += lsb->lsb_value;
348 /* R_X86_64_RELATIVE should not associate with a symbol. */
349 lre->lre_sym = NULL;
350 break;
352 case R_X86_64_DTPMOD64:
354 * Relocation R_X86_64_DTPMOD64 generated for local dynamic
355 * TLS model should not assoicate with a symbol.
357 if (lre->lre_type == R_X86_64_DTPMOD64 &&
358 lsb->lsb_tls_ld)
359 lre->lre_sym = NULL;
360 break;
362 default:
363 break;
367 static void
368 _finalize_got_and_plt(struct ld *ld)
370 struct ld_output *lo;
371 struct ld_input_section *got_is, *rela_got_is, *plt_is, *rela_plt_is;
372 struct ld_output_section *got_os, *plt_os, *rela_plt_os;
373 struct ld_reloc_entry *lre;
374 struct ld_symbol *lsb;
375 char dynamic_symbol[] = "_DYNAMIC";
376 uint8_t *got, *plt;
377 uint64_t u64;
378 int32_t s32, pltgot, gotpcrel;
379 int i, j;
381 lo = ld->ld_output;
382 assert(lo != NULL);
385 * Intiailze all .got section entries to zero.
387 got_is = _find_and_create_got_section(ld, 0);
388 if (got_is != NULL)
389 memset(got_is->is_ibuf, 0, got_is->is_size);
392 * Search for GOT relocations that requires filling in symbol
393 * value.
395 rela_got_is = ld_input_find_internal_section(ld, ".rela.got");
396 if (rela_got_is != NULL && rela_got_is->is_reloc != NULL) {
397 STAILQ_FOREACH(lre, rela_got_is->is_reloc, lre_next) {
398 if (lre->lre_type == R_X86_64_RELATIVE) {
399 lsb = lre->lre_sym;
400 got = (uint8_t *) got_is->is_ibuf +
401 lsb->lsb_got_off;
402 WRITE_64(got, lsb->lsb_value);
408 * Find the .plt section. The buffers should have been allocated
409 * at this point.
411 plt_is = _find_and_create_plt_section(ld, 0);
412 if (plt_is == NULL)
413 return;
414 plt_os = plt_is->is_output;
415 plt = plt_is->is_ibuf;
416 assert(plt != NULL);
419 * Find the .got.plt and .rela.plt section. If the .plt section
420 * exists, the .got.plt and .rela.plt section should exist too.
422 got_is = _find_and_create_gotplt_section(ld, 0);
423 assert(got_is != NULL);
424 got_os = got_is->is_output;
425 lo->lo_gotplt = got_os;
426 got = got_is->is_ibuf;
427 assert(got != NULL);
428 rela_plt_is = ld_input_find_internal_section(ld, ".rela.plt");
429 assert(rela_plt_is != NULL);
430 rela_plt_os = rela_plt_is->is_output;
431 lo->lo_rel_plt = rela_plt_os;
433 /* Point sh_info field of the .rela.plt to .plt section. */
434 rela_plt_os->os_info = plt_os;
436 /* Fill in the value of symbol _DYNAMIC in the first GOT entry. */
437 ld_symbols_get_value(ld, dynamic_symbol, &u64);
438 WRITE_64(got, u64);
439 got += 8;
441 /* Reserve the second and the third entry for the dynamic linker. */
442 memset(got, 0, 16);
443 got += 16;
446 * Write the initial PLT entry.
449 /* Calculate the relative offset from PLT to GOT. */
450 pltgot = got_os->os_addr - plt_os->os_addr;
453 * Push the second GOT entry to the stack for the dynamic
454 * linker. (PUSH reg/memXX [RIP+disp32]) (6 bytes for push)
456 WRITE_8(plt, 0xff);
457 WRITE_8(plt + 1, 0x35);
458 s32 = pltgot - 6 + 8;
459 WRITE_32(plt + 2, s32);
460 plt += 6;
463 * Jump to the address in the third GOT entry (call into
464 * the dynamic linker). (JMP reg/memXX [RIP+disp32])
465 * (6 bytes for jmp)
467 WRITE_8(plt, 0xff);
468 WRITE_8(plt + 1, 0x25);
469 s32 = pltgot - 12 + 16;
470 WRITE_32(plt + 2, s32);
471 plt += 6;
473 /* Padding: 4-byte nop. (NOP [rAx+disp8]) */
474 WRITE_8(plt, 0x0f);
475 WRITE_8(plt + 1, 0x1f);
476 WRITE_8(plt + 2, 0x40);
477 WRITE_8(plt + 3, 0x0);
478 plt += 4;
481 * Walk through the sorted PLT relocations in the output section
482 * and fill in each GOT and PLT entries.
484 i = 3;
485 j = 0;
486 STAILQ_FOREACH(lre, rela_plt_is->is_reloc, lre_next) {
487 lsb = ld_symbols_ref(lre->lre_sym);
490 * Set symbol's PLT offset to the address of this PLT entry.
491 * The PLT offset is used in relocation processing later.
493 lsb->lsb_plt_off = plt_os->os_addr + (i - 2) * 16;
496 * Update the offset for the R_X86_64_JUMP_SLOT relocation
497 * entry, pointing to the corresponding GOT entry.
499 lre->lre_offset = got_os->os_addr + i * 8;
502 * Calculate the IP-relative offset to the GOT entry for
503 * this function. (6 bytes for jmp)
505 gotpcrel = pltgot + i * 8 - (i - 2) * 16 - 6;
508 * PLT: Jump to the address in the GOT entry for this
509 * function. (JMP reg/memXX [RIP+disp32])
511 WRITE_8(plt, 0xff);
512 WRITE_8(plt + 1, 0x25);
513 WRITE_32(plt + 2, gotpcrel);
514 plt += 6;
517 * PLT: Symbol is not resolved, push the relocation index to
518 * the stack. (PUSH imm32)
520 WRITE_8(plt, 0x68);
521 WRITE_32(plt + 1, j);
522 plt += 5;
525 * PLT: Jump to the first PLT entry, eventually call the
526 * dynamic linker. (JMP rel32off)
528 WRITE_8(plt, 0xe9);
529 s32 = - (i - 1) * 16;
530 WRITE_32(plt + 1, s32);
531 plt += 5;
534 * GOT: Write the GOT entry for this function, pointing to
535 * the push op.
537 u64 = plt_os->os_addr + (i - 2) * 16 + 6;
538 WRITE_64(got, u64);
540 /* Increase relocation entry index. */
541 j++;
543 /* Move to next GOT entry. */
544 got += 8;
545 i++;
548 assert(got == (uint8_t *) got_is->is_ibuf + got_is->is_size);
549 assert(plt == (uint8_t *) plt_is->is_ibuf + plt_is->is_size);
552 static void
553 _scan_reloc(struct ld *ld, struct ld_input_section *is,
554 struct ld_reloc_entry *lre)
556 struct ld_symbol *lsb;
557 enum ld_tls_relax tr;
559 lsb = ld_symbols_ref(lre->lre_sym);
562 * TODO: We do not yet support "Large Models" and relevant
563 * relocation types R_X86_64_GOT64, R_X86_64_GOTPCREL64,
564 * R_X86_64_GOTPC64, R_X86_64_GOTPLT64 and R_X86_64_PLTOFF64.
565 * Refer to AMD64 ELF ABI for details.
568 switch (lre->lre_type) {
569 case R_X86_64_NONE:
570 break;
572 case R_X86_64_64:
573 case R_X86_64_32:
574 case R_X86_64_32S:
575 case R_X86_64_16:
576 case R_X86_64_8:
579 * For a local symbol, if the linker output a PIE or DSO,
580 * we should generate a R_X86_64_RELATIVE reloc for
581 * R_X86_64_64. We don't know how to generate dynamic reloc
582 * for other reloc types since R_X86_64_RELATIVE is 64 bits.
583 * We can not use them directly either because FreeBSD rtld(1)
584 * (and probably glibc) doesn't accept absolute address
585 * reloction other than R_X86_64_64.
587 if (lsb->lsb_bind == STB_LOCAL) {
588 if (ld->ld_pie || ld->ld_dso) {
589 if (lre->lre_type == R_X86_64_64)
590 _create_dynamic_reloc(ld, is, lsb,
591 R_X86_64_RELATIVE, lre->lre_offset,
592 lre->lre_addend);
593 else
594 _warn_pic(ld, lre);
596 break;
600 * For a global symbol, we probably need to generate PLT entry
601 * and/or a dynamic relocation.
603 * Note here, normally the compiler will generate a PC-relative
604 * relocation for function calls. However, if the code retrieve
605 * the address of a function and call it indirectly, assembler
606 * will generate absolute relocation instead. That's why we
607 * should check if we need to create a PLT entry here. Also, if
608 * we're going to create the PLT entry, we should also set the
609 * symbol value to the address of PLT entry just in case the
610 * function address is used to compare with other function
611 * addresses. (If PLT address is used, function will have
612 * unified address in the main executable and DSOs)
614 if (ld_reloc_require_plt(ld, lre)) {
615 if (!lsb->lsb_plt) {
616 _reserve_gotplt_entry(ld, lsb);
617 _reserve_plt_entry(ld, lsb);
620 * Note here even if we have generated PLT for this
621 * function before, we still need to set this flag.
622 * It's possible that we first see the relative
623 * relocation then this absolute relocation, in
624 * other words, the same function can be called in
625 * different ways.
627 lsb->lsb_func_addr = 1;
630 if (ld_reloc_require_copy_reloc(ld, lre) &&
631 !lsb->lsb_copy_reloc)
632 _create_copy_reloc(ld, lsb);
633 else if (ld_reloc_require_dynamic_reloc(ld, lre)) {
634 /* We only support R_X86_64_64. (See above) */
635 if (lre->lre_type != R_X86_64_64) {
636 _warn_pic(ld, lre);
637 break;
640 * Check if we can relax R_X86_64_64 to
641 * R_X86_64_RELATIVE instead.
643 if (ld_reloc_relative_relax(ld, lre))
644 _create_dynamic_reloc(ld, is, lsb,
645 R_X86_64_RELATIVE, lre->lre_offset,
646 lre->lre_addend);
647 else
648 _create_dynamic_reloc(ld, is, lsb,
649 R_X86_64_64, lre->lre_offset,
650 lre->lre_addend);
653 break;
655 case R_X86_64_PLT32:
657 * In some cases we don't really need to generate a PLT
658 * entry, then a R_X86_64_PLT32 relocation can be relaxed
659 * to a R_X86_64_PC32 relocation.
662 if (lsb->lsb_bind == STB_LOCAL ||
663 !ld_reloc_require_plt(ld, lre)) {
664 lre->lre_type = R_X86_64_PC32;
665 break;
669 * If linker outputs an normal executable and the symbol is
670 * defined but is not defined inside a DSO, we can generate
671 * a R_X86_64_PC32 relocation instead.
673 if (ld->ld_exec && lsb->lsb_shndx != SHN_UNDEF &&
674 (lsb->lsb_input == NULL ||
675 lsb->lsb_input->li_type != LIT_DSO)) {
676 lre->lre_type = R_X86_64_PC32;
677 break;
680 /* Create an PLT entry otherwise. */
681 if (!lsb->lsb_plt) {
682 _reserve_gotplt_entry(ld, lsb);
683 _reserve_plt_entry(ld, lsb);
685 break;
687 case R_X86_64_PC64:
688 case R_X86_64_PC32:
689 case R_X86_64_PC16:
690 case R_X86_64_PC8:
693 * When these relocations apply to a global symbol, we should
694 * check if we need to generate PLT entry and/or a dynamic
695 * relocation.
697 if (lsb->lsb_bind != STB_LOCAL) {
698 if (ld_reloc_require_plt(ld, lre) && !lsb->lsb_plt) {
699 _reserve_gotplt_entry(ld, lsb);
700 _reserve_plt_entry(ld, lsb);
703 if (ld_reloc_require_copy_reloc(ld, lre) &&
704 !lsb->lsb_copy_reloc)
705 _create_copy_reloc(ld, lsb);
706 else if (ld_reloc_require_dynamic_reloc(ld, lre)) {
708 * We can not generate dynamic relocation for
709 * these PC-relative relocation since they
710 * are probably not supported by the runtime
711 * linkers.
713 * Note: FreeBSD rtld(1) does support
714 * R_X86_64_PC32.
716 _warn_pic(ld, lre);
719 break;
721 case R_X86_64_GOTOFF64:
722 case R_X86_64_GOTPC32:
724 * These relocation types use GOT address as a base address
725 * and instruct the linker to build a GOT.
727 (void) _find_and_create_got_section(ld, 1);
728 break;
730 case R_X86_64_GOT32:
731 case R_X86_64_GOTPCREL:
733 * These relocation types instruct the linker to build a
734 * GOT and generate a GOT entry.
736 if (!lsb->lsb_got) {
737 _reserve_got_entry(ld, lsb, 1);
739 * TODO: For now we always create a R_X86_64_GLOB_DAT
740 * relocation for a GOT entry. There are cases that
741 * the symbol's address is known at link time and
742 * the GOT entry value can be filled in by the program
743 * linker instead.
745 if (ld_reloc_require_glob_dat(ld, lre))
746 _create_got_reloc(ld, lsb, R_X86_64_GLOB_DAT,
747 lsb->lsb_got_off);
748 else
749 _create_got_reloc(ld, lsb, R_X86_64_RELATIVE,
750 lsb->lsb_got_off);
752 break;
754 case R_X86_64_TLSGD: /* Global Dynamic */
755 tr = _tls_check_relax(ld, lre);
756 switch (tr) {
757 case TLS_RELAX_NONE:
758 _create_tls_gd_reloc(ld, lsb);
759 break;
760 case TLS_RELAX_INIT_EXEC:
761 _create_tls_ie_reloc(ld, lsb);
762 break;
763 case TLS_RELAX_LOCAL_EXEC:
764 break;
765 default:
766 ld_fatal(ld, "Internal: invalid TLS relaxation %d",
767 tr);
768 break;
770 break;
772 case R_X86_64_TLSLD: /* Local Dynamic */
773 tr = _tls_check_relax(ld, lre);
774 if (tr == TLS_RELAX_NONE)
775 _create_tls_ld_reloc(ld, lsb);
776 else if (tr != TLS_RELAX_LOCAL_EXEC)
777 ld_fatal(ld, "Internal: invalid TLS relaxation %d",
778 tr);
779 break;
781 case R_X86_64_DTPOFF32:
782 /* Handled by R_X86_64_TLSLD case. */
783 break;
785 case R_X86_64_GOTTPOFF: /* Initial Exec */
786 tr = _tls_check_relax(ld, lre);
787 if (tr == TLS_RELAX_NONE)
788 _create_tls_ie_reloc(ld, lsb);
789 else if (tr != TLS_RELAX_LOCAL_EXEC)
790 ld_fatal(ld, "Internal: invalid TLS relaxation %d",
791 tr);
792 break;
794 case R_X86_64_TPOFF32: /* Local Exec */
795 /* No further relaxation possible. */
796 break;
798 case R_X86_64_GOTPC32_TLSDESC:
799 case R_X86_64_TLSDESC_CALL:
800 /* TODO. */
801 break;
803 default:
804 ld_warn(ld, "can not handle relocation %ju",
805 lre->lre_type);
806 break;
810 static uint64_t
811 _got_offset(struct ld *ld, struct ld_symbol *lsb)
813 struct ld_output_section *os;
815 assert(lsb->lsb_got);
817 if (ld->ld_got == NULL) {
818 ld->ld_got = _find_and_create_got_section(ld, 0);
819 assert(ld->ld_got != NULL);
822 os = ld->ld_got->is_output;
824 return (os->os_addr + ld->ld_got->is_reloff + lsb->lsb_got_off);
827 static void
828 _process_reloc(struct ld *ld, struct ld_input_section *is,
829 struct ld_reloc_entry *lre, struct ld_symbol *lsb, uint8_t *buf)
831 struct ld_state *ls;
832 struct ld_output *lo;
833 uint64_t u64, s, l, p, g;
834 int64_t s64;
835 uint32_t u32;
836 int32_t s32;
837 enum ld_tls_relax tr;
839 ls = &ld->ld_state;
841 lo = ld->ld_output;
842 assert(lo != NULL);
844 l = lsb->lsb_plt_off;
845 p = lre->lre_offset + is->is_output->os_addr + is->is_reloff;
846 s = lsb->lsb_value;
848 switch (lre->lre_type) {
849 case R_X86_64_NONE:
850 break;
852 case R_X86_64_64:
853 WRITE_64(buf + lre->lre_offset, s + lre->lre_addend);
854 break;
856 case R_X86_64_PC32:
857 if (lsb->lsb_plt)
858 s32 = l + lre->lre_addend - p;
859 else
860 s32 = s + lre->lre_addend - p;
861 WRITE_32(buf + lre->lre_offset, s32);
862 break;
864 case R_X86_64_PLT32:
865 if (!ls->ls_ignore_next_plt) {
866 s32 = l + lre->lre_addend - p;
867 WRITE_32(buf + lre->lre_offset, s32);
868 } else
869 ls->ls_ignore_next_plt = 0;
870 break;
872 case R_X86_64_GOTPCREL:
873 g = _got_offset(ld, lsb);
874 s32 = g + lre->lre_addend - p;
875 WRITE_32(buf + lre->lre_offset, s32);
876 break;
878 case R_X86_64_32:
879 u64 = s + lre->lre_addend;
880 u32 = u64 & 0xffffffff;
881 if (u64 != u32)
882 ld_fatal(ld, "R_X86_64_32 relocation failed");
883 WRITE_32(buf + lre->lre_offset, u32);
884 break;
886 case R_X86_64_32S:
887 s64 = s + lre->lre_addend;
888 s32 = s64 & 0xffffffff;
889 if (s64 != s32)
890 ld_fatal(ld, "R_X86_64_32S relocation failed");
891 WRITE_32(buf + lre->lre_offset, s32);
892 break;
894 case R_X86_64_TLSGD: /* Global Dynamic */
895 tr = _tls_check_relax(ld, lre);
896 switch (tr) {
897 case TLS_RELAX_NONE:
898 g = _got_offset(ld, lsb);
899 s32 = g + lre->lre_addend - p;
900 WRITE_32(buf + lre->lre_offset, s32);
901 break;
902 case TLS_RELAX_INIT_EXEC:
903 g = _got_offset(ld, lsb);
904 _tls_relax_gd_to_ie(ld, ls, lo, lre, p, g, buf);
905 break;
906 case TLS_RELAX_LOCAL_EXEC:
907 _tls_relax_gd_to_le(ld, ls, lo, lre, lsb, buf);
908 break;
909 default:
910 ld_fatal(ld, "Internal: invalid TLS relaxation %d",
911 tr);
912 break;
914 break;
916 case R_X86_64_TLSLD: /* Local Dynamic */
917 tr = _tls_check_relax(ld, lre);
918 switch (tr) {
919 case TLS_RELAX_NONE:
920 g = _got_offset(ld, lsb);
921 s32 = g + lre->lre_addend - p;
922 WRITE_32(buf + lre->lre_offset, s32);
923 break;
924 case TLS_RELAX_LOCAL_EXEC:
925 _tls_relax_ld_to_le(ld, ls, lre, buf);
926 break;
927 default:
928 ld_fatal(ld, "Internal: invalid TLS relaxation %d",
929 tr);
930 break;
932 break;
934 case R_X86_64_DTPOFF32: /* Local Dynamic (offset) */
935 tr = _tls_check_relax(ld, lre);
936 switch (tr) {
937 case TLS_RELAX_NONE:
938 s32 = _tls_dtpoff(lo, lsb);
939 WRITE_32(buf + lre->lre_offset, s32);
940 break;
941 case TLS_RELAX_LOCAL_EXEC:
942 s32 = _tls_tpoff(lo, lsb);
943 WRITE_32(buf + lre->lre_offset, s32);
944 break;
945 default:
946 ld_fatal(ld, "Internal: invalid TLS relaxation %d",
947 tr);
948 break;
950 break;
952 case R_X86_64_GOTTPOFF: /* Initial Exec */
953 tr = _tls_check_relax(ld, lre);
954 switch (tr) {
955 case TLS_RELAX_NONE:
956 g = _got_offset(ld, lsb);
957 s32 = g + lre->lre_addend - p;
958 WRITE_32(buf + lre->lre_offset, s32);
959 break;
960 case TLS_RELAX_LOCAL_EXEC:
961 _tls_relax_ie_to_le(ld, lo, lre, lsb, buf);
962 break;
963 default:
964 ld_fatal(ld, "Internal: invalid TLS relaxation %d",
965 tr);
966 break;
968 break;
970 case R_X86_64_TPOFF32: /* Local Exec */
971 s32 = _tls_tpoff(lo, lsb);
972 WRITE_32(buf + lre->lre_offset, s32);
973 break;
975 default:
976 ld_warn(ld, "Relocation %s not supported",
977 elftc_reloc_type_str(EM_X86_64, lre->lre_type));
978 break;
982 static void
983 _adjust_reloc(struct ld *ld, struct ld_input_section *is,
984 struct ld_reloc_entry *lre, struct ld_symbol *lsb, uint8_t *buf)
986 struct ld_input_section *_is;
988 (void) ld;
989 (void) is;
990 (void) buf;
992 /* Only need to adjust relocation against section symbols. */
993 if (lsb->lsb_type != STT_SECTION)
994 return;
996 if ((_is = lsb->lsb_is) == NULL || _is->is_output == NULL)
997 return;
1000 * Update the relocation addend to point to the new location
1001 * in the output object.
1003 lre->lre_addend += _is->is_reloff;
1006 static enum ld_tls_relax
1007 _tls_check_relax(struct ld *ld, struct ld_reloc_entry *lre)
1009 struct ld_symbol *lsb;
1011 lsb = ld_symbols_ref(lre->lre_sym);
1014 * If the linker is performing -static linking, we should always
1015 * use the Local Exec model.
1017 if (!ld->ld_dynamic_link)
1018 return (TLS_RELAX_LOCAL_EXEC);
1021 * If the linker is creating a DSO, we can not perform any TLS
1022 * relaxation.
1024 if (ld->ld_dso)
1025 return (TLS_RELAX_NONE);
1028 * The linker is creating an executable, if the symbol is
1029 * defined in a regular object, we can use the Local Exec model.
1031 if (lsb->lsb_shndx != SHN_UNDEF && ld_symbols_in_regular(lsb))
1032 return (TLS_RELAX_LOCAL_EXEC);
1035 * If the TLS model is Global Dynamic, we can relax it to Initial
1036 * Exec model since the linker is creating an executable.
1038 if (lre->lre_type == R_X86_64_TLSGD)
1039 return (TLS_RELAX_INIT_EXEC);
1041 /* For all the other cases, no relaxation can be done. */
1042 return (TLS_RELAX_NONE);
1045 static int32_t
1046 _tls_tpoff(struct ld_output *lo, struct ld_symbol *lsb)
1048 int32_t tls_off;
1050 tls_off = -roundup(lo->lo_tls_size, lo->lo_tls_align);
1052 return (tls_off + (lsb->lsb_value - lo->lo_tls_addr));
1055 static int32_t
1056 _tls_dtpoff(struct ld_output *lo, struct ld_symbol *lsb)
1059 return (lsb->lsb_value - lo->lo_tls_addr);
1062 static int
1063 _tls_verify_gd(uint8_t *buf, uint64_t off)
1066 * Global Dynamic model:
1068 * 0x00 .byte 0x66
1069 * 0x01 leaq x@tlsgd(%rip), %rdi
1070 * 0x08 .word 0x6666
1071 * 0x0a rex64
1072 * 0x0b call _tls_get_addr@plt
1074 uint8_t gd[] = "\x66\x48\x8d\x3d\x00\x00\x00\x00"
1075 "\x66\x66\x48\xe8\x00\x00\x00\x00";
1077 if (memcmp(buf + off, gd, sizeof(gd) - 1) == 0)
1078 return (1);
1080 return (0);
1083 static int
1084 _tls_verify_ld(uint8_t *buf, uint64_t off)
1087 * Local Dynamic model:
1089 * 0x00 leaq x@tlsld(%rip), %rdi
1090 * 0x07 call _tls_get_addr@plt
1092 uint8_t ld[] = "\x48\x8d\x3d\x00\x00\x00\x00"
1093 "\xe8\x00\x00\x00\x00";
1095 if (memcmp(buf + off, ld, sizeof(ld) - 1) == 0)
1096 return (1);
1098 return (0);
1101 static void
1102 _tls_relax_gd_to_ie(struct ld *ld, struct ld_state *ls, struct ld_output *lo,
1103 struct ld_reloc_entry *lre, uint64_t p, uint64_t g, uint8_t *buf)
1106 * Initial Exec model:
1108 * 0x00 movq %fs:0, %rax
1109 * 0x09 addq x@gottpoff(%rip), %rax
1111 uint8_t ie[] = "\x64\x48\x8b\x04\x25\x00\x00\x00\x00"
1112 "\x48\x03\x05\x00\x00\x00\x00";
1113 int32_t s32;
1115 assert(lre->lre_type == R_X86_64_TLSGD);
1117 if (!_tls_verify_gd(buf, lre->lre_offset - 4))
1118 ld_warn(ld, "unrecognized TLS global dynamic model code");
1120 /* Rewrite Global Dynamic to Initial Exec model. */
1121 memcpy((uint8_t *) buf + lre->lre_offset - 4, ie, sizeof(ie) - 1);
1124 * R_X86_64_TLSGD relocation is applied at gd[4]. After it's relaxed
1125 * to Initial Exec model, the resulting R_X86_64_GOTTPOFF relocation
1126 * should be applied at ie[12]. The addend should remain the same
1127 * since instruction "leaq x@tlsgd(%rip), %rdi" and
1128 * "addq x@gottpoff(%rip), %rax" has the same length. `p' is moved
1129 * 8 bytes forward.
1131 s32 = g + lre->lre_addend - (p + 8);
1132 WRITE_32(buf + lre->lre_offset + 8, s32);
1134 /* Ignore the next R_X86_64_PLT32 relocation for _tls_get_addr. */
1135 ls->ls_ignore_next_plt = 1;
1138 static void
1139 _tls_relax_gd_to_le(struct ld *ld, struct ld_state *ls, struct ld_output *lo,
1140 struct ld_reloc_entry *lre, struct ld_symbol *lsb, uint8_t *buf)
1143 * Local Exec model:
1145 * 0x00 movq %fs:0, %rax
1146 * 0x09 leaq x@tpoff(%rax), %rax
1148 uint8_t le[] = "\x64\x48\x8b\x04\x25\x00\x00\x00\x00"
1149 "\x48\x8d\x80\x00\x00\x00\x00";
1150 int32_t s32;
1152 if (!_tls_verify_gd(buf, lre->lre_offset - 4))
1153 ld_warn(ld, "unrecognized TLS global dynamic model code");
1155 /* Rewrite Global Dynamic to Local Exec model. */
1156 memcpy((uint8_t *) buf + lre->lre_offset - 4, le, sizeof(le) - 1);
1159 * R_X86_64_TLSGD relocation is applied at gd[4]. After it's relaxed
1160 * to Local Exec model, the resulting R_X86_64_TPOFF32 should be
1161 * applied at le[12].
1163 s32 = _tls_tpoff(lo, lsb);
1164 WRITE_32(buf + lre->lre_offset + 8, s32);
1166 /* Ignore the next R_X86_64_PLT32 relocation for _tls_get_addr. */
1167 ls->ls_ignore_next_plt = 1;
1170 static void
1171 _tls_relax_ld_to_le(struct ld *ld, struct ld_state *ls,
1172 struct ld_reloc_entry *lre, uint8_t *buf)
1175 * Local Exec model: (with padding)
1177 * 0x00 .word 0x6666
1178 * 0x02 .byte 0x66
1179 * 0x03 movq %fs:0, %rax
1181 uint8_t le_p[] = "\x66\x66\x66\x64\x48\x8b\x04\x25\x00\x00\x00\x00";
1183 assert(lre->lre_type == R_X86_64_TLSLD);
1185 if (!_tls_verify_ld(buf, lre->lre_offset - 3))
1186 ld_warn(ld, "unrecognized TLS local dynamic model code");
1188 /* Rewrite Local Dynamic to Local Exec model. */
1189 memcpy(buf + lre->lre_offset - 3, le_p, sizeof(le_p) - 1);
1191 /* Ignore the next R_X86_64_PLT32 relocation for _tls_get_addr. */
1192 ls->ls_ignore_next_plt = 1;
1195 static void
1196 _tls_relax_ie_to_le(struct ld *ld, struct ld_output *lo,
1197 struct ld_reloc_entry *lre, struct ld_symbol *lsb, uint8_t *buf)
1199 int32_t s32;
1200 uint8_t reg;
1202 (void) ld;
1204 assert(lre->lre_type == R_X86_64_GOTTPOFF);
1207 * Rewrite Initial Exec to Local Exec model: rewrite
1208 * "movq 0x0(%rip),%reg" to "movq 0x0,%reg". or,
1209 * "addq 0x0(%rip),%rsp" to "addq 0x0,%rsp". or,
1210 * "addq 0x0(%rip),%reg" to "leaq 0x0(%reg),%reg"
1212 reg = buf[lre->lre_offset - 1] >> 3;
1213 if (buf[lre->lre_offset - 2] == 0x8b) {
1214 /* movq 0x0(%rip),%reg -> movq 0x0,%reg. */
1215 buf[lre->lre_offset - 2] = 0xc7;
1216 buf[lre->lre_offset - 1] = 0xc0 | reg; /* Set r/m to `reg' */
1218 * Set REX.B (high bit for r/m) if REX.R (high bit for reg)
1219 * is set.
1221 if (buf[lre->lre_offset - 3] == 0x4c)
1222 buf[lre->lre_offset - 3] = 0x49;
1223 } else if (reg == 4) {
1224 /* addq 0x0(%rip),%rsp -> addq 0x0,%rsp */
1225 buf[lre->lre_offset - 2] = 0x81;
1226 buf[lre->lre_offset - 1] = 0xc0 | reg; /* Set r/m to `reg' */
1228 * Set REX.B (high bit for r/m) if REX.R (high bit for reg)
1229 * is set.
1231 if (buf[lre->lre_offset - 3] == 0x4c)
1232 buf[lre->lre_offset - 3] = 0x49;
1233 } else {
1234 /* addq 0x0(%rip),%reg -> leaq 0x0(%reg),%reg */
1235 buf[lre->lre_offset - 2] = 0x8d;
1236 /* Both reg and r/m in ModRM should be set to `reg' */
1237 buf[lre->lre_offset - 1] = 0x80 | reg | (reg << 3);
1238 /* Set both REX.B and REX.R if REX.R is set */
1239 if (buf[lre->lre_offset - 3] == 0x4c)
1240 buf[lre->lre_offset - 3] = 0x4d;
1243 * R_X86_64_GOTTPOFF relocation is applied at ie[12]. After it's
1244 * relaxed to Local Exec model, the resulting R_X86_64_TPOFF32
1245 * should be applied at le[12]. Thus the offset remains the same.
1247 s32 = _tls_tpoff(lo, lsb);
1248 WRITE_32(buf + lre->lre_offset, s32);
1251 static void
1252 _create_tls_gd_reloc(struct ld *ld, struct ld_symbol *lsb)
1256 * Reserve 2 GOT entries and generate R_X86_64_DTPMOD64 and
1257 * R_X86_64_DTPOFF64 relocations.
1259 if (!lsb->lsb_got) {
1260 _reserve_got_entry(ld, lsb, 2);
1261 _create_got_reloc(ld, lsb, R_X86_64_DTPMOD64,
1262 lsb->lsb_got_off);
1263 _create_got_reloc(ld, lsb, R_X86_64_DTPOFF64,
1264 lsb->lsb_got_off + 8);
1268 static void
1269 _create_tls_ld_reloc(struct ld *ld, struct ld_symbol *lsb)
1272 /* Reserve 2 GOT entries and generate R_X86_64_DTPMOD64 reloation. */
1273 if (!lsb->lsb_got) {
1274 _reserve_got_entry(ld, lsb, 2);
1275 _create_got_reloc(ld, lsb, R_X86_64_DTPMOD64,
1276 lsb->lsb_got_off);
1277 lsb->lsb_tls_ld = 1;
1281 static void
1282 _create_tls_ie_reloc(struct ld *ld, struct ld_symbol *lsb)
1285 /* Reserve 1 GOT entry and generate R_X86_64_TPOFF64 relocation. */
1286 if (!lsb->lsb_got) {
1287 _reserve_got_entry(ld, lsb, 1);
1288 _create_got_reloc(ld, lsb, R_X86_64_TPOFF64,
1289 lsb->lsb_got_off);
1293 void
1294 amd64_register(struct ld *ld)
1296 struct ld_arch *amd64, *amd64_alt;
1298 if ((amd64 = calloc(1, sizeof(*amd64))) == NULL)
1299 ld_fatal_std(ld, "calloc");
1301 snprintf(amd64->name, sizeof(amd64->name), "%s", "amd64");
1303 amd64->script = amd64_script;
1304 amd64->interp = "/libexec/ld-elf.so.1";
1305 amd64->get_max_page_size = _get_max_page_size;
1306 amd64->get_common_page_size = _get_common_page_size;
1307 amd64->scan_reloc = _scan_reloc;
1308 amd64->process_reloc = _process_reloc;
1309 amd64->adjust_reloc = _adjust_reloc;
1310 amd64->is_absolute_reloc = _is_absolute_reloc;
1311 amd64->is_relative_reloc = _is_relative_reloc;
1312 amd64->finalize_reloc = _finalize_reloc;
1313 amd64->finalize_got_and_plt = _finalize_got_and_plt;
1314 amd64->reloc_is_64bit = 1;
1315 amd64->reloc_is_rela = 1;
1316 amd64->reloc_entsize = sizeof(Elf64_Rela);
1318 HASH_ADD_STR(ld->ld_arch_list, name, amd64);
1320 if ((amd64_alt = calloc(1, sizeof(*amd64_alt))) == NULL)
1321 ld_fatal_std(ld, "calloc");
1322 memcpy(amd64_alt, amd64, sizeof(struct ld_arch));
1323 amd64_alt->alias = amd64;
1324 snprintf(amd64_alt->name, sizeof(amd64_alt->name), "%s", "x86-64");
1326 HASH_ADD_STR(ld->ld_arch_list, name, amd64_alt);