1 /* $NetBSD: ppc_reloc.c,v 1.10 2001/09/10 06:09:41 mycroft Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 * Copyright (C) 1998 Tsubai Masanari
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/param.h>
42 #include <machine/cpu.h>
43 #include <machine/atomic.h>
44 #include <machine/md_var.h>
49 #define _ppc_ha(x) ((((u_int32_t)(x) & 0x8000) ? \
50 ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
51 #define _ppc_la(x) ((u_int32_t)(x) & 0xffff)
53 #define min(a,b) (((a) < (b)) ? (a) : (b))
54 #define max(a,b) (((a) > (b)) ? (a) : (b))
56 #define PLT_EXTENDED_BEGIN (1 << 13)
57 #define JMPTAB_BASE(N) (18 + N*2 + ((N > PLT_EXTENDED_BEGIN) ? \
58 (N - PLT_EXTENDED_BEGIN)*2 : 0))
61 * Process the R_PPC_COPY relocations
64 do_copy_relocations(Obj_Entry *dstobj)
66 const Elf_Rela *relalim;
70 * COPY relocs are invalid outside of the main program
72 assert(dstobj->mainprog);
74 relalim = (const Elf_Rela *)((const char *) dstobj->rela +
76 for (rela = dstobj->rela; rela < relalim; rela++) {
78 const Elf_Sym *dstsym;
82 const Elf_Sym *srcsym = NULL;
83 const Obj_Entry *srcobj, *defobj;
87 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
91 dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
92 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
93 name = dstobj->strtab + dstsym->st_name;
94 size = dstsym->st_size;
95 symlook_init(&req, name);
96 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
97 req.flags = SYMLOOK_EARLY;
99 for (srcobj = globallist_next(dstobj); srcobj != NULL;
100 srcobj = globallist_next(srcobj)) {
101 res = symlook_obj(&req, srcobj);
103 srcsym = req.sym_out;
104 defobj = req.defobj_out;
109 if (srcobj == NULL) {
110 _rtld_error("Undefined symbol \"%s\" "
111 " referenced from COPY"
112 " relocation in %s", name, dstobj->path);
116 srcaddr = (const void *)(defobj->relocbase+srcsym->st_value);
117 memcpy(dstaddr, srcaddr, size);
118 dbg("copy_reloc: src=%p,dst=%p,size=%d\n",srcaddr,dstaddr,size);
126 * Perform early relocation of the run-time linker image
129 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
131 const Elf_Rela *rela = NULL, *relalim;
136 * Extract the rela/relasz values from the dynamic section
138 for (; dynp->d_tag != DT_NULL; dynp++) {
139 switch (dynp->d_tag) {
141 rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
144 relasz = dynp->d_un.d_val;
150 * Relocate these values
152 relalim = (const Elf_Rela *)((const char *)rela + relasz);
153 for (; rela < relalim; rela++) {
154 where = (Elf_Addr *)(relocbase + rela->r_offset);
155 *where = (Elf_Addr)(relocbase + rela->r_addend);
161 * Relocate a non-PLT object with addend.
164 reloc_nonplt_object(Obj_Entry *obj_rtld __unused, Obj_Entry *obj,
165 const Elf_Rela *rela, SymCache *cache, int flags, RtldLockState *lockstate)
167 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
169 const Obj_Entry *defobj;
172 switch (ELF_R_TYPE(rela->r_info)) {
177 case R_PPC_ADDR32: /* word32 S + A */
178 case R_PPC_GLOB_DAT: /* word32 S + A */
179 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
180 flags, cache, lockstate);
185 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
188 /* Don't issue write if unnecessary; avoid COW page fault */
194 case R_PPC_RELATIVE: /* word32 B + A */
195 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
197 /* As above, don't issue write unnecessarily */
205 * These are deferred until all other relocations
206 * have been done. All we do here is make sure
207 * that the COPY relocation is not in a shared
208 * library. They are allowed only in executable
211 if (!obj->mainprog) {
212 _rtld_error("%s: Unexpected R_COPY "
213 " relocation in shared library",
221 * These will be handled by the plt/jmpslot routines
226 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
227 flags, cache, lockstate);
232 *where = (Elf_Addr) defobj->tlsindex;
237 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
238 flags, cache, lockstate);
244 * We lazily allocate offsets for static TLS as we
245 * see the first relocation that references the
246 * TLS block. This allows us to support (small
247 * amounts of) static TLS in dynamically loaded
248 * modules. If we run out of space, we generate an
251 if (!defobj->tls_done) {
252 if (!allocate_tls_offset(
253 __DECONST(Obj_Entry *, defobj))) {
254 _rtld_error("%s: No space available for static "
255 "Thread Local Storage", obj->path);
260 *(Elf_Addr **)where = *where * sizeof(Elf_Addr)
261 + (Elf_Addr *)(def->st_value + rela->r_addend
262 + defobj->tlsoffset - TLS_TP_OFFSET - TLS_TCB_SIZE);
267 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
268 flags, cache, lockstate);
273 *where += (Elf_Addr)(def->st_value + rela->r_addend
279 _rtld_error("%s: Unsupported relocation type %d"
280 " in non-PLT relocations\n", obj->path,
281 ELF_R_TYPE(rela->r_info));
289 * Process non-PLT relocations
292 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
293 RtldLockState *lockstate)
295 const Elf_Rela *relalim;
296 const Elf_Rela *rela;
297 const Elf_Phdr *phdr;
301 if ((flags & SYMLOOK_IFUNC) != 0)
302 /* XXX not implemented */
306 * The dynamic loader may be called from a thread, we have
307 * limited amounts of stack available so we cannot use alloca().
309 if (obj != obj_rtld) {
310 cache = calloc(obj->dynsymcount, sizeof(SymCache));
311 /* No need to check for NULL here */
316 * From the SVR4 PPC ABI:
317 * "The PowerPC family uses only the Elf32_Rela relocation
318 * entries with explicit addends."
320 relalim = (const Elf_Rela *)((const char *)obj->rela + obj->relasize);
321 for (rela = obj->rela; rela < relalim; rela++) {
322 if (reloc_nonplt_object(obj_rtld, obj, rela, cache, flags,
332 * Synchronize icache for executable segments in case we made
335 for (phdr = obj->phdr;
336 (const char *)phdr < (const char *)obj->phdr + obj->phsize;
338 if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_X) != 0) {
339 __syncicache(obj->relocbase + phdr->p_vaddr,
348 * Initialise a PLT slot to the resolving trampoline
351 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
353 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
354 Elf_Addr *pltresolve, *pltlongresolve, *jmptab;
356 int N = obj->pltrelasize / sizeof(Elf_Rela);
359 reloff = rela - obj->pltrela;
364 pltlongresolve = obj->pltgot + 5;
365 pltresolve = pltlongresolve + 5;
367 distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
369 dbg(" reloc_plt_object: where=%p,pltres=%p,reloff=%x,distance=%x",
370 (void *)where, (void *)pltresolve, reloff, distance);
372 if (reloff < PLT_EXTENDED_BEGIN) {
375 where[0] = 0x39600000 | reloff;
376 where[1] = 0x48000000 | (distance & 0x03fffffc);
378 jmptab = obj->pltgot + JMPTAB_BASE(N);
379 jmptab[reloff] = (u_int)pltlongresolve;
381 /* lis r11,jmptab[reloff]@ha */
382 /* lwzu r12,jmptab[reloff]@l(r11) */
385 where[0] = 0x3d600000 | _ppc_ha(&jmptab[reloff]);
386 where[1] = 0x858b0000 | _ppc_la(&jmptab[reloff]);
387 where[2] = 0x7d8903a6;
388 where[3] = 0x4e800420;
393 * The icache will be sync'd in reloc_plt, which is called
394 * after all the slots have been updated
402 * Process the PLT relocations.
405 reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused)
407 const Elf_Rela *relalim;
408 const Elf_Rela *rela;
409 int N = obj->pltrelasize / sizeof(Elf_Rela);
411 if (obj->pltrelasize != 0) {
413 relalim = (const Elf_Rela *)((const char *)obj->pltrela +
415 for (rela = obj->pltrela; rela < relalim; rela++) {
416 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
418 if (reloc_plt_object(obj, rela) < 0) {
425 * Sync the icache for the byte range represented by the
426 * trampoline routines and call slots.
428 if (obj->pltgot != NULL)
429 __syncicache(obj->pltgot, JMPTAB_BASE(N)*4);
436 * LD_BIND_NOW was set - force relocation for all jump slots
439 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
441 const Obj_Entry *defobj;
442 const Elf_Rela *relalim;
443 const Elf_Rela *rela;
448 relalim = (const Elf_Rela *)((const char *)obj->pltrela +
450 for (rela = obj->pltrela; rela < relalim; rela++) {
451 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
452 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
453 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
454 SYMLOOK_IN_PLT | flags, NULL, lockstate);
456 dbg("reloc_jmpslots: sym not found");
460 target = (Elf_Addr)(defobj->relocbase + def->st_value);
464 dbg("\"%s\" in \"%s\" --> %p in \"%s\"",
465 defobj->strtab + def->st_name, basename(obj->path),
466 (void *)target, basename(defobj->path));
469 reloc_jmpslot(where, target, defobj, obj,
470 (const Elf_Rel *) rela);
473 obj->jmpslots_done = true;
480 * Update the value of a PLT jump slot. Branch directly to the target if
481 * it is within +/- 32Mb, otherwise go indirectly via the pltcall
482 * trampoline call and jump table.
485 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target,
486 const Obj_Entry *defobj __unused, const Obj_Entry *obj, const Elf_Rel *rel)
489 const Elf_Rela *rela = (const Elf_Rela *) rel;
491 dbg(" reloc_jmpslot: where=%p, target=%p",
492 (void *)wherep, (void *)target);
498 * At the PLT entry pointed at by `wherep', construct
499 * a direct transfer to the now fully resolved function
502 offset = target - (Elf_Addr)wherep;
504 if (abs((int)offset) < 32*1024*1024) { /* inside 32MB? */
505 /* b value # branch directly */
506 *wherep = 0x48000000 | (offset & 0x03fffffc);
507 __syncicache(wherep, 4);
509 Elf_Addr *pltcall, *jmptab;
511 int N = obj->pltrelasize / sizeof(Elf_Rela);
512 int reloff = rela - obj->pltrela;
517 pltcall = obj->pltgot;
519 dbg(" reloc_jmpslot: indir, reloff=%x, N=%x\n",
522 jmptab = obj->pltgot + JMPTAB_BASE(N);
523 jmptab[reloff] = target;
524 mb(); /* Order jmptab update before next changes */
526 if (reloff < PLT_EXTENDED_BEGIN) {
527 /* for extended PLT entries, we keep the old code */
529 distance = (Elf_Addr)pltcall - (Elf_Addr)(wherep + 1);
532 /* b pltcall # use indirect pltcall routine */
534 /* first instruction same as before */
535 wherep[1] = 0x48000000 | (distance & 0x03fffffc);
536 __syncicache(wherep, 8);
545 reloc_iresolve(Obj_Entry *obj __unused,
546 struct Struct_RtldLockState *lockstate __unused)
549 /* XXX not implemented */
554 reloc_gnu_ifunc(Obj_Entry *obj __unused, int flags __unused,
555 struct Struct_RtldLockState *lockstate __unused)
558 /* XXX not implemented */
563 * Setup the plt glue routines.
565 #define PLTCALL_SIZE 20
566 #define PLTLONGRESOLVE_SIZE 20
567 #define PLTRESOLVE_SIZE 24
570 init_pltgot(Obj_Entry *obj)
572 Elf_Word *pltcall, *pltresolve, *pltlongresolve;
574 int N = obj->pltrelasize / sizeof(Elf_Rela);
576 pltcall = obj->pltgot;
578 if (pltcall == NULL) {
583 * From the SVR4 PPC ABI:
585 * 'The first 18 words (72 bytes) of the PLT are reserved for
586 * use by the dynamic linker.
588 * 'If the executable or shared object requires N procedure
589 * linkage table entries, the link editor shall reserve 3*N
590 * words (12*N bytes) following the 18 reserved words. The
591 * first 2*N of these words are the procedure linkage table
592 * entries themselves. The static linker directs calls to bytes
593 * (72 + (i-1)*8), for i between 1 and N inclusive. The remaining
594 * N words (4*N bytes) are reserved for use by the dynamic linker.'
598 * Copy the absolute-call assembler stub into the first part of
599 * the reserved PLT area.
601 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
604 * Determine the address of the jumptable, which is the dyn-linker
605 * reserved area after the call cells. Write the absolute address
606 * of the jumptable into the absolute-call assembler code so it
607 * can determine this address.
609 jmptab = obj->pltgot + JMPTAB_BASE(N);
610 pltcall[1] |= _ppc_ha(jmptab); /* addis 11,11,jmptab@ha */
611 pltcall[2] |= _ppc_la(jmptab); /* lwz 11,jmptab@l(11) */
614 * Skip down 20 bytes into the initial reserved area and copy
615 * in the standard resolving assembler call. Into this assembler,
616 * insert the absolute address of the _rtld_bind_start routine
617 * and the address of the relocation object.
619 * We place pltlongresolve first, so it can fix up its arguments
620 * and then fall through to the regular PLT resolver.
622 pltlongresolve = obj->pltgot + 5;
624 memcpy(pltlongresolve, _rtld_powerpc_pltlongresolve,
625 PLTLONGRESOLVE_SIZE);
626 pltlongresolve[0] |= _ppc_ha(jmptab); /* lis 12,jmptab@ha */
627 pltlongresolve[1] |= _ppc_la(jmptab); /* addi 12,12,jmptab@l */
629 pltresolve = pltlongresolve + PLTLONGRESOLVE_SIZE/sizeof(uint32_t);
630 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
631 pltresolve[0] |= _ppc_ha(_rtld_bind_start);
632 pltresolve[1] |= _ppc_la(_rtld_bind_start);
633 pltresolve[3] |= _ppc_ha(obj);
634 pltresolve[4] |= _ppc_la(obj);
637 * The icache will be sync'd in reloc_plt, which is called
638 * after all the slots have been updated
643 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
655 allocate_initial_tls(Obj_Entry *list)
660 * Fix the size of the static TLS block by using the maximum
661 * offset allocated so far and adding a bit for dynamic modules to
665 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
667 tp = (Elf_Addr **)((char *) allocate_tls(list, NULL, TLS_TCB_SIZE, 8)
668 + TLS_TP_OFFSET + TLS_TCB_SIZE);
671 * XXX gcc seems to ignore 'tp = _tp;'
674 __asm __volatile("mr 2,%0" :: "r"(tp));
678 __tls_get_addr(tls_index* ti)
680 register Elf_Addr **tp;
683 __asm __volatile("mr %0,2" : "=r"(tp));
684 p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET
685 - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset);
687 return (p + TLS_DTV_OFFSET);