1 /* Test that --glibc-hwcaps-prepend works, using dlopen and /etc/ld.so.cache.
2 Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
24 #include <support/check.h>
25 #include <support/support.h>
26 #include <support/xdlfcn.h>
27 #include <support/xunistd.h>
29 /* Invoke /sbin/ldconfig with some error checking. */
33 char *command
= xasprintf ("%s/ldconfig", support_install_rootsbindir
);
34 TEST_COMPARE (system (command
), 0);
38 /* The library under test. */
39 #define SONAME "libmarkermod1.so"
44 if (dlopen (SONAME
, RTLD_NOW
) != NULL
)
45 FAIL_EXIT1 (SONAME
" is already on the search path");
48 /* Install the default implementation of libmarkermod1.so. */
49 char *conf_path
= xasprintf ("%s/ld.so.conf", support_sysconfdir_prefix
);
50 xmkdirp (support_sysconfdir_prefix
, 0777);
51 support_write_file_string (conf_path
, "/glibc-test/lib\n");
54 xmkdirp ("/glibc-test/lib/glibc-hwcaps/prepend2", 0777);
55 xmkdirp ("/glibc-test/lib/glibc-hwcaps/prepend3", 0777);
57 char *src
= xasprintf ("%s/elf/libmarkermod1-1.so", support_objdir_root
);
58 support_copy_file (src
, "/glibc-test/lib/" SONAME
);
63 /* The default implementation can now be loaded. */
64 void *handle
= xdlopen (SONAME
, RTLD_NOW
);
65 int (*marker1
) (void) = xdlsym (handle
, "marker1");
66 TEST_COMPARE (marker1 (), 1);
70 /* Add the first override to the directory that is searched last. */
72 char *src
= xasprintf ("%s/elf/libmarkermod1-2.so", support_objdir_root
);
73 support_copy_file (src
, "/glibc-test/lib/glibc-hwcaps/prepend2/"
78 /* This is still the first implementation. The cache has not been
80 void *handle
= xdlopen (SONAME
, RTLD_NOW
);
81 int (*marker1
) (void) = xdlsym (handle
, "marker1");
82 TEST_COMPARE (marker1 (), 1);
87 /* After running ldconfig, it is the second implementation. */
88 void *handle
= xdlopen (SONAME
, RTLD_NOW
);
89 int (*marker1
) (void) = xdlsym (handle
, "marker1");
90 TEST_COMPARE (marker1 (), 2);
94 /* Add the second override to the directory that is searched first. */
96 char *src
= xasprintf ("%s/elf/libmarkermod1-3.so", support_objdir_root
);
97 support_copy_file (src
, "/glibc-test/lib/glibc-hwcaps/prepend3/"
102 /* This is still the second implementation. */
103 void *handle
= xdlopen (SONAME
, RTLD_NOW
);
104 int (*marker1
) (void) = xdlsym (handle
, "marker1");
105 TEST_COMPARE (marker1 (), 2);
110 /* After running ldconfig, it is the third implementation. */
111 void *handle
= xdlopen (SONAME
, RTLD_NOW
);
112 int (*marker1
) (void) = xdlsym (handle
, "marker1");
113 TEST_COMPARE (marker1 (), 3);
117 /* Remove the second override again, without running ldconfig.
118 Ideally, this would revert to implementation 2. However, in the
119 current implementation, the cache returns exactly one file name
120 which does not exist after unlinking, so the dlopen fails. */
121 xunlink ("/glibc-test/lib/glibc-hwcaps/prepend3/" SONAME
);
122 TEST_VERIFY (dlopen (SONAME
, RTLD_NOW
) == NULL
);
125 /* After running ldconfig, the second implementation is available
127 void *handle
= xdlopen (SONAME
, RTLD_NOW
);
128 int (*marker1
) (void) = xdlsym (handle
, "marker1");
129 TEST_COMPARE (marker1 (), 2);
137 prepare (int argc
, char **argv
)
139 const char *no_restart
= "no-restart";
140 if (argc
== 2 && strcmp (argv
[1], no_restart
) == 0)
142 /* Re-execute the test with an explicit loader invocation. */
143 execl (support_objdir_elf_ldso
,
144 support_objdir_elf_ldso
,
145 "--glibc-hwcaps-prepend", "prepend3:prepend2",
148 printf ("error: execv of %s failed: %m\n", argv
[0]);
152 #define PREPARE prepare
153 #include <support/test-driver.c>