]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
C++11 thread_local destructors support
authorSiddhesh Poyarekar <siddhesh@redhat.com>
Mon, 18 Feb 2013 13:38:21 +0000 (19:08 +0530)
committerSiddhesh Poyarekar <siddhesh@redhat.com>
Mon, 18 Feb 2013 13:38:21 +0000 (19:08 +0530)
This feature is specifically for the C++ compiler to offload calling
thread_local object destructors on thread program exit, to glibc.
This is to overcome the possible complication of destructors of
thread_local objects getting called after the DSO in which they're
defined is unloaded by the dynamic linker.  The DSO is marked as
'unloadable' if it has a constructed thread_local object and marked as
'unloadable' again when all the constructed thread_local objects
defined in it are destroyed.

40 files changed:
ChangeLog
Versions.def
include/link.h
include/stdlib.h
nptl/pthread_create.c
ports/ChangeLog.alpha
ports/ChangeLog.arm
ports/ChangeLog.ia64
ports/ChangeLog.m68k
ports/ChangeLog.mips
ports/ChangeLog.powerpc
ports/ChangeLog.tile
ports/sysdeps/unix/sysv/linux/alpha/nptl/libc.abilist
ports/sysdeps/unix/sysv/linux/arm/nptl/libc.abilist
ports/sysdeps/unix/sysv/linux/ia64/nptl/libc.abilist
ports/sysdeps/unix/sysv/linux/m68k/coldfire/nptl/libc.abilist
ports/sysdeps/unix/sysv/linux/m68k/m680x0/nptl/libc.abilist
ports/sysdeps/unix/sysv/linux/mips/mips32/nptl/libc.abilist
ports/sysdeps/unix/sysv/linux/mips/mips64/n32/nptl/libc.abilist
ports/sysdeps/unix/sysv/linux/mips/mips64/n64/nptl/libc.abilist
ports/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/nptl/libc.abilist
ports/sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/nptl/libc.abilist
ports/sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/nptl/libc.abilist
ports/sysdeps/unix/sysv/linux/tile/tilepro/nptl/libc.abilist
stdlib/Makefile
stdlib/Versions
stdlib/cxa_thread_atexit_impl.c [new file with mode: 0644]
stdlib/exit.c
stdlib/tst-tls-atexit-lib.c [new file with mode: 0644]
stdlib/tst-tls-atexit.c [new file with mode: 0644]
sysdeps/unix/sysv/linux/i386/nptl/libc.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/nptl/libc.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc64/nptl/libc.abilist
sysdeps/unix/sysv/linux/s390/s390-32/nptl/libc.abilist
sysdeps/unix/sysv/linux/s390/s390-64/nptl/libc.abilist
sysdeps/unix/sysv/linux/sh/nptl/libc.abilist
sysdeps/unix/sysv/linux/sparc/sparc32/nptl/libc.abilist
sysdeps/unix/sysv/linux/sparc/sparc64/nptl/libc.abilist
sysdeps/unix/sysv/linux/x86_64/64/nptl/libc.abilist
sysdeps/unix/sysv/linux/x86_64/x32/nptl/libc.abilist

index 20a7aa1cc53aa698625714143b28f153125c6d7e..44ab1f884680f9ca02886bf302e47077e9395580 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,38 @@
 2013-02-18  Siddhesh Poyarekar  <siddhesh@redhat.com>
 
+       * Versions.def: Add GLIBC_2.18.
+       * include/link.h (struct link_map): New member l_tls_dtor_count.
+       * include/stdlib.h (__cxa_thread_atexit_impl): Declare.
+       (__call_tls_dtors): Likewise.
+       * sysdeps/unix/sysv/linux/i386/nptl/libc.abilist: Add
+       __cxa_thread_atexit_impl.
+       * sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/nptl/libc.abilist:
+       Likewise.
+       * sysdeps/unix/sysv/linux/powerpc/powerpc64/nptl/libc.abilist:
+       Likewise.
+       * sysdeps/unix/sysv/linux/s390/s390-32/nptl/libc.abilist:
+       Likewise.
+       * sysdeps/unix/sysv/linux/s390/s390-64/nptl/libc.abilist:
+       Likewise.
+       * sysdeps/unix/sysv/linux/sh/nptl/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/sparc/sparc32/nptl/libc.abilist:
+       Likewise.
+       * sysdeps/unix/sysv/linux/sparc/sparc64/nptl/libc.abilist:
+       Likewise.
+       * sysdeps/unix/sysv/linux/x86_64/64/nptl/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/x86_64/x32/nptl/libc.abilist:
+       Likewise.
+       * stdlib/Makefile (routines): Add __cxa_thread_atexit_impl.
+       (tests): Add test case tst-tls-atexit.
+       (modules-names): Add shared library for tst-tls-atexit.
+       * stdlib/Versions (GLIBC_2.17): Add __cxa_thread_atexit_impl.
+       (GLIBC_PRIVATE): Add __call_tls_dtors.
+       * stdlib/cxa_thread_atexit_impl.c: New file with helper function
+       for libstdc++.
+       * stdlib/exit.c (__run_exit_handlers): Call __call_tls_dtors.
+       * stdlib/tst-tls-atexit.c: New test case.
+       * stdlib/tst-tls-atexit-lib.c: New test case.
+
        * misc/tst-pselect.c: Include stdlib.h for declaration of exit.
        * nptl/sysdeps/pthread/tst-timer.c: Likewise.
        * nptl/tst-barrier4.c: Likewise.
index 3c9e0aedbc46a82f843dfafad652c7a917fd1616..8651992a1030f50a96b2b4a77a2e810ee46c9741 100644 (file)
@@ -34,6 +34,7 @@ libc {
   GLIBC_2.15
   GLIBC_2.16
   GLIBC_2.17
+  GLIBC_2.18
   HURD_CTHREADS_0.3
 %ifdef EXPORT_UNWIND_FIND_FDE
   GCC_3.0
index 230e95d6c8eb67adc37893bf7051fc327d3ac47a..7dc3cd1e3294d9f3af17d3f76f439e41ad0f9bea 100644 (file)
@@ -302,6 +302,9 @@ struct link_map
     /* Index of the module in the dtv array.  */
     size_t l_tls_modid;
 
+    /* Number of thread_local objects constructed by this DSO.  */
+    size_t l_tls_dtor_count;
+
     /* Information used to change permission after the relocations are
        done.  */
     ElfW(Addr) l_relro_addr;
index 2e536641b9d00fb51208aaf1dc9396618a3cf8a3..db1812d85ad6f9dd0230ea4a4332be1039726ef9 100644 (file)
@@ -100,6 +100,11 @@ extern int __cxa_atexit (void (*func) (void *), void *arg, void *d);
 extern int __cxa_atexit_internal (void (*func) (void *), void *arg, void *d)
      attribute_hidden;
 
+extern int __cxa_thread_atexit_impl (void (*func) (void *), void *arg,
+                                    void *d);
+extern void __call_tls_dtors (void);
+libc_hidden_proto (__call_tls_dtors);
+
 extern void __cxa_finalize (void *d);
 
 extern int __posix_memalign (void **memptr, size_t alignment, size_t size);
index 4fe0755079e5491ad360c3b4f26c182543a0bd6e..c6f2fdd724d6832b6e57ecb0e99185f0ffb14168 100644 (file)
@@ -311,6 +311,9 @@ start_thread (void *arg)
 #endif
     }
 
+  /* Call destructors for the thread_local TLS variables.  */
+  __call_tls_dtors ();
+
   /* Run the destructor for the thread-local data.  */
   __nptl_deallocate_tsd ();
 
