537
allocate_and_init (struct link_map *map)
538
allocate_and_init (dtv_t *dtv, struct link_map *map)
540
541
newp = __signal_safe_memalign (map->l_tls_align, map->l_tls_blocksize);
541
542
if (newp == NULL)
544
/* Initialize the memory. */
545
/* Initialize the memory. Since this is our thread's space, we are
546
under a signal mask, and no one has touched this section before,
547
we can safely just overwrite whatever's there. */
545
548
memset (__mempcpy (newp, map->l_tls_initimage, map->l_tls_initimage_size),
546
549
'\0', map->l_tls_blocksize - map->l_tls_initimage_size);
551
dtv->pointer.val = newp;
725
738
the_map = listp->slotinfo[idx].map;
729
/* Make sure that, if a dlopen running in parallel forces the
730
variable into static storage, we'll wait until the address in the
731
static TLS block is set up, and use that. If we're undecided
732
yet, make sure we make the decision holding the lock as well. */
733
if (__builtin_expect (the_map->l_tls_offset
734
!= FORCED_DYNAMIC_TLS_OFFSET, 0))
736
__rtld_lock_lock_recursive (GL(dl_load_lock));
737
if (__builtin_expect (the_map->l_tls_offset == NO_TLS_OFFSET, 1))
739
the_map->l_tls_offset = FORCED_DYNAMIC_TLS_OFFSET;
740
__rtld_lock_unlock_recursive (GL(dl_load_lock));
744
__rtld_lock_unlock_recursive (GL(dl_load_lock));
745
if (__builtin_expect (the_map->l_tls_offset
746
!= FORCED_DYNAMIC_TLS_OFFSET, 1))
748
void *p = dtv[GET_ADDR_MODULE].pointer.val;
749
if (__builtin_expect (p == TLS_DTV_UNALLOCATED, 0))
752
return (char *) p + GET_ADDR_OFFSET;
756
void *p = dtv[GET_ADDR_MODULE].pointer.val = allocate_and_init (the_map);
757
dtv[GET_ADDR_MODULE].pointer.is_static = false;
759
return (char *) p + GET_ADDR_OFFSET;
741
_dl_mask_all_signals (&old);
743
/* As with update_slotinfo, we use the sigmask as a check against
745
if (dtv[GET_ADDR_MODULE].pointer.val != TLS_DTV_UNALLOCATED)
748
/* Synchronize against a parallel dlopen() forcing this variable
749
into static storage. If that happens, we have to be more careful
750
about initializing the area, as that dlopen() will be iterating
751
the threads to do so itself. */
753
if ((offset = the_map->l_tls_offset) == NO_TLS_OFFSET)
755
/* l_tls_offset starts out at NO_TLS_OFFSET, and all attempts to
756
change it go from NO_TLS_OFFSET to some other value. We use
757
compare_and_exchange to ensure only one attempt succeeds. We
758
don't actually need any memory ordering here, but _acq is the
759
weakest available. */
760
(void) atomic_compare_and_exchange_bool_acq (&the_map->l_tls_offset,
761
FORCED_DYNAMIC_TLS_OFFSET,
763
offset = the_map->l_tls_offset;
764
assert (offset != NO_TLS_OFFSET);
766
if (offset == FORCED_DYNAMIC_TLS_OFFSET)
768
allocate_and_init (&dtv[GET_ADDR_MODULE], the_map);
772
void **pp = &dtv[GET_ADDR_MODULE].pointer.val;
773
while (atomic_forced_read (*pp) == TLS_DTV_UNALLOCATED)
775
/* for lack of a better (safe) thing to do, just spin.
776
Someone else (not us; it's done under a signal mask) set
777
this map to a static TLS offset, and they'll iterate all
778
threads to initialize it. They'll eventually write
779
to pointer.val, at which point we know they've fully
780
completed initialization. */
783
/* Make sure we've picked up their initialization of the actual
784
block; this pairs against the write barrier in
785
init_one_static_tls, guaranteeing that we see their write of
786
the tls_initimage into the static region. */
787
atomic_read_barrier ();
790
assert (dtv[GET_ADDR_MODULE].pointer.val != TLS_DTV_UNALLOCATED);
791
_dl_unmask_signals (&old);
793
return (char *) dtv[GET_ADDR_MODULE].pointer.val + GET_ADDR_OFFSET;