Add example hellow.c program and script for building it
[glibc/nacl-glibc.git] / csu / libc-tls.c
blob8de3f8eb1a27d8647b1d9365e89a6e9ab5c129e8
1 /* Initialization code for TLS in statically linked application.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006 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 extern ElfW(Phdr) *_dl_phdr;
33 extern size_t _dl_phnum;
36 static dtv_t static_dtv[2 + TLS_SLOTINFO_SURPLUS];
39 static struct
41 struct dtv_slotinfo_list si;
42 /* The dtv_slotinfo_list data structure does not include the actual
43 information since it is defined as an array of size zero. We define
44 here the necessary entries. Note that it is not important whether
45 there is padding or not since we will always access the information
46 through the 'si' element. */
47 struct dtv_slotinfo info[2 + TLS_SLOTINFO_SURPLUS];
48 } static_slotinfo;
50 /* Fake link map for the application. */
51 static struct link_map static_map;
54 /* Highest dtv index currently needed. */
55 size_t _dl_tls_max_dtv_idx;
56 /* Flag signalling whether there are gaps in the module ID allocation. */
57 bool _dl_tls_dtv_gaps;
58 /* Information about the dtv slots. */
59 struct dtv_slotinfo_list *_dl_tls_dtv_slotinfo_list;
60 /* Number of modules in the static TLS block. */
61 size_t _dl_tls_static_nelem;
62 /* Size of the static TLS block. Giving this initialized value
63 preallocates some surplus bytes in the static TLS area. */
64 size_t _dl_tls_static_size = 2048;
65 /* Size actually allocated in the static TLS block. */
66 size_t _dl_tls_static_used;
67 /* Alignment requirement of the static TLS block. */
68 size_t _dl_tls_static_align;
70 /* Generation counter for the dtv. */
71 size_t _dl_tls_generation;
74 /* Additional definitions needed by TLS initialization. */
75 #ifdef TLS_INIT_HELPER
76 TLS_INIT_HELPER
77 #endif
79 static inline void
80 init_slotinfo (void)
82 /* Create the slotinfo list. */
83 static_slotinfo.si.len = (((char *) (&static_slotinfo + 1)
84 - (char *) &static_slotinfo.si.slotinfo[0])
85 / sizeof static_slotinfo.si.slotinfo[0]);
86 // static_slotinfo.si.next = NULL; already zero
88 /* The slotinfo list. Will be extended by the code doing dynamic
89 linking. */
90 GL(dl_tls_max_dtv_idx) = 1;
91 GL(dl_tls_dtv_slotinfo_list) = &static_slotinfo.si;
94 static inline void
95 init_static_tls (size_t memsz, size_t align)
97 /* That is the size of the TLS memory for this object. The initialized
98 value of _dl_tls_static_size is provided by dl-open.c to request some
99 surplus that permits dynamic loading of modules with IE-model TLS. */
100 GL(dl_tls_static_size) = roundup (memsz + GL(dl_tls_static_size),
101 TLS_TCB_ALIGN);
102 GL(dl_tls_static_used) = memsz;
103 /* The alignment requirement for the static TLS block. */
104 GL(dl_tls_static_align) = align;
105 /* Number of elements in the static TLS block. */
106 GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
109 void *__tls_template_start;
110 void *__tls_template_tdata_end;
111 void *__tls_template_end;
113 __asm__(".global __gnacl_tls_init; __gnacl_tls_init = 0x1a40 ");
114 int __gnacl_tls_init(void const *buf, size_t size);
116 void
117 __libc_setup_tls (size_t tcbsize, size_t tcbalign)
119 void *tlsblock;
120 size_t memsz = 0;
121 size_t filesz = 0;
122 void *initimage = NULL;
123 size_t align = 0;
124 size_t max_align = tcbalign;
125 size_t tcb_offset;
126 ElfW(Phdr) *phdr;
128 #if 0
129 /* Look through the TLS segment if there is any. */
130 if (_dl_phdr != NULL)
131 for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr)
132 if (phdr->p_type == PT_TLS)
134 /* Remember the values we need. */
135 memsz = phdr->p_memsz;
136 filesz = phdr->p_filesz;
137 initimage = (void *) phdr->p_vaddr;
138 align = phdr->p_align;
139 if (phdr->p_align > max_align)
140 max_align = phdr->p_align;
141 break;
143 #endif
144 initimage = (void *) &__tls_template_start;
145 memsz = (size_t) &__tls_template_end - (size_t) &__tls_template_start;
146 filesz = (size_t) &__tls_template_tdata_end - (size_t) &__tls_template_start;
147 align = 4;
149 /* We have to set up the TCB block which also (possibly) contains
150 'errno'. Therefore we avoid 'malloc' which might touch 'errno'.
151 Instead we use 'sbrk' which would only uses 'errno' if it fails.
152 In this case we are right away out of memory and the user gets
153 what she/he deserves.
155 The initialized value of _dl_tls_static_size is provided by dl-open.c
156 to request some surplus that permits dynamic loading of modules with
157 IE-model TLS. */
158 #if TLS_TCB_AT_TP
159 tcb_offset = roundup (memsz + GL(dl_tls_static_size), tcbalign);
160 tlsblock = __sbrk (tcb_offset + tcbsize + max_align);
161 #elif TLS_DTV_AT_TP
162 tcb_offset = roundup (tcbsize, align ?: 1);
163 tlsblock = __sbrk (tcb_offset + memsz + max_align
164 + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size));
165 tlsblock += TLS_PRE_TCB_SIZE;
166 #else
167 /* In case a model with a different layout for the TCB and DTV
168 is defined add another #elif here and in the following #ifs. */
169 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
170 #endif
172 /* Align the TLS block. */
173 tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
174 & ~(max_align - 1));
176 /* Initialize the dtv. [0] is the length, [1] the generation counter. */
177 static_dtv[0].counter = (sizeof (static_dtv) / sizeof (static_dtv[0])) - 2;
178 // static_dtv[1].counter = 0; would be needed if not already done
180 /* Initialize the TLS block. */
181 #if TLS_TCB_AT_TP
182 static_dtv[2].pointer.val = ((char *) tlsblock + tcb_offset
183 - roundup (memsz, align ?: 1));
184 static_map.l_tls_offset = roundup (memsz, align ?: 1);
185 #elif TLS_DTV_AT_TP
186 static_dtv[2].pointer.val = (char *) tlsblock + tcb_offset;
187 static_map.l_tls_offset = tcb_offset;
188 #else
189 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
190 #endif
191 static_dtv[2].pointer.is_static = true;
192 /* sbrk gives us zero'd memory, so we don't need to clear the remainder. */
193 memcpy (static_dtv[2].pointer.val, initimage, filesz);
195 /* Install the pointer to the dtv. */
197 /* Initialize the thread pointer. */
198 #if TLS_TCB_AT_TP
199 INSTALL_DTV ((char *) tlsblock + tcb_offset, static_dtv);
201 //const char *lossage = TLS_INIT_TP ((char *) tlsblock + tcb_offset, 0);
202 __gnacl_tls_init (tlsblock + tcb_offset, 100);
203 tcbhead_t *head = (void *) tlsblock + tcb_offset;
204 head->tcb = head;
205 head->self = head;
206 const char *lossage = NULL;
207 #elif TLS_DTV_AT_TP
208 # error "not supported"
209 INSTALL_DTV (tlsblock, static_dtv);
210 const char *lossage = TLS_INIT_TP (tlsblock, 0);
211 #else
212 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
213 #endif
214 if (__builtin_expect (lossage != NULL, 0))
215 __libc_fatal (lossage);
217 /* We have to create a fake link map which normally would be created
218 by the dynamic linker. It just has to have enough information to
219 make the TLS routines happy. */
220 static_map.l_tls_align = align;
221 static_map.l_tls_blocksize = memsz;
222 static_map.l_tls_initimage = initimage;
223 static_map.l_tls_initimage_size = filesz;
224 static_map.l_type = lt_executable;
225 static_map.l_tls_modid = 1;
227 init_slotinfo ();
228 // static_slotinfo.si.slotinfo[1].gen = 0; already zero
229 static_slotinfo.si.slotinfo[1].map = &static_map;
231 memsz = roundup (memsz, align ?: 1);
233 #if TLS_TCB_AT_TP
234 memsz += tcbsize;
235 #elif TLS_DTV_AT_TP
236 memsz += tcb_offset;
237 #endif
239 init_static_tls (memsz, MAX (TLS_TCB_ALIGN, max_align));
242 /* This is called only when the data structure setup was skipped at startup,
243 when there was no need for it then. Now we have dynamically loaded
244 something needing TLS, or libpthread needs it. */
246 internal_function
247 _dl_tls_setup (void)
249 init_slotinfo ();
250 init_static_tls (
251 #if TLS_TCB_AT_TP
252 TLS_TCB_SIZE,
253 #else
255 #endif
256 TLS_TCB_ALIGN);
257 return 0;
261 /* This is the minimal initialization function used when libpthread is
262 not used. */
263 void
264 __attribute__ ((weak))
265 __pthread_initialize_minimal (void)
267 __libc_setup_tls (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN);