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