crtreloc.c 3.8 KB

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