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/md_var.h>
48 #if !defined(_CALL_ELF) || _CALL_ELF == 1
57 * Process the R_PPC_COPY relocations
60 do_copy_relocations(Obj_Entry *dstobj)
62 const Elf_Rela *relalim;
66 * COPY relocs are invalid outside of the main program
68 assert(dstobj->mainprog);
70 relalim = (const Elf_Rela *)((const char *) dstobj->rela +
72 for (rela = dstobj->rela; rela < relalim; rela++) {
74 const Elf_Sym *dstsym;
78 const Elf_Sym *srcsym = NULL;
79 const Obj_Entry *srcobj, *defobj;
83 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
87 dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
88 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
89 name = dstobj->strtab + dstsym->st_name;
90 size = dstsym->st_size;
91 symlook_init(&req, name);
92 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
93 req.flags = SYMLOOK_EARLY;
95 for (srcobj = globallist_next(dstobj); srcobj != NULL;
96 srcobj = globallist_next(srcobj)) {
97 res = symlook_obj(&req, srcobj);
100 defobj = req.defobj_out;
105 if (srcobj == NULL) {
106 _rtld_error("Undefined symbol \"%s\" "
107 " referenced from COPY"
108 " relocation in %s", name, dstobj->path);
112 srcaddr = (const void *)(defobj->relocbase+srcsym->st_value);
113 memcpy(dstaddr, srcaddr, size);
114 dbg("copy_reloc: src=%p,dst=%p,size=%zd\n",srcaddr,dstaddr,size);
122 * Perform early relocation of the run-time linker image
125 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
127 const Elf_Rela *rela = NULL, *relalim;
132 * Extract the rela/relasz values from the dynamic section
134 for (; dynp->d_tag != DT_NULL; dynp++) {
135 switch (dynp->d_tag) {
137 rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
140 relasz = dynp->d_un.d_val;
146 * Relocate these values
148 relalim = (const Elf_Rela *)((const char *)rela + relasz);
149 for (; rela < relalim; rela++) {
150 where = (Elf_Addr *)(relocbase + rela->r_offset);
151 *where = (Elf_Addr)(relocbase + rela->r_addend);
157 * Relocate a non-PLT object with addend.
160 reloc_nonplt_object(Obj_Entry *obj_rtld __unused, Obj_Entry *obj,
161 const Elf_Rela *rela, SymCache *cache, int flags, RtldLockState *lockstate)
163 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
165 const Obj_Entry *defobj;
168 switch (ELF_R_TYPE(rela->r_info)) {
173 case R_PPC64_UADDR64: /* doubleword64 S + A */
176 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
177 flags, cache, lockstate);
182 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
185 /* Don't issue write if unnecessary; avoid COW page fault */
191 case R_PPC_RELATIVE: /* doubleword64 B + A */
192 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
194 /* As above, don't issue write unnecessarily */
202 * These are deferred until all other relocations
203 * have been done. All we do here is make sure
204 * that the COPY relocation is not in a shared
205 * library. They are allowed only in executable
208 if (!obj->mainprog) {
209 _rtld_error("%s: Unexpected R_COPY "
210 " relocation in shared library",
218 * These will be handled by the plt/jmpslot routines
222 case R_PPC64_DTPMOD64:
223 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
224 flags, cache, lockstate);
229 *where = (Elf_Addr) defobj->tlsindex;
233 case R_PPC64_TPREL64:
234 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
235 flags, cache, lockstate);
241 * We lazily allocate offsets for static TLS as we
242 * see the first relocation that references the
243 * TLS block. This allows us to support (small
244 * amounts of) static TLS in dynamically loaded
245 * modules. If we run out of space, we generate an
248 if (!defobj->tls_done) {
249 if (!allocate_tls_offset(
250 __DECONST(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 - TLS_TCB_SIZE);
263 case R_PPC64_DTPREL64:
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 %ld"
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;
294 const Elf_Phdr *phdr;
296 int bytes = obj->dynsymcount * sizeof(SymCache);
299 if ((flags & SYMLOOK_IFUNC) != 0)
300 /* XXX not implemented */
304 * The dynamic loader may be called from a thread, we have
305 * limited amounts of stack available so we cannot use alloca().
307 if (obj != obj_rtld) {
308 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON,
310 if (cache == MAP_FAILED)
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,
329 munmap(cache, bytes);
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,
349 * Initialise a PLT slot to the resolving trampoline
352 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
354 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
357 reloff = rela - obj->pltrela;
359 dbg(" reloc_plt_object: where=%p,reloff=%lx,glink=%#lx", (void *)where,
362 #if !defined(_CALL_ELF) || _CALL_ELF == 1
363 /* Glink code is 3 instructions after the first 32k, 2 before */
364 *where = (Elf_Addr)obj->glink + 32 +
365 8*((reloff < 0x8000) ? reloff : 0x8000) +
366 12*((reloff < 0x8000) ? 0 : (reloff - 0x8000));
368 *where = (Elf_Addr)obj->glink + 4*reloff + 32;
376 * Process the PLT relocations.
379 reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused)
381 const Elf_Rela *relalim;
382 const Elf_Rela *rela;
384 if (obj->pltrelasize != 0) {
385 relalim = (const Elf_Rela *)((const char *)obj->pltrela +
387 for (rela = obj->pltrela; rela < relalim; rela++) {
388 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
390 if (reloc_plt_object(obj, rela) < 0) {
401 * LD_BIND_NOW was set - force relocation for all jump slots
404 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
406 const Obj_Entry *defobj;
407 const Elf_Rela *relalim;
408 const Elf_Rela *rela;
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);
417 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
418 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
419 SYMLOOK_IN_PLT | flags, NULL, lockstate);
421 dbg("reloc_jmpslots: sym not found");
425 target = (Elf_Addr)(defobj->relocbase + def->st_value);
427 if (def == &sym_zero) {
428 /* Zero undefined weak symbols */
429 #if !defined(_CALL_ELF) || _CALL_ELF == 1
430 bzero(where, sizeof(struct funcdesc));
435 reloc_jmpslot(where, target, defobj, obj,
436 (const Elf_Rel *) rela);
440 obj->jmpslots_done = true;
447 * Update the value of a PLT jump slot.
450 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj,
451 const Obj_Entry *obj __unused, const Elf_Rel *rel __unused)
455 * At the PLT entry pointed at by `wherep', construct
456 * a direct transfer to the now fully resolved function
460 #if !defined(_CALL_ELF) || _CALL_ELF == 1
461 dbg(" reloc_jmpslot: where=%p, target=%p (%#lx + %#lx)",
462 (void *)wherep, (void *)target, *(Elf_Addr *)target,
463 (Elf_Addr)defobj->relocbase);
469 * For the trampoline, the second two elements of the function
470 * descriptor are unused, so we are fine replacing those at any time
471 * with the real ones with no thread safety implications. However, we
472 * need to make sure the main entry point pointer ([0]) is seen to be
473 * modified *after* the second two elements. This can't be done in
474 * general, since there are no barriers in the reading code, but put in
475 * some isyncs to at least make it a little better.
477 memcpy(wherep, (void *)target, sizeof(struct funcdesc));
478 wherep[2] = ((Elf_Addr *)target)[2];
479 wherep[1] = ((Elf_Addr *)target)[1];
480 __asm __volatile ("isync" : : : "memory");
481 wherep[0] = ((Elf_Addr *)target)[0];
482 __asm __volatile ("isync" : : : "memory");
484 if (((struct funcdesc *)(wherep))->addr < (Elf_Addr)defobj->relocbase) {
486 * It is possible (LD_BIND_NOW) that the function
487 * descriptor we are copying has not yet been relocated.
488 * If this happens, fix it. Don't worry about threading in
489 * this case since LD_BIND_NOW makes it irrelevant.
492 ((struct funcdesc *)(wherep))->addr +=
493 (Elf_Addr)defobj->relocbase;
494 ((struct funcdesc *)(wherep))->toc +=
495 (Elf_Addr)defobj->relocbase;
499 dbg(" reloc_jmpslot: where=%p, target=%p", (void *)wherep,
510 reloc_iresolve(Obj_Entry *obj __unused,
511 struct Struct_RtldLockState *lockstate __unused)
514 /* XXX not implemented */
519 reloc_gnu_ifunc(Obj_Entry *obj __unused, int flags __unused,
520 struct Struct_RtldLockState *lockstate __unused)
523 /* XXX not implemented */
528 init_pltgot(Obj_Entry *obj)
532 pltcall = obj->pltgot;
534 if (pltcall == NULL) {
538 #if defined(_CALL_ELF) && _CALL_ELF == 2
539 pltcall[0] = (Elf_Addr)&_rtld_bind_start;
540 pltcall[1] = (Elf_Addr)obj;
542 memcpy(pltcall, _rtld_bind_start, sizeof(struct funcdesc));
543 pltcall[2] = (Elf_Addr)obj;
548 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
560 allocate_initial_tls(Obj_Entry *list)
565 * Fix the size of the static TLS block by using the maximum
566 * offset allocated so far and adding a bit for dynamic modules to
570 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
572 tp = (Elf_Addr **)((char *)allocate_tls(list, NULL, TLS_TCB_SIZE, 16)
573 + TLS_TP_OFFSET + TLS_TCB_SIZE);
575 __asm __volatile("mr 13,%0" :: "r"(tp));
579 __tls_get_addr(tls_index* ti)
584 __asm __volatile("mr %0,13" : "=r"(tp));
585 p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET
586 - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset);
588 return (p + TLS_DTV_OFFSET);