/* Locating objects in the process image. ld.so implementation.
Copyright (C) 2021-2023 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 <atomic.h>
#include <atomic_wide_counter.h>
#include <dl-find_object.h>
#include <dlfcn.h>
#include <ldsodefs.h>
#include <link.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
/* Fallback implementation of _dl_find_object. It uses a linear
search, needs locking, and is not async-signal-safe. It is used in
_dl_find_object prior to initialization, when called from audit
modules. It also serves as the reference implementation for
_dl_find_object. */
static int
_dl_find_object_slow (void *pc, struct dl_find_object *result)
{
ElfW(Addr) addr = (ElfW(Addr)) pc;
for (Lmid_t ns = 0; ns < GL(dl_nns); ++ns)
for (struct link_map *l = GL(dl_ns)[ns]._ns_loaded; l != NULL;
l = l->l_next)
if (addr >= l->l_map_start && addr < l->l_map_end
&& (l->l_contiguous || _dl_addr_inside_object (l, addr)))
{
assert (ns == l->l_ns);
struct dl_find_object_internal internal;
_dl_find_object_from_map (l, &internal);
_dl_find_object_to_external (&internal, result);
return 1;
}
/* Object not found. */
return -1;
}
/* Data for the main executable. There is usually a large gap between
the main executable and initially loaded shared objects. Record
the main executable separately, to increase the chance that the
range for the non-closeable mappings below covers only the shared
objects (and not also the gap bet