Home | History | Annotate | Line # | Download | only in powerpc
ppc_reloc.c revision 1.36
      1 /*	$NetBSD: ppc_reloc.c,v 1.36 2005/07/28 10:54:30 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1998	Tsubai Masanari
      5  * Portions copyright 2002 Charles M. Hannum <root (at) ihack.net>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <stdarg.h>
     32 #include <stdio.h>
     33 #include <stdlib.h>
     34 #include <string.h>
     35 #include <sys/types.h>
     36 #include <sys/stat.h>
     37 #include <machine/cpu.h>
     38 
     39 #include "debug.h"
     40 #include "rtld.h"
     41 
     42 void _rtld_powerpc_pltcall(Elf_Word);
     43 void _rtld_powerpc_pltresolve(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 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     52 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
     53     const Elf_Rela *, int, Elf_Addr *);
     54 
     55 /*
     56  * Setup the plt glue routines.
     57  */
     58 #define PLTCALL_SIZE	20
     59 #define PLTRESOLVE_SIZE	24
     60 
     61 void
     62 _rtld_setup_pltgot(const Obj_Entry *obj)
     63 {
     64 	Elf_Word *pltcall, *pltresolve;
     65 	Elf_Word *jmptab;
     66 	int N = obj->pltrelalim - obj->pltrela;
     67 
     68 	/* Entries beyond 8192 take twice as much space. */
     69 	if (N > 8192)
     70 		N += N-8192;
     71 
     72 	pltcall = obj->pltgot;
     73 	jmptab = pltcall + 18 + N * 2;
     74 
     75 	memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
     76 	pltcall[1] |= ha(jmptab);
     77 	pltcall[2] |= l(jmptab);
     78 
     79 	pltresolve = obj->pltgot + 8;
     80 
     81 	memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
     82 	pltresolve[0] |= ha(_rtld_bind_start);
     83 	pltresolve[1] |= l(_rtld_bind_start);
     84 	pltresolve[3] |= ha(obj);
     85 	pltresolve[4] |= l(obj);
     86 
     87 	__syncicache(pltcall, 72 + N * 8);
     88 }
     89 
     90 void
     91 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     92 {
     93 	const Elf_Rela *rela = 0, *relalim;
     94 	Elf_Addr relasz = 0;
     95 	Elf_Addr *where;
     96 
     97 	for (; dynp->d_tag != DT_NULL; dynp++) {
     98 		switch (dynp->d_tag) {
     99 		case DT_RELA:
    100 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    101 			break;
    102 		case DT_RELASZ:
    103 			relasz = dynp->d_un.d_val;
    104 			break;
    105 		}
    106 	}
    107 	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
    108 	for (; rela < relalim; rela++) {
    109 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    110 		*where = (Elf_Addr)(relocbase + rela->r_addend);
    111 	}
    112 }
    113 
    114 int
    115 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
    116 {
    117 	const Elf_Rela *rela;
    118 
    119 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    120 		Elf_Addr        *where;
    121 		const Elf_Sym   *def;
    122 		const Obj_Entry *defobj;
    123 		Elf_Addr         tmp;
    124 		unsigned long	 symnum;
    125 
    126 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    127 		symnum = ELF_R_SYM(rela->r_info);
    128 
    129 		switch (ELF_R_TYPE(rela->r_info)) {
    130 #if 1 /* XXX Should not be necessary. */
    131 		case R_TYPE(JMP_SLOT):
    132 #endif
    133 		case R_TYPE(NONE):
    134 			break;
    135 
    136 		case R_TYPE(32):	/* word32 S + A */
    137 		case R_TYPE(GLOB_DAT):	/* word32 S + A */
    138 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    139 			if (def == NULL)
    140 				return -1;
    141 
    142 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    143 			    rela->r_addend);
    144 			if (*where != tmp)
    145 				*where = tmp;
    146 			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
    147 			    obj->strtab + obj->symtab[symnum].st_name,
    148 			    obj->path, (void *)*where, defobj->path));
    149 			break;
    150 
    151 		case R_TYPE(RELATIVE):	/* word32 B + A */
    152 			*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
    153 			rdbg(("RELATIVE in %s --> %p", obj->path,
    154 			    (void *)*where));
    155 			break;
    156 
    157 		case R_TYPE(COPY):
    158 			/*
    159 			 * These are deferred until all other relocations have
    160 			 * been done.  All we do here is make sure that the
    161 			 * COPY relocation is not in a shared library.  They
    162 			 * are allowed only in executable files.
    163 			 */
    164 			if (obj->isdynamic) {
    165 				_rtld_error(
    166 			"%s: Unexpected R_COPY relocation in shared library",
    167 				    obj->path);
    168 				return -1;
    169 			}
    170 			rdbg(("COPY (avoid in main)"));
    171 			break;
    172 
    173 		default:
    174 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    175 			    "addend = %p, contents = %p, symbol = %s",
    176 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    177 			    (void *)rela->r_offset, (void *)rela->r_addend,
    178 			    (void *)*where,
    179 			    obj->strtab + obj->symtab[symnum].st_name));
    180 			_rtld_error("%s: Unsupported relocation type %ld "
    181 			    "in non-PLT relocations\n",
    182 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    183 			return -1;
    184 		}
    185 	}
    186 	return 0;
    187 }
    188 
    189 int
    190 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    191 {
    192 	const Elf_Rela *rela;
    193 	int reloff;
    194 
    195 	for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
    196 		Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    197 		int distance;
    198 		Elf_Addr *pltresolve;
    199 
    200 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    201 
    202 		pltresolve = obj->pltgot + 8;
    203 
    204 		if (reloff < 32768) {
    205 	       		/* li	r11,reloff */
    206 			*where++ = 0x39600000 | reloff;
    207 		} else {
    208 			/* lis  r11,ha(reloff) */
    209 			/* addi	r11,l(reloff) */
    210 			*where++ = 0x3d600000 | ha(reloff);
    211 			*where++ = 0x396b0000 | l(reloff);
    212 		}
    213 		/* b	pltresolve */
    214 		distance = (Elf_Addr)pltresolve - (Elf_Addr)where;
    215 		*where++ = 0x48000000 | (distance & 0x03fffffc);
    216 		/* __syncicache(where - 12, 12); */
    217 	}
    218 
    219 	return 0;
    220 }
    221 
    222 static inline int
    223 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, int reloff, Elf_Addr *tp)
    224 {
    225 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    226 	Elf_Addr value;
    227 	const Elf_Sym *def;
    228 	const Obj_Entry *defobj;
    229 	int distance;
    230 
    231 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    232 
    233 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    234 	if (def == NULL)
    235 		return -1;
    236 
    237 	value = (Elf_Addr)(defobj->relocbase + def->st_value);
    238 	distance = value - (Elf_Addr)where;
    239 	rdbg(("bind now/fixup in %s --> new=%p",
    240 	    defobj->strtab + def->st_name, (void *)value));
    241 
    242 	if (abs(distance) < 32*1024*1024) {	/* inside 32MB? */
    243 		/* b	value	# branch directly */
    244 		*where = 0x48000000 | (distance & 0x03fffffc);
    245 		__syncicache(where, 4);
    246 	} else {
    247 		Elf_Addr *pltcall, *jmptab;
    248 		int N = obj->pltrelalim - obj->pltrela;
    249 
    250 		/* Entries beyond 8192 take twice as much space. */
    251 		if (N > 8192)
    252 			N += N-8192;
    253 
    254 		pltcall = obj->pltgot;
    255 		jmptab = pltcall + 18 + N * 2;
    256 
    257 		jmptab[reloff] = value;
    258 
    259 		if (reloff < 32768) {
    260 			/* li	r11,reloff */
    261 			*where++ = 0x39600000 | reloff;
    262 		} else {
    263 			/* lis  r11,ha(reloff) */
    264 			/* addi	r11,l(reloff) */
    265 			*where++ = 0x3d600000 | ha(reloff);
    266 			*where++ = 0x396b0000 | l(reloff);
    267 		}
    268 		/* b	pltcall	*/
    269 		distance = (Elf_Addr)pltcall - (Elf_Addr)where;
    270 		*where++ = 0x48000000 | (distance & 0x03fffffc);
    271 		__syncicache(where - 12, 12);
    272 	}
    273 
    274 	if (tp)
    275 		*tp = value;
    276 	return 0;
    277 }
    278 
    279 caddr_t
    280 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    281 {
    282 	const Elf_Rela *rela = obj->pltrela + reloff;
    283 	Elf_Addr new_value;
    284 	int err;
    285 
    286 	err = _rtld_relocate_plt_object(obj, rela, reloff, &new_value);
    287 	if (err)
    288 		_rtld_die();
    289 
    290 	return (caddr_t)new_value;
    291 }
    292 
    293 int
    294 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    295 {
    296 	const Elf_Rela *rela;
    297 	int reloff;
    298 
    299 	for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
    300 		if (_rtld_relocate_plt_object(obj, rela, reloff, NULL) < 0)
    301 			return -1;
    302 	}
    303 	return 0;
    304 }
    305