xref: /onnv-gate/usr/src/cmd/sgs/rtld/amd64/amd64_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 /*
23*12449SRod.Evans@Sun.COM  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
256812Sraf 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * amd64 machine dependent and ELF file class dependent functions.
280Sstevel@tonic-gate  * Contains routines for performing function binding and symbol relocations.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include	<stdio.h>
320Sstevel@tonic-gate #include	<sys/elf.h>
330Sstevel@tonic-gate #include	<sys/elf_amd64.h>
340Sstevel@tonic-gate #include	<sys/mman.h>
350Sstevel@tonic-gate #include	<dlfcn.h>
360Sstevel@tonic-gate #include	<synch.h>
370Sstevel@tonic-gate #include	<string.h>
381618Srie #include	<debug.h>
391618Srie #include	<reloc.h>
401618Srie #include	<conv.h>
410Sstevel@tonic-gate #include	"_rtld.h"
420Sstevel@tonic-gate #include	"_audit.h"
430Sstevel@tonic-gate #include	"_elf.h"
44*12449SRod.Evans@Sun.COM #include	"_inline_gen.h"
45*12449SRod.Evans@Sun.COM #include	"_inline_reloc.h"
460Sstevel@tonic-gate #include	"msg.h"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate extern void	elf_rtbndr(Rt_map *, ulong_t, caddr_t);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate int
elf_mach_flags_check(Rej_desc * rej,Ehdr * ehdr)510Sstevel@tonic-gate elf_mach_flags_check(Rej_desc *rej, Ehdr *ehdr)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	/*
540Sstevel@tonic-gate 	 * Check machine type and flags.
550Sstevel@tonic-gate 	 */
560Sstevel@tonic-gate 	if (ehdr->e_flags != 0) {
570Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_BADFLAG;
580Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_flags;
590Sstevel@tonic-gate 		return (0);
600Sstevel@tonic-gate 	}
610Sstevel@tonic-gate 	return (1);
620Sstevel@tonic-gate }
630Sstevel@tonic-gate 
640Sstevel@tonic-gate void
ldso_plt_init(Rt_map * lmp)658598SRod.Evans@Sun.COM ldso_plt_init(Rt_map *lmp)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	/*
680Sstevel@tonic-gate 	 * There is no need to analyze ld.so because we don't map in any of
690Sstevel@tonic-gate 	 * its dependencies.  However we may map these dependencies in later
700Sstevel@tonic-gate 	 * (as if ld.so had dlopened them), so initialize the plt and the
710Sstevel@tonic-gate 	 * permission information.
720Sstevel@tonic-gate 	 */
730Sstevel@tonic-gate 	if (PLTGOT(lmp))
748598SRod.Evans@Sun.COM 		elf_plt_init((PLTGOT(lmp)), (caddr_t)lmp);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static const uchar_t dyn_plt_template[] = {
780Sstevel@tonic-gate /* 0x00 */  0x55,			/* pushq %rbp */
790Sstevel@tonic-gate /* 0x01 */  0x48, 0x89, 0xe5,		/* movq  %rsp, %rbp */
800Sstevel@tonic-gate /* 0x04 */  0x48, 0x83, 0xec, 0x10,	/* subq	 $0x10, %rsp */
810Sstevel@tonic-gate /* 0x08 */  0x4c, 0x8d, 0x1d, 0x00,	/* leaq  trace_fields(%rip), %r11 */
820Sstevel@tonic-gate 		0x00, 0x00, 0x00,
830Sstevel@tonic-gate /* 0x0f */  0x4c, 0x89, 0x5d, 0xf8,	/* movq  %r11, -0x8(%rbp) */
840Sstevel@tonic-gate /* 0x13 */  0x49, 0xbb, 0x00, 0x00, 	/* movq  $elf_plt_trace, %r11 */
850Sstevel@tonic-gate 		0x00, 0x00, 0x00,
860Sstevel@tonic-gate 		0x00, 0x00, 0x00,
870Sstevel@tonic-gate /* 0x1d */  0x41, 0xff, 0xe3		/* jmp   *%r11 */
880Sstevel@tonic-gate /* 0x20 */
890Sstevel@tonic-gate };
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * And the virutal outstanding relocations against the
930Sstevel@tonic-gate  * above block are:
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  *	reloc		offset	Addend	symbol
960Sstevel@tonic-gate  *	R_AMD64_PC32	0x0b	-4	trace_fields
970Sstevel@tonic-gate  *	R_AMD64_64	0x15	0	elf_plt_trace
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate #define	TRCREL1OFF	0x0b
1010Sstevel@tonic-gate #define	TRCREL2OFF	0x15
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate int	dyn_plt_ent_size = sizeof (dyn_plt_template);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * the dynamic plt entry is:
1070Sstevel@tonic-gate  *
1080Sstevel@tonic-gate  *	pushq	%rbp
1090Sstevel@tonic-gate  *	movq	%rsp, %rbp
1100Sstevel@tonic-gate  *	subq	$0x10, %rsp
1110Sstevel@tonic-gate  *	leaq	trace_fields(%rip), %r11
1120Sstevel@tonic-gate  *	movq	%r11, -0x8(%rbp)
1130Sstevel@tonic-gate  *	movq	$elf_plt_trace, %r11
1140Sstevel@tonic-gate  *	jmp	*%r11
1150Sstevel@tonic-gate  * dyn_data:
1160Sstevel@tonic-gate  *	.align  8
1170Sstevel@tonic-gate  *	uintptr_t	reflmp
1180Sstevel@tonic-gate  *	uintptr_t	deflmp
1190Sstevel@tonic-gate  *	uint_t		symndx
1200Sstevel@tonic-gate  *	uint_t		sb_flags
1210Sstevel@tonic-gate  *	Sym		symdef
1220Sstevel@tonic-gate  */
1230Sstevel@tonic-gate static caddr_t
elf_plt_trace_write(ulong_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)1240Sstevel@tonic-gate elf_plt_trace_write(ulong_t roffset, Rt_map *rlmp, Rt_map *dlmp, Sym *sym,
1250Sstevel@tonic-gate     uint_t symndx, uint_t pltndx, caddr_t to, uint_t sb_flags, int *fail)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	extern int	elf_plt_trace();
1280Sstevel@tonic-gate 	ulong_t		got_entry;
1290Sstevel@tonic-gate 	uchar_t		*dyn_plt;
1300Sstevel@tonic-gate 	uintptr_t	*dyndata;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	/*
1330Sstevel@tonic-gate 	 * We only need to add the glue code if there is an auditing
1340Sstevel@tonic-gate 	 * library that is interested in this binding.
1350Sstevel@tonic-gate 	 */
1360Sstevel@tonic-gate 	dyn_plt = (uchar_t *)((uintptr_t)AUDINFO(rlmp)->ai_dynplts +
1374679Srie 	    (pltndx * dyn_plt_ent_size));
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	/*
1400Sstevel@tonic-gate 	 * Have we initialized this dynamic plt entry yet?  If we haven't do it
1410Sstevel@tonic-gate 	 * now.  Otherwise this function has been called before, but from a
1420Sstevel@tonic-gate 	 * different plt (ie. from another shared object).  In that case
1430Sstevel@tonic-gate 	 * we just set the plt to point to the new dyn_plt.
1440Sstevel@tonic-gate 	 */
1450Sstevel@tonic-gate 	if (*dyn_plt == 0) {
1468598SRod.Evans@Sun.COM 		Sym	*symp;
1470Sstevel@tonic-gate 		Xword	symvalue;
1481618Srie 		Lm_list	*lml = LIST(rlmp);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 		(void) memcpy((void *)dyn_plt, dyn_plt_template,
1510Sstevel@tonic-gate 		    sizeof (dyn_plt_template));
1520Sstevel@tonic-gate 		dyndata = (uintptr_t *)((uintptr_t)dyn_plt +
1530Sstevel@tonic-gate 		    ROUND(sizeof (dyn_plt_template), M_WORD_ALIGN));
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 		/*
1560Sstevel@tonic-gate 		 * relocate:
1570Sstevel@tonic-gate 		 *	leaq	trace_fields(%rip), %r11
1580Sstevel@tonic-gate 		 *	R_AMD64_PC32	0x0b	-4	trace_fields
1590Sstevel@tonic-gate 		 */
1600Sstevel@tonic-gate 		symvalue = (Xword)((uintptr_t)dyndata -
1610Sstevel@tonic-gate 		    (uintptr_t)(&dyn_plt[TRCREL1OFF]) - 4);
1625189Sab196087 		if (do_reloc_rtld(R_AMD64_PC32, &dyn_plt[TRCREL1OFF],
1631618Srie 		    &symvalue, MSG_ORIG(MSG_SYM_LADYNDATA),
1641618Srie 		    MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) {
1650Sstevel@tonic-gate 			*fail = 1;
1660Sstevel@tonic-gate 			return (0);
1670Sstevel@tonic-gate 		}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 		/*
1700Sstevel@tonic-gate 		 * relocating:
1710Sstevel@tonic-gate 		 *	movq	$elf_plt_trace, %r11
1720Sstevel@tonic-gate 		 *	R_AMD64_64	0x15	0	elf_plt_trace
1730Sstevel@tonic-gate 		 */
1740Sstevel@tonic-gate 		symvalue = (Xword)elf_plt_trace;
1755189Sab196087 		if (do_reloc_rtld(R_AMD64_64, &dyn_plt[TRCREL2OFF],
1761618Srie 		    &symvalue, MSG_ORIG(MSG_SYM_ELFPLTTRACE),
1771618Srie 		    MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) {
1780Sstevel@tonic-gate 			*fail = 1;
1790Sstevel@tonic-gate 			return (0);
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 		*dyndata++ = (uintptr_t)rlmp;
1830Sstevel@tonic-gate 		*dyndata++ = (uintptr_t)dlmp;
1840Sstevel@tonic-gate 		*dyndata = (uintptr_t)(((uint64_t)sb_flags << 32) | symndx);
1850Sstevel@tonic-gate 		dyndata++;
1860Sstevel@tonic-gate 		symp = (Sym *)dyndata;
1870Sstevel@tonic-gate 		*symp = *sym;
1880Sstevel@tonic-gate 		symp->st_value = (Addr)to;
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	got_entry = (ulong_t)roffset;
1920Sstevel@tonic-gate 	*(ulong_t *)got_entry = (ulong_t)dyn_plt;
1930Sstevel@tonic-gate 	return ((caddr_t)dyn_plt);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate  * Function binding routine - invoked on the first call to a function through
1980Sstevel@tonic-gate  * the procedure linkage table;
1990Sstevel@tonic-gate  * passes first through an assembly language interface.
2000Sstevel@tonic-gate  *
2010Sstevel@tonic-gate  * Takes the offset into the relocation table of the associated
2020Sstevel@tonic-gate  * relocation entry and the address of the link map (rt_private_map struct)
2030Sstevel@tonic-gate  * for the entry.
2040Sstevel@tonic-gate  *
2050Sstevel@tonic-gate  * Returns the address of the function referenced after re-writing the PLT
2060Sstevel@tonic-gate  * entry to invoke the function directly.
2070Sstevel@tonic-gate  *
2080Sstevel@tonic-gate  * On error, causes process to terminate with a signal.
2090Sstevel@tonic-gate  */
2100Sstevel@tonic-gate ulong_t
elf_bndr(Rt_map * lmp,ulong_t pltndx,caddr_t from)2110Sstevel@tonic-gate elf_bndr(Rt_map *lmp, ulong_t pltndx, caddr_t from)
2120Sstevel@tonic-gate {
2138598SRod.Evans@Sun.COM 	Rt_map		*nlmp, *llmp;
2140Sstevel@tonic-gate 	ulong_t		addr, reloff, symval, rsymndx;
2150Sstevel@tonic-gate 	char		*name;
2160Sstevel@tonic-gate 	Rela		*rptr;
2175220Srie 	Sym		*rsym, *nsym;
2181618Srie 	uint_t		binfo, sb_flags = 0, dbg_class;
2190Sstevel@tonic-gate 	Slookup		sl;
22011827SRod.Evans@Sun.COM 	Sresult		sr;
2211618Srie 	int		entry, lmflags;
2221618Srie 	Lm_list		*lml;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	/*
2250Sstevel@tonic-gate 	 * For compatibility with libthread (TI_VERSION 1) we track the entry
2260Sstevel@tonic-gate 	 * value.  A zero value indicates we have recursed into ld.so.1 to
2270Sstevel@tonic-gate 	 * further process a locking request.  Under this recursion we disable
2280Sstevel@tonic-gate 	 * tsort and cleanup activities.
2290Sstevel@tonic-gate 	 */
2306515Sraf 	entry = enter(0);
2310Sstevel@tonic-gate 
2321618Srie 	lml = LIST(lmp);
2331618Srie 	if ((lmflags = lml->lm_flags) & LML_FLG_RTLDLM) {
2341618Srie 		dbg_class = dbg_desc->d_class;
2351618Srie 		dbg_desc->d_class = 0;
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	/*
2390Sstevel@tonic-gate 	 * Perform some basic sanity checks.  If we didn't get a load map or
2400Sstevel@tonic-gate 	 * the relocation offset is invalid then its possible someone has walked
2410Sstevel@tonic-gate 	 * over the .got entries or jumped to plt0 out of the blue.
2420Sstevel@tonic-gate 	 */
2430Sstevel@tonic-gate 	if ((!lmp) && (pltndx <=
2440Sstevel@tonic-gate 	    (ulong_t)PLTRELSZ(lmp) / (ulong_t)RELENT(lmp))) {
2454734Sab196087 		Conv_inv_buf_t inv_buf;
2464734Sab196087 
2471618Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_PLTREF),
2484734Sab196087 		    conv_reloc_amd64_type(R_AMD64_JUMP_SLOT, 0, &inv_buf),
2491618Srie 		    EC_NATPTR(lmp), EC_XWORD(pltndx), EC_NATPTR(from));
2501618Srie 		rtldexit(lml, 1);
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 	reloff = pltndx * (ulong_t)RELENT(lmp);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/*
2550Sstevel@tonic-gate 	 * Use relocation entry to get symbol table entry and symbol name.
2560Sstevel@tonic-gate 	 */
2570Sstevel@tonic-gate 	addr = (ulong_t)JMPREL(lmp);
2580Sstevel@tonic-gate 	rptr = (Rela *)(addr + reloff);
2590Sstevel@tonic-gate 	rsymndx = ELF_R_SYM(rptr->r_info);
2605220Srie 	rsym = (Sym *)((ulong_t)SYMTAB(lmp) + (rsymndx * SYMENT(lmp)));
2615220Srie 	name = (char *)(STRTAB(lmp) + rsym->st_name);
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	/*
2640Sstevel@tonic-gate 	 * Determine the last link-map of this list, this'll be the starting
2650Sstevel@tonic-gate 	 * point for any tsort() processing.
2660Sstevel@tonic-gate 	 */
2671618Srie 	llmp = lml->lm_tail;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	/*
27011827SRod.Evans@Sun.COM 	 * Find definition for symbol.  Initialize the symbol lookup, and
27111827SRod.Evans@Sun.COM 	 * symbol result, data structures.
2720Sstevel@tonic-gate 	 */
2735950Srie 	SLOOKUP_INIT(sl, name, lmp, lml->lm_head, ld_entry_cnt, 0,
2745950Srie 	    rsymndx, rsym, 0, LKUP_DEFT);
27511827SRod.Evans@Sun.COM 	SRESULT_INIT(sr, name);
2760Sstevel@tonic-gate 
27711827SRod.Evans@Sun.COM 	if (lookup_sym(&sl, &sr, &binfo, NULL) == 0) {
2781618Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOSYM), NAME(lmp),
2790Sstevel@tonic-gate 		    demangle(name));
2801618Srie 		rtldexit(lml, 1);
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 
28311827SRod.Evans@Sun.COM 	name = (char *)sr.sr_name;
28411827SRod.Evans@Sun.COM 	nlmp = sr.sr_dmap;
28511827SRod.Evans@Sun.COM 	nsym = sr.sr_sym;
28611827SRod.Evans@Sun.COM 
2870Sstevel@tonic-gate 	symval = nsym->st_value;
28811827SRod.Evans@Sun.COM 
2890Sstevel@tonic-gate 	if (!(FLAGS(nlmp) & FLG_RT_FIXED) &&
2900Sstevel@tonic-gate 	    (nsym->st_shndx != SHN_ABS))
2910Sstevel@tonic-gate 		symval += ADDR(nlmp);
2920Sstevel@tonic-gate 	if ((lmp != nlmp) && ((FLAGS1(nlmp) & FL1_RT_NOINIFIN) == 0)) {
2930Sstevel@tonic-gate 		/*
2940Sstevel@tonic-gate 		 * Record that this new link map is now bound to the caller.
2950Sstevel@tonic-gate 		 */
2960Sstevel@tonic-gate 		if (bind_one(lmp, nlmp, BND_REFER) == 0)
2971618Srie 			rtldexit(lml, 1);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3008598SRod.Evans@Sun.COM 	if ((lml->lm_tflags | AFLAGS(lmp)) & LML_TFLG_AUD_SYMBIND) {
3010Sstevel@tonic-gate 		uint_t	symndx = (((uintptr_t)nsym -
3024679Srie 		    (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp));
3030Sstevel@tonic-gate 		symval = audit_symbind(lmp, nlmp, nsym, symndx, symval,
3044679Srie 		    &sb_flags);
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	if (!(rtld_flags & RT_FL_NOBIND)) {
3080Sstevel@tonic-gate 		addr = rptr->r_offset;
3090Sstevel@tonic-gate 		if (!(FLAGS(lmp) & FLG_RT_FIXED))
3100Sstevel@tonic-gate 			addr += ADDR(lmp);
3118598SRod.Evans@Sun.COM 		if (((lml->lm_tflags | AFLAGS(lmp)) &
3120Sstevel@tonic-gate 		    (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) &&
3130Sstevel@tonic-gate 		    AUDINFO(lmp)->ai_dynplts) {
3140Sstevel@tonic-gate 			int	fail = 0;
3150Sstevel@tonic-gate 			uint_t	pltndx = reloff / sizeof (Rela);
3160Sstevel@tonic-gate 			uint_t	symndx = (((uintptr_t)nsym -
3174679Srie 			    (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp));
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 			symval = (ulong_t)elf_plt_trace_write(addr, lmp, nlmp,
3200Sstevel@tonic-gate 			    nsym, symndx, pltndx, (caddr_t)symval, sb_flags,
3210Sstevel@tonic-gate 			    &fail);
3220Sstevel@tonic-gate 			if (fail)
3231618Srie 				rtldexit(lml, 1);
3240Sstevel@tonic-gate 		} else {
3250Sstevel@tonic-gate 			/*
3260Sstevel@tonic-gate 			 * Write standard PLT entry to jump directly
3270Sstevel@tonic-gate 			 * to newly bound function.
3280Sstevel@tonic-gate 			 */
3290Sstevel@tonic-gate 			*(ulong_t *)addr = symval;
3300Sstevel@tonic-gate 		}
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	/*
3340Sstevel@tonic-gate 	 * Print binding information and rebuild PLT entry.
3350Sstevel@tonic-gate 	 */
3361618Srie 	DBG_CALL(Dbg_bind_global(lmp, (Addr)from, (Off)(from - ADDR(lmp)),
3378598SRod.Evans@Sun.COM 	    (Xword)(reloff / sizeof (Rela)), PLT_T_FULL, nlmp, (Addr)symval,
3388598SRod.Evans@Sun.COM 	    nsym->st_value, name, binfo));
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	/*
3410Sstevel@tonic-gate 	 * Complete any processing for newly loaded objects.  Note we don't
3420Sstevel@tonic-gate 	 * know exactly where any new objects are loaded (we know the object
3430Sstevel@tonic-gate 	 * that supplied the symbol, but others may have been loaded lazily as
3440Sstevel@tonic-gate 	 * we searched for the symbol), so sorting starts from the last
3450Sstevel@tonic-gate 	 * link-map know on entry to this routine.
3460Sstevel@tonic-gate 	 */
3470Sstevel@tonic-gate 	if (entry)
3484679Srie 		load_completion(llmp);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	/*
3510Sstevel@tonic-gate 	 * Some operations like dldump() or dlopen()'ing a relocatable object
3520Sstevel@tonic-gate 	 * result in objects being loaded on rtld's link-map, make sure these
3530Sstevel@tonic-gate 	 * objects are initialized also.
3540Sstevel@tonic-gate 	 */
3558598SRod.Evans@Sun.COM 	if ((LIST(nlmp)->lm_flags & LML_FLG_RTLDLM) && LIST(nlmp)->lm_init)
3564679Srie 		load_completion(nlmp);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	/*
3590Sstevel@tonic-gate 	 * Make sure the object to which we've bound has had it's .init fired.
3600Sstevel@tonic-gate 	 * Cleanup before return to user code.
3610Sstevel@tonic-gate 	 */
3620Sstevel@tonic-gate 	if (entry) {
3630Sstevel@tonic-gate 		is_dep_init(nlmp, lmp);
3646515Sraf 		leave(lml, 0);
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	if (lmflags & LML_FLG_RTLDLM)
3681618Srie 		dbg_desc->d_class = dbg_class;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	return (symval);
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate /*
3740Sstevel@tonic-gate  * Read and process the relocations for one link object, we assume all
3750Sstevel@tonic-gate  * relocation sections for loadable segments are stored contiguously in
3760Sstevel@tonic-gate  * the file.
3770Sstevel@tonic-gate  */
3780Sstevel@tonic-gate int
elf_reloc(Rt_map * lmp,uint_t plt,int * in_nfavl,APlist ** textrel)3798598SRod.Evans@Sun.COM elf_reloc(Rt_map *lmp, uint_t plt, int *in_nfavl, APlist **textrel)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	ulong_t		relbgn, relend, relsiz, basebgn;
3820Sstevel@tonic-gate 	ulong_t		pltbgn, pltend, _pltbgn, _pltend;
3838598SRod.Evans@Sun.COM 	ulong_t		roffset, rsymndx, psymndx = 0;
3848598SRod.Evans@Sun.COM 	ulong_t		dsymndx;
3850Sstevel@tonic-gate 	uchar_t		rtype;
3860Sstevel@tonic-gate 	long		reladd, value, pvalue;
3870Sstevel@tonic-gate 	Sym		*symref, *psymref, *symdef, *psymdef;
388*12449SRod.Evans@Sun.COM 	Syminfo		*sip;
3890Sstevel@tonic-gate 	char		*name, *pname;
3900Sstevel@tonic-gate 	Rt_map		*_lmp, *plmp;
3918598SRod.Evans@Sun.COM 	int		ret = 1, noplt = 0;
3920Sstevel@tonic-gate 	int		relacount = RELACOUNT(lmp), plthint = 0;
3930Sstevel@tonic-gate 	Rela		*rel;
3940Sstevel@tonic-gate 	uint_t		binfo, pbinfo;
3955892Sab196087 	APlist		*bound = NULL;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/*
3980Sstevel@tonic-gate 	 * Although only necessary for lazy binding, initialize the first
3990Sstevel@tonic-gate 	 * global offset entry to go to elf_rtbndr().  dbx(1) seems
4000Sstevel@tonic-gate 	 * to find this useful.
4010Sstevel@tonic-gate 	 */
4020Sstevel@tonic-gate 	if ((plt == 0) && PLTGOT(lmp)) {
4038598SRod.Evans@Sun.COM 		mmapobj_result_t	*mpp;
4048598SRod.Evans@Sun.COM 
4058598SRod.Evans@Sun.COM 		/*
4068598SRod.Evans@Sun.COM 		 * Make sure the segment is writable.
4078598SRod.Evans@Sun.COM 		 */
4088598SRod.Evans@Sun.COM 		if ((((mpp =
4098598SRod.Evans@Sun.COM 		    find_segment((caddr_t)PLTGOT(lmp), lmp)) != NULL) &&
4108598SRod.Evans@Sun.COM 		    ((mpp->mr_prot & PROT_WRITE) == 0)) &&
4118598SRod.Evans@Sun.COM 		    ((set_prot(lmp, mpp, 1) == 0) ||
4128598SRod.Evans@Sun.COM 		    (aplist_append(textrel, mpp, AL_CNT_TEXTREL) == NULL)))
4138598SRod.Evans@Sun.COM 			return (0);
4148598SRod.Evans@Sun.COM 
4158598SRod.Evans@Sun.COM 		elf_plt_init(PLTGOT(lmp), (caddr_t)lmp);
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	/*
4190Sstevel@tonic-gate 	 * Initialize the plt start and end addresses.
4200Sstevel@tonic-gate 	 */
4210Sstevel@tonic-gate 	if ((pltbgn = (ulong_t)JMPREL(lmp)) != 0)
4220Sstevel@tonic-gate 		pltend = pltbgn + (ulong_t)(PLTRELSZ(lmp));
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	relsiz = (ulong_t)(RELENT(lmp));
4250Sstevel@tonic-gate 	basebgn = ADDR(lmp);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	if (PLTRELSZ(lmp))
4280Sstevel@tonic-gate 		plthint = PLTRELSZ(lmp) / relsiz;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	/*
4310Sstevel@tonic-gate 	 * If we've been called upon to promote an RTLD_LAZY object to an
4320Sstevel@tonic-gate 	 * RTLD_NOW then we're only interested in scaning the .plt table.
4330Sstevel@tonic-gate 	 * An uninitialized .plt is the case where the associated got entry
4340Sstevel@tonic-gate 	 * points back to the plt itself.  Determine the range of the real .plt
4350Sstevel@tonic-gate 	 * entries using the _PROCEDURE_LINKAGE_TABLE_ symbol.
4360Sstevel@tonic-gate 	 */
4370Sstevel@tonic-gate 	if (plt) {
4380Sstevel@tonic-gate 		Slookup	sl;
43911827SRod.Evans@Sun.COM 		Sresult	sr;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 		relbgn = pltbgn;
4420Sstevel@tonic-gate 		relend = pltend;
4430Sstevel@tonic-gate 		if (!relbgn || (relbgn == relend))
4440Sstevel@tonic-gate 			return (1);
4450Sstevel@tonic-gate 
4465950Srie 		/*
44711827SRod.Evans@Sun.COM 		 * Initialize the symbol lookup, and symbol result, data
44811827SRod.Evans@Sun.COM 		 * structures.
4495950Srie 		 */
4505950Srie 		SLOOKUP_INIT(sl, MSG_ORIG(MSG_SYM_PLT), lmp, lmp, ld_entry_cnt,
4515950Srie 		    elf_hash(MSG_ORIG(MSG_SYM_PLT)), 0, 0, 0, LKUP_DEFT);
45211827SRod.Evans@Sun.COM 		SRESULT_INIT(sr, MSG_ORIG(MSG_SYM_PLT));
4530Sstevel@tonic-gate 
45411827SRod.Evans@Sun.COM 		if (elf_find_sym(&sl, &sr, &binfo, NULL) == 0)
4550Sstevel@tonic-gate 			return (1);
4560Sstevel@tonic-gate 
45711827SRod.Evans@Sun.COM 		symdef = sr.sr_sym;
4580Sstevel@tonic-gate 		_pltbgn = symdef->st_value;
4590Sstevel@tonic-gate 		if (!(FLAGS(lmp) & FLG_RT_FIXED) &&
4600Sstevel@tonic-gate 		    (symdef->st_shndx != SHN_ABS))
4610Sstevel@tonic-gate 			_pltbgn += basebgn;
4620Sstevel@tonic-gate 		_pltend = _pltbgn + (((PLTRELSZ(lmp) / relsiz)) *
4634679Srie 		    M_PLT_ENTSIZE) + M_PLT_RESERVSZ;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	} else {
4660Sstevel@tonic-gate 		/*
4670Sstevel@tonic-gate 		 * The relocation sections appear to the run-time linker as a
4680Sstevel@tonic-gate 		 * single table.  Determine the address of the beginning and end
4690Sstevel@tonic-gate 		 * of this table.  There are two different interpretations of
4700Sstevel@tonic-gate 		 * the ABI at this point:
4710Sstevel@tonic-gate 		 *
4720Sstevel@tonic-gate 		 *   o	The REL table and its associated RELSZ indicate the
4730Sstevel@tonic-gate 		 *	concatenation of *all* relocation sections (this is the
4740Sstevel@tonic-gate 		 *	model our link-editor constructs).
4750Sstevel@tonic-gate 		 *
4760Sstevel@tonic-gate 		 *   o	The REL table and its associated RELSZ indicate the
4770Sstevel@tonic-gate 		 *	concatenation of all *but* the .plt relocations.  These
4780Sstevel@tonic-gate 		 *	relocations are specified individually by the JMPREL and
4790Sstevel@tonic-gate 		 *	PLTRELSZ entries.
4800Sstevel@tonic-gate 		 *
4810Sstevel@tonic-gate 		 * Determine from our knowledege of the relocation range and
4820Sstevel@tonic-gate 		 * .plt range, the range of the total relocation table.  Note
4830Sstevel@tonic-gate 		 * that one other ABI assumption seems to be that the .plt
4840Sstevel@tonic-gate 		 * relocations always follow any other relocations, the
4850Sstevel@tonic-gate 		 * following range checking drops that assumption.
4860Sstevel@tonic-gate 		 */
4870Sstevel@tonic-gate 		relbgn = (ulong_t)(REL(lmp));
4880Sstevel@tonic-gate 		relend = relbgn + (ulong_t)(RELSZ(lmp));
4890Sstevel@tonic-gate 		if (pltbgn) {
4900Sstevel@tonic-gate 			if (!relbgn || (relbgn > pltbgn))
4910Sstevel@tonic-gate 				relbgn = pltbgn;
4920Sstevel@tonic-gate 			if (!relbgn || (relend < pltend))
4930Sstevel@tonic-gate 				relend = pltend;
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 	if (!relbgn || (relbgn == relend)) {
4971618Srie 		DBG_CALL(Dbg_reloc_run(lmp, 0, plt, DBG_REL_NONE));
4980Sstevel@tonic-gate 		return (1);
4990Sstevel@tonic-gate 	}
5001618Srie 	DBG_CALL(Dbg_reloc_run(lmp, M_REL_SHT_TYPE, plt, DBG_REL_START));
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	/*
5030Sstevel@tonic-gate 	 * If we're processing a dynamic executable in lazy mode there is no
5040Sstevel@tonic-gate 	 * need to scan the .rel.plt table, however if we're processing a shared
5050Sstevel@tonic-gate 	 * object in lazy mode the .got addresses associated to each .plt must
5060Sstevel@tonic-gate 	 * be relocated to reflect the location of the shared object.
5070Sstevel@tonic-gate 	 */
5080Sstevel@tonic-gate 	if (pltbgn && ((MODE(lmp) & RTLD_NOW) == 0) &&
5090Sstevel@tonic-gate 	    (FLAGS(lmp) & FLG_RT_FIXED))
5100Sstevel@tonic-gate 		noplt = 1;
5110Sstevel@tonic-gate 
512*12449SRod.Evans@Sun.COM 	sip = SYMINFO(lmp);
5130Sstevel@tonic-gate 	/*
5140Sstevel@tonic-gate 	 * Loop through relocations.
5150Sstevel@tonic-gate 	 */
5160Sstevel@tonic-gate 	while (relbgn < relend) {
5178598SRod.Evans@Sun.COM 		mmapobj_result_t	*mpp;
5188598SRod.Evans@Sun.COM 		uint_t			sb_flags = 0;
5190Sstevel@tonic-gate 
5206206Sab196087 		rtype = ELF_R_TYPE(((Rela *)relbgn)->r_info, M_MACH);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 		/*
5230Sstevel@tonic-gate 		 * If this is a RELATIVE relocation in a shared object (the
5240Sstevel@tonic-gate 		 * common case), and if we are not debugging, then jump into a
5258598SRod.Evans@Sun.COM 		 * tighter relocation loop (elf_reloc_relative).
5260Sstevel@tonic-gate 		 */
5270Sstevel@tonic-gate 		if ((rtype == R_AMD64_RELATIVE) &&
5281618Srie 		    ((FLAGS(lmp) & FLG_RT_FIXED) == 0) && (DBG_ENABLED == 0)) {
5290Sstevel@tonic-gate 			if (relacount) {
5308598SRod.Evans@Sun.COM 				relbgn = elf_reloc_relative_count(relbgn,
531*12449SRod.Evans@Sun.COM 				    relacount, relsiz, basebgn, lmp,
532*12449SRod.Evans@Sun.COM 				    textrel, 0);
5330Sstevel@tonic-gate 				relacount = 0;
5340Sstevel@tonic-gate 			} else {
5350Sstevel@tonic-gate 				relbgn = elf_reloc_relative(relbgn, relend,
536*12449SRod.Evans@Sun.COM 				    relsiz, basebgn, lmp, textrel, 0);
5370Sstevel@tonic-gate 			}
5380Sstevel@tonic-gate 			if (relbgn >= relend)
5390Sstevel@tonic-gate 				break;
5406206Sab196087 			rtype = ELF_R_TYPE(((Rela *)relbgn)->r_info, M_MACH);
5410Sstevel@tonic-gate 		}
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 		roffset = ((Rela *)relbgn)->r_offset;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 		/*
5460Sstevel@tonic-gate 		 * If this is a shared object, add the base address to offset.
5470Sstevel@tonic-gate 		 */
5480Sstevel@tonic-gate 		if (!(FLAGS(lmp) & FLG_RT_FIXED)) {
5490Sstevel@tonic-gate 			/*
5500Sstevel@tonic-gate 			 * If we're processing lazy bindings, we have to step
5510Sstevel@tonic-gate 			 * through the plt entries and add the base address
5520Sstevel@tonic-gate 			 * to the corresponding got entry.
5530Sstevel@tonic-gate 			 */
5540Sstevel@tonic-gate 			if (plthint && (plt == 0) &&
5550Sstevel@tonic-gate 			    (rtype == R_AMD64_JUMP_SLOT) &&
5560Sstevel@tonic-gate 			    ((MODE(lmp) & RTLD_NOW) == 0)) {
557*12449SRod.Evans@Sun.COM 				relbgn = elf_reloc_relative_count(relbgn,
558*12449SRod.Evans@Sun.COM 				    plthint, relsiz, basebgn, lmp, textrel, 1);
559*12449SRod.Evans@Sun.COM 				plthint = 0;
5600Sstevel@tonic-gate 				continue;
5610Sstevel@tonic-gate 			}
5620Sstevel@tonic-gate 			roffset += basebgn;
5630Sstevel@tonic-gate 		}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 		reladd = (long)(((Rela *)relbgn)->r_addend);
5660Sstevel@tonic-gate 		rsymndx = ELF_R_SYM(((Rela *)relbgn)->r_info);
5670Sstevel@tonic-gate 		rel = (Rela *)relbgn;
5680Sstevel@tonic-gate 		relbgn += relsiz;
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 		/*
5710Sstevel@tonic-gate 		 * Optimizations.
5720Sstevel@tonic-gate 		 */
5730Sstevel@tonic-gate 		if (rtype == R_AMD64_NONE)
5740Sstevel@tonic-gate 			continue;
5750Sstevel@tonic-gate 		if (noplt && ((ulong_t)rel >= pltbgn) &&
5760Sstevel@tonic-gate 		    ((ulong_t)rel < pltend)) {
5770Sstevel@tonic-gate 			relbgn = pltend;
5780Sstevel@tonic-gate 			continue;
5790Sstevel@tonic-gate 		}
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 		/*
5828598SRod.Evans@Sun.COM 		 * If we're promoting plts, determine if this one has already
5838598SRod.Evans@Sun.COM 		 * been written.
5848598SRod.Evans@Sun.COM 		 */
5858598SRod.Evans@Sun.COM 		if (plt && ((*(ulong_t *)roffset < _pltbgn) ||
5868598SRod.Evans@Sun.COM 		    (*(ulong_t *)roffset > _pltend)))
5878598SRod.Evans@Sun.COM 			continue;
5888598SRod.Evans@Sun.COM 
5898598SRod.Evans@Sun.COM 		/*
5900Sstevel@tonic-gate 		 * If this relocation is not against part of the image
5910Sstevel@tonic-gate 		 * mapped into memory we skip it.
5920Sstevel@tonic-gate 		 */
5938598SRod.Evans@Sun.COM 		if ((mpp = find_segment((caddr_t)roffset, lmp)) == NULL) {
5940Sstevel@tonic-gate 			elf_reloc_bad(lmp, (void *)rel, rtype, roffset,
5950Sstevel@tonic-gate 			    rsymndx);
5960Sstevel@tonic-gate 			continue;
5970Sstevel@tonic-gate 		}
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 		binfo = 0;
6000Sstevel@tonic-gate 		/*
6010Sstevel@tonic-gate 		 * If a symbol index is specified then get the symbol table
6020Sstevel@tonic-gate 		 * entry, locate the symbol definition, and determine its
6030Sstevel@tonic-gate 		 * address.
6040Sstevel@tonic-gate 		 */
6050Sstevel@tonic-gate 		if (rsymndx) {
6060Sstevel@tonic-gate 			/*
607*12449SRod.Evans@Sun.COM 			 * If a Syminfo section is provided, determine if this
608*12449SRod.Evans@Sun.COM 			 * symbol is deferred, and if so, skip this relocation.
609*12449SRod.Evans@Sun.COM 			 */
610*12449SRod.Evans@Sun.COM 			if (sip && is_sym_deferred((ulong_t)rel, basebgn, lmp,
611*12449SRod.Evans@Sun.COM 			    textrel, sip, rsymndx))
612*12449SRod.Evans@Sun.COM 				continue;
613*12449SRod.Evans@Sun.COM 
614*12449SRod.Evans@Sun.COM 			/*
6150Sstevel@tonic-gate 			 * Get the local symbol table entry.
6160Sstevel@tonic-gate 			 */
6170Sstevel@tonic-gate 			symref = (Sym *)((ulong_t)SYMTAB(lmp) +
6184679Srie 			    (rsymndx * SYMENT(lmp)));
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 			/*
6210Sstevel@tonic-gate 			 * If this is a local symbol, just use the base address.
6220Sstevel@tonic-gate 			 * (we should have no local relocations in the
6230Sstevel@tonic-gate 			 * executable).
6240Sstevel@tonic-gate 			 */
6250Sstevel@tonic-gate 			if (ELF_ST_BIND(symref->st_info) == STB_LOCAL) {
6260Sstevel@tonic-gate 				value = basebgn;
62711827SRod.Evans@Sun.COM 				name = NULL;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 				/*
6302145Srie 				 * Special case TLS relocations.
6310Sstevel@tonic-gate 				 */
6322145Srie 				if (rtype == R_AMD64_DTPMOD64) {
6332145Srie 					/*
6342145Srie 					 * Use the TLS modid.
6352145Srie 					 */
6360Sstevel@tonic-gate 					value = TLSMODID(lmp);
6372145Srie 
6382145Srie 				} else if ((rtype == R_AMD64_TPOFF64) ||
6392145Srie 				    (rtype == R_AMD64_TPOFF32)) {
6402145Srie 					if ((value = elf_static_tls(lmp, symref,
6412145Srie 					    rel, rtype, 0, roffset, 0)) == 0) {
6422145Srie 						ret = 0;
6432145Srie 						break;
6442145Srie 					}
6452145Srie 				}
6460Sstevel@tonic-gate 			} else {
6470Sstevel@tonic-gate 				/*
6480Sstevel@tonic-gate 				 * If the symbol index is equal to the previous
6490Sstevel@tonic-gate 				 * symbol index relocation we processed then
6500Sstevel@tonic-gate 				 * reuse the previous values. (Note that there
6510Sstevel@tonic-gate 				 * have been cases where a relocation exists
6520Sstevel@tonic-gate 				 * against a copy relocation symbol, our ld(1)
6530Sstevel@tonic-gate 				 * should optimize this away, but make sure we
6540Sstevel@tonic-gate 				 * don't use the same symbol information should
6550Sstevel@tonic-gate 				 * this case exist).
6560Sstevel@tonic-gate 				 */
6570Sstevel@tonic-gate 				if ((rsymndx == psymndx) &&
6580Sstevel@tonic-gate 				    (rtype != R_AMD64_COPY)) {
6590Sstevel@tonic-gate 					/* LINTED */
6600Sstevel@tonic-gate 					if (psymdef == 0) {
6611618Srie 						DBG_CALL(Dbg_bind_weak(lmp,
6621618Srie 						    (Addr)roffset, (Addr)
6630Sstevel@tonic-gate 						    (roffset - basebgn), name));
6640Sstevel@tonic-gate 						continue;
6650Sstevel@tonic-gate 					}
6660Sstevel@tonic-gate 					/* LINTED */
6670Sstevel@tonic-gate 					value = pvalue;
6680Sstevel@tonic-gate 					/* LINTED */
6690Sstevel@tonic-gate 					name = pname;
6700Sstevel@tonic-gate 					/* LINTED */
6710Sstevel@tonic-gate 					symdef = psymdef;
6720Sstevel@tonic-gate 					/* LINTED */
6730Sstevel@tonic-gate 					symref = psymref;
6740Sstevel@tonic-gate 					/* LINTED */
6750Sstevel@tonic-gate 					_lmp = plmp;
6760Sstevel@tonic-gate 					/* LINTED */
6770Sstevel@tonic-gate 					binfo = pbinfo;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 					if ((LIST(_lmp)->lm_tflags |
6808598SRod.Evans@Sun.COM 					    AFLAGS(_lmp)) &
6810Sstevel@tonic-gate 					    LML_TFLG_AUD_SYMBIND) {
6820Sstevel@tonic-gate 						value = audit_symbind(lmp, _lmp,
6830Sstevel@tonic-gate 						    /* LINTED */
6840Sstevel@tonic-gate 						    symdef, dsymndx, value,
6850Sstevel@tonic-gate 						    &sb_flags);
6860Sstevel@tonic-gate 					}
6870Sstevel@tonic-gate 				} else {
6880Sstevel@tonic-gate 					Slookup		sl;
68911827SRod.Evans@Sun.COM 					Sresult		sr;
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 					/*
6920Sstevel@tonic-gate 					 * Lookup the symbol definition.
69311827SRod.Evans@Sun.COM 					 * Initialize the symbol lookup, and
69411827SRod.Evans@Sun.COM 					 * symbol result, data structure.
6950Sstevel@tonic-gate 					 */
6960Sstevel@tonic-gate 					name = (char *)(STRTAB(lmp) +
6970Sstevel@tonic-gate 					    symref->st_name);
6980Sstevel@tonic-gate 
6995950Srie 					SLOOKUP_INIT(sl, name, lmp, 0,
7005950Srie 					    ld_entry_cnt, 0, rsymndx, symref,
7015950Srie 					    rtype, LKUP_STDRELOC);
70211827SRod.Evans@Sun.COM 					SRESULT_INIT(sr, name);
70311827SRod.Evans@Sun.COM 					symdef = NULL;
7040Sstevel@tonic-gate 
70511827SRod.Evans@Sun.COM 					if (lookup_sym(&sl, &sr, &binfo,
70611827SRod.Evans@Sun.COM 					    in_nfavl)) {
70711827SRod.Evans@Sun.COM 						name = (char *)sr.sr_name;
70811827SRod.Evans@Sun.COM 						_lmp = sr.sr_dmap;
70911827SRod.Evans@Sun.COM 						symdef = sr.sr_sym;
71011827SRod.Evans@Sun.COM 					}
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 					/*
7130Sstevel@tonic-gate 					 * If the symbol is not found and the
7140Sstevel@tonic-gate 					 * reference was not to a weak symbol,
7150Sstevel@tonic-gate 					 * report an error.  Weak references
7160Sstevel@tonic-gate 					 * may be unresolved.
7170Sstevel@tonic-gate 					 */
7184679Srie 					/* BEGIN CSTYLED */
7190Sstevel@tonic-gate 					if (symdef == 0) {
7206150Srie 					    if (sl.sl_bind != STB_WEAK) {
7216150Srie 						if (elf_reloc_error(lmp, name,
7226150Srie 						    rel, binfo))
7236150Srie 							continue;
7241618Srie 
7258598SRod.Evans@Sun.COM 						ret = 0;
7268598SRod.Evans@Sun.COM 						break;
7276150Srie 
7280Sstevel@tonic-gate 					    } else {
7290Sstevel@tonic-gate 						psymndx = rsymndx;
7300Sstevel@tonic-gate 						psymdef = 0;
7310Sstevel@tonic-gate 
7321618Srie 						DBG_CALL(Dbg_bind_weak(lmp,
7331618Srie 						    (Addr)roffset, (Addr)
7340Sstevel@tonic-gate 						    (roffset - basebgn), name));
7350Sstevel@tonic-gate 						continue;
7360Sstevel@tonic-gate 					    }
7370Sstevel@tonic-gate 					}
7384679Srie 					/* END CSTYLED */
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 					/*
7410Sstevel@tonic-gate 					 * If symbol was found in an object
7420Sstevel@tonic-gate 					 * other than the referencing object
7430Sstevel@tonic-gate 					 * then record the binding.
7440Sstevel@tonic-gate 					 */
7450Sstevel@tonic-gate 					if ((lmp != _lmp) && ((FLAGS1(_lmp) &
7460Sstevel@tonic-gate 					    FL1_RT_NOINIFIN) == 0)) {
7475892Sab196087 						if (aplist_test(&bound, _lmp,
7480Sstevel@tonic-gate 						    AL_CNT_RELBIND) == 0) {
7490Sstevel@tonic-gate 							ret = 0;
7500Sstevel@tonic-gate 							break;
7510Sstevel@tonic-gate 						}
7520Sstevel@tonic-gate 					}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 					/*
7550Sstevel@tonic-gate 					 * Calculate the location of definition;
7560Sstevel@tonic-gate 					 * symbol value plus base address of
7570Sstevel@tonic-gate 					 * containing shared object.
7580Sstevel@tonic-gate 					 */
7592850Srie 					if (IS_SIZE(rtype))
7602850Srie 						value = symdef->st_size;
7612850Srie 					else
7622850Srie 						value = symdef->st_value;
7632850Srie 
7640Sstevel@tonic-gate 					if (!(FLAGS(_lmp) & FLG_RT_FIXED) &&
7652850Srie 					    !(IS_SIZE(rtype)) &&
7660Sstevel@tonic-gate 					    (symdef->st_shndx != SHN_ABS) &&
7670Sstevel@tonic-gate 					    (ELF_ST_TYPE(symdef->st_info) !=
7680Sstevel@tonic-gate 					    STT_TLS))
7690Sstevel@tonic-gate 						value += ADDR(_lmp);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 					/*
7720Sstevel@tonic-gate 					 * Retain this symbol index and the
7730Sstevel@tonic-gate 					 * value in case it can be used for the
7740Sstevel@tonic-gate 					 * subsequent relocations.
7750Sstevel@tonic-gate 					 */
7760Sstevel@tonic-gate 					if (rtype != R_AMD64_COPY) {
7770Sstevel@tonic-gate 						psymndx = rsymndx;
7780Sstevel@tonic-gate 						pvalue = value;
7790Sstevel@tonic-gate 						pname = name;
7800Sstevel@tonic-gate 						psymdef = symdef;
7810Sstevel@tonic-gate 						psymref = symref;
7820Sstevel@tonic-gate 						plmp = _lmp;
7830Sstevel@tonic-gate 						pbinfo = binfo;
7840Sstevel@tonic-gate 					}
7850Sstevel@tonic-gate 					if ((LIST(_lmp)->lm_tflags |
7868598SRod.Evans@Sun.COM 					    AFLAGS(_lmp)) &
7870Sstevel@tonic-gate 					    LML_TFLG_AUD_SYMBIND) {
7880Sstevel@tonic-gate 						dsymndx = (((uintptr_t)symdef -
7890Sstevel@tonic-gate 						    (uintptr_t)SYMTAB(_lmp)) /
7900Sstevel@tonic-gate 						    SYMENT(_lmp));
7910Sstevel@tonic-gate 						value = audit_symbind(lmp, _lmp,
7920Sstevel@tonic-gate 						    symdef, dsymndx, value,
7930Sstevel@tonic-gate 						    &sb_flags);
7940Sstevel@tonic-gate 					}
7950Sstevel@tonic-gate 				}
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 				/*
7980Sstevel@tonic-gate 				 * If relocation is PC-relative, subtract
7990Sstevel@tonic-gate 				 * offset address.
8000Sstevel@tonic-gate 				 */
8010Sstevel@tonic-gate 				if (IS_PC_RELATIVE(rtype))
8020Sstevel@tonic-gate 					value -= roffset;
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 				/*
8052145Srie 				 * Special case TLS relocations.
8060Sstevel@tonic-gate 				 */
8072145Srie 				if (rtype == R_AMD64_DTPMOD64) {
8082145Srie 					/*
8092145Srie 					 * Relocation value is the TLS modid.
8102145Srie 					 */
8110Sstevel@tonic-gate 					value = TLSMODID(_lmp);
8122145Srie 
8132145Srie 				} else if ((rtype == R_AMD64_TPOFF64) ||
8142145Srie 				    (rtype == R_AMD64_TPOFF32)) {
8152145Srie 					if ((value = elf_static_tls(_lmp,
8162145Srie 					    symdef, rel, rtype, name, roffset,
8172145Srie 					    value)) == 0) {
8182145Srie 						ret = 0;
8192145Srie 						break;
8202145Srie 					}
8212145Srie 				}
8220Sstevel@tonic-gate 			}
8230Sstevel@tonic-gate 		} else {
8240Sstevel@tonic-gate 			/*
8252145Srie 			 * Special cases.
8260Sstevel@tonic-gate 			 */
8272145Srie 			if (rtype == R_AMD64_DTPMOD64) {
8282145Srie 				/*
8292145Srie 				 * TLS relocation value is the TLS modid.
8302145Srie 				 */
8310Sstevel@tonic-gate 				value = TLSMODID(lmp);
8322145Srie 			} else
8330Sstevel@tonic-gate 				value = basebgn;
83411827SRod.Evans@Sun.COM 
83511827SRod.Evans@Sun.COM 			name = NULL;
8360Sstevel@tonic-gate 		}
8370Sstevel@tonic-gate 
8382145Srie 		DBG_CALL(Dbg_reloc_in(LIST(lmp), ELF_DBG_RTLD, M_MACH,
8399406SAli.Bahrami@Sun.COM 		    M_REL_SHT_TYPE, rel, NULL, 0, name));
8402145Srie 
8410Sstevel@tonic-gate 		/*
8428598SRod.Evans@Sun.COM 		 * Make sure the segment is writable.
8430Sstevel@tonic-gate 		 */
8448598SRod.Evans@Sun.COM 		if (((mpp->mr_prot & PROT_WRITE) == 0) &&
8458598SRod.Evans@Sun.COM 		    ((set_prot(lmp, mpp, 1) == 0) ||
8468598SRod.Evans@Sun.COM 		    (aplist_append(textrel, mpp, AL_CNT_TEXTREL) == NULL))) {
8478598SRod.Evans@Sun.COM 			ret = 0;
8488598SRod.Evans@Sun.COM 			break;
8490Sstevel@tonic-gate 		}
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 		/*
8520Sstevel@tonic-gate 		 * Call relocation routine to perform required relocation.
8530Sstevel@tonic-gate 		 */
8540Sstevel@tonic-gate 		switch (rtype) {
8550Sstevel@tonic-gate 		case R_AMD64_COPY:
8560Sstevel@tonic-gate 			if (elf_copy_reloc(name, symref, lmp, (void *)roffset,
8570Sstevel@tonic-gate 			    symdef, _lmp, (const void *)value) == 0)
8580Sstevel@tonic-gate 				ret = 0;
8590Sstevel@tonic-gate 			break;
8600Sstevel@tonic-gate 		case R_AMD64_JUMP_SLOT:
8618598SRod.Evans@Sun.COM 			if (((LIST(lmp)->lm_tflags | AFLAGS(lmp)) &
8620Sstevel@tonic-gate 			    (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) &&
8630Sstevel@tonic-gate 			    AUDINFO(lmp)->ai_dynplts) {
8640Sstevel@tonic-gate 				int	fail = 0;
8650Sstevel@tonic-gate 				int	pltndx = (((ulong_t)rel -
8664679Srie 				    (uintptr_t)JMPREL(lmp)) / relsiz);
8670Sstevel@tonic-gate 				int	symndx = (((uintptr_t)symdef -
8684679Srie 				    (uintptr_t)SYMTAB(_lmp)) / SYMENT(_lmp));
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 				(void) elf_plt_trace_write(roffset, lmp, _lmp,
8710Sstevel@tonic-gate 				    symdef, symndx, pltndx, (caddr_t)value,
8720Sstevel@tonic-gate 				    sb_flags, &fail);
8730Sstevel@tonic-gate 				if (fail)
8740Sstevel@tonic-gate 					ret = 0;
8750Sstevel@tonic-gate 			} else {
8760Sstevel@tonic-gate 				/*
8770Sstevel@tonic-gate 				 * Write standard PLT entry to jump directly
8780Sstevel@tonic-gate 				 * to newly bound function.
8790Sstevel@tonic-gate 				 */
8801618Srie 				DBG_CALL(Dbg_reloc_apply_val(LIST(lmp),
8811618Srie 				    ELF_DBG_RTLD, (Xword)roffset,
8820Sstevel@tonic-gate 				    (Xword)value));
8830Sstevel@tonic-gate 				*(ulong_t *)roffset = value;
8840Sstevel@tonic-gate 			}
8850Sstevel@tonic-gate 			break;
8860Sstevel@tonic-gate 		default:
8870Sstevel@tonic-gate 			value += reladd;
8880Sstevel@tonic-gate 			/*
8890Sstevel@tonic-gate 			 * Write the relocation out.
8900Sstevel@tonic-gate 			 */
8915189Sab196087 			if (do_reloc_rtld(rtype, (uchar_t *)roffset,
8921618Srie 			    (Xword *)&value, name, NAME(lmp), LIST(lmp)) == 0)
8930Sstevel@tonic-gate 				ret = 0;
8940Sstevel@tonic-gate 
8951618Srie 			DBG_CALL(Dbg_reloc_apply_val(LIST(lmp), ELF_DBG_RTLD,
8961618Srie 			    (Xword)roffset, (Xword)value));
8970Sstevel@tonic-gate 		}
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 		if ((ret == 0) &&
9000Sstevel@tonic-gate 		    ((LIST(lmp)->lm_flags & LML_FLG_TRC_WARN) == 0))
9010Sstevel@tonic-gate 			break;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 		if (binfo) {
9041618Srie 			DBG_CALL(Dbg_bind_global(lmp, (Addr)roffset,
9051618Srie 			    (Off)(roffset - basebgn), (Xword)(-1), PLT_T_FULL,
9061618Srie 			    _lmp, (Addr)value, symdef->st_value, name, binfo));
9070Sstevel@tonic-gate 		}
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 
9108598SRod.Evans@Sun.COM 	return (relocate_finish(lmp, bound, ret));
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate /*
9140Sstevel@tonic-gate  * Initialize the first few got entries so that function calls go to
9150Sstevel@tonic-gate  * elf_rtbndr:
9160Sstevel@tonic-gate  *
9170Sstevel@tonic-gate  *	GOT[GOT_XLINKMAP] =	the address of the link map
9180Sstevel@tonic-gate  *	GOT[GOT_XRTLD] =	the address of rtbinder
9190Sstevel@tonic-gate  */
9200Sstevel@tonic-gate void
elf_plt_init(void * got,caddr_t l)9210Sstevel@tonic-gate elf_plt_init(void *got, caddr_t l)
9220Sstevel@tonic-gate {
9230Sstevel@tonic-gate 	uint64_t	*_got;
9240Sstevel@tonic-gate 	/* LINTED */
9250Sstevel@tonic-gate 	Rt_map		*lmp = (Rt_map *)l;
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	_got = (uint64_t *)got + M_GOT_XLINKMAP;
9280Sstevel@tonic-gate 	*_got = (uint64_t)lmp;
9290Sstevel@tonic-gate 	_got = (uint64_t *)got + M_GOT_XRTLD;
9300Sstevel@tonic-gate 	*_got = (uint64_t)elf_rtbndr;
9310Sstevel@tonic-gate }
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate /*
9340Sstevel@tonic-gate  * Plt writing interface to allow debugging initialization to be generic.
9350Sstevel@tonic-gate  */
9360Sstevel@tonic-gate Pltbindtype
9370Sstevel@tonic-gate /* ARGSUSED1 */
elf_plt_write(uintptr_t addr,uintptr_t vaddr,void * rptr,uintptr_t symval,Xword pltndx)9380Sstevel@tonic-gate elf_plt_write(uintptr_t addr, uintptr_t vaddr, void *rptr, uintptr_t symval,
9390Sstevel@tonic-gate 	Xword pltndx)
9400Sstevel@tonic-gate {
9410Sstevel@tonic-gate 	Rela		*rel = (Rela*)rptr;
9420Sstevel@tonic-gate 	uintptr_t	pltaddr;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	pltaddr = addr + rel->r_offset;
9450Sstevel@tonic-gate 	*(ulong_t *)pltaddr = (ulong_t)symval + rel->r_addend;
9460Sstevel@tonic-gate 	DBG_CALL(pltcntfull++);
9470Sstevel@tonic-gate 	return (PLT_T_FULL);
9480Sstevel@tonic-gate }
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate /*
9510Sstevel@tonic-gate  * Provide a machine specific interface to the conversion routine.  By calling
9520Sstevel@tonic-gate  * the machine specific version, rather than the generic version, we insure that
9530Sstevel@tonic-gate  * the data tables/strings for all known machine versions aren't dragged into
9540Sstevel@tonic-gate  * ld.so.1.
9550Sstevel@tonic-gate  */
9560Sstevel@tonic-gate const char *
_conv_reloc_type(uint_t rel)9571618Srie _conv_reloc_type(uint_t rel)
9580Sstevel@tonic-gate {
9598598SRod.Evans@Sun.COM 	static Conv_inv_buf_t	inv_buf;
9604734Sab196087 
9614734Sab196087 	return (conv_reloc_amd64_type(rel, 0, &inv_buf));
9620Sstevel@tonic-gate }
963