Include <sys/types.h> instead of <stddef.h> since ssize_t is needed as well. Replace...
[glibc.git] / sysdeps / generic / dl-tls.c
blobc9694644493908a50a14a934a454b14babe27744
1 /* Thread-local storage handling in the ELF dynamic linker. Generic version.
2 Copyright (C) 2002 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/param.h>
26 #include <tls.h>
28 /* We don't need any of this if TLS is not supported. */
29 #ifdef USE_TLS
31 # include <dl-tls.h>
32 # include <ldsodefs.h>
34 /* Value used for dtv entries for which the allocation is delayed. */
35 # define TLS_DTV_UNALLOCATED ((void *) -1l)
38 /* Out-of-memory handler. */
39 # ifdef SHARED
40 static void
41 __attribute__ ((__noreturn__))
42 oom (void)
44 _dl_fatal_printf ("cannot allocate memory for thread-local data: ABORT\n");
46 # endif
50 size_t
51 internal_function
52 _dl_next_tls_modid (void)
54 size_t result;
56 if (__builtin_expect (GL(dl_tls_dtv_gaps), false))
58 size_t disp = 0;
59 struct dtv_slotinfo_list *runp = GL(dl_tls_dtv_slotinfo_list);
61 /* Note that this branch will never be executed during program
62 start since there are no gaps at that time. Therefore it
63 does not matter that the dl_tls_dtv_slotinfo is not allocated
64 yet when the function is called for the first times. */
65 result = GL(dl_tls_static_nelem) + 1;
66 /* If the following would not be true we mustn't have assumed
67 there is a gap. */
68 assert (result <= GL(dl_tls_max_dtv_idx));
71 while (result - disp < runp->len)
73 if (runp->slotinfo[result - disp].map == NULL)
74 break;
76 ++result;
77 assert (result <= GL(dl_tls_max_dtv_idx) + 1);
80 if (result - disp < runp->len)
81 break;
83 disp += runp->len;
85 while ((runp = runp->next) != NULL);
87 if (result >= GL(dl_tls_max_dtv_idx))
89 /* The new index must indeed be exactly one higher than the
90 previous high. */
91 assert (result == GL(dl_tls_max_dtv_idx));
93 /* There is no gap anymore. */
94 GL(dl_tls_dtv_gaps) = false;
96 goto nogaps;
99 else
101 /* No gaps, allocate a new entry. */
102 nogaps:
103 result = ++GL(dl_tls_max_dtv_idx);
106 return result;
110 void
111 internal_function
112 _dl_determine_tlsoffset (void)
114 struct dtv_slotinfo *slotinfo;
115 size_t max_align = __alignof__ (void *);
116 size_t offset;
117 size_t cnt;
119 /* The first element of the dtv slot info list is allocated. */
120 assert (GL(dl_tls_dtv_slotinfo_list) != NULL);
121 /* There is at this point only one element in the
122 dl_tls_dtv_slotinfo_list list. */
123 assert (GL(dl_tls_dtv_slotinfo_list)->next == NULL);
125 # if TLS_TCB_AT_TP
126 /* We simply start with zero. */
127 offset = 0;
129 slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo;
130 for (cnt = 1; slotinfo[cnt].map != NULL; ++cnt)
132 assert (cnt < GL(dl_tls_dtv_slotinfo_list)->len);
134 max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
136 /* Compute the offset of the next TLS block. */
137 offset = roundup (offset + slotinfo[cnt].map->l_tls_blocksize,
138 slotinfo[cnt].map->l_tls_align);
140 /* XXX For some architectures we perhaps should store the
141 negative offset. */
142 slotinfo[cnt].map->l_tls_offset = offset;
145 /* The thread descriptor (pointed to by the thread pointer) has its
146 own alignment requirement. Adjust the static TLS size
147 and TLS offsets appropriately. */
148 // XXX How to deal with this. We cannot simply add zero bytes
149 // XXX after the first (closest to the TCB) TLS block since this
150 // XXX would invalidate the offsets the linker creates for the LE
151 // XXX model.
153 GL(dl_tls_static_size) = offset + TLS_TCB_SIZE;
154 # elif TLS_DTV_AT_TP
155 /* The TLS blocks start right after the TCB. */
156 offset = TLS_TCB_SIZE;
158 /* The first block starts right after the TCB. */
159 slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo;
160 if (slotinfo[1].map != NULL)
162 size_t prev_size;
164 offset = roundup (offset, slotinfo[1].map->l_tls_align);
165 slotinfo[1].map->l_tls_offset = offset;
166 max_align = slotinfo[1].map->l_tls_align;
167 prev_size = slotinfo[1].map->l_tls_blocksize;
169 for (cnt = 2; slotinfo[cnt].map != NULL; ++cnt)
171 assert (cnt < GL(dl_tls_dtv_slotinfo_list)->len);
173 max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
175 /* Compute the offset of the next TLS block. */
176 offset = roundup (offset + prev_size,
177 slotinfo[cnt].map->l_tls_align);
179 /* XXX For some architectures we perhaps should store the
180 negative offset. */
181 slotinfo[cnt].map->l_tls_offset = offset;
183 prev_size = slotinfo[cnt].map->l_tls_blocksize;
186 offset += prev_size;
189 GL(dl_tls_static_size) = offset;
190 # else
191 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
192 # endif
194 /* The alignment requirement for the static TLS block. */
195 GL(dl_tls_static_align) = MAX (TLS_TCB_ALIGN, max_align);
199 static void *
200 internal_function
201 allocate_dtv (void *result)
203 dtv_t *dtv;
204 size_t dtv_length;
206 /* We allocate a few more elements in the dtv than are needed for the
207 initial set of modules. This should avoid in most cases expansions
208 of the dtv. */
209 dtv_length = GL(dl_tls_max_dtv_idx) + DTV_SURPLUS;
210 dtv = (dtv_t *) malloc ((dtv_length + 2) * sizeof (dtv_t));
211 if (dtv != NULL)
213 /* This is the initial length of the dtv. */
214 dtv[0].counter = dtv_length;
215 /* Initialize all of the rest of the dtv (including the
216 generation counter) with zero to indicate nothing there. */
217 memset (dtv + 1, '\0', (dtv_length + 1) * sizeof (dtv_t));
219 /* Add the dtv to the thread data structures. */
220 INSTALL_DTV (result, dtv);
222 else
223 result = NULL;
225 return result;
229 /* Get size and alignment requirements of the static TLS block. */
230 void
231 internal_function
232 _dl_get_tls_static_info (size_t *sizep, size_t *alignp)
234 *sizep = GL(dl_tls_static_size);
235 *alignp = GL(dl_tls_static_align);
239 void *
240 internal_function
241 _dl_allocate_tls_storage (void)
243 void *result;
245 /* Allocate a correctly aligned chunk of memory. */
246 result = __libc_memalign (GL(dl_tls_static_align), GL(dl_tls_static_size));
247 if (__builtin_expect (result != NULL, 0))
249 /* Allocate the DTV. */
250 void *allocated = result;
252 # if TLS_TCB_AT_TP
253 /* The TCB follows the TLS blocks. */
254 result = (char *) result + GL(dl_tls_static_size) - TLS_TCB_SIZE;
255 # endif
257 result = allocate_dtv (result);
258 if (result == NULL)
259 free (allocated);
262 return result;
266 void *
267 internal_function
268 _dl_allocate_tls_init (void *result)
270 dtv_t *dtv = GET_DTV (result);
271 struct dtv_slotinfo_list *listp;
272 size_t total = 0;
274 if (result == NULL)
275 /* The memory allocation failed. */
276 return NULL;
278 /* We have to look prepare the dtv for all currently loaded
279 modules using TLS. For those which are dynamically loaded we
280 add the values indicating deferred allocation. */
281 listp = GL(dl_tls_dtv_slotinfo_list);
282 while (1)
284 size_t cnt;
286 for (cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
288 struct link_map *map;
289 void *dest;
291 /* Check for the total number of used slots. */
292 if (total + cnt > GL(dl_tls_max_dtv_idx))
293 break;
295 map = listp->slotinfo[cnt].map;
296 if (map == NULL)
297 /* Unused entry. */
298 continue;
300 if (map->l_type == lt_loaded)
302 /* For dynamically loaded modules we simply store
303 the value indicating deferred allocation. */
304 dtv[map->l_tls_modid].pointer = TLS_DTV_UNALLOCATED;
305 continue;
308 assert (map->l_tls_modid == cnt);
309 assert (map->l_tls_blocksize >= map->l_tls_initimage_size);
310 # if TLS_TCB_AT_TP
311 assert (map->l_tls_offset >= map->l_tls_blocksize);
312 dest = (char *) result - map->l_tls_offset;
313 # elif TLS_DTV_AT_TP
314 dest = (char *) result + map->l_tls_offset;
315 # else
316 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
317 # endif
319 /* Copy the initialization image and clear the BSS part. */
320 dtv[map->l_tls_modid].pointer = dest;
321 memset (__mempcpy (dest, map->l_tls_initimage,
322 map->l_tls_initimage_size), '\0',
323 map->l_tls_blocksize - map->l_tls_initimage_size);
326 total += cnt;
327 if (total >= GL(dl_tls_max_dtv_idx))
328 break;
330 listp = listp->next;
331 assert (listp != NULL);
334 return result;
337 void *
338 internal_function
339 _dl_allocate_tls (void *mem)
341 return _dl_allocate_tls_init (mem == NULL
342 ? _dl_allocate_tls_storage ()
343 : allocate_dtv (mem));
345 INTDEF(_dl_allocate_tls)
348 void
349 internal_function
350 _dl_deallocate_tls (void *tcb, bool dealloc_tcb)
352 dtv_t *dtv = GET_DTV (tcb);
354 /* The array starts with dtv[-1]. */
355 free (dtv - 1);
357 if (dealloc_tcb)
358 free (tcb);
363 # ifdef SHARED
364 /* The __tls_get_addr function has two basic forms which differ in the
365 arguments. The IA-64 form takes two parameters, the module ID and
366 offset. The form used, among others, on IA-32 takes a reference to
367 a special structure which contain the same information. The second
368 form seems to be more often used (in the moment) so we default to
369 it. Users of the IA-64 form have to provide adequate definitions
370 of the following macros. */
371 # ifndef GET_ADDR_ARGS
372 # define GET_ADDR_ARGS tls_index *ti
373 # endif
374 # ifndef GET_ADDR_MODULE
375 # define GET_ADDR_MODULE ti->ti_module
376 # endif
377 # ifndef GET_ADDR_OFFSET
378 # define GET_ADDR_OFFSET ti->ti_offset
379 # endif
380 /* Systems which do not have tls_index also probably have to define
381 DONT_USE_TLS_INDEX. */
383 # ifndef __TLS_GET_ADDR
384 # define __TLS_GET_ADDR __tls_get_addr
385 # endif
388 /* Return the symbol address given the map of the module it is in and
389 the symbol record. This is used in dl-sym.c. */
390 void *
391 internal_function
392 _dl_tls_symaddr (struct link_map *map, const ElfW(Sym) *ref)
394 # ifndef DONT_USE_TLS_INDEX
395 tls_index tmp =
397 .ti_module = map->l_tls_modid,
398 .ti_offset = ref->st_value
401 return __TLS_GET_ADDR (&tmp);
402 # else
403 return __TLS_GET_ADDR (map->l_tls_modid, ref->st_value);
404 # endif
408 static void *
409 allocate_and_init (struct link_map *map)
411 void *newp;
413 newp = __libc_memalign (map->l_tls_align, map->l_tls_blocksize);
414 if (newp == NULL)
415 oom ();
417 /* Initialize the memory. */
418 memset (__mempcpy (newp, map->l_tls_initimage, map->l_tls_initimage_size),
419 '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
421 return newp;
425 /* The generic dynamic and local dynamic model cannot be used in
426 statically linked applications. */
427 void *
428 __tls_get_addr (GET_ADDR_ARGS)
430 dtv_t *dtv = THREAD_DTV ();
431 struct link_map *the_map = NULL;
432 void *p;
434 if (__builtin_expect (dtv[0].counter != GL(dl_tls_generation), 0))
436 struct dtv_slotinfo_list *listp;
437 size_t idx;
439 /* The global dl_tls_dtv_slotinfo array contains for each module
440 index the generation counter current when the entry was
441 created. This array never shrinks so that all module indices
442 which were valid at some time can be used to access it.
443 Before the first use of a new module index in this function
444 the array was extended appropriately. Access also does not
445 have to be guarded against modifications of the array. It is
446 assumed that pointer-size values can be read atomically even
447 in SMP environments. It is possible that other threads at
448 the same time dynamically load code and therefore add to the
449 slotinfo list. This is a problem since we must not pick up
450 any information about incomplete work. The solution to this
451 is to ignore all dtv slots which were created after the one
452 we are currently interested. We know that dynamic loading
453 for this module is completed and this is the last load
454 operation we know finished. */
455 idx = GET_ADDR_MODULE;
456 listp = GL(dl_tls_dtv_slotinfo_list);
457 while (idx >= listp->len)
459 idx -= listp->len;
460 listp = listp->next;
463 if (dtv[0].counter < listp->slotinfo[idx].gen)
465 /* The generation counter for the slot is higher than what
466 the current dtv implements. We have to update the whole
467 dtv but only those entries with a generation counter <=
468 the one for the entry we need. */
469 size_t new_gen = listp->slotinfo[idx].gen;
470 size_t total = 0;
472 /* We have to look through the entire dtv slotinfo list. */
473 listp = GL(dl_tls_dtv_slotinfo_list);
476 size_t cnt;
478 for (cnt = total = 0 ? 1 : 0; cnt < listp->len; ++cnt)
480 size_t gen = listp->slotinfo[cnt].gen;
481 struct link_map *map;
482 size_t modid;
484 if (gen > new_gen)
485 /* This is a slot for a generation younger than
486 the one we are handling now. It might be
487 incompletely set up so ignore it. */
488 continue;
490 /* If the entry is older than the current dtv layout
491 we know we don't have to handle it. */
492 if (gen <= dtv[0].counter)
493 continue;
495 /* If there is no map this means the entry is empty. */
496 map = listp->slotinfo[cnt].map;
497 if (map == NULL)
499 /* If this modid was used at some point the memory
500 might still be allocated. */
501 if (dtv[total + cnt].pointer != TLS_DTV_UNALLOCATED)
502 free (dtv[total + cnt].pointer);
504 continue;
507 /* Check whether the current dtv array is large enough. */
508 modid = map->l_tls_modid;
509 assert (total + cnt == modid);
510 if (dtv[-1].counter < modid)
512 /* Reallocate the dtv. */
513 dtv_t *newp;
514 size_t newsize = GL(dl_tls_max_dtv_idx) + DTV_SURPLUS;
515 size_t oldsize = dtv[-1].counter;
517 assert (map->l_tls_modid <= newsize);
519 if (dtv == GL(dl_initial_dtv))
521 /* This is the initial dtv that was allocated
522 during rtld startup using the dl-minimal.c
523 malloc instead of the real malloc. We can't
524 free it, we have to abandon the old storage. */
526 newp = malloc ((2 + newsize) * sizeof (dtv_t));
527 if (newp == NULL)
528 oom ();
529 memcpy (newp, &dtv[-1], oldsize * sizeof (dtv_t));
531 else
533 newp = realloc (&dtv[-1],
534 (2 + newsize) * sizeof (dtv_t));
535 if (newp == NULL)
536 oom ();
539 newp[0].counter = newsize;
541 /* Clear the newly allocated part. */
542 memset (newp + 2 + oldsize, '\0',
543 (newsize - oldsize) * sizeof (dtv_t));
545 /* Point dtv to the generation counter. */
546 dtv = &newp[1];
548 /* Install this new dtv in the thread data
549 structures. */
550 INSTALL_NEW_DTV (dtv);
553 /* If there is currently memory allocate for this
554 dtv entry free it. */
555 /* XXX Ideally we will at some point create a memory
556 pool. */
557 if (dtv[modid].pointer != TLS_DTV_UNALLOCATED)
558 /* Note that free is called for NULL is well. We
559 deallocate even if it is this dtv entry we are
560 supposed to load. The reason is that we call
561 memalign and not malloc. */
562 free (dtv[modid].pointer);
564 /* This module is loaded dynamically- We defer
565 memory allocation. */
566 dtv[modid].pointer = TLS_DTV_UNALLOCATED;
568 if (modid == GET_ADDR_MODULE)
569 the_map = map;
572 total += listp->len;
574 while ((listp = listp->next) != NULL);
576 /* This will be the new maximum generation counter. */
577 dtv[0].counter = new_gen;
581 p = dtv[GET_ADDR_MODULE].pointer;
583 if (__builtin_expect (p == TLS_DTV_UNALLOCATED, 0))
585 /* The allocation was deferred. Do it now. */
586 if (the_map == NULL)
588 /* Find the link map for this module. */
589 size_t idx = GET_ADDR_MODULE;
590 struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list);
592 while (idx >= listp->len)
594 idx -= listp->len;
595 listp = listp->next;
598 the_map = listp->slotinfo[idx].map;
601 p = dtv[GET_ADDR_MODULE].pointer = allocate_and_init (the_map);
604 return (char *) p + GET_ADDR_OFFSET;
606 # endif
608 #endif /* use TLS */