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>
45 #define _ppc_ha(x) ((((u_int32_t)(x) & 0x8000) ? \
46 ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
47 #define _ppc_la(x) ((u_int32_t)(x) & 0xffff)
50 * Process the R_PPC_COPY relocations
53 do_copy_relocations(Obj_Entry *dstobj)
55 const Elf_Rela *relalim;
59 * COPY relocs are invalid outside of the main program
61 assert(dstobj->mainprog);
63 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
65 for (rela = dstobj->rela; rela < relalim; rela++) {
67 const Elf_Sym *dstsym;
72 const Elf_Sym *srcsym = NULL;
76 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
80 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
81 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
82 name = dstobj->strtab + dstsym->st_name;
83 hash = elf_hash(name);
84 size = dstsym->st_size;
85 ve = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
87 for (srcobj = dstobj->next; srcobj != NULL;
88 srcobj = srcobj->next) {
89 if ((srcsym = symlook_obj(name, hash, srcobj, ve, 0))
96 _rtld_error("Undefined symbol \"%s\" "
97 " referenced from COPY"
98 " relocation in %s", name, dstobj->path);
102 srcaddr = (const void *) (srcobj->relocbase+srcsym->st_value);
103 memcpy(dstaddr, srcaddr, size);
104 dbg("copy_reloc: src=%p,dst=%p,size=%d\n",srcaddr,dstaddr,size);
112 * Perform early relocation of the run-time linker image
115 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
117 const Elf_Rela *rela = 0, *relalim;
122 * Extract the rela/relasz values from the dynamic section
124 for (; dynp->d_tag != DT_NULL; dynp++) {
125 switch (dynp->d_tag) {
127 rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
130 relasz = dynp->d_un.d_val;
136 * Relocate these values
138 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
139 for (; rela < relalim; rela++) {
140 where = (Elf_Addr *)(relocbase + rela->r_offset);
141 *where = (Elf_Addr)(relocbase + rela->r_addend);
147 * Relocate a non-PLT object with addend.
150 reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
153 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
155 const Obj_Entry *defobj;
158 switch (ELF_R_TYPE(rela->r_info)) {
163 case R_PPC_ADDR32: /* word32 S + A */
164 case R_PPC_GLOB_DAT: /* word32 S + A */
165 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
171 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
174 /* Don't issue write if unnecessary; avoid COW page fault */
180 case R_PPC_RELATIVE: /* word32 B + A */
181 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
183 /* As above, don't issue write unnecessarily */
191 * These are deferred until all other relocations
192 * have been done. All we do here is make sure
193 * that the COPY relocation is not in a shared
194 * library. They are allowed only in executable
197 if (!obj->mainprog) {
198 _rtld_error("%s: Unexpected R_COPY "
199 " relocation in shared library",
207 * These will be handled by the plt/jmpslot routines
212 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
218 *where = (Elf_Addr) defobj->tlsindex;
223 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
230 * We lazily allocate offsets for static TLS as we
231 * see the first relocation that references the
232 * TLS block. This allows us to support (small
233 * amounts of) static TLS in dynamically loaded
234 * modules. If we run out of space, we generate an
237 if (!defobj->tls_done) {
238 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
239 _rtld_error("%s: No space available for static "
240 "Thread Local Storage", obj->path);
245 *(Elf_Addr **)where = *where * sizeof(Elf_Addr)
246 + (Elf_Addr *)(def->st_value + rela->r_addend
247 + defobj->tlsoffset - TLS_TP_OFFSET);
252 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
258 *where += (Elf_Addr)(def->st_value + rela->r_addend
264 _rtld_error("%s: Unsupported relocation type %d"
265 " in non-PLT relocations\n", obj->path,
266 ELF_R_TYPE(rela->r_info));
274 * Process non-PLT relocations
277 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
279 const Elf_Rela *relalim;
280 const Elf_Rela *rela;
282 int bytes = obj->nchains * sizeof(SymCache);
286 * The dynamic loader may be called from a thread, we have
287 * limited amounts of stack available so we cannot use alloca().
289 if (obj != obj_rtld) {
290 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON,
292 if (cache == MAP_FAILED)
298 * From the SVR4 PPC ABI:
299 * "The PowerPC family uses only the Elf32_Rela relocation
300 * entries with explicit addends."
302 relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
303 for (rela = obj->rela; rela < relalim; rela++) {
304 if (reloc_nonplt_object(obj_rtld, obj, rela, cache) < 0)
310 munmap(cache, bytes);
317 * Initialise a PLT slot to the resolving trampoline
320 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
322 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
323 Elf_Addr *pltresolve;
327 reloff = rela - obj->pltrela;
329 if ((reloff < 0) || (reloff >= 0x8000)) {
333 pltresolve = obj->pltgot + 8;
335 distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
337 dbg(" reloc_plt_object: where=%p,pltres=%p,reloff=%x,distance=%x",
338 (void *)where, (void *)pltresolve, reloff, distance);
342 where[0] = 0x39600000 | reloff;
343 where[1] = 0x48000000 | (distance & 0x03fffffc);
346 * The icache will be sync'd in init_pltgot, which is called
347 * after all the slots have been updated
355 * Process the PLT relocations.
358 reloc_plt(Obj_Entry *obj)
360 const Elf_Rela *relalim;
361 const Elf_Rela *rela;
363 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)
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,
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 reloc_jmpslot(where, target, defobj, obj,
414 (const Elf_Rel *) rela);
417 obj->jmpslots_done = true;
424 * Update the value of a PLT jump slot. Branch directly to the target if
425 * it is within +/- 32Mb, otherwise go indirectly via the pltcall
426 * trampoline call and jump table.
429 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj,
430 const Obj_Entry *obj, const Elf_Rel *rel)
433 const Elf_Rela *rela = (const Elf_Rela *) rel;
435 dbg(" reloc_jmpslot: where=%p, target=%p",
436 (void *)wherep, (void *)target);
439 * At the PLT entry pointed at by `wherep', construct
440 * a direct transfer to the now fully resolved function
443 offset = target - (Elf_Addr)wherep;
445 if (abs(offset) < 32*1024*1024) { /* inside 32MB? */
446 /* b value # branch directly */
447 *wherep = 0x48000000 | (offset & 0x03fffffc);
448 __syncicache(wherep, 4);
450 Elf_Addr *pltcall, *jmptab;
452 int N = obj->pltrelasize / sizeof(Elf_Rela);
453 int reloff = rela - obj->pltrela;
455 if ((reloff < 0) || (reloff >= 0x8000)) {
459 pltcall = obj->pltgot;
461 dbg(" reloc_jmpslot: indir, reloff=%d, N=%d\n",
464 jmptab = obj->pltgot + 18 + N * 2;
465 jmptab[reloff] = target;
467 distance = (Elf_Addr)pltcall - (Elf_Addr)(wherep + 1);
470 /* b pltcall # use indirect pltcall routine */
471 wherep[0] = 0x39600000 | reloff;
472 wherep[1] = 0x48000000 | (distance & 0x03fffffc);
473 __syncicache(wherep, 8);
481 * Setup the plt glue routines.
483 #define PLTCALL_SIZE 20
484 #define PLTRESOLVE_SIZE 24
487 init_pltgot(Obj_Entry *obj)
489 Elf_Word *pltcall, *pltresolve;
491 int N = obj->pltrelasize / sizeof(Elf_Rela);
493 pltcall = obj->pltgot;
495 if (pltcall == NULL) {
500 * From the SVR4 PPC ABI:
502 * 'The first 18 words (72 bytes) of the PLT are reserved for
503 * use by the dynamic linker.
505 * 'If the executable or shared object requires N procedure
506 * linkage table entries, the link editor shall reserve 3*N
507 * words (12*N bytes) following the 18 reserved words. The
508 * first 2*N of these words are the procedure linkage table
509 * entries themselves. The static linker directs calls to bytes
510 * (72 + (i-1)*8), for i between 1 and N inclusive. The remaining
511 * N words (4*N bytes) are reserved for use by the dynamic linker.'
515 * Copy the absolute-call assembler stub into the first part of
516 * the reserved PLT area.
518 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
521 * Determine the address of the jumptable, which is the dyn-linker
522 * reserved area after the call cells. Write the absolute address
523 * of the jumptable into the absolute-call assembler code so it
524 * can determine this address.
526 jmptab = pltcall + 18 + N * 2;
527 pltcall[1] |= _ppc_ha(jmptab); /* addis 11,11,jmptab@ha */
528 pltcall[2] |= _ppc_la(jmptab); /* lwz 11,jmptab@l(11) */
531 * Skip down 32 bytes into the initial reserved area and copy
532 * in the standard resolving assembler call. Into this assembler,
533 * insert the absolute address of the _rtld_bind_start routine
534 * and the address of the relocation object.
536 pltresolve = obj->pltgot + 8;
538 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
539 pltresolve[0] |= _ppc_ha(_rtld_bind_start);
540 pltresolve[1] |= _ppc_la(_rtld_bind_start);
541 pltresolve[3] |= _ppc_ha(obj);
542 pltresolve[4] |= _ppc_la(obj);
545 * Sync the icache for the byte range represented by the
546 * trampoline routines and call slots.
548 __syncicache(pltcall, 72 + N * 8);
552 allocate_initial_tls(Obj_Entry *list)
554 register Elf_Addr **tp __asm__("r2");
558 * Fix the size of the static TLS block by using the maximum
559 * offset allocated so far and adding a bit for dynamic modules to
563 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
565 _tp = (Elf_Addr **) ((char *) allocate_tls(list, NULL, TLS_TCB_SIZE, 8)
566 + TLS_TP_OFFSET + TLS_TCB_SIZE);
569 * XXX gcc seems to ignore 'tp = _tp;'
572 __asm __volatile("mr %0,%1" : "=r"(tp) : "r"(_tp));
576 __tls_get_addr(tls_index* ti)
578 register Elf_Addr **tp __asm__("r2");
581 p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET
582 - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset);
584 return (p + TLS_DTV_OFFSET);