| Commit | Line | Data |
|---|---|---|
| b00401f0 DX |
1 | /*- |
| 2 | * Copyright (c) 2004 Doug Rabson | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 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. | |
| 13 | * | |
| 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 | |
| 24 | * SUCH DAMAGE. | |
| 25 | * | |
| 26 | * $FreeBSD: src/lib/libc/gen/tls.c,v 1.7 2005/03/01 23:42:00 davidxu Exp $ | |
| b00401f0 DX |
27 | */ |
| 28 | ||
| 29 | /* | |
| 30 | * Define stubs for TLS internals so that programs and libraries can | |
| 31 | * link. These functions will be replaced by functional versions at | |
| 32 | * runtime from ld-elf.so.1. | |
| 33 | */ | |
| 34 | ||
| bc633d63 | 35 | #include <sys/tls.h> |
| 9e2ee207 JS |
36 | |
| 37 | #include <machine/tls.h> | |
| 38 | ||
| b00401f0 DX |
39 | #include <stdlib.h> |
| 40 | #include <string.h> | |
| 41 | #include <elf.h> | |
| 42 | #include <assert.h> | |
| a1eee96a MD |
43 | #include <errno.h> |
| 44 | #include <unistd.h> | |
| 92df6c3e | 45 | |
| b00401f0 DX |
46 | #include "libc_private.h" |
| 47 | ||
| 92df6c3e DX |
48 | __weak_reference(__libc_allocate_tls, _rtld_allocate_tls); |
| 49 | __weak_reference(__libc_free_tls, _rtld_free_tls); | |
| a1eee96a | 50 | __weak_reference(__libc_call_init, _rtld_call_init); |
| 92df6c3e DX |
51 | #ifdef __i386__ |
| 52 | __weak_reference(___libc_tls_get_addr, ___tls_get_addr); | |
| 53 | #endif | |
| 54 | __weak_reference(__libc_tls_get_addr, __tls_get_addr); | |
| a1eee96a MD |
55 | __weak_reference(__libc_tls_get_addr_tcb, __tls_get_addr_tcb); |
| 56 | __weak_reference(_libc_init_tls, _init_tls); | |
| 92df6c3e | 57 | |
| a1eee96a | 58 | struct tls_tcb *__libc_allocate_tls(void); |
| f20fd431 | 59 | void __libc_free_tls(struct tls_tcb *tcb); |
| b00401f0 | 60 | |
| 9e2ee207 JS |
61 | #if !defined(RTLD_STATIC_TLS_VARIANT_II) |
| 62 | #error "Unsupported TLS layout" | |
| b00401f0 DX |
63 | #endif |
| 64 | ||
| 65 | #ifndef PIC | |
| 66 | ||
| 67 | #define round(size, align) \ | |
| 68 | (((size) + (align) - 1) & ~((align) - 1)) | |
| 69 | ||
| 70 | static size_t tls_static_space; | |
| 71 | static size_t tls_init_size; | |
| b00401f0 | 72 | static void *tls_init; |
| a1eee96a | 73 | static struct tls_tcb *initial_tcb; |
| b00401f0 DX |
74 | #endif |
| 75 | ||
| b00401f0 DX |
76 | #ifdef __i386__ |
| 77 | ||
| 92df6c3e DX |
78 | /* GNU ABI */ |
| 79 | ||
| 80 | void *___libc_tls_get_addr(void *ti) __attribute__((__regparm__(1))); | |
| b00401f0 | 81 | |
| b00401f0 DX |
82 | __attribute__((__regparm__(1))) |
| 83 | void * | |
| 92df6c3e | 84 | ___libc_tls_get_addr(void *ti __unused) |
| b00401f0 DX |
85 | { |
| 86 | return (0); | |
| 87 | } | |
| 88 | ||
| 89 | #endif | |
| 90 | ||
| 92df6c3e | 91 | void *__libc_tls_get_addr(void *ti); |
| a1eee96a | 92 | void *__libc_tls_get_addr_tcb(struct tls_tcb *, void *); |
| bc633d63 | 93 | |
| b00401f0 | 94 | void * |
| 92df6c3e | 95 | __libc_tls_get_addr(void *ti __unused) |
| b00401f0 | 96 | { |
| a1eee96a MD |
97 | return (NULL); |
| 98 | } | |
| 99 | ||
| 100 | void * | |
| 101 | __libc_tls_get_addr_tcb(struct tls_tcb *tcb __unused, void *got_ptr __unused) | |
| 102 | { | |
| 103 | return (NULL); | |
| b00401f0 DX |
104 | } |
| 105 | ||
| 92df6c3e DX |
106 | #ifndef PIC |
| 107 | ||
| b00401f0 | 108 | /* |
| a1eee96a | 109 | * Free Static TLS, weakly bound to _rtld_free_tls() |
| b00401f0 DX |
110 | */ |
| 111 | void | |
| f20fd431 | 112 | __libc_free_tls(struct tls_tcb *tcb) |
| b00401f0 | 113 | { |
| bc633d63 | 114 | size_t data_size; |
| b00401f0 | 115 | |
| bc633d63 MD |
116 | data_size = (tls_static_space + RTLD_STATIC_TLS_ALIGN_MASK) & |
| 117 | ~RTLD_STATIC_TLS_ALIGN_MASK; | |
| a1eee96a MD |
118 | |
| 119 | if (tcb == initial_tcb) { | |
| 120 | /* initial_tcb was allocated with sbrk(), cannot call free() */ | |
| 121 | } else { | |
| 122 | free((char *)tcb - data_size); | |
| 123 | } | |
| b00401f0 DX |
124 | } |
| 125 | ||
| b00401f0 | 126 | /* |
| a1eee96a | 127 | * Allocate Static TLS, weakly bound to _rtld_allocate_tls() |
| a378ce7d | 128 | * |
| a1eee96a MD |
129 | * NOTE! There is a chicken-and-egg problem here because no TLS exists |
| 130 | * on the first call into this function. | |
| b00401f0 | 131 | */ |
| bc633d63 | 132 | struct tls_tcb * |
| a1eee96a | 133 | __libc_allocate_tls(void) |
| b00401f0 | 134 | { |
| bc633d63 MD |
135 | size_t data_size; |
| 136 | struct tls_tcb *tcb; | |
| b00401f0 DX |
137 | Elf_Addr *dtv; |
| 138 | ||
| bc633d63 MD |
139 | data_size = (tls_static_space + RTLD_STATIC_TLS_ALIGN_MASK) & |
| 140 | ~RTLD_STATIC_TLS_ALIGN_MASK; | |
| a378ce7d | 141 | |
| a1eee96a MD |
142 | /* |
| 143 | * Allocate space. malloc() may require a working TLS segment | |
| 144 | * so we use sbrk() for main's TLS. | |
| 145 | */ | |
| 146 | if (initial_tcb == NULL) | |
| 97c6bef2 | 147 | tcb = sbrk(data_size + sizeof(*tcb) + 3 * sizeof(*dtv)); |
| a378ce7d | 148 | else |
| 97c6bef2 | 149 | tcb = malloc(data_size + sizeof(*tcb) + 3 * sizeof(*dtv)); |
| a1eee96a | 150 | |
| bc633d63 | 151 | tcb = (struct tls_tcb *)((char *)tcb + data_size); |
| a1eee96a | 152 | dtv = (Elf_Addr *)(tcb + 1); |
| 9e2ee207 | 153 | |
| a1eee96a | 154 | memset(tcb, 0, sizeof(*tcb)); |
| 9e2ee207 JS |
155 | #ifdef RTLD_TCB_HAS_SELF_POINTER |
| 156 | tcb->tcb_self = tcb; | |
| 157 | #endif | |
| 158 | tcb->tcb_dtv = dtv; | |
| b00401f0 | 159 | |
| a1eee96a MD |
160 | /* |
| 161 | * Dummy-up the module array. A static binary has only one. This | |
| 162 | * allows us to support the generic __tls_get_addr compiler ABI | |
| 163 | * function. However, if there is no RTLD linked in, nothing in | |
| 164 | * the program should ever call __tls_get_addr (and our version | |
| 165 | * of it doesn't do anything). | |
| 166 | */ | |
| b00401f0 DX |
167 | dtv[0] = 1; |
| 168 | dtv[1] = 1; | |
| bc633d63 | 169 | dtv[2] = (Elf_Addr)((char *)tcb - tls_static_space); |
| b00401f0 | 170 | |
| a1eee96a MD |
171 | memcpy((char *)tcb - tls_static_space, |
| 172 | tls_init, tls_init_size); | |
| 173 | memset((char *)tcb - tls_static_space + tls_init_size, | |
| 174 | 0, tls_static_space - tls_init_size); | |
| 175 | ||
| 176 | /* | |
| 177 | * Activate the initial TCB | |
| 178 | */ | |
| 179 | if (initial_tcb == NULL) { | |
| 180 | initial_tcb = tcb; | |
| 181 | tls_set_tcb(tcb); | |
| b00401f0 | 182 | } |
| a1eee96a | 183 | return (tcb); |
| 92df6c3e DX |
184 | } |
| 185 | ||
| b00401f0 | 186 | #else |
| 92df6c3e | 187 | |
| bc633d63 | 188 | struct tls_tcb * |
| a1eee96a | 189 | __libc_allocate_tls(void) |
| 92df6c3e | 190 | { |
| a1eee96a | 191 | return (NULL); |
| b00401f0 DX |
192 | } |
| 193 | ||
| 92df6c3e | 194 | void |
| f20fd431 | 195 | __libc_free_tls(struct tls_tcb *tcb __unused) |
| 92df6c3e DX |
196 | { |
| 197 | } | |
| 198 | ||
| 199 | #endif /* PIC */ | |
| b00401f0 | 200 | |
| a1eee96a MD |
201 | void |
| 202 | __libc_call_init(void) | |
| 203 | { | |
| 204 | } | |
| 205 | ||
| b00401f0 DX |
206 | extern char **environ; |
| 207 | ||
| a1eee96a MD |
208 | struct tls_tcb * |
| 209 | _libc_init_tls(void) | |
| b00401f0 | 210 | { |
| a1eee96a MD |
211 | struct tls_tcb *tcb; |
| 212 | ||
| b00401f0 | 213 | #ifndef PIC |
| a1eee96a MD |
214 | /* |
| 215 | * If this is a static binary there is no RTLD and so we have not | |
| 216 | * yet calculated the static space requirement. Do so now. | |
| 217 | */ | |
| b00401f0 DX |
218 | Elf_Addr *sp; |
| 219 | Elf_Auxinfo *aux, *auxp; | |
| 220 | Elf_Phdr *phdr; | |
| 221 | size_t phent, phnum; | |
| 222 | int i; | |
| b00401f0 DX |
223 | |
| 224 | sp = (Elf_Addr *) environ; | |
| 225 | while (*sp++ != 0) | |
| 226 | ; | |
| 227 | aux = (Elf_Auxinfo *) sp; | |
| 678e8cc6 | 228 | phdr = NULL; |
| b00401f0 DX |
229 | phent = phnum = 0; |
| 230 | for (auxp = aux; auxp->a_type != AT_NULL; auxp++) { | |
| 231 | switch (auxp->a_type) { | |
| 232 | case AT_PHDR: | |
| 233 | phdr = auxp->a_un.a_ptr; | |
| 234 | break; | |
| 235 | ||
| 236 | case AT_PHENT: | |
| 237 | phent = auxp->a_un.a_val; | |
| 238 | break; | |
| 239 | ||
| 240 | case AT_PHNUM: | |
| 241 | phnum = auxp->a_un.a_val; | |
| 242 | break; | |
| 243 | } | |
| 244 | } | |
| 678e8cc6 | 245 | if (phdr == NULL || phent != sizeof(Elf_Phdr) || phnum == 0) |
| b0e25abd | 246 | return(NULL); |
| b00401f0 DX |
247 | |
| 248 | for (i = 0; (unsigned)i < phnum; i++) { | |
| 249 | if (phdr[i].p_type == PT_TLS) { | |
| b00401f0 DX |
250 | tls_static_space = round(phdr[i].p_memsz, |
| 251 | phdr[i].p_align); | |
| b00401f0 DX |
252 | tls_init_size = phdr[i].p_filesz; |
| 253 | tls_init = (void*) phdr[i].p_vaddr; | |
| 254 | } | |
| 255 | } | |
| a1eee96a | 256 | #endif |
| b00401f0 | 257 | |
| a1eee96a MD |
258 | /* |
| 259 | * Allocate the initial TLS segment. The TLS has not been set up | |
| 260 | * yet for either the static or dynamic linked case (RTLD no longer | |
| 261 | * sets up an initial TLS segment for us). | |
| 262 | */ | |
| 263 | tcb = _libc_allocate_tls(); | |
| 7d6ff7ad | 264 | tls_set_tcb(tcb); |
| a1eee96a MD |
265 | return(tcb); |
| 266 | } | |
| 267 | ||
| 268 | /* | |
| 269 | * Allocate a standard TLS. This function is called by libc and by | |
| 270 | * thread libraries to create a new TCB with libc-related fields properly | |
| 271 | * initialized (whereas _rtld_allocate_tls() is unable to completely set | |
| 272 | * up the TCB). | |
| 273 | * | |
| 274 | * Note that this is different from __libc_allocate_tls which is the | |
| 275 | * weakly bound symbol that handles the case where _rtld_allocate_tls | |
| 276 | * does not exist. | |
| 277 | */ | |
| 278 | struct tls_tcb * | |
| 279 | _libc_allocate_tls(void) | |
| 280 | { | |
| 281 | struct tls_tcb *tcb; | |
| 282 | ||
| 283 | tcb = _rtld_allocate_tls(); | |
| 284 | ||
| 285 | #if 0 | |
| 286 | #if defined(__thread) | |
| 287 | /* non-TLS libc */ | |
| 288 | tcb->tcb_errno_p = &errno; | |
| 289 | #elif defined(PIC) | |
| 290 | /* TLS libc dynamically linked */ | |
| 291 | tcb->tcb_errno_p = __tls_get_addr_tcb(tcb, __get_errno_GOT_ptr()); | |
| 292 | #else | |
| 293 | /* TLS libc (threaded or unthreaded) */ | |
| 294 | tcb->tcb_errno_p = (void *)((char *)tcb + __get_errno_GS_offset()); | |
| b00401f0 | 295 | #endif |
| a1eee96a MD |
296 | #endif |
| 297 | return(tcb); | |
| b00401f0 | 298 | } |
| a1eee96a | 299 |