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))
60 void _rtld_bind_secureplt_start(void);
63 * Process the R_PPC_COPY relocations
66 do_copy_relocations(Obj_Entry *dstobj)
68 const Elf_Rela *relalim;
72 * COPY relocs are invalid outside of the main program
74 assert(dstobj->mainprog);
76 relalim = (const Elf_Rela *)((const char *) dstobj->rela +
78 for (rela = dstobj->rela; rela < relalim; rela++) {
80 const Elf_Sym *dstsym;
84 const Elf_Sym *srcsym = NULL;
85 const Obj_Entry *srcobj, *defobj;
89 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
93 dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
94 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
95 name = dstobj->strtab + dstsym->st_name;
96 size = dstsym->st_size;
97 symlook_init(&req, name);
98 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
99 req.flags = SYMLOOK_EARLY;
101 for (srcobj = globallist_next(dstobj); srcobj != NULL;
102 srcobj = globallist_next(srcobj)) {
103 res = symlook_obj(&req, srcobj);
105 srcsym = req.sym_out;
106 defobj = req.defobj_out;
111 if (srcobj == NULL) {
112 _rtld_error("Undefined symbol \"%s\" "
113 " referenced from COPY"
114 " relocation in %s", name, dstobj->path);
118 srcaddr = (const void *)(defobj->relocbase+srcsym->st_value);
119 memcpy(dstaddr, srcaddr, size);
120 dbg("copy_reloc: src=%p,dst=%p,size=%d\n",srcaddr,dstaddr,size);
128 * Perform early relocation of the run-time linker image
131 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
133 const Elf_Rela *rela = NULL, *relalim;
138 * Extract the rela/relasz values from the dynamic section
140 for (; dynp->d_tag != DT_NULL; dynp++) {
141 switch (dynp->d_tag) {
143 rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
146 relasz = dynp->d_un.d_val;
152 * Relocate these values
154 relalim = (const Elf_Rela *)((const char *)rela + relasz);
155 for (; rela < relalim; rela++) {
156 where = (Elf_Addr *)(relocbase + rela->r_offset);
157 *where = (Elf_Addr)(relocbase + rela->r_addend);
163 * Relocate a non-PLT object with addend.
166 reloc_nonplt_object(Obj_Entry *obj_rtld __unused, Obj_Entry *obj,
167 const Elf_Rela *rela, SymCache *cache, int flags, RtldLockState *lockstate)
169 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
171 const Obj_Entry *defobj;
174 switch (ELF_R_TYPE(rela->r_info)) {
179 case R_PPC_ADDR32: /* word32 S + A */
180 case R_PPC_GLOB_DAT: /* word32 S + A */
181 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
182 flags, cache, lockstate);
187 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
190 /* Don't issue write if unnecessary; avoid COW page fault */
196 case R_PPC_RELATIVE: /* word32 B + A */
197 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
199 /* As above, don't issue write unnecessarily */
207 * These are deferred until all other relocations
208 * have been done. All we do here is make sure
209 * that the COPY relocation is not in a shared
210 * library. They are allowed only in executable
213 if (!obj->mainprog) {
214 _rtld_error("%s: Unexpected R_COPY "
215 " relocation in shared library",
223 * These will be handled by the plt/jmpslot routines
228 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
229 flags, cache, lockstate);
234 *where = (Elf_Addr) defobj->tlsindex;
239 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
240 flags, cache, lockstate);
246 * We lazily allocate offsets for static TLS as we
247 * see the first relocation that references the
248 * TLS block. This allows us to support (small
249 * amounts of) static TLS in dynamically loaded
250 * modules. If we run out of space, we generate an
253 if (!defobj->tls_done) {
254 if (!allocate_tls_offset(
255 __DECONST(Obj_Entry *, defobj))) {
256 _rtld_error("%s: No space available for static "
257 "Thread Local Storage", obj->path);
262 *(Elf_Addr **)where = *where * sizeof(Elf_Addr)
263 + (Elf_Addr *)(def->st_value + rela->r_addend
264 + defobj->tlsoffset - TLS_TP_OFFSET - TLS_TCB_SIZE);
269 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
270 flags, cache, lockstate);
275 *where += (Elf_Addr)(def->st_value + rela->r_addend
281 _rtld_error("%s: Unsupported relocation type %d"
282 " in non-PLT relocations\n", obj->path,
283 ELF_R_TYPE(rela->r_info));
291 * Process non-PLT relocations
294 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
295 RtldLockState *lockstate)
297 const Elf_Rela *relalim;
298 const Elf_Rela *rela;
299 const Elf_Phdr *phdr;
303 if ((flags & SYMLOOK_IFUNC) != 0)
304 /* XXX not implemented */
308 * The dynamic loader may be called from a thread, we have
309 * limited amounts of stack available so we cannot use alloca().
311 if (obj != obj_rtld) {
312 cache = calloc(obj->dynsymcount, sizeof(SymCache));
313 /* No need to check for NULL here */
318 * From the SVR4 PPC ABI:
319 * "The PowerPC family uses only the Elf32_Rela relocation
320 * entries with explicit addends."
322 relalim = (const Elf_Rela *)((const char *)obj->rela + obj->relasize);
323 for (rela = obj->rela; rela < relalim; rela++) {
324 if (reloc_nonplt_object(obj_rtld, obj, rela, cache, flags,
334 * Synchronize icache for executable segments in case we made
337 for (phdr = obj->phdr;
338 (const char *)phdr < (const char *)obj->phdr + obj->phsize;
340 if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_X) != 0) {
341 __syncicache(obj->relocbase + phdr->p_vaddr,
350 * Initialise a PLT slot to the resolving trampoline
353 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
355 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
356 Elf_Addr *pltresolve, *pltlongresolve, *jmptab;
358 int N = obj->pltrelasize / sizeof(Elf_Rela);
361 reloff = rela - obj->pltrela;
366 if (obj->gotptr != NULL) {
367 *where += (Elf_Addr)obj->relocbase;
371 pltlongresolve = obj->pltgot + 5;
372 pltresolve = pltlongresolve + 5;
374 distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
376 dbg(" reloc_plt_object: where=%p,pltres=%p,reloff=%x,distance=%x",
377 (void *)where, (void *)pltresolve, reloff, distance);
379 if (reloff < PLT_EXTENDED_BEGIN) {
382 where[0] = 0x39600000 | reloff;
383 where[1] = 0x48000000 | (distance & 0x03fffffc);
385 jmptab = obj->pltgot + JMPTAB_BASE(N);
386 jmptab[reloff] = (u_int)pltlongresolve;
388 /* lis r11,jmptab[reloff]@ha */
389 /* lwzu r12,jmptab[reloff]@l(r11) */
392 where[0] = 0x3d600000 | _ppc_ha(&jmptab[reloff]);
393 where[1] = 0x858b0000 | _ppc_la(&jmptab[reloff]);
394 where[2] = 0x7d8903a6;
395 where[3] = 0x4e800420;
400 * The icache will be sync'd in reloc_plt, which is called
401 * after all the slots have been updated
409 * Process the PLT relocations.
412 reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused)
414 const Elf_Rela *relalim;
415 const Elf_Rela *rela;
416 int N = obj->pltrelasize / sizeof(Elf_Rela);
418 if (obj->pltrelasize != 0) {
420 relalim = (const Elf_Rela *)((const char *)obj->pltrela +
422 for (rela = obj->pltrela; rela < relalim; rela++) {
423 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
425 if (reloc_plt_object(obj, rela) < 0) {
432 * Sync the icache for the byte range represented by the
433 * trampoline routines and call slots.
435 if (obj->pltgot != NULL && obj->gotptr == NULL)
436 __syncicache(obj->pltgot, JMPTAB_BASE(N)*4);
443 * LD_BIND_NOW was set - force relocation for all jump slots
446 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
448 const Obj_Entry *defobj;
449 const Elf_Rela *relalim;
450 const Elf_Rela *rela;
455 relalim = (const Elf_Rela *)((const char *)obj->pltrela +
457 for (rela = obj->pltrela; rela < relalim; rela++) {
458 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
459 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
460 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
461 SYMLOOK_IN_PLT | flags, NULL, lockstate);
463 dbg("reloc_jmpslots: sym not found");
467 target = (Elf_Addr)(defobj->relocbase + def->st_value);
471 dbg("\"%s\" in \"%s\" --> %p in \"%s\"",
472 defobj->strtab + def->st_name, basename(obj->path),
473 (void *)target, basename(defobj->path));
476 reloc_jmpslot(where, target, defobj, obj,
477 (const Elf_Rel *) rela);
480 obj->jmpslots_done = true;
487 * Update the value of a PLT jump slot. Branch directly to the target if
488 * it is within +/- 32Mb, otherwise go indirectly via the pltcall
489 * trampoline call and jump table.
492 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target,
493 const Obj_Entry *defobj __unused, const Obj_Entry *obj, const Elf_Rel *rel)
496 const Elf_Rela *rela = (const Elf_Rela *) rel;
498 dbg(" reloc_jmpslot: where=%p, target=%p",
499 (void *)wherep, (void *)target);
505 * At the PLT entry pointed at by `wherep', construct
506 * a direct transfer to the now fully resolved function
509 offset = target - (Elf_Addr)wherep;
511 if (obj->gotptr != NULL) {
512 assert(wherep >= (Elf_Word *)obj->pltgot);
514 (Elf_Word *)obj->pltgot + obj->pltrelasize);
519 if (abs((int)offset) < 32*1024*1024) { /* inside 32MB? */
520 /* b value # branch directly */
521 *wherep = 0x48000000 | (offset & 0x03fffffc);
522 __syncicache(wherep, 4);
524 Elf_Addr *pltcall, *jmptab;
526 int N = obj->pltrelasize / sizeof(Elf_Rela);
527 int reloff = rela - obj->pltrela;
532 pltcall = obj->pltgot;
534 dbg(" reloc_jmpslot: indir, reloff=%x, N=%x\n",
537 jmptab = obj->pltgot + JMPTAB_BASE(N);
538 jmptab[reloff] = target;
539 mb(); /* Order jmptab update before next changes */
541 if (reloff < PLT_EXTENDED_BEGIN) {
542 /* for extended PLT entries, we keep the old code */
544 distance = (Elf_Addr)pltcall - (Elf_Addr)(wherep + 1);
547 /* b pltcall # use indirect pltcall routine */
549 /* first instruction same as before */
550 wherep[1] = 0x48000000 | (distance & 0x03fffffc);
551 __syncicache(wherep, 8);
560 reloc_iresolve(Obj_Entry *obj __unused,
561 struct Struct_RtldLockState *lockstate __unused)
564 /* XXX not implemented */
569 reloc_gnu_ifunc(Obj_Entry *obj __unused, int flags __unused,
570 struct Struct_RtldLockState *lockstate __unused)
573 /* XXX not implemented */
578 * Setup the plt glue routines.
580 #define PLTCALL_SIZE 20
581 #define PLTLONGRESOLVE_SIZE 20
582 #define PLTRESOLVE_SIZE 24
585 init_pltgot(Obj_Entry *obj)
587 Elf_Word *pltcall, *pltresolve, *pltlongresolve;
589 int N = obj->pltrelasize / sizeof(Elf_Rela);
591 pltcall = obj->pltgot;
593 if (pltcall == NULL) {
597 /* Handle Secure-PLT first, if applicable. */
598 if (obj->gotptr != NULL) {
599 obj->gotptr[1] = (Elf_Addr)_rtld_bind_secureplt_start;
600 obj->gotptr[2] = (Elf_Addr)obj;
601 dbg("obj %s secure-plt gotptr=%p start=%p obj=%p",
602 obj->path, obj->gotptr,
603 (void *)obj->gotptr[1], (void *)obj->gotptr[2]);
608 * From the SVR4 PPC ABI:
610 * 'The first 18 words (72 bytes) of the PLT are reserved for
611 * use by the dynamic linker.
613 * 'If the executable or shared object requires N procedure
614 * linkage table entries, the link editor shall reserve 3*N
615 * words (12*N bytes) following the 18 reserved words. The
616 * first 2*N of these words are the procedure linkage table
617 * entries themselves. The static linker directs calls to bytes
618 * (72 + (i-1)*8), for i between 1 and N inclusive. The remaining
619 * N words (4*N bytes) are reserved for use by the dynamic linker.'
623 * Copy the absolute-call assembler stub into the first part of
624 * the reserved PLT area.
626 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
629 * Determine the address of the jumptable, which is the dyn-linker
630 * reserved area after the call cells. Write the absolute address
631 * of the jumptable into the absolute-call assembler code so it
632 * can determine this address.
634 jmptab = obj->pltgot + JMPTAB_BASE(N);
635 pltcall[1] |= _ppc_ha(jmptab); /* addis 11,11,jmptab@ha */
636 pltcall[2] |= _ppc_la(jmptab); /* lwz 11,jmptab@l(11) */
639 * Skip down 20 bytes into the initial reserved area and copy
640 * in the standard resolving assembler call. Into this assembler,
641 * insert the absolute address of the _rtld_bind_start routine
642 * and the address of the relocation object.
644 * We place pltlongresolve first, so it can fix up its arguments
645 * and then fall through to the regular PLT resolver.
647 pltlongresolve = obj->pltgot + 5;
649 memcpy(pltlongresolve, _rtld_powerpc_pltlongresolve,
650 PLTLONGRESOLVE_SIZE);
651 pltlongresolve[0] |= _ppc_ha(jmptab); /* lis 12,jmptab@ha */
652 pltlongresolve[1] |= _ppc_la(jmptab); /* addi 12,12,jmptab@l */
654 pltresolve = pltlongresolve + PLTLONGRESOLVE_SIZE/sizeof(uint32_t);
655 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
656 pltresolve[0] |= _ppc_ha(_rtld_bind_start);
657 pltresolve[1] |= _ppc_la(_rtld_bind_start);
658 pltresolve[3] |= _ppc_ha(obj);
659 pltresolve[4] |= _ppc_la(obj);
662 * The icache will be sync'd in reloc_plt, which is called
663 * after all the slots have been updated
668 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
680 allocate_initial_tls(Obj_Entry *list)
685 * Fix the size of the static TLS block by using the maximum
686 * offset allocated so far and adding a bit for dynamic modules to
690 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
692 tp = (Elf_Addr **)((char *) allocate_tls(list, NULL, TLS_TCB_SIZE, 8)
693 + TLS_TP_OFFSET + TLS_TCB_SIZE);
696 * XXX gcc seems to ignore 'tp = _tp;'
699 __asm __volatile("mr 2,%0" :: "r"(tp));
703 __tls_get_addr(tls_index* ti)
705 register Elf_Addr **tp;
708 __asm __volatile("mr %0,2" : "=r"(tp));
709 p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET
710 - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset);
712 return (p + TLS_DTV_OFFSET);