index 991c36aea265c6393cd5f06cb0d2b6f9d68212ec..0ac8add048aa2faae255e4a1d4056474d94fd6c3 100644 (file)
@@ -1,3 +1,8 @@
+2013-02-18  Siddhesh Poyarekar  <siddhesh@redhat.com>
+
+       * sysdeps/unix/sysv/linux/alpha/nptl/libc.abilist: Add
+       __cxa_thread_atexit_impl.
+
 2013-02-14  Joseph Myers  <joseph@codesourcery.com>
 
        [BZ #13550]
index 2596b8da3587259562518661b7243cf3699697f1..3a5880bd39d7adf5163e37d11959b3d56eee584c 100644 (file)
@@ -1,3 +1,8 @@
+2013-02-18  Siddhesh Poyarekar  <siddhesh@redhat.com>
+
+       * sysdeps/unix/sysv/linux/arm/nptl/libc.abilist: Add
+       __cxa_thread_atexit_impl.
+
 2013-02-13  Joseph Myers  <joseph@codesourcery.com>
 
        [BZ #13550]
index 26d99f7b225e968e8dbc2b4aba9df9bc919824ae..d9fda457fca83685e9f39718d9f0e828d778844d 100644 (file)
@@ -1,3 +1,8 @@
+2013-02-18  Siddhesh Poyarekar  <siddhesh@redhat.com>
+
+       * sysdeps/unix/sysv/linux/ia64/nptl/libc.abilist: Add
+       __cxa_thread_atexit_impl.
+
 2013-02-08  Joseph Myers  <joseph@codesourcery.com>
 
        [BZ #13550]
index dfbe0df6c18ee515e09d8934e12909f042117de0..e7cb81e8f8a3a711fdb00b5ad81f685b29cf0d45 100644 (file)
@@ -1,3 +1,10 @@
+2013-02-18  Siddhesh Poyarekar  <siddhesh@redhat.com>
+
+       * sysdeps/unix/sysv/linux/m68k/coldfire/nptl/libc.abilist: Add
+       __cxa_thread_atexit_impl.
+       * sysdeps/unix/sysv/linux/m68k/m680x0/nptl/libc.abilist:
+       Likewise.
+
 2013-02-08  Andreas Schwab  <schwab@linux-m68k.org>
 
        * sysdeps/unix/sysv/linux/m68k/kernel-features.h
index b91a1a8619275b0fbaf70710b14b7db9e20433ed..de967c65d17bad999295a546de4f86154e9e7211 100644 (file)
@@ -1,3 +1,12 @@
+2013-02-18  Siddhesh Poyarekar  <siddhesh@redhat.com>
+
+       * sysdeps/unix/sysv/linux/mips/mips32/nptl/libc.abilist: Add
+       __cxa_thread_atexit_impl.
+       * sysdeps/unix/sysv/linux/mips/mips64/n32/nptl/libc.abilist:
+       Likewise.
+       * sysdeps/unix/sysv/linux/mips/mips64/n64/nptl/libc.abilist:
+       Likewise.
+
 2013-02-13  Joseph Myers  <joseph@codesourcery.com>
 
        [BZ #13550]
index ecac5274396a035b3ef8ee2d84d5160d47d3b0a3..c66dc07e432f5590226a3b26597664065d3006d5 100644 (file)
@@ -1,3 +1,10 @@
+2013-02-18  Siddhesh Poyarekar  <siddhesh@redhat.com>
+
+       * sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/nptl/libc.abilist:
+       Add __cxa_thread_atexit_impl.
+       * sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/nptl/libc.abilist:
+       Likewise.
+
 2013-02-14  Joseph Myers  <joseph@codesourcery.com>
 
        [BZ #13550]
index a5eaec3a9b6fb18820895474f8bbadab8dafab02..a473b3084712fca894ee28935b9b7199a84929c0 100644 (file)
@@ -1,3 +1,10 @@
+2013-02-18  Siddhesh Poyarekar  <siddhesh@redhat.com>
+
+       * sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/nptl/libc.abilist:
+       Add __cxa_thread_atexit_impl.
+       * sysdeps/unix/sysv/linux/tile/tilepro/nptl/libc.abilist:
+       Likewise.
+
 2013-01-10  Chris Metcalf  <cmetcalf@tilera.com>
 
        * sysdeps/unix/sysv/linux/tile/tilegx/ldd-rewrite.sed: New file.
index 1d0cc7ec317ae11464fa8637707ed121d652f7e9..980e08857ae9a4aff744a04b1d6a34fa6a89ffd4 100644 (file)
@@ -1819,6 +1819,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.2
  GLIBC_2.2 A
  _IO_adjust_wcolumn F
index ceab6b2cd9d04844f2883a3a772acecbab731809..ce45208b5f3a1ee469365766ecafeab529a3b2e5 100644 (file)
@@ -86,6 +86,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.4
  GLIBC_2.4 A
  _Exit F
index b3510fe2a281081c92f68bcd72c8e6d22cca5480..067552d17573789b7312cd06db6b88ffe5d2cbcf 100644 (file)
@@ -86,6 +86,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.2
  GLIBC_2.2 A
  _Exit F
index 3c40379aacde4958ea02648def4ea2477a7ac428..f06cc8ef8b6497c87f115772c77404a12176c105 100644 (file)
@@ -87,6 +87,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.4
  GLIBC_2.4 A
  _Exit F
index f998b1b2764255b3f5f874c0eaf7b5b5172413eb..9010ea733b6ab75c3504d64d4c18b473d2b581fc 100644 (file)
@@ -1775,6 +1775,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.2
  GLIBC_2.2 A
  _IO_adjust_wcolumn F
index 73788692361e5b2d1dfc28c3284830fdc9916561..f8cefd1f51126413c9921d19ed21c33b9826bd9e 100644 (file)
@@ -2250,3 +2250,6 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
index df2e63728a0436268c5255401607330a1f548041..9dbbd97c8ebf78f960b6f07a8631e25fa3dab525 100644 (file)
@@ -1398,6 +1398,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.2
  GLIBC_2.2 A
  _Exit F
index 22b3068d8f089ef5545917b39e0b68d0122beb97..c7e46aa8694ef41007aa6b9c505bc3625930ffcd 100644 (file)
@@ -1396,6 +1396,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.2
  GLIBC_2.2 A
  _Exit F
index 0efc6b55c42233971507297d30460ef5407c9f31..9b6d6637484094b1b61019e1a09cc85cde89c50e 100644 (file)
@@ -1781,6 +1781,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.2
  GLIBC_2.2 A
  _IO_adjust_wcolumn F
index d79b2df55d47c3a6e1dae718302a3a4dd3537be6..caf74b89e10b64fbaaf0aa51ad5422f5e8480439 100644 (file)
@@ -2088,3 +2088,6 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
index f61740567526d79b5a00fac00fb8f2b35b328739..68d975be55f8724f7bd91f1bc0c87a981e33d151 100644 (file)
@@ -2088,3 +2088,6 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
index d79b2df55d47c3a6e1dae718302a3a4dd3537be6..caf74b89e10b64fbaaf0aa51ad5422f5e8480439 100644 (file)
@@ -2088,3 +2088,6 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
index e2078f88778bed4581e6087119ff49276e0c7fdf..6f98c71c649eaa116f86422f1e869b348c816ff1 100644 (file)
@@ -34,7 +34,7 @@ routines      :=                                                            \
        bsearch qsort msort                                                   \
        getenv putenv setenv secure-getenv                                    \
        exit on_exit atexit cxa_atexit cxa_finalize old_atexit                \
-       quick_exit at_quick_exit cxa_at_quick_exit                            \
+       quick_exit at_quick_exit cxa_at_quick_exit cxa_thread_atexit_impl     \
        abs labs llabs                                                        \
        div ldiv lldiv                                                        \
        mblen mbstowcs mbtowc wcstombs wctomb                                 \
@@ -71,9 +71,11 @@ tests                := tst-strtol tst-strtod testmb testrand testsort testdiv   \
                   tst-makecontext2 tst-strtod6 tst-unsetenv1               \
                   tst-makecontext3 bug-getcontext bug-fmtmsg1              \
                   tst-secure-getenv tst-strtod-overflow tst-strtod-round   \
-                  tst-tininess tst-strtod-underflow
+                  tst-tininess tst-strtod-underflow tst-tls-atexit
 tests-static   := tst-secure-getenv
 
+modules-names  = tst-tls-atexit-lib
+
 include ../Makeconfig
 
 ifeq ($(build-shared),yes)
@@ -155,3 +157,9 @@ $(objpfx)bug-getcontext: $(link-libm)
 $(objpfx)tst-strtod-round: $(link-libm)
 $(objpfx)tst-tininess: $(link-libm)
 $(objpfx)tst-strtod-underflow: $(link-libm)
+
+tst-tls-atexit-lib.so-no-z-defs = yes
+
+LDFLAGS-tst-tls-atexit = $(common-objpfx)nptl/libpthread.so \
+                        $(common-objpfx)dlfcn/libdl.so
+$(objpfx)tst-tls-atexit.out: $(objpfx)tst-tls-atexit-lib.so
index 250bd5fad7a31c2d92a947f29bc68dc7c3550f15..f1777dfcf4d799a118d6a41d00538fdf0b951ae0 100644 (file)
@@ -106,6 +106,9 @@ libc {
   GLIBC_2.17 {
     secure_getenv;
   }
+  GLIBC_2.18 {
+    __cxa_thread_atexit_impl;
+  }
   GLIBC_PRIVATE {
     # functions which have an additional interface since they are
     # are cancelable.
@@ -114,5 +117,6 @@ libc {
     __abort_msg;
     # Used from other libraries
     __libc_secure_getenv;
+    __call_tls_dtors;
   }
 }
diff --git a/stdlib/cxa_thread_atexit_impl.c b/stdlib/cxa_thread_atexit_impl.c
new file mode 100644 (file)
index 0000000..9638efc
--- /dev/null
@@ -0,0 +1,102 @@
+/* Register destructors for C++ TLS variables declared with thread_local.
+   Copyright (C) 2012 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <stdlib.h>
+#include <ldsodefs.h>
+
+typedef void (*dtor_func) (void *);
+
+struct dtor_list
+{
+  dtor_func func;
+  void *obj;
+  struct link_map *map;
+  struct dtor_list *next;
+};
+
+static __thread struct dtor_list *tls_dtor_list;
+static __thread void *dso_symbol_cache;
+static __thread struct link_map *lm_cache;
+
+/* Register a destructor for TLS variables declared with the 'thread_local'
+   keyword.  This function is only called from code generated by the C++
+   compiler.  FUNC is the destructor function and OBJ is the object to be
+   passed to the destructor.  DSO_SYMBOL is the __dso_handle symbol that each
+   DSO has at a unique address in its map, added from crtbegin.o during the
+   linking phase.  */
+int
+__cxa_thread_atexit_impl (dtor_func func, void *obj, void *dso_symbol)
+{
+  /* Prepend.  */
+  struct dtor_list *new = calloc (1, sizeof (struct dtor_list));
+  new->func = func;
+  new->obj = obj;
+  new->next = tls_dtor_list;
+  tls_dtor_list = new;
+
+  /* See if we already encountered the DSO.  */
+  __rtld_lock_lock_recursive (GL(dl_load_lock));
+
+  if (__builtin_expect (dso_symbol_cache != dso_symbol, 0))
+    {
+      ElfW(Addr) caller = (ElfW(Addr)) dso_symbol;
+
+      struct link_map *l = _dl_find_dso_for_object (caller);
+
+      /* If the address is not recognized the call comes from the main
+         program (we hope).  */
+      lm_cache = l ? l : GL(dl_ns)[LM_ID_BASE]._ns_loaded;
+    }
+  /* A destructor could result in a thread_local construction and the former
+     could have cleared the flag.  */
+  if (lm_cache->l_type == lt_loaded && lm_cache->l_tls_dtor_count == 0)
+    lm_cache->l_flags_1 |= DF_1_NODELETE;
+
+  new->map = lm_cache;
+  new->map->l_tls_dtor_count++;
+
+  __rtld_lock_unlock_recursive (GL(dl_load_lock));
+
+  return 0;
+}
+
+/* Call the destructors.  This is called either when a thread returns from the
+   initial function or when the process exits via the exit(3) function.  */
+void
+__call_tls_dtors (void)
+{
+  while (tls_dtor_list)
+    {
+      struct dtor_list *cur = tls_dtor_list;
+      tls_dtor_list = tls_dtor_list->next;
+
+      cur->func (cur->obj);
+
+      __rtld_lock_lock_recursive (GL(dl_load_lock));
+
+      /* Allow DSO unload if count drops to zero.  */
+      cur->map->l_tls_dtor_count--;
+      if (cur->map->l_tls_dtor_count == 0 && cur->map->l_type == lt_loaded)
+        cur->map->l_flags_1 &= ~DF_1_NODELETE;
+
+      __rtld_lock_unlock_recursive (GL(dl_load_lock));
+
+      free (cur);
+    }
+}
+libc_hidden_def (__call_tls_dtors)
index e9a2139f4eef6c33f55fe8c4a53ddccba99e0403..2e86caa2d44026392b1816fe96ceb480057bc31a 100644 (file)
@@ -33,6 +33,9 @@ attribute_hidden
 __run_exit_handlers (int status, struct exit_function_list **listp,
                     bool run_list_atexit)
 {
+  /* First, call the TLS destructors.  */
+  __call_tls_dtors ();
+
   /* We do it this way to handle recursive calls to exit () made by
      the functions registered with `atexit' and `on_exit'. We call
      everyone on the list and use the status value in the last
diff --git a/stdlib/tst-tls-atexit-lib.c b/stdlib/tst-tls-atexit-lib.c
new file mode 100644 (file)
index 0000000..aab28bb
--- /dev/null
@@ -0,0 +1,36 @@
+/* Verify that DSO is unloaded only if its TLS objects are destroyed - the DSO.
+   Copyright (C) 2012 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+extern void *__dso_handle;
+
+typedef struct
+{
+  void *val;
+} A;
+
+/* We only care about the destructor.  */
+void A_dtor (void *obj)
+{
+  ((A *)obj)->val = obj;
+}
+
+void do_foo (void)
+{
+  static __thread A b;
+  __cxa_thread_atexit_impl (A_dtor, &b, __dso_handle);
+}
diff --git a/stdlib/tst-tls-atexit.c b/stdlib/tst-tls-atexit.c
new file mode 100644 (file)
index 0000000..b7312cb
--- /dev/null
@@ -0,0 +1,111 @@
+/* Verify that DSO is unloaded only if its TLS objects are destroyed.
+   Copyright (C) 2012 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/* There are two tests in this test case.  The first is implicit where it is
+   assumed that the destructor call on exit of the LOAD function does not
+   segfault.  The other is a verification that after the thread has exited, a
+   dlclose will unload the DSO.  */
+
+#include <dlfcn.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+void *handle;
+pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
+
+void *
+load (void *u)
+{
+  pthread_mutex_lock (&m);
+  handle = dlopen ("$ORIGIN/tst-tls-atexit-lib.so", RTLD_LAZY);
+  if (!handle)
+    {
+      printf ("Unable to load DSO: %s\n", dlerror ());
+      return (void *) (uintptr_t) 1;
+    }
+
+  void (*foo) (void) = (void (*) (void)) dlsym(handle, "do_foo");
+
+  if (!foo)
+    {
+      printf ("Unable to find symbol: %s\n", dlerror ());
+      exit (1);
+    }
+
+  foo ();
+
+  /* This should not unload the DSO.  If it does, then the thread exit will
+     result in a segfault.  */
+  dlclose (handle);
+  pthread_mutex_unlock (&m);
+
+  return NULL;
+}
+
+int
+main (void)
+{
+  pthread_t t;
+  int ret;
+  void *thr_ret;
+
+  if ((ret = pthread_create (&t, NULL, load, NULL)) != 0)
+    {
+      printf ("pthread_create failed: %s\n", strerror (ret));
+      return 1;
+    }
+
+  if ((ret = pthread_join (t, &thr_ret)) != 0)
+    {
+      printf ("pthread_create failed: %s\n", strerror (ret));
+      return 1;
+    }
+
+  if (thr_ret != NULL)
+    return 1;
+
+  /* Now this should unload the DSO.  */
+  dlclose (handle);
+
+  /* Run through our maps and ensure that the DSO is unloaded.  */
+  FILE *f = fopen ("/proc/self/maps", "r");
+
+  if (f == NULL)
+    {
+      perror ("Failed to open /proc/self/maps");
+      fprintf (stderr, "Skipping verification of DSO unload\n");
+      return 0;
+    }
+
+  char *line = NULL;
+  size_t s = 0;
+  while (getline (&line, &s, f) > 0)
+    {
+      if (strstr (line, "tst-tls-atexit-lib.so"))
+        {
+         printf ("DSO not unloaded yet:\n%s", line);
+         return 1;
+       }
+    }
+  free (line);
+
+  return 0;
+}
index 67d592959b2afb66ac48634d735dc3782abeb088..3cb314ddfc8e0372816434d0e1731432be489b46 100644 (file)
@@ -1819,6 +1819,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.2
  GLIBC_2.2 A
  _IO_adjust_wcolumn F
index 8e4595825b15a280841869977d9869e369b136b0..f27b48b3c6d8eadc9fde22e6f33753239f1699dc 100644 (file)
@@ -1781,6 +1781,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.2
  GLIBC_2.2 A
  _IO_adjust_wcolumn F
index 8eaaccdfc77840f056cab32cf68688ee8d2bbc3f..195b58757b2b136307dce7f44d52d851723d965f 100644 (file)
@@ -87,6 +87,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.3
  GLIBC_2.3 A
  _Exit F
index d9914ff53fa2c98d97b6030d07395c777ad373b1..b6256d5c255d63e539c86c5a9dfbe00536002e5c 100644 (file)
@@ -1771,6 +1771,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.2
  GLIBC_2.2 A
  _IO_adjust_wcolumn F
index ef1ead30697d78534b621f7ee6b78419f22477a5..265f66d90509e3b4f4587f0ac75d74eac029d339 100644 (file)
@@ -92,6 +92,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.2
  GLIBC_2.2 A
  _Exit F
index 733b550fee04474f6d9d9fc713a2193c4fe2bf8d..a653292c243e376cb527f9e3dea716091c19326d 100644 (file)
@@ -92,6 +92,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.2
  GLIBC_2.2 A
  _Exit F
index 3a96ea883bc594bf002851881ca28868fefd3c19..9defbdf1335c641a0c0399ea4fc9e44a92e1e641 100644 (file)
@@ -1776,6 +1776,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.2
  GLIBC_2.2 A
  _IO_adjust_wcolumn F
index aa892b8b8bcb3500a6d9b843b30e576dbfc1e36a..35987faa6eeae430cde40cedbb44867993e626c2 100644 (file)
@@ -97,6 +97,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.2
  GLIBC_2.2 A
  _Exit F
index a42d424cdebc48a49016928e03ac7667a2f32ea8..914b5908f0fb415a04c87f0aaeecaa02a08e5d02 100644 (file)
@@ -88,6 +88,9 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F
 GLIBC_2.2.5
  GLIBC_2.2.5 A
  _Exit F
index 108b80fb43ee75a6f9787e354596ebc10d0638d0..0f64c8d20fbee20d6917282b1977f77c74ef1c93 100644 (file)
@@ -2086,3 +2086,6 @@ GLIBC_2.17
  clock_nanosleep F
  clock_settime F
  secure_getenv F
+GLIBC_2.18
+ GLIBC_2.18 A
+ __cxa_thread_atexit_impl F