Home | History | Annotate | Line # | Download | only in alpha
alpha_reloc.c revision 1.36
      1 /*	$NetBSD: alpha_reloc.c,v 1.36 2010/01/14 11:58:31 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright 1996, 1997, 1998, 1999 John D. Polstra.
     40  * All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     52  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     53  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     54  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     55  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     56  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     60  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 #ifndef lint
     65 __RCSID("$NetBSD: alpha_reloc.c,v 1.36 2010/01/14 11:58:31 skrll Exp $");
     66 #endif /* not lint */
     67 
     68 #include <sys/types.h>
     69 #include <sys/stat.h>
     70 #include <string.h>
     71 
     72 #include "rtld.h"
     73 #include "debug.h"
     74 
     75 #ifdef RTLD_DEBUG_ALPHA
     76 #define	adbg(x)		xprintf x
     77 #else
     78 #define	adbg(x)		/* nothing */
     79 #endif
     80 
     81 void _rtld_bind_start(void);
     82 void _rtld_bind_start_old(void);
     83 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     84 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     85 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
     86     const Elf_Rela *, Elf_Addr *);
     87 
     88 void
     89 _rtld_setup_pltgot(const Obj_Entry *obj)
     90 {
     91 	uint32_t word0;
     92 
     93 	/*
     94 	 * The PLTGOT on the Alpha looks like this:
     95 	 *
     96 	 *	PLT HEADER
     97 	 *	.
     98 	 *	. 32 bytes
     99 	 *	.
    100 	 *	PLT ENTRY #0
    101 	 *	.
    102 	 *	. 12 bytes
    103 	 *	.
    104 	 *	PLT ENTRY #1
    105 	 *	.
    106 	 *	. 12 bytes
    107 	 *	.
    108 	 *	etc.
    109 	 *
    110 	 * The old-format entries look like (displacements filled in
    111 	 * by the linker):
    112 	 *
    113 	 *	ldah	$28, 0($31)		# 0x279f0000
    114 	 *	lda	$28, 0($28)		# 0x239c0000
    115 	 *	br	$31, plt0		# 0xc3e00000
    116 	 *
    117 	 * The new-format entries look like:
    118 	 *
    119 	 *	br	$28, plt0		# 0xc3800000
    120 	 *					# 0x00000000
    121 	 *					# 0x00000000
    122 	 *
    123 	 * What we do is fetch the first PLT entry and check to
    124 	 * see the first word of it matches the first word of the
    125 	 * old format.  If so, we use a binding routine that can
    126 	 * handle the old format, otherwise we use a binding routine
    127 	 * that handles the new format.
    128 	 *
    129 	 * Note that this is done on a per-object basis, we can mix
    130 	 * and match shared objects build with both the old and new
    131 	 * linker.
    132 	 */
    133 	word0 = *(uint32_t *)(((char *) obj->pltgot) + 32);
    134 	if ((word0 & 0xffff0000) == 0x279f0000) {
    135 		/* Old PLT entry format. */
    136 		adbg(("ALPHA: object %p has old PLT format\n", obj));
    137 		obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start_old;
    138 		obj->pltgot[3] = (Elf_Addr) obj;
    139 	} else {
    140 		/* New PLT entry format. */
    141 		adbg(("ALPHA: object %p has new PLT format\n", obj));
    142 		obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
    143 		obj->pltgot[3] = (Elf_Addr) obj;
    144 	}
    145 
    146 	__asm volatile("imb");
    147 }
    148 
    149 /*
    150  * It is possible for the compiler to emit relocations for unaligned data.
    151  * We handle this situation with these inlines.
    152  */
    153 #define	RELOC_ALIGNED_P(x) \
    154 	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
    155 
    156 static inline Elf_Addr
    157 load_ptr(void *where)
    158 {
    159 	Elf_Addr res;
    160 
    161 	memcpy(&res, where, sizeof(res));
    162 
    163 	return (res);
    164 }
    165 
    166 static inline void
    167 store_ptr(void *where, Elf_Addr val)
    168 {
    169 
    170 	memcpy(where, &val, sizeof(val));
    171 }
    172 
    173 void
    174 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    175 {
    176 	const Elf_Rela *rela = 0, *relalim;
    177 	Elf_Addr relasz = 0;
    178 	Elf_Addr *where;
    179 
    180 	for (; dynp->d_tag != DT_NULL; dynp++) {
    181 		switch (dynp->d_tag) {
    182 		case DT_RELA:
    183 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    184 			break;
    185 		case DT_RELASZ:
    186 			relasz = dynp->d_un.d_val;
    187 			break;
    188 		}
    189 	}
    190 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
    191 	for (; rela < relalim; rela++) {
    192 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    193 		/* XXX For some reason I see a few GLOB_DAT relocs here. */
    194 		*where += (Elf_Addr)relocbase;
    195 	}
    196 }
    197 
    198 int
    199 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
    200 {
    201 	const Elf_Rela *rela;
    202 	Elf_Addr target = -1;
    203 
    204 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    205 		Elf_Addr        *where;
    206 		const Elf_Sym   *def;
    207 		const Obj_Entry *defobj;
    208 		Elf_Addr         tmp;
    209 		unsigned long	 symnum;
    210 
    211 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    212 		symnum = ELF_R_SYM(rela->r_info);
    213 
    214 		switch (ELF_R_TYPE(rela->r_info)) {
    215 		case R_TYPE(NONE):
    216 			break;
    217 
    218 		case R_TYPE(REFQUAD):
    219 		case R_TYPE(GLOB_DAT):
    220 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    221 			if (def == NULL)
    222 				return -1;
    223 			target = (Elf_Addr)(defobj->relocbase +
    224 			    def->st_value);
    225 
    226 			tmp = target + rela->r_addend;
    227 			if (__predict_true(RELOC_ALIGNED_P(where))) {
    228 				if (*where != tmp)
    229 					*where = tmp;
    230 			} else {
    231 				if (load_ptr(where) != tmp)
    232 					store_ptr(where, tmp);
    233 			}
    234 			rdbg(("REFQUAD/GLOB_DAT %s in %s --> %p in %s",
    235 			    obj->strtab + obj->symtab[symnum].st_name,
    236 			    obj->path, (void *)tmp, defobj->path));
    237 			break;
    238 
    239 		case R_TYPE(RELATIVE):
    240 			if (__predict_true(RELOC_ALIGNED_P(where)))
    241 				*where += (Elf_Addr)obj->relocbase;
    242 			else
    243 				store_ptr(where,
    244 				    load_ptr(where) + (Elf_Addr)obj->relocbase);
    245 			rdbg(("RELATIVE in %s --> %p", obj->path,
    246 			    (void *)*where));
    247 			break;
    248 
    249 		case R_TYPE(COPY):
    250 			/*
    251 			 * These are deferred until all other relocations have
    252 			 * been done.  All we do here is make sure that the
    253 			 * COPY relocation is not in a shared library.  They
    254 			 * are allowed only in executable files.
    255 			 */
    256 			if (obj->isdynamic) {
    257 				_rtld_error(
    258 			"%s: Unexpected R_COPY relocation in shared library",
    259 				    obj->path);
    260 				return -1;
    261 			}
    262 			rdbg(("COPY (avoid in main)"));
    263 			break;
    264 
    265 		default:
    266 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    267 			    "addend = %p, contents = %p, symbol = %s",
    268 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    269 			    (void *)rela->r_offset, (void *)rela->r_addend,
    270 			    (void *)load_ptr(where),
    271 			    obj->strtab + obj->symtab[symnum].st_name));
    272 			_rtld_error("%s: Unsupported relocation type %ld "
    273 			    "in non-PLT relocations",
    274 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    275 			return -1;
    276 		}
    277 	}
    278 	return 0;
    279 }
    280 
    281 int
    282 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    283 {
    284 	const Elf_Rela *rela;
    285 
    286 	if (!obj->relocbase)
    287 		return 0;
    288 
    289 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    290 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    291 
    292 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    293 
    294 		/* Just relocate the GOT slots pointing into the PLT */
    295 		*where += (Elf_Addr)obj->relocbase;
    296 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    297 	}
    298 
    299 	return 0;
    300 }
    301 
    302 static inline int
    303 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
    304     Elf_Addr *tp)
    305 {
    306 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    307 	Elf_Addr new_value;
    308 	const Elf_Sym *def;
    309 	const Obj_Entry *defobj;
    310 	Elf_Addr stubaddr;
    311 	unsigned long info = rela->r_info;
    312 
    313 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
    314 
    315 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    316 	if (__predict_false(def == NULL))
    317 		return -1;
    318 	if (__predict_false(def == &_rtld_sym_zero))
    319 		return 0;
    320 
    321 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    322 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    323 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    324 
    325 	if ((stubaddr = *where) != new_value) {
    326 		int64_t delta, idisp;
    327 		uint32_t insn[3], *stubptr;
    328 		int insncnt;
    329 		Elf_Addr pc;
    330 
    331 		/* Point this GOT entry at the target. */
    332 		*where = new_value;
    333 
    334 		/*
    335 		 * Alpha shared objects may have multiple GOTs, each
    336 		 * of which may point to this entry in the PLT.  But,
    337 		 * we only have a reference to the first GOT entry which
    338 		 * points to this PLT entry.  In order to avoid having to
    339 		 * re-bind this call every time a non-first GOT entry is
    340 		 * used, we will attempt to patch up the PLT entry to
    341 		 * reference the target, rather than the binder.
    342 		 *
    343 		 * When the PLT stub gets control, PV contains the address
    344 		 * of the PLT entry.  Each PLT entry has room for 3 insns.
    345 		 * If the displacement of the target from PV fits in a signed
    346 		 * 32-bit integer, we can simply add it to PV.  Otherwise,
    347 		 * we must load the GOT entry itself into PV.
    348 		 *
    349 		 * Note if the shared object uses the old PLT format, then
    350 		 * we cannot patch up the PLT safely, and so we skip it
    351 		 * in that case[*].
    352 		 *
    353 		 * [*] Actually, if we're not doing lazy-binding, then
    354 		 * we *can* (and do) patch up this PLT entry; the PLTGOT
    355 		 * thunk won't yet point to any binder entry point, and
    356 		 * so this test will fail as it would for the new PLT
    357 		 * entry format.
    358 		 */
    359 		if (obj->pltgot[2] == (Elf_Addr) &_rtld_bind_start_old) {
    360 			rdbg(("  old PLT format"));
    361 			goto out;
    362 		}
    363 
    364 		delta = new_value - stubaddr;
    365 		rdbg(("  stubaddr=%p, where-stubaddr=%ld, delta=%ld",
    366 		    (void *)stubaddr, (long)where - (long)stubaddr,
    367 		    (long)delta));
    368 		insncnt = 0;
    369 		if ((int32_t)delta == delta) {
    370 			/*
    371 			 * We can adjust PV with an LDA, LDAH sequence.
    372 			 *
    373 			 * First, build an LDA insn to adjust the low 16
    374 			 * bits.
    375 			 */
    376 			insn[insncnt++] = 0x08 << 26 | 27 << 21 | 27 << 16 |
    377 			    (delta & 0xffff);
    378 			rdbg(("  LDA  $27,%d($27)", (int16_t)delta));
    379 			/*
    380 			 * Adjust the delta to account for the effects of
    381 			 * the LDA, including sign-extension.
    382 			 */
    383 			delta -= (int16_t)delta;
    384 			if (delta != 0) {
    385 				/*
    386 				 * Build an LDAH instruction to adjust the
    387 				 * high 16 bits.
    388 				 */
    389 				insn[insncnt++] = 0x09 << 26 | 27 << 21 |
    390 				    27 << 16 | ((delta >> 16) & 0xffff);
    391 				rdbg(("  LDAH $27,%d($27)",
    392 				    (int16_t)(delta >> 16)));
    393 			}
    394 		} else {
    395 			int64_t dhigh;
    396 
    397 			/* We must load the GOT entry. */
    398 			delta = (Elf_Addr)where - stubaddr;
    399 
    400 			/*
    401 			 * If the GOT entry is too far away from the PLT
    402 			 * entry, then we can't patch up the PLT entry.
    403 			 * This PLT entry will have to be bound for each
    404 			 * GOT entry except for the first one.  This program
    405 			 * will still run, albeit very slowly.  It is very
    406 			 * unlikely that this case will ever happen in
    407 			 * practice.
    408 			 */
    409 			if ((int32_t)delta != delta) {
    410 				rdbg(("  PLT stub too far from GOT to relocate"));
    411 				goto out;
    412 			}
    413 			dhigh = delta - (int16_t)delta;
    414 			if (dhigh != 0) {
    415 				/*
    416 				 * Build an LDAH instruction to adjust the
    417 				 * high 16 bits.
    418 				 */
    419 				insn[insncnt++] = 0x09 << 26 | 27 << 21 |
    420 				    27 << 16 | ((dhigh >> 16) & 0xffff);
    421 				rdbg(("  LDAH $27,%d($27)",
    422 				    (int16_t)(dhigh >> 16)));
    423 			}
    424 			/* Build an LDQ to load the GOT entry. */
    425 			insn[insncnt++] = 0x29 << 26 | 27 << 21 |
    426 			    27 << 16 | (delta & 0xffff);
    427 			rdbg(("  LDQ  $27,%d($27)",
    428 			    (int16_t)delta));
    429 		}
    430 
    431 		/*
    432 		 * Now, build a JMP or BR insn to jump to the target.  If
    433 		 * the displacement fits in a sign-extended 21-bit field,
    434 		 * we can use the more efficient BR insn.  Otherwise, we
    435 		 * have to jump indirect through PV.
    436 		 */
    437 		pc = stubaddr + (4 * (insncnt + 1));
    438 		idisp = (int64_t)(new_value - pc) >> 2;
    439 		if (-0x100000 <= idisp && idisp < 0x100000) {
    440 			insn[insncnt++] = 0x30 << 26 | 31 << 21 |
    441 			    (idisp & 0x1fffff);
    442 			rdbg(("  BR   $31,%p", (void *)new_value));
    443 		} else {
    444 			insn[insncnt++] = 0x1a << 26 | 31 << 21 |
    445 			    27 << 16 | (idisp & 0x3fff);
    446 			rdbg(("  JMP  $31,($27),%d",
    447 			    (int)(idisp & 0x3fff)));
    448 		}
    449 
    450 		/*
    451 		 * Fill in the tail of the PLT entry first, for reentrancy.
    452 		 * Until we have overwritten the first insn (an unconditional
    453 		 * branch), the remaining insns have no effect.
    454 		 */
    455 		stubptr = (uint32_t *)stubaddr;
    456 		while (insncnt > 1) {
    457 			insncnt--;
    458 			stubptr[insncnt] = insn[insncnt];
    459 		}
    460 		/*
    461 		 * Commit the tail of the insn sequence to memory
    462 		 * before overwriting the first insn.
    463 		 */
    464 		__asm volatile("wmb" ::: "memory");
    465 		stubptr[0] = insn[0];
    466 		/*
    467 		 * I-stream will be sync'd when we either return from
    468 		 * the binder (lazy bind case) or when the PLTGOT thunk
    469 		 * is patched up (bind-now case).
    470 		 */
    471 	}
    472 out:
    473 	if (tp)
    474 		*tp = new_value;
    475 
    476 	return 0;
    477 }
    478 
    479 caddr_t
    480 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    481 {
    482 	const Elf_Rela *rela =
    483 	    (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
    484 	Elf_Addr result = 0; /* XXX gcc */
    485 	int err;
    486 
    487 	err = _rtld_relocate_plt_object(obj, rela, &result);
    488 	if (err)
    489 		_rtld_die();
    490 
    491 	return (caddr_t)result;
    492 }
    493 
    494 int
    495 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    496 {
    497 	const Elf_Rela *rela;
    498 
    499 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
    500 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
    501 			return -1;
    502 
    503 	return 0;
    504 }
    505