2 * Copyright (c) 2012,2013 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
29 #include "ld_dynamic.h"
31 #include "ld_layout.h"
32 #include "ld_output.h"
34 #include "ld_symbols.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
,
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
,
51 static struct ld_input_section
*_find_and_create_gotplt_section(struct ld
*ld
,
53 static struct ld_input_section
*_find_and_create_plt_section(struct ld
*ld
,
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
,
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
,
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
);
89 _get_max_page_size(struct ld
*ld
)
97 _get_common_page_size(struct ld
*ld
)
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
)
116 _is_relative_reloc(uint64_t r
)
119 if (r
== R_X86_64_RELATIVE
)
126 _warn_pic(struct ld
*ld
, struct ld_reloc_entry
*lre
)
128 struct ld_symbol
*lsb
;
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
);
138 ld_warn(ld
, "relocation %s can not be used by runtime linker;"
139 " recompile with -fPIC", elftc_reloc_type_str(EM_X86_64
,
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");
154 is
= ld_input_add_internal_section(ld
, ".got");
157 is
->is_type
= SHT_PROGBITS
;
158 is
->is_flags
= SHF_ALLOC
| SHF_WRITE
;
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");
175 is
= ld_input_add_internal_section(ld
, ".got.plt");
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
);
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");
203 is
= ld_input_add_internal_section(ld
, ".plt");
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);
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. */
227 /* Reserve GOT entries. */
228 lsb
->lsb_got_off
= ld_input_reserve_ibuf(is
, num
);
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);
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);
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
,
273 _create_got_reloc(struct ld
*ld
, struct ld_symbol
*lsb
, uint64_t type
,
276 struct ld_input_section
*tis
;
278 tis
= _find_and_create_got_section(ld
, 0);
281 ld_reloc_create_entry(ld
, ".rela.got", tis
, type
, lsb
, offset
, 0);
283 if (type
!= R_X86_64_RELATIVE
)
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");
297 ld_reloc_create_entry(ld
, ".rela.bss", tis
, R_X86_64_COPY
, lsb
,
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
);
313 ld_reloc_create_entry(ld
, ".rela.data.rel.ro.local",
314 is
, type
, lsb
, offset
, addend
);
316 if (is
->is_flags
& SHF_WRITE
)
317 ld_reloc_create_entry(ld
, ".rela.data.rel",
318 is
, type
, lsb
, offset
, addend
);
320 ld_reloc_create_entry(ld
, ".rela.data.rel.ro",
321 is
, type
, lsb
, offset
, addend
);
324 if (type
!= R_X86_64_RELATIVE
)
329 _finalize_reloc(struct ld
*ld
, struct ld_input_section
*tis
,
330 struct ld_reloc_entry
*lre
)
332 struct ld_symbol
*lsb
;
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
346 lre
->lre_addend
+= lsb
->lsb_value
;
348 /* R_X86_64_RELATIVE should not associate with a symbol. */
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
&&
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";
378 int32_t s32
, pltgot
, gotpcrel
;
385 * Intiailze all .got section entries to zero.
387 got_is
= _find_and_create_got_section(ld
, 0);
389 memset(got_is
->is_ibuf
, 0, got_is
->is_size
);
392 * Search for GOT relocations that requires filling in symbol
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
) {
400 got
= (uint8_t *) got_is
->is_ibuf
+
402 WRITE_64(got
, lsb
->lsb_value
);
408 * Find the .plt section. The buffers should have been allocated
411 plt_is
= _find_and_create_plt_section(ld
, 0);
414 plt_os
= plt_is
->is_output
;
415 plt
= plt_is
->is_ibuf
;
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
;
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
);
441 /* Reserve the second and the third entry for the dynamic linker. */
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)
457 WRITE_8(plt
+ 1, 0x35);
458 s32
= pltgot
- 6 + 8;
459 WRITE_32(plt
+ 2, s32
);
463 * Jump to the address in the third GOT entry (call into
464 * the dynamic linker). (JMP reg/memXX [RIP+disp32])
468 WRITE_8(plt
+ 1, 0x25);
469 s32
= pltgot
- 12 + 16;
470 WRITE_32(plt
+ 2, s32
);
473 /* Padding: 4-byte nop. (NOP [rAx+disp8]) */
475 WRITE_8(plt
+ 1, 0x1f);
476 WRITE_8(plt
+ 2, 0x40);
477 WRITE_8(plt
+ 3, 0x0);
481 * Walk through the sorted PLT relocations in the output section
482 * and fill in each GOT and PLT entries.
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])
512 WRITE_8(plt
+ 1, 0x25);
513 WRITE_32(plt
+ 2, gotpcrel
);
517 * PLT: Symbol is not resolved, push the relocation index to
518 * the stack. (PUSH imm32)
521 WRITE_32(plt
+ 1, j
);
525 * PLT: Jump to the first PLT entry, eventually call the
526 * dynamic linker. (JMP rel32off)
529 s32
= - (i
- 1) * 16;
530 WRITE_32(plt
+ 1, s32
);
534 * GOT: Write the GOT entry for this function, pointing to
537 u64
= plt_os
->os_addr
+ (i
- 2) * 16 + 6;
540 /* Increase relocation entry index. */
543 /* Move to next GOT entry. */
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
);
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
) {
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
,
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
)) {
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
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
) {
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
,
648 _create_dynamic_reloc(ld
, is
, lsb
,
649 R_X86_64_64
, lre
->lre_offset
,
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
;
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
;
680 /* Create an PLT entry otherwise. */
682 _reserve_gotplt_entry(ld
, lsb
);
683 _reserve_plt_entry(ld
, lsb
);
693 * When these relocations apply to a global symbol, we should
694 * check if we need to generate PLT entry and/or a dynamic
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
713 * Note: FreeBSD rtld(1) does support
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);
731 case R_X86_64_GOTPCREL
:
733 * These relocation types instruct the linker to build a
734 * GOT and generate a GOT entry.
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
745 if (ld_reloc_require_glob_dat(ld
, lre
))
746 _create_got_reloc(ld
, lsb
, R_X86_64_GLOB_DAT
,
749 _create_got_reloc(ld
, lsb
, R_X86_64_RELATIVE
,
754 case R_X86_64_TLSGD
: /* Global Dynamic */
755 tr
= _tls_check_relax(ld
, lre
);
758 _create_tls_gd_reloc(ld
, lsb
);
760 case TLS_RELAX_INIT_EXEC
:
761 _create_tls_ie_reloc(ld
, lsb
);
763 case TLS_RELAX_LOCAL_EXEC
:
766 ld_fatal(ld
, "Internal: invalid TLS relaxation %d",
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",
781 case R_X86_64_DTPOFF32
:
782 /* Handled by R_X86_64_TLSLD case. */
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",
794 case R_X86_64_TPOFF32
: /* Local Exec */
795 /* No further relaxation possible. */
798 case R_X86_64_GOTPC32_TLSDESC
:
799 case R_X86_64_TLSDESC_CALL
:
804 ld_warn(ld
, "can not handle relocation %ju",
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
);
828 _process_reloc(struct ld
*ld
, struct ld_input_section
*is
,
829 struct ld_reloc_entry
*lre
, struct ld_symbol
*lsb
, uint8_t *buf
)
832 struct ld_output
*lo
;
833 uint64_t u64
, s
, l
, p
, g
;
837 enum ld_tls_relax tr
;
844 l
= lsb
->lsb_plt_off
;
845 p
= lre
->lre_offset
+ is
->is_output
->os_addr
+ is
->is_reloff
;
848 switch (lre
->lre_type
) {
853 WRITE_64(buf
+ lre
->lre_offset
, s
+ lre
->lre_addend
);
858 s32
= l
+ lre
->lre_addend
- p
;
860 s32
= s
+ lre
->lre_addend
- p
;
861 WRITE_32(buf
+ lre
->lre_offset
, s32
);
865 if (!ls
->ls_ignore_next_plt
) {
866 s32
= l
+ lre
->lre_addend
- p
;
867 WRITE_32(buf
+ lre
->lre_offset
, s32
);
869 ls
->ls_ignore_next_plt
= 0;
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
);
879 u64
= s
+ lre
->lre_addend
;
880 u32
= u64
& 0xffffffff;
882 ld_fatal(ld
, "R_X86_64_32 relocation failed");
883 WRITE_32(buf
+ lre
->lre_offset
, u32
);
887 s64
= s
+ lre
->lre_addend
;
888 s32
= s64
& 0xffffffff;
890 ld_fatal(ld
, "R_X86_64_32S relocation failed");
891 WRITE_32(buf
+ lre
->lre_offset
, s32
);
894 case R_X86_64_TLSGD
: /* Global Dynamic */
895 tr
= _tls_check_relax(ld
, lre
);
898 g
= _got_offset(ld
, lsb
);
899 s32
= g
+ lre
->lre_addend
- p
;
900 WRITE_32(buf
+ lre
->lre_offset
, s32
);
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
);
906 case TLS_RELAX_LOCAL_EXEC
:
907 _tls_relax_gd_to_le(ld
, ls
, lo
, lre
, lsb
, buf
);
910 ld_fatal(ld
, "Internal: invalid TLS relaxation %d",
916 case R_X86_64_TLSLD
: /* Local Dynamic */
917 tr
= _tls_check_relax(ld
, lre
);
920 g
= _got_offset(ld
, lsb
);
921 s32
= g
+ lre
->lre_addend
- p
;
922 WRITE_32(buf
+ lre
->lre_offset
, s32
);
924 case TLS_RELAX_LOCAL_EXEC
:
925 _tls_relax_ld_to_le(ld
, ls
, lre
, buf
);
928 ld_fatal(ld
, "Internal: invalid TLS relaxation %d",
934 case R_X86_64_DTPOFF32
: /* Local Dynamic (offset) */
935 tr
= _tls_check_relax(ld
, lre
);
938 s32
= _tls_dtpoff(lo
, lsb
);
939 WRITE_32(buf
+ lre
->lre_offset
, s32
);
941 case TLS_RELAX_LOCAL_EXEC
:
942 s32
= _tls_tpoff(lo
, lsb
);
943 WRITE_32(buf
+ lre
->lre_offset
, s32
);
946 ld_fatal(ld
, "Internal: invalid TLS relaxation %d",
952 case R_X86_64_GOTTPOFF
: /* Initial Exec */
953 tr
= _tls_check_relax(ld
, lre
);
956 g
= _got_offset(ld
, lsb
);
957 s32
= g
+ lre
->lre_addend
- p
;
958 WRITE_32(buf
+ lre
->lre_offset
, s32
);
960 case TLS_RELAX_LOCAL_EXEC
:
961 _tls_relax_ie_to_le(ld
, lo
, lre
, lsb
, buf
);
964 ld_fatal(ld
, "Internal: invalid TLS relaxation %d",
970 case R_X86_64_TPOFF32
: /* Local Exec */
971 s32
= _tls_tpoff(lo
, lsb
);
972 WRITE_32(buf
+ lre
->lre_offset
, s32
);
976 ld_warn(ld
, "Relocation %s not supported",
977 elftc_reloc_type_str(EM_X86_64
, lre
->lre_type
));
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
;
992 /* Only need to adjust relocation against section symbols. */
993 if (lsb
->lsb_type
!= STT_SECTION
)
996 if ((_is
= lsb
->lsb_is
) == NULL
|| _is
->is_output
== NULL
)
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
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
);
1046 _tls_tpoff(struct ld_output
*lo
, struct ld_symbol
*lsb
)
1050 tls_off
= -roundup(lo
->lo_tls_size
, lo
->lo_tls_align
);
1052 return (tls_off
+ (lsb
->lsb_value
- lo
->lo_tls_addr
));
1056 _tls_dtpoff(struct ld_output
*lo
, struct ld_symbol
*lsb
)
1059 return (lsb
->lsb_value
- lo
->lo_tls_addr
);
1063 _tls_verify_gd(uint8_t *buf
, uint64_t off
)
1066 * Global Dynamic model:
1069 * 0x01 leaq x@tlsgd(%rip), %rdi
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)
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)
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";
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
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;
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
)
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";
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;
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)
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;
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
)
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)
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)
1231 if (buf
[lre
->lre_offset
- 3] == 0x4c)
1232 buf
[lre
->lre_offset
- 3] = 0x49;
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
);
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
,
1263 _create_got_reloc(ld
, lsb
, R_X86_64_DTPOFF64
,
1264 lsb
->lsb_got_off
+ 8);
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
,
1277 lsb
->lsb_tls_ld
= 1;
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
,
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
);