dl-static.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Variable initialization. RISC-V version
  2. Copyright (C) 2001-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library. If not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <ldsodefs.h>
  16. #ifdef SHARED
  17. void
  18. _dl_var_init (void *array[])
  19. {
  20. /* It has to match "variables" below. */
  21. enum
  22. {
  23. DL_PAGESIZE = 0
  24. };
  25. GLRO(dl_pagesize) = *((size_t *) array[DL_PAGESIZE]);
  26. }
  27. #else
  28. static void *variables[] =
  29. {
  30. &GLRO(dl_pagesize)
  31. };
  32. static void
  33. _dl_unprotect_relro (struct link_map *l)
  34. {
  35. ElfW(Addr) start = ((l->l_addr + l->l_relro_addr)
  36. & ~(GLRO(dl_pagesize) - 1));
  37. ElfW(Addr) end = ((l->l_addr + l->l_relro_addr + l->l_relro_size)
  38. & ~(GLRO(dl_pagesize) - 1));
  39. if (start != end)
  40. __mprotect ((void *) start, end - start, PROT_READ | PROT_WRITE);
  41. }
  42. void
  43. _dl_static_init (struct link_map *l)
  44. {
  45. struct link_map *rtld_map = l;
  46. struct r_scope_elem **scope;
  47. const ElfW(Sym) *ref = NULL;
  48. lookup_t loadbase;
  49. void (*f) (void *[]);
  50. size_t i;
  51. loadbase = _dl_lookup_symbol_x ("_dl_var_init", l, &ref, l->l_local_scope,
  52. NULL, 0, 1, NULL);
  53. for (scope = l->l_local_scope; *scope != NULL; scope++)
  54. for (i = 0; i < (*scope)->r_nlist; i++)
  55. if ((*scope)->r_list[i] == loadbase)
  56. {
  57. rtld_map = (*scope)->r_list[i];
  58. break;
  59. }
  60. if (ref != NULL)
  61. {
  62. f = (void (*) (void *[])) DL_SYMBOL_ADDRESS (loadbase, ref);
  63. _dl_unprotect_relro (rtld_map);
  64. f (variables);
  65. _dl_protect_relro (rtld_map);
  66. }
  67. }
  68. #endif