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