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