00867ccb946c475b57cd689fbeef1c95513fb662
[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  */
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 <sys/tls.h>
37
38 #include <machine/tls.h>
39
40 #include <stdlib.h>
41 #include <string.h>
42 #include <elf.h>
43 #include <assert.h>
44 #include <errno.h>
45 #include <unistd.h>
46
47 #include "libc_private.h"
48
49 __weak_reference(__libc_allocate_tls, _rtld_allocate_tls);
50 __weak_reference(__libc_free_tls, _rtld_free_tls);
51 __weak_reference(__libc_call_init, _rtld_call_init);
52 #ifdef __i386__
53 __weak_reference(___libc_tls_get_addr, ___tls_get_addr);
54 #endif
55 __weak_reference(__libc_tls_get_addr, __tls_get_addr);
56 __weak_reference(__libc_tls_get_addr_tcb, __tls_get_addr_tcb);
57 __weak_reference(_libc_init_tls, _init_tls);
58
59 struct tls_tcb *__libc_allocate_tls(void);
60 void __libc_free_tls(struct tls_tcb *tcb);
61
62 #if !defined(RTLD_STATIC_TLS_VARIANT_II)
63 #error "Unsupported TLS layout"
64 #endif
65
66 #ifndef PIC
67
68 #define round(size, align) \
69         (((size) + (align) - 1) & ~((align) - 1))
70
71 static size_t tls_static_space;
72 static size_t tls_init_size;
73 static void *tls_init;
74 static struct tls_tcb *initial_tcb;
75 #endif
76
77 #ifdef __i386__
78
79 /* GNU ABI */
80
81 void *___libc_tls_get_addr(void *ti) __attribute__((__regparm__(1)));
82
83 __attribute__((__regparm__(1)))
84 void *
85 ___libc_tls_get_addr(void *ti __unused)
86 {
87         return (0);
88 }
89
90 #endif
91
92 void *__libc_tls_get_addr(void *ti);
93 void *__libc_tls_get_addr_tcb(struct tls_tcb *, void *);
94
95 void *
96 __libc_tls_get_addr(void *ti __unused)
97 {
98         return (NULL);
99 }
100
101 void *
102 __libc_tls_get_addr_tcb(struct tls_tcb *tcb __unused, void *got_ptr __unused)
103 {
104         return (NULL);
105 }
106
107 #ifndef PIC
108
109 /*
110  * Free Static TLS, weakly bound to _rtld_free_tls()
111  */
112 void
113 __libc_free_tls(struct tls_tcb *tcb)
114 {
115         size_t data_size;
116
117         data_size = (tls_static_space + RTLD_STATIC_TLS_ALIGN_MASK) &
118                     ~RTLD_STATIC_TLS_ALIGN_MASK;
119
120         if (tcb == initial_tcb) {
121                 /* initial_tcb was allocated with sbrk(), cannot call free() */
122         } else {
123                 free((char *)tcb - data_size);
124         }
125 }
126
127 /*
128  * Allocate Static TLS, weakly bound to _rtld_allocate_tls()
129  *
130  * NOTE!  There is a chicken-and-egg problem here because no TLS exists
131  * on the first call into this function. 
132  */
133 struct tls_tcb *
134 __libc_allocate_tls(void)
135 {
136         size_t data_size;
137         struct tls_tcb *tcb;
138         Elf_Addr *dtv;
139
140         data_size = (tls_static_space + RTLD_STATIC_TLS_ALIGN_MASK) &
141                     ~RTLD_STATIC_TLS_ALIGN_MASK;
142
143         /*
144          * Allocate space.  malloc() may require a working TLS segment
145          * so we use sbrk() for main's TLS.
146          */
147         if (initial_tcb == NULL)
148                 tcb = sbrk(data_size + sizeof(*tcb) + 3 * sizeof(*dtv));
149         else
150                 tcb = malloc(data_size + sizeof(*tcb) + 3 * sizeof(*dtv));
151
152         tcb = (struct tls_tcb *)((char *)tcb + data_size);
153         dtv = (Elf_Addr *)(tcb + 1);
154
155         memset(tcb, 0, sizeof(*tcb));
156 #ifdef RTLD_TCB_HAS_SELF_POINTER
157         tcb->tcb_self = tcb;
158 #endif
159         tcb->tcb_dtv = dtv;
160
161         /*
162          * Dummy-up the module array.  A static binary has only one.  This
163          * allows us to support the generic __tls_get_addr compiler ABI
164          * function.  However, if there is no RTLD linked in, nothing in
165          * the program should ever call __tls_get_addr (and our version
166          * of it doesn't do anything).
167          */
168         dtv[0] = 1;
169         dtv[1] = 1;
170         dtv[2] = (Elf_Addr)((char *)tcb - tls_static_space);
171
172         memcpy((char *)tcb - tls_static_space,
173                 tls_init, tls_init_size);
174         memset((char *)tcb - tls_static_space + tls_init_size,
175                 0, tls_static_space - tls_init_size);
176
177         /*
178          * Activate the initial TCB
179          */
180         if (initial_tcb == NULL) {
181                 initial_tcb = tcb;
182                 tls_set_tcb(tcb);
183         }
184         return (tcb);
185 }
186
187 #else
188
189 struct tls_tcb *
190 __libc_allocate_tls(void)
191 {
192         return (NULL);
193 }
194
195 void
196 __libc_free_tls(struct tls_tcb *tcb __unused)
197 {
198 }
199
200 #endif /* PIC */
201
202 void
203 __libc_call_init(void)
204 {
205 }
206
207 extern char **environ;
208
209 struct tls_tcb *
210 _libc_init_tls(void)
211 {
212         struct tls_tcb *tcb;
213
214 #ifndef PIC
215         /*
216          * If this is a static binary there is no RTLD and so we have not
217          * yet calculated the static space requirement.  Do so now.
218          */
219         Elf_Addr *sp;
220         Elf_Auxinfo *aux, *auxp;
221         Elf_Phdr *phdr;
222         size_t phent, phnum;
223         int i;
224
225         sp = (Elf_Addr *) environ;
226         while (*sp++ != 0)
227                 ;
228         aux = (Elf_Auxinfo *) sp;
229         phdr = NULL;
230         phent = phnum = 0;
231         for (auxp = aux; auxp->a_type != AT_NULL; auxp++) {
232                 switch (auxp->a_type) {
233                 case AT_PHDR:
234                         phdr = auxp->a_un.a_ptr;
235                         break;
236
237                 case AT_PHENT:
238                         phent = auxp->a_un.a_val;
239                         break;
240
241                 case AT_PHNUM:
242                         phnum = auxp->a_un.a_val;
243                         break;
244                 }
245         }
246         if (phdr == NULL || phent != sizeof(Elf_Phdr) || phnum == 0)
247                 return(NULL);
248
249         for (i = 0; (unsigned)i < phnum; i++) {
250                 if (phdr[i].p_type == PT_TLS) {
251                         tls_static_space = round(phdr[i].p_memsz,
252                             phdr[i].p_align);
253                         tls_init_size = phdr[i].p_filesz;
254                         tls_init = (void*) phdr[i].p_vaddr;
255                 }
256         }
257 #endif
258
259         /*
260          * Allocate the initial TLS segment.  The TLS has not been set up
261          * yet for either the static or dynamic linked case (RTLD no longer
262          * sets up an initial TLS segment for us).
263          */
264         tcb = _libc_allocate_tls();
265         tls_set_tcb(tcb);
266         return(tcb);
267 }
268
269 /*
270  * Allocate a standard TLS.  This function is called by libc and by 
271  * thread libraries to create a new TCB with libc-related fields properly
272  * initialized (whereas _rtld_allocate_tls() is unable to completely set
273  * up the TCB).
274  *
275  * Note that this is different from __libc_allocate_tls which is the
276  * weakly bound symbol that handles the case where _rtld_allocate_tls
277  * does not exist.
278  */
279 struct tls_tcb *
280 _libc_allocate_tls(void)
281 {
282         struct tls_tcb *tcb;
283
284         tcb = _rtld_allocate_tls();
285
286 #if 0
287 #if defined(__thread)
288         /* non-TLS libc */
289         tcb->tcb_errno_p = &errno;
290 #elif defined(PIC)
291         /* TLS libc dynamically linked */
292         tcb->tcb_errno_p = __tls_get_addr_tcb(tcb, __get_errno_GOT_ptr());
293 #else
294         /* TLS libc (threaded or unthreaded) */
295         tcb->tcb_errno_p = (void *)((char *)tcb + __get_errno_GS_offset());
296 #endif
297 #endif
298         return(tcb);
299 }
300