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 #define _ppc_ha(x) ((((u_int32_t)(x) & 0x8000) ? \
47 ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
48 #define _ppc_la(x) ((u_int32_t)(x) & 0xffff)
51 * Process the R_PPC_COPY relocations
54 do_copy_relocations(Obj_Entry *dstobj)
56 const Elf_Rela *relalim;
60 * COPY relocs are invalid outside of the main program
62 assert(dstobj->mainprog);
64 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
66 for (rela = dstobj->rela; rela < relalim; rela++) {
68 const Elf_Sym *dstsym;
73 const Elf_Sym *srcsym = NULL;
77 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
81 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
82 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
83 name = dstobj->strtab + dstsym->st_name;
84 hash = elf_hash(name);
85 size = dstsym->st_size;
86 ve = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
88 for (srcobj = dstobj->next; srcobj != NULL;
89 srcobj = srcobj->next) {
90 if ((srcsym = symlook_obj(name, hash, srcobj, ve, 0))
97 _rtld_error("Undefined symbol \"%s\" "
98 " referenced from COPY"
99 " relocation in %s", name, dstobj->path);
103 srcaddr = (const void *) (srcobj->relocbase+srcsym->st_value);
104 memcpy(dstaddr, srcaddr, size);
105 dbg("copy_reloc: src=%p,dst=%p,size=%d\n",srcaddr,dstaddr,size);
113 * Perform early relocation of the run-time linker image
116 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
118 const Elf_Rela *rela = 0, *relalim;
123 * Extract the rela/relasz values from the dynamic section
125 for (; dynp->d_tag != DT_NULL; dynp++) {
126 switch (dynp->d_tag) {
128 rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
131 relasz = dynp->d_un.d_val;
137 * Relocate these values
139 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
140 for (; rela < relalim; rela++) {
141 where = (Elf_Addr *)(relocbase + rela->r_offset);
142 *where = (Elf_Addr)(relocbase + rela->r_addend);
148 * Relocate a non-PLT object with addend.
151 reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
154 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
156 const Obj_Entry *defobj;
159 switch (ELF_R_TYPE(rela->r_info)) {
164 case R_PPC_ADDR32: /* word32 S + A */
165 case R_PPC_GLOB_DAT: /* word32 S + A */
166 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
172 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
175 /* Don't issue write if unnecessary; avoid COW page fault */
181 case R_PPC_RELATIVE: /* word32 B + A */
182 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
184 /* As above, don't issue write unnecessarily */
192 * These are deferred until all other relocations
193 * have been done. All we do here is make sure
194 * that the COPY relocation is not in a shared
195 * library. They are allowed only in executable
198 if (!obj->mainprog) {
199 _rtld_error("%s: Unexpected R_COPY "
200 " relocation in shared library",
208 * These will be handled by the plt/jmpslot routines
213 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
219 *where = (Elf_Addr) defobj->tlsindex;
224 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
231 * We lazily allocate offsets for static TLS as we
232 * see the first relocation that references the
233 * TLS block. This allows us to support (small
234 * amounts of) static TLS in dynamically loaded
235 * modules. If we run out of space, we generate an
238 if (!defobj->tls_done) {
239 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
240 _rtld_error("%s: No space available for static "
241 "Thread Local Storage", obj->path);
246 *(Elf_Addr **)where = *where * sizeof(Elf_Addr)
247 + (Elf_Addr *)(def->st_value + rela->r_addend
248 + defobj->tlsoffset - TLS_TP_OFFSET);
253 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
259 *where += (Elf_Addr)(def->st_value + rela->r_addend
265 _rtld_error("%s: Unsupported relocation type %d"
266 " in non-PLT relocations\n", obj->path,
267 ELF_R_TYPE(rela->r_info));
275 * Process non-PLT relocations
278 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
280 const Elf_Rela *relalim;
281 const Elf_Rela *rela;
283 int bytes = obj->nchains * sizeof(SymCache);
287 * The dynamic loader may be called from a thread, we have
288 * limited amounts of stack available so we cannot use alloca().
290 if (obj != obj_rtld) {
291 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON,
293 if (cache == MAP_FAILED)
299 * From the SVR4 PPC ABI:
300 * "The PowerPC family uses only the Elf32_Rela relocation
301 * entries with explicit addends."
303 relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
304 for (rela = obj->rela; rela < relalim; rela++) {
305 if (reloc_nonplt_object(obj_rtld, obj, rela, cache) < 0)
311 munmap(cache, bytes);
318 * Initialise a PLT slot to the resolving trampoline
321 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
323 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
324 Elf_Addr *pltresolve;
328 reloff = rela - obj->pltrela;
330 if ((reloff < 0) || (reloff >= 0x8000)) {
334 pltresolve = obj->pltgot + 8;
336 distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
338 dbg(" reloc_plt_object: where=%p,pltres=%p,reloff=%x,distance=%x",
339 (void *)where, (void *)pltresolve, reloff, distance);
343 where[0] = 0x39600000 | reloff;
344 where[1] = 0x48000000 | (distance & 0x03fffffc);
347 * The icache will be sync'd in init_pltgot, which is called
348 * after all the slots have been updated
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) {
366 relalim = (const Elf_Rela *)((char *)obj->pltrela +
368 for (rela = obj->pltrela; rela < relalim; rela++) {
369 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
371 if (reloc_plt_object(obj, rela) < 0) {
382 * LD_BIND_NOW was set - force relocation for all jump slots
385 reloc_jmpslots(Obj_Entry *obj)
387 const Obj_Entry *defobj;
388 const Elf_Rela *relalim;
389 const Elf_Rela *rela;
394 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
395 for (rela = obj->pltrela; rela < relalim; rela++) {
396 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
397 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
398 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
401 dbg("reloc_jmpslots: sym not found");
405 target = (Elf_Addr)(defobj->relocbase + def->st_value);
409 dbg("\"%s\" in \"%s\" --> %p in \"%s\"",
410 defobj->strtab + def->st_name, basename(obj->path),
411 (void *)target, basename(defobj->path));
414 reloc_jmpslot(where, target, defobj, obj,
415 (const Elf_Rel *) rela);
418 obj->jmpslots_done = true;
425 * Update the value of a PLT jump slot. Branch directly to the target if
426 * it is within +/- 32Mb, otherwise go indirectly via the pltcall
427 * trampoline call and jump table.
430 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj,
431 const Obj_Entry *obj, const Elf_Rel *rel)
434 const Elf_Rela *rela = (const Elf_Rela *) rel;
436 dbg(" reloc_jmpslot: where=%p, target=%p",
437 (void *)wherep, (void *)target);
440 * At the PLT entry pointed at by `wherep', construct
441 * a direct transfer to the now fully resolved function
444 offset = target - (Elf_Addr)wherep;
446 if (abs(offset) < 32*1024*1024) { /* inside 32MB? */
447 /* b value # branch directly */
448 *wherep = 0x48000000 | (offset & 0x03fffffc);
449 __syncicache(wherep, 4);
451 Elf_Addr *pltcall, *jmptab;
453 int N = obj->pltrelasize / sizeof(Elf_Rela);
454 int reloff = rela - obj->pltrela;
456 if ((reloff < 0) || (reloff >= 0x8000)) {
460 pltcall = obj->pltgot;
462 dbg(" reloc_jmpslot: indir, reloff=%d, N=%d\n",
465 jmptab = obj->pltgot + 18 + N * 2;
466 jmptab[reloff] = target;
468 distance = (Elf_Addr)pltcall - (Elf_Addr)(wherep + 1);
471 /* b pltcall # use indirect pltcall routine */
472 wherep[0] = 0x39600000 | reloff;
473 wherep[1] = 0x48000000 | (distance & 0x03fffffc);
474 __syncicache(wherep, 8);
482 * Setup the plt glue routines.
484 #define PLTCALL_SIZE 20
485 #define PLTRESOLVE_SIZE 24
488 init_pltgot(Obj_Entry *obj)
490 Elf_Word *pltcall, *pltresolve;
492 int N = obj->pltrelasize / sizeof(Elf_Rela);
494 pltcall = obj->pltgot;
496 if (pltcall == NULL) {
501 * From the SVR4 PPC ABI:
503 * 'The first 18 words (72 bytes) of the PLT are reserved for
504 * use by the dynamic linker.
506 * 'If the executable or shared object requires N procedure
507 * linkage table entries, the link editor shall reserve 3*N
508 * words (12*N bytes) following the 18 reserved words. The
509 * first 2*N of these words are the procedure linkage table
510 * entries themselves. The static linker directs calls to bytes
511 * (72 + (i-1)*8), for i between 1 and N inclusive. The remaining
512 * N words (4*N bytes) are reserved for use by the dynamic linker.'
516 * Copy the absolute-call assembler stub into the first part of
517 * the reserved PLT area.
519 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
522 * Determine the address of the jumptable, which is the dyn-linker
523 * reserved area after the call cells. Write the absolute address
524 * of the jumptable into the absolute-call assembler code so it
525 * can determine this address.
527 jmptab = pltcall + 18 + N * 2;
528 pltcall[1] |= _ppc_ha(jmptab); /* addis 11,11,jmptab@ha */
529 pltcall[2] |= _ppc_la(jmptab); /* lwz 11,jmptab@l(11) */
532 * Skip down 32 bytes into the initial reserved area and copy
533 * in the standard resolving assembler call. Into this assembler,
534 * insert the absolute address of the _rtld_bind_start routine
535 * and the address of the relocation object.
537 pltresolve = obj->pltgot + 8;
539 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
540 pltresolve[0] |= _ppc_ha(_rtld_bind_start);
541 pltresolve[1] |= _ppc_la(_rtld_bind_start);
542 pltresolve[3] |= _ppc_ha(obj);
543 pltresolve[4] |= _ppc_la(obj);
546 * Sync the icache for the byte range represented by the
547 * trampoline routines and call slots.
549 __syncicache(pltcall, 72 + N * 8);
553 allocate_initial_tls(Obj_Entry *list)
555 register Elf_Addr **tp __asm__("r2");
559 * Fix the size of the static TLS block by using the maximum
560 * offset allocated so far and adding a bit for dynamic modules to
564 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
566 _tp = (Elf_Addr **) ((char *) allocate_tls(list, NULL, TLS_TCB_SIZE, 8)
567 + TLS_TP_OFFSET + TLS_TCB_SIZE);
570 * XXX gcc seems to ignore 'tp = _tp;'
573 __asm __volatile("mr %0,%1" : "=r"(tp) : "r"(_tp));
577 __tls_get_addr(tls_index* ti)
579 register Elf_Addr **tp __asm__("r2");
582 p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET
583 - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset);
585 return (p + TLS_DTV_OFFSET);