Home | History | Annotate | Line # | Download | only in alpha
alpha_reloc.c revision 1.19
      1 /*	$NetBSD: alpha_reloc.c,v 1.19 2002/09/14 23:21:13 thorpej 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/types.h>
     64 #include <sys/stat.h>
     65 #include <string.h>
     66 
     67 #include "rtld.h"
     68 #include "debug.h"
     69 
     70 #ifdef RTLD_DEBUG_ALPHA
     71 #define	adbg(x)		xprintf x
     72 #else
     73 #define	adbg(x)		/* nothing */
     74 #endif
     75 
     76 void _rtld_bind_start(void);
     77 void _rtld_bind_start_old(void);
     78 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     79 
     80 void
     81 _rtld_setup_pltgot(const Obj_Entry *obj)
     82 {
     83 	uint32_t word0;
     84 
     85 	/*
     86 	 * The PLTGOT on the Alpha looks like this:
     87 	 *
     88 	 *	PLT HEADER
     89 	 *	.
     90 	 *	. 32 bytes
     91 	 *	.
     92 	 *	PLT ENTRY #0
     93 	 *	.
     94 	 *	. 12 bytes
     95 	 *	.
     96 	 *	PLT ENTRY #1
     97 	 *	.
     98 	 *	. 12 bytes
     99 	 *	.
    100 	 *	etc.
    101 	 *
    102 	 * The old-format entries look like (displacements filled in
    103 	 * by the linker):
    104 	 *
    105 	 *	ldah	$28, 0($31)		# 0x279f0000
    106 	 *	lda	$28, 0($28)		# 0x239c0000
    107 	 *	br	$31, plt0		# 0xc3e00000
    108 	 *
    109 	 * The new-format entries look like:
    110 	 *
    111 	 *	br	$28, plt0		# 0xc3800000
    112 	 *					# 0x00000000
    113 	 *					# 0x00000000
    114 	 *
    115 	 * What we do is fetch the first PLT entry and check to
    116 	 * see the first word of it matches the first word of the
    117 	 * old format.  If so, we use a binding routine that can
    118 	 * handle the old format, otherwise we use a binding routine
    119 	 * that handles the new format.
    120 	 *
    121 	 * Note that this is done on a per-object basis, we can mix
    122 	 * and match shared objects build with both the old and new
    123 	 * linker.
    124 	 */
    125 	word0 = *(uint32_t *)(((char *) obj->pltgot) + 32);
    126 	if ((word0 & 0xffff0000) == 0x279f0000) {
    127 		/* Old PLT entry format. */
    128 		adbg(("ALPHA: object %p has old PLT format\n", obj));
    129 		obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start_old;
    130 		obj->pltgot[3] = (Elf_Addr) obj;
    131 	} else {
    132 		/* New PLT entry format. */
    133 		adbg(("ALPHA: object %p has new PLT format\n", obj));
    134 		obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
    135 		obj->pltgot[3] = (Elf_Addr) obj;
    136 	}
    137 
    138 	__asm __volatile("imb");
    139 }
    140 
    141 /*
    142  * It is possible for the compiler to emit relocations for unaligned data.
    143  * We handle this situation with these inlines.
    144  */
    145 #define	RELOC_ALIGNED_P(x) \
    146 	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
    147 
    148 static __inline Elf_Addr
    149 load_ptr(void *where)
    150 {
    151 	Elf_Addr res;
    152 
    153 	memcpy(&res, where, sizeof(res));
    154 
    155 	return (res);
    156 }
    157 
    158 static __inline void
    159 store_ptr(void *where, Elf_Addr val)
    160 {
    161 
    162 	memcpy(where, &val, sizeof(val));
    163 }
    164 
    165 void
    166 _rtld_relocate_nonplt_self(dynp, relocbase)
    167 	Elf_Dyn *dynp;
    168 	Elf_Addr relocbase;
    169 {
    170 	const Elf_Rela *rela = 0, *relalim;
    171 	Elf_Addr relasz = 0;
    172 	Elf_Addr *where;
    173 
    174 	for (; dynp->d_tag != DT_NULL; dynp++) {
    175 		switch (dynp->d_tag) {
    176 		case DT_RELA:
    177 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    178 			break;
    179 		case DT_RELASZ:
    180 			relasz = dynp->d_un.d_val;
    181 			break;
    182 		}
    183 	}
    184 	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
    185 	for (; rela < relalim; rela++) {
    186 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    187 		/* XXX For some reason I see a few GLOB_DAT relocs here. */
    188 		*where += (Elf_Addr)relocbase;
    189 	}
    190 }
    191 
    192 int
    193 _rtld_relocate_nonplt_objects(obj, self)
    194 	const Obj_Entry *obj;
    195 	bool self;
    196 {
    197 	const Elf_Rela *rela;
    198 
    199 	if (self)
    200 		return 0;
    201 
    202 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    203 		Elf_Addr        *where;
    204 		const Elf_Sym   *def;
    205 		const Obj_Entry *defobj;
    206 		Elf_Addr         tmp;
    207 		unsigned long	 symnum;
    208 
    209 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    210 		symnum = ELF_R_SYM(rela->r_info);
    211 
    212 		switch (ELF_R_TYPE(rela->r_info)) {
    213 		case R_TYPE(NONE):
    214 			break;
    215 
    216 		case R_TYPE(REFQUAD):
    217 		case R_TYPE(GLOB_DAT):
    218 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    219 			if (def == NULL)
    220 				return -1;
    221 
    222 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
    223 			    rela->r_addend;
    224 			if (__predict_true(RELOC_ALIGNED_P(where))) {
    225 				if (*where != tmp)
    226 					*where = tmp;
    227 			} else {
    228 				if (load_ptr(where) != tmp)
    229 					store_ptr(where, tmp);
    230 			}
    231 			rdbg(("REFQUAD/GLOB_DAT %s in %s --> %p in %s",
    232 			    obj->strtab + obj->symtab[symnum].st_name,
    233 			    obj->path, (void *)tmp, defobj->path));
    234 			break;
    235 
    236 		case R_TYPE(RELATIVE):
    237 			if (__predict_true(RELOC_ALIGNED_P(where)))
    238 				*where += (Elf_Addr)obj->relocbase;
    239 			else
    240 				store_ptr(where,
    241 				    load_ptr(where) + (Elf_Addr)obj->relocbase);
    242 			rdbg(("RELATIVE in %s --> %p", obj->path,
    243 			    (void *)*where));
    244 			break;
    245 
    246 		case R_TYPE(COPY):
    247 			/*
    248 			 * These are deferred until all other relocations have
    249 			 * been done.  All we do here is make sure that the
    250 			 * COPY relocation is not in a shared library.  They
    251 			 * are allowed only in executable files.
    252 			 */
    253 			if (obj->isdynamic) {
    254 				_rtld_error(
    255 			"%s: Unexpected R_COPY relocation in shared library",
    256 				    obj->path);
    257 				return -1;
    258 			}
    259 			rdbg(("COPY (avoid in main)"));
    260 			break;
    261 
    262 		default:
    263 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    264 			    "addend = %p, contents = %p, symbol = %s",
    265 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    266 			    (void *)rela->r_offset, (void *)rela->r_addend,
    267 			    (void *)load_ptr(where),
    268 			    obj->strtab + obj->symtab[symnum].st_name));
    269 			_rtld_error("%s: Unsupported relocation type %ld "
    270 			    "in non-PLT relocations\n",
    271 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    272 			return -1;
    273 		}
    274 	}
    275 	return 0;
    276 }
    277 
    278 int
    279 _rtld_relocate_plt_lazy(obj)
    280 	const Obj_Entry *obj;
    281 {
    282 	const Elf_Rela *rela;
    283 
    284 	if (!obj->isdynamic)
    285 		return 0;
    286 
    287 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    288 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    289 
    290 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    291 
    292 		/* Just relocate the GOT slots pointing into the PLT */
    293 		*where += (Elf_Addr)obj->relocbase;
    294 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    295 	}
    296 
    297 	return 0;
    298 }
    299 
    300 int
    301 _rtld_relocate_plt_object(obj, rela, addrp)
    302 	const Obj_Entry *obj;
    303 	const Elf_Rela *rela;
    304 	caddr_t *addrp;
    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 
    312 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    313 
    314 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    315 	if (def == NULL)
    316 		return -1;
    317 
    318 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    319 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    320 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    321 
    322 	if ((stubaddr = *where) != new_value) {
    323 		int64_t delta, idisp;
    324 		uint32_t insn[3], *stubptr;
    325 		int insncnt;
    326 		Elf_Addr pc;
    327 
    328 		/* Point this GOT entry at the target. */
    329 		*where = new_value;
    330 
    331 		/*
    332 		 * Alpha shared objects may have multiple GOTs, each
    333 		 * of which may point to this entry in the PLT.  But,
    334 		 * we only have a reference to the first GOT entry which
    335 		 * points to this PLT entry.  In order to avoid having to
    336 		 * re-bind this call every time a non-first GOT entry is
    337 		 * used, we will attempt to patch up the PLT entry to
    338 		 * reference the target, rather than the binder.
    339 		 *
    340 		 * When the PLT stub gets control, PV contains the address
    341 		 * of the PLT entry.  Each PLT entry has room for 3 insns.
    342 		 * If the displacement of the target from PV fits in a signed
    343 		 * 32-bit integer, we can simply add it to PV.  Otherwise,
    344 		 * we must load the GOT entry itself into PV.
    345 		 *
    346 		 * Note if the shared object uses the old PLT format, then
    347 		 * we cannot patch up the PLT safely, and so we skip it
    348 		 * in that case[*].
    349 		 *
    350 		 * [*] Actually, if we're not doing lazy-binding, then
    351 		 * we *can* (and do) patch up this PLT entry; the PLTGOT
    352 		 * thunk won't yet point to any binder entry point, and
    353 		 * so this test will fail as it would for the new PLT
    354 		 * entry format.
    355 		 */
    356 		if (obj->pltgot[2] == (Elf_Addr) &_rtld_bind_start_old) {
    357 			rdbg(("  old PLT format"));
    358 			goto out;
    359 		}
    360 
    361 		delta = new_value - stubaddr;
    362 		rdbg(("  stubaddr=%p, where-stubaddr=%ld, delta=%ld",
    363 		    (void *)stubaddr, (long)where - (long)stubaddr,
    364 		    (long)delta));
    365 		insncnt = 0;
    366 		if ((int32_t)delta == delta) {
    367 			/*
    368 			 * We can adjust PV with an LDA, LDAH sequence.
    369 			 *
    370 			 * First, build an LDA insn to adjust the low 16
    371 			 * bits.
    372 			 */
    373 			insn[insncnt++] = 0x08 << 26 | 27 << 21 | 27 << 16 |
    374 			    (delta & 0xffff);
    375 			rdbg(("  LDA  $27,%d($27)", (int16_t)delta));
    376 			/*
    377 			 * Adjust the delta to account for the effects of
    378 			 * the LDA, including sign-extension.
    379 			 */
    380 			delta -= (int16_t)delta;
    381 			if (delta != 0) {
    382 				/*
    383 				 * Build an LDAH instruction to adjust the
    384 				 * high 16 bits.
    385 				 */
    386 				insn[insncnt++] = 0x09 << 26 | 27 << 21 |
    387 				    27 << 16 | ((delta >> 16) & 0xffff);
    388 				rdbg(("  LDAH $27,%d($27)",
    389 				    (int16_t)(delta >> 16)));
    390 			}
    391 		} else {
    392 			int64_t dhigh;
    393 
    394 			/* We must load the GOT entry. */
    395 			delta = (Elf_Addr)where - stubaddr;
    396 
    397 			/*
    398 			 * If the GOT entry is too far away from the PLT
    399 			 * entry, then we can't patch up the PLT entry.
    400 			 * This PLT entry will have to be bound for each
    401 			 * GOT entry except for the first one.  This program
    402 			 * will still run, albeit very slowly.  It is very
    403 			 * unlikely that this case will ever happen in
    404 			 * practice.
    405 			 */
    406 			if ((int32_t)delta != delta) {
    407 				rdbg(("  PLT stub too far from GOT to relocate"));
    408 				goto out;
    409 			}
    410 			dhigh = delta - (int16_t)delta;
    411 			if (dhigh != 0) {
    412 				/*
    413 				 * Build an LDAH instruction to adjust the
    414 				 * high 16 bits.
    415 				 */
    416 				insn[insncnt++] = 0x09 << 26 | 27 << 21 |
    417 				    27 << 16 | ((dhigh >> 16) & 0xffff);
    418 				rdbg(("  LDAH $27,%d($27)",
    419 				    (int16_t)(dhigh >> 16)));
    420 			}
    421 			/* Build an LDQ to load the GOT entry. */
    422 			insn[insncnt++] = 0x29 << 26 | 27 << 21 |
    423 			    27 << 16 | (delta & 0xffff);
    424 			rdbg(("  LDQ  $27,%d($27)",
    425 			    (int16_t)delta));
    426 		}
    427 
    428 		/*
    429 		 * Now, build a JMP or BR insn to jump to the target.  If
    430 		 * the displacement fits in a sign-extended 21-bit field,
    431 		 * we can use the more efficient BR insn.  Otherwise, we
    432 		 * have to jump indirect through PV.
    433 		 */
    434 		pc = stubaddr + (4 * (insncnt + 1));
    435 		idisp = (int64_t)(new_value - pc) >> 2;
    436 		if (-0x100000 <= idisp && idisp < 0x100000) {
    437 			insn[insncnt++] = 0x30 << 26 | 31 << 21 |
    438 			    (idisp & 0x1fffff);
    439 			rdbg(("  BR   $31,%p", (void *)new_value));
    440 		} else {
    441 			insn[insncnt++] = 0x1a << 26 | 31 << 21 |
    442 			    27 << 16 | (idisp & 0x3fff);
    443 			rdbg(("  JMP  $31,($27),%d",
    444 			    (int)(idisp & 0x3fff)));
    445 		}
    446 
    447 		/*
    448 		 * Fill in the tail of the PLT entry first, for reentrancy.
    449 		 * Until we have overwritten the first insn (an unconditional
    450 		 * branch), the remaining insns have no effect.
    451 		 */
    452 		stubptr = (uint32_t *)stubaddr;
    453 		while (insncnt > 1) {
    454 			insncnt--;
    455 			stubptr[insncnt] = insn[insncnt];
    456 		}
    457 		/*
    458 		 * Commit the tail of the insn sequence to memory
    459 		 * before overwriting the first insn.
    460 		 */
    461 		__asm __volatile("wmb" ::: "memory");
    462 		stubptr[0] = insn[0];
    463 		/*
    464 		 * I-stream will be sync'd when we either return from
    465 		 * the binder (lazy bind case) or when the PLTGOT thunk
    466 		 * is patched up (bind-now case).
    467 		 */
    468 	}
    469 
    470  out:
    471 	*addrp = (caddr_t)new_value;
    472 	return 0;
    473 }
    474