dl-iterate-phdr.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. extern __typeof(dl_iterate_phdr) __dl_iterate_phdr;
  14. hidden_proto(__dl_iterate_phdr)
  15. int
  16. __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data)
  17. {
  18. int ret = 0;
  19. #ifndef __ARCH_HAS_NO_SHARED__
  20. struct elf_resolve *l;
  21. struct dl_phdr_info info;
  22. for (l = _dl_loaded_modules; l != NULL; l = l->next) {
  23. info.dlpi_addr = l->loadaddr;
  24. info.dlpi_name = l->libname;
  25. info.dlpi_phdr = l->ppnt;
  26. info.dlpi_phnum = l->n_phent;
  27. ret = callback (&info, sizeof (struct dl_phdr_info), data);
  28. if (ret)
  29. break;
  30. }
  31. #endif
  32. return ret;
  33. }
  34. hidden_def (__dl_iterate_phdr)
  35. # ifdef SHARED
  36. weak_alias(__dl_iterate_phdr, dl_iterate_phdr)
  37. # else
  38. /* dl-support.c defines these and initializes them early on. */
  39. extern ElfW(Phdr) *_dl_phdr;
  40. extern size_t _dl_phnum;
  41. int
  42. dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
  43. size_t size, void *data), void *data)
  44. {
  45. if (_dl_phnum != 0)
  46. {
  47. /* This entry describes this statically-linked program itself. */
  48. struct dl_phdr_info info;
  49. int ret;
  50. info.dlpi_addr = 0;
  51. info.dlpi_name = "";
  52. info.dlpi_phdr = _dl_phdr;
  53. info.dlpi_phnum = _dl_phnum;
  54. ret = (*callback) (&info, sizeof (struct dl_phdr_info), data);
  55. if (ret)
  56. return ret;
  57. }
  58. /* Then invoke callback on loaded modules, if any */
  59. return __dl_iterate_phdr (callback, data);
  60. }
  61. # endif
  62. #endif