alpha_reloc.c revision 1.25 1 /* $NetBSD: alpha_reloc.c,v 1.25 2005/05/25 13:39:46 skrll Exp $ */
2
3 /*
4 * Copyright (c) 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Copyright 1996, 1997, 1998, 1999 John D. Polstra.
40 * All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 */
62
63 #include <sys/types.h>
64 #include <sys/stat.h>
65 #include <string.h>
66
67 #include "rtld.h"
68 #include "debug.h"
69
70 #ifdef RTLD_DEBUG_ALPHA
71 #define adbg(x) xprintf x
72 #else
73 #define adbg(x) /* nothing */
74 #endif
75
76 void _rtld_bind_start(void);
77 void _rtld_bind_start_old(void);
78 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
79 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
80 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
81 const Elf_Rela *, Elf_Addr *tp);
82
83
84 void
85 _rtld_setup_pltgot(const Obj_Entry *obj)
86 {
87 uint32_t word0;
88
89 /*
90 * The PLTGOT on the Alpha looks like this:
91 *
92 * PLT HEADER
93 * .
94 * . 32 bytes
95 * .
96 * PLT ENTRY #0
97 * .
98 * . 12 bytes
99 * .
100 * PLT ENTRY #1
101 * .
102 * . 12 bytes
103 * .
104 * etc.
105 *
106 * The old-format entries look like (displacements filled in
107 * by the linker):
108 *
109 * ldah $28, 0($31) # 0x279f0000
110 * lda $28, 0($28) # 0x239c0000
111 * br $31, plt0 # 0xc3e00000
112 *
113 * The new-format entries look like:
114 *
115 * br $28, plt0 # 0xc3800000
116 * # 0x00000000
117 * # 0x00000000
118 *
119 * What we do is fetch the first PLT entry and check to
120 * see the first word of it matches the first word of the
121 * old format. If so, we use a binding routine that can
122 * handle the old format, otherwise we use a binding routine
123 * that handles the new format.
124 *
125 * Note that this is done on a per-object basis, we can mix
126 * and match shared objects build with both the old and new
127 * linker.
128 */
129 word0 = *(uint32_t *)(((char *) obj->pltgot) + 32);
130 if ((word0 & 0xffff0000) == 0x279f0000) {
131 /* Old PLT entry format. */
132 adbg(("ALPHA: object %p has old PLT format\n", obj));
133 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start_old;
134 obj->pltgot[3] = (Elf_Addr) obj;
135 } else {
136 /* New PLT entry format. */
137 adbg(("ALPHA: object %p has new PLT format\n", obj));
138 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
139 obj->pltgot[3] = (Elf_Addr) obj;
140 }
141
142 __asm __volatile("imb");
143 }
144
145 /*
146 * It is possible for the compiler to emit relocations for unaligned data.
147 * We handle this situation with these inlines.
148 */
149 #define RELOC_ALIGNED_P(x) \
150 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
151
152 static __inline Elf_Addr
153 load_ptr(void *where)
154 {
155 Elf_Addr res;
156
157 memcpy(&res, where, sizeof(res));
158
159 return (res);
160 }
161
162 static __inline void
163 store_ptr(void *where, Elf_Addr val)
164 {
165
166 memcpy(where, &val, sizeof(val));
167 }
168
169 void
170 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
171 {
172 const Elf_Rela *rela = 0, *relalim;
173 Elf_Addr relasz = 0;
174 Elf_Addr *where;
175
176 for (; dynp->d_tag != DT_NULL; dynp++) {
177 switch (dynp->d_tag) {
178 case DT_RELA:
179 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
180 break;
181 case DT_RELASZ:
182 relasz = dynp->d_un.d_val;
183 break;
184 }
185 }
186 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
187 for (; rela < relalim; rela++) {
188 where = (Elf_Addr *)(relocbase + rela->r_offset);
189 /* XXX For some reason I see a few GLOB_DAT relocs here. */
190 *where += (Elf_Addr)relocbase;
191 }
192 }
193
194 int
195 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
196 {
197 const Elf_Rela *rela;
198 #define COMBRELOC
199 #ifdef COMBRELOC
200 unsigned long lastsym = -1;
201 #endif
202 Elf_Addr target;
203
204 for (rela = obj->rela; rela < obj->relalim; rela++) {
205 Elf_Addr *where;
206 const Elf_Sym *def;
207 const Obj_Entry *defobj;
208 Elf_Addr tmp;
209 unsigned long symnum;
210
211 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
212 symnum = ELF_R_SYM(rela->r_info);
213
214 switch (ELF_R_TYPE(rela->r_info)) {
215 case R_TYPE(NONE):
216 break;
217
218 case R_TYPE(REFQUAD):
219 case R_TYPE(GLOB_DAT):
220 #ifdef COMBRELOC
221 if (symnum != lastsym) {
222 #endif
223 def = _rtld_find_symdef(symnum, obj, &defobj,
224 false);
225 if (def == NULL)
226 return -1;
227 target = (Elf_Addr)(defobj->relocbase +
228 def->st_value);
229 #ifdef COMBRELOC
230 lastsym = symnum;
231 }
232 #endif
233
234 tmp = target + rela->r_addend;
235 if (__predict_true(RELOC_ALIGNED_P(where))) {
236 if (*where != tmp)
237 *where = tmp;
238 } else {
239 if (load_ptr(where) != tmp)
240 store_ptr(where, tmp);
241 }
242 rdbg(("REFQUAD/GLOB_DAT %s in %s --> %p in %s",
243 obj->strtab + obj->symtab[symnum].st_name,
244 obj->path, (void *)tmp, defobj->path));
245 break;
246
247 case R_TYPE(RELATIVE):
248 if (__predict_true(RELOC_ALIGNED_P(where)))
249 *where += (Elf_Addr)obj->relocbase;
250 else
251 store_ptr(where,
252 load_ptr(where) + (Elf_Addr)obj->relocbase);
253 rdbg(("RELATIVE in %s --> %p", obj->path,
254 (void *)*where));
255 break;
256
257 case R_TYPE(COPY):
258 /*
259 * These are deferred until all other relocations have
260 * been done. All we do here is make sure that the
261 * COPY relocation is not in a shared library. They
262 * are allowed only in executable files.
263 */
264 if (obj->isdynamic) {
265 _rtld_error(
266 "%s: Unexpected R_COPY relocation in shared library",
267 obj->path);
268 return -1;
269 }
270 rdbg(("COPY (avoid in main)"));
271 break;
272
273 default:
274 rdbg(("sym = %lu, type = %lu, offset = %p, "
275 "addend = %p, contents = %p, symbol = %s",
276 symnum, (u_long)ELF_R_TYPE(rela->r_info),
277 (void *)rela->r_offset, (void *)rela->r_addend,
278 (void *)load_ptr(where),
279 obj->strtab + obj->symtab[symnum].st_name));
280 _rtld_error("%s: Unsupported relocation type %ld "
281 "in non-PLT relocations\n",
282 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
283 return -1;
284 }
285 }
286 return 0;
287 }
288
289 int
290 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
291 {
292 const Elf_Rela *rela;
293
294 if (!obj->relocbase)
295 return 0;
296
297 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
298 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
299
300 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
301
302 /* Just relocate the GOT slots pointing into the PLT */
303 *where += (Elf_Addr)obj->relocbase;
304 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
305 }
306
307 return 0;
308 }
309
310 static inline int
311 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
312 {
313 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
314 Elf_Addr new_value;
315 const Elf_Sym *def;
316 const Obj_Entry *defobj;
317 Elf_Addr stubaddr;
318
319 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
320
321 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
322 if (def == NULL)
323 return -1;
324
325 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
326 rdbg(("bind now/fixup in %s --> old=%p new=%p",
327 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
328
329 if ((stubaddr = *where) != new_value) {
330 int64_t delta, idisp;
331 uint32_t insn[3], *stubptr;
332 int insncnt;
333 Elf_Addr pc;
334
335 /* Point this GOT entry at the target. */
336 *where = new_value;
337
338 /*
339 * Alpha shared objects may have multiple GOTs, each
340 * of which may point to this entry in the PLT. But,
341 * we only have a reference to the first GOT entry which
342 * points to this PLT entry. In order to avoid having to
343 * re-bind this call every time a non-first GOT entry is
344 * used, we will attempt to patch up the PLT entry to
345 * reference the target, rather than the binder.
346 *
347 * When the PLT stub gets control, PV contains the address
348 * of the PLT entry. Each PLT entry has room for 3 insns.
349 * If the displacement of the target from PV fits in a signed
350 * 32-bit integer, we can simply add it to PV. Otherwise,
351 * we must load the GOT entry itself into PV.
352 *
353 * Note if the shared object uses the old PLT format, then
354 * we cannot patch up the PLT safely, and so we skip it
355 * in that case[*].
356 *
357 * [*] Actually, if we're not doing lazy-binding, then
358 * we *can* (and do) patch up this PLT entry; the PLTGOT
359 * thunk won't yet point to any binder entry point, and
360 * so this test will fail as it would for the new PLT
361 * entry format.
362 */
363 if (obj->pltgot[2] == (Elf_Addr) &_rtld_bind_start_old) {
364 rdbg((" old PLT format"));
365 goto out;
366 }
367
368 delta = new_value - stubaddr;
369 rdbg((" stubaddr=%p, where-stubaddr=%ld, delta=%ld",
370 (void *)stubaddr, (long)where - (long)stubaddr,
371 (long)delta));
372 insncnt = 0;
373 if ((int32_t)delta == delta) {
374 /*
375 * We can adjust PV with an LDA, LDAH sequence.
376 *
377 * First, build an LDA insn to adjust the low 16
378 * bits.
379 */
380 insn[insncnt++] = 0x08 << 26 | 27 << 21 | 27 << 16 |
381 (delta & 0xffff);
382 rdbg((" LDA $27,%d($27)", (int16_t)delta));
383 /*
384 * Adjust the delta to account for the effects of
385 * the LDA, including sign-extension.
386 */
387 delta -= (int16_t)delta;
388 if (delta != 0) {
389 /*
390 * Build an LDAH instruction to adjust the
391 * high 16 bits.
392 */
393 insn[insncnt++] = 0x09 << 26 | 27 << 21 |
394 27 << 16 | ((delta >> 16) & 0xffff);
395 rdbg((" LDAH $27,%d($27)",
396 (int16_t)(delta >> 16)));
397 }
398 } else {
399 int64_t dhigh;
400
401 /* We must load the GOT entry. */
402 delta = (Elf_Addr)where - stubaddr;
403
404 /*
405 * If the GOT entry is too far away from the PLT
406 * entry, then we can't patch up the PLT entry.
407 * This PLT entry will have to be bound for each
408 * GOT entry except for the first one. This program
409 * will still run, albeit very slowly. It is very
410 * unlikely that this case will ever happen in
411 * practice.
412 */
413 if ((int32_t)delta != delta) {
414 rdbg((" PLT stub too far from GOT to relocate"));
415 goto out;
416 }
417 dhigh = delta - (int16_t)delta;
418 if (dhigh != 0) {
419 /*
420 * Build an LDAH instruction to adjust the
421 * high 16 bits.
422 */
423 insn[insncnt++] = 0x09 << 26 | 27 << 21 |
424 27 << 16 | ((dhigh >> 16) & 0xffff);
425 rdbg((" LDAH $27,%d($27)",
426 (int16_t)(dhigh >> 16)));
427 }
428 /* Build an LDQ to load the GOT entry. */
429 insn[insncnt++] = 0x29 << 26 | 27 << 21 |
430 27 << 16 | (delta & 0xffff);
431 rdbg((" LDQ $27,%d($27)",
432 (int16_t)delta));
433 }
434
435 /*
436 * Now, build a JMP or BR insn to jump to the target. If
437 * the displacement fits in a sign-extended 21-bit field,
438 * we can use the more efficient BR insn. Otherwise, we
439 * have to jump indirect through PV.
440 */
441 pc = stubaddr + (4 * (insncnt + 1));
442 idisp = (int64_t)(new_value - pc) >> 2;
443 if (-0x100000 <= idisp && idisp < 0x100000) {
444 insn[insncnt++] = 0x30 << 26 | 31 << 21 |
445 (idisp & 0x1fffff);
446 rdbg((" BR $31,%p", (void *)new_value));
447 } else {
448 insn[insncnt++] = 0x1a << 26 | 31 << 21 |
449 27 << 16 | (idisp & 0x3fff);
450 rdbg((" JMP $31,($27),%d",
451 (int)(idisp & 0x3fff)));
452 }
453
454 /*
455 * Fill in the tail of the PLT entry first, for reentrancy.
456 * Until we have overwritten the first insn (an unconditional
457 * branch), the remaining insns have no effect.
458 */
459 stubptr = (uint32_t *)stubaddr;
460 while (insncnt > 1) {
461 insncnt--;
462 stubptr[insncnt] = insn[insncnt];
463 }
464 /*
465 * Commit the tail of the insn sequence to memory
466 * before overwriting the first insn.
467 */
468 __asm __volatile("wmb" ::: "memory");
469 stubptr[0] = insn[0];
470 /*
471 * I-stream will be sync'd when we either return from
472 * the binder (lazy bind case) or when the PLTGOT thunk
473 * is patched up (bind-now case).
474 */
475 }
476 out:
477 if (tp)
478 *tp = new_value;
479
480 return 0;
481 }
482
483 caddr_t
484 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
485 {
486 const Elf_Rela *rela = (const Elf_Rela *)((caddr_t)obj->pltrela + reloff);
487 Elf_Addr result;
488 int err;
489
490 err = _rtld_relocate_plt_object(obj, rela, &result);
491 if (err)
492 _rtld_die();
493
494 return (caddr_t)result;
495 }
496
497 int
498 _rtld_relocate_plt_objects(const Obj_Entry *obj)
499 {
500 const Elf_Rela *rela;
501
502 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
503 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
504 return -1;
505
506 return 0;
507 }
508