alpha_reloc.c revision 1.16 1 /* $NetBSD: alpha_reloc.c,v 1.16 2002/09/12 20:20:59 mycroft 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
66 #include "rtld.h"
67 #include "debug.h"
68
69 #ifdef RTLD_DEBUG_ALPHA
70 #define adbg(x) if (dodebug) xprintf x
71 #else
72 #define adbg(x) /* nothing */
73 #endif
74
75 void _rtld_bind_start(void);
76 void _rtld_bind_start_old(void);
77 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
78
79 void
80 _rtld_setup_pltgot(const Obj_Entry *obj)
81 {
82 uint32_t word0;
83
84 /*
85 * The PLTGOT on the Alpha looks like this:
86 *
87 * PLT HEADER
88 * .
89 * . 32 bytes
90 * .
91 * PLT ENTRY #0
92 * .
93 * . 12 bytes
94 * .
95 * PLT ENTRY #1
96 * .
97 * . 12 bytes
98 * .
99 * etc.
100 *
101 * The old-format entries look like (displacements filled in
102 * by the linker):
103 *
104 * ldah $28, 0($31) # 0x279f0000
105 * lda $28, 0($28) # 0x239c0000
106 * br $31, plt0 # 0xc3e00000
107 *
108 * The new-format entries look like:
109 *
110 * br $28, plt0 # 0xc3800000
111 * # 0x00000000
112 * # 0x00000000
113 *
114 * What we do is fetch the first PLT entry and check to
115 * see the first word of it matches the first word of the
116 * old format. If so, we use a binding routine that can
117 * handle the old format, otherwise we use a binding routine
118 * that handles the new format.
119 *
120 * Note that this is done on a per-object basis, we can mix
121 * and match shared objects build with both the old and new
122 * linker.
123 */
124 word0 = *(uint32_t *)(((char *) obj->pltgot) + 32);
125 if ((word0 & 0xffff0000) == 0x279f0000) {
126 /* Old PLT entry format. */
127 adbg(("ALPHA: object %p has old PLT format\n", obj));
128 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start_old;
129 obj->pltgot[3] = (Elf_Addr) obj;
130 } else {
131 /* New PLT entry format. */
132 adbg(("ALPHA: object %p has new PLT format\n", obj));
133 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
134 obj->pltgot[3] = (Elf_Addr) obj;
135 }
136
137 __asm __volatile("imb");
138 }
139
140 void
141 _rtld_relocate_nonplt_self(dynp, relocbase)
142 Elf_Dyn *dynp;
143 Elf_Addr relocbase;
144 {
145 const Elf_Rela *rela = 0, *relalim;
146 Elf_Addr relasz = 0;
147 Elf_Addr *where;
148
149 for (; dynp->d_tag != DT_NULL; dynp++) {
150 switch (dynp->d_tag) {
151 case DT_RELA:
152 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
153 break;
154 case DT_RELASZ:
155 relasz = dynp->d_un.d_val;
156 break;
157 }
158 }
159 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
160 for (; rela < relalim; rela++) {
161 where = (Elf_Addr *)(relocbase + rela->r_offset);
162 /* XXX For some reason I see a few GLOB_DAT relocs here. */
163 *where += (Elf_Addr)relocbase;
164 }
165 }
166
167 int
168 _rtld_relocate_nonplt_objects(obj, self, dodebug)
169 const Obj_Entry *obj;
170 bool self;
171 bool dodebug;
172 {
173 const Elf_Rela *rela;
174
175 if (self)
176 return 0;
177
178 for (rela = obj->rela; rela < obj->relalim; rela++) {
179 Elf_Addr *where;
180 const Elf_Sym *def;
181 const Obj_Entry *defobj;
182 Elf_Addr tmp;
183 unsigned long symnum;
184
185 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
186 symnum = ELF_R_SYM(rela->r_info);
187
188 switch (ELF_R_TYPE(rela->r_info)) {
189 case R_TYPE(NONE):
190 break;
191
192 case R_TYPE(REFQUAD):
193 def = _rtld_find_symdef(symnum, obj, &defobj, false);
194 if (def == NULL)
195 return -1;
196
197 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
198 *where + rela->r_addend;
199 if (*where != tmp)
200 *where = tmp;
201 rdbg(dodebug, ("REFQUAD %s in %s --> %p in %s",
202 obj->strtab + obj->symtab[symnum].st_name,
203 obj->path, (void *)*where, defobj->path));
204 break;
205
206 case R_TYPE(GLOB_DAT):
207 def = _rtld_find_symdef(symnum, obj, &defobj, false);
208 if (def == NULL)
209 return -1;
210
211 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
212 rela->r_addend;
213 if (*where != tmp)
214 *where = tmp;
215 rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
216 obj->strtab + obj->symtab[symnum].st_name,
217 obj->path, (void *)*where, defobj->path));
218 break;
219
220 case R_TYPE(RELATIVE):
221 *where += (Elf_Addr)obj->relocbase;
222 rdbg(dodebug, ("RELATIVE in %s --> %p",
223 obj->path, (void *)*where));
224 break;
225
226 case R_TYPE(COPY):
227 /*
228 * These are deferred until all other relocations have
229 * been done. All we do here is make sure that the
230 * COPY relocation is not in a shared library. They
231 * are allowed only in executable files.
232 */
233 if (obj->isdynamic) {
234 _rtld_error(
235 "%s: Unexpected R_COPY relocation in shared library",
236 obj->path);
237 return -1;
238 }
239 rdbg(dodebug, ("COPY (avoid in main)"));
240 break;
241
242 default:
243 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
244 "addend = %p, contents = %p, symbol = %s",
245 symnum, (u_long)ELF_R_TYPE(rela->r_info),
246 (void *)rela->r_offset, (void *)rela->r_addend,
247 (void *)*where,
248 obj->strtab + obj->symtab[symnum].st_name));
249 _rtld_error("%s: Unsupported relocation type %ld "
250 "in non-PLT relocations\n",
251 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
252 return -1;
253 }
254 }
255 return 0;
256 }
257
258 int
259 _rtld_relocate_plt_lazy(obj, dodebug)
260 const Obj_Entry *obj;
261 bool dodebug;
262 {
263 const Elf_Rela *rela;
264
265 if (!obj->isdynamic)
266 return 0;
267
268 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
269 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
270
271 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
272
273 /* Just relocate the GOT slots pointing into the PLT */
274 *where += (Elf_Addr)obj->relocbase;
275 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
276 (void *)*where));
277 }
278
279 return 0;
280 }
281
282 int
283 _rtld_relocate_plt_object(obj, rela, addrp, dodebug)
284 const Obj_Entry *obj;
285 const Elf_Rela *rela;
286 caddr_t *addrp;
287 bool dodebug;
288 {
289 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
290 Elf_Addr new_value;
291 const Elf_Sym *def;
292 const Obj_Entry *defobj;
293 Elf_Addr stubaddr;
294
295 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
296
297 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
298 if (def == NULL)
299 return -1;
300
301 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
302 rdbg(dodebug, ("bind now/fixup in %s --> old=%p new=%p",
303 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
304
305 if ((stubaddr = *where) != new_value) {
306 int64_t delta, idisp;
307 uint32_t insn[3], *stubptr;
308 int insncnt;
309 Elf_Addr pc;
310
311 /* Point this GOT entry at the target. */
312 *where = new_value;
313
314 /*
315 * Alpha shared objects may have multiple GOTs, each
316 * of which may point to this entry in the PLT. But,
317 * we only have a reference to the first GOT entry which
318 * points to this PLT entry. In order to avoid having to
319 * re-bind this call every time a non-first GOT entry is
320 * used, we will attempt to patch up the PLT entry to
321 * reference the target, rather than the binder.
322 *
323 * When the PLT stub gets control, PV contains the address
324 * of the PLT entry. Each PLT entry has room for 3 insns.
325 * If the displacement of the target from PV fits in a signed
326 * 32-bit integer, we can simply add it to PV. Otherwise,
327 * we must load the GOT entry itself into PV.
328 *
329 * Note if the shared object uses the old PLT format, then
330 * we cannot patch up the PLT safely, and so we skip it
331 * in that case[*].
332 *
333 * [*] Actually, if we're not doing lazy-binding, then
334 * we *can* (and do) patch up this PLT entry; the PLTGOT
335 * thunk won't yet point to any binder entry point, and
336 * so this test will fail as it would for the new PLT
337 * entry format.
338 */
339 if (obj->pltgot[2] == (Elf_Addr) &_rtld_bind_start_old) {
340 rdbg(dodebug, (" old PLT format"));
341 goto out;
342 }
343
344 delta = new_value - stubaddr;
345 rdbg(dodebug, (" stubaddr=%p, where-stubaddr=%ld, delta=%ld",
346 (void *)stubaddr, (long)where - (long)stubaddr,
347 (long)delta));
348 insncnt = 0;
349 if ((int32_t)delta == delta) {
350 /*
351 * We can adjust PV with an LDA, LDAH sequence.
352 *
353 * First, build an LDA insn to adjust the low 16
354 * bits.
355 */
356 insn[insncnt++] = 0x08 << 26 | 27 << 21 | 27 << 16 |
357 (delta & 0xffff);
358 rdbg(dodebug, (" LDA $27,%d($27)", (int16_t)delta));
359 /*
360 * Adjust the delta to account for the effects of
361 * the LDA, including sign-extension.
362 */
363 delta -= (int16_t)delta;
364 if (delta != 0) {
365 /*
366 * Build an LDAH instruction to adjust the
367 * high 16 bits.
368 */
369 insn[insncnt++] = 0x09 << 26 | 27 << 21 |
370 27 << 16 | ((delta >> 16) & 0xffff);
371 rdbg(dodebug, (" LDAH $27,%d($27)",
372 (int16_t)(delta >> 16)));
373 }
374 } else {
375 int64_t dhigh;
376
377 /* We must load the GOT entry. */
378 delta = (Elf_Addr)where - stubaddr;
379
380 /*
381 * If the GOT entry is too far away from the PLT
382 * entry, then we can't patch up the PLT entry.
383 * This PLT entry will have to be bound for each
384 * GOT entry except for the first one. This program
385 * will still run, albeit very slowly. It is very
386 * unlikely that this case will ever happen in
387 * practice.
388 */
389 if ((int32_t)delta != delta) {
390 rdbg(dodebug,
391 (" PLT stub too far from GOT to relocate"));
392 goto out;
393 }
394 dhigh = delta - (int16_t)delta;
395 if (dhigh != 0) {
396 /*
397 * Build an LDAH instruction to adjust the
398 * high 16 bits.
399 */
400 insn[insncnt++] = 0x09 << 26 | 27 << 21 |
401 27 << 16 | ((dhigh >> 16) & 0xffff);
402 rdbg(dodebug, (" LDAH $27,%d($27)",
403 (int16_t)(dhigh >> 16)));
404 }
405 /* Build an LDQ to load the GOT entry. */
406 insn[insncnt++] = 0x29 << 26 | 27 << 21 |
407 27 << 16 | (delta & 0xffff);
408 rdbg(dodebug, (" LDQ $27,%d($27)",
409 (int16_t)delta));
410 }
411
412 /*
413 * Now, build a JMP or BR insn to jump to the target. If
414 * the displacement fits in a sign-extended 21-bit field,
415 * we can use the more efficient BR insn. Otherwise, we
416 * have to jump indirect through PV.
417 */
418 pc = stubaddr + (4 * (insncnt + 1));
419 idisp = (int64_t)(new_value - pc) >> 2;
420 if (-0x100000 <= idisp && idisp < 0x100000) {
421 insn[insncnt++] = 0x30 << 26 | 31 << 21 |
422 (idisp & 0x1fffff);
423 rdbg(dodebug, (" BR $31,%p", (void *)new_value));
424 } else {
425 insn[insncnt++] = 0x1a << 26 | 31 << 21 |
426 27 << 16 | (idisp & 0x3fff);
427 rdbg(dodebug, (" JMP $31,($27),%d",
428 (int)(idisp & 0x3fff)));
429 }
430
431 /*
432 * Fill in the tail of the PLT entry first, for reentrancy.
433 * Until we have overwritten the first insn (an unconditional
434 * branch), the remaining insns have no effect.
435 */
436 stubptr = (uint32_t *)stubaddr;
437 while (insncnt > 1) {
438 insncnt--;
439 stubptr[insncnt] = insn[insncnt];
440 }
441 /*
442 * Commit the tail of the insn sequence to memory
443 * before overwriting the first insn.
444 */
445 __asm __volatile("wmb" ::: "memory");
446 stubptr[0] = insn[0];
447 /*
448 * I-stream will be sync'd when we either return from
449 * the binder (lazy bind case) or when the PLTGOT thunk
450 * is patched up (bind-now case).
451 */
452 }
453
454 out:
455 *addrp = (caddr_t)new_value;
456 return 0;
457 }
458