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>
53 * Process the R_PPC_COPY relocations
56 do_copy_relocations(Obj_Entry *dstobj)
58 const Elf_Rela *relalim;
62 * COPY relocs are invalid outside of the main program
64 assert(dstobj->mainprog);
66 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
68 for (rela = dstobj->rela; rela < relalim; rela++) {
70 const Elf_Sym *dstsym;
74 const Elf_Sym *srcsym = NULL;
75 const Obj_Entry *srcobj, *defobj;
79 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
83 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
84 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
85 name = dstobj->strtab + dstsym->st_name;
86 size = dstsym->st_size;
87 symlook_init(&req, name);
88 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
90 for (srcobj = dstobj->next; srcobj != NULL;
91 srcobj = srcobj->next) {
92 res = symlook_obj(&req, srcobj);
95 defobj = req.defobj_out;
100 if (srcobj == NULL) {
101 _rtld_error("Undefined symbol \"%s\" "
102 " referenced from COPY"
103 " relocation in %s", name, dstobj->path);
107 srcaddr = (const void *) (defobj->relocbase+srcsym->st_value);
108 memcpy(dstaddr, srcaddr, size);
109 dbg("copy_reloc: src=%p,dst=%p,size=%zd\n",srcaddr,dstaddr,size);
117 * Perform early relocation of the run-time linker image
120 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
122 const Elf_Rela *rela = 0, *relalim;
127 * Extract the rela/relasz values from the dynamic section
129 for (; dynp->d_tag != DT_NULL; dynp++) {
130 switch (dynp->d_tag) {
132 rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
135 relasz = dynp->d_un.d_val;
141 * Relocate these values
143 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
144 for (; rela < relalim; rela++) {
145 where = (Elf_Addr *)(relocbase + rela->r_offset);
146 *where = (Elf_Addr)(relocbase + rela->r_addend);
152 * Relocate a non-PLT object with addend.
155 reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
156 SymCache *cache, RtldLockState *lockstate)
158 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
160 const Obj_Entry *defobj;
163 switch (ELF_R_TYPE(rela->r_info)) {
168 case R_PPC64_UADDR64: /* doubleword64 S + A */
171 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
172 false, cache, lockstate);
177 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
180 /* Don't issue write if unnecessary; avoid COW page fault */
186 case R_PPC_RELATIVE: /* doubleword64 B + A */
187 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
189 /* As above, don't issue write unnecessarily */
197 * These are deferred until all other relocations
198 * have been done. All we do here is make sure
199 * that the COPY relocation is not in a shared
200 * library. They are allowed only in executable
203 if (!obj->mainprog) {
204 _rtld_error("%s: Unexpected R_COPY "
205 " relocation in shared library",
213 * These will be handled by the plt/jmpslot routines
217 case R_PPC64_DTPMOD64:
218 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
219 false, cache, lockstate);
224 *where = (Elf_Addr) defobj->tlsindex;
228 case R_PPC64_TPREL64:
229 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
230 false, cache, lockstate);
236 * We lazily allocate offsets for static TLS as we
237 * see the first relocation that references the
238 * TLS block. This allows us to support (small
239 * amounts of) static TLS in dynamically loaded
240 * modules. If we run out of space, we generate an
243 if (!defobj->tls_done) {
244 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
245 _rtld_error("%s: No space available for static "
246 "Thread Local Storage", obj->path);
251 *(Elf_Addr **)where = *where * sizeof(Elf_Addr)
252 + (Elf_Addr *)(def->st_value + rela->r_addend
253 + defobj->tlsoffset - TLS_TP_OFFSET);
257 case R_PPC64_DTPREL64:
258 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
259 false, cache, lockstate);
264 *where += (Elf_Addr)(def->st_value + rela->r_addend
270 _rtld_error("%s: Unsupported relocation type %ld"
271 " in non-PLT relocations\n", obj->path,
272 ELF_R_TYPE(rela->r_info));
280 * Process non-PLT relocations
283 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, RtldLockState *lockstate)
285 const Elf_Rela *relalim;
286 const Elf_Rela *rela;
288 int bytes = obj->nchains * sizeof(SymCache);
292 * The dynamic loader may be called from a thread, we have
293 * limited amounts of stack available so we cannot use alloca().
295 if (obj != obj_rtld) {
296 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON,
298 if (cache == MAP_FAILED)
304 * From the SVR4 PPC ABI:
305 * "The PowerPC family uses only the Elf32_Rela relocation
306 * entries with explicit addends."
308 relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
309 for (rela = obj->rela; rela < relalim; rela++) {
310 if (reloc_nonplt_object(obj_rtld, obj, rela, cache, lockstate)
317 munmap(cache, bytes);
324 * Initialise a PLT slot to the resolving trampoline
327 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
329 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
333 reloff = rela - obj->pltrela;
335 if (obj->priv == NULL)
336 obj->priv = malloc(obj->pltrelasize);
337 glink = obj->priv + reloff*sizeof(Elf_Addr)*2;
339 dbg(" reloc_plt_object: where=%p,reloff=%lx,glink=%p", (void *)where, reloff, glink);
341 memcpy(where, _rtld_bind_start, sizeof(struct funcdesc));
342 ((struct funcdesc *)(where))->env = (Elf_Addr)glink;
343 *(glink++) = (Elf_Addr)obj;
344 *(glink++) = reloff*sizeof(Elf_Rela);
351 * Process the PLT relocations.
354 reloc_plt(Obj_Entry *obj)
356 const Elf_Rela *relalim;
357 const Elf_Rela *rela;
359 if (obj->pltrelasize != 0) {
360 relalim = (const Elf_Rela *)((char *)obj->pltrela +
362 for (rela = obj->pltrela; rela < relalim; rela++) {
363 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
365 if (reloc_plt_object(obj, rela) < 0) {
376 * LD_BIND_NOW was set - force relocation for all jump slots
379 reloc_jmpslots(Obj_Entry *obj, RtldLockState *lockstate)
381 const Obj_Entry *defobj;
382 const Elf_Rela *relalim;
383 const Elf_Rela *rela;
388 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
389 for (rela = obj->pltrela; rela < relalim; rela++) {
390 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
391 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
392 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
393 true, NULL, lockstate);
395 dbg("reloc_jmpslots: sym not found");
399 target = (Elf_Addr)(defobj->relocbase + def->st_value);
403 dbg("\"%s\" in \"%s\" --> %p in \"%s\"",
404 defobj->strtab + def->st_name, basename(obj->path),
405 (void *)target, basename(defobj->path));
408 if (def == &sym_zero) {
409 /* Zero undefined weak symbols */
410 bzero(where, sizeof(struct funcdesc));
412 reloc_jmpslot(where, target, defobj, obj,
413 (const Elf_Rel *) rela);
417 obj->jmpslots_done = true;
424 * Update the value of a PLT jump slot.
427 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj,
428 const Obj_Entry *obj, const Elf_Rel *rel)
430 dbg(" reloc_jmpslot: where=%p, target=%p (%#lx + %#lx)",
431 (void *)wherep, (void *)target, *(Elf_Addr *)target,
432 (Elf_Addr)defobj->relocbase);
435 * At the PLT entry pointed at by `wherep', construct
436 * a direct transfer to the now fully resolved function
440 memcpy(wherep, (void *)target, sizeof(struct funcdesc));
441 if (((struct funcdesc *)(wherep))->addr < (Elf_Addr)defobj->relocbase) {
443 * XXX: It is possible (e.g. LD_BIND_NOW) that the function
444 * descriptor we are copying has not yet been relocated.
445 * If this happens, fix it.
448 ((struct funcdesc *)(wherep))->addr +=
449 (Elf_Addr)defobj->relocbase;
450 ((struct funcdesc *)(wherep))->toc +=
451 (Elf_Addr)defobj->relocbase;
454 __asm __volatile("dcbst 0,%0; sync" :: "r"(wherep) : "memory");
460 init_pltgot(Obj_Entry *obj)
465 allocate_initial_tls(Obj_Entry *list)
467 register Elf_Addr **tp __asm__("r13");
471 * Fix the size of the static TLS block by using the maximum
472 * offset allocated so far and adding a bit for dynamic modules to
476 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
478 _tp = (Elf_Addr **) ((char *)allocate_tls(list, NULL, TLS_TCB_SIZE, 16)
479 + TLS_TP_OFFSET + TLS_TCB_SIZE);
482 * XXX gcc seems to ignore 'tp = _tp;'
485 __asm __volatile("mr %0,%1" : "=r"(tp) : "r"(_tp));
489 __tls_get_addr(tls_index* ti)
491 register Elf_Addr **tp __asm__("r13");
494 p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET
495 - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset);
497 return (p + TLS_DTV_OFFSET);