dl-iterate-phdr.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. ret = callback (&info, sizeof (struct dl_phdr_info), data);
  26. if (ret)
  27. break;
  28. }
  29. #endif
  30. return ret;
  31. }
  32. # ifdef SHARED
  33. weak_alias(__dl_iterate_phdr, dl_iterate_phdr)
  34. # else
  35. /* dl-support.c defines these and initializes them early on. */
  36. extern ElfW(Phdr) *_dl_phdr;
  37. extern size_t _dl_phnum;
  38. int
  39. dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
  40. size_t size, void *data), void *data)
  41. {
  42. if (_dl_phnum != 0)
  43. {
  44. /* This entry describes this statically-linked program itself. */
  45. struct dl_phdr_info info;
  46. int ret;
  47. #if defined(__FRV_FDPIC__) || defined(__BFIN_FDPIC__) || defined(__FDPIC__)
  48. info.dlpi_addr.map = NULL;
  49. info.dlpi_addr.got_value = NULL;
  50. #elif defined(__DSBT__)
  51. info.dlpi_addr.map = NULL;
  52. #else
  53. info.dlpi_addr = 0;
  54. #endif
  55. info.dlpi_name = "";
  56. info.dlpi_phdr = _dl_phdr;
  57. info.dlpi_phnum = _dl_phnum;
  58. ret = (*callback) (&info, sizeof (struct dl_phdr_info), data);
  59. if (ret)
  60. return ret;
  61. }
  62. /* Then invoke callback on loaded modules, if any */
  63. return __dl_iterate_phdr (callback, data);
  64. }
  65. # endif
  66. #endif