Blame sysdeps/x86_64/dl-tlsdesc.S

Packit 6c4009
/* Thread-local storage handling in the ELF dynamic linker.  x86_64 version.
Packit 6c4009
   Copyright (C) 2004-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <tls.h>
Packit 6c4009
#include "tlsdesc.h"
Packit 6c4009
Packit 6c4009
	.text
Packit 6c4009
Packit 6c4009
     /* This function is used to compute the TP offset for symbols in
Packit 6c4009
	Static TLS, i.e., whose TP offset is the same for all
Packit 6c4009
	threads.
Packit 6c4009
Packit 6c4009
	The incoming %rax points to the TLS descriptor, such that
Packit 6c4009
	0(%rax) points to _dl_tlsdesc_return itself, and 8(%rax) holds
Packit 6c4009
	the TP offset of the symbol corresponding to the object
Packit 6c4009
	denoted by the argument.  */
Packit 6c4009
Packit 6c4009
	.hidden _dl_tlsdesc_return
Packit 6c4009
	.global	_dl_tlsdesc_return
Packit 6c4009
	.type	_dl_tlsdesc_return,@function
Packit 6c4009
	cfi_startproc
Packit 6c4009
	.align 16
Packit 6c4009
_dl_tlsdesc_return:
Packit 6c4009
	_CET_ENDBR
Packit 6c4009
	movq	8(%rax), %rax
Packit 6c4009
	ret
Packit 6c4009
	cfi_endproc
Packit 6c4009
	.size	_dl_tlsdesc_return, .-_dl_tlsdesc_return
Packit 6c4009
Packit 6c4009
     /* This function is used for undefined weak TLS symbols, for
Packit 6c4009
	which the base address (i.e., disregarding any addend) should
Packit 6c4009
	resolve to NULL.
Packit 6c4009
Packit 6c4009
	%rax points to the TLS descriptor, such that 0(%rax) points to
Packit 6c4009
	_dl_tlsdesc_undefweak itself, and 8(%rax) holds the addend.
Packit 6c4009
	We return the addend minus the TP, such that, when the caller
Packit 6c4009
	adds TP, it gets the addend back.  If that's zero, as usual,
Packit 6c4009
	that's most likely a NULL pointer.  */
Packit 6c4009
Packit 6c4009
	.hidden _dl_tlsdesc_undefweak
Packit 6c4009
	.global	_dl_tlsdesc_undefweak
Packit 6c4009
	.type	_dl_tlsdesc_undefweak,@function
Packit 6c4009
	cfi_startproc
Packit 6c4009
	.align 16
Packit 6c4009
_dl_tlsdesc_undefweak:
Packit 6c4009
	_CET_ENDBR
Packit 6c4009
	movq	8(%rax), %rax
Packit 6c4009
	subq	%fs:0, %rax
Packit 6c4009
	ret
Packit 6c4009
	cfi_endproc
Packit 6c4009
	.size	_dl_tlsdesc_undefweak, .-_dl_tlsdesc_undefweak
Packit 6c4009
Packit 6c4009
#ifdef SHARED
Packit 6c4009
	.hidden _dl_tlsdesc_dynamic
Packit 6c4009
	.global	_dl_tlsdesc_dynamic
Packit 6c4009
	.type	_dl_tlsdesc_dynamic,@function
Packit 6c4009
Packit 6c4009
     /* %rax points to the TLS descriptor, such that 0(%rax) points to
Packit 6c4009
	_dl_tlsdesc_dynamic itself, and 8(%rax) points to a struct
Packit 6c4009
	tlsdesc_dynamic_arg object.  It must return in %rax the offset
Packit 6c4009
	between the thread pointer and the object denoted by the
Packit 6c4009
	argument, without clobbering any registers.
Packit 6c4009
Packit 6c4009
	The assembly code that follows is a rendition of the following
Packit 6c4009
	C code, hand-optimized a little bit.
Packit 6c4009
Packit 6c4009
ptrdiff_t
Packit 6c4009
_dl_tlsdesc_dynamic (register struct tlsdesc *tdp asm ("%rax"))
Packit 6c4009
{
Packit 6c4009
  struct tlsdesc_dynamic_arg *td = tdp->arg;
Packit 6c4009
  dtv_t *dtv = *(dtv_t **)((char *)__thread_pointer + DTV_OFFSET);
Packit 6c4009
  if (__builtin_expect (td->gen_count <= dtv[0].counter
Packit 6c4009
			&& (dtv[td->tlsinfo.ti_module].pointer.val
Packit 6c4009
			    != TLS_DTV_UNALLOCATED),
Packit 6c4009
			1))
Packit 6c4009
    return dtv[td->tlsinfo.ti_module].pointer.val + td->tlsinfo.ti_offset
Packit 6c4009
      - __thread_pointer;
Packit 6c4009
Packit 6c4009
  return __tls_get_addr_internal (&td->tlsinfo) - __thread_pointer;
Packit 6c4009
}
Packit 6c4009
*/
Packit 6c4009
	cfi_startproc
Packit 6c4009
	.align 16
