libc - Use sbrk() system call, remove brk() (2)
[dragonfly.git] / lib / libc / gen / tls.c
blob63b437b5c62187e98e3a76d5802bba8309a65ea8
1 /*-
2 * Copyright (c) 2004 Doug Rabson
3 * All rights reserved.
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.
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.
26 * $FreeBSD: src/lib/libc/gen/tls.c,v 1.7 2005/03/01 23:42:00 davidxu Exp $
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.
35 #include <sys/param.h>
36 #include <sys/tls.h>
37 #include <sys/mman.h>
39 #include <machine/tls.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <elf.h>
44 #include <assert.h>
45 #include <errno.h>
46 #include <unistd.h>
48 #include "libc_private.h"
50 struct tls_tcb *__libc_allocate_tls(void);
51 void __libc_free_tls(struct tls_tcb *tcb);
53 #if !defined(RTLD_STATIC_TLS_VARIANT_II)
54 #error "Unsupported TLS layout"
55 #endif
57 #ifndef PIC
59 static size_t tls_static_space;
60 static size_t tls_init_size;
61 static void *tls_init;
62 static struct tls_tcb *initial_tcb;
63 #endif
65 void *__libc_tls_get_addr(void *ti);
66 void *__libc_tls_get_addr_tcb(struct tls_tcb *, void *);
68 void *
69 __libc_tls_get_addr(void *ti __unused)
71 return (NULL);
74 void *
75 __libc_tls_get_addr_tcb(struct tls_tcb *tcb __unused, void *got_ptr __unused)
77 return (NULL);
80 #ifndef PIC
83 * Free Static TLS, weakly bound to _rtld_free_tls()
85 void
86 __libc_free_tls(struct tls_tcb *tcb)
88 size_t data_size;
90 data_size = (tls_static_space + RTLD_STATIC_TLS_ALIGN_MASK) &
91 ~RTLD_STATIC_TLS_ALIGN_MASK;
93 if (tcb == initial_tcb) {
94 /* initial_tcb was allocated with mmap(), cannot call free() */
95 } else {
96 free((char *)tcb - data_size);
101 * Allocate Static TLS, weakly bound to _rtld_allocate_tls()
103 * NOTE! There is a chicken-and-egg problem here because no TLS exists
104 * on the first call into this function.
106 struct tls_tcb *
107 __libc_allocate_tls(void)
109 size_t data_size;
110 struct tls_tcb *tcb;
111 Elf_Addr *dtv;
113 data_size = (tls_static_space + RTLD_STATIC_TLS_ALIGN_MASK) &
114 ~RTLD_STATIC_TLS_ALIGN_MASK;
117 * Allocate space. malloc() may require a working TLS segment
118 * so we use sbrk() for main's TLS. Oops, but in order to be
119 * compatible with older kernels we cannot use sbrk() because it
120 * will generate an errno (which needs the TLS), so use mmap().
122 if (initial_tcb == NULL) {
123 size_t bytes;
125 bytes = data_size + sizeof(*tcb) + 3 * sizeof(*dtv);
126 bytes = (bytes + PAGE_MASK) & ~(size_t)PAGE_MASK;
127 tcb = mmap((void *)1, bytes,
128 PROT_READ | PROT_WRITE,
129 MAP_PRIVATE | MAP_ANON,
130 -1, 0);
131 } else {
132 tcb = malloc(data_size + sizeof(*tcb) + 3 * sizeof(*dtv));
135 tcb = (struct tls_tcb *)((char *)tcb + data_size);
136 dtv = (Elf_Addr *)(tcb + 1);
138 memset(tcb, 0, sizeof(*tcb));
139 #ifdef RTLD_TCB_HAS_SELF_POINTER
140 tcb->tcb_self = tcb;
141 #endif
142 tcb->tcb_dtv = dtv;
145 * Dummy-up the module array. A static binary has only one. This
146 * allows us to support the generic __tls_get_addr compiler ABI
147 * function. However, if there is no RTLD linked in, nothing in
148 * the program should ever call __tls_get_addr (and our version
149 * of it doesn't do anything).
151 dtv[0] = 1;
152 dtv[1] = 1;
153 dtv[2] = (Elf_Addr)((char *)tcb - tls_static_space);
155 memcpy((char *)tcb - tls_static_space,
156 tls_init, tls_init_size);
157 memset((char *)tcb - tls_static_space + tls_init_size,
158 0, tls_static_space - tls_init_size);
161 * Activate the initial TCB
163 if (initial_tcb == NULL) {
164 initial_tcb = tcb;
165 tls_set_tcb(tcb);
167 return (tcb);
170 #else
172 struct tls_tcb *
173 __libc_allocate_tls(void)
175 return (NULL);
178 void
179 __libc_free_tls(struct tls_tcb *tcb __unused)
183 #endif /* PIC */
185 void
186 __libc_call_init(void)
190 extern char **environ;
192 struct tls_tcb *
193 _libc_init_tls(void)
195 struct tls_tcb *tcb;
197 #ifndef PIC
199 * If this is a static binary there is no RTLD and so we have not
200 * yet calculated the static space requirement. Do so now.
202 Elf_Addr *sp;
203 Elf_Auxinfo *aux, *auxp;
204 Elf_Phdr *phdr;
205 size_t phent, phnum;
206 int i;
208 sp = (Elf_Addr *) environ;
209 while (*sp++ != 0)
211 aux = (Elf_Auxinfo *) sp;
212 phdr = NULL;
213 phent = phnum = 0;
214 for (auxp = aux; auxp->a_type != AT_NULL; auxp++) {
215 switch (auxp->a_type) {
216 case AT_PHDR:
217 phdr = auxp->a_un.a_ptr;
218 break;
220 case AT_PHENT:
221 phent = auxp->a_un.a_val;
222 break;
224 case AT_PHNUM:
225 phnum = auxp->a_un.a_val;
226 break;
229 if (phdr == NULL || phent != sizeof(Elf_Phdr) || phnum == 0)
230 return(NULL);
232 for (i = 0; (unsigned)i < phnum; i++) {
233 if (phdr[i].p_type == PT_TLS) {
234 tls_static_space = roundup2(phdr[i].p_memsz,
235 phdr[i].p_align);
236 tls_init_size = phdr[i].p_filesz;
237 tls_init = (void*) phdr[i].p_vaddr;
240 #endif
243 * Allocate the initial TLS segment. The TLS has not been set up
244 * yet for either the static or dynamic linked case (RTLD no longer
245 * sets up an initial TLS segment for us).
247 tcb = _libc_allocate_tls();
248 tls_set_tcb(tcb);
249 return(tcb);
253 * Allocate a standard TLS. This function is called by libc and by
254 * thread libraries to create a new TCB with libc-related fields properly
255 * initialized (whereas _rtld_allocate_tls() is unable to completely set
256 * up the TCB).
258 * Note that this is different from __libc_allocate_tls which is the
259 * weakly bound symbol that handles the case where _rtld_allocate_tls
260 * does not exist.
262 struct tls_tcb *
263 _libc_allocate_tls(void)
265 struct tls_tcb *tcb;
267 tcb = _rtld_allocate_tls();
269 #if 0
270 #if defined(__thread)
271 /* non-TLS libc */
272 tcb->tcb_errno_p = &errno;
273 #elif defined(PIC)
274 /* TLS libc dynamically linked */
275 tcb->tcb_errno_p = __tls_get_addr_tcb(tcb, __get_errno_GOT_ptr());
276 #else
277 /* TLS libc (threaded or unthreaded) */
278 tcb->tcb_errno_p = (void *)((char *)tcb + __get_errno_GS_offset());
279 #endif
280 #endif
281 return(tcb);
284 __weak_reference(__libc_allocate_tls, _rtld_allocate_tls);
285 __weak_reference(__libc_free_tls, _rtld_free_tls);
286 __weak_reference(__libc_call_init, _rtld_call_init);
287 __weak_reference(__libc_tls_get_addr, __tls_get_addr);
288 __weak_reference(__libc_tls_get_addr_tcb, __tls_get_addr_tcb);
289 __weak_reference(_libc_init_tls, _init_tls);