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.
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, see
17 <http://www.gnu.org/licenses/>. */
24 #include <sys/param.h>
32 #error makefile bug, this file is for static only
36 extern ElfW(Phdr
) *_dl_phdr
;
37 extern size_t _dl_phnum
;
40 /* phdr->p_vaddr is not valid in FDPIC mode. To find tdata start we
41 use the linker script defined symbol __tdata_start. */
42 extern int __tdata_start
;
48 dtv_t static_dtv
[2 + TLS_SLOTINFO_SURPLUS
];
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 struct dtv_slotinfo info
[2 + TLS_SLOTINFO_SURPLUS
];
62 /* Fake link map for the application. */
63 static struct link_map static_map
;
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
;
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
;
81 /* Generation counter for the dtv. */
82 size_t _dl_tls_generation
;
85 /* Additional definitions needed by TLS initialization. */
86 #ifdef TLS_INIT_HELPER
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
99 /* The slotinfo list. Will be extended by the code doing dynamic
101 GL(dl_tls_max_dtv_idx
) = 1;
102 GL(dl_tls_dtv_slotinfo_list
) = &static_slotinfo
.si
;
106 init_static_tls (size_t memsz
, size_t align
)
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
),
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
);
120 void __libc_setup_tls (size_t tcbsize
, size_t tcbalign
);
122 __libc_setup_tls (size_t tcbsize
, size_t tcbalign
)
127 void *initimage
= NULL
;
129 size_t max_align
= tcbalign
;
133 /* Look through the TLS segment if there is any. */
134 if (_dl_phdr
!= NULL
)
135 for (phdr
= _dl_phdr
; phdr
< &_dl_phdr
[_dl_phnum
]; ++phdr
)
136 if (phdr
->p_type
== PT_TLS
)
138 /* Remember the values we need. */
139 memsz
= phdr
->p_memsz
;
140 filesz
= phdr
->p_filesz
;
142 initimage
= (void *) &__tdata_start
;
144 initimage
= (void *) phdr
->p_vaddr
;
146 align
= phdr
->p_align
;
147 if (phdr
->p_align
> max_align
)
148 max_align
= phdr
->p_align
;
152 /* We have to set up the TCB block which also (possibly) contains
153 'errno'. Therefore we avoid 'malloc' which might touch 'errno'.
154 Instead we use 'sbrk' which would only uses 'errno' if it fails.
155 In this case we are right away out of memory and the user gets
156 what she/he deserves.
158 The initialized value of _dl_tls_static_size is provided by dl-open.c
159 to request some surplus that permits dynamic loading of modules with
161 /* Use mmap instead of sbrk since this commit disables sbrk area
162 for FDPIC MMU-less platforms:
163 fs/binfmt_elf_fdpic.c: fix brk area overlap with stack on NOMMU
164 https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/fs/binfmt_elf_fdpic.c?id=4ac313111018cb44ecc250445de5ccb93026a980
166 # if defined(TLS_TCB_AT_TP)
167 tcb_offset
= roundup (memsz
+ GL(dl_tls_static_size
), tcbalign
);
168 # if defined(__FDPIC__)
169 tlsblock
= mmap (NULL
, tcb_offset
+ tcbsize
+ max_align
,
170 PROT_READ
|PROT_WRITE
, MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
172 tlsblock
= sbrk (tcb_offset
+ tcbsize
+ max_align
);
174 # elif defined(TLS_DTV_AT_TP)
175 tcb_offset
= roundup (tcbsize
, align
?: 1);
176 # if defined(__FDPIC__)
177 tlsblock
= mmap (NULL
, tcb_offset
+ memsz
+ max_align
+ TLS_PRE_TCB_SIZE
+ GL(dl_tls_static_size
),
178 PROT_READ
|PROT_WRITE
, MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
180 tlsblock
= sbrk (tcb_offset
+ memsz
+ max_align
181 + TLS_PRE_TCB_SIZE
+ GL(dl_tls_static_size
));
183 memset(tlsblock
, '\0', tcb_offset
+ memsz
+ max_align
+ TLS_PRE_TCB_SIZE
+ GL(dl_tls_static_size
));
184 tlsblock
+= TLS_PRE_TCB_SIZE
;
186 /* In case a model with a different layout for the TCB and DTV
187 is defined add another #elif here and in the following #ifs. */
188 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
191 /* Align the TLS block. */
192 tlsblock
= (void *) (((uintptr_t) tlsblock
+ max_align
- 1)
195 /* Initialize the dtv. [0] is the length, [1] the generation counter. */
196 static_dtv
[0].counter
= (sizeof (static_dtv
) / sizeof (static_dtv
[0])) - 2;
197 // static_dtv[1].counter = 0; would be needed if not already done
199 /* Initialize the TLS block. */
200 # if defined(TLS_TCB_AT_TP)
201 static_dtv
[2].pointer
.val
= ((char *) tlsblock
+ tcb_offset
202 - roundup (memsz
, align
?: 1));
203 static_map
.l_tls_offset
= roundup (memsz
, align
?: 1);
204 # elif defined(TLS_DTV_AT_TP)
205 static_dtv
[2].pointer
.val
= (char *) tlsblock
+ tcb_offset
;
206 static_map
.l_tls_offset
= tcb_offset
;
208 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
210 static_dtv
[2].pointer
.is_static
= true;
211 /* sbrk gives us zero'd memory, so we don't need to clear the remainder. */
212 memcpy (static_dtv
[2].pointer
.val
, initimage
, filesz
);
214 /* Install the pointer to the dtv. */
216 /* Initialize the thread pointer. */
217 # if defined(TLS_TCB_AT_TP)
218 INSTALL_DTV ((char *) tlsblock
+ tcb_offset
, static_dtv
);
220 const char *lossage
= TLS_INIT_TP ((char *) tlsblock
+ tcb_offset
, 0);
221 # elif defined(TLS_DTV_AT_TP)
222 INSTALL_DTV (tlsblock
, static_dtv
);
223 const char *lossage
= (char *)TLS_INIT_TP (tlsblock
, 0);
225 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
227 if (__builtin_expect (lossage
!= NULL
, 0))
230 /* We have to create a fake link map which normally would be created
231 by the dynamic linker. It just has to have enough information to
232 make the TLS routines happy. */
233 static_map
.l_tls_align
= align
;
234 static_map
.l_tls_blocksize
= memsz
;
235 static_map
.l_tls_initimage
= initimage
;
236 static_map
.l_tls_initimage_size
= filesz
;
237 static_map
.l_tls_modid
= 1;
240 // static_slotinfo.si.slotinfo[1].gen = 0; already zero
241 static_slotinfo
.si
.slotinfo
[1].map
= &static_map
;
243 memsz
= roundup (memsz
, align
?: 1);
245 # if defined(TLS_TCB_AT_TP)
247 # elif defined(TLS_DTV_AT_TP)
251 init_static_tls (memsz
, MAX (TLS_TCB_ALIGN
, max_align
));
254 /* This is called only when the data structure setup was skipped at startup,
255 when there was no need for it then. Now we have dynamically loaded
256 something needing TLS, or libpthread needs it. */
263 # if defined(TLS_TCB_AT_TP)
272 extern void __pthread_initialize_minimal(void) __attribute__((weak
));
274 /* This is the minimal initialization function used when libpthread is
277 __attribute__ ((weak
))
278 __pthread_initialize_minimal (void)
280 __libc_setup_tls (TLS_INIT_TCB_SIZE
, TLS_INIT_TCB_ALIGN
);