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