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