mdreloc.c revision 1.42 1 /* $NetBSD: mdreloc.c,v 1.42 2017/08/10 19:03:26 joerg Exp $ */
2
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: mdreloc.c,v 1.42 2017/08/10 19:03:26 joerg Exp $");
6 #endif /* not lint */
7
8 #include <sys/types.h>
9 #include <string.h>
10
11 #include "debug.h"
12 #include "rtld.h"
13
14 void _rtld_bind_start(void);
15 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
16 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
17
18 void
19 _rtld_setup_pltgot(const Obj_Entry *obj)
20 {
21 obj->pltgot[1] = (Elf_Addr) obj;
22 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
23 }
24
25 void
26 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
27 {
28 const Elf_Rel *rel = 0, *rellim;
29 Elf_Addr relsz = 0;
30 Elf_Addr *where;
31
32 for (; dynp->d_tag != DT_NULL; dynp++) {
33 switch (dynp->d_tag) {
34 case DT_REL:
35 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
36 break;
37 case DT_RELSZ:
38 relsz = dynp->d_un.d_val;
39 break;
40 }
41 }
42 rellim = (const Elf_Rel *)((const uint8_t *)rel + relsz);
43 for (; rel < rellim; rel++) {
44 where = (Elf_Addr *)(relocbase + rel->r_offset);
45 *where += (Elf_Addr)relocbase;
46 }
47 }
48
49 /*
50 * It is possible for the compiler to emit relocations for unaligned data.
51 * We handle this situation with these inlines.
52 */
53 #define RELOC_ALIGNED_P(x) \
54 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
55
56 static inline Elf_Addr
57 load_ptr(void *where)
58 {
59 Elf_Addr res;
60
61 memcpy(&res, where, sizeof(res));
62
63 return (res);
64 }
65
66 static inline void
67 store_ptr(void *where, Elf_Addr val)
68 {
69
70 memcpy(where, &val, sizeof(val));
71 }
72
73 int
74 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
75 {
76 const Elf_Rel *rel;
77 const Elf_Sym *def = NULL;
78 const Obj_Entry *defobj = NULL;
79 unsigned long last_symnum = ULONG_MAX;
80
81 for (rel = obj->rel; rel < obj->rellim; rel++) {
82 Elf_Addr *where;
83 Elf_Addr tmp;
84 unsigned long symnum;
85
86 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
87
88 switch (ELF_R_TYPE(rel->r_info)) {
89 case R_TYPE(PC24): /* word32 S - P + A */
90 case R_TYPE(ABS32): /* word32 B + S + A */
91 case R_TYPE(GLOB_DAT): /* word32 B + S */
92 case R_TYPE(TLS_DTPOFF32):
93 case R_TYPE(TLS_DTPMOD32):
94 case R_TYPE(TLS_TPOFF32):
95 symnum = ELF_R_SYM(rel->r_info);
96 if (last_symnum != symnum) {
97 last_symnum = symnum;
98 def = _rtld_find_symdef(symnum, obj, &defobj,
99 false);
100 if (def == NULL)
101 return -1;
102 }
103 break;
104
105 default:
106 break;
107 }
108
109 switch (ELF_R_TYPE(rel->r_info)) {
110 case R_TYPE(NONE):
111 break;
112
113 #if 1 /* XXX should not occur */
114 case R_TYPE(PC24): { /* word32 S - P + A */
115 Elf32_Sword addend;
116
117 /*
118 * Extract addend and sign-extend if needed.
119 */
120 addend = *where;
121 if (addend & 0x00800000)
122 addend |= 0xff000000;
123 tmp = (Elf_Addr)obj->relocbase + def->st_value
124 - (Elf_Addr)where + (addend << 2);
125 if ((tmp & 0xfe000000) != 0xfe000000 &&
126 (tmp & 0xfe000000) != 0) {
127 _rtld_error(
128 "%s: R_ARM_PC24 relocation @ %p to %s failed "
129 "(displacement %ld (%#lx) out of range)",
130 obj->path, where,
131 obj->strtab + obj->symtab[
132 ELF_R_SYM(rel->r_info)].st_name,
133 (long) tmp, (long) tmp);
134 return -1;
135 }
136 tmp >>= 2;
137 *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
138 rdbg(("PC24 %s in %s --> %p @ %p in %s",
139 obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
140 .st_name, obj->path, (void *)*where, where,
141 defobj->path));
142 break;
143 }
144 #endif
145
146 case R_TYPE(ABS32): /* word32 B + S + A */
147 case R_TYPE(GLOB_DAT): /* word32 B + S */
148 if (__predict_true(RELOC_ALIGNED_P(where))) {
149 tmp = *where + (Elf_Addr)defobj->relocbase +
150 def->st_value;
151 /* Set the Thumb bit, if needed. */
152 if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
153 tmp |= 1;
154 *where = tmp;
155 } else {
156 tmp = load_ptr(where) +
157 (Elf_Addr)defobj->relocbase +
158 def->st_value;
159 /* Set the Thumb bit, if needed. */
160 if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
161 tmp |= 1;
162 store_ptr(where, tmp);
163 }
164 rdbg(("ABS32/GLOB_DAT %s in %s --> %p @ %p in %s",
165 obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
166 .st_name, obj->path, (void *)tmp, where,
167 defobj->path));
168 break;
169
170 case R_TYPE(RELATIVE): /* word32 B + A */
171 if (__predict_true(RELOC_ALIGNED_P(where))) {
172 tmp = *where + (Elf_Addr)obj->relocbase;
173 *where = tmp;
174 } else {
175 tmp = load_ptr(where) +
176 (Elf_Addr)obj->relocbase;
177 store_ptr(where, tmp);
178 }
179 rdbg(("RELATIVE in %s --> %p", obj->path,
180 (void *)tmp));
181 break;
182
183 case R_TYPE(COPY):
184 /*
185 * These are deferred until all other relocations have
186 * been done. All we do here is make sure that the
187 * COPY relocation is not in a shared library. They
188 * are allowed only in executable files.
189 */
190 if (obj->isdynamic) {
191 _rtld_error(
192 "%s: Unexpected R_COPY relocation in shared library",
193 obj->path);
194 return -1;
195 }
196 rdbg(("COPY (avoid in main)"));
197 break;
198
199 case R_TYPE(TLS_DTPOFF32):
200 tmp = (Elf_Addr)(def->st_value);
201 if (__predict_true(RELOC_ALIGNED_P(where)))
202 *where = tmp;
203 else
204 store_ptr(where, tmp);
205
206 rdbg(("TLS_DTPOFF32 %s in %s --> %p",
207 obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
208 .st_name, obj->path, (void *)tmp));
209
210 break;
211 case R_TYPE(TLS_DTPMOD32):
212 tmp = (Elf_Addr)(defobj->tlsindex);
213 if (__predict_true(RELOC_ALIGNED_P(where)))
214 *where = tmp;
215 else
216 store_ptr(where, tmp);
217
218 rdbg(("TLS_DTPMOD32 %s in %s --> %p",
219 obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
220 .st_name, obj->path, (void *)tmp));
221
222 break;
223
224 case R_TYPE(TLS_TPOFF32):
225 if (!defobj->tls_done &&
226 _rtld_tls_offset_allocate(obj))
227 return -1;
228
229 tmp = (Elf_Addr)def->st_value + defobj->tlsoffset +
230 sizeof(struct tls_tcb);
231 if (__predict_true(RELOC_ALIGNED_P(where)))
232 *where = tmp;
233 else
234 store_ptr(where, tmp);
235 rdbg(("TLS_TPOFF32 %s in %s --> %p",
236 obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
237 .st_name, obj->path, (void *)tmp));
238 break;
239
240 default:
241 rdbg(("sym = %lu, type = %lu, offset = %p, "
242 "contents = %p",
243 (u_long)ELF_R_SYM(rel->r_info),
244 (u_long)ELF_R_TYPE(rel->r_info),
245 (void *)rel->r_offset, (void *)load_ptr(where)));
246 _rtld_error("%s: Unsupported relocation type %ld "
247 "in non-PLT relocations",
248 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
249 return -1;
250 }
251 }
252 return 0;
253 }
254
255 int
256 _rtld_relocate_plt_lazy(Obj_Entry *obj)
257 {
258 const Elf_Rel *rel;
259
260 if (!obj->relocbase)
261 return 0;
262
263 for (rel = obj->pltrellim; rel-- > obj->pltrel; ) {
264 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
265
266 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT) ||
267 ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE));
268
269 if (ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE))
270 obj->ifunc_remaining = obj->pltrellim - rel;
271
272 /* Just relocate the GOT slots pointing into the PLT */
273 *where += (Elf_Addr)obj->relocbase;
274 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
275 }
276
277 return 0;
278 }
279
280 void
281 _rtld_call_ifunc(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
282 {
283 const Elf_Rel *rel;
284 Elf_Addr *where, target;
285
286 while (obj->ifunc_remaining > 0 && _rtld_objgen == cur_objgen) {
287 rel = obj->pltrellim - obj->ifunc_remaining;
288 --obj->ifunc_remaining;
289 if (ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE)) {
290 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
291 _rtld_exclusive_exit(mask);
292 target = _rtld_resolve_ifunc2(obj, *where);
293 _rtld_exclusive_enter(mask);
294 if (*where != target)
295 *where = target;
296 }
297 }
298 }
299
300 static int
301 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
302 Elf_Addr *tp)
303 {
304 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
305 Elf_Addr new_value;
306 const Elf_Sym *def;
307 const Obj_Entry *defobj;
308 unsigned long info = rel->r_info;
309
310 assert(ELF_R_TYPE(info) == R_TYPE(JUMP_SLOT));
311
312 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
313 if (__predict_false(def == NULL))
314 return -1;
315 if (__predict_false(def == &_rtld_sym_zero))
316 return 0;
317
318 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
319 if (tp == NULL)
320 return 0;
321 new_value = _rtld_resolve_ifunc(defobj, def);
322 } else {
323 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
324 }
325 /* Set the Thumb bit, if needed. */
326 if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
327 new_value |= 1;
328 rdbg(("bind now/fixup in %s --> old=%p new=%p",
329 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
330 if (*where != new_value)
331 *where = new_value;
332 if (tp)
333 *tp = new_value;
334
335 return 0;
336 }
337
338 caddr_t
339 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
340 {
341 const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel + reloff);
342 Elf_Addr new_value = 0; /* XXX gcc */
343 int err;
344
345 _rtld_shared_enter();
346 err = _rtld_relocate_plt_object(obj, rel, &new_value);
347 if (err)
348 _rtld_die();
349 _rtld_shared_exit();
350
351 return (caddr_t)new_value;
352 }
353 int
354 _rtld_relocate_plt_objects(const Obj_Entry *obj)
355 {
356 const Elf_Rel *rel;
357 int err = 0;
358
359 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
360 err = _rtld_relocate_plt_object(obj, rel, NULL);
361 if (err)
362 break;
363 }
364
365 return err;
366 }
367