Add sysdeps/ieee754/soft-fp.
[glibc.git] / elf / dl-tls.c
blobd791a5c06bd73cf7b7be4044f4e3696848c62714
1 /* Thread-local storage handling in the ELF dynamic linker. Generic version.
2 Copyright (C) 2002-2017 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 <assert.h>
20 #include <errno.h>
21 #include <libintl.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/param.h>
26 #include <atomic.h>
28 #include <tls.h>
29 #include <dl-tls.h>
30 #include <ldsodefs.h>
32 /* Amount of excess space to allocate in the static TLS area
33 to allow dynamic loading of modules defining IE-model TLS data. */
34 #define TLS_STATIC_SURPLUS 64 + DL_NNS * 100
37 /* Out-of-memory handler. */
38 static void
39 __attribute__ ((__noreturn__))
40 oom (void)
42 _dl_fatal_printf ("cannot allocate memory for thread-local data: ABORT\n");
46 size_t
47 _dl_next_tls_modid (void)
49 size_t result;
51 if (__builtin_expect (GL(dl_tls_dtv_gaps), false))
53 size_t disp = 0;
54 struct dtv_slotinfo_list *runp = GL(dl_tls_dtv_slotinfo_list);
56 /* Note that this branch will never be executed during program
57 start since there are no gaps at that time. Therefore it
58 does not matter that the dl_tls_dtv_slotinfo is not allocated
59 yet when the function is called for the first times.
61 NB: the offset +1 is due to the fact that DTV[0] is used
62 for something else. */
63 result = GL(dl_tls_static_nelem) + 1;
64 if (result <= GL(dl_tls_max_dtv_idx))
67 while (result - disp < runp->len)
69 if (runp->slotinfo[result - disp].map == NULL)
70 break;
72 ++result;
73 assert (result <= GL(dl_tls_max_dtv_idx) + 1);
76 if (result - disp < runp->len)
77 break;
79 disp += runp->len;
81 while ((runp = runp->next) != NULL);
83 if (result > GL(dl_tls_max_dtv_idx))
85 /* The new index must indeed be exactly one higher than the
86 previous high. */
87 assert (result == GL(dl_tls_max_dtv_idx) + 1);
88 /* There is no gap anymore. */
89 GL(dl_tls_dtv_gaps) = false;
91 goto nogaps;
94 else
96 /* No gaps, allocate a new entry. */
97 nogaps:
99 result = ++GL(dl_tls_max_dtv_idx);
102 return result;
106 size_t
107 _dl_count_modids (void)
109 /* It is rare that we have gaps; see elf/dl-open.c (_dl_open) where
110 we fail to load a module and unload it leaving a gap. If we don't
111 have gaps then the number of modids is the current maximum so
112 return that. */
113 if (__glibc_likely (!GL(dl_tls_dtv_gaps)))
114 return GL(dl_tls_max_dtv_idx);
116 /* We have gaps and are forced to count the non-NULL entries. */
117 size_t n = 0;
118 struct dtv_slotinfo_list *runp = GL(dl_tls_dtv_slotinfo_list);
119 while (runp != NULL)
121 for (size_t i = 0; i < runp->len; ++i)
122 if (runp->slotinfo[i].map != NULL)
123 ++n;
125 runp = runp->next;
128 return n;
132 #ifdef SHARED
133 void
134 _dl_determine_tlsoffset (void)
136 size_t max_align = TLS_TCB_ALIGN;
137 size_t freetop = 0;
138 size_t freebottom = 0;
140 /* The first element of the dtv slot info list is allocated. */
141 assert (GL(dl_tls_dtv_slotinfo_list) != NULL);
142 /* There is at this point only one element in the
143 dl_tls_dtv_slotinfo_list list. */
144 assert (GL(dl_tls_dtv_slotinfo_list)->next == NULL);
146 struct dtv_slotinfo *slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo;
148 /* Determining the offset of the various parts of the static TLS
149 block has several dependencies. In addition we have to work
150 around bugs in some toolchains.
152 Each TLS block from the objects available at link time has a size
153 and an alignment requirement. The GNU ld computes the alignment
154 requirements for the data at the positions *in the file*, though.
155 I.e, it is not simply possible to allocate a block with the size
156 of the TLS program header entry. The data is layed out assuming
157 that the first byte of the TLS block fulfills
159 p_vaddr mod p_align == &TLS_BLOCK mod p_align
161 This means we have to add artificial padding at the beginning of
162 the TLS block. These bytes are never used for the TLS data in
163 this module but the first byte allocated must be aligned
164 according to mod p_align == 0 so that the first byte of the TLS
165 block is aligned according to p_vaddr mod p_align. This is ugly
166 and the linker can help by computing the offsets in the TLS block
167 assuming the first byte of the TLS block is aligned according to
168 p_align.
170 The extra space which might be allocated before the first byte of
171 the TLS block need not go unused. The code below tries to use
172 that memory for the next TLS block. This can work if the total
173 memory requirement for the next TLS block is smaller than the
174 gap. */
176 #if TLS_TCB_AT_TP
177 /* We simply start with zero. */
178 size_t offset = 0;
180 for (size_t cnt = 0; slotinfo[cnt].map != NULL; ++cnt)
182 assert (cnt < GL(dl_tls_dtv_slotinfo_list)->len);
184 size_t firstbyte = (-slotinfo[cnt].map->l_tls_firstbyte_offset
185 & (slotinfo[cnt].map->l_tls_align - 1));
186 size_t off;
187 max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
189 if (freebottom - freetop >= slotinfo[cnt].map->l_tls_blocksize)
191 off = roundup (freetop + slotinfo[cnt].map->l_tls_blocksize
192 - firstbyte, slotinfo[cnt].map->l_tls_align)
193 + firstbyte;
194 if (off <= freebottom)
196 freetop = off;
198 /* XXX For some architectures we perhaps should store the
199 negative offset. */
200 slotinfo[cnt].map->l_tls_offset = off;
201 continue;
205 off = roundup (offset + slotinfo[cnt].map->l_tls_blocksize - firstbyte,
206 slotinfo[cnt].map->l_tls_align) + firstbyte;
207 if (off > offset + slotinfo[cnt].map->l_tls_blocksize
208 + (freebottom - freetop))
210 freetop = offset;
211 freebottom = off - slotinfo[cnt].map->l_tls_blocksize;
213 offset = off;
215 /* XXX For some architectures we perhaps should store the
216 negative offset. */
217 slotinfo[cnt].map->l_tls_offset = off;
220 GL(dl_tls_static_used) = offset;
221 GL(dl_tls_static_size) = (roundup (offset + TLS_STATIC_SURPLUS, max_align)
222 + TLS_TCB_SIZE);
223 #elif TLS_DTV_AT_TP
224 /* The TLS blocks start right after the TCB. */
225 size_t offset = TLS_TCB_SIZE;
227 for (size_t cnt = 0; slotinfo[cnt].map != NULL; ++cnt)
229 assert (cnt < GL(dl_tls_dtv_slotinfo_list)->len);
231 size_t firstbyte = (-slotinfo[cnt].map->l_tls_firstbyte_offset
232 & (slotinfo[cnt].map->l_tls_align - 1));
233 size_t off;
234 max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
236 if (slotinfo[cnt].map->l_tls_blocksize <= freetop - freebottom)
238 off = roundup (freebottom, slotinfo[cnt].map->l_tls_align);
239 if (off - freebottom < firstbyte)
240 off += slotinfo[cnt].map->l_tls_align;
241 if (off + slotinfo[cnt].map->l_tls_blocksize - firstbyte <= freetop)
243 slotinfo[cnt].map->l_tls_offset = off - firstbyte;
244 freebottom = (off + slotinfo[cnt].map->l_tls_blocksize
245 - firstbyte);
246 continue;
250 off = roundup (offset, slotinfo[cnt].map->l_tls_align);
251 if (off - offset < firstbyte)
252 off += slotinfo[cnt].map->l_tls_align;
254 slotinfo[cnt].map->l_tls_offset = off - firstbyte;
255 if (off - firstbyte - offset > freetop - freebottom)
257 freebottom = offset;
258 freetop = off - firstbyte;
261 offset = off + slotinfo[cnt].map->l_tls_blocksize - firstbyte;
264 GL(dl_tls_static_used) = offset;
265 GL(dl_tls_static_size) = roundup (offset + TLS_STATIC_SURPLUS,
266 TLS_TCB_ALIGN);
267 #else
268 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
269 #endif
271 /* The alignment requirement for the static TLS block. */
272 GL(dl_tls_static_align) = max_align;
274 #endif /* SHARED */
276 static void *
277 allocate_dtv (void *result)
279 dtv_t *dtv;
280 size_t dtv_length;
282 /* We allocate a few more elements in the dtv than are needed for the
283 initial set of modules. This should avoid in most cases expansions
284 of the dtv. */
285 dtv_length = GL(dl_tls_max_dtv_idx) + DTV_SURPLUS;
286 dtv = calloc (dtv_length + 2, sizeof (dtv_t));
287 if (dtv != NULL)
289 /* This is the initial length of the dtv. */
290 dtv[0].counter = dtv_length;
292 /* The rest of the dtv (including the generation counter) is
293 Initialize with zero to indicate nothing there. */
295 /* Add the dtv to the thread data structures. */
296 INSTALL_DTV (result, dtv);
298 else
299 result = NULL;
301 return result;
305 /* Get size and alignment requirements of the static TLS block. */
306 void
307 _dl_get_tls_static_info (size_t *sizep, size_t *alignp)
309 *sizep = GL(dl_tls_static_size);
310 *alignp = GL(dl_tls_static_align);
313 /* Derive the location of the pointer to the start of the original
314 allocation (before alignment) from the pointer to the TCB. */
315 static inline void **
316 tcb_to_pointer_to_free_location (void *tcb)
318 #if TLS_TCB_AT_TP
319 /* The TCB follows the TLS blocks, and the pointer to the front
320 follows the TCB. */
321 void **original_pointer_location = tcb + TLS_TCB_SIZE;
322 #elif TLS_DTV_AT_TP
323 /* The TCB comes first, preceded by the pre-TCB, and the pointer is
324 before that. */
325 void **original_pointer_location = tcb - TLS_PRE_TCB_SIZE - sizeof (void *);
326 #endif
327 return original_pointer_location;
330 void *
331 _dl_allocate_tls_storage (void)
333 void *result;
334 size_t size = GL(dl_tls_static_size);
336 #if TLS_DTV_AT_TP
337 /* Memory layout is:
338 [ TLS_PRE_TCB_SIZE ] [ TLS_TCB_SIZE ] [ TLS blocks ]
339 ^ This should be returned. */
340 size += TLS_PRE_TCB_SIZE;
341 #endif
343 /* Perform the allocation. Reserve space for the required alignment
344 and the pointer to the original allocation. */
345 size_t alignment = GL(dl_tls_static_align);
346 void *allocated = malloc (size + alignment + sizeof (void *));
347 if (__glibc_unlikely (allocated == NULL))
348 return NULL;
350 /* Perform alignment and allocate the DTV. */
351 #if TLS_TCB_AT_TP
352 /* The TCB follows the TLS blocks, which determine the alignment.
353 (TCB alignment requirements have been taken into account when
354 calculating GL(dl_tls_static_align).) */
355 void *aligned = (void *) roundup ((uintptr_t) allocated, alignment);
356 result = aligned + size - TLS_TCB_SIZE;
358 /* Clear the TCB data structure. We can't ask the caller (i.e.
359 libpthread) to do it, because we will initialize the DTV et al. */
360 memset (result, '\0', TLS_TCB_SIZE);
361 #elif TLS_DTV_AT_TP
362 /* Pre-TCB and TCB come before the TLS blocks. The layout computed
363 in _dl_determine_tlsoffset assumes that the TCB is aligned to the
364 TLS block alignment, and not just the TLS blocks after it. This
365 can leave an unused alignment gap between the TCB and the TLS
366 blocks. */
367 result = (void *) roundup
368 (sizeof (void *) + TLS_PRE_TCB_SIZE + (uintptr_t) allocated,
369 alignment);
371 /* Clear the TCB data structure and TLS_PRE_TCB_SIZE bytes before
372 it. We can't ask the caller (i.e. libpthread) to do it, because
373 we will initialize the DTV et al. */
374 memset (result - TLS_PRE_TCB_SIZE, '\0', TLS_PRE_TCB_SIZE + TLS_TCB_SIZE);
375 #endif
377 /* Record the value of the original pointer for later
378 deallocation. */
379 *tcb_to_pointer_to_free_location (result) = allocated;
381 result = allocate_dtv (result);
382 if (result == NULL)
383 free (allocated);
384 return result;
388 #ifndef SHARED
389 extern dtv_t _dl_static_dtv[];
390 # define _dl_initial_dtv (&_dl_static_dtv[1])
391 #endif
393 static dtv_t *
394 _dl_resize_dtv (dtv_t *dtv)
396 /* Resize the dtv. */
397 dtv_t *newp;
398 /* Load GL(dl_tls_max_dtv_idx) atomically since it may be written to by
399 other threads concurrently. */
400 size_t newsize
401 = atomic_load_acquire (&GL(dl_tls_max_dtv_idx)) + DTV_SURPLUS;
402 size_t oldsize = dtv[-1].counter;
404 if (dtv == GL(dl_initial_dtv))
406 /* This is the initial dtv that was either statically allocated in
407 __libc_setup_tls or allocated during rtld startup using the
408 dl-minimal.c malloc instead of the real malloc. We can't free
409 it, we have to abandon the old storage. */
411 newp = malloc ((2 + newsize) * sizeof (dtv_t));
412 if (newp == NULL)
413 oom ();
414 memcpy (newp, &dtv[-1], (2 + oldsize) * sizeof (dtv_t));
416 else
418 newp = realloc (&dtv[-1],
419 (2 + newsize) * sizeof (dtv_t));
420 if (newp == NULL)
421 oom ();
424 newp[0].counter = newsize;
426 /* Clear the newly allocated part. */
427 memset (newp + 2 + oldsize, '\0',
428 (newsize - oldsize) * sizeof (dtv_t));
430 /* Return the generation counter. */
431 return &newp[1];
435 void *
436 _dl_allocate_tls_init (void *result)
438 if (result == NULL)
439 /* The memory allocation failed. */
440 return NULL;
442 dtv_t *dtv = GET_DTV (result);
443 struct dtv_slotinfo_list *listp;
444 size_t total = 0;
445 size_t maxgen = 0;
447 /* Check if the current dtv is big enough. */
448 if (dtv[-1].counter < GL(dl_tls_max_dtv_idx))
450 /* Resize the dtv. */
451 dtv = _dl_resize_dtv (dtv);
453 /* Install this new dtv in the thread data structures. */
454 INSTALL_DTV (result, &dtv[-1]);
457 /* We have to prepare the dtv for all currently loaded modules using
458 TLS. For those which are dynamically loaded we add the values
459 indicating deferred allocation. */
460 listp = GL(dl_tls_dtv_slotinfo_list);
461 while (1)
463 size_t cnt;
465 for (cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
467 struct link_map *map;
468 void *dest;
470 /* Check for the total number of used slots. */
471 if (total + cnt > GL(dl_tls_max_dtv_idx))
472 break;
474 map = listp->slotinfo[cnt].map;
475 if (map == NULL)
476 /* Unused entry. */
477 continue;
479 /* Keep track of the maximum generation number. This might
480 not be the generation counter. */
481 assert (listp->slotinfo[cnt].gen <= GL(dl_tls_generation));
482 maxgen = MAX (maxgen, listp->slotinfo[cnt].gen);
484 dtv[map->l_tls_modid].pointer.val = TLS_DTV_UNALLOCATED;
485 dtv[map->l_tls_modid].pointer.to_free = NULL;
487 if (map->l_tls_offset == NO_TLS_OFFSET
488 || map->l_tls_offset == FORCED_DYNAMIC_TLS_OFFSET)
489 continue;
491 assert (map->l_tls_modid == total + cnt);
492 assert (map->l_tls_blocksize >= map->l_tls_initimage_size);
493 #if TLS_TCB_AT_TP
494 assert ((size_t) map->l_tls_offset >= map->l_tls_blocksize);
495 dest = (char *) result - map->l_tls_offset;
496 #elif TLS_DTV_AT_TP
497 dest = (char *) result + map->l_tls_offset;
498 #else
499 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
500 #endif
502 /* Set up the DTV entry. The simplified __tls_get_addr that
503 some platforms use in static programs requires it. */
504 dtv[map->l_tls_modid].pointer.val = dest;
506 /* Copy the initialization image and clear the BSS part. */
507 memset (__mempcpy (dest, map->l_tls_initimage,
508 map->l_tls_initimage_size), '\0',
509 map->l_tls_blocksize - map->l_tls_initimage_size);
512 total += cnt;
513 if (total >= GL(dl_tls_max_dtv_idx))
514 break;
516 listp = listp->next;
517 assert (listp != NULL);
520 /* The DTV version is up-to-date now. */
521 dtv[0].counter = maxgen;
523 return result;
525 rtld_hidden_def (_dl_allocate_tls_init)
527 void *
528 _dl_allocate_tls (void *mem)
530 return _dl_allocate_tls_init (mem == NULL
531 ? _dl_allocate_tls_storage ()
532 : allocate_dtv (mem));
534 rtld_hidden_def (_dl_allocate_tls)
537 void
538 _dl_deallocate_tls (void *tcb, bool dealloc_tcb)
540 dtv_t *dtv = GET_DTV (tcb);
542 /* We need to free the memory allocated for non-static TLS. */
543 for (size_t cnt = 0; cnt < dtv[-1].counter; ++cnt)
544 free (dtv[1 + cnt].pointer.to_free);
546 /* The array starts with dtv[-1]. */
547 if (dtv != GL(dl_initial_dtv))
548 free (dtv - 1);
550 if (dealloc_tcb)
551 free (*tcb_to_pointer_to_free_location (tcb));
553 rtld_hidden_def (_dl_deallocate_tls)
556 #ifdef SHARED
557 /* The __tls_get_addr function has two basic forms which differ in the
558 arguments. The IA-64 form takes two parameters, the module ID and
559 offset. The form used, among others, on IA-32 takes a reference to
560 a special structure which contain the same information. The second
561 form seems to be more often used (in the moment) so we default to
562 it. Users of the IA-64 form have to provide adequate definitions
563 of the following macros. */
564 # ifndef GET_ADDR_ARGS
565 # define GET_ADDR_ARGS tls_index *ti
566 # define GET_ADDR_PARAM ti
567 # endif
568 # ifndef GET_ADDR_MODULE
569 # define GET_ADDR_MODULE ti->ti_module
570 # endif
571 # ifndef GET_ADDR_OFFSET
572 # define GET_ADDR_OFFSET ti->ti_offset
573 # endif
575 /* Allocate one DTV entry. */
576 static struct dtv_pointer
577 allocate_dtv_entry (size_t alignment, size_t size)
579 if (powerof2 (alignment) && alignment <= _Alignof (max_align_t))
581 /* The alignment is supported by malloc. */
582 void *ptr = malloc (size);
583 return (struct dtv_pointer) { ptr, ptr };
586 /* Emulate memalign to by manually aligning a pointer returned by
587 malloc. First compute the size with an overflow check. */
588 size_t alloc_size = size + alignment;
589 if (alloc_size < size)
590 return (struct dtv_pointer) {};
592 /* Perform the allocation. This is the pointer we need to free
593 later. */
594 void *start = malloc (alloc_size);
595 if (start == NULL)
596 return (struct dtv_pointer) {};
598 /* Find the aligned position within the larger allocation. */
599 void *aligned = (void *) roundup ((uintptr_t) start, alignment);
601 return (struct dtv_pointer) { .val = aligned, .to_free = start };
604 static struct dtv_pointer
605 allocate_and_init (struct link_map *map)
607 struct dtv_pointer result = allocate_dtv_entry
608 (map->l_tls_align, map->l_tls_blocksize);
609 if (result.val == NULL)
610 oom ();
612 /* Initialize the memory. */
613 memset (__mempcpy (result.val, map->l_tls_initimage,
614 map->l_tls_initimage_size),
615 '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
617 return result;
621 struct link_map *
622 _dl_update_slotinfo (unsigned long int req_modid)
624 struct link_map *the_map = NULL;
625 dtv_t *dtv = THREAD_DTV ();
627 /* The global dl_tls_dtv_slotinfo array contains for each module
628 index the generation counter current when the entry was created.
629 This array never shrinks so that all module indices which were
630 valid at some time can be used to access it. Before the first
631 use of a new module index in this function the array was extended
632 appropriately. Access also does not have to be guarded against
633 modifications of the array. It is assumed that pointer-size
634 values can be read atomically even in SMP environments. It is
635 possible that other threads at the same time dynamically load
636 code and therefore add to the slotinfo list. This is a problem
637 since we must not pick up any information about incomplete work.
638 The solution to this is to ignore all dtv slots which were
639 created after the one we are currently interested. We know that
640 dynamic loading for this module is completed and this is the last
641 load operation we know finished. */
642 unsigned long int idx = req_modid;
643 struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list);
645 while (idx >= listp->len)
647 idx -= listp->len;
648 listp = listp->next;
651 if (dtv[0].counter < listp->slotinfo[idx].gen)
653 /* The generation counter for the slot is higher than what the
654 current dtv implements. We have to update the whole dtv but
655 only those entries with a generation counter <= the one for
656 the entry we need. */
657 size_t new_gen = listp->slotinfo[idx].gen;
658 size_t total = 0;
660 /* We have to look through the entire dtv slotinfo list. */
661 listp = GL(dl_tls_dtv_slotinfo_list);
664 for (size_t cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
666 size_t gen = listp->slotinfo[cnt].gen;
668 if (gen > new_gen)
669 /* This is a slot for a generation younger than the
670 one we are handling now. It might be incompletely
671 set up so ignore it. */
672 continue;
674 /* If the entry is older than the current dtv layout we
675 know we don't have to handle it. */
676 if (gen <= dtv[0].counter)
677 continue;
679 /* If there is no map this means the entry is empty. */
680 struct link_map *map = listp->slotinfo[cnt].map;
681 if (map == NULL)
683 if (dtv[-1].counter >= total + cnt)
685 /* If this modid was used at some point the memory
686 might still be allocated. */
687 free (dtv[total + cnt].pointer.to_free);
688 dtv[total + cnt].pointer.val = TLS_DTV_UNALLOCATED;
689 dtv[total + cnt].pointer.to_free = NULL;
692 continue;
695 /* Check whether the current dtv array is large enough. */
696 size_t modid = map->l_tls_modid;
697 assert (total + cnt == modid);
698 if (dtv[-1].counter < modid)
700 /* Resize the dtv. */
701 dtv = _dl_resize_dtv (dtv);
703 assert (modid <= dtv[-1].counter);
705 /* Install this new dtv in the thread data
706 structures. */
707 INSTALL_NEW_DTV (dtv);
710 /* If there is currently memory allocate for this
711 dtv entry free it. */
712 /* XXX Ideally we will at some point create a memory
713 pool. */
714 free (dtv[modid].pointer.to_free);
715 dtv[modid].pointer.val = TLS_DTV_UNALLOCATED;
716 dtv[modid].pointer.to_free = NULL;
718 if (modid == req_modid)
719 the_map = map;
722 total += listp->len;
724 while ((listp = listp->next) != NULL);
726 /* This will be the new maximum generation counter. */
727 dtv[0].counter = new_gen;
730 return the_map;
734 static void *
735 __attribute_noinline__
736 tls_get_addr_tail (GET_ADDR_ARGS, dtv_t *dtv, struct link_map *the_map)
738 /* The allocation was deferred. Do it now. */
739 if (the_map == NULL)
741 /* Find the link map for this module. */
742 size_t idx = GET_ADDR_MODULE;
743 struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list);
745 while (idx >= listp->len)
747 idx -= listp->len;
748 listp = listp->next;
751 the_map = listp->slotinfo[idx].map;
754 /* Make sure that, if a dlopen running in parallel forces the
755 variable into static storage, we'll wait until the address in the
756 static TLS block is set up, and use that. If we're undecided
757 yet, make sure we make the decision holding the lock as well. */
758 if (__glibc_unlikely (the_map->l_tls_offset
759 != FORCED_DYNAMIC_TLS_OFFSET))
761 __rtld_lock_lock_recursive (GL(dl_load_lock));
762 if (__glibc_likely (the_map->l_tls_offset == NO_TLS_OFFSET))
764 the_map->l_tls_offset = FORCED_DYNAMIC_TLS_OFFSET;
765 __rtld_lock_unlock_recursive (GL(dl_load_lock));
767 else if (__glibc_likely (the_map->l_tls_offset
768 != FORCED_DYNAMIC_TLS_OFFSET))
770 #if TLS_TCB_AT_TP
771 void *p = (char *) THREAD_SELF - the_map->l_tls_offset;
772 #elif TLS_DTV_AT_TP
773 void *p = (char *) THREAD_SELF + the_map->l_tls_offset + TLS_PRE_TCB_SIZE;
774 #else
775 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
776 #endif
777 __rtld_lock_unlock_recursive (GL(dl_load_lock));
779 dtv[GET_ADDR_MODULE].pointer.to_free = NULL;
780 dtv[GET_ADDR_MODULE].pointer.val = p;
782 return (char *) p + GET_ADDR_OFFSET;
784 else
785 __rtld_lock_unlock_recursive (GL(dl_load_lock));
787 struct dtv_pointer result = allocate_and_init (the_map);
788 dtv[GET_ADDR_MODULE].pointer = result;
789 assert (result.to_free != NULL);
791 return (char *) result.val + GET_ADDR_OFFSET;
795 static struct link_map *
796 __attribute_noinline__
797 update_get_addr (GET_ADDR_ARGS)
799 struct link_map *the_map = _dl_update_slotinfo (GET_ADDR_MODULE);
800 dtv_t *dtv = THREAD_DTV ();
802 void *p = dtv[GET_ADDR_MODULE].pointer.val;
804 if (__glibc_unlikely (p == TLS_DTV_UNALLOCATED))
805 return tls_get_addr_tail (GET_ADDR_PARAM, dtv, the_map);
807 return (void *) p + GET_ADDR_OFFSET;
810 /* For all machines that have a non-macro version of __tls_get_addr, we
811 want to use rtld_hidden_proto/rtld_hidden_def in order to call the
812 internal alias for __tls_get_addr from ld.so. This avoids a PLT entry
813 in ld.so for __tls_get_addr. */
815 #ifndef __tls_get_addr
816 extern void * __tls_get_addr (GET_ADDR_ARGS);
817 rtld_hidden_proto (__tls_get_addr)
818 rtld_hidden_def (__tls_get_addr)
819 #endif
821 /* The generic dynamic and local dynamic model cannot be used in
822 statically linked applications. */
823 void *
824 __tls_get_addr (GET_ADDR_ARGS)
826 dtv_t *dtv = THREAD_DTV ();
828 if (__glibc_unlikely (dtv[0].counter != GL(dl_tls_generation)))
829 return update_get_addr (GET_ADDR_PARAM);
831 void *p = dtv[GET_ADDR_MODULE].pointer.val;
833 if (__glibc_unlikely (p == TLS_DTV_UNALLOCATED))
834 return tls_get_addr_tail (GET_ADDR_PARAM, dtv, NULL);
836 return (char *) p + GET_ADDR_OFFSET;
838 #endif
841 /* Look up the module's TLS block as for __tls_get_addr,
842 but never touch anything. Return null if it's not allocated yet. */
843 void *
844 _dl_tls_get_addr_soft (struct link_map *l)
846 if (__glibc_unlikely (l->l_tls_modid == 0))
847 /* This module has no TLS segment. */
848 return NULL;
850 dtv_t *dtv = THREAD_DTV ();
851 if (__glibc_unlikely (dtv[0].counter != GL(dl_tls_generation)))
853 /* This thread's DTV is not completely current,
854 but it might already cover this module. */
856 if (l->l_tls_modid >= dtv[-1].counter)
857 /* Nope. */
858 return NULL;
860 size_t idx = l->l_tls_modid;
861 struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list);
862 while (idx >= listp->len)
864 idx -= listp->len;
865 listp = listp->next;
868 /* We've reached the slot for this module.
869 If its generation counter is higher than the DTV's,
870 this thread does not know about this module yet. */
871 if (dtv[0].counter < listp->slotinfo[idx].gen)
872 return NULL;
875 void *data = dtv[l->l_tls_modid].pointer.val;
876 if (__glibc_unlikely (data == TLS_DTV_UNALLOCATED))
877 /* The DTV is current, but this thread has not yet needed
878 to allocate this module's segment. */
879 data = NULL;
881 return data;
885 void
886 _dl_add_to_slotinfo (struct link_map *l)
888 /* Now that we know the object is loaded successfully add
889 modules containing TLS data to the dtv info table. We
890 might have to increase its size. */
891 struct dtv_slotinfo_list *listp;
892 struct dtv_slotinfo_list *prevp;
893 size_t idx = l->l_tls_modid;
895 /* Find the place in the dtv slotinfo list. */
896 listp = GL(dl_tls_dtv_slotinfo_list);
897 prevp = NULL; /* Needed to shut up gcc. */
900 /* Does it fit in the array of this list element? */
901 if (idx < listp->len)
902 break;
903 idx -= listp->len;
904 prevp = listp;
905 listp = listp->next;
907 while (listp != NULL);
909 if (listp == NULL)
911 /* When we come here it means we have to add a new element
912 to the slotinfo list. And the new module must be in
913 the first slot. */
914 assert (idx == 0);
916 listp = prevp->next = (struct dtv_slotinfo_list *)
917 malloc (sizeof (struct dtv_slotinfo_list)
918 + TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
919 if (listp == NULL)
921 /* We ran out of memory. We will simply fail this
922 call but don't undo anything we did so far. The
923 application will crash or be terminated anyway very
924 soon. */
926 /* We have to do this since some entries in the dtv
927 slotinfo array might already point to this
928 generation. */
929 ++GL(dl_tls_generation);
931 _dl_signal_error (ENOMEM, "dlopen", NULL, N_("\
932 cannot create TLS data structures"));
935 listp->len = TLS_SLOTINFO_SURPLUS;
936 listp->next = NULL;
937 memset (listp->slotinfo, '\0',
938 TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
941 /* Add the information into the slotinfo data structure. */
942 listp->slotinfo[idx].map = l;
943 listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;