From 4c533566c2bb94162bcc1f66c5cf9db609e20803 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Thu, 17 Jan 2008 20:20:00 +0000 Subject: * include/link.h (FORCED_DYNAMIC_TLS_OFFSET): Define. * elf/dl-close.c (_dl_close): Check for it. * elf/dl-reloc.c (CHECK_STATIC_TLS): Likewise. (_dl_allocate_static_tls): Likewise. * elf/dl-tls.c (_dl_allocate_tls_init): Likewise. (__tls_get_addr): Protect from race conditions in setting l_tls_offset to it. * elf/tst-tls16.c: New file. * elf/tst-tlsmod16a.c: New file. * elf/tst-tlsmod16b.c: New file. * elf/Makefile: Add rules to build and run tst-tls16. --- elf/dl-tls.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'elf/dl-tls.c') diff --git a/elf/dl-tls.c b/elf/dl-tls.c index d5865ab..3059481 100644 --- a/elf/dl-tls.c +++ b/elf/dl-tls.c @@ -413,7 +413,8 @@ _dl_allocate_tls_init (void *result) not be the generation counter. */ maxgen = MAX (maxgen, listp->slotinfo[cnt].gen); - if (map->l_tls_offset == NO_TLS_OFFSET) + if (map->l_tls_offset == NO_TLS_OFFSET + || map->l_tls_offset == FORCED_DYNAMIC_TLS_OFFSET) { /* For dynamically loaded modules we simply store the value indicating deferred allocation. */ @@ -702,6 +703,7 @@ __tls_get_addr (GET_ADDR_ARGS) if (__builtin_expect (dtv[0].counter != GL(dl_tls_generation), 0)) the_map = _dl_update_slotinfo (GET_ADDR_MODULE); + retry: p = dtv[GET_ADDR_MODULE].pointer.val; if (__builtin_expect (p == TLS_DTV_UNALLOCATED, 0)) @@ -722,6 +724,28 @@ __tls_get_addr (GET_ADDR_ARGS) the_map = listp->slotinfo[idx].map; } + /* Make sure that, if a dlopen running in parallel forces the + variable into static storage, we'll wait until the address in + the static TLS block is set up, and use that. If we're + undecided yet, make sure we make the decision holding the + lock as well. */ + if (__builtin_expect (the_map->l_tls_offset + != FORCED_DYNAMIC_TLS_OFFSET, 0)) + { + __rtld_lock_lock_recursive (GL(dl_load_lock)); + if (__builtin_expect (the_map->l_tls_offset == NO_TLS_OFFSET, 1)) + { + the_map->l_tls_offset = FORCED_DYNAMIC_TLS_OFFSET; + __rtld_lock_unlock_recursive (GL(dl_load_lock)); + } + else + { + __rtld_lock_unlock_recursive (GL(dl_load_lock)); + if (__builtin_expect (the_map->l_tls_offset + != FORCED_DYNAMIC_TLS_OFFSET, 1)) + goto retry; + } + } p = dtv[GET_ADDR_MODULE].pointer.val = allocate_and_init (the_map); dtv[GET_ADDR_MODULE].pointer.is_static = false; } -- cgit v1.1