Home | History | Annotate | Line # | Download | only in powerpc
ppc_reloc.c revision 1.24
      1 /*	$NetBSD: ppc_reloc.c,v 1.24 2002/09/12 22:56:30 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1998	Tsubai Masanari
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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.
     17  *
     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.
     28  */
     29 
     30 #include <stdarg.h>
     31 #include <stdio.h>
     32 #include <stdlib.h>
     33 #include <string.h>
     34 #include <sys/types.h>
     35 #include <sys/stat.h>
     36 #include <machine/cpu.h>
     37 
     38 #include "debug.h"
     39 #include "rtld.h"
     40 
     41 caddr_t _rtld_bind_powerpc __P((const Obj_Entry *, Elf_Word));
     42 void _rtld_powerpc_pltcall __P((Elf_Word));
     43 void _rtld_powerpc_pltresolve __P((Elf_Word, Elf_Word));
     44 
     45 #define ha(x) ((((u_int32_t)(x) & 0x8000) ? \
     46 			((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
     47 #define l(x) ((u_int32_t)(x) & 0xffff)
     48 
     49 void _rtld_bind_start(void);
     50 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     51 
     52 /*
     53  * Bind a pltgot slot indexed by reloff.
     54  */
     55 caddr_t
     56 _rtld_bind_powerpc(obj, reloff)
     57 	const Obj_Entry *obj;
     58 	Elf_Word reloff;
     59 {
     60 	const Elf_Rela *rela;
     61 	caddr_t		addr;
     62 
     63 	if (reloff < 0 || reloff >= 0x8000) {
     64 		dbg(("_rtld_bind_powerpc: broken reloff %x", reloff));
     65 		_rtld_die();
     66 	}
     67 
     68 	rela = obj->pltrela + reloff;
     69 
     70 	if (_rtld_relocate_plt_object(obj, rela, &addr, true) < 0)
     71 		_rtld_die();
     72 
     73 	return addr;
     74 }
     75 
     76 int
     77 _rtld_relocate_plt_object(
     78 	const Obj_Entry *obj,
     79 	const Elf_Rela *rela,
     80 	caddr_t *addrp)
     81 {
     82 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
     83 	int distance;
     84 
     85 	const Elf_Sym *def;
     86 	const Obj_Entry *defobj;
     87 	Elf_Addr value;
     88 
     89 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
     90 
     91 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
     92 	    true);
     93 	if (def == NULL)
     94 		return (-1);
     95 
     96 	value = (Elf_Addr)(defobj->relocbase + def->st_value);
     97 	distance = value - (Elf_Addr)where;
     98 
     99 	if (abs(distance) < 32*1024*1024) {	/* inside 32MB? */
    100 		/* b	value	# branch directly */
    101 		*where = 0x48000000 | (distance & 0x03fffffc);
    102 		__syncicache(where, 4);
    103 	} else {
    104 		Elf_Addr *pltcall, *jmptab;
    105 		int N = obj->pltrelalim - obj->pltrela;
    106 		int reloff = rela - obj->pltrela;
    107 
    108 		if (reloff < 0 || reloff >= 0x8000)
    109 			return (-1);
    110 
    111 		pltcall = obj->pltgot;
    112 
    113 		jmptab = pltcall + 18 + N * 2;
    114 		jmptab[reloff] = value;
    115 
    116 		distance = (Elf_Addr)pltcall - (Elf_Addr)(where + 1);
    117 
    118 		/* li	r11,reloff */
    119 		/* b	pltcall		# use pltcall routine */
    120 		where[0] = 0x39600000 | reloff;
    121 		where[1] = 0x48000000 | (distance & 0x03fffffc);
    122 		__syncicache(where, 8);
    123 	}
    124 
    125 	*addrp = (caddr_t)value;
    126 	return (0);
    127 }
    128 
    129 /*
    130  * Setup the plt glue routines.
    131  */
    132 #define PLTCALL_SIZE	20
    133 #define PLTRESOLVE_SIZE	24
    134 
    135 void
    136 _rtld_setup_pltgot(obj)
    137 	const Obj_Entry *obj;
    138 {
    139 	Elf_Word *pltcall, *pltresolve;
    140 	Elf_Word *jmptab;
    141 	int N = obj->pltrelalim - obj->pltrela;
    142 
    143 	pltcall = obj->pltgot;
    144 
    145 	memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
    146 	jmptab = pltcall + 18 + N * 2;
    147 	pltcall[1] |= ha(jmptab);
    148 	pltcall[2] |= l(jmptab);
    149 
    150 	pltresolve = obj->pltgot + 8;
    151 
    152 	memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
    153 	pltresolve[0] |= ha(_rtld_bind_start);
    154 	pltresolve[1] |= l(_rtld_bind_start);
    155 	pltresolve[3] |= ha(obj);
    156 	pltresolve[4] |= l(obj);
    157 
    158 	__syncicache(pltcall, 72 + N * 8);
    159 }
    160 
    161 void
    162 _rtld_relocate_nonplt_self(dynp, relocbase)
    163 	Elf_Dyn *dynp;
    164 	Elf_Addr relocbase;
    165 {
    166 	const Elf_Rela *rela = 0, *relalim;
    167 	Elf_Addr relasz = 0;
    168 	Elf_Addr *where;
    169 
    170 	for (; dynp->d_tag != DT_NULL; dynp++) {
    171 		switch (dynp->d_tag) {
    172 		case DT_RELA:
    173 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    174 			break;
    175 		case DT_RELASZ:
    176 			relasz = dynp->d_un.d_val;
    177 			break;
    178 		}
    179 	}
    180 	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
    181 	for (; rela < relalim; rela++) {
    182 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    183 		*where = (Elf_Addr)(relocbase + rela->r_addend);
    184 	}
    185 }
    186 
    187 int
    188 _rtld_relocate_nonplt_objects(obj, self)
    189 	const Obj_Entry *obj;
    190 	bool self;
    191 {
    192 	const Elf_Rela *rela;
    193 
    194 	if (self)
    195 		return 0;
    196 
    197 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    198 		Elf_Addr        *where;
    199 		const Elf_Sym   *def;
    200 		const Obj_Entry *defobj;
    201 		Elf_Addr         tmp;
    202 		unsigned long	 symnum;
    203 
    204 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    205 		symnum = ELF_R_SYM(rela->r_info);
    206 
    207 		switch (ELF_R_TYPE(rela->r_info)) {
    208 		case R_TYPE(NONE):
    209 			break;
    210 
    211 		case R_TYPE(32):	/* word32 S + A */
    212 		case R_TYPE(GLOB_DAT):	/* word32 S + A */
    213 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    214 			if (def == NULL)
    215 				return -1;
    216 
    217 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    218 			    rela->r_addend);
    219 			if (*where != tmp)
    220 				*where = tmp;
    221 			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
    222 			    obj->strtab + obj->symtab[symnum].st_name,
    223 			    obj->path, (void *)*where, defobj->path));
    224 			break;
    225 
    226 		case R_TYPE(RELATIVE):	/* word32 B + A */
    227 			*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
    228 			rdbg(("RELATIVE in %s --> %p", obj->path,
    229 			    (void *)*where));
    230 			break;
    231 
    232 		case R_TYPE(COPY):
    233 			/*
    234 			 * These are deferred until all other relocations have
    235 			 * been done.  All we do here is make sure that the
    236 			 * COPY relocation is not in a shared library.  They
    237 			 * are allowed only in executable files.
    238 			 */
    239 			if (obj->isdynamic) {
    240 				_rtld_error(
    241 			"%s: Unexpected R_COPY relocation in shared library",
    242 				    obj->path);
    243 				return -1;
    244 			}
    245 			rdbg(("COPY (avoid in main)"));
    246 			break;
    247 
    248 		default:
    249 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    250 			    "addend = %p, contents = %p, symbol = %s",
    251 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    252 			    (void *)rela->r_offset, (void *)rela->r_addend,
    253 			    (void *)*where,
    254 			    obj->strtab + obj->symtab[symnum].st_name));
    255 			_rtld_error("%s: Unsupported relocation type %ld "
    256 			    "in non-PLT relocations\n",
    257 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    258 			return -1;
    259 		}
    260 	}
    261 	return 0;
    262 }
    263 
    264 int
    265 _rtld_relocate_plt_lazy(obj)
    266 	const Obj_Entry *obj;
    267 {
    268 	const Elf_Rela *rela;
    269 
    270 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    271 		Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    272 		int distance;
    273 		Elf_Addr *pltresolve;
    274 		int reloff = rela - obj->pltrela;
    275 
    276 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    277 
    278 		if (reloff < 0 || reloff >= 0x8000)
    279 			return (-1);
    280 
    281 		pltresolve = obj->pltgot + 8;
    282 
    283 		distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
    284 
    285        		/* li	r11,reloff */
    286 		/* b	pltresolve   */
    287 		where[0] = 0x39600000 | reloff;
    288 		where[1] = 0x48000000 | (distance & 0x03fffffc);
    289 		/* __syncicache(where, 8); */
    290 	}
    291 
    292 	return 0;
    293 }
    294