1 /* Verify that DSO is unloaded only if its TLS objects are destroyed.
2 Copyright (C) 2013-2015 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, see
17 <http://www.gnu.org/licenses/>. */
19 /* This test dynamically loads a DSO and spawns a thread that subsequently
20 calls into the DSO to register a destructor for an object in the DSO and
21 then calls dlclose on the handle for the DSO. When the thread exits, the
22 DSO should not be unloaded or else the destructor called during thread exit
23 will crash. Further in the main thread, the DSO is opened and closed again,
24 at which point the DSO should be unloaded. */
34 #define DSO_NAME "$ORIGIN/tst-tls-atexit-lib.so"
36 /* Walk through the map in the _r_debug structure to see if our lib is still
41 struct link_map
*lm
= (struct link_map
*) _r_debug
.r_map
;
43 for (; lm
; lm
= lm
->l_next
)
44 if (lm
->l_type
== lt_loaded
&& lm
->l_name
45 && strcmp (basename (DSO_NAME
), basename (lm
->l_name
)) == 0)
50 /* Accept a valid handle returned by DLOPEN, load the reg_dtor symbol to
51 register a destructor and then call dlclose on the handle. The dlclose
52 should not unload the DSO since the destructor has not been called yet. */
54 reg_dtor_and_close (void *h
)
56 void (*reg_dtor
) (void) = (void (*) (void)) dlsym (h
, "reg_dtor");
60 printf ("Unable to find symbol: %s\n", dlerror ());
61 return (void *) (uintptr_t) 1;
72 spawn_thread (void *h
)
78 if ((ret
= pthread_create (&t
, NULL
, reg_dtor_and_close
, h
)) != 0)
80 printf ("pthread_create failed: %s\n", strerror (ret
));
84 if ((ret
= pthread_join (t
, &thr_ret
)) != 0)
86 printf ("pthread_join failed: %s\n", strerror (ret
));
100 void *h1
= dlopen (DSO_NAME
, RTLD_LAZY
);
103 printf ("h1: Unable to load DSO: %s\n", dlerror ());
107 if (spawn_thread (h1
) != 0)
110 /* Now this should unload the DSO. FIXME: This is a bug, calling dlclose
111 like this is actually wrong, but it works because cxa_thread_atexit_impl
112 has a bug which results in dlclose allowing this to work. */
115 /* Check link maps to ensure that the DSO has unloaded. */
122 #define TEST_FUNCTION do_test ()
123 #include "../test-skeleton.c"