Update.
[glibc.git] / sysdeps / generic / dl-tls.c
blob099742ceff5468d8a247dfd2dfe653bccb9cf9ee
1 /* Thread-local storage handling in the ELF dynamic linker. Generic version.
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, 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 <errno.h>
22 #include <libintl.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/param.h>
28 #include <tls.h>
30 /* We don't need any of this if TLS is not supported. */
31 #ifdef USE_TLS
33 # include <dl-tls.h>
34 # include <ldsodefs.h>
36 /* Amount of excess space to allocate in the static TLS area
37 to allow dynamic loading of modules defining IE-model TLS data. */
38 # define TLS_STATIC_SURPLUS 64 + DL_NNS * 100
40 /* Value used for dtv entries for which the allocation is delayed. */
41 # define TLS_DTV_UNALLOCATED ((void *) -1l)
44 /* Out-of-memory handler. */
45 # ifdef SHARED
46 static void
47 __attribute__ ((__noreturn__))
48 oom (void)
50 _dl_fatal_printf ("cannot allocate memory for thread-local data: ABORT\n");
52 # endif
56 size_t
57 internal_function
58 _dl_next_tls_modid (void)
60 size_t result;
62 if (__builtin_expect (GL(dl_tls_dtv_gaps), false))
64 size_t disp = 0;
65 struct dtv_slotinfo_list *runp = GL(dl_tls_dtv_slotinfo_list);
67 /* Note that this branch will never be executed during program
68 start since there are no gaps at that time. Therefore it
69 does not matter that the dl_tls_dtv_slotinfo is not allocated
70 yet when the function is called for the first times.
72 NB: the offset +1 is due to the fact that DTV[0] is used
73 for something else. */
74 result = GL(dl_tls_static_nelem) + 1;
75 /* If the following would not be true we mustn't have assumed
76 there is a gap. */
77 assert (result <= GL(dl_tls_max_dtv_idx));
80 while (result - disp < runp->len)
82 if (runp->slotinfo[result - disp].map == NULL)
83 break;
85 ++result;
86 assert (result <= GL(dl_tls_max_dtv_idx) + 1);
89 if (result - disp < runp->len)
90 break;
92 disp += runp->len;
94 while ((runp = runp->next) != NULL);
96 if (result > GL(dl_tls_max_dtv_idx))
98 /* The new index must indeed be exactly one higher than the
99 previous high. */
100 assert (result == GL(dl_tls_max_dtv_idx) + 1);
102 /* There is no gap anymore. */
103 GL(dl_tls_dtv_gaps) = false;
105 goto nogaps;
108 else
110 /* No gaps, allocate a new entry. */
111 nogaps:
112 result = ++GL(dl_tls_max_dtv_idx);
115 return result;
118 # ifdef SHARED
120 void
121 internal_function
122 _dl_determine_tlsoffset (void)
124 size_t max_align = TLS_TCB_ALIGN;
125 size_t freetop = 0;
126 size_t freebottom = 0;
128 /* The first element of the dtv slot info list is allocated. */
129 assert (GL(dl_tls_dtv_slotinfo_list) != NULL);
130 /* There is at this point only one element in the
131 dl_tls_dtv_slotinfo_list list. */
132 assert (GL(dl_tls_dtv_slotinfo_list)->next == NULL);
134 struct dtv_slotinfo *slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo;
136 /* Determining the offset of the various parts of the static TLS
137 block has several dependencies. In addition we have to work
138 around bugs in some toolchains.
140 Each TLS block from the objects available at link time has a size
141 and an alignment requirement. The GNU ld computes the alignment
142 requirements for the data at the positions *in the file*, though.
143 I.e, it is not simply possible to allocate a block with the size
144 of the TLS program header entry. The data is layed out assuming
145 that the first byte of the TLS block fulfills
147 p_vaddr mod p_align == &TLS_BLOCK mod p_align
149 This means we have to add artificial padding at the beginning of
150 the TLS block. These bytes are never used for the TLS data in
151 this module but the first byte allocated must be aligned
152 according to mod p_align == 0 so that the first byte of the TLS
153 block is aligned according to p_vaddr mod p_align. This is ugly
154 and the linker can help by computing the offsets in the TLS block
155 assuming the first byte of the TLS block is aligned according to
156 p_align.
158 The extra space which might be allocated before the first byte of
159 the TLS block need not go unused. The code below tries to use
160 that memory for the next TLS block. This can work if the total
161 memory requirement for the next TLS block is smaller than the
162 gap. */
164 # if TLS_TCB_AT_TP
165 /* We simply start with zero. */
166 size_t offset = 0;
168 for (size_t cnt = 0; slotinfo[cnt].map != NULL; ++cnt)
170 assert (cnt < GL(dl_tls_dtv_slotinfo_list)->len);
172 size_t firstbyte = (-slotinfo[cnt].map->l_tls_firstbyte_offset
173 & (slotinfo[cnt].map->l_tls_align - 1));
174 size_t off;
175 max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
177 if (freebottom - freetop >= slotinfo[cnt].map->l_tls_blocksize)
179 off = roundup (freetop + slotinfo[cnt].map->l_tls_blocksize
180 - firstbyte, slotinfo[cnt].map->l_tls_align)
181 + firstbyte;
182 if (off <= freebottom)
184 freetop = off;
186 /* XXX For some architectures we perhaps should store the
187 negative offset. */
188 slotinfo[cnt].map->l_tls_offset = off;
189 continue;
193 off = roundup (offset + slotinfo[cnt].map->l_tls_blocksize - firstbyte,
194 slotinfo[cnt].map->l_tls_align) + firstbyte;
195 if (off > offset + slotinfo[cnt].map->l_tls_blocksize
196 + (freebottom - freetop))
198 freetop = offset;
199 freebottom = off - slotinfo[cnt].map->l_tls_blocksize;
201 offset = off;
203 /* XXX For some architectures we perhaps should store the
204 negative offset. */
205 slotinfo[cnt].map->l_tls_offset = off;
208 GL(dl_tls_static_used) = offset;
209 GL(dl_tls_static_size) = (roundup (offset + TLS_STATIC_SURPLUS, max_align)
210 + TLS_TCB_SIZE);
211 # elif TLS_DTV_AT_TP
212 /* The TLS blocks start right after the TCB. */
213 size_t offset = TLS_TCB_SIZE;
215 for (size_t cnt = 0; slotinfo[cnt].map != NULL; ++cnt)
217 assert (cnt < GL(dl_tls_dtv_slotinfo_list)->len);
219 size_t firstbyte = (-slotinfo[cnt].map->l_tls_firstbyte_offset
220 & (slotinfo[cnt].map->l_tls_align - 1));
221 size_t off;
222 max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
224 if (slotinfo[cnt].map->l_tls_blocksize <= freetop - freebottom)
226 off = roundup (freebottom, slotinfo[cnt].map->l_tls_align);
227 if (off - freebottom < firstbyte)
228 off += slotinfo[cnt].map->l_tls_align;
229 if (off + slotinfo[cnt].map->l_tls_blocksize - firstbyte <= freetop)
231 slotinfo[cnt].map->l_tls_offset = off - firstbyte;
232 freebottom = (off + slotinfo[cnt].map->l_tls_blocksize
233 - firstbyte);
234 continue;
238 off = roundup (offset, slotinfo[cnt].map->l_tls_align);
239 if (off - offset < firstbyte)
240 off += slotinfo[cnt].map->l_tls_align;
242 slotinfo[cnt].map->l_tls_offset = off - firstbyte;
243 if (off - firstbyte - offset > freetop - freebottom)
245 freebottom = offset;
246 freetop = off - firstbyte;
249 offset = off + slotinfo[cnt].map->l_tls_blocksize - firstbyte;
252 GL(dl_tls_static_used) = offset;
253 GL(dl_tls_static_size) = roundup (offset + TLS_STATIC_SURPLUS,
254 TLS_TCB_ALIGN);
255 # else
256 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
257 # endif
259 /* The alignment requirement for the static TLS block. */
260 GL(dl_tls_static_align) = max_align;
264 /* This is called only when the data structure setup was skipped at startup,
265 when there was no need for it then. Now we have dynamically loaded
266 something needing TLS, or libpthread needs it. */
268 internal_function
269 _dl_tls_setup (void)
271 assert (GL(dl_tls_dtv_slotinfo_list) == NULL);
272 assert (GL(dl_tls_max_dtv_idx) == 0);
274 const size_t nelem = 2 + TLS_SLOTINFO_SURPLUS;
276 GL(dl_tls_dtv_slotinfo_list)
277 = calloc (1, (sizeof (struct dtv_slotinfo_list)
278 + nelem * sizeof (struct dtv_slotinfo)));
279 if (GL(dl_tls_dtv_slotinfo_list) == NULL)
280 return -1;
282 GL(dl_tls_dtv_slotinfo_list)->len = nelem;
284 /* Number of elements in the static TLS block. It can't be zero
285 because of various assumptions. The one element is null. */
286 GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx) = 1;
288 /* This initializes more variables for us. */
289 _dl_determine_tlsoffset ();
291 return 0;
293 rtld_hidden_def (_dl_tls_setup)
294 # endif
296 static void *
297 internal_function
298 allocate_dtv (void *result)
300 dtv_t *dtv;
301 size_t dtv_length;
303 /* We allocate a few more elements in the dtv than are needed for the
304 initial set of modules. This should avoid in most cases expansions
305 of the dtv. */
306 dtv_length = GL(dl_tls_max_dtv_idx) + DTV_SURPLUS;
307 dtv = calloc (dtv_length + 2, sizeof (dtv_t));
308 if (dtv != NULL)
310 /* This is the initial length of the dtv. */
311 dtv[0].counter = dtv_length;
313 /* The rest of the dtv (including the generation counter) is
314 Initialize with zero to indicate nothing there. */
316 /* Add the dtv to the thread data structures. */
317 INSTALL_DTV (result, dtv);
319 else
320 result = NULL;
322 return result;
326 /* Get size and alignment requirements of the static TLS block. */
327 void
328 internal_function
329 _dl_get_tls_static_info (size_t *sizep, size_t *alignp)
331 *sizep = GL(dl_tls_static_size);
332 *alignp = GL(dl_tls_static_align);
336 void *
337 internal_function
338 _dl_allocate_tls_storage (void)
340 void *result;
341 size_t size = GL(dl_tls_static_size);
343 # if TLS_DTV_AT_TP
344 /* Memory layout is:
345 [ TLS_PRE_TCB_SIZE ] [ TLS_TCB_SIZE ] [ TLS blocks ]
346 ^ This should be returned. */
347 size += (TLS_PRE_TCB_SIZE + GL(dl_tls_static_align) - 1)
348 & ~(GL(dl_tls_static_align) - 1);
349 # endif
351 /* Allocate a correctly aligned chunk of memory. */
352 result = __libc_memalign (GL(dl_tls_static_align), size);
353 if (__builtin_expect (result != NULL, 1))
355 /* Allocate the DTV. */
356 void *allocated = result;
358 # if TLS_TCB_AT_TP
359 /* The TCB follows the TLS blocks. */
360 result = (char *) result + size - TLS_TCB_SIZE;
362 /* Clear the TCB data structure. We can't ask the caller (i.e.
363 libpthread) to do it, because we will initialize the DTV et al. */
364 memset (result, '\0', TLS_TCB_SIZE);
365 # elif TLS_DTV_AT_TP
366 result = (char *) result + size - GL(dl_tls_static_size);
368 /* Clear the TCB data structure and TLS_PRE_TCB_SIZE bytes before it.
369 We can't ask the caller (i.e. libpthread) to do it, because we will
370 initialize the DTV et al. */
371 memset ((char *) result - TLS_PRE_TCB_SIZE, '\0',
372 TLS_PRE_TCB_SIZE + TLS_TCB_SIZE);
373 # endif
375 result = allocate_dtv (result);
376 if (result == NULL)
377 free (allocated);
380 return result;
384 void *
385 internal_function
386 _dl_allocate_tls_init (void *result)
388 if (result == NULL)
389 /* The memory allocation failed. */
390 return NULL;
392 dtv_t *dtv = GET_DTV (result);
393 struct dtv_slotinfo_list *listp;
394 size_t total = 0;
395 size_t maxgen = 0;
397 /* We have to prepare the dtv for all currently loaded modules using
398 TLS. For those which are dynamically loaded we add the values
399 indicating deferred allocation. */
400 listp = GL(dl_tls_dtv_slotinfo_list);
401 while (1)
403 size_t cnt;
405 for (cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
407 struct link_map *map;
408 void *dest;
410 /* Check for the total number of used slots. */
411 if (total + cnt > GL(dl_tls_max_dtv_idx))
412 break;
414 map = listp->slotinfo[cnt].map;
415 if (map == NULL)
416 /* Unused entry. */
417 continue;
419 /* Keep track of the maximum generation number. This might
420 not be the generation counter. */
421 maxgen = MAX (maxgen, listp->slotinfo[cnt].gen);
423 if (map->l_tls_offset == NO_TLS_OFFSET)
425 /* For dynamically loaded modules we simply store
426 the value indicating deferred allocation. */
427 dtv[map->l_tls_modid].pointer.val = TLS_DTV_UNALLOCATED;
428 dtv[map->l_tls_modid].pointer.is_static = false;
429 continue;
432 assert (map->l_tls_modid == cnt);
433 assert (map->l_tls_blocksize >= map->l_tls_initimage_size);
434 # if TLS_TCB_AT_TP
435 assert ((size_t) map->l_tls_offset >= map->l_tls_blocksize);
436 dest = (char *) result - map->l_tls_offset;
437 # elif TLS_DTV_AT_TP
438 dest = (char *) result + map->l_tls_offset;
439 # else
440 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
441 # endif
443 /* Copy the initialization image and clear the BSS part. */
444 dtv[map->l_tls_modid].pointer.val = dest;
445 dtv[map->l_tls_modid].pointer.is_static = true;
446 memset (__mempcpy (dest, map->l_tls_initimage,
447 map->l_tls_initimage_size), '\0',
448 map->l_tls_blocksize - map->l_tls_initimage_size);
451 total += cnt;
452 if (total >= GL(dl_tls_max_dtv_idx))
453 break;
455 listp = listp->next;
456 assert (listp != NULL);
459 /* The DTV version is up-to-date now. */
460 dtv[0].counter = maxgen;
462 return result;
464 rtld_hidden_def (_dl_allocate_tls_init)
466 void *
467 internal_function
468 _dl_allocate_tls (void *mem)
470 return _dl_allocate_tls_init (mem == NULL
471 ? _dl_allocate_tls_storage ()
472 : allocate_dtv (mem));
474 rtld_hidden_def (_dl_allocate_tls)
477 void
478 internal_function
479 _dl_deallocate_tls (void *tcb, bool dealloc_tcb)
481 dtv_t *dtv = GET_DTV (tcb);
483 /* We need to free the memory allocated for non-static TLS. */
484 for (size_t cnt = 0; cnt < dtv[-1].counter; ++cnt)
485 if (! dtv[1 + cnt].pointer.is_static
486 && dtv[1 + cnt].pointer.val != TLS_DTV_UNALLOCATED)
487 free (dtv[1 + cnt].pointer.val);
489 /* The array starts with dtv[-1]. */
490 #ifdef SHARED
491 if (dtv != GL(dl_initial_dtv))
492 #endif
493 free (dtv - 1);
495 if (dealloc_tcb)
497 # if TLS_TCB_AT_TP
498 /* The TCB follows the TLS blocks. Back up to free the whole block. */
499 tcb -= GL(dl_tls_static_size) - TLS_TCB_SIZE;
500 # elif TLS_DTV_AT_TP
501 /* Back up the TLS_PRE_TCB_SIZE bytes. */
502 tcb -= (TLS_PRE_TCB_SIZE + GL(dl_tls_static_align) - 1)
503 & ~(GL(dl_tls_static_align) - 1);
504 # endif
505 free (tcb);
508 rtld_hidden_def (_dl_deallocate_tls)
511 # ifdef SHARED
512 /* The __tls_get_addr function has two basic forms which differ in the
513 arguments. The IA-64 form takes two parameters, the module ID and
514 offset. The form used, among others, on IA-32 takes a reference to
515 a special structure which contain the same information. The second
516 form seems to be more often used (in the moment) so we default to
517 it. Users of the IA-64 form have to provide adequate definitions
518 of the following macros. */
519 # ifndef GET_ADDR_ARGS
520 # define GET_ADDR_ARGS tls_index *ti
521 # endif
522 # ifndef GET_ADDR_MODULE
523 # define GET_ADDR_MODULE ti->ti_module
524 # endif
525 # ifndef GET_ADDR_OFFSET
526 # define GET_ADDR_OFFSET ti->ti_offset
527 # endif
530 static void *
531 allocate_and_init (struct link_map *map)
533 void *newp;
535 newp = __libc_memalign (map->l_tls_align, map->l_tls_blocksize);
536 if (newp == NULL)
537 oom ();
539 /* Initialize the memory. */
540 memset (__mempcpy (newp, map->l_tls_initimage, map->l_tls_initimage_size),
541 '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
543 return newp;
547 struct link_map *
548 _dl_update_slotinfo (unsigned long int req_modid)
550 struct link_map *the_map = NULL;
551 dtv_t *dtv = THREAD_DTV ();
553 /* The global dl_tls_dtv_slotinfo array contains for each module
554 index the generation counter current when the entry was created.
555 This array never shrinks so that all module indices which were
556 valid at some time can be used to access it. Before the first
557 use of a new module index in this function the array was extended
558 appropriately. Access also does not have to be guarded against
559 modifications of the array. It is assumed that pointer-size
560 values can be read atomically even in SMP environments. It is
561 possible that other threads at the same time dynamically load
562 code and therefore add to the slotinfo list. This is a problem
563 since we must not pick up any information about incomplete work.
564 The solution to this is to ignore all dtv slots which were
565 created after the one we are currently interested. We know that
566 dynamic loading for this module is completed and this is the last
567 load operation we know finished. */
568 unsigned long int idx = req_modid;
569 struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list);
571 while (idx >= listp->len)
573 idx -= listp->len;
574 listp = listp->next;
577 if (dtv[0].counter < listp->slotinfo[idx].gen)
579 /* The generation counter for the slot is higher than what the
580 current dtv implements. We have to update the whole dtv but
581 only those entries with a generation counter <= the one for
582 the entry we need. */
583 size_t new_gen = listp->slotinfo[idx].gen;
584 size_t total = 0;
586 /* We have to look through the entire dtv slotinfo list. */
587 listp = GL(dl_tls_dtv_slotinfo_list);
590 for (size_t cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
592 size_t gen = listp->slotinfo[cnt].gen;
594 if (gen > new_gen)
595 /* This is a slot for a generation younger than the
596 one we are handling now. It might be incompletely
597 set up so ignore it. */
598 continue;
600 /* If the entry is older than the current dtv layout we
601 know we don't have to handle it. */
602 if (gen <= dtv[0].counter)
603 continue;
605 /* If there is no map this means the entry is empty. */
606 struct link_map *map = listp->slotinfo[cnt].map;
607 if (map == NULL)
609 /* If this modid was used at some point the memory
610 might still be allocated. */
611 if (! dtv[total + cnt].pointer.is_static
612 && dtv[total + cnt].pointer.val != TLS_DTV_UNALLOCATED)
614 free (dtv[total + cnt].pointer.val);
615 dtv[total + cnt].pointer.val = TLS_DTV_UNALLOCATED;
618 continue;
621 /* Check whether the current dtv array is large enough. */
622 size_t modid = map->l_tls_modid;
623 assert (total + cnt == modid);
624 if (dtv[-1].counter < modid)
626 /* Reallocate the dtv. */
627 dtv_t *newp;
628 size_t newsize = GL(dl_tls_max_dtv_idx) + DTV_SURPLUS;
629 size_t oldsize = dtv[-1].counter;
631 assert (map->l_tls_modid <= newsize);
633 if (dtv == GL(dl_initial_dtv))
635 /* This is the initial dtv that was allocated
636 during rtld startup using the dl-minimal.c
637 malloc instead of the real malloc. We can't
638 free it, we have to abandon the old storage. */
640 newp = malloc ((2 + newsize) * sizeof (dtv_t));
641 if (newp == NULL)
642 oom ();
643 memcpy (newp, &dtv[-1], oldsize * sizeof (dtv_t));
645 else
647 newp = realloc (&dtv[-1],
648 (2 + newsize) * sizeof (dtv_t));
649 if (newp == NULL)
650 oom ();
653 newp[0].counter = newsize;
655 /* Clear the newly allocated part. */
656 memset (newp + 2 + oldsize, '\0',
657 (newsize - oldsize) * sizeof (dtv_t));
659 /* Point dtv to the generation counter. */
660 dtv = &newp[1];
662 /* Install this new dtv in the thread data
663 structures. */
664 INSTALL_NEW_DTV (dtv);
667 /* If there is currently memory allocate for this
668 dtv entry free it. */
669 /* XXX Ideally we will at some point create a memory
670 pool. */
671 if (! dtv[modid].pointer.is_static
672 && dtv[modid].pointer.val != TLS_DTV_UNALLOCATED)
673 /* Note that free is called for NULL is well. We
674 deallocate even if it is this dtv entry we are
675 supposed to load. The reason is that we call
676 memalign and not malloc. */
677 free (dtv[modid].pointer.val);
679 /* This module is loaded dynamically- We defer memory
680 allocation. */
681 dtv[modid].pointer.is_static = false;
682 dtv[modid].pointer.val = TLS_DTV_UNALLOCATED;
684 if (modid == req_modid)
685 the_map = map;
688 total += listp->len;
690 while ((listp = listp->next) != NULL);
692 /* This will be the new maximum generation counter. */
693 dtv[0].counter = new_gen;
696 return the_map;
700 /* The generic dynamic and local dynamic model cannot be used in
701 statically linked applications. */
702 void *
703 __tls_get_addr (GET_ADDR_ARGS)
705 dtv_t *dtv = THREAD_DTV ();
706 struct link_map *the_map = NULL;
707 void *p;
709 if (__builtin_expect (dtv[0].counter != GL(dl_tls_generation), 0))
710 the_map = _dl_update_slotinfo (GET_ADDR_MODULE);
712 p = dtv[GET_ADDR_MODULE].pointer.val;
714 if (__builtin_expect (p == TLS_DTV_UNALLOCATED, 0))
716 /* The allocation was deferred. Do it now. */
717 if (the_map == NULL)
719 /* Find the link map for this module. */
720 size_t idx = GET_ADDR_MODULE;
721 struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list);
723 while (idx >= listp->len)
725 idx -= listp->len;
726 listp = listp->next;
729 the_map = listp->slotinfo[idx].map;
732 p = dtv[GET_ADDR_MODULE].pointer.val = allocate_and_init (the_map);
733 dtv[GET_ADDR_MODULE].pointer.is_static = false;
736 return (char *) p + GET_ADDR_OFFSET;
738 # endif
742 void
743 _dl_add_to_slotinfo (struct link_map *l)
745 /* Now that we know the object is loaded successfully add
746 modules containing TLS data to the dtv info table. We
747 might have to increase its size. */
748 struct dtv_slotinfo_list *listp;
749 struct dtv_slotinfo_list *prevp;
750 size_t idx = l->l_tls_modid;
752 /* Find the place in the dtv slotinfo list. */
753 listp = GL(dl_tls_dtv_slotinfo_list);
754 prevp = NULL; /* Needed to shut up gcc. */
757 /* Does it fit in the array of this list element? */
758 if (idx < listp->len)
759 break;
760 idx -= listp->len;
761 prevp = listp;
762 listp = listp->next;
764 while (listp != NULL);
766 if (listp == NULL)
768 /* When we come here it means we have to add a new element
769 to the slotinfo list. And the new module must be in
770 the first slot. */
771 assert (idx == 0);
773 listp = prevp->next = (struct dtv_slotinfo_list *)
774 malloc (sizeof (struct dtv_slotinfo_list)
775 + TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
776 if (listp == NULL)
778 /* We ran out of memory. We will simply fail this
779 call but don't undo anything we did so far. The
780 application will crash or be terminated anyway very
781 soon. */
783 /* We have to do this since some entries in the dtv
784 slotinfo array might already point to this
785 generation. */
786 ++GL(dl_tls_generation);
788 _dl_signal_error (ENOMEM, "dlopen", NULL, N_("\
789 cannot create TLS data structures"));
792 listp->len = TLS_SLOTINFO_SURPLUS;
793 listp->next = NULL;
794 memset (listp->slotinfo, '\0',
795 TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
798 /* Add the information into the slotinfo data structure. */
799 listp->slotinfo[idx].map = l;
800 listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
802 #endif /* use TLS */