Home | History | Annotate | Line # | Download | only in sparc64
      1 /*	$NetBSD: mdreloc.c,v 1.72 2024/08/03 21:59:58 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Eduardo Horvath.
      5  * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Paul Kranenburg and by Charles M. Hannum.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * SPARC ELF relocations.
     35  *
     36  * Reference:
     37  *
     38  *	SPARC Compliance Definition 2.4.1
     39  *	http://sparc.org/wp-content/uploads/2014/01/SCD.2.4.1.pdf.gz
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 #ifndef lint
     44 __RCSID("$NetBSD: mdreloc.c,v 1.72 2024/08/03 21:59:58 riastradh Exp $");
     45 #endif /* not lint */
     46 
     47 #include <machine/elf_support.h>
     48 
     49 #include <errno.h>
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <unistd.h>
     54 
     55 #include "rtldenv.h"
     56 #include "debug.h"
     57 #include "rtld.h"
     58 
     59 /*
     60  * The following table holds for each relocation type:
     61  *	- the width in bits of the memory location the relocation
     62  *	  applies to (not currently used)
     63  *	- the number of bits the relocation value must be shifted to the
     64  *	  right (i.e. discard least significant bits) to fit into
     65  *	  the appropriate field in the instruction word.
     66  *	- flags indicating whether
     67  *		* the relocation involves a symbol
     68  *		* the relocation is relative to the current position
     69  *		* the relocation is for a GOT entry
     70  *		* the relocation is relative to the load address
     71  *
     72  */
     73 #define _RF_S		0x80000000		/* Resolve symbol */
     74 #define _RF_A		0x40000000		/* Use addend */
     75 #define _RF_P		0x20000000		/* Location relative */
     76 #define _RF_G		0x10000000		/* GOT offset */
     77 #define _RF_B		0x08000000		/* Load address relative */
     78 #define _RF_U		0x04000000		/* Unaligned */
     79 #define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
     80 #define _RF_RS(s)	( (s) & 0xff)		/* right shift */
     81 static const int reloc_target_flags[R_TYPE(TLS_TPOFF64)+1] = {
     82 	0,							/* NONE */
     83 	_RF_S|_RF_A|		_RF_SZ(8)  | _RF_RS(0),		/* RELOC_8 */
     84 	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* RELOC_16 */
     85 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELOC_32 */
     86 	_RF_S|_RF_A|_RF_P|	_RF_SZ(8)  | _RF_RS(0),		/* DISP_8 */
     87 	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* DISP_16 */
     88 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* DISP_32 */
     89 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_30 */
     90 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_22 */
     91 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HI22 */
     92 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 22 */
     93 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 13 */
     94 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LO10 */
     95 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT10 */
     96 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT13 */
     97 	_RF_G|			_RF_SZ(32) | _RF_RS(10),	/* GOT22 */
     98 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC10 */
     99 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC22 */
    100 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WPLT30 */
    101 				_RF_SZ(32) | _RF_RS(0),		/* COPY */
    102 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* GLOB_DAT */
    103 				_RF_SZ(32) | _RF_RS(0),		/* JMP_SLOT */
    104 	      _RF_A|	_RF_B|	_RF_SZ(64) | _RF_RS(0),		/* RELATIVE */
    105 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(32) | _RF_RS(0),		/* UA_32 */
    106 
    107 	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
    108 	      _RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIPLT22 */
    109 	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
    110 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT32 */
    111 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PCPLT22 */
    112 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT10 */
    113 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 10 */
    114 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 11 */
    115 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* 64 */
    116 	_RF_S|_RF_A|/*extra*/	_RF_SZ(32) | _RF_RS(0),		/* OLO10 */
    117 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(42),	/* HH22 */
    118 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(32),	/* HM10 */
    119 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* LM22 */
    120 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(42),	/* PC_HH22 */
    121 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(32),	/* PC_HM10 */
    122 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC_LM22 */
    123 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP16 */
    124 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP19 */
    125 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_JMP */
    126 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 7 */
    127 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 5 */
    128 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 6 */
    129 	_RF_S|_RF_A|_RF_P|	_RF_SZ(64) | _RF_RS(0),		/* DISP64 */
    130 	      _RF_A|		_RF_SZ(64) | _RF_RS(0),		/* PLT64 */
    131 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIX22 */
    132 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOX10 */
    133 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(22),	/* H44 */
    134 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(12),	/* M44 */
    135 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* L44 */
    136 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* REGISTER */
    137 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(64) | _RF_RS(0),		/* UA64 */
    138 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(16) | _RF_RS(0),		/* UA16 */
    139 /* TLS relocs not represented here! */
    140 };
    141 
    142 #ifdef RTLD_DEBUG_RELOC
    143 static const char *reloc_names[] = {
    144 	"NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
    145 	"DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
    146 	"22", "13", "LO10", "GOT10", "GOT13",
    147 	"GOT22", "PC10", "PC22", "WPLT30", "COPY",
    148 	"GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32",
    149 	"HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
    150 	"10", "11", "64", "OLO10", "HH22",
    151 	"HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22",
    152 	"WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6",
    153 	"DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44",
    154 	"L44", "REGISTER", "UA64", "UA16",
    155 	"TLS_GD_HI22", "TLS_GD_LO10", "TLS_GD_ADD", "TLS_GD_CALL",
    156 	"TLS_LDM_HI22", "TLS_LDM_LO10", "TLS_LDM_ADD", "TLS_LDM_CALL",
    157 	"TLS_LDO_HIX22", "TLS_LDO_LOX10", "TLS_LDO_ADD", "TLS_IE_HI22",
    158 	"TLS_IE_LO10", "TLS_IE_LD", "TLS_IE_LDX", "TLS_IE_ADD", "TLS_LE_HIX22",
    159 	"TLS_LE_LOX10", "TLS_DTPMOD32", "TLS_DTPMOD64", "TLS_DTPOFF32",
    160 	"TLS_DTPOFF64", "TLS_TPOFF32", "TLS_TPOFF64",
    161 };
    162 #endif
    163 
    164 #define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
    165 #define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
    166 #define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
    167 #define RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
    168 #define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
    169 #define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
    170 #define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
    171 #define RELOC_TLS(t)			(t >= R_TYPE(TLS_GD_HI22))
    172 
    173 static const long reloc_target_bitmask[] = {
    174 #define _BM(x)	(~(-(1ULL << (x))))
    175 	0,				/* NONE */
    176 	_BM(8), _BM(16), _BM(32),	/* RELOC_8, _16, _32 */
    177 	_BM(8), _BM(16), _BM(32),	/* DISP8, DISP16, DISP32 */
    178 	_BM(30), _BM(22),		/* WDISP30, WDISP22 */
    179 	_BM(22), _BM(22),		/* HI22, _22 */
    180 	_BM(13), _BM(10),		/* RELOC_13, _LO10 */
    181 	_BM(10), _BM(13), _BM(22),	/* GOT10, GOT13, GOT22 */
    182 	_BM(10), _BM(22),		/* _PC10, _PC22 */
    183 	_BM(30), 0,			/* _WPLT30, _COPY */
    184 	-1, _BM(32), -1,		/* _GLOB_DAT, JMP_SLOT, _RELATIVE */
    185 	_BM(32), _BM(32),		/* _UA32, PLT32 */
    186 	_BM(22), _BM(10),		/* _HIPLT22, LOPLT10 */
    187 	_BM(32), _BM(22), _BM(10),	/* _PCPLT32, _PCPLT22, _PCPLT10 */
    188 	_BM(10), _BM(11), -1,		/* _10, _11, _64 */
    189 	_BM(13), _BM(22),		/* _OLO10, _HH22 */
    190 	_BM(10), _BM(22),		/* _HM10, _LM22 */
    191 	_BM(22), _BM(10), _BM(22),	/* _PC_HH22, _PC_HM10, _PC_LM22 */
    192 	_BM(16), _BM(19),		/* _WDISP16, _WDISP19 */
    193 	-1,				/* GLOB_JMP */
    194 	_BM(7), _BM(5), _BM(6),		/* _7, _5, _6 */
    195 	-1, -1,				/* DISP64, PLT64 */
    196 	_BM(22), _BM(13),		/* HIX22, LOX10 */
    197 	_BM(22), _BM(10), _BM(12),	/* H44, M44, L44 */
    198 	-1, -1, _BM(16),		/* REGISTER, UA64, UA16 */
    199 #undef _BM
    200 };
    201 #define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
    202 
    203 /*
    204  * Instruction templates:
    205  */
    206 
    207 
    208 /* %hi(v)/%lo(v) with variable shift */
    209 #define	HIVAL(v, s)	(((v) >> (s)) & 0x003fffff)
    210 #define LOVAL(v, s)	(((v) >> (s)) & 0x000003ff)
    211 
    212 void _rtld_bind_start_0(long, long);
    213 void _rtld_bind_start_1(long, long);
    214 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
    215 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
    216 
    217 /*
    218  * Install rtld function call into this PLT slot.
    219  */
    220 #define	SAVE		0x9de3bf50	/* i.e. `save %sp,-176,%sp' */
    221 #define	SETHI_l0	0x21000000
    222 #define	SETHI_l1	0x23000000
    223 #define	OR_l0_l0	0xa0142000
    224 #define	SLLX_l0_32_l0	0xa12c3020
    225 #define	OR_l0_l1_l0	0xa0140011
    226 #define	JMPL_l0_o0	0x91c42000
    227 #define	MOV_g1_o1	0x92100001
    228 
    229 void _rtld_install_plt(Elf_Word *, Elf_Addr);
    230 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
    231     const Elf_Rela *, Elf_Addr *);
    232 
    233 void
    234 _rtld_install_plt(Elf_Word *pltgot, Elf_Addr proc)
    235 {
    236 	pltgot[0] = SAVE;
    237 	pltgot[1] = SETHI_l0  | HIVAL(proc, 42);
    238 	pltgot[2] = SETHI_l1  | HIVAL(proc, 10);
    239 	pltgot[3] = OR_l0_l0  | LOVAL(proc, 32);
    240 	pltgot[4] = SLLX_l0_32_l0;
    241 	pltgot[5] = OR_l0_l1_l0;
    242 	pltgot[6] = JMPL_l0_o0 | LOVAL(proc, 0);
    243 	pltgot[7] = MOV_g1_o1;
    244 }
    245 
    246 void
    247 _rtld_setup_pltgot(const Obj_Entry *obj)
    248 {
    249 	/*
    250 	 * On sparc64 we got troubles.
    251 	 *
    252 	 * Instructions are 4 bytes long.
    253 	 * Elf[64]_Addr is 8 bytes long, so are our pltglot[]
    254 	 * array entries.
    255 	 * Each PLT entry jumps to PLT0 to enter the dynamic
    256 	 * linker.
    257 	 * Loading an arbitrary 64-bit pointer takes 6
    258 	 * instructions and 2 registers.
    259 	 *
    260 	 * Somehow we need to issue a save to get a new stack
    261 	 * frame, load the address of the dynamic linker, and
    262 	 * jump there, in 8 instructions or less.
    263 	 *
    264 	 * Oh, we need to fill out both PLT0 and PLT1.
    265 	 */
    266 	{
    267 		Elf_Word *entry = (Elf_Word *)obj->pltgot;
    268 
    269 		/* Install in entries 0 and 1 */
    270 		_rtld_install_plt(&entry[0], (Elf_Addr) &_rtld_bind_start_0);
    271 		_rtld_install_plt(&entry[8], (Elf_Addr) &_rtld_bind_start_1);
    272 
    273 		/*
    274 		 * Install the object reference in first slot
    275 		 * of entry 2.
    276 		 */
    277 		obj->pltgot[8] = (Elf_Addr) obj;
    278 	}
    279 }
    280 
    281 void
    282 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    283 {
    284 	const Elf_Rela *rela = 0, *relalim;
    285 	Elf_Addr relasz = 0;
    286 	Elf_Addr *where;
    287 
    288 	for (; dynp->d_tag != DT_NULL; dynp++) {
    289 		switch (dynp->d_tag) {
    290 		case DT_RELA:
    291 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    292 			break;
    293 		case DT_RELASZ:
    294 			relasz = dynp->d_un.d_val;
    295 			break;
    296 		}
    297 	}
    298 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
    299 	for (; rela < relalim; rela++) {
    300 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    301 		*where = (Elf_Addr)(relocbase + rela->r_addend);
    302 	}
    303 }
    304 
    305 int
    306 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
    307 {
    308 	const Elf_Rela *rela;
    309 	const Elf_Sym *def = NULL;
    310 	const Obj_Entry *defobj = NULL;
    311 	unsigned long last_symnum = ULONG_MAX;
    312 
    313 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    314 		Elf_Addr *where;
    315 		Elf_Word type;
    316 		Elf_Addr value = 0, mask;
    317 		unsigned long symnum;
    318 
    319 		where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
    320 
    321 		type = ELF_R_TYPE(rela->r_info);
    322 		if (type == R_TYPE(NONE))
    323 			continue;
    324 
    325 		/* OLO10 relocations have extra info */
    326 		if ((type & 0x00ff) == R_SPARC_OLO10)
    327 			type = R_SPARC_OLO10;
    328 
    329 		/* We do JMP_SLOTs in _rtld_bind() below */
    330 		if (type == R_TYPE(JMP_SLOT))
    331 			continue;
    332 
    333 		/* IFUNC relocations are handled in _rtld_call_ifunc */
    334 		if (type == R_TYPE(IRELATIVE)) {
    335 			if (obj->ifunc_remaining_nonplt == 0) {
    336 				obj->ifunc_remaining_nonplt =
    337 				    obj->relalim - rela;
    338 			}
    339 			continue;
    340 		}
    341 
    342 		/* COPY relocs are also handled elsewhere */
    343 		if (type == R_TYPE(COPY))
    344 			continue;
    345 
    346 		/*
    347 		 * We use the fact that relocation types are an `enum'
    348 		 * Note: R_SPARC_TLS_TPOFF64 is currently numerically largest.
    349 		 */
    350 		if (type > R_TYPE(TLS_TPOFF64)) {
    351 			dbg(("unknown relocation type %x at %p", type, rela));
    352 			return -1;
    353 		}
    354 
    355 		value = rela->r_addend;
    356 
    357 		if (RELOC_RESOLVE_SYMBOL(type) || RELOC_TLS(type)) {
    358 			symnum = ELF_R_SYM(rela->r_info);
    359 			if (last_symnum != symnum) {
    360 				last_symnum = symnum;
    361 				def = _rtld_find_symdef(symnum, obj, &defobj,
    362 				    false);
    363 				if (def == NULL)
    364 					return -1;
    365 			}
    366 		}
    367 
    368 		/*
    369 		 * Handle TLS relocations here, they are different.
    370 		 */
    371 		if (RELOC_TLS(type)) {
    372 			switch (type) {
    373 			case R_TYPE(TLS_DTPMOD64):
    374 				*where = (Elf64_Addr)defobj->tlsindex;
    375 
    376 				rdbg(("TLS_DTPMOD64 %s in %s --> %p",
    377 				    obj->strtab +
    378 				    obj->symtab[symnum].st_name,
    379 				    obj->path, (void *)*where));
    380 
    381 				break;
    382 
    383 			case R_TYPE(TLS_DTPOFF64):
    384 				*where = (Elf64_Addr)(def->st_value
    385 				    + rela->r_addend);
    386 
    387 				rdbg(("DTPOFF64 %s in %s --> %p",
    388 				    obj->strtab +
    389 				        obj->symtab[symnum].st_name,
    390 				    obj->path, (void *)*where));
    391 
    392 				break;
    393 
    394 			case R_TYPE(TLS_TPOFF64):
    395 				if (!defobj->tls_static &&
    396 				    _rtld_tls_offset_allocate(__UNCONST(defobj)))
    397 					return -1;
    398 
    399 				*where = (Elf64_Addr)(def->st_value -
    400 				    defobj->tlsoffset + rela->r_addend);
    401 
    402 				rdbg(("TLS_TPOFF64 %s in %s --> %p",
    403 				    obj->strtab + obj->symtab[symnum].st_name,
    404 				    obj->path, (void *)*where));
    405 
    406 				break;
    407 			}
    408 			continue;
    409 		}
    410 
    411 		/*
    412 		 * Handle relative relocs here, as an optimization.
    413 		 */
    414 		if (type == R_TYPE(RELATIVE)) {
    415 			*where = (Elf_Addr)(obj->relocbase + value);
    416 			rdbg(("RELATIVE in %s --> %p", obj->path,
    417 			    (void *)*where));
    418 			continue;
    419 		}
    420 
    421 		if (RELOC_RESOLVE_SYMBOL(type)) {
    422 			/* Add in the symbol's absolute address */
    423 			value += (Elf_Addr)(defobj->relocbase + def->st_value);
    424 		}
    425 
    426 		if (type == R_SPARC_OLO10) {
    427 			value = (value & 0x3ff)
    428 			    + (((Elf64_Xword)rela->r_info<<32)>>40);
    429 		}
    430 
    431 		if (RELOC_PC_RELATIVE(type)) {
    432 			value -= (Elf_Addr)where;
    433 		}
    434 
    435 		if (RELOC_BASE_RELATIVE(type)) {
    436 			/*
    437 			 * Note that even though sparcs use `Elf_rela'
    438 			 * exclusively we still need the implicit memory addend
    439 			 * in relocations referring to GOT entries.
    440 			 * Undoubtedly, someone f*cked this up in the distant
    441 			 * past, and now we're stuck with it in the name of
    442 			 * compatibility for all eternity..
    443 			 *
    444 			 * In any case, the implicit and explicit should be
    445 			 * mutually exclusive. We provide a check for that
    446 			 * here.
    447 			 */
    448 #ifdef DIAGNOSTIC
    449 			if (value != 0 && *where != 0) {
    450 				xprintf("BASE_REL(%s): where=%p, *where 0x%lx, "
    451 					"addend=0x%lx, base %p\n",
    452 					obj->path, where, *where,
    453 					rela->r_addend, obj->relocbase);
    454 			}
    455 #endif
    456 			/* XXXX -- apparently we ignore the preexisting value */
    457 			value += (Elf_Addr)(obj->relocbase);
    458 		}
    459 
    460 		mask = RELOC_VALUE_BITMASK(type);
    461 		value >>= RELOC_VALUE_RIGHTSHIFT(type);
    462 		value &= mask;
    463 
    464 		if (RELOC_UNALIGNED(type)) {
    465 			/* Handle unaligned relocations. */
    466 			Elf_Addr tmp = 0;
    467 			char *ptr = (char *)where;
    468 			int i, size = RELOC_TARGET_SIZE(type)/8;
    469 
    470 			/* Read it in one byte at a time. */
    471 			for (i=0; i<size; i++)
    472 				tmp = (tmp << 8) | ptr[i];
    473 
    474 			tmp &= ~mask;
    475 			tmp |= value;
    476 
    477 			/* Write it back out. */
    478 			for (i=0; i<size; i++)
    479 				ptr[i] = ((tmp >> (8*i)) & 0xff);
    480 #ifdef RTLD_DEBUG_RELOC
    481 			value = (Elf_Addr)tmp;
    482 #endif
    483 
    484 		} else if (RELOC_TARGET_SIZE(type) > 32) {
    485 			*where &= ~mask;
    486 			*where |= value;
    487 #ifdef RTLD_DEBUG_RELOC
    488 			value = (Elf_Addr)*where;
    489 #endif
    490 		} else {
    491 			Elf32_Addr *where32 = (Elf32_Addr *)where;
    492 
    493 			*where32 &= ~mask;
    494 			*where32 |= value;
    495 #ifdef RTLD_DEBUG_RELOC
    496 			value = (Elf_Addr)*where32;
    497 #endif
    498 		}
    499 
    500 #ifdef RTLD_DEBUG_RELOC
    501 		if (RELOC_RESOLVE_SYMBOL(type)) {
    502 			rdbg(("%s %s in %s --> %p in %s", reloc_names[type],
    503 			    obj->strtab + obj->symtab[symnum].st_name,
    504 			    obj->path, (void *)value, defobj->path));
    505 		} else {
    506 			rdbg(("%s in %s --> %p", reloc_names[type],
    507 			    obj->path, (void *)value));
    508 		}
    509 #endif
    510 	}
    511 	return (0);
    512 }
    513 
    514 int
    515 _rtld_relocate_plt_lazy(Obj_Entry *obj)
    516 {
    517 	const Elf_Rela *rela;
    518 
    519 	for (rela = obj->pltrelalim; rela-- > obj->pltrela; ) {
    520 		if (ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_IREL))
    521 			obj->ifunc_remaining = obj->pltrelalim - rela + 1;
    522 	}
    523 
    524 	return 0;
    525 }
    526 
    527 caddr_t
    528 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    529 {
    530 	const Elf_Rela *rela = obj->pltrela + reloff;
    531 	Elf_Addr result;
    532 	int err;
    533 
    534 	result = 0;	/* XXX gcc */
    535 
    536 	if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT) ||
    537 	    ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_IREL)) {
    538 		/*
    539 		 * XXXX
    540 		 *
    541 		 * The first four PLT entries are reserved.  There is some
    542 		 * disagreement whether they should have associated relocation
    543 		 * entries.  Both the SPARC 32-bit and 64-bit ELF
    544 		 * specifications say that they should have relocation entries,
    545 		 * but the 32-bit SPARC binutils do not generate them, and now
    546 		 * the 64-bit SPARC binutils have stopped generating them too.
    547 		 *
    548 		 * So, to provide binary compatibility, we will check the first
    549 		 * entry, if it is reserved it should not be of the type
    550 		 * JMP_SLOT or JMP_REL.  If it is either of those, then
    551 		 * the 4 reserved entries were not generated and our index
    552 		 * is 4 entries too far.
    553 		 */
    554 		rela -= 4;
    555 	}
    556 
    557 	_rtld_shared_enter();
    558 	err = _rtld_relocate_plt_object(obj, rela, &result);
    559 	if (err)
    560 		_rtld_die();
    561 	_rtld_shared_exit();
    562 
    563 	return (caddr_t)result;
    564 }
    565 
    566 int
    567 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    568 {
    569 	const Elf_Rela *rela;
    570 
    571 	rela = obj->pltrela;
    572 
    573 	/*
    574 	 * Check for first four reserved entries - and skip them.
    575 	 * See above for details.
    576 	 */
    577 	if (ELF_R_TYPE(obj->pltrela->r_info) != R_TYPE(JMP_SLOT) &&
    578 	    ELF_R_TYPE(obj->pltrela->r_info) != R_TYPE(JMP_IREL))
    579 		rela += 4;
    580 
    581 	for (; rela < obj->pltrelalim; rela++)
    582 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
    583 			return -1;
    584 
    585 	return 0;
    586 }
    587 
    588 static inline void
    589 _rtld_write_plt(Elf_Word *where, Elf_Addr value, const Elf_Rela *rela,
    590     const Obj_Entry *obj)
    591 {
    592 	if (rela && rela->r_addend) {
    593 		Elf_Addr *ptr = (Elf_Addr *)where;
    594 		/*
    595 		 * This entry is >= 32768.  The relocations points to a
    596 		 * PC-relative pointer to the bind_0 stub at the top of the
    597 		 * PLT section.  Update it to point to the target function.
    598 		 */
    599 		ptr[0] += value - (Elf_Addr)obj->pltgot;
    600 	} else {
    601 		sparc_write_branch(where + 1, (void *)value);
    602 	}
    603 }
    604 
    605 /*
    606  * New inline function that is called by _rtld_relocate_plt_object and
    607  * _rtld_bind
    608  */
    609 static inline int
    610 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
    611     Elf_Addr *tp)
    612 {
    613 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    614 	const Elf_Sym *def;
    615 	const Obj_Entry *defobj;
    616 	Elf_Addr value;
    617 	unsigned long info = rela->r_info;
    618 
    619 	if (ELF_R_TYPE(info) == R_TYPE(JMP_IREL))
    620 		return 0;
    621 
    622 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
    623 
    624 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    625 	if (__predict_false(def == NULL))
    626 		return -1;
    627 	if (__predict_false(def == &_rtld_sym_zero))
    628 		return 0;
    629 
    630 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    631 		if (tp == NULL)
    632 			return 0;
    633 		value = _rtld_resolve_ifunc(defobj, def);
    634 	} else {
    635 		value = (Elf_Addr)(defobj->relocbase + def->st_value);
    636 	}
    637 	rdbg(("bind now/fixup in %s at %p --> new=%p",
    638 	    defobj->strtab + def->st_name, (void*)where, (void *)value));
    639 
    640 	_rtld_write_plt(where, value, rela, obj);
    641 
    642 	if (tp)
    643 		*tp = value;
    644 
    645 	return 0;
    646 }
    647