[PATCH v1 2/2] elf: Add la_objsearch2 with lmid parameter for rtld-audit [BZ #34093]
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Fri Nov 6 11:49:41 GMT 2026
On 27/10/26 07:45, Frédéric Bérat wrote:
> Add la_objsearch2 to the rtld-audit interface so auditors can identify
> the destination namespace of a library search.
>
> Prefer la_objsearch2 when an auditor defines both search callbacks, and
> retain la_objsearch as the fallback when the new callback is absent.
>
> Include the audit regression tests, including search-name and
> namespace checks in tst-audit29. Document the callback contract and
> older-linker fallback in the manual, and include the NEWS announcement.
>
> Set LAV_CURRENT to 3 across architectures while retaining the
> compatibility qualifications for LoongArch, where version 3 was already
> in use and does not by itself establish support for la_objsearch2.
> ---
> NEWS | 10 +
> bits/link_lavcurrent.h | 2 +-
> elf/Makefile | 12 +
> elf/dl-audit.c | 13 +-
> elf/dl-load.c | 37 +--
> elf/link.h | 2 +
> elf/rtld.c | 5 +-
> elf/tst-audit29.c | 268 ++++++++++++++++++
> elf/tst-audit30.c | 111 ++++++++
> elf/tst-auditmod29.c | 49 ++++
> .../link_lavcurrent.h => elf/tst-auditmod30.c | 31 +-
Maybe use --no-renames (and I need to double-check if I have this enable
by default as well).
> manual/dynlink.texi | 23 ++
> sysdeps/aarch64/dl-audit-check.h | 2 +-
> sysdeps/generic/ldsodefs.h | 8 +-
> sysdeps/loongarch/dl-audit-check.h | 4 +-
> 15 files changed, 540 insertions(+), 37 deletions(-)
> create mode 100644 elf/tst-audit29.c
> create mode 100644 elf/tst-audit30.c
> create mode 100644 elf/tst-auditmod29.c
> rename sysdeps/loongarch/bits/link_lavcurrent.h => elf/tst-auditmod30.c (56%)
>
> diff --git a/NEWS b/NEWS
> index 1d6d0c8e02..7d2d12c537 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -9,6 +9,16 @@ Version 2.45
>
> Major new features:
>
> +* The rtld-audit interface function la_objsearch2 has been added to
> + <link.h>. It provides the target namespace ID (Lmid_t lmid) where the
> + shared object is being loaded, allowing audit modules to monitor and
> + modify library searches per namespace. The LAV_CURRENT macro is now 3 on
> + all architectures; it was already 3 on LoongArch.
> + Existing audit modules returning version 2 remain supported on all
> + architectures except LoongArch (which requires version 3 due to vector
> + register layout additions in glibc 2.39). Audit API version 3 alone does
> + not establish support for la_objsearch2 on older LoongArch dynamic linkers.
> +
So maybe bump to 4 on all architectures, so that version >= 4 in la_version
means la_objsearch2 is available everywhere (and kkipping 3 elsewhere is
harmless). The range checks in this patch can stay as they are, and th
aarch64 one is needed with any bump. The LoongArch override can still be
removed.
> * Additional optimized and correctly rounded mathematical functions have
> been imported from the CORE-MATH project, in particular cbrt.
>
> diff --git a/bits/link_lavcurrent.h b/bits/link_lavcurrent.h
> index 699b1667f5..a988402735 100644
> --- a/bits/link_lavcurrent.h
> +++ b/bits/link_lavcurrent.h
> @@ -22,4 +22,4 @@
> #endif
>
> /* Version numbers for la_version handshake interface. */
> -#define LAV_CURRENT 2
> +#define LAV_CURRENT 3
> diff --git a/elf/Makefile b/elf/Makefile
> index 2c74e11946..f14c7242ce 100644
> --- a/elf/Makefile
> +++ b/elf/Makefile
> @@ -431,6 +431,8 @@ tests += \
> tst-audit25a \
> tst-audit25b \
> tst-audit28 \
> + tst-audit29 \
> + tst-audit30 \
> tst-auditmany \
> tst-auxobj \
> tst-auxobj-dlopen \
> @@ -967,6 +969,8 @@ modules-names += \
> tst-auditmod24d \
> tst-auditmod25 \
> tst-auditmod28 \
> + tst-auditmod29 \
> + tst-auditmod30 \
> tst-auditmod9a \
> tst-auditmod9b \
> tst-auxvalmod \
> @@ -2866,6 +2870,14 @@ $(objpfx)tst-audit28.out: $(objpfx)tst-auditmod28.so
> $(objpfx)tst-auditmod28.so: $(libsupport)
> tst-audit28-ENV = LD_AUDIT=$(objpfx)tst-auditmod28.so
>
> +$(objpfx)tst-audit29.out: $(objpfx)tst-auditmod29.so \
> + $(objpfx)tst-audit18mod.so
> +tst-audit29-ARGS = -- $(host-test-program-cmd)
> +
> +$(objpfx)tst-audit30.out: $(objpfx)tst-auditmod30.so \
> + $(objpfx)tst-audit18mod.so
> +tst-audit30-ARGS = -- $(host-test-program-cmd)
> +
> # tst-sonamemove links against an older implementation of the library.
> LDFLAGS-tst-sonamemove-linkmod1.so = \
> -Wl,--version-script=tst-sonamemove-linkmod1.map \
> diff --git a/elf/dl-audit.c b/elf/dl-audit.c
> index aadc8ecc79..9ae7cdc4c6 100644
> --- a/elf/dl-audit.c
> +++ b/elf/dl-audit.c
> @@ -51,7 +51,8 @@ _dl_audit_activity_nsid (Lmid_t nsid, int action)
> }
>
> const char *
> -_dl_audit_objsearch (const char *name, struct link_map *l, unsigned int code)
> +_dl_audit_objsearch (const char *name, struct link_map *l, Lmid_t nsid,
> + unsigned int code)
> {
> if (l == NULL || l->l_auditing || code == 0)
> return name;
The l == NULL check means dlmopen with a '/' in the name never reaches
the callback for the object itself. With this patch applied:
dlopen ("/abs/x.so") LA_SER_ORIG reported
dlmopen (LM_ID_BASE, "/abs/x.so") no callback
dlmopen (LM_ID_NEWLM, "/abs/x.so") no callback
dlmopen (ns, "./x.so") no callback
dlmopen (ns, "libm.so.6") LA_SER_ORIG reported, correct lmid
I suggest fixing this in a separate patch before this one:
--- a/elf/dl-open.c
+++ b/elf/dl-open.c
@@ -857,7 +857,11 @@
in case we have a DST, or when the file name has no path in
which case we need to look along the RUNPATH/RPATH of the caller. */
if (nsid == __LM_ID_CALLER || strchr (file, '$') != NULL
- || strchr (file, '/') == NULL)
+ || strchr (file, '/') == NULL
+#ifdef SHARED
+ || __glibc_unlikely (GLRO(dl_naudit) > 0)
+#endif
+ )
{
struct dl_find_object dlfo;
if (_dl_find_object ((void *) caller_dlopen, &dlfo) == 0)
With it, all the cases above report LA_SER_ORIG with the destination
lmid, and elf/tests still passes. Existing la_objsearch auditors will
start seeing these events too, which matches what dlopen already does.
Another possibility is not to change this, but extend the manual to make
explicit dlmopen calls make no search callback.
> @@ -59,9 +60,15 @@ _dl_audit_objsearch (const char *name, struct link_map *l, unsigned int code)
> struct audit_ifaces *afct = GLRO(dl_audit);
> for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
> {
> - if (afct->objsearch != NULL)
> + struct auditstate *state = link_map_audit_state (l, cnt);
> + if (afct->objsearch2 != NULL)
> + {
> + name = afct->objsearch2 (name, &state->cookie, nsid, code);
> + if (name == NULL)
> + return NULL;
> + }
> + else if (afct->objsearch != NULL)
> {
> - struct auditstate *state = link_map_audit_state (l, cnt);
> name = afct->objsearch (name, &state->cookie, code);
> if (name == NULL)
> return NULL;
> diff --git a/elf/dl-load.c b/elf/dl-load.c
> index 1621cb1bde..d99b07f52e 100644
> --- a/elf/dl-load.c
> +++ b/elf/dl-load.c
> @@ -1533,7 +1533,7 @@ print_search_path (struct r_search_path_elem **list,
> In that case, FD is consumed for both successful and error returns. */
> static int
> open_verify (const char *name, int fd,
> - struct filebuf *fbp, struct link_map *loader,
> + struct filebuf *fbp, struct link_map *loader, Lmid_t nsid,
> int whatcode, int mode, bool *found_other_class, bool free_name)
> {
> /* This is the expected ELF header. */
> @@ -1567,7 +1567,8 @@ open_verify (const char *name, int fd,
> if (__glibc_unlikely (GLRO(dl_naudit) > 0))
> {
> const char *original_name = name;
> - name = _dl_audit_objsearch (name, loader, whatcode);
> + /* nsid specifies the destination namespace for the object. */
> + name = _dl_audit_objsearch (name, loader, nsid, whatcode);
> if (name == NULL)
> return -1;
>
> @@ -1732,8 +1733,8 @@ open_verify (const char *name, int fd,
> static int
> open_path (const char *name, size_t namelen, int mode,
> struct r_search_path_struct *sps, char **realname,
> - struct filebuf *fbp, struct link_map *loader, int whatcode,
> - bool *found_other_class)
> + struct filebuf *fbp, struct link_map *loader, Lmid_t nsid,
> + int whatcode, bool *found_other_class)
> {
> struct r_search_path_elem **dirs = sps->dirs;
> int fd = -1;
> @@ -1795,7 +1796,7 @@ open_path (const char *name, size_t namelen, int mode,
> if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
> _dl_debug_printf (" trying file=%s\n", buf);
>
> - fd = open_verify (buf, -1, fbp, loader, whatcode, mode,
> + fd = open_verify (buf, -1, fbp, loader, nsid, whatcode, mode,
> found_other_class, false);
> if (this_dir->status[cnt] == unknown)
> {
> @@ -1967,7 +1968,8 @@ _dl_map_new_object (struct link_map *loader, const char *name,
> if (__glibc_unlikely (GLRO(dl_naudit) > 0))
> {
> const char *before = name;
> - name = _dl_audit_objsearch (name, loader, LA_SER_ORIG);
> + /* nsid specifies the destination namespace for the object. */
> + name = _dl_audit_objsearch (name, loader, nsid, LA_SER_ORIG);
> if (name == NULL)
> {
> fd = -1;
> @@ -2005,7 +2007,7 @@ _dl_map_new_object (struct link_map *loader, const char *name,
> {
> fd = open_path (name, namelen, mode,
> &l->l_rpath_dirs,
> - &realname, &fb, loader, LA_SER_RUNPATH,
> + &realname, &fb, loader, nsid, LA_SER_RUNPATH,
> &found_other_class);
> if (fd != -1)
> break;
> @@ -2021,8 +2023,8 @@ _dl_map_new_object (struct link_map *loader, const char *name,
> "RPATH"))
> fd = open_path (name, namelen, mode,
> &main_map->l_rpath_dirs,
> - &realname, &fb, loader ?: main_map, LA_SER_RUNPATH,
> - &found_other_class);
> + &realname, &fb, loader ?: main_map, nsid,
> + LA_SER_RUNPATH, &found_other_class);
>
> /* Also try DT_RUNPATH in the executable for LD_AUDIT dlopen
> call. */
> @@ -2035,7 +2037,7 @@ _dl_map_new_object (struct link_map *loader, const char *name,
> if (cache_rpath (main_map, &l_rpath_dirs,
> DT_RUNPATH, "RUNPATH"))
> fd = open_path (name, namelen, mode, &l_rpath_dirs,
> - &realname, &fb, loader ?: main_map,
> + &realname, &fb, loader ?: main_map, nsid,
> LA_SER_RUNPATH, &found_other_class);
> }
> }
> @@ -2044,7 +2046,7 @@ _dl_map_new_object (struct link_map *loader, const char *name,
> if (fd == -1 && __rtld_env_path_list.dirs != (void *) -1)
> fd = open_path (name, namelen, mode, &__rtld_env_path_list,
> &realname, &fb,
> - loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded,
> + loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded, nsid,
> LA_SER_LIBPATH, &found_other_class);
>
> /* Look at the RUNPATH information for this binary. */
> @@ -2052,7 +2054,7 @@ _dl_map_new_object (struct link_map *loader, const char *name,
> && cache_rpath (loader, &loader->l_runpath_dirs,
> DT_RUNPATH, "RUNPATH"))
> fd = open_path (name, namelen, mode,
> - &loader->l_runpath_dirs, &realname, &fb, loader,
> + &loader->l_runpath_dirs, &realname, &fb, loader, nsid,
> LA_SER_RUNPATH, &found_other_class);
>
> #ifdef USE_LDCONFIG
> @@ -2102,8 +2104,8 @@ _dl_map_new_object (struct link_map *loader, const char *name,
> {
> fd = open_verify (cached, -1,
> &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded,
> - LA_SER_CONFIG, mode, &found_other_class,
> - false);
> + nsid, LA_SER_CONFIG, mode,
> + &found_other_class, false);
> if (__glibc_likely (fd != -1))
> realname = cached;
> else
> @@ -2119,7 +2121,8 @@ _dl_map_new_object (struct link_map *loader, const char *name,
> || __glibc_likely (!(l->l_flags_1 & DF_1_NODEFLIB)))
> && __rtld_search_dirs.dirs != (void *) -1)
> fd = open_path (name, namelen, mode, &__rtld_search_dirs,
> - &realname, &fb, l, LA_SER_DEFAULT, &found_other_class);
> + &realname, &fb, l, nsid, LA_SER_DEFAULT,
> + &found_other_class);
>
> /* Add another newline when we are tracing the library loading. */
> if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
> @@ -2136,8 +2139,8 @@ _dl_map_new_object (struct link_map *loader, const char *name,
> else
> {
> fd = open_verify (realname, -1, &fb,
> - loader ?: GL(dl_ns)[nsid]._ns_loaded, 0, mode,
> - &found_other_class, true);
> + loader ?: GL(dl_ns)[nsid]._ns_loaded, nsid, 0,
> + mode, &found_other_class, true);
> if (__glibc_unlikely (fd == -1))
> free (realname);
> }
> diff --git a/elf/link.h b/elf/link.h
> index 8a06075113..463a879963 100644
> --- a/elf/link.h
> +++ b/elf/link.h
> @@ -193,6 +193,8 @@ extern unsigned int la_version (unsigned int __version);
> extern void la_activity (uintptr_t *__cookie, unsigned int __flag);
> extern char *la_objsearch (const char *__name, uintptr_t *__cookie,
> unsigned int __flag);
> +extern char *la_objsearch2 (const char *__name, uintptr_t *__cookie,
> + Lmid_t __lmid, unsigned int __flag);
> extern unsigned int la_objopen (struct link_map *__map, Lmid_t __lmid,
> uintptr_t *__cookie);
> extern void la_preinit (uintptr_t *__cookie);
> diff --git a/elf/rtld.c b/elf/rtld.c
> index 9233c50256..ae850faae3 100644
> --- a/elf/rtld.c
> +++ b/elf/rtld.c
> @@ -963,7 +963,7 @@ ERROR: audit interface '%s' requires version %d (maximum supported version %d);
> return;
> }
>
> - enum { naudit_ifaces = 8 };
> + enum { naudit_ifaces = 9 };
> union
> {
> struct audit_ifaces ifaces;
> @@ -983,7 +983,8 @@ ERROR: audit interface '%s' requires version %d (maximum supported version %d);
> #define STRING(s) __STRING (s)
> "la_" STRING (ARCH_LA_PLTENTER) "\0"
> "la_" STRING (ARCH_LA_PLTEXIT) "\0"
> - "la_objclose\0";
> + "la_objclose\0"
> + "la_objsearch2\0";
> unsigned int cnt = 0;
> const char *cp = audit_iface_names;
> do
> diff --git a/elf/tst-audit29.c b/elf/tst-audit29.c
> new file mode 100644
> index 0000000000..f7a93ad06e
> --- /dev/null
> +++ b/elf/tst-audit29.c
> @@ -0,0 +1,268 @@
> +/* Test la_objsearch2 audit callback with Lmid_t and dlinfo(RTLD_DI_LMID).
> + Copyright (C) 2026 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 <errno.h>
> +#include <getopt.h>
> +#include <gnu/lib-names.h>
> +#include <limits.h>
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <unistd.h>
> +#include <dlfcn.h>
> +#include <link.h>
> +#include <support/capture_subprocess.h>
> +#include <support/check.h>
> +#include <support/xdlfcn.h>
> +#include <support/xstdio.h>
> +
> +static int restart;
> +#define CMDLINE_OPTIONS \
> + { "restart", no_argument, &restart, 1 },
> +
> +static int
> +handle_restart (void)
> +{
> + /* 1. Base namespace (LM_ID_BASE / 0) check:
> + Load tst-audit18mod.so to trigger genuine library search. */
> + void *h_base = xdlopen ("tst-audit18mod.so", RTLD_NOW);
> +
> + /* Verify xdlinfo positive return with RTLD_DI_PHDR. */
> + const ElfW(Phdr) *phdr = NULL;
> + int phnum = xdlinfo (h_base, RTLD_DI_PHDR, &phdr);
> + TEST_VERIFY (phnum > 0);
> + TEST_VERIFY (phdr != NULL);
This is unrelated to la_objsearch2. If it is needed, it belongs with
the xdlinfo patch.
> +
> + Lmid_t lmid_base = 0xdeadbeef;
> + xdlinfo (h_base, RTLD_DI_LMID, &lmid_base);
> + TEST_COMPARE (lmid_base, LM_ID_BASE);
> + printf ("SCENARIO: base_lmid=%ld\n", (long int) lmid_base);
> + xdlclose (h_base);
> +
> + /* 2. New namespace (LM_ID_NEWLM) check. */
> + void *h_new = xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW);
> + Lmid_t lmid_new = 0xdeadbeef;
> + xdlinfo (h_new, RTLD_DI_LMID, &lmid_new);
> + TEST_VERIFY (lmid_new != LM_ID_BASE);
> + printf ("SCENARIO: newlm_lmid=%ld\n", (long int) lmid_new);
> +
> + /* 3. Existing namespace check (lmid_new). */
> + void *h_existing = xdlmopen (lmid_new, "tst-audit18mod.so", RTLD_NOW);
> + Lmid_t lmid_existing = 0xdeadbeef;
> + xdlinfo (h_existing, RTLD_DI_LMID, &lmid_existing);
> + TEST_COMPARE (lmid_existing, lmid_new);
> + printf ("SCENARIO: existing_lmid=%ld\n", (long int) lmid_existing);
> +
> + xdlclose (h_existing);
> + xdlclose (h_new);
> + return 0;
> +}
> +
> +static int
> +do_test (int argc, char *argv[])
> +{
> + if (restart)
> + return handle_restart ();
> +
> + /* Provide dynamic headroom for arguments. */
> + char *spargv[argc + 3];
> + int i = 0;
> + for (; i < argc - 1; i++)
> + spargv[i] = argv[i + 1];
> + spargv[i++] = (char *) "--direct";
> + spargv[i++] = (char *) "--restart";
> + spargv[i] = NULL;
> +
> + setenv ("LD_AUDIT", "tst-auditmod29.so", 1);
> + struct support_capture_subprocess result
> + = support_capture_subprogram (spargv[0], spargv, NULL);
We have support_spawn_wrapped for this case, I usually find this resulting a
slight simnpler test.
> + support_capture_subprocess_check (&result, "tst-audit29", 0,
> + sc_allow_stdout | sc_allow_stderr);
> +
> + bool found_objsearch2_base = false;
> + bool found_objsearch2_newlm = false;
> + bool found_objsearch2_existing = false;
> + bool found_legacy_objsearch = false;
> +
> + long int base_lmid = -1;
> + long int newlm_lmid = -1;
> + long int existing_lmid = -1;
> + bool seen_base = false;
> + bool seen_newlm = false;
> + bool seen_existing = false;
> +
> + /* Parse stdout from restart for scenario lmid values. */
The child already checks that the base lmid is LM_ID_BASE and that the
existing namespace lmid matches the new one, so the parent only needs
the new namespace lmid.
> + FILE *out_stdout = xfmemopen (result.out.buffer, result.out.length, "r");
> + char *line = NULL;
> + size_t line_len = 0;
> + while (xgetline (&line, &line_len, out_stdout))
> + {
> + if (strncmp (line, "SCENARIO:", sizeof ("SCENARIO:") - 1) != 0)
> + continue;
> +
> + const char *value = NULL;
> + long int *lmid = NULL;
> + bool *seen = NULL;
> + if (strncmp (line, "SCENARIO: base_lmid=",
> + sizeof ("SCENARIO: base_lmid=") - 1) == 0)
> + {
> + value = line + sizeof ("SCENARIO: base_lmid=") - 1;
> + lmid = &base_lmid;
> + seen = &seen_base;
> + }
> + else if (strncmp (line, "SCENARIO: newlm_lmid=",
> + sizeof ("SCENARIO: newlm_lmid=") - 1) == 0)
> + {
> + value = line + sizeof ("SCENARIO: newlm_lmid=") - 1;
> + lmid = &newlm_lmid;
> + seen = &seen_newlm;
> + }
> + else if (strncmp (line, "SCENARIO: existing_lmid=",
> + sizeof ("SCENARIO: existing_lmid=") - 1) == 0)
> + {
> + value = line + sizeof ("SCENARIO: existing_lmid=") - 1;
> + lmid = &existing_lmid;
> + seen = &seen_existing;
> + }
> +
> + TEST_VERIFY (value != NULL);
> + if (value == NULL)
> + continue;
> + TEST_VERIFY (!*seen);
> + if (*seen)
> + continue;
> + *seen = true;
> +
> + const char *digits = *value == '-' ? value + 1 : value;
> + if (*digits < '0' || *digits > '9')
> + {
> + TEST_VERIFY (false);
> + continue;
> + }
> + char *end;
> + errno = 0;
> + long int parsed = strtol (value, &end, 10);
> + if (errno != 0 || (*end != '\0'
> + && (*end != '\n' || end[1] != '\0')))
> + {
> + TEST_VERIFY (false);
> + continue;
> + }
> + *lmid = parsed;
> + }
> + free (line);
> + line = NULL;
> + line_len = 0;
> + xfclose (out_stdout);
> +
> + TEST_VERIFY (seen_base);
> + TEST_VERIFY (seen_newlm);
> + TEST_VERIFY (seen_existing);
> + TEST_COMPARE (base_lmid, (long int) LM_ID_BASE);
> + TEST_VERIFY (newlm_lmid > (long int) LM_ID_BASE);
> + TEST_COMPARE (existing_lmid, newlm_lmid);
> +
> + /* Parse stderr from restart for la_objsearch2 calls. */
I think you can simplify with a sscanf loop:
char *name;
long int lmid;
unsigned int flag;
if (sscanf (line, "la_objsearch2: name=%ms lmid=%ld flag=%u",
&name, &lmid, &flag) == 3)
{
...
free (name);
}
A malformed line then leaves the found_* flags unset, so the test still
fails.
> + FILE *out_stderr = xfmemopen (result.err.buffer, result.err.length, "r");
> + while (xgetline (&line, &line_len, out_stderr))
> + {
> + if (strstr (line, "la_objsearch:") != NULL)
> + found_legacy_objsearch = true;
> +
> + if (strncmp (line, "la_objsearch2:",
> + sizeof ("la_objsearch2:") - 1) == 0)
> + {
> + bool valid = strncmp (line, "la_objsearch2: name=",
> + sizeof ("la_objsearch2: name=") - 1) == 0;
> + if (!valid)
> + {
> + TEST_VERIFY (valid);
> + continue;
> + }
> + const char *name = line + sizeof ("la_objsearch2: name=") - 1;
> + const char *field = strstr (name, " lmid=");
> + valid = field != NULL && field != name;
> + if (!valid)
> + {
> + TEST_VERIFY (valid);
> + continue;
> + }
> +
> + const char *number = field + sizeof (" lmid=") - 1;
> + const char *digits = *number == '-' ? number + 1 : number;
> + valid = *digits >= '0' && *digits <= '9';
> + if (!valid)
> + {
> + TEST_VERIFY (valid);
> + continue;
> + }
> + char *end;
> + errno = 0;
> + long int lmid = strtol (number, &end, 10);
> + valid = errno == 0 && strncmp (end, " flag=", sizeof (" flag=") - 1) == 0;
> + if (!valid)
> + {
> + TEST_VERIFY (valid);
> + continue;
> + }
> + number = end + sizeof (" flag=") - 1;
> + valid = *number >= '0' && *number <= '9';
> + if (!valid)
> + {
> + TEST_VERIFY (valid);
> + continue;
> + }
> + errno = 0;
> + unsigned long int flag = strtoul (number, &end, 10);
> + valid = errno == 0 && flag <= UINT_MAX
> + && (*end == '\0' || (*end == '\n' && end[1] == '\0'));
> + TEST_VERIFY (valid);
> + if (!valid || flag != LA_SER_ORIG)
> + continue;
> +
> + size_t name_len = field - name;
> + bool module_name = name_len == sizeof ("tst-audit18mod.so") - 1
> + && memcmp (name, "tst-audit18mod.so", name_len) == 0;
> + bool libc_name = name_len == sizeof (LIBC_SO) - 1
> + && memcmp (name, LIBC_SO, name_len) == 0;
> + if (module_name && lmid == base_lmid)
> + found_objsearch2_base = true;
> + if (libc_name && lmid == newlm_lmid)
> + found_objsearch2_newlm = true;
> + if (module_name && lmid == existing_lmid)
> + found_objsearch2_existing = true;
> + }
> + }
> + free (line);
> + xfclose (out_stderr);
> +
> + /* Verify la_objsearch2 preference over legacy la_objsearch. */
> + TEST_VERIFY (!found_legacy_objsearch);
> +
> + /* Verify each original request used its destination namespace. */
> + TEST_VERIFY (found_objsearch2_base);
> + TEST_VERIFY (found_objsearch2_newlm);
> + TEST_VERIFY (found_objsearch2_existing);
> +
> + support_capture_subprocess_free (&result);
> + return 0;
> +}
> +
> +#define TEST_FUNCTION_ARGV do_test
> +#include <support/test-driver.c>
> diff --git a/elf/tst-audit30.c b/elf/tst-audit30.c
> new file mode 100644
> index 0000000000..1952dda665
> --- /dev/null
> +++ b/elf/tst-audit30.c
> @@ -0,0 +1,111 @@
> +/* Test legacy la_objsearch fallback when la_objsearch2 is not provided.
> + Copyright (C) 2026 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 <getopt.h>
> +#include <gnu/lib-names.h>
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <unistd.h>
> +#include <dlfcn.h>
> +#include <link.h>
> +#include <support/capture_subprocess.h>
> +#include <support/check.h>
> +#include <support/xdlfcn.h>
> +#include <support/xstdio.h>
> +
> +static int restart;
> +#define CMDLINE_OPTIONS \
> + { "restart", no_argument, &restart, 1 },
> +
> +static int
> +handle_restart (void)
> +{
> + /* 1. Base namespace library search. */
> + void *h_base = xdlopen ("tst-audit18mod.so", RTLD_NOW);
> + xdlclose (h_base);
> +
> + /* 2. New namespace library search. */
> + void *h_new = xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW);
> + Lmid_t lmid_new = 0xdeadbeef;
> + xdlinfo (h_new, RTLD_DI_LMID, &lmid_new);
> + TEST_VERIFY (lmid_new != LM_ID_BASE);
> +
> + /* 3. Existing new namespace library search. */
> + void *h_existing = xdlmopen (lmid_new, "tst-audit18mod.so", RTLD_NOW);
> + xdlclose (h_existing);
> + xdlclose (h_new);
> + return 0;
> +}
> +
> +static int
> +do_test (int argc, char *argv[])
> +{
> + if (restart)
> + return handle_restart ();
> +
> + /* Provide dynamic headroom for arguments. */
> + char *spargv[argc + 3];
> + int i = 0;
> + for (; i < argc - 1; i++)
> + spargv[i] = argv[i + 1];
> + spargv[i++] = (char *) "--direct";
> + spargv[i++] = (char *) "--restart";
> + spargv[i] = NULL;
> +
> + setenv ("LD_AUDIT", "tst-auditmod30.so", 1);
> + struct support_capture_subprocess result
> + = support_capture_subprogram (spargv[0], spargv, NULL);
> + support_capture_subprocess_check (&result, "tst-audit30", 0,
> + sc_allow_stdout | sc_allow_stderr);
> +
> + bool found_legacy_objsearch = false;
> + bool found_objsearch2 = false;
> +
> + /* Parse stderr from restart for la_objsearch calls. */
> + FILE *out_stderr = xfmemopen (result.err.buffer, result.err.length, "r");
> + char *line = NULL;
> + size_t line_len = 0;
> + while (xgetline (&line, &line_len, out_stderr))
> + {
> + if (strstr (line, "tst-audit18mod.so") != NULL)
> + {
> + if (strstr (line, "la_objsearch:") != NULL)
> + found_legacy_objsearch = true;
> + if (strstr (line, "la_objsearch2:") != NULL)
> + found_objsearch2 = true;
> + }
> + }
> + free (line);
> + line = NULL;
> + line_len = 0;
> + xfclose (out_stderr);
> +
> + /* Verify fallback to legacy la_objsearch succeeded. */
> + TEST_VERIFY (found_legacy_objsearch);
> +
> + /* Verify la_objsearch2 was never called since auditor did not export it. */
> + TEST_VERIFY (!found_objsearch2);
> +
> + support_capture_subprocess_free (&result);
> + return 0;
> +}
> +
> +#define TEST_FUNCTION_ARGV do_test
> +#include <support/test-driver.c>
> diff --git a/elf/tst-auditmod29.c b/elf/tst-auditmod29.c
> new file mode 100644
> index 0000000000..fd83005760
> --- /dev/null
> +++ b/elf/tst-auditmod29.c
> @@ -0,0 +1,49 @@
> +/* Auditor module testing la_objsearch2 with Lmid_t parameter.
> + Copyright (C) 2026 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 <link.h>
> +#include <stdio.h>
> +
> +unsigned int
> +la_version (unsigned int version)
> +{
> + fprintf (stderr, "la_version: %u\n", version);
> + return LAV_CURRENT;
> +}
> +
> +char *
> +la_objsearch (const char *name, uintptr_t *cookie, unsigned int flag)
> +{
> + fprintf (stderr, "la_objsearch: name=%s flag=%u\n", name, flag);
> + return (char *) name;
> +}
> +
> +char *
> +la_objsearch2 (const char *name, uintptr_t *cookie, Lmid_t lmid,
> + unsigned int flag)
> +{
> + fprintf (stderr, "la_objsearch2: name=%s lmid=%ld flag=%u\n",
> + name, (long int) lmid, flag);
> + return (char *) name;
> +}
> +
> +unsigned int
> +la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie)
> +{
> + return LA_FLG_BINDTO | LA_FLG_BINDFROM;
> +}
> diff --git a/sysdeps/loongarch/bits/link_lavcurrent.h b/elf/tst-auditmod30.c
> similarity index 56%
> rename from sysdeps/loongarch/bits/link_lavcurrent.h
> rename to elf/tst-auditmod30.c
> index 9718a88a13..15b2e0d0ad 100644
> --- a/sysdeps/loongarch/bits/link_lavcurrent.h
> +++ b/elf/tst-auditmod30.c
> @@ -1,6 +1,5 @@
> -/* Data structure for communication from the run-time dynamic linker for
> - loaded ELF shared objects. LAV_CURRENT definition.
> - Copyright (C) 2023-2026 Free Software Foundation, Inc.
> +/* Auditor module testing legacy la_objsearch fallback.
> + Copyright (C) 2026 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
> @@ -17,9 +16,25 @@
> License along with the GNU C Library; if not, see
> <https://www.gnu.org/licenses/>. */
>
> -#ifndef _LINK_H
> -# error "Never include <bits/link_lavcurrent.h> directly; use <link.h> instead."
> -#endif
> +#include <link.h>
> +#include <stdio.h>
>
> -/* Version numbers for la_version handshake interface. */
> -#define LAV_CURRENT 3
> +unsigned int
> +la_version (unsigned int version)
> +{
> + fprintf (stderr, "la_version: %u\n", version);
> + return LAV_CURRENT;
> +}
> +
> +char *
> +la_objsearch (const char *name, uintptr_t *cookie, unsigned int flag)
> +{
> + fprintf (stderr, "la_objsearch: name=%s flag=%u\n", name, flag);
> + return (char *) name;
> +}
> +
> +unsigned int
> +la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie)
> +{
> + return LA_FLG_BINDTO | LA_FLG_BINDFROM;
> +}
> diff --git a/manual/dynlink.texi b/manual/dynlink.texi
> index f7f35ca9bd..d4a3898fb0 100644
> --- a/manual/dynlink.texi
> +++ b/manual/dynlink.texi
> @@ -968,6 +968,29 @@ Unused.
>
> @end deftypefun
>
> +@deftypefun {char *} la_objsearch2 (const char *@var{name}, uintptr_t *@var{privdata}, Lmid_t @var{lmid}, unsigned int @var{flag})
> +An auditor can provide this callback for the same search events as
> +@code{la_objsearch}. The @var{name}, @var{privdata}, and @var{flag}
> +arguments and the return value have the semantics described for
> +@code{la_objsearch}, including the rules for alternate strings and
> +@code{NULL}. The @var{lmid} argument identifies the destination link
> +namespace for the search, which need not be the namespace of the object
> +doing the search, whose private data @var{privdata} points to.
I think it is worth to add lmid may not have been announced yet. For
LM_ID_NEWLM this is the first callback to report the new id, before
la_activity (LA_ACT_ADD) and la_objopen. If the load fails, no other
callback reports it, as with dlmopen (LM_ID_NEWLM, "nonexistent.so"),
and a later dlmopen can reuse the id.
> +
> +On a dynamic linker that supports this callback, if an auditor defines
> +both @code{la_objsearch2} and @code{la_objsearch}, only
> +@code{la_objsearch2} is called for that auditor for a given search
> +event. If the auditor does not define @code{la_objsearch2}, the
> +dynamic linker calls its @code{la_objsearch}, if defined.
> +
> +Auditors that also support older dynamic linkers should define
> +@code{la_objsearch} as well. This preserves search interception on
> +older linkers, but the legacy callback does not receive the destination
> +namespace. Audit API version 3 alone does not establish that a linker
> +supports @code{la_objsearch2}: older LoongArch linkers already used
> +version 3.
> +@end deftypefun
> +
> @deftypefun {unsigned int} la_objopen (struct link_map *@var{map}, Lmid_t @var{lmid}, uintptr_t *@var{privdata})
> Called when a shared object is loaded, including the main, ld.so, and
> vDSO objects. Unlike @code{la_activity} this callback is called once
> diff --git a/sysdeps/aarch64/dl-audit-check.h b/sysdeps/aarch64/dl-audit-check.h
> index c49c39b62d..e5b7bf7f96 100644
> --- a/sysdeps/aarch64/dl-audit-check.h
> +++ b/sysdeps/aarch64/dl-audit-check.h
> @@ -24,5 +24,5 @@ _dl_audit_check_version (unsigned int lav)
> missing indirect result save/restore makes _dl_runtime_profile
> potentially trigger undefined behavior if the function returns a large
> struct (even when PLT trace is not requested). */
> - return lav == LAV_CURRENT;
> + return lav >= 2 && lav <= LAV_CURRENT;
> }
> diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
> index 305ca6e0df..40374c6724 100644
> --- a/sysdeps/generic/ldsodefs.h
> +++ b/sysdeps/generic/ldsodefs.h
> @@ -260,6 +260,7 @@ struct audit_ifaces
> #endif
> };
> unsigned int (*objclose) (uintptr_t *);
> + char *(*objsearch2) (const char *, uintptr_t *, Lmid_t, unsigned int);
>
> struct audit_ifaces *next;
> };
> @@ -1380,11 +1381,10 @@ link_map_audit_state (struct link_map *l, size_t index)
> }
> }
>
> -/* Call the la_objsearch from the audit modules from the link map L. If
> - ORIGNAME is non NULL, it is updated with the previous name prior calling
> - la_objsearch. */
> +/* Call la_objsearch2 / la_objsearch from the audit modules for the link map L
> + and destination namespace NSID. */
> const char *_dl_audit_objsearch (const char *name, struct link_map *l,
> - unsigned int code)
> + Lmid_t nsid, unsigned int code)
> attribute_hidden;
>
> /* Call the la_activity from the audit modules from the link map L and issues
> diff --git a/sysdeps/loongarch/dl-audit-check.h b/sysdeps/loongarch/dl-audit-check.h
> index d92474377d..edeb29e9ed 100644
> --- a/sysdeps/loongarch/dl-audit-check.h
> +++ b/sysdeps/loongarch/dl-audit-check.h
> @@ -19,5 +19,7 @@
> static inline bool
> _dl_audit_check_version (unsigned int lav)
> {
> - return lav == LAV_CURRENT;
> + /* Audit versions prior to 3 do not support LASX/LSX vector registers
> + in La_loongarch_regs and La_loongarch_retval. */
> + return lav >= 3 && lav <= LAV_CURRENT;
> }
More information about the Libc-alpha
mailing list