wordexp: Fix the usage of the internal _itoa function
[uclibc-ng.git] / ldso / ldso / dl-tls.c
blob80da815efc39345fe1d84919b442a21717ace373
1 /*
2 * Thread-local storage handling in the ELF dynamic linker.
4 * Copyright (C) 2005 by Steven J. Hill <sjhill@realitydiluted.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. The name of the above contributors may not be
12 * used to endorse or promote products derived from this software
13 * without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
28 #include <tls.h>
29 #include <dl-tls.h>
30 #include <ldsodefs.h>
32 void *(*_dl_calloc_function) (size_t __nmemb, size_t __size) = NULL;
33 void *(*_dl_realloc_function) (void *__ptr, size_t __size) = NULL;
34 void *(*_dl_memalign_function) (size_t __boundary, size_t __size) = NULL;
36 void (*_dl_free_function) (void *__ptr);
37 void *_dl_memalign (size_t __boundary, size_t __size);
38 struct link_map *_dl_update_slotinfo (unsigned long int req_modid);
40 /* Round up N to the nearest multiple of P, where P is a power of 2
41 --- without using libgcc division routines. */
42 #define roundup_pow2(n, p) (((n) + (p) - 1) & ~((p) - 1))
44 void *
45 _dl_calloc (size_t __nmemb, size_t __size)
47 void *result;
48 size_t size = (__size * __nmemb);
50 if (_dl_calloc_function)
51 return (*_dl_calloc_function) (__nmemb, __size);
53 if ((result = _dl_malloc(size)) != NULL) {
54 _dl_memset(result, 0, size);
57 return result;
60 void *
61 _dl_realloc (void * __ptr, size_t __size)
63 if (_dl_realloc_function)
64 return (*_dl_realloc_function) (__ptr, __size);
66 _dl_debug_early("NOT IMPLEMENTED PROPERLY!!!\n");
67 return NULL;
70 /* The __tls_get_addr function has two basic forms which differ in the
71 arguments. The IA-64 form takes two parameters, the module ID and
72 offset. The form used, among others, on IA-32 takes a reference to
73 a special structure which contain the same information. The second
74 form seems to be more often used (in the moment) so we default to
75 it. Users of the IA-64 form have to provide adequate definitions
76 of the following macros. */
77 #ifndef GET_ADDR_ARGS
78 # define GET_ADDR_ARGS tls_index *ti
79 #endif
80 #ifndef GET_ADDR_MODULE
81 # define GET_ADDR_MODULE ti->ti_module
82 #endif
83 #ifndef GET_ADDR_OFFSET
84 # define GET_ADDR_OFFSET ti->ti_offset
85 #endif
88 * Amount of excess space to allocate in the static TLS area
89 * to allow dynamic loading of modules defining IE-model TLS data.
91 #define TLS_STATIC_SURPLUS 64 + DL_NNS * 100
93 /* Value used for dtv entries for which the allocation is delayed. */
94 #define TLS_DTV_UNALLOCATED ((void *) -1l)
97 * We are trying to perform a static TLS relocation in MAP, but it was
98 * dynamically loaded. This can only work if there is enough surplus in
99 * the static TLS area already allocated for each running thread. If this
100 * object's TLS segment is too big to fit, we fail. If it fits,
101 * we set MAP->l_tls_offset and return.
104 internal_function
105 _dl_try_allocate_static_tls (struct link_map* map)
107 /* If the alignment requirements are too high fail. */
108 if (map->l_tls_align > _dl_tls_static_align)
110 fail:
111 return -1;
114 # ifdef TLS_TCB_AT_TP
115 size_t freebytes;
116 size_t n;
117 size_t blsize;
119 freebytes = _dl_tls_static_size - _dl_tls_static_used - TLS_TCB_SIZE;
121 blsize = map->l_tls_blocksize + map->l_tls_firstbyte_offset;
122 if (freebytes < blsize)
123 goto fail;
125 n = (freebytes - blsize) & ~(map->l_tls_align - 1);
127 size_t offset = _dl_tls_static_used + (freebytes - n
128 - map->l_tls_firstbyte_offset);
130 map->l_tls_offset = _dl_tls_static_used = offset;
131 # elif defined(TLS_DTV_AT_TP)
132 size_t used;
133 size_t check;
135 size_t offset = roundup_pow2 (_dl_tls_static_used, map->l_tls_align);
136 used = offset + map->l_tls_blocksize;
137 check = used;
139 /* dl_tls_static_used includes the TCB at the beginning. */
140 if (check > _dl_tls_static_size)
141 goto fail;
143 map->l_tls_offset = offset;
144 _dl_tls_static_used = used;
145 # else
146 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
147 # endif
150 * If the object is not yet relocated we cannot initialize the
151 * static TLS region. Delay it.
153 if (((struct elf_resolve *) map)->init_flag & RELOCS_DONE)
155 #ifdef SHARED
157 * Update the slot information data for at least the generation of
158 * the DSO we are allocating data for.
160 if (__builtin_expect (THREAD_DTV()[0].counter != _dl_tls_generation, 0))
161 (void) _dl_update_slotinfo (map->l_tls_modid);
162 #endif
163 _dl_init_static_tls (map);
165 else
166 map->l_need_tls_init = 1;
168 return 0;
172 * This function intentionally does not return any value but signals error
173 * directly, as static TLS should be rare and code handling it should
174 * not be inlined as much as possible.
176 void
177 internal_function __attribute_noinline__
178 _dl_allocate_static_tls (struct link_map *map)
180 if (_dl_try_allocate_static_tls (map)) {
181 _dl_dprintf(2, "cannot allocate memory in static TLS block");
182 _dl_exit(30);
186 #ifdef SHARED
187 /* Initialize static TLS area and DTV for current (only) thread.
188 libpthread implementations should provide their own hook
189 to handle all threads. */
190 void
191 attribute_hidden __attribute_noinline__
192 _dl_nothread_init_static_tls (struct link_map *map)
194 # ifdef TLS_TCB_AT_TP
195 void *dest = (char *) THREAD_SELF - map->l_tls_offset;
196 # elif defined(TLS_DTV_AT_TP)
197 void *dest = (char *) THREAD_SELF + map->l_tls_offset + TLS_PRE_TCB_SIZE;
198 # else
199 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
200 # endif
202 /* Fill in the DTV slot so that a later LD/GD access will find it. */
203 dtv_t *dtv = THREAD_DTV ();
204 if (!(map->l_tls_modid <= dtv[-1].counter)) {
205 _dl_dprintf(2, "map->l_tls_modid <= dtv[-1].counter FAILED!\n");
206 _dl_exit(30);
208 dtv[map->l_tls_modid].pointer.val = dest;
209 dtv[map->l_tls_modid].pointer.is_static = true;
211 /* Initialize the memory. */
212 _dl_memcpy(dest, map->l_tls_initimage, map->l_tls_initimage_size);
213 _dl_memset((dest + map->l_tls_initimage_size), '\0',
214 map->l_tls_blocksize - map->l_tls_initimage_size);
216 #endif
218 /* Taken from glibc/sysdeps/generic/dl-tls.c */
219 static void
220 oom (void)
222 _dl_debug_early("cannot allocate thread-local memory: ABORT\n");
223 _dl_exit(30);
226 size_t
227 internal_function
228 _dl_next_tls_modid (void)
230 size_t result;
232 if (__builtin_expect (_dl_tls_dtv_gaps, false))
234 size_t disp = 0;
235 struct dtv_slotinfo_list *runp = _dl_tls_dtv_slotinfo_list;
237 /* Note that this branch will never be executed during program
238 start since there are no gaps at that time. Therefore it
239 does not matter that the dl_tls_dtv_slotinfo is not allocated
240 yet when the function is called for the first times.
242 NB: the offset +1 is due to the fact that DTV[0] is used
243 for something else. */
244 result = _dl_tls_static_nelem + 1;
245 if (result <= _dl_tls_max_dtv_idx)
248 while (result - disp < runp->len)
250 if (runp->slotinfo[result - disp].map == NULL)
251 break;
253 ++result;
254 _dl_assert (result <= _dl_tls_max_dtv_idx + 1);
257 if (result - disp < runp->len)
258 break;
260 disp += runp->len;
262 while ((runp = runp->next) != NULL);
264 if (result > _dl_tls_max_dtv_idx)
266 /* The new index must indeed be exactly one higher than the
267 previous high. */
268 _dl_assert (result == _dl_tls_max_dtv_idx + 1);
269 /* There is no gap anymore. */
270 _dl_tls_dtv_gaps = false;
272 goto nogaps;
275 else
277 /* No gaps, allocate a new entry. */
278 nogaps:
280 result = ++_dl_tls_max_dtv_idx;
283 return result;
286 void
287 internal_function
288 _dl_determine_tlsoffset (void)
290 size_t max_align = TLS_TCB_ALIGN;
291 size_t freetop = 0;
292 size_t freebottom = 0;
294 /* The first element of the dtv slot info list is allocated. */
295 _dl_assert (_dl_tls_dtv_slotinfo_list != NULL);
296 /* There is at this point only one element in the
297 dl_tls_dtv_slotinfo_list list. */
298 _dl_assert (_dl_tls_dtv_slotinfo_list->next == NULL);
300 struct dtv_slotinfo *slotinfo = _dl_tls_dtv_slotinfo_list->slotinfo;
302 /* Determining the offset of the various parts of the static TLS
303 block has several dependencies. In addition we have to work
304 around bugs in some toolchains.
306 Each TLS block from the objects available at link time has a size
307 and an alignment requirement. The GNU ld computes the alignment
308 requirements for the data at the positions *in the file*, though.
309 I.e, it is not simply possible to allocate a block with the size
310 of the TLS program header entry. The data is layed out assuming
311 that the first byte of the TLS block fulfills
313 p_vaddr mod p_align == &TLS_BLOCK mod p_align
315 This means we have to add artificial padding at the beginning of
316 the TLS block. These bytes are never used for the TLS data in
317 this module but the first byte allocated must be aligned
318 according to mod p_align == 0 so that the first byte of the TLS
319 block is aligned according to p_vaddr mod p_align. This is ugly
320 and the linker can help by computing the offsets in the TLS block
321 assuming the first byte of the TLS block is aligned according to
322 p_align.
324 The extra space which might be allocated before the first byte of
325 the TLS block need not go unused. The code below tries to use
326 that memory for the next TLS block. This can work if the total
327 memory requirement for the next TLS block is smaller than the
328 gap. */
330 # ifdef TLS_TCB_AT_TP
331 /* We simply start with zero. */
332 size_t cnt, offset = 0;
334 for (cnt = 1; slotinfo[cnt].map != NULL; ++cnt)
336 _dl_assert (cnt < _dl_tls_dtv_slotinfo_list->len);
338 size_t firstbyte = (-slotinfo[cnt].map->l_tls_firstbyte_offset
339 & (slotinfo[cnt].map->l_tls_align - 1));
340 size_t off;
341 max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
343 if (freebottom - freetop >= slotinfo[cnt].map->l_tls_blocksize)
345 off = roundup_pow2 (freetop + slotinfo[cnt].map->l_tls_blocksize
346 - firstbyte, slotinfo[cnt].map->l_tls_align)
347 + firstbyte;
348 if (off <= freebottom)
350 freetop = off;
352 /* XXX For some architectures we perhaps should store the
353 negative offset. */
354 slotinfo[cnt].map->l_tls_offset = off;
355 continue;
359 off = roundup_pow2 (offset + slotinfo[cnt].map->l_tls_blocksize
360 - firstbyte, slotinfo[cnt].map->l_tls_align)
361 + firstbyte;
362 if (off > offset + slotinfo[cnt].map->l_tls_blocksize
363 + (freebottom - freetop))
365 freetop = offset;
366 freebottom = off - slotinfo[cnt].map->l_tls_blocksize;
368 offset = off;
370 /* XXX For some architectures we perhaps should store the
371 negative offset. */
372 slotinfo[cnt].map->l_tls_offset = off;
375 _dl_tls_static_used = offset;
376 _dl_tls_static_size = (roundup_pow2 (offset + TLS_STATIC_SURPLUS, max_align)
377 + TLS_TCB_SIZE);
378 # elif defined(TLS_DTV_AT_TP)
379 /* The TLS blocks start right after the TCB. */
380 size_t offset = TLS_TCB_SIZE;
381 size_t cnt;
383 for (cnt = 1; slotinfo[cnt].map != NULL; ++cnt)
385 _dl_assert (cnt < _dl_tls_dtv_slotinfo_list->len);
387 size_t firstbyte = (-slotinfo[cnt].map->l_tls_firstbyte_offset
388 & (slotinfo[cnt].map->l_tls_align - 1));
389 size_t off;
390 max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
392 if (slotinfo[cnt].map->l_tls_blocksize <= freetop - freebottom)
394 off = roundup_pow2 (freebottom, slotinfo[cnt].map->l_tls_align);
395 if (off - freebottom < firstbyte)
396 off += slotinfo[cnt].map->l_tls_align;
397 if (off + slotinfo[cnt].map->l_tls_blocksize - firstbyte <= freetop)
399 slotinfo[cnt].map->l_tls_offset = off - firstbyte;
400 freebottom = (off + slotinfo[cnt].map->l_tls_blocksize
401 - firstbyte);
402 continue;
406 off = roundup_pow2 (offset, slotinfo[cnt].map->l_tls_align);
407 if (off - offset < firstbyte)
408 off += slotinfo[cnt].map->l_tls_align;
410 slotinfo[cnt].map->l_tls_offset = off - firstbyte;
411 if (off - firstbyte - offset > freetop - freebottom)
413 freebottom = offset;
414 freetop = off - firstbyte;
417 offset = off + slotinfo[cnt].map->l_tls_blocksize - firstbyte;
420 _dl_tls_static_used = offset;
421 _dl_tls_static_size = roundup_pow2 (offset + TLS_STATIC_SURPLUS,
422 TLS_TCB_ALIGN);
423 # else
424 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
425 # endif
427 /* The alignment requirement for the static TLS block. */
428 _dl_tls_static_align = max_align;
431 /* This is called only when the data structure setup was skipped at startup,
432 when there was no need for it then. Now we have dynamically loaded
433 something needing TLS, or libpthread needs it. */
434 rtld_hidden_proto(_dl_tls_setup)
436 internal_function
437 _dl_tls_setup (void)
439 _dl_assert (_dl_tls_dtv_slotinfo_list == NULL);
440 _dl_assert (_dl_tls_max_dtv_idx == 0);
442 const size_t nelem = 2 + TLS_SLOTINFO_SURPLUS;
444 _dl_tls_dtv_slotinfo_list
445 = _dl_calloc (1, (sizeof (struct dtv_slotinfo_list)
446 + nelem * sizeof (struct dtv_slotinfo)));
447 if (_dl_tls_dtv_slotinfo_list == NULL)
448 return -1;
450 _dl_tls_dtv_slotinfo_list->len = nelem;
452 /* Number of elements in the static TLS block. It can't be zero
453 because of various assumptions. The one element is null. */
454 _dl_tls_static_nelem = _dl_tls_max_dtv_idx = 1;
456 /* This initializes more variables for us. */
457 _dl_determine_tlsoffset ();
459 return 0;
461 rtld_hidden_def (_dl_tls_setup)
463 static void *
464 internal_function
465 allocate_dtv (void *result)
467 dtv_t *dtv;
468 size_t dtv_length;
470 /* We allocate a few more elements in the dtv than are needed for the
471 initial set of modules. This should avoid in most cases expansions
472 of the dtv. */
473 dtv_length = _dl_tls_max_dtv_idx + DTV_SURPLUS;
474 dtv = _dl_calloc (dtv_length + 2, sizeof (dtv_t));
475 if (dtv != NULL)
477 /* This is the initial length of the dtv. */
478 dtv[0].counter = dtv_length;
480 /* The rest of the dtv (including the generation counter) is
481 Initialize with zero to indicate nothing there. */
483 /* Add the dtv to the thread data structures. */
484 INSTALL_DTV (result, dtv);
486 else
487 result = NULL;
489 return result;
492 /* Get size and alignment requirements of the static TLS block. */
493 void
494 internal_function
495 _dl_get_tls_static_info (size_t *sizep, size_t *alignp)
497 *sizep = _dl_tls_static_size;
498 *alignp = _dl_tls_static_align;
501 void *
502 internal_function
503 _dl_allocate_tls_storage (void)
505 void *result;
506 size_t size = _dl_tls_static_size;
508 # if defined(TLS_DTV_AT_TP)
509 /* Memory layout is:
510 [ TLS_PRE_TCB_SIZE ] [ TLS_TCB_SIZE ] [ TLS blocks ]
511 ^ This should be returned. */
512 size += (TLS_PRE_TCB_SIZE + _dl_tls_static_align - 1)
513 & ~(_dl_tls_static_align - 1);
514 # endif
516 /* Allocate a correctly aligned chunk of memory. */
517 result = _dl_memalign (_dl_tls_static_align, size);
518 if (__builtin_expect (result != NULL, 1))
520 /* Allocate the DTV. */
521 void *allocated = result;
523 # ifdef TLS_TCB_AT_TP
524 /* The TCB follows the TLS blocks. */
525 result = (char *) result + size - TLS_TCB_SIZE;
527 /* Clear the TCB data structure. We can't ask the caller (i.e.
528 libpthread) to do it, because we will initialize the DTV et al. */
529 _dl_memset (result, '\0', TLS_TCB_SIZE);
530 # elif defined(TLS_DTV_AT_TP)
531 result = (char *) result + size - _dl_tls_static_size;
533 /* Clear the TCB data structure and TLS_PRE_TCB_SIZE bytes before it.
534 We can't ask the caller (i.e. libpthread) to do it, because we will
535 initialize the DTV et al. */
536 _dl_memset ((char *) result - TLS_PRE_TCB_SIZE, '\0',
537 TLS_PRE_TCB_SIZE + TLS_TCB_SIZE);
538 # endif
540 result = allocate_dtv (result);
541 if (result == NULL)
542 _dl_free (allocated);
545 return result;
548 void *
549 internal_function
550 _dl_allocate_tls_init (void *result)
552 if (result == NULL)
553 /* The memory allocation failed. */
554 return NULL;
556 dtv_t *dtv = GET_DTV (result);
557 struct dtv_slotinfo_list *listp;
558 size_t total = 0;
559 size_t maxgen = 0;
561 /* We have to prepare the dtv for all currently loaded modules using
562 TLS. For those which are dynamically loaded we add the values
563 indicating deferred allocation. */
564 listp = _dl_tls_dtv_slotinfo_list;
565 while (1)
567 size_t cnt;
569 for (cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
571 struct link_map *map;
572 void *dest;
574 /* Check for the total number of used slots. */
575 if (total + cnt > _dl_tls_max_dtv_idx)
576 break;
578 map = listp->slotinfo[cnt].map;
579 if (map == NULL)
580 /* Unused entry. */
581 continue;
583 /* Keep track of the maximum generation number. This might
584 not be the generation counter. */
585 maxgen = MAX (maxgen, listp->slotinfo[cnt].gen);
587 if (map->l_tls_offset == NO_TLS_OFFSET)
589 /* For dynamically loaded modules we simply store
590 the value indicating deferred allocation. */
591 dtv[map->l_tls_modid].pointer.val = TLS_DTV_UNALLOCATED;
592 dtv[map->l_tls_modid].pointer.is_static = false;
593 continue;
596 _dl_assert (map->l_tls_modid == cnt);
597 _dl_assert (map->l_tls_blocksize >= map->l_tls_initimage_size);
598 # ifdef TLS_TCB_AT_TP
599 _dl_assert ((size_t) map->l_tls_offset >= map->l_tls_blocksize);
600 dest = (char *) result - map->l_tls_offset;
601 # elif defined(TLS_DTV_AT_TP)
602 dest = (char *) result + map->l_tls_offset;
603 # else
604 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
605 # endif
607 /* Copy the initialization image and clear the BSS part. */
608 dtv[map->l_tls_modid].pointer.val = dest;
609 dtv[map->l_tls_modid].pointer.is_static = true;
610 _dl_memcpy(dest, map->l_tls_initimage, map->l_tls_initimage_size);
611 _dl_memset((dest + map->l_tls_initimage_size), '\0',
612 map->l_tls_blocksize - map->l_tls_initimage_size);
616 total += cnt;
617 if (total >= _dl_tls_max_dtv_idx)
618 break;
620 listp = listp->next;
621 _dl_assert (listp != NULL);
624 /* The DTV version is up-to-date now. */
625 dtv[0].counter = maxgen;
627 return result;
630 void *
631 internal_function
632 _dl_allocate_tls (void *mem)
634 return _dl_allocate_tls_init (mem == NULL
635 ? _dl_allocate_tls_storage ()
636 : allocate_dtv (mem));
639 void
640 internal_function
641 _dl_deallocate_tls (void *tcb, bool dealloc_tcb)
643 dtv_t *dtv = GET_DTV (tcb);
644 size_t cnt;
646 /* We need to free the memory allocated for non-static TLS. */
647 for (cnt = 0; cnt < dtv[-1].counter; ++cnt)
648 if (! dtv[1 + cnt].pointer.is_static
649 && dtv[1 + cnt].pointer.val != TLS_DTV_UNALLOCATED)
650 _dl_free (dtv[1 + cnt].pointer.val);
652 /* The array starts with dtv[-1]. */
653 if (dtv != _dl_initial_dtv)
654 _dl_free (dtv - 1);
656 if (dealloc_tcb)
658 # ifdef TLS_TCB_AT_TP
659 /* The TCB follows the TLS blocks. Back up to free the whole block. */
660 tcb -= _dl_tls_static_size - TLS_TCB_SIZE;
661 # elif defined(TLS_DTV_AT_TP)
662 /* Back up the TLS_PRE_TCB_SIZE bytes. */
663 tcb -= (TLS_PRE_TCB_SIZE + _dl_tls_static_align - 1)
664 & ~(_dl_tls_static_align - 1);
665 # endif
666 _dl_free (tcb);
670 static void *
671 allocate_and_init (struct link_map *map)
673 void *newp;
675 newp = _dl_memalign (map->l_tls_align, map->l_tls_blocksize);
676 if (newp == NULL)
678 _dl_dprintf(2, "%s:%d: Out of memory!!!\n", __func__, __LINE__);
679 _dl_exit(1);
682 /* Initialize the memory. */
683 _dl_memcpy (newp, map->l_tls_initimage, map->l_tls_initimage_size);
684 _dl_memset ((newp + map->l_tls_initimage_size), '\0',
685 map->l_tls_blocksize - map->l_tls_initimage_size);
687 return newp;
690 struct link_map *
691 _dl_update_slotinfo (unsigned long int req_modid)
693 struct link_map *the_map = NULL;
694 dtv_t *dtv = THREAD_DTV ();
696 /* The global dl_tls_dtv_slotinfo array contains for each module
697 index the generation counter current when the entry was created.
698 This array never shrinks so that all module indices which were
699 valid at some time can be used to access it. Before the first
700 use of a new module index in this function the array was extended
701 appropriately. Access also does not have to be guarded against
702 modifications of the array. It is assumed that pointer-size
703 values can be read atomically even in SMP environments. It is
704 possible that other threads at the same time dynamically load
705 code and therefore add to the slotinfo list. This is a problem
706 since we must not pick up any information about incomplete work.
707 The solution to this is to ignore all dtv slots which were
708 created after the one we are currently interested. We know that
709 dynamic loading for this module is completed and this is the last
710 load operation we know finished. */
711 unsigned long int idx = req_modid;
712 struct dtv_slotinfo_list *listp = _dl_tls_dtv_slotinfo_list;
714 _dl_debug_early ("Updating slotinfo for module %d\n", req_modid);
716 while (idx >= listp->len)
718 idx -= listp->len;
719 listp = listp->next;
722 if (dtv[0].counter < listp->slotinfo[idx].gen)
724 /* The generation counter for the slot is higher than what the
725 current dtv implements. We have to update the whole dtv but
726 only those entries with a generation counter <= the one for
727 the entry we need. */
728 size_t new_gen = listp->slotinfo[idx].gen;
729 size_t total = 0;
731 /* We have to look through the entire dtv slotinfo list. */
732 listp = _dl_tls_dtv_slotinfo_list;
735 size_t cnt;
737 for (cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
739 size_t gen = listp->slotinfo[cnt].gen;
741 if (gen > new_gen)
742 /* This is a slot for a generation younger than the
743 one we are handling now. It might be incompletely
744 set up so ignore it. */
745 continue;
747 /* If the entry is older than the current dtv layout we
748 know we don't have to handle it. */
749 if (gen <= dtv[0].counter)
750 continue;
752 /* If there is no map this means the entry is empty. */
753 struct link_map *map = listp->slotinfo[cnt].map;
754 if (map == NULL)
756 /* If this modid was used at some point the memory
757 might still be allocated. */
758 if (! dtv[total + cnt].pointer.is_static
759 && dtv[total + cnt].pointer.val != TLS_DTV_UNALLOCATED)
761 _dl_free (dtv[total + cnt].pointer.val);
762 dtv[total + cnt].pointer.val = TLS_DTV_UNALLOCATED;
765 continue;
768 /* Check whether the current dtv array is large enough. */
769 size_t modid = map->l_tls_modid;
770 _dl_assert (total + cnt == modid);
771 if (dtv[-1].counter < modid)
773 /* Reallocate the dtv. */
774 dtv_t *newp;
775 size_t newsize = _dl_tls_max_dtv_idx + DTV_SURPLUS;
776 size_t oldsize = dtv[-1].counter;
778 _dl_assert (map->l_tls_modid <= newsize);
780 if (dtv == _dl_initial_dtv)
782 /* This is the initial dtv that was allocated
783 during rtld startup using the dl-minimal.c
784 malloc instead of the real malloc. We can't
785 free it, we have to abandon the old storage. */
787 newp = _dl_malloc ((2 + newsize) * sizeof (dtv_t));
788 if (newp == NULL)
789 oom ();
790 _dl_memcpy (newp, &dtv[-1], oldsize * sizeof (dtv_t));
792 else
794 newp = _dl_realloc (&dtv[-1],
795 (2 + newsize) * sizeof (dtv_t));
796 if (newp == NULL)
797 oom ();
800 newp[0].counter = newsize;
802 /* Clear the newly allocated part. */
803 _dl_memset (newp + 2 + oldsize, '\0',
804 (newsize - oldsize) * sizeof (dtv_t));
806 /* Point dtv to the generation counter. */
807 dtv = &newp[1];
809 /* Install this new dtv in the thread data
810 structures. */
811 INSTALL_NEW_DTV (dtv);
814 /* If there is currently memory allocate for this
815 dtv entry free it. */
816 /* XXX Ideally we will at some point create a memory
817 pool. */
818 if (! dtv[modid].pointer.is_static
819 && dtv[modid].pointer.val != TLS_DTV_UNALLOCATED)
820 /* Note that free is called for NULL is well. We
821 deallocate even if it is this dtv entry we are
822 supposed to load. The reason is that we call
823 memalign and not malloc. */
824 _dl_free (dtv[modid].pointer.val);
826 /* This module is loaded dynamically- We defer memory
827 allocation. */
828 dtv[modid].pointer.is_static = false;
829 dtv[modid].pointer.val = TLS_DTV_UNALLOCATED;
831 if (modid == req_modid)
832 the_map = map;
835 total += listp->len;
837 while ((listp = listp->next) != NULL);
839 /* This will be the new maximum generation counter. */
840 dtv[0].counter = new_gen;
843 return the_map;
847 /* The generic dynamic and local dynamic model cannot be used in
848 statically linked applications. */
849 void *
850 __tls_get_addr (GET_ADDR_ARGS)
852 dtv_t *dtv = THREAD_DTV ();
853 struct link_map *the_map = NULL;
854 void *p;
856 if (__builtin_expect (dtv[0].counter != _dl_tls_generation, 0))
858 the_map = _dl_update_slotinfo (GET_ADDR_MODULE);
859 dtv = THREAD_DTV ();
862 p = dtv[GET_ADDR_MODULE].pointer.val;
864 if (__builtin_expect (p == TLS_DTV_UNALLOCATED, 0))
866 /* The allocation was deferred. Do it now. */
867 if (the_map == NULL)
869 /* Find the link map for this module. */
870 size_t idx = GET_ADDR_MODULE;
871 struct dtv_slotinfo_list *listp = _dl_tls_dtv_slotinfo_list;
873 while (idx >= listp->len)
875 idx -= listp->len;
876 listp = listp->next;
879 the_map = listp->slotinfo[idx].map;
882 p = dtv[GET_ADDR_MODULE].pointer.val = allocate_and_init (the_map);
883 dtv[GET_ADDR_MODULE].pointer.is_static = false;
886 return (char *) p + GET_ADDR_OFFSET;
889 void
890 _dl_add_to_slotinfo (struct link_map *l)
892 /* Now that we know the object is loaded successfully add
893 modules containing TLS data to the dtv info table. We
894 might have to increase its size. */
895 struct dtv_slotinfo_list *listp;
896 struct dtv_slotinfo_list *prevp;
897 size_t idx = l->l_tls_modid;
899 _dl_debug_early("Adding to slotinfo for %s\n", l->l_name);
901 /* Find the place in the dtv slotinfo list. */
902 listp = _dl_tls_dtv_slotinfo_list;
903 prevp = NULL; /* Needed to shut up gcc. */
906 /* Does it fit in the array of this list element? */
907 if (idx < listp->len)
908 break;
909 idx -= listp->len;
910 prevp = listp;
911 listp = listp->next;
913 while (listp != NULL);
915 if (listp == NULL)
917 /* When we come here it means we have to add a new element
918 to the slotinfo list. And the new module must be in
919 the first slot. */
920 _dl_assert (idx == 0);
922 listp = prevp->next = (struct dtv_slotinfo_list *)
923 _dl_malloc (sizeof (struct dtv_slotinfo_list)
924 + TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
925 if (listp == NULL)
927 /* We ran out of memory. We will simply fail this
928 call but don't undo anything we did so far. The
929 application will crash or be terminated anyway very
930 soon. */
932 /* We have to do this since some entries in the dtv
933 slotinfo array might already point to this
934 generation. */
935 ++_dl_tls_generation;
937 _dl_dprintf(2, "cannot create TLS data structures: ABORT\n");
938 _dl_exit (127);
941 listp->len = TLS_SLOTINFO_SURPLUS;
942 listp->next = NULL;
943 _dl_memset (listp->slotinfo, '\0',
944 TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
947 /* Add the information into the slotinfo data structure. */
948 listp->slotinfo[idx].map = l;
949 listp->slotinfo[idx].gen = _dl_tls_generation + 1;
950 /* ??? ideally this would be done once per call to dlopen. However there's
951 no easy way to indicate whether a library used TLS, so do it here
952 instead. */
953 /* Bump the TLS generation number. */
954 _dl_tls_generation++;
957 /* Taken from glibc/elf/rtld.c */
958 static bool tls_init_tp_called;
960 rtld_hidden_proto(_dl_initial_error_catch_tsd)
961 /* _dl_error_catch_tsd points to this for the single-threaded case.
962 It's reset by the thread library for multithreaded programs. */
963 void ** __attribute__ ((const))
964 _dl_initial_error_catch_tsd (void)
966 static
967 #if 0 /* def ARCH_NEEDS_BOOTSTRAP_RELOCS */
968 /* If we have to do bootstrap relocs anyway we might as well */
969 __thread
970 # endif
971 void *__tsd_data;
972 return &__tsd_data;
975 #ifdef SHARED
976 void*
977 internal_function
978 init_tls (void);
980 rtld_hidden_proto(init_tls)
981 void *
982 internal_function
983 init_tls (void)
985 /* Number of elements in the static TLS block. */
986 _dl_tls_static_nelem = _dl_tls_max_dtv_idx;
988 /* Do not do this twice. The audit interface might have required
989 the DTV interfaces to be set up early. */
990 if (_dl_initial_dtv != NULL)
991 return NULL;
993 /* Allocate the array which contains the information about the
994 dtv slots. We allocate a few entries more than needed to
995 avoid the need for reallocation. */
996 size_t nelem = _dl_tls_max_dtv_idx + 1 + TLS_SLOTINFO_SURPLUS;
998 /* Allocate. */
999 _dl_assert (_dl_tls_dtv_slotinfo_list == NULL);
1000 _dl_tls_dtv_slotinfo_list = (struct dtv_slotinfo_list *)
1001 _dl_calloc (sizeof (struct dtv_slotinfo_list)
1002 + nelem * sizeof (struct dtv_slotinfo), 1);
1003 /* No need to check the return value. If memory allocation failed
1004 the program would have been terminated. */
1006 struct dtv_slotinfo *slotinfo = _dl_tls_dtv_slotinfo_list->slotinfo;
1007 _dl_tls_dtv_slotinfo_list->len = nelem;
1008 _dl_tls_dtv_slotinfo_list->next = NULL;
1010 /* Fill in the information from the loaded modules. No namespace
1011 but the base one can be filled at this time. */
1012 int i = 0;
1013 struct link_map *l;
1014 for (l = (struct link_map *) _dl_loaded_modules; l != NULL; l = l->l_next)
1015 if (l->l_tls_blocksize != 0)
1017 /* This is a module with TLS data. Store the map reference.
1018 The generation counter is zero. */
1020 /* Skeep slot[0]: it will be never used */
1021 slotinfo[++i].map = l;
1023 _dl_assert (i == _dl_tls_max_dtv_idx);
1025 /* Compute the TLS offsets for the various blocks. */
1026 _dl_determine_tlsoffset ();
1028 /* Construct the static TLS block and the dtv for the initial
1029 thread. For some platforms this will include allocating memory
1030 for the thread descriptor. The memory for the TLS block will
1031 never be freed. It should be allocated accordingly. The dtv
1032 array can be changed if dynamic loading requires it. */
1033 void *tcbp = _dl_allocate_tls_storage ();
1034 if (tcbp == NULL) {
1035 _dl_debug_early("\ncannot allocate TLS data structures for initial thread");
1036 _dl_exit(30);
1039 /* Store for detection of the special case by __tls_get_addr
1040 so it knows not to pass this dtv to the normal realloc. */
1041 _dl_initial_dtv = GET_DTV (tcbp);
1043 /* And finally install it for the main thread. If ld.so itself uses
1044 TLS we know the thread pointer was initialized earlier. */
1045 const char *lossage = (char *)TLS_INIT_TP (tcbp, USE___THREAD);
1046 if(__builtin_expect (lossage != NULL, 0)) {
1047 _dl_debug_early("cannot set up thread-local storage: %s\n", lossage);
1048 _dl_exit(30);
1050 tls_init_tp_called = true;
1052 return tcbp;
1054 rtld_hidden_def (init_tls)
1055 #endif