alpha_reloc.c revision 1.46 1 /* $NetBSD: alpha_reloc.c,v 1.46 2025/04/18 17:56:49 riastradh 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/cdefs.h>
64 #ifndef lint
65 __RCSID("$NetBSD: alpha_reloc.c,v 1.46 2025/04/18 17:56:49 riastradh Exp $");
66 #endif /* not lint */
67
68 #include <sys/types.h>
69 #include <sys/tls.h>
70 #include <string.h>
71
72 #include "rtld.h"
73 #include "debug.h"
74
75 #ifdef RTLD_DEBUG_ALPHA
76 #define adbg(x) xprintf x
77 #else
78 #define adbg(x) /* nothing */
79 #endif
80
81 void _rtld_bind_start_secureplt(void);
82 void _rtld_bind_start(void);
83 void _rtld_bind_start_old(void);
84 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
85 caddr_t _rtld_bind(const Obj_Entry *, Elf_Addr);
86 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
87 const Elf_Rela *, Elf_Addr *);
88
89 void
90 _rtld_setup_pltgot(const Obj_Entry *obj)
91 {
92 uint32_t word0;
93
94 /*
95 * If we're using Alpha secureplt, the PLTGOT points to the
96 * .got.plt section. Just fill in the rtld binding stub and
97 * we're done -- we're not writing to instruction memory, so no
98 * imb needed.
99 */
100 if (obj->secureplt) {
101 obj->pltgot[0] = (Elf_Addr) _rtld_bind_start_secureplt;
102 obj->pltgot[1] = (Elf_Addr) obj;
103 return;
104 }
105
106 /*
107 * The non-secureplt PLTGOT on the Alpha looks like this:
108 *
109 * PLT HEADER
110 * .
111 * . 32 bytes
112 * .
113 * PLT ENTRY #0
114 * .
115 * . 12 bytes
116 * .
117 * PLT ENTRY #1
118 * .
119 * . 12 bytes
120 * .
121 * etc.
122 *
123 * The old-format entries look like (displacements filled in
124 * by the linker):
125 *
126 * ldah $28, 0($31) # 0x279f0000
127 * lda $28, 0($28) # 0x239c0000
128 * br $31, plt0 # 0xc3e00000
129 *
130 * The new-format entries look like:
131 *
132 * br $28, plt0 # 0xc3800000
133 * # 0x00000000
134 * # 0x00000000
135 *
136 * What we do is fetch the first PLT entry and check to
137 * see the first word of it matches the first word of the
138 * old format. If so, we use a binding routine that can
139 * handle the old format, otherwise we use a binding routine
140 * that handles the new format.
141 *
142 * Note that this is done on a per-object basis, we can mix
143 * and match shared objects build with both the old and new
144 * linker.
145 */
146 word0 = *(uint32_t *)(((char *) obj->pltgot) + 32);
147 if ((word0 & 0xffff0000) == 0x279f0000) {
148 /* Old PLT entry format. */
149 adbg(("ALPHA: object %p has old PLT format\n", obj));
150 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start_old;
151 obj->pltgot[3] = (Elf_Addr) obj;
152 } else {
153 /* New PLT entry format. */
154 adbg(("ALPHA: object %p has new PLT format\n", obj));
155 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
156 obj->pltgot[3] = (Elf_Addr) obj;
157 }
158
159 __asm volatile("imb");
160 }
161
162 /*
163 * It is possible for the compiler to emit relocations for unaligned data.
164 * We handle this situation with these inlines.
165 */
166 #define RELOC_ALIGNED_P(x) \
167 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
168
169 static inline Elf_Addr
170 load_ptr(void *where)
171 {
172 Elf_Addr res;
173
174 memcpy(&res, where, sizeof(res));
175
176 return (res);
177 }
178
179 static inline void
180 store_ptr(void *where, Elf_Addr val)
181 {
182
183 memcpy(where, &val, sizeof(val));
184 }
185
186 void
187 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
188 {
189 const Elf_Rela *rela = 0, *relalim;
190 Elf_Addr relasz = 0;
191 Elf_Addr *where;
192
193 for (; dynp->d_tag != DT_NULL; dynp++) {
194 switch (dynp->d_tag) {
195 case DT_RELA:
196 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
197 break;
198 case DT_RELASZ:
199 relasz = dynp->d_un.d_val;
200 break;
201 }
202 }
203 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
204 for (; rela < relalim; rela++) {
205 where = (Elf_Addr *)(relocbase + rela->r_offset);
206 /* XXX For some reason I see a few GLOB_DAT relocs here. */
207 *where += (Elf_Addr)relocbase;
208 }
209 }
210
211 int
212 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
213 {
214 const Elf_Rela *rela;
215 Elf_Addr target = -1;
216 const Elf_Sym *def = NULL;
217 const Obj_Entry *defobj = NULL;
218 unsigned long last_symnum = ULONG_MAX;
219
220 for (rela = obj->rela; rela < obj->relalim; rela++) {
221 Elf_Addr *where;
222 Elf_Addr tmp;
223 unsigned long symnum;
224
225 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
226
227 switch (ELF_R_TYPE(rela->r_info)) {
228 case R_TYPE(REFQUAD):
229 case R_TYPE(GLOB_DAT):
230 case R_TYPE(TPREL64):
231 case R_TYPE(DTPMOD64):
232 case R_TYPE(DTPREL64):
233 symnum = ELF_R_SYM(rela->r_info);
234 if (last_symnum != symnum) {
235 last_symnum = symnum;
236 def = _rtld_find_symdef(symnum, obj, &defobj,
237 false);
238 if (def == NULL)
239 return -1;
240 }
241 break;
242
243 default:
244 break;
245 }
246
247 switch (ELF_R_TYPE(rela->r_info)) {
248 case R_TYPE(NONE):
249 break;
250
251 case R_TYPE(REFQUAD):
252 case R_TYPE(GLOB_DAT):
253 target = (Elf_Addr)(defobj->relocbase +
254 def->st_value);
255
256 tmp = target + rela->r_addend;
257 if (__predict_true(RELOC_ALIGNED_P(where))) {
258 if (*where != tmp)
259 *where = tmp;
260 } else {
261 if (load_ptr(where) != tmp)
262 store_ptr(where, tmp);
263 }
264 rdbg(("REFQUAD/GLOB_DAT %s in %s --> %p in %s",
265 obj->strtab + obj->symtab[symnum].st_name,
266 obj->path, (void *)tmp, defobj->path));
267 break;
268
269 case R_TYPE(RELATIVE):
270 if (__predict_true(RELOC_ALIGNED_P(where)))
271 *where += (Elf_Addr)obj->relocbase;
272 else
273 store_ptr(where,
274 load_ptr(where) + (Elf_Addr)obj->relocbase);
275 rdbg(("RELATIVE in %s --> %p", obj->path,
276 (void *)*where));
277 break;
278
279 case R_TYPE(COPY):
280 /*
281 * These are deferred until all other relocations have
282 * been done. All we do here is make sure that the
283 * COPY relocation is not in a shared library. They
284 * are allowed only in executable files.
285 */
286 if (obj->isdynamic) {
287 _rtld_error(
288 "%s: Unexpected R_COPY relocation in shared library",
289 obj->path);
290 return -1;
291 }
292 rdbg(("COPY (avoid in main)"));
293 break;
294
295 case R_TYPE(TPREL64):
296 if (!defobj->tls_static &&
297 _rtld_tls_offset_allocate(__UNCONST(defobj)))
298 return -1;
299
300 tmp = (Elf64_Addr)(def->st_value +
301 sizeof(struct tls_tcb) + defobj->tlsoffset +
302 rela->r_addend);
303
304 if (__predict_true(RELOC_ALIGNED_P(where)))
305 *where = tmp;
306 else
307 store_ptr(where, tmp);
308
309 rdbg(("TPREL64 %s in %s --> %p",
310 obj->strtab + obj->symtab[symnum].st_name,
311 obj->path, (void *)*where));
312
313 break;
314
315 case R_TYPE(DTPMOD64):
316 tmp = (Elf64_Addr)defobj->tlsindex;
317 if (__predict_true(RELOC_ALIGNED_P(where)))
318 *where = tmp;
319 else
320 store_ptr(where, tmp);
321
322 rdbg(("DTPMOD64 %s in %s --> %p",
323 obj->strtab + obj->symtab[symnum].st_name,
324 obj->path, (void *)*where));
325
326 break;
327
328 case R_TYPE(DTPREL64):
329 tmp = (Elf64_Addr)(def->st_value + rela->r_addend);
330 if (__predict_true(RELOC_ALIGNED_P(where)))
331 *where = tmp;
332 else
333 store_ptr(where, tmp);
334
335 rdbg(("DTPREL64 %s in %s --> %p",
336 obj->strtab + obj->symtab[symnum].st_name,
337 obj->path, (void *)*where));
338
339 break;
340
341 default:
342 rdbg(("sym = %lu, type = %lu, offset = %p, "
343 "addend = %p, contents = %p, symbol = %s",
344 (u_long)ELF_R_SYM(rela->r_info),
345 (u_long)ELF_R_TYPE(rela->r_info),
346 (void *)rela->r_offset, (void *)rela->r_addend,
347 (void *)load_ptr(where),
348 obj->strtab + obj->symtab[symnum].st_name));
349 _rtld_error("%s: Unsupported relocation type %ld "
350 "in non-PLT relocations",
351 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
352 return -1;
353 }
354 }
355 return 0;
356 }
357
358 int
359 _rtld_relocate_plt_lazy(Obj_Entry *obj)
360 {
361 const Elf_Rela *rela;
362
363 if (!obj->relocbase)
364 return 0;
365
366 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
367 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
368
369 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
370
371 /* Just relocate the GOT slots pointing into the PLT */
372 *where += (Elf_Addr)obj->relocbase;
373 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
374 }
375
376 return 0;
377 }
378
379 static inline int
380 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
381 Elf_Addr *tp)
382 {
383 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
384 Elf_Addr new_value;
385 const Elf_Sym *def;
386 const Obj_Entry *defobj;
387 Elf_Addr stubaddr;
388 unsigned long info = rela->r_info;
389
390 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
391
392 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
393 if (__predict_false(def == NULL))
394 return -1;
395 if (__predict_false(def == &_rtld_sym_zero))
396 return 0;
397
398 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
399 if (tp == NULL)
400 return 0;
401 new_value = _rtld_resolve_ifunc(defobj, def);
402 } else {
403 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
404 }
405 rdbg(("bind now/fixup in %s --> old=%p new=%p",
406 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
407
408 if ((stubaddr = *where) != new_value) {
409 int64_t delta, idisp;
410 uint32_t insn[3], *stubptr;
411 int insncnt;
412 Elf_Addr pc;
413
414 /* Point this GOT entry at the target. */
415 *where = new_value;
416
417 /*
418 * Alpha shared objects may have multiple GOTs, each
419 * of which may point to this entry in the PLT. But,
420 * we only have a reference to the first GOT entry which
421 * points to this PLT entry. In order to avoid having to
422 * re-bind this call every time a non-first GOT entry is
423 * used, we will attempt to patch up the PLT entry to
424 * reference the target, rather than the binder.
425 *
426 * When the PLT stub gets control, PV contains the address
427 * of the PLT entry. Each PLT entry has room for 3 insns.
428 * If the displacement of the target from PV fits in a signed
429 * 32-bit integer, we can simply add it to PV. Otherwise,
430 * we must load the GOT entry itself into PV.
431 *
432 * Note if the shared object uses the old PLT format, then
433 * we cannot patch up the PLT safely, and so we skip it
434 * in that case[*]. And if the shared object has a read-only
435 * secureplt, then we also skip it.
436 *
437 * [*] Actually, if we're not doing lazy-binding, then
438 * we *can* (and do) patch up this PLT entry; the PLTGOT
439 * thunk won't yet point to any binder entry point, and
440 * so this test will fail as it would for the new PLT
441 * entry format.
442 */
443 if (obj->secureplt) {
444 rdbg((" secureplt format"));
445 goto out;
446 }
447 if (obj->pltgot[2] == (Elf_Addr) &_rtld_bind_start_old) {
448 rdbg((" old PLT format"));
449 goto out;
450 }
451
452 delta = new_value - stubaddr;
453 rdbg((" stubaddr=%p, where-stubaddr=%ld, delta=%ld",
454 (void *)stubaddr, (long)where - (long)stubaddr,
455 (long)delta));
456 insncnt = 0;
457 if ((int32_t)delta == delta) {
458 /*
459 * We can adjust PV with an LDA, LDAH sequence.
460 *
461 * First, build an LDA insn to adjust the low 16
462 * bits.
463 */
464 insn[insncnt++] = 0x08 << 26 | 27 << 21 | 27 << 16 |
465 (delta & 0xffff);
466 rdbg((" LDA $27,%d($27)", (int16_t)delta));
467 /*
468 * Adjust the delta to account for the effects of
469 * the LDA, including sign-extension.
470 */
471 delta -= (int16_t)delta;
472 if (delta != 0) {
473 /*
474 * Build an LDAH instruction to adjust the
475 * high 16 bits.
476 */
477 insn[insncnt++] = 0x09 << 26 | 27 << 21 |
478 27 << 16 | ((delta >> 16) & 0xffff);
479 rdbg((" LDAH $27,%d($27)",
480 (int16_t)(delta >> 16)));
481 }
482 } else {
483 int64_t dhigh;
484
485 /* We must load the GOT entry. */
486 delta = (Elf_Addr)where - stubaddr;
487
488 /*
489 * If the GOT entry is too far away from the PLT
490 * entry, then we can't patch up the PLT entry.
491 * This PLT entry will have to be bound for each
492 * GOT entry except for the first one. This program
493 * will still run, albeit very slowly. It is very
494 * unlikely that this case will ever happen in
495 * practice.
496 */
497 if ((int32_t)delta != delta) {
498 rdbg((" PLT stub too far from GOT to relocate"));
499 goto out;
500 }
501 dhigh = delta - (int16_t)delta;
502 if (dhigh != 0) {
503 /*
504 * Build an LDAH instruction to adjust the
505 * high 16 bits.
506 */
507 insn[insncnt++] = 0x09 << 26 | 27 << 21 |
508 27 << 16 | ((dhigh >> 16) & 0xffff);
509 rdbg((" LDAH $27,%d($27)",
510 (int16_t)(dhigh >> 16)));
511 }
512 /* Build an LDQ to load the GOT entry. */
513 insn[insncnt++] = 0x29 << 26 | 27 << 21 |
514 27 << 16 | (delta & 0xffff);
515 rdbg((" LDQ $27,%d($27)",
516 (int16_t)delta));
517 }
518
519 /*
520 * Now, build a JMP or BR insn to jump to the target. If
521 * the displacement fits in a sign-extended 21-bit field,
522 * we can use the more efficient BR insn. Otherwise, we
523 * have to jump indirect through PV.
524 */
525 pc = stubaddr + (4 * (insncnt + 1));
526 idisp = (int64_t)(new_value - pc) >> 2;
527 if (-0x100000 <= idisp && idisp < 0x100000) {
528 insn[insncnt++] = 0x30 << 26 | 31 << 21 |
529 (idisp & 0x1fffff);
530 rdbg((" BR $31,%p", (void *)new_value));
531 } else {
532 insn[insncnt++] = 0x1a << 26 | 31 << 21 |
533 27 << 16 | (idisp & 0x3fff);
534 rdbg((" JMP $31,($27),%d",
535 (int)(idisp & 0x3fff)));
536 }
537
538 /*
539 * Fill in the tail of the PLT entry first, for reentrancy.
540 * Until we have overwritten the first insn (an unconditional
541 * branch), the remaining insns have no effect.
542 */
543 stubptr = (uint32_t *)stubaddr;
544 while (insncnt > 1) {
545 insncnt--;
546 stubptr[insncnt] = insn[insncnt];
547 }
548 /*
549 * Commit the tail of the insn sequence to memory
550 * before overwriting the first insn.
551 */
552 __asm volatile("wmb" ::: "memory");
553 stubptr[0] = insn[0];
554 /*
555 * I-stream will be sync'd when we either return from
556 * the binder (lazy bind case) or when the PLTGOT thunk
557 * is patched up (bind-now case).
558 */
559 }
560 out:
561 if (tp)
562 *tp = new_value;
563
564 return 0;
565 }
566
567 caddr_t
568 _rtld_bind(const Obj_Entry *obj, Elf_Addr reloff)
569 {
570 const Elf_Rela *rela =
571 (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
572 Elf_Addr result = 0; /* XXX gcc */
573 int err;
574
575 _rtld_shared_enter();
576 err = _rtld_relocate_plt_object(obj, rela, &result);
577 if (err)
578 _rtld_die();
579 _rtld_shared_exit();
580
581 return (caddr_t)result;
582 }
583
584 int
585 _rtld_relocate_plt_objects(const Obj_Entry *obj)
586 {
587 const Elf_Rela *rela;
588
589 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
590 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
591 return -1;
592
593 return 0;
594 }
595