mdreloc.c revision 1.23 1 /* $NetBSD: mdreloc.c,v 1.23 2003/07/26 15:04:38 mrg Exp $ */
2
3 #include <sys/types.h>
4 #include <sys/stat.h>
5
6 #include <string.h>
7
8 #include "debug.h"
9 #include "rtld.h"
10
11 void _rtld_bind_start(void);
12 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
13 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
14
15 void
16 _rtld_setup_pltgot(const Obj_Entry *obj)
17 {
18 obj->pltgot[1] = (Elf_Addr) obj;
19 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
20 }
21
22 void
23 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
24 {
25 const Elf_Rel *rel = 0, *rellim;
26 Elf_Addr relsz = 0;
27 Elf_Addr *where;
28
29 for (; dynp->d_tag != DT_NULL; dynp++) {
30 switch (dynp->d_tag) {
31 case DT_REL:
32 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
33 break;
34 case DT_RELSZ:
35 relsz = dynp->d_un.d_val;
36 break;
37 }
38 }
39 rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
40 for (; rel < rellim; rel++) {
41 where = (Elf_Addr *)(relocbase + rel->r_offset);
42 *where += (Elf_Addr)relocbase;
43 }
44 }
45
46 /*
47 * It is possible for the compiler to emit relocations for unaligned data.
48 * We handle this situation with these inlines.
49 */
50 #define RELOC_ALIGNED_P(x) \
51 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
52
53 static __inline Elf_Addr
54 load_ptr(void *where)
55 {
56 Elf_Addr res;
57
58 memcpy(&res, where, sizeof(res));
59
60 return (res);
61 }
62
63 static __inline void
64 store_ptr(void *where, Elf_Addr val)
65 {
66
67 memcpy(where, &val, sizeof(val));
68 }
69
70 int
71 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
72 {
73 const Elf_Rel *rel;
74
75 for (rel = obj->rel; rel < obj->rellim; rel++) {
76 Elf_Addr *where;
77 const Elf_Sym *def;
78 const Obj_Entry *defobj;
79 Elf_Addr tmp;
80 unsigned long symnum;
81
82 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
83 symnum = ELF_R_SYM(rel->r_info);
84
85 switch (ELF_R_TYPE(rel->r_info)) {
86 case R_TYPE(NONE):
87 break;
88
89 #if 1 /* XXX should not occur */
90 case R_TYPE(PC24): { /* word32 S - P + A */
91 Elf32_Sword addend;
92
93 /*
94 * Extract addend and sign-extend if needed.
95 */
96 addend = *where;
97 if (addend & 0x00800000)
98 addend |= 0xff000000;
99
100 def = _rtld_find_symdef(symnum, obj, &defobj, false);
101 if (def == NULL)
102 return -1;
103 tmp = (Elf_Addr)obj->relocbase + def->st_value
104 - (Elf_Addr)where + (addend << 2);
105 if ((tmp & 0xfe000000) != 0xfe000000 &&
106 (tmp & 0xfe000000) != 0) {
107 _rtld_error(
108 "%s: R_ARM_PC24 relocation @ %p to %s failed "
109 "(displacement %ld (%#lx) out of range)",
110 obj->path, where,
111 obj->strtab + obj->symtab[symnum].st_name,
112 (long) tmp, (long) tmp);
113 return -1;
114 }
115 tmp >>= 2;
116 *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
117 rdbg(("PC24 %s in %s --> %p @ %p in %s",
118 obj->strtab + obj->symtab[symnum].st_name,
119 obj->path, (void *)*where, where, defobj->path));
120 break;
121 }
122 #endif
123
124 case R_TYPE(ABS32): /* word32 B + S + A */
125 case R_TYPE(GLOB_DAT): /* word32 B + S */
126 def = _rtld_find_symdef(symnum, obj, &defobj, false);
127 if (def == NULL)
128 return -1;
129 if (__predict_true(RELOC_ALIGNED_P(where))) {
130 tmp = *where + (Elf_Addr)defobj->relocbase +
131 def->st_value;
132 *where = tmp;
133 } else {
134 tmp = load_ptr(where) +
135 (Elf_Addr)defobj->relocbase +
136 def->st_value;
137 store_ptr(where, tmp);
138 }
139 rdbg(("ABS32/GLOB_DAT %s in %s --> %p @ %p in %s",
140 obj->strtab + obj->symtab[symnum].st_name,
141 obj->path, (void *)tmp, where, defobj->path));
142 break;
143
144 case R_TYPE(RELATIVE): /* word32 B + A */
145 if (__predict_true(RELOC_ALIGNED_P(where))) {
146 tmp = *where + (Elf_Addr)obj->relocbase;
147 *where = tmp;
148 } else {
149 tmp = load_ptr(where) +
150 (Elf_Addr)obj->relocbase;
151 store_ptr(where, tmp);
152 }
153 rdbg(("RELATIVE in %s --> %p", obj->path,
154 (void *)tmp));
155 break;
156
157 case R_TYPE(COPY):
158 /*
159 * These are deferred until all other relocations have
160 * been done. All we do here is make sure that the
161 * COPY relocation is not in a shared library. They
162 * are allowed only in executable files.
163 */
164 if (obj->isdynamic) {
165 _rtld_error(
166 "%s: Unexpected R_COPY relocation in shared library",
167 obj->path);
168 return -1;
169 }
170 rdbg(("COPY (avoid in main)"));
171 break;
172
173 default:
174 rdbg(("sym = %lu, type = %lu, offset = %p, "
175 "contents = %p, symbol = %s",
176 symnum, (u_long)ELF_R_TYPE(rel->r_info),
177 (void *)rel->r_offset, (void *)load_ptr(where),
178 obj->strtab + obj->symtab[symnum].st_name));
179 _rtld_error("%s: Unsupported relocation type %ld "
180 "in non-PLT relocations\n",
181 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
182 return -1;
183 }
184 }
185 return 0;
186 }
187
188 int
189 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
190 {
191 const Elf_Rel *rel;
192
193 if (!obj->relocbase)
194 return 0;
195
196 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
197 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
198
199 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
200
201 /* Just relocate the GOT slots pointing into the PLT */
202 *where += (Elf_Addr)obj->relocbase;
203 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
204 }
205
206 return 0;
207 }
208
209 caddr_t
210 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
211 {
212 const Elf_Rel *rel = (const Elf_Rel *)((caddr_t)obj->pltrel + reloff);
213 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
214 Elf_Addr new_value;
215 const Elf_Sym *def;
216 const Obj_Entry *defobj;
217
218 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
219
220 def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true);
221 if (def == NULL)
222 _rtld_die();
223
224 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
225 rdbg(("bind now/fixup in %s --> old=%p new=%p",
226 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
227 if (*where != new_value)
228 *where = new_value;
229
230 return (caddr_t)new_value;
231 }
232
233 int
234 _rtld_relocate_plt_objects(const Obj_Entry *obj)
235 {
236 const Elf_Rel *rel;
237
238 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
239 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
240 Elf_Addr target;
241 const Elf_Sym *def;
242 const Obj_Entry *defobj;
243
244 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
245
246 def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
247 true);
248 if (def == NULL)
249 return -1;
250 target = (Elf_Addr)(defobj->relocbase + def->st_value);
251 rdbg(("bind now/fixup in %s --> old=%p new=%p",
252 defobj->strtab + def->st_name, (void *)*where,
253 (void *)target));
254 if (*where != target)
255 *where = target;
256 }
257 return 0;
258 }
259