dl-iterate-phdr.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifdef __FDPIC__
  51. info.dlpi_addr.map = NULL;
  52. info.dlpi_addr.got_value = NULL;
  53. #else
  54. info.dlpi_addr = 0;
  55. #endif
  56. info.dlpi_name = "";
  57. info.dlpi_phdr = _dl_phdr;
  58. info.dlpi_phnum = _dl_phnum;
  59. ret = (*callback) (&info, sizeof (struct dl_phdr_info), data);
  60. if (ret)
  61. return ret;
  62. }
  63. /* Then invoke callback on loaded modules, if any */
  64. return __dl_iterate_phdr (callback, data);
  65. }
  66. # endif
  67. #endif