Always allocate static TLS space.
[dragonfly.git] / lib / libc / gen / tls.c
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: 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.7 2005/04/28 18:29:54 joerg Exp $
28  */
29
30 /*
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.
34  */
35
36 #include <sys/cdefs.h>
37 #include <sys/tls.h>
38
39 #include <machine/tls.h>
40
41 #include <stdlib.h>
42 #include <string.h>
43 #include <elf.h>
44 #include <assert.h>
45
46 #include "libc_private.h"
47
48 __weak_reference(__libc_allocate_tls, _rtld_allocate_tls);
49 __weak_reference(__libc_free_tls, _rtld_free_tls);
50 #ifdef __i386__
51 __weak_reference(___libc_tls_get_addr, ___tls_get_addr);
52 #endif
53 __weak_reference(__libc_tls_get_addr, __tls_get_addr);
54
55 struct tls_tcb *__libc_allocate_tls(struct tls_tcb *old_tcb);
56 void __libc_free_tls(struct tls_tcb *tcb);
57
58 #if !defined(RTLD_STATIC_TLS_VARIANT_II)
59 #error "Unsupported TLS layout"
60 #endif
61
62 #ifndef PIC
63
64 #define round(size, align) \
65         (((size) + (align) - 1) & ~((align) - 1))
66
67 static size_t tls_static_space;
68 static size_t tls_init_size;
69 static void *tls_init;
70 #endif
71
72 #ifdef __i386__
73
74 /* GNU ABI */
75
76 void *___libc_tls_get_addr(void *ti) __attribute__((__regparm__(1)));
77
78 __attribute__((__regparm__(1)))
79 void *
80 ___libc_tls_get_addr(void *ti __unused)
81 {
82         return (0);
83 }
84
85 #endif
86
87 void *__libc_tls_get_addr(void *ti);
88
89 void *
90 __libc_tls_get_addr(void *ti __unused)
91 {
92         return (0);
93 }
94
95 #ifndef PIC
96
97 /*
98  * Free Static TLS
99  */
100 void
101 __libc_free_tls(struct tls_tcb *tcb)
102 {
103         size_t data_size;
104
105         if (tcb->tcb_dtv)
106                 free(tcb->tcb_dtv);
107         data_size = (tls_static_space + RTLD_STATIC_TLS_ALIGN_MASK) &
108                     ~RTLD_STATIC_TLS_ALIGN_MASK;
109         free((char *)tcb - data_size);
110 }
111
112 /*
113  * Allocate Static TLS.
114  */
115 struct tls_tcb *
116 __libc_allocate_tls(struct tls_tcb *old_tcb)
117 {
118         size_t data_size;
119         struct tls_tcb *tcb;
120         Elf_Addr *dtv;
121
122         data_size = (tls_static_space + RTLD_STATIC_TLS_ALIGN_MASK) &
123                     ~RTLD_STATIC_TLS_ALIGN_MASK;
124         tcb = malloc(data_size + sizeof(*tcb));
125         tcb = (struct tls_tcb *)((char *)tcb + data_size);
126         dtv = malloc(3 * sizeof(Elf_Addr));
127
128 #ifdef RTLD_TCB_HAS_SELF_POINTER
129         tcb->tcb_self = tcb;
130 #endif
131         tcb->tcb_dtv = dtv;
132         tcb->tcb_pthread = NULL;
133
134         dtv[0] = 1;
135         dtv[1] = 1;
136         dtv[2] = (Elf_Addr)((char *)tcb - tls_static_space);
137
138         if (old_tcb) {
139                 /*
140                  * Copy the static TLS block over whole.
141                  */
142                 memcpy((char *)tcb - tls_static_space,
143                         (char *)old_tcb - tls_static_space,
144                         tls_static_space);
145
146                 /*
147                  * We assume that this block was the one we created with
148                  * allocate_initial_tls().
149                  */
150                 _rtld_free_tls(old_tcb);
151         } else {
152                 memcpy((char *)tcb - tls_static_space,
153                         tls_init, tls_init_size);
154                 memset((char *)tcb - tls_static_space + tls_init_size,
155                         0, tls_static_space - tls_init_size);
156         }
157         return (tcb);
158 }
159
160 #else
161
162 struct tls_tcb *
163 __libc_allocate_tls(struct tls_tcb *old_tls __unused)
164 {
165         return (0);
166 }
167
168 void
169 __libc_free_tls(struct tls_tcb *tcb __unused)
170 {
171 }
172
173 #endif /* PIC */
174
175 extern char **environ;
176
177 void
178 _init_tls()
179 {
180 #ifndef PIC
181         Elf_Addr *sp;
182         Elf_Auxinfo *aux, *auxp;
183         Elf_Phdr *phdr;
184         size_t phent, phnum;
185         int i;
186         struct tls_tcb *tcb;
187
188         sp = (Elf_Addr *) environ;
189         while (*sp++ != 0)
190                 ;
191         aux = (Elf_Auxinfo *) sp;
192         phdr = 0;
193         phent = phnum = 0;
194         for (auxp = aux; auxp->a_type != AT_NULL; auxp++) {
195                 switch (auxp->a_type) {
196                 case AT_PHDR:
197                         phdr = auxp->a_un.a_ptr;
198                         break;
199
200                 case AT_PHENT:
201                         phent = auxp->a_un.a_val;
202                         break;
203
204                 case AT_PHNUM:
205                         phnum = auxp->a_un.a_val;
206                         break;
207                 }
208         }
209         if (phdr == 0 || phent != sizeof(Elf_Phdr) || phnum == 0)
210                 return;
211
212         for (i = 0; (unsigned)i < phnum; i++) {
213                 if (phdr[i].p_type == PT_TLS) {
214                         tls_static_space = round(phdr[i].p_memsz,
215                             phdr[i].p_align);
216                         tls_init_size = phdr[i].p_filesz;
217                         tls_init = (void*) phdr[i].p_vaddr;
218                 }
219         }
220
221         tcb = _rtld_allocate_tls(NULL);
222         tls_set_tcb(tcb);
223 #endif
224 }