1 /* Test that --glibc-hwcaps-prepend works, using dlopen and /etc/ld.so.cache.
2 Copyright (C) 2020-2022 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");
47 /* Install the default implementation of libmarkermod1.so. */
48 xmkdirp ("/etc", 0777);
49 support_write_file_string ("/etc/ld.so.conf", "/glibc-test/lib\n");
50 xmkdirp ("/glibc-test/lib/glibc-hwcaps/prepend2", 0777);
51 xmkdirp ("/glibc-test/lib/glibc-hwcaps/prepend3", 0777);
53 char *src
= xasprintf ("%s/elf/libmarkermod1-1.so", support_objdir_root
);
54 support_copy_file (src
, "/glibc-test/lib/" SONAME
);
59 /* The default implementation can now be loaded. */
60 void *handle
= xdlopen (SONAME
, RTLD_NOW
);
61 int (*marker1
) (void) = xdlsym (handle
, "marker1");
62 TEST_COMPARE (marker1 (), 1);
66 /* Add the first override to the directory that is searched last. */
68 char *src
= xasprintf ("%s/elf/libmarkermod1-2.so", support_objdir_root
);
69 support_copy_file (src
, "/glibc-test/lib/glibc-hwcaps/prepend2/"
74 /* This is still the first implementation. The cache has not been
76 void *handle
= xdlopen (SONAME
, RTLD_NOW
);
77 int (*marker1
) (void) = xdlsym (handle
, "marker1");
78 TEST_COMPARE (marker1 (), 1);
83 /* After running ldconfig, it is the second implementation. */
84 void *handle
= xdlopen (SONAME
, RTLD_NOW
);
85 int (*marker1
) (void) = xdlsym (handle
, "marker1");
86 TEST_COMPARE (marker1 (), 2);
90 /* Add the second override to the directory that is searched first. */
92 char *src
= xasprintf ("%s/elf/libmarkermod1-3.so", support_objdir_root
);
93 support_copy_file (src
, "/glibc-test/lib/glibc-hwcaps/prepend3/"
98 /* This is still the second implementation. */
99 void *handle
= xdlopen (SONAME
, RTLD_NOW
);
100 int (*marker1
) (void) = xdlsym (handle
, "marker1");
101 TEST_COMPARE (marker1 (), 2);
106 /* After running ldconfig, it is the third implementation. */
107 void *handle
= xdlopen (SONAME
, RTLD_NOW
);
108 int (*marker1
) (void) = xdlsym (handle
, "marker1");
109 TEST_COMPARE (marker1 (), 3);
113 /* Remove the second override again, without running ldconfig.
114 Ideally, this would revert to implementation 2. However, in the
115 current implementation, the cache returns exactly one file name
116 which does not exist after unlinking, so the dlopen fails. */
117 xunlink ("/glibc-test/lib/glibc-hwcaps/prepend3/" SONAME
);
118 TEST_VERIFY (dlopen (SONAME
, RTLD_NOW
) == NULL
);
121 /* After running ldconfig, the second implementation is available
123 void *handle
= xdlopen (SONAME
, RTLD_NOW
);
124 int (*marker1
) (void) = xdlsym (handle
, "marker1");
125 TEST_COMPARE (marker1 (), 2);
133 prepare (int argc
, char **argv
)
135 const char *no_restart
= "no-restart";
136 if (argc
== 2 && strcmp (argv
[1], no_restart
) == 0)
138 /* Re-execute the test with an explicit loader invocation. */
139 execl (support_objdir_elf_ldso
,
140 support_objdir_elf_ldso
,
141 "--glibc-hwcaps-prepend", "prepend3:prepend2",
144 printf ("error: execv of %s failed: %m\n", argv
[0]);
148 #define PREPARE prepare
149 #include <support/test-driver.c>