crtreloc.c 3.1 KB

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