Packit 6c4009
_dl_tlsdesc_dynamic:
Packit 6c4009
	_CET_ENDBR
Packit 6c4009
	/* Preserve call-clobbered registers that we modify.
Packit 6c4009
	   We need two scratch regs anyway.  */
Packit 6c4009
	movq	%rsi, -16(%rsp)
Packit 6c4009
	movq	%fs:DTV_OFFSET, %rsi
Packit 6c4009
	movq	%rdi, -8(%rsp)
Packit 6c4009
	movq	TLSDESC_ARG(%rax), %rdi
Packit 6c4009
	movq	(%rsi), %rax
Packit 6c4009
	cmpq	%rax, TLSDESC_GEN_COUNT(%rdi)
Packit 6c4009
	ja	.Lslow
Packit 6c4009
	movq	TLSDESC_MODID(%rdi), %rax
Packit 6c4009
	salq	$4, %rax
Packit 6c4009
	movq	(%rax,%rsi), %rax
Packit 6c4009
	cmpq	$-1, %rax
Packit 6c4009
	je	.Lslow
Packit 6c4009
	addq	TLSDESC_MODOFF(%rdi), %rax
Packit 6c4009
.Lret:
Packit 6c4009
	movq	-16(%rsp), %rsi
Packit 6c4009
	subq	%fs:0, %rax
Packit 6c4009
	movq	-8(%rsp), %rdi
Packit 6c4009
	ret
Packit 6c4009
.Lslow:
Packit 6c4009
	/* Besides rdi and rsi, saved above, save rdx, rcx, r8, r9,
Packit 6c4009
	   r10 and r11.  Also, align the stack, that's off by 8 bytes.	*/
Packit 6c4009
	subq	$72, %rsp
Packit 6c4009
	cfi_adjust_cfa_offset (72)
Packit 6c4009
	movq	%rdx, 8(%rsp)
Packit 6c4009
	movq	%rcx, 16(%rsp)
Packit 6c4009
	movq	%r8, 24(%rsp)
Packit 6c4009
	movq	%r9, 32(%rsp)
Packit 6c4009
	movq	%r10, 40(%rsp)
Packit 6c4009
	movq	%r11, 48(%rsp)
Packit 6c4009
	/* %rdi already points to the tlsinfo data structure.  */
Packit 6c4009
#ifdef NO_RTLD_HIDDEN
Packit 6c4009
	call	JUMPTARGET (__tls_get_addr)
Packit 6c4009
#else
Packit 6c4009
	call	HIDDEN_JUMPTARGET (__tls_get_addr)
Packit 6c4009
#endif
Packit 6c4009
	movq	8(%rsp), %rdx
Packit 6c4009
	movq	16(%rsp), %rcx
Packit 6c4009
	movq	24(%rsp), %r8
Packit 6c4009
	movq	32(%rsp), %r9
Packit 6c4009
	movq	40(%rsp), %r10
Packit 6c4009
	movq	48(%rsp), %r11
Packit 6c4009
	addq	$72, %rsp
Packit 6c4009
	cfi_adjust_cfa_offset (-72)
Packit 6c4009
	jmp	.Lret
Packit 6c4009
	cfi_endproc
Packit 6c4009
	.size	_dl_tlsdesc_dynamic, .-_dl_tlsdesc_dynamic
Packit 6c4009
#endif /* SHARED */
Packit 6c4009
Packit 6c4009
     /* This function is a wrapper for a lazy resolver for TLS_DESC
Packit 6c4009
	RELA relocations.  The incoming 0(%rsp) points to the caller's
Packit 6c4009
	link map, pushed by the dynamic object's internal lazy TLS
Packit 6c4009
	resolver front-end before tail-calling us.  We need to pop it
Packit 6c4009
	ourselves.  %rax points to a TLS descriptor, such that 0(%rax)
Packit 6c4009
	holds the address of the internal resolver front-end (unless
Packit 6c4009
	some other thread beat us to resolving it) and 8(%rax) holds a
Packit 6c4009
	pointer to the relocation.
Packit 6c4009
Packit 6c4009
	When the actual resolver returns, it will have adjusted the
Packit 6c4009
	TLS descriptor such that we can tail-call it for it to return
Packit 6c4009
	the TP offset of the symbol.  */
Packit 6c4009
Packit 6c4009
	.hidden _dl_tlsdesc_resolve_rela
Packit 6c4009
	.global	_dl_tlsdesc_resolve_rela
Packit 6c4009
	.type	_dl_tlsdesc_resolve_rela,@function
Packit 6c4009
	cfi_startproc
Packit 6c4009
	.align 16
Packit 6c4009
	/* The PLT entry will have pushed the link_map pointer.  */
Packit 6c4009
_dl_tlsdesc_resolve_rela:
Packit 6c4009
	_CET_ENDBR
Packit 6c4009
	cfi_adjust_cfa_offset (8)
Packit 6c4009
	/* Save all call-clobbered registers.  Add 8 bytes for push in
Packit 6c4009
	   the PLT entry to align the stack.  */
Packit 6c4009
	subq	$80, %rsp
Packit 6c4009
	cfi_adjust_cfa_offset (80)
