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