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