xref: /illumos-gate/usr/src/cmd/sgs/rtld/i386/i386_elf.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215aefb655Srie 
227c478bd9Sstevel@tonic-gate /*
23cce0e03bSab196087  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
26*7257d1b4Sraf 
27*7257d1b4Sraf /*
28*7257d1b4Sraf  *	Copyright (c) 1988 AT&T
29*7257d1b4Sraf  *	  All Rights Reserved
30*7257d1b4Sraf  */
31*7257d1b4Sraf 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * x86 machine dependent and ELF file class dependent functions.
367c478bd9Sstevel@tonic-gate  * Contains routines for performing function binding and symbol relocations.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include	<stdio.h>
407c478bd9Sstevel@tonic-gate #include	<sys/elf.h>
417c478bd9Sstevel@tonic-gate #include	<sys/elf_386.h>
427c478bd9Sstevel@tonic-gate #include	<sys/mman.h>
437c478bd9Sstevel@tonic-gate #include	<dlfcn.h>
447c478bd9Sstevel@tonic-gate #include	<synch.h>
457c478bd9Sstevel@tonic-gate #include	<string.h>
465aefb655Srie #include	<debug.h>
475aefb655Srie #include	<reloc.h>
485aefb655Srie #include	<conv.h>
497c478bd9Sstevel@tonic-gate #include	"_rtld.h"
507c478bd9Sstevel@tonic-gate #include	"_audit.h"
517c478bd9Sstevel@tonic-gate #include	"_elf.h"
527c478bd9Sstevel@tonic-gate #include	"msg.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate extern void	elf_rtbndr(Rt_map *, ulong_t, caddr_t);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate int
587c478bd9Sstevel@tonic-gate elf_mach_flags_check(Rej_desc *rej, Ehdr *ehdr)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	/*
617c478bd9Sstevel@tonic-gate 	 * Check machine type and flags.
627c478bd9Sstevel@tonic-gate 	 */
637c478bd9Sstevel@tonic-gate 	if (ehdr->e_flags != 0) {
647c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_BADFLAG;
657c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_flags;
667c478bd9Sstevel@tonic-gate 		return (0);
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 	return (1);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate void
727c478bd9Sstevel@tonic-gate ldso_plt_init(Rt_map * lmp)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	/*
757c478bd9Sstevel@tonic-gate 	 * There is no need to analyze ld.so because we don't map in any of
767c478bd9Sstevel@tonic-gate 	 * its dependencies.  However we may map these dependencies in later
777c478bd9Sstevel@tonic-gate 	 * (as if ld.so had dlopened them), so initialize the plt and the
787c478bd9Sstevel@tonic-gate 	 * permission information.
797c478bd9Sstevel@tonic-gate 	 */
807c478bd9Sstevel@tonic-gate 	if (PLTGOT(lmp))
817c478bd9Sstevel@tonic-gate 		elf_plt_init((PLTGOT(lmp)), (caddr_t)lmp);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static const uchar_t dyn_plt_template[] = {
857c478bd9Sstevel@tonic-gate /* 0x00 */  0x55,				/* pushl %ebp */
867c478bd9Sstevel@tonic-gate /* 0x01 */  0x8b, 0xec,				/* movl %esp, %ebp */
877c478bd9Sstevel@tonic-gate /* 0x03 */  0x68, 0x00, 0x00, 0x00, 0x00,	/* pushl trace_fields */
887c478bd9Sstevel@tonic-gate /* 0x08 */  0xe9, 0xfc, 0xff, 0xff, 0xff, 0xff	/* jmp  elf_plt_trace */
897c478bd9Sstevel@tonic-gate };
907c478bd9Sstevel@tonic-gate int	dyn_plt_ent_size = sizeof (dyn_plt_template);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * the dynamic plt entry is:
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  *	pushl	%ebp
967c478bd9Sstevel@tonic-gate  *	movl	%esp, %ebp
977c478bd9Sstevel@tonic-gate  *	pushl	tfp
987c478bd9Sstevel@tonic-gate  *	jmp	elf_plt_trace
997c478bd9Sstevel@tonic-gate  * dyn_data:
1007c478bd9Sstevel@tonic-gate  *	.align  4
1017c478bd9Sstevel@tonic-gate  *	uintptr_t	reflmp
1027c478bd9Sstevel@tonic-gate  *	uintptr_t	deflmp
1037c478bd9Sstevel@tonic-gate  *	uint_t		symndx
1047c478bd9Sstevel@tonic-gate  *	uint_t		sb_flags
1057c478bd9Sstevel@tonic-gate  *	Sym		symdef
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate static caddr_t
1087c478bd9Sstevel@tonic-gate elf_plt_trace_write(uint_t roffset, Rt_map *rlmp, Rt_map *dlmp, Sym *sym,
1097c478bd9Sstevel@tonic-gate     uint_t symndx, uint_t pltndx, caddr_t to, uint_t sb_flags, int *fail)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	extern int	elf_plt_trace();
1127c478bd9Sstevel@tonic-gate 	ulong_t		got_entry;
1137c478bd9Sstevel@tonic-gate 	uchar_t		*dyn_plt;
1147c478bd9Sstevel@tonic-gate 	uintptr_t	*dyndata;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/*
1177c478bd9Sstevel@tonic-gate 	 * We only need to add the glue code if there is an auditing
1187c478bd9Sstevel@tonic-gate 	 * library that is interested in this binding.
1197c478bd9Sstevel@tonic-gate 	 */
1207c478bd9Sstevel@tonic-gate 	dyn_plt = (uchar_t *)((uintptr_t)AUDINFO(rlmp)->ai_dynplts +
1217c478bd9Sstevel@tonic-gate 	    (pltndx * dyn_plt_ent_size));
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	/*
1247c478bd9Sstevel@tonic-gate 	 * Have we initialized this dynamic plt entry yet?  If we haven't do it
1257c478bd9Sstevel@tonic-gate 	 * now.  Otherwise this function has been called before, but from a
1267c478bd9Sstevel@tonic-gate 	 * different plt (ie. from another shared object).  In that case
1277c478bd9Sstevel@tonic-gate 	 * we just set the plt to point to the new dyn_plt.
1287c478bd9Sstevel@tonic-gate 	 */
1297c478bd9Sstevel@tonic-gate 	if (*dyn_plt == 0) {
1307c478bd9Sstevel@tonic-gate 		Sym	*symp;
1317c478bd9Sstevel@tonic-gate 		Word	symvalue;
1325aefb655Srie 		Lm_list	*lml = LIST(rlmp);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 		(void) memcpy((void *)dyn_plt, dyn_plt_template,
1357c478bd9Sstevel@tonic-gate 		    sizeof (dyn_plt_template));
1367c478bd9Sstevel@tonic-gate 		dyndata = (uintptr_t *)((uintptr_t)dyn_plt +
1377c478bd9Sstevel@tonic-gate 		    ROUND(sizeof (dyn_plt_template), M_WORD_ALIGN));
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		/*
1407c478bd9Sstevel@tonic-gate 		 * relocate:
1417c478bd9Sstevel@tonic-gate 		 *	pushl	dyn_data
1427c478bd9Sstevel@tonic-gate 		 */
1437c478bd9Sstevel@tonic-gate 		symvalue = (Word)dyndata;
144f3324781Sab196087 		if (do_reloc_rtld(R_386_32, &dyn_plt[4], &symvalue,
1457c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_SYM_LADYNDATA),
1465aefb655Srie 		    MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) {
1477c478bd9Sstevel@tonic-gate 			*fail = 1;
1487c478bd9Sstevel@tonic-gate 			return (0);
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 		/*
1527c478bd9Sstevel@tonic-gate 		 * jmps are relative, so I need to figure out the relative
1537c478bd9Sstevel@tonic-gate 		 * address to elf_plt_trace.
1547c478bd9Sstevel@tonic-gate 		 *
1557c478bd9Sstevel@tonic-gate 		 * relocating:
1567c478bd9Sstevel@tonic-gate 		 *	jmp	elf_plt_trace
1577c478bd9Sstevel@tonic-gate 		 */
1587c478bd9Sstevel@tonic-gate 		symvalue = (ulong_t)(elf_plt_trace) - (ulong_t)(dyn_plt + 9);
159f3324781Sab196087 		if (do_reloc_rtld(R_386_PC32, &dyn_plt[9], &symvalue,
1607c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_SYM_ELFPLTTRACE),
1615aefb655Srie 		    MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) {
1627c478bd9Sstevel@tonic-gate 			*fail = 1;
1637c478bd9Sstevel@tonic-gate 			return (0);
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		*dyndata++ = (uintptr_t)rlmp;
1677c478bd9Sstevel@tonic-gate 		*dyndata++ = (uintptr_t)dlmp;
1687c478bd9Sstevel@tonic-gate 		*dyndata++ = (uint_t)symndx;
1697c478bd9Sstevel@tonic-gate 		*dyndata++ = (uint_t)sb_flags;
1707c478bd9Sstevel@tonic-gate 		symp = (Sym *)dyndata;
1717c478bd9Sstevel@tonic-gate 		*symp = *sym;
1727c478bd9Sstevel@tonic-gate 		symp->st_name += (Word)STRTAB(dlmp);
1737c478bd9Sstevel@tonic-gate 		symp->st_value = (Addr)to;
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	got_entry = (ulong_t)roffset;
1777c478bd9Sstevel@tonic-gate 	*(ulong_t *)got_entry = (ulong_t)dyn_plt;
1787c478bd9Sstevel@tonic-gate 	return ((caddr_t)dyn_plt);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * Function binding routine - invoked on the first call to a function through
1847c478bd9Sstevel@tonic-gate  * the procedure linkage table;
1857c478bd9Sstevel@tonic-gate  * passes first through an assembly language interface.
1867c478bd9Sstevel@tonic-gate  *
1877c478bd9Sstevel@tonic-gate  * Takes the offset into the relocation table of the associated
1887c478bd9Sstevel@tonic-gate  * relocation entry and the address of the link map (rt_private_map struct)
1897c478bd9Sstevel@tonic-gate  * for the entry.
1907c478bd9Sstevel@tonic-gate  *
1917c478bd9Sstevel@tonic-gate  * Returns the address of the function referenced after re-writing the PLT
1927c478bd9Sstevel@tonic-gate  * entry to invoke the function directly.
1937c478bd9Sstevel@tonic-gate  *
1947c478bd9Sstevel@tonic-gate  * On error, causes process to terminate with a signal.
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate ulong_t
1977c478bd9Sstevel@tonic-gate elf_bndr(Rt_map *lmp, ulong_t reloff, caddr_t from)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	Rt_map		*nlmp, *llmp;
2007c478bd9Sstevel@tonic-gate 	ulong_t		addr, symval, rsymndx;
2017c478bd9Sstevel@tonic-gate 	char		*name;
2027c478bd9Sstevel@tonic-gate 	Rel		*rptr;
20360758829Srie 	Sym		*rsym, *nsym;
2045aefb655Srie 	uint_t		binfo, sb_flags = 0, dbg_class;
2057c478bd9Sstevel@tonic-gate 	Slookup		sl;
2065aefb655Srie 	int		entry, lmflags;
2075aefb655Srie 	Lm_list		*lml;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * For compatibility with libthread (TI_VERSION 1) we track the entry
2117c478bd9Sstevel@tonic-gate 	 * value.  A zero value indicates we have recursed into ld.so.1 to
2127c478bd9Sstevel@tonic-gate 	 * further process a locking request.  Under this recursion we disable
2137c478bd9Sstevel@tonic-gate 	 * tsort and cleanup activities.
2147c478bd9Sstevel@tonic-gate 	 */
2158cd45542Sraf 	entry = enter(0);
2167c478bd9Sstevel@tonic-gate 
2175aefb655Srie 	lml = LIST(lmp);
2185aefb655Srie 	if ((lmflags = lml->lm_flags) & LML_FLG_RTLDLM) {
2195aefb655Srie 		dbg_class = dbg_desc->d_class;
2205aefb655Srie 		dbg_desc->d_class = 0;
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/*
2247c478bd9Sstevel@tonic-gate 	 * Perform some basic sanity checks.  If we didn't get a load map or
2257c478bd9Sstevel@tonic-gate 	 * the relocation offset is invalid then its possible someone has walked
2267c478bd9Sstevel@tonic-gate 	 * over the .got entries or jumped to plt0 out of the blue.
2277c478bd9Sstevel@tonic-gate 	 */
2287c478bd9Sstevel@tonic-gate 	if (!lmp || ((reloff % sizeof (Rel)) != 0)) {
229de777a60Sab196087 		Conv_inv_buf_t inv_buf;
230de777a60Sab196087 
2315aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_PLTREF),
232de777a60Sab196087 		    conv_reloc_386_type(R_386_JMP_SLOT, 0, &inv_buf),
2335aefb655Srie 		    EC_NATPTR(lmp), EC_XWORD(reloff), EC_NATPTR(from));
2345aefb655Srie 		rtldexit(lml, 1);
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/*
2387c478bd9Sstevel@tonic-gate 	 * Use relocation entry to get symbol table entry and symbol name.
2397c478bd9Sstevel@tonic-gate 	 */
2407c478bd9Sstevel@tonic-gate 	addr = (ulong_t)JMPREL(lmp);
2417c478bd9Sstevel@tonic-gate 	rptr = (Rel *)(addr + reloff);
2427c478bd9Sstevel@tonic-gate 	rsymndx = ELF_R_SYM(rptr->r_info);
24360758829Srie 	rsym = (Sym *)((ulong_t)SYMTAB(lmp) + (rsymndx * SYMENT(lmp)));
24460758829Srie 	name = (char *)(STRTAB(lmp) + rsym->st_name);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	/*
2477c478bd9Sstevel@tonic-gate 	 * Determine the last link-map of this list, this'll be the starting
2487c478bd9Sstevel@tonic-gate 	 * point for any tsort() processing.
2497c478bd9Sstevel@tonic-gate 	 */
2505aefb655Srie 	llmp = lml->lm_tail;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
25375e7992aSrie 	 * Find definition for symbol.  Initialize the symbol lookup data
25475e7992aSrie 	 * structure.
2557c478bd9Sstevel@tonic-gate 	 */
25675e7992aSrie 	SLOOKUP_INIT(sl, name, lmp, lml->lm_head, ld_entry_cnt, 0,
25775e7992aSrie 	    rsymndx, rsym, 0, LKUP_DEFT);
2587c478bd9Sstevel@tonic-gate 
2599aa23310Srie 	if ((nsym = lookup_sym(&sl, &nlmp, &binfo, NULL)) == 0) {
2605aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOSYM), NAME(lmp),
2617c478bd9Sstevel@tonic-gate 		    demangle(name));
2625aefb655Srie 		rtldexit(lml, 1);
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	symval = nsym->st_value;
2667c478bd9Sstevel@tonic-gate 	if (!(FLAGS(nlmp) & FLG_RT_FIXED) &&
2677c478bd9Sstevel@tonic-gate 	    (nsym->st_shndx != SHN_ABS))
2687c478bd9Sstevel@tonic-gate 		symval += ADDR(nlmp);
2697c478bd9Sstevel@tonic-gate 	if ((lmp != nlmp) && ((FLAGS1(nlmp) & FL1_RT_NOINIFIN) == 0)) {
2707c478bd9Sstevel@tonic-gate 		/*
2717c478bd9Sstevel@tonic-gate 		 * Record that this new link map is now bound to the caller.
2727c478bd9Sstevel@tonic-gate 		 */
2737c478bd9Sstevel@tonic-gate 		if (bind_one(lmp, nlmp, BND_REFER) == 0)
2745aefb655Srie 			rtldexit(lml, 1);
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 
2775aefb655Srie 	if ((lml->lm_tflags | FLAGS1(lmp)) & LML_TFLG_AUD_SYMBIND) {
2787c478bd9Sstevel@tonic-gate 		uint_t	symndx = (((uintptr_t)nsym -
2797c478bd9Sstevel@tonic-gate 		    (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp));
2807c478bd9Sstevel@tonic-gate 		symval = audit_symbind(lmp, nlmp, nsym, symndx, symval,
2817c478bd9Sstevel@tonic-gate 		    &sb_flags);
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if (!(rtld_flags & RT_FL_NOBIND)) {
2857c478bd9Sstevel@tonic-gate 		addr = rptr->r_offset;
2867c478bd9Sstevel@tonic-gate 		if (!(FLAGS(lmp) & FLG_RT_FIXED))
2877c478bd9Sstevel@tonic-gate 			addr += ADDR(lmp);
2885aefb655Srie 		if (((lml->lm_tflags | FLAGS1(lmp)) &
2897c478bd9Sstevel@tonic-gate 		    (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) &&
2907c478bd9Sstevel@tonic-gate 		    AUDINFO(lmp)->ai_dynplts) {
2917c478bd9Sstevel@tonic-gate 			int	fail = 0;
2927c478bd9Sstevel@tonic-gate 			uint_t	pltndx = reloff / sizeof (Rel);
2937c478bd9Sstevel@tonic-gate 			uint_t	symndx = (((uintptr_t)nsym -
2947247f888Srie 			    (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp));
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 			symval = (ulong_t)elf_plt_trace_write(addr, lmp, nlmp,
2977c478bd9Sstevel@tonic-gate 			    nsym, symndx, pltndx, (caddr_t)symval, sb_flags,
2987c478bd9Sstevel@tonic-gate 			    &fail);
2997c478bd9Sstevel@tonic-gate 			if (fail)
3005aefb655Srie 				rtldexit(lml, 1);
3017c478bd9Sstevel@tonic-gate 		} else {
3027c478bd9Sstevel@tonic-gate 			/*
3037c478bd9Sstevel@tonic-gate 			 * Write standard PLT entry to jump directly
3047c478bd9Sstevel@tonic-gate 			 * to newly bound function.
3057c478bd9Sstevel@tonic-gate 			 */
3067c478bd9Sstevel@tonic-gate 			*(ulong_t *)addr = symval;
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	/*
3117c478bd9Sstevel@tonic-gate 	 * Print binding information and rebuild PLT entry.
3127c478bd9Sstevel@tonic-gate 	 */
3135aefb655Srie 	DBG_CALL(Dbg_bind_global(lmp, (Addr)from, (Off)(from - ADDR(lmp)),
3145aefb655Srie 	    (Xword)(reloff / sizeof (Rel)), PLT_T_FULL, nlmp, (Addr)symval,
3155aefb655Srie 	    nsym->st_value, name, binfo));
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/*
3187c478bd9Sstevel@tonic-gate 	 * Complete any processing for newly loaded objects.  Note we don't
3197c478bd9Sstevel@tonic-gate 	 * know exactly where any new objects are loaded (we know the object
3207c478bd9Sstevel@tonic-gate 	 * that supplied the symbol, but others may have been loaded lazily as
3217c478bd9Sstevel@tonic-gate 	 * we searched for the symbol), so sorting starts from the last
3227c478bd9Sstevel@tonic-gate 	 * link-map know on entry to this routine.
3237c478bd9Sstevel@tonic-gate 	 */
3247c478bd9Sstevel@tonic-gate 	if (entry)
3257247f888Srie 		load_completion(llmp);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	/*
3287c478bd9Sstevel@tonic-gate 	 * Some operations like dldump() or dlopen()'ing a relocatable object
3297c478bd9Sstevel@tonic-gate 	 * result in objects being loaded on rtld's link-map, make sure these
3307c478bd9Sstevel@tonic-gate 	 * objects are initialized also.
3317c478bd9Sstevel@tonic-gate 	 */
3327c478bd9Sstevel@tonic-gate 	if ((LIST(nlmp)->lm_flags & LML_FLG_RTLDLM) && LIST(nlmp)->lm_init)
3337247f888Srie 		load_completion(nlmp);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	/*
3367c478bd9Sstevel@tonic-gate 	 * If the object we've bound to is in the process of being initialized
3377c478bd9Sstevel@tonic-gate 	 * by another thread, determine whether we should block.
3387c478bd9Sstevel@tonic-gate 	 */
3397c478bd9Sstevel@tonic-gate 	is_dep_ready(nlmp, lmp, DBG_WAIT_SYMBOL);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/*
3427c478bd9Sstevel@tonic-gate 	 * Make sure the object to which we've bound has had it's .init fired.
3437c478bd9Sstevel@tonic-gate 	 * Cleanup before return to user code.
3447c478bd9Sstevel@tonic-gate 	 */
3457c478bd9Sstevel@tonic-gate 	if (entry) {
3467c478bd9Sstevel@tonic-gate 		is_dep_init(nlmp, lmp);
3478cd45542Sraf 		leave(lml, 0);
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if (lmflags & LML_FLG_RTLDLM)
3515aefb655Srie 		dbg_desc->d_class = dbg_class;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	return (symval);
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate  * When the relocation loop realizes that it's dealing with relative
3597c478bd9Sstevel@tonic-gate  * relocations in a shared object, it breaks into this tighter loop
3607c478bd9Sstevel@tonic-gate  * as an optimization.
3617c478bd9Sstevel@tonic-gate  */
3627c478bd9Sstevel@tonic-gate ulong_t
3637c478bd9Sstevel@tonic-gate elf_reloc_relative(ulong_t relbgn, ulong_t relend, ulong_t relsiz,
3647c478bd9Sstevel@tonic-gate     ulong_t basebgn, ulong_t etext, ulong_t emap)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	ulong_t roffset = ((Rel *)relbgn)->r_offset;
3677c478bd9Sstevel@tonic-gate 	char rtype;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	do {
3707c478bd9Sstevel@tonic-gate 		roffset += basebgn;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		/*
3737c478bd9Sstevel@tonic-gate 		 * If this relocation is against an address not mapped in,
3747c478bd9Sstevel@tonic-gate 		 * then break out of the relative relocation loop, falling
3757c478bd9Sstevel@tonic-gate 		 * back on the main relocation loop.
3767c478bd9Sstevel@tonic-gate 		 */
3777c478bd9Sstevel@tonic-gate 		if (roffset < etext || roffset > emap)
3787c478bd9Sstevel@tonic-gate 			break;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 		/*
3817c478bd9Sstevel@tonic-gate 		 * Perform the actual relocation.
3827c478bd9Sstevel@tonic-gate 		 */
3837c478bd9Sstevel@tonic-gate 		*((ulong_t *)roffset) += basebgn;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 		relbgn += relsiz;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 		if (relbgn >= relend)
3887c478bd9Sstevel@tonic-gate 			break;
3897c478bd9Sstevel@tonic-gate 
390ba2be530Sab196087 		rtype = ELF_R_TYPE(((Rel *)relbgn)->r_info, M_MACH);
3917c478bd9Sstevel@tonic-gate 		roffset = ((Rel *)relbgn)->r_offset;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	} while (rtype == R_386_RELATIVE);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	return (relbgn);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate /*
3997c478bd9Sstevel@tonic-gate  * This is the tightest loop for RELATIVE relocations for those
4007c478bd9Sstevel@tonic-gate  * objects built with the DT_RELACOUNT .dynamic entry.
4017c478bd9Sstevel@tonic-gate  */
4027c478bd9Sstevel@tonic-gate ulong_t
4037c478bd9Sstevel@tonic-gate elf_reloc_relacount(ulong_t relbgn, ulong_t relacount, ulong_t relsiz,
4047c478bd9Sstevel@tonic-gate     ulong_t basebgn)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	ulong_t roffset = ((Rel *) relbgn)->r_offset;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	for (; relacount; relacount--) {
4097c478bd9Sstevel@tonic-gate 		roffset += basebgn;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 		/*
4127c478bd9Sstevel@tonic-gate 		 * Perform the actual relocation.
4137c478bd9Sstevel@tonic-gate 		 */
4147c478bd9Sstevel@tonic-gate 		*((ulong_t *)roffset) += basebgn;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 		relbgn += relsiz;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		roffset = ((Rel *)relbgn)->r_offset;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	return (relbgn);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate /*
4267c478bd9Sstevel@tonic-gate  * Read and process the relocations for one link object, we assume all
4277c478bd9Sstevel@tonic-gate  * relocation sections for loadable segments are stored contiguously in
4287c478bd9Sstevel@tonic-gate  * the file.
4297c478bd9Sstevel@tonic-gate  */
4307c478bd9Sstevel@tonic-gate int
4319aa23310Srie elf_reloc(Rt_map *lmp, uint_t plt, int *in_nfavl)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	ulong_t		relbgn, relend, relsiz, basebgn;
4347c478bd9Sstevel@tonic-gate 	ulong_t		pltbgn, pltend, _pltbgn, _pltend;
4357c478bd9Sstevel@tonic-gate 	ulong_t		roffset, rsymndx, psymndx = 0, etext  = ETEXT(lmp);
4367c478bd9Sstevel@tonic-gate 	ulong_t		emap, dsymndx;
4377c478bd9Sstevel@tonic-gate 	uchar_t		rtype;
4387c478bd9Sstevel@tonic-gate 	long		value, pvalue;
4397c478bd9Sstevel@tonic-gate 	Sym		*symref, *psymref, *symdef, *psymdef;
4407c478bd9Sstevel@tonic-gate 	char		*name, *pname;
4417c478bd9Sstevel@tonic-gate 	Rt_map		*_lmp, *plmp;
4427c478bd9Sstevel@tonic-gate 	int		textrel = 0, ret = 1, noplt = 0;
4437c478bd9Sstevel@tonic-gate 	int		relacount = RELACOUNT(lmp), plthint = 0;
4447c478bd9Sstevel@tonic-gate 	Rel		*rel;
4457c478bd9Sstevel@tonic-gate 	uint_t		binfo, pbinfo;
446cce0e03bSab196087 	APlist		*bound = NULL;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * Although only necessary for lazy binding, initialize the first
4507c478bd9Sstevel@tonic-gate 	 * global offset entry to go to elf_rtbndr().  dbx(1) seems
4517c478bd9Sstevel@tonic-gate 	 * to find this useful.
4527c478bd9Sstevel@tonic-gate 	 */
4537c478bd9Sstevel@tonic-gate 	if ((plt == 0) && PLTGOT(lmp)) {
4547c478bd9Sstevel@tonic-gate 		if ((ulong_t)PLTGOT(lmp) < etext) {
4557c478bd9Sstevel@tonic-gate 			if (elf_set_prot(lmp, PROT_WRITE) == 0)
4567c478bd9Sstevel@tonic-gate 				return (0);
4577c478bd9Sstevel@tonic-gate 			textrel = 1;
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 		elf_plt_init(PLTGOT(lmp), (caddr_t)lmp);
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/*
4637c478bd9Sstevel@tonic-gate 	 * Initialize the plt start and end addresses.
4647c478bd9Sstevel@tonic-gate 	 */
4657c478bd9Sstevel@tonic-gate 	if ((pltbgn = (ulong_t)JMPREL(lmp)) != 0)
4667c478bd9Sstevel@tonic-gate 		pltend = pltbgn + (ulong_t)(PLTRELSZ(lmp));
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	relsiz = (ulong_t)(RELENT(lmp));
4707c478bd9Sstevel@tonic-gate 	basebgn = ADDR(lmp);
4717c478bd9Sstevel@tonic-gate 	emap = ADDR(lmp) + MSIZE(lmp);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	if (PLTRELSZ(lmp))
4747c478bd9Sstevel@tonic-gate 		plthint = PLTRELSZ(lmp) / relsiz;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	/*
4777c478bd9Sstevel@tonic-gate 	 * If we've been called upon to promote an RTLD_LAZY object to an
4787c478bd9Sstevel@tonic-gate 	 * RTLD_NOW then we're only interested in scaning the .plt table.
4797c478bd9Sstevel@tonic-gate 	 * An uninitialized .plt is the case where the associated got entry
4807c478bd9Sstevel@tonic-gate 	 * points back to the plt itself.  Determine the range of the real .plt
4817c478bd9Sstevel@tonic-gate 	 * entries using the _PROCEDURE_LINKAGE_TABLE_ symbol.
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	if (plt) {
4847c478bd9Sstevel@tonic-gate 		Slookup	sl;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 		relbgn = pltbgn;
4877c478bd9Sstevel@tonic-gate 		relend = pltend;
4887c478bd9Sstevel@tonic-gate 		if (!relbgn || (relbgn == relend))
4897c478bd9Sstevel@tonic-gate 			return (1);
4907c478bd9Sstevel@tonic-gate 
49175e7992aSrie 		/*
49275e7992aSrie 		 * Initialize the symbol lookup data structure.
49375e7992aSrie 		 */
49475e7992aSrie 		SLOOKUP_INIT(sl, MSG_ORIG(MSG_SYM_PLT), lmp, lmp, ld_entry_cnt,
49575e7992aSrie 		    elf_hash(MSG_ORIG(MSG_SYM_PLT)), 0, 0, 0, LKUP_DEFT);
4967c478bd9Sstevel@tonic-gate 
4979aa23310Srie 		if ((symdef = elf_find_sym(&sl, &_lmp, &binfo, NULL)) == 0)
4987c478bd9Sstevel@tonic-gate 			return (1);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 		_pltbgn = symdef->st_value;
5017c478bd9Sstevel@tonic-gate 		if (!(FLAGS(lmp) & FLG_RT_FIXED) &&
5027c478bd9Sstevel@tonic-gate 		    (symdef->st_shndx != SHN_ABS))
5037c478bd9Sstevel@tonic-gate 			_pltbgn += basebgn;
5047c478bd9Sstevel@tonic-gate 		_pltend = _pltbgn + (((PLTRELSZ(lmp) / relsiz)) *
5057c478bd9Sstevel@tonic-gate 		    M_PLT_ENTSIZE) + M_PLT_RESERVSZ;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	} else {
5087c478bd9Sstevel@tonic-gate 		/*
5097c478bd9Sstevel@tonic-gate 		 * The relocation sections appear to the run-time linker as a
5107c478bd9Sstevel@tonic-gate 		 * single table.  Determine the address of the beginning and end
5117c478bd9Sstevel@tonic-gate 		 * of this table.  There are two different interpretations of
5127c478bd9Sstevel@tonic-gate 		 * the ABI at this point:
5137c478bd9Sstevel@tonic-gate 		 *
5147c478bd9Sstevel@tonic-gate 		 *   o	The REL table and its associated RELSZ indicate the
5157c478bd9Sstevel@tonic-gate 		 *	concatenation of *all* relocation sections (this is the
5167c478bd9Sstevel@tonic-gate 		 *	model our link-editor constructs).
5177c478bd9Sstevel@tonic-gate 		 *
5187c478bd9Sstevel@tonic-gate 		 *   o	The REL table and its associated RELSZ indicate the
5197c478bd9Sstevel@tonic-gate 		 *	concatenation of all *but* the .plt relocations.  These
5207c478bd9Sstevel@tonic-gate 		 *	relocations are specified individually by the JMPREL and
5217c478bd9Sstevel@tonic-gate 		 *	PLTRELSZ entries.
5227c478bd9Sstevel@tonic-gate 		 *
5237c478bd9Sstevel@tonic-gate 		 * Determine from our knowledege of the relocation range and
5247c478bd9Sstevel@tonic-gate 		 * .plt range, the range of the total relocation table.  Note
5257c478bd9Sstevel@tonic-gate 		 * that one other ABI assumption seems to be that the .plt
5267c478bd9Sstevel@tonic-gate 		 * relocations always follow any other relocations, the
5277c478bd9Sstevel@tonic-gate 		 * following range checking drops that assumption.
5287c478bd9Sstevel@tonic-gate 		 */
5297c478bd9Sstevel@tonic-gate 		relbgn = (ulong_t)(REL(lmp));
5307c478bd9Sstevel@tonic-gate 		relend = relbgn + (ulong_t)(RELSZ(lmp));
5317c478bd9Sstevel@tonic-gate 		if (pltbgn) {
5327c478bd9Sstevel@tonic-gate 			if (!relbgn || (relbgn > pltbgn))
5337c478bd9Sstevel@tonic-gate 				relbgn = pltbgn;
5347c478bd9Sstevel@tonic-gate 			if (!relbgn || (relend < pltend))
5357c478bd9Sstevel@tonic-gate 				relend = pltend;
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 	if (!relbgn || (relbgn == relend)) {
5395aefb655Srie 		DBG_CALL(Dbg_reloc_run(lmp, 0, plt, DBG_REL_NONE));
5407c478bd9Sstevel@tonic-gate 		return (1);
5417c478bd9Sstevel@tonic-gate 	}
5425aefb655Srie 	DBG_CALL(Dbg_reloc_run(lmp, M_REL_SHT_TYPE, plt, DBG_REL_START));
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	/*
5457c478bd9Sstevel@tonic-gate 	 * If we're processing a dynamic executable in lazy mode there is no
5467c478bd9Sstevel@tonic-gate 	 * need to scan the .rel.plt table, however if we're processing a shared
5477c478bd9Sstevel@tonic-gate 	 * object in lazy mode the .got addresses associated to each .plt must
5487c478bd9Sstevel@tonic-gate 	 * be relocated to reflect the location of the shared object.
5497c478bd9Sstevel@tonic-gate 	 */
5507c478bd9Sstevel@tonic-gate 	if (pltbgn && ((MODE(lmp) & RTLD_NOW) == 0) &&
5517c478bd9Sstevel@tonic-gate 	    (FLAGS(lmp) & FLG_RT_FIXED))
5527c478bd9Sstevel@tonic-gate 		noplt = 1;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/*
5557c478bd9Sstevel@tonic-gate 	 * Loop through relocations.
5567c478bd9Sstevel@tonic-gate 	 */
5577c478bd9Sstevel@tonic-gate 	while (relbgn < relend) {
5587c478bd9Sstevel@tonic-gate 		uint_t	sb_flags = 0;
5597c478bd9Sstevel@tonic-gate 
560ba2be530Sab196087 		rtype = ELF_R_TYPE(((Rel *)relbgn)->r_info, M_MACH);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 		/*
5637c478bd9Sstevel@tonic-gate 		 * If this is a RELATIVE relocation in a shared object (the
5647c478bd9Sstevel@tonic-gate 		 * common case), and if we are not debugging, then jump into a
5657c478bd9Sstevel@tonic-gate 		 * tighter relocation loop (elf_reloc_relative).  Only make the
5667c478bd9Sstevel@tonic-gate 		 * jump if we've been given a hint on the number of relocations.
5677c478bd9Sstevel@tonic-gate 		 */
5687c478bd9Sstevel@tonic-gate 		if ((rtype == R_386_RELATIVE) &&
5695aefb655Srie 		    ((FLAGS(lmp) & FLG_RT_FIXED) == 0) && (DBG_ENABLED == 0)) {
5707c478bd9Sstevel@tonic-gate 			/*
5717c478bd9Sstevel@tonic-gate 			 * It's possible that the relative relocation block
5727c478bd9Sstevel@tonic-gate 			 * has relocations against the text segment as well
5737c478bd9Sstevel@tonic-gate 			 * as the data segment.  Since our optimized relocation
5747c478bd9Sstevel@tonic-gate 			 * engine does not check which segment the relocation
5757c478bd9Sstevel@tonic-gate 			 * is against - just mprotect it now if it's been
5767c478bd9Sstevel@tonic-gate 			 * marked as containing TEXTREL's.
5777c478bd9Sstevel@tonic-gate 			 */
5787c478bd9Sstevel@tonic-gate 			if ((textrel == 0) && (FLAGS1(lmp) & FL1_RT_TEXTREL)) {
5797c478bd9Sstevel@tonic-gate 				if (elf_set_prot(lmp, PROT_WRITE) == 0) {
5807c478bd9Sstevel@tonic-gate 					ret = 0;
5817c478bd9Sstevel@tonic-gate 					break;
5827c478bd9Sstevel@tonic-gate 				}
5837c478bd9Sstevel@tonic-gate 				textrel = 1;
5847c478bd9Sstevel@tonic-gate 			}
585d326b23bSrie 
5867c478bd9Sstevel@tonic-gate 			if (relacount) {
5877c478bd9Sstevel@tonic-gate 				relbgn = elf_reloc_relacount(relbgn, relacount,
5887c478bd9Sstevel@tonic-gate 				    relsiz, basebgn);
5897c478bd9Sstevel@tonic-gate 				relacount = 0;
5907c478bd9Sstevel@tonic-gate 			} else {
5917c478bd9Sstevel@tonic-gate 				relbgn = elf_reloc_relative(relbgn, relend,
5927c478bd9Sstevel@tonic-gate 				    relsiz, basebgn, etext, emap);
5937c478bd9Sstevel@tonic-gate 			}
5947c478bd9Sstevel@tonic-gate 			if (relbgn >= relend)
5957c478bd9Sstevel@tonic-gate 				break;
596ba2be530Sab196087 			rtype = ELF_R_TYPE(((Rel *)relbgn)->r_info, M_MACH);
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		roffset = ((Rel *)relbgn)->r_offset;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		/*
6027c478bd9Sstevel@tonic-gate 		 * If this is a shared object, add the base address to offset.
6037c478bd9Sstevel@tonic-gate 		 */
6047c478bd9Sstevel@tonic-gate 		if (!(FLAGS(lmp) & FLG_RT_FIXED)) {
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 			/*
6077c478bd9Sstevel@tonic-gate 			 * If we're processing lazy bindings, we have to step
6087c478bd9Sstevel@tonic-gate 			 * through the plt entries and add the base address
6097c478bd9Sstevel@tonic-gate 			 * to the corresponding got entry.
6107c478bd9Sstevel@tonic-gate 			 */
6117c478bd9Sstevel@tonic-gate 			if (plthint && (plt == 0) &&
6127c478bd9Sstevel@tonic-gate 			    (rtype == R_386_JMP_SLOT) &&
6137c478bd9Sstevel@tonic-gate 			    ((MODE(lmp) & RTLD_NOW) == 0)) {
6147c478bd9Sstevel@tonic-gate 				relbgn = elf_reloc_relacount(relbgn,
6157c478bd9Sstevel@tonic-gate 				    plthint, relsiz, basebgn);
6167c478bd9Sstevel@tonic-gate 				plthint = 0;
6177c478bd9Sstevel@tonic-gate 				continue;
6187c478bd9Sstevel@tonic-gate 			}
6197c478bd9Sstevel@tonic-gate 			roffset += basebgn;
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 		rsymndx = ELF_R_SYM(((Rel *)relbgn)->r_info);
6237c478bd9Sstevel@tonic-gate 		rel = (Rel *)relbgn;
6247c478bd9Sstevel@tonic-gate 		relbgn += relsiz;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 		/*
6277c478bd9Sstevel@tonic-gate 		 * Optimizations.
6287c478bd9Sstevel@tonic-gate 		 */
6297c478bd9Sstevel@tonic-gate 		if (rtype == R_386_NONE)
6307c478bd9Sstevel@tonic-gate 			continue;
6317c478bd9Sstevel@tonic-gate 		if (noplt && ((ulong_t)rel >= pltbgn) &&
6327c478bd9Sstevel@tonic-gate 		    ((ulong_t)rel < pltend)) {
6337c478bd9Sstevel@tonic-gate 			relbgn = pltend;
6347c478bd9Sstevel@tonic-gate 			continue;
6357c478bd9Sstevel@tonic-gate 		}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		/*
6387c478bd9Sstevel@tonic-gate 		 * If we're promoting plts determine if this one has already
6397c478bd9Sstevel@tonic-gate 		 * been written.
6407c478bd9Sstevel@tonic-gate 		 */
6417c478bd9Sstevel@tonic-gate 		if (plt) {
6427c478bd9Sstevel@tonic-gate 			if ((*(ulong_t *)roffset < _pltbgn) ||
6437c478bd9Sstevel@tonic-gate 			    (*(ulong_t *)roffset > _pltend))
6447c478bd9Sstevel@tonic-gate 				continue;
6457c478bd9Sstevel@tonic-gate 		}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		/*
6487c478bd9Sstevel@tonic-gate 		 * If this relocation is not against part of the image
6497c478bd9Sstevel@tonic-gate 		 * mapped into memory we skip it.
6507c478bd9Sstevel@tonic-gate 		 */
6517c478bd9Sstevel@tonic-gate 		if ((roffset < ADDR(lmp)) || (roffset > (ADDR(lmp) +
6527c478bd9Sstevel@tonic-gate 		    MSIZE(lmp)))) {
6537247f888Srie 			elf_reloc_bad(lmp, (void *)rel, rtype, roffset,
6547247f888Srie 			    rsymndx);
6557c478bd9Sstevel@tonic-gate 			continue;
6567c478bd9Sstevel@tonic-gate 		}
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 		binfo = 0;
6597c478bd9Sstevel@tonic-gate 		/*
6607c478bd9Sstevel@tonic-gate 		 * If a symbol index is specified then get the symbol table
6617c478bd9Sstevel@tonic-gate 		 * entry, locate the symbol definition, and determine its
6627c478bd9Sstevel@tonic-gate 		 * address.
6637c478bd9Sstevel@tonic-gate 		 */
6647c478bd9Sstevel@tonic-gate 		if (rsymndx) {
6657c478bd9Sstevel@tonic-gate 			/*
6667c478bd9Sstevel@tonic-gate 			 * Get the local symbol table entry.
6677c478bd9Sstevel@tonic-gate 			 */
6687c478bd9Sstevel@tonic-gate 			symref = (Sym *)((ulong_t)SYMTAB(lmp) +
6697c478bd9Sstevel@tonic-gate 			    (rsymndx * SYMENT(lmp)));
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 			/*
6727c478bd9Sstevel@tonic-gate 			 * If this is a local symbol, just use the base address.
6737c478bd9Sstevel@tonic-gate 			 * (we should have no local relocations in the
6747c478bd9Sstevel@tonic-gate 			 * executable).
6757c478bd9Sstevel@tonic-gate 			 */
6767c478bd9Sstevel@tonic-gate 			if (ELF_ST_BIND(symref->st_info) == STB_LOCAL) {
6777c478bd9Sstevel@tonic-gate 				value = basebgn;
6787c478bd9Sstevel@tonic-gate 				name = (char *)0;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 				/*
681d326b23bSrie 				 * Special case TLS relocations.
6827c478bd9Sstevel@tonic-gate 				 */
683d326b23bSrie 				if (rtype == R_386_TLS_DTPMOD32) {
684d326b23bSrie 					/*
685d326b23bSrie 					 * Use the TLS modid.
686d326b23bSrie 					 */
6877c478bd9Sstevel@tonic-gate 					value = TLSMODID(lmp);
688d326b23bSrie 
689d326b23bSrie 				} else if (rtype == R_386_TLS_TPOFF) {
690d326b23bSrie 					if ((value = elf_static_tls(lmp, symref,
691d326b23bSrie 					    rel, rtype, 0, roffset, 0)) == 0) {
692d326b23bSrie 						ret = 0;
693d326b23bSrie 						break;
694d326b23bSrie 					}
695d326b23bSrie 				}
6967c478bd9Sstevel@tonic-gate 			} else {
6977c478bd9Sstevel@tonic-gate 				/*
6987c478bd9Sstevel@tonic-gate 				 * If the symbol index is equal to the previous
6997c478bd9Sstevel@tonic-gate 				 * symbol index relocation we processed then
7007c478bd9Sstevel@tonic-gate 				 * reuse the previous values. (Note that there
7017c478bd9Sstevel@tonic-gate 				 * have been cases where a relocation exists
7027c478bd9Sstevel@tonic-gate 				 * against a copy relocation symbol, our ld(1)
7037c478bd9Sstevel@tonic-gate 				 * should optimize this away, but make sure we
7047c478bd9Sstevel@tonic-gate 				 * don't use the same symbol information should
7057c478bd9Sstevel@tonic-gate 				 * this case exist).
7067c478bd9Sstevel@tonic-gate 				 */
7077c478bd9Sstevel@tonic-gate 				if ((rsymndx == psymndx) &&
7087c478bd9Sstevel@tonic-gate 				    (rtype != R_386_COPY)) {
7097c478bd9Sstevel@tonic-gate 					/* LINTED */
7107c478bd9Sstevel@tonic-gate 					if (psymdef == 0) {
7115aefb655Srie 						DBG_CALL(Dbg_bind_weak(lmp,
7125aefb655Srie 						    (Addr)roffset, (Addr)
7137c478bd9Sstevel@tonic-gate 						    (roffset - basebgn), name));
7147c478bd9Sstevel@tonic-gate 						continue;
7157c478bd9Sstevel@tonic-gate 					}
7167c478bd9Sstevel@tonic-gate 					/* LINTED */
7177c478bd9Sstevel@tonic-gate 					value = pvalue;
7187c478bd9Sstevel@tonic-gate 					/* LINTED */
7197c478bd9Sstevel@tonic-gate 					name = pname;
7207c478bd9Sstevel@tonic-gate 					/* LINTED */
7217c478bd9Sstevel@tonic-gate 					symdef = psymdef;
7227c478bd9Sstevel@tonic-gate 					/* LINTED */
7237c478bd9Sstevel@tonic-gate 					symref = psymref;
7247c478bd9Sstevel@tonic-gate 					/* LINTED */
7257c478bd9Sstevel@tonic-gate 					_lmp = plmp;
7267c478bd9Sstevel@tonic-gate 					/* LINTED */
7277c478bd9Sstevel@tonic-gate 					binfo = pbinfo;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 					if ((LIST(_lmp)->lm_tflags |
7307c478bd9Sstevel@tonic-gate 					    FLAGS1(_lmp)) &
7317c478bd9Sstevel@tonic-gate 					    LML_TFLG_AUD_SYMBIND) {
7327c478bd9Sstevel@tonic-gate 						value = audit_symbind(lmp, _lmp,
7337c478bd9Sstevel@tonic-gate 						    /* LINTED */
7347c478bd9Sstevel@tonic-gate 						    symdef, dsymndx, value,
7357c478bd9Sstevel@tonic-gate 						    &sb_flags);
7367c478bd9Sstevel@tonic-gate 					}
7377c478bd9Sstevel@tonic-gate 				} else {
7387c478bd9Sstevel@tonic-gate 					Slookup		sl;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 					/*
7417c478bd9Sstevel@tonic-gate 					 * Lookup the symbol definition.
74275e7992aSrie 					 * Initialize the symbol lookup data
74375e7992aSrie 					 * structure.
7447c478bd9Sstevel@tonic-gate 					 */
7457c478bd9Sstevel@tonic-gate 					name = (char *)(STRTAB(lmp) +
7467c478bd9Sstevel@tonic-gate 					    symref->st_name);
7477c478bd9Sstevel@tonic-gate 
74875e7992aSrie 					SLOOKUP_INIT(sl, name, lmp, 0,
74975e7992aSrie 					    ld_entry_cnt, 0, rsymndx, symref,
75075e7992aSrie 					    rtype, LKUP_STDRELOC);
7517c478bd9Sstevel@tonic-gate 
7529aa23310Srie 					symdef = lookup_sym(&sl, &_lmp,
7539aa23310Srie 					    &binfo, in_nfavl);
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 					/*
7567c478bd9Sstevel@tonic-gate 					 * If the symbol is not found and the
7577c478bd9Sstevel@tonic-gate 					 * reference was not to a weak symbol,
7587c478bd9Sstevel@tonic-gate 					 * report an error.  Weak references
7597c478bd9Sstevel@tonic-gate 					 * may be unresolved.
7607c478bd9Sstevel@tonic-gate 					 */
7617247f888Srie 					/* BEGIN CSTYLED */
7627c478bd9Sstevel@tonic-gate 					if (symdef == 0) {
76360758829Srie 					    if (sl.sl_bind != STB_WEAK) {
764dae2dfb7Srie 						if (elf_reloc_error(lmp, name,
765dae2dfb7Srie 						    rel, binfo))
7667c478bd9Sstevel@tonic-gate 							continue;
767dae2dfb7Srie 
7687c478bd9Sstevel@tonic-gate 						ret = 0;
7697c478bd9Sstevel@tonic-gate 						break;
770dae2dfb7Srie 
7717c478bd9Sstevel@tonic-gate 					    } else {
7727c478bd9Sstevel@tonic-gate 						psymndx = rsymndx;
7737c478bd9Sstevel@tonic-gate 						psymdef = 0;
7747c478bd9Sstevel@tonic-gate 
7755aefb655Srie 						DBG_CALL(Dbg_bind_weak(lmp,
7765aefb655Srie 						    (Addr)roffset, (Addr)
7777c478bd9Sstevel@tonic-gate 						    (roffset - basebgn), name));
7787c478bd9Sstevel@tonic-gate 						continue;
7797c478bd9Sstevel@tonic-gate 					    }
7807c478bd9Sstevel@tonic-gate 					}
7817247f888Srie 					/* END CSTYLED */
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 					/*
7847c478bd9Sstevel@tonic-gate 					 * If symbol was found in an object
7857c478bd9Sstevel@tonic-gate 					 * other than the referencing object
7867c478bd9Sstevel@tonic-gate 					 * then record the binding.
7877c478bd9Sstevel@tonic-gate 					 */
7887c478bd9Sstevel@tonic-gate 					if ((lmp != _lmp) && ((FLAGS1(_lmp) &
7897c478bd9Sstevel@tonic-gate 					    FL1_RT_NOINIFIN) == 0)) {
790cce0e03bSab196087 						if (aplist_test(&bound, _lmp,
7917c478bd9Sstevel@tonic-gate 						    AL_CNT_RELBIND) == 0) {
7927c478bd9Sstevel@tonic-gate 							ret = 0;
7937c478bd9Sstevel@tonic-gate 							break;
7947c478bd9Sstevel@tonic-gate 						}
7957c478bd9Sstevel@tonic-gate 					}
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 					/*
7987c478bd9Sstevel@tonic-gate 					 * Calculate the location of definition;
7997c478bd9Sstevel@tonic-gate 					 * symbol value plus base address of
8007c478bd9Sstevel@tonic-gate 					 * containing shared object.
8017c478bd9Sstevel@tonic-gate 					 */
8022926dd2eSrie 					if (IS_SIZE(rtype))
8032926dd2eSrie 						value = symdef->st_size;
8042926dd2eSrie 					else
8057c478bd9Sstevel@tonic-gate 						value = symdef->st_value;
8062926dd2eSrie 
8077c478bd9Sstevel@tonic-gate 					if (!(FLAGS(_lmp) & FLG_RT_FIXED) &&
8082926dd2eSrie 					    !(IS_SIZE(rtype)) &&
8097c478bd9Sstevel@tonic-gate 					    (symdef->st_shndx != SHN_ABS) &&
8107c478bd9Sstevel@tonic-gate 					    (ELF_ST_TYPE(symdef->st_info) !=
8117c478bd9Sstevel@tonic-gate 					    STT_TLS))
8127c478bd9Sstevel@tonic-gate 						value += ADDR(_lmp);
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 					/*
8157c478bd9Sstevel@tonic-gate 					 * Retain this symbol index and the
8167c478bd9Sstevel@tonic-gate 					 * value in case it can be used for the
8177c478bd9Sstevel@tonic-gate 					 * subsequent relocations.
8187c478bd9Sstevel@tonic-gate 					 */
8197c478bd9Sstevel@tonic-gate 					if (rtype != R_386_COPY) {
8207c478bd9Sstevel@tonic-gate 						psymndx = rsymndx;
8217c478bd9Sstevel@tonic-gate 						pvalue = value;
8227c478bd9Sstevel@tonic-gate 						pname = name;
8237c478bd9Sstevel@tonic-gate 						psymdef = symdef;
8247c478bd9Sstevel@tonic-gate 						psymref = symref;
8257c478bd9Sstevel@tonic-gate 						plmp = _lmp;
8267c478bd9Sstevel@tonic-gate 						pbinfo = binfo;
8277c478bd9Sstevel@tonic-gate 					}
8287c478bd9Sstevel@tonic-gate 					if ((LIST(_lmp)->lm_tflags |
8297c478bd9Sstevel@tonic-gate 					    FLAGS1(_lmp)) &
8307c478bd9Sstevel@tonic-gate 					    LML_TFLG_AUD_SYMBIND) {
8317c478bd9Sstevel@tonic-gate 						dsymndx = (((uintptr_t)symdef -
8327c478bd9Sstevel@tonic-gate 						    (uintptr_t)SYMTAB(_lmp)) /
8337c478bd9Sstevel@tonic-gate 						    SYMENT(_lmp));
8347c478bd9Sstevel@tonic-gate 						value = audit_symbind(lmp, _lmp,
8357c478bd9Sstevel@tonic-gate 						    symdef, dsymndx, value,
8367c478bd9Sstevel@tonic-gate 						    &sb_flags);
8377c478bd9Sstevel@tonic-gate 					}
8387c478bd9Sstevel@tonic-gate 				}
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 				/*
8417c478bd9Sstevel@tonic-gate 				 * If relocation is PC-relative, subtract
8427c478bd9Sstevel@tonic-gate 				 * offset address.
8437c478bd9Sstevel@tonic-gate 				 */
8447c478bd9Sstevel@tonic-gate 				if (IS_PC_RELATIVE(rtype))
8457c478bd9Sstevel@tonic-gate 					value -= roffset;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 				/*
848d326b23bSrie 				 * Special case TLS relocations.
8497c478bd9Sstevel@tonic-gate 				 */
850d326b23bSrie 				if (rtype == R_386_TLS_DTPMOD32) {
851d326b23bSrie 					/*
852d326b23bSrie 					 * Relocation value is the TLS modid.
853d326b23bSrie 					 */
8547c478bd9Sstevel@tonic-gate 					value = TLSMODID(_lmp);
855d326b23bSrie 
856d326b23bSrie 				} else if (rtype == R_386_TLS_TPOFF) {
857d326b23bSrie 					if ((value = elf_static_tls(_lmp,
858d326b23bSrie 					    symdef, rel, rtype, name, roffset,
859d326b23bSrie 					    value)) == 0) {
860d326b23bSrie 						ret = 0;
861d326b23bSrie 						break;
862d326b23bSrie 					}
863d326b23bSrie 				}
8647c478bd9Sstevel@tonic-gate 			}
8657c478bd9Sstevel@tonic-gate 		} else {
8667c478bd9Sstevel@tonic-gate 			/*
867d326b23bSrie 			 * Special cases.
8687c478bd9Sstevel@tonic-gate 			 */
869d326b23bSrie 			if (rtype == R_386_TLS_DTPMOD32) {
870d326b23bSrie 				/*
871d326b23bSrie 				 * TLS relocation value is the TLS modid.
872d326b23bSrie 				 */
8737c478bd9Sstevel@tonic-gate 				value = TLSMODID(lmp);
874d326b23bSrie 			} else
8757c478bd9Sstevel@tonic-gate 				value = basebgn;
8767c478bd9Sstevel@tonic-gate 			name = (char *)0;
8777c478bd9Sstevel@tonic-gate 		}
8787c478bd9Sstevel@tonic-gate 
879d326b23bSrie 		DBG_CALL(Dbg_reloc_in(LIST(lmp), ELF_DBG_RTLD, M_MACH,
880d326b23bSrie 		    M_REL_SHT_TYPE, rel, NULL, name));
881d326b23bSrie 
8827c478bd9Sstevel@tonic-gate 		/*
8837c478bd9Sstevel@tonic-gate 		 * If this object has relocations in the text segment, turn
8847c478bd9Sstevel@tonic-gate 		 * off the write protect.
8857c478bd9Sstevel@tonic-gate 		 */
8867c478bd9Sstevel@tonic-gate 		if ((roffset < etext) && (textrel == 0)) {
8877c478bd9Sstevel@tonic-gate 			if (elf_set_prot(lmp, PROT_WRITE) == 0) {
8887c478bd9Sstevel@tonic-gate 				ret = 0;
8897c478bd9Sstevel@tonic-gate 				break;
8907c478bd9Sstevel@tonic-gate 			}
8917c478bd9Sstevel@tonic-gate 			textrel = 1;
8927c478bd9Sstevel@tonic-gate 		}
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 		/*
8957c478bd9Sstevel@tonic-gate 		 * Call relocation routine to perform required relocation.
8967c478bd9Sstevel@tonic-gate 		 */
8977c478bd9Sstevel@tonic-gate 		switch (rtype) {
8987c478bd9Sstevel@tonic-gate 		case R_386_COPY:
8997c478bd9Sstevel@tonic-gate 			if (elf_copy_reloc(name, symref, lmp, (void *)roffset,
9007c478bd9Sstevel@tonic-gate 			    symdef, _lmp, (const void *)value) == 0)
9017c478bd9Sstevel@tonic-gate 				ret = 0;
9027c478bd9Sstevel@tonic-gate 			break;
9037c478bd9Sstevel@tonic-gate 		case R_386_JMP_SLOT:
9047c478bd9Sstevel@tonic-gate 			if (((LIST(lmp)->lm_tflags | FLAGS1(lmp)) &
9057c478bd9Sstevel@tonic-gate 			    (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) &&
9067c478bd9Sstevel@tonic-gate 			    AUDINFO(lmp)->ai_dynplts) {
9077c478bd9Sstevel@tonic-gate 				int	fail = 0;
9087c478bd9Sstevel@tonic-gate 				int	pltndx = (((ulong_t)rel -
9097c478bd9Sstevel@tonic-gate 				    (uintptr_t)JMPREL(lmp)) / relsiz);
9107c478bd9Sstevel@tonic-gate 				int	symndx = (((uintptr_t)symdef -
9117247f888Srie 				    (uintptr_t)SYMTAB(_lmp)) / SYMENT(_lmp));
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 				(void) elf_plt_trace_write(roffset, lmp, _lmp,
9147c478bd9Sstevel@tonic-gate 				    symdef, symndx, pltndx, (caddr_t)value,
9157c478bd9Sstevel@tonic-gate 				    sb_flags, &fail);
9167c478bd9Sstevel@tonic-gate 				if (fail)
9177c478bd9Sstevel@tonic-gate 					ret = 0;
9187c478bd9Sstevel@tonic-gate 			} else {
9197c478bd9Sstevel@tonic-gate 				/*
9207c478bd9Sstevel@tonic-gate 				 * Write standard PLT entry to jump directly
9217c478bd9Sstevel@tonic-gate 				 * to newly bound function.
9227c478bd9Sstevel@tonic-gate 				 */
9235aefb655Srie 				DBG_CALL(Dbg_reloc_apply_val(LIST(lmp),
9245aefb655Srie 				    ELF_DBG_RTLD, (Xword)roffset,
9257c478bd9Sstevel@tonic-gate 				    (Xword)value));
9267c478bd9Sstevel@tonic-gate 				*(ulong_t *)roffset = value;
9277c478bd9Sstevel@tonic-gate 			}
9287c478bd9Sstevel@tonic-gate 			break;
9297c478bd9Sstevel@tonic-gate 		default:
9307c478bd9Sstevel@tonic-gate 			/*
9317c478bd9Sstevel@tonic-gate 			 * Write the relocation out.
9327c478bd9Sstevel@tonic-gate 			 */
933f3324781Sab196087 			if (do_reloc_rtld(rtype, (uchar_t *)roffset,
934f3324781Sab196087 			    (Word *)&value, name, NAME(lmp), LIST(lmp)) == 0)
9357c478bd9Sstevel@tonic-gate 				ret = 0;
9367c478bd9Sstevel@tonic-gate 
9375aefb655Srie 			DBG_CALL(Dbg_reloc_apply_val(LIST(lmp), ELF_DBG_RTLD,
9385aefb655Srie 			    (Xword)roffset, (Xword)value));
9397c478bd9Sstevel@tonic-gate 		}
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		if ((ret == 0) &&
9427c478bd9Sstevel@tonic-gate 		    ((LIST(lmp)->lm_flags & LML_FLG_TRC_WARN) == 0))
9437c478bd9Sstevel@tonic-gate 			break;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 		if (binfo) {
9465aefb655Srie 			DBG_CALL(Dbg_bind_global(lmp, (Addr)roffset,
9475aefb655Srie 			    (Off)(roffset - basebgn), (Xword)(-1), PLT_T_FULL,
9485aefb655Srie 			    _lmp, (Addr)value, symdef->st_value, name, binfo));
9497c478bd9Sstevel@tonic-gate 		}
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	return (relocate_finish(lmp, bound, textrel, ret));
9537c478bd9Sstevel@tonic-gate }
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate /*
9567c478bd9Sstevel@tonic-gate  * Initialize the first few got entries so that function calls go to
9577c478bd9Sstevel@tonic-gate  * elf_rtbndr:
9587c478bd9Sstevel@tonic-gate  *
9597c478bd9Sstevel@tonic-gate  *	GOT[GOT_XLINKMAP] =	the address of the link map
9607c478bd9Sstevel@tonic-gate  *	GOT[GOT_XRTLD] =	the address of rtbinder
9617c478bd9Sstevel@tonic-gate  */
9627c478bd9Sstevel@tonic-gate void
9637c478bd9Sstevel@tonic-gate elf_plt_init(void *got, caddr_t l)
9647c478bd9Sstevel@tonic-gate {
9657c478bd9Sstevel@tonic-gate 	uint_t		*_got;
9667c478bd9Sstevel@tonic-gate 	/* LINTED */
9677c478bd9Sstevel@tonic-gate 	Rt_map		*lmp = (Rt_map *)l;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	_got = (uint_t *)got + M_GOT_XLINKMAP;
9707c478bd9Sstevel@tonic-gate 	*_got = (uint_t)lmp;
9717c478bd9Sstevel@tonic-gate 	_got = (uint_t *)got + M_GOT_XRTLD;
9727c478bd9Sstevel@tonic-gate 	*_got = (uint_t)elf_rtbndr;
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate /*
9767c478bd9Sstevel@tonic-gate  * For SVR4 Intel compatability.  USL uses /usr/lib/libc.so.1 as the run-time
9777c478bd9Sstevel@tonic-gate  * linker, so the interpreter's address will differ from /usr/lib/ld.so.1.
9787c478bd9Sstevel@tonic-gate  * Further, USL has special _iob[] and _ctype[] processing that makes up for the
9797c478bd9Sstevel@tonic-gate  * fact that these arrays do not have associated copy relocations.  So we try
9807c478bd9Sstevel@tonic-gate  * and make up for that here.  Any relocations found will be added to the global
9817c478bd9Sstevel@tonic-gate  * copy relocation list and will be processed in setup().
9827c478bd9Sstevel@tonic-gate  */
9837c478bd9Sstevel@tonic-gate static int
9847c478bd9Sstevel@tonic-gate _elf_copy_reloc(const char *name, Rt_map *rlmp, Rt_map *dlmp)
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate 	Sym		*symref, *symdef;
9877c478bd9Sstevel@tonic-gate 	caddr_t 	ref, def;
9887c478bd9Sstevel@tonic-gate 	Rt_map		*_lmp;
9897c478bd9Sstevel@tonic-gate 	Rel		rel;
9907c478bd9Sstevel@tonic-gate 	Slookup		sl;
9917c478bd9Sstevel@tonic-gate 	uint_t		binfo;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	/*
9947c478bd9Sstevel@tonic-gate 	 * Determine if the special symbol exists as a reference in the dynamic
9957c478bd9Sstevel@tonic-gate 	 * executable, and that an associated definition exists in libc.so.1.
99675e7992aSrie 	 *
99775e7992aSrie 	 * Initialize the symbol lookup data structure.
9987c478bd9Sstevel@tonic-gate 	 */
99975e7992aSrie 	SLOOKUP_INIT(sl, name, rlmp, rlmp, ld_entry_cnt, 0, 0, 0, 0,
100075e7992aSrie 	    LKUP_FIRST);
10017c478bd9Sstevel@tonic-gate 
10029aa23310Srie 	if ((symref = lookup_sym(&sl, &_lmp, &binfo, NULL)) == 0)
10037c478bd9Sstevel@tonic-gate 		return (1);
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	sl.sl_imap = dlmp;
10067c478bd9Sstevel@tonic-gate 	sl.sl_flags = LKUP_DEFT;
10077c478bd9Sstevel@tonic-gate 
10089aa23310Srie 	if ((symdef = lookup_sym(&sl, &_lmp, &binfo, NULL)) == 0)
10097c478bd9Sstevel@tonic-gate 		return (1);
10107c478bd9Sstevel@tonic-gate 	if (strcmp(NAME(_lmp), MSG_ORIG(MSG_PTH_LIBC)))
10117c478bd9Sstevel@tonic-gate 		return (1);
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	/*
10147c478bd9Sstevel@tonic-gate 	 * Determine the reference and definition addresses.
10157c478bd9Sstevel@tonic-gate 	 */
10167c478bd9Sstevel@tonic-gate 	ref = (void *)(symref->st_value);
10177c478bd9Sstevel@tonic-gate 	if (!(FLAGS(rlmp) & FLG_RT_FIXED))
10187c478bd9Sstevel@tonic-gate 		ref += ADDR(rlmp);
10197c478bd9Sstevel@tonic-gate 	def = (void *)(symdef->st_value);
10207c478bd9Sstevel@tonic-gate 	if (!(FLAGS(_lmp) & FLG_RT_FIXED))
10217c478bd9Sstevel@tonic-gate 		def += ADDR(_lmp);
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	/*
10247c478bd9Sstevel@tonic-gate 	 * Set up a relocation entry for debugging and call the generic copy
10257c478bd9Sstevel@tonic-gate 	 * relocation function to provide symbol size error checking and to
10267c478bd9Sstevel@tonic-gate 	 * record the copy relocation that must be performed.
10277c478bd9Sstevel@tonic-gate 	 */
10287c478bd9Sstevel@tonic-gate 	rel.r_offset = (Addr)ref;
10297c478bd9Sstevel@tonic-gate 	rel.r_info = (Word)R_386_COPY;
10305aefb655Srie 	DBG_CALL(Dbg_reloc_in(LIST(rlmp), ELF_DBG_RTLD, M_MACH, M_REL_SHT_TYPE,
10315aefb655Srie 	    &rel, NULL, name));
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	return (elf_copy_reloc((char *)name, symref, rlmp, (void *)ref, symdef,
10347c478bd9Sstevel@tonic-gate 	    _lmp, (void *)def));
10357c478bd9Sstevel@tonic-gate }
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate int
10387c478bd9Sstevel@tonic-gate elf_copy_gen(Rt_map *lmp)
10397c478bd9Sstevel@tonic-gate {
10407c478bd9Sstevel@tonic-gate 	if (interp && ((ulong_t)interp->i_faddr !=
10417c478bd9Sstevel@tonic-gate 	    r_debug.rtd_rdebug.r_ldbase) &&
10427c478bd9Sstevel@tonic-gate 	    !(strcmp(interp->i_name, MSG_ORIG(MSG_PTH_LIBC)))) {
10437c478bd9Sstevel@tonic-gate 
10445aefb655Srie 		DBG_CALL(Dbg_reloc_run(lmp, M_REL_SHT_TYPE, 0,
10457c478bd9Sstevel@tonic-gate 		    DBG_REL_START));
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 		if (_elf_copy_reloc(MSG_ORIG(MSG_SYM_CTYPE), lmp,
10487c478bd9Sstevel@tonic-gate 		    (Rt_map *)NEXT(lmp)) == 0)
10497c478bd9Sstevel@tonic-gate 			return (0);
10507c478bd9Sstevel@tonic-gate 		if (_elf_copy_reloc(MSG_ORIG(MSG_SYM_IOB), lmp,
10517c478bd9Sstevel@tonic-gate 		    (Rt_map *)NEXT(lmp)) == 0)
10527c478bd9Sstevel@tonic-gate 			return (0);
10537c478bd9Sstevel@tonic-gate 	}
10547c478bd9Sstevel@tonic-gate 	return (1);
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate /*
10587c478bd9Sstevel@tonic-gate  * Plt writing interface to allow debugging initialization to be generic.
10597c478bd9Sstevel@tonic-gate  */
10607c478bd9Sstevel@tonic-gate Pltbindtype
10617c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
10627c478bd9Sstevel@tonic-gate elf_plt_write(uintptr_t addr, uintptr_t vaddr, void *rptr, uintptr_t symval,
10637c478bd9Sstevel@tonic-gate 	Xword pltndx)
10647c478bd9Sstevel@tonic-gate {
10657c478bd9Sstevel@tonic-gate 	Rel		*rel = (Rel*)rptr;
10667c478bd9Sstevel@tonic-gate 	uintptr_t	pltaddr;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	pltaddr = addr + rel->r_offset;
10697c478bd9Sstevel@tonic-gate 	*(ulong_t *)pltaddr = (ulong_t)symval;
10707c478bd9Sstevel@tonic-gate 	DBG_CALL(pltcntfull++);
10717c478bd9Sstevel@tonic-gate 	return (PLT_T_FULL);
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate /*
10757c478bd9Sstevel@tonic-gate  * Provide a machine specific interface to the conversion routine.  By calling
10767c478bd9Sstevel@tonic-gate  * the machine specific version, rather than the generic version, we insure that
10777c478bd9Sstevel@tonic-gate  * the data tables/strings for all known machine versions aren't dragged into
10787c478bd9Sstevel@tonic-gate  * ld.so.1.
10797c478bd9Sstevel@tonic-gate  */
10807c478bd9Sstevel@tonic-gate const char *
10815aefb655Srie _conv_reloc_type(uint_t rel)
10827c478bd9Sstevel@tonic-gate {
1083de777a60Sab196087 	static Conv_inv_buf_t inv_buf;
1084de777a60Sab196087 
1085de777a60Sab196087 	return (conv_reloc_386_type(rel, 0, &inv_buf));
10867c478bd9Sstevel@tonic-gate }
1087