tst-dlinfo-phdr.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Test for dlinfo (RTLD_DI_PHDR).
  2. Copyright (C) 2022-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 <link.h>
  17. #include <stdbool.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <sys/auxv.h>
  21. #include <support/check.h>
  22. #include <support/xdlfcn.h>
  23. /* Used to verify that the program header array appears as expected
  24. among the dl_iterate_phdr callback invocations. */
  25. struct dlip_callback_args
  26. {
  27. struct link_map *l; /* l->l_addr is used to find the object. */
  28. const ElfW(Phdr) *phdr; /* Expected program header pointed. */
  29. int phnum; /* Expected program header count. */
  30. bool found; /* True if l->l_addr has been found. */
  31. };
  32. static int
  33. dlip_callback (struct dl_phdr_info *dlpi, size_t size, void *closure)
  34. {
  35. TEST_COMPARE (sizeof (*dlpi), size);
  36. struct dlip_callback_args *args = closure;
  37. if (dlpi->dlpi_addr == args->l->l_addr)
  38. {
  39. TEST_VERIFY (!args->found);
  40. args->found = true;
  41. TEST_VERIFY (args->phdr == dlpi->dlpi_phdr);
  42. TEST_COMPARE (args->phnum, dlpi->dlpi_phnum);
  43. }
  44. return 0;
  45. }
  46. static int
  47. do_test (void)
  48. {
  49. /* Avoid a copy relocation. */
  50. struct r_debug *debug = xdlsym (RTLD_DEFAULT, "_r_debug");
  51. struct link_map *l = (struct link_map *) debug->r_map;
  52. TEST_VERIFY_EXIT (l != NULL);
  53. do
  54. {
  55. printf ("info: checking link map %p (%p) for \"%s\"\n",
  56. l, l->l_phdr, l->l_name);
  57. /* Cause dlerror () to return an error message. */
  58. dlsym (RTLD_DEFAULT, "does-not-exist");
  59. /* Use the extension that link maps are valid dlopen handles. */
  60. const ElfW(Phdr) *phdr;
  61. int phnum = dlinfo (l, RTLD_DI_PHDR, &phdr);
  62. TEST_VERIFY (phnum >= 0);
  63. /* Verify that the error message has been cleared. */
  64. TEST_COMPARE_STRING (dlerror (), NULL);
  65. TEST_VERIFY (phdr == l->l_phdr);
  66. TEST_COMPARE (phnum, l->l_phnum);
  67. /* Check that we can find PT_DYNAMIC among the array. */
  68. {
  69. bool dynamic_found = false;
  70. for (int i = 0; i < phnum; ++i)
  71. if (phdr[i].p_type == PT_DYNAMIC)
  72. {
  73. dynamic_found = true;
  74. TEST_COMPARE ((ElfW(Addr)) l->l_ld, l->l_addr + phdr[i].p_vaddr);
  75. }
  76. TEST_VERIFY (dynamic_found);
  77. }
  78. /* Check that dl_iterate_phdr finds the link map with the same
  79. program headers. */
  80. {
  81. struct dlip_callback_args args =
  82. {
  83. .l = l,
  84. .phdr = phdr,
  85. .phnum = phnum,
  86. .found = false,
  87. };
  88. TEST_COMPARE (dl_iterate_phdr (dlip_callback, &args), 0);
  89. TEST_VERIFY (args.found);
  90. }
  91. if (l->l_prev == NULL)
  92. {
  93. /* This is the executable, so the information is also
  94. available via getauxval. */
  95. TEST_COMPARE_STRING (l->l_name, "");
  96. TEST_VERIFY (phdr == (const ElfW(Phdr) *) getauxval (AT_PHDR));
  97. TEST_COMPARE (phnum, getauxval (AT_PHNUM));
  98. }
  99. l = l->l_next;
  100. }
  101. while (l != NULL);
  102. return 0;
  103. }
  104. #include <support/test-driver.c>