Home | History | Annotate | Line # | Download | only in powerpc
ppc_reloc.c revision 1.18
      1 /*	$NetBSD: ppc_reloc.c,v 1.18 2002/09/06 03:12:07 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((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 /*
     50  * Bind a pltgot slot indexed by reloff.
     51  */
     52 caddr_t
     53 _rtld_bind_powerpc(obj, reloff)
     54 	Obj_Entry *obj;
     55 	Elf_Word reloff;
     56 {
     57 	const Elf_Rela *rela;
     58 	caddr_t		addr;
     59 
     60 	if (reloff < 0 || reloff >= 0x8000) {
     61 		dbg(("_rtld_bind_powerpc: broken reloff %x", reloff));
     62 		_rtld_die();
     63 	}
     64 
     65 	rela = obj->pltrela + reloff;
     66 
     67 	if (_rtld_relocate_plt_object(obj, rela, &addr, true) < 0)
     68 		_rtld_die();
     69 
     70 	return addr;
     71 }
     72 
     73 int
     74 _rtld_relocate_plt_object(
     75 	const Obj_Entry *obj,
     76 	const Elf_Rela *rela,
     77 	caddr_t *addrp,
     78 	bool dodebug)
     79 {
     80 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
     81 	int distance;
     82 
     83 	const Elf_Sym *def;
     84 	const Obj_Entry *defobj;
     85 	Elf_Addr value;
     86 
     87 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
     88 
     89 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
     90 	    true);
     91 	if (def == NULL)
     92 		return (-1);
     93 
     94 	value = (Elf_Addr)(defobj->relocbase + def->st_value);
     95 	distance = value - (Elf_Addr)where;
     96 
     97 	if (abs(distance) < 32*1024*1024) {	/* inside 32MB? */
     98 		/* b	value	# branch directly */
     99 		*where = 0x48000000 | (distance & 0x03fffffc);
    100 		__syncicache(where, 4);
    101 	} else {
    102 		Elf_Addr *pltcall, *jmptab;
    103 		int N = obj->pltrelalim - obj->pltrela;
    104 		int reloff = rela - obj->pltrela;
    105 
    106 		if (reloff < 0 || reloff >= 0x8000)
    107 			return (-1);
    108 
    109 		pltcall = obj->pltgot;
    110 
    111 		jmptab = pltcall + 18 + N * 2;
    112 		jmptab[reloff] = value;
    113 
    114 		distance = (Elf_Addr)pltcall - (Elf_Addr)(where + 1);
    115 
    116 		/* li	r11,reloff */
    117 		/* b	pltcall		# use pltcall routine */
    118 		where[0] = 0x39600000 | reloff;
    119 		where[1] = 0x48000000 | (distance & 0x03fffffc);
    120 		__syncicache(where, 8);
    121 	}
    122 
    123 	*addrp = (caddr_t)value;
    124 	return (0);
    125 }
    126 
    127 /*
    128  * Setup the plt glue routines.
    129  */
    130 #define PLTCALL_SIZE	20
    131 #define PLTRESOLVE_SIZE	24
    132 
    133 void
    134 _rtld_setup_pltgot(obj)
    135 	const Obj_Entry *obj;
    136 {
    137 	Elf_Word *pltcall, *pltresolve;
    138 	Elf_Word *jmptab;
    139 	int N = obj->pltrelalim - obj->pltrela;
    140 
    141 	pltcall = obj->pltgot;
    142 
    143 	memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
    144 	jmptab = pltcall + 18 + N * 2;
    145 	pltcall[1] |= ha(jmptab);
    146 	pltcall[2] |= l(jmptab);
    147 
    148 	pltresolve = obj->pltgot + 8;
    149 
    150 	memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
    151 	pltresolve[0] |= ha(_rtld_bind_start);
    152 	pltresolve[1] |= l(_rtld_bind_start);
    153 	pltresolve[3] |= ha(obj);
    154 	pltresolve[4] |= l(obj);
    155 
    156 	__syncicache(pltcall, 72 + N * 8);
    157 }
    158 
    159 int
    160 _rtld_relocate_nonplt_objects(obj, dodebug)
    161 	const Obj_Entry *obj;
    162 	bool dodebug;
    163 {
    164 	const Elf_Rela *rela;
    165 
    166 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    167 		Elf_Addr        *where;
    168 		const Elf_Sym   *def;
    169 		const Obj_Entry *defobj;
    170 		Elf_Addr         tmp;
    171 		unsigned long	 symnum;
    172 
    173 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    174 		symnum = ELF_R_SYM(rela->r_info);
    175 
    176 		switch (ELF_R_TYPE(rela->r_info)) {
    177 		case R_TYPE(NONE):
    178 			break;
    179 
    180 		case R_TYPE(32):	/* word32 S + A */
    181 		case R_TYPE(GLOB_DAT):	/* word32 S + A */
    182 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    183 			if (def == NULL)
    184 				return -1;
    185 
    186 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    187 			    rela->r_addend);
    188 			if (*where != tmp)
    189 				*where = tmp;
    190 			rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
    191 			    obj->strtab + obj->symtab[symnum].st_name,
    192 			    obj->path, (void *)*where, defobj->path));
    193 			break;
    194 
    195 		case R_TYPE(RELATIVE):	/* word32 B + A */
    196 			tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
    197 			if (*where != tmp)
    198 				*where = tmp;
    199 			rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
    200 			    (void *)*where));
    201 			break;
    202 
    203 		case R_TYPE(COPY):
    204 			/*
    205 			 * These are deferred until all other relocations have
    206 			 * been done.  All we do here is make sure that the
    207 			 * COPY relocation is not in a shared library.  They
    208 			 * are allowed only in executable files.
    209 			 */
    210 			if (!obj->mainprog) {
    211 				_rtld_error(
    212 			"%s: Unexpected R_COPY relocation in shared library",
    213 				    obj->path);
    214 				return -1;
    215 			}
    216 			rdbg(dodebug, ("COPY (avoid in main)"));
    217 			break;
    218 
    219 		default:
    220 			rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    221 			    "addend = %p, contents = %p, symbol = %s",
    222 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    223 			    (void *)rela->r_offset, (void *)rela->r_addend,
    224 			    (void *)*where,
    225 			    obj->strtab + obj->symtab[symnum].st_name));
    226 			_rtld_error("%s: Unsupported relocation type %ld "
    227 			    "in non-PLT relocations\n",
    228 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    229 			return -1;
    230 		}
    231 	}
    232 	return 0;
    233 }
    234 
    235 int
    236 _rtld_relocate_plt_lazy(obj, dodebug)
    237 	const Obj_Entry *obj;
    238 	bool dodebug;
    239 {
    240 	const Elf_Rela *rela;
    241 
    242 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    243 		Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    244 		int distance;
    245 		Elf_Addr *pltresolve;
    246 		int reloff = rela - obj->pltrela;
    247 
    248 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    249 
    250 		if (reloff < 0 || reloff >= 0x8000)
    251 			return (-1);
    252 
    253 		pltresolve = obj->pltgot + 8;
    254 
    255 		distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
    256 
    257        		/* li	r11,reloff */
    258 		/* b	pltresolve   */
    259 		where[0] = 0x39600000 | reloff;
    260 		where[1] = 0x48000000 | (distance & 0x03fffffc);
    261 		/* __syncicache(where, 8); */
    262 	}
    263 
    264 	return 0;
    265 }
    266