1/* Call the termination functions of loaded shared objects.
2 Copyright (C) 1995-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19#include <assert.h>
20#include <string.h>
21#include <ldsodefs.h>
22#include <elf-initfini.h>
23
24void
25_dl_fini (void)
26{
27 /* Lots of fun ahead. We have to call the destructors for all still
28 loaded objects, in all namespaces. The problem is that the ELF
29 specification now demands that dependencies between the modules
30 are taken into account. I.e., the destructor for a module is
31 called before the ones for any of its dependencies.
32
33 To make things more complicated, we cannot simply use the reverse
34 order of the constructors. Since the user might have loaded objects
35 using `dlopen' there are possibly several other modules with its
36 dependencies to be taken into account. Therefore we have to start
37 determining the order of the modules once again from the beginning. */
38
39 /* We run the destructors of the main namespaces last. As for the
40 other namespaces, we pick run the destructors in them in reverse
41 order of the namespace ID. */
42#ifdef SHARED
43 int do_audit = 0;
44 again:
45#endif
46 for (Lmid_t ns = GL(dl_nns) - 1; ns >= 0; --ns)
47 {
48 /* Protect against concurrent loads and unloads. */
49 __rtld_lock_lock_recursive (GL(dl_load_lock));
50
51 unsigned int nloaded = GL(dl_ns)[ns]._ns_nloaded;
52 /* No need to do anything for empty namespaces or those used for
53 auditing DSOs. */
54 if (nloaded == 0
55#ifdef SHARED
56 || GL(dl_ns)[ns]._ns_loaded->l_auditing != do_audit
57#endif
58 )
59 __rtld_lock_unlock_recursive (GL(dl_load_lock));
60 else
61 {
62#ifdef SHARED
63 _dl_audit_activity_nsid (ns, LA_ACT_DELETE);
64#endif
65
66 /* Now we can allocate an array to hold all the pointers and
67 copy the pointers in. */
68 struct link_map *maps[nloaded];
69
70 unsigned int i;
71 struct link_map *l;
72 struct link_map *proxy_link_map = NULL;
73 assert (nloaded != 0 || GL(dl_ns)[ns]._ns_loaded == NULL);
74 for (l = GL(dl_ns)[ns]._ns_loaded, i = 0; l != NULL; l = l->l_next)
75 /* Do not handle ld.so in secondary namespaces. */
76 if (l == l->l_real)
77 {
78 assert (i < nloaded);
79
80 maps[i] = l;
81 l->l_idx = i;
82 ++i;
83
84 /* Bump l_direct_opencount of all objects so that they
85 are not dlclose()ed from underneath us. */
86 ++l->l_direct_opencount;
87 }
88 else
89 /* Used below to call la_objclose for the ld.so proxy
90 link map. */
91 proxy_link_map = l;
92
93 assert (ns != LM_ID_BASE || i == nloaded);
94 assert (ns == LM_ID_BASE || i == nloaded || i == nloaded - 1);
95 unsigned int nmaps = i;
96
97 /* Now we have to do the sorting. We can skip looking for the
98 binary itself which is at the front of the search list for
99 the main namespace. */
100 _dl_sort_maps (maps, nmaps, force_first: (ns == LM_ID_BASE), true);
101
102 /* We do not rely on the linked list of loaded object anymore
103 from this point on. We have our own list here (maps). The
104 various members of this list cannot vanish since the open
105 count is too high and will be decremented in this loop. So
106 we release the lock so that some code which might be called
107 from a destructor can directly or indirectly access the
108 lock. */
109 __rtld_lock_unlock_recursive (GL(dl_load_lock));
110
111 /* 'maps' now contains the objects in the right order. Now
112 call the destructors. We have to process this array from
113 the front. */
114 for (i = 0; i < nmaps; ++i)
115 {
116 struct link_map *l = maps[i];
117
118 if (l->l_init_called)
119 {
120 _dl_call_fini (map: l);
121#ifdef SHARED
122 /* Auditing checkpoint: another object closed. */
123 _dl_audit_objclose (l);
124#endif
125 }
126
127 /* Correct the previous increment. */
128 --l->l_direct_opencount;
129 }
130
131 if (proxy_link_map != NULL)
132 _dl_audit_objclose (l: proxy_link_map);
133
134#ifdef SHARED
135 _dl_audit_activity_nsid (ns, LA_ACT_CONSISTENT);
136#endif
137 }
138 }
139
140#ifdef SHARED
141 if (! do_audit && GLRO(dl_naudit) > 0)
142 {
143 do_audit = 1;
144 goto again;
145 }
146
147 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS))
148 _dl_debug_printf ("\nruntime linker statistics:\n"
149 " final number of relocations: %lu\n"
150 "final number of relocations from cache: %lu\n",
151 GL(dl_num_relocations),
152 GL(dl_num_cache_relocations));
153#endif
154}
155

source code of glibc/elf/dl-fini.c