tst-dladdr.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Test for dladdr.
  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 <errno.h>
  17. #include <error.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #define TEST_FUNCTION do_test ()
  22. extern int do_test (void);
  23. int
  24. do_test (void)
  25. {
  26. void *handle;
  27. int (*sym) (void); /* We load ref1 from glreflib1.c. */
  28. Dl_info info;
  29. int ret;
  30. handle = dlopen ("glreflib1.so", RTLD_NOW);
  31. if (handle == NULL)
  32. error (EXIT_FAILURE, 0, "cannot load: glreflib1.so");
  33. sym = dlsym (handle, "ref1");
  34. if (sym == NULL)
  35. error (EXIT_FAILURE, 0, "dlsym failed");
  36. memset (&info, 0, sizeof (info));
  37. ret = dladdr (sym, &info);
  38. if (ret == 0)
  39. error (EXIT_FAILURE, 0, "dladdr failed");
  40. printf ("ret = %d\n", ret);
  41. printf ("info.dli_fname = %p (\"%s\")\n", info.dli_fname, info.dli_fname);
  42. printf ("info.dli_fbase = %p\n", info.dli_fbase);
  43. printf ("info.dli_sname = %p (\"%s\")\n", info.dli_sname, info.dli_sname);
  44. printf ("info.dli_saddr = %p\n", info.dli_saddr);
  45. if (info.dli_fname == NULL)
  46. error (EXIT_FAILURE, 0, "dli_fname is NULL");
  47. if (info.dli_fbase == NULL)
  48. error (EXIT_FAILURE, 0, "dli_fbase is NULL");
  49. if (info.dli_sname == NULL)
  50. error (EXIT_FAILURE, 0, "dli_sname is NULL");
  51. if (info.dli_saddr == NULL)
  52. error (EXIT_FAILURE, 0, "dli_saddr is NULL");
  53. dlclose (handle);
  54. return 0;
  55. }
  56. #include "../test-skeleton.c"