dl-iterate-phdr.c 1.9 KB

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