tst-dlinfo.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Test for dlinfo.
  2. Copyright (C) 2003-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 <stdio.h>
  17. #include <stdlib.h>
  18. #include <error.h>
  19. #define TEST_FUNCTION do_test ()
  20. static int
  21. do_test (void)
  22. {
  23. int status = 0;
  24. void *handle = dlopen ("glreflib3.so", RTLD_NOW);
  25. if (handle == NULL)
  26. error (EXIT_FAILURE, 0, "cannot load: glreflib1.so: %s", dlerror ());
  27. #define TRY(req, arg) \
  28. if (dlinfo (handle, req, arg) != 0) \
  29. { \
  30. printf ("dlinfo failed for %s: %s\n", #req, dlerror ()); \
  31. status = 1; \
  32. } \
  33. else
  34. struct link_map *l;
  35. TRY (RTLD_DI_LINKMAP, &l)
  36. {
  37. if (l != handle)
  38. {
  39. printf ("bogus link_map? %p != %p\n", l, handle);
  40. status = 1;
  41. }
  42. }
  43. char origin[8192]; /* >= PATH_MAX, in theory */
  44. TRY (RTLD_DI_ORIGIN, origin)
  45. {
  46. printf ("origin: %s\n", origin);
  47. }
  48. Dl_serinfo counts;
  49. TRY (RTLD_DI_SERINFOSIZE, &counts)
  50. {
  51. Dl_serinfo *buf = alloca (counts.dls_size);
  52. buf->dls_cnt = counts.dls_cnt;
  53. buf->dls_size = counts.dls_size;
  54. printf ("%u library directories\n", buf->dls_cnt);
  55. TRY (RTLD_DI_SERINFO, buf)
  56. {
  57. if (counts.dls_cnt != buf->dls_cnt)
  58. {
  59. printf ("??? became %u library directories\n", buf->dls_cnt);
  60. status = 1;
  61. }
  62. for (unsigned int i = 0; i < buf->dls_cnt; ++i)
  63. printf ("\t%#02x\t%s\n",
  64. buf->dls_serpath[i].dls_flags,
  65. buf->dls_serpath[i].dls_name);
  66. }
  67. }
  68. unsigned long int lmid = 0xdeadbeefUL;
  69. if (dlinfo (handle, RTLD_DI_LMID, &lmid) != 0)
  70. printf ("dlinfo refuses RTLD_DI_LMID: %s\n", dlerror ());
  71. else
  72. {
  73. printf ("dlinfo RTLD_DI_LMID worked? %#lx\n", lmid);
  74. status = lmid == 0xdeadbeefUL;
  75. }
  76. #undef TRY
  77. dlclose (handle);
  78. return status;
  79. }
  80. #include "../test-skeleton.c"