Home | History | Annotate | Line # | Download | only in tls
tls.c revision 1.8.16.2
      1  1.8.16.2    martin /*	$NetBSD: tls.c,v 1.8.16.2 2020/04/13 08:03:11 martin Exp $	*/
      2       1.1     joerg 
      3       1.1     joerg /*-
      4       1.1     joerg  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5       1.1     joerg  * All rights reserved.
      6       1.1     joerg  *
      7       1.1     joerg  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1     joerg  * by Joerg Sonnenberger.
      9       1.1     joerg  *
     10       1.1     joerg  * Redistribution and use in source and binary forms, with or without
     11       1.1     joerg  * modification, are permitted provided that the following conditions
     12       1.1     joerg  * are met:
     13       1.1     joerg  * 1. Redistributions of source code must retain the above copyright
     14       1.1     joerg  *    notice, this list of conditions and the following disclaimer.
     15       1.1     joerg  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1     joerg  *    notice, this list of conditions and the following disclaimer in the
     17       1.1     joerg  *    documentation and/or other materials provided with the distribution.
     18       1.1     joerg  *
     19       1.1     joerg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1     joerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1     joerg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1     joerg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1     joerg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1     joerg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1     joerg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1     joerg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1     joerg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1     joerg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1     joerg  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1     joerg  */
     31       1.1     joerg 
     32       1.1     joerg #include <sys/cdefs.h>
     33  1.8.16.2    martin __RCSID("$NetBSD: tls.c,v 1.8.16.2 2020/04/13 08:03:11 martin Exp $");
     34       1.1     joerg 
     35       1.1     joerg #include "namespace.h"
     36       1.1     joerg 
     37       1.2     joerg #define	_rtld_tls_allocate	__libc_rtld_tls_allocate
     38       1.2     joerg #define	_rtld_tls_free		__libc_rtld_tls_free
     39       1.2     joerg 
     40       1.1     joerg #include <sys/tls.h>
     41       1.1     joerg 
     42       1.1     joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
     43       1.1     joerg 
     44       1.1     joerg #include <sys/param.h>
     45       1.1     joerg #include <sys/mman.h>
     46       1.1     joerg #include <link_elf.h>
     47       1.1     joerg #include <lwp.h>
     48  1.8.16.1  christos #include <stdbool.h>
     49  1.8.16.2    martin #include <stdalign.h>
     50       1.1     joerg #include <stddef.h>
     51       1.1     joerg #include <stdlib.h>
     52       1.1     joerg #include <string.h>
     53       1.1     joerg #include <unistd.h>
     54       1.1     joerg 
     55       1.1     joerg __dso_hidden void	__libc_static_tls_setup(void);
     56       1.1     joerg 
     57  1.8.16.1  christos static bool is_dynamic;
     58       1.1     joerg static const void *tls_initaddr;
     59       1.1     joerg static size_t tls_initsize;
     60       1.1     joerg static size_t tls_size;
     61       1.1     joerg static size_t tls_allocation;
     62       1.1     joerg static void *initial_thread_tcb;
     63       1.1     joerg 
     64       1.4     skrll void * __libc_tls_get_addr(void);
     65       1.4     skrll 
     66       1.4     skrll __weak_alias(__tls_get_addr, __libc_tls_get_addr)
     67       1.1     joerg #ifdef __i386__
     68       1.4     skrll __weak_alias(___tls_get_addr, __libc_tls_get_addr)
     69       1.1     joerg #endif
     70       1.1     joerg 
     71       1.4     skrll void *
     72       1.4     skrll __libc_tls_get_addr(void)
     73       1.4     skrll {
     74       1.4     skrll 
     75       1.4     skrll 	abort();
     76       1.5        he 	/* NOTREACHED */
     77       1.4     skrll }
     78       1.4     skrll 
     79       1.1     joerg __weak_alias(_rtld_tls_allocate, __libc_rtld_tls_allocate)
     80       1.1     joerg 
     81       1.1     joerg struct tls_tcb *
     82       1.1     joerg _rtld_tls_allocate(void)
     83       1.1     joerg {
     84       1.1     joerg 	struct tls_tcb *tcb;
     85       1.1     joerg 	uint8_t *p;
     86       1.1     joerg 
     87       1.1     joerg 	if (initial_thread_tcb == NULL) {
     88  1.8.16.2    martin #ifdef __HAVE_TLS_VARIANT_I
     89  1.8.16.2    martin 		tls_allocation = tls_size;
     90  1.8.16.2    martin #else
     91  1.8.16.2    martin 		tls_allocation = roundup2(tls_size, alignof(max_align_t));
     92       1.1     joerg #endif
     93       1.1     joerg 
     94  1.8.16.2    martin 		initial_thread_tcb = p = mmap(NULL,
     95  1.8.16.2    martin 		    tls_allocation + sizeof(*tcb), PROT_READ | PROT_WRITE,
     96  1.8.16.2    martin 		    MAP_ANON, -1, 0);
     97       1.1     joerg 	} else {
     98  1.8.16.2    martin 		p = calloc(1, tls_allocation + sizeof(*tcb));
     99       1.1     joerg 	}
    100       1.1     joerg 	if (p == NULL) {
    101       1.1     joerg 		static const char msg[] =  "TLS allocation failed, terminating\n";
    102       1.1     joerg 		write(STDERR_FILENO, msg, sizeof(msg));
    103       1.1     joerg 		_exit(127);
    104       1.1     joerg 	}
    105       1.1     joerg #ifdef __HAVE_TLS_VARIANT_I
    106       1.1     joerg 	/* LINTED */
    107       1.1     joerg 	tcb = (struct tls_tcb *)p;
    108       1.1     joerg 	p += sizeof(struct tls_tcb);
    109       1.1     joerg #else
    110       1.1     joerg 	/* LINTED tls_size is rounded above */
    111  1.8.16.2    martin 	tcb = (struct tls_tcb *)(p + tls_allocation);
    112  1.8.16.2    martin 	p = (uint8_t *)tcb - tls_size;
    113       1.1     joerg 	tcb->tcb_self = tcb;
    114       1.1     joerg #endif
    115       1.1     joerg 	memcpy(p, tls_initaddr, tls_initsize);
    116       1.1     joerg 
    117       1.1     joerg 	return tcb;
    118       1.1     joerg }
    119       1.1     joerg 
    120       1.1     joerg __weak_alias(_rtld_tls_free, __libc_rtld_tls_free)
    121       1.1     joerg 
    122       1.1     joerg void
    123       1.1     joerg _rtld_tls_free(struct tls_tcb *tcb)
    124       1.1     joerg {
    125       1.1     joerg 	uint8_t *p;
    126       1.1     joerg 
    127       1.1     joerg #ifdef __HAVE_TLS_VARIANT_I
    128       1.1     joerg 	/* LINTED */
    129       1.1     joerg 	p = (uint8_t *)tcb;
    130       1.1     joerg #else
    131       1.1     joerg 	/* LINTED */
    132  1.8.16.2    martin 	p = (uint8_t *)tcb - tls_allocation;
    133       1.1     joerg #endif
    134       1.1     joerg 	if (p == initial_thread_tcb)
    135  1.8.16.2    martin 		munmap(p, tls_allocation + sizeof(*tcb));
    136       1.1     joerg 	else
    137       1.1     joerg 		free(p);
    138       1.1     joerg }
    139       1.1     joerg 
    140       1.7      matt static int __section(".text.startup")
    141       1.1     joerg __libc_static_tls_setup_cb(struct dl_phdr_info *data, size_t len, void *cookie)
    142       1.1     joerg {
    143       1.1     joerg 	const Elf_Phdr *phdr = data->dlpi_phdr;
    144       1.1     joerg 	const Elf_Phdr *phlimit = data->dlpi_phdr + data->dlpi_phnum;
    145       1.1     joerg 
    146       1.1     joerg 	for (; phdr < phlimit; ++phdr) {
    147  1.8.16.1  christos 		if (phdr->p_type == PT_INTERP) {
    148  1.8.16.1  christos 			is_dynamic = true;
    149  1.8.16.1  christos 			return -1;
    150  1.8.16.1  christos 		}
    151       1.1     joerg 		if (phdr->p_type != PT_TLS)
    152       1.1     joerg 			continue;
    153       1.1     joerg 		tls_initaddr = (void *)(phdr->p_vaddr + data->dlpi_addr);
    154       1.1     joerg 		tls_initsize = phdr->p_filesz;
    155  1.8.16.2    martin #ifdef __HAVE_TLS_VARIANT_I
    156       1.1     joerg 		tls_size = phdr->p_memsz;
    157  1.8.16.2    martin #else
    158  1.8.16.2    martin 		tls_size = roundup2(phdr->p_memsz, phdr->p_align);
    159  1.8.16.2    martin #endif
    160       1.1     joerg 	}
    161       1.1     joerg 	return 0;
    162       1.1     joerg }
    163       1.1     joerg 
    164       1.1     joerg void
    165       1.1     joerg __libc_static_tls_setup(void)
    166       1.1     joerg {
    167       1.1     joerg 	struct tls_tcb *tcb;
    168       1.1     joerg 
    169       1.1     joerg 	dl_iterate_phdr(__libc_static_tls_setup_cb, NULL);
    170  1.8.16.1  christos 	if (is_dynamic)
    171  1.8.16.1  christos 		return;
    172       1.1     joerg 
    173       1.1     joerg 	tcb = _rtld_tls_allocate();
    174       1.3      matt #ifdef __HAVE___LWP_SETTCB
    175       1.3      matt 	__lwp_settcb(tcb);
    176       1.3      matt #else
    177       1.1     joerg 	_lwp_setprivate(tcb);
    178       1.3      matt #endif
    179       1.1     joerg }
    180       1.1     joerg 
    181       1.1     joerg #endif /* __HAVE_TLS_VARIANT_I || __HAVE_TLS_VARIANT_II */
    182