1/* Call the termination functions of loaded shared objects.
2 Copyright (C) 1995-2022 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 assert (nloaded != 0 || GL(dl_ns)[ns]._ns_loaded == NULL);
73 for (l = GL(dl_ns)[ns]._ns_loaded, i = 0; l != NULL; l = l->l_next)
74 /* Do not handle ld.so in secondary namespaces. */
75 if (l == l->l_real)
76 {
77 assert (i < nloaded);
78
79 maps[i] = l;
80 l->l_idx = i;
81 ++i;
82
83 /* Bump l_direct_opencount of all objects so that they
84 are not dlclose()ed from underneath us. */
85 ++l->l_direct_opencount;
86 }
87 assert (ns != LM_ID_BASE || i == nloaded);
88 assert (ns == LM_ID_BASE || i == nloaded || i == nloaded - 1);
89 unsigned int nmaps = i;
90
91 /* Now we have to do the sorting. We can skip looking for the
92 binary itself which is at the front of the search list for
93 the main namespace. */
94 _dl_sort_maps (maps, nmaps, force_first: (ns == LM_ID_BASE), true);
95
96 /* We do not rely on the linked list of loaded object anymore
97 from this point on. We have our own list here (maps). The
98 various members of this list cannot vanish since the open
99 count is too high and will be decremented in this loop. So
100 we release the lock so that some code which might be called
101 from a destructor can directly or indirectly access the
102 lock. */
103 __rtld_lock_unlock_recursive (GL(dl_load_lock));
104
105 /* 'maps' now contains the objects in the right order. Now
106 call the destructors. We have to process this array from
107 the front. */
108 for (i = 0; i < nmaps; ++i)
109 {
110 struct link_map *l = maps[i];
111
112 if (l->l_init_called)
113 {
114 _dl_call_fini (map: l);
115#ifdef SHARED
116 /* Auditing checkpoint: another object closed. */
117 _dl_audit_objclose (l);
118#endif
119 }
120
121 /* Correct the previous increment. */
122 --l->l_direct_opencount;
123 }
124
125#ifdef SHARED
126 _dl_audit_activity_nsid (ns, LA_ACT_CONSISTENT);
127#endif
128 }
129 }
130
131#ifdef SHARED
132 if (! do_audit && GLRO(dl_naudit) > 0)
133 {
134 do_audit = 1;
135 goto again;
136 }
137
138 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS))
139 _dl_debug_printf ("\nruntime linker statistics:\n"
140 " final number of relocations: %lu\n"
141 "final number of relocations from cache: %lu\n",
142 GL(dl_num_relocations),
143 GL(dl_num_cache_relocations));
144#endif
145}
146

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