xref: /onnv-gate/usr/src/cmd/sgs/rtld/sparc/sparc_elf.c (revision 4734:a4708faa3e85)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * 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.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211618Srie 
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
240Sstevel@tonic-gate  *	  All Rights Reserved
250Sstevel@tonic-gate  *
263731Srie  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
271618Srie  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * SPARC machine dependent and ELF file class dependent functions.
330Sstevel@tonic-gate  * Contains routines for performing function binding and symbol relocations.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate #include	"_synonyms.h"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include	<stdio.h>
380Sstevel@tonic-gate #include	<sys/elf.h>
390Sstevel@tonic-gate #include	<sys/elf_SPARC.h>
400Sstevel@tonic-gate #include	<sys/mman.h>
410Sstevel@tonic-gate #include	<dlfcn.h>
420Sstevel@tonic-gate #include	<synch.h>
430Sstevel@tonic-gate #include	<string.h>
441618Srie #include	<debug.h>
451618Srie #include	<reloc.h>
461618Srie #include	<conv.h>
470Sstevel@tonic-gate #include	"_rtld.h"
480Sstevel@tonic-gate #include	"_audit.h"
490Sstevel@tonic-gate #include	"_elf.h"
500Sstevel@tonic-gate #include	"msg.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 
530Sstevel@tonic-gate extern void	iflush_range(caddr_t, size_t);
540Sstevel@tonic-gate extern void	plt_full_range(uintptr_t, uintptr_t);
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 
570Sstevel@tonic-gate int
580Sstevel@tonic-gate elf_mach_flags_check(Rej_desc *rej, Ehdr *ehdr)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	/*
610Sstevel@tonic-gate 	 * Check machine type and flags.
620Sstevel@tonic-gate 	 */
630Sstevel@tonic-gate 	if (ehdr->e_machine != EM_SPARC) {
640Sstevel@tonic-gate 		if (ehdr->e_machine != EM_SPARC32PLUS) {
650Sstevel@tonic-gate 			rej->rej_type = SGS_REJ_MACH;
660Sstevel@tonic-gate 			rej->rej_info = (uint_t)ehdr->e_machine;
670Sstevel@tonic-gate 			return (0);
680Sstevel@tonic-gate 		}
690Sstevel@tonic-gate 		if ((ehdr->e_flags & EF_SPARC_32PLUS) == 0) {
700Sstevel@tonic-gate 			rej->rej_type = SGS_REJ_MISFLAG;
710Sstevel@tonic-gate 			rej->rej_info = (uint_t)ehdr->e_flags;
720Sstevel@tonic-gate 			return (0);
730Sstevel@tonic-gate 		}
740Sstevel@tonic-gate 		if ((ehdr->e_flags & ~at_flags) & EF_SPARC_32PLUS_MASK) {
750Sstevel@tonic-gate 			rej->rej_type = SGS_REJ_BADFLAG;
760Sstevel@tonic-gate 			rej->rej_info = (uint_t)ehdr->e_flags;
770Sstevel@tonic-gate 			return (0);
780Sstevel@tonic-gate 		}
790Sstevel@tonic-gate 	} else if ((ehdr->e_flags & ~EF_SPARCV9_MM) != 0) {
800Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_BADFLAG;
810Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_flags;
820Sstevel@tonic-gate 		return (0);
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 	return (1);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate 
870Sstevel@tonic-gate void
880Sstevel@tonic-gate ldso_plt_init(Rt_map * lmp)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	/*
910Sstevel@tonic-gate 	 * There is no need to analyze ld.so because we don't map in any of
920Sstevel@tonic-gate 	 * its dependencies.  However we may map these dependencies in later
930Sstevel@tonic-gate 	 * (as if ld.so had dlopened them), so initialize the plt and the
940Sstevel@tonic-gate 	 * permission information.
950Sstevel@tonic-gate 	 */
960Sstevel@tonic-gate 	if (PLTGOT(lmp))
970Sstevel@tonic-gate 		elf_plt_init((PLTGOT(lmp)), (caddr_t)lmp);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * elf_plt_write() will test to see how far away our destination
1020Sstevel@tonic-gate  *	address lies.  If it is close enough that a branch can
1030Sstevel@tonic-gate  *	be used instead of a jmpl - we will fill the plt in with
1040Sstevel@tonic-gate  * 	single branch.  The branches are much quicker then
1050Sstevel@tonic-gate  *	a jmpl instruction - see bug#4356879 for further
1060Sstevel@tonic-gate  *	details.
1070Sstevel@tonic-gate  *
1080Sstevel@tonic-gate  *	NOTE: we pass in both a 'pltaddr' and a 'vpltaddr' since
1090Sstevel@tonic-gate  *		librtld/dldump update PLT's who's physical
1100Sstevel@tonic-gate  *		address is not the same as the 'virtual' runtime
1110Sstevel@tonic-gate  *		address.
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate Pltbindtype
1140Sstevel@tonic-gate /* ARGSUSED4 */
1150Sstevel@tonic-gate elf_plt_write(uintptr_t addr, uintptr_t vaddr, void *rptr, uintptr_t symval,
1160Sstevel@tonic-gate 	Xword pltndx)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	Rela		*rel = (Rela *)rptr;
1190Sstevel@tonic-gate 	uintptr_t	vpltaddr, pltaddr;
1200Sstevel@tonic-gate 	long		disp;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	pltaddr = addr + rel->r_offset;
1240Sstevel@tonic-gate 	vpltaddr = vaddr + rel->r_offset;
1250Sstevel@tonic-gate 	disp = symval - vpltaddr - 4;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	/*
1280Sstevel@tonic-gate 	 * Test if the destination address is close enough to use
1290Sstevel@tonic-gate 	 * a ba,a... instruction to reach it.
1300Sstevel@tonic-gate 	 */
1310Sstevel@tonic-gate 	if (S_INRANGE(disp, 23) && !(rtld_flags & RT_FL_NOBAPLT)) {
1320Sstevel@tonic-gate 		uint_t		*pltent, bainstr;
1330Sstevel@tonic-gate 		Pltbindtype	rc;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 		pltent = (uint_t *)pltaddr;
1360Sstevel@tonic-gate 		/*
1370Sstevel@tonic-gate 		 * The
1380Sstevel@tonic-gate 		 *
1390Sstevel@tonic-gate 		 *	ba,a,pt %icc, <dest>
1400Sstevel@tonic-gate 		 *
1410Sstevel@tonic-gate 		 * is the most efficient of the PLT's.  If we
1420Sstevel@tonic-gate 		 * are within +-20 bits *and* running on a
1430Sstevel@tonic-gate 		 * v8plus architecture - use that branch.
1440Sstevel@tonic-gate 		 */
1450Sstevel@tonic-gate 		if ((at_flags & EF_SPARC_32PLUS) &&
1460Sstevel@tonic-gate 		    S_INRANGE(disp, 20)) {
1470Sstevel@tonic-gate 			bainstr = M_BA_A_PT;	/* ba,a,pt %icc,<dest> */
1480Sstevel@tonic-gate 			bainstr |= (S_MASK(19) & (disp >> 2));
1490Sstevel@tonic-gate 			rc = PLT_T_21D;
1500Sstevel@tonic-gate 			DBG_CALL(pltcnt21d++);
1510Sstevel@tonic-gate 		} else {
1520Sstevel@tonic-gate 			/*
1530Sstevel@tonic-gate 			 * Otherwise - we fall back to the good old
1540Sstevel@tonic-gate 			 *
1550Sstevel@tonic-gate 			 *	ba,a	<dest>
1560Sstevel@tonic-gate 			 *
1570Sstevel@tonic-gate 			 * Which still beats a jmpl instruction.
1580Sstevel@tonic-gate 			 */
1590Sstevel@tonic-gate 			bainstr = M_BA_A;		/* ba,a <dest> */
1600Sstevel@tonic-gate 			bainstr |= (S_MASK(22) & (disp >> 2));
1610Sstevel@tonic-gate 			rc = PLT_T_24D;
1620Sstevel@tonic-gate 			DBG_CALL(pltcnt24d++);
1630Sstevel@tonic-gate 		}
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 		pltent[2] = M_NOP;		/* nop instr */
1660Sstevel@tonic-gate 		pltent[1] = bainstr;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 		iflush_range((char *)(&pltent[1]), 4);
1690Sstevel@tonic-gate 		pltent[0] = M_NOP;		/* nop instr */
1700Sstevel@tonic-gate 		iflush_range((char *)(&pltent[0]), 4);
1710Sstevel@tonic-gate 		return (rc);
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	/*
1750Sstevel@tonic-gate 	 * The PLT destination is not in reach of
1760Sstevel@tonic-gate 	 * a branch instruction - so we fall back
1770Sstevel@tonic-gate 	 * to a 'jmpl' sequence.
1780Sstevel@tonic-gate 	 */
1790Sstevel@tonic-gate 	plt_full_range(pltaddr, symval);
1800Sstevel@tonic-gate 	DBG_CALL(pltcntfull++);
1810Sstevel@tonic-gate 	return (PLT_T_FULL);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * Local storage space created on the stack created for this glue
1870Sstevel@tonic-gate  * code includes space for:
1880Sstevel@tonic-gate  *		0x4	pointer to dyn_data
1890Sstevel@tonic-gate  *		0x4	size prev stack frame
1900Sstevel@tonic-gate  */
1910Sstevel@tonic-gate static const uchar_t dyn_plt_template[] = {
1920Sstevel@tonic-gate /* 0x00 */	0x80, 0x90, 0x00, 0x1e,	/* tst   %fp */
1930Sstevel@tonic-gate /* 0x04 */	0x02, 0x80, 0x00, 0x04, /* be    0x14 */
1940Sstevel@tonic-gate /* 0x08 */	0x82, 0x27, 0x80, 0x0e,	/* sub   %sp, %fp, %g1 */
1950Sstevel@tonic-gate /* 0x0c */	0x10, 0x80, 0x00, 0x03, /* ba	 0x20 */
1960Sstevel@tonic-gate /* 0x10 */	0x01, 0x00, 0x00, 0x00, /* nop */
1970Sstevel@tonic-gate /* 0x14 */	0x82, 0x10, 0x20, 0x60, /* mov	0x60, %g1 */
1980Sstevel@tonic-gate /* 0x18 */	0x9d, 0xe3, 0xbf, 0x98,	/* save	%sp, -0x68, %sp */
1990Sstevel@tonic-gate /* 0x1c */	0xc2, 0x27, 0xbf, 0xf8,	/* st	%g1, [%fp + -0x8] */
2000Sstevel@tonic-gate /* 0x20 */	0x03, 0x00, 0x00, 0x00,	/* sethi %hi(val), %g1 */
2010Sstevel@tonic-gate /* 0x24 */	0x82, 0x10, 0x60, 0x00, /* or	 %g1, %lo(val), %g1 */
2020Sstevel@tonic-gate /* 0x28 */	0x40, 0x00, 0x00, 0x00,	/* call  <rel_addr> */
2030Sstevel@tonic-gate /* 0x2c */	0xc2, 0x27, 0xbf, 0xfc	/* st    %g1, [%fp + -0x4] */
2040Sstevel@tonic-gate };
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate int	dyn_plt_ent_size = sizeof (dyn_plt_template) +
2070Sstevel@tonic-gate 		sizeof (uintptr_t) +	/* reflmp */
2080Sstevel@tonic-gate 		sizeof (uintptr_t) +	/* deflmp */
2090Sstevel@tonic-gate 		sizeof (ulong_t) +	/* symndx */
2100Sstevel@tonic-gate 		sizeof (ulong_t) +	/* sb_flags */
2110Sstevel@tonic-gate 		sizeof (Sym);		/* symdef */
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate  * the dynamic plt entry is:
2150Sstevel@tonic-gate  *
2160Sstevel@tonic-gate  *	tst	%fp
2170Sstevel@tonic-gate  *	be	1f
2180Sstevel@tonic-gate  *	nop
2190Sstevel@tonic-gate  *	sub	%sp, %fp, %g1
2200Sstevel@tonic-gate  *	ba	2f
2210Sstevel@tonic-gate  *	nop
2220Sstevel@tonic-gate  * 1:
2230Sstevel@tonic-gate  *	mov	SA(MINFRAME), %g1	! if %fp is null this is the
2240Sstevel@tonic-gate  *					!   'minimum stack'.  %fp is null
2250Sstevel@tonic-gate  *					!   on the initial stack frame
2260Sstevel@tonic-gate  * 2:
2270Sstevel@tonic-gate  *	save	%sp, -(SA(MINFRAME) + 2 * CLONGSIZE), %sp
2280Sstevel@tonic-gate  *	st	%g1, [%fp + -0x8] ! store prev_stack size in [%fp - 8]
2290Sstevel@tonic-gate  *	sethi	%hi(dyn_data), %g1
2300Sstevel@tonic-gate  *	or	%g1, %lo(dyn_data), %g1
2310Sstevel@tonic-gate  *	call	elf_plt_trace
2320Sstevel@tonic-gate  *	st	%g1, [%fp + -0x4] ! store dyn_data ptr in [%fp - 4]
2330Sstevel@tonic-gate  * dyn data:
2340Sstevel@tonic-gate  *	uintptr_t	reflmp
2350Sstevel@tonic-gate  *	uintptr_t	deflmp
2360Sstevel@tonic-gate  *	ulong_t		symndx
2370Sstevel@tonic-gate  *	ulong_t		sb_flags
2380Sstevel@tonic-gate  *	Sym		symdef
2390Sstevel@tonic-gate  */
2400Sstevel@tonic-gate static caddr_t
2410Sstevel@tonic-gate elf_plt_trace_write(caddr_t addr, Rela *rptr, Rt_map *rlmp, Rt_map *dlmp,
2420Sstevel@tonic-gate     Sym *sym, ulong_t symndx, ulong_t pltndx, caddr_t to, ulong_t sb_flags,
2430Sstevel@tonic-gate     int *fail)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	extern ulong_t	elf_plt_trace();
2461618Srie 	uintptr_t	dyn_plt, *dyndata;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/*
2490Sstevel@tonic-gate 	 * If both pltenter & pltexit have been disabled there
2500Sstevel@tonic-gate 	 * there is no reason to even create the glue code.
2510Sstevel@tonic-gate 	 */
2520Sstevel@tonic-gate 	if ((sb_flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)) ==
2530Sstevel@tonic-gate 	    (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)) {
2540Sstevel@tonic-gate 		(void) elf_plt_write((uintptr_t)addr, (uintptr_t)addr,
2550Sstevel@tonic-gate 		    rptr, (uintptr_t)to, pltndx);
2560Sstevel@tonic-gate 		return (to);
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/*
2600Sstevel@tonic-gate 	 * We only need to add the glue code if there is an auditing
2610Sstevel@tonic-gate 	 * library that is interested in this binding.
2620Sstevel@tonic-gate 	 */
2630Sstevel@tonic-gate 	dyn_plt = (uintptr_t)AUDINFO(rlmp)->ai_dynplts +
2644679Srie 	    (pltndx * dyn_plt_ent_size);
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	/*
2670Sstevel@tonic-gate 	 * Have we initialized this dynamic plt entry yet?  If we haven't do it
2680Sstevel@tonic-gate 	 * now.  Otherwise this function has been called before, but from a
2690Sstevel@tonic-gate 	 * different plt (ie. from another shared object).  In that case
2700Sstevel@tonic-gate 	 * we just set the plt to point to the new dyn_plt.
2710Sstevel@tonic-gate 	 */
2720Sstevel@tonic-gate 	if (*(uint_t *)dyn_plt == 0) {
2731618Srie 		Sym	*symp;
2740Sstevel@tonic-gate 		Xword	symvalue;
2751618Srie 		Lm_list	*lml = LIST(rlmp);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		(void) memcpy((void *)dyn_plt, dyn_plt_template,
2780Sstevel@tonic-gate 		    sizeof (dyn_plt_template));
2790Sstevel@tonic-gate 		dyndata = (uintptr_t *)(dyn_plt + sizeof (dyn_plt_template));
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 		/*
2820Sstevel@tonic-gate 		 * relocating:
2830Sstevel@tonic-gate 		 *	sethi	%hi(dyndata), %g1
2840Sstevel@tonic-gate 		 */
2850Sstevel@tonic-gate 		symvalue = (Xword)dyndata;
2860Sstevel@tonic-gate 		if (do_reloc(R_SPARC_HI22, (uchar_t *)(dyn_plt + 0x20),
2870Sstevel@tonic-gate 		    &symvalue, MSG_ORIG(MSG_SYM_LADYNDATA),
2881618Srie 		    MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) {
2890Sstevel@tonic-gate 			*fail = 1;
2900Sstevel@tonic-gate 			return (0);
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		/*
2940Sstevel@tonic-gate 		 * relocating:
2950Sstevel@tonic-gate 		 *	or	%g1, %lo(dyndata), %g1
2960Sstevel@tonic-gate 		 */
2970Sstevel@tonic-gate 		symvalue = (Xword)dyndata;
2980Sstevel@tonic-gate 		if (do_reloc(R_SPARC_LO10, (uchar_t *)(dyn_plt + 0x24),
2990Sstevel@tonic-gate 		    &symvalue, MSG_ORIG(MSG_SYM_LADYNDATA),
3001618Srie 		    MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) {
3010Sstevel@tonic-gate 			*fail = 1;
3020Sstevel@tonic-gate 			return (0);
3030Sstevel@tonic-gate 		}
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		/*
3060Sstevel@tonic-gate 		 * relocating:
3070Sstevel@tonic-gate 		 *	call	elf_plt_trace
3080Sstevel@tonic-gate 		 */
3090Sstevel@tonic-gate 		symvalue = (Xword)((uintptr_t)&elf_plt_trace -
3101618Srie 		    (dyn_plt + 0x28));
3110Sstevel@tonic-gate 		if (do_reloc(R_SPARC_WDISP30, (uchar_t *)(dyn_plt + 0x28),
3120Sstevel@tonic-gate 		    &symvalue, MSG_ORIG(MSG_SYM_ELFPLTTRACE),
3131618Srie 		    MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) {
3140Sstevel@tonic-gate 			*fail = 1;
3150Sstevel@tonic-gate 			return (0);
3160Sstevel@tonic-gate 		}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 		*dyndata++ = (uintptr_t)rlmp;
3190Sstevel@tonic-gate 		*dyndata++ = (uintptr_t)dlmp;
3200Sstevel@tonic-gate 		*(ulong_t *)dyndata++ = symndx;
3210Sstevel@tonic-gate 		*(ulong_t *)dyndata++ = sb_flags;
3220Sstevel@tonic-gate 		symp = (Sym *)dyndata;
3230Sstevel@tonic-gate 		*symp = *sym;
3240Sstevel@tonic-gate 		symp->st_name += (Word)STRTAB(dlmp);
3250Sstevel@tonic-gate 		symp->st_value = (Addr)to;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 		iflush_range((void *)dyn_plt, sizeof (dyn_plt_template));
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	(void) elf_plt_write((uintptr_t)addr, (uintptr_t)addr,
3314679Srie 	    rptr, (uintptr_t)dyn_plt, 0);
3320Sstevel@tonic-gate 	return ((caddr_t)dyn_plt);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate /*
3370Sstevel@tonic-gate  * Function binding routine - invoked on the first call to a function through
3380Sstevel@tonic-gate  * the procedure linkage table;
3390Sstevel@tonic-gate  * passes first through an assembly language interface.
3400Sstevel@tonic-gate  *
3410Sstevel@tonic-gate  * Takes the address of the PLT entry where the call originated,
3420Sstevel@tonic-gate  * the offset into the relocation table of the associated
3430Sstevel@tonic-gate  * relocation entry and the address of the link map (rt_private_map struct)
3440Sstevel@tonic-gate  * for the entry.
3450Sstevel@tonic-gate  *
3460Sstevel@tonic-gate  * Returns the address of the function referenced after re-writing the PLT
3470Sstevel@tonic-gate  * entry to invoke the function directly.
3480Sstevel@tonic-gate  *
3490Sstevel@tonic-gate  * On error, causes process to terminate with a signal.
3500Sstevel@tonic-gate  */
3510Sstevel@tonic-gate ulong_t
3520Sstevel@tonic-gate elf_bndr(Rt_map *lmp, ulong_t pltoff, caddr_t from)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate 	Rt_map		*nlmp, *llmp;
3550Sstevel@tonic-gate 	ulong_t		addr, vaddr, reloff, symval, rsymndx;
3560Sstevel@tonic-gate 	char		*name;
3570Sstevel@tonic-gate 	Rela		*rptr;
3580Sstevel@tonic-gate 	Sym		*sym, *nsym;
3590Sstevel@tonic-gate 	Xword		pltndx;
3600Sstevel@tonic-gate 	uint_t		binfo, sb_flags = 0;
3610Sstevel@tonic-gate 	Slookup		sl;
3620Sstevel@tonic-gate 	Pltbindtype	pbtype;
3631618Srie 	int		entry, lmflags;
3641618Srie 	uint_t		dbg_class;
3651618Srie 	Lm_list		*lml = LIST(lmp);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	/*
3680Sstevel@tonic-gate 	 * For compatibility with libthread (TI_VERSION 1) we track the entry
3690Sstevel@tonic-gate 	 * value.  A zero value indicates we have recursed into ld.so.1 to
3700Sstevel@tonic-gate 	 * further process a locking request.  Under this recursion we disable
3710Sstevel@tonic-gate 	 * tsort and cleanup activities.
3720Sstevel@tonic-gate 	 */
3730Sstevel@tonic-gate 	entry = enter();
3740Sstevel@tonic-gate 
3751618Srie 	if ((lmflags = lml->lm_flags) & LML_FLG_RTLDLM) {
3761618Srie 		dbg_class = dbg_desc->d_class;
3771618Srie 		dbg_desc->d_class = 0;
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	/*
3810Sstevel@tonic-gate 	 * Must calculate true plt relocation address from reloc.
3820Sstevel@tonic-gate 	 * Take offset, subtract number of reserved PLT entries, and divide
3830Sstevel@tonic-gate 	 * by PLT entry size, which should give the index of the plt
3840Sstevel@tonic-gate 	 * entry (and relocation entry since they have been defined to be
3850Sstevel@tonic-gate 	 * in the same order).  Then we must multiply by the size of
3860Sstevel@tonic-gate 	 * a relocation entry, which will give us the offset of the
3870Sstevel@tonic-gate 	 * plt relocation entry from the start of them given by JMPREL(lm).
3880Sstevel@tonic-gate 	 */
3890Sstevel@tonic-gate 	addr = pltoff - M_PLT_RESERVSZ;
3900Sstevel@tonic-gate 	pltndx = addr / M_PLT_ENTSIZE;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	/*
3930Sstevel@tonic-gate 	 * Perform some basic sanity checks.  If we didn't get a load map
3940Sstevel@tonic-gate 	 * or the plt offset is invalid then its possible someone has walked
3950Sstevel@tonic-gate 	 * over the plt entries or jumped to plt0 out of the blue.
3960Sstevel@tonic-gate 	 */
3970Sstevel@tonic-gate 	if (!lmp || ((addr % M_PLT_ENTSIZE) != 0)) {
398*4734Sab196087 		Conv_inv_buf_t	inv_buf;
399*4734Sab196087 
4001618Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_PLTREF),
401*4734Sab196087 		    conv_reloc_SPARC_type(R_SPARC_JMP_SLOT, 0, &inv_buf),
4021618Srie 		    EC_NATPTR(lmp), EC_XWORD(pltoff), EC_NATPTR(from));
4031618Srie 		rtldexit(lml, 1);
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 	reloff = pltndx * sizeof (Rela);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	/*
4080Sstevel@tonic-gate 	 * Use relocation entry to get symbol table entry and symbol name.
4090Sstevel@tonic-gate 	 */
4100Sstevel@tonic-gate 	addr = (ulong_t)JMPREL(lmp);
4110Sstevel@tonic-gate 	rptr = (Rela *)(addr + reloff);
4120Sstevel@tonic-gate 	rsymndx = ELF_R_SYM(rptr->r_info);
4130Sstevel@tonic-gate 	sym = (Sym *)((ulong_t)SYMTAB(lmp) + (rsymndx * SYMENT(lmp)));
4140Sstevel@tonic-gate 	name = (char *)(STRTAB(lmp) + sym->st_name);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	/*
4170Sstevel@tonic-gate 	 * Determine the last link-map of this list, this'll be the starting
4180Sstevel@tonic-gate 	 * point for any tsort() processing.
4190Sstevel@tonic-gate 	 */
4201618Srie 	llmp = lml->lm_tail;
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	/*
4230Sstevel@tonic-gate 	 * Find definition for symbol.
4240Sstevel@tonic-gate 	 */
4250Sstevel@tonic-gate 	sl.sl_name = name;
4260Sstevel@tonic-gate 	sl.sl_cmap = lmp;
4271618Srie 	sl.sl_imap = lml->lm_head;
4280Sstevel@tonic-gate 	sl.sl_hash = 0;
4290Sstevel@tonic-gate 	sl.sl_rsymndx = rsymndx;
4300Sstevel@tonic-gate 	sl.sl_flags = LKUP_DEFT;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	if ((nsym = lookup_sym(&sl, &nlmp, &binfo)) == 0) {
4331618Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOSYM), NAME(lmp),
4340Sstevel@tonic-gate 		    demangle(name));
4351618Srie 		rtldexit(lml, 1);
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	symval = nsym->st_value;
4390Sstevel@tonic-gate 	if (!(FLAGS(nlmp) & FLG_RT_FIXED) &&
4400Sstevel@tonic-gate 	    (nsym->st_shndx != SHN_ABS))
4410Sstevel@tonic-gate 		symval += ADDR(nlmp);
4420Sstevel@tonic-gate 	if ((lmp != nlmp) && ((FLAGS1(nlmp) & FL1_RT_NOINIFIN) == 0)) {
4430Sstevel@tonic-gate 		/*
4440Sstevel@tonic-gate 		 * Record that this new link map is now bound to the caller.
4450Sstevel@tonic-gate 		 */
4460Sstevel@tonic-gate 		if (bind_one(lmp, nlmp, BND_REFER) == 0)
4471618Srie 			rtldexit(lml, 1);
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate 
4501618Srie 	if ((lml->lm_tflags | FLAGS1(lmp)) & LML_TFLG_AUD_SYMBIND) {
4510Sstevel@tonic-gate 		ulong_t	symndx = (((uintptr_t)nsym -
4524679Srie 		    (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp));
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		symval = audit_symbind(lmp, nlmp, nsym, symndx, symval,
4554679Srie 		    &sb_flags);
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	if (FLAGS(lmp) & FLG_RT_FIXED)
4590Sstevel@tonic-gate 		vaddr = 0;
4600Sstevel@tonic-gate 	else
4610Sstevel@tonic-gate 		vaddr = ADDR(lmp);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	pbtype = PLT_T_NONE;
4640Sstevel@tonic-gate 	if (!(rtld_flags & RT_FL_NOBIND)) {
4651618Srie 		if (((lml->lm_tflags | FLAGS1(lmp)) &
4660Sstevel@tonic-gate 		    (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) &&
4670Sstevel@tonic-gate 		    AUDINFO(lmp)->ai_dynplts) {
4680Sstevel@tonic-gate 			int	fail = 0;
4690Sstevel@tonic-gate 			ulong_t	symndx = (((uintptr_t)nsym -
4704679Srie 			    (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp));
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 			symval = (ulong_t)elf_plt_trace_write((caddr_t)vaddr,
4730Sstevel@tonic-gate 			    rptr, lmp, nlmp, nsym, symndx, pltndx,
4740Sstevel@tonic-gate 			    (caddr_t)symval, sb_flags, &fail);
4750Sstevel@tonic-gate 			if (fail)
4761618Srie 				rtldexit(lml, 1);
4770Sstevel@tonic-gate 		} else {
4780Sstevel@tonic-gate 			/*
4790Sstevel@tonic-gate 			 * Write standard PLT entry to jump directly
4800Sstevel@tonic-gate 			 * to newly bound function.
4810Sstevel@tonic-gate 			 */
4820Sstevel@tonic-gate 			pbtype = elf_plt_write((uintptr_t)vaddr,
4834679Srie 			    (uintptr_t)vaddr, rptr, symval, pltndx);
4840Sstevel@tonic-gate 		}
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/*
4880Sstevel@tonic-gate 	 * Print binding information and rebuild PLT entry.
4890Sstevel@tonic-gate 	 */
4901618Srie 	DBG_CALL(Dbg_bind_global(lmp, (Addr)from, (Off)(from - ADDR(lmp)),
4911618Srie 	    pltndx, pbtype, nlmp, (Addr)symval, nsym->st_value, name, binfo));
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	/*
4940Sstevel@tonic-gate 	 * Complete any processing for newly loaded objects.  Note we don't
4950Sstevel@tonic-gate 	 * know exactly where any new objects are loaded (we know the object
4960Sstevel@tonic-gate 	 * that supplied the symbol, but others may have been loaded lazily as
4970Sstevel@tonic-gate 	 * we searched for the symbol), so sorting starts from the last
4980Sstevel@tonic-gate 	 * link-map know on entry to this routine.
4990Sstevel@tonic-gate 	 */
5000Sstevel@tonic-gate 	if (entry)
5014679Srie 		load_completion(llmp);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	/*
5040Sstevel@tonic-gate 	 * Some operations like dldump() or dlopen()'ing a relocatable object
5050Sstevel@tonic-gate 	 * result in objects being loaded on rtld's link-map, make sure these
5060Sstevel@tonic-gate 	 * objects are initialized also.
5070Sstevel@tonic-gate 	 */
5080Sstevel@tonic-gate 	if ((LIST(nlmp)->lm_flags & LML_FLG_RTLDLM) && LIST(nlmp)->lm_init)
5094679Srie 		load_completion(nlmp);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	/*
5120Sstevel@tonic-gate 	 * If the object we've bound to is in the process of being initialized
5130Sstevel@tonic-gate 	 * by another thread, determine whether we should block.
5140Sstevel@tonic-gate 	 */
5150Sstevel@tonic-gate 	is_dep_ready(nlmp, lmp, DBG_WAIT_SYMBOL);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	/*
5180Sstevel@tonic-gate 	 * Make sure the object to which we've bound has had it's .init fired.
5190Sstevel@tonic-gate 	 * Cleanup before return to user code.
5200Sstevel@tonic-gate 	 */
5210Sstevel@tonic-gate 	if (entry) {
5220Sstevel@tonic-gate 		is_dep_init(nlmp, lmp);
5231618Srie 		leave(lml);
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	if (lmflags & LML_FLG_RTLDLM)
5271618Srie 		dbg_desc->d_class = dbg_class;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	return (symval);
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate  * Read and process the relocations for one link object, we assume all
5350Sstevel@tonic-gate  * relocation sections for loadable segments are stored contiguously in
5360Sstevel@tonic-gate  * the file.
5370Sstevel@tonic-gate  */
5380Sstevel@tonic-gate int
5390Sstevel@tonic-gate elf_reloc(Rt_map *lmp, uint_t plt)
5400Sstevel@tonic-gate {
5410Sstevel@tonic-gate 	ulong_t		relbgn, relend, relsiz, basebgn, pltbgn, pltend;
5420Sstevel@tonic-gate 	ulong_t		roffset, rsymndx, psymndx = 0, etext = ETEXT(lmp);
5430Sstevel@tonic-gate 	ulong_t		emap, dsymndx, pltndx;
5440Sstevel@tonic-gate 	uchar_t		rtype;
5450Sstevel@tonic-gate 	long		reladd, value, pvalue;
5460Sstevel@tonic-gate 	Sym		*symref, *psymref, *symdef, *psymdef;
5470Sstevel@tonic-gate 	char		*name, *pname;
5480Sstevel@tonic-gate 	Rt_map		*_lmp, *plmp;
5490Sstevel@tonic-gate 	int		textrel = 0, ret = 1, noplt = 0;
5500Sstevel@tonic-gate 	long		relacount = RELACOUNT(lmp);
5510Sstevel@tonic-gate 	Rela		*rel;
5520Sstevel@tonic-gate 	Pltbindtype	pbtype;
5530Sstevel@tonic-gate 	uint_t		binfo, pbinfo;
5540Sstevel@tonic-gate 	Alist		*bound = 0;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	/*
5570Sstevel@tonic-gate 	 * If an object has any DT_REGISTER entries associated with
5580Sstevel@tonic-gate 	 * it, they are processed now.
5590Sstevel@tonic-gate 	 */
5600Sstevel@tonic-gate 	if ((plt == 0) && (FLAGS(lmp) & FLG_RT_REGSYMS)) {
5610Sstevel@tonic-gate 		if (elf_regsyms(lmp) == 0)
5620Sstevel@tonic-gate 			return (0);
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	/*
5660Sstevel@tonic-gate 	 * Although only necessary for lazy binding, initialize the first
5670Sstevel@tonic-gate 	 * procedure linkage table entry to go to elf_rtbndr().  dbx(1) seems
5680Sstevel@tonic-gate 	 * to find this useful.
5690Sstevel@tonic-gate 	 */
5700Sstevel@tonic-gate 	if ((plt == 0) && PLTGOT(lmp)) {
5710Sstevel@tonic-gate 		if ((ulong_t)PLTGOT(lmp) < etext) {
5720Sstevel@tonic-gate 			if (elf_set_prot(lmp, PROT_WRITE) == 0)
5730Sstevel@tonic-gate 				return (0);
5740Sstevel@tonic-gate 			textrel = 1;
5750Sstevel@tonic-gate 		}
5760Sstevel@tonic-gate 		elf_plt_init(PLTGOT(lmp), (caddr_t)lmp);
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	/*
5800Sstevel@tonic-gate 	 * Initialize the plt start and end addresses.
5810Sstevel@tonic-gate 	 */
5820Sstevel@tonic-gate 	if ((pltbgn = (ulong_t)JMPREL(lmp)) != 0)
5830Sstevel@tonic-gate 		pltend = pltbgn + (ulong_t)(PLTRELSZ(lmp));
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	/*
5860Sstevel@tonic-gate 	 * If we've been called upon to promote an RTLD_LAZY object to an
5870Sstevel@tonic-gate 	 * RTLD_NOW then we're only interested in scaning the .plt table.
5880Sstevel@tonic-gate 	 */
5890Sstevel@tonic-gate 	if (plt) {
5900Sstevel@tonic-gate 		relbgn = pltbgn;
5910Sstevel@tonic-gate 		relend = pltend;
5920Sstevel@tonic-gate 	} else {
5930Sstevel@tonic-gate 		/*
5940Sstevel@tonic-gate 		 * The relocation sections appear to the run-time linker as a
5950Sstevel@tonic-gate 		 * single table.  Determine the address of the beginning and end
5960Sstevel@tonic-gate 		 * of this table.  There are two different interpretations of
5970Sstevel@tonic-gate 		 * the ABI at this point:
5980Sstevel@tonic-gate 		 *
5990Sstevel@tonic-gate 		 *   o	The REL table and its associated RELSZ indicate the
6000Sstevel@tonic-gate 		 *	concatenation of *all* relocation sections (this is the
6010Sstevel@tonic-gate 		 *	model our link-editor constructs).
6020Sstevel@tonic-gate 		 *
6030Sstevel@tonic-gate 		 *   o	The REL table and its associated RELSZ indicate the
6040Sstevel@tonic-gate 		 *	concatenation of all *but* the .plt relocations.  These
6050Sstevel@tonic-gate 		 *	relocations are specified individually by the JMPREL and
6060Sstevel@tonic-gate 		 *	PLTRELSZ entries.
6070Sstevel@tonic-gate 		 *
6080Sstevel@tonic-gate 		 * Determine from our knowledege of the relocation range and
6090Sstevel@tonic-gate 		 * .plt range, the range of the total relocation table.  Note
6100Sstevel@tonic-gate 		 * that one other ABI assumption seems to be that the .plt
6110Sstevel@tonic-gate 		 * relocations always follow any other relocations, the
6120Sstevel@tonic-gate 		 * following range checking drops that assumption.
6130Sstevel@tonic-gate 		 */
6140Sstevel@tonic-gate 		relbgn = (ulong_t)(REL(lmp));
6150Sstevel@tonic-gate 		relend = relbgn + (ulong_t)(RELSZ(lmp));
6160Sstevel@tonic-gate 		if (pltbgn) {
6170Sstevel@tonic-gate 			if (!relbgn || (relbgn > pltbgn))
6180Sstevel@tonic-gate 				relbgn = pltbgn;
6190Sstevel@tonic-gate 			if (!relbgn || (relend < pltend))
6200Sstevel@tonic-gate 				relend = pltend;
6210Sstevel@tonic-gate 		}
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 	if (!relbgn || (relbgn == relend)) {
6241618Srie 		DBG_CALL(Dbg_reloc_run(lmp, 0, plt, DBG_REL_NONE));
6250Sstevel@tonic-gate 		return (1);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	relsiz = (ulong_t)(RELENT(lmp));
6290Sstevel@tonic-gate 	basebgn = ADDR(lmp);
6300Sstevel@tonic-gate 	emap = ADDR(lmp) + MSIZE(lmp);
6310Sstevel@tonic-gate 
6321618Srie 	DBG_CALL(Dbg_reloc_run(lmp, M_REL_SHT_TYPE, plt, DBG_REL_START));
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	/*
6350Sstevel@tonic-gate 	 * If we're processing in lazy mode there is no need to scan the
6360Sstevel@tonic-gate 	 * .rela.plt table.
6370Sstevel@tonic-gate 	 */
6380Sstevel@tonic-gate 	if (pltbgn && ((MODE(lmp) & RTLD_NOW) == 0))
6390Sstevel@tonic-gate 		noplt = 1;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	/*
6420Sstevel@tonic-gate 	 * Loop through relocations.
6430Sstevel@tonic-gate 	 */
6440Sstevel@tonic-gate 	while (relbgn < relend) {
6450Sstevel@tonic-gate 		Addr		vaddr;
6460Sstevel@tonic-gate 		uint_t		sb_flags = 0;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		rtype = ELF_R_TYPE(((Rela *)relbgn)->r_info);
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 		/*
6510Sstevel@tonic-gate 		 * If this is a RELATIVE relocation in a shared object (the
6520Sstevel@tonic-gate 		 * common case), and if we are not debugging, then jump into a
6530Sstevel@tonic-gate 		 * tighter relocation loop (elf_reloc_relative).  Only make the
6540Sstevel@tonic-gate 		 * jump if we've been given a hint on the number of relocations.
6550Sstevel@tonic-gate 		 */
6560Sstevel@tonic-gate 		if ((rtype == R_SPARC_RELATIVE) &&
6571618Srie 		    ((FLAGS(lmp) & FLG_RT_FIXED) == 0) && (DBG_ENABLED == 0)) {
6580Sstevel@tonic-gate 			/*
6590Sstevel@tonic-gate 			 * It's possible that the relative relocation block
6600Sstevel@tonic-gate 			 * has relocations against the text segment as well
6610Sstevel@tonic-gate 			 * as the data segment.  Since our optimized relocation
6620Sstevel@tonic-gate 			 * engine does not check which segment the relocation
6630Sstevel@tonic-gate 			 * is against - just mprotect it now if it's been
6640Sstevel@tonic-gate 			 * marked as containing TEXTREL's.
6650Sstevel@tonic-gate 			 */
6660Sstevel@tonic-gate 			if ((textrel == 0) && (FLAGS1(lmp) & FL1_RT_TEXTREL)) {
6670Sstevel@tonic-gate 				if (elf_set_prot(lmp, PROT_WRITE) == 0) {
6680Sstevel@tonic-gate 					ret = 0;
6690Sstevel@tonic-gate 					break;
6700Sstevel@tonic-gate 				}
6710Sstevel@tonic-gate 				textrel = 1;
6720Sstevel@tonic-gate 			}
6730Sstevel@tonic-gate 			if (relacount) {
6740Sstevel@tonic-gate 				relbgn = elf_reloc_relacount(relbgn, relacount,
6750Sstevel@tonic-gate 				    relsiz, basebgn);
6760Sstevel@tonic-gate 				relacount = 0;
6770Sstevel@tonic-gate 			} else {
6780Sstevel@tonic-gate 				relbgn = elf_reloc_relative(relbgn, relend,
6790Sstevel@tonic-gate 				    relsiz, basebgn, etext, emap);
6800Sstevel@tonic-gate 			}
6810Sstevel@tonic-gate 			if (relbgn >= relend)
6820Sstevel@tonic-gate 				break;
6830Sstevel@tonic-gate 			rtype = ELF_R_TYPE(((Rela *)relbgn)->r_info);
6840Sstevel@tonic-gate 		}
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		roffset = ((Rela *)relbgn)->r_offset;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 		reladd = (long)(((Rela *)relbgn)->r_addend);
6890Sstevel@tonic-gate 		rsymndx = ELF_R_SYM(((Rela *)relbgn)->r_info);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		rel = (Rela *)relbgn;
6920Sstevel@tonic-gate 		relbgn += relsiz;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		/*
6950Sstevel@tonic-gate 		 * Optimizations.
6960Sstevel@tonic-gate 		 */
6970Sstevel@tonic-gate 		if (rtype == R_SPARC_NONE)
6980Sstevel@tonic-gate 			continue;
6990Sstevel@tonic-gate 		if (noplt && ((ulong_t)rel >= pltbgn) &&
7000Sstevel@tonic-gate 		    ((ulong_t)rel < pltend)) {
7010Sstevel@tonic-gate 			relbgn = pltend;
7020Sstevel@tonic-gate 			continue;
7030Sstevel@tonic-gate 		}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 		if (rtype != R_SPARC_REGISTER) {
7060Sstevel@tonic-gate 			/*
7070Sstevel@tonic-gate 			 * If this is a shared object, add the base address
7080Sstevel@tonic-gate 			 * to offset.
7090Sstevel@tonic-gate 			 */
7100Sstevel@tonic-gate 			if (!(FLAGS(lmp) & FLG_RT_FIXED))
7110Sstevel@tonic-gate 				roffset += basebgn;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 			/*
7140Sstevel@tonic-gate 			 * If this relocation is not against part of the image
7150Sstevel@tonic-gate 			 * mapped into memory we skip it.
7160Sstevel@tonic-gate 			 */
7170Sstevel@tonic-gate 			if ((roffset < ADDR(lmp)) || (roffset > (ADDR(lmp) +
7180Sstevel@tonic-gate 			    MSIZE(lmp)))) {
7190Sstevel@tonic-gate 				elf_reloc_bad(lmp, (void *)rel, rtype, roffset,
7200Sstevel@tonic-gate 				    rsymndx);
7210Sstevel@tonic-gate 				continue;
7220Sstevel@tonic-gate 			}
7230Sstevel@tonic-gate 		}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 		/*
7263731Srie 		 * If we're promoting .plts try and determine if this one has
7273731Srie 		 * already been written.  An uninitialized .plts' second
7283731Srie 		 * instruction is a branch.  Note, elf_plt_write() optimizes
7293731Srie 		 * .plt relocations, and it's possible that a relocated entry
7303731Srie 		 * is a branch.  If this is the case, we can't tell the
7313731Srie 		 * difference between an uninitialized .plt and a relocated,
7323731Srie 		 * .plt that uses a branch.  In this case, we'll simply redo
7333731Srie 		 * the relocation calculation, which is a bit sad.
7340Sstevel@tonic-gate 		 */
7350Sstevel@tonic-gate 		if (plt) {
7360Sstevel@tonic-gate 			ulong_t	*_roffset = (ulong_t *)roffset;
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 			_roffset++;
7390Sstevel@tonic-gate 			if ((*_roffset & (~(S_MASK(22)))) != M_BA_A)
7400Sstevel@tonic-gate 				continue;
7410Sstevel@tonic-gate 		}
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 		binfo = 0;
7440Sstevel@tonic-gate 		pltndx = (ulong_t)-1;
7450Sstevel@tonic-gate 		pbtype = PLT_T_NONE;
7460Sstevel@tonic-gate 		/*
7470Sstevel@tonic-gate 		 * If a symbol index is specified then get the symbol table
7480Sstevel@tonic-gate 		 * entry, locate the symbol definition, and determine its
7490Sstevel@tonic-gate 		 * address.
7500Sstevel@tonic-gate 		 */
7510Sstevel@tonic-gate 		if (rsymndx) {
7520Sstevel@tonic-gate 			/*
7530Sstevel@tonic-gate 			 * Get the local symbol table entry.
7540Sstevel@tonic-gate 			 */
7550Sstevel@tonic-gate 			symref = (Sym *)((ulong_t)SYMTAB(lmp) +
7560Sstevel@tonic-gate 			    (rsymndx * SYMENT(lmp)));
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 			/*
7590Sstevel@tonic-gate 			 * If this is a local symbol, just use the base address.
7600Sstevel@tonic-gate 			 * (we should have no local relocations in the
7610Sstevel@tonic-gate 			 * executable).
7620Sstevel@tonic-gate 			 */
7630Sstevel@tonic-gate 			if (ELF_ST_BIND(symref->st_info) == STB_LOCAL) {
7640Sstevel@tonic-gate 				value = basebgn;
7650Sstevel@tonic-gate 				name = (char *)0;
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 				/*
7682145Srie 				 * Special case TLS relocations.
7690Sstevel@tonic-gate 				 */
7702145Srie 				if (rtype == R_SPARC_TLS_DTPMOD32) {
7712145Srie 					/*
7722145Srie 					 * Use the TLS modid.
7732145Srie 					 */
7740Sstevel@tonic-gate 					value = TLSMODID(lmp);
7752145Srie 
7762145Srie 				} else if (rtype == R_SPARC_TLS_TPOFF32) {
7772145Srie 					if ((value = elf_static_tls(lmp, symref,
7782145Srie 					    rel, rtype, 0, roffset, 0)) == 0) {
7792145Srie 						ret = 0;
7802145Srie 						break;
7812145Srie 					}
7822145Srie 				}
7830Sstevel@tonic-gate 			} else {
7840Sstevel@tonic-gate 				/*
7850Sstevel@tonic-gate 				 * If the symbol index is equal to the previous
7860Sstevel@tonic-gate 				 * symbol index relocation we processed then
7870Sstevel@tonic-gate 				 * reuse the previous values. (Note that there
7880Sstevel@tonic-gate 				 * have been cases where a relocation exists
7890Sstevel@tonic-gate 				 * against a copy relocation symbol, our ld(1)
7900Sstevel@tonic-gate 				 * should optimize this away, but make sure we
7910Sstevel@tonic-gate 				 * don't use the same symbol information should
7920Sstevel@tonic-gate 				 * this case exist).
7930Sstevel@tonic-gate 				 */
7940Sstevel@tonic-gate 				if ((rsymndx == psymndx) &&
7950Sstevel@tonic-gate 				    (rtype != R_SPARC_COPY)) {
7960Sstevel@tonic-gate 					/* LINTED */
7970Sstevel@tonic-gate 					if (psymdef == 0) {
7981618Srie 						DBG_CALL(Dbg_bind_weak(lmp,
7991618Srie 						    (Addr)roffset, (Addr)
8000Sstevel@tonic-gate 						    (roffset - basebgn), name));
8010Sstevel@tonic-gate 						continue;
8020Sstevel@tonic-gate 					}
8030Sstevel@tonic-gate 					/* LINTED */
8040Sstevel@tonic-gate 					value = pvalue;
8050Sstevel@tonic-gate 					/* LINTED */
8060Sstevel@tonic-gate 					name = pname;
8070Sstevel@tonic-gate 					symdef = psymdef;
8080Sstevel@tonic-gate 					/* LINTED */
8090Sstevel@tonic-gate 					symref = psymref;
8100Sstevel@tonic-gate 					/* LINTED */
8110Sstevel@tonic-gate 					_lmp = plmp;
8120Sstevel@tonic-gate 					/* LINTED */
8130Sstevel@tonic-gate 					binfo = pbinfo;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 					if ((LIST(_lmp)->lm_tflags |
8160Sstevel@tonic-gate 					    FLAGS1(_lmp)) &
8170Sstevel@tonic-gate 					    LML_TFLG_AUD_SYMBIND) {
8180Sstevel@tonic-gate 						value = audit_symbind(lmp, _lmp,
8190Sstevel@tonic-gate 						    /* LINTED */
8200Sstevel@tonic-gate 						    symdef, dsymndx, value,
8210Sstevel@tonic-gate 						    &sb_flags);
8220Sstevel@tonic-gate 					}
8230Sstevel@tonic-gate 				} else {
8240Sstevel@tonic-gate 					Slookup		sl;
8250Sstevel@tonic-gate 					uchar_t		bind;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 					/*
8280Sstevel@tonic-gate 					 * Lookup the symbol definition.
8290Sstevel@tonic-gate 					 */
8300Sstevel@tonic-gate 					name = (char *)(STRTAB(lmp) +
8310Sstevel@tonic-gate 					    symref->st_name);
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 					sl.sl_name = name;
8340Sstevel@tonic-gate 					sl.sl_cmap = lmp;
8350Sstevel@tonic-gate 					sl.sl_imap = 0;
8360Sstevel@tonic-gate 					sl.sl_hash = 0;
8370Sstevel@tonic-gate 					sl.sl_rsymndx = rsymndx;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 					if (rtype == R_SPARC_COPY)
8400Sstevel@tonic-gate 						sl.sl_flags = LKUP_COPY;
8410Sstevel@tonic-gate 					else
8420Sstevel@tonic-gate 						sl.sl_flags = LKUP_DEFT;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 					sl.sl_flags |= LKUP_ALLCNTLIST;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 					if (rtype != R_SPARC_JMP_SLOT)
8470Sstevel@tonic-gate 						sl.sl_flags |= LKUP_SPEC;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 					bind = ELF_ST_BIND(symref->st_info);
8500Sstevel@tonic-gate 					if (bind == STB_WEAK)
8510Sstevel@tonic-gate 						sl.sl_flags |= LKUP_WEAK;
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 					symdef = lookup_sym(&sl, &_lmp, &binfo);
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 					/*
8560Sstevel@tonic-gate 					 * If the symbol is not found and the
8570Sstevel@tonic-gate 					 * reference was not to a weak symbol,
8580Sstevel@tonic-gate 					 * report an error.  Weak references
8590Sstevel@tonic-gate 					 * may be unresolved.
8600Sstevel@tonic-gate 					 */
8614679Srie 					/* BEGIN CSTYLED */
8620Sstevel@tonic-gate 					if (symdef == 0) {
8631618Srie 					    Lm_list	*lml = LIST(lmp);
8641618Srie 
8650Sstevel@tonic-gate 					    if (bind != STB_WEAK) {
8661618Srie 						if (lml->lm_flags &
8670Sstevel@tonic-gate 						    LML_FLG_IGNRELERR) {
8680Sstevel@tonic-gate 						    continue;
8691618Srie 						} else if (lml->lm_flags &
8700Sstevel@tonic-gate 						    LML_FLG_TRC_WARN) {
8710Sstevel@tonic-gate 						    (void) printf(MSG_INTL(
8720Sstevel@tonic-gate 							MSG_LDD_SYM_NFOUND),
8730Sstevel@tonic-gate 							demangle(name),
8740Sstevel@tonic-gate 							NAME(lmp));
8750Sstevel@tonic-gate 						    continue;
8760Sstevel@tonic-gate 						} else {
8772145Srie 						    DBG_CALL(Dbg_reloc_in(lml,
8782145Srie 							ELF_DBG_RTLD, M_MACH,
8792145Srie 							M_REL_SHT_TYPE, rel,
8802145Srie 							NULL, name));
8811618Srie 						    eprintf(lml, ERR_FATAL,
8820Sstevel@tonic-gate 							MSG_INTL(MSG_REL_NOSYM),
8830Sstevel@tonic-gate 							NAME(lmp),
8840Sstevel@tonic-gate 							demangle(name));
8850Sstevel@tonic-gate 						    ret = 0;
8860Sstevel@tonic-gate 						    break;
8870Sstevel@tonic-gate 						}
8880Sstevel@tonic-gate 					    } else {
8890Sstevel@tonic-gate 						psymndx = rsymndx;
8900Sstevel@tonic-gate 						psymdef = 0;
8910Sstevel@tonic-gate 
8921618Srie 						DBG_CALL(Dbg_bind_weak(lmp,
8931618Srie 						    (Addr)roffset, (Addr)
8940Sstevel@tonic-gate 						    (roffset - basebgn), name));
8950Sstevel@tonic-gate 						continue;
8960Sstevel@tonic-gate 					    }
8970Sstevel@tonic-gate 					}
8984679Srie 					/* END CSTYLED */
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 					/*
9010Sstevel@tonic-gate 					 * If symbol was found in an object
9020Sstevel@tonic-gate 					 * other than the referencing object
9030Sstevel@tonic-gate 					 * then record the binding.
9040Sstevel@tonic-gate 					 */
9050Sstevel@tonic-gate 					if ((lmp != _lmp) && ((FLAGS1(_lmp) &
9060Sstevel@tonic-gate 					    FL1_RT_NOINIFIN) == 0)) {
9070Sstevel@tonic-gate 						if (alist_test(&bound, _lmp,
9080Sstevel@tonic-gate 						    sizeof (Rt_map *),
9090Sstevel@tonic-gate 						    AL_CNT_RELBIND) == 0) {
9100Sstevel@tonic-gate 							ret = 0;
9110Sstevel@tonic-gate 							break;
9120Sstevel@tonic-gate 						}
9130Sstevel@tonic-gate 					}
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 					/*
9160Sstevel@tonic-gate 					 * Calculate the location of definition;
9170Sstevel@tonic-gate 					 * symbol value plus base address of
9180Sstevel@tonic-gate 					 * containing shared object.
9190Sstevel@tonic-gate 					 */
9202850Srie 					if (IS_SIZE(rtype))
9212850Srie 						value = symdef->st_size;
9222850Srie 					else
9232850Srie 						value = symdef->st_value;
9242850Srie 
9250Sstevel@tonic-gate 					if (!(FLAGS(_lmp) & FLG_RT_FIXED) &&
9262850Srie 					    !(IS_SIZE(rtype)) &&
9270Sstevel@tonic-gate 					    (symdef->st_shndx != SHN_ABS) &&
9280Sstevel@tonic-gate 					    (ELF_ST_TYPE(symdef->st_info) !=
9290Sstevel@tonic-gate 					    STT_TLS))
9300Sstevel@tonic-gate 						value += ADDR(_lmp);
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 					/*
9330Sstevel@tonic-gate 					 * Retain this symbol index and the
9340Sstevel@tonic-gate 					 * value in case it can be used for the
9350Sstevel@tonic-gate 					 * subsequent relocations.
9360Sstevel@tonic-gate 					 */
9370Sstevel@tonic-gate 					if (rtype != R_SPARC_COPY) {
9380Sstevel@tonic-gate 						psymndx = rsymndx;
9390Sstevel@tonic-gate 						pvalue = value;
9400Sstevel@tonic-gate 						pname = name;
9410Sstevel@tonic-gate 						psymdef = symdef;
9420Sstevel@tonic-gate 						psymref = symref;
9430Sstevel@tonic-gate 						plmp = _lmp;
9440Sstevel@tonic-gate 						pbinfo = binfo;
9450Sstevel@tonic-gate 					}
9460Sstevel@tonic-gate 					if ((LIST(_lmp)->lm_tflags |
9470Sstevel@tonic-gate 					    FLAGS1(_lmp)) &
9480Sstevel@tonic-gate 					    LML_TFLG_AUD_SYMBIND) {
9490Sstevel@tonic-gate 						dsymndx = (((uintptr_t)symdef -
9500Sstevel@tonic-gate 						    (uintptr_t)SYMTAB(_lmp)) /
9510Sstevel@tonic-gate 						    SYMENT(_lmp));
9520Sstevel@tonic-gate 						value = audit_symbind(lmp, _lmp,
9530Sstevel@tonic-gate 						    symdef, dsymndx, value,
9540Sstevel@tonic-gate 						    &sb_flags);
9550Sstevel@tonic-gate 					}
9560Sstevel@tonic-gate 				}
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 				/*
9590Sstevel@tonic-gate 				 * If relocation is PC-relative, subtract
9600Sstevel@tonic-gate 				 * offset address.
9610Sstevel@tonic-gate 				 */
9620Sstevel@tonic-gate 				if (IS_PC_RELATIVE(rtype))
9630Sstevel@tonic-gate 					value -= roffset;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 				/*
9662145Srie 				 * Special case TLS relocations.
9670Sstevel@tonic-gate 				 */
9682145Srie 				if (rtype == R_SPARC_TLS_DTPMOD32) {
9692145Srie 					/*
9702145Srie 					 * Relocation value is the TLS modid.
9712145Srie 					 */
9720Sstevel@tonic-gate 					value = TLSMODID(_lmp);
9732145Srie 
9742145Srie 				} else if (rtype == R_SPARC_TLS_TPOFF32) {
9752145Srie 					if ((value = elf_static_tls(_lmp,
9762145Srie 					    symdef, rel, rtype, name, roffset,
9772145Srie 					    value)) == 0) {
9782145Srie 						ret = 0;
9792145Srie 						break;
9802145Srie 					}
9812145Srie 				}
9820Sstevel@tonic-gate 			}
9830Sstevel@tonic-gate 		} else {
9840Sstevel@tonic-gate 			/*
9852145Srie 			 * Special cases.
9860Sstevel@tonic-gate 			 */
9872145Srie 			if (rtype == R_SPARC_REGISTER) {
9882145Srie 				/*
9892145Srie 				 * A register symbol associated with symbol
9902145Srie 				 * index 0 is initialized (i.e. relocated) to
9912145Srie 				 * a constant in the r_addend field rather than
9922145Srie 				 * to a symbol value.
9932145Srie 				 */
9940Sstevel@tonic-gate 				value = 0;
9952145Srie 
9962145Srie 			} else if (rtype == R_SPARC_TLS_DTPMOD32) {
9972145Srie 				/*
9982145Srie 				 * TLS relocation value is the TLS modid.
9992145Srie 				 */
10000Sstevel@tonic-gate 				value = TLSMODID(lmp);
10012145Srie 			} else
10020Sstevel@tonic-gate 				value = basebgn;
10030Sstevel@tonic-gate 			name = (char *)0;
10040Sstevel@tonic-gate 		}
10050Sstevel@tonic-gate 
10062145Srie 		DBG_CALL(Dbg_reloc_in(LIST(lmp), ELF_DBG_RTLD, M_MACH,
10072145Srie 		    M_REL_SHT_TYPE, rel, NULL, name));
10082145Srie 
10090Sstevel@tonic-gate 		/*
10100Sstevel@tonic-gate 		 * If this object has relocations in the text segment, turn
10110Sstevel@tonic-gate 		 * off the write protect.
10120Sstevel@tonic-gate 		 */
10130Sstevel@tonic-gate 		if ((rtype != R_SPARC_REGISTER) && (roffset < etext) &&
10140Sstevel@tonic-gate 		    (textrel == 0)) {
10150Sstevel@tonic-gate 			if (elf_set_prot(lmp, PROT_WRITE) == 0) {
10160Sstevel@tonic-gate 				ret = 0;
10170Sstevel@tonic-gate 				break;
10180Sstevel@tonic-gate 			}
10190Sstevel@tonic-gate 			textrel = 1;
10200Sstevel@tonic-gate 		}
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 		/*
10230Sstevel@tonic-gate 		 * Call relocation routine to perform required relocation.
10240Sstevel@tonic-gate 		 */
10250Sstevel@tonic-gate 		switch (rtype) {
10260Sstevel@tonic-gate 		case R_SPARC_REGISTER:
10270Sstevel@tonic-gate 			/*
10280Sstevel@tonic-gate 			 * The v9 ABI 4.2.4 says that system objects may,
10290Sstevel@tonic-gate 			 * but are not required to, use register symbols
10300Sstevel@tonic-gate 			 * to inidcate how they use global registers. Thus
10310Sstevel@tonic-gate 			 * at least %g6, %g7 must be allowed in addition
10320Sstevel@tonic-gate 			 * to %g2 and %g3.
10330Sstevel@tonic-gate 			 */
10340Sstevel@tonic-gate 			value += reladd;
10350Sstevel@tonic-gate 			if (roffset == STO_SPARC_REGISTER_G1) {
10360Sstevel@tonic-gate 				set_sparc_g1(value);
10370Sstevel@tonic-gate 			} else if (roffset == STO_SPARC_REGISTER_G2) {
10380Sstevel@tonic-gate 				set_sparc_g2(value);
10390Sstevel@tonic-gate 			} else if (roffset == STO_SPARC_REGISTER_G3) {
10400Sstevel@tonic-gate 				set_sparc_g3(value);
10410Sstevel@tonic-gate 			} else if (roffset == STO_SPARC_REGISTER_G4) {
10420Sstevel@tonic-gate 				set_sparc_g4(value);
10430Sstevel@tonic-gate 			} else if (roffset == STO_SPARC_REGISTER_G5) {
10440Sstevel@tonic-gate 				set_sparc_g5(value);
10450Sstevel@tonic-gate 			} else if (roffset == STO_SPARC_REGISTER_G6) {
10460Sstevel@tonic-gate 				set_sparc_g6(value);
10470Sstevel@tonic-gate 			} else if (roffset == STO_SPARC_REGISTER_G7) {
10480Sstevel@tonic-gate 				set_sparc_g7(value);
10490Sstevel@tonic-gate 			} else {
10501618Srie 				eprintf(LIST(lmp), ERR_FATAL,
10511618Srie 				    MSG_INTL(MSG_REL_BADREG), NAME(lmp),
10521618Srie 				    EC_ADDR(roffset));
10530Sstevel@tonic-gate 				ret = 0;
10540Sstevel@tonic-gate 				break;
10550Sstevel@tonic-gate 			}
10560Sstevel@tonic-gate 
10571618Srie 			DBG_CALL(Dbg_reloc_apply_reg(LIST(lmp), ELF_DBG_RTLD,
10581618Srie 			    M_MACH, (Xword)roffset, (Xword)value));
10590Sstevel@tonic-gate 			break;
10600Sstevel@tonic-gate 		case R_SPARC_COPY:
10610Sstevel@tonic-gate 			if (elf_copy_reloc(name, symref, lmp, (void *)roffset,
10620Sstevel@tonic-gate 			    symdef, _lmp, (const void *)value) == 0)
10630Sstevel@tonic-gate 				ret = 0;
10640Sstevel@tonic-gate 			break;
10650Sstevel@tonic-gate 		case R_SPARC_JMP_SLOT:
10660Sstevel@tonic-gate 			pltndx = ((ulong_t)rel -
10674679Srie 			    (uintptr_t)JMPREL(lmp)) / relsiz;
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 			if (FLAGS(lmp) & FLG_RT_FIXED)
10700Sstevel@tonic-gate 				vaddr = 0;
10710Sstevel@tonic-gate 			else
10720Sstevel@tonic-gate 				vaddr = ADDR(lmp);
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 			if (((LIST(lmp)->lm_tflags | FLAGS1(lmp)) &
10750Sstevel@tonic-gate 			    (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) &&
10760Sstevel@tonic-gate 			    AUDINFO(lmp)->ai_dynplts) {
10770Sstevel@tonic-gate 				int	fail = 0;
10780Sstevel@tonic-gate 				ulong_t	symndx = (((uintptr_t)symdef -
10794679Srie 				    (uintptr_t)SYMTAB(_lmp)) / SYMENT(_lmp));
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 				(void) elf_plt_trace_write((caddr_t)vaddr,
10820Sstevel@tonic-gate 				    (Rela *)rel, lmp, _lmp, symdef, symndx,
10830Sstevel@tonic-gate 				    pltndx, (caddr_t)value, sb_flags, &fail);
10840Sstevel@tonic-gate 				if (fail)
10850Sstevel@tonic-gate 					ret = 0;
10860Sstevel@tonic-gate 			} else {
10870Sstevel@tonic-gate 				/*
10880Sstevel@tonic-gate 				 * Write standard PLT entry to jump directly
10890Sstevel@tonic-gate 				 * to newly bound function.
10900Sstevel@tonic-gate 				 */
10911618Srie 				DBG_CALL(Dbg_reloc_apply_val(LIST(lmp),
10921618Srie 				    ELF_DBG_RTLD, (Xword)roffset,
10930Sstevel@tonic-gate 				    (Xword)value));
10940Sstevel@tonic-gate 				pbtype = elf_plt_write((uintptr_t)vaddr,
10950Sstevel@tonic-gate 				    (uintptr_t)vaddr, (void *)rel, value,
10960Sstevel@tonic-gate 				    pltndx);
10970Sstevel@tonic-gate 			}
10980Sstevel@tonic-gate 			break;
10990Sstevel@tonic-gate 		default:
11000Sstevel@tonic-gate 			value += reladd;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 			/*
11030Sstevel@tonic-gate 			 * Write the relocation out.  If this relocation is a
11040Sstevel@tonic-gate 			 * common basic write, skip the doreloc() engine.
11050Sstevel@tonic-gate 			 */
11060Sstevel@tonic-gate 			if ((rtype == R_SPARC_GLOB_DAT) ||
11070Sstevel@tonic-gate 			    (rtype == R_SPARC_32)) {
11080Sstevel@tonic-gate 				if (roffset & 0x3) {
1109*4734Sab196087 					Conv_inv_buf_t inv_buf;
1110*4734Sab196087 
11111618Srie 					eprintf(LIST(lmp), ERR_FATAL,
11120Sstevel@tonic-gate 					    MSG_INTL(MSG_REL_NONALIGN),
1113*4734Sab196087 					    conv_reloc_SPARC_type(rtype,
1114*4734Sab196087 					    0, &inv_buf),
11150Sstevel@tonic-gate 					    NAME(lmp), demangle(name),
11160Sstevel@tonic-gate 					    EC_OFF(roffset));
11170Sstevel@tonic-gate 					ret = 0;
11180Sstevel@tonic-gate 				} else
11190Sstevel@tonic-gate 					*(uint_t *)roffset += value;
11200Sstevel@tonic-gate 			} else {
11210Sstevel@tonic-gate 				if (do_reloc(rtype, (uchar_t *)roffset,
11221618Srie 				    (Xword *)&value, name,
11231618Srie 				    NAME(lmp), LIST(lmp)) == 0)
11240Sstevel@tonic-gate 					ret = 0;
11250Sstevel@tonic-gate 			}
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 			/*
11280Sstevel@tonic-gate 			 * The value now contains the 'bit-shifted' value that
11290Sstevel@tonic-gate 			 * was or'ed into memory (this was set by do_reloc()).
11300Sstevel@tonic-gate 			 */
11311618Srie 			DBG_CALL(Dbg_reloc_apply_val(LIST(lmp), ELF_DBG_RTLD,
11321618Srie 			    (Xword)roffset, (Xword)value));
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 			/*
11350Sstevel@tonic-gate 			 * If this relocation is against a text segment, make
11360Sstevel@tonic-gate 			 * sure that the instruction cache is flushed.
11370Sstevel@tonic-gate 			 */
11380Sstevel@tonic-gate 			if (textrel)
11390Sstevel@tonic-gate 				iflush_range((caddr_t)roffset, 0x4);
11400Sstevel@tonic-gate 		}
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 		if ((ret == 0) &&
11430Sstevel@tonic-gate 		    ((LIST(lmp)->lm_flags & LML_FLG_TRC_WARN) == 0))
11440Sstevel@tonic-gate 			break;
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 		if (binfo) {
11471618Srie 			DBG_CALL(Dbg_bind_global(lmp, (Addr)roffset,
11481618Srie 			    (Off)(roffset - basebgn), pltndx, pbtype,
11491618Srie 			    _lmp, (Addr)value, symdef->st_value, name, binfo));
11500Sstevel@tonic-gate 		}
11510Sstevel@tonic-gate 	}
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	return (relocate_finish(lmp, bound, textrel, ret));
11540Sstevel@tonic-gate }
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate /*
11570Sstevel@tonic-gate  * Provide a machine specific interface to the conversion routine.  By calling
11580Sstevel@tonic-gate  * the machine specific version, rather than the generic version, we insure that
11590Sstevel@tonic-gate  * the data tables/strings for all known machine versions aren't dragged into
11600Sstevel@tonic-gate  * ld.so.1.
11610Sstevel@tonic-gate  */
11620Sstevel@tonic-gate const char *
11631618Srie _conv_reloc_type(uint_t rel)
11640Sstevel@tonic-gate {
1165*4734Sab196087 	static Conv_inv_buf_t	inv_buf;
1166*4734Sab196087 
1167*4734Sab196087 	return (conv_reloc_SPARC_type(rel, 0, &inv_buf));
11680Sstevel@tonic-gate }
1169