Home | History | Annotate | Line # | Download | only in powerpc
ppc_reloc.c revision 1.47
      1 /*	$NetBSD: ppc_reloc.c,v 1.47 2011/02/10 02:28:20 matt 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.47 2011/02/10 02:28:20 matt 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 <machine/cpu.h>
     42 
     43 #include "debug.h"
     44 #include "rtld.h"
     45 
     46 void _rtld_powerpc_pltcall(Elf_Word);
     47 void _rtld_powerpc_pltresolve(Elf_Word, Elf_Word);
     48 
     49 #define ha(x) ((((u_int32_t)(x) & 0x8000) ? \
     50 			((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
     51 #define l(x) ((u_int32_t)(x) & 0xffff)
     52 
     53 void _rtld_bind_bssplt_start(void);
     54 void _rtld_bind_secureplt_start(void);
     55 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     56 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     57 static 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 (for bss-plt).
     72  */
     73 #define PLTCALL_SIZE	20
     74 #define PLTRESOLVE_SIZE	24
     75 
     76 void
     77 _rtld_setup_pltgot(const Obj_Entry *obj)
     78 {
     79 	/*
     80 	 * Secure-PLT is much more sane.
     81 	 */
     82 	if (obj->gotptr != NULL) {
     83 		obj->gotptr[1] = (Elf_Addr) _rtld_bind_secureplt_start;
     84 		obj->gotptr[2] = (Elf_Addr) obj;
     85 		dbg(("obj %s secure-plt gotptr=%p start=%p obj=%p",
     86 		    obj->path, obj->gotptr,
     87 		    (void *) obj->gotptr[1], (void *) obj->gotptr[2]));
     88 	} else {
     89 		Elf_Word *pltcall, *pltresolve;
     90 		Elf_Word *jmptab;
     91 		int N = obj->pltrelalim - obj->pltrela;
     92 
     93 		/* Entries beyond 8192 take twice as much space. */
     94 		if (N > 8192)
     95 			N += N-8192;
     96 
     97 		dbg(("obj %s bss-plt pltgot=%p jmptab=%u start=%p obj=%p",
     98 		    obj->path, obj->pltgot, 18 + N * 2,
     99 		    _rtld_bind_bssplt_start, obj));
    100 
    101 		pltcall = obj->pltgot;
    102 		jmptab = pltcall + 18 + N * 2;
    103 
    104 		memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
    105 		pltcall[1] |= ha(jmptab);
    106 		pltcall[2] |= l(jmptab);
    107 
    108 		pltresolve = obj->pltgot + 8;
    109 
    110 		memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
    111 		pltresolve[0] |= ha(_rtld_bind_bssplt_start);
    112 		pltresolve[1] |= l(_rtld_bind_bssplt_start);
    113 		pltresolve[3] |= ha(obj);
    114 		pltresolve[4] |= l(obj);
    115 
    116 		/*
    117 		 * Invalidate the icache for only the code part of the PLT
    118 		 * (and not the jump table at the end).
    119 		 */
    120 		__syncicache(pltcall, (char *)jmptab - (char *)pltcall);
    121 	}
    122 }
    123 
    124 void
    125 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    126 {
    127 	const Elf_Rela *rela = 0, *relalim;
    128 	Elf_Addr relasz = 0;
    129 	Elf_Addr *where;
    130 
    131 	for (; dynp->d_tag != DT_NULL; dynp++) {
    132 		switch (dynp->d_tag) {
    133 		case DT_RELA:
    134 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    135 			break;
    136 		case DT_RELASZ:
    137 			relasz = dynp->d_un.d_val;
    138 			break;
    139 		}
    140 	}
    141 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
    142 	for (; rela < relalim; rela++) {
    143 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    144 		*where = (Elf_Addr)(relocbase + rela->r_addend);
    145 	}
    146 }
    147 
    148 int
    149 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
    150 {
    151 	const Elf_Rela *rela;
    152 
    153 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    154 		Elf_Addr        *where;
    155 		const Elf_Sym   *def;
    156 		const Obj_Entry *defobj;
    157 		Elf_Addr         tmp;
    158 		unsigned long	 symnum;
    159 
    160 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    161 		symnum = ELF_R_SYM(rela->r_info);
    162 
    163 		switch (ELF_R_TYPE(rela->r_info)) {
    164 #if 1 /* XXX Should not be necessary. */
    165 		case R_TYPE(JMP_SLOT):
    166 #endif
    167 		case R_TYPE(NONE):
    168 			break;
    169 
    170 		case R_TYPE(32):	/* word32 S + A */
    171 		case R_TYPE(GLOB_DAT):	/* word32 S + A */
    172 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    173 			if (def == NULL)
    174 				return -1;
    175 
    176 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    177 			    rela->r_addend);
    178 			if (*where != tmp)
    179 				*where = tmp;
    180 			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
    181 			    obj->strtab + obj->symtab[symnum].st_name,
    182 			    obj->path, (void *)*where, defobj->path));
    183 			break;
    184 
    185 		case R_TYPE(RELATIVE):	/* word32 B + A */
    186 			*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
    187 			rdbg(("RELATIVE in %s --> %p", obj->path,
    188 			    (void *)*where));
    189 			break;
    190 
    191 		case R_TYPE(COPY):
    192 			/*
    193 			 * These are deferred until all other relocations have
    194 			 * been done.  All we do here is make sure that the
    195 			 * COPY relocation is not in a shared library.  They
    196 			 * are allowed only in executable files.
    197 			 */
    198 			if (obj->isdynamic) {
    199 				_rtld_error(
    200 			"%s: Unexpected R_COPY relocation in shared library",
    201 				    obj->path);
    202 				return -1;
    203 			}
    204 			rdbg(("COPY (avoid in main)"));
    205 			break;
    206 
    207 		default:
    208 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    209 			    "addend = %p, contents = %p, symbol = %s",
    210 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    211 			    (void *)rela->r_offset, (void *)rela->r_addend,
    212 			    (void *)*where,
    213 			    obj->strtab + obj->symtab[symnum].st_name));
    214 			_rtld_error("%s: Unsupported relocation type %ld "
    215 			    "in non-PLT relocations",
    216 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    217 			return -1;
    218 		}
    219 	}
    220 	return 0;
    221 }
    222 
    223 int
    224 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    225 {
    226 	Elf_Addr * const pltresolve = obj->pltgot + 8;
    227 	const Elf_Rela *rela;
    228 	int reloff;
    229 
    230 	for (rela = obj->pltrela, reloff = 0;
    231 	     rela < obj->pltrelalim;
    232 	     rela++, reloff++) {
    233 		Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    234 
    235 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    236 
    237 		if (obj->gotptr != NULL) {
    238 			/*
    239 			 * For now, simply treat then as relative.
    240 			 */
    241 			*where += (Elf_Addr)obj->relocbase;
    242 		} else {
    243 			int distance;
    244 
    245 			if (reloff < 32768) {
    246 				/* li	r11,reloff */
    247 				*where++ = 0x39600000 | reloff;
    248 			} else {
    249 				/* lis  r11,ha(reloff) */
    250 				/* addi	r11,l(reloff) */
    251 				*where++ = 0x3d600000 | ha(reloff);
    252 				*where++ = 0x396b0000 | l(reloff);
    253 			}
    254 			/* b	pltresolve */
    255 			distance = (Elf_Addr)pltresolve - (Elf_Addr)where;
    256 			*where++ = 0x48000000 | (distance & 0x03fffffc);
    257 
    258 			/*
    259 			 * Icache invalidation is not done for each entry here
    260 			 * because we sync the entire code part of the PLT once
    261 			 * in _rtld_setup_pltgot() after all the entries have been
    262 			 * initialized.
    263 			 */
    264 			/* __syncicache(where - 3, 12); */
    265 		}
    266 	}
    267 
    268 	return 0;
    269 }
    270 
    271 static int
    272 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, int reloff, Elf_Addr *tp)
    273 {
    274 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    275 	Elf_Addr value;
    276 	const Elf_Sym *def;
    277 	const Obj_Entry *defobj;
    278 	int distance;
    279 	unsigned long info = rela->r_info;
    280 
    281 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
    282 
    283 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    284 	if (__predict_false(def == NULL))
    285 		return -1;
    286 	if (__predict_false(def == &_rtld_sym_zero))
    287 		return 0;
    288 
    289 	value = (Elf_Addr)(defobj->relocbase + def->st_value);
    290 	distance = value - (Elf_Addr)where;
    291 	rdbg(("bind now/fixup in %s --> new=%p",
    292 	    defobj->strtab + def->st_name, (void *)value));
    293 
    294 	if (obj->gotptr != NULL) {
    295 		/*
    296 		 * For Secure-PLT we simply replace the entry in GOT with the address
    297 		 * of the routine.
    298 		 */
    299 		assert(where >= (Elf_Word *)obj->pltgot);
    300 		assert(where < (Elf_Word *)obj->pltgot + (obj->pltrelalim - obj->pltrela));
    301 		*where = value;
    302 	} else if (abs(distance) < 32*1024*1024) {	/* inside 32MB? */
    303 		/* b	value	# branch directly */
    304 		*where = 0x48000000 | (distance & 0x03fffffc);
    305 		__syncicache(where, 4);
    306 	} else {
    307 		Elf_Addr *pltcall, *jmptab;
    308 		int N = obj->pltrelalim - obj->pltrela;
    309 
    310 		/* Entries beyond 8192 take twice as much space. */
    311 		if (N > 8192)
    312 			N += N-8192;
    313 
    314 		pltcall = obj->pltgot;
    315 		jmptab = pltcall + 18 + N * 2;
    316 
    317 		jmptab[reloff] = value;
    318 
    319 		if (reloff < 32768) {
    320 			/* li	r11,reloff */
    321 			*where++ = 0x39600000 | reloff;
    322 		} else {
    323 #ifdef notyet
    324 			/* lis  r11,ha(value) */
    325 			/* addi	r11,l(value) */
    326 			/* mtctr r11 */
    327 			/* bctr */
    328 			*where++ = 0x3d600000 | ha(value);
    329 			*where++ = 0x396b0000 | l(value);
    330 			*where++ = 0x7d6903a6;
    331 			*where++ = 0x4e800420;
    332 #else
    333 			/* lis  r11,ha(reloff) */
    334 			/* addi	r11,l(reloff) */
    335 			*where++ = 0x3d600000 | ha(reloff);
    336 			*where++ = 0x396b0000 | l(reloff);
    337 #endif
    338 		}
    339 		/* b	pltcall	*/
    340 		distance = (Elf_Addr)pltcall - (Elf_Addr)where;
    341 		*where++ = 0x48000000 | (distance & 0x03fffffc);
    342 		__syncicache(where - 3, 12);
    343 	}
    344 
    345 	if (tp)
    346 		*tp = value;
    347 	return 0;
    348 }
    349 
    350 caddr_t
    351 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    352 {
    353 	const Elf_Rela *rela = obj->pltrela + reloff;
    354 	Elf_Addr new_value;
    355 	int err;
    356 
    357 	new_value = 0;	/* XXX gcc */
    358 
    359 	err = _rtld_relocate_plt_object(obj, rela, reloff, &new_value);
    360 	if (err)
    361 		_rtld_die();
    362 
    363 	return (caddr_t)new_value;
    364 }
    365 
    366 int
    367 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    368 {
    369 	const Elf_Rela *rela;
    370 	int reloff;
    371 
    372 	for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
    373 		if (_rtld_relocate_plt_object(obj, rela, reloff, NULL) < 0)
    374 			return -1;
    375 	}
    376 	return 0;
    377 }
    378