dl-iterate-phdr.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Get loaded objects program headers.
  2. Copyright (C) 2001,2002,2003,2004,2006,2007 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2001.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License as
  7. published by the Free Software Foundation; either version 2.1 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA.
  17. */
  18. #include <link.h>
  19. #include <ldso.h>
  20. /* we want this in libc but nowhere else */
  21. #ifdef __USE_GNU
  22. extern __typeof(dl_iterate_phdr) __dl_iterate_phdr;
  23. hidden_proto(__dl_iterate_phdr)
  24. int
  25. __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data)
  26. {
  27. struct elf_resolve *l;
  28. struct dl_phdr_info info;
  29. int ret = 0;
  30. for (l = _dl_loaded_modules; l != NULL; l = l->next) {
  31. info.dlpi_addr = l->loadaddr;
  32. info.dlpi_name = l->libname;
  33. info.dlpi_phdr = l->ppnt;
  34. info.dlpi_phnum = l->n_phent;
  35. ret = callback (&info, sizeof (struct dl_phdr_info), data);
  36. if (ret)
  37. break;
  38. }
  39. return ret;
  40. }
  41. hidden_def (__dl_iterate_phdr)
  42. # ifdef SHARED
  43. weak_alias(__dl_iterate_phdr, dl_iterate_phdr)
  44. # else
  45. /* dl-support.c defines these and initializes them early on. */
  46. extern ElfW(Phdr) *_dl_phdr;
  47. extern size_t _dl_phnum;
  48. int
  49. dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
  50. size_t size, void *data), void *data)
  51. {
  52. if (_dl_phnum != 0)
  53. {
  54. /* This entry describes this statically-linked program itself. */
  55. struct dl_phdr_info info;
  56. int ret;
  57. info.dlpi_addr = 0;
  58. info.dlpi_name = "";
  59. info.dlpi_phdr = _dl_phdr;
  60. info.dlpi_phnum = _dl_phnum;
  61. ret = (*callback) (&info, sizeof (struct dl_phdr_info), data);
  62. if (ret)
  63. return ret;
  64. }
  65. /* Then invoke callback on loaded modules, if any */
  66. return __dl_iterate_phdr (callback, data);
  67. }
  68. # endif
  69. #endif