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