1/* Check that dlfcn errors are reported properly after dlmopen. Test module.
2 Copyright (C) 2021-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 <http://www.gnu.org/licenses/>. */
18
19#include <dlfcn.h>
20#include <stddef.h>
21#include <stdio.h>
22#include <string.h>
23#include <support/check.h>
24
25/* Note: This object is not linked into the main program, so we cannot
26 use delayed test failure reporting via TEST_VERIFY etc., and have
27 to use FAIL_EXIT1 (or something else that calls exit). */
28
29void
30call_dlsym (const char *name)
31{
32 void *ptr = dlsym (NULL, name: name);
33 if (ptr != NULL)
34 FAIL_EXIT1 ("dlsym did not fail as expected for: %s", name);
35 const char *message = dlerror ();
36 if (strstr (haystack: message, needle: ": undefined symbol: does not exist X") == NULL)
37 FAIL_EXIT1 ("invalid dlsym error message for [[%s]]: %s", name, message);
38 message = dlerror ();
39 if (message != NULL)
40 FAIL_EXIT1 ("second dlsym for [[%s]]: %s", name, message);
41}
42
43void
44call_dlopen (const char *name)
45{
46 void *handle = dlopen (file: name, RTLD_NOW);
47 if (handle != NULL)
48 FAIL_EXIT1 ("dlopen did not fail as expected for: %s", name);
49 const char *message = dlerror ();
50 if (strstr (haystack: message, needle: "X: cannot open shared object file:"
51 " No such file or directory") == NULL
52 && strstr (haystack: message, needle: "X: cannot open shared object file:"
53 " File name too long") == NULL)
54 FAIL_EXIT1 ("invalid dlopen error message for [[%s]]: %s", name, message);
55 message = dlerror ();
56 if (message != NULL)
57 FAIL_EXIT1 ("second dlopen for [[%s]]: %s", name, message);
58}
59

source code of glibc/elf/tst-dlmopen-dlerror-mod.c