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