(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / generic / libc-tls.c
blobb5ecc3643682ad0bc6a826bca0032cb48cfec146
1 /* Initialization code for TLS in statically linked application.
2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <ldsodefs.h>
22 #include <tls.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <sys/param.h>
28 #ifdef SHARED
29 #error makefile bug, this file is for static only
30 #endif
32 #ifdef USE_TLS
33 extern ElfW(Phdr) *_dl_phdr;
34 extern size_t _dl_phnum;
37 /* DTV with just one element plus overhead. */
38 static dtv_t static_dtv[3];
41 static struct
43 struct dtv_slotinfo_list si;
44 /* The dtv_slotinfo_list data structure does not include the actual
45 information since it is defined as an array of size zero. We define
46 here the necessary entries. Note that it is not important whether
47 there is padding or not since we will always access the information
48 through the 'si' element. */
49 struct dtv_slotinfo info[2 + TLS_SLOTINFO_SURPLUS];
50 } static_slotinfo;
52 /* Fake link map for the application. */
53 static struct link_map static_map;
56 /* Highest dtv index currently needed. */
57 size_t _dl_tls_max_dtv_idx;
58 /* Flag signalling whether there are gaps in the module ID allocation. */
59 bool _dl_tls_dtv_gaps;
60 /* Information about the dtv slots. */
61 struct dtv_slotinfo_list *_dl_tls_dtv_slotinfo_list;
62 /* Number of modules in the static TLS block. */
63 size_t _dl_tls_static_nelem;
64 /* Size of the static TLS block. */
65 size_t _dl_tls_static_size;
66 /* Size actually allocated in the static TLS block. */
67 size_t _dl_tls_static_used;
68 /* Alignment requirement of the static TLS block. */
69 size_t _dl_tls_static_align;
71 /* Generation counter for the dtv. */
72 size_t _dl_tls_generation;
75 /* Additional definitions needed by TLS initialization. */
76 #ifdef TLS_INIT_HELPER
77 TLS_INIT_HELPER
78 #endif
80 static inline void
81 init_slotinfo (void)
83 /* Create the slotinfo list. */
84 static_slotinfo.si.len = (((char *) (&static_slotinfo + 1)
85 - (char *) &static_slotinfo.si.slotinfo[0])
86 / sizeof static_slotinfo.si.slotinfo[0]);
87 // static_slotinfo.si.next = NULL; already zero
89 /* The slotinfo list. Will be extended by the code doing dynamic
90 linking. */
91 GL(dl_tls_max_dtv_idx) = 1;
92 GL(dl_tls_dtv_slotinfo_list) = &static_slotinfo.si;
95 static inline void
96 init_static_tls (size_t memsz, size_t align)
98 /* That is the size of the TLS memory for this object. The initialized
99 value of _dl_tls_static_size is provided by dl-open.c to request some
100 surplus that permits dynamic loading of modules with IE-model TLS. */
101 GL(dl_tls_static_size) = roundup (memsz + GL(dl_tls_static_size),
102 TLS_TCB_ALIGN);
103 GL(dl_tls_static_used) = memsz;
104 /* The alignment requirement for the static TLS block. */
105 GL(dl_tls_static_align) = align;
106 /* Number of elements in the static TLS block. */
107 GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
110 void
111 __libc_setup_tls (size_t tcbsize, size_t tcbalign)
113 void *tlsblock;
114 size_t memsz = 0;
115 size_t filesz = 0;
116 void *initimage = NULL;
117 size_t align = 0;
118 size_t max_align = tcbalign;
119 size_t tcb_offset;
120 ElfW(Phdr) *phdr;
122 /* Look through the TLS segment if there is any. */
123 if (_dl_phdr != NULL)
124 for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr)
125 if (phdr->p_type == PT_TLS)
127 /* Remember the values we need. */
128 memsz = phdr->p_memsz;
129 filesz = phdr->p_filesz;
130 initimage = (void *) phdr->p_vaddr;
131 align = phdr->p_align;
132 if (phdr->p_align > max_align)
133 max_align = phdr->p_align;
134 break;
137 #ifdef TLS_INIT_TP_EXPENSIVE
138 if (memsz == 0 && tcbsize <= TLS_INIT_TCB_SIZE)
140 /* We do not need a TLS block and no thread descriptor. */
141 # ifdef NONTLS_INIT_TP
142 NONTLS_INIT_TP;
143 # endif
144 return;
146 #endif
148 /* We have to set up the TCB block which also (possibly) contains
149 'errno'. Therefore we avoid 'malloc' which might touch 'errno'.
150 Instead we use 'sbrk' which would only uses 'errno' if it fails.
151 In this case we are right away out of memory and the user gets
152 what she/he deserves.
154 The initialized value of _dl_tls_static_size is provided by dl-open.c
155 to request some surplus that permits dynamic loading of modules with
156 IE-model TLS. */
157 # if TLS_TCB_AT_TP
158 tcb_offset = roundup (memsz + GL(dl_tls_static_size), tcbalign);
159 tlsblock = __sbrk (tcb_offset + tcbsize + max_align);
160 # elif TLS_DTV_AT_TP
161 tcb_offset = roundup (tcbsize, align ?: 1);
162 tlsblock = __sbrk (tcb_offset + memsz + max_align
163 + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size));
164 tlsblock += TLS_PRE_TCB_SIZE;
165 # else
166 /* In case a model with a different layout for the TCB and DTV
167 is defined add another #elif here and in the following #ifs. */
168 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
169 # endif
171 /* Align the TLS block. */
172 tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
173 & ~(max_align - 1));
175 /* Initialize the dtv. [0] is the length, [1] the generation counter. */
176 static_dtv[0].counter = 1;
177 // static_dtv[1].counter = 0; would be needed if not already done
179 /* Initialize the TLS block. */
180 # if TLS_TCB_AT_TP
181 static_dtv[2].pointer = ((char *) tlsblock + tcb_offset
182 - roundup (memsz, align ?: 1));
183 static_map.l_tls_offset = roundup (memsz, align ?: 1);
184 # elif TLS_DTV_AT_TP
185 static_dtv[2].pointer = (char *) tlsblock + tcb_offset;
186 static_map.l_tls_offset = tcb_offset;
187 # else
188 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
189 # endif
190 /* sbrk gives us zero'd memory, so we don't need to clear the remainder. */
191 memcpy (static_dtv[2].pointer, initimage, filesz);
193 /* Install the pointer to the dtv. */
195 /* Initialize the thread pointer. */
196 # if TLS_TCB_AT_TP
197 INSTALL_DTV ((char *) tlsblock + tcb_offset, static_dtv);
199 const char *lossage = TLS_INIT_TP ((char *) tlsblock + tcb_offset, 0);
200 # elif TLS_DTV_AT_TP
201 INSTALL_DTV (tlsblock, static_dtv);
202 const char *lossage = TLS_INIT_TP (tlsblock, 0);
203 # else
204 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
205 # endif
206 if (__builtin_expect (lossage != NULL, 0))
207 __libc_fatal (lossage);
209 /* We have to create a fake link map which normally would be created
210 by the dynamic linker. It just has to have enough information to
211 make the TLS routines happy. */
212 static_map.l_tls_align = align;
213 static_map.l_tls_blocksize = memsz;
214 static_map.l_tls_initimage = initimage;
215 static_map.l_tls_initimage_size = filesz;
216 static_map.l_type = lt_executable;
217 static_map.l_tls_modid = 1;
219 init_slotinfo ();
220 // static_slotinfo.si.slotinfo[1].gen = 0; already zero
221 static_slotinfo.si.slotinfo[1].map = &static_map;
223 memsz = roundup (memsz, align ?: 1);
225 # if TLS_TCB_AT_TP
226 memsz += tcbsize;
227 # elif TLS_DTV_AT_TP
228 memsz += tcb_offset;
229 # endif
231 init_static_tls (memsz, MAX (TLS_TCB_ALIGN, max_align));
234 /* This is called only when the data structure setup was skipped at startup,
235 when there was no need for it then. Now we have dynamically loaded
236 something needing TLS, or libpthread needs it. */
238 internal_function
239 _dl_tls_setup (void)
241 init_slotinfo ();
242 init_static_tls (
243 # if TLS_TCB_AT_TP
244 TLS_TCB_SIZE,
245 # else
247 # endif
248 TLS_TCB_ALIGN);
249 return 0;
253 /* This is the minimal initialization function used when libpthread is
254 not used. */
255 void
256 __attribute__ ((weak))
257 __pthread_initialize_minimal (void)
259 __libc_setup_tls (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN);
262 #elif defined NONTLS_INIT_TP
264 /* This is the minimal initialization function used when libpthread is
265 not used. */
266 void
267 __attribute__ ((weak))
268 __pthread_initialize_minimal (void)
270 NONTLS_INIT_TP;
273 #endif