crtreloc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  2. written by Alexandre Oliva <aoliva@redhat.com>
  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 License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. In addition to the permissions in the GNU Lesser General Public
  9. License, the Free Software Foundation gives you unlimited
  10. permission to link the compiled version of this file with other
  11. programs, and to distribute those programs without any restriction
  12. coming from the use of this file. (The GNU Lesser General Public
  13. License restrictions do apply in other respects; for example, they
  14. cover modification of the file, and distribution when not linked
  15. into another program.)
  16. The GNU C Library is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. Library General Public License for more details.
  20. You should have received a copy of the GNU Lesser General Public
  21. License along with the GNU C Library; see the file COPYING.LIB. If
  22. not, see <http://www.gnu.org/licenses/>. */
  23. #include <sys/types.h>
  24. #include <link.h>
  25. /* This file is to be compiled into crt object files, to enable
  26. executables to easily self-relocate. */
  27. /* Compute the runtime address of pointer in the range [p,e), and then
  28. map the pointer pointed by it. */
  29. static __always_inline void ***
  30. reloc_range_indirect (void ***p, void ***e,
  31. const struct elf32_fdpic_loadmap *map)
  32. {
  33. while (p < e)
  34. {
  35. void *ptr = __reloc_pointer (*p, map);
  36. if (ptr)
  37. {
  38. void *pt;
  39. if ((long)ptr & 3)
  40. __builtin_memcpy(&pt, ptr, sizeof(pt));
  41. else
  42. pt = *(void**)ptr;
  43. pt = __reloc_pointer (pt, map);
  44. if ((long)ptr & 3)
  45. __builtin_memcpy(ptr, &pt, sizeof(pt));
  46. else
  47. *(void**)ptr = pt;
  48. }
  49. p++;
  50. }
  51. return p;
  52. }
  53. /* Call __reloc_range_indirect for the given range except for the last
  54. entry, whose contents are only relocated. It's expected to hold
  55. the GOT value. */
  56. void* attribute_hidden
  57. __self_reloc (const struct elf32_fdpic_loadmap *map,
  58. void ***p, void ***e)
  59. {
  60. p = reloc_range_indirect (p, e-1, map);
  61. if (p >= e)
  62. return (void*)-1;
  63. return __reloc_pointer (*p, map);
  64. }
  65. #if 0
  66. /* These are other functions that might be useful, but that we don't
  67. need. */
  68. /* Remap pointers in [p,e). */
  69. static __always_inline void**
  70. reloc_range (void **p, void **e,
  71. const struct elf32_fdpic_loadmap *map)
  72. {
  73. while (p < e)
  74. {
  75. *p = __reloc_pointer (*p, map);
  76. p++;
  77. }
  78. return p;
  79. }
  80. /* Remap p, adjust e by the same offset, then map the pointers in the
  81. range determined by them. */
  82. void attribute_hidden
  83. __reloc_range (const struct elf32_fdpic_loadmap *map,
  84. void **p, void **e)
  85. {
  86. void **old = p;
  87. p = __reloc_pointer (p, map);
  88. e += p - old;
  89. reloc_range (p, e, map);
  90. }
  91. /* Remap p, adjust e by the same offset, then map pointers referenced
  92. by the (unadjusted) pointers in the range. Return the relocated
  93. value of the last pointer in the range. */
  94. void* attribute_hidden
  95. __reloc_range_indirect (const struct elf32_fdpic_loadmap *map,
  96. void ***p, void ***e)
  97. {
  98. void ***old = p;
  99. p = __reloc_pointer (p, map);
  100. e += p - old;
  101. return reloc_range_indirect (p, e, map);
  102. }
  103. #endif