crtreloc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. 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. Library 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; see the file COPYING.LIB. If
  14. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  15. Cambridge, MA 02139, USA. */
  16. #define _GNU_SOURCE 1
  17. #include <sys/types.h>
  18. #include <link.h>
  19. /* This file is to be compiled into crt object files, to enable
  20. executables to easily self-relocate. */
  21. #define hidden __attribute__((__visibility__("hidden")))
  22. /* Compute the runtime address of pointer in the range [p,e), and then
  23. map the pointer pointed by it. */
  24. inline static void ***
  25. reloc_range_indirect (void ***p, void ***e,
  26. const struct elf32_fdpic_loadmap *map)
  27. {
  28. while (p < e)
  29. {
  30. void *ptr = __reloc_pointer (*p, map);
  31. if (ptr)
  32. {
  33. void *pt;
  34. if ((long)ptr & 3)
  35. __builtin_memcpy(&pt, ptr, sizeof(pt));
  36. else
  37. pt = *(void**)ptr;
  38. pt = __reloc_pointer (pt, map);
  39. if ((long)ptr & 3)
  40. __builtin_memcpy(ptr, &pt, sizeof(pt));
  41. else
  42. *(void**)ptr = pt;
  43. }
  44. p++;
  45. }
  46. return p;
  47. }
  48. /* Call __reloc_range_indirect for the given range except for the last
  49. entry, whose contents are only relocated. It's expected to hold
  50. the GOT value. */
  51. void* hidden
  52. __self_reloc (const struct elf32_fdpic_loadmap *map,
  53. void ***p, void ***e)
  54. {
  55. p = reloc_range_indirect (p, e-1, map);
  56. if (p >= e)
  57. return (void*)-1;
  58. return __reloc_pointer (*p, map);
  59. }
  60. #if 0
  61. /* These are other functions that might be useful, but that we don't
  62. need. */
  63. /* Remap pointers in [p,e). */
  64. inline static void**
  65. reloc_range (void **p, void **e,
  66. const struct elf32_fdpic_loadmap *map)
  67. {
  68. while (p < e)
  69. {
  70. *p = __reloc_pointer (*p, map);
  71. p++;
  72. }
  73. return p;
  74. }
  75. /* Remap p, adjust e by the same offset, then map the pointers in the
  76. range determined by them. */
  77. void hidden
  78. __reloc_range (const struct elf32_fdpic_loadmap *map,
  79. void **p, void **e)
  80. {
  81. void **old = p;
  82. p = __reloc_pointer (p, map);
  83. e += p - old;
  84. reloc_range (p, e, map);
  85. }
  86. /* Remap p, adjust e by the same offset, then map pointers referenced
  87. by the (unadjusted) pointers in the range. Return the relocated
  88. value of the last pointer in the range. */
  89. void* hidden
  90. __reloc_range_indirect (const struct elf32_fdpic_loadmap *map,
  91. void ***p, void ***e)
  92. {
  93. void ***old = p;
  94. p = __reloc_pointer (p, map);
  95. e += p - old;
  96. return reloc_range_indirect (p, e, map);
  97. }
  98. #endif