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));
89 req.flags = SYMLOOK_EARLY;
91 for (srcobj = dstobj->next; srcobj != NULL;
92 srcobj = srcobj->next) {
93 res = symlook_obj(&req, srcobj);
96 defobj = req.defobj_out;
101 if (srcobj == NULL) {
102 _rtld_error("Undefined symbol \"%s\" "
103 " referenced from COPY"
104 " relocation in %s", name, dstobj->path);
108 srcaddr = (const void *) (defobj->relocbase+srcsym->st_value);
109 memcpy(dstaddr, srcaddr, size);
110 dbg("copy_reloc: src=%p,dst=%p,size=%zd\n",srcaddr,dstaddr,size);
118 * Perform early relocation of the run-time linker image
121 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
123 const Elf_Rela *rela = 0, *relalim;
128 * Extract the rela/relasz values from the dynamic section
130 for (; dynp->d_tag != DT_NULL; dynp++) {
131 switch (dynp->d_tag) {
133 rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
136 relasz = dynp->d_un.d_val;
142 * Relocate these values
144 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
145 for (; rela < relalim; rela++) {
146 where = (Elf_Addr *)(relocbase + rela->r_offset);
147 *where = (Elf_Addr)(relocbase + rela->r_addend);
153 * Relocate a non-PLT object with addend.
156 reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
157 SymCache *cache, int flags, RtldLockState *lockstate)
159 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
161 const Obj_Entry *defobj;
164 switch (ELF_R_TYPE(rela->r_info)) {
169 case R_PPC64_UADDR64: /* doubleword64 S + A */
172 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
173 flags, cache, lockstate);
178 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
181 /* Don't issue write if unnecessary; avoid COW page fault */
187 case R_PPC_RELATIVE: /* doubleword64 B + A */
188 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
190 /* As above, don't issue write unnecessarily */
198 * These are deferred until all other relocations
199 * have been done. All we do here is make sure
200 * that the COPY relocation is not in a shared
201 * library. They are allowed only in executable
204 if (!obj->mainprog) {
205 _rtld_error("%s: Unexpected R_COPY "
206 " relocation in shared library",
214 * These will be handled by the plt/jmpslot routines
218 case R_PPC64_DTPMOD64:
219 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
220 flags, cache, lockstate);
225 *where = (Elf_Addr) defobj->tlsindex;
229 case R_PPC64_TPREL64:
230 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
231 flags, cache, lockstate);
237 * We lazily allocate offsets for static TLS as we
238 * see the first relocation that references the
239 * TLS block. This allows us to support (small
240 * amounts of) static TLS in dynamically loaded
241 * modules. If we run out of space, we generate an
244 if (!defobj->tls_done) {
245 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
246 _rtld_error("%s: No space available for static "
247 "Thread Local Storage", obj->path);
252 *(Elf_Addr **)where = *where * sizeof(Elf_Addr)
253 + (Elf_Addr *)(def->st_value + rela->r_addend
254 + defobj->tlsoffset - TLS_TP_OFFSET);
258 case R_PPC64_DTPREL64:
259 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
260 flags, cache, lockstate);
265 *where += (Elf_Addr)(def->st_value + rela->r_addend
271 _rtld_error("%s: Unsupported relocation type %ld"
272 " in non-PLT relocations\n", obj->path,
273 ELF_R_TYPE(rela->r_info));
281 * Process non-PLT relocations
284 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
285 RtldLockState *lockstate)
287 const Elf_Rela *relalim;
288 const Elf_Rela *rela;
290 int bytes = obj->dynsymcount * sizeof(SymCache);
294 * The dynamic loader may be called from a thread, we have
295 * limited amounts of stack available so we cannot use alloca().
297 if (obj != obj_rtld) {
298 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON,
300 if (cache == MAP_FAILED)
306 * From the SVR4 PPC ABI:
307 * "The PowerPC family uses only the Elf32_Rela relocation
308 * entries with explicit addends."
310 relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
311 for (rela = obj->rela; rela < relalim; rela++) {
312 if (reloc_nonplt_object(obj_rtld, obj, rela, cache, flags,
319 munmap(cache, bytes);
321 /* Synchronize icache for text seg in case we made any changes */
322 __syncicache(obj->mapbase, obj->textsize);
329 * Initialise a PLT slot to the resolving trampoline
332 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
334 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
338 reloff = rela - obj->pltrela;
340 if (obj->priv == NULL)
341 obj->priv = xmalloc(obj->pltrelasize);
342 glink = obj->priv + reloff*sizeof(Elf_Addr)*2;
344 dbg(" reloc_plt_object: where=%p,reloff=%lx,glink=%p", (void *)where, reloff, glink);
346 memcpy(where, _rtld_bind_start, sizeof(struct funcdesc));
347 ((struct funcdesc *)(where))->env = (Elf_Addr)glink;
348 *(glink++) = (Elf_Addr)obj;
349 *(glink++) = reloff*sizeof(Elf_Rela);
356 * Process the PLT relocations.
359 reloc_plt(Obj_Entry *obj)
361 const Elf_Rela *relalim;
362 const Elf_Rela *rela;
364 if (obj->pltrelasize != 0) {
365 relalim = (const Elf_Rela *)((char *)obj->pltrela +
367 for (rela = obj->pltrela; rela < relalim; rela++) {
368 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
370 if (reloc_plt_object(obj, rela) < 0) {
381 * LD_BIND_NOW was set - force relocation for all jump slots
384 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
386 const Obj_Entry *defobj;
387 const Elf_Rela *relalim;
388 const Elf_Rela *rela;
393 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
394 for (rela = obj->pltrela; rela < relalim; rela++) {
395 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
396 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
397 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
398 SYMLOOK_IN_PLT | flags, NULL, lockstate);
400 dbg("reloc_jmpslots: sym not found");
404 target = (Elf_Addr)(defobj->relocbase + def->st_value);
408 dbg("\"%s\" in \"%s\" --> %p in \"%s\"",
409 defobj->strtab + def->st_name, basename(obj->path),
410 (void *)target, basename(defobj->path));
413 if (def == &sym_zero) {
414 /* Zero undefined weak symbols */
415 bzero(where, sizeof(struct funcdesc));
417 reloc_jmpslot(where, target, defobj, obj,
418 (const Elf_Rel *) rela);
422 obj->jmpslots_done = true;
429 * Update the value of a PLT jump slot.
432 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj,
433 const Obj_Entry *obj, const Elf_Rel *rel)
435 dbg(" reloc_jmpslot: where=%p, target=%p (%#lx + %#lx)",
436 (void *)wherep, (void *)target, *(Elf_Addr *)target,
437 (Elf_Addr)defobj->relocbase);
440 * At the PLT entry pointed at by `wherep', construct
441 * a direct transfer to the now fully resolved function
445 memcpy(wherep, (void *)target, sizeof(struct funcdesc));
446 if (((struct funcdesc *)(wherep))->addr < (Elf_Addr)defobj->relocbase) {
448 * XXX: It is possible (e.g. LD_BIND_NOW) that the function
449 * descriptor we are copying has not yet been relocated.
450 * If this happens, fix it.
453 ((struct funcdesc *)(wherep))->addr +=
454 (Elf_Addr)defobj->relocbase;
455 ((struct funcdesc *)(wherep))->toc +=
456 (Elf_Addr)defobj->relocbase;
459 __asm __volatile("dcbst 0,%0; sync" :: "r"(wherep) : "memory");
465 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
468 /* XXX not implemented */
473 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
474 struct Struct_RtldLockState *lockstate)
477 /* XXX not implemented */
482 init_pltgot(Obj_Entry *obj)
487 allocate_initial_tls(Obj_Entry *list)
489 register Elf_Addr **tp __asm__("r13");
493 * Fix the size of the static TLS block by using the maximum
494 * offset allocated so far and adding a bit for dynamic modules to
498 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
500 _tp = (Elf_Addr **) ((char *)allocate_tls(list, NULL, TLS_TCB_SIZE, 16)
501 + TLS_TP_OFFSET + TLS_TCB_SIZE);
504 * XXX gcc seems to ignore 'tp = _tp;'
507 __asm __volatile("mr %0,%1" : "=r"(tp) : "r"(_tp));
511 __tls_get_addr(tls_index* ti)
513 register Elf_Addr **tp __asm__("r13");
516 p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET
517 - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset);
519 return (p + TLS_DTV_OFFSET);