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