Home | History | Annotate | Line # | Download | only in powerpc
ppc_reloc.c revision 1.22
      1 /*	$NetBSD: ppc_reloc.c,v 1.22 2002/09/11 21:14:08 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_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     50 
     51 /*
     52  * Bind a pltgot slot indexed by reloff.
     53  */
     54 caddr_t
     55 _rtld_bind_powerpc(obj, reloff)
     56 	const Obj_Entry *obj;
     57 	Elf_Word reloff;
     58 {
     59 	const Elf_Rela *rela;
     60 	caddr_t		addr;
     61 
     62 	if (reloff < 0 || reloff >= 0x8000) {
     63 		dbg(("_rtld_bind_powerpc: broken reloff %x", reloff));
     64 		_rtld_die();
     65 	}
     66 
     67 	rela = obj->pltrela + reloff;
     68 
     69 	if (_rtld_relocate_plt_object(obj, rela, &addr, true) < 0)
     70 		_rtld_die();
     71 
     72 	return addr;
     73 }
     74 
     75 int
     76 _rtld_relocate_plt_object(
     77 	const Obj_Entry *obj,
     78 	const Elf_Rela *rela,
     79 	caddr_t *addrp,
     80 	bool dodebug)
     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, dodebug)
    189 	const Obj_Entry *obj;
    190 	bool self;
    191 	bool dodebug;
    192 {
    193 	const Elf_Rela *rela;
    194 
    195 	if (self)
    196 		return 0;
    197 
    198 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    199 		Elf_Addr        *where;
    200 		const Elf_Sym   *def;
    201 		const Obj_Entry *defobj;
    202 		Elf_Addr         tmp;
    203 		unsigned long	 symnum;
    204 
    205 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    206 		symnum = ELF_R_SYM(rela->r_info);
    207 
    208 		switch (ELF_R_TYPE(rela->r_info)) {
    209 		case R_TYPE(NONE):
    210 			break;
    211 
    212 		case R_TYPE(32):	/* word32 S + A */
    213 		case R_TYPE(GLOB_DAT):	/* word32 S + A */
    214 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    215 			if (def == NULL)
    216 				return -1;
    217 
    218 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    219 			    rela->r_addend);
    220 			if (*where != tmp)
    221 				*where = tmp;
    222 			rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
    223 			    obj->strtab + obj->symtab[symnum].st_name,
    224 			    obj->path, (void *)*where, defobj->path));
    225 			break;
    226 
    227 		case R_TYPE(RELATIVE):	/* word32 B + A */
    228 			*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
    229 			rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
    230 			    (void *)*where));
    231 			break;
    232 
    233 		case R_TYPE(COPY):
    234 			/*
    235 			 * These are deferred until all other relocations have
    236 			 * been done.  All we do here is make sure that the
    237 			 * COPY relocation is not in a shared library.  They
    238 			 * are allowed only in executable files.
    239 			 */
    240 			if (obj->isdynamic) {
    241 				_rtld_error(
    242 			"%s: Unexpected R_COPY relocation in shared library",
    243 				    obj->path);
    244 				return -1;
    245 			}
    246 			rdbg(dodebug, ("COPY (avoid in main)"));
    247 			break;
    248 
    249 		default:
    250 			rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    251 			    "addend = %p, contents = %p, symbol = %s",
    252 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    253 			    (void *)rela->r_offset, (void *)rela->r_addend,
    254 			    (void *)*where,
    255 			    obj->strtab + obj->symtab[symnum].st_name));
    256 			_rtld_error("%s: Unsupported relocation type %ld "
    257 			    "in non-PLT relocations\n",
    258 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    259 			return -1;
    260 		}
    261 	}
    262 	return 0;
    263 }
    264 
    265 int
    266 _rtld_relocate_plt_lazy(obj, dodebug)
    267 	const Obj_Entry *obj;
    268 	bool dodebug;
    269 {
    270 	const Elf_Rela *rela;
    271 
    272 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    273 		Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    274 		int distance;
    275 		Elf_Addr *pltresolve;
    276 		int reloff = rela - obj->pltrela;
    277 
    278 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    279 
    280 		if (reloff < 0 || reloff >= 0x8000)
    281 			return (-1);
    282 
    283 		pltresolve = obj->pltgot + 8;
    284 
    285 		distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
    286 
    287        		/* li	r11,reloff */
    288 		/* b	pltresolve   */
    289 		where[0] = 0x39600000 | reloff;
    290 		where[1] = 0x48000000 | (distance & 0x03fffffc);
    291 		/* __syncicache(where, 8); */
    292 	}
    293 
    294 	return 0;
    295 }
    296