]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-close.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / elf / dl-close.c
CommitLineData
26b4d766 1/* Close a shared object opened by `_dl_open'.
04277e02 2 Copyright (C) 1996-2019 Free Software Foundation, Inc.
afd4eb37
UD
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
41bdb6e2
AJ
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.
afd4eb37
UD
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
41bdb6e2 13 Lesser General Public License for more details.
afd4eb37 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
ba79d61b 18
7afab53d 19#include <assert.h>
ba79d61b 20#include <dlfcn.h>
1100f849 21#include <errno.h>
8e17ea58 22#include <libintl.h>
bfc832cc 23#include <stddef.h>
b209e34a 24#include <stdio.h>
ba79d61b 25#include <stdlib.h>
8d6468d0 26#include <string.h>
9dcafc55 27#include <unistd.h>
ec999b8e 28#include <libc-lock.h>
b8445829 29#include <ldsodefs.h>
ba79d61b
RM
30#include <sys/types.h>
31#include <sys/mman.h>
609cf614 32#include <sysdep-cancel.h>
df94b641 33#include <tls.h>
815e6fa3 34#include <stap-probe.h>
ba79d61b 35
fcccd512
RM
36#include <dl-unmap-segments.h>
37
ba79d61b 38
dacc8ffa
UD
39/* Type of the constructor functions. */
40typedef void (*fini_t) (void);
41
42
1100f849
UD
43/* Special l_idx value used to indicate which objects remain loaded. */
44#define IDX_STILL_USED -1
45
46
fc093be1
UD
47/* Returns true we an non-empty was found. */
48static bool
1f0c4a10
RM
49remove_slotinfo (size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
50 bool should_be_there)
fc093be1
UD
51{
52 if (idx - disp >= listp->len)
53 {
1f0c4a10
RM
54 if (listp->next == NULL)
55 {
56 /* The index is not actually valid in the slotinfo list,
8265947d
RM
57 because this object was closed before it was fully set
58 up due to some error. */
1f0c4a10
RM
59 assert (! should_be_there);
60 }
61 else
62 {
63 if (remove_slotinfo (idx, listp->next, disp + listp->len,
64 should_be_there))
65 return true;
fc093be1 66
1f0c4a10
RM
67 /* No non-empty entry. Search from the end of this element's
68 slotinfo array. */
69 idx = disp + listp->len;
70 }
fc093be1
UD
71 }
72 else
73 {
74 struct link_map *old_map = listp->slotinfo[idx - disp].map;
fc093be1 75
2430d57a
RM
76 /* The entry might still be in its unused state if we are closing an
77 object that wasn't fully set up. */
a1ffb40e 78 if (__glibc_likely (old_map != NULL))
2430d57a
RM
79 {
80 assert (old_map->l_tls_modid == idx);
81
82 /* Mark the entry as unused. */
83 listp->slotinfo[idx - disp].gen = GL(dl_tls_generation) + 1;
84 listp->slotinfo[idx - disp].map = NULL;
85 }
fc093be1
UD
86
87 /* If this is not the last currently used entry no need to look
88 further. */
2430d57a 89 if (idx != GL(dl_tls_max_dtv_idx))
fc093be1 90 return true;
fc093be1
UD
91 }
92
06a04e09 93 while (idx - disp > (disp == 0 ? 1 + GL(dl_tls_static_nelem) : 0))
fc093be1
UD
94 {
95 --idx;
96
97 if (listp->slotinfo[idx - disp].map != NULL)
98 {
99 /* Found a new last used index. */
100 GL(dl_tls_max_dtv_idx) = idx;
101 return true;
102 }
103 }
104
105 /* No non-entry in this list element. */
106 return false;
107}
fc093be1
UD
108
109
ba79d61b 110void
02d5e5d9 111_dl_close_worker (struct link_map *map, bool force)
ba79d61b 112{
c0f62c56 113 /* One less direct use. */
c0f62c56
UD
114 --map->l_direct_opencount;
115
bfc832cc
UD
116 /* If _dl_close is called recursively (some destructor call dlclose),
117 just record that the parent _dl_close will need to do garbage collection
118 again and return. */
119 static enum { not_pending, pending, rerun } dl_close_state;
120
121 if (map->l_direct_opencount > 0 || map->l_type != lt_loaded
122 || dl_close_state != not_pending)
26b4d766 123 {
0479b305
AS
124 if (map->l_direct_opencount == 0 && map->l_type == lt_loaded)
125 dl_close_state = rerun;
bfc832cc 126
26b4d766 127 /* There are still references to this object. Do nothing more. */
a1ffb40e 128 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
20fe49b9
UD
129 _dl_debug_printf ("\nclosing file=%s; direct_opencount=%u\n",
130 map->l_name, map->l_direct_opencount);
a334319f 131
26b4d766
UD
132 return;
133 }
ba79d61b 134
2e81d449
UD
135 Lmid_t nsid = map->l_ns;
136 struct link_namespaces *ns = &GL(dl_ns)[nsid];
137
bfc832cc
UD
138 retry:
139 dl_close_state = pending;
140
bfc832cc 141 bool any_tls = false;
2e81d449 142 const unsigned int nloaded = ns->_ns_nloaded;
c3381f3e
UD
143 char used[nloaded];
144 char done[nloaded];
20fe49b9
UD
145 struct link_map *maps[nloaded];
146
f25238ff
MO
147 /* Clear DF_1_NODELETE to force object deletion. We don't need to touch
148 l_tls_dtor_count because forced object deletion only happens when an
149 error occurs during object load. Destructor registration for TLS
150 non-POD objects should not have happened till then for this
151 object. */
152 if (force)
153 map->l_flags_1 &= ~DF_1_NODELETE;
154
bfc832cc 155 /* Run over the list and assign indexes to the link maps and enter
20fe49b9
UD
156 them into the MAPS array. */
157 int idx = 0;
2e81d449 158 for (struct link_map *l = ns->_ns_loaded; l != NULL; l = l->l_next)
20fe49b9
UD
159 {
160 l->l_idx = idx;
161 maps[idx] = l;
162 ++idx;
02d5e5d9 163
20fe49b9
UD
164 }
165 assert (idx == nloaded);
c4bb124a 166
20fe49b9
UD
167 /* Prepare the bitmaps. */
168 memset (used, '\0', sizeof (used));
169 memset (done, '\0', sizeof (done));
0ecb606c 170
20fe49b9
UD
171 /* Keep track of the lowest index link map we have covered already. */
172 int done_index = -1;
173 while (++done_index < nloaded)
0ecb606c 174 {
20fe49b9
UD
175 struct link_map *l = maps[done_index];
176
c3381f3e 177 if (done[done_index])
20fe49b9
UD
178 /* Already handled. */
179 continue;
180
181 /* Check whether this object is still used. */
182 if (l->l_type == lt_loaded
183 && l->l_direct_opencount == 0
184 && (l->l_flags_1 & DF_1_NODELETE) == 0
90b37cac
SP
185 /* See CONCURRENCY NOTES in cxa_thread_atexit_impl.c to know why
186 acquire is sufficient and correct. */
187 && atomic_load_acquire (&l->l_tls_dtor_count) == 0
c3381f3e 188 && !used[done_index])
20fe49b9
UD
189 continue;
190
191 /* We need this object and we handle it now. */
c3381f3e
UD
192 done[done_index] = 1;
193 used[done_index] = 1;
194 /* Signal the object is still needed. */
1100f849 195 l->l_idx = IDX_STILL_USED;
20fe49b9
UD
196
197 /* Mark all dependencies as used. */
198 if (l->l_initfini != NULL)
199 {
36129722
CD
200 /* We are always the zeroth entry, and since we don't include
201 ourselves in the dependency analysis start at 1. */
20fe49b9
UD
202 struct link_map **lp = &l->l_initfini[1];
203 while (*lp != NULL)
204 {
1100f849 205 if ((*lp)->l_idx != IDX_STILL_USED)
556224ab 206 {
c3381f3e
UD
207 assert ((*lp)->l_idx >= 0 && (*lp)->l_idx < nloaded);
208
209 if (!used[(*lp)->l_idx])
210 {
211 used[(*lp)->l_idx] = 1;
36129722
CD
212 /* If we marked a new object as used, and we've
213 already processed it, then we need to go back
214 and process again from that point forward to
215 ensure we keep all of its dependencies also. */
c3381f3e
UD
216 if ((*lp)->l_idx - 1 < done_index)
217 done_index = (*lp)->l_idx - 1;
218 }
556224ab 219 }
556224ab 220
20fe49b9
UD
221 ++lp;
222 }
223 }
224 /* And the same for relocation dependencies. */
225 if (l->l_reldeps != NULL)
385b4cf4 226 for (unsigned int j = 0; j < l->l_reldeps->act; ++j)
20fe49b9 227 {
385b4cf4 228 struct link_map *jmap = l->l_reldeps->list[j];
20fe49b9 229
1100f849 230 if (jmap->l_idx != IDX_STILL_USED)
556224ab 231 {
c3381f3e
UD
232 assert (jmap->l_idx >= 0 && jmap->l_idx < nloaded);
233
234 if (!used[jmap->l_idx])
235 {
236 used[jmap->l_idx] = 1;
237 if (jmap->l_idx - 1 < done_index)
238 done_index = jmap->l_idx - 1;
239 }
556224ab
UD
240 }
241 }
20fe49b9 242 }
42c4f32a 243
c2c299fd
AS
244 /* Sort the entries. We can skip looking for the binary itself which is
245 at the front of the search list for the main namespace. */
246 _dl_sort_maps (maps + (nsid == LM_ID_BASE), nloaded - (nsid == LM_ID_BASE),
247 used + (nsid == LM_ID_BASE), true);
c3381f3e 248
a709dd43 249 /* Call all termination functions at once. */
29f97654 250#ifdef SHARED
2e81d449 251 bool do_audit = GLRO(dl_naudit) > 0 && !ns->_ns_loaded->l_auditing;
29f97654 252#endif
20fe49b9 253 bool unload_any = false;
e4eb675d 254 bool scope_mem_left = false;
e8b6b64d 255 unsigned int unload_global = 0;
20fe49b9 256 unsigned int first_loaded = ~0;
ffd0e1b7 257 for (unsigned int i = 0; i < nloaded; ++i)
a709dd43 258 {
20fe49b9 259 struct link_map *imap = maps[i];
9dcafc55
UD
260
261 /* All elements must be in the same namespace. */
2e81d449 262 assert (imap->l_ns == nsid);
9dcafc55 263
c3381f3e 264 if (!used[i])
a709dd43 265 {
20fe49b9
UD
266 assert (imap->l_type == lt_loaded
267 && (imap->l_flags_1 & DF_1_NODELETE) == 0);
268
aff4519d
UD
269 /* Call its termination function. Do not do it for
270 half-cooked objects. */
271 if (imap->l_init_called)
dacc8ffa 272 {
ac53c9c6
UD
273 /* When debugging print a message first. */
274 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS,
275 0))
276 _dl_debug_printf ("\ncalling fini: %s [%lu]\n\n",
2e81d449 277 imap->l_name, nsid);
ac53c9c6 278
aff4519d
UD
279 if (imap->l_info[DT_FINI_ARRAY] != NULL)
280 {
281 ElfW(Addr) *array =
282 (ElfW(Addr) *) (imap->l_addr
283 + imap->l_info[DT_FINI_ARRAY]->d_un.d_ptr);
284 unsigned int sz = (imap->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
285 / sizeof (ElfW(Addr)));
aff4519d 286
62f29da7
UD
287 while (sz-- > 0)
288 ((fini_t) array[sz]) ();
aff4519d
UD
289 }
290
291 /* Next try the old-style destructor. */
292 if (imap->l_info[DT_FINI] != NULL)
daf75146
GM
293 DL_CALL_DT_FINI (imap, ((void *) imap->l_addr
294 + imap->l_info[DT_FINI]->d_un.d_ptr));
dacc8ffa
UD
295 }
296
9dcafc55 297#ifdef SHARED
a3d731d3 298 /* Auditing checkpoint: we remove an object. */
a1ffb40e 299 if (__glibc_unlikely (do_audit))
9dcafc55
UD
300 {
301 struct audit_ifaces *afct = GLRO(dl_audit);
302 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
303 {
304 if (afct->objclose != NULL)
305 /* Return value is ignored. */
306 (void) afct->objclose (&imap->l_audit[cnt].cookie);
307
308 afct = afct->next;
309 }
310 }
311#endif
312
20fe49b9
UD
313 /* This object must not be used anymore. */
314 imap->l_removed = 1;
aff4519d 315
20fe49b9
UD
316 /* We indeed have an object to remove. */
317 unload_any = true;
aff4519d 318
e8b6b64d
UD
319 if (imap->l_global)
320 ++unload_global;
321
20fe49b9
UD
322 /* Remember where the first dynamically loaded object is. */
323 if (i < first_loaded)
324 first_loaded = i;
a709dd43 325 }
c3381f3e 326 /* Else used[i]. */
20fe49b9
UD
327 else if (imap->l_type == lt_loaded)
328 {
1100f849
UD
329 struct r_scope_elem *new_list = NULL;
330
331 if (imap->l_searchlist.r_list == NULL && imap->l_initfini != NULL)
20fe49b9 332 {
bfc832cc 333 /* The object is still used. But one of the objects we are
20fe49b9
UD
334 unloading right now is responsible for loading it. If
335 the current object does not have it's own scope yet we
336 have to create one. This has to be done before running
337 the finalizers.
338
339 To do this count the number of dependencies. */
340 unsigned int cnt;
341 for (cnt = 1; imap->l_initfini[cnt] != NULL; ++cnt)
342 ;
343
344 /* We simply reuse the l_initfini list. */
345 imap->l_searchlist.r_list = &imap->l_initfini[cnt + 1];
346 imap->l_searchlist.r_nlist = cnt;
347
1100f849 348 new_list = &imap->l_searchlist;
20fe49b9 349 }
1100f849
UD
350
351 /* Count the number of scopes which remain after the unload.
352 When we add the local search list count it. Always add
353 one for the terminating NULL pointer. */
354 size_t remain = (new_list != NULL) + 1;
355 bool removed_any = false;
c0a777e8 356 for (size_t cnt = 0; imap->l_scope[cnt] != NULL; ++cnt)
1100f849
UD
357 /* This relies on l_scope[] entries being always set either
358 to its own l_symbolic_searchlist address, or some map's
359 l_searchlist address. */
c0a777e8 360 if (imap->l_scope[cnt] != &imap->l_symbolic_searchlist)
1100f849
UD
361 {
362 struct link_map *tmap = (struct link_map *)
c0a777e8 363 ((char *) imap->l_scope[cnt]
1100f849 364 - offsetof (struct link_map, l_searchlist));
2e81d449 365 assert (tmap->l_ns == nsid);
1100f849
UD
366 if (tmap->l_idx == IDX_STILL_USED)
367 ++remain;
368 else
369 removed_any = true;
370 }
371 else
372 ++remain;
373
374 if (removed_any)
1ee2ff20 375 {
1100f849
UD
376 /* Always allocate a new array for the scope. This is
377 necessary since we must be able to determine the last
378 user of the current array. If possible use the link map's
379 memory. */
380 size_t new_size;
c0a777e8
UD
381 struct r_scope_elem **newp;
382
383#define SCOPE_ELEMS(imap) \
384 (sizeof (imap->l_scope_mem) / sizeof (imap->l_scope_mem[0]))
385
386 if (imap->l_scope != imap->l_scope_mem
387 && remain < SCOPE_ELEMS (imap))
1100f849 388 {
c0a777e8
UD
389 new_size = SCOPE_ELEMS (imap);
390 newp = imap->l_scope_mem;
1100f849
UD
391 }
392 else
393 {
394 new_size = imap->l_scope_max;
c0a777e8
UD
395 newp = (struct r_scope_elem **)
396 malloc (new_size * sizeof (struct r_scope_elem *));
1100f849
UD
397 if (newp == NULL)
398 _dl_signal_error (ENOMEM, "dlclose", NULL,
399 N_("cannot create scope list"));
400 }
401
1100f849
UD
402 /* Copy over the remaining scope elements. */
403 remain = 0;
c0a777e8 404 for (size_t cnt = 0; imap->l_scope[cnt] != NULL; ++cnt)
1ee2ff20 405 {
c0a777e8 406 if (imap->l_scope[cnt] != &imap->l_symbolic_searchlist)
1ee2ff20 407 {
1100f849 408 struct link_map *tmap = (struct link_map *)
c0a777e8 409 ((char *) imap->l_scope[cnt]
1100f849
UD
410 - offsetof (struct link_map, l_searchlist));
411 if (tmap->l_idx != IDX_STILL_USED)
412 {
413 /* Remove the scope. Or replace with own map's
414 scope. */
415 if (new_list != NULL)
416 {
c0a777e8 417 newp[remain++] = new_list;
1100f849
UD
418 new_list = NULL;
419 }
420 continue;
421 }
1ee2ff20 422 }
1100f849 423
c0a777e8 424 newp[remain++] = imap->l_scope[cnt];
1ee2ff20 425 }
c0a777e8 426 newp[remain] = NULL;
1100f849 427
c0a777e8 428 struct r_scope_elem **old = imap->l_scope;
1100f849 429
e4eb675d 430 imap->l_scope = newp;
1100f849
UD
431
432 /* No user anymore, we can free it now. */
c0a777e8 433 if (old != imap->l_scope_mem)
e4eb675d
UD
434 {
435 if (_dl_scope_free (old))
436 /* If _dl_scope_free used THREAD_GSCOPE_WAIT (),
437 no need to repeat it. */
438 scope_mem_left = false;
439 }
440 else
441 scope_mem_left = true;
1100f849
UD
442
443 imap->l_scope_max = new_size;
1ee2ff20 444 }
39dd69df
AS
445 else if (new_list != NULL)
446 {
447 /* We didn't change the scope array, so reset the search
448 list. */
449 imap->l_searchlist.r_list = NULL;
450 imap->l_searchlist.r_nlist = 0;
451 }
42c4f32a 452
c3381f3e 453 /* The loader is gone, so mark the object as not having one.
1100f849
UD
454 Note: l_idx != IDX_STILL_USED -> object will be removed. */
455 if (imap->l_loader != NULL
456 && imap->l_loader->l_idx != IDX_STILL_USED)
20fe49b9 457 imap->l_loader = NULL;
aff4519d 458
20fe49b9
UD
459 /* Remember where the first dynamically loaded object is. */
460 if (i < first_loaded)
461 first_loaded = i;
462 }
a709dd43
UD
463 }
464
20fe49b9
UD
465 /* If there are no objects to unload, do nothing further. */
466 if (!unload_any)
467 goto out;
468
9dcafc55
UD
469#ifdef SHARED
470 /* Auditing checkpoint: we will start deleting objects. */
a1ffb40e 471 if (__glibc_unlikely (do_audit))
9dcafc55 472 {
2e81d449 473 struct link_map *head = ns->_ns_loaded;
9dcafc55
UD
474 struct audit_ifaces *afct = GLRO(dl_audit);
475 /* Do not call the functions for any auditing object. */
476 if (head->l_auditing == 0)
477 {
478 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
479 {
480 if (afct->activity != NULL)
481 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_DELETE);
482
483 afct = afct->next;
484 }
485 }
486 }
487#endif
488
4d6acc61 489 /* Notify the debugger we are about to remove some loaded objects. */
2e81d449 490 struct r_debug *r = _dl_debug_initialize (0, nsid);
9dcafc55
UD
491 r->r_state = RT_DELETE;
492 _dl_debug_state ();
815e6fa3 493 LIBC_PROBE (unmap_start, 2, nsid, r);
4d6acc61 494
e8b6b64d
UD
495 if (unload_global)
496 {
497 /* Some objects are in the global scope list. Remove them. */
498 struct r_scope_elem *ns_msl = ns->_ns_main_searchlist;
499 unsigned int i;
500 unsigned int j = 0;
501 unsigned int cnt = ns_msl->r_nlist;
502
503 while (cnt > 0 && ns_msl->r_list[cnt - 1]->l_removed)
504 --cnt;
505
506 if (cnt + unload_global == ns_msl->r_nlist)
507 /* Speed up removing most recently added objects. */
508 j = cnt;
509 else
b7c08a66 510 for (i = 0; i < cnt; i++)
e8b6b64d
UD
511 if (ns_msl->r_list[i]->l_removed == 0)
512 {
513 if (i != j)
514 ns_msl->r_list[j] = ns_msl->r_list[i];
515 j++;
516 }
517 ns_msl->r_nlist = j;
e4eb675d 518 }
e8b6b64d 519
e4eb675d
UD
520 if (!RTLD_SINGLE_THREAD_P
521 && (unload_global
522 || scope_mem_left
523 || (GL(dl_scope_free_list) != NULL
524 && GL(dl_scope_free_list)->count)))
525 {
526 THREAD_GSCOPE_WAIT ();
527
528 /* Now we can free any queued old scopes. */
385b4cf4 529 struct dl_scope_free_list *fsl = GL(dl_scope_free_list);
e4eb675d
UD
530 if (fsl != NULL)
531 while (fsl->count > 0)
532 free (fsl->list[--fsl->count]);
e8b6b64d
UD
533 }
534
541765b6
UD
535 size_t tls_free_start;
536 size_t tls_free_end;
537 tls_free_start = tls_free_end = NO_TLS_OFFSET;
c877418f 538
5a2a1d75
AS
539 /* We modify the list of loaded objects. */
540 __rtld_lock_lock_recursive (GL(dl_load_write_lock));
541
ba79d61b
RM
542 /* Check each element of the search list to see if all references to
543 it are gone. */
ffd0e1b7 544 for (unsigned int i = first_loaded; i < nloaded; ++i)
ba79d61b 545 {
20fe49b9 546 struct link_map *imap = maps[i];
c3381f3e 547 if (!used[i])
ba79d61b 548 {
20fe49b9 549 assert (imap->l_type == lt_loaded);
a8a1269d 550
ba79d61b
RM
551 /* That was the last reference, and this was a dlopen-loaded
552 object. We can unmap it. */
ba79d61b 553
1f0c4a10 554 /* Remove the object from the dtv slotinfo array if it uses TLS. */
a1ffb40e 555 if (__glibc_unlikely (imap->l_tls_blocksize > 0))
a04586d8 556 {
a04586d8 557 any_tls = true;
bb4cb252 558
9dcafc55
UD
559 if (GL(dl_tls_dtv_slotinfo_list) != NULL
560 && ! remove_slotinfo (imap->l_tls_modid,
561 GL(dl_tls_dtv_slotinfo_list), 0,
562 imap->l_init_called))
fc093be1
UD
563 /* All dynamically loaded modules with TLS are unloaded. */
564 GL(dl_tls_max_dtv_idx) = GL(dl_tls_static_nelem);
c877418f 565
4c533566
UD
566 if (imap->l_tls_offset != NO_TLS_OFFSET
567 && imap->l_tls_offset != FORCED_DYNAMIC_TLS_OFFSET)
c877418f
RM
568 {
569 /* Collect a contiguous chunk built from the objects in
570 this search list, going in either direction. When the
571 whole chunk is at the end of the used area then we can
572 reclaim it. */
11bf311e 573#if TLS_TCB_AT_TP
541765b6
UD
574 if (tls_free_start == NO_TLS_OFFSET
575 || (size_t) imap->l_tls_offset == tls_free_start)
576 {
577 /* Extend the contiguous chunk being reclaimed. */
578 tls_free_start
579 = imap->l_tls_offset - imap->l_tls_blocksize;
580
581 if (tls_free_end == NO_TLS_OFFSET)
582 tls_free_end = imap->l_tls_offset;
583 }
584 else if (imap->l_tls_offset - imap->l_tls_blocksize
585 == tls_free_end)
586 /* Extend the chunk backwards. */
587 tls_free_end = imap->l_tls_offset;
588 else
589 {
590 /* This isn't contiguous with the last chunk freed.
591 One of them will be leaked unless we can free
592 one block right away. */
593 if (tls_free_end == GL(dl_tls_static_used))
594 {
595 GL(dl_tls_static_used) = tls_free_start;
596 tls_free_end = imap->l_tls_offset;
597 tls_free_start
598 = tls_free_end - imap->l_tls_blocksize;
599 }
600 else if ((size_t) imap->l_tls_offset
601 == GL(dl_tls_static_used))
602 GL(dl_tls_static_used)
603 = imap->l_tls_offset - imap->l_tls_blocksize;
604 else if (tls_free_end < (size_t) imap->l_tls_offset)
605 {
606 /* We pick the later block. It has a chance to
607 be freed. */
608 tls_free_end = imap->l_tls_offset;
609 tls_free_start
610 = tls_free_end - imap->l_tls_blocksize;
611 }
612 }
11bf311e 613#elif TLS_DTV_AT_TP
66bdbaa4
AM
614 if (tls_free_start == NO_TLS_OFFSET)
615 {
616 tls_free_start = imap->l_tls_firstbyte_offset;
617 tls_free_end = (imap->l_tls_offset
618 + imap->l_tls_blocksize);
619 }
620 else if (imap->l_tls_firstbyte_offset == tls_free_end)
c877418f 621 /* Extend the contiguous chunk being reclaimed. */
66bdbaa4 622 tls_free_end = imap->l_tls_offset + imap->l_tls_blocksize;
c877418f
RM
623 else if (imap->l_tls_offset + imap->l_tls_blocksize
624 == tls_free_start)
625 /* Extend the chunk backwards. */
66bdbaa4
AM
626 tls_free_start = imap->l_tls_firstbyte_offset;
627 /* This isn't contiguous with the last chunk freed.
628 One of them will be leaked unless we can free
629 one block right away. */
630 else if (imap->l_tls_offset + imap->l_tls_blocksize
631 == GL(dl_tls_static_used))
632 GL(dl_tls_static_used) = imap->l_tls_firstbyte_offset;
633 else if (tls_free_end == GL(dl_tls_static_used))
c877418f 634 {
66bdbaa4
AM
635 GL(dl_tls_static_used) = tls_free_start;
636 tls_free_start = imap->l_tls_firstbyte_offset;
637 tls_free_end = imap->l_tls_offset + imap->l_tls_blocksize;
638 }
639 else if (tls_free_end < imap->l_tls_firstbyte_offset)
640 {
641 /* We pick the later block. It has a chance to
642 be freed. */
643 tls_free_start = imap->l_tls_firstbyte_offset;
644 tls_free_end = imap->l_tls_offset + imap->l_tls_blocksize;
c877418f 645 }
11bf311e
UD
646#else
647# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
648#endif
c877418f 649 }
a04586d8 650 }
a04586d8 651
02d5e5d9
PK
652 /* Reset unique symbols if forced. */
653 if (force)
654 {
655 struct unique_sym_table *tab = &ns->_ns_unique_sym_table;
656 __rtld_lock_lock_recursive (tab->lock);
657 struct unique_sym *entries = tab->entries;
658 if (entries != NULL)
659 {
660 size_t idx, size = tab->size;
661 for (idx = 0; idx < size; ++idx)
662 {
663 /* Clear unique symbol entries that belong to this
664 object. */
665 if (entries[idx].name != NULL
666 && entries[idx].map == imap)
667 {
668 entries[idx].name = NULL;
669 entries[idx].hashval = 0;
670 tab->n_elements--;
671 }
672 }
673 }
674 __rtld_lock_unlock_recursive (tab->lock);
675 }
676
a8a1269d 677 /* We can unmap all the maps at once. We determined the
4ce636da
UD
678 start address and length when we loaded the object and
679 the `munmap' call does the rest. */
09bf6406 680 DL_UNMAP (imap);
22bc7978 681
ba79d61b 682 /* Finally, unlink the data structure and free it. */
2bd2cad9
RM
683#if DL_NNS == 1
684 /* The assert in the (imap->l_prev == NULL) case gives
685 the compiler license to warn that NS points outside
686 the dl_ns array bounds in that case (as nsid != LM_ID_BASE
687 is tantamount to nsid >= DL_NNS). That should be impossible
688 in this configuration, so just assert about it instead. */
689 assert (nsid == LM_ID_BASE);
690 assert (imap->l_prev != NULL);
691#else
692 if (imap->l_prev == NULL)
c0f62c56 693 {
2e81d449 694 assert (nsid != LM_ID_BASE);
b7c08a66
RM
695 ns->_ns_loaded = imap->l_next;
696
697 /* Update the pointer to the head of the list
698 we leave for debuggers to examine. */
699 r->r_map = (void *) ns->_ns_loaded;
c0f62c56 700 }
2bd2cad9
RM
701 else
702#endif
703 imap->l_prev->l_next = imap->l_next;
c0f62c56 704
2e81d449 705 --ns->_ns_nloaded;
c0f62c56 706 if (imap->l_next != NULL)
af69217f 707 imap->l_next->l_prev = imap->l_prev;
a8a1269d 708
556224ab
UD
709 free (imap->l_versions);
710 if (imap->l_origin != (char *) -1)
a8a1269d
UD
711 free ((char *) imap->l_origin);
712
20fe49b9 713 free (imap->l_reldeps);
4b4fcf99 714
ac53c9c6 715 /* Print debugging message. */
a1ffb40e 716 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
ac53c9c6
UD
717 _dl_debug_printf ("\nfile=%s [%lu]; destroying link map\n",
718 imap->l_name, imap->l_ns);
719
4ce636da 720 /* This name always is allocated. */
a8a1269d 721 free (imap->l_name);
4ce636da 722 /* Remove the list with all the names of the shared object. */
20fe49b9
UD
723
724 struct libname_list *lnp = imap->l_libname;
a8a1269d
UD
725 do
726 {
76156ea1 727 struct libname_list *this = lnp;
a8a1269d 728 lnp = lnp->next;
11810621
UD
729 if (!this->dont_free)
730 free (this);
a8a1269d
UD
731 }
732 while (lnp != NULL);
a8a1269d 733
4ce636da 734 /* Remove the searchlists. */
20fe49b9 735 free (imap->l_initfini);
4ce636da 736
5a21d307 737 /* Remove the scope array if we allocated it. */
c0a777e8
UD
738 if (imap->l_scope != imap->l_scope_mem)
739 free (imap->l_scope);
5a21d307 740
7bcaca43 741 if (imap->l_phdr_allocated)
15925412 742 free ((void *) imap->l_phdr);
7bcaca43 743
f55727ca
UD
744 if (imap->l_rpath_dirs.dirs != (void *) -1)
745 free (imap->l_rpath_dirs.dirs);
746 if (imap->l_runpath_dirs.dirs != (void *) -1)
747 free (imap->l_runpath_dirs.dirs);
748
af69217f 749 free (imap);
ba79d61b
RM
750 }
751 }
752
5a2a1d75
AS
753 __rtld_lock_unlock_recursive (GL(dl_load_write_lock));
754
c877418f 755 /* If we removed any object which uses TLS bump the generation counter. */
bb4cb252 756 if (any_tls)
c877418f 757 {
a1ffb40e 758 if (__glibc_unlikely (++GL(dl_tls_generation) == 0))
8b748aed 759 _dl_fatal_printf ("TLS generation counter wrapped! Please report as described in "REPORT_BUGS_TO".\n");
c877418f
RM
760
761 if (tls_free_end == GL(dl_tls_static_used))
762 GL(dl_tls_static_used) = tls_free_start;
763 }
a04586d8 764
9dcafc55
UD
765#ifdef SHARED
766 /* Auditing checkpoint: we have deleted all objects. */
a1ffb40e 767 if (__glibc_unlikely (do_audit))
9dcafc55 768 {
2e81d449 769 struct link_map *head = ns->_ns_loaded;
9dcafc55
UD
770 /* Do not call the functions for any auditing object. */
771 if (head->l_auditing == 0)
772 {
773 struct audit_ifaces *afct = GLRO(dl_audit);
774 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
775 {
776 if (afct->activity != NULL)
777 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_CONSISTENT);
778
779 afct = afct->next;
780 }
781 }
782 }
783#endif
784
22c83193
UD
785 if (__builtin_expect (ns->_ns_loaded == NULL, 0)
786 && nsid == GL(dl_nns) - 1)
787 do
0d23a5c1 788 --GL(dl_nns);
22c83193
UD
789 while (GL(dl_ns)[GL(dl_nns) - 1]._ns_loaded == NULL);
790
e3e5f672 791 /* Notify the debugger those objects are finalized and gone. */
9dcafc55
UD
792 r->r_state = RT_CONSISTENT;
793 _dl_debug_state ();
815e6fa3 794 LIBC_PROBE (unmap_complete, 2, nsid, r);
4b4fcf99 795
bfc832cc 796 /* Recheck if we need to retry, release the lock. */
20fe49b9 797 out:
bfc832cc
UD
798 if (dl_close_state == rerun)
799 goto retry;
800
801 dl_close_state = not_pending;
131c4428
UD
802}
803
804
805void
806_dl_close (void *_map)
807{
808 struct link_map *map = _map;
809
57707b7f
CD
810 /* We must take the lock to examine the contents of map and avoid
811 concurrent dlopens. */
812 __rtld_lock_lock_recursive (GL(dl_load_lock));
813
814 /* At this point we are guaranteed nobody else is touching the list of
815 loaded maps, but a concurrent dlclose might have freed our map
816 before we took the lock. There is no way to detect this (see below)
817 so we proceed assuming this isn't the case. First see whether we
818 can remove the object at all. */
a1ffb40e 819 if (__glibc_unlikely (map->l_flags_1 & DF_1_NODELETE))
131c4428 820 {
131c4428 821 /* Nope. Do nothing. */
57707b7f 822 __rtld_lock_unlock_recursive (GL(dl_load_lock));
131c4428
UD
823 return;
824 }
825
57707b7f
CD
826 /* At present this is an unreliable check except in the case where the
827 caller has recursively called dlclose and we are sure the link map
828 has not been freed. In a non-recursive dlclose the map itself
829 might have been freed and this access is potentially a data race
830 with whatever other use this memory might have now, or worse we
831 might silently corrupt memory if it looks enough like a link map.
832 POSIX has language in dlclose that appears to guarantee that this
833 should be a detectable case and given that dlclose should be threadsafe
834 we need this to be a reliable detection.
835 This is bug 20990. */
131c4428 836 if (__builtin_expect (map->l_direct_opencount, 1) == 0)
57707b7f
CD
837 {
838 __rtld_lock_unlock_recursive (GL(dl_load_lock));
839 _dl_signal_error (0, map->l_name, NULL, N_("shared object not open"));
840 }
131c4428 841
02d5e5d9 842 _dl_close_worker (map, false);
131c4428 843
d3c9f895 844 __rtld_lock_unlock_recursive (GL(dl_load_lock));
e3e5f672 845}