1/* Test dlopen rollback after failures involving NODELETE objects (bug 20839).
2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19#include <dlfcn.h>
20#include <errno.h>
21#include <stddef.h>
22#include <stdio.h>
23#include <string.h>
24#include <support/check.h>
25#include <support/xdlfcn.h>
26
27static int
28do_test (void)
29{
30 TEST_VERIFY (dlsym (NULL, "no_delete_mod_function") == NULL);
31
32 /* This is expected to fail because of the missing dependency. */
33 puts (s: "info: attempting to load tst-dlopenfailmod1.so");
34 TEST_VERIFY (dlopen ("tst-dlopenfailmod1.so", RTLD_LAZY) == NULL);
35 const char *message = dlerror ();
36 TEST_COMPARE_STRING (message,
37 "tst-dlopenfail-missingmod.so:"
38 " cannot open shared object file:"
39 " No such file or directory");
40
41 /* Do not probe for the presence of the tst-dlopenfailnodelmod.so at
42 this point because that might trigger relocation if bug 20839 is
43 present, obscuring a subsequent crash. */
44
45 /* This is expected to succeed. */
46 puts (s: "info: loading tst-dlopenfailmod2.so");
47 void *handle = xdlopen (filename: "tst-dlopenfailmod2.so", RTLD_NOW);
48 xdlclose (handle);
49
50 /* The NODELETE module should remain loaded. */
51 TEST_VERIFY (dlopen ("tst-dlopenfailnodelmod.so", RTLD_LAZY | RTLD_NOLOAD)
52 != NULL);
53 /* But the symbol is not in the global scope. */
54 TEST_VERIFY (dlsym (NULL, "no_delete_mod_function") == NULL);
55
56 /* We can make tst-dlopenfailnodelmod.so global, and then the symbol
57 should become available. */
58 TEST_VERIFY (dlopen ("tst-dlopenfailnodelmod.so", RTLD_LAZY | RTLD_GLOBAL)
59 != NULL);
60 void (*no_delete_mod_function) (void)
61 = dlsym (NULL, name: "no_delete_mod_function");
62 TEST_VERIFY_EXIT (no_delete_mod_function != NULL);
63
64 /* Hopefully, no_delete_mod_function is sufficiently complex to
65 depend on relocations. */
66 no_delete_mod_function ();
67
68 return 0;
69}
70
71#include <support/test-driver.c>
72

source code of glibc/elf/tst-dlopenfail.c