xref: /onnv-gate/usr/src/cmd/sgs/libld/common/machrel.sparc.c (revision 13074:787bf65954d0)
11618Srie /*
21618Srie  * CDDL HEADER START
31618Srie  *
41618Srie  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
71618Srie  *
81618Srie  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91618Srie  * or http://www.opensolaris.org/os/licensing.
101618Srie  * See the License for the specific language governing permissions
111618Srie  * and limitations under the License.
121618Srie  *
131618Srie  * When distributing Covered Code, include this CDDL HEADER in each
141618Srie  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151618Srie  * If applicable, add the following below this CDDL HEADER, with the
161618Srie  * fields enclosed by brackets "[]" replaced with your own identifying
171618Srie  * information: Portions Copyright [yyyy] [name of copyright owner]
181618Srie  *
191618Srie  * CDDL HEADER END
201618Srie  */
211618Srie 
221618Srie /*
231618Srie  *	Copyright (c) 1988 AT&T
241618Srie  *	  All Rights Reserved
251618Srie  *
2612155SAli.Bahrami@Sun.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
271618Srie  */
281618Srie 
296206Sab196087 /* Get the sparc version of the relocation engine */
306206Sab196087 #define	DO_RELOC_LIBLD_SPARC
316206Sab196087 
321618Srie #include	<string.h>
331618Srie #include	<stdio.h>
341618Srie #include	<sys/elf_SPARC.h>
351618Srie #include	<debug.h>
361618Srie #include	<reloc.h>
376206Sab196087 #include	<sparc/machdep_sparc.h>
381618Srie #include	"msg.h"
391618Srie #include	"_libld.h"
406206Sab196087 #include	"machsym.sparc.h"
416206Sab196087 
421618Srie /*
431618Srie  * Local Variable Definitions
441618Srie  */
451618Srie static Sword neggotoffset = 0;		/* off. of GOT table from GOT symbol */
461618Srie static Sword smlgotcnt = M_GOT_XNumber;	/* no. of small GOT symbols */
475152Sab196087 static Sword mixgotcnt = 0;		/* # syms with both large/small GOT */
481618Srie 
499131SRod.Evans@Sun.COM /*
509131SRod.Evans@Sun.COM  * Search the GOT index list for a GOT entry with a matching reference and the
519131SRod.Evans@Sun.COM  * proper addend.
529131SRod.Evans@Sun.COM  */
539131SRod.Evans@Sun.COM static Gotndx *
ld_find_got_ndx(Alist * alp,Gotref gref,Ofl_desc * ofl,Rel_desc * rdesc)549131SRod.Evans@Sun.COM ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc)
559131SRod.Evans@Sun.COM {
569131SRod.Evans@Sun.COM 	Aliste	idx;
579131SRod.Evans@Sun.COM 	Gotndx	*gnp;
589131SRod.Evans@Sun.COM 
599131SRod.Evans@Sun.COM 	assert(rdesc != 0);
609131SRod.Evans@Sun.COM 
619131SRod.Evans@Sun.COM 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
629131SRod.Evans@Sun.COM 		return (ofl->ofl_tlsldgotndx);
639131SRod.Evans@Sun.COM 
649131SRod.Evans@Sun.COM 	for (ALIST_TRAVERSE(alp, idx, gnp)) {
659131SRod.Evans@Sun.COM 		if ((rdesc->rel_raddend == gnp->gn_addend) &&
669131SRod.Evans@Sun.COM 		    (gref == gnp->gn_gotref))
679131SRod.Evans@Sun.COM 			return (gnp);
689131SRod.Evans@Sun.COM 	}
699131SRod.Evans@Sun.COM 	return (NULL);
709131SRod.Evans@Sun.COM }
719131SRod.Evans@Sun.COM 
729131SRod.Evans@Sun.COM static Xword
ld_calc_got_offset(Rel_desc * rdesc,Ofl_desc * ofl)739131SRod.Evans@Sun.COM ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
749131SRod.Evans@Sun.COM {
759131SRod.Evans@Sun.COM 	Os_desc		*osp = ofl->ofl_osgot;
769131SRod.Evans@Sun.COM 	Sym_desc	*sdp = rdesc->rel_sym;
779131SRod.Evans@Sun.COM 	Xword		gotndx;
789131SRod.Evans@Sun.COM 	Gotref		gref;
799131SRod.Evans@Sun.COM 	Gotndx		*gnp;
809131SRod.Evans@Sun.COM 
819131SRod.Evans@Sun.COM 	if (rdesc->rel_flags & FLG_REL_DTLS)
829131SRod.Evans@Sun.COM 		gref = GOT_REF_TLSGD;
839131SRod.Evans@Sun.COM 	else if (rdesc->rel_flags & FLG_REL_MTLS)
849131SRod.Evans@Sun.COM 		gref = GOT_REF_TLSLD;
859131SRod.Evans@Sun.COM 	else if (rdesc->rel_flags & FLG_REL_STLS)
869131SRod.Evans@Sun.COM 		gref = GOT_REF_TLSIE;
879131SRod.Evans@Sun.COM 	else
889131SRod.Evans@Sun.COM 		gref = GOT_REF_GENERIC;
899131SRod.Evans@Sun.COM 
909131SRod.Evans@Sun.COM 	gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, rdesc);
919131SRod.Evans@Sun.COM 	assert(gnp);
929131SRod.Evans@Sun.COM 
939131SRod.Evans@Sun.COM 	gotndx = (Xword)gnp->gn_gotndx;
949131SRod.Evans@Sun.COM 
959131SRod.Evans@Sun.COM 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
969131SRod.Evans@Sun.COM 	    (rdesc->rel_rtype == M_R_DTPOFF))
979131SRod.Evans@Sun.COM 		gotndx++;
989131SRod.Evans@Sun.COM 
999131SRod.Evans@Sun.COM 	return ((Xword)((osp->os_shdr->sh_addr) + (gotndx * M_GOT_ENTSIZE) +
1009131SRod.Evans@Sun.COM 	    (-neggotoffset * M_GOT_ENTSIZE)));
1019131SRod.Evans@Sun.COM }
1029131SRod.Evans@Sun.COM 
1036206Sab196087 static Word
ld_init_rel(Rel_desc * reld,Word * typedata,void * reloc)10412155SAli.Bahrami@Sun.COM ld_init_rel(Rel_desc *reld, Word *typedata, void *reloc)
1051618Srie {
1069131SRod.Evans@Sun.COM 	Rela	*rela = (Rela *)reloc;
1071618Srie 
1081618Srie 	/* LINTED */
1096206Sab196087 	reld->rel_rtype = (Word)ELF_R_TYPE(rela->r_info, M_MACH);
1101618Srie 	reld->rel_roffset = rela->r_offset;
1111618Srie 	reld->rel_raddend = rela->r_addend;
11212155SAli.Bahrami@Sun.COM 	*typedata = (Word)ELF_R_TYPE_DATA(rela->r_info);
1131618Srie 
1141618Srie 	reld->rel_flags |= FLG_REL_RELA;
1151618Srie 
1161618Srie 	return ((Word)ELF_R_SYM(rela->r_info));
1171618Srie }
1181618Srie 
1196206Sab196087 static void
ld_mach_eflags(Ehdr * ehdr,Ofl_desc * ofl)1201618Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
1211618Srie {
1221618Srie 	Word		eflags = ofl->ofl_dehdr->e_flags;
1231618Srie 	Word		memopt1, memopt2;
1241618Srie 	static int	firstpass;
1251618Srie 
1261618Srie 	/*
1271618Srie 	 * If a *PLUS relocatable is included, the output object is type *PLUS.
1281618Srie 	 */
1291618Srie 	if ((ehdr->e_machine == EM_SPARC32PLUS) &&
1301618Srie 	    (ehdr->e_flags & EF_SPARC_32PLUS))
1311618Srie 		ofl->ofl_dehdr->e_machine = EM_SPARC32PLUS;
1321618Srie 
1331618Srie 	/*
1341618Srie 	 * On the first pass, we don't yet have a memory model to compare
1351618Srie 	 * against, therefore the initial file becomes our baseline.  Subsequent
1361618Srie 	 * passes will do the comparison described below.
1371618Srie 	 */
1381618Srie 	if (firstpass == 0) {
1391618Srie 		ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
1401618Srie 		firstpass++;
1411618Srie 		return;
1421618Srie 	}
1431618Srie 
1441618Srie 	/*
1451618Srie 	 * Determine which memory model to mark the binary with.  The options
1461618Srie 	 * are (most restrictive to least):
1471618Srie 	 *
1481618Srie 	 *	EF_SPARCV9_TSO		0x0 	Total Store Order
1491618Srie 	 *	EF_SPARCV9_PSO		0x1	Partial Store Order
1501618Srie 	 *	EF_SPARCV9_RMO		0x2	Relaxed Memory Order
1511618Srie 	 *
1521618Srie 	 * Mark the binary with the most restrictive option encountered from a
1531618Srie 	 * relocatable object included in the link.
1541618Srie 	 */
1551618Srie 	eflags |= (ehdr->e_flags & ~EF_SPARCV9_MM);
1561618Srie 	memopt1 = eflags & EF_SPARCV9_MM;
1571618Srie 	memopt2 = ehdr->e_flags & EF_SPARCV9_MM;
1581618Srie 	eflags &= ~EF_SPARCV9_MM;
1591618Srie 
1601618Srie 	if ((memopt1 == EF_SPARCV9_TSO) || (memopt2 == EF_SPARCV9_TSO))
1611618Srie 		/* EMPTY */
1621618Srie 		;
1631618Srie 	else if ((memopt1 == EF_SPARCV9_PSO) || (memopt2 == EF_SPARCV9_PSO))
1641618Srie 		eflags |= EF_SPARCV9_PSO;
1651618Srie 	else
1661618Srie 		eflags |= EF_SPARCV9_RMO;
1671618Srie 
1681618Srie 	ofl->ofl_dehdr->e_flags = eflags;
1691618Srie }
1701618Srie 
1716206Sab196087 static void
ld_mach_make_dynamic(Ofl_desc * ofl,size_t * cnt)1721618Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
1731618Srie {
1741618Srie 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
1751618Srie 		/*
1761618Srie 		 * Create this entry if we are going to create a PLT table.
1771618Srie 		 */
1781618Srie 		if (ofl->ofl_pltcnt)
1791618Srie 			(*cnt)++;		/* DT_PLTGOT */
1801618Srie 	}
1811618Srie }
1821618Srie 
1836206Sab196087 static void
ld_mach_update_odynamic(Ofl_desc * ofl,Dyn ** dyn)1842145Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
1851618Srie {
1861618Srie 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
1871618Srie 		(*dyn)->d_tag = DT_PLTGOT;
1881618Srie 		if (ofl->ofl_osplt)
1891618Srie 			(*dyn)->d_un.d_ptr = ofl->ofl_osplt->os_shdr->sh_addr;
1901618Srie 		else
1911618Srie 			(*dyn)->d_un.d_ptr = 0;
1921618Srie 		(*dyn)++;
1931618Srie 	}
1941618Srie }
1951618Srie 
1961618Srie #if	defined(_ELF64)
1971618Srie 
1986206Sab196087 static Xword
ld_calc_plt_addr(Sym_desc * sdp,Ofl_desc * ofl)1991618Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
2001618Srie {
2011618Srie 	Xword	value, pltndx, farpltndx;
2021618Srie 
2031618Srie 	pltndx = sdp->sd_aux->sa_PLTndx + M_PLT_XNumber - 1;
2041618Srie 
2051618Srie 	if ((pltndx) < M64_PLT_NEARPLTS) {
2061618Srie 		value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
2071618Srie 		    (pltndx * M_PLT_ENTSIZE);
2081618Srie 		return (value);
2091618Srie 	}
2101618Srie 
2111618Srie 	farpltndx = pltndx - M64_PLT_NEARPLTS;
2121618Srie 
2131618Srie 	/*
2141618Srie 	 * pltoffset of a far plt is calculated by:
2151618Srie 	 *
2161618Srie 	 *	<size of near plt table> +
2171618Srie 	 *	<size of preceding far plt blocks> +
2181618Srie 	 *	<blockndx * sizeof (far plt entsize)>
2191618Srie 	 */
2201618Srie 	value =
2211618Srie 	    /* size of near plt table */
2221618Srie 	    (M64_PLT_NEARPLTS * M_PLT_ENTSIZE) +
2231618Srie 	    /* size of preceding far plt blocks */
2241618Srie 	    ((farpltndx / M64_PLT_FBLKCNTS) *
2251618Srie 	    ((M64_PLT_FENTSIZE + sizeof (Addr)) *
2261618Srie 	    M64_PLT_FBLKCNTS)) +
2271618Srie 	    /* pltblockendx * fentsize */
2281618Srie 	    ((farpltndx % M64_PLT_FBLKCNTS) * M64_PLT_FENTSIZE);
2291618Srie 
2301618Srie 	value += (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
2311618Srie 	return (value);
2321618Srie }
2331618Srie 
2341618Srie /*
2351618Srie  * Instructions required for Far PLT's
2361618Srie  */
2376206Sab196087 static uchar_t farplt_instrs[24] = {
2386206Sab196087 	0x8a, 0x10, 0x00, 0x0f,		/* mov   %o7, %g5	*/
2396206Sab196087 	0x40, 0x00, 0x00, 0x02,		/* call  . + 0x8	*/
2406206Sab196087 	0x01, 0x00, 0x00, 0x00,		/* nop			*/
2416206Sab196087 	0xc2, 0x5b, 0xe0, 0x00,		/* ldx   [%o7 + 0], %g1	*/
2426206Sab196087 	0x83, 0xc3, 0xc0, 0x01,		/* jmpl  %o7 + %g1, %g1	*/
2436206Sab196087 	0x9e, 0x10, 0x00, 0x05		/* mov   %g5, %o7	*/
2441618Srie };
2451618Srie 
2461618Srie /*
2471618Srie  * Far PLT'S:
2481618Srie  *
2491618Srie  * Far PLT's are established in blocks of '160' at a time.  These
2501618Srie  * PLT's consist of 6 instructions (24 bytes) and 1 pointer (8 bytes).
2511618Srie  * The instructions are collected together in blocks of 160 entries
2521618Srie  * followed by 160 pointers.  The last group of entries and pointers
2531618Srie  * may contain less then 160 items.  No padding is required.
2541618Srie  *
2551618Srie  *	.PLT32768:
2561618Srie  *		mov	%o7, %g5
2571618Srie  *		call	. + 8
2581618Srie  *		nop
2591618Srie  *		ldx	[%o7 + .PLTP32768 - (.PLT32768 + 4)], %g1
2601618Srie  *		jmpl	%o7 + %g1, %g1
2611618Srie  *		mov	%g5, %o7
2621618Srie  *	................................
2631618Srie  *	.PLT32927:
2641618Srie  *		mov	%o7, %g5
2651618Srie  *		call	. + 8
2661618Srie  *		nop
2671618Srie  *		ldx	[%o7 + .PLTP32927 - (.PLT32927 + 4)], %g1
2681618Srie  *		jmpl	%o7 + %g1, %g1
2691618Srie  *		mov	%g5, %o7
2701618Srie  *	.PLTP32768:
2711618Srie  *		.xword .PLT0-(.PLT32768+4)
2721618Srie  *	................................
2731618Srie  *	.PLTP32927:
2741618Srie  *		.xword .PLT0-(.PLT32927+4)
2751618Srie  *
2761618Srie  */
2776206Sab196087 static void
plt_far_entry(Ofl_desc * ofl,Xword pltndx,Xword * roffset,Sxword * raddend)2781618Srie plt_far_entry(Ofl_desc *ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
2791618Srie {
2801618Srie 	uint_t		blockndx;	/* # of far PLT blocks */
2811618Srie 	uint_t		farblkcnt;	/* Index to far PLT block */
2821618Srie 	Xword		farpltndx;	/* index of Far Plt */
2831618Srie 	Xword		farpltblkndx;	/* index of PLT in BLOCK */
2841618Srie 	uint32_t	*pltent;	/* ptr to plt instr. sequence */
2851618Srie 	uint64_t	*pltentptr;	/* ptr to plt addr ptr */
2861618Srie 	Sxword		pltblockoff;	/* offset to Far plt block */
2871618Srie 	Sxword		pltoff;		/* offset to PLT instr. sequence */
2881618Srie 	Sxword		pltptroff;	/* offset to PLT addr ptr */
2891618Srie 	uchar_t		*pltbuf;	/* ptr to PLT's in file */
2901618Srie 
2911618Srie 
2921618Srie 	farblkcnt = ((ofl->ofl_pltcnt - 1 +
2934734Sab196087 	    M_PLT_XNumber - M64_PLT_NEARPLTS) / M64_PLT_FBLKCNTS);
2941618Srie 
2951618Srie 	/*
2961618Srie 	 * Determine the 'Far' PLT index.
2971618Srie 	 */
2981618Srie 	farpltndx = pltndx - 1 + M_PLT_XNumber - M64_PLT_NEARPLTS;
2991618Srie 	farpltblkndx = farpltndx % M64_PLT_FBLKCNTS;
3001618Srie 
3011618Srie 	/*
3021618Srie 	 * Determine what FPLT block this plt falls into.
3031618Srie 	 */
3041618Srie 	blockndx = (uint_t)(farpltndx / M64_PLT_FBLKCNTS);
3051618Srie 
3061618Srie 	/*
3071618Srie 	 * Calculate the starting offset of the Far PLT block
3081618Srie 	 * that this PLT is a member of.
3091618Srie 	 */
3101618Srie 	pltblockoff = (M64_PLT_NEARPLTS * M_PLT_ENTSIZE) +
3114734Sab196087 	    (blockndx * M64_PLT_FBLOCKSZ);
3121618Srie 
3131618Srie 	pltoff = pltblockoff +
3144734Sab196087 	    (farpltblkndx * M64_PLT_FENTSIZE);
3151618Srie 
3161618Srie 	pltptroff = pltblockoff;
3171618Srie 
3181618Srie 
3191618Srie 	if (farblkcnt > blockndx) {
3201618Srie 		/*
3211618Srie 		 * If this is a full block - the 'pltptroffs' start
3221618Srie 		 * after 160 fplts.
3231618Srie 		 */
3241618Srie 		pltptroff += (M64_PLT_FBLKCNTS * M64_PLT_FENTSIZE) +
3254734Sab196087 		    (farpltblkndx * M64_PLT_PSIZE);
3261618Srie 	} else {
3271618Srie 		Xword	lastblkpltndx;
3281618Srie 		/*
3291618Srie 		 * If this is the last block - the the pltptr's start
3301618Srie 		 * after the last FPLT instruction sequence.
3311618Srie 		 */
3321618Srie 		lastblkpltndx = (ofl->ofl_pltcnt - 1 + M_PLT_XNumber -
3334734Sab196087 		    M64_PLT_NEARPLTS) % M64_PLT_FBLKCNTS;
3341618Srie 		pltptroff += ((lastblkpltndx + 1) * M64_PLT_FENTSIZE) +
3354734Sab196087 		    (farpltblkndx * M64_PLT_PSIZE);
3361618Srie 	}
3371618Srie 	pltbuf = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
3381618Srie 
3391618Srie 	/*
3401618Srie 	 * For far-plts, the Raddend and Roffset fields are defined
3411618Srie 	 * to be:
3421618Srie 	 *
3431618Srie 	 *	roffset:	address of .PLTP#
3441618Srie 	 *	raddend:	-(.PLT#+4)
3451618Srie 	 */
3461618Srie 	*roffset = pltptroff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
3471618Srie 	*raddend = -(pltoff + 4 + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr));
3481618Srie 
3491618Srie 	/* LINTED */
3501618Srie 	pltent = (uint32_t *)(pltbuf + pltoff);
3511618Srie 	/* LINTED */
3521618Srie 	pltentptr = (uint64_t *)(pltbuf + pltptroff);
3531618Srie 	(void) memcpy(pltent, farplt_instrs, sizeof (farplt_instrs));
3541618Srie 
3551618Srie 	/*
3561618Srie 	 *  update
3571618Srie 	 *	ldx   [%o7 + 0], %g1
3581618Srie 	 * to
3591618Srie 	 *	ldx   [%o7 + .PLTP# - (.PLT# + 4)], %g1
3601618Srie 	 */
3611618Srie 	/* LINTED */
3621618Srie 	pltent[3] |= (uint32_t)(pltptroff - (pltoff + 4));
3631618Srie 
3641618Srie 	/*
3651618Srie 	 * Store:
3661618Srie 	 *	.PLTP#
3671618Srie 	 *		.xword	.PLT0 - .PLT# + 4
3681618Srie 	 */
3691618Srie 	*pltentptr = -(pltoff + 4);
3701618Srie }
3711618Srie 
3721618Srie /*
3731618Srie  *	Build a single V9 P.L.T. entry - code is:
3741618Srie  *
3751618Srie  *	For Target Addresses +/- 4GB of the entry
3761618Srie  *	-----------------------------------------
3771618Srie  *	sethi	(. - .PLT0), %g1
3781618Srie  *	ba,a	%xcc, .PLT1
3791618Srie  *	nop
3801618Srie  *	nop
3811618Srie  *	nop
3821618Srie  *	nop
3831618Srie  *	nop
3841618Srie  *	nop
3851618Srie  *
3861618Srie  *	For Target Addresses +/- 2GB of the entry
3871618Srie  *	-----------------------------------------
3881618Srie  *
3891618Srie  *	.PLT0 is the address of the first entry in the P.L.T.
3901618Srie  *	This one is filled in by the run-time link editor. We just
3911618Srie  *	have to leave space for it.
3921618Srie  */
3931618Srie static void
plt_entry(Ofl_desc * ofl,Xword pltndx,Xword * roffset,Sxword * raddend)3941618Srie plt_entry(Ofl_desc *ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
3951618Srie {
3966206Sab196087 	uchar_t	*pltent;	/* PLT entry being created. */
3976206Sab196087 	Sxword	pltoff;		/* Offset of this entry from PLT top */
3986206Sab196087 	int	bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
3991618Srie 
4001618Srie 	/*
4011618Srie 	 *  The second part of the V9 ABI (sec. 5.2.4)
4021618Srie 	 *  applies to plt entries greater than 0x8000 (32,768).
4031618Srie 	 *  This is handled in 'plt_far_entry()'
4041618Srie 	 */
4051618Srie 	if ((pltndx - 1 + M_PLT_XNumber) >= M64_PLT_NEARPLTS) {
4061618Srie 		plt_far_entry(ofl, pltndx, roffset, raddend);
4071618Srie 		return;
4081618Srie 	}
4091618Srie 
4101618Srie 	pltoff = M_PLT_RESERVSZ + (pltndx - 1) * M_PLT_ENTSIZE;
4114734Sab196087 	pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf + pltoff;
4121618Srie 
4131618Srie 	*roffset = pltoff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
4141618Srie 	*raddend = 0;
4151618Srie 
4161618Srie 	/*
4171618Srie 	 * PLT[0]: sethi %hi(. - .L0), %g1
4181618Srie 	 */
4191618Srie 	/* LINTED */
4201618Srie 	*(Word *)pltent = M_SETHIG1 | pltoff;
4216206Sab196087 	if (bswap)
4226206Sab196087 		/* LINTED */
4236206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4241618Srie 
4251618Srie 	/*
4261618Srie 	 * PLT[1]: ba,a %xcc, .PLT1 (.PLT1 accessed as a
4271618Srie 	 * PC-relative index of longwords).
4281618Srie 	 */
4291618Srie 	pltent += M_PLT_INSSIZE;
4301618Srie 	pltoff += M_PLT_INSSIZE;
4311618Srie 	pltoff = -pltoff;
4321618Srie 	/* LINTED */
4331618Srie 	*(Word *)pltent = M_BA_A_XCC |
4344734Sab196087 	    (((pltoff + M_PLT_ENTSIZE) >> 2) & S_MASK(19));
4356206Sab196087 	if (bswap)
4366206Sab196087 		/* LINTED */
4376206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4381618Srie 
4391618Srie 	/*
4401618Srie 	 * PLT[2]: sethi 0, %g0 (NOP for delay slot of eventual CTI).
4411618Srie 	 */
4421618Srie 	pltent += M_PLT_INSSIZE;
4431618Srie 	/* LINTED */
4441618Srie 	*(Word *)pltent = M_NOP;
4456206Sab196087 	if (bswap)
4466206Sab196087 		/* LINTED */
4476206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4481618Srie 
4491618Srie 	/*
4501618Srie 	 * PLT[3]: sethi 0, %g0 (NOP for PLT padding).
4511618Srie 	 */
4521618Srie 	pltent += M_PLT_INSSIZE;
4531618Srie 	/* LINTED */
4541618Srie 	*(Word *)pltent = M_NOP;
4556206Sab196087 	if (bswap)
4566206Sab196087 		/* LINTED */
4576206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4581618Srie 
4591618Srie 	/*
4601618Srie 	 * PLT[4]: sethi 0, %g0 (NOP for PLT padding).
4611618Srie 	 */
4621618Srie 	pltent += M_PLT_INSSIZE;
4631618Srie 	/* LINTED */
4641618Srie 	*(Word *)pltent = M_NOP;
4656206Sab196087 	if (bswap)
4666206Sab196087 		/* LINTED */
4676206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4681618Srie 
4691618Srie 	/*
4701618Srie 	 * PLT[5]: sethi 0, %g0 (NOP for PLT padding).
4711618Srie 	 */
4721618Srie 	pltent += M_PLT_INSSIZE;
4731618Srie 	/* LINTED */
4741618Srie 	*(Word *)pltent = M_NOP;
4756206Sab196087 	if (bswap)
4766206Sab196087 		/* LINTED */
4776206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4781618Srie 
4791618Srie 	/*
4801618Srie 	 * PLT[6]: sethi 0, %g0 (NOP for PLT padding).
4811618Srie 	 */
4821618Srie 	pltent += M_PLT_INSSIZE;
4831618Srie 	/* LINTED */
4841618Srie 	*(Word *)pltent = M_NOP;
4856206Sab196087 	if (bswap)
4866206Sab196087 		/* LINTED */
4876206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4881618Srie 
4891618Srie 	/*
4901618Srie 	 * PLT[7]: sethi 0, %g0 (NOP for PLT padding).
4911618Srie 	 */
4921618Srie 	pltent += M_PLT_INSSIZE;
4931618Srie 	/* LINTED */
4941618Srie 	*(Word *)pltent = M_NOP;
4956206Sab196087 	if (bswap)
4966206Sab196087 		/* LINTED */
4976206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4981618Srie }
4991618Srie 
5001618Srie 
5011618Srie #else  /* Elf 32 */
5021618Srie 
5036206Sab196087 static Xword
ld_calc_plt_addr(Sym_desc * sdp,Ofl_desc * ofl)5041618Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
5051618Srie {
5061618Srie 	Xword	value, pltndx;
5071618Srie 
5081618Srie 	pltndx = sdp->sd_aux->sa_PLTndx + M_PLT_XNumber - 1;
5091618Srie 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
5101618Srie 	    (pltndx * M_PLT_ENTSIZE);
5111618Srie 	return (value);
5121618Srie }
5131618Srie 
5141618Srie 
5151618Srie /*
5161618Srie  *	Build a single P.L.T. entry - code is:
5171618Srie  *
5181618Srie  *	sethi	(. - .L0), %g1
5191618Srie  *	ba,a	.L0
5201618Srie  *	sethi	0, %g0		(nop)
5211618Srie  *
5221618Srie  *	.L0 is the address of the first entry in the P.L.T.
5231618Srie  *	This one is filled in by the run-time link editor. We just
5241618Srie  *	have to leave space for it.
5251618Srie  */
5261618Srie static void
plt_entry(Ofl_desc * ofl,Xword pltndx,Xword * roffset,Sxword * raddend)5271618Srie plt_entry(Ofl_desc * ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
5281618Srie {
5299131SRod.Evans@Sun.COM 	Byte	*pltent;	/* PLT entry being created. */
5301618Srie 	Sxword	pltoff;	/* Offset of this entry from PLT top */
5316206Sab196087 	int	bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
5321618Srie 
5331618Srie 	pltoff = M_PLT_RESERVSZ + (pltndx - 1) * M_PLT_ENTSIZE;
5341618Srie 	pltent = (Byte *)ofl->ofl_osplt->os_outdata->d_buf + pltoff;
5351618Srie 
5361618Srie 	*roffset = pltoff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
5371618Srie 	*raddend = 0;
5381618Srie 
5391618Srie 	/*
5401618Srie 	 * PLT[0]: sethi %hi(. - .L0), %g1
5411618Srie 	 */
5421618Srie 	/* LINTED */
5431618Srie 	*(Word *)pltent = M_SETHIG1 | pltoff;
5446206Sab196087 	if (bswap)
5456206Sab196087 		/* LINTED */
5466206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
5471618Srie 
5481618Srie 	/*
5491618Srie 	 * PLT[1]: ba,a .L0 (.L0 accessed as a PC-relative index of longwords)
5501618Srie 	 */
5511618Srie 	pltent += M_PLT_INSSIZE;
5521618Srie 	pltoff += M_PLT_INSSIZE;
5531618Srie 	pltoff = -pltoff;
5541618Srie 	/* LINTED */
5551618Srie 	*(Word *)pltent = M_BA_A | ((pltoff >> 2) & S_MASK(22));
5566206Sab196087 	if (bswap)
5576206Sab196087 		/* LINTED */
5586206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
5591618Srie 
5601618Srie 	/*
5611618Srie 	 * PLT[2]: sethi 0, %g0 (NOP for delay slot of eventual CTI).
5621618Srie 	 */
5631618Srie 	pltent += M_PLT_INSSIZE;
5641618Srie 	/* LINTED */
5651618Srie 	*(Word *)pltent = M_SETHIG0;
5666206Sab196087 	if (bswap)
5676206Sab196087 		/* LINTED */
5686206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
5691618Srie 
5701618Srie 	/*
5711618Srie 	 * PLT[3]: sethi 0, %g0 (NOP for PLT padding).
5721618Srie 	 */
5731618Srie 	pltent += M_PLT_INSSIZE;
5741618Srie 	/* LINTED */
5751618Srie 	*(Word *)pltent = M_SETHIG0;
5766206Sab196087 	if (bswap)
5776206Sab196087 		/* LINTED */
5786206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
5791618Srie }
5801618Srie 
5811618Srie #endif /* _ELF64 */
5821618Srie 
5836206Sab196087 static uintptr_t
ld_perform_outreloc(Rel_desc * orsp,Ofl_desc * ofl,Boolean * remain_seen)584*13074SAli.Bahrami@Oracle.COM ld_perform_outreloc(Rel_desc *orsp, Ofl_desc *ofl, Boolean *remain_seen)
5851618Srie {
5869131SRod.Evans@Sun.COM 	Os_desc		*relosp, *osp = NULL;
5876206Sab196087 	Xword		ndx, roffset, value;
5886206Sab196087 	Sxword		raddend;
5896206Sab196087 	const Rel_entry	*rep;
5906206Sab196087 	Rela		rea;
5916206Sab196087 	char		*relbits;
5929131SRod.Evans@Sun.COM 	Sym_desc	*sdp, *psym = NULL;
5936206Sab196087 	int		sectmoved = 0;
5946206Sab196087 	Word		dtflags1 = ofl->ofl_dtflags_1;
5956299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
5961618Srie 
5971618Srie 	raddend = orsp->rel_raddend;
5981618Srie 	sdp = orsp->rel_sym;
5991618Srie 
6001618Srie 	/*
6011618Srie 	 * Special case, a regsiter symbol associated with symbol
6021618Srie 	 * index 0 is initialized (i.e. relocated) to a constant
6031618Srie 	 * in the r_addend field rather than to a symbol value.
6041618Srie 	 */
6051618Srie 	if ((orsp->rel_rtype == M_R_REGISTER) && !sdp) {
6061618Srie 		relosp = ofl->ofl_osrel;
6071618Srie 		relbits = (char *)relosp->os_outdata->d_buf;
6081618Srie 
6091618Srie 		rea.r_info = ELF_R_INFO(0,
61012155SAli.Bahrami@Sun.COM 		    ELF_R_TYPE_INFO(RELAUX_GET_TYPEDATA(orsp),
61112155SAli.Bahrami@Sun.COM 		    orsp->rel_rtype));
6121618Srie 		rea.r_offset = orsp->rel_roffset;
6131618Srie 		rea.r_addend = raddend;
6141618Srie 		DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea,
61512155SAli.Bahrami@Sun.COM 		    relosp->os_name, ld_reloc_sym_name(orsp)));
6161618Srie 
6171618Srie 		assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
6181618Srie 		(void) memcpy((relbits + relosp->os_szoutrels),
6191618Srie 		    (char *)&rea, sizeof (Rela));
6201618Srie 		relosp->os_szoutrels += (Xword)sizeof (Rela);
6211618Srie 
6221618Srie 		return (1);
6231618Srie 	}
6241618Srie 
6251618Srie 	/*
6261618Srie 	 * If the section this relocation is against has been discarded
6271618Srie 	 * (-zignore), then also discard (skip) the relocation itself.
6281618Srie 	 */
6291618Srie 	if (orsp->rel_isdesc && ((orsp->rel_flags &
6301618Srie 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
6311618Srie 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
6321618Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
6331618Srie 		return (1);
6341618Srie 	}
6351618Srie 
6361618Srie 	/*
6371618Srie 	 * If this is a relocation against a move table, or expanded move
6381618Srie 	 * table, adjust the relocation entries.
6391618Srie 	 */
64012155SAli.Bahrami@Sun.COM 	if (RELAUX_GET_MOVE(orsp))
6411618Srie 		ld_adj_movereloc(ofl, orsp);
6421618Srie 
6431618Srie 	/*
6441618Srie 	 * If this is a relocation against a section then we need to adjust the
6451618Srie 	 * raddend field to compensate for the new position of the input section
6461618Srie 	 * within the new output section.
6471618Srie 	 */
6481618Srie 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
6499131SRod.Evans@Sun.COM 		if (ofl->ofl_parsyms &&
6501618Srie 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
6511618Srie 		    (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
6521618Srie 			/*
6531618Srie 			 * If the symbol is moved, adjust the value
6541618Srie 			 */
6551618Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
6561618Srie 			sectmoved = 1;
6571618Srie 			if (ofl->ofl_flags & FLG_OF_RELOBJ)
6581618Srie 				raddend = psym->sd_sym->st_value;
6591618Srie 			else
6601618Srie 				raddend = psym->sd_sym->st_value -
6611618Srie 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
6621618Srie 			/* LINTED */
6631618Srie 			raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
6641618Srie 			if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
6651618Srie 				raddend +=
6664734Sab196087 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
6671618Srie 		} else {
6681618Srie 			/* LINTED */
6691618Srie 			raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
6701618Srie 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
6711618Srie 				raddend +=
6724734Sab196087 				    sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
6731618Srie 		}
6741618Srie 	}
6751618Srie 
6761618Srie 	value = sdp->sd_sym->st_value;
6771618Srie 
6781618Srie 	if (orsp->rel_flags & FLG_REL_GOT) {
6791618Srie 		osp = ofl->ofl_osgot;
6801618Srie 		roffset = ld_calc_got_offset(orsp, ofl);
6811618Srie 
6821618Srie 	} else if (orsp->rel_flags & FLG_REL_PLT) {
6831618Srie 		osp = ofl->ofl_osplt;
6841618Srie 		plt_entry(ofl, sdp->sd_aux->sa_PLTndx, &roffset, &raddend);
6851618Srie 	} else if (orsp->rel_flags & FLG_REL_BSS) {
6861618Srie 		/*
6871618Srie 		 * This must be a R_SPARC_COPY.  For these set the roffset to
6881618Srie 		 * point to the new symbols location.
6891618Srie 		 */
6901618Srie 		osp = ofl->ofl_isbss->is_osdesc;
6911618Srie 		roffset = (Xword)value;
6921618Srie 
6931618Srie 		/*
6941618Srie 		 * The raddend doesn't mean anything in an R_SPARC_COPY
6951618Srie 		 * relocation.  Null it out because it can confuse people.
6961618Srie 		 */
6971618Srie 		raddend = 0;
6981618Srie 	} else if (orsp->rel_flags & FLG_REL_REG) {
6991618Srie 		/*
7001618Srie 		 * The offsets of relocations against register symbols
7011618Srie 		 * identifiy the register directly - so the offset
7021618Srie 		 * does not need to be adjusted.
7031618Srie 		 */
7041618Srie 		roffset = orsp->rel_roffset;
7051618Srie 	} else {
70612155SAli.Bahrami@Sun.COM 		osp = RELAUX_GET_OSDESC(orsp);
7071618Srie 
7081618Srie 		/*
7091618Srie 		 * Calculate virtual offset of reference point; equals offset
7101618Srie 		 * into section + vaddr of section for loadable sections, or
7111618Srie 		 * offset plus section displacement for nonloadable sections.
7121618Srie 		 */
7131618Srie 		roffset = orsp->rel_roffset +
7141618Srie 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
7151618Srie 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
7161618Srie 			roffset += orsp->rel_isdesc->is_osdesc->
7171618Srie 			    os_shdr->sh_addr;
7181618Srie 	}
7191618Srie 
7201618Srie 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
7211618Srie 		relosp = ofl->ofl_osrel;
7221618Srie 
7231618Srie 	/*
7241618Srie 	 * Verify that the output relocations offset meets the
7251618Srie 	 * alignment requirements of the relocation being processed.
7261618Srie 	 */
7271618Srie 	rep = &reloc_table[orsp->rel_rtype];
7281618Srie 	if (((flags & FLG_OF_RELOBJ) || !(dtflags1 & DF_1_NORELOC)) &&
7291618Srie 	    !(rep->re_flags & FLG_RE_UNALIGN)) {
7301618Srie 		if (((rep->re_fsize == 2) && (roffset & 0x1)) ||
7311618Srie 		    ((rep->re_fsize == 4) && (roffset & 0x3)) ||
7321618Srie 		    ((rep->re_fsize == 8) && (roffset & 0x7))) {
7334734Sab196087 			Conv_inv_buf_t inv_buf;
7344734Sab196087 
735*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NONALIGN),
7364734Sab196087 			    conv_reloc_SPARC_type(orsp->rel_rtype, 0, &inv_buf),
7371618Srie 			    orsp->rel_isdesc->is_file->ifl_name,
73812155SAli.Bahrami@Sun.COM 			    ld_reloc_sym_name(orsp), EC_XWORD(roffset));
7391618Srie 			return (S_ERROR);
7401618Srie 		}
7411618Srie 	}
7421618Srie 
7431618Srie 	/*
7441618Srie 	 * Assign the symbols index for the output relocation.  If the
7451618Srie 	 * relocation refers to a SECTION symbol then it's index is based upon
7461618Srie 	 * the output sections symbols index.  Otherwise the index can be
7471618Srie 	 * derived from the symbols index itself.
7481618Srie 	 */
7491618Srie 	if (orsp->rel_rtype == R_SPARC_RELATIVE)
7501618Srie 		ndx = STN_UNDEF;
7511618Srie 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
7521618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
7531618Srie 		if (sectmoved == 0) {
7541618Srie 			/*
7551618Srie 			 * Check for a null input section. This can
7561618Srie 			 * occur if this relocation references a symbol
7571618Srie 			 * generated by sym_add_sym().
7581618Srie 			 */
7599131SRod.Evans@Sun.COM 			if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
7609131SRod.Evans@Sun.COM 				ndx = sdp->sd_isc->is_osdesc->os_identndx;
7611618Srie 			else
7621618Srie 				ndx = sdp->sd_shndx;
7631618Srie 		} else
7648159SAli.Bahrami@Sun.COM 			ndx = ofl->ofl_parexpnndx;
7651618Srie 	} else
7661618Srie 		ndx = sdp->sd_symndx;
7671618Srie 
7681618Srie 	/*
7691618Srie 	 * Add the symbols 'value' to the addend field.
7701618Srie 	 */
7711618Srie 	if (orsp->rel_flags & FLG_REL_ADVAL)
7721618Srie 		raddend += value;
7731618Srie 
7741618Srie 	/*
7752647Srie 	 * The addend field for R_SPARC_TLS_DTPMOD32 and R_SPARC_TLS_DTPMOD64
7762647Srie 	 * mean nothing.  The addend is propagated in the corresponding
7772647Srie 	 * R_SPARC_TLS_DTPOFF* relocations.
7781618Srie 	 */
7792647Srie 	if (orsp->rel_rtype == M_R_DTPMOD)
7801618Srie 		raddend = 0;
7811618Srie 
7821618Srie 	relbits = (char *)relosp->os_outdata->d_buf;
7831618Srie 
78412155SAli.Bahrami@Sun.COM 	rea.r_info = ELF_R_INFO(ndx,
78512155SAli.Bahrami@Sun.COM 	    ELF_R_TYPE_INFO(RELAUX_GET_TYPEDATA(orsp), orsp->rel_rtype));
7861618Srie 	rea.r_offset = roffset;
7871618Srie 	rea.r_addend = raddend;
7881618Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
78912155SAli.Bahrami@Sun.COM 	    ld_reloc_sym_name(orsp)));
7901618Srie 
7911618Srie 	/*
7921618Srie 	 * Assert we haven't walked off the end of our relocation table.
7931618Srie 	 */
7941618Srie 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
7951618Srie 
7961618Srie 	(void) memcpy((relbits + relosp->os_szoutrels),
7971618Srie 	    (char *)&rea, sizeof (Rela));
7981618Srie 	relosp->os_szoutrels += (Xword)sizeof (Rela);
7991618Srie 
8001618Srie 	/*
8011618Srie 	 * Determine if this relocation is against a non-writable, allocatable
8021618Srie 	 * section.  If so we may need to provide a text relocation diagnostic.
8031618Srie 	 */
804*13074SAli.Bahrami@Oracle.COM 	ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
8051618Srie 	return (1);
8061618Srie }
8071618Srie 
8081618Srie 
8091618Srie /*
8101618Srie  * Sparc Instructions for TLS processing
8111618Srie  */
8121618Srie #if	defined(_ELF64)
8131618Srie #define	TLS_GD_IE_LD	0xd0580000	/* ldx [%g0 + %g0], %o0 */
8141618Srie #else
8151618Srie #define	TLS_GD_IE_LD	0xd0000000	/* ld [%g0 + %g0], %o0 */
8161618Srie #endif
8171618Srie #define	TLS_GD_IE_ADD	0x9001c008	/* add %g7, %o0, %o0 */
8181618Srie 
8191618Srie #define	TLS_GD_LE_XOR	0x80182000	/* xor %g0, 0, %g0 */
8201618Srie #define	TLS_IE_LE_OR	0x80100000	/* or %g0, %o0, %o1 */
8211618Srie 					/*  synthetic: mov %g0, %g0 */
8221618Srie 
8231618Srie #define	TLS_LD_LE_CLRO0	0x90100000	/* clr	%o0 */
8241618Srie 
8251618Srie #define	FM3_REG_MSK_RD	(0x1f << 25)	/* Formate (3) rd register mask */
8261618Srie 					/*	bits 25->29 */
8271618Srie #define	FM3_REG_MSK_RS1	(0x1f << 14)	/* Formate (3) rs1 register mask */
8281618Srie 					/*	bits 14->18 */
8291618Srie #define	FM3_REG_MSK_RS2	0x1f		/* Formate (3) rs2 register mask */
8301618Srie 					/*	bits 0->4 */
8311618Srie 
8321618Srie #define	REG_G7		7		/* %g7 register */
8331618Srie 
8341618Srie static Fixupret
tls_fixups(Ofl_desc * ofl,Rel_desc * arsp)8351618Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
8361618Srie {
8371618Srie 	Sym_desc	*sdp = arsp->rel_sym;
8381618Srie 	Word		rtype = arsp->rel_rtype;
8396206Sab196087 	Word		*offset, w;
8406206Sab196087 	int		bswap = OFL_SWAP_RELOC_DATA(ofl, arsp);
8411618Srie 
8426206Sab196087 
8436206Sab196087 	offset = (Word *)((uintptr_t)arsp->rel_roffset +
8444734Sab196087 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
84512155SAli.Bahrami@Sun.COM 	    (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
8461618Srie 
8471618Srie 	if (sdp->sd_ref == REF_DYN_NEED) {
8481618Srie 		/*
8491618Srie 		 * IE reference model
8501618Srie 		 */
8511618Srie 		switch (rtype) {
8521618Srie 		case R_SPARC_TLS_GD_HI22:
8531618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
85412155SAli.Bahrami@Sun.COM 			    R_SPARC_TLS_IE_HI22, arsp,
85512155SAli.Bahrami@Sun.COM 			    ld_reloc_sym_name));
8561618Srie 			arsp->rel_rtype = R_SPARC_TLS_IE_HI22;
8571618Srie 			return (FIX_RELOC);
8581618Srie 
8591618Srie 		case R_SPARC_TLS_GD_LO10:
8601618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
86112155SAli.Bahrami@Sun.COM 			    R_SPARC_TLS_IE_LO10, arsp,
86212155SAli.Bahrami@Sun.COM 			    ld_reloc_sym_name));
8631618Srie 			arsp->rel_rtype = R_SPARC_TLS_IE_LO10;
8641618Srie 			return (FIX_RELOC);
8651618Srie 
8661618Srie 		case R_SPARC_TLS_GD_ADD:
8671618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
86812155SAli.Bahrami@Sun.COM 			    R_SPARC_NONE, arsp, ld_reloc_sym_name));
8696206Sab196087 			w = bswap ? ld_bswap_Word(*offset) : *offset;
8706206Sab196087 			w = (TLS_GD_IE_LD |
8716206Sab196087 			    (w & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RS2)));
8726206Sab196087 			*offset = bswap ? ld_bswap_Word(w) : w;
8731618Srie 			return (FIX_DONE);
8741618Srie 
8751618Srie 		case R_SPARC_TLS_GD_CALL:
8761618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
87712155SAli.Bahrami@Sun.COM 			    R_SPARC_NONE, arsp, ld_reloc_sym_name));
8781618Srie 			*offset = TLS_GD_IE_ADD;
8796206Sab196087 			if (bswap)
8806206Sab196087 				*offset = ld_bswap_Word(*offset);
8811618Srie 			return (FIX_DONE);
8821618Srie 		}
8831618Srie 		return (FIX_RELOC);
8841618Srie 	}
8851618Srie 
8861618Srie 	/*
8871618Srie 	 * LE reference model
8881618Srie 	 */
8891618Srie 	switch (rtype) {
8901618Srie 	case R_SPARC_TLS_IE_HI22:
8911618Srie 	case R_SPARC_TLS_GD_HI22:
8921618Srie 	case R_SPARC_TLS_LDO_HIX22:
8933304Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
89412155SAli.Bahrami@Sun.COM 		    R_SPARC_TLS_LE_HIX22, arsp, ld_reloc_sym_name));
8951618Srie 		arsp->rel_rtype = R_SPARC_TLS_LE_HIX22;
8961618Srie 		return (FIX_RELOC);
8971618Srie 
8981618Srie 	case R_SPARC_TLS_LDO_LOX10:
8993304Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
90012155SAli.Bahrami@Sun.COM 		    R_SPARC_TLS_LE_LOX10, arsp, ld_reloc_sym_name));
9011618Srie 		arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
9021618Srie 		return (FIX_RELOC);
9031618Srie 
9041618Srie 	case R_SPARC_TLS_IE_LO10:
9051618Srie 	case R_SPARC_TLS_GD_LO10:
9061618Srie 		/*
9071618Srie 		 * Current instruction is:
9081618Srie 		 *
9091618Srie 		 *	or r1, %lo(x), r2
9101618Srie 		 *		or
9111618Srie 		 *	add r1, %lo(x), r2
9121618Srie 		 *
9131618Srie 		 * Need to udpate this to:
9141618Srie 		 *
9151618Srie 		 *	xor r1, %lox(x), r2
9161618Srie 		 */
9173304Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
91812155SAli.Bahrami@Sun.COM 		    R_SPARC_TLS_LE_LOX10, arsp, ld_reloc_sym_name));
9196206Sab196087 		w = bswap ? ld_bswap_Word(*offset) : *offset;
9206206Sab196087 		w = TLS_GD_LE_XOR |
9216206Sab196087 		    (w & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RD));
9226206Sab196087 		*offset = bswap ? ld_bswap_Word(w) : w;
9231618Srie 		arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
9241618Srie 		return (FIX_RELOC);
9251618Srie 
9261618Srie 	case R_SPARC_TLS_IE_LD:
9271618Srie 	case R_SPARC_TLS_IE_LDX:
9281618Srie 		/*
9291618Srie 		 * Current instruction:
9301618Srie 		 * 	ld{x}	[r1 + r2], r3
9311618Srie 		 *
9321618Srie 		 * Need to update this to:
9331618Srie 		 *
9341618Srie 		 *	mov	r2, r3   (or  %g0, r2, r3)
9351618Srie 		 */
9363304Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
93712155SAli.Bahrami@Sun.COM 		    R_SPARC_NONE, arsp, ld_reloc_sym_name));
9386206Sab196087 		w = bswap ? ld_bswap_Word(*offset) : *offset;
9396206Sab196087 		w = (w & (FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | TLS_IE_LE_OR;
9406206Sab196087 		*offset = bswap ? ld_bswap_Word(w) : w;
9411618Srie 		return (FIX_DONE);
9421618Srie 
9431618Srie 	case R_SPARC_TLS_LDO_ADD:
9441618Srie 	case R_SPARC_TLS_GD_ADD:
9451618Srie 		/*
9461618Srie 		 * Current instruction is:
9471618Srie 		 *
9481618Srie 		 *	add gptr_reg, r2, r3
9491618Srie 		 *
9501618Srie 		 * Need to updated this to:
9511618Srie 		 *
9521618Srie 		 *	add %g7, r2, r3
9531618Srie 		 */
9543304Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
95512155SAli.Bahrami@Sun.COM 		    R_SPARC_NONE, arsp, ld_reloc_sym_name));
9566206Sab196087 		w = bswap ? ld_bswap_Word(*offset) : *offset;
9576206Sab196087 		w = w & (~FM3_REG_MSK_RS1);
9586206Sab196087 		w = w | (REG_G7 << 14);
9596206Sab196087 		*offset = bswap ? ld_bswap_Word(w) : w;
9601618Srie 		return (FIX_DONE);
9611618Srie 
9621618Srie 	case R_SPARC_TLS_LDM_CALL:
9633304Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
96412155SAli.Bahrami@Sun.COM 		    R_SPARC_NONE, arsp, ld_reloc_sym_name));
9651618Srie 		*offset = TLS_LD_LE_CLRO0;
9666206Sab196087 		if (bswap)
9676206Sab196087 			*offset = ld_bswap_Word(*offset);
9681618Srie 		return (FIX_DONE);
9691618Srie 
9701618Srie 	case R_SPARC_TLS_LDM_HI22:
9711618Srie 	case R_SPARC_TLS_LDM_LO10:
9721618Srie 	case R_SPARC_TLS_LDM_ADD:
9731618Srie 	case R_SPARC_TLS_IE_ADD:
9741618Srie 	case R_SPARC_TLS_GD_CALL:
9753304Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
97612155SAli.Bahrami@Sun.COM 		    R_SPARC_NONE, arsp, ld_reloc_sym_name));
9771618Srie 		*offset = M_NOP;
9786206Sab196087 		if (bswap)
9796206Sab196087 			*offset = ld_bswap_Word(*offset);
9801618Srie 		return (FIX_DONE);
9811618Srie 	}
9821618Srie 	return (FIX_RELOC);
9831618Srie }
9841618Srie 
9851618Srie #define	GOTOP_ADDINST	0x80000000	/* add %g0, %g0, %g0 */
9861618Srie 
9871618Srie static Fixupret
gotop_fixups(Ofl_desc * ofl,Rel_desc * arsp)9881618Srie gotop_fixups(Ofl_desc *ofl, Rel_desc *arsp)
9891618Srie {
9901618Srie 	Word		rtype = arsp->rel_rtype;
9916206Sab196087 	Word		*offset, w;
9921618Srie 	const char	*ifl_name;
9934734Sab196087 	Conv_inv_buf_t	inv_buf;
9946206Sab196087 	int		bswap;
9951618Srie 
9961618Srie 	switch (rtype) {
9971618Srie 	case R_SPARC_GOTDATA_OP_HIX22:
9983304Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
99912155SAli.Bahrami@Sun.COM 		    R_SPARC_GOTDATA_HIX22, arsp, ld_reloc_sym_name));
10001618Srie 		arsp->rel_rtype = R_SPARC_GOTDATA_HIX22;
10011618Srie 		return (FIX_RELOC);
10021618Srie 
10031618Srie 	case R_SPARC_GOTDATA_OP_LOX10:
10043304Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
100512155SAli.Bahrami@Sun.COM 		    R_SPARC_GOTDATA_LOX10, arsp, ld_reloc_sym_name));
10061618Srie 		arsp->rel_rtype = R_SPARC_GOTDATA_LOX10;
10071618Srie 		return (FIX_RELOC);
10081618Srie 
10091618Srie 	case R_SPARC_GOTDATA_OP:
10101618Srie 		/*
10111618Srie 		 * Current instruction:
10121618Srie 		 * 	ld{x}	[r1 + r2], r3
10131618Srie 		 *
10141618Srie 		 * Need to update this to:
10151618Srie 		 *
10161618Srie 		 *	add	r1, r2, r3
10171618Srie 		 */
10183304Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
101912155SAli.Bahrami@Sun.COM 		    R_SPARC_NONE, arsp, ld_reloc_sym_name));
10206206Sab196087 		offset = (Word *)(uintptr_t)(arsp->rel_roffset +
10211618Srie 		    _elf_getxoff(arsp->rel_isdesc->is_indata) +
102212155SAli.Bahrami@Sun.COM 		    (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
10236206Sab196087 		bswap = OFL_SWAP_RELOC_DATA(ofl, arsp);
10246206Sab196087 		w = bswap ? ld_bswap_Word(*offset) : *offset;
10256206Sab196087 		w = (w & (FM3_REG_MSK_RS1 |
10261618Srie 		    FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | GOTOP_ADDINST;
10276206Sab196087 		*offset = bswap ? ld_bswap_Word(w) : w;
10281618Srie 		return (FIX_DONE);
10291618Srie 	}
10301618Srie 	/*
10311618Srie 	 * We should not get here
10321618Srie 	 */
10331618Srie 	if (arsp->rel_isdesc->is_file)
10341618Srie 		ifl_name = arsp->rel_isdesc->is_file->ifl_name;
10351618Srie 	else
10361618Srie 		ifl_name = MSG_INTL(MSG_STR_NULL);
10371618Srie 
1038*13074SAli.Bahrami@Oracle.COM 	ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADGOTFIX),
10394734Sab196087 	    conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
104012155SAli.Bahrami@Sun.COM 	    ifl_name, ld_reloc_sym_name(arsp));
10411618Srie 
10421618Srie 	assert(0);
10431618Srie 	return (FIX_ERROR);
10441618Srie }
10451618Srie 
10466206Sab196087 static uintptr_t
ld_do_activerelocs(Ofl_desc * ofl)10471618Srie ld_do_activerelocs(Ofl_desc *ofl)
10481618Srie {
10491618Srie 	Rel_desc	*arsp;
105012155SAli.Bahrami@Sun.COM 	Rel_cachebuf	*rcbp;
10519131SRod.Evans@Sun.COM 	Aliste		idx;
10521618Srie 	uintptr_t	return_code = 1;
10536299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
10541618Srie 
105512155SAli.Bahrami@Sun.COM 	if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
10562647Srie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
10572647Srie 
10581618Srie 	/*
10591618Srie 	 * Process active relocations.
10601618Srie 	 */
106112155SAli.Bahrami@Sun.COM 	REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
106212155SAli.Bahrami@Sun.COM 		uchar_t		*addr;
106312155SAli.Bahrami@Sun.COM 		Xword		value;
106412155SAli.Bahrami@Sun.COM 		Sym_desc	*sdp;
106512155SAli.Bahrami@Sun.COM 		const char	*ifl_name;
106612155SAli.Bahrami@Sun.COM 		Xword		refaddr;
106712155SAli.Bahrami@Sun.COM 		Os_desc		*osp;
106812155SAli.Bahrami@Sun.COM 
106912155SAli.Bahrami@Sun.COM 		/*
107012155SAli.Bahrami@Sun.COM 		 * If the section this relocation is against has been discarded
107112155SAli.Bahrami@Sun.COM 		 * (-zignore), then discard (skip) the relocation itself.
107212155SAli.Bahrami@Sun.COM 		 */
107312155SAli.Bahrami@Sun.COM 		if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
107412155SAli.Bahrami@Sun.COM 		    ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
107512155SAli.Bahrami@Sun.COM 		    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
107612155SAli.Bahrami@Sun.COM 			DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
107712155SAli.Bahrami@Sun.COM 			continue;
107812155SAli.Bahrami@Sun.COM 		}
107912155SAli.Bahrami@Sun.COM 
108012155SAli.Bahrami@Sun.COM 		/*
108112155SAli.Bahrami@Sun.COM 		 * Perform any required TLS fixups.
108212155SAli.Bahrami@Sun.COM 		 */
108312155SAli.Bahrami@Sun.COM 		if (arsp->rel_flags & FLG_REL_TLSFIX) {
108412155SAli.Bahrami@Sun.COM 			Fixupret	ret;
10851618Srie 
108612155SAli.Bahrami@Sun.COM 			if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
108712155SAli.Bahrami@Sun.COM 				return (S_ERROR);
108812155SAli.Bahrami@Sun.COM 			if (ret == FIX_DONE)
108912155SAli.Bahrami@Sun.COM 				continue;
109012155SAli.Bahrami@Sun.COM 		}
109112155SAli.Bahrami@Sun.COM 
109212155SAli.Bahrami@Sun.COM 		/*
109312155SAli.Bahrami@Sun.COM 		 * Perform any required GOTOP fixups.
109412155SAli.Bahrami@Sun.COM 		 */
109512155SAli.Bahrami@Sun.COM 		if (arsp->rel_flags & FLG_REL_GOTFIX) {
109612155SAli.Bahrami@Sun.COM 			Fixupret	ret;
109712155SAli.Bahrami@Sun.COM 
109812155SAli.Bahrami@Sun.COM 			if ((ret = gotop_fixups(ofl, arsp)) == FIX_ERROR)
109912155SAli.Bahrami@Sun.COM 				return (S_ERROR);
110012155SAli.Bahrami@Sun.COM 			if (ret == FIX_DONE)
11011618Srie 				continue;
110212155SAli.Bahrami@Sun.COM 		}
110312155SAli.Bahrami@Sun.COM 
110412155SAli.Bahrami@Sun.COM 		/*
110512155SAli.Bahrami@Sun.COM 		 * If this is a relocation against the move table, or
110612155SAli.Bahrami@Sun.COM 		 * expanded move table, adjust the relocation entries.
110712155SAli.Bahrami@Sun.COM 		 */
110812155SAli.Bahrami@Sun.COM 		if (RELAUX_GET_MOVE(arsp))
110912155SAli.Bahrami@Sun.COM 			ld_adj_movereloc(ofl, arsp);
111012155SAli.Bahrami@Sun.COM 
111112155SAli.Bahrami@Sun.COM 		sdp = arsp->rel_sym;
111212155SAli.Bahrami@Sun.COM 		refaddr = arsp->rel_roffset +
111312155SAli.Bahrami@Sun.COM 		    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
111412155SAli.Bahrami@Sun.COM 
111512155SAli.Bahrami@Sun.COM 		if ((arsp->rel_flags & FLG_REL_CLVAL) ||
111612155SAli.Bahrami@Sun.COM 		    (arsp->rel_flags & FLG_REL_GOTCL))
111712155SAli.Bahrami@Sun.COM 			value = 0;
111812155SAli.Bahrami@Sun.COM 		else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
111912155SAli.Bahrami@Sun.COM 			Sym_desc	*sym;
11201618Srie 
11211618Srie 			/*
112212155SAli.Bahrami@Sun.COM 			 * The value for a symbol pointing to a SECTION
112312155SAli.Bahrami@Sun.COM 			 * is based off of that sections position.
11241618Srie 			 */
112512155SAli.Bahrami@Sun.COM 			if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
112612155SAli.Bahrami@Sun.COM 			    (sym = ld_am_I_partial(arsp, arsp->rel_raddend))) {
112712155SAli.Bahrami@Sun.COM 				/*
112812155SAli.Bahrami@Sun.COM 				 * The symbol was moved, so adjust the value
112912155SAli.Bahrami@Sun.COM 				 * relative to the new section.
113012155SAli.Bahrami@Sun.COM 				 */
113112155SAli.Bahrami@Sun.COM 				value = _elf_getxoff(sym->sd_isc->is_indata);
113212155SAli.Bahrami@Sun.COM 				if (sym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
113312155SAli.Bahrami@Sun.COM 					value += sym->sd_isc->
113412155SAli.Bahrami@Sun.COM 					    is_osdesc->os_shdr->sh_addr;
11351618Srie 
11361618Srie 				/*
113712155SAli.Bahrami@Sun.COM 				 * The original raddend covers the displacement
113812155SAli.Bahrami@Sun.COM 				 * from the section start to the desired
113912155SAli.Bahrami@Sun.COM 				 * address. The value computed above gets us
114012155SAli.Bahrami@Sun.COM 				 * from the section start to the start of the
114112155SAli.Bahrami@Sun.COM 				 * symbol range. Adjust the old raddend to
114212155SAli.Bahrami@Sun.COM 				 * remove the offset from section start to
114312155SAli.Bahrami@Sun.COM 				 * symbol start, leaving the displacement
114412155SAli.Bahrami@Sun.COM 				 * within the range of the symbol.
11451618Srie 				 */
114612155SAli.Bahrami@Sun.COM 				arsp->rel_raddend -= sym->sd_osym->st_value;
114712155SAli.Bahrami@Sun.COM 			} else {
114812155SAli.Bahrami@Sun.COM 				value = _elf_getxoff(sdp->sd_isc->is_indata);
114912155SAli.Bahrami@Sun.COM 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
115012155SAli.Bahrami@Sun.COM 					value += sdp->sd_isc->
115112155SAli.Bahrami@Sun.COM 					    is_osdesc->os_shdr->sh_addr;
11521618Srie 			}
11531618Srie 
115412155SAli.Bahrami@Sun.COM 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
115512155SAli.Bahrami@Sun.COM 				value -= ofl->ofl_tlsphdr->p_vaddr;
115612155SAli.Bahrami@Sun.COM 
115712155SAli.Bahrami@Sun.COM 		} else if (IS_SIZE(arsp->rel_rtype)) {
11581618Srie 			/*
115912155SAli.Bahrami@Sun.COM 			 * Size relocations require the symbols size.
116012155SAli.Bahrami@Sun.COM 			 */
116112155SAli.Bahrami@Sun.COM 			value = sdp->sd_sym->st_size;
116212155SAli.Bahrami@Sun.COM 
116312155SAli.Bahrami@Sun.COM 		} else if ((sdp->sd_flags & FLG_SY_CAP) &&
116412155SAli.Bahrami@Sun.COM 		    sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
116512155SAli.Bahrami@Sun.COM 			/*
116612155SAli.Bahrami@Sun.COM 			 * If relocation is against a capabilities symbol, we
116712155SAli.Bahrami@Sun.COM 			 * need to jump to an associated PLT, so that at runtime
116812155SAli.Bahrami@Sun.COM 			 * ld.so.1 is involved to determine the best binding
116912155SAli.Bahrami@Sun.COM 			 * choice. Otherwise, the value is the symbols value.
11701618Srie 			 */
117112155SAli.Bahrami@Sun.COM 			value = ld_calc_plt_addr(sdp, ofl);
117212155SAli.Bahrami@Sun.COM 
117312155SAli.Bahrami@Sun.COM 		} else
117412155SAli.Bahrami@Sun.COM 			value = sdp->sd_sym->st_value;
117512155SAli.Bahrami@Sun.COM 
117612155SAli.Bahrami@Sun.COM 		/*
117712155SAli.Bahrami@Sun.COM 		 * Relocation against the GLOBAL_OFFSET_TABLE.
117812155SAli.Bahrami@Sun.COM 		 */
117912155SAli.Bahrami@Sun.COM 		if ((arsp->rel_flags & FLG_REL_GOT) &&
118012155SAli.Bahrami@Sun.COM 		    !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
118112155SAli.Bahrami@Sun.COM 			return (S_ERROR);
118212155SAli.Bahrami@Sun.COM 		osp = RELAUX_GET_OSDESC(arsp);
118312155SAli.Bahrami@Sun.COM 
118412155SAli.Bahrami@Sun.COM 		/*
118512155SAli.Bahrami@Sun.COM 		 * If loadable and not producing a relocatable object add the
118612155SAli.Bahrami@Sun.COM 		 * sections virtual address to the reference address.
118712155SAli.Bahrami@Sun.COM 		 */
118812155SAli.Bahrami@Sun.COM 		if ((arsp->rel_flags & FLG_REL_LOAD) &&
118912155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0))
119012155SAli.Bahrami@Sun.COM 			refaddr +=
119112155SAli.Bahrami@Sun.COM 			    arsp->rel_isdesc->is_osdesc->os_shdr->sh_addr;
119212155SAli.Bahrami@Sun.COM 
119312155SAli.Bahrami@Sun.COM 		/*
119412155SAli.Bahrami@Sun.COM 		 * If this entry has a PLT assigned to it, its value is actually
119512155SAli.Bahrami@Sun.COM 		 * the address of the PLT (and not the address of the function).
119612155SAli.Bahrami@Sun.COM 		 */
119712155SAli.Bahrami@Sun.COM 		if (IS_PLT(arsp->rel_rtype)) {
119812155SAli.Bahrami@Sun.COM 			if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
119912155SAli.Bahrami@Sun.COM 				value = ld_calc_plt_addr(sdp, ofl);
120012155SAli.Bahrami@Sun.COM 		}
120112155SAli.Bahrami@Sun.COM 
120212155SAli.Bahrami@Sun.COM 		/*
120312155SAli.Bahrami@Sun.COM 		 * Add relocations addend to value.  Add extra
120412155SAli.Bahrami@Sun.COM 		 * relocation addend if needed.
120512155SAli.Bahrami@Sun.COM 		 */
120612155SAli.Bahrami@Sun.COM 		value += arsp->rel_raddend;
120712155SAli.Bahrami@Sun.COM 		if (IS_EXTOFFSET(arsp->rel_rtype))
120812155SAli.Bahrami@Sun.COM 			value += RELAUX_GET_TYPEDATA(arsp);
120912155SAli.Bahrami@Sun.COM 
121012155SAli.Bahrami@Sun.COM 		/*
121112155SAli.Bahrami@Sun.COM 		 * Determine whether the value needs further adjustment. Filter
121212155SAli.Bahrami@Sun.COM 		 * through the attributes of the relocation to determine what
121312155SAli.Bahrami@Sun.COM 		 * adjustment is required.  Note, many of the following cases
121412155SAli.Bahrami@Sun.COM 		 * are only applicable when a .got is present.  As a .got is
121512155SAli.Bahrami@Sun.COM 		 * not generated when a relocatable object is being built,
121612155SAli.Bahrami@Sun.COM 		 * any adjustments that require a .got need to be skipped.
121712155SAli.Bahrami@Sun.COM 		 */
121812155SAli.Bahrami@Sun.COM 		if ((arsp->rel_flags & FLG_REL_GOT) &&
121912155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
122012155SAli.Bahrami@Sun.COM 			Xword		R1addr;
122112155SAli.Bahrami@Sun.COM 			uintptr_t	R2addr;
122212155SAli.Bahrami@Sun.COM 			Sword		gotndx;
122312155SAli.Bahrami@Sun.COM 			Gotndx		*gnp;
122412155SAli.Bahrami@Sun.COM 			Gotref		gref;
12251618Srie 
12261618Srie 			/*
122712155SAli.Bahrami@Sun.COM 			 * Clear the GOT table entry, on SPARC we clear
122812155SAli.Bahrami@Sun.COM 			 * the entry and the 'value' if needed is stored
122912155SAli.Bahrami@Sun.COM 			 * in an output relocations addend.
123012155SAli.Bahrami@Sun.COM 			 *
123112155SAli.Bahrami@Sun.COM 			 * Calculate offset into GOT at which to apply
123212155SAli.Bahrami@Sun.COM 			 * the relocation.
12331618Srie 			 */
123412155SAli.Bahrami@Sun.COM 			if (arsp->rel_flags & FLG_REL_DTLS)
123512155SAli.Bahrami@Sun.COM 				gref = GOT_REF_TLSGD;
123612155SAli.Bahrami@Sun.COM 			else if (arsp->rel_flags & FLG_REL_MTLS)
123712155SAli.Bahrami@Sun.COM 				gref = GOT_REF_TLSLD;
123812155SAli.Bahrami@Sun.COM 			else if (arsp->rel_flags & FLG_REL_STLS)
123912155SAli.Bahrami@Sun.COM 				gref = GOT_REF_TLSIE;
124012155SAli.Bahrami@Sun.COM 			else
124112155SAli.Bahrami@Sun.COM 				gref = GOT_REF_GENERIC;
12421618Srie 
124312155SAli.Bahrami@Sun.COM 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
124412155SAli.Bahrami@Sun.COM 			assert(gnp);
12451618Srie 
124612155SAli.Bahrami@Sun.COM 			if (arsp->rel_rtype == M_R_DTPOFF)
124712155SAli.Bahrami@Sun.COM 				gotndx = gnp->gn_gotndx + 1;
124812155SAli.Bahrami@Sun.COM 			else
124912155SAli.Bahrami@Sun.COM 				gotndx = gnp->gn_gotndx;
12501618Srie 
125112155SAli.Bahrami@Sun.COM 			/* LINTED */
125212155SAli.Bahrami@Sun.COM 			R1addr = (Xword)((-neggotoffset * M_GOT_ENTSIZE) +
125312155SAli.Bahrami@Sun.COM 			    (gotndx * M_GOT_ENTSIZE));
12541618Srie 
125512155SAli.Bahrami@Sun.COM 			/*
125612155SAli.Bahrami@Sun.COM 			 * Add the GOTs data's offset.
125712155SAli.Bahrami@Sun.COM 			 */
125812155SAli.Bahrami@Sun.COM 			R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
12591618Srie 
126012155SAli.Bahrami@Sun.COM 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
126112155SAli.Bahrami@Sun.COM 			    ELF_DBG_LD_ACT, M_MACH, SHT_RELA,
126212155SAli.Bahrami@Sun.COM 			    arsp, R1addr, value, ld_reloc_sym_name));
12631618Srie 
12641618Srie 			/*
126512155SAli.Bahrami@Sun.COM 			 * And do it.
12661618Srie 			 */
126712155SAli.Bahrami@Sun.COM 			if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
126812155SAli.Bahrami@Sun.COM 				*(Xword *)R2addr = ld_bswap_Xword(value);
126912155SAli.Bahrami@Sun.COM 			else
127012155SAli.Bahrami@Sun.COM 				*(Xword *)R2addr = value;
127112155SAli.Bahrami@Sun.COM 			continue;
127212155SAli.Bahrami@Sun.COM 
127312155SAli.Bahrami@Sun.COM 		} else if (IS_GOT_BASED(arsp->rel_rtype) &&
127412155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
127512155SAli.Bahrami@Sun.COM 			value -= (ofl->ofl_osgot->os_shdr->sh_addr +
127612155SAli.Bahrami@Sun.COM 			    (-neggotoffset * M_GOT_ENTSIZE));
127712155SAli.Bahrami@Sun.COM 
127812155SAli.Bahrami@Sun.COM 		} else if (IS_PC_RELATIVE(arsp->rel_rtype)) {
127912155SAli.Bahrami@Sun.COM 			value -= refaddr;
128012155SAli.Bahrami@Sun.COM 
128112155SAli.Bahrami@Sun.COM 		} else if (IS_TLS_INS(arsp->rel_rtype) &&
128212155SAli.Bahrami@Sun.COM 		    IS_GOT_RELATIVE(arsp->rel_rtype) &&
128312155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
128412155SAli.Bahrami@Sun.COM 			Gotndx	*gnp;
128512155SAli.Bahrami@Sun.COM 			Gotref	gref;
12864734Sab196087 
128712155SAli.Bahrami@Sun.COM 			if (arsp->rel_flags & FLG_REL_STLS)
128812155SAli.Bahrami@Sun.COM 				gref = GOT_REF_TLSIE;
128912155SAli.Bahrami@Sun.COM 			else if (arsp->rel_flags & FLG_REL_DTLS)
129012155SAli.Bahrami@Sun.COM 				gref = GOT_REF_TLSGD;
129112155SAli.Bahrami@Sun.COM 			else if (arsp->rel_flags & FLG_REL_MTLS)
129212155SAli.Bahrami@Sun.COM 				gref = GOT_REF_TLSLD;
129312155SAli.Bahrami@Sun.COM 
129412155SAli.Bahrami@Sun.COM 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
129512155SAli.Bahrami@Sun.COM 			assert(gnp);
129612155SAli.Bahrami@Sun.COM 
129712155SAli.Bahrami@Sun.COM 			value = gnp->gn_gotndx * M_GOT_ENTSIZE;
129812155SAli.Bahrami@Sun.COM 
129912155SAli.Bahrami@Sun.COM 		} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
130012155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
130112155SAli.Bahrami@Sun.COM 			Gotndx	*gnp;
130212155SAli.Bahrami@Sun.COM 
130312155SAli.Bahrami@Sun.COM 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
130412155SAli.Bahrami@Sun.COM 			    GOT_REF_GENERIC, ofl, arsp);
130512155SAli.Bahrami@Sun.COM 			assert(gnp);
130612155SAli.Bahrami@Sun.COM 
130712155SAli.Bahrami@Sun.COM 			value = gnp->gn_gotndx * M_GOT_ENTSIZE;
130812155SAli.Bahrami@Sun.COM 
130912155SAli.Bahrami@Sun.COM 		} else if ((arsp->rel_flags & FLG_REL_STLS) &&
131012155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
131112155SAli.Bahrami@Sun.COM 			Xword	tlsstatsize;
13121618Srie 
13131618Srie 			/*
131412155SAli.Bahrami@Sun.COM 			 * This is the LE TLS reference model. Static offset is
131512155SAli.Bahrami@Sun.COM 			 * hard-coded, and negated so that it can be added to
131612155SAli.Bahrami@Sun.COM 			 * the thread pointer (%g7)
13171618Srie 			 */
131812155SAli.Bahrami@Sun.COM 			tlsstatsize =
131912155SAli.Bahrami@Sun.COM 			    S_ROUND(ofl->ofl_tlsphdr->p_memsz, M_TLSSTATALIGN);
132012155SAli.Bahrami@Sun.COM 			value = -(tlsstatsize - value);
132112155SAli.Bahrami@Sun.COM 		}
13221618Srie 
132312155SAli.Bahrami@Sun.COM 		if (arsp->rel_isdesc->is_file)
132412155SAli.Bahrami@Sun.COM 			ifl_name = arsp->rel_isdesc->is_file->ifl_name;
132512155SAli.Bahrami@Sun.COM 		else
132612155SAli.Bahrami@Sun.COM 			ifl_name = MSG_INTL(MSG_STR_NULL);
13271618Srie 
132812155SAli.Bahrami@Sun.COM 		/*
132912155SAli.Bahrami@Sun.COM 		 * Make sure we have data to relocate.  Compiler and assembler
133012155SAli.Bahrami@Sun.COM 		 * developers have been known to generate relocations against
133112155SAli.Bahrami@Sun.COM 		 * invalid sections (normally .bss), so for their benefit give
133212155SAli.Bahrami@Sun.COM 		 * them sufficient information to help analyze the problem.
133312155SAli.Bahrami@Sun.COM 		 * End users should never see this.
133412155SAli.Bahrami@Sun.COM 		 */
133512155SAli.Bahrami@Sun.COM 		if (arsp->rel_isdesc->is_indata->d_buf == 0) {
133612155SAli.Bahrami@Sun.COM 			Conv_inv_buf_t	inv_buf;
13371618Srie 
1338*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
133912155SAli.Bahrami@Sun.COM 			    conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
134012155SAli.Bahrami@Sun.COM 			    ifl_name, ld_reloc_sym_name(arsp),
134112155SAli.Bahrami@Sun.COM 			    EC_WORD(arsp->rel_isdesc->is_scnndx),
134212155SAli.Bahrami@Sun.COM 			    arsp->rel_isdesc->is_name);
134312155SAli.Bahrami@Sun.COM 			return (S_ERROR);
134412155SAli.Bahrami@Sun.COM 		}
134512155SAli.Bahrami@Sun.COM 
134612155SAli.Bahrami@Sun.COM 		/*
134712155SAli.Bahrami@Sun.COM 		 * Get the address of the data item we need to modify.
134812155SAli.Bahrami@Sun.COM 		 */
134912155SAli.Bahrami@Sun.COM 		addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
135012155SAli.Bahrami@Sun.COM 		    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
13511618Srie 
135212155SAli.Bahrami@Sun.COM 		DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
135312155SAli.Bahrami@Sun.COM 		    M_MACH, SHT_RELA, arsp, EC_NATPTR(addr), value,
135412155SAli.Bahrami@Sun.COM 		    ld_reloc_sym_name));
135512155SAli.Bahrami@Sun.COM 		addr += (uintptr_t)osp->os_outdata->d_buf;
135612155SAli.Bahrami@Sun.COM 
135712155SAli.Bahrami@Sun.COM 		if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
135812155SAli.Bahrami@Sun.COM 		    ofl->ofl_size) || (arsp->rel_roffset >
135912155SAli.Bahrami@Sun.COM 		    osp->os_shdr->sh_size)) {
136012155SAli.Bahrami@Sun.COM 			Conv_inv_buf_t	inv_buf;
136112155SAli.Bahrami@Sun.COM 			int		class;
136212155SAli.Bahrami@Sun.COM 
136312155SAli.Bahrami@Sun.COM 			if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
136412155SAli.Bahrami@Sun.COM 			    ofl->ofl_size)
136512155SAli.Bahrami@Sun.COM 				class = ERR_FATAL;
136612155SAli.Bahrami@Sun.COM 			else
136712155SAli.Bahrami@Sun.COM 				class = ERR_WARNING;
13681618Srie 
1369*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
137012155SAli.Bahrami@Sun.COM 			    conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
137112155SAli.Bahrami@Sun.COM 			    ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
137212155SAli.Bahrami@Sun.COM 			    arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
137312155SAli.Bahrami@Sun.COM 			    EC_ADDR((uintptr_t)addr -
137412155SAli.Bahrami@Sun.COM 			    (uintptr_t)ofl->ofl_nehdr));
13751618Srie 
137612155SAli.Bahrami@Sun.COM 			if (class == ERR_FATAL) {
137712155SAli.Bahrami@Sun.COM 				return_code = S_ERROR;
137812155SAli.Bahrami@Sun.COM 				continue;
13791618Srie 			}
13801618Srie 		}
138112155SAli.Bahrami@Sun.COM 
138212155SAli.Bahrami@Sun.COM 		/*
138312155SAli.Bahrami@Sun.COM 		 * If '-z noreloc' is specified - skip the do_reloc stage.
138412155SAli.Bahrami@Sun.COM 		 */
138512155SAli.Bahrami@Sun.COM 		if (OFL_DO_RELOC(ofl)) {
138612155SAli.Bahrami@Sun.COM 			if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
138712155SAli.Bahrami@Sun.COM 			    ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
1388*13074SAli.Bahrami@Oracle.COM 			    ofl->ofl_lml) == 0) {
1389*13074SAli.Bahrami@Oracle.COM 				ofl->ofl_flags |= FLG_OF_FATAL;
139012155SAli.Bahrami@Sun.COM 				return_code = S_ERROR;
1391*13074SAli.Bahrami@Oracle.COM 			}
139212155SAli.Bahrami@Sun.COM 		}
13931618Srie 	}
13941618Srie 	return (return_code);
13951618Srie }
13961618Srie 
13976206Sab196087 static uintptr_t
ld_add_outrel(Word flags,Rel_desc * rsp,Ofl_desc * ofl)13981618Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
13991618Srie {
14001618Srie 	Rel_desc	*orsp;
14011618Srie 	Sym_desc	*sdp = rsp->rel_sym;
14024734Sab196087 	Conv_inv_buf_t	inv_buf;
14031618Srie 
14041618Srie 	/*
14051618Srie 	 * Static executables *do not* want any relocations against them.
14061618Srie 	 * Since our engine still creates relocations against a WEAK UNDEFINED
14071618Srie 	 * symbol in a static executable, it's best to disable them here
14081618Srie 	 * instead of through out the relocation code.
14091618Srie 	 */
141010792SRod.Evans@Sun.COM 	if (OFL_IS_STATIC_EXEC(ofl))
14111618Srie 		return (1);
14121618Srie 
14131618Srie 	/*
14141618Srie 	 * Certain relocations do not make sense in a 64bit shared object,
14151618Srie 	 * if building a shared object do a sanity check on the output
14161618Srie 	 * relocations being created.
14171618Srie 	 */
14181618Srie 	if (ofl->ofl_flags & FLG_OF_SHAROBJ) {
14191618Srie 		Word	rtype = rsp->rel_rtype;
14201618Srie 		/*
14211618Srie 		 * Because the R_SPARC_HIPLT22 & R_SPARC_LOPLT10 relocations
14221618Srie 		 * are not relative they make no sense to create in a shared
14231618Srie 		 * object - so emit the proper error message if that occurs.
14241618Srie 		 */
14254734Sab196087 		if ((rtype == R_SPARC_HIPLT22) || (rtype == R_SPARC_LOPLT10)) {
1426*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_UNRELREL),
14274734Sab196087 			    conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
14281618Srie 			    rsp->rel_isdesc->is_file->ifl_name,
142912155SAli.Bahrami@Sun.COM 			    ld_reloc_sym_name(rsp));
14301618Srie 			return (S_ERROR);
14311618Srie 		}
14321618Srie #if	defined(_ELF64)
14331618Srie 		/*
14341618Srie 		 * Each of the following relocations requires that the
14351618Srie 		 * object being built be loaded in either the upper 32 or
14361618Srie 		 * 44 bit range of memory.  Since shared libraries traditionally
14371618Srie 		 * are loaded in the lower range of memory - this isn't going
14381618Srie 		 * to work.
14391618Srie 		 */
14401618Srie 		if ((rtype == R_SPARC_H44) || (rtype == R_SPARC_M44) ||
14411618Srie 		    (rtype == R_SPARC_L44)) {
1442*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_SHOBJABS44),
14434734Sab196087 			    conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
14441618Srie 			    rsp->rel_isdesc->is_file->ifl_name,
144512155SAli.Bahrami@Sun.COM 			    ld_reloc_sym_name(rsp));
14461618Srie 			return (S_ERROR);
14471618Srie 		}
14481618Srie #endif
14491618Srie 	}
14501618Srie 
14511618Srie 	/*
14521618Srie 	 * If we are adding a output relocation against a section
14531618Srie 	 * symbol (non-RELATIVE) then mark that section.  These sections
14541618Srie 	 * will be added to the .dynsym symbol table.
14551618Srie 	 */
14561618Srie 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
14571618Srie 	    ((flags & FLG_REL_SCNNDX) ||
14581618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
14591618Srie 
14601618Srie 		/*
14611618Srie 		 * If this is a COMMON symbol - no output section
14621618Srie 		 * exists yet - (it's created as part of sym_validate()).
14631618Srie 		 * So - we mark here that when it's created it should
14641618Srie 		 * be tagged with the FLG_OS_OUTREL flag.
14651618Srie 		 */
14661618Srie 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
14671682Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
14681618Srie 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
14691618Srie 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
14701618Srie 			else
14711618Srie 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
14721618Srie 		} else {
147311827SRod.Evans@Sun.COM 			Os_desc	*osp;
147411827SRod.Evans@Sun.COM 			Is_desc	*isp = sdp->sd_isc;
14751618Srie 
147611827SRod.Evans@Sun.COM 			if (isp && ((osp = isp->is_osdesc) != NULL) &&
147711827SRod.Evans@Sun.COM 			    ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
14781618Srie 				ofl->ofl_dynshdrcnt++;
14791618Srie 				osp->os_flags |= FLG_OS_OUTREL;
14801618Srie 			}
14811618Srie 		}
14821618Srie 	}
14831618Srie 
148412155SAli.Bahrami@Sun.COM 	/* Enter it into the output relocation cache */
148512155SAli.Bahrami@Sun.COM 	if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
148612155SAli.Bahrami@Sun.COM 		return (S_ERROR);
14871618Srie 
14881618Srie 	if (flags & FLG_REL_GOT)
14891618Srie 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
14901618Srie 	else if (flags & FLG_REL_PLT)
14911618Srie 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
14921618Srie 	else if (flags & FLG_REL_BSS)
14931618Srie 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
14941618Srie 	else if (flags & FLG_REL_NOINFO)
14951618Srie 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
14961618Srie 	else
149712155SAli.Bahrami@Sun.COM 		RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rela);
14981618Srie 
14991618Srie 	if (orsp->rel_rtype == M_R_RELATIVE)
15001618Srie 		ofl->ofl_relocrelcnt++;
15011618Srie 
15021618Srie #if	defined(_ELF64)
15031618Srie 	/*
15041618Srie 	 * When building a 64-bit object any R_SPARC_WDISP30 relocation is given
15051618Srie 	 * a plt padding entry, unless we're building a relocatable object
15061618Srie 	 * (ld -r) or -b is in effect.
15071618Srie 	 */
15081618Srie 	if ((orsp->rel_rtype == R_SPARC_WDISP30) &&
15091618Srie 	    ((ofl->ofl_flags & (FLG_OF_BFLAG | FLG_OF_RELOBJ)) == 0) &&
15101618Srie 	    ((orsp->rel_sym->sd_flags & FLG_SY_PLTPAD) == 0)) {
15111618Srie 		ofl->ofl_pltpad++;
15121618Srie 		orsp->rel_sym->sd_flags |= FLG_SY_PLTPAD;
15131618Srie 	}
15141618Srie #endif
15151618Srie 	/*
15161618Srie 	 * We don't perform sorting on PLT relocations because
15171618Srie 	 * they have already been assigned a PLT index and if we
15181618Srie 	 * were to sort them we would have to re-assign the plt indexes.
15191618Srie 	 */
15201618Srie 	if (!(flags & FLG_REL_PLT))
15211618Srie 		ofl->ofl_reloccnt++;
15221618Srie 
15231618Srie 	/*
15241618Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
15251618Srie 	 */
15261618Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
15271618Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
15281618Srie 
15291618Srie 	/*
15301618Srie 	 * Identify and possibly warn of a displacement relocation.
15311618Srie 	 */
15321618Srie 	if (orsp->rel_flags & FLG_REL_DISP) {
15331618Srie 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
15341618Srie 
15351618Srie 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
15361618Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
15371618Srie 	}
15381618Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
15391618Srie 	    M_MACH, orsp));
15401618Srie 	return (1);
15411618Srie }
15421618Srie 
15431618Srie /*
15441618Srie  * Process relocation against a register symbol.  Note, of -z muldefs is in
15451618Srie  * effect there may have been multiple register definitions, which would have
15461618Srie  * been processed as non-fatal, with the first definition winning.  But, we
15471618Srie  * will also process multiple relocations for these multiple definitions.  In
15481618Srie  * this case we must only preserve the relocation for the definition that was
15491618Srie  * kept.  The sad part is that register relocations don't typically specify
15501618Srie  * the register symbol with which they are associated, so we might have to
15511618Srie  * search the input files global symbols to determine if this relocation is
15521618Srie  * appropriate.
15531618Srie  */
15546206Sab196087 static uintptr_t
ld_reloc_register(Rel_desc * rsp,Is_desc * isp,Ofl_desc * ofl)15559131SRod.Evans@Sun.COM ld_reloc_register(Rel_desc *rsp, Is_desc *isp, Ofl_desc *ofl)
15561618Srie {
15571618Srie 	if (ofl->ofl_flags & FLG_OF_MULDEFS) {
15589131SRod.Evans@Sun.COM 		Ifl_desc	*ifl = isp->is_file;
15599131SRod.Evans@Sun.COM 		Sym_desc	*sdp = rsp->rel_sym;
15601618Srie 
15611618Srie 		if (sdp == 0) {
15621618Srie 			Xword		offset = rsp->rel_roffset;
15631618Srie 			Word		ndx;
15641618Srie 
15651618Srie 			for (ndx = ifl->ifl_locscnt;
15661618Srie 			    ndx < ifl->ifl_symscnt; ndx++) {
15671618Srie 				if (((sdp = ifl->ifl_oldndx[ndx]) != 0) &&
15681618Srie 				    (sdp->sd_flags & FLG_SY_REGSYM) &&
15691618Srie 				    (sdp->sd_sym->st_value == offset))
15701618Srie 					break;
15711618Srie 			}
15721618Srie 		}
15731618Srie 		if (sdp && (sdp->sd_file != ifl))
15741618Srie 			return (1);
15751618Srie 	}
15761618Srie 	return (ld_add_outrel((rsp->rel_flags | FLG_REL_REG), rsp, ofl));
15771618Srie }
15781618Srie 
15791618Srie /*
15801618Srie  * process relocation for a LOCAL symbol
15811618Srie  */
15826206Sab196087 static uintptr_t
ld_reloc_local(Rel_desc * rsp,Ofl_desc * ofl)15839131SRod.Evans@Sun.COM ld_reloc_local(Rel_desc *rsp, Ofl_desc *ofl)
15841618Srie {
15856299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
15861618Srie 	Sym_desc	*sdp = rsp->rel_sym;
15871682Srie 	Word		shndx = sdp->sd_sym->st_shndx;
15881618Srie 
15891618Srie 	/*
15901618Srie 	 * if ((shared object) and (not pc relative relocation) and
15911618Srie 	 *    (not against ABS symbol))
15921618Srie 	 * then
15931618Srie 	 *	if (rtype != R_SPARC_32)
15941618Srie 	 *	then
15951618Srie 	 *		build relocation against section
15961618Srie 	 *	else
15971618Srie 	 *		build R_SPARC_RELATIVE
15981618Srie 	 *	fi
15991618Srie 	 * fi
16001618Srie 	 */
16011618Srie 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
16022850Srie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
16031618Srie 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
16041618Srie 	    !(rsp->rel_isdesc != NULL &&
16051618Srie 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
16061618Srie 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
16071618Srie 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
16081618Srie 		Word	ortype = rsp->rel_rtype;
16091618Srie 
16101618Srie 		if ((rsp->rel_rtype != R_SPARC_32) &&
16111618Srie 		    (rsp->rel_rtype != R_SPARC_PLT32) &&
16121618Srie 		    (rsp->rel_rtype != R_SPARC_64))
16131618Srie 			return (ld_add_outrel((FLG_REL_SCNNDX | FLG_REL_ADVAL),
16141618Srie 			    rsp, ofl));
16151618Srie 
16161618Srie 		rsp->rel_rtype = R_SPARC_RELATIVE;
16171618Srie 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
16181618Srie 			return (S_ERROR);
16191618Srie 		rsp->rel_rtype = ortype;
16201618Srie 		return (1);
16211618Srie 	}
16221618Srie 
16231618Srie 	/*
16241618Srie 	 * If the relocation is against a 'non-allocatable' section
16251618Srie 	 * and we can not resolve it now - then give a warning
16261618Srie 	 * message.
16271618Srie 	 *
16281618Srie 	 * We can not resolve the symbol if either:
16291618Srie 	 *	a) it's undefined
16301618Srie 	 *	b) it's defined in a shared library and a
16311618Srie 	 *	   COPY relocation hasn't moved it to the executable
16321618Srie 	 *
16331618Srie 	 * Note: because we process all of the relocations against the
16341618Srie 	 *	text segment before any others - we know whether
16351618Srie 	 *	or not a copy relocation will be generated before
16361618Srie 	 *	we get here (see reloc_init()->reloc_segments()).
16371618Srie 	 */
16381618Srie 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
16391618Srie 	    ((shndx == SHN_UNDEF) ||
16401618Srie 	    ((sdp->sd_ref == REF_DYN_NEED) &&
16411618Srie 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
16424734Sab196087 		Conv_inv_buf_t	inv_buf;
164312155SAli.Bahrami@Sun.COM 		Os_desc		*osp = RELAUX_GET_OSDESC(rsp);
16444734Sab196087 
16451618Srie 		/*
16461618Srie 		 * If the relocation is against a SHT_SUNW_ANNOTATE
16471618Srie 		 * section - then silently ignore that the relocation
16481618Srie 		 * can not be resolved.
16491618Srie 		 */
165012155SAli.Bahrami@Sun.COM 		if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
16511618Srie 			return (0);
1652*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
16534734Sab196087 		    conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
16541618Srie 		    rsp->rel_isdesc->is_file->ifl_name,
165512155SAli.Bahrami@Sun.COM 		    ld_reloc_sym_name(rsp), osp->os_name);
16561618Srie 		return (1);
16571618Srie 	}
16581618Srie 
16591618Srie 	/*
16601618Srie 	 * Perform relocation.
16611618Srie 	 */
16621618Srie 	return (ld_add_actrel(NULL, rsp, ofl));
16631618Srie }
16641618Srie 
16653304Srie /*
16663304Srie  * Establish a relocation transition.  Note, at this point of input relocation
16673304Srie  * processing, we have no idea of the relocation value that will be used in
16683304Srie  * the eventual relocation calculation.  This value is only known after the
16693304Srie  * initial image has been constructed.  Therefore, there is a small chance
16703304Srie  * that a value can exceed the capabilities of the transitioned relocation.
16713304Srie  * One example might be the offset from the GOT to a symbol.
16723304Srie  *
16733304Srie  * The only instance of this failure discovered so far has been via the use of
16743304Srie  * ABS symbols to represent an external memory location.  This situation is
16753304Srie  * rare, since ABS symbols aren't typically generated by the compilers.
16763304Srie  * Therefore, our solution is to excluded ABS symbols from the transition
16773304Srie  * relocation possibilities.  As an additional safeguard, if an inappropriate
16783304Srie  * value is passed to the final relocation engine, a verification ("V")
16793304Srie  * relocation should trigger a fatal error condition.
16803304Srie  */
16816206Sab196087 static uintptr_t
ld_reloc_GOTOP(Boolean local,Rel_desc * rsp,Ofl_desc * ofl)16823304Srie ld_reloc_GOTOP(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
16831618Srie {
16841618Srie 	Word	rtype = rsp->rel_rtype;
16851618Srie 
16863304Srie 	if (!local || (rsp->rel_sym->sd_sym->st_shndx == SHN_ABS)) {
16871618Srie 		/*
16881618Srie 		 * When binding to a external symbol, no fixups are required
16891618Srie 		 * and the GOTDATA_OP relocation can be ignored.
16901618Srie 		 */
16911618Srie 		if (rtype == R_SPARC_GOTDATA_OP)
16921618Srie 			return (1);
16931618Srie 		return (ld_reloc_GOT_relative(local, rsp, ofl));
16941618Srie 	}
16951618Srie 
16961618Srie 	/*
16971618Srie 	 * When binding to a local symbol the relocations can be transitioned:
16981618Srie 	 *
16991618Srie 	 *	R_*_GOTDATA_OP_HIX22 -> R_*_GOTDATA_HIX22
17001618Srie 	 *	R_*_GOTDATA_OP_LOX10 -> R_*_GOTDATA_LOX10
17011618Srie 	 *	R_*_GOTDATA_OP ->	instruction fixup
17021618Srie 	 */
17031618Srie 	return (ld_add_actrel(FLG_REL_GOTFIX, rsp, ofl));
17041618Srie }
17051618Srie 
17066206Sab196087 static uintptr_t
ld_reloc_TLS(Boolean local,Rel_desc * rsp,Ofl_desc * ofl)17073304Srie ld_reloc_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
17081618Srie {
17091618Srie 	Word		rtype = rsp->rel_rtype;
17101618Srie 	Sym_desc	*sdp = rsp->rel_sym;
17116299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
17121618Srie 	Gotndx		*gnp;
17131618Srie 
17141618Srie 	/*
17152145Srie 	 * If we're building an executable - use either the IE or LE access
17162145Srie 	 * model.  If we're building a shared object process any IE model.
17171618Srie 	 */
17182145Srie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
17191618Srie 		/*
17202145Srie 		 * Set the DF_STATIC_TLS flag.
17211618Srie 		 */
17221618Srie 		ofl->ofl_dtflags |= DF_STATIC_TLS;
17231618Srie 
17242145Srie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
17251618Srie 			/*
17262145Srie 			 * When processing static TLS - these relocations
17271618Srie 			 * can be ignored.
17281618Srie 			 */
17291618Srie 			if ((rtype == R_SPARC_TLS_IE_LD) ||
17301618Srie 			    (rtype == R_SPARC_TLS_IE_LDX) ||
17311618Srie 			    (rtype == R_SPARC_TLS_IE_ADD))
17321618Srie 				return (1);
17331618Srie 
17341618Srie 			/*
17352145Srie 			 * Assign a GOT entry for IE static TLS references.
17361618Srie 			 */
17371618Srie 			if (((rtype == R_SPARC_TLS_GD_HI22) ||
17381618Srie 			    (rtype == R_SPARC_TLS_GD_LO10) ||
17391618Srie 			    (rtype == R_SPARC_TLS_IE_HI22) ||
17401618Srie 			    (rtype == R_SPARC_TLS_IE_LO10)) &&
17419131SRod.Evans@Sun.COM 			    ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
17429131SRod.Evans@Sun.COM 			    GOT_REF_TLSIE, ofl, rsp)) == NULL)) {
17432145Srie 
17442145Srie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
17452145Srie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
17469131SRod.Evans@Sun.COM 				    rtype, M_R_TPOFF, NULL) == S_ERROR)
17471618Srie 					return (S_ERROR);
17481618Srie 			}
17491618Srie 
17502145Srie 			/*
17512145Srie 			 * IE access model.
17522145Srie 			 */
17531618Srie 			if (IS_TLS_IE(rtype))
17541618Srie 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
17551618Srie 
17561618Srie 			/*
17572145Srie 			 * Fixups are required for other executable models.
17581618Srie 			 */
17591618Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
17601618Srie 			    rsp, ofl));
17611618Srie 		}
17622145Srie 
17631618Srie 		/*
17642145Srie 		 * LE access model.
17651618Srie 		 */
17661618Srie 		if (IS_TLS_LE(rtype))
17671618Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
17681618Srie 
17691618Srie 		/*
17702145Srie 		 * When processing static TLS - these relocations can be
17712145Srie 		 * ignored.
17721618Srie 		 */
17731618Srie 		if (rtype == R_SPARC_TLS_IE_ADD)
17741618Srie 			return (1);
17751618Srie 
17761618Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
17771618Srie 		    rsp, ofl));
17781618Srie 	}
17791618Srie 
17801618Srie 	/*
17812145Srie 	 * Building a shared object.
17822145Srie 	 *
17832145Srie 	 * For dynamic TLS references, ADD relocations are ignored.
17841618Srie 	 */
17851618Srie 	if ((rtype == R_SPARC_TLS_GD_ADD) || (rtype == R_SPARC_TLS_LDM_ADD) ||
17861618Srie 	    (rtype == R_SPARC_TLS_LDO_ADD))
17871618Srie 		return (1);
17881618Srie 
17891618Srie 	/*
17901618Srie 	 * Assign a GOT entry for a dynamic TLS reference.
17911618Srie 	 */
17921618Srie 	if (((rtype == R_SPARC_TLS_LDM_HI22) ||
17931618Srie 	    (rtype == R_SPARC_TLS_LDM_LO10)) &&
17949131SRod.Evans@Sun.COM 	    ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSLD,
17959131SRod.Evans@Sun.COM 	    ofl, rsp)) == NULL)) {
17961618Srie 
17972145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
17982145Srie 		    FLG_REL_MTLS, rtype, M_R_DTPMOD, 0) == S_ERROR)
17991618Srie 			return (S_ERROR);
18001618Srie 
18012145Srie 	} else if (((rtype == R_SPARC_TLS_GD_HI22) ||
18022145Srie 	    (rtype == R_SPARC_TLS_GD_LO10)) &&
18039131SRod.Evans@Sun.COM 	    ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD,
18049131SRod.Evans@Sun.COM 	    ofl, rsp)) == NULL)) {
18051618Srie 
18062145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
18072145Srie 		    FLG_REL_DTLS, rtype, M_R_DTPMOD, M_R_DTPOFF) == S_ERROR)
18081618Srie 			return (S_ERROR);
18092145Srie 	}
18101618Srie 
18111618Srie 	/*
18121618Srie 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
18132145Srie 	 * cause a call to __tls_get_addr().  Convert this relocation to that
18142145Srie 	 * symbol now, and prepare for the PLT magic.
18151618Srie 	 */
18161618Srie 	if ((rtype == R_SPARC_TLS_GD_CALL) || (rtype == R_SPARC_TLS_LDM_CALL)) {
18173862Srie 		Sym_desc	*tlsgetsym;
18181618Srie 
18191618Srie 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_U),
18203862Srie 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
18211618Srie 			return (S_ERROR);
18222145Srie 
18231618Srie 		rsp->rel_sym = tlsgetsym;
18241618Srie 		rsp->rel_rtype = R_SPARC_WPLT30;
18252145Srie 
18261618Srie 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
18271618Srie 			return (S_ERROR);
18282145Srie 
18291618Srie 		rsp->rel_sym = sdp;
18301618Srie 		rsp->rel_rtype = rtype;
18311618Srie 		return (1);
18321618Srie 	}
18331618Srie 
18341618Srie 	if (IS_TLS_LD(rtype))
18351618Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
18361618Srie 
18371618Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
18381618Srie }
18391618Srie 
18401618Srie /*
18411618Srie  * ld_allocate_got: if a GOT is to be made, after the section is built this
18421618Srie  * function is called to allocate all the GOT slots.  The allocation is
18431618Srie  * deferred until after all GOTs have been counted and sorted according
18441618Srie  * to their size, for only then will we know how to allocate them on
18451618Srie  * a processor like SPARC which has different models for addressing the
18461618Srie  * GOT.  SPARC has two: small and large, small uses a signed 13-bit offset
18471618Srie  * into the GOT, whereas large uses an unsigned 32-bit offset.
18481618Srie  */
18491618Srie static	Sword small_index;	/* starting index for small GOT entries */
18505152Sab196087 static	Sword mixed_index;	/* starting index for mixed GOT entries */
18511618Srie static	Sword large_index;	/* starting index for large GOT entries */
18521618Srie 
18536206Sab196087 static uintptr_t
ld_assign_got(Ofl_desc * ofl,Sym_desc * sdp)18549131SRod.Evans@Sun.COM ld_assign_got(Ofl_desc *ofl, Sym_desc *sdp)
18551618Srie {
18569131SRod.Evans@Sun.COM 	Aliste idx;
18579131SRod.Evans@Sun.COM 	Gotndx *gnp;
18581618Srie 
18599131SRod.Evans@Sun.COM 	for (ALIST_TRAVERSE(sdp->sd_GOTndxs, idx, gnp)) {
18601618Srie 		uint_t	gotents;
18619131SRod.Evans@Sun.COM 		Gotref	gref = gnp->gn_gotref;
18629131SRod.Evans@Sun.COM 
18631618Srie 		if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
18641618Srie 			gotents = 2;
18651618Srie 		else
18661618Srie 			gotents = 1;
18671618Srie 
18681618Srie 		switch (gnp->gn_gotndx) {
18691618Srie 		case M_GOT_SMALL:
18701618Srie 			gnp->gn_gotndx = small_index;
18711618Srie 			small_index += gotents;
18721618Srie 			if (small_index == 0)
18731618Srie 				small_index = M_GOT_XNumber;
18741618Srie 			break;
18755152Sab196087 		case M_GOT_MIXED:
18765152Sab196087 			gnp->gn_gotndx = mixed_index;
18775152Sab196087 			mixed_index += gotents;
18785152Sab196087 			break;
18791618Srie 		case M_GOT_LARGE:
18801618Srie 			gnp->gn_gotndx = large_index;
18811618Srie 			large_index += gotents;
18821618Srie 			break;
18831618Srie 		default:
1884*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_ASSIGNGOT),
18851618Srie 			    EC_XWORD(gnp->gn_gotndx), demangle(sdp->sd_name));
18861618Srie 			return (S_ERROR);
18871618Srie 		}
18881618Srie 	}
18891618Srie 	return (1);
18901618Srie }
18911618Srie 
18926206Sab196087 static uintptr_t
ld_assign_got_ndx(Alist ** alpp,Gotndx * pgnp,Gotref gref,Ofl_desc * ofl,Rel_desc * rsp,Sym_desc * sdp)18939131SRod.Evans@Sun.COM ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
18949131SRod.Evans@Sun.COM     Rel_desc *rsp, Sym_desc *sdp)
18951618Srie {
18961618Srie 	Xword		raddend;
18979131SRod.Evans@Sun.COM 	Gotndx		gn, *gnp;
18989131SRod.Evans@Sun.COM 	Aliste		idx;
18991618Srie 	uint_t		gotents;
19001618Srie 
19015152Sab196087 	/* Some TLS requires two relocations with two GOT entries */
19021618Srie 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
19031618Srie 		gotents = 2;
19041618Srie 	else
19051618Srie 		gotents = 1;
19061618Srie 
19075152Sab196087 	raddend = rsp->rel_raddend;
19085152Sab196087 	if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref)) {
19095152Sab196087 
19105152Sab196087 		/*
19115152Sab196087 		 * If an entry for this addend already exists, determine if it
19125152Sab196087 		 * has mixed mode GOT access (both PIC and pic).
19135152Sab196087 		 *
19145152Sab196087 		 * In order to be accessible by both large and small pic,
19155152Sab196087 		 * a mixed mode GOT must be located in the positive index
19165152Sab196087 		 * range above _GLOBAL_OFFSET_TABLE_, and in the range
19175152Sab196087 		 * reachable small pic. This is necessary because the large
19185152Sab196087 		 * PIC mode cannot use a negative offset. This implies that
19195152Sab196087 		 * there can be no more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
19205152Sab196087 		 * such entries.
19215152Sab196087 		 */
19225152Sab196087 		switch (pgnp->gn_gotndx) {
19235152Sab196087 		case M_GOT_SMALL:
19245152Sab196087 			/*
19255152Sab196087 			 * This one was previously identified as a small
19265152Sab196087 			 * GOT. If this access is large, then convert
19275152Sab196087 			 * it to mixed.
19285152Sab196087 			 */
19295152Sab196087 			if (rsp->rel_rtype != R_SPARC_GOT13) {
19305152Sab196087 				pgnp->gn_gotndx = M_GOT_MIXED;
19315152Sab196087 				mixgotcnt += gotents;
19325152Sab196087 			}
19335152Sab196087 			break;
19345152Sab196087 
19355152Sab196087 		case M_GOT_LARGE:
19365152Sab196087 			/*
19375152Sab196087 			 * This one was previously identified as a large
19385152Sab196087 			 * GOT. If this access is small, convert it to mixed.
19395152Sab196087 			 */
19405152Sab196087 			if (rsp->rel_rtype == R_SPARC_GOT13) {
19415152Sab196087 				smlgotcnt += gotents;
19425152Sab196087 				mixgotcnt += gotents;
19435152Sab196087 				pgnp->gn_gotndx = M_GOT_MIXED;
19445152Sab196087 				sdp->sd_flags |= FLG_SY_SMGOT;
19455152Sab196087 			}
19465152Sab196087 			break;
19475152Sab196087 		}
19485152Sab196087 		return (1);
19495152Sab196087 	}
19505152Sab196087 
19519131SRod.Evans@Sun.COM 	gn.gn_addend = raddend;
19529131SRod.Evans@Sun.COM 	gn.gn_gotref = gref;
19539131SRod.Evans@Sun.COM 
19549131SRod.Evans@Sun.COM 	if (rsp->rel_rtype == R_SPARC_GOT13) {
19559131SRod.Evans@Sun.COM 		gn.gn_gotndx = M_GOT_SMALL;
19569131SRod.Evans@Sun.COM 		smlgotcnt += gotents;
19579131SRod.Evans@Sun.COM 		sdp->sd_flags |= FLG_SY_SMGOT;
19589131SRod.Evans@Sun.COM 	} else
19599131SRod.Evans@Sun.COM 		gn.gn_gotndx = M_GOT_LARGE;
19609131SRod.Evans@Sun.COM 
19619131SRod.Evans@Sun.COM 	ofl->ofl_gotcnt += gotents;
19629131SRod.Evans@Sun.COM 
19639131SRod.Evans@Sun.COM 	if (gref == GOT_REF_TLSLD) {
19649131SRod.Evans@Sun.COM 		if (ofl->ofl_tlsldgotndx == NULL) {
19659131SRod.Evans@Sun.COM 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
19669131SRod.Evans@Sun.COM 				return (S_ERROR);
19679131SRod.Evans@Sun.COM 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
19689131SRod.Evans@Sun.COM 			ofl->ofl_tlsldgotndx = gnp;
19699131SRod.Evans@Sun.COM 		}
19709131SRod.Evans@Sun.COM 		return (1);
19719131SRod.Evans@Sun.COM 	}
19729131SRod.Evans@Sun.COM 
19739131SRod.Evans@Sun.COM 	idx = 0;
19749131SRod.Evans@Sun.COM 	for (ALIST_TRAVERSE(*alpp, idx, gnp)) {
19759131SRod.Evans@Sun.COM 		if (gnp->gn_addend > raddend)
19761618Srie 			break;
19771618Srie 	}
19781618Srie 
19791618Srie 	/*
19809131SRod.Evans@Sun.COM 	 * GOT indexes are maintained on an Alist, where there is typically
19819131SRod.Evans@Sun.COM 	 * only one index.  The usage of this list is to scan the list to find
19829131SRod.Evans@Sun.COM 	 * an index, and then apply that index immediately to a relocation.
19839131SRod.Evans@Sun.COM 	 * Thus there are no external references to these GOT index structures
19849131SRod.Evans@Sun.COM 	 * that can be compromised by the Alist being reallocated.
19851618Srie 	 */
19869131SRod.Evans@Sun.COM 	if (alist_insert(alpp, &gn, sizeof (Gotndx),
19879131SRod.Evans@Sun.COM 	    AL_CNT_SDP_GOT, idx) == NULL)
19881618Srie 		return (S_ERROR);
19891618Srie 
19901618Srie 	return (1);
19911618Srie }
19921618Srie 
19936206Sab196087 static void
ld_assign_plt_ndx(Sym_desc * sdp,Ofl_desc * ofl)19941618Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
19951618Srie {
19961618Srie 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
19971618Srie }
19981618Srie 
19991618Srie 
20006206Sab196087 static uintptr_t
ld_allocate_got(Ofl_desc * ofl)20011618Srie ld_allocate_got(Ofl_desc * ofl)
20021618Srie {
20035152Sab196087 	const Sword	first_large_ndx = M_GOT_MAXSMALL / 2;
20049131SRod.Evans@Sun.COM 	Sym_desc	*sdp;
20051618Srie 	Addr		addr;
20061618Srie 
20071618Srie 	/*
20085152Sab196087 	 * Sanity check -- is this going to fit at all? There are two
20095152Sab196087 	 * limits to be concerned about:
20105152Sab196087 	 *	1) There is a limit on the number of small pic GOT indices,
20115152Sab196087 	 *		given by M_GOT_MAXSMALL.
20125152Sab196087 	 *	2) If there are more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
20135152Sab196087 	 *		small GOT indices, there will be items at negative
20145152Sab196087 	 *		offsets from _GLOBAL_OFFSET_TABLE_. Items that are
20155152Sab196087 	 *		accessed via large (PIC) code cannot reach these
20165152Sab196087 	 *		negative slots, so mixed mode items must be in the
20175152Sab196087 	 *		non-negative range. This implies a limit of
20185152Sab196087 	 *		(M_GOT_MAXSMALL/2 - M_GOT_XNumber) mixed mode indices.
20191618Srie 	 */
20205152Sab196087 	if (smlgotcnt > M_GOT_MAXSMALL) {
2021*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_SMALLGOT),
20221618Srie 		    EC_WORD(smlgotcnt), M_GOT_MAXSMALL);
20231618Srie 		return (S_ERROR);
20241618Srie 	}
20255152Sab196087 	if (mixgotcnt > (first_large_ndx - M_GOT_XNumber)) {
2026*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_MIXEDGOT),
20275152Sab196087 		    EC_WORD(mixgotcnt), first_large_ndx - M_GOT_XNumber);
20285152Sab196087 		return (S_ERROR);
20295152Sab196087 	}
20301618Srie 
20311618Srie 	/*
20321618Srie 	 * Set starting offset to be either 0, or a negative index into
20331618Srie 	 * the GOT based on the number of small symbols we've got.
20341618Srie 	 */
20355152Sab196087 	neggotoffset = ((smlgotcnt >= first_large_ndx) ?
20365152Sab196087 	    (first_large_ndx - smlgotcnt) : 0);
20371618Srie 
20381618Srie 	/*
20395152Sab196087 	 * Initialize the got offsets used by assign_got() to
20405152Sab196087 	 * locate GOT items:
20415152Sab196087 	 *	small - Starting index of items referenced only
20425152Sab196087 	 *		by small offsets (-Kpic).
20435152Sab196087 	 *	mixed - Starting index of items referenced
20445152Sab196087 	 *		by both large (-KPIC) and small (-Kpic).
20455152Sab196087 	 *	large - Indexes referenced only by large (-KPIC)
20465152Sab196087 	 *
20475152Sab196087 	 *  Small items can have negative indexes (i.e. lie below
20485152Sab196087 	 *	_GLOBAL_OFFSET_TABLE_). Mixed and large items must have
20495152Sab196087 	 *	non-negative offsets.
20501618Srie 	 */
20515152Sab196087 	small_index = (neggotoffset == 0) ? M_GOT_XNumber : neggotoffset;
20521618Srie 	large_index = neggotoffset + smlgotcnt;
20535152Sab196087 	mixed_index = large_index - mixgotcnt;
20541618Srie 
20551618Srie 	/*
20561618Srie 	 * Assign bias to GOT symbols.
20571618Srie 	 */
20581618Srie 	addr = -neggotoffset * M_GOT_ENTSIZE;
205910792SRod.Evans@Sun.COM 	if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL), SYM_NOHASH,
206010792SRod.Evans@Sun.COM 	    NULL, ofl)) != NULL)
20611618Srie 		sdp->sd_sym->st_value = addr;
206210792SRod.Evans@Sun.COM 	if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH,
206310792SRod.Evans@Sun.COM 	    NULL, ofl)) != NULL)
20641618Srie 		sdp->sd_sym->st_value = addr;
20651618Srie 
20661618Srie 	if (ofl->ofl_tlsldgotndx) {
20671618Srie 		ofl->ofl_tlsldgotndx->gn_gotndx = large_index;
20681618Srie 		large_index += 2;
20691618Srie 	}
20701618Srie 	return (1);
20711618Srie }
20721618Srie 
20731618Srie /*
20741618Srie  * Initializes .got[0] with the _DYNAMIC symbol value.
20751618Srie  */
20766206Sab196087 static uintptr_t
ld_fillin_gotplt(Ofl_desc * ofl)20772145Srie ld_fillin_gotplt(Ofl_desc *ofl)
20781618Srie {
20791618Srie 	if (ofl->ofl_osgot) {
20802145Srie 		Sym_desc	*sdp;
20811618Srie 
20821618Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
208310792SRod.Evans@Sun.COM 		    SYM_NOHASH, NULL, ofl)) != NULL) {
20842145Srie 			uchar_t	*genptr;
20852145Srie 
20862145Srie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
20871618Srie 			    (-neggotoffset * M_GOT_ENTSIZE) +
20881618Srie 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
20891618Srie 			/* LINTED */
20901618Srie 			*((Xword *)genptr) = sdp->sd_sym->st_value;
20916206Sab196087 			if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
20926206Sab196087 				/* LINTED */
20936206Sab196087 				*((Xword *)genptr) =
20946206Sab196087 				    /* LINTED */
20956206Sab196087 				    ld_bswap_Xword(*((Xword *)genptr));
20961618Srie 		}
20971618Srie 	}
20981618Srie 	return (1);
20991618Srie }
21006206Sab196087 
21016206Sab196087 
21026206Sab196087 
21036206Sab196087 /*
21046206Sab196087  * Template for generating "void (*)(void)" function
21056206Sab196087  */
21066206Sab196087 static const uchar_t nullfunc_tmpl[] = {
21076206Sab196087 /* 0x00 */	0x81, 0xc3, 0xe0, 0x08,		/* retl */
21086206Sab196087 /* 0x04 */	0x01, 0x00, 0x00, 0x00		/* nop */
21096206Sab196087 };
21106206Sab196087 
21116206Sab196087 
21126206Sab196087 
21136206Sab196087 /*
21146206Sab196087  * Return the ld_targ definition for this target.
21156206Sab196087  */
21166206Sab196087 const Target *
ld_targ_init_sparc(void)21176206Sab196087 ld_targ_init_sparc(void)
21186206Sab196087 {
21196206Sab196087 	static const Target _ld_targ = {
21206206Sab196087 		{			/* Target_mach */
21216206Sab196087 			M_MACH,			/* m_mach */
21226206Sab196087 			M_MACHPLUS,		/* m_machplus */
21236206Sab196087 			M_FLAGSPLUS,		/* m_flagsplus */
21246206Sab196087 			M_CLASS,		/* m_class */
21256206Sab196087 			M_DATA,			/* m_data */
21266206Sab196087 
21276206Sab196087 			M_SEGM_ALIGN,		/* m_segm_align */
21286206Sab196087 			M_SEGM_ORIGIN,		/* m_segm_origin */
21298501SRod.Evans@Sun.COM 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
21306206Sab196087 			M_DATASEG_PERM,		/* m_dataseg_perm */
213111734SAli.Bahrami@Sun.COM 			M_STACK_PERM,		/* m_stack_perm */
21326206Sab196087 			M_WORD_ALIGN,		/* m_word_align */
21336206Sab196087 						/* m_def_interp */
21346206Sab196087 #if	defined(_ELF64)
21356206Sab196087 			MSG_ORIG(MSG_PTH_RTLD_SPARCV9),
21366206Sab196087 #else
21376206Sab196087 			MSG_ORIG(MSG_PTH_RTLD),
21386206Sab196087 #endif
21396206Sab196087 
21406206Sab196087 			/* Relocation type codes */
21416206Sab196087 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
21426206Sab196087 			M_R_COPY,		/* m_r_copy */
21436206Sab196087 			M_R_GLOB_DAT,		/* m_r_glob_dat */
21446206Sab196087 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
21456206Sab196087 			M_R_NUM,		/* m_r_num */
21466206Sab196087 			M_R_NONE,		/* m_r_none */
21476206Sab196087 			M_R_RELATIVE,		/* m_r_relative */
21486206Sab196087 			M_R_REGISTER,		/* m_r_register */
21496206Sab196087 
21506206Sab196087 			/* Relocation related constants */
21516206Sab196087 			M_REL_DT_COUNT,		/* m_rel_dt_count */
21526206Sab196087 			M_REL_DT_ENT,		/* m_rel_dt_ent */
21536206Sab196087 			M_REL_DT_SIZE,		/* m_rel_dt_size */
21546206Sab196087 			M_REL_DT_TYPE,		/* m_rel_dt_type */
21556206Sab196087 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
21566206Sab196087 
21576206Sab196087 			/* GOT related constants */
21586206Sab196087 			M_GOT_ENTSIZE,		/* m_got_entsize */
21596206Sab196087 			M_GOT_XNumber,		/* m_got_xnumber */
21606206Sab196087 
21616206Sab196087 			/* PLT related constants */
21626206Sab196087 			M_PLT_ALIGN,		/* m_plt_align */
21636206Sab196087 			M_PLT_ENTSIZE,		/* m_plt_entsize */
21646206Sab196087 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
21656206Sab196087 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
21666206Sab196087 
21679085SAli.Bahrami@Sun.COM 			/* Section type of .eh_frame/.eh_frame_hdr sections */
21689085SAli.Bahrami@Sun.COM 			SHT_PROGBITS,		/* m_sht_unwind */
21699085SAli.Bahrami@Sun.COM 
21706206Sab196087 			M_DT_REGISTER,		/* m_dt_register */
21716206Sab196087 		},
21726206Sab196087 		{			/* Target_machid */
21736206Sab196087 			M_ID_ARRAY,		/* id_array */
21746206Sab196087 			M_ID_BSS,		/* id_bss */
21756206Sab196087 			M_ID_CAP,		/* id_cap */
217611827SRod.Evans@Sun.COM 			M_ID_CAPINFO,		/* id_capinfo */
217711827SRod.Evans@Sun.COM 			M_ID_CAPCHAIN,		/* id_capchain */
21786206Sab196087 			M_ID_DATA,		/* id_data */
21796206Sab196087 			M_ID_DYNAMIC,		/* id_dynamic */
21806206Sab196087 			M_ID_DYNSORT,		/* id_dynsort */
21816206Sab196087 			M_ID_DYNSTR,		/* id_dynstr */
21826206Sab196087 			M_ID_DYNSYM,		/* id_dynsym */
21836206Sab196087 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
21846206Sab196087 			M_ID_GOT,		/* id_got */
21856206Sab196087 			M_ID_GOTDATA,		/* id_gotdata */
21866206Sab196087 			M_ID_HASH,		/* id_hash */
21876206Sab196087 			M_ID_INTERP,		/* id_interp */
21886206Sab196087 			M_ID_UNKNOWN,		/* id_lbss (unused) */
21896206Sab196087 			M_ID_LDYNSYM,		/* id_ldynsym */
21906206Sab196087 			M_ID_NOTE,		/* id_note */
21916206Sab196087 			M_ID_NULL,		/* id_null */
21926206Sab196087 			M_ID_PLT,		/* id_plt */
21936206Sab196087 			M_ID_REL,		/* id_rel */
21946206Sab196087 			M_ID_STRTAB,		/* id_strtab */
21956206Sab196087 			M_ID_SYMINFO,		/* id_syminfo */
21966206Sab196087 			M_ID_SYMTAB,		/* id_symtab */
21976206Sab196087 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
21986206Sab196087 			M_ID_TEXT,		/* id_text */
21996206Sab196087 			M_ID_TLS,		/* id_tls */
22006206Sab196087 			M_ID_TLSBSS,		/* id_tlsbss */
22016206Sab196087 			M_ID_UNKNOWN,		/* id_unknown */
22029085SAli.Bahrami@Sun.COM 			M_ID_UNWIND,		/* id_unwind */
22039085SAli.Bahrami@Sun.COM 			M_ID_UNWINDHDR,		/* id_unwindhdr */
22046206Sab196087 			M_ID_USER,		/* id_user */
22056206Sab196087 			M_ID_VERSION,		/* id_version */
22066206Sab196087 		},
22076206Sab196087 		{			/* Target_nullfunc */
22086206Sab196087 			nullfunc_tmpl,		/* nf_template */
22096206Sab196087 			sizeof (nullfunc_tmpl),	/* nf_size */
22106206Sab196087 		},
221110809SAli.Bahrami@Sun.COM 		{			/* Target_fillfunc */
221210809SAli.Bahrami@Sun.COM 			/*
221310809SAli.Bahrami@Sun.COM 			 * On sparc, special filling of executable sections
221410809SAli.Bahrami@Sun.COM 			 * is undesirable, and the default 0 fill supplied
221510809SAli.Bahrami@Sun.COM 			 * by libelf is preferred:
221610809SAli.Bahrami@Sun.COM 			 *
221710809SAli.Bahrami@Sun.COM 			 * -	0 fill is interpreted as UNIMP instructions,
221810809SAli.Bahrami@Sun.COM 			 *	which cause an illegal_instruction_trap. These
221910809SAli.Bahrami@Sun.COM 			 *	serve as a sentinel against poorly written
222010809SAli.Bahrami@Sun.COM 			 *	code. The sparc architecture manual discusses
222110809SAli.Bahrami@Sun.COM 			 *	this as providing a measure of runtime safety.
222210809SAli.Bahrami@Sun.COM 			 *
222310809SAli.Bahrami@Sun.COM 			 * -	The one place where a hole should conceivably
222410809SAli.Bahrami@Sun.COM 			 *	be filled with NOP instructions is in the
222510809SAli.Bahrami@Sun.COM 			 *	.init/.fini sections. However, the sparc
222610809SAli.Bahrami@Sun.COM 			 *	assembler sizes the sections it generates
222710809SAli.Bahrami@Sun.COM 			 *	to a multiple of the section alignment, and as
222810809SAli.Bahrami@Sun.COM 			 *	such, takes the filling task out of our hands.
222910809SAli.Bahrami@Sun.COM 			 *	Furthermore, the sparc assembler uses 0-fill
223010809SAli.Bahrami@Sun.COM 			 *	for this, forcing the authors of sparc
223110809SAli.Bahrami@Sun.COM 			 *	assembler for .init/.fini sections to be aware
223210809SAli.Bahrami@Sun.COM 			 *	of this case and explicitly supply NOP fill.
223310809SAli.Bahrami@Sun.COM 			 *	Hence, there is no role for the link-editor.
223410809SAli.Bahrami@Sun.COM 			 */
223510809SAli.Bahrami@Sun.COM 			NULL			/* ff_execfill */
223610809SAli.Bahrami@Sun.COM 		},
22376206Sab196087 		{			/* Target_machrel */
22386206Sab196087 			reloc_table,
22396206Sab196087 
22406206Sab196087 			ld_init_rel,		/* mr_init_rel */
22416206Sab196087 			ld_mach_eflags,		/* mr_mach_eflags */
22426206Sab196087 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
22436206Sab196087 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
22446206Sab196087 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
22456206Sab196087 			ld_perform_outreloc,	/* mr_perform_outreloc */
22466206Sab196087 			ld_do_activerelocs,	/* mr_do_activerelocs */
22476206Sab196087 			ld_add_outrel,		/* mr_add_outrel */
22486206Sab196087 			ld_reloc_register,	/* mr_reloc_register */
22496206Sab196087 			ld_reloc_local,		/* mr_reloc_local */
22506206Sab196087 			ld_reloc_GOTOP,		/* mr_reloc_GOTOP */
22516206Sab196087 			ld_reloc_TLS,		/* mr_reloc_TLS */
22526206Sab196087 			ld_assign_got,		/* mr_assign_got */
22539131SRod.Evans@Sun.COM 			ld_find_got_ndx,	/* mr_find_got_ndx */
22546206Sab196087 			ld_calc_got_offset,	/* mr_calc_got_offset */
22556206Sab196087 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
22566206Sab196087 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
22576206Sab196087 			ld_allocate_got,	/* mr_allocate_got */
22586206Sab196087 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
22596206Sab196087 		},
22606206Sab196087 		{			/* Target_machsym */
22616206Sab196087 			ld_reg_check_sparc,	/* ms_reg_check */
22626206Sab196087 			ld_mach_sym_typecheck_sparc, /* ms_mach_sym_typecheck */
22636206Sab196087 			ld_is_regsym_sparc,	/* ms_is_regsym */
22646206Sab196087 			ld_reg_find_sparc,	/* ms_reg_find */
22656206Sab196087 			ld_reg_enter_sparc	/* ms_reg_enter */
22666206Sab196087 		}
22676206Sab196087 	};
22686206Sab196087 
22696206Sab196087 	return (&_ld_targ);
22706206Sab196087 }
2271