Home | History | Annotate | Line # | Download | only in ld.elf_so
tls.c revision 1.20
      1  1.20  riastrad /*	$NetBSD: tls.c,v 1.20 2024/07/22 23:14:25 riastradh Exp $	*/
      2   1.1     joerg /*-
      3   1.1     joerg  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      4   1.1     joerg  * All rights reserved.
      5   1.1     joerg  *
      6   1.1     joerg  * This code is derived from software contributed to The NetBSD Foundation
      7   1.1     joerg  * by Joerg Sonnenberger.
      8   1.1     joerg  *
      9   1.1     joerg  * Redistribution and use in source and binary forms, with or without
     10   1.1     joerg  * modification, are permitted provided that the following conditions
     11   1.1     joerg  * are met:
     12   1.1     joerg  * 1. Redistributions of source code must retain the above copyright
     13   1.1     joerg  *    notice, this list of conditions and the following disclaimer.
     14   1.1     joerg  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1     joerg  *    notice, this list of conditions and the following disclaimer in the
     16   1.1     joerg  *    documentation and/or other materials provided with the distribution.
     17   1.1     joerg  *
     18   1.1     joerg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19   1.1     joerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20   1.1     joerg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21   1.1     joerg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22   1.1     joerg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23   1.1     joerg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24   1.1     joerg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25   1.1     joerg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26   1.1     joerg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27   1.1     joerg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28   1.1     joerg  * POSSIBILITY OF SUCH DAMAGE.
     29   1.1     joerg  */
     30   1.1     joerg 
     31   1.1     joerg #include <sys/cdefs.h>
     32  1.20  riastrad __RCSID("$NetBSD: tls.c,v 1.20 2024/07/22 23:14:25 riastradh Exp $");
     33  1.20  riastrad 
     34  1.20  riastrad /*
     35  1.20  riastrad  * Thread-local storage
     36  1.20  riastrad  *
     37  1.20  riastrad  * Reference:
     38  1.20  riastrad  *
     39  1.20  riastrad  *	[ELFTLS] Ulrich Drepper, `ELF Handling For Thread-Local
     40  1.20  riastrad  *	Storage', Version 0.21, 2023-08-22.
     41  1.20  riastrad  *	https://akkadia.org/drepper/tls.pdf
     42  1.20  riastrad  *	https://web.archive.org/web/20240718081934/https://akkadia.org/drepper/tls.pdf
     43  1.20  riastrad  */
     44   1.1     joerg 
     45   1.1     joerg #include <sys/param.h>
     46   1.2     joerg #include <sys/ucontext.h>
     47   1.1     joerg #include <lwp.h>
     48  1.14     joerg #include <stdalign.h>
     49  1.13     joerg #include <stddef.h>
     50   1.1     joerg #include <string.h>
     51   1.8     skrll #include "debug.h"
     52   1.1     joerg #include "rtld.h"
     53   1.1     joerg 
     54   1.1     joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
     55   1.1     joerg 
     56   1.4     joerg static struct tls_tcb *_rtld_tls_allocate_locked(void);
     57  1.18     joerg static void *_rtld_tls_module_allocate(struct tls_tcb *, size_t);
     58   1.4     joerg 
     59  1.20  riastrad /*
     60  1.20  riastrad  * DTV offset
     61  1.20  riastrad  *
     62  1.20  riastrad  *	On some architectures (m68k, mips, or1k, powerpc, and riscv),
     63  1.20  riastrad  *	the DTV offsets passed to __tls_get_addr have a bias relative
     64  1.20  riastrad  *	to the start of the DTV, in order to maximize the range of TLS
     65  1.20  riastrad  *	offsets that can be used by instruction encodings with signed
     66  1.20  riastrad  *	displacements.
     67  1.20  riastrad  */
     68   1.3      matt #ifndef TLS_DTV_OFFSET
     69   1.3      matt #define	TLS_DTV_OFFSET	0
     70   1.3      matt #endif
     71   1.3      matt 
     72   1.1     joerg static size_t _rtld_tls_static_space;	/* Static TLS space allocated */
     73   1.1     joerg static size_t _rtld_tls_static_offset;	/* Next offset for static TLS to use */
     74  1.20  riastrad size_t _rtld_tls_dtv_generation = 1;	/* Bumped on each load of obj w/ TLS */
     75  1.20  riastrad size_t _rtld_tls_max_index = 1;		/* Max index into up-to-date DTV */
     76   1.1     joerg 
     77  1.20  riastrad /*
     78  1.20  riastrad  * DTV -- Dynamic Thread Vector
     79  1.20  riastrad  *
     80  1.20  riastrad  *	The DTV is a per-thread array that maps each module with
     81  1.20  riastrad  *	thread-local storage to a pointer into part of the thread's TCB
     82  1.20  riastrad  *	(thread control block), or dynamically loaded TLS blocks,
     83  1.20  riastrad  *	reserved for that module's storage.
     84  1.20  riastrad  *
     85  1.20  riastrad  *	The TCB itself, struct tls_tcb, has a pointer to the DTV at
     86  1.20  riastrad  *	tcb->tcb_dtv.
     87  1.20  riastrad  *
     88  1.20  riastrad  *	The layout is:
     89  1.20  riastrad  *
     90  1.20  riastrad  *		+---------------+
     91  1.20  riastrad  *		| max index     | -1    max index i for which dtv[i] is alloced
     92  1.20  riastrad  *		+---------------+
     93  1.20  riastrad  *		| generation    |  0    void **dtv points here
     94  1.20  riastrad  *		+---------------+
     95  1.20  riastrad  *		| obj 1 tls ptr |  1    TLS pointer for obj w/ obj->tlsindex 1
     96  1.20  riastrad  *		+---------------+
     97  1.20  riastrad  *		| obj 2 tls ptr |  2    TLS pointer for obj w/ obj->tlsindex 2
     98  1.20  riastrad  *		+---------------+
     99  1.20  riastrad  *		  .
    100  1.20  riastrad  *		  .
    101  1.20  riastrad  *		  .
    102  1.20  riastrad  *
    103  1.20  riastrad  *	The values of obj->tlsindex start at 1; this way,
    104  1.20  riastrad  *	dtv[obj->tlsindex] works, when dtv[0] is the generation.  The
    105  1.20  riastrad  *	TLS pointers go either into the static thread-local storage,
    106  1.20  riastrad  *	for the initial objects (i.e., those loaded at startup), or
    107  1.20  riastrad  *	into TLS blocks dynamically allocated for objects that
    108  1.20  riastrad  *	dynamically loaded by dlopen.
    109  1.20  riastrad  *
    110  1.20  riastrad  *	The generation field is a cache of the global generation number
    111  1.20  riastrad  *	_rtld_tls_dtv_generation, which is bumped every time an object
    112  1.20  riastrad  *	with TLS is loaded in _rtld_map_object, and cached by
    113  1.20  riastrad  *	__tls_get_addr (via _rtld_tls_get_addr) when a newly loaded
    114  1.20  riastrad  *	module lies outside the bounds of the current DTV.
    115  1.20  riastrad  *
    116  1.20  riastrad  *	XXX Why do we keep max index and generation separately?  They
    117  1.20  riastrad  *	appear to be initialized the same, always incremented together,
    118  1.20  riastrad  *	and always stored together.
    119  1.20  riastrad  *
    120  1.20  riastrad  *	XXX Why is this not a struct?
    121  1.20  riastrad  *
    122  1.20  riastrad  *		struct dtv {
    123  1.20  riastrad  *			size_t	dtv_gen;
    124  1.20  riastrad  *			void	*dtv_module[];
    125  1.20  riastrad  *		};
    126  1.20  riastrad  */
    127  1.15     skrll #define	DTV_GENERATION(dtv)		((size_t)((dtv)[0]))
    128  1.15     skrll #define	DTV_MAX_INDEX(dtv)		((size_t)((dtv)[-1]))
    129   1.1     joerg #define	SET_DTV_GENERATION(dtv, val)	(dtv)[0] = (void *)(size_t)(val)
    130   1.1     joerg #define	SET_DTV_MAX_INDEX(dtv, val)	(dtv)[-1] = (void *)(size_t)(val)
    131   1.1     joerg 
    132  1.20  riastrad /*
    133  1.20  riastrad  * _rtld_tls_get_addr(tcb, idx, offset)
    134  1.20  riastrad  *
    135  1.20  riastrad  *	Slow path for __tls_get_addr (see below), called to allocate
    136  1.20  riastrad  *	TLS space if needed for the object obj with obj->tlsindex idx,
    137  1.20  riastrad  *	at offset, which must be below obj->tlssize.
    138  1.20  riastrad  *
    139  1.20  riastrad  *	This may allocate a DTV if the current one is too old, and it
    140  1.20  riastrad  *	may allocate a dynamically loaded TLS block if there isn't one
    141  1.20  riastrad  *	already allocated for it.
    142  1.20  riastrad  *
    143  1.20  riastrad  *	XXX Why is the first argument passed as `void *tls' instead of
    144  1.20  riastrad  *	just `struct tls_tcb *tcb'?
    145  1.20  riastrad  */
    146   1.1     joerg void *
    147   1.1     joerg _rtld_tls_get_addr(void *tls, size_t idx, size_t offset)
    148   1.1     joerg {
    149   1.1     joerg 	struct tls_tcb *tcb = tls;
    150   1.1     joerg 	void **dtv, **new_dtv;
    151   1.5     joerg 	sigset_t mask;
    152   1.1     joerg 
    153   1.5     joerg 	_rtld_exclusive_enter(&mask);
    154   1.4     joerg 
    155   1.1     joerg 	dtv = tcb->tcb_dtv;
    156   1.1     joerg 
    157  1.20  riastrad 	/*
    158  1.20  riastrad 	 * If the generation number has changed, we have to allocate a
    159  1.20  riastrad 	 * new DTV.
    160  1.20  riastrad 	 *
    161  1.20  riastrad 	 * XXX Do we really?  Isn't it enough to check whether idx <=
    162  1.20  riastrad 	 * DTV_MAX_INDEX(dtv)?
    163  1.20  riastrad 	 */
    164   1.1     joerg 	if (__predict_false(DTV_GENERATION(dtv) != _rtld_tls_dtv_generation)) {
    165   1.1     joerg 		size_t to_copy = DTV_MAX_INDEX(dtv);
    166   1.1     joerg 
    167   1.1     joerg 		new_dtv = xcalloc((2 + _rtld_tls_max_index) * sizeof(*dtv));
    168  1.20  riastrad 		++new_dtv;		/* advance past DTV_MAX_INDEX */
    169  1.20  riastrad 		if (to_copy > _rtld_tls_max_index)	/* XXX How? */
    170   1.1     joerg 			to_copy = _rtld_tls_max_index;
    171   1.1     joerg 		memcpy(new_dtv + 1, dtv + 1, to_copy * sizeof(*dtv));
    172  1.20  riastrad 		xfree(dtv - 1);		/* retreat back to DTV_MAX_INDEX */
    173   1.1     joerg 		dtv = tcb->tcb_dtv = new_dtv;
    174   1.1     joerg 		SET_DTV_MAX_INDEX(dtv, _rtld_tls_max_index);
    175   1.1     joerg 		SET_DTV_GENERATION(dtv, _rtld_tls_dtv_generation);
    176   1.1     joerg 	}
    177   1.1     joerg 
    178   1.1     joerg 	if (__predict_false(dtv[idx] == NULL))
    179  1.18     joerg 		dtv[idx] = _rtld_tls_module_allocate(tcb, idx);
    180   1.1     joerg 
    181   1.5     joerg 	_rtld_exclusive_exit(&mask);
    182   1.4     joerg 
    183   1.1     joerg 	return (uint8_t *)dtv[idx] + offset;
    184   1.1     joerg }
    185   1.1     joerg 
    186  1.20  riastrad /*
    187  1.20  riastrad  * _rtld_tls_initial_allocation()
    188  1.20  riastrad  *
    189  1.20  riastrad  *	Allocate the TCB (thread control block) for the initial thread,
    190  1.20  riastrad  *	once the static TLS space usage has been determined (plus some
    191  1.20  riastrad  *	slop to allow certain special cases like Mesa to be dlopened).
    192  1.20  riastrad  *
    193  1.20  riastrad  *	This must be done _after_ all initial objects (i.e., those
    194  1.20  riastrad  *	loaded at startup, as opposed to objects dynamically loaded by
    195  1.20  riastrad  *	dlopen) have had TLS offsets allocated if need be by
    196  1.20  riastrad  *	_rtld_tls_offset_allocate, and have had relocations processed.
    197  1.20  riastrad  */
    198   1.1     joerg void
    199   1.1     joerg _rtld_tls_initial_allocation(void)
    200   1.1     joerg {
    201   1.1     joerg 	struct tls_tcb *tcb;
    202   1.1     joerg 
    203   1.1     joerg 	_rtld_tls_static_space = _rtld_tls_static_offset +
    204   1.1     joerg 	    RTLD_STATIC_TLS_RESERVATION;
    205   1.1     joerg 
    206   1.1     joerg #ifndef __HAVE_TLS_VARIANT_I
    207   1.1     joerg 	_rtld_tls_static_space = roundup2(_rtld_tls_static_space,
    208  1.14     joerg 	    alignof(max_align_t));
    209   1.1     joerg #endif
    210   1.8     skrll 	dbg(("_rtld_tls_static_space %zu", _rtld_tls_static_space));
    211   1.1     joerg 
    212   1.4     joerg 	tcb = _rtld_tls_allocate_locked();
    213   1.3      matt #ifdef __HAVE___LWP_SETTCB
    214   1.3      matt 	__lwp_settcb(tcb);
    215   1.3      matt #else
    216   1.1     joerg 	_lwp_setprivate(tcb);
    217   1.3      matt #endif
    218   1.1     joerg }
    219   1.1     joerg 
    220  1.20  riastrad /*
    221  1.20  riastrad  * _rtld_tls_allocate_locked()
    222  1.20  riastrad  *
    223  1.20  riastrad  *	Internal subroutine to allocate a TCB (thread control block)
    224  1.20  riastrad  *	for the current thread.
    225  1.20  riastrad  *
    226  1.20  riastrad  *	This allocates a DTV and a TCB that points to it, including
    227  1.20  riastrad  *	static space in the TCB for the TLS of the initial objects.
    228  1.20  riastrad  *	TLS blocks for dynamically loaded objects are allocated lazily.
    229  1.20  riastrad  *
    230  1.20  riastrad  *	Caller must either be single-threaded (at startup via
    231  1.20  riastrad  *	_rtld_tls_initial_allocation) or hold the rtld exclusive lock
    232  1.20  riastrad  *	(via _rtld_tls_allocate).
    233  1.20  riastrad  */
    234   1.4     joerg static struct tls_tcb *
    235   1.4     joerg _rtld_tls_allocate_locked(void)
    236   1.1     joerg {
    237   1.1     joerg 	Obj_Entry *obj;
    238   1.1     joerg 	struct tls_tcb *tcb;
    239   1.1     joerg 	uint8_t *p, *q;
    240   1.1     joerg 
    241   1.1     joerg 	p = xcalloc(_rtld_tls_static_space + sizeof(struct tls_tcb));
    242   1.1     joerg #ifdef __HAVE_TLS_VARIANT_I
    243   1.1     joerg 	tcb = (struct tls_tcb *)p;
    244   1.1     joerg 	p += sizeof(struct tls_tcb);
    245   1.1     joerg #else
    246   1.1     joerg 	p += _rtld_tls_static_space;
    247   1.1     joerg 	tcb = (struct tls_tcb *)p;
    248   1.1     joerg 	tcb->tcb_self = tcb;
    249   1.1     joerg #endif
    250  1.17  riastrad 	dbg(("lwp %d tls tcb %p", _lwp_self(), tcb));
    251   1.1     joerg 	tcb->tcb_dtv = xcalloc(sizeof(*tcb->tcb_dtv) * (2 + _rtld_tls_max_index));
    252  1.20  riastrad 	++tcb->tcb_dtv;		/* advance past DTV_MAX_INDEX */
    253   1.1     joerg 	SET_DTV_MAX_INDEX(tcb->tcb_dtv, _rtld_tls_max_index);
    254   1.1     joerg 	SET_DTV_GENERATION(tcb->tcb_dtv, _rtld_tls_dtv_generation);
    255   1.1     joerg 
    256   1.1     joerg 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
    257  1.18     joerg 		if (obj->tls_static) {
    258   1.1     joerg #ifdef __HAVE_TLS_VARIANT_I
    259   1.1     joerg 			q = p + obj->tlsoffset;
    260   1.1     joerg #else
    261   1.1     joerg 			q = p - obj->tlsoffset;
    262   1.1     joerg #endif
    263  1.17  riastrad 			dbg(("%s: [lwp %d] tls dtv %p index %zu offset %zu",
    264  1.17  riastrad 			    obj->path, _lwp_self(),
    265  1.17  riastrad 			    q, obj->tlsindex, obj->tlsoffset));
    266  1.11     joerg 			if (obj->tlsinitsize)
    267  1.11     joerg 				memcpy(q, obj->tlsinit, obj->tlsinitsize);
    268   1.1     joerg 			tcb->tcb_dtv[obj->tlsindex] = q;
    269   1.1     joerg 		}
    270   1.1     joerg 	}
    271   1.1     joerg 
    272   1.1     joerg 	return tcb;
    273   1.1     joerg }
    274   1.1     joerg 
    275  1.20  riastrad /*
    276  1.20  riastrad  * _rtld_tls_allocate()
    277  1.20  riastrad  *
    278  1.20  riastrad  *	Allocate a TCB (thread control block) for the current thread.
    279  1.20  riastrad  *
    280  1.20  riastrad  *	Called by pthread_create for non-initial threads.  (The initial
    281  1.20  riastrad  *	thread's TCB is allocated by _rtld_tls_initial_allocation.)
    282  1.20  riastrad  */
    283   1.4     joerg struct tls_tcb *
    284   1.4     joerg _rtld_tls_allocate(void)
    285   1.4     joerg {
    286   1.4     joerg 	struct tls_tcb *tcb;
    287   1.5     joerg 	sigset_t mask;
    288   1.4     joerg 
    289   1.5     joerg 	_rtld_exclusive_enter(&mask);
    290   1.4     joerg 	tcb = _rtld_tls_allocate_locked();
    291   1.5     joerg 	_rtld_exclusive_exit(&mask);
    292   1.4     joerg 
    293   1.4     joerg 	return tcb;
    294   1.4     joerg }
    295   1.4     joerg 
    296  1.20  riastrad /*
    297  1.20  riastrad  * _rtld_tls_free(tcb)
    298  1.20  riastrad  *
    299  1.20  riastrad  *	Free a TCB allocated with _rtld_tls_allocate.
    300  1.20  riastrad  *
    301  1.20  riastrad  *	Frees any TLS blocks for dynamically loaded objects that tcb's
    302  1.20  riastrad  *	DTV points to, and frees tcb's DTV, and frees tcb.
    303  1.20  riastrad  */
    304   1.1     joerg void
    305   1.1     joerg _rtld_tls_free(struct tls_tcb *tcb)
    306   1.1     joerg {
    307   1.1     joerg 	size_t i, max_index;
    308  1.11     joerg 	uint8_t *p, *p_end;
    309   1.5     joerg 	sigset_t mask;
    310   1.1     joerg 
    311   1.5     joerg 	_rtld_exclusive_enter(&mask);
    312   1.4     joerg 
    313   1.1     joerg #ifdef __HAVE_TLS_VARIANT_I
    314   1.1     joerg 	p = (uint8_t *)tcb;
    315   1.1     joerg #else
    316   1.1     joerg 	p = (uint8_t *)tcb - _rtld_tls_static_space;
    317   1.1     joerg #endif
    318  1.11     joerg 	p_end = p + _rtld_tls_static_space;
    319  1.11     joerg 
    320  1.11     joerg 	max_index = DTV_MAX_INDEX(tcb->tcb_dtv);
    321  1.11     joerg 	for (i = 1; i <= max_index; ++i) {
    322  1.11     joerg 		if ((uint8_t *)tcb->tcb_dtv[i] < p ||
    323  1.11     joerg 		    (uint8_t *)tcb->tcb_dtv[i] >= p_end)
    324  1.11     joerg 			xfree(tcb->tcb_dtv[i]);
    325  1.11     joerg 	}
    326  1.20  riastrad 	xfree(tcb->tcb_dtv - 1);	/* retreat back to DTV_MAX_INDEX */
    327   1.1     joerg 	xfree(p);
    328   1.4     joerg 
    329   1.5     joerg 	_rtld_exclusive_exit(&mask);
    330   1.1     joerg }
    331   1.1     joerg 
    332  1.20  riastrad /*
    333  1.20  riastrad  * _rtld_tls_module_allocate(tcb, idx)
    334  1.20  riastrad  *
    335  1.20  riastrad  *	Allocate thread-local storage in the thread with the given TCB
    336  1.20  riastrad  *	(thread control block) for the object obj whose obj->tlsindex
    337  1.20  riastrad  *	is idx.
    338  1.20  riastrad  *
    339  1.20  riastrad  *	If obj has had space in static TLS reserved (obj->tls_static),
    340  1.20  riastrad  *	return a pointer into that.  Otherwise, allocate a TLS block,
    341  1.20  riastrad  *	mark obj as having a TLS block allocated (obj->tls_dynamic),
    342  1.20  riastrad  *	and return it.
    343  1.20  riastrad  *
    344  1.20  riastrad  *	Called by _rtld_tls_get_addr to get the thread-local storage
    345  1.20  riastrad  *	for an object the first time around.
    346  1.20  riastrad  */
    347  1.18     joerg static void *
    348  1.18     joerg _rtld_tls_module_allocate(struct tls_tcb *tcb, size_t idx)
    349   1.1     joerg {
    350   1.1     joerg 	Obj_Entry *obj;
    351   1.1     joerg 	uint8_t *p;
    352   1.1     joerg 
    353   1.1     joerg 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
    354   1.1     joerg 		if (obj->tlsindex == idx)
    355   1.1     joerg 			break;
    356   1.1     joerg 	}
    357   1.1     joerg 	if (obj == NULL) {
    358   1.1     joerg 		_rtld_error("Module for TLS index %zu missing", idx);
    359   1.1     joerg 		_rtld_die();
    360   1.1     joerg 	}
    361  1.18     joerg 	if (obj->tls_static) {
    362  1.18     joerg #ifdef __HAVE_TLS_VARIANT_I
    363  1.19     joerg 		p = (uint8_t *)tcb + obj->tlsoffset + sizeof(struct tls_tcb);
    364  1.18     joerg #else
    365  1.18     joerg 		p = (uint8_t *)tcb - obj->tlsoffset;
    366  1.18     joerg #endif
    367  1.18     joerg 		return p;
    368  1.18     joerg 	}
    369   1.1     joerg 
    370   1.1     joerg 	p = xmalloc(obj->tlssize);
    371   1.1     joerg 	memcpy(p, obj->tlsinit, obj->tlsinitsize);
    372   1.1     joerg 	memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize);
    373   1.1     joerg 
    374  1.18     joerg 	obj->tls_dynamic = 1;
    375  1.18     joerg 
    376   1.1     joerg 	return p;
    377   1.1     joerg }
    378   1.1     joerg 
    379  1.20  riastrad /*
    380  1.20  riastrad  * _rtld_tls_offset_allocate(obj)
    381  1.20  riastrad  *
    382  1.20  riastrad  *	Allocate a static thread-local storage offset for obj.
    383  1.20  riastrad  *
    384  1.20  riastrad  *	Called by _rtld at startup for all initial objects.  Called
    385  1.20  riastrad  *	also by MD relocation logic, which is allowed (for Mesa) to
    386  1.20  riastrad  *	allocate an additional 64 bytes (RTLD_STATIC_TLS_RESERVATION)
    387  1.20  riastrad  *	of static thread-local storage in dlopened objects.
    388  1.20  riastrad  */
    389   1.1     joerg int
    390   1.1     joerg _rtld_tls_offset_allocate(Obj_Entry *obj)
    391   1.1     joerg {
    392   1.1     joerg 	size_t offset, next_offset;
    393   1.1     joerg 
    394  1.18     joerg 	if (obj->tls_dynamic)
    395  1.18     joerg 		return -1;
    396  1.18     joerg 
    397  1.18     joerg 	if (obj->tls_static)
    398   1.1     joerg 		return 0;
    399   1.1     joerg 	if (obj->tlssize == 0) {
    400   1.1     joerg 		obj->tlsoffset = 0;
    401  1.18     joerg 		obj->tls_static = 1;
    402   1.1     joerg 		return 0;
    403   1.1     joerg 	}
    404   1.1     joerg 
    405   1.1     joerg #ifdef __HAVE_TLS_VARIANT_I
    406   1.1     joerg 	offset = roundup2(_rtld_tls_static_offset, obj->tlsalign);
    407   1.1     joerg 	next_offset = offset + obj->tlssize;
    408   1.1     joerg #else
    409   1.1     joerg 	offset = roundup2(_rtld_tls_static_offset + obj->tlssize,
    410   1.1     joerg 	    obj->tlsalign);
    411   1.1     joerg 	next_offset = offset;
    412   1.1     joerg #endif
    413   1.1     joerg 
    414   1.1     joerg 	/*
    415   1.1     joerg 	 * Check if the static allocation was already done.
    416   1.1     joerg 	 * This happens if dynamically loaded modules want to use
    417   1.1     joerg 	 * static TLS space.
    418   1.1     joerg 	 *
    419   1.1     joerg 	 * XXX Keep an actual free list and callbacks for initialisation.
    420   1.1     joerg 	 */
    421   1.1     joerg 	if (_rtld_tls_static_space) {
    422   1.1     joerg 		if (obj->tlsinitsize) {
    423   1.1     joerg 			_rtld_error("%s: Use of initialized "
    424   1.7     joerg 			    "Thread Local Storage with model initial-exec "
    425   1.1     joerg 			    "and dlopen is not supported",
    426   1.1     joerg 			    obj->path);
    427   1.1     joerg 			return -1;
    428   1.1     joerg 		}
    429   1.1     joerg 		if (next_offset > _rtld_tls_static_space) {
    430   1.1     joerg 			_rtld_error("%s: No space available "
    431   1.1     joerg 			    "for static Thread Local Storage",
    432   1.1     joerg 			    obj->path);
    433   1.1     joerg 			return -1;
    434   1.1     joerg 		}
    435   1.1     joerg 	}
    436   1.1     joerg 	obj->tlsoffset = offset;
    437  1.16  riastrad 	dbg(("%s: static tls offset 0x%zx size %zu\n",
    438  1.16  riastrad 	    obj->path, obj->tlsoffset, obj->tlssize));
    439   1.1     joerg 	_rtld_tls_static_offset = next_offset;
    440  1.18     joerg 	obj->tls_static = 1;
    441   1.1     joerg 
    442   1.1     joerg 	return 0;
    443   1.1     joerg }
    444   1.1     joerg 
    445  1.20  riastrad /*
    446  1.20  riastrad  * _rtld_tls_offset_free(obj)
    447  1.20  riastrad  *
    448  1.20  riastrad  *	Free a static thread-local storage offset for obj.
    449  1.20  riastrad  *
    450  1.20  riastrad  *	Called by dlclose (via _rtld_unload_object -> _rtld_obj_free).
    451  1.20  riastrad  *
    452  1.20  riastrad  *	Since static thread-local storage is normally not used by
    453  1.20  riastrad  *	dlopened objects (with the exception of Mesa), this doesn't do
    454  1.20  riastrad  *	anything to recycle the space right now.
    455  1.20  riastrad  */
    456   1.1     joerg void
    457   1.1     joerg _rtld_tls_offset_free(Obj_Entry *obj)
    458   1.1     joerg {
    459   1.1     joerg 
    460   1.1     joerg 	/*
    461   1.1     joerg 	 * XXX See above.
    462   1.1     joerg 	 */
    463  1.18     joerg 	obj->tls_static = 0;
    464   1.1     joerg 	return;
    465   1.1     joerg }
    466   1.1     joerg 
    467  1.12       rin #if defined(__HAVE_COMMON___TLS_GET_ADDR) && defined(RTLD_LOADER)
    468   1.2     joerg /*
    469  1.20  riastrad  * __tls_get_addr(tlsindex)
    470  1.20  riastrad  *
    471  1.20  riastrad  *	Symbol directly called by code generated by the compiler for
    472  1.20  riastrad  *	references thread-local storage in the general-dynamic or
    473  1.20  riastrad  *	local-dynamic TLS models (but not initial-exec or local-exec).
    474  1.20  riastrad  *
    475  1.20  riastrad  *	The argument is a pointer to
    476  1.20  riastrad  *
    477  1.20  riastrad  *		struct {
    478  1.20  riastrad  *			unsigned long int ti_module;
    479  1.20  riastrad  *			unsigned long int ti_offset;
    480  1.20  riastrad  *		};
    481  1.20  riastrad  *
    482  1.20  riastrad  *	 as in, e.g., [ELFTLS] Sec. 3.4.3.  This coincides with the
    483  1.20  riastrad  *	 type size_t[2] on all architectures that use this common
    484  1.20  riastrad  *	 __tls_get_addr definition (XXX but why do we write it as
    485  1.20  riastrad  *	 size_t[2]?).
    486  1.20  riastrad  *
    487  1.20  riastrad  *	 ti_module, i.e., arg[0], is the obj->tlsindex assigned at
    488  1.20  riastrad  *	 load-time by _rtld_map_object, and ti_offset, i.e., arg[1], is
    489  1.20  riastrad  *	 assigned at link-time by ld(1), possibly adjusted by
    490  1.20  riastrad  *	 TLS_DTV_OFFSET.
    491  1.20  riastrad  *
    492  1.20  riastrad  *	 Some architectures -- specifically IA-64 -- use a different
    493  1.20  riastrad  *	 calling convention.  Some architectures -- specifically i386
    494  1.20  riastrad  *	 -- also use another entry point ___tls_get_addr (that's three
    495  1.20  riastrad  *	 leading underscores) with a different calling convention.
    496   1.2     joerg  */
    497   1.2     joerg void *
    498   1.2     joerg __tls_get_addr(void *arg_)
    499   1.2     joerg {
    500   1.2     joerg 	size_t *arg = (size_t *)arg_;
    501   1.2     joerg 	void **dtv;
    502   1.3      matt #ifdef __HAVE___LWP_GETTCB_FAST
    503   1.3      matt 	struct tls_tcb * const tcb = __lwp_gettcb_fast();
    504   1.3      matt #else
    505   1.3      matt 	struct tls_tcb * const tcb = __lwp_getprivate_fast();
    506   1.3      matt #endif
    507   1.3      matt 	size_t idx = arg[0], offset = arg[1] + TLS_DTV_OFFSET;
    508   1.2     joerg 
    509   1.2     joerg 	dtv = tcb->tcb_dtv;
    510   1.2     joerg 
    511  1.20  riastrad 	/*
    512  1.20  riastrad 	 * Fast path: access to an already allocated DTV entry.  This
    513  1.20  riastrad 	 * checks the current limit and the entry without needing any
    514  1.20  riastrad 	 * locking.  Entries are only freed on dlclose() and it is an
    515  1.20  riastrad 	 * application bug if code of the module is still running at
    516  1.20  riastrad 	 * that point.
    517  1.20  riastrad 	 */
    518   1.2     joerg 	if (__predict_true(idx < DTV_MAX_INDEX(dtv) && dtv[idx] != NULL))
    519   1.2     joerg 		return (uint8_t *)dtv[idx] + offset;
    520   1.2     joerg 
    521   1.2     joerg 	return _rtld_tls_get_addr(tcb, idx, offset);
    522   1.2     joerg }
    523   1.2     joerg #endif
    524   1.2     joerg 
    525   1.1     joerg #endif /* __HAVE_TLS_VARIANT_I || __HAVE_TLS_VARIANT_II */
    526