crtreloc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #ifdef __FDPIC__
  24. #include <sys/types.h>
  25. #include <link.h>
  26. /* This file is to be compiled into crt object files, to enable
  27. executables to easily self-relocate. */
  28. union word {
  29. char c[4];
  30. void *v;
  31. };
  32. /* Compute the runtime address of pointer in the range [p,e), and then
  33. map the pointer pointed by it. */
  34. static __always_inline void ***
  35. reloc_range_indirect (void ***p, void ***e,
  36. const struct elf32_fdpic_loadmap *map)
  37. {
  38. while (p < e)
  39. {
  40. if (*p != (void **)-1)
  41. {
  42. void *ptr = __reloc_pointer (*p, map);
  43. if (ptr != (void *)-1)
  44. {
  45. void *pt;
  46. if ((long)ptr & 3)
  47. {
  48. unsigned char *c = ptr;
  49. int i;
  50. unsigned long v = 0;
  51. for (i = 0; i < 4; i++)
  52. v |= c[i] << 8 * i;
  53. pt = (void *)v;
  54. }
  55. else
  56. pt = *(void**)ptr;
  57. pt = __reloc_pointer (pt, map);
  58. if ((long)ptr & 3)
  59. {
  60. unsigned char *c = ptr;
  61. int i;
  62. unsigned long v = (unsigned long)pt;
  63. for (i = 0; i < 4; i++, v >>= 8)
  64. c[i] = v;
  65. }
  66. else
  67. *(void**)ptr = pt;
  68. }
  69. }
  70. p++;
  71. }
  72. return p;
  73. }
  74. /* Call __reloc_range_indirect for the given range except for the last
  75. entry, whose contents are only relocated. It's expected to hold
  76. the GOT value. */
  77. attribute_hidden void*
  78. __self_reloc (const struct elf32_fdpic_loadmap *map,
  79. void ***p, void ***e)
  80. {
  81. p = reloc_range_indirect (p, e-1, map);
  82. if (p >= e)
  83. return (void*)-1;
  84. return __reloc_pointer (*p, map);
  85. }
  86. #if 0
  87. /* These are other functions that might be useful, but that we don't
  88. need. */
  89. /* Remap pointers in [p,e). */
  90. static __always_inline void**
  91. reloc_range (void **p, void **e,
  92. const struct elf32_fdpic_loadmap *map)
  93. {
  94. while (p < e)
  95. {
  96. *p = __reloc_pointer (*p, map);
  97. p++;
  98. }
  99. return p;
  100. }
  101. /* Remap p, adjust e by the same offset, then map the pointers in the
  102. range determined by them. */
  103. void attribute_hidden
  104. __reloc_range (const struct elf32_fdpic_loadmap *map,
  105. void **p, void **e)
  106. {
  107. void **old = p;
  108. p = __reloc_pointer (p, map);
  109. e += p - old;
  110. reloc_range (p, e, map);
  111. }
  112. /* Remap p, adjust e by the same offset, then map pointers referenced
  113. by the (unadjusted) pointers in the range. Return the relocated
  114. value of the last pointer in the range. */
  115. void* attribute_hidden
  116. __reloc_range_indirect (const struct elf32_fdpic_loadmap *map,
  117. void ***p, void ***e)
  118. {
  119. void ***old = p;
  120. p = __reloc_pointer (p, map);
  121. e += p - old;
  122. return reloc_range_indirect (p, e, map);
  123. }
  124. #endif
  125. #endif /* __FDPIC__ */