xref: /netbsd-src/libexec/ld.elf_so/arch/sparc64/mdreloc.c (revision ce716eeb9a02c7ecc82ab81d906a970d97432925)
1*ce716eebSriastradh /*	$NetBSD: mdreloc.c,v 1.72 2024/08/03 21:59:58 riastradh Exp $	*/
23cf5c2adSeeh 
33cf5c2adSeeh /*-
43cf5c2adSeeh  * Copyright (c) 2000 Eduardo Horvath.
5729925dfSmycroft  * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
63cf5c2adSeeh  * All rights reserved.
73cf5c2adSeeh  *
83cf5c2adSeeh  * This code is derived from software contributed to The NetBSD Foundation
9f6db390bSmycroft  * by Paul Kranenburg and by Charles M. Hannum.
103cf5c2adSeeh  *
113cf5c2adSeeh  * Redistribution and use in source and binary forms, with or without
123cf5c2adSeeh  * modification, are permitted provided that the following conditions
133cf5c2adSeeh  * are met:
143cf5c2adSeeh  * 1. Redistributions of source code must retain the above copyright
153cf5c2adSeeh  *    notice, this list of conditions and the following disclaimer.
163cf5c2adSeeh  * 2. Redistributions in binary form must reproduce the above copyright
173cf5c2adSeeh  *    notice, this list of conditions and the following disclaimer in the
183cf5c2adSeeh  *    documentation and/or other materials provided with the distribution.
193cf5c2adSeeh  *
203cf5c2adSeeh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
213cf5c2adSeeh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
223cf5c2adSeeh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
233cf5c2adSeeh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
243cf5c2adSeeh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
253cf5c2adSeeh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
263cf5c2adSeeh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
273cf5c2adSeeh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
283cf5c2adSeeh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
293cf5c2adSeeh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
303cf5c2adSeeh  * POSSIBILITY OF SUCH DAMAGE.
313cf5c2adSeeh  */
323cf5c2adSeeh 
33ddde66e5Suwe /*
34ddde66e5Suwe  * SPARC ELF relocations.
35ddde66e5Suwe  *
36ddde66e5Suwe  * Reference:
37ddde66e5Suwe  *
38ddde66e5Suwe  *	SPARC Compliance Definition 2.4.1
39ddde66e5Suwe  *	http://sparc.org/wp-content/uploads/2014/01/SCD.2.4.1.pdf.gz
40ddde66e5Suwe  */
41ddde66e5Suwe 
42680596d0Sskrll #include <sys/cdefs.h>
43680596d0Sskrll #ifndef lint
44*ce716eebSriastradh __RCSID("$NetBSD: mdreloc.c,v 1.72 2024/08/03 21:59:58 riastradh Exp $");
45680596d0Sskrll #endif /* not lint */
46680596d0Sskrll 
472d65afd2Sjoerg #include <machine/elf_support.h>
482d65afd2Sjoerg 
493cf5c2adSeeh #include <errno.h>
503cf5c2adSeeh #include <stdio.h>
513cf5c2adSeeh #include <stdlib.h>
523cf5c2adSeeh #include <string.h>
533cf5c2adSeeh #include <unistd.h>
543cf5c2adSeeh 
553cf5c2adSeeh #include "rtldenv.h"
563cf5c2adSeeh #include "debug.h"
573cf5c2adSeeh #include "rtld.h"
583cf5c2adSeeh 
593cf5c2adSeeh /*
603cf5c2adSeeh  * The following table holds for each relocation type:
613cf5c2adSeeh  *	- the width in bits of the memory location the relocation
623cf5c2adSeeh  *	  applies to (not currently used)
633cf5c2adSeeh  *	- the number of bits the relocation value must be shifted to the
643cf5c2adSeeh  *	  right (i.e. discard least significant bits) to fit into
653cf5c2adSeeh  *	  the appropriate field in the instruction word.
663cf5c2adSeeh  *	- flags indicating whether
673cf5c2adSeeh  *		* the relocation involves a symbol
683cf5c2adSeeh  *		* the relocation is relative to the current position
693cf5c2adSeeh  *		* the relocation is for a GOT entry
703cf5c2adSeeh  *		* the relocation is relative to the load address
713cf5c2adSeeh  *
723cf5c2adSeeh  */
733cf5c2adSeeh #define _RF_S		0x80000000		/* Resolve symbol */
743cf5c2adSeeh #define _RF_A		0x40000000		/* Use addend */
753cf5c2adSeeh #define _RF_P		0x20000000		/* Location relative */
763cf5c2adSeeh #define _RF_G		0x10000000		/* GOT offset */
773cf5c2adSeeh #define _RF_B		0x08000000		/* Load address relative */
7855c1b7fbSeeh #define _RF_U		0x04000000		/* Unaligned */
793cf5c2adSeeh #define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
803cf5c2adSeeh #define _RF_RS(s)	( (s) & 0xff)		/* right shift */
813a4af491Smartin static const int reloc_target_flags[R_TYPE(TLS_TPOFF64)+1] = {
823cf5c2adSeeh 	0,							/* NONE */
833cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(8)  | _RF_RS(0),		/* RELOC_8 */
843cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* RELOC_16 */
853cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELOC_32 */
863cf5c2adSeeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(8)  | _RF_RS(0),		/* DISP_8 */
873cf5c2adSeeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* DISP_16 */
883cf5c2adSeeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* DISP_32 */
893cf5c2adSeeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_30 */
903cf5c2adSeeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_22 */
913cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HI22 */
923cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 22 */
933cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 13 */
943cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LO10 */
953cf5c2adSeeh 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT10 */
963cf5c2adSeeh 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT13 */
973cf5c2adSeeh 	_RF_G|			_RF_SZ(32) | _RF_RS(10),	/* GOT22 */
983cf5c2adSeeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC10 */
993cf5c2adSeeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC22 */
1003cf5c2adSeeh 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WPLT30 */
1013cf5c2adSeeh 				_RF_SZ(32) | _RF_RS(0),		/* COPY */
1023cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* GLOB_DAT */
1033cf5c2adSeeh 				_RF_SZ(32) | _RF_RS(0),		/* JMP_SLOT */
1043cf5c2adSeeh 	      _RF_A|	_RF_B|	_RF_SZ(64) | _RF_RS(0),		/* RELATIVE */
10555c1b7fbSeeh 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(32) | _RF_RS(0),		/* UA_32 */
1063cf5c2adSeeh 
1073cf5c2adSeeh 	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
1083cf5c2adSeeh 	      _RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIPLT22 */
1093cf5c2adSeeh 	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
1103cf5c2adSeeh 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT32 */
1113cf5c2adSeeh 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PCPLT22 */
1123cf5c2adSeeh 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT10 */
1133cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 10 */
1143cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 11 */
1153cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* 64 */
1163cf5c2adSeeh 	_RF_S|_RF_A|/*extra*/	_RF_SZ(32) | _RF_RS(0),		/* OLO10 */
1173cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(42),	/* HH22 */
1183cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(32),	/* HM10 */
1193cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* LM22 */
1203cf5c2adSeeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(42),	/* PC_HH22 */
1213cf5c2adSeeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(32),	/* PC_HM10 */
1223cf5c2adSeeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC_LM22 */
1233cf5c2adSeeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP16 */
1243cf5c2adSeeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP19 */
1253cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_JMP */
1263cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 7 */
1273cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 5 */
1283cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 6 */
1293cf5c2adSeeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(64) | _RF_RS(0),		/* DISP64 */
1303cf5c2adSeeh 	      _RF_A|		_RF_SZ(64) | _RF_RS(0),		/* PLT64 */
1313cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIX22 */
1323cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOX10 */
1333cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(22),	/* H44 */
1343cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(12),	/* M44 */
1353cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* L44 */
1363cf5c2adSeeh 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* REGISTER */
13755c1b7fbSeeh 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(64) | _RF_RS(0),		/* UA64 */
13855c1b7fbSeeh 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(16) | _RF_RS(0),		/* UA16 */
1393a4af491Smartin /* TLS relocs not represented here! */
1403cf5c2adSeeh };
1413cf5c2adSeeh 
1423cf5c2adSeeh #ifdef RTLD_DEBUG_RELOC
1433cf5c2adSeeh static const char *reloc_names[] = {
1443cf5c2adSeeh 	"NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
1453cf5c2adSeeh 	"DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
1463cf5c2adSeeh 	"22", "13", "LO10", "GOT10", "GOT13",
1473cf5c2adSeeh 	"GOT22", "PC10", "PC22", "WPLT30", "COPY",
1483cf5c2adSeeh 	"GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32",
1493cf5c2adSeeh 	"HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
1503cf5c2adSeeh 	"10", "11", "64", "OLO10", "HH22",
1513cf5c2adSeeh 	"HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22",
1523cf5c2adSeeh 	"WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6",
1533cf5c2adSeeh 	"DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44",
1543a4af491Smartin 	"L44", "REGISTER", "UA64", "UA16",
1553a4af491Smartin 	"TLS_GD_HI22", "TLS_GD_LO10", "TLS_GD_ADD", "TLS_GD_CALL",
1563a4af491Smartin 	"TLS_LDM_HI22", "TLS_LDM_LO10", "TLS_LDM_ADD", "TLS_LDM_CALL",
1573a4af491Smartin 	"TLS_LDO_HIX22", "TLS_LDO_LOX10", "TLS_LDO_ADD", "TLS_IE_HI22",
1583a4af491Smartin 	"TLS_IE_LO10", "TLS_IE_LD", "TLS_IE_LDX", "TLS_IE_ADD", "TLS_LE_HIX22",
1593a4af491Smartin 	"TLS_LE_LOX10", "TLS_DTPMOD32", "TLS_DTPMOD64", "TLS_DTPOFF32",
1603a4af491Smartin 	"TLS_DTPOFF64", "TLS_TPOFF32", "TLS_TPOFF64",
1613cf5c2adSeeh };
1623cf5c2adSeeh #endif
1633cf5c2adSeeh 
1643cf5c2adSeeh #define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
1653cf5c2adSeeh #define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
1663cf5c2adSeeh #define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
16755c1b7fbSeeh #define RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
16855c1b7fbSeeh #define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
1693cf5c2adSeeh #define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
1703cf5c2adSeeh #define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
1713a4af491Smartin #define RELOC_TLS(t)			(t >= R_TYPE(TLS_GD_HI22))
1723cf5c2adSeeh 
1733eee01c5Smycroft static const long reloc_target_bitmask[] = {
1743cf5c2adSeeh #define _BM(x)	(~(-(1ULL << (x))))
1753cf5c2adSeeh 	0,				/* NONE */
1763cf5c2adSeeh 	_BM(8), _BM(16), _BM(32),	/* RELOC_8, _16, _32 */
1773cf5c2adSeeh 	_BM(8), _BM(16), _BM(32),	/* DISP8, DISP16, DISP32 */
1783cf5c2adSeeh 	_BM(30), _BM(22),		/* WDISP30, WDISP22 */
1793cf5c2adSeeh 	_BM(22), _BM(22),		/* HI22, _22 */
1803cf5c2adSeeh 	_BM(13), _BM(10),		/* RELOC_13, _LO10 */
1813cf5c2adSeeh 	_BM(10), _BM(13), _BM(22),	/* GOT10, GOT13, GOT22 */
1823cf5c2adSeeh 	_BM(10), _BM(22),		/* _PC10, _PC22 */
1833cf5c2adSeeh 	_BM(30), 0,			/* _WPLT30, _COPY */
184b87a9303Smartin 	-1, _BM(32), -1,		/* _GLOB_DAT, JMP_SLOT, _RELATIVE */
1853cf5c2adSeeh 	_BM(32), _BM(32),		/* _UA32, PLT32 */
1863cf5c2adSeeh 	_BM(22), _BM(10),		/* _HIPLT22, LOPLT10 */
1873cf5c2adSeeh 	_BM(32), _BM(22), _BM(10),	/* _PCPLT32, _PCPLT22, _PCPLT10 */
1883cf5c2adSeeh 	_BM(10), _BM(11), -1,		/* _10, _11, _64 */
189fee8b601Smartin 	_BM(13), _BM(22),		/* _OLO10, _HH22 */
1903cf5c2adSeeh 	_BM(10), _BM(22),		/* _HM10, _LM22 */
1913cf5c2adSeeh 	_BM(22), _BM(10), _BM(22),	/* _PC_HH22, _PC_HM10, _PC_LM22 */
1923cf5c2adSeeh 	_BM(16), _BM(19),		/* _WDISP16, _WDISP19 */
1933cf5c2adSeeh 	-1,				/* GLOB_JMP */
194c60b2e0bSmartin 	_BM(7), _BM(5), _BM(6),		/* _7, _5, _6 */
1953cf5c2adSeeh 	-1, -1,				/* DISP64, PLT64 */
1963cf5c2adSeeh 	_BM(22), _BM(13),		/* HIX22, LOX10 */
197abea8b31Smartin 	_BM(22), _BM(10), _BM(12),	/* H44, M44, L44 */
1983cf5c2adSeeh 	-1, -1, _BM(16),		/* REGISTER, UA64, UA16 */
1993cf5c2adSeeh #undef _BM
2003cf5c2adSeeh };
2013cf5c2adSeeh #define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
2023cf5c2adSeeh 
2033cf5c2adSeeh /*
2043cf5c2adSeeh  * Instruction templates:
2053cf5c2adSeeh  */
2063cf5c2adSeeh 
2073cf5c2adSeeh 
208d4e6f3ffSmycroft /* %hi(v)/%lo(v) with variable shift */
2093cf5c2adSeeh #define	HIVAL(v, s)	(((v) >> (s)) & 0x003fffff)
210d4e6f3ffSmycroft #define LOVAL(v, s)	(((v) >> (s)) & 0x000003ff)
2113cf5c2adSeeh 
212de4565eeSmycroft void _rtld_bind_start_0(long, long);
213de4565eeSmycroft void _rtld_bind_start_1(long, long);
214403e42f6Smycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
2155f573ab6Sskrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
216729925dfSmycroft 
217729925dfSmycroft /*
218729925dfSmycroft  * Install rtld function call into this PLT slot.
219729925dfSmycroft  */
22026043b23Smycroft #define	SAVE		0x9de3bf50	/* i.e. `save %sp,-176,%sp' */
221729925dfSmycroft #define	SETHI_l0	0x21000000
222729925dfSmycroft #define	SETHI_l1	0x23000000
223729925dfSmycroft #define	OR_l0_l0	0xa0142000
224729925dfSmycroft #define	SLLX_l0_32_l0	0xa12c3020
225729925dfSmycroft #define	OR_l0_l1_l0	0xa0140011
226d4e6f3ffSmycroft #define	JMPL_l0_o0	0x91c42000
227d4e6f3ffSmycroft #define	MOV_g1_o1	0x92100001
228729925dfSmycroft 
2299f486044Sskrll void _rtld_install_plt(Elf_Word *, Elf_Addr);
2309f486044Sskrll static inline int _rtld_relocate_plt_object(const Obj_Entry *,
2319f486044Sskrll     const Elf_Rela *, Elf_Addr *);
232729925dfSmycroft 
233729925dfSmycroft void
2345f573ab6Sskrll _rtld_install_plt(Elf_Word *pltgot, Elf_Addr proc)
235729925dfSmycroft {
236729925dfSmycroft 	pltgot[0] = SAVE;
237729925dfSmycroft 	pltgot[1] = SETHI_l0  | HIVAL(proc, 42);
238729925dfSmycroft 	pltgot[2] = SETHI_l1  | HIVAL(proc, 10);
239d4e6f3ffSmycroft 	pltgot[3] = OR_l0_l0  | LOVAL(proc, 32);
240729925dfSmycroft 	pltgot[4] = SLLX_l0_32_l0;
241729925dfSmycroft 	pltgot[5] = OR_l0_l1_l0;
242d4e6f3ffSmycroft 	pltgot[6] = JMPL_l0_o0 | LOVAL(proc, 0);
243d4e6f3ffSmycroft 	pltgot[7] = MOV_g1_o1;
244729925dfSmycroft }
245729925dfSmycroft 
246729925dfSmycroft void
247729925dfSmycroft _rtld_setup_pltgot(const Obj_Entry *obj)
248729925dfSmycroft {
249729925dfSmycroft 	/*
250729925dfSmycroft 	 * On sparc64 we got troubles.
251729925dfSmycroft 	 *
252729925dfSmycroft 	 * Instructions are 4 bytes long.
253729925dfSmycroft 	 * Elf[64]_Addr is 8 bytes long, so are our pltglot[]
254729925dfSmycroft 	 * array entries.
255729925dfSmycroft 	 * Each PLT entry jumps to PLT0 to enter the dynamic
256729925dfSmycroft 	 * linker.
257729925dfSmycroft 	 * Loading an arbitrary 64-bit pointer takes 6
258729925dfSmycroft 	 * instructions and 2 registers.
259729925dfSmycroft 	 *
260729925dfSmycroft 	 * Somehow we need to issue a save to get a new stack
261729925dfSmycroft 	 * frame, load the address of the dynamic linker, and
262729925dfSmycroft 	 * jump there, in 8 instructions or less.
263729925dfSmycroft 	 *
264729925dfSmycroft 	 * Oh, we need to fill out both PLT0 and PLT1.
265729925dfSmycroft 	 */
266729925dfSmycroft 	{
267729925dfSmycroft 		Elf_Word *entry = (Elf_Word *)obj->pltgot;
268729925dfSmycroft 
269729925dfSmycroft 		/* Install in entries 0 and 1 */
270729925dfSmycroft 		_rtld_install_plt(&entry[0], (Elf_Addr) &_rtld_bind_start_0);
271729925dfSmycroft 		_rtld_install_plt(&entry[8], (Elf_Addr) &_rtld_bind_start_1);
272729925dfSmycroft 
273729925dfSmycroft 		/*
274729925dfSmycroft 		 * Install the object reference in first slot
275729925dfSmycroft 		 * of entry 2.
276729925dfSmycroft 		 */
277729925dfSmycroft 		obj->pltgot[8] = (Elf_Addr) obj;
278729925dfSmycroft 	}
279729925dfSmycroft }
280729925dfSmycroft 
281729925dfSmycroft void
2825f573ab6Sskrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
283729925dfSmycroft {
284729925dfSmycroft 	const Elf_Rela *rela = 0, *relalim;
285729925dfSmycroft 	Elf_Addr relasz = 0;
286729925dfSmycroft 	Elf_Addr *where;
287729925dfSmycroft 
288729925dfSmycroft 	for (; dynp->d_tag != DT_NULL; dynp++) {
289729925dfSmycroft 		switch (dynp->d_tag) {
290729925dfSmycroft 		case DT_RELA:
291729925dfSmycroft 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
292729925dfSmycroft 			break;
293729925dfSmycroft 		case DT_RELASZ:
294729925dfSmycroft 			relasz = dynp->d_un.d_val;
295729925dfSmycroft 			break;
296729925dfSmycroft 		}
297729925dfSmycroft 	}
298b4fba76bSlukem 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
299729925dfSmycroft 	for (; rela < relalim; rela++) {
300729925dfSmycroft 		where = (Elf_Addr *)(relocbase + rela->r_offset);
301729925dfSmycroft 		*where = (Elf_Addr)(relocbase + rela->r_addend);
302729925dfSmycroft 	}
303729925dfSmycroft }
304403e42f6Smycroft 
3053cf5c2adSeeh int
306f40b256fSjoerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
3073cf5c2adSeeh {
308729925dfSmycroft 	const Elf_Rela *rela;
3093cc1de3dSmartin 	const Elf_Sym *def = NULL;
3103cc1de3dSmartin 	const Obj_Entry *defobj = NULL;
311e45d4ba0Sjoerg 	unsigned long last_symnum = ULONG_MAX;
312729925dfSmycroft 
313729925dfSmycroft 	for (rela = obj->rela; rela < obj->relalim; rela++) {
314729925dfSmycroft 		Elf_Addr *where;
315729925dfSmycroft 		Elf_Word type;
316729925dfSmycroft 		Elf_Addr value = 0, mask;
317729925dfSmycroft 		unsigned long symnum;
318729925dfSmycroft 
319729925dfSmycroft 		where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
320729925dfSmycroft 
321729925dfSmycroft 		type = ELF_R_TYPE(rela->r_info);
322729925dfSmycroft 		if (type == R_TYPE(NONE))
323729925dfSmycroft 			continue;
324729925dfSmycroft 
3250180ae1cSmartin 		/* OLO10 relocations have extra info */
3260180ae1cSmartin 		if ((type & 0x00ff) == R_SPARC_OLO10)
3270180ae1cSmartin 			type = R_SPARC_OLO10;
3280180ae1cSmartin 
329729925dfSmycroft 		/* We do JMP_SLOTs in _rtld_bind() below */
330729925dfSmycroft 		if (type == R_TYPE(JMP_SLOT))
331729925dfSmycroft 			continue;
332729925dfSmycroft 
333610e531eSjoerg 		/* IFUNC relocations are handled in _rtld_call_ifunc */
334610e531eSjoerg 		if (type == R_TYPE(IRELATIVE)) {
335f80c3669Sjoerg 			if (obj->ifunc_remaining_nonplt == 0) {
336f80c3669Sjoerg 				obj->ifunc_remaining_nonplt =
337f80c3669Sjoerg 				    obj->relalim - rela;
338f80c3669Sjoerg 			}
339610e531eSjoerg 			continue;
340610e531eSjoerg 		}
341610e531eSjoerg 
342729925dfSmycroft 		/* COPY relocs are also handled elsewhere */
343729925dfSmycroft 		if (type == R_TYPE(COPY))
344729925dfSmycroft 			continue;
345729925dfSmycroft 
346729925dfSmycroft 		/*
347729925dfSmycroft 		 * We use the fact that relocation types are an `enum'
3483a4af491Smartin 		 * Note: R_SPARC_TLS_TPOFF64 is currently numerically largest.
349729925dfSmycroft 		 */
3500180ae1cSmartin 		if (type > R_TYPE(TLS_TPOFF64)) {
3510180ae1cSmartin 			dbg(("unknown relocation type %x at %p", type, rela));
3520180ae1cSmartin 			return -1;
3530180ae1cSmartin 		}
354729925dfSmycroft 
355729925dfSmycroft 		value = rela->r_addend;
356729925dfSmycroft 
357e45d4ba0Sjoerg 		if (RELOC_RESOLVE_SYMBOL(type) || RELOC_TLS(type)) {
358e45d4ba0Sjoerg 			symnum = ELF_R_SYM(rela->r_info);
359e45d4ba0Sjoerg 			if (last_symnum != symnum) {
360e45d4ba0Sjoerg 				last_symnum = symnum;
361e45d4ba0Sjoerg 				def = _rtld_find_symdef(symnum, obj, &defobj,
362e45d4ba0Sjoerg 				    false);
363e45d4ba0Sjoerg 				if (def == NULL)
364e45d4ba0Sjoerg 					return -1;
365e45d4ba0Sjoerg 			}
366e45d4ba0Sjoerg 		}
367e45d4ba0Sjoerg 
368729925dfSmycroft 		/*
3693a4af491Smartin 		 * Handle TLS relocations here, they are different.
3703a4af491Smartin 		 */
3713a4af491Smartin 		if (RELOC_TLS(type)) {
3723a4af491Smartin 			switch (type) {
3733a4af491Smartin 			case R_TYPE(TLS_DTPMOD64):
3743a4af491Smartin 				*where = (Elf64_Addr)defobj->tlsindex;
3753a4af491Smartin 
3763a4af491Smartin 				rdbg(("TLS_DTPMOD64 %s in %s --> %p",
3773a4af491Smartin 				    obj->strtab +
3783a4af491Smartin 				    obj->symtab[symnum].st_name,
3793a4af491Smartin 				    obj->path, (void *)*where));
3803a4af491Smartin 
3813a4af491Smartin 				break;
3823a4af491Smartin 
3833a4af491Smartin 			case R_TYPE(TLS_DTPOFF64):
3843a4af491Smartin 				*where = (Elf64_Addr)(def->st_value
3853a4af491Smartin 				    + rela->r_addend);
3863a4af491Smartin 
3873a4af491Smartin 				rdbg(("DTPOFF64 %s in %s --> %p",
3883a4af491Smartin 				    obj->strtab +
3893a4af491Smartin 				        obj->symtab[symnum].st_name,
3903a4af491Smartin 				    obj->path, (void *)*where));
3913a4af491Smartin 
3923a4af491Smartin 				break;
3933a4af491Smartin 
3943a4af491Smartin 			case R_TYPE(TLS_TPOFF64):
3953caa8dc7Sjoerg 				if (!defobj->tls_static &&
3963caa8dc7Sjoerg 				    _rtld_tls_offset_allocate(__UNCONST(defobj)))
3973a4af491Smartin 					return -1;
3983a4af491Smartin 
3993a4af491Smartin 				*where = (Elf64_Addr)(def->st_value -
40028b12dabSjoerg 				    defobj->tlsoffset + rela->r_addend);
4013a4af491Smartin 
4023a4af491Smartin 				rdbg(("TLS_TPOFF64 %s in %s --> %p",
40328b12dabSjoerg 				    obj->strtab + obj->symtab[symnum].st_name,
4043a4af491Smartin 				    obj->path, (void *)*where));
4053a4af491Smartin 
4063a4af491Smartin 				break;
4073a4af491Smartin 			}
4083a4af491Smartin 			continue;
4093a4af491Smartin 		}
4103a4af491Smartin 
4113a4af491Smartin 		/*
412729925dfSmycroft 		 * Handle relative relocs here, as an optimization.
413729925dfSmycroft 		 */
414729925dfSmycroft 		if (type == R_TYPE(RELATIVE)) {
415729925dfSmycroft 			*where = (Elf_Addr)(obj->relocbase + value);
416729925dfSmycroft 			rdbg(("RELATIVE in %s --> %p", obj->path,
417729925dfSmycroft 			    (void *)*where));
418729925dfSmycroft 			continue;
419729925dfSmycroft 		}
420729925dfSmycroft 
421729925dfSmycroft 		if (RELOC_RESOLVE_SYMBOL(type)) {
422729925dfSmycroft 			/* Add in the symbol's absolute address */
423729925dfSmycroft 			value += (Elf_Addr)(defobj->relocbase + def->st_value);
424729925dfSmycroft 		}
425729925dfSmycroft 
4260180ae1cSmartin 		if (type == R_SPARC_OLO10) {
4270180ae1cSmartin 			value = (value & 0x3ff)
4280180ae1cSmartin 			    + (((Elf64_Xword)rela->r_info<<32)>>40);
4290180ae1cSmartin 		}
4300180ae1cSmartin 
431729925dfSmycroft 		if (RELOC_PC_RELATIVE(type)) {
432729925dfSmycroft 			value -= (Elf_Addr)where;
433729925dfSmycroft 		}
434729925dfSmycroft 
435729925dfSmycroft 		if (RELOC_BASE_RELATIVE(type)) {
436729925dfSmycroft 			/*
437729925dfSmycroft 			 * Note that even though sparcs use `Elf_rela'
438729925dfSmycroft 			 * exclusively we still need the implicit memory addend
439729925dfSmycroft 			 * in relocations referring to GOT entries.
440729925dfSmycroft 			 * Undoubtedly, someone f*cked this up in the distant
441729925dfSmycroft 			 * past, and now we're stuck with it in the name of
442729925dfSmycroft 			 * compatibility for all eternity..
443729925dfSmycroft 			 *
444729925dfSmycroft 			 * In any case, the implicit and explicit should be
445729925dfSmycroft 			 * mutually exclusive. We provide a check for that
446729925dfSmycroft 			 * here.
447729925dfSmycroft 			 */
448729925dfSmycroft #ifdef DIAGNOSTIC
449729925dfSmycroft 			if (value != 0 && *where != 0) {
450729925dfSmycroft 				xprintf("BASE_REL(%s): where=%p, *where 0x%lx, "
451729925dfSmycroft 					"addend=0x%lx, base %p\n",
452729925dfSmycroft 					obj->path, where, *where,
453729925dfSmycroft 					rela->r_addend, obj->relocbase);
454729925dfSmycroft 			}
455729925dfSmycroft #endif
456729925dfSmycroft 			/* XXXX -- apparently we ignore the preexisting value */
457729925dfSmycroft 			value += (Elf_Addr)(obj->relocbase);
458729925dfSmycroft 		}
459729925dfSmycroft 
460729925dfSmycroft 		mask = RELOC_VALUE_BITMASK(type);
461729925dfSmycroft 		value >>= RELOC_VALUE_RIGHTSHIFT(type);
462729925dfSmycroft 		value &= mask;
463729925dfSmycroft 
464729925dfSmycroft 		if (RELOC_UNALIGNED(type)) {
465729925dfSmycroft 			/* Handle unaligned relocations. */
466729925dfSmycroft 			Elf_Addr tmp = 0;
467729925dfSmycroft 			char *ptr = (char *)where;
468729925dfSmycroft 			int i, size = RELOC_TARGET_SIZE(type)/8;
469729925dfSmycroft 
470729925dfSmycroft 			/* Read it in one byte at a time. */
471729925dfSmycroft 			for (i=0; i<size; i++)
472729925dfSmycroft 				tmp = (tmp << 8) | ptr[i];
473729925dfSmycroft 
474729925dfSmycroft 			tmp &= ~mask;
475729925dfSmycroft 			tmp |= value;
476729925dfSmycroft 
477729925dfSmycroft 			/* Write it back out. */
478729925dfSmycroft 			for (i=0; i<size; i++)
479729925dfSmycroft 				ptr[i] = ((tmp >> (8*i)) & 0xff);
480729925dfSmycroft #ifdef RTLD_DEBUG_RELOC
481729925dfSmycroft 			value = (Elf_Addr)tmp;
482729925dfSmycroft #endif
483729925dfSmycroft 
484729925dfSmycroft 		} else if (RELOC_TARGET_SIZE(type) > 32) {
485729925dfSmycroft 			*where &= ~mask;
486729925dfSmycroft 			*where |= value;
487729925dfSmycroft #ifdef RTLD_DEBUG_RELOC
488729925dfSmycroft 			value = (Elf_Addr)*where;
489729925dfSmycroft #endif
490729925dfSmycroft 		} else {
491729925dfSmycroft 			Elf32_Addr *where32 = (Elf32_Addr *)where;
492729925dfSmycroft 
493729925dfSmycroft 			*where32 &= ~mask;
494729925dfSmycroft 			*where32 |= value;
495729925dfSmycroft #ifdef RTLD_DEBUG_RELOC
496729925dfSmycroft 			value = (Elf_Addr)*where32;
497729925dfSmycroft #endif
498729925dfSmycroft 		}
499729925dfSmycroft 
500729925dfSmycroft #ifdef RTLD_DEBUG_RELOC
501729925dfSmycroft 		if (RELOC_RESOLVE_SYMBOL(type)) {
502729925dfSmycroft 			rdbg(("%s %s in %s --> %p in %s", reloc_names[type],
503729925dfSmycroft 			    obj->strtab + obj->symtab[symnum].st_name,
504458dbd70Spetrov 			    obj->path, (void *)value, defobj->path));
505729925dfSmycroft 		} else {
506729925dfSmycroft 			rdbg(("%s in %s --> %p", reloc_names[type],
507458dbd70Spetrov 			    obj->path, (void *)value));
508729925dfSmycroft 		}
509729925dfSmycroft #endif
510729925dfSmycroft 	}
511729925dfSmycroft 	return (0);
512729925dfSmycroft }
513729925dfSmycroft 
514729925dfSmycroft int
515e78cfb8eSjoerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
516729925dfSmycroft {
517610e531eSjoerg 	const Elf_Rela *rela;
518610e531eSjoerg 
519610e531eSjoerg 	for (rela = obj->pltrelalim; rela-- > obj->pltrela; ) {
520610e531eSjoerg 		if (ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_IREL))
521610e531eSjoerg 			obj->ifunc_remaining = obj->pltrelalim - rela + 1;
522610e531eSjoerg 	}
523610e531eSjoerg 
524610e531eSjoerg 	return 0;
525729925dfSmycroft }
526729925dfSmycroft 
527729925dfSmycroft caddr_t
5285f573ab6Sskrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
529729925dfSmycroft {
530028dd515Smycroft 	const Elf_Rela *rela = obj->pltrela + reloff;
53135130888Smartin 	Elf_Addr result;
53235130888Smartin 	int err;
5333cf5c2adSeeh 
534084c0528Smrg 	result = 0;	/* XXX gcc */
535084c0528Smrg 
536e5e34a6cSjoerg 	if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT) ||
537e5e34a6cSjoerg 	    ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_IREL)) {
538729925dfSmycroft 		/*
539729925dfSmycroft 		 * XXXX
540729925dfSmycroft 		 *
541729925dfSmycroft 		 * The first four PLT entries are reserved.  There is some
542729925dfSmycroft 		 * disagreement whether they should have associated relocation
543729925dfSmycroft 		 * entries.  Both the SPARC 32-bit and 64-bit ELF
544729925dfSmycroft 		 * specifications say that they should have relocation entries,
545729925dfSmycroft 		 * but the 32-bit SPARC binutils do not generate them, and now
546729925dfSmycroft 		 * the 64-bit SPARC binutils have stopped generating them too.
547729925dfSmycroft 		 *
548729925dfSmycroft 		 * So, to provide binary compatibility, we will check the first
549729925dfSmycroft 		 * entry, if it is reserved it should not be of the type
550e5e34a6cSjoerg 		 * JMP_SLOT or JMP_REL.  If it is either of those, then
551e5e34a6cSjoerg 		 * the 4 reserved entries were not generated and our index
552e5e34a6cSjoerg 		 * is 4 entries too far.
553729925dfSmycroft 		 */
554729925dfSmycroft 		rela -= 4;
555729925dfSmycroft 	}
556729925dfSmycroft 
557cb1cd7e8Sjoerg 	_rtld_shared_enter();
55835130888Smartin 	err = _rtld_relocate_plt_object(obj, rela, &result);
55912bd4dbdSchristos 	if (err)
56035130888Smartin 		_rtld_die();
561cb1cd7e8Sjoerg 	_rtld_shared_exit();
56235130888Smartin 
56335130888Smartin 	return (caddr_t)result;
56435130888Smartin }
56535130888Smartin 
56635130888Smartin int
56735130888Smartin _rtld_relocate_plt_objects(const Obj_Entry *obj)
56835130888Smartin {
56935130888Smartin 	const Elf_Rela *rela;
57035130888Smartin 
57135130888Smartin 	rela = obj->pltrela;
57235130888Smartin 
57335130888Smartin 	/*
57435130888Smartin 	 * Check for first four reserved entries - and skip them.
57535130888Smartin 	 * See above for details.
57635130888Smartin 	 */
577e5e34a6cSjoerg 	if (ELF_R_TYPE(obj->pltrela->r_info) != R_TYPE(JMP_SLOT) &&
578e5e34a6cSjoerg 	    ELF_R_TYPE(obj->pltrela->r_info) != R_TYPE(JMP_IREL))
57935130888Smartin 		rela += 4;
58035130888Smartin 
58135130888Smartin 	for (; rela < obj->pltrelalim; rela++)
58235130888Smartin 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
58335130888Smartin 			return -1;
58435130888Smartin 
58535130888Smartin 	return 0;
58635130888Smartin }
58735130888Smartin 
588610e531eSjoerg static inline void
589610e531eSjoerg _rtld_write_plt(Elf_Word *where, Elf_Addr value, const Elf_Rela *rela,
590610e531eSjoerg     const Obj_Entry *obj)
59135130888Smartin {
592610e531eSjoerg 	if (rela && rela->r_addend) {
5933cf5c2adSeeh 		Elf_Addr *ptr = (Elf_Addr *)where;
5943cf5c2adSeeh 		/*
59500794552Smycroft 		 * This entry is >= 32768.  The relocations points to a
59600794552Smycroft 		 * PC-relative pointer to the bind_0 stub at the top of the
59700794552Smycroft 		 * PLT section.  Update it to point to the target function.
5983cf5c2adSeeh 		 */
599f6db390bSmycroft 		ptr[0] += value - (Elf_Addr)obj->pltgot;
6003cf5c2adSeeh 	} else {
6012d65afd2Sjoerg 		sparc_write_branch(where + 1, (void *)value);
6023cf5c2adSeeh 	}
603610e531eSjoerg }
604610e531eSjoerg 
605610e531eSjoerg /*
606610e531eSjoerg  * New inline function that is called by _rtld_relocate_plt_object and
607610e531eSjoerg  * _rtld_bind
608610e531eSjoerg  */
609610e531eSjoerg static inline int
610610e531eSjoerg _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
611610e531eSjoerg     Elf_Addr *tp)
612610e531eSjoerg {
613610e531eSjoerg 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
614610e531eSjoerg 	const Elf_Sym *def;
615610e531eSjoerg 	const Obj_Entry *defobj;
616610e531eSjoerg 	Elf_Addr value;
617610e531eSjoerg 	unsigned long info = rela->r_info;
618610e531eSjoerg 
619610e531eSjoerg 	if (ELF_R_TYPE(info) == R_TYPE(JMP_IREL))
620610e531eSjoerg 		return 0;
621610e531eSjoerg 
622610e531eSjoerg 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
623610e531eSjoerg 
624610e531eSjoerg 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
625610e531eSjoerg 	if (__predict_false(def == NULL))
626610e531eSjoerg 		return -1;
627610e531eSjoerg 	if (__predict_false(def == &_rtld_sym_zero))
628610e531eSjoerg 		return 0;
629610e531eSjoerg 
630610e531eSjoerg 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
631610e531eSjoerg 		if (tp == NULL)
632610e531eSjoerg 			return 0;
633610e531eSjoerg 		value = _rtld_resolve_ifunc(defobj, def);
634610e531eSjoerg 	} else {
635610e531eSjoerg 		value = (Elf_Addr)(defobj->relocbase + def->st_value);
636610e531eSjoerg 	}
637610e531eSjoerg 	rdbg(("bind now/fixup in %s at %p --> new=%p",
638610e531eSjoerg 	    defobj->strtab + def->st_name, (void*)where, (void *)value));
639610e531eSjoerg 
640610e531eSjoerg 	_rtld_write_plt(where, value, rela, obj);
6413cf5c2adSeeh 
64235130888Smartin 	if (tp)
64335130888Smartin 		*tp = value;
64435130888Smartin 
64535130888Smartin 	return 0;
646a04012e7Smycroft }
647