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
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 In addition to the permissions in the GNU Lesser General Public
11 License, the Free Software Foundation gives you unlimited
12 permission to link the compiled version of this file with other
13 programs, and to distribute those programs without any restriction
14 coming from the use of this file. (The GNU Lesser General Public
15 License restrictions do apply in other respects; for example, they
16 cover modification of the file, and distribution when not linked
17 into another program.)
18
19 The GNU C Library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Library General Public License for more details.
23
24 You should have received a copy of the GNU Lesser General Public
25 License along with the GNU C Library; see the file COPYING.LIB. If
26 not, see <http://www.gnu.org/licenses/>. */
27
28 #ifdef __FDPIC__
29
30 #include <sys/types.h>
31 #include <link.h>
32
33 /* This file is to be compiled into crt object files, to enable
34 executables to easily self-relocate. */
35
36 union word {
37 char c[4];
38 void *v;
39 };
40
41 /* Compute the runtime address of pointer in the range [p,e), and then
42 map the pointer pointed by it. */
43 static __always_inline void ***
reloc_range_indirect(void *** p,void *** e,const struct elf32_fdpic_loadmap * map)44 reloc_range_indirect (void ***p, void ***e,
45 const struct elf32_fdpic_loadmap *map)
46 {
47 while (p < e)
48 {
49 if (*p != (void **)-1)
50 {
51 void *ptr = __reloc_pointer (*p, map);
52 if (ptr != (void *)-1)
53 {
54 void *pt;
55 if ((long)ptr & 3)
56 {
57 unsigned char *c = ptr;
58 int i;
59 unsigned long v = 0;
60 for (i = 0; i < 4; i++)
61 v |= c[i] << 8 * i;
62 pt = (void *)v;
63 }
64 else
65 pt = *(void**)ptr;
66 pt = __reloc_pointer (pt, map);
67 if ((long)ptr & 3)
68 {
69 unsigned char *c = ptr;
70 int i;
71 unsigned long v = (unsigned long)pt;
72 for (i = 0; i < 4; i++, v >>= 8)
73 c[i] = v;
74 }
75 else
76 *(void**)ptr = pt;
77 }
78 }
79 p++;
80 }
81 return p;
82 }
83
84 /* Call __reloc_range_indirect for the given range except for the last
85 entry, whose contents are only relocated. It's expected to hold
86 the GOT value. */
87 attribute_hidden void*
__self_reloc(const struct elf32_fdpic_loadmap * map,void *** p,void *** e)88 __self_reloc (const struct elf32_fdpic_loadmap *map,
89 void ***p, void ***e)
90 {
91 p = reloc_range_indirect (p, e-1, map);
92
93 if (p >= e)
94 return (void*)-1;
95
96 return __reloc_pointer (*p, map);
97 }
98
99 #if 0
100 /* These are other functions that might be useful, but that we don't
101 need. */
102
103 /* Remap pointers in [p,e). */
104 static __always_inline void**
105 reloc_range (void **p, void **e,
106 const struct elf32_fdpic_loadmap *map)
107 {
108 while (p < e)
109 {
110 *p = __reloc_pointer (*p, map);
111 p++;
112 }
113 return p;
114 }
115
116 /* Remap p, adjust e by the same offset, then map the pointers in the
117 range determined by them. */
118 void attribute_hidden
119 __reloc_range (const struct elf32_fdpic_loadmap *map,
120 void **p, void **e)
121 {
122 void **old = p;
123
124 p = __reloc_pointer (p, map);
125 e += p - old;
126 reloc_range (p, e, map);
127 }
128
129 /* Remap p, adjust e by the same offset, then map pointers referenced
130 by the (unadjusted) pointers in the range. Return the relocated
131 value of the last pointer in the range. */
132 void* attribute_hidden
133 __reloc_range_indirect (const struct elf32_fdpic_loadmap *map,
134 void ***p, void ***e)
135 {
136 void ***old = p;
137
138 p = __reloc_pointer (p, map);
139 e += p - old;
140 return reloc_range_indirect (p, e, map);
141 }
142 #endif
143
144 #endif /* __FDPIC__ */
145