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
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.
35 #include <sys/cdefs.h>
40 #include "libc_private.h"
42 /* Provided by jemalloc to avoid bootstrapping issues. */
43 void *__jemalloc_a0malloc(size_t size);
44 void *__jemalloc_a0calloc(size_t num, size_t size);
45 void __jemalloc_a0free(void *ptr);
47 __weak_reference(__libc_allocate_tls, _rtld_allocate_tls);
48 __weak_reference(__libc_free_tls, _rtld_free_tls);
52 __weak_reference(___libc_tls_get_addr, ___tls_get_addr);
53 __attribute__((__regparm__(1))) void * ___libc_tls_get_addr(void *);
57 void * __libc_tls_get_addr(void *);
58 __weak_reference(__libc_tls_get_addr, __tls_get_addr);
60 void *_rtld_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign);
61 void _rtld_free_tls(void *tls, size_t tcbsize, size_t tcbalign);
62 void *__libc_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign);
63 void __libc_free_tls(void *tls, size_t tcbsize, size_t tcbalign);
65 #if defined(__ia64__) || defined(__amd64__)
66 #define TLS_TCB_ALIGN 16
67 #elif defined(__powerpc__) || defined(__i386__) || defined(__arm__) || \
68 defined(__sparc64__) || defined(__mips__)
69 #define TLS_TCB_ALIGN sizeof(void *)
71 #error TLS_TCB_ALIGN undefined for target architecture
74 #if defined(__arm__) || defined(__ia64__) || defined(__mips__) || \
78 #if defined(__i386__) || defined(__amd64__) || defined(__sparc64__)
79 #define TLS_VARIANT_II
84 #define round(size, align) \
85 (((size) + (align) - 1) & ~((align) - 1))
87 static size_t tls_static_space;
88 static size_t tls_init_size;
89 static void *tls_init;
96 __attribute__((__regparm__(1)))
98 ___libc_tls_get_addr(void *ti __unused)
106 __libc_tls_get_addr(void *ti __unused)
115 #define TLS_TCB_SIZE (2 * sizeof(void *))
118 * Free Static TLS using the Variant I method.
121 __libc_free_tls(void *tcb, size_t tcbsize, size_t tcbalign __unused)
126 tls = (Elf_Addr **)((Elf_Addr)tcb + tcbsize - TLS_TCB_SIZE);
128 __jemalloc_a0free(dtv);
129 __jemalloc_a0free(tcb);
133 * Allocate Static TLS using the Variant I method.
136 __libc_allocate_tls(void *oldtcb, size_t tcbsize, size_t tcbalign __unused)
142 if (oldtcb != NULL && tcbsize == TLS_TCB_SIZE)
145 tcb = __jemalloc_a0calloc(1, tls_static_space + tcbsize - TLS_TCB_SIZE);
146 tls = (Elf_Addr **)(tcb + tcbsize - TLS_TCB_SIZE);
148 if (oldtcb != NULL) {
149 memcpy(tls, oldtcb, tls_static_space);
150 __jemalloc_a0free(oldtcb);
152 /* Adjust the DTV. */
154 dtv[2] = (Elf_Addr)tls + TLS_TCB_SIZE;
156 dtv = __jemalloc_a0malloc(3 * sizeof(Elf_Addr));
160 dtv[2] = (Elf_Addr)tls + TLS_TCB_SIZE;
162 if (tls_init_size > 0)
163 memcpy((void*)dtv[2], tls_init, tls_init_size);
171 #ifdef TLS_VARIANT_II
173 #define TLS_TCB_SIZE (3 * sizeof(Elf_Addr))
176 * Free Static TLS using the Variant II method.
179 __libc_free_tls(void *tcb, size_t tcbsize __unused, size_t tcbalign)
183 Elf_Addr tlsstart, tlsend;
186 * Figure out the size of the initial TLS block so that we can
187 * find stuff which ___tls_get_addr() allocated dynamically.
189 size = round(tls_static_space, tcbalign);
191 dtv = ((Elf_Addr**)tcb)[1];
192 tlsend = (Elf_Addr) tcb;
193 tlsstart = tlsend - size;
194 __jemalloc_a0free((void*) tlsstart);
195 __jemalloc_a0free(dtv);
199 * Allocate Static TLS using the Variant II method.
202 __libc_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign)
207 Elf_Addr segbase, oldsegbase;
209 size = round(tls_static_space, tcbalign);
211 if (tcbsize < 2 * sizeof(Elf_Addr))
212 tcbsize = 2 * sizeof(Elf_Addr);
213 tls = __jemalloc_a0calloc(1, size + tcbsize);
214 dtv = __jemalloc_a0malloc(3 * sizeof(Elf_Addr));
216 segbase = (Elf_Addr)(tls + size);
217 ((Elf_Addr*)segbase)[0] = segbase;
218 ((Elf_Addr*)segbase)[1] = (Elf_Addr) dtv;
222 dtv[2] = segbase - tls_static_space;
226 * Copy the static TLS block over whole.
228 oldsegbase = (Elf_Addr) oldtls;
229 memcpy((void *)(segbase - tls_static_space),
230 (const void *)(oldsegbase - tls_static_space),
234 * We assume that this block was the one we created with
235 * allocate_initial_tls().
237 _rtld_free_tls(oldtls, 2*sizeof(Elf_Addr), sizeof(Elf_Addr));
239 memcpy((void *)(segbase - tls_static_space),
240 tls_init, tls_init_size);
241 memset((void *)(segbase - tls_static_space + tls_init_size),
242 0, tls_static_space - tls_init_size);
245 return (void*) segbase;
248 #endif /* TLS_VARIANT_II */
253 __libc_allocate_tls(void *oldtls __unused, size_t tcbsize __unused,
254 size_t tcbalign __unused)
260 __libc_free_tls(void *tcb __unused, size_t tcbsize __unused,
261 size_t tcbalign __unused)
267 extern char **environ;
274 Elf_Auxinfo *aux, *auxp;
280 sp = (Elf_Addr *) environ;
283 aux = (Elf_Auxinfo *) sp;
286 for (auxp = aux; auxp->a_type != AT_NULL; auxp++) {
287 switch (auxp->a_type) {
289 phdr = auxp->a_un.a_ptr;
293 phent = auxp->a_un.a_val;
297 phnum = auxp->a_un.a_val;
301 if (phdr == NULL || phent != sizeof(Elf_Phdr) || phnum == 0)
304 for (i = 0; (unsigned) i < phnum; i++) {
305 if (phdr[i].p_type == PT_TLS) {
306 tls_static_space = round(phdr[i].p_memsz,
308 tls_init_size = phdr[i].p_filesz;
309 tls_init = (void*) phdr[i].p_vaddr;
315 * tls_static_space should include space for TLS structure
317 tls_static_space += TLS_TCB_SIZE;
320 tls = _rtld_allocate_tls(NULL, TLS_TCB_SIZE, TLS_TCB_ALIGN);