find_got.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Copyright (C) 2018 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <link.h>
  15. #include <unwind.h>
  16. #if __FDPIC__
  17. #include <bits/elf-fdpic.h>
  18. struct got_callback_data
  19. {
  20. _Unwind_Ptr pc;
  21. _Unwind_Ptr got;
  22. };
  23. static __always_inline int
  24. __dl_addr_in_loadaddr(void *p, struct elf32_fdpic_loadaddr loadaddr)
  25. {
  26. struct elf32_fdpic_loadmap *map = loadaddr.map;
  27. int c;
  28. for (c = 0; c < map->nsegs; c++)
  29. if ((void *)map->segs[c].addr <= p &&
  30. (char *)p < (char *)map->segs[c].addr + map->segs[c].p_memsz)
  31. return 1;
  32. return 0;
  33. }
  34. /* Callback to return the GOT value if the module contains PC. */
  35. static int
  36. find_got_callback (struct dl_phdr_info * info, size_t size, void * ptr)
  37. {
  38. int match = 0;
  39. struct got_callback_data *data = (struct got_callback_data *) ptr;
  40. if (__dl_addr_in_loadaddr((void *) data->pc, info->dlpi_addr)) {
  41. match = 1;
  42. data->got = (_Unwind_Ptr) info->dlpi_addr.got_value;
  43. }
  44. return match;
  45. }
  46. /* Find the GOT that corresponds to the module containing PC. */
  47. _Unwind_Ptr __gnu_Unwind_Find_got (_Unwind_Ptr pc);
  48. _Unwind_Ptr __gnu_Unwind_Find_got (_Unwind_Ptr pc)
  49. {
  50. struct got_callback_data data;
  51. data.pc = pc;
  52. if (dl_iterate_phdr (find_got_callback, &data) <= 0)
  53. return 0;
  54. return data.got;
  55. }
  56. #endif