2 * Copyright (c) 2004 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/lib/libc/gen/tls.c,v 1.7 2005/03/01 23:42:00 davidxu Exp $
27 * $DragonFly: src/lib/libc/gen/tls.c,v 1.1 2005/03/08 13:04:38 davidxu Exp $
31 * Define stubs for TLS internals so that programs and libraries can
32 * link. These functions will be replaced by functional versions at
33 * runtime from ld-elf.so.1.
40 #include "libc_private.h"
42 /* XXX not sure what variants to use for arm. */
44 #if defined(__ia64__) || defined(__powerpc__)
47 #if defined(__i386__) || defined(__amd64__) || defined(__sparc64__) || \
49 #define TLS_VARIANT_II
54 #define round(size, align) \
55 (((size) + (align) - 1) & ~((align) - 1))
57 static size_t tls_static_space;
58 static size_t tls_init_size;
60 static size_t tls_init_offset;
62 static void *tls_init;
65 void *_rtld_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign);
66 void _rtld_free_tls(void *tcb, size_t tcbsize, size_t tcbalign);
67 void *__tls_get_addr(void *);
71 extern void *___tls_get_addr(void *ti) __attribute__((__regparm__(1)));
73 #pragma weak ___tls_get_addr
74 __attribute__((__regparm__(1)))
76 ___tls_get_addr(void *ti __unused)
83 #pragma weak __tls_get_addr
85 __tls_get_addr(void *ti __unused)
92 #pragma weak _rtld_free_tls
94 * Free Static TLS using the Variant I method.
97 _rtld_free_tls(void *tls, size_t tcbsize, size_t tcbalign)
102 dtv = ((Elf_Addr**)tls)[0];
108 #pragma weak _rtld_allocate_tls
110 * Allocate Static TLS using the Variant I method.
113 _rtld_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign)
120 size = tls_static_space;
125 dtv = malloc(3 * sizeof(Elf_Addr));
127 *(Elf_Addr**) tls = dtv;
131 dtv[2] = (Elf_Addr)(tls + tls_init_offset);
134 * Copy the static TLS block over whole.
136 memcpy(tls + tls_init_offset,
137 (char*) oldtls + tls_init_offset,
138 tls_static_space - tls_init_offset);
141 * We assume that this block was the one we created with
142 * allocate_initial_tls().
144 _rtld_free_tls(oldtls, 2*sizeof(Elf_Addr), sizeof(Elf_Addr));
146 memcpy(tls + tls_init_offset, tls_init, tls_init_size);
147 memset(tls + tls_init_offset + tls_init_size,
148 0, tls_static_space - tls_init_size);
159 #ifdef TLS_VARIANT_II
162 * Free Static TLS using the Variant II method.
164 #pragma weak _rtld_free_tls
166 _rtld_free_tls(void *tcb, size_t tcbsize, size_t tcbalign)
171 Elf_Addr tlsstart, tlsend;
174 * Figure out the size of the initial TLS block so that we can
175 * find stuff which ___tls_get_addr() allocated dynamically.
177 size = round(tls_static_space, tcbalign);
179 dtv = ((Elf_Addr**)tcb)[1];
180 tlsend = (Elf_Addr) tcb;
181 tlsstart = tlsend - size;
182 free((void*) tlsstart);
187 #pragma weak _rtld_allocate_tls
189 * Allocate Static TLS using the Variant II method.
192 _rtld_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign)
198 Elf_Addr segbase, oldsegbase;
200 size = round(tls_static_space, tcbalign);
202 assert(tcbsize >= 2*sizeof(Elf_Addr));
203 tls = malloc(size + tcbsize);
204 dtv = malloc(3 * sizeof(Elf_Addr));
206 segbase = (Elf_Addr)(tls + size);
207 ((Elf_Addr*)segbase)[0] = segbase;
208 ((Elf_Addr*)segbase)[1] = (Elf_Addr) dtv;
212 dtv[2] = segbase - tls_static_space;
216 * Copy the static TLS block over whole.
218 oldsegbase = (Elf_Addr) oldtls;
219 memcpy((void *)(segbase - tls_static_space),
220 (const void *)(oldsegbase - tls_static_space),
224 * We assume that this block was the one we created with
225 * allocate_initial_tls().
227 _rtld_free_tls(oldtls, 2*sizeof(Elf_Addr), sizeof(Elf_Addr));
229 memcpy((void *)(segbase - tls_static_space),
230 tls_init, tls_init_size);
231 memset((void *)(segbase - tls_static_space + tls_init_size),
232 0, tls_static_space - tls_init_size);
235 return (void*) segbase;
241 #endif /* TLS_VARIANT_II */
243 extern char **environ;
250 Elf_Auxinfo *aux, *auxp;
256 sp = (Elf_Addr *) environ;
259 aux = (Elf_Auxinfo *) sp;
262 for (auxp = aux; auxp->a_type != AT_NULL; auxp++) {
263 switch (auxp->a_type) {
265 phdr = auxp->a_un.a_ptr;
269 phent = auxp->a_un.a_val;
273 phnum = auxp->a_un.a_val;
277 if (phdr == 0 || phent != sizeof(Elf_Phdr) || phnum == 0)
280 for (i = 0; (unsigned)i < phnum; i++) {
281 if (phdr[i].p_type == PT_TLS) {
283 tls_static_space = round(2*sizeof(Elf_Addr),
284 phdr[i].p_align) + phdr[i].p_memsz;
285 tls_init_offset = round(2*sizeof(Elf_Addr),
288 tls_static_space = round(phdr[i].p_memsz,
291 tls_init_size = phdr[i].p_filesz;
292 tls_init = (void*) phdr[i].p_vaddr;
296 tls = _rtld_allocate_tls(NULL, 2*sizeof(Elf_Addr),
299 _set_tp(tls, 2 * sizeof(Elf_Addr));