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