Report dlsym, dlvsym lookup errors using dlerror [BZ #19509]
[glibc.git] / elf / tst-dlsym-error.c
blob11b035881dcfd96ec9e5fe095d3cbbe032ffe2f6
1 /* Test error reporting for dlsym, dlvsym failures.
2 Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. */
19 #include <dlfcn.h>
20 #include <gnu/lib-names.h>
21 #include <stdio.h>
22 #include <stdlib.h>
24 /* Used to disambiguate symbol names. */
25 static int counter;
27 static void
28 test_one (void *handle, const char *name, void *(func) (void *, const char *),
29 const char *suffix)
31 ++counter;
32 char symbol[32];
33 snprintf (symbol, sizeof (symbol), "no_such_symbol_%d", counter);
34 char *expected_message;
35 if (asprintf (&expected_message, ": undefined symbol: %s%s",
36 symbol, suffix) < 0)
38 printf ("error: asprintf: %m\n");
39 abort ();
42 void *addr = func (handle, symbol);
43 if (addr != NULL)
45 printf ("error: %s: found symbol \"no_such_symbol\"\n", name);
46 abort ();
48 const char *message = dlerror ();
49 if (message == NULL)
51 printf ("error: %s: missing error message\n", name);
52 abort ();
54 const char *message_without_path = strchrnul (message, ':');
55 if (strcmp (message_without_path, expected_message) != 0)
57 printf ("error: %s: unexpected error message: %s\n", name, message);
58 abort ();
60 free (expected_message);
62 message = dlerror ();
63 if (message != NULL)
65 printf ("error: %s: unexpected error message: %s\n", name, message);
66 abort ();
70 static void
71 test_handles (const char *name, void *(func) (void *, const char *),
72 const char *suffix)
74 test_one (RTLD_DEFAULT, name, func, suffix);
75 test_one (RTLD_NEXT, name, func, suffix);
77 void *handle = dlopen (LIBC_SO, RTLD_LAZY);
78 if (handle == NULL)
80 printf ("error: cannot dlopen %s: %s\n", LIBC_SO, dlerror ());
81 abort ();
83 test_one (handle, name, func, suffix);
84 dlclose (handle);
87 static void *
88 dlvsym_no_such_version (void *handle, const char *name)
90 return dlvsym (handle, name, "NO_SUCH_VERSION");
93 static void *
94 dlvsym_glibc_private (void *handle, const char *name)
96 return dlvsym (handle, name, "GLIBC_PRIVATE");
99 static int
100 do_test (void)
102 test_handles ("dlsym", dlsym, "");
103 test_handles ("dlvsym", dlvsym_no_such_version,
104 ", version NO_SUCH_VERSION");
105 test_handles ("dlvsym", dlvsym_glibc_private,
106 ", version GLIBC_PRIVATE");
108 return 0;
112 #define TEST_FUNCTION do_test ()
113 #include "../test-skeleton.c"