OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / libpthread / nptl / sysdeps / generic / dl-tls.c
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.
4
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.
9
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.
14
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/>.  */
18
19 #if defined SHARED || defined NOT_IN_libc
20 # error in buildsystem: This file is for libc.a
21 #endif
22 #include <libintl.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <sys/param.h>
26 #include <tls.h>
27 #include <dl-tls.h>
28 #include <ldsodefs.h>
29 #include <dl-elf.h>
30 #include <dl-hash.h>
31
32 #include <assert.h>
33 #include <link.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <stdio.h>
37
38 #define _dl_malloc      malloc
39 #define _dl_memset      memset
40 #define _dl_mempcpy     mempcpy
41 #define _dl_dprintf     fprintf
42 #define _dl_debug_file  stderr
43 #define _dl_exit        exit
44
45 /* Amount of excess space to allocate in the static TLS area
46    to allow dynamic loading of modules defining IE-model TLS data.  */
47 # define TLS_STATIC_SURPLUS     64 + DL_NNS * 100
48
49 /* Value used for dtv entries for which the allocation is delayed.  */
50 # define TLS_DTV_UNALLOCATED    ((void *) -1l)
51
52
53 /* Out-of-memory handler.  */
54 # ifdef SHARED
55 static void
56 __attribute__ ((__noreturn__))
57 oom (void)
58 {
59         do {
60                 _dl_dprintf (_dl_debug_file,
61                         "cannot allocate thread-local memory: ABORT\n");
62                 _dl_exit (127);
63         } while (1);
64 }
65 # endif
66
67
68 void *_dl_memalign(size_t alignment, size_t bytes);
69 void *_dl_memalign(size_t alignment, size_t bytes)
70 {
71         return _dl_malloc(bytes);
72 }
73
74
75 /*
76  * We are trying to perform a static TLS relocation in MAP, but it was
77  * dynamically loaded.  This can only work if there is enough surplus in
78  * the static TLS area already allocated for each running thread.  If this
79  * object's TLS segment is too big to fit, we fail.  If it fits,
80  * we set MAP->l_tls_offset and return.
81  * This function intentionally does not return any value but signals error
82  * directly, as static TLS should be rare and code handling it should
83  * not be inlined as much as possible.
84  */
85
86
87 void
88 internal_function __attribute_noinline__
89 _dl_allocate_static_tls (struct link_map *map)
90 {
91         /* If the alignment requirements are too high fail.  */
92         if (map->l_tls_align > _dl_tls_static_align)
93         {
94 fail:
95                 _dl_dprintf(_dl_debug_file, "cannot allocate memory in static TLS block");
96                 _dl_exit(30);
97         }
98
99 # if defined(TLS_TCB_AT_TP)
100         size_t freebytes;
101         size_t n;
102         size_t blsize;
103
104         freebytes = _dl_tls_static_size - _dl_tls_static_used - TLS_TCB_SIZE;
105
106         blsize = map->l_tls_blocksize + map->l_tls_firstbyte_offset;
107         if (freebytes < blsize)
108                 goto fail;
109
110         n = (freebytes - blsize) / map->l_tls_align;
111
112         size_t offset = _dl_tls_static_used + (freebytes - n * map->l_tls_align
113                 - map->l_tls_firstbyte_offset);
114
115         map->l_tls_offset = _dl_tls_static_used = offset;
116 # elif defined(TLS_DTV_AT_TP)
117         size_t used;
118         size_t check;
119
120         size_t offset = roundup (_dl_tls_static_used, map->l_tls_align);
121         used = offset + map->l_tls_blocksize;
122         check = used;
123
124         /* dl_tls_static_used includes the TCB at the beginning. */
125         if (check > _dl_tls_static_size)
126                 goto fail;
127
128         map->l_tls_offset = offset;
129         _dl_tls_static_used = used;
130 # else
131 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
132 # endif
133
134         /*
135          * If the object is not yet relocated we cannot initialize the
136          * static TLS region.  Delay it.
137          */
138         if (((struct elf_resolve *) map)->init_flag & RELOCS_DONE)
139     {
140 #ifdef SHARED
141                 /*
142                  * Update the slot information data for at least the generation of
143                  * the DSO we are allocating data for.
144                  */
145                 if (__builtin_expect (THREAD_DTV()[0].counter != _dl_tls_generation, 0))
146                         (void) _dl_update_slotinfo (map->l_tls_modid);
147 #endif
148                 _dl_init_static_tls (map);
149         }
150         else
151                 map->l_need_tls_init = 1;
152 }
153
154 size_t
155 internal_function
156 _dl_next_tls_modid (void)
157 {
158   size_t result;
159
160   if (__builtin_expect (GL(dl_tls_dtv_gaps), false))
161     {
162       size_t disp = 0;
163       struct dtv_slotinfo_list *runp = GL(dl_tls_dtv_slotinfo_list);
164
165       /* Note that this branch will never be executed during program
166          start since there are no gaps at that time.  Therefore it
167          does not matter that the dl_tls_dtv_slotinfo is not allocated
168          yet when the function is called for the first times.
169
170          NB: the offset +1 is due to the fact that DTV[0] is used
171          for something else.  */
172       result = GL(dl_tls_static_nelem) + 1;
173       if (result <= GL(dl_tls_max_dtv_idx))
174         do
175           {
176             while (result - disp < runp->len)
177               {
178                 if (runp->slotinfo[result - disp].map == NULL)
179                   break;
180
181                 ++result;
182                 assert (result <= GL(dl_tls_max_dtv_idx) + 1);
183               }
184
185             if (result - disp < runp->len)
186               break;
187
188             disp += runp->len;
189           }
190         while ((runp = runp->next) != NULL);
191
192       if (result > GL(dl_tls_max_dtv_idx))
193         {
194           /* The new index must indeed be exactly one higher than the
195              previous high.  */
196           assert (result == GL(dl_tls_max_dtv_idx) + 1);
197           /* There is no gap anymore.  */
198           GL(dl_tls_dtv_gaps) = false;
199
200           goto nogaps;
201         }
202     }
203   else
204     {
205       /* No gaps, allocate a new entry.  */
206     nogaps:
207
208       result = ++GL(dl_tls_max_dtv_idx);
209     }
210
211   return result;
212 }
213
214
215 # ifdef SHARED
216 void
217 internal_function
218 _dl_determine_tlsoffset (void)
219 {
220   size_t max_align = TLS_TCB_ALIGN;
221   size_t freetop = 0;
222   size_t freebottom = 0;
223
224   /* The first element of the dtv slot info list is allocated.  */
225   assert (GL(dl_tls_dtv_slotinfo_list) != NULL);
226   /* There is at this point only one element in the
227      dl_tls_dtv_slotinfo_list list.  */
228   assert (GL(dl_tls_dtv_slotinfo_list)->next == NULL);
229
230   struct dtv_slotinfo *slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo;
231
232   /* Determining the offset of the various parts of the static TLS
233      block has several dependencies.  In addition we have to work
234      around bugs in some toolchains.
235
236      Each TLS block from the objects available at link time has a size
237      and an alignment requirement.  The GNU ld computes the alignment
238      requirements for the data at the positions *in the file*, though.
239      I.e, it is not simply possible to allocate a block with the size
240      of the TLS program header entry.  The data is layed out assuming
241      that the first byte of the TLS block fulfills
242
243        p_vaddr mod p_align == &TLS_BLOCK mod p_align
244
245      This means we have to add artificial padding at the beginning of
246      the TLS block.  These bytes are never used for the TLS data in
247      this module but the first byte allocated must be aligned
248      according to mod p_align == 0 so that the first byte of the TLS
249      block is aligned according to p_vaddr mod p_align.  This is ugly
250      and the linker can help by computing the offsets in the TLS block
251      assuming the first byte of the TLS block is aligned according to
252      p_align.
253
254      The extra space which might be allocated before the first byte of
255      the TLS block need not go unused.  The code below tries to use
256      that memory for the next TLS block.  This can work if the total
257      memory requirement for the next TLS block is smaller than the
258      gap.  */
259
260 # if defined(TLS_TCB_AT_TP)
261   /* We simply start with zero.  */
262   size_t offset = 0;
263
264   size_t cnt;
265   for (cnt = 0; slotinfo[cnt].map != NULL; ++cnt)
266     {
267       assert (cnt < GL(dl_tls_dtv_slotinfo_list)->len);
268
269       size_t firstbyte = (-slotinfo[cnt].map->l_tls_firstbyte_offset
270                           & (slotinfo[cnt].map->l_tls_align - 1));
271       size_t off;
272       max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
273
274       if (freebottom - freetop >= slotinfo[cnt].map->l_tls_blocksize)
275         {
276           off = roundup (freetop + slotinfo[cnt].map->l_tls_blocksize
277                          - firstbyte, slotinfo[cnt].map->l_tls_align)
278                 + firstbyte;
279           if (off <= freebottom)
280             {
281               freetop = off;
282
283               /* XXX For some architectures we perhaps should store the
284                  negative offset.  */
285               slotinfo[cnt].map->l_tls_offset = off;
286               continue;
287             }
288         }
289
290       off = roundup (offset + slotinfo[cnt].map->l_tls_blocksize - firstbyte,
291                      slotinfo[cnt].map->l_tls_align) + firstbyte;
292       if (off > offset + slotinfo[cnt].map->l_tls_blocksize
293                 + (freebottom - freetop))
294         {
295           freetop = offset;
296           freebottom = off - slotinfo[cnt].map->l_tls_blocksize;
297         }
298       offset = off;
299
300       /* XXX For some architectures we perhaps should store the
301          negative offset.  */
302       slotinfo[cnt].map->l_tls_offset = off;
303     }
304
305   GL(dl_tls_static_used) = offset;
306   GL(dl_tls_static_size) = (roundup (offset + TLS_STATIC_SURPLUS, max_align)
307                             + TLS_TCB_SIZE);
308 # elif defined(TLS_DTV_AT_TP)
309   /* The TLS blocks start right after the TCB.  */
310   size_t offset = TLS_TCB_SIZE;
311   size_t cnt;
312
313   for (cnt = 0; slotinfo[cnt].map != NULL; ++cnt)
314     {
315       assert (cnt < GL(dl_tls_dtv_slotinfo_list)->len);
316
317       size_t firstbyte = (-slotinfo[cnt].map->l_tls_firstbyte_offset
318                           & (slotinfo[cnt].map->l_tls_align - 1));
319       size_t off;
320       max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
321
322       if (slotinfo[cnt].map->l_tls_blocksize <= freetop - freebottom)
323         {
324           off = roundup (freebottom, slotinfo[cnt].map->l_tls_align);
325           if (off - freebottom < firstbyte)
326             off += slotinfo[cnt].map->l_tls_align;
327           if (off + slotinfo[cnt].map->l_tls_blocksize - firstbyte <= freetop)
328             {
329               slotinfo[cnt].map->l_tls_offset = off - firstbyte;
330               freebottom = (off + slotinfo[cnt].map->l_tls_blocksize
331                             - firstbyte);
332               continue;
333             }
334         }
335
336       off = roundup (offset, slotinfo[cnt].map->l_tls_align);
337       if (off - offset < firstbyte)
338         off += slotinfo[cnt].map->l_tls_align;
339
340       slotinfo[cnt].map->l_tls_offset = off - firstbyte;
341       if (off - firstbyte - offset > freetop - freebottom)
342         {
343           freebottom = offset;
344           freetop = off - firstbyte;
345         }
346
347       offset = off + slotinfo[cnt].map->l_tls_blocksize - firstbyte;
348     }
349
350   GL(dl_tls_static_used) = offset;
351   GL(dl_tls_static_size) = roundup (offset + TLS_STATIC_SURPLUS,
352                                     TLS_TCB_ALIGN);
353 # else
354 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
355 # endif
356
357   /* The alignment requirement for the static TLS block.  */
358   GL(dl_tls_static_align) = max_align;
359 }
360
361
362 /* This is called only when the data structure setup was skipped at startup,
363    when there was no need for it then.  Now we have dynamically loaded
364    something needing TLS, or libpthread needs it.  */
365 int
366 internal_function
367 _dl_tls_setup (void)
368 {
369   assert (GL(dl_tls_dtv_slotinfo_list) == NULL);
370   assert (GL(dl_tls_max_dtv_idx) == 0);
371
372   const size_t nelem = 2 + TLS_SLOTINFO_SURPLUS;
373
374   GL(dl_tls_dtv_slotinfo_list)
375     = calloc (1, (sizeof (struct dtv_slotinfo_list)
376                   + nelem * sizeof (struct dtv_slotinfo)));
377   if (GL(dl_tls_dtv_slotinfo_list) == NULL)
378     return -1;
379
380   GL(dl_tls_dtv_slotinfo_list)->len = nelem;
381
382   /* Number of elements in the static TLS block.  It can't be zero
383      because of various assumptions.  The one element is null.  */
384   GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx) = 1;
385
386   /* This initializes more variables for us.  */
387   _dl_determine_tlsoffset ();
388
389   return 0;
390 }
391 # endif
392
393 static void *
394 internal_function
395 allocate_dtv (void *result)
396 {
397   dtv_t *dtv;
398   size_t dtv_length;
399
400   /* We allocate a few more elements in the dtv than are needed for the
401      initial set of modules.  This should avoid in most cases expansions
402      of the dtv.  */
403   dtv_length = GL(dl_tls_max_dtv_idx) + DTV_SURPLUS;
404   dtv = calloc (dtv_length + 2, sizeof (dtv_t));
405   if (dtv != NULL)
406     {
407       /* This is the initial length of the dtv.  */
408       dtv[0].counter = dtv_length;
409
410       /* The rest of the dtv (including the generation counter) is
411          Initialize with zero to indicate nothing there.  */
412
413       /* Add the dtv to the thread data structures.  */
414       INSTALL_DTV (result, dtv);
415     }
416   else
417     result = NULL;
418
419   return result;
420 }
421
422
423 /* Get size and alignment requirements of the static TLS block.  */
424 void
425 internal_function
426 _dl_get_tls_static_info (size_t *sizep, size_t *alignp)
427 {
428   *sizep = GL(dl_tls_static_size);
429   *alignp = GL(dl_tls_static_align);
430 }
431
432
433 void *
434 internal_function
435 _dl_allocate_tls_storage (void)
436 {
437   void *result;
438   size_t size = GL(dl_tls_static_size);
439
440 # if defined(TLS_DTV_AT_TP)
441   /* Memory layout is:
442      [ TLS_PRE_TCB_SIZE ] [ TLS_TCB_SIZE ] [ TLS blocks ]
443                           ^ This should be returned.  */
444   size += (TLS_PRE_TCB_SIZE + GL(dl_tls_static_align) - 1)
445           & ~(GL(dl_tls_static_align) - 1);
446 # endif
447
448   /* Allocate a correctly aligned chunk of memory.  */
449   result = _dl_memalign (GL(dl_tls_static_align), size);
450   if (__builtin_expect (result != NULL, 1))
451     {
452       /* Allocate the DTV.  */
453       void *allocated = result;
454
455 # if defined(TLS_TCB_AT_TP)
456       /* The TCB follows the TLS blocks.  */
457       result = (char *) result + size - TLS_TCB_SIZE;
458
459       /* Clear the TCB data structure.  We can't ask the caller (i.e.
460          libpthread) to do it, because we will initialize the DTV et al.  */
461       _dl_memset (result, '\0', TLS_TCB_SIZE);
462 # elif defined(TLS_DTV_AT_TP)
463       result = (char *) result + size - GL(dl_tls_static_size);
464
465       /* Clear the TCB data structure and TLS_PRE_TCB_SIZE bytes before it.
466          We can't ask the caller (i.e. libpthread) to do it, because we will
467          initialize the DTV et al.  */
468       _dl_memset ((char *) result - TLS_PRE_TCB_SIZE, '\0',
469               TLS_PRE_TCB_SIZE + TLS_TCB_SIZE);
470 # endif
471
472       result = allocate_dtv (result);
473       if (result == NULL)
474         free (allocated);
475     }
476
477   return result;
478 }
479
480
481 void *
482 internal_function
483 _dl_allocate_tls_init (void *result)
484 {
485   if (result == NULL)
486     /* The memory allocation failed.  */
487     return NULL;
488
489   dtv_t *dtv = GET_DTV (result);
490   struct dtv_slotinfo_list *listp;
491   size_t total = 0;
492   size_t maxgen = 0;
493
494   /* We have to prepare the dtv for all currently loaded modules using
495      TLS.  For those which are dynamically loaded we add the values
496      indicating deferred allocation.  */
497   listp = GL(dl_tls_dtv_slotinfo_list);
498   while (1)
499     {
500       size_t cnt;
501
502       for (cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
503         {
504           struct link_map *map;
505           void *dest;
506
507           /* Check for the total number of used slots.  */
508           if (total + cnt > GL(dl_tls_max_dtv_idx))
509             break;
510
511           map = listp->slotinfo[cnt].map;
512           if (map == NULL)
513             /* Unused entry.  */
514             continue;
515
516           /* Keep track of the maximum generation number.  This might
517              not be the generation counter.  */
518           maxgen = MAX (maxgen, listp->slotinfo[cnt].gen);
519
520           if (map->l_tls_offset == NO_TLS_OFFSET)
521             {
522               /* For dynamically loaded modules we simply store
523                  the value indicating deferred allocation.  */
524               dtv[map->l_tls_modid].pointer.val = TLS_DTV_UNALLOCATED;
525               dtv[map->l_tls_modid].pointer.is_static = false;
526               continue;
527             }
528
529           assert (map->l_tls_modid == cnt);
530           assert (map->l_tls_blocksize >= map->l_tls_initimage_size);
531 # if defined(TLS_TCB_AT_TP)
532           assert ((size_t) map->l_tls_offset >= map->l_tls_blocksize);
533           dest = (char *) result - map->l_tls_offset;
534 # elif defined(TLS_DTV_AT_TP)
535           dest = (char *) result + map->l_tls_offset;
536 # else
537 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
538 # endif
539
540           /* Copy the initialization image and clear the BSS part.  */
541           dtv[map->l_tls_modid].pointer.val = dest;
542           dtv[map->l_tls_modid].pointer.is_static = true;
543           _dl_memset (_dl_mempcpy (dest, map->l_tls_initimage,
544                              map->l_tls_initimage_size), '\0',
545                   map->l_tls_blocksize - map->l_tls_initimage_size);
546         }
547
548       total += cnt;
549       if (total >= GL(dl_tls_max_dtv_idx))
550         break;
551
552       listp = listp->next;
553       assert (listp != NULL);
554     }
555
556   /* The DTV version is up-to-date now.  */
557   dtv[0].counter = maxgen;
558
559   return result;
560 }
561
562 void *
563 internal_function
564 _dl_allocate_tls (void *mem)
565 {
566   return _dl_allocate_tls_init (mem == NULL
567                                 ? _dl_allocate_tls_storage ()
568                                 : allocate_dtv (mem));
569 }
570
571
572 void
573 internal_function
574 _dl_deallocate_tls (void *tcb, bool dealloc_tcb)
575 {
576   dtv_t *dtv = GET_DTV (tcb);
577   size_t cnt;
578
579   /* We need to free the memory allocated for non-static TLS.  */
580   for (cnt = 0; cnt < dtv[-1].counter; ++cnt)
581     if (! dtv[1 + cnt].pointer.is_static
582         && dtv[1 + cnt].pointer.val != TLS_DTV_UNALLOCATED)
583       free (dtv[1 + cnt].pointer.val);
584
585   /* The array starts with dtv[-1].  */
586 #ifdef SHARED
587   if (dtv != GL(dl_initial_dtv))
588 #endif
589     free (dtv - 1);
590
591   if (dealloc_tcb)
592     {
593 # if defined(TLS_TCB_AT_TP)
594       /* The TCB follows the TLS blocks.  Back up to free the whole block.  */
595       tcb -= GL(dl_tls_static_size) - TLS_TCB_SIZE;
596 # elif defined(TLS_DTV_AT_TP)
597       /* Back up the TLS_PRE_TCB_SIZE bytes.  */
598       tcb -= (TLS_PRE_TCB_SIZE + GL(dl_tls_static_align) - 1)
599              & ~(GL(dl_tls_static_align) - 1);
600 # endif
601       free (tcb);
602     }
603 }
604
605
606 # ifdef SHARED
607 /* The __tls_get_addr function has two basic forms which differ in the
608    arguments.  The IA-64 form takes two parameters, the module ID and
609    offset.  The form used, among others, on IA-32 takes a reference to
610    a special structure which contain the same information.  The second
611    form seems to be more often used (in the moment) so we default to
612    it.  Users of the IA-64 form have to provide adequate definitions
613    of the following macros.  */
614 #  ifndef GET_ADDR_ARGS
615 #   define GET_ADDR_ARGS tls_index *ti
616 #  endif
617 #  ifndef GET_ADDR_MODULE
618 #   define GET_ADDR_MODULE ti->ti_module
619 #  endif
620 #  ifndef GET_ADDR_OFFSET
621 #   define GET_ADDR_OFFSET ti->ti_offset
622 #  endif
623
624
625 static void *
626 allocate_and_init (struct link_map *map)
627 {
628   void *newp;
629
630   newp = _dl_memalign (map->l_tls_align, map->l_tls_blocksize);
631   if (newp == NULL)
632     oom ();
633
634   /* Initialize the memory.  */
635   _dl_memset (_dl_mempcpy (newp, map->l_tls_initimage, map->l_tls_initimage_size),
636           '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
637
638   return newp;
639 }
640
641
642 struct link_map *
643 _dl_update_slotinfo (unsigned long int req_modid)
644 {
645   struct link_map *the_map = NULL;
646   dtv_t *dtv = THREAD_DTV ();
647
648   /* The global dl_tls_dtv_slotinfo array contains for each module
649      index the generation counter current when the entry was created.
650      This array never shrinks so that all module indices which were
651      valid at some time can be used to access it.  Before the first
652      use of a new module index in this function the array was extended
653      appropriately.  Access also does not have to be guarded against
654      modifications of the array.  It is assumed that pointer-size
655      values can be read atomically even in SMP environments.  It is
656      possible that other threads at the same time dynamically load
657      code and therefore add to the slotinfo list.  This is a problem
658      since we must not pick up any information about incomplete work.
659      The solution to this is to ignore all dtv slots which were
660      created after the one we are currently interested.  We know that
661      dynamic loading for this module is completed and this is the last
662      load operation we know finished.  */
663   unsigned long int idx = req_modid;
664   struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list);
665
666   while (idx >= listp->len)
667     {
668       idx -= listp->len;
669       listp = listp->next;
670     }
671
672   if (dtv[0].counter < listp->slotinfo[idx].gen)
673     {
674       /* The generation counter for the slot is higher than what the
675          current dtv implements.  We have to update the whole dtv but
676          only those entries with a generation counter <= the one for
677          the entry we need.  */
678       size_t new_gen = listp->slotinfo[idx].gen;
679       size_t total = 0;
680
681       /* We have to look through the entire dtv slotinfo list.  */
682       listp =  GL(dl_tls_dtv_slotinfo_list);
683       do
684         {
685           size_t cnt;
686
687           for (cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
688             {
689               size_t gen = listp->slotinfo[cnt].gen;
690
691               if (gen > new_gen)
692                 /* This is a slot for a generation younger than the
693                    one we are handling now.  It might be incompletely
694                    set up so ignore it.  */
695                 continue;
696
697               /* If the entry is older than the current dtv layout we
698                  know we don't have to handle it.  */
699               if (gen <= dtv[0].counter)
700                 continue;
701
702               /* If there is no map this means the entry is empty.  */
703               struct link_map *map = listp->slotinfo[cnt].map;
704               if (map == NULL)
705                 {
706                   /* If this modid was used at some point the memory
707                      might still be allocated.  */
708                   if (! dtv[total + cnt].pointer.is_static
709                       && dtv[total + cnt].pointer.val != TLS_DTV_UNALLOCATED)
710                     {
711                       free (dtv[total + cnt].pointer.val);
712                       dtv[total + cnt].pointer.val = TLS_DTV_UNALLOCATED;
713                     }
714
715                   continue;
716                 }
717
718               /* Check whether the current dtv array is large enough.  */
719               size_t modid = map->l_tls_modid;
720               assert (total + cnt == modid);
721               if (dtv[-1].counter < modid)
722                 {
723                   /* Reallocate the dtv.  */
724                   dtv_t *newp;
725                   size_t newsize = GL(dl_tls_max_dtv_idx) + DTV_SURPLUS;
726                   size_t oldsize = dtv[-1].counter;
727
728                   assert (map->l_tls_modid <= newsize);
729
730                   if (dtv == GL(dl_initial_dtv))
731                     {
732                       /* This is the initial dtv that was allocated
733                          during rtld startup using the dl-minimal.c
734                          malloc instead of the real malloc.  We can't
735                          free it, we have to abandon the old storage.  */
736
737                       newp = malloc ((2 + newsize) * sizeof (dtv_t));
738                       if (newp == NULL)
739                         oom ();
740                       _dl_memcpy (newp, &dtv[-1], oldsize * sizeof (dtv_t));
741                     }
742                   else
743                     {
744                       newp = realloc (&dtv[-1],
745                                       (2 + newsize) * sizeof (dtv_t));
746                       if (newp == NULL)
747                         oom ();
748                     }
749
750                   newp[0].counter = newsize;
751
752                   /* Clear the newly allocated part.  */
753                   _dl_memset (newp + 2 + oldsize, '\0',
754                           (newsize - oldsize) * sizeof (dtv_t));
755
756                   /* Point dtv to the generation counter.  */
757                   dtv = &newp[1];
758
759                   /* Install this new dtv in the thread data
760                      structures.  */
761                   INSTALL_NEW_DTV (dtv);
762                 }
763
764               /* If there is currently memory allocate for this
765                  dtv entry free it.  */
766               /* XXX Ideally we will at some point create a memory
767                  pool.  */
768               if (! dtv[modid].pointer.is_static
769                   && dtv[modid].pointer.val != TLS_DTV_UNALLOCATED)
770                 /* Note that free is called for NULL is well.  We
771                    deallocate even if it is this dtv entry we are
772                    supposed to load.  The reason is that we call
773                    memalign and not malloc.  */
774                 free (dtv[modid].pointer.val);
775
776               /* This module is loaded dynamically- We defer memory
777                  allocation.  */
778               dtv[modid].pointer.is_static = false;
779               dtv[modid].pointer.val = TLS_DTV_UNALLOCATED;
780
781               if (modid == req_modid)
782                 the_map = map;
783             }
784
785           total += listp->len;
786         }
787       while ((listp = listp->next) != NULL);
788
789       /* This will be the new maximum generation counter.  */
790       dtv[0].counter = new_gen;
791     }
792
793   return the_map;
794 }
795
796
797 /* The generic dynamic and local dynamic model cannot be used in
798    statically linked applications.  */
799 void *
800 __tls_get_addr (GET_ADDR_ARGS)
801 {
802   dtv_t *dtv = THREAD_DTV ();
803   struct link_map *the_map = NULL;
804   void *p;
805
806   if (__builtin_expect (dtv[0].counter != GL(dl_tls_generation), 0))
807     the_map = _dl_update_slotinfo (GET_ADDR_MODULE);
808
809   p = dtv[GET_ADDR_MODULE].pointer.val;
810
811   if (__builtin_expect (p == TLS_DTV_UNALLOCATED, 0))
812     {
813       /* The allocation was deferred.  Do it now.  */
814       if (the_map == NULL)
815         {
816           /* Find the link map for this module.  */
817           size_t idx = GET_ADDR_MODULE;
818           struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list);
819
820           while (idx >= listp->len)
821             {
822               idx -= listp->len;
823               listp = listp->next;
824             }
825
826           the_map = listp->slotinfo[idx].map;
827         }
828
829       p = dtv[GET_ADDR_MODULE].pointer.val = allocate_and_init (the_map);
830       dtv[GET_ADDR_MODULE].pointer.is_static = false;
831     }
832
833   return (char *) p + GET_ADDR_OFFSET;
834 }
835 # endif
836
837
838
839 void _dl_add_to_slotinfo (struct link_map  *l);
840 void
841 _dl_add_to_slotinfo (struct link_map  *l)
842 {
843   /* Now that we know the object is loaded successfully add
844      modules containing TLS data to the dtv info table.  We
845      might have to increase its size.  */
846   struct dtv_slotinfo_list *listp;
847   struct dtv_slotinfo_list *prevp;
848   size_t idx = l->l_tls_modid;
849
850   /* Find the place in the dtv slotinfo list.  */
851   listp = GL(dl_tls_dtv_slotinfo_list);
852   prevp = NULL;         /* Needed to shut up gcc.  */
853   do
854     {
855       /* Does it fit in the array of this list element?  */
856       if (idx < listp->len)
857         break;
858       idx -= listp->len;
859       prevp = listp;
860       listp = listp->next;
861     }
862   while (listp != NULL);
863
864   if (listp == NULL)
865     {
866       /* When we come here it means we have to add a new element
867          to the slotinfo list.  And the new module must be in
868          the first slot.  */
869       assert (idx == 0);
870
871       listp = prevp->next = (struct dtv_slotinfo_list *)
872         malloc (sizeof (struct dtv_slotinfo_list)
873                 + TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
874       if (listp == NULL)
875         {
876           /* We ran out of memory.  We will simply fail this
877              call but don't undo anything we did so far.  The
878              application will crash or be terminated anyway very
879              soon.  */
880
881           /* We have to do this since some entries in the dtv
882              slotinfo array might already point to this
883              generation.  */
884           ++GL(dl_tls_generation);
885
886           _dl_dprintf (_dl_debug_file,
887                         "cannot create TLS data structures: ABORT\n");
888           _dl_exit (127);
889         }
890
891       listp->len = TLS_SLOTINFO_SURPLUS;
892       listp->next = NULL;
893       _dl_memset (listp->slotinfo, '\0',
894               TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
895     }
896
897   /* Add the information into the slotinfo data structure.  */
898   listp->slotinfo[idx].map = l;
899   listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
900 }