1 /* $NetBSD: ppc_reloc.c,v 1.10 2001/09/10 06:09:41 mycroft Exp $ */
4 * Copyright (C) 1998 Tsubai Masanari
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/param.h>
40 #include <machine/cpu.h>
41 #include <machine/atomic.h>
42 #include <machine/md_var.h>
47 #define _ppc_ha(x) ((((u_int32_t)(x) & 0x8000) ? \
48 ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
49 #define _ppc_la(x) ((u_int32_t)(x) & 0xffff)
51 #define min(a,b) (((a) < (b)) ? (a) : (b))
52 #define max(a,b) (((a) > (b)) ? (a) : (b))
54 #define PLT_EXTENDED_BEGIN (1 << 13)
55 #define JMPTAB_BASE(N) (18 + N*2 + ((N > PLT_EXTENDED_BEGIN) ? \
56 (N - PLT_EXTENDED_BEGIN)*2 : 0))
59 * Process the R_PPC_COPY relocations
62 do_copy_relocations(Obj_Entry *dstobj)
64 const Elf_Rela *relalim;
68 * COPY relocs are invalid outside of the main program
70 assert(dstobj->mainprog);
72 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
74 for (rela = dstobj->rela; rela < relalim; rela++) {
76 const Elf_Sym *dstsym;
80 const Elf_Sym *srcsym = NULL;
81 const Obj_Entry *srcobj, *defobj;
85 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
89 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
90 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
91 name = dstobj->strtab + dstsym->st_name;
92 size = dstsym->st_size;
93 symlook_init(&req, name);
94 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
95 req.flags = SYMLOOK_EARLY;
97 for (srcobj = globallist_next(dstobj); srcobj != NULL;
98 srcobj = globallist_next(srcobj)) {
99 res = symlook_obj(&req, srcobj);
101 srcsym = req.sym_out;
102 defobj = req.defobj_out;
107 if (srcobj == NULL) {
108 _rtld_error("Undefined symbol \"%s\" "
109 " referenced from COPY"
110 " relocation in %s", name, dstobj->path);
114 srcaddr = (const void *) (defobj->relocbase+srcsym->st_value);
115 memcpy(dstaddr, srcaddr, size);
116 dbg("copy_reloc: src=%p,dst=%p,size=%d\n",srcaddr,dstaddr,size);
124 * Perform early relocation of the run-time linker image
127 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
129 const Elf_Rela *rela = NULL, *relalim;
134 * Extract the rela/relasz values from the dynamic section
136 for (; dynp->d_tag != DT_NULL; dynp++) {
137 switch (dynp->d_tag) {
139 rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
142 relasz = dynp->d_un.d_val;
148 * Relocate these values
150 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
151 for (; rela < relalim; rela++) {
152 where = (Elf_Addr *)(relocbase + rela->r_offset);
153 *where = (Elf_Addr)(relocbase + rela->r_addend);
159 * Relocate a non-PLT object with addend.
162 reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
163 SymCache *cache, int flags, RtldLockState *lockstate)
165 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
167 const Obj_Entry *defobj;
170 switch (ELF_R_TYPE(rela->r_info)) {
175 case R_PPC_ADDR32: /* word32 S + A */
176 case R_PPC_GLOB_DAT: /* word32 S + A */
177 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
178 flags, cache, lockstate);
183 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
186 /* Don't issue write if unnecessary; avoid COW page fault */
192 case R_PPC_RELATIVE: /* word32 B + A */
193 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
195 /* As above, don't issue write unnecessarily */
203 * These are deferred until all other relocations
204 * have been done. All we do here is make sure
205 * that the COPY relocation is not in a shared
206 * library. They are allowed only in executable
209 if (!obj->mainprog) {
210 _rtld_error("%s: Unexpected R_COPY "
211 " relocation in shared library",
219 * These will be handled by the plt/jmpslot routines
224 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
225 flags, cache, lockstate);
230 *where = (Elf_Addr) defobj->tlsindex;
235 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
236 flags, cache, lockstate);
242 * We lazily allocate offsets for static TLS as we
243 * see the first relocation that references the
244 * TLS block. This allows us to support (small
245 * amounts of) static TLS in dynamically loaded
246 * modules. If we run out of space, we generate an
249 if (!defobj->tls_done) {
250 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
251 _rtld_error("%s: No space available for static "
252 "Thread Local Storage", obj->path);
257 *(Elf_Addr **)where = *where * sizeof(Elf_Addr)
258 + (Elf_Addr *)(def->st_value + rela->r_addend
259 + defobj->tlsoffset - TLS_TP_OFFSET);
264 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
265 flags, cache, lockstate);
270 *where += (Elf_Addr)(def->st_value + rela->r_addend
276 _rtld_error("%s: Unsupported relocation type %d"
277 " in non-PLT relocations\n", obj->path,
278 ELF_R_TYPE(rela->r_info));
286 * Process non-PLT relocations
289 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
290 RtldLockState *lockstate)
292 const Elf_Rela *relalim;
293 const Elf_Rela *rela;
297 if ((flags & SYMLOOK_IFUNC) != 0)
298 /* XXX not implemented */
302 * The dynamic loader may be called from a thread, we have
303 * limited amounts of stack available so we cannot use alloca().
305 if (obj != obj_rtld) {
306 cache = calloc(obj->dynsymcount, sizeof(SymCache));
307 /* No need to check for NULL here */
312 * From the SVR4 PPC ABI:
313 * "The PowerPC family uses only the Elf32_Rela relocation
314 * entries with explicit addends."
316 relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
317 for (rela = obj->rela; rela < relalim; rela++) {
318 if (reloc_nonplt_object(obj_rtld, obj, rela, cache, flags,
327 /* Synchronize icache for text seg in case we made any changes */
328 __syncicache(obj->mapbase, obj->textsize);
334 * Initialise a PLT slot to the resolving trampoline
337 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
339 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
340 Elf_Addr *pltresolve, *pltlongresolve, *jmptab;
342 int N = obj->pltrelasize / sizeof(Elf_Rela);
345 reloff = rela - obj->pltrela;
350 pltlongresolve = obj->pltgot + 5;
351 pltresolve = pltlongresolve + 5;
353 distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
355 dbg(" reloc_plt_object: where=%p,pltres=%p,reloff=%x,distance=%x",
356 (void *)where, (void *)pltresolve, reloff, distance);
358 if (reloff < PLT_EXTENDED_BEGIN) {
361 where[0] = 0x39600000 | reloff;
362 where[1] = 0x48000000 | (distance & 0x03fffffc);
364 jmptab = obj->pltgot + JMPTAB_BASE(N);
365 jmptab[reloff] = (u_int)pltlongresolve;
367 /* lis r11,jmptab[reloff]@ha */
368 /* lwzu r12,jmptab[reloff]@l(r11) */
371 where[0] = 0x3d600000 | _ppc_ha(&jmptab[reloff]);
372 where[1] = 0x858b0000 | _ppc_la(&jmptab[reloff]);
373 where[2] = 0x7d8903a6;
374 where[3] = 0x4e800420;
379 * The icache will be sync'd in reloc_plt, which is called
380 * after all the slots have been updated
388 * Process the PLT relocations.
391 reloc_plt(Obj_Entry *obj)
393 const Elf_Rela *relalim;
394 const Elf_Rela *rela;
395 int N = obj->pltrelasize / sizeof(Elf_Rela);
397 if (obj->pltrelasize != 0) {
399 relalim = (const Elf_Rela *)((char *)obj->pltrela +
401 for (rela = obj->pltrela; rela < relalim; rela++) {
402 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
404 if (reloc_plt_object(obj, rela) < 0) {
411 * Sync the icache for the byte range represented by the
412 * trampoline routines and call slots.
414 if (obj->pltgot != NULL)
415 __syncicache(obj->pltgot, JMPTAB_BASE(N)*4);
422 * LD_BIND_NOW was set - force relocation for all jump slots
425 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
427 const Obj_Entry *defobj;
428 const Elf_Rela *relalim;
429 const Elf_Rela *rela;
434 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
435 for (rela = obj->pltrela; rela < relalim; rela++) {
436 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
437 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
438 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
439 SYMLOOK_IN_PLT | flags, NULL, lockstate);
441 dbg("reloc_jmpslots: sym not found");
445 target = (Elf_Addr)(defobj->relocbase + def->st_value);
449 dbg("\"%s\" in \"%s\" --> %p in \"%s\"",
450 defobj->strtab + def->st_name, basename(obj->path),
451 (void *)target, basename(defobj->path));
454 reloc_jmpslot(where, target, defobj, obj,
455 (const Elf_Rel *) rela);
458 obj->jmpslots_done = true;
465 * Update the value of a PLT jump slot. Branch directly to the target if
466 * it is within +/- 32Mb, otherwise go indirectly via the pltcall
467 * trampoline call and jump table.
470 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj,
471 const Obj_Entry *obj, const Elf_Rel *rel)
474 const Elf_Rela *rela = (const Elf_Rela *) rel;
476 dbg(" reloc_jmpslot: where=%p, target=%p",
477 (void *)wherep, (void *)target);
480 * At the PLT entry pointed at by `wherep', construct
481 * a direct transfer to the now fully resolved function
484 offset = target - (Elf_Addr)wherep;
486 if (abs((int)offset) < 32*1024*1024) { /* inside 32MB? */
487 /* b value # branch directly */
488 *wherep = 0x48000000 | (offset & 0x03fffffc);
489 __syncicache(wherep, 4);
491 Elf_Addr *pltcall, *jmptab;
493 int N = obj->pltrelasize / sizeof(Elf_Rela);
494 int reloff = rela - obj->pltrela;
499 pltcall = obj->pltgot;
501 dbg(" reloc_jmpslot: indir, reloff=%x, N=%x\n",
504 jmptab = obj->pltgot + JMPTAB_BASE(N);
505 jmptab[reloff] = target;
506 mb(); /* Order jmptab update before next changes */
508 if (reloff < PLT_EXTENDED_BEGIN) {
509 /* for extended PLT entries, we keep the old code */
511 distance = (Elf_Addr)pltcall - (Elf_Addr)(wherep + 1);
514 /* b pltcall # use indirect pltcall routine */
516 /* first instruction same as before */
517 wherep[1] = 0x48000000 | (distance & 0x03fffffc);
518 __syncicache(wherep, 8);
526 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
529 /* XXX not implemented */
534 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
535 struct Struct_RtldLockState *lockstate)
538 /* XXX not implemented */
543 * Setup the plt glue routines.
545 #define PLTCALL_SIZE 20
546 #define PLTLONGRESOLVE_SIZE 20
547 #define PLTRESOLVE_SIZE 24
550 init_pltgot(Obj_Entry *obj)
552 Elf_Word *pltcall, *pltresolve, *pltlongresolve;
554 int N = obj->pltrelasize / sizeof(Elf_Rela);
556 pltcall = obj->pltgot;
558 if (pltcall == NULL) {
563 * From the SVR4 PPC ABI:
565 * 'The first 18 words (72 bytes) of the PLT are reserved for
566 * use by the dynamic linker.
568 * 'If the executable or shared object requires N procedure
569 * linkage table entries, the link editor shall reserve 3*N
570 * words (12*N bytes) following the 18 reserved words. The
571 * first 2*N of these words are the procedure linkage table
572 * entries themselves. The static linker directs calls to bytes
573 * (72 + (i-1)*8), for i between 1 and N inclusive. The remaining
574 * N words (4*N bytes) are reserved for use by the dynamic linker.'
578 * Copy the absolute-call assembler stub into the first part of
579 * the reserved PLT area.
581 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
584 * Determine the address of the jumptable, which is the dyn-linker
585 * reserved area after the call cells. Write the absolute address
586 * of the jumptable into the absolute-call assembler code so it
587 * can determine this address.
589 jmptab = obj->pltgot + JMPTAB_BASE(N);
590 pltcall[1] |= _ppc_ha(jmptab); /* addis 11,11,jmptab@ha */
591 pltcall[2] |= _ppc_la(jmptab); /* lwz 11,jmptab@l(11) */
594 * Skip down 20 bytes into the initial reserved area and copy
595 * in the standard resolving assembler call. Into this assembler,
596 * insert the absolute address of the _rtld_bind_start routine
597 * and the address of the relocation object.
599 * We place pltlongresolve first, so it can fix up its arguments
600 * and then fall through to the regular PLT resolver.
602 pltlongresolve = obj->pltgot + 5;
604 memcpy(pltlongresolve, _rtld_powerpc_pltlongresolve,
605 PLTLONGRESOLVE_SIZE);
606 pltlongresolve[0] |= _ppc_ha(jmptab); /* lis 12,jmptab@ha */
607 pltlongresolve[1] |= _ppc_la(jmptab); /* addi 12,12,jmptab@l */
609 pltresolve = pltlongresolve + PLTLONGRESOLVE_SIZE/sizeof(uint32_t);
610 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
611 pltresolve[0] |= _ppc_ha(_rtld_bind_start);
612 pltresolve[1] |= _ppc_la(_rtld_bind_start);
613 pltresolve[3] |= _ppc_ha(obj);
614 pltresolve[4] |= _ppc_la(obj);
617 * The icache will be sync'd in reloc_plt, which is called
618 * after all the slots have been updated
623 ifunc_init(Elf_Auxinfo aux_info[static AT_COUNT] __unused)
628 allocate_initial_tls(Obj_Entry *list)
633 * Fix the size of the static TLS block by using the maximum
634 * offset allocated so far and adding a bit for dynamic modules to
638 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
640 tp = (Elf_Addr **) ((char *) allocate_tls(list, NULL, TLS_TCB_SIZE, 8)
641 + TLS_TP_OFFSET + TLS_TCB_SIZE);
644 * XXX gcc seems to ignore 'tp = _tp;'
647 __asm __volatile("mr 2,%0" :: "r"(tp));
651 __tls_get_addr(tls_index* ti)
653 register Elf_Addr **tp;
656 __asm __volatile("mr %0,2" : "=r"(tp));
657 p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET
658 - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset);
660 return (p + TLS_DTV_OFFSET);