/* Close a shared object opened by `_dl_open'.
Copyright (C) 1996-2025 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <assert.h>
#include <dlfcn.h>
#include <errno.h>
#include <libintl.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libc-lock.h>
#include <ldsodefs.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sysdep-cancel.h>
#include <tls.h>
#include <stap-probe.h>
#include <dl-find_object.h>
#include <dl-unmap-segments.h>
/* Special l_idx value used to indicate which objects remain loaded. */
#define IDX_STILL_USED -1
/* Returns true we an non-empty was found. */
static bool
remove_slotinfo (size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
bool should_be_there)
{
if (idx - disp >= listp->len)
{
if (listp->next == NULL)
{
/* The index is not actually valid in the slotinfo list,
because this object was closed before it was fully set
up due to some error. */
assert (! should_be_there);
}
else
{
if (remove_slotinfo (idx, listp->next, disp + listp->len,
should_be_there))
return true;
/* No non-empty entry. Search from the end of this element's
slotinfo array. */
idx = disp + listp->len;
}
}
else
{
struct link_map *old_map = listp->slotinfo[idx - disp].map;
/* The entry might still be in its unused state if we are closing an
object that wasn't fully set up. */
if (__glibc_likely (old_map != NULL))
{
/* Mark the entry as unused. These can be read concurrently. */
atomic_store_relaxed (&listp->slotinfo[idx - disp].gen,
GL(dl_tls_generation) + 1);
atomic_store_relaxed (&listp->slotinfo[idx - disp].map, NULL);
}
/* If this is not the last currently used entry no need to look
further. */
if (idx != GL(dl_tls_max_dtv_idx))
{
/* There is an unused dtv entry in the middle. */
GL(dl_tls_dtv_gaps) = true;
return true;
}
}
while (idx -