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