dl-iterate-phdr.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Get loaded objects program headers.
  2. Based on GNU C library (file: libc/elf/dl-iteratephdr.c)
  3. Copyright (C) 2001,2002,2003,2004,2006,2007 Free Software Foundation, Inc.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2001.
  5. Copyright (C) 2008 STMicroelectronics Ltd.
  6. Author: Carmelo Amoroso <carmelo.amoroso@st.com>
  7. Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  8. */
  9. #include <link.h>
  10. #include <ldso.h>
  11. /* we want this in libc but nowhere else */
  12. #ifdef __USE_GNU
  13. static int
  14. __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data)
  15. {
  16. int ret = 0;
  17. #ifndef __ARCH_HAS_NO_SHARED__
  18. struct elf_resolve *l;
  19. struct dl_phdr_info info;
  20. for (l = _dl_loaded_modules; l != NULL; l = l->next) {
  21. info.dlpi_addr = l->loadaddr;
  22. info.dlpi_name = l->libname;
  23. info.dlpi_phdr = l->ppnt;
  24. info.dlpi_phnum = l->n_phent;
  25. info.dlpi_adds = _dl_load_adds;
  26. info.dlpi_subs = _dl_load_subs;
  27. #if defined(USE_TLS) && USE_TLS
  28. info.dlpi_tls_modid = l->l_tls_modid;
  29. # ifdef SHARED
  30. /* Resolves into ld.so at runtime; static libc has no DTV. */
  31. info.dlpi_tls_data = _dl_tls_get_addr_soft((struct link_map *) l);
  32. # else
  33. info.dlpi_tls_data = NULL;
  34. # endif
  35. #else
  36. info.dlpi_tls_modid = 0;
  37. info.dlpi_tls_data = NULL;
  38. #endif
  39. ret = callback (&info, sizeof (struct dl_phdr_info), data);
  40. if (ret)
  41. break;
  42. }
  43. #endif
  44. return ret;
  45. }
  46. # ifdef SHARED
  47. weak_alias(__dl_iterate_phdr, dl_iterate_phdr)
  48. # else
  49. /* dl-support.c defines these and initializes them early on. */
  50. extern ElfW(Phdr) *_dl_phdr;
  51. extern size_t _dl_phnum;
  52. int
  53. dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
  54. size_t size, void *data), void *data)
  55. {
  56. if (_dl_phnum != 0)
  57. {
  58. /* This entry describes this statically-linked program itself. */
  59. struct dl_phdr_info info;
  60. int ret;
  61. #if defined(__FRV_FDPIC__) || defined(__BFIN_FDPIC__) || defined(__FDPIC__)
  62. info.dlpi_addr.map = NULL;
  63. info.dlpi_addr.got_value = NULL;
  64. #elif defined(__DSBT__)
  65. info.dlpi_addr.map = NULL;
  66. #else
  67. info.dlpi_addr = 0;
  68. #endif
  69. info.dlpi_name = "";
  70. info.dlpi_phdr = _dl_phdr;
  71. info.dlpi_phnum = _dl_phnum;
  72. info.dlpi_adds = 0;
  73. info.dlpi_subs = 0;
  74. /* No easy access to TLS info on the static fallback path; report
  75. 0 / NULL. libsanitizer will skip TLS tracking for this entry,
  76. which is acceptable for a single statically linked program. */
  77. info.dlpi_tls_modid = 0;
  78. info.dlpi_tls_data = NULL;
  79. ret = (*callback) (&info, sizeof (struct dl_phdr_info), data);
  80. if (ret)
  81. return ret;
  82. }
  83. /* Then invoke callback on loaded modules, if any */
  84. return __dl_iterate_phdr (callback, data);
  85. }
  86. # endif
  87. #endif