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.3 2005/03/20 14:57:28 joerg 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.
36 #include <sys/cdefs.h>
42 #include "libc_private.h"
44 __weak_reference(__libc_allocate_tls, _rtld_allocate_tls);
45 __weak_reference(__libc_free_tls, _rtld_free_tls);
47 __weak_reference(___libc_tls_get_addr, ___tls_get_addr);
49 __weak_reference(__libc_tls_get_addr, __tls_get_addr);
51 void *_rtld_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign);
52 void _rtld_free_tls(void *tls, size_t tcbsize, size_t tcbalign);
53 void *__libc_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign);
54 void __libc_free_tls(void *tls, size_t tcbsize, size_t tcbalign);
56 #if defined(__ia64__) || defined(__powerpc__)
59 #if defined(__i386__) || defined(__amd64__) || defined(__sparc64__) || \
61 #define TLS_VARIANT_II
66 #define round(size, align) \
67 (((size) + (align) - 1) & ~((align) - 1))
69 static size_t tls_static_space;
70 static size_t tls_init_size;
72 static size_t tls_init_offset;
74 static void *tls_init;
81 void *___libc_tls_get_addr(void *ti) __attribute__((__regparm__(1)));
83 __attribute__((__regparm__(1)))
85 ___libc_tls_get_addr(void *ti __unused)
92 void *__libc_tls_get_addr(void *ti);
94 __libc_tls_get_addr(void *ti __unused)
104 * Free Static TLS using the Variant I method.
107 __libc_free_tls(void *tls, size_t tcbsize __unused, size_t tcbalign __unused)
111 dtv = *(Elf_Addr **)tls;
117 * Allocate Static TLS using the Variant I method.
120 __libc_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign __unused)
126 size = tls_static_space;
131 dtv = malloc(3 * sizeof(Elf_Addr));
133 *(Elf_Addr **) tls = dtv;
137 dtv[2] = (Elf_Addr)(tls + tls_init_offset);
140 * Copy the static TLS block over whole.
142 memcpy(tls + tls_init_offset,
143 (const uint8_t *) oldtls + tls_init_offset,
144 tls_static_space - tls_init_offset);
147 * We assume that this block was the one we created with
148 * allocate_initial_tls().
150 _rtld_free_tls(oldtls, 2 * sizeof(Elf_Addr), sizeof(Elf_Addr));
152 memcpy(tls + tls_init_offset, tls_init, tls_init_size);
153 memset(tls + tls_init_offset + tls_init_size,
154 0, tls_static_space - tls_init_size);
162 #ifdef TLS_VARIANT_II
165 * Free Static TLS using the Variant II method.
168 __libc_free_tls(void *tcb, size_t tcbsize __unused, size_t tcbalign)
172 Elf_Addr tlsstart, tlsend;
175 * Figure out the size of the initial TLS block so that we can
176 * find stuff which ___tls_get_addr() allocated dynamically.
178 size = round(tls_static_space, tcbalign);
180 dtv = ((Elf_Addr**)tcb)[1];
181 tlsend = (Elf_Addr) tcb;
182 tlsstart = tlsend - size;
183 free((void*) tlsstart);
188 * Allocate Static TLS using the Variant II method.
191 __libc_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign)
196 Elf_Addr segbase, oldsegbase;
198 size = round(tls_static_space, tcbalign);
200 assert(tcbsize >= 2*sizeof(Elf_Addr));
201 tls = malloc(size + tcbsize);
202 dtv = malloc(3 * sizeof(Elf_Addr));
204 segbase = (Elf_Addr)(tls + size);
205 ((Elf_Addr*)segbase)[0] = segbase;
206 ((Elf_Addr*)segbase)[1] = (Elf_Addr) dtv;
210 dtv[2] = segbase - tls_static_space;
214 * Copy the static TLS block over whole.
216 oldsegbase = (Elf_Addr) oldtls;
217 memcpy((void *)(segbase - tls_static_space),
218 (const void *)(oldsegbase - tls_static_space),
222 * We assume that this block was the one we created with
223 * allocate_initial_tls().
225 _rtld_free_tls(oldtls, 2*sizeof(Elf_Addr), sizeof(Elf_Addr));
227 memcpy((void *)(segbase - tls_static_space),
228 tls_init, tls_init_size);
229 memset((void *)(segbase - tls_static_space + tls_init_size),
230 0, tls_static_space - tls_init_size);
233 return (void*) segbase;
236 #endif /* TLS_VARIANT_II */
241 __libc_allocate_tls(void *oldtls __unused, size_t tcbsize __unused,
242 size_t tcbalign __unused)
248 __libc_free_tls(void *tcb __unused, size_t tcbsize __unused,
249 size_t tcbalign __unused)
255 extern char **environ;
262 Elf_Auxinfo *aux, *auxp;
268 sp = (Elf_Addr *) environ;
271 aux = (Elf_Auxinfo *) sp;
274 for (auxp = aux; auxp->a_type != AT_NULL; auxp++) {
275 switch (auxp->a_type) {
277 phdr = auxp->a_un.a_ptr;
281 phent = auxp->a_un.a_val;
285 phnum = auxp->a_un.a_val;
289 if (phdr == 0 || phent != sizeof(Elf_Phdr) || phnum == 0)
292 for (i = 0; (unsigned)i < phnum; i++) {
293 if (phdr[i].p_type == PT_TLS) {
295 tls_static_space = round(2*sizeof(Elf_Addr),
296 phdr[i].p_align) + phdr[i].p_memsz;
297 tls_init_offset = round(2*sizeof(Elf_Addr),
300 tls_static_space = round(phdr[i].p_memsz,
303 tls_init_size = phdr[i].p_filesz;
304 tls_init = (void*) phdr[i].p_vaddr;
308 tls = _rtld_allocate_tls(NULL, 2*sizeof(Elf_Addr),
311 _set_tp(tls, 2 * sizeof(Elf_Addr));