nptl: Replace sbrk with mmap
[uclibc-ng.git] / libpthread / nptl / sysdeps / generic / libc-tls.c
bloba6df4cdc475ae02ddd903b2461361c076304993e
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/>. */
19 #include <errno.h>
20 #include <ldsodefs.h>
21 #include <tls.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <sys/param.h>
25 #include <elf.h>
26 #include <link.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <sys/mman.h>
31 #ifdef SHARED
32 #error makefile bug, this file is for static only
33 #endif
35 #if USE_TLS
36 extern ElfW(Phdr) *_dl_phdr;
37 extern size_t _dl_phnum;
39 #ifdef __FDPIC__
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;
43 #endif
45 static dtv_t static_dtv[2 + TLS_SLOTINFO_SURPLUS];
48 static struct
50 struct dtv_slotinfo_list si;
51 /* The dtv_slotinfo_list data structure does not include the actual
52 information since it is defined as an array of size zero. We define
53 here the necessary entries. Note that it is not important whether
54 there is padding or not since we will always access the information
55 through the 'si' element. */
56 struct dtv_slotinfo info[2 + TLS_SLOTINFO_SURPLUS];
57 } static_slotinfo;
59 /* Fake link map for the application. */
60 static struct link_map static_map;
63 /* Highest dtv index currently needed. */
64 size_t _dl_tls_max_dtv_idx;
65 /* Flag signalling whether there are gaps in the module ID allocation. */
66 bool _dl_tls_dtv_gaps;
67 /* Information about the dtv slots. */
68 struct dtv_slotinfo_list *_dl_tls_dtv_slotinfo_list;
69 /* Number of modules in the static TLS block. */
70 size_t _dl_tls_static_nelem;
71 /* Size of the static TLS block. */
72 size_t _dl_tls_static_size;
73 /* Size actually allocated in the static TLS block. */
74 size_t _dl_tls_static_used;
75 /* Alignment requirement of the static TLS block. */
76 size_t _dl_tls_static_align;
78 /* Generation counter for the dtv. */
79 size_t _dl_tls_generation;
82 /* Additional definitions needed by TLS initialization. */
83 #ifdef TLS_INIT_HELPER
84 TLS_INIT_HELPER
85 #endif
87 static inline void
88 init_slotinfo (void)
90 /* Create the slotinfo list. */
91 static_slotinfo.si.len = (((char *) (&static_slotinfo + 1)
92 - (char *) &static_slotinfo.si.slotinfo[0])
93 / sizeof static_slotinfo.si.slotinfo[0]);
94 // static_slotinfo.si.next = NULL; already zero
96 /* The slotinfo list. Will be extended by the code doing dynamic
97 linking. */
98 GL(dl_tls_max_dtv_idx) = 1;
99 GL(dl_tls_dtv_slotinfo_list) = &static_slotinfo.si;
102 static inline void
103 init_static_tls (size_t memsz, size_t align)
105 /* That is the size of the TLS memory for this object. The initialized
106 value of _dl_tls_static_size is provided by dl-open.c to request some
107 surplus that permits dynamic loading of modules with IE-model TLS. */
108 GL(dl_tls_static_size) = roundup (memsz + GL(dl_tls_static_size),
109 TLS_TCB_ALIGN);
110 GL(dl_tls_static_used) = memsz;
111 /* The alignment requirement for the static TLS block. */
112 GL(dl_tls_static_align) = align;
113 /* Number of elements in the static TLS block. */
114 GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
117 void __libc_setup_tls (size_t tcbsize, size_t tcbalign);
118 void
119 __libc_setup_tls (size_t tcbsize, size_t tcbalign)
121 void *tlsblock;
122 size_t memsz = 0;
123 size_t filesz = 0;
124 void *initimage = NULL;
125 size_t align = 0;
126 size_t max_align = tcbalign;
127 size_t tcb_offset;
128 ElfW(Phdr) *phdr;
130 /* Look through the TLS segment if there is any. */
131 if (_dl_phdr != NULL)
132 for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr)
133 if (phdr->p_type == PT_TLS)
135 /* Remember the values we need. */
136 memsz = phdr->p_memsz;
137 filesz = phdr->p_filesz;
138 #ifdef __FDPIC__
139 initimage = (void *) &__tdata_start;
140 #else
141 initimage = (void *) phdr->p_vaddr;
142 #endif
143 align = phdr->p_align;
144 if (phdr->p_align > max_align)
145 max_align = phdr->p_align;
146 break;
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 /* Use mmap instead of sbrk since this commit disables sbrk area
159 for FDPIC MMU-less platforms:
160 fs/binfmt_elf_fdpic.c: fix brk area overlap with stack on NOMMU
161 https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/fs/binfmt_elf_fdpic.c?id=4ac313111018cb44ecc250445de5ccb93026a980
163 # if defined(TLS_TCB_AT_TP)
164 tcb_offset = roundup (memsz + GL(dl_tls_static_size), tcbalign);
165 # if defined(__FDPIC__)
166 tlsblock = mmap (NULL, tcb_offset + tcbsize + max_align,
167 PROT_READ|PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
168 # else
169 tlsblock = sbrk (tcb_offset + tcbsize + max_align);
170 # endif
171 # elif defined(TLS_DTV_AT_TP)
172 tcb_offset = roundup (tcbsize, align ?: 1);
173 # if defined(__FDPIC__)
174 tlsblock = mmap (NULL, tcb_offset + memsz + max_align + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size),
175 PROT_READ|PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
176 # else
177 tlsblock = sbrk (tcb_offset + memsz + max_align
178 + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size));
179 # endif
180 memset(tlsblock, '\0', tcb_offset + memsz + max_align + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size));
181 tlsblock += TLS_PRE_TCB_SIZE;
182 # else
183 /* In case a model with a different layout for the TCB and DTV
184 is defined add another #elif here and in the following #ifs. */
185 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
186 # endif
188 /* Align the TLS block. */
189 tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
190 & ~(max_align - 1));
192 /* Initialize the dtv. [0] is the length, [1] the generation counter. */
193 static_dtv[0].counter = (sizeof (static_dtv) / sizeof (static_dtv[0])) - 2;
194 // static_dtv[1].counter = 0; would be needed if not already done
196 /* Initialize the TLS block. */
197 # if defined(TLS_TCB_AT_TP)
198 static_dtv[2].pointer.val = ((char *) tlsblock + tcb_offset
199 - roundup (memsz, align ?: 1));
200 static_map.l_tls_offset = roundup (memsz, align ?: 1);
201 # elif defined(TLS_DTV_AT_TP)
202 static_dtv[2].pointer.val = (char *) tlsblock + tcb_offset;
203 static_map.l_tls_offset = tcb_offset;
204 # else
205 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
206 # endif
207 static_dtv[2].pointer.is_static = true;
208 /* sbrk gives us zero'd memory, so we don't need to clear the remainder. */
209 memcpy (static_dtv[2].pointer.val, initimage, filesz);
211 /* Install the pointer to the dtv. */
213 /* Initialize the thread pointer. */
214 # if defined(TLS_TCB_AT_TP)
215 INSTALL_DTV ((char *) tlsblock + tcb_offset, static_dtv);
217 const char *lossage = TLS_INIT_TP ((char *) tlsblock + tcb_offset, 0);
218 # elif defined(TLS_DTV_AT_TP)
219 INSTALL_DTV (tlsblock, static_dtv);
220 const char *lossage = (char *)TLS_INIT_TP (tlsblock, 0);
221 # else
222 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
223 # endif
224 if (__builtin_expect (lossage != NULL, 0))
225 abort();
227 /* We have to create a fake link map which normally would be created
228 by the dynamic linker. It just has to have enough information to
229 make the TLS routines happy. */
230 static_map.l_tls_align = align;
231 static_map.l_tls_blocksize = memsz;
232 static_map.l_tls_initimage = initimage;
233 static_map.l_tls_initimage_size = filesz;
234 static_map.l_tls_modid = 1;
236 init_slotinfo ();
237 // static_slotinfo.si.slotinfo[1].gen = 0; already zero
238 static_slotinfo.si.slotinfo[1].map = &static_map;
240 memsz = roundup (memsz, align ?: 1);
242 # if defined(TLS_TCB_AT_TP)
243 memsz += tcbsize;
244 # elif defined(TLS_DTV_AT_TP)
245 memsz += tcb_offset;
246 # endif
248 init_static_tls (memsz, MAX (TLS_TCB_ALIGN, max_align));
251 /* This is called only when the data structure setup was skipped at startup,
252 when there was no need for it then. Now we have dynamically loaded
253 something needing TLS, or libpthread needs it. */
255 internal_function
256 _dl_tls_setup (void)
258 init_slotinfo ();
259 init_static_tls (
260 # if defined(TLS_TCB_AT_TP)
261 TLS_TCB_SIZE,
262 # else
264 # endif
265 TLS_TCB_ALIGN);
266 return 0;
269 extern void __pthread_initialize_minimal(void) __attribute__((weak));
271 /* This is the minimal initialization function used when libpthread is
272 not used. */
273 void
274 __attribute__ ((weak))
275 __pthread_initialize_minimal (void)
277 __libc_setup_tls (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN);
280 #endif