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$ 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 <stdlib.h> 36 #include <string.h> 37 #include <elf.h> 38 #include <assert.h> 39 #include "libc_private.h" 40 41 /* XXX not sure what variants to use for arm. */ 42 43 #if defined(__ia64__) || defined(__alpha__) || defined(__powerpc__) 44 #define TLS_VARIANT_I 45 #endif 46 #if defined(__i386__) || defined(__amd64__) || defined(__sparc64__) || \ 47 defined(__arm__) 48 #define TLS_VARIANT_II 49 #endif 50 51 #ifndef PIC 52 53 #define round(size, align) \ 54 (((size) + (align) - 1) & ~((align) - 1)) 55 56 static size_t tls_static_space; 57 static size_t tls_init_size; 58 #ifdef TLS_VARIANT_I 59 static size_t tls_init_offset; 60 #endif 61 static void *tls_init; 62 63 void _rtld_free_tls(void *tcb, size_t tcbsize, size_t tcbalign); 64 #endif 65 66 #ifdef __i386__ 67 68 extern void *___tls_get_addr(void *ti) __attribute__((__regparm__(1))); 69 70 #pragma weak ___tls_get_addr 71 __attribute__((__regparm__(1))) 72 void * 73 ___tls_get_addr(void *ti) 74 { 75 return (0); 76 } 77 78 #endif 79 80 #pragma weak __tls_get_addr 81 void * 82 __tls_get_addr() 83 { 84 return (0); 85 } 86 87 #ifdef TLS_VARIANT_I 88 89 void 90 _rtld_free_tls(void *tls, size_t tcbsize, size_t tcbalign) 91 { 92 #ifndef PIC 93 Elf_Addr* dtv; 94 95 dtv = ((Elf_Addr**)tls)[0]; 96 free(tls); 97 free(dtv); 98 #endif 99 } 100 101 /* 102 * Allocate Static TLS using the Variant I method. 103 */ 104 void * 105 _rtld_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign) 106 { 107 #ifndef PIC 108 size_t size; 109 char *tls; 110 Elf_Addr *dtv; 111 112 size = tls_static_space; 113 if (size < tcbsize) 114 size = tcbsize; 115 116 tls = malloc(size); 117 dtv = malloc(3 * sizeof(Elf_Addr)); 118 119 *(Elf_Addr**) tls = dtv; 120 121 dtv[0] = 1; 122 dtv[1] = 1; 123 dtv[2] = (Elf_Addr)(tls + tls_init_offset); 124 if (oldtls) { 125 /* 126 * Copy the static TLS block over whole. 127 */ 128 memcpy(tls + tls_init_offset, 129 (char*) oldtls + tls_init_offset, 130 tls_static_space - tls_init_offset); 131 132 /* 133 * We assume that this block was the one we created with 134 * allocate_initial_tls(). 135 */ 136 _rtld_free_tls(oldtls, 2*sizeof(Elf_Addr), sizeof(Elf_Addr)); 137 } else { 138 memcpy(tls + tls_init_offset, tls_init, tls_init_size); 139 memset(tls + tls_init_offset + tls_init_size, 140 0, tls_static_space - tls_init_size); 141 } 142 143 return tls; 144 #else 145 return (0); 146 #endif 147 } 148 149 #endif 150 151 #ifdef TLS_VARIANT_II 152 153 /* 154 * Free Static TLS using the Variant II method. 155 */ 156 #pragma weak _rtld_free_tls 157 void 158 _rtld_free_tls(void *tcb, size_t tcbsize, size_t tcbalign) 159 { 160 #ifndef PIC 161 size_t size; 162 Elf_Addr* dtv; 163 Elf_Addr tlsstart, tlsend; 164 165 /* 166 * Figure out the size of the initial TLS block so that we can 167 * find stuff which ___tls_get_addr() allocated dynamically. 168 */ 169 size = round(tls_static_space, tcbalign); 170 171 dtv = ((Elf_Addr**)tcb)[1]; 172 tlsend = (Elf_Addr) tcb; 173 tlsstart = tlsend - size; 174 free((void*) tlsstart); 175 free(dtv); 176 #endif 177 } 178 179 #pragma weak _rtld_allocate_tls 180 /* 181 * Allocate Static TLS using the Variant II method. 182 */ 183 void * 184 _rtld_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign) 185 { 186 #ifndef PIC 187 size_t size; 188 char *tls; 189 Elf_Addr *dtv; 190 Elf_Addr segbase, oldsegbase; 191 192 size = round(tls_static_space, tcbalign); 193 194 assert(tcbsize >= 2*sizeof(Elf_Addr)); 195 tls = malloc(size + tcbsize); 196 dtv = malloc(3 * sizeof(Elf_Addr)); 197 198 segbase = (Elf_Addr)(tls + size); 199 ((Elf_Addr*)segbase)[0] = segbase; 200 ((Elf_Addr*)segbase)[1] = (Elf_Addr) dtv; 201 202 dtv[0] = 1; 203 dtv[1] = 1; 204 dtv[2] = segbase - tls_static_space; 205 206 if (oldtls) { 207 /* 208 * Copy the static TLS block over whole. 209 */ 210 oldsegbase = (Elf_Addr) oldtls; 211 memcpy((void *)(segbase - tls_static_space), 212 (const void *)(oldsegbase - tls_static_space), 213 tls_static_space); 214 215 /* 216 * We assume that this block was the one we created with 217 * allocate_initial_tls(). 218 */ 219 _rtld_free_tls(oldtls, 2*sizeof(Elf_Addr), sizeof(Elf_Addr)); 220 } else { 221 memcpy((void *)(segbase - tls_static_space), 222 tls_init, tls_init_size); 223 memset((void *)(segbase - tls_static_space + tls_init_size), 224 0, tls_static_space - tls_init_size); 225 } 226 227 return (void*) segbase; 228 #else 229 return (0); 230 #endif 231 } 232 233 #endif 234 235 void 236 _init_tls() 237 { 238 #ifndef PIC 239 extern char **environ; 240 Elf_Addr *sp; 241 Elf_Auxinfo *aux, *auxp; 242 Elf_Phdr *phdr; 243 size_t phent, phnum; 244 int i; 245 void *tls; 246 247 sp = (Elf_Addr *) environ; 248 while (*sp++ != 0) 249 ; 250 aux = (Elf_Auxinfo *) sp; 251 phdr = 0; 252 phent = phnum = 0; 253 for (auxp = aux; auxp->a_type != AT_NULL; auxp++) { 254 switch (auxp->a_type) { 255 case AT_PHDR: 256 phdr = auxp->a_un.a_ptr; 257 break; 258 259 case AT_PHENT: 260 phent = auxp->a_un.a_val; 261 break; 262 263 case AT_PHNUM: 264 phnum = auxp->a_un.a_val; 265 break; 266 } 267 } 268 if (phdr == 0 || phent != sizeof(Elf_Phdr) || phnum == 0) 269 return; 270 271 for (i = 0; i < phnum; i++) { 272 if (phdr[i].p_type == PT_TLS) { 273 #ifdef TLS_VARIANT_I 274 tls_static_space = round(2*sizeof(Elf_Addr), 275 phdr[i].p_align) + phdr[i].p_memsz; 276 tls_init_offset = round(2*sizeof(Elf_Addr), 277 phdr[i].p_align); 278 #else 279 tls_static_space = round(phdr[i].p_memsz, 280 phdr[i].p_align); 281 #endif 282 tls_init_size = phdr[i].p_filesz; 283 tls_init = (void*) phdr[i].p_vaddr; 284 } 285 } 286 287 tls = _rtld_allocate_tls(NULL, 2*sizeof(Elf_Addr), 288 sizeof(Elf_Addr)); 289 290 _set_tp(tls); 291 #endif 292 } 293