Revert GLIBC_PTHREAD_DEFAULT_STACKSIZE changes.
[jlayton/glibc.git] / csu / libc-tls.c
1 /* Initialization code for TLS in statically linked application.
2    Copyright (C) 2002-2013 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
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.
9
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.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include <ldsodefs.h>
21 #include <tls.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <sys/param.h>
25
26
27 #ifdef SHARED
28  #error makefile bug, this file is for static only
29 #endif
30
31 extern ElfW(Phdr) *_dl_phdr;
32 extern size_t _dl_phnum;
33
34
35 dtv_t _dl_static_dtv[2 + TLS_SLOTINFO_SURPLUS];
36
37
38 static struct
39 {
40   struct dtv_slotinfo_list si;
41   /* The dtv_slotinfo_list data structure does not include the actual
42      information since it is defined as an array of size zero.  We define
43      here the necessary entries.  Note that it is not important whether
44      there is padding or not since we will always access the information
45      through the 'si' element.  */
46   struct dtv_slotinfo info[2 + TLS_SLOTINFO_SURPLUS];
47 } static_slotinfo;
48
49 /* Fake link map for the application.  */
50 static struct link_map static_map;
51
52
53 /* Highest dtv index currently needed.  */
54 size_t _dl_tls_max_dtv_idx;
55 /* Flag signalling whether there are gaps in the module ID allocation.  */
56 bool _dl_tls_dtv_gaps;
57 /* Information about the dtv slots.  */
58 struct dtv_slotinfo_list *_dl_tls_dtv_slotinfo_list;
59 /* Number of modules in the static TLS block.  */
60 size_t _dl_tls_static_nelem;
61 /* Size of the static TLS block.  Giving this initialized value
62    preallocates some surplus bytes in the static TLS area.  */
63 size_t _dl_tls_static_size = 2048;
64 /* Size actually allocated in the static TLS block.  */
65 size_t _dl_tls_static_used;
66 /* Alignment requirement of the static TLS block.  */
67 size_t _dl_tls_static_align;
68
69 /* Generation counter for the dtv.  */
70 size_t _dl_tls_generation;
71
72
73 /* Additional definitions needed by TLS initialization.  */
74 #ifdef TLS_INIT_HELPER
75 TLS_INIT_HELPER
76 #endif
77
78 static void
79 init_slotinfo (void)
80 {
81   /* Create the slotinfo list.  */
82   static_slotinfo.si.len = (((char *) (&static_slotinfo + 1)
83                              - (char *) &static_slotinfo.si.slotinfo[0])
84                             / sizeof static_slotinfo.si.slotinfo[0]);
85   // static_slotinfo.si.next = NULL;    already zero
86
87   /* The slotinfo list.  Will be extended by the code doing dynamic
88      linking.  */
89   GL(dl_tls_max_dtv_idx) = 1;
90   GL(dl_tls_dtv_slotinfo_list) = &static_slotinfo.si;
91 }
92
93 static void
94 init_static_tls (size_t memsz, size_t align)
95 {
96   /* That is the size of the TLS memory for this object.  The initialized
97      value of _dl_tls_static_size is provided by dl-open.c to request some
98      surplus that permits dynamic loading of modules with IE-model TLS.  */
99   GL(dl_tls_static_size) = roundup (memsz + GL(dl_tls_static_size),
100                                     TLS_TCB_ALIGN);
101 #if TLS_TCB_AT_TP
102   GL(dl_tls_static_size) += TLS_TCB_SIZE;
103 #endif
104   GL(dl_tls_static_used) = memsz;
105   /* The alignment requirement for the static TLS block.  */
106   GL(dl_tls_static_align) = align;
107   /* Number of elements in the static TLS block.  */
108   GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
109 }
110
111 void
112 __libc_setup_tls (size_t tcbsize, size_t tcbalign)
113 {
114   void *tlsblock;
115   size_t memsz = 0;
116   size_t filesz = 0;
117   void *initimage = NULL;
118   size_t align = 0;
119   size_t max_align = tcbalign;
120   size_t tcb_offset;
121   ElfW(Phdr) *phdr;
122
123   /* Look through the TLS segment if there is any.  */
124   if (_dl_phdr != NULL)
125     for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr)
126       if (phdr->p_type == PT_TLS)
127         {
128           /* Remember the values we need.  */
129           memsz = phdr->p_memsz;
130           filesz = phdr->p_filesz;
131           initimage = (void *) phdr->p_vaddr;
132           align = phdr->p_align;
133           if (phdr->p_align > max_align)
134             max_align = phdr->p_align;
135           break;
136         }
137
138   /* We have to set up the TCB block which also (possibly) contains
139      'errno'.  Therefore we avoid 'malloc' which might touch 'errno'.
140      Instead we use 'sbrk' which would only uses 'errno' if it fails.
141      In this case we are right away out of memory and the user gets
142      what she/he deserves.
143
144      The initialized value of _dl_tls_static_size is provided by dl-open.c
145      to request some surplus that permits dynamic loading of modules with
146      IE-model TLS.  */
147 #if TLS_TCB_AT_TP
148   tcb_offset = roundup (memsz + GL(dl_tls_static_size), tcbalign);
149   tlsblock = __sbrk (tcb_offset + tcbsize + max_align);
150 #elif TLS_DTV_AT_TP
151   tcb_offset = roundup (tcbsize, align ?: 1);
152   tlsblock = __sbrk (tcb_offset + memsz + max_align
153                      + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size));
154   tlsblock += TLS_PRE_TCB_SIZE;
155 #else
156   /* In case a model with a different layout for the TCB and DTV
157      is defined add another #elif here and in the following #ifs.  */
158 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
159 #endif
160
161   /* Align the TLS block.  */
162   tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
163                        & ~(max_align - 1));
164
165   /* Initialize the dtv.  [0] is the length, [1] the generation counter.  */
166   _dl_static_dtv[0].counter = (sizeof (_dl_static_dtv) / sizeof (_dl_static_dtv[0])) - 2;
167   // _dl_static_dtv[1].counter = 0;             would be needed if not already done
168
169   /* Initialize the TLS block.  */
170 #if TLS_TCB_AT_TP
171   _dl_static_dtv[2].pointer.val = ((char *) tlsblock + tcb_offset
172                                - roundup (memsz, align ?: 1));
173   static_map.l_tls_offset = roundup (memsz, align ?: 1);
174 #elif TLS_DTV_AT_TP
175   _dl_static_dtv[2].pointer.val = (char *) tlsblock + tcb_offset;
176   static_map.l_tls_offset = tcb_offset;
177 #else
178 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
179 #endif
180   _dl_static_dtv[2].pointer.is_static = true;
181   /* sbrk gives us zero'd memory, so we don't need to clear the remainder.  */
182   memcpy (_dl_static_dtv[2].pointer.val, initimage, filesz);
183
184   /* Install the pointer to the dtv.  */
185
186   /* Initialize the thread pointer.  */
187 #if TLS_TCB_AT_TP
188   INSTALL_DTV ((char *) tlsblock + tcb_offset, _dl_static_dtv);
189
190   const char *lossage = TLS_INIT_TP ((char *) tlsblock + tcb_offset, 0);
191 #elif TLS_DTV_AT_TP
192   INSTALL_DTV (tlsblock, _dl_static_dtv);
193   const char *lossage = TLS_INIT_TP (tlsblock, 0);
194 #else
195 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
196 #endif
197   if (__builtin_expect (lossage != NULL, 0))
198     __libc_fatal (lossage);
199
200   /* We have to create a fake link map which normally would be created
201      by the dynamic linker.  It just has to have enough information to
202      make the TLS routines happy.  */
203   static_map.l_tls_align = align;
204   static_map.l_tls_blocksize = memsz;
205   static_map.l_tls_initimage = initimage;
206   static_map.l_tls_initimage_size = filesz;
207   static_map.l_type = lt_executable;
208   static_map.l_tls_modid = 1;
209
210   init_slotinfo ();
211   // static_slotinfo.si.slotinfo[1].gen = 0; already zero
212   static_slotinfo.si.slotinfo[1].map = &static_map;
213
214   memsz = roundup (memsz, align ?: 1);
215
216 #if TLS_DTV_AT_TP
217   memsz += tcb_offset;
218 #endif
219
220   init_static_tls (memsz, MAX (TLS_TCB_ALIGN, max_align));
221 }
222
223 /* This is called only when the data structure setup was skipped at startup,
224    when there was no need for it then.  Now we have dynamically loaded
225    something needing TLS, or libpthread needs it.  */
226 int
227 internal_function
228 _dl_tls_setup (void)
229 {
230   init_slotinfo ();
231   init_static_tls (
232 #if TLS_TCB_AT_TP
233                    TLS_TCB_SIZE,
234 #else
235                    0,
236 #endif
237                    TLS_TCB_ALIGN);
238   return 0;
239 }
240
241
242 /* This is the minimal initialization function used when libpthread is
243    not used.  */
244 void
245 __attribute__ ((weak))
246 __pthread_initialize_minimal (void)
247 {
248   __libc_setup_tls (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN);
249 }