xref: /onnv-gate/usr/src/cmd/sgs/rtld/i386/i386_elf.c (revision 12449:a87750d92895)
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 /*
236812Sraf  *	Copyright (c) 1988 AT&T
246812Sraf  *	  All Rights Reserved
25*12449SRod.Evans@Sun.COM  *
26*12449SRod.Evans@Sun.COM  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
276812Sraf  */
286812Sraf 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * x86 machine dependent and ELF file class dependent functions.
310Sstevel@tonic-gate  * Contains routines for performing function binding and symbol relocations.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include	<stdio.h>
350Sstevel@tonic-gate #include	<sys/elf.h>
360Sstevel@tonic-gate #include	<sys/elf_386.h>
370Sstevel@tonic-gate #include	<sys/mman.h>
380Sstevel@tonic-gate #include	<dlfcn.h>
390Sstevel@tonic-gate #include	<synch.h>
400Sstevel@tonic-gate #include	<string.h>
411618Srie #include	<debug.h>
421618Srie #include	<reloc.h>
431618Srie #include	<conv.h>
440Sstevel@tonic-gate #include	"_rtld.h"
450Sstevel@tonic-gate #include	"_audit.h"
460Sstevel@tonic-gate #include	"_elf.h"
47*12449SRod.Evans@Sun.COM #include	"_inline_gen.h"
48*12449SRod.Evans@Sun.COM #include	"_inline_reloc.h"
490Sstevel@tonic-gate #include	"msg.h"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate extern void	elf_rtbndr(Rt_map *, ulong_t, caddr_t);
520Sstevel@tonic-gate 
530Sstevel@tonic-gate int
elf_mach_flags_check(Rej_desc * rej,Ehdr * ehdr)540Sstevel@tonic-gate elf_mach_flags_check(Rej_desc *rej, Ehdr *ehdr)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	/*
570Sstevel@tonic-gate 	 * Check machine type and flags.
580Sstevel@tonic-gate 	 */
590Sstevel@tonic-gate 	if (ehdr->e_flags != 0) {
600Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_BADFLAG;
610Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_flags;
620Sstevel@tonic-gate 		return (0);
630Sstevel@tonic-gate 	}
640Sstevel@tonic-gate 	return (1);
650Sstevel@tonic-gate }
660Sstevel@tonic-gate 
670Sstevel@tonic-gate void
ldso_plt_init(Rt_map * lmp)688598SRod.Evans@Sun.COM ldso_plt_init(Rt_map *lmp)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	/*
710Sstevel@tonic-gate 	 * There is no need to analyze ld.so because we don't map in any of
720Sstevel@tonic-gate 	 * its dependencies.  However we may map these dependencies in later
730Sstevel@tonic-gate 	 * (as if ld.so had dlopened them), so initialize the plt and the
740Sstevel@tonic-gate 	 * permission information.
750Sstevel@tonic-gate 	 */
760Sstevel@tonic-gate 	if (PLTGOT(lmp))
770Sstevel@tonic-gate 		elf_plt_init((PLTGOT(lmp)), (caddr_t)lmp);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static const uchar_t dyn_plt_template[] = {
810Sstevel@tonic-gate /* 0x00 */  0x55,				/* pushl %ebp */
820Sstevel@tonic-gate /* 0x01 */  0x8b, 0xec,				/* movl %esp, %ebp */
830Sstevel@tonic-gate /* 0x03 */  0x68, 0x00, 0x00, 0x00, 0x00,	/* pushl trace_fields */
840Sstevel@tonic-gate /* 0x08 */  0xe9, 0xfc, 0xff, 0xff, 0xff, 0xff	/* jmp  elf_plt_trace */
850Sstevel@tonic-gate };
860Sstevel@tonic-gate int	dyn_plt_ent_size = sizeof (dyn_plt_template);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * the dynamic plt entry is:
900Sstevel@tonic-gate  *
910Sstevel@tonic-gate  *	pushl	%ebp
920Sstevel@tonic-gate  *	movl	%esp, %ebp
930Sstevel@tonic-gate  *	pushl	tfp
940Sstevel@tonic-gate  *	jmp	elf_plt_trace
950Sstevel@tonic-gate  * dyn_data:
960Sstevel@tonic-gate  *	.align  4
970Sstevel@tonic-gate  *	uintptr_t	reflmp
980Sstevel@tonic-gate  *	uintptr_t	deflmp
990Sstevel@tonic-gate  *	uint_t		symndx
1000Sstevel@tonic-gate  *	uint_t		sb_flags
1010Sstevel@tonic-gate  *	Sym		symdef
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate static caddr_t
elf_plt_trace_write(uint_t roffset,Rt_map * rlmp,Rt_map * dlmp,Sym * sym,uint_t symndx,uint_t pltndx,caddr_t to,uint_t sb_flags,int * fail)1040Sstevel@tonic-gate elf_plt_trace_write(uint_t roffset, Rt_map *rlmp, Rt_map *dlmp, Sym *sym,
1050Sstevel@tonic-gate     uint_t symndx, uint_t pltndx, caddr_t to, uint_t sb_flags, int *fail)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	extern int	elf_plt_trace();
1080Sstevel@tonic-gate 	ulong_t		got_entry;
1090Sstevel@tonic-gate 	uchar_t		*dyn_plt;
1100Sstevel@tonic-gate 	uintptr_t	*dyndata;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	/*
1130Sstevel@tonic-gate 	 * We only need to add the glue code if there is an auditing
1140Sstevel@tonic-gate 	 * library that is interested in this binding.
1150Sstevel@tonic-gate 	 */
1160Sstevel@tonic-gate 	dyn_plt = (uchar_t *)((uintptr_t)AUDINFO(rlmp)->ai_dynplts +
1171618Srie 	    (pltndx * dyn_plt_ent_size));
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	/*
1200Sstevel@tonic-gate 	 * Have we initialized this dynamic plt entry yet?  If we haven't do it
1210Sstevel@tonic-gate 	 * now.  Otherwise this function has been called before, but from a
1220Sstevel@tonic-gate 	 * different plt (ie. from another shared object).  In that case
1230Sstevel@tonic-gate 	 * we just set the plt to point to the new dyn_plt.
1240Sstevel@tonic-gate 	 */
1250Sstevel@tonic-gate 	if (*dyn_plt == 0) {
1261618Srie 		Sym	*symp;
1270Sstevel@tonic-gate 		Word	symvalue;
1281618Srie 		Lm_list	*lml = LIST(rlmp);
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 		(void) memcpy((void *)dyn_plt, dyn_plt_template,
1310Sstevel@tonic-gate 		    sizeof (dyn_plt_template));
1320Sstevel@tonic-gate 		dyndata = (uintptr_t *)((uintptr_t)dyn_plt +
1330Sstevel@tonic-gate 		    ROUND(sizeof (dyn_plt_template), M_WORD_ALIGN));
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 		/*
1360Sstevel@tonic-gate 		 * relocate:
1370Sstevel@tonic-gate 		 *	pushl	dyn_data
1380Sstevel@tonic-gate 		 */
1390Sstevel@tonic-gate 		symvalue = (Word)dyndata;
1405189Sab196087 		if (do_reloc_rtld(R_386_32, &dyn_plt[4], &symvalue,
1410Sstevel@tonic-gate 		    MSG_ORIG(MSG_SYM_LADYNDATA),
1421618Srie 		    MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) {
1430Sstevel@tonic-gate 			*fail = 1;
1440Sstevel@tonic-gate 			return (0);
1450Sstevel@tonic-gate 		}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 		/*
1480Sstevel@tonic-gate 		 * jmps are relative, so I need to figure out the relative
1490Sstevel@tonic-gate 		 * address to elf_plt_trace.
1500Sstevel@tonic-gate 		 *
1510Sstevel@tonic-gate 		 * relocating:
1520Sstevel@tonic-gate 		 *	jmp	elf_plt_trace
1530Sstevel@tonic-gate 		 */
1540Sstevel@tonic-gate 		symvalue = (ulong_t)(elf_plt_trace) - (ulong_t)(dyn_plt + 9);
1555189Sab196087 		if (do_reloc_rtld(R_386_PC32, &dyn_plt[9], &symvalue,
1560Sstevel@tonic-gate 		    MSG_ORIG(MSG_SYM_ELFPLTTRACE),
1571618Srie 		    MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) {
1580Sstevel@tonic-gate 			*fail = 1;
1590Sstevel@tonic-gate 			return (0);
1600Sstevel@tonic-gate 		}
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 		*dyndata++ = (uintptr_t)rlmp;
1630Sstevel@tonic-gate 		*dyndata++ = (uintptr_t)dlmp;
1640Sstevel@tonic-gate 		*dyndata++ = (uint_t)symndx;
1650Sstevel@tonic-gate 		*dyndata++ = (uint_t)sb_flags;
1660Sstevel@tonic-gate 		symp = (Sym *)dyndata;
1670Sstevel@tonic-gate 		*symp = *sym;
1680Sstevel@tonic-gate 		symp->st_name += (Word)STRTAB(dlmp);
1690Sstevel@tonic-gate 		symp->st_value = (Addr)to;
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	got_entry = (ulong_t)roffset;
1730Sstevel@tonic-gate 	*(ulong_t *)got_entry = (ulong_t)dyn_plt;
1740Sstevel@tonic-gate 	return ((caddr_t)dyn_plt);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate  * Function binding routine - invoked on the first call to a function through
1790Sstevel@tonic-gate  * the procedure linkage table;
1800Sstevel@tonic-gate  * passes first through an assembly language interface.
1810Sstevel@tonic-gate  *
1820Sstevel@tonic-gate  * Takes the offset into the relocation table of the associated
1830Sstevel@tonic-gate  * relocation entry and the address of the link map (rt_private_map struct)
1840Sstevel@tonic-gate  * for the entry.
1850Sstevel@tonic-gate  *
1860Sstevel@tonic-gate  * Returns the address of the function referenced after re-writing the PLT
1870Sstevel@tonic-gate  * entry to invoke the function directly.
1880Sstevel@tonic-gate  *
1890Sstevel@tonic-gate  * On error, causes process to terminate with a signal.
1900Sstevel@tonic-gate  */
1910Sstevel@tonic-gate ulong_t
elf_bndr(Rt_map * lmp,ulong_t reloff,caddr_t from)1920Sstevel@tonic-gate elf_bndr(Rt_map *lmp, ulong_t reloff, caddr_t from)
1930Sstevel@tonic-gate {
1941618Srie 	Rt_map		*nlmp, *llmp;
1950Sstevel@tonic-gate 	ulong_t		addr, symval, rsymndx;
1960Sstevel@tonic-gate 	char		*name;
1970Sstevel@tonic-gate 	Rel		*rptr;
1985220Srie 	Sym		*rsym, *nsym;
1991618Srie 	uint_t		binfo, sb_flags = 0, dbg_class;
2000Sstevel@tonic-gate 	Slookup		sl;
20111827SRod.Evans@Sun.COM 	Sresult		sr;
2021618Srie 	int		entry, lmflags;
2031618Srie 	Lm_list		*lml;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	/*
2060Sstevel@tonic-gate 	 * For compatibility with libthread (TI_VERSION 1) we track the entry
2070Sstevel@tonic-gate 	 * value.  A zero value indicates we have recursed into ld.so.1 to
2080Sstevel@tonic-gate 	 * further process a locking request.  Under this recursion we disable
2090Sstevel@tonic-gate 	 * tsort and cleanup activities.
2100Sstevel@tonic-gate 	 */
2116515Sraf 	entry = enter(0);
2120Sstevel@tonic-gate 
2131618Srie 	lml = LIST(lmp);
2141618Srie 	if ((lmflags = lml->lm_flags) & LML_FLG_RTLDLM) {
2151618Srie 		dbg_class = dbg_desc->d_class;
2161618Srie 		dbg_desc->d_class = 0;
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	/*
2200Sstevel@tonic-gate 	 * Perform some basic sanity checks.  If we didn't get a load map or
2210Sstevel@tonic-gate 	 * the relocation offset is invalid then its possible someone has walked
2220Sstevel@tonic-gate 	 * over the .got entries or jumped to plt0 out of the blue.
2230Sstevel@tonic-gate 	 */
2240Sstevel@tonic-gate 	if (!lmp || ((reloff % sizeof (Rel)) != 0)) {
2254734Sab196087 		Conv_inv_buf_t inv_buf;
2264734Sab196087 
2271618Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_PLTREF),
2284734Sab196087 		    conv_reloc_386_type(R_386_JMP_SLOT, 0, &inv_buf),
2291618Srie 		    EC_NATPTR(lmp), EC_XWORD(reloff), EC_NATPTR(from));
2301618Srie 		rtldexit(lml, 1);
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	/*
2340Sstevel@tonic-gate 	 * Use relocation entry to get symbol table entry and symbol name.
2350Sstevel@tonic-gate 	 */
2360Sstevel@tonic-gate 	addr = (ulong_t)JMPREL(lmp);
2370Sstevel@tonic-gate 	rptr = (Rel *)(addr + reloff);
2380Sstevel@tonic-gate 	rsymndx = ELF_R_SYM(rptr->r_info);
2395220Srie 	rsym = (Sym *)((ulong_t)SYMTAB(lmp) + (rsymndx * SYMENT(lmp)));
2405220Srie 	name = (char *)(STRTAB(lmp) + rsym->st_name);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	/*
2430Sstevel@tonic-gate 	 * Determine the last link-map of this list, this'll be the starting
2440Sstevel@tonic-gate 	 * point for any tsort() processing.
2450Sstevel@tonic-gate 	 */
2461618Srie 	llmp = lml->lm_tail;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/*
24911827SRod.Evans@Sun.COM 	 * Find definition for symbol.  Initialize the symbol lookup, and
25011827SRod.Evans@Sun.COM 	 * symbol result, data structures.
2510Sstevel@tonic-gate 	 */
2525950Srie 	SLOOKUP_INIT(sl, name, lmp, lml->lm_head, ld_entry_cnt, 0,
2535950Srie 	    rsymndx, rsym, 0, LKUP_DEFT);
25411827SRod.Evans@Sun.COM 	SRESULT_INIT(sr, name);
2550Sstevel@tonic-gate 
25611827SRod.Evans@Sun.COM 	if (lookup_sym(&sl, &sr, &binfo, NULL) == 0) {
2571618Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOSYM), NAME(lmp),
2580Sstevel@tonic-gate 		    demangle(name));
2591618Srie 		rtldexit(lml, 1);
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 
26211827SRod.Evans@Sun.COM 	name = (char *)sr.sr_name;
26311827SRod.Evans@Sun.COM 	nlmp = sr.sr_dmap;
26411827SRod.Evans@Sun.COM 	nsym = sr.sr_sym;
26511827SRod.Evans@Sun.COM 
2660Sstevel@tonic-gate 	symval = nsym->st_value;
26711827SRod.Evans@Sun.COM 
2680Sstevel@tonic-gate 	if (!(FLAGS(nlmp) & FLG_RT_FIXED) &&
2690Sstevel@tonic-gate 	    (nsym->st_shndx != SHN_ABS))
2700Sstevel@tonic-gate 		symval += ADDR(nlmp);
2710Sstevel@tonic-gate 	if ((lmp != nlmp) && ((FLAGS1(nlmp) & FL1_RT_NOINIFIN) == 0)) {
2720Sstevel@tonic-gate 		/*
2730Sstevel@tonic-gate 		 * Record that this new link map is now bound to the caller.
2740Sstevel@tonic-gate 		 */
2750Sstevel@tonic-gate 		if (bind_one(lmp, nlmp, BND_REFER) == 0)
2761618Srie 			rtldexit(lml, 1);
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
2798598SRod.Evans@Sun.COM 	if ((lml->lm_tflags | AFLAGS(lmp)) & LML_TFLG_AUD_SYMBIND) {
2800Sstevel@tonic-gate 		uint_t	symndx = (((uintptr_t)nsym -
2814679Srie 		    (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp));
2820Sstevel@tonic-gate 		symval = audit_symbind(lmp, nlmp, nsym, symndx, symval,
2834679Srie 		    &sb_flags);
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if (!(rtld_flags & RT_FL_NOBIND)) {
2870Sstevel@tonic-gate 		addr = rptr->r_offset;
2880Sstevel@tonic-gate 		if (!(FLAGS(lmp) & FLG_RT_FIXED))
2890Sstevel@tonic-gate 			addr += ADDR(lmp);
2908598SRod.Evans@Sun.COM 		if (((lml->lm_tflags | AFLAGS(lmp)) &
2910Sstevel@tonic-gate 		    (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) &&
2920Sstevel@tonic-gate 		    AUDINFO(lmp)->ai_dynplts) {
2930Sstevel@tonic-gate 			int	fail = 0;
2940Sstevel@tonic-gate 			uint_t	pltndx = reloff / sizeof (Rel);
2950Sstevel@tonic-gate 			uint_t	symndx = (((uintptr_t)nsym -
2964679Srie 			    (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp));
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 			symval = (ulong_t)elf_plt_trace_write(addr, lmp, nlmp,
2990Sstevel@tonic-gate 			    nsym, symndx, pltndx, (caddr_t)symval, sb_flags,
3000Sstevel@tonic-gate 			    &fail);
3010Sstevel@tonic-gate 			if (fail)
3021618Srie 				rtldexit(lml, 1);
3030Sstevel@tonic-gate 		} else {
3040Sstevel@tonic-gate 			/*
3050Sstevel@tonic-gate 			 * Write standard PLT entry to jump directly
3060Sstevel@tonic-gate 			 * to newly bound function.
3070Sstevel@tonic-gate 			 */
3080Sstevel@tonic-gate 			*(ulong_t *)addr = symval;
3090Sstevel@tonic-gate 		}
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	/*
3130Sstevel@tonic-gate 	 * Print binding information and rebuild PLT entry.
3140Sstevel@tonic-gate 	 */
3151618Srie 	DBG_CALL(Dbg_bind_global(lmp, (Addr)from, (Off)(from - ADDR(lmp)),
3161618Srie 	    (Xword)(reloff / sizeof (Rel)), PLT_T_FULL, nlmp, (Addr)symval,
3171618Srie 	    nsym->st_value, name, binfo));
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	/*
3200Sstevel@tonic-gate 	 * Complete any processing for newly loaded objects.  Note we don't
3210Sstevel@tonic-gate 	 * know exactly where any new objects are loaded (we know the object
3220Sstevel@tonic-gate 	 * that supplied the symbol, but others may have been loaded lazily as
3230Sstevel@tonic-gate 	 * we searched for the symbol), so sorting starts from the last
3240Sstevel@tonic-gate 	 * link-map know on entry to this routine.
3250Sstevel@tonic-gate 	 */
3260Sstevel@tonic-gate 	if (entry)
3274679Srie 		load_completion(llmp);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	/*
3300Sstevel@tonic-gate 	 * Some operations like dldump() or dlopen()'ing a relocatable object
3310Sstevel@tonic-gate 	 * result in objects being loaded on rtld's link-map, make sure these
3320Sstevel@tonic-gate 	 * objects are initialized also.
3330Sstevel@tonic-gate 	 */
3340Sstevel@tonic-gate 	if ((LIST(nlmp)->lm_flags & LML_FLG_RTLDLM) && LIST(nlmp)->lm_init)
3354679Srie 		load_completion(nlmp);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	/*
3380Sstevel@tonic-gate 	 * Make sure the object to which we've bound has had it's .init fired.
3390Sstevel@tonic-gate 	 * Cleanup before return to user code.
3400Sstevel@tonic-gate 	 */
3410Sstevel@tonic-gate 	if (entry) {
3420Sstevel@tonic-gate 		is_dep_init(nlmp, lmp);
3436515Sraf 		leave(lml, 0);
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if (lmflags & LML_FLG_RTLDLM)
3471618Srie 		dbg_desc->d_class = dbg_class;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	return (symval);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate /*
3530Sstevel@tonic-gate  * Read and process the relocations for one link object, we assume all
3540Sstevel@tonic-gate  * relocation sections for loadable segments are stored contiguously in
3550Sstevel@tonic-gate  * the file.
3560Sstevel@tonic-gate  */
3570Sstevel@tonic-gate int
elf_reloc(Rt_map * lmp,uint_t plt,int * in_nfavl,APlist ** textrel)3588598SRod.Evans@Sun.COM elf_reloc(Rt_map *lmp, uint_t plt, int *in_nfavl, APlist **textrel)
3590Sstevel@tonic-gate {
3608598SRod.Evans@Sun.COM 	ulong_t		relbgn, relend, relsiz, basebgn, pltbgn, pltend;
3618598SRod.Evans@Sun.COM 	ulong_t		_pltbgn, _pltend;
3628598SRod.Evans@Sun.COM 	ulong_t		dsymndx, roffset, rsymndx, psymndx = 0;
3630Sstevel@tonic-gate 	uchar_t		rtype;
3640Sstevel@tonic-gate 	long		value, pvalue;
3650Sstevel@tonic-gate 	Sym		*symref, *psymref, *symdef, *psymdef;
366*12449SRod.Evans@Sun.COM 	Syminfo		*sip;
3670Sstevel@tonic-gate 	char		*name, *pname;
3680Sstevel@tonic-gate 	Rt_map		*_lmp, *plmp;
3698598SRod.Evans@Sun.COM 	int		ret = 1, noplt = 0;
3700Sstevel@tonic-gate 	int		relacount = RELACOUNT(lmp), plthint = 0;
3710Sstevel@tonic-gate 	Rel		*rel;
3720Sstevel@tonic-gate 	uint_t		binfo, pbinfo;
3735892Sab196087 	APlist		*bound = NULL;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	/*
3760Sstevel@tonic-gate 	 * Although only necessary for lazy binding, initialize the first
3770Sstevel@tonic-gate 	 * global offset entry to go to elf_rtbndr().  dbx(1) seems
3780Sstevel@tonic-gate 	 * to find this useful.
3790Sstevel@tonic-gate 	 */
3800Sstevel@tonic-gate 	if ((plt == 0) && PLTGOT(lmp)) {
3818598SRod.Evans@Sun.COM 		mmapobj_result_t	*mpp;
3828598SRod.Evans@Sun.COM 
3838598SRod.Evans@Sun.COM 		/*
3848598SRod.Evans@Sun.COM 		 * Make sure the segment is writable.
3858598SRod.Evans@Sun.COM 		 */
3868598SRod.Evans@Sun.COM 		if ((((mpp =
3878598SRod.Evans@Sun.COM 		    find_segment((caddr_t)PLTGOT(lmp), lmp)) != NULL) &&
3888598SRod.Evans@Sun.COM 		    ((mpp->mr_prot & PROT_WRITE) == 0)) &&
3898598SRod.Evans@Sun.COM 		    ((set_prot(lmp, mpp, 1) == 0) ||
3908598SRod.Evans@Sun.COM 		    (aplist_append(textrel, mpp, AL_CNT_TEXTREL) == NULL)))
3918598SRod.Evans@Sun.COM 			return (0);
3928598SRod.Evans@Sun.COM 
3930Sstevel@tonic-gate 		elf_plt_init(PLTGOT(lmp), (caddr_t)lmp);
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	/*
3970Sstevel@tonic-gate 	 * Initialize the plt start and end addresses.
3980Sstevel@tonic-gate 	 */
3990Sstevel@tonic-gate 	if ((pltbgn = (ulong_t)JMPREL(lmp)) != 0)
4000Sstevel@tonic-gate 		pltend = pltbgn + (ulong_t)(PLTRELSZ(lmp));
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	relsiz = (ulong_t)(RELENT(lmp));
4030Sstevel@tonic-gate 	basebgn = ADDR(lmp);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	if (PLTRELSZ(lmp))
4060Sstevel@tonic-gate 		plthint = PLTRELSZ(lmp) / relsiz;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	/*
4090Sstevel@tonic-gate 	 * If we've been called upon to promote an RTLD_LAZY object to an
4100Sstevel@tonic-gate 	 * RTLD_NOW then we're only interested in scaning the .plt table.
4110Sstevel@tonic-gate 	 * An uninitialized .plt is the case where the associated got entry
4120Sstevel@tonic-gate 	 * points back to the plt itself.  Determine the range of the real .plt
4130Sstevel@tonic-gate 	 * entries using the _PROCEDURE_LINKAGE_TABLE_ symbol.
4140Sstevel@tonic-gate 	 */
4150Sstevel@tonic-gate 	if (plt) {
4160Sstevel@tonic-gate 		Slookup	sl;
41711827SRod.Evans@Sun.COM 		Sresult	sr;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 		relbgn = pltbgn;
4200Sstevel@tonic-gate 		relend = pltend;
4210Sstevel@tonic-gate 		if (!relbgn || (relbgn == relend))
4220Sstevel@tonic-gate 			return (1);
4230Sstevel@tonic-gate 
4245950Srie 		/*
42511827SRod.Evans@Sun.COM 		 * Initialize the symbol lookup, and symbol result, data
42611827SRod.Evans@Sun.COM 		 * structures.
4275950Srie 		 */
4285950Srie 		SLOOKUP_INIT(sl, MSG_ORIG(MSG_SYM_PLT), lmp, lmp, ld_entry_cnt,
4295950Srie 		    elf_hash(MSG_ORIG(MSG_SYM_PLT)), 0, 0, 0, LKUP_DEFT);
43011827SRod.Evans@Sun.COM 		SRESULT_INIT(sr, MSG_ORIG(MSG_SYM_PLT));
4310Sstevel@tonic-gate 
43211827SRod.Evans@Sun.COM 		if (elf_find_sym(&sl, &sr, &binfo, NULL) == 0)
4330Sstevel@tonic-gate 			return (1);
4340Sstevel@tonic-gate 
43511827SRod.Evans@Sun.COM 		symdef = sr.sr_sym;
4360Sstevel@tonic-gate 		_pltbgn = symdef->st_value;
4370Sstevel@tonic-gate 		if (!(FLAGS(lmp) & FLG_RT_FIXED) &&
4380Sstevel@tonic-gate 		    (symdef->st_shndx != SHN_ABS))
4390Sstevel@tonic-gate 			_pltbgn += basebgn;
4400Sstevel@tonic-gate 		_pltend = _pltbgn + (((PLTRELSZ(lmp) / relsiz)) *
4414679Srie 		    M_PLT_ENTSIZE) + M_PLT_RESERVSZ;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	} else {
4440Sstevel@tonic-gate 		/*
4450Sstevel@tonic-gate 		 * The relocation sections appear to the run-time linker as a
4460Sstevel@tonic-gate 		 * single table.  Determine the address of the beginning and end
4470Sstevel@tonic-gate 		 * of this table.  There are two different interpretations of
4480Sstevel@tonic-gate 		 * the ABI at this point:
4490Sstevel@tonic-gate 		 *
4500Sstevel@tonic-gate 		 *   o	The REL table and its associated RELSZ indicate the
4510Sstevel@tonic-gate 		 *	concatenation of *all* relocation sections (this is the
4520Sstevel@tonic-gate 		 *	model our link-editor constructs).
4530Sstevel@tonic-gate 		 *
4540Sstevel@tonic-gate 		 *   o	The REL table and its associated RELSZ indicate the
4550Sstevel@tonic-gate 		 *	concatenation of all *but* the .plt relocations.  These
4560Sstevel@tonic-gate 		 *	relocations are specified individually by the JMPREL and
4570Sstevel@tonic-gate 		 *	PLTRELSZ entries.
4580Sstevel@tonic-gate 		 *
4590Sstevel@tonic-gate 		 * Determine from our knowledege of the relocation range and
4600Sstevel@tonic-gate 		 * .plt range, the range of the total relocation table.  Note
4610Sstevel@tonic-gate 		 * that one other ABI assumption seems to be that the .plt
4620Sstevel@tonic-gate 		 * relocations always follow any other relocations, the
4630Sstevel@tonic-gate 		 * following range checking drops that assumption.
4640Sstevel@tonic-gate 		 */
4650Sstevel@tonic-gate 		relbgn = (ulong_t)(REL(lmp));
4660Sstevel@tonic-gate 		relend = relbgn + (ulong_t)(RELSZ(lmp));
4670Sstevel@tonic-gate 		if (pltbgn) {
4680Sstevel@tonic-gate 			if (!relbgn || (relbgn > pltbgn))
4690Sstevel@tonic-gate 				relbgn = pltbgn;
4700Sstevel@tonic-gate 			if (!relbgn || (relend < pltend))
4710Sstevel@tonic-gate 				relend = pltend;
4720Sstevel@tonic-gate 		}
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 	if (!relbgn || (relbgn == relend)) {
4751618Srie 		DBG_CALL(Dbg_reloc_run(lmp, 0, plt, DBG_REL_NONE));
4760Sstevel@tonic-gate 		return (1);
4770Sstevel@tonic-gate 	}
4781618Srie 	DBG_CALL(Dbg_reloc_run(lmp, M_REL_SHT_TYPE, plt, DBG_REL_START));
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	/*
4810Sstevel@tonic-gate 	 * If we're processing a dynamic executable in lazy mode there is no
4820Sstevel@tonic-gate 	 * need to scan the .rel.plt table, however if we're processing a shared
4830Sstevel@tonic-gate 	 * object in lazy mode the .got addresses associated to each .plt must
4840Sstevel@tonic-gate 	 * be relocated to reflect the location of the shared object.
4850Sstevel@tonic-gate 	 */
4860Sstevel@tonic-gate 	if (pltbgn && ((MODE(lmp) & RTLD_NOW) == 0) &&
4870Sstevel@tonic-gate 	    (FLAGS(lmp) & FLG_RT_FIXED))
4880Sstevel@tonic-gate 		noplt = 1;
4890Sstevel@tonic-gate 
490*12449SRod.Evans@Sun.COM 	sip = SYMINFO(lmp);
4910Sstevel@tonic-gate 	/*
4920Sstevel@tonic-gate 	 * Loop through relocations.
4930Sstevel@tonic-gate 	 */
4940Sstevel@tonic-gate 	while (relbgn < relend) {
4958598SRod.Evans@Sun.COM 		mmapobj_result_t	*mpp;
4968598SRod.Evans@Sun.COM 		uint_t			sb_flags = 0;
4970Sstevel@tonic-gate 
4986206Sab196087 		rtype = ELF_R_TYPE(((Rel *)relbgn)->r_info, M_MACH);
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 		/*
5010Sstevel@tonic-gate 		 * If this is a RELATIVE relocation in a shared object (the
5020Sstevel@tonic-gate 		 * common case), and if we are not debugging, then jump into a
5038598SRod.Evans@Sun.COM 		 * tighter relocation loop (elf_reloc_relative).
5040Sstevel@tonic-gate 		 */
5050Sstevel@tonic-gate 		if ((rtype == R_386_RELATIVE) &&
5061618Srie 		    ((FLAGS(lmp) & FLG_RT_FIXED) == 0) && (DBG_ENABLED == 0)) {
5070Sstevel@tonic-gate 			if (relacount) {
5088598SRod.Evans@Sun.COM 				relbgn = elf_reloc_relative_count(relbgn,
509*12449SRod.Evans@Sun.COM 				    relacount, relsiz, basebgn, lmp,
510*12449SRod.Evans@Sun.COM 				    textrel, 0);
5110Sstevel@tonic-gate 				relacount = 0;
5120Sstevel@tonic-gate 			} else {
5130Sstevel@tonic-gate 				relbgn = elf_reloc_relative(relbgn, relend,
514*12449SRod.Evans@Sun.COM 				    relsiz, basebgn, lmp, textrel, 0);
5150Sstevel@tonic-gate 			}
5160Sstevel@tonic-gate 			if (relbgn >= relend)
5170Sstevel@tonic-gate 				break;
5186206Sab196087 			rtype = ELF_R_TYPE(((Rel *)relbgn)->r_info, M_MACH);
5190Sstevel@tonic-gate 		}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 		roffset = ((Rel *)relbgn)->r_offset;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 		/*
5240Sstevel@tonic-gate 		 * If this is a shared object, add the base address to offset.
5250Sstevel@tonic-gate 		 */
5260Sstevel@tonic-gate 		if (!(FLAGS(lmp) & FLG_RT_FIXED)) {
5270Sstevel@tonic-gate 			/*
5280Sstevel@tonic-gate 			 * If we're processing lazy bindings, we have to step
5290Sstevel@tonic-gate 			 * through the plt entries and add the base address
5300Sstevel@tonic-gate 			 * to the corresponding got entry.
5310Sstevel@tonic-gate 			 */
5320Sstevel@tonic-gate 			if (plthint && (plt == 0) &&
5330Sstevel@tonic-gate 			    (rtype == R_386_JMP_SLOT) &&
5340Sstevel@tonic-gate 			    ((MODE(lmp) & RTLD_NOW) == 0)) {
5358598SRod.Evans@Sun.COM 				relbgn = elf_reloc_relative_count(relbgn,
536*12449SRod.Evans@Sun.COM 				    plthint, relsiz, basebgn, lmp, textrel, 0);
5370Sstevel@tonic-gate 				plthint = 0;
5380Sstevel@tonic-gate 				continue;
5390Sstevel@tonic-gate 			}
5400Sstevel@tonic-gate 			roffset += basebgn;
5410Sstevel@tonic-gate 		}
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 		rsymndx = ELF_R_SYM(((Rel *)relbgn)->r_info);
5440Sstevel@tonic-gate 		rel = (Rel *)relbgn;
5450Sstevel@tonic-gate 		relbgn += relsiz;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 		/*
5480Sstevel@tonic-gate 		 * Optimizations.
5490Sstevel@tonic-gate 		 */
5500Sstevel@tonic-gate 		if (rtype == R_386_NONE)
5510Sstevel@tonic-gate 			continue;
5520Sstevel@tonic-gate 		if (noplt && ((ulong_t)rel >= pltbgn) &&
5530Sstevel@tonic-gate 		    ((ulong_t)rel < pltend)) {
5540Sstevel@tonic-gate 			relbgn = pltend;
5550Sstevel@tonic-gate 			continue;
5560Sstevel@tonic-gate 		}
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 		/*
5598598SRod.Evans@Sun.COM 		 * If we're promoting plts, determine if this one has already
5600Sstevel@tonic-gate 		 * been written.
5610Sstevel@tonic-gate 		 */
5628598SRod.Evans@Sun.COM 		if (plt && ((*(ulong_t *)roffset < _pltbgn) ||
5638598SRod.Evans@Sun.COM 		    (*(ulong_t *)roffset > _pltend)))
5648598SRod.Evans@Sun.COM 			continue;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 		/*
5670Sstevel@tonic-gate 		 * If this relocation is not against part of the image
5680Sstevel@tonic-gate 		 * mapped into memory we skip it.
5690Sstevel@tonic-gate 		 */
5708598SRod.Evans@Sun.COM 		if ((mpp = find_segment((caddr_t)roffset, lmp)) == NULL) {
5714679Srie 			elf_reloc_bad(lmp, (void *)rel, rtype, roffset,
5724679Srie 			    rsymndx);
5730Sstevel@tonic-gate 			continue;
5740Sstevel@tonic-gate 		}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 		binfo = 0;
5770Sstevel@tonic-gate 		/*
5780Sstevel@tonic-gate 		 * If a symbol index is specified then get the symbol table
5790Sstevel@tonic-gate 		 * entry, locate the symbol definition, and determine its
5800Sstevel@tonic-gate 		 * address.
5810Sstevel@tonic-gate 		 */
5820Sstevel@tonic-gate 		if (rsymndx) {
5830Sstevel@tonic-gate 			/*
584*12449SRod.Evans@Sun.COM 			 * If a Syminfo section is provided, determine if this
585*12449SRod.Evans@Sun.COM 			 * symbol is deferred, and if so, skip this relocation.
586*12449SRod.Evans@Sun.COM 			 */
587*12449SRod.Evans@Sun.COM 			if (sip && is_sym_deferred((ulong_t)rel, basebgn, lmp,
588*12449SRod.Evans@Sun.COM 			    textrel, sip, rsymndx))
589*12449SRod.Evans@Sun.COM 				continue;
590*12449SRod.Evans@Sun.COM 
591*12449SRod.Evans@Sun.COM 			/*
5920Sstevel@tonic-gate 			 * Get the local symbol table entry.
5930Sstevel@tonic-gate 			 */
5940Sstevel@tonic-gate 			symref = (Sym *)((ulong_t)SYMTAB(lmp) +
5950Sstevel@tonic-gate 			    (rsymndx * SYMENT(lmp)));
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 			/*
5980Sstevel@tonic-gate 			 * If this is a local symbol, just use the base address.
5990Sstevel@tonic-gate 			 * (we should have no local relocations in the
6000Sstevel@tonic-gate 			 * executable).
6010Sstevel@tonic-gate 			 */
6020Sstevel@tonic-gate 			if (ELF_ST_BIND(symref->st_info) == STB_LOCAL) {
6030Sstevel@tonic-gate 				value = basebgn;
60411827SRod.Evans@Sun.COM 				name = NULL;
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 				/*
6072145Srie 				 * Special case TLS relocations.
6080Sstevel@tonic-gate 				 */
6092145Srie 				if (rtype == R_386_TLS_DTPMOD32) {
6102145Srie 					/*
6112145Srie 					 * Use the TLS modid.
6122145Srie 					 */
6130Sstevel@tonic-gate 					value = TLSMODID(lmp);
6142145Srie 
6152145Srie 				} else if (rtype == R_386_TLS_TPOFF) {
6162145Srie 					if ((value = elf_static_tls(lmp, symref,
6172145Srie 					    rel, rtype, 0, roffset, 0)) == 0) {
6182145Srie 						ret = 0;
6192145Srie 						break;
6202145Srie 					}
6212145Srie 				}
6220Sstevel@tonic-gate 			} else {
6230Sstevel@tonic-gate 				/*
6240Sstevel@tonic-gate 				 * If the symbol index is equal to the previous
6250Sstevel@tonic-gate 				 * symbol index relocation we processed then
6260Sstevel@tonic-gate 				 * reuse the previous values. (Note that there
6270Sstevel@tonic-gate 				 * have been cases where a relocation exists
6280Sstevel@tonic-gate 				 * against a copy relocation symbol, our ld(1)
6290Sstevel@tonic-gate 				 * should optimize this away, but make sure we
6300Sstevel@tonic-gate 				 * don't use the same symbol information should
6310Sstevel@tonic-gate 				 * this case exist).
6320Sstevel@tonic-gate 				 */
6330Sstevel@tonic-gate 				if ((rsymndx == psymndx) &&
6340Sstevel@tonic-gate 				    (rtype != R_386_COPY)) {
6350Sstevel@tonic-gate 					/* LINTED */
6360Sstevel@tonic-gate 					if (psymdef == 0) {
6371618Srie 						DBG_CALL(Dbg_bind_weak(lmp,
6381618Srie 						    (Addr)roffset, (Addr)
6390Sstevel@tonic-gate 						    (roffset - basebgn), name));
6400Sstevel@tonic-gate 						continue;
6410Sstevel@tonic-gate 					}
6420Sstevel@tonic-gate 					/* LINTED */
6430Sstevel@tonic-gate 					value = pvalue;
6440Sstevel@tonic-gate 					/* LINTED */
6450Sstevel@tonic-gate 					name = pname;
6460Sstevel@tonic-gate 					/* LINTED */
6470Sstevel@tonic-gate 					symdef = psymdef;
6480Sstevel@tonic-gate 					/* LINTED */
6490Sstevel@tonic-gate 					symref = psymref;
6500Sstevel@tonic-gate 					/* LINTED */
6510Sstevel@tonic-gate 					_lmp = plmp;
6520Sstevel@tonic-gate 					/* LINTED */
6530Sstevel@tonic-gate 					binfo = pbinfo;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 					if ((LIST(_lmp)->lm_tflags |
6568598SRod.Evans@Sun.COM 					    AFLAGS(_lmp)) &
6570Sstevel@tonic-gate 					    LML_TFLG_AUD_SYMBIND) {
6580Sstevel@tonic-gate 						value = audit_symbind(lmp, _lmp,
6590Sstevel@tonic-gate 						    /* LINTED */
6600Sstevel@tonic-gate 						    symdef, dsymndx, value,
6610Sstevel@tonic-gate 						    &sb_flags);
6620Sstevel@tonic-gate 					}
6630Sstevel@tonic-gate 				} else {
6640Sstevel@tonic-gate 					Slookup		sl;
66511827SRod.Evans@Sun.COM 					Sresult		sr;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 					/*
6680Sstevel@tonic-gate 					 * Lookup the symbol definition.
66911827SRod.Evans@Sun.COM 					 * Initialize the symbol lookup, and
67011827SRod.Evans@Sun.COM 					 * symbol result, data structures.
6710Sstevel@tonic-gate 					 */
6720Sstevel@tonic-gate 					name = (char *)(STRTAB(lmp) +
6730Sstevel@tonic-gate 					    symref->st_name);
6740Sstevel@tonic-gate 
6755950Srie 					SLOOKUP_INIT(sl, name, lmp, 0,
6765950Srie 					    ld_entry_cnt, 0, rsymndx, symref,
6775950Srie 					    rtype, LKUP_STDRELOC);
67811827SRod.Evans@Sun.COM 					SRESULT_INIT(sr, name);
67911827SRod.Evans@Sun.COM 					symdef = NULL;
6800Sstevel@tonic-gate 
68111827SRod.Evans@Sun.COM 					if (lookup_sym(&sl, &sr, &binfo,
68211827SRod.Evans@Sun.COM 					    in_nfavl)) {
68311827SRod.Evans@Sun.COM 						name = (char *)sr.sr_name;
68411827SRod.Evans@Sun.COM 						_lmp = sr.sr_dmap;
68511827SRod.Evans@Sun.COM 						symdef = sr.sr_sym;
68611827SRod.Evans@Sun.COM 					}
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 					/*
6890Sstevel@tonic-gate 					 * If the symbol is not found and the
6900Sstevel@tonic-gate 					 * reference was not to a weak symbol,
6910Sstevel@tonic-gate 					 * report an error.  Weak references
6920Sstevel@tonic-gate 					 * may be unresolved.
6930Sstevel@tonic-gate 					 */
6944679Srie 					/* BEGIN CSTYLED */
6950Sstevel@tonic-gate 					if (symdef == 0) {
6966150Srie 					    if (sl.sl_bind != STB_WEAK) {
6976150Srie 						if (elf_reloc_error(lmp, name,
6986150Srie 						    rel, binfo))
6996150Srie 							continue;
7008598SRod.Evans@Sun.COM 
7018598SRod.Evans@Sun.COM 					   	ret = 0;
7026150Srie 						break;
7031618Srie 
7040Sstevel@tonic-gate 					    } else {
7050Sstevel@tonic-gate 						psymndx = rsymndx;
7060Sstevel@tonic-gate 						psymdef = 0;
7070Sstevel@tonic-gate 
7081618Srie 						DBG_CALL(Dbg_bind_weak(lmp,
7091618Srie 						    (Addr)roffset, (Addr)
7100Sstevel@tonic-gate 						    (roffset - basebgn), name));
7110Sstevel@tonic-gate 						continue;
7120Sstevel@tonic-gate 					    }
7130Sstevel@tonic-gate 					}
7144679Srie 					/* END CSTYLED */
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 					/*
7170Sstevel@tonic-gate 					 * If symbol was found in an object
7180Sstevel@tonic-gate 					 * other than the referencing object
7190Sstevel@tonic-gate 					 * then record the binding.
7200Sstevel@tonic-gate 					 */
7210Sstevel@tonic-gate 					if ((lmp != _lmp) && ((FLAGS1(_lmp) &
7220Sstevel@tonic-gate 					    FL1_RT_NOINIFIN) == 0)) {
7235892Sab196087 						if (aplist_test(&bound, _lmp,
7240Sstevel@tonic-gate 						    AL_CNT_RELBIND) == 0) {
7250Sstevel@tonic-gate 							ret = 0;
7260Sstevel@tonic-gate 							break;
7270Sstevel@tonic-gate 						}
7280Sstevel@tonic-gate 					}
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 					/*
7310Sstevel@tonic-gate 					 * Calculate the location of definition;
7320Sstevel@tonic-gate 					 * symbol value plus base address of
7330Sstevel@tonic-gate 					 * containing shared object.
7340Sstevel@tonic-gate 					 */
7352850Srie 					if (IS_SIZE(rtype))
7362850Srie 						value = symdef->st_size;
7372850Srie 					else
7382850Srie 						value = symdef->st_value;
7392850Srie 
7400Sstevel@tonic-gate 					if (!(FLAGS(_lmp) & FLG_RT_FIXED) &&
7412850Srie 					    !(IS_SIZE(rtype)) &&
7420Sstevel@tonic-gate 					    (symdef->st_shndx != SHN_ABS) &&
7430Sstevel@tonic-gate 					    (ELF_ST_TYPE(symdef->st_info) !=
7440Sstevel@tonic-gate 					    STT_TLS))
7450Sstevel@tonic-gate 						value += ADDR(_lmp);
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 					/*
7480Sstevel@tonic-gate 					 * Retain this symbol index and the
7490Sstevel@tonic-gate 					 * value in case it can be used for the
7500Sstevel@tonic-gate 					 * subsequent relocations.
7510Sstevel@tonic-gate 					 */
7520Sstevel@tonic-gate 					if (rtype != R_386_COPY) {
7530Sstevel@tonic-gate 						psymndx = rsymndx;
7540Sstevel@tonic-gate 						pvalue = value;
7550Sstevel@tonic-gate 						pname = name;
7560Sstevel@tonic-gate 						psymdef = symdef;
7570Sstevel@tonic-gate 						psymref = symref;
7580Sstevel@tonic-gate 						plmp = _lmp;
7590Sstevel@tonic-gate 						pbinfo = binfo;
7600Sstevel@tonic-gate 					}
7610Sstevel@tonic-gate 					if ((LIST(_lmp)->lm_tflags |
7628598SRod.Evans@Sun.COM 					    AFLAGS(_lmp)) &
7630Sstevel@tonic-gate 					    LML_TFLG_AUD_SYMBIND) {
7640Sstevel@tonic-gate 						dsymndx = (((uintptr_t)symdef -
7650Sstevel@tonic-gate 						    (uintptr_t)SYMTAB(_lmp)) /
7660Sstevel@tonic-gate 						    SYMENT(_lmp));
7670Sstevel@tonic-gate 						value = audit_symbind(lmp, _lmp,
7680Sstevel@tonic-gate 						    symdef, dsymndx, value,
7690Sstevel@tonic-gate 						    &sb_flags);
7700Sstevel@tonic-gate 					}
7710Sstevel@tonic-gate 				}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 				/*
7740Sstevel@tonic-gate 				 * If relocation is PC-relative, subtract
7750Sstevel@tonic-gate 				 * offset address.
7760Sstevel@tonic-gate 				 */
7770Sstevel@tonic-gate 				if (IS_PC_RELATIVE(rtype))
7780Sstevel@tonic-gate 					value -= roffset;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 				/*
7812145Srie 				 * Special case TLS relocations.
7820Sstevel@tonic-gate 				 */
7832145Srie 				if (rtype == R_386_TLS_DTPMOD32) {
7842145Srie 					/*
7852145Srie 					 * Relocation value is the TLS modid.
7862145Srie 					 */
7870Sstevel@tonic-gate 					value = TLSMODID(_lmp);
7882145Srie 
7892145Srie 				} else if (rtype == R_386_TLS_TPOFF) {
7902145Srie 					if ((value = elf_static_tls(_lmp,
7912145Srie 					    symdef, rel, rtype, name, roffset,
7922145Srie 					    value)) == 0) {
7932145Srie 						ret = 0;
7942145Srie 						break;
7952145Srie 					}
7962145Srie 				}
7970Sstevel@tonic-gate 			}
7980Sstevel@tonic-gate 		} else {
7990Sstevel@tonic-gate 			/*
8002145Srie 			 * Special cases.
8010Sstevel@tonic-gate 			 */
8022145Srie 			if (rtype == R_386_TLS_DTPMOD32) {
8032145Srie 				/*
8042145Srie 				 * TLS relocation value is the TLS modid.
8052145Srie 				 */
8060Sstevel@tonic-gate 				value = TLSMODID(lmp);
8072145Srie 			} else
8080Sstevel@tonic-gate 				value = basebgn;
80911827SRod.Evans@Sun.COM 
81011827SRod.Evans@Sun.COM 			name = NULL;
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 
8132145Srie 		DBG_CALL(Dbg_reloc_in(LIST(lmp), ELF_DBG_RTLD, M_MACH,
8149406SAli.Bahrami@Sun.COM 		    M_REL_SHT_TYPE, rel, NULL, 0, name));
8152145Srie 
8160Sstevel@tonic-gate 		/*
8178598SRod.Evans@Sun.COM 		 * Make sure the segment is writable.
8180Sstevel@tonic-gate 		 */
8198598SRod.Evans@Sun.COM 		if (((mpp->mr_prot & PROT_WRITE) == 0) &&
8208598SRod.Evans@Sun.COM 		    ((set_prot(lmp, mpp, 1) == 0) ||
8218598SRod.Evans@Sun.COM 		    (aplist_append(textrel, mpp, AL_CNT_TEXTREL) == NULL))) {
8228598SRod.Evans@Sun.COM 			ret = 0;
8238598SRod.Evans@Sun.COM 			break;
8240Sstevel@tonic-gate 		}
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 		/*
8270Sstevel@tonic-gate 		 * Call relocation routine to perform required relocation.
8280Sstevel@tonic-gate 		 */
8290Sstevel@tonic-gate 		switch (rtype) {
8300Sstevel@tonic-gate 		case R_386_COPY:
8310Sstevel@tonic-gate 			if (elf_copy_reloc(name, symref, lmp, (void *)roffset,
8320Sstevel@tonic-gate 			    symdef, _lmp, (const void *)value) == 0)
8330Sstevel@tonic-gate 				ret = 0;
8340Sstevel@tonic-gate 			break;
8350Sstevel@tonic-gate 		case R_386_JMP_SLOT:
8368598SRod.Evans@Sun.COM 			if (((LIST(lmp)->lm_tflags | AFLAGS(lmp)) &
8370Sstevel@tonic-gate 			    (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) &&
8380Sstevel@tonic-gate 			    AUDINFO(lmp)->ai_dynplts) {
8390Sstevel@tonic-gate 				int	fail = 0;
8400Sstevel@tonic-gate 				int	pltndx = (((ulong_t)rel -
8414679Srie 				    (uintptr_t)JMPREL(lmp)) / relsiz);
8420Sstevel@tonic-gate 				int	symndx = (((uintptr_t)symdef -
8434679Srie 				    (uintptr_t)SYMTAB(_lmp)) / SYMENT(_lmp));
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 				(void) elf_plt_trace_write(roffset, lmp, _lmp,
8460Sstevel@tonic-gate 				    symdef, symndx, pltndx, (caddr_t)value,
8470Sstevel@tonic-gate 				    sb_flags, &fail);
8480Sstevel@tonic-gate 				if (fail)
8490Sstevel@tonic-gate 					ret = 0;
8500Sstevel@tonic-gate 			} else {
8510Sstevel@tonic-gate 				/*
8520Sstevel@tonic-gate 				 * Write standard PLT entry to jump directly
8530Sstevel@tonic-gate 				 * to newly bound function.
8540Sstevel@tonic-gate 				 */
8551618Srie 				DBG_CALL(Dbg_reloc_apply_val(LIST(lmp),
8561618Srie 				    ELF_DBG_RTLD, (Xword)roffset,
8570Sstevel@tonic-gate 				    (Xword)value));
8580Sstevel@tonic-gate 				*(ulong_t *)roffset = value;
8590Sstevel@tonic-gate 			}
8600Sstevel@tonic-gate 			break;
8610Sstevel@tonic-gate 		default:
8620Sstevel@tonic-gate 			/*
8630Sstevel@tonic-gate 			 * Write the relocation out.
8640Sstevel@tonic-gate 			 */
8655189Sab196087 			if (do_reloc_rtld(rtype, (uchar_t *)roffset,
8665189Sab196087 			    (Word *)&value, name, NAME(lmp), LIST(lmp)) == 0)
8670Sstevel@tonic-gate 				ret = 0;
8680Sstevel@tonic-gate 
8691618Srie 			DBG_CALL(Dbg_reloc_apply_val(LIST(lmp), ELF_DBG_RTLD,
8701618Srie 			    (Xword)roffset, (Xword)value));
8710Sstevel@tonic-gate 		}
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 		if ((ret == 0) &&
8740Sstevel@tonic-gate 		    ((LIST(lmp)->lm_flags & LML_FLG_TRC_WARN) == 0))
8750Sstevel@tonic-gate 			break;
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 		if (binfo) {
8781618Srie 			DBG_CALL(Dbg_bind_global(lmp, (Addr)roffset,
8791618Srie 			    (Off)(roffset - basebgn), (Xword)(-1), PLT_T_FULL,
8801618Srie 			    _lmp, (Addr)value, symdef->st_value, name, binfo));
8810Sstevel@tonic-gate 		}
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 
8848598SRod.Evans@Sun.COM 	return (relocate_finish(lmp, bound, ret));
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate /*
8880Sstevel@tonic-gate  * Initialize the first few got entries so that function calls go to
8890Sstevel@tonic-gate  * elf_rtbndr:
8900Sstevel@tonic-gate  *
8910Sstevel@tonic-gate  *	GOT[GOT_XLINKMAP] =	the address of the link map
8920Sstevel@tonic-gate  *	GOT[GOT_XRTLD] =	the address of rtbinder
8930Sstevel@tonic-gate  */
8940Sstevel@tonic-gate void
elf_plt_init(void * got,caddr_t l)8950Sstevel@tonic-gate elf_plt_init(void *got, caddr_t l)
8960Sstevel@tonic-gate {
8970Sstevel@tonic-gate 	uint_t		*_got;
8980Sstevel@tonic-gate 	/* LINTED */
8990Sstevel@tonic-gate 	Rt_map		*lmp = (Rt_map *)l;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	_got = (uint_t *)got + M_GOT_XLINKMAP;
9020Sstevel@tonic-gate 	*_got = (uint_t)lmp;
9030Sstevel@tonic-gate 	_got = (uint_t *)got + M_GOT_XRTLD;
9040Sstevel@tonic-gate 	*_got = (uint_t)elf_rtbndr;
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate /*
9080Sstevel@tonic-gate  * For SVR4 Intel compatability.  USL uses /usr/lib/libc.so.1 as the run-time
9090Sstevel@tonic-gate  * linker, so the interpreter's address will differ from /usr/lib/ld.so.1.
9100Sstevel@tonic-gate  * Further, USL has special _iob[] and _ctype[] processing that makes up for the
9110Sstevel@tonic-gate  * fact that these arrays do not have associated copy relocations.  So we try
9120Sstevel@tonic-gate  * and make up for that here.  Any relocations found will be added to the global
9130Sstevel@tonic-gate  * copy relocation list and will be processed in setup().
9140Sstevel@tonic-gate  */
9150Sstevel@tonic-gate static int
_elf_copy_reloc(const char * name,Rt_map * rlmp,Rt_map * dlmp)9160Sstevel@tonic-gate _elf_copy_reloc(const char *name, Rt_map *rlmp, Rt_map *dlmp)
9170Sstevel@tonic-gate {
9180Sstevel@tonic-gate 	Sym		*symref, *symdef;
9190Sstevel@tonic-gate 	caddr_t 	ref, def;
9200Sstevel@tonic-gate 	Rt_map		*_lmp;
9210Sstevel@tonic-gate 	Rel		rel;
9220Sstevel@tonic-gate 	Slookup		sl;
92311827SRod.Evans@Sun.COM 	Sresult		sr;
9240Sstevel@tonic-gate 	uint_t		binfo;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	/*
9270Sstevel@tonic-gate 	 * Determine if the special symbol exists as a reference in the dynamic
9280Sstevel@tonic-gate 	 * executable, and that an associated definition exists in libc.so.1.
9295950Srie 	 *
93011827SRod.Evans@Sun.COM 	 * Initialize the symbol lookup, and symbol result, data structures.
9310Sstevel@tonic-gate 	 */
9325950Srie 	SLOOKUP_INIT(sl, name, rlmp, rlmp, ld_entry_cnt, 0, 0, 0, 0,
9335950Srie 	    LKUP_FIRST);
93411827SRod.Evans@Sun.COM 	SRESULT_INIT(sr, name);
9350Sstevel@tonic-gate 
93611827SRod.Evans@Sun.COM 	if (lookup_sym(&sl, &sr, &binfo, NULL) == 0)
93711827SRod.Evans@Sun.COM 		return (1);
93811827SRod.Evans@Sun.COM 	symref = sr.sr_sym;
93911827SRod.Evans@Sun.COM 
94011827SRod.Evans@Sun.COM 	SLOOKUP_INIT(sl, name, rlmp, dlmp, ld_entry_cnt, 0, 0, 0, 0,
94111827SRod.Evans@Sun.COM 	    LKUP_DEFT);
94211827SRod.Evans@Sun.COM 	SRESULT_INIT(sr, name);
94311827SRod.Evans@Sun.COM 
94411827SRod.Evans@Sun.COM 	if (lookup_sym(&sl, &sr, &binfo, NULL) == 0)
9450Sstevel@tonic-gate 		return (1);
9460Sstevel@tonic-gate 
94711827SRod.Evans@Sun.COM 	_lmp = sr.sr_dmap;
94811827SRod.Evans@Sun.COM 	symdef = sr.sr_sym;
9490Sstevel@tonic-gate 
95011827SRod.Evans@Sun.COM 	if (strcmp(NAME(sr.sr_dmap), MSG_ORIG(MSG_PTH_LIBC)))
9510Sstevel@tonic-gate 		return (1);
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	/*
9540Sstevel@tonic-gate 	 * Determine the reference and definition addresses.
9550Sstevel@tonic-gate 	 */
9560Sstevel@tonic-gate 	ref = (void *)(symref->st_value);
9570Sstevel@tonic-gate 	if (!(FLAGS(rlmp) & FLG_RT_FIXED))
9580Sstevel@tonic-gate 		ref += ADDR(rlmp);
9590Sstevel@tonic-gate 	def = (void *)(symdef->st_value);
96011827SRod.Evans@Sun.COM 	if (!(FLAGS(sr.sr_dmap) & FLG_RT_FIXED))
9610Sstevel@tonic-gate 		def += ADDR(_lmp);
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	/*
9640Sstevel@tonic-gate 	 * Set up a relocation entry for debugging and call the generic copy
9650Sstevel@tonic-gate 	 * relocation function to provide symbol size error checking and to
9660Sstevel@tonic-gate 	 * record the copy relocation that must be performed.
9670Sstevel@tonic-gate 	 */
9680Sstevel@tonic-gate 	rel.r_offset = (Addr)ref;
9690Sstevel@tonic-gate 	rel.r_info = (Word)R_386_COPY;
9701618Srie 	DBG_CALL(Dbg_reloc_in(LIST(rlmp), ELF_DBG_RTLD, M_MACH, M_REL_SHT_TYPE,
9719406SAli.Bahrami@Sun.COM 	    &rel, NULL, 0, name));
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	return (elf_copy_reloc((char *)name, symref, rlmp, (void *)ref, symdef,
9740Sstevel@tonic-gate 	    _lmp, (void *)def));
9750Sstevel@tonic-gate }
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate int
elf_copy_gen(Rt_map * lmp)9780Sstevel@tonic-gate elf_copy_gen(Rt_map *lmp)
9790Sstevel@tonic-gate {
9800Sstevel@tonic-gate 	if (interp && ((ulong_t)interp->i_faddr !=
9810Sstevel@tonic-gate 	    r_debug.rtd_rdebug.r_ldbase) &&
9820Sstevel@tonic-gate 	    !(strcmp(interp->i_name, MSG_ORIG(MSG_PTH_LIBC)))) {
9830Sstevel@tonic-gate 
9841618Srie 		DBG_CALL(Dbg_reloc_run(lmp, M_REL_SHT_TYPE, 0,
9850Sstevel@tonic-gate 		    DBG_REL_START));
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 		if (_elf_copy_reloc(MSG_ORIG(MSG_SYM_CTYPE), lmp,
9880Sstevel@tonic-gate 		    (Rt_map *)NEXT(lmp)) == 0)
9890Sstevel@tonic-gate 			return (0);
9900Sstevel@tonic-gate 		if (_elf_copy_reloc(MSG_ORIG(MSG_SYM_IOB), lmp,
9910Sstevel@tonic-gate 		    (Rt_map *)NEXT(lmp)) == 0)
9920Sstevel@tonic-gate 			return (0);
9930Sstevel@tonic-gate 	}
9940Sstevel@tonic-gate 	return (1);
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate /*
9980Sstevel@tonic-gate  * Plt writing interface to allow debugging initialization to be generic.
9990Sstevel@tonic-gate  */
10000Sstevel@tonic-gate Pltbindtype
10010Sstevel@tonic-gate /* ARGSUSED1 */
elf_plt_write(uintptr_t addr,uintptr_t vaddr,void * rptr,uintptr_t symval,Xword pltndx)10020Sstevel@tonic-gate elf_plt_write(uintptr_t addr, uintptr_t vaddr, void *rptr, uintptr_t symval,
10030Sstevel@tonic-gate 	Xword pltndx)
10040Sstevel@tonic-gate {
10050Sstevel@tonic-gate 	Rel		*rel = (Rel*)rptr;
10060Sstevel@tonic-gate 	uintptr_t	pltaddr;
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	pltaddr = addr + rel->r_offset;
10090Sstevel@tonic-gate 	*(ulong_t *)pltaddr = (ulong_t)symval;
10100Sstevel@tonic-gate 	DBG_CALL(pltcntfull++);
10110Sstevel@tonic-gate 	return (PLT_T_FULL);
10120Sstevel@tonic-gate }
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate /*
10150Sstevel@tonic-gate  * Provide a machine specific interface to the conversion routine.  By calling
10160Sstevel@tonic-gate  * the machine specific version, rather than the generic version, we insure that
10170Sstevel@tonic-gate  * the data tables/strings for all known machine versions aren't dragged into
10180Sstevel@tonic-gate  * ld.so.1.
10190Sstevel@tonic-gate  */
10200Sstevel@tonic-gate const char *
_conv_reloc_type(uint_t rel)10211618Srie _conv_reloc_type(uint_t rel)
10220Sstevel@tonic-gate {
10238598SRod.Evans@Sun.COM 	static Conv_inv_buf_t	inv_buf;
10244734Sab196087 
10254734Sab196087 	return (conv_reloc_386_type(rel, 0, &inv_buf));
10260Sstevel@tonic-gate }
1027