glrefmain.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Test for dependency tracking added by relocations.
  2. Copyright (C) 2000-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <dlfcn.h>
  16. #include <error.h>
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. static void *
  21. load (const char *name)
  22. {
  23. void *d = dlopen (name, RTLD_LAZY | RTLD_GLOBAL);
  24. if (d == NULL)
  25. error (EXIT_FAILURE, errno, "cannot load `%s'", name);
  26. return d;
  27. }
  28. #define TEST_FUNCTION do_test ()
  29. extern int do_test (void);
  30. int
  31. do_test (void)
  32. {
  33. void *d1;
  34. void *d2;
  35. int (*f) (void);
  36. d1 = load ("glreflib1.so");
  37. d2 = load ("glreflib2.so");
  38. f = dlsym (d2, "ref2");
  39. if (f == NULL)
  40. error (EXIT_FAILURE, errno, "cannot get pointer to `%s'", "ref2");
  41. if (f () != 42)
  42. error (EXIT_FAILURE, 0, "wrong result from `%s'", "ref2");
  43. puts ("Correct result in first call");
  44. fflush (stdout);
  45. /* Now unload the first file. */
  46. dlclose (d1);
  47. puts ("About to call the second time");
  48. fflush (stdout);
  49. /* Try calling the function again. */
  50. if (f () != 42)
  51. error (EXIT_FAILURE, 0, "wrong result from `%s' (second call)", "ref2");
  52. puts ("Second call succeeded!");
  53. fflush (stdout);
  54. dlclose (d2);
  55. puts ("glreflib2 also closed");
  56. fflush (stdout);
  57. return 0;
  58. }
  59. #include "../test-skeleton.c"