]>
Commit | Line | Data |
---|---|---|
8a30f00f UD |
1 | /* Initialization code for TLS in statically linked application. |
2 | Copyright (C) 2002 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 <ldsodefs.h> | |
21 | #include <tls.h> | |
22 | #include <unistd.h> | |
23 | ||
24 | ||
25 | #ifdef USE_TLS | |
26 | extern ElfW(Phdr) *_dl_phdr; | |
27 | extern size_t _dl_phnum; | |
28 | ||
29 | ||
30 | /* DTV with just one element plus overhead. */ | |
31 | static dtv_t static_dtv[3]; | |
32 | ||
33 | ||
34 | static struct | |
35 | { | |
36 | struct dtv_slotinfo_list si; | |
37 | /* The dtv_slotinfo_list data structure does not include the actual | |
38 | informatin since it is defined as an array of size zero. We | |
39 | define here the necessary entries. Not that it is not important | |
40 | whether there is padding or not since we will always access the | |
41 | informatin through the 'si' element. */ | |
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 | ||
49 | void | |
50 | __libc_setup_tls (size_t tcbsize, size_t tcbalign) | |
51 | { | |
52 | void *tlsblock; | |
53 | size_t memsz = 0; | |
54 | size_t filesz = 0; | |
a816b435 | 55 | size_t initimage = 0; |
8a30f00f UD |
56 | size_t align = 0; |
57 | size_t max_align = tcbalign; | |
58 | size_t loadaddr = ~0ul; | |
59 | size_t tcb_offset; | |
a162642d | 60 | ElfW(Phdr) *phdr; |
8a30f00f UD |
61 | |
62 | /* Look through the TLS segment if there is any. */ | |
63 | if (_dl_phdr != NULL) | |
a162642d RM |
64 | for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr) |
65 | { | |
8a30f00f UD |
66 | if (phdr->p_type == PT_TLS) |
67 | { | |
68 | /* Remember the values we need. */ | |
69 | memsz = phdr->p_memsz; | |
70 | filesz = phdr->p_filesz; | |
a816b435 | 71 | initimage = phdr->p_vaddr; |
8a30f00f UD |
72 | align = phdr->p_align; |
73 | if (phdr->p_align > max_align) | |
74 | max_align = phdr->p_align; | |
75 | } | |
76 | else if (phdr->p_type == PT_LOAD) | |
77 | { | |
78 | /* We have to find the load address which is not easy. | |
79 | Look for the load segment with the lowest address. */ | |
80 | if (phdr->p_vaddr < loadaddr) | |
a162642d | 81 | loadaddr = phdr->p_vaddr; |
8a30f00f UD |
82 | } |
83 | } | |
84 | ||
85 | if (memsz == 0 && tcbsize == 0) | |
86 | /* We do not need a TLS block and no thread descriptor. */ | |
87 | return; | |
88 | ||
89 | /* We have to set up the TCB block which also (possibly) contains | |
90 | 'errno'. Therefore we avoid 'malloc' which might touch 'errno'. | |
91 | Instead we use 'sbrk' which would only uses 'errno' if it fails. | |
92 | In this case we are right away out of memory and the user gets | |
93 | what she/he deserves. */ | |
94 | # if TLS_TCB_AT_TP | |
95 | tlsblock = __sbrk (roundup (memsz, tcbalign) + tcbsize + max_align); | |
96 | # elif TLS_DTV_AT_TP | |
97 | tlsblock = __sbrk (roundup (tcbsize, align) + memsz + max_align); | |
98 | # else | |
99 | /* In case a model with a different layout for the TCB and DTV | |
100 | is defined add another #elif here and in the following #ifs. */ | |
101 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" | |
102 | # endif | |
103 | ||
104 | /* Align the TLS block. */ | |
105 | tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1) | |
106 | & ~(max_align - 1)); | |
107 | ||
108 | /* Initialize the dtv. [0] is the length, [1] the generation counter. */ | |
109 | static_dtv[0].counter = 1; | |
110 | // static_dtv[1].counter = 0; would be needed if not already done | |
111 | ||
112 | /* Initialize the TLS block. */ | |
113 | # if TLS_TCB_AT_TP | |
114 | static_dtv[2].pointer = tlsblock; | |
115 | # elif TLS_DTV_AT_TP | |
116 | tcb_offset = roundup (tcbsize, align); | |
117 | static_dtv[2].pointer = (char *) tlsblock + tcb_offset; | |
118 | # else | |
119 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" | |
120 | # endif | |
a816b435 | 121 | memset (__mempcpy (static_dtv[2].pointer, (char *) loadaddr + initimage, |
8a30f00f UD |
122 | filesz), |
123 | '\0', memsz - filesz); | |
124 | ||
125 | /* Install the pointer to the dtv. */ | |
126 | ||
127 | /* Initialize the thread pointer. */ | |
128 | # if TLS_TCB_AT_TP | |
129 | tcb_offset = roundup (memsz, tcbalign); | |
130 | ||
131 | INSTALL_DTV ((char *) tlsblock + tcb_offset, static_dtv); | |
132 | ||
133 | TLS_INIT_TP ((char *) tlsblock + tcb_offset); | |
134 | # elif TLS_DTV_AT_TP | |
135 | INSTALL_DTV (tlsblock, static_dtv); | |
136 | TLS_INIT_TP (tlsblock); | |
137 | # else | |
138 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" | |
139 | # endif | |
140 | ||
141 | /* We have to create a fake link map which normally would be created | |
a816b435 | 142 | by the dynamic linker. It just has to have enough information to |
8a30f00f UD |
143 | make the TLS routines happy. */ |
144 | static_map.l_tls_align = align; | |
145 | static_map.l_tls_blocksize = memsz; | |
146 | static_map.l_tls_initimage_size = filesz; | |
a816b435 | 147 | static_map.l_tls_offset = tcb_offset; |
8a30f00f UD |
148 | static_map.l_type = lt_executable; |
149 | static_map.l_tls_modid = 1; | |
150 | ||
151 | /* Create the slotinfo list. */ | |
152 | static_slotinfo.si.len = 1; /* Only one element. */ | |
153 | // static_slotinfo.si.next = NULL; already zero | |
154 | ||
155 | static_slotinfo.si.slotinfo[1].gen = 0; | |
156 | static_slotinfo.si.slotinfo[1].map = &static_map; | |
157 | ||
158 | /* The slotinfo list. Will be extended by the code doing dynamic | |
159 | linking. */ | |
160 | GL(dl_tls_max_dtv_idx) = 1; | |
161 | GL(dl_tls_dtv_slotinfo_list) = &static_slotinfo.si; | |
162 | ||
163 | /* That is the size of the TLS memory for this object. */ | |
164 | # if TLS_TCB_AT_TP | |
165 | GL(dl_tls_static_size) = roundup (memsz, align ?: 1) + tcbsize; | |
166 | #else | |
167 | GL(dl_tls_static_size) = roundup (memsz, align ?: 1); | |
168 | #endif | |
169 | } | |
170 | ||
171 | ||
172 | /* This is the minimal initialization function used when libpthread is | |
173 | not used. */ | |
174 | void | |
175 | __attribute__ ((weak)) | |
176 | __pthread_initialize_minimal (void) | |
177 | { | |
178 | __libc_setup_tls (0, 1); | |
179 | } | |
180 | #endif |