xref: /freebsd/lib/libc/gen/tls.c (revision ad30f8e79bd1007cc2476e491bd21b4f5e389e0a)
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$
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 <stdlib.h>
37 #include <string.h>
38 #include <elf.h>
39 
40 #include "libc_private.h"
41 
42 __weak_reference(__libc_allocate_tls, _rtld_allocate_tls);
43 __weak_reference(__libc_free_tls, _rtld_free_tls);
44 
45 #ifdef __i386__
46 
47 __weak_reference(___libc_tls_get_addr, ___tls_get_addr);
48 __attribute__((__regparm__(1))) void * ___libc_tls_get_addr(void *);
49 
50 #endif
51 
52 void * __libc_tls_get_addr(void *);
53 __weak_reference(__libc_tls_get_addr, __tls_get_addr);
54 
55 void *_rtld_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign);
56 void _rtld_free_tls(void *tls, size_t tcbsize, size_t tcbalign);
57 void *__libc_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign);
58 void __libc_free_tls(void *tls, size_t tcbsize, size_t tcbalign);
59 
60 #if defined(__ia64__) || defined(__amd64__)
61 #define TLS_TCB_ALIGN 16
62 #elif defined(__powerpc__) || defined(__i386__) || defined(__arm__) || \
63     defined(__sparc64__) || defined(__mips__)
64 #define TLS_TCB_ALIGN sizeof(void *)
65 #else
66 #error TLS_TCB_ALIGN undefined for target architecture
67 #endif
68 
69 #if defined(__ia64__) || defined(__powerpc__)
70 #define TLS_VARIANT_I
71 #endif
72 #if defined(__i386__) || defined(__amd64__) || defined(__sparc64__) || \
73     defined(__arm__) || defined(__mips__)
74 #define TLS_VARIANT_II
75 #endif
76 
77 #ifndef PIC
78 
79 #define round(size, align) \
80 	(((size) + (align) - 1) & ~((align) - 1))
81 
82 static size_t tls_static_space;
83 static size_t tls_init_size;
84 static void *tls_init;
85 #endif
86 
87 #ifdef __i386__
88 
89 /* GNU ABI */
90 
91 __attribute__((__regparm__(1)))
92 void *
93 ___libc_tls_get_addr(void *ti __unused)
94 {
95 	return (0);
96 }
97 
98 #endif
99 
100 void *
101 __libc_tls_get_addr(void *ti __unused)
102 {
103 	return (0);
104 }
105 
106 #ifndef PIC
107 
108 #ifdef TLS_VARIANT_I
109 
110 #define	TLS_TCB_SIZE	(2 * sizeof(void *))
111 
112 /*
113  * Free Static TLS using the Variant I method.
114  */
115 void
116 __libc_free_tls(void *tcb, size_t tcbsize, size_t tcbalign __unused)
117 {
118 	Elf_Addr *dtv;
119 	Elf_Addr **tls;
120 
121 	tls = (Elf_Addr **)((Elf_Addr)tcb + tcbsize - TLS_TCB_SIZE);
122 	dtv = tls[0];
123 	free(dtv);
124 	free(tcb);
125 }
126 
127 /*
128  * Allocate Static TLS using the Variant I method.
129  */
130 void *
131 __libc_allocate_tls(void *oldtcb, size_t tcbsize, size_t tcbalign __unused)
132 {
133 	Elf_Addr *dtv;
134 	Elf_Addr **tls;
135 	char *tcb;
136 
137 	if (oldtcb != NULL && tcbsize == TLS_TCB_SIZE)
138 		return (oldtcb);
139 
140 	tcb = calloc(1, tls_static_space + tcbsize - TLS_TCB_SIZE);
141 	tls = (Elf_Addr **)(tcb + tcbsize - TLS_TCB_SIZE);
142 
143 	if (oldtcb != NULL) {
144 		memcpy(tls, oldtcb, tls_static_space);
145 		free(oldtcb);
146 
147 		/* Adjust the DTV. */
148 		dtv = tls[0];
149 		dtv[2] = (Elf_Addr)tls + TLS_TCB_SIZE;
150 	} else {
151 		dtv = malloc(3 * sizeof(Elf_Addr));
152 		tls[0] = dtv;
153 		dtv[0] = 1;
154 		dtv[1] = 1;
155 		dtv[2] = (Elf_Addr)tls + TLS_TCB_SIZE;
156 
157 		if (tls_init_size > 0)
158 			memcpy((void*)dtv[2], tls_init, tls_init_size);
159 		if (tls_static_space > tls_init_size)
160 			memset((void*)(dtv[2] + tls_init_size), 0,
161 			    tls_static_space - tls_init_size);
162 	}
163 
164 	return(tcb);
165 }
166 
167 #endif
168 
169 #ifdef TLS_VARIANT_II
170 
171 #define	TLS_TCB_SIZE	(3 * sizeof(Elf_Addr))
172 
173 /*
174  * Free Static TLS using the Variant II method.
175  */
176 void
177 __libc_free_tls(void *tcb, size_t tcbsize __unused, size_t tcbalign)
178 {
179 	size_t size;
180 	Elf_Addr* dtv;
181 	Elf_Addr tlsstart, tlsend;
182 
183 	/*
184 	 * Figure out the size of the initial TLS block so that we can
185 	 * find stuff which ___tls_get_addr() allocated dynamically.
186 	 */
187 	size = round(tls_static_space, tcbalign);
188 
189 	dtv = ((Elf_Addr**)tcb)[1];
190 	tlsend = (Elf_Addr) tcb;
191 	tlsstart = tlsend - size;
192 	free((void*) tlsstart);
193 	free(dtv);
194 }
195 
196 /*
197  * Allocate Static TLS using the Variant II method.
198  */
199 void *
200 __libc_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign)
201 {
202 	size_t size;
203 	char *tls;
204 	Elf_Addr *dtv;
205 	Elf_Addr segbase, oldsegbase;
206 
207 	size = round(tls_static_space, tcbalign);
208 
209 	if (tcbsize < 2 * sizeof(Elf_Addr))
210 		tcbsize = 2 * sizeof(Elf_Addr);
211 	tls = calloc(1, size + tcbsize);
212 	dtv = malloc(3 * sizeof(Elf_Addr));
213 
214 	segbase = (Elf_Addr)(tls + size);
215 	((Elf_Addr*)segbase)[0] = segbase;
216 	((Elf_Addr*)segbase)[1] = (Elf_Addr) dtv;
217 
218 	dtv[0] = 1;
219 	dtv[1] = 1;
220 	dtv[2] = segbase - tls_static_space;
221 
222 	if (oldtls) {
223 		/*
224 		 * Copy the static TLS block over whole.
225 		 */
226 		oldsegbase = (Elf_Addr) oldtls;
227 		memcpy((void *)(segbase - tls_static_space),
228 		    (const void *)(oldsegbase - tls_static_space),
229 		    tls_static_space);
230 
231 		/*
232 		 * We assume that this block was the one we created with
233 		 * allocate_initial_tls().
234 		 */
235 		_rtld_free_tls(oldtls, 2*sizeof(Elf_Addr), sizeof(Elf_Addr));
236 	} else {
237 		memcpy((void *)(segbase - tls_static_space),
238 		    tls_init, tls_init_size);
239 		memset((void *)(segbase - tls_static_space + tls_init_size),
240 		    0, tls_static_space - tls_init_size);
241 	}
242 
243 	return (void*) segbase;
244 }
245 
246 #endif /* TLS_VARIANT_II */
247 
248 #else
249 
250 void *
251 __libc_allocate_tls(void *oldtls __unused, size_t tcbsize __unused,
252 	size_t tcbalign __unused)
253 {
254 	return (0);
255 }
256 
257 void
258 __libc_free_tls(void *tcb __unused, size_t tcbsize __unused,
259 	size_t tcbalign __unused)
260 {
261 }
262 
263 #endif /* PIC */
264 
265 extern char **environ;
266 
267 void
268 _init_tls()
269 {
270 #ifndef PIC
271 	Elf_Addr *sp;
272 	Elf_Auxinfo *aux, *auxp;
273 	Elf_Phdr *phdr;
274 	size_t phent, phnum;
275 	int i;
276 	void *tls;
277 
278 	sp = (Elf_Addr *) environ;
279 	while (*sp++ != 0)
280 		;
281 	aux = (Elf_Auxinfo *) sp;
282 	phdr = 0;
283 	phent = phnum = 0;
284 	for (auxp = aux; auxp->a_type != AT_NULL; auxp++) {
285 		switch (auxp->a_type) {
286 		case AT_PHDR:
287 			phdr = auxp->a_un.a_ptr;
288 			break;
289 
290 		case AT_PHENT:
291 			phent = auxp->a_un.a_val;
292 			break;
293 
294 		case AT_PHNUM:
295 			phnum = auxp->a_un.a_val;
296 			break;
297 		}
298 	}
299 	if (phdr == 0 || phent != sizeof(Elf_Phdr) || phnum == 0)
300 		return;
301 
302 	for (i = 0; (unsigned) i < phnum; i++) {
303 		if (phdr[i].p_type == PT_TLS) {
304 			tls_static_space = round(phdr[i].p_memsz,
305 			    phdr[i].p_align);
306 			tls_init_size = phdr[i].p_filesz;
307 			tls_init = (void*) phdr[i].p_vaddr;
308 		}
309 	}
310 
311 	tls = _rtld_allocate_tls(NULL, TLS_TCB_SIZE, TLS_TCB_ALIGN);
312 
313 	_set_tp(tls);
314 #endif
315 }
316