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