Add tst-gnu2-tls2mod1 to test-internal-extras
[glibc.git] / elf / tst-nodelete-dlclose-dso.c
blob02e85d1eabcf86a1035820a3f618555ed1697f89
1 /* Bug 11941: Improper assert map->l_init_called in dlclose.
2 Copyright (C) 2016-2024 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 <https://www.gnu.org/licenses/>. */
19 /* This is the primary DSO that is loaded by the application. This DSO
20 then loads a plugin with RTLD_NODELETE. This plugin depends on this
21 DSO. This dependency chain means that at application shutdown the
22 plugin will be destructed first. Thus by the time this DSO is
23 destructed we will be calling dlclose on an object that has already
24 been destructed. It is allowed to call dlclose in this way and
25 should not assert. */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <dlfcn.h>
30 /* Plugin to load. */
31 static void *plugin_lib = NULL;
32 /* Plugin function. */
33 static void (*plugin_func) (void);
34 #define LIB_PLUGIN "tst-nodelete-dlclose-plugin.so"
36 /* This function is never called but the plugin references it.
37 We do this to avoid any future --as-needed from removing the
38 plugin's DT_NEEDED on this DSO (required for the test). */
39 void
40 primary_reference (void)
42 printf ("INFO: Called primary_reference function.\n");
45 void
46 primary (void)
48 char *error;
50 plugin_lib = dlopen (LIB_PLUGIN, RTLD_NOW | RTLD_LOCAL | RTLD_NODELETE);
51 if (plugin_lib == NULL)
53 printf ("ERROR: Unable to load plugin library.\n");
54 exit (EXIT_FAILURE);
56 dlerror ();
58 plugin_func = (void (*) (void)) dlsym (plugin_lib, "plugin_func");
59 error = dlerror ();
60 if (error != NULL)
62 printf ("ERROR: Unable to find symbol with error \"%s\".",
63 error);
64 exit (EXIT_FAILURE);
67 return;
70 __attribute__ ((destructor))
71 static void
72 primary_dtor (void)
74 int ret;
76 printf ("INFO: Calling primary destructor.\n");
78 /* The destructor runs in the test driver also, which
79 hasn't called primary, in that case do nothing. */
80 if (plugin_lib == NULL)
81 return;
83 ret = dlclose (plugin_lib);
84 if (ret != 0)
86 printf ("ERROR: Calling dlclose failed with \"%s\"\n",
87 dlerror ());
88 exit (EXIT_FAILURE);