Home | History | Annotate | Line # | Download | only in powerpc
ppc_reloc.c revision 1.39
      1 /*	$NetBSD: ppc_reloc.c,v 1.39 2005/09/27 07:20:32 chs 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 <sys/cdefs.h>
     32 #ifndef lint
     33 __RCSID("$NetBSD: ppc_reloc.c,v 1.39 2005/09/27 07:20:32 chs Exp $");
     34 #endif /* not lint */
     35 
     36 #include <stdarg.h>
     37 #include <stdio.h>
     38 #include <stdlib.h>
     39 #include <string.h>
     40 #include <sys/types.h>
     41 #include <sys/stat.h>
     42 #include <machine/cpu.h>
     43 
     44 #include "debug.h"
     45 #include "rtld.h"
     46 
     47 void _rtld_powerpc_pltcall(Elf_Word);
     48 void _rtld_powerpc_pltresolve(Elf_Word, Elf_Word);
     49 
     50 #define ha(x) ((((u_int32_t)(x) & 0x8000) ? \
     51 			((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
     52 #define l(x) ((u_int32_t)(x) & 0xffff)
     53 
     54 void _rtld_bind_start(void);
     55 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     56 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     57 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
     58     const Elf_Rela *, int, Elf_Addr *);
     59 
     60 /*
     61  * The PPC PLT format consists of three sections:
     62  * (1) The "pltcall" and "pltresolve" glue code.  This is always 18 words.
     63  * (2) The code part of the PLT entries.  There are 2 words per entry for
     64  *     up to 8192 entries, then 4 words per entry for any additional entries.
     65  * (3) The data part of the PLT entries, comprising a jump table.
     66  *     This section is half the size of the second section (ie. 1 or 2 words
     67  *     per entry).
     68  */
     69 
     70 /*
     71  * Setup the plt glue routines.
     72  */
     73 #define PLTCALL_SIZE	20
     74 #define PLTRESOLVE_SIZE	24
     75 
     76 void
     77 _rtld_setup_pltgot(const Obj_Entry *obj)
     78 {
     79 	Elf_Word *pltcall, *pltresolve;
     80 	Elf_Word *jmptab;
     81 	int N = obj->pltrelalim - obj->pltrela;
     82 
     83 	/* Entries beyond 8192 take twice as much space. */
     84 	if (N > 8192)
     85 		N += N-8192;
     86 
     87 	pltcall = obj->pltgot;
     88 	jmptab = pltcall + 18 + N * 2;
     89 
     90 	memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
     91 	pltcall[1] |= ha(jmptab);
     92 	pltcall[2] |= l(jmptab);
     93 
     94 	pltresolve = obj->pltgot + 8;
     95 
     96 	memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
     97 	pltresolve[0] |= ha(_rtld_bind_start);
     98 	pltresolve[1] |= l(_rtld_bind_start);
     99 	pltresolve[3] |= ha(obj);
    100 	pltresolve[4] |= l(obj);
    101 
    102 	/*
    103 	 * Invalidate the icache for only the code part of the PLT
    104 	 * (and not the jump table at the end).
    105 	 */
    106 	__syncicache(pltcall, (char *)jmptab - (char *)pltcall);
    107 }
    108 
    109 void
    110 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    111 {
    112 	const Elf_Rela *rela = 0, *relalim;
    113 	Elf_Addr relasz = 0;
    114 	Elf_Addr *where;
    115 
    116 	for (; dynp->d_tag != DT_NULL; dynp++) {
    117 		switch (dynp->d_tag) {
    118 		case DT_RELA:
    119 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    120 			break;
    121 		case DT_RELASZ:
    122 			relasz = dynp->d_un.d_val;
    123 			break;
    124 		}
    125 	}
    126 	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
    127 	for (; rela < relalim; rela++) {
    128 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    129 		*where = (Elf_Addr)(relocbase + rela->r_addend);
    130 	}
    131 }
    132 
    133 int
    134 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
    135 {
    136 	const Elf_Rela *rela;
    137 
    138 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    139 		Elf_Addr        *where;
    140 		const Elf_Sym   *def;
    141 		const Obj_Entry *defobj;
    142 		Elf_Addr         tmp;
    143 		unsigned long	 symnum;
    144 
    145 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    146 		symnum = ELF_R_SYM(rela->r_info);
    147 
    148 		switch (ELF_R_TYPE(rela->r_info)) {
    149 #if 1 /* XXX Should not be necessary. */
    150 		case R_TYPE(JMP_SLOT):
    151 #endif
    152 		case R_TYPE(NONE):
    153 			break;
    154 
    155 		case R_TYPE(32):	/* word32 S + A */
    156 		case R_TYPE(GLOB_DAT):	/* word32 S + A */
    157 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    158 			if (def == NULL)
    159 				return -1;
    160 
    161 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    162 			    rela->r_addend);
    163 			if (*where != tmp)
    164 				*where = tmp;
    165 			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
    166 			    obj->strtab + obj->symtab[symnum].st_name,
    167 			    obj->path, (void *)*where, defobj->path));
    168 			break;
    169 
    170 		case R_TYPE(RELATIVE):	/* word32 B + A */
    171 			*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
    172 			rdbg(("RELATIVE in %s --> %p", obj->path,
    173 			    (void *)*where));
    174 			break;
    175 
    176 		case R_TYPE(COPY):
    177 			/*
    178 			 * These are deferred until all other relocations have
    179 			 * been done.  All we do here is make sure that the
    180 			 * COPY relocation is not in a shared library.  They
    181 			 * are allowed only in executable files.
    182 			 */
    183 			if (obj->isdynamic) {
    184 				_rtld_error(
    185 			"%s: Unexpected R_COPY relocation in shared library",
    186 				    obj->path);
    187 				return -1;
    188 			}
    189 			rdbg(("COPY (avoid in main)"));
    190 			break;
    191 
    192 		default:
    193 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    194 			    "addend = %p, contents = %p, symbol = %s",
    195 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    196 			    (void *)rela->r_offset, (void *)rela->r_addend,
    197 			    (void *)*where,
    198 			    obj->strtab + obj->symtab[symnum].st_name));
    199 			_rtld_error("%s: Unsupported relocation type %ld "
    200 			    "in non-PLT relocations\n",
    201 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    202 			return -1;
    203 		}
    204 	}
    205 	return 0;
    206 }
    207 
    208 int
    209 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    210 {
    211 	const Elf_Rela *rela;
    212 	int reloff;
    213 
    214 	for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
    215 		Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    216 		int distance;
    217 		Elf_Addr *pltresolve;
    218 
    219 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    220 
    221 		pltresolve = obj->pltgot + 8;
    222 
    223 		if (reloff < 32768) {
    224 	       		/* li	r11,reloff */
    225 			*where++ = 0x39600000 | reloff;
    226 		} else {
    227 			/* lis  r11,ha(reloff) */
    228 			/* addi	r11,l(reloff) */
    229 			*where++ = 0x3d600000 | ha(reloff);
    230 			*where++ = 0x396b0000 | l(reloff);
    231 		}
    232 		/* b	pltresolve */
    233 		distance = (Elf_Addr)pltresolve - (Elf_Addr)where;
    234 		*where++ = 0x48000000 | (distance & 0x03fffffc);
    235 
    236 		/*
    237 		 * Icache invalidation is not done for each entry here
    238 		 * because we sync the entire code part of the PLT once
    239 		 * in _rtld_setup_pltgot() after all the entries have been
    240 		 * initialized.
    241 		 */
    242 		/* __syncicache(where - 3, 12); */
    243 	}
    244 
    245 	return 0;
    246 }
    247 
    248 static inline int
    249 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, int reloff, Elf_Addr *tp)
    250 {
    251 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    252 	Elf_Addr value;
    253 	const Elf_Sym *def;
    254 	const Obj_Entry *defobj;
    255 	int distance;
    256 
    257 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    258 
    259 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    260 	if (def == NULL)
    261 		return -1;
    262 
    263 	value = (Elf_Addr)(defobj->relocbase + def->st_value);
    264 	distance = value - (Elf_Addr)where;
    265 	rdbg(("bind now/fixup in %s --> new=%p",
    266 	    defobj->strtab + def->st_name, (void *)value));
    267 
    268 	if (abs(distance) < 32*1024*1024) {	/* inside 32MB? */
    269 		/* b	value	# branch directly */
    270 		*where = 0x48000000 | (distance & 0x03fffffc);
    271 		__syncicache(where, 4);
    272 	} else {
    273 		Elf_Addr *pltcall, *jmptab;
    274 		int N = obj->pltrelalim - obj->pltrela;
    275 
    276 		/* Entries beyond 8192 take twice as much space. */
    277 		if (N > 8192)
    278 			N += N-8192;
    279 
    280 		pltcall = obj->pltgot;
    281 		jmptab = pltcall + 18 + N * 2;
    282 
    283 		jmptab[reloff] = value;
    284 
    285 		if (reloff < 32768) {
    286 			/* li	r11,reloff */
    287 			*where++ = 0x39600000 | reloff;
    288 		} else {
    289 			/* lis  r11,ha(reloff) */
    290 			/* addi	r11,l(reloff) */
    291 			*where++ = 0x3d600000 | ha(reloff);
    292 			*where++ = 0x396b0000 | l(reloff);
    293 		}
    294 		/* b	pltcall	*/
    295 		distance = (Elf_Addr)pltcall - (Elf_Addr)where;
    296 		*where++ = 0x48000000 | (distance & 0x03fffffc);
    297 		__syncicache(where - 3, 12);
    298 	}
    299 
    300 	if (tp)
    301 		*tp = value;
    302 	return 0;
    303 }
    304 
    305 caddr_t
    306 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    307 {
    308 	const Elf_Rela *rela = obj->pltrela + reloff;
    309 	Elf_Addr new_value;
    310 	int err;
    311 
    312 	err = _rtld_relocate_plt_object(obj, rela, reloff, &new_value);
    313 	if (err)
    314 		_rtld_die();
    315 
    316 	return (caddr_t)new_value;
    317 }
    318 
    319 int
    320 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    321 {
    322 	const Elf_Rela *rela;
    323 	int reloff;
    324 
    325 	for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
    326 		if (_rtld_relocate_plt_object(obj, rela, reloff, NULL) < 0)
    327 			return -1;
    328 	}
    329 	return 0;
    330 }
    331