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/md_var.h>
46 #if !defined(_CALL_ELF) || _CALL_ELF == 1
55 * Process the R_PPC_COPY relocations
58 do_copy_relocations(Obj_Entry *dstobj)
60 const Elf_Rela *relalim;
64 * COPY relocs are invalid outside of the main program
66 assert(dstobj->mainprog);
68 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
70 for (rela = dstobj->rela; rela < relalim; rela++) {
72 const Elf_Sym *dstsym;
76 const Elf_Sym *srcsym = NULL;
77 const Obj_Entry *srcobj, *defobj;
81 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
85 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
86 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
87 name = dstobj->strtab + dstsym->st_name;
88 size = dstsym->st_size;
89 symlook_init(&req, name);
90 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
91 req.flags = SYMLOOK_EARLY;
93 for (srcobj = globallist_next(dstobj); srcobj != NULL;
94 srcobj = globallist_next(srcobj)) {
95 res = symlook_obj(&req, srcobj);
98 defobj = req.defobj_out;
103 if (srcobj == NULL) {
104 _rtld_error("Undefined symbol \"%s\" "
105 " referenced from COPY"
106 " relocation in %s", name, dstobj->path);
110 srcaddr = (const void *) (defobj->relocbase+srcsym->st_value);
111 memcpy(dstaddr, srcaddr, size);
112 dbg("copy_reloc: src=%p,dst=%p,size=%zd\n",srcaddr,dstaddr,size);
120 * Perform early relocation of the run-time linker image
123 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
125 const Elf_Rela *rela = NULL, *relalim;
130 * Extract the rela/relasz values from the dynamic section
132 for (; dynp->d_tag != DT_NULL; dynp++) {
133 switch (dynp->d_tag) {
135 rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
138 relasz = dynp->d_un.d_val;
144 * Relocate these values
146 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
147 for (; rela < relalim; rela++) {
148 where = (Elf_Addr *)(relocbase + rela->r_offset);
149 *where = (Elf_Addr)(relocbase + rela->r_addend);
155 * Relocate a non-PLT object with addend.
158 reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
159 SymCache *cache, int flags, RtldLockState *lockstate)
161 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
163 const Obj_Entry *defobj;
166 switch (ELF_R_TYPE(rela->r_info)) {
171 case R_PPC64_UADDR64: /* doubleword64 S + A */
174 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
175 flags, cache, lockstate);
180 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
183 /* Don't issue write if unnecessary; avoid COW page fault */
189 case R_PPC_RELATIVE: /* doubleword64 B + A */
190 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
192 /* As above, don't issue write unnecessarily */
200 * These are deferred until all other relocations
201 * have been done. All we do here is make sure
202 * that the COPY relocation is not in a shared
203 * library. They are allowed only in executable
206 if (!obj->mainprog) {
207 _rtld_error("%s: Unexpected R_COPY "
208 " relocation in shared library",
216 * These will be handled by the plt/jmpslot routines
220 case R_PPC64_DTPMOD64:
221 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
222 flags, cache, lockstate);
227 *where = (Elf_Addr) defobj->tlsindex;
231 case R_PPC64_TPREL64:
232 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
233 flags, cache, lockstate);
239 * We lazily allocate offsets for static TLS as we
240 * see the first relocation that references the
241 * TLS block. This allows us to support (small
242 * amounts of) static TLS in dynamically loaded
243 * modules. If we run out of space, we generate an
246 if (!defobj->tls_done) {
247 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
248 _rtld_error("%s: No space available for static "
249 "Thread Local Storage", obj->path);
254 *(Elf_Addr **)where = *where * sizeof(Elf_Addr)
255 + (Elf_Addr *)(def->st_value + rela->r_addend
256 + defobj->tlsoffset - TLS_TP_OFFSET);
260 case R_PPC64_DTPREL64:
261 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
262 flags, cache, lockstate);
267 *where += (Elf_Addr)(def->st_value + rela->r_addend
273 _rtld_error("%s: Unsupported relocation type %ld"
274 " in non-PLT relocations\n", obj->path,
275 ELF_R_TYPE(rela->r_info));
283 * Process non-PLT relocations
286 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
287 RtldLockState *lockstate)
289 const Elf_Rela *relalim;
290 const Elf_Rela *rela;
292 int bytes = obj->dynsymcount * sizeof(SymCache);
295 if ((flags & SYMLOOK_IFUNC) != 0)
296 /* XXX not implemented */
300 * The dynamic loader may be called from a thread, we have
301 * limited amounts of stack available so we cannot use alloca().
303 if (obj != obj_rtld) {
304 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON,
306 if (cache == MAP_FAILED)
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,
325 munmap(cache, bytes);
327 /* Synchronize icache for text seg in case we made any changes */
328 __syncicache(obj->mapbase, obj->textsize);
335 * Initialise a PLT slot to the resolving trampoline
338 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
340 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
343 reloff = rela - obj->pltrela;
345 dbg(" reloc_plt_object: where=%p,reloff=%lx,glink=%#lx", (void *)where,
348 #if !defined(_CALL_ELF) || _CALL_ELF == 1
349 /* Glink code is 3 instructions after the first 32k, 2 before */
350 *where = (Elf_Addr)obj->glink + 32 +
351 8*((reloff < 0x8000) ? reloff : 0x8000) +
352 12*((reloff < 0x8000) ? 0 : (reloff - 0x8000));
354 *where = (Elf_Addr)obj->glink + 4*reloff + 32;
362 * Process the PLT relocations.
365 reloc_plt(Obj_Entry *obj)
367 const Elf_Rela *relalim;
368 const Elf_Rela *rela;
370 if (obj->pltrelasize != 0) {
371 relalim = (const Elf_Rela *)((char *)obj->pltrela +
373 for (rela = obj->pltrela; rela < relalim; rela++) {
374 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
376 if (reloc_plt_object(obj, rela) < 0) {
387 * LD_BIND_NOW was set - force relocation for all jump slots
390 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
392 const Obj_Entry *defobj;
393 const Elf_Rela *relalim;
394 const Elf_Rela *rela;
399 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
400 for (rela = obj->pltrela; rela < relalim; rela++) {
401 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
402 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
403 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
404 SYMLOOK_IN_PLT | flags, NULL, lockstate);
406 dbg("reloc_jmpslots: sym not found");
410 target = (Elf_Addr)(defobj->relocbase + def->st_value);
412 if (def == &sym_zero) {
413 /* Zero undefined weak symbols */
414 #if !defined(_CALL_ELF) || _CALL_ELF == 1
415 bzero(where, sizeof(struct funcdesc));
420 reloc_jmpslot(where, target, defobj, obj,
421 (const Elf_Rel *) rela);
425 obj->jmpslots_done = true;
432 * Update the value of a PLT jump slot.
435 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj,
436 const Obj_Entry *obj, const Elf_Rel *rel)
440 * At the PLT entry pointed at by `wherep', construct
441 * a direct transfer to the now fully resolved function
445 #if !defined(_CALL_ELF) || _CALL_ELF == 1
446 dbg(" reloc_jmpslot: where=%p, target=%p (%#lx + %#lx)",
447 (void *)wherep, (void *)target, *(Elf_Addr *)target,
448 (Elf_Addr)defobj->relocbase);
454 * For the trampoline, the second two elements of the function
455 * descriptor are unused, so we are fine replacing those at any time
456 * with the real ones with no thread safety implications. However, we
457 * need to make sure the main entry point pointer ([0]) is seen to be
458 * modified *after* the second two elements. This can't be done in
459 * general, since there are no barriers in the reading code, but put in
460 * some isyncs to at least make it a little better.
462 memcpy(wherep, (void *)target, sizeof(struct funcdesc));
463 wherep[2] = ((Elf_Addr *)target)[2];
464 wherep[1] = ((Elf_Addr *)target)[1];
465 __asm __volatile ("isync" : : : "memory");
466 wherep[0] = ((Elf_Addr *)target)[0];
467 __asm __volatile ("isync" : : : "memory");
469 if (((struct funcdesc *)(wherep))->addr < (Elf_Addr)defobj->relocbase) {
471 * It is possible (LD_BIND_NOW) that the function
472 * descriptor we are copying has not yet been relocated.
473 * If this happens, fix it. Don't worry about threading in
474 * this case since LD_BIND_NOW makes it irrelevant.
477 ((struct funcdesc *)(wherep))->addr +=
478 (Elf_Addr)defobj->relocbase;
479 ((struct funcdesc *)(wherep))->toc +=
480 (Elf_Addr)defobj->relocbase;
484 dbg(" reloc_jmpslot: where=%p, target=%p", (void *)wherep,
495 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
498 /* XXX not implemented */
503 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
504 struct Struct_RtldLockState *lockstate)
507 /* XXX not implemented */
512 init_pltgot(Obj_Entry *obj)
516 pltcall = obj->pltgot;
518 if (pltcall == NULL) {
522 #if defined(_CALL_ELF) && _CALL_ELF == 2
523 pltcall[0] = (Elf_Addr)&_rtld_bind_start;
524 pltcall[1] = (Elf_Addr)obj;
526 memcpy(pltcall, _rtld_bind_start, sizeof(struct funcdesc));
527 pltcall[2] = (Elf_Addr)obj;
532 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
537 allocate_initial_tls(Obj_Entry *list)
542 * Fix the size of the static TLS block by using the maximum
543 * offset allocated so far and adding a bit for dynamic modules to
547 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
549 tp = (Elf_Addr **) ((char *)allocate_tls(list, NULL, TLS_TCB_SIZE, 16)
550 + TLS_TP_OFFSET + TLS_TCB_SIZE);
552 __asm __volatile("mr 13,%0" :: "r"(tp));
556 __tls_get_addr(tls_index* ti)
561 __asm __volatile("mr %0,13" : "=r"(tp));
562 p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET
563 - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset);
565 return (p + TLS_DTV_OFFSET);