Packit 6c4009
	movq	%rax, (%rsp)
Packit 6c4009
	movq	%rdi, 8(%rsp)
Packit 6c4009
	movq	%rax, %rdi	/* Pass tlsdesc* in %rdi.  */
Packit 6c4009
	movq	%rsi, 16(%rsp)
Packit 6c4009
	movq	80(%rsp), %rsi	/* Pass link_map* in %rsi.  */
Packit 6c4009
	movq	%r8, 24(%rsp)
Packit 6c4009
	movq	%r9, 32(%rsp)
Packit 6c4009
	movq	%r10, 40(%rsp)
Packit 6c4009
	movq	%r11, 48(%rsp)
Packit 6c4009
	movq	%rdx, 56(%rsp)
Packit 6c4009
	movq	%rcx, 64(%rsp)
Packit 6c4009
	call	_dl_tlsdesc_resolve_rela_fixup
Packit 6c4009
	movq	(%rsp), %rax
Packit 6c4009
	movq	8(%rsp), %rdi
Packit 6c4009
	movq	16(%rsp), %rsi
Packit 6c4009
	movq	24(%rsp), %r8
Packit 6c4009
	movq	32(%rsp), %r9
Packit 6c4009
	movq	40(%rsp), %r10
Packit 6c4009
	movq	48(%rsp), %r11
Packit 6c4009
	movq	56(%rsp), %rdx
Packit 6c4009
	movq	64(%rsp), %rcx
Packit 6c4009
	addq	$88, %rsp
Packit 6c4009
	cfi_adjust_cfa_offset (-88)
Packit 6c4009
	jmp	*(%rax)
Packit 6c4009
	cfi_endproc
Packit 6c4009
	.size	_dl_tlsdesc_resolve_rela, .-_dl_tlsdesc_resolve_rela
Packit 6c4009
Packit 6c4009
     /* This function is a placeholder for lazy resolving of TLS
Packit 6c4009
	relocations.  Once some thread starts resolving a TLS
Packit 6c4009
	relocation, it sets up the TLS descriptor to use this
Packit 6c4009
	resolver, such that other threads that would attempt to
Packit 6c4009
	resolve it concurrently may skip the call to the original lazy
Packit 6c4009
	resolver and go straight to a condition wait.
Packit 6c4009
Packit 6c4009
	When the actual resolver returns, it will have adjusted the
Packit 6c4009
	TLS descriptor such that we can tail-call it for it to return
Packit 6c4009
	the TP offset of the symbol.  */
Packit 6c4009
Packit 6c4009
	.hidden _dl_tlsdesc_resolve_hold
Packit 6c4009
	.global	_dl_tlsdesc_resolve_hold
Packit 6c4009
	.type	_dl_tlsdesc_resolve_hold,@function
Packit 6c4009
	cfi_startproc
Packit 6c4009
	.align 16
Packit 6c4009
_dl_tlsdesc_resolve_hold:
Packit 6c4009
0:
Packit 6c4009
	_CET_ENDBR
Packit 6c4009
	/* Save all call-clobbered registers.  */
Packit 6c4009
	subq	$72, %rsp
Packit 6c4009
	cfi_adjust_cfa_offset (72)
Packit 6c4009
	movq	%rax, (%rsp)
Packit 6c4009
	movq	%rdi, 8(%rsp)
Packit 6c4009
	movq	%rax, %rdi	/* Pass tlsdesc* in %rdi.  */
Packit 6c4009
	movq	%rsi, 16(%rsp)
Packit 6c4009
	/* Pass _dl_tlsdesc_resolve_hold's address in %rsi.  */
Packit 6c4009
	leaq	. - _dl_tlsdesc_resolve_hold(%rip), %rsi
Packit 6c4009
	movq	%r8, 24(%rsp)
Packit 6c4009
	movq	%r9, 32(%rsp)
Packit 6c4009
	movq	%r10, 40(%rsp)
Packit 6c4009
	movq	%r11, 48(%rsp)
Packit 6c4009
	movq	%rdx, 56(%rsp)
Packit 6c4009
	movq	%rcx, 64(%rsp)
Packit 6c4009
	call	_dl_tlsdesc_resolve_hold_fixup
Packit 6c4009
1:
Packit 6c4009
	movq	(%rsp), %rax
Packit 6c4009
	movq	8(%rsp), %rdi
Packit 6c4009
	movq	16(%rsp), %rsi
Packit 6c4009
	movq	24(%rsp), %r8
Packit 6c4009
	movq	32(%rsp), %r9
Packit 6c4009
	movq	40(%rsp), %r10
Packit 6c4009
	movq	48(%rsp), %r11
Packit 6c4009
	movq	56(%rsp), %rdx
Packit 6c4009
	movq	64(%rsp), %rcx
Packit 6c4009
	addq	$72, %rsp
Packit 6c4009
	cfi_adjust_cfa_offset (-72)
Packit 6c4009
	jmp	*(%rax)
Packit 6c4009
	cfi_endproc
Packit 6c4009
	.size	_dl_tlsdesc_resolve_hold, .-_dl_tlsdesc_resolve_hold