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