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