]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/libpthread/src/sysdeps/generic/libc-tls.cc
update
[l4.git] / l4 / pkg / uclibc / lib / libpthread / src / sysdeps / generic / libc-tls.cc
1 /* Initialization code for TLS in statically linked application.
2    Copyright (C) 2002, 2003, 2004, 2005 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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
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>
26 #include <elf.h>
27 #include <link.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include <l4/re/env>
32 #include <l4/re/dataspace>
33 #include <l4/re/mem_alloc>
34 #include <l4/re/rm>
35
36 #ifdef SHARED
37  #error makefile bug, this file is for static only
38 #endif
39
40 #if USE_TLS
41 extern ElfW(Phdr) *_dl_phdr;
42 extern size_t _dl_phnum;
43
44 extern "C"
45 __attribute__ ((weak))
46 void *__libc_alloc_initial_tls(unsigned long size) __THROW;
47
48 static dtv_t static_dtv[2 + TLS_SLOTINFO_SURPLUS];
49
50
51 static struct
52 {
53   struct dtv_slotinfo_list si;
54   /* The dtv_slotinfo_list data structure does not include the actual
55      information since it is defined as an array of size zero.  We define
56      here the necessary entries.  Note that it is not important whether
57      there is padding or not since we will always access the information
58      through the 'si' element.  */
59   dtv_slotinfo_list::dtv_slotinfo info[2 + TLS_SLOTINFO_SURPLUS];
60 } static_slotinfo;
61
62 /* Fake link map for the application.  */
63 static struct link_map static_map;
64
65
66 /* Highest dtv index currently needed.  */
67 size_t _dl_tls_max_dtv_idx;
68 /* Flag signalling whether there are gaps in the module ID allocation.  */
69 bool _dl_tls_dtv_gaps;
70 /* Information about the dtv slots.  */
71 struct dtv_slotinfo_list *_dl_tls_dtv_slotinfo_list;
72 /* Number of modules in the static TLS block.  */
73 size_t _dl_tls_static_nelem;
74 /* Size of the static TLS block.  */
75 size_t _dl_tls_static_size __attribute__((weak)) = TLS_TCB_SIZE;
76 /* Size actually allocated in the static TLS block.  */
77 size_t _dl_tls_static_used;
78 /* Alignment requirement of the static TLS block.  */
79 size_t _dl_tls_static_align;
80
81 /* Generation counter for the dtv.  */
82 size_t _dl_tls_generation;
83
84
85 /* Additional definitions needed by TLS initialization.  */
86 #ifdef TLS_INIT_HELPER
87 TLS_INIT_HELPER
88 #endif
89
90 static inline void
91 init_slotinfo (void)
92 {
93   /* Create the slotinfo list.  */
94   static_slotinfo.si.len = (((char *) (&static_slotinfo + 1)
95                              - (char *) &static_slotinfo.si.slotinfo[0])
96                             / sizeof static_slotinfo.si.slotinfo[0]);
97   // static_slotinfo.si.next = NULL;    already zero
98
99   /* The slotinfo list.  Will be extended by the code doing dynamic
100      linking.  */
101   GL(dl_tls_max_dtv_idx) = 1;
102   GL(dl_tls_dtv_slotinfo_list) = &static_slotinfo.si;
103 }
104
105 static inline void
106 init_static_tls (size_t memsz, size_t align)
107 {
108   /* That is the size of the TLS memory for this object.  The initialized
109      value of _dl_tls_static_size is provided by dl-open.c to request some
110      surplus that permits dynamic loading of modules with IE-model TLS.  */
111   GL(dl_tls_static_size) = roundup (memsz + GL(dl_tls_static_size),
112                                     TLS_TCB_ALIGN);
113   GL(dl_tls_static_used) = memsz;
114   /* The alignment requirement for the static TLS block.  */
115   GL(dl_tls_static_align) = align;
116   /* Number of elements in the static TLS block.  */
117   GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
118 }
119
120 __attribute__ ((weak))
121 void *__libc_alloc_initial_tls(unsigned long size) __THROW
122 {
123   using namespace L4;
124   using namespace L4Re;
125   Cap<Dataspace> ds(Env::env()->first_free_cap() << L4_CAP_SHIFT);
126   ::l4re_global_env->first_free_cap += 1;
127   if (Env::env()->mem_alloc()->alloc(size, ds, 0) < 0)
128     return NULL;
129
130   void *addr = NULL;
131   if(Env::env()->rm()->attach(&addr, size, Rm::Search_addr, ds, 0, 0) < 0)
132     return NULL;
133
134   return addr;
135 }
136 extern "C"
137 void
138 __libc_setup_tls (size_t tcbsize, size_t tcbalign)
139 {
140   void *tlsblock;
141   size_t memsz = 0;
142   size_t filesz = 0;
143   void *initimage = NULL;
144   size_t align = 0;
145   size_t max_align = tcbalign;
146   size_t tcb_offset;
147   ElfW(Phdr) *phdr;
148   /* Look through the TLS segment if there is any.  */
149   if (_dl_phdr != NULL)
150     for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr)
151       if (phdr->p_type == PT_TLS)
152         {
153           /* Remember the values we need.  */
154           memsz = phdr->p_memsz;
155           filesz = phdr->p_filesz;
156           initimage = (void *) phdr->p_vaddr;
157           align = phdr->p_align;
158           if (phdr->p_align > max_align)
159             max_align = phdr->p_align;
160           break;
161         }
162   /* We have to set up the TCB block which also (possibly) contains
163      'errno'.  Therefore we avoid 'malloc' which might touch 'errno'.
164      Instead we use 'sbrk' which would only uses 'errno' if it fails.
165      In this case we are right away out of memory and the user gets
166      what she/he deserves.
167
168      The initialized value of _dl_tls_static_size is provided by dl-open.c
169      to request some surplus that permits dynamic loading of modules with
170      IE-model TLS.  */
171 # if defined(TLS_TCB_AT_TP)
172   tcb_offset = roundup (memsz + GL(dl_tls_static_size), tcbalign);
173   tlsblock = __libc_alloc_initial_tls(tcb_offset + tcbsize + max_align);
174 # elif defined(TLS_DTV_AT_TP)
175   tcb_offset = roundup (tcbsize, align ?: 1);
176   tlsblock = __libc_alloc_initial_tls(tcb_offset + memsz + max_align
177                      + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size));
178   //tlsblock += TLS_PRE_TCB_SIZE;
179   tlsblock = (char *)tlsblock + TLS_PRE_TCB_SIZE;
180 # else
181   /* In case a model with a different layout for the TCB and DTV
182      is defined add another #elif here and in the following #ifs.  */
183 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
184 # endif
185
186   /* Align the TLS block.  */
187   tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
188                        & ~(max_align - 1));
189
190   /* Initialize the dtv.  [0] is the length, [1] the generation counter.  */
191   static_dtv[0].counter = (sizeof (static_dtv) / sizeof (static_dtv[0])) - 2;
192   // static_dtv[1].counter = 0;         would be needed if not already done
193
194   /* Initialize the TLS block.  */
195 # if defined(TLS_TCB_AT_TP)
196   static_dtv[2].pointer.val = ((char *) tlsblock + tcb_offset
197                                - roundup (memsz, align ?: 1));
198   static_map.l_tls_offset = roundup (memsz, align ?: 1);
199 # elif defined(TLS_DTV_AT_TP)
200   static_dtv[2].pointer.val = (char *) tlsblock + tcb_offset;
201   static_map.l_tls_offset = tcb_offset;
202 # else
203 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
204 # endif
205   static_dtv[2].pointer.is_static = true;
206   /* sbrk gives us zero'd memory, so we don't need to clear the remainder.  */
207   memcpy (static_dtv[2].pointer.val, initimage, filesz);
208
209   /* Install the pointer to the dtv.  */
210
211   /* Initialize the thread pointer.  */
212 # if defined(TLS_TCB_AT_TP)
213   INSTALL_DTV ((char *) tlsblock + tcb_offset, static_dtv);
214   const char *lossage = TLS_INIT_TP ((char *) tlsblock + tcb_offset, 0);
215 # elif defined(TLS_DTV_AT_TP)
216   INSTALL_DTV (tlsblock, static_dtv);
217   const char *lossage = (char *)TLS_INIT_TP (tlsblock, 0);
218 # else
219 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
220 # endif
221   if (__builtin_expect (lossage != NULL, 0))
222     abort();
223
224   /* We have to create a fake link map which normally would be created
225      by the dynamic linker.  It just has to have enough information to
226      make the TLS routines happy.  */
227   static_map.l_tls_align = align;
228   static_map.l_tls_blocksize = memsz;
229   static_map.l_tls_initimage = initimage;
230   static_map.l_tls_initimage_size = filesz;
231   static_map.l_tls_modid = 1;
232
233   init_slotinfo ();
234   // static_slotinfo.si.slotinfo[1].gen = 0; already zero
235   static_slotinfo.si.slotinfo[1].map = &static_map;
236
237   memsz = roundup (memsz, align ?: 1);
238
239 # if defined(TLS_TCB_AT_TP)
240   memsz += tcbsize;
241 # elif defined(TLS_DTV_AT_TP)
242   memsz += tcb_offset;
243 # endif
244
245   init_static_tls (memsz, MAX (TLS_TCB_ALIGN, max_align));
246 }
247
248 /* This is called only when the data structure setup was skipped at startup,
249    when there was no need for it then.  Now we have dynamically loaded
250    something needing TLS, or libpthread needs it.  */
251 int
252 internal_function
253 _dl_tls_setup (void)
254 {
255   init_slotinfo ();
256   init_static_tls (
257 # if defined(TLS_TCB_AT_TP)
258                    TLS_TCB_SIZE,
259 # else
260                    0,
261 # endif
262                    TLS_TCB_ALIGN);
263   return 0;
264 }
265
266
267 /* This is the minimal initialization function used when libpthread is
268    not used.  */
269 extern "C"
270 void
271 __attribute__ ((weak))
272 __pthread_initialize_minimal (void)
273 {
274   __libc_setup_tls (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN);
275 }
276
277 #elif defined NONTLS_INIT_TP
278
279 /* This is the minimal initialization function used when libpthread is
280    not used.  */
281 extern "C"
282 void
283 __attribute__ ((weak))
284 __pthread_initialize_minimal (void)
285 {
286   NONTLS_INIT_TP;
287 }
288
289 #endif