1 /* Thread-local storage handling in the ELF dynamic linker. Generic version.
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include <sys/param.h>
28 /* We don't need any of this if TLS is not supported. */
32 # include <ldsodefs.h>
34 /* Value used for dtv entries for which the allocation is delayed. */
35 # define TLS_DTV_UNALLOCATED ((void *) -1l)
38 /* Out-of-memory handler. */
41 __attribute__ ((__noreturn__
))
44 _dl_fatal_printf ("cannot allocate memory for thread-local data: ABORT\n");
52 _dl_next_tls_modid (void)
56 if (__builtin_expect (GL(dl_tls_dtv_gaps
), false))
59 struct dtv_slotinfo_list
*runp
= GL(dl_tls_dtv_slotinfo_list
);
61 /* Note that this branch will never be executed during program
62 start since there are no gaps at that time. Therefore it
63 does not matter that the dl_tls_dtv_slotinfo is not allocated
64 yet when the function is called for the first times. */
65 result
= GL(dl_tls_static_nelem
) + 1;
66 /* If the following would not be true we mustn't have assumed
68 assert (result
<= GL(dl_tls_max_dtv_idx
));
71 while (result
- disp
< runp
->len
)
73 if (runp
->slotinfo
[result
- disp
].map
== NULL
)
77 assert (result
<= GL(dl_tls_max_dtv_idx
) + 1);
80 if (result
- disp
< runp
->len
)
85 while ((runp
= runp
->next
) != NULL
);
87 if (result
>= GL(dl_tls_max_dtv_idx
))
89 /* The new index must indeed be exactly one higher than the
91 assert (result
== GL(dl_tls_max_dtv_idx
));
93 /* There is no gap anymore. */
94 GL(dl_tls_dtv_gaps
) = false;
101 /* No gaps, allocate a new entry. */
103 result
= ++GL(dl_tls_max_dtv_idx
);
112 _dl_determine_tlsoffset (void)
114 struct dtv_slotinfo
*slotinfo
;
115 size_t max_align
= __alignof__ (void *);
119 /* The first element of the dtv slot info list is allocated. */
120 assert (GL(dl_tls_dtv_slotinfo_list
) != NULL
);
121 /* There is at this point only one element in the
122 dl_tls_dtv_slotinfo_list list. */
123 assert (GL(dl_tls_dtv_slotinfo_list
)->next
== NULL
);
126 /* We simply start with zero. */
129 slotinfo
= GL(dl_tls_dtv_slotinfo_list
)->slotinfo
;
130 for (cnt
= 1; slotinfo
[cnt
].map
!= NULL
; ++cnt
)
132 assert (cnt
< GL(dl_tls_dtv_slotinfo_list
)->len
);
134 max_align
= MAX (max_align
, slotinfo
[cnt
].map
->l_tls_align
);
136 /* Compute the offset of the next TLS block. */
137 offset
= roundup (offset
+ slotinfo
[cnt
].map
->l_tls_blocksize
,
138 slotinfo
[cnt
].map
->l_tls_align
);
140 /* XXX For some architectures we perhaps should store the
142 slotinfo
[cnt
].map
->l_tls_offset
= offset
;
145 /* The thread descriptor (pointed to by the thread pointer) has its
146 own alignment requirement. Adjust the static TLS size
147 and TLS offsets appropriately. */
148 // XXX How to deal with this. We cannot simply add zero bytes
149 // XXX after the first (closest to the TCB) TLS block since this
150 // XXX would invalidate the offsets the linker creates for the LE
153 GL(dl_tls_static_size
) = offset
+ TLS_TCB_SIZE
;
155 /* The TLS blocks start right after the TCB. */
156 offset
= TLS_TCB_SIZE
;
158 /* The first block starts right after the TCB. */
159 slotinfo
= GL(dl_tls_dtv_slotinfo_list
)->slotinfo
;
160 if (slotinfo
[1].map
!= NULL
)
164 offset
= roundup (offset
, slotinfo
[1].map
->l_tls_align
);
165 slotinfo
[1].map
->l_tls_offset
= offset
;
166 max_align
= slotinfo
[1].map
->l_tls_align
;
167 prev_size
= slotinfo
[1].map
->l_tls_blocksize
;
169 for (cnt
= 2; slotinfo
[cnt
].map
!= NULL
; ++cnt
)
171 assert (cnt
< GL(dl_tls_dtv_slotinfo_list
)->len
);
173 max_align
= MAX (max_align
, slotinfo
[cnt
].map
->l_tls_align
);
175 /* Compute the offset of the next TLS block. */
176 offset
= roundup (offset
+ prev_size
,
177 slotinfo
[cnt
].map
->l_tls_align
);
179 /* XXX For some architectures we perhaps should store the
181 slotinfo
[cnt
].map
->l_tls_offset
= offset
;
183 prev_size
= slotinfo
[cnt
].map
->l_tls_blocksize
;
189 GL(dl_tls_static_size
) = offset
;
191 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
194 /* The alignment requirement for the static TLS block. */
195 GL(dl_tls_static_align
) = MAX (TLS_TCB_ALIGN
, max_align
);
201 allocate_dtv (void *result
)
206 /* We allocate a few more elements in the dtv than are needed for the
207 initial set of modules. This should avoid in most cases expansions
209 dtv_length
= GL(dl_tls_max_dtv_idx
) + DTV_SURPLUS
;
210 dtv
= (dtv_t
*) malloc ((dtv_length
+ 2) * sizeof (dtv_t
));
213 /* This is the initial length of the dtv. */
214 dtv
[0].counter
= dtv_length
;
215 /* Initialize all of the rest of the dtv (including the
216 generation counter) with zero to indicate nothing there. */
217 memset (dtv
+ 1, '\0', (dtv_length
+ 1) * sizeof (dtv_t
));
219 /* Add the dtv to the thread data structures. */
220 INSTALL_DTV (result
, dtv
);
229 /* Get size and alignment requirements of the static TLS block. */
232 _dl_get_tls_static_info (size_t *sizep
, size_t *alignp
)
234 *sizep
= GL(dl_tls_static_size
);
235 *alignp
= GL(dl_tls_static_align
);
241 _dl_allocate_tls_storage (void)
245 /* Allocate a correctly aligned chunk of memory. */
246 result
= __libc_memalign (GL(dl_tls_static_align
), GL(dl_tls_static_size
));
247 if (__builtin_expect (result
!= NULL
, 0))
249 /* Allocate the DTV. */
250 void *allocated
= result
;
253 /* The TCB follows the TLS blocks. */
254 result
= (char *) result
+ GL(dl_tls_static_size
) - TLS_TCB_SIZE
;
257 result
= allocate_dtv (result
);
268 _dl_allocate_tls_init (void *result
)
270 dtv_t
*dtv
= GET_DTV (result
);
271 struct dtv_slotinfo_list
*listp
;
275 /* The memory allocation failed. */
278 /* We have to look prepare the dtv for all currently loaded
279 modules using TLS. For those which are dynamically loaded we
280 add the values indicating deferred allocation. */
281 listp
= GL(dl_tls_dtv_slotinfo_list
);
286 for (cnt
= total
== 0 ? 1 : 0; cnt
< listp
->len
; ++cnt
)
288 struct link_map
*map
;
291 /* Check for the total number of used slots. */
292 if (total
+ cnt
> GL(dl_tls_max_dtv_idx
))
295 map
= listp
->slotinfo
[cnt
].map
;
300 if (map
->l_type
== lt_loaded
)
302 /* For dynamically loaded modules we simply store
303 the value indicating deferred allocation. */
304 dtv
[map
->l_tls_modid
].pointer
= TLS_DTV_UNALLOCATED
;
308 assert (map
->l_tls_modid
== cnt
);
309 assert (map
->l_tls_blocksize
>= map
->l_tls_initimage_size
);
311 assert (map
->l_tls_offset
>= map
->l_tls_blocksize
);
312 dest
= (char *) result
- map
->l_tls_offset
;
314 dest
= (char *) result
+ map
->l_tls_offset
;
316 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
319 /* Copy the initialization image and clear the BSS part. */
320 dtv
[map
->l_tls_modid
].pointer
= dest
;
321 memset (__mempcpy (dest
, map
->l_tls_initimage
,
322 map
->l_tls_initimage_size
), '\0',
323 map
->l_tls_blocksize
- map
->l_tls_initimage_size
);
327 if (total
>= GL(dl_tls_max_dtv_idx
))
331 assert (listp
!= NULL
);
339 _dl_allocate_tls (void *mem
)
341 return _dl_allocate_tls_init (mem
== NULL
342 ? _dl_allocate_tls_storage ()
343 : allocate_dtv (mem
));
345 INTDEF(_dl_allocate_tls
)
350 _dl_deallocate_tls (void *tcb
, bool dealloc_tcb
)
352 dtv_t
*dtv
= GET_DTV (tcb
);
354 /* The array starts with dtv[-1]. */
364 /* The __tls_get_addr function has two basic forms which differ in the
365 arguments. The IA-64 form takes two parameters, the module ID and
366 offset. The form used, among others, on IA-32 takes a reference to
367 a special structure which contain the same information. The second
368 form seems to be more often used (in the moment) so we default to
369 it. Users of the IA-64 form have to provide adequate definitions
370 of the following macros. */
371 # ifndef GET_ADDR_ARGS
372 # define GET_ADDR_ARGS tls_index *ti
374 # ifndef GET_ADDR_MODULE
375 # define GET_ADDR_MODULE ti->ti_module
377 # ifndef GET_ADDR_OFFSET
378 # define GET_ADDR_OFFSET ti->ti_offset
380 /* Systems which do not have tls_index also probably have to define
381 DONT_USE_TLS_INDEX. */
383 # ifndef __TLS_GET_ADDR
384 # define __TLS_GET_ADDR __tls_get_addr
388 /* Return the symbol address given the map of the module it is in and
389 the symbol record. This is used in dl-sym.c. */
392 _dl_tls_symaddr (struct link_map
*map
, const ElfW(Sym
) *ref
)
394 # ifndef DONT_USE_TLS_INDEX
397 .ti_module
= map
->l_tls_modid
,
398 .ti_offset
= ref
->st_value
401 return __TLS_GET_ADDR (&tmp
);
403 return __TLS_GET_ADDR (map
->l_tls_modid
, ref
->st_value
);
409 allocate_and_init (struct link_map
*map
)
413 newp
= __libc_memalign (map
->l_tls_align
, map
->l_tls_blocksize
);
417 /* Initialize the memory. */
418 memset (__mempcpy (newp
, map
->l_tls_initimage
, map
->l_tls_initimage_size
),
419 '\0', map
->l_tls_blocksize
- map
->l_tls_initimage_size
);
425 /* The generic dynamic and local dynamic model cannot be used in
426 statically linked applications. */
428 __tls_get_addr (GET_ADDR_ARGS
)
430 dtv_t
*dtv
= THREAD_DTV ();
431 struct link_map
*the_map
= NULL
;
434 if (__builtin_expect (dtv
[0].counter
!= GL(dl_tls_generation
), 0))
436 struct dtv_slotinfo_list
*listp
;
439 /* The global dl_tls_dtv_slotinfo array contains for each module
440 index the generation counter current when the entry was
441 created. This array never shrinks so that all module indices
442 which were valid at some time can be used to access it.
443 Before the first use of a new module index in this function
444 the array was extended appropriately. Access also does not
445 have to be guarded against modifications of the array. It is
446 assumed that pointer-size values can be read atomically even
447 in SMP environments. It is possible that other threads at
448 the same time dynamically load code and therefore add to the
449 slotinfo list. This is a problem since we must not pick up
450 any information about incomplete work. The solution to this
451 is to ignore all dtv slots which were created after the one
452 we are currently interested. We know that dynamic loading
453 for this module is completed and this is the last load
454 operation we know finished. */
455 idx
= GET_ADDR_MODULE
;
456 listp
= GL(dl_tls_dtv_slotinfo_list
);
457 while (idx
>= listp
->len
)
463 if (dtv
[0].counter
< listp
->slotinfo
[idx
].gen
)
465 /* The generation counter for the slot is higher than what
466 the current dtv implements. We have to update the whole
467 dtv but only those entries with a generation counter <=
468 the one for the entry we need. */
469 size_t new_gen
= listp
->slotinfo
[idx
].gen
;
472 /* We have to look through the entire dtv slotinfo list. */
473 listp
= GL(dl_tls_dtv_slotinfo_list
);
478 for (cnt
= total
= 0 ? 1 : 0; cnt
< listp
->len
; ++cnt
)
480 size_t gen
= listp
->slotinfo
[cnt
].gen
;
481 struct link_map
*map
;
485 /* This is a slot for a generation younger than
486 the one we are handling now. It might be
487 incompletely set up so ignore it. */
490 /* If the entry is older than the current dtv layout
491 we know we don't have to handle it. */
492 if (gen
<= dtv
[0].counter
)
495 /* If there is no map this means the entry is empty. */
496 map
= listp
->slotinfo
[cnt
].map
;
499 /* If this modid was used at some point the memory
500 might still be allocated. */
501 if (dtv
[total
+ cnt
].pointer
!= TLS_DTV_UNALLOCATED
)
502 free (dtv
[total
+ cnt
].pointer
);
507 /* Check whether the current dtv array is large enough. */
508 modid
= map
->l_tls_modid
;
509 assert (total
+ cnt
== modid
);
510 if (dtv
[-1].counter
< modid
)
512 /* Reallocate the dtv. */
514 size_t newsize
= GL(dl_tls_max_dtv_idx
) + DTV_SURPLUS
;
515 size_t oldsize
= dtv
[-1].counter
;
517 assert (map
->l_tls_modid
<= newsize
);
519 if (dtv
== GL(dl_initial_dtv
))
521 /* This is the initial dtv that was allocated
522 during rtld startup using the dl-minimal.c
523 malloc instead of the real malloc. We can't
524 free it, we have to abandon the old storage. */
526 newp
= malloc ((2 + newsize
) * sizeof (dtv_t
));
529 memcpy (newp
, &dtv
[-1], oldsize
* sizeof (dtv_t
));
533 newp
= realloc (&dtv
[-1],
534 (2 + newsize
) * sizeof (dtv_t
));
539 newp
[0].counter
= newsize
;
541 /* Clear the newly allocated part. */
542 memset (newp
+ 2 + oldsize
, '\0',
543 (newsize
- oldsize
) * sizeof (dtv_t
));
545 /* Point dtv to the generation counter. */
548 /* Install this new dtv in the thread data
550 INSTALL_NEW_DTV (dtv
);
553 /* If there is currently memory allocate for this
554 dtv entry free it. */
555 /* XXX Ideally we will at some point create a memory
557 if (dtv
[modid
].pointer
!= TLS_DTV_UNALLOCATED
)
558 /* Note that free is called for NULL is well. We
559 deallocate even if it is this dtv entry we are
560 supposed to load. The reason is that we call
561 memalign and not malloc. */
562 free (dtv
[modid
].pointer
);
564 /* This module is loaded dynamically- We defer
565 memory allocation. */
566 dtv
[modid
].pointer
= TLS_DTV_UNALLOCATED
;
568 if (modid
== GET_ADDR_MODULE
)
574 while ((listp
= listp
->next
) != NULL
);
576 /* This will be the new maximum generation counter. */
577 dtv
[0].counter
= new_gen
;
581 p
= dtv
[GET_ADDR_MODULE
].pointer
;
583 if (__builtin_expect (p
== TLS_DTV_UNALLOCATED
, 0))
585 /* The allocation was deferred. Do it now. */
588 /* Find the link map for this module. */
589 size_t idx
= GET_ADDR_MODULE
;
590 struct dtv_slotinfo_list
*listp
= GL(dl_tls_dtv_slotinfo_list
);
592 while (idx
>= listp
->len
)
598 the_map
= listp
->slotinfo
[idx
].map
;
601 p
= dtv
[GET_ADDR_MODULE
].pointer
= allocate_and_init (the_map
);
604 return (char *) p
+ GET_ADDR_OFFSET
;