xref: /onnv-gate/usr/src/cmd/sgs/libld/common/machrel.intel.c (revision 13074:787bf65954d0)
11618Srie /*
21618Srie  * CDDL HEADER START
31618Srie  *
41618Srie  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
71618Srie  *
81618Srie  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91618Srie  * or http://www.opensolaris.org/os/licensing.
101618Srie  * See the License for the specific language governing permissions
111618Srie  * and limitations under the License.
121618Srie  *
131618Srie  * When distributing Covered Code, include this CDDL HEADER in each
141618Srie  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151618Srie  * If applicable, add the following below this CDDL HEADER, with the
161618Srie  * fields enclosed by brackets "[]" replaced with your own identifying
171618Srie  * information: Portions Copyright [yyyy] [name of copyright owner]
181618Srie  *
191618Srie  * CDDL HEADER END
201618Srie  */
211618Srie 
221618Srie /*
231618Srie  *	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.
241618Srie  *	Copyright (c) 1988 AT&T
251618Srie  *	  All Rights Reserved
261618Srie  *
2712155SAli.Bahrami@Sun.COM  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
281618Srie  */
291618Srie 
306206Sab196087 /* Get the x86 version of the relocation engine */
316206Sab196087 #define	DO_RELOC_LIBLD_X86
326206Sab196087 
331618Srie #include	<string.h>
341618Srie #include	<stdio.h>
351618Srie #include	<sys/elf_386.h>
361618Srie #include	<debug.h>
371618Srie #include	<reloc.h>
386206Sab196087 #include	<i386/machdep_x86.h>
391618Srie #include	"msg.h"
401618Srie #include	"_libld.h"
411618Srie 
429131SRod.Evans@Sun.COM /*
439131SRod.Evans@Sun.COM  * Search the GOT index list for a GOT entry with a matching reference.
449131SRod.Evans@Sun.COM  */
459131SRod.Evans@Sun.COM /* ARGSUSED3 */
469131SRod.Evans@Sun.COM static Gotndx *
ld_find_got_ndx(Alist * alp,Gotref gref,Ofl_desc * ofl,Rel_desc * rdesc)479131SRod.Evans@Sun.COM ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc)
489131SRod.Evans@Sun.COM {
499131SRod.Evans@Sun.COM 	Aliste	idx;
509131SRod.Evans@Sun.COM 	Gotndx	*gnp;
519131SRod.Evans@Sun.COM 
529131SRod.Evans@Sun.COM 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
539131SRod.Evans@Sun.COM 		return (ofl->ofl_tlsldgotndx);
549131SRod.Evans@Sun.COM 
559131SRod.Evans@Sun.COM 	for (ALIST_TRAVERSE(alp, idx, gnp)) {
569131SRod.Evans@Sun.COM 		if (gnp->gn_gotref == gref)
579131SRod.Evans@Sun.COM 			return (gnp);
589131SRod.Evans@Sun.COM 	}
599131SRod.Evans@Sun.COM 	return (NULL);
609131SRod.Evans@Sun.COM }
616206Sab196087 
629131SRod.Evans@Sun.COM static Xword
ld_calc_got_offset(Rel_desc * rdesc,Ofl_desc * ofl)639131SRod.Evans@Sun.COM ld_calc_got_offset(Rel_desc *rdesc, Ofl_desc *ofl)
649131SRod.Evans@Sun.COM {
659131SRod.Evans@Sun.COM 	Os_desc		*osp = ofl->ofl_osgot;
669131SRod.Evans@Sun.COM 	Sym_desc	*sdp = rdesc->rel_sym;
679131SRod.Evans@Sun.COM 	Xword		gotndx;
689131SRod.Evans@Sun.COM 	Gotref		gref;
699131SRod.Evans@Sun.COM 	Gotndx		*gnp;
709131SRod.Evans@Sun.COM 
719131SRod.Evans@Sun.COM 	if (rdesc->rel_flags & FLG_REL_DTLS)
729131SRod.Evans@Sun.COM 		gref = GOT_REF_TLSGD;
739131SRod.Evans@Sun.COM 	else if (rdesc->rel_flags & FLG_REL_MTLS)
749131SRod.Evans@Sun.COM 		gref = GOT_REF_TLSLD;
759131SRod.Evans@Sun.COM 	else if (rdesc->rel_flags & FLG_REL_STLS)
769131SRod.Evans@Sun.COM 		gref = GOT_REF_TLSIE;
779131SRod.Evans@Sun.COM 	else
789131SRod.Evans@Sun.COM 		gref = GOT_REF_GENERIC;
799131SRod.Evans@Sun.COM 
809131SRod.Evans@Sun.COM 	gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
819131SRod.Evans@Sun.COM 	assert(gnp);
829131SRod.Evans@Sun.COM 
839131SRod.Evans@Sun.COM 	gotndx = (Xword)gnp->gn_gotndx;
849131SRod.Evans@Sun.COM 
859131SRod.Evans@Sun.COM 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
869131SRod.Evans@Sun.COM 	    (rdesc->rel_rtype == R_386_TLS_DTPOFF32))
879131SRod.Evans@Sun.COM 		gotndx++;
889131SRod.Evans@Sun.COM 
899131SRod.Evans@Sun.COM 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
909131SRod.Evans@Sun.COM }
916206Sab196087 
926206Sab196087 static Word
ld_init_rel(Rel_desc * reld,Word * typedata,void * reloc)9312155SAli.Bahrami@Sun.COM ld_init_rel(Rel_desc *reld, Word *typedata, void *reloc)
941618Srie {
959131SRod.Evans@Sun.COM 	Rel	*rel = (Rel *)reloc;
961618Srie 
971618Srie 	/* LINTED */
986206Sab196087 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
991618Srie 	reld->rel_roffset = rel->r_offset;
1001618Srie 	reld->rel_raddend = 0;
10112155SAli.Bahrami@Sun.COM 	*typedata = 0;
1021618Srie 
1031618Srie 	return ((Word)ELF_R_SYM(rel->r_info));
1041618Srie }
1051618Srie 
1066206Sab196087 static void
ld_mach_eflags(Ehdr * ehdr,Ofl_desc * ofl)1071618Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
1081618Srie {
1091618Srie 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
1101618Srie }
1111618Srie 
1126206Sab196087 static void
ld_mach_make_dynamic(Ofl_desc * ofl,size_t * cnt)1131618Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
1141618Srie {
1151618Srie 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
1161618Srie 		/*
1171618Srie 		 * Create this entry if we are going to create a PLT table.
1181618Srie 		 */
1191618Srie 		if (ofl->ofl_pltcnt)
1201618Srie 			(*cnt)++;		/* DT_PLTGOT */
1211618Srie 	}
1221618Srie }
1231618Srie 
1246206Sab196087 static void
ld_mach_update_odynamic(Ofl_desc * ofl,Dyn ** dyn)1252145Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
1261618Srie {
1271618Srie 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
1281618Srie 		(*dyn)->d_tag = DT_PLTGOT;
1291618Srie 		if (ofl->ofl_osgot)
1301618Srie 			(*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
1311618Srie 		else
1321618Srie 			(*dyn)->d_un.d_ptr = 0;
1331618Srie 		(*dyn)++;
1341618Srie 	}
1351618Srie }
1361618Srie 
1376206Sab196087 static Xword
ld_calc_plt_addr(Sym_desc * sdp,Ofl_desc * ofl)1381618Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
1391618Srie {
1401618Srie 	Xword	value;
1411618Srie 
1421618Srie 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
1431618Srie 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
1441618Srie 	return (value);
1451618Srie }
1461618Srie 
1471618Srie /*
1481618Srie  *  Build a single plt entry - code is:
1491618Srie  *	if (building a.out)
1501618Srie  *		JMP	*got_off
1511618Srie  *	else
1521618Srie  *		JMP	*got_off@GOT(%ebx)
1531618Srie  *	PUSHL	&rel_off
1541618Srie  *	JMP	-n(%pc)		# -n is pcrel offset to first plt entry
1551618Srie  *
1561618Srie  *	The got_off@GOT entry gets filled with the address of the PUSHL,
1571618Srie  *	so the first pass through the plt jumps back here, jumping
1581618Srie  *	in turn to the first plt entry, which jumps to the dynamic
1591618Srie  *	linker.	 The dynamic linker then patches the GOT, rerouting
1601618Srie  *	future plt calls to the proper destination.
1611618Srie  */
1621618Srie static void
plt_entry(Ofl_desc * ofl,Word rel_off,Sym_desc * sdp)1631618Srie plt_entry(Ofl_desc * ofl, Word rel_off, Sym_desc * sdp)
1641618Srie {
1651618Srie 	uchar_t		*pltent, *gotent;
1661618Srie 	Sword		plt_off;
1671618Srie 	Word		got_off;
1686206Sab196087 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1691618Srie 
1701618Srie 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
1711618Srie 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
1721618Srie 	    M_PLT_ENTSIZE);
1731618Srie 	pltent = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf) + plt_off;
1741618Srie 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
1751618Srie 
1761618Srie 	/*
1771618Srie 	 * Fill in the got entry with the address of the next instruction.
1781618Srie 	 */
1791618Srie 	/* LINTED */
1801618Srie 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
1811618Srie 	    M_PLT_INSSIZE;
1826206Sab196087 	if (bswap)
1836206Sab196087 		/* LINTED */
1846206Sab196087 		*(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
1851618Srie 
1861618Srie 	if (!(ofl->ofl_flags & FLG_OF_SHAROBJ)) {
1871618Srie 		pltent[0] = M_SPECIAL_INST;
1881618Srie 		pltent[1] = M_JMP_DISP_IND;
1891618Srie 		pltent += 2;
1901618Srie 		/* LINTED */
1911618Srie 		*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->sh_addr +
1924734Sab196087 		    got_off);
1931618Srie 	} else {
1941618Srie 		pltent[0] = M_SPECIAL_INST;
1951618Srie 		pltent[1] = M_JMP_REG_DISP_IND;
1961618Srie 		pltent += 2;
1971618Srie 		/* LINTED */
1981618Srie 		*(Word *)pltent = (Word)got_off;
1991618Srie 	}
2006206Sab196087 	if (bswap)
2016206Sab196087 		/* LINTED */
2026206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2031618Srie 	pltent += 4;
2041618Srie 
2051618Srie 	pltent[0] = M_INST_PUSHL;
2061618Srie 	pltent++;
2071618Srie 	/* LINTED */
2081618Srie 	*(Word *)pltent = (Word)rel_off;
2096206Sab196087 	if (bswap)
2106206Sab196087 		/* LINTED */
2116206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2121618Srie 	pltent += 4;
2131618Srie 
2141618Srie 	plt_off = -(plt_off + 16);	/* JMP, PUSHL, JMP take 16 bytes */
2151618Srie 	pltent[0] = M_INST_JMP;
2161618Srie 	pltent++;
2171618Srie 	/* LINTED */
2181618Srie 	*(Word *)pltent = (Word)plt_off;
2196206Sab196087 	if (bswap)
2206206Sab196087 		/* LINTED */
2216206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2221618Srie }
2231618Srie 
2246206Sab196087 static uintptr_t
ld_perform_outreloc(Rel_desc * orsp,Ofl_desc * ofl,Boolean * remain_seen)225*13074SAli.Bahrami@Oracle.COM ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl, Boolean *remain_seen)
2261618Srie {
2271618Srie 	Os_desc *	relosp, * osp = 0;
2281618Srie 	Word		ndx, roffset, value;
2291618Srie 	Rel		rea;
2301618Srie 	char		*relbits;
2311618Srie 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
2321618Srie 	int		sectmoved = 0;
2331618Srie 
2341618Srie 	sdp = orsp->rel_sym;
2351618Srie 
2361618Srie 	/*
2371618Srie 	 * If the section this relocation is against has been discarded
2381618Srie 	 * (-zignore), then also discard (skip) the relocation itself.
2391618Srie 	 */
2401618Srie 	if (orsp->rel_isdesc && ((orsp->rel_flags &
2411618Srie 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
2421618Srie 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
2431618Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
2441618Srie 		return (1);
2451618Srie 	}
2461618Srie 
2471618Srie 	/*
2481618Srie 	 * If this is a relocation against a move table, or expanded move
2491618Srie 	 * table, adjust the relocation entries.
2501618Srie 	 */
25112155SAli.Bahrami@Sun.COM 	if (RELAUX_GET_MOVE(orsp))
2521618Srie 		ld_adj_movereloc(ofl, orsp);
2531618Srie 
2541618Srie 	/*
2551618Srie 	 * If this is a relocation against a section using a partial initialized
2561618Srie 	 * symbol, adjust the embedded symbol info.
2571618Srie 	 *
2581618Srie 	 * The second argument of the am_I_partial() is the value stored at the
2591618Srie 	 * target address relocation is going to be applied.
2601618Srie 	 */
2611618Srie 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
2629131SRod.Evans@Sun.COM 		if (ofl->ofl_parsyms &&
2631618Srie 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
2641618Srie 		    /* LINTED */
2651618Srie 		    (psym = ld_am_I_partial(orsp, *(Xword *)
2661618Srie 		    ((uchar_t *)(orsp->rel_isdesc->is_indata->d_buf) +
2671618Srie 		    orsp->rel_roffset)))) {
2681618Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
2691618Srie 			sectmoved = 1;
2704734Sab196087 		}
2711618Srie 	}
2721618Srie 
2731618Srie 	value = sdp->sd_sym->st_value;
2741618Srie 
2751618Srie 	if (orsp->rel_flags & FLG_REL_GOT) {
2761618Srie 		osp = ofl->ofl_osgot;
2771618Srie 		roffset = (Word)ld_calc_got_offset(orsp, ofl);
2781618Srie 
2791618Srie 	} else if (orsp->rel_flags & FLG_REL_PLT) {
2801618Srie 		/*
2811618Srie 		 * Note that relocations for PLT's actually
2821618Srie 		 * cause a relocation againt the GOT.
2831618Srie 		 */
2841618Srie 		osp = ofl->ofl_osplt;
2851618Srie 		roffset = (Word) (ofl->ofl_osgot->os_shdr->sh_addr) +
2861618Srie 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2871618Srie 
2881618Srie 		plt_entry(ofl, osp->os_relosdesc->os_szoutrels, sdp);
2891618Srie 
2901618Srie 	} else if (orsp->rel_flags & FLG_REL_BSS) {
2911618Srie 		/*
2921618Srie 		 * This must be a R_386_COPY.  For these set the roffset to
2931618Srie 		 * point to the new symbols location.
2941618Srie 		 */
2951618Srie 		osp = ofl->ofl_isbss->is_osdesc;
2961618Srie 		roffset = (Word)value;
2971618Srie 	} else {
29812155SAli.Bahrami@Sun.COM 		osp = RELAUX_GET_OSDESC(orsp);
2991618Srie 
3001618Srie 		/*
3011618Srie 		 * Calculate virtual offset of reference point; equals offset
3021618Srie 		 * into section + vaddr of section for loadable sections, or
3031618Srie 		 * offset plus section displacement for nonloadable sections.
3041618Srie 		 */
3051618Srie 		roffset = orsp->rel_roffset +
3061618Srie 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
3071618Srie 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
3081618Srie 			roffset += orsp->rel_isdesc->is_osdesc->
3091618Srie 			    os_shdr->sh_addr;
3101618Srie 	}
3111618Srie 
3121618Srie 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
3131618Srie 		relosp = ofl->ofl_osrel;
3141618Srie 
3151618Srie 	/*
3161618Srie 	 * Assign the symbols index for the output relocation.  If the
3171618Srie 	 * relocation refers to a SECTION symbol then it's index is based upon
3181618Srie 	 * the output sections symbols index.  Otherwise the index can be
3191618Srie 	 * derived from the symbols index itself.
3201618Srie 	 */
3211618Srie 	if (orsp->rel_rtype == R_386_RELATIVE)
3221618Srie 		ndx = STN_UNDEF;
3231618Srie 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
3241618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
3251618Srie 		if (sectmoved == 0) {
3261618Srie 			/*
3271618Srie 			 * Check for a null input section. This can
3281618Srie 			 * occur if this relocation references a symbol
3291618Srie 			 * generated by sym_add_sym().
3301618Srie 			 */
3319131SRod.Evans@Sun.COM 			if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
3329131SRod.Evans@Sun.COM 				ndx = sdp->sd_isc->is_osdesc->os_identndx;
3331618Srie 			else
3341618Srie 				ndx = sdp->sd_shndx;
3351618Srie 		} else
3368159SAli.Bahrami@Sun.COM 			ndx = ofl->ofl_parexpnndx;
3371618Srie 	} else
3381618Srie 		ndx = sdp->sd_symndx;
3391618Srie 
3405892Sab196087 	/*
3415892Sab196087 	 * If we have a replacement value for the relocation
3425892Sab196087 	 * target, put it in place now.
3435892Sab196087 	 */
3445892Sab196087 	if (orsp->rel_flags & FLG_REL_NADDEND) {
3455892Sab196087 		Xword	addend = orsp->rel_raddend;
3465892Sab196087 		uchar_t	*addr;
3475892Sab196087 
3485892Sab196087 		/*
3495892Sab196087 		 * Get the address of the data item we need to modify.
3505892Sab196087 		 */
3515892Sab196087 		addr = (uchar_t *)((uintptr_t)orsp->rel_roffset +
3525892Sab196087 		    (uintptr_t)_elf_getxoff(orsp->rel_isdesc->is_indata));
35312155SAli.Bahrami@Sun.COM 		addr += (uintptr_t)RELAUX_GET_OSDESC(orsp)->os_outdata->d_buf;
3545892Sab196087 		if (ld_reloc_targval_set(ofl, orsp, addr, addend) == 0)
3555892Sab196087 			return (S_ERROR);
3565892Sab196087 	}
3575892Sab196087 
3581618Srie 	relbits = (char *)relosp->os_outdata->d_buf;
3591618Srie 
3601618Srie 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
3611618Srie 	rea.r_offset = roffset;
3621618Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_REL, &rea, relosp->os_name,
36312155SAli.Bahrami@Sun.COM 	    ld_reloc_sym_name(orsp)));
3641618Srie 
3651618Srie 	/*
3661618Srie 	 * Assert we haven't walked off the end of our relocation table.
3671618Srie 	 */
3681618Srie 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
3691618Srie 
3701618Srie 	(void) memcpy((relbits + relosp->os_szoutrels),
3711618Srie 	    (char *)&rea, sizeof (Rel));
3721618Srie 	relosp->os_szoutrels += sizeof (Rel);
3731618Srie 
3741618Srie 	/*
3751618Srie 	 * Determine if this relocation is against a non-writable, allocatable
3761618Srie 	 * section.  If so we may need to provide a text relocation diagnostic.
3771618Srie 	 * Note that relocations against the .plt (R_386_JMP_SLOT) actually
3781618Srie 	 * result in modifications to the .got.
3791618Srie 	 */
3801618Srie 	if (orsp->rel_rtype == R_386_JMP_SLOT)
3811618Srie 		osp = ofl->ofl_osgot;
3821618Srie 
383*13074SAli.Bahrami@Oracle.COM 	ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
3841618Srie 	return (1);
3851618Srie }
3861618Srie 
3871618Srie /*
3881618Srie  * i386 Instructions for TLS processing
3891618Srie  */
3901618Srie static uchar_t tlsinstr_gd_ie[] = {
3911618Srie 	/*
3921618Srie 	 * 0x00	movl %gs:0x0, %eax
3931618Srie 	 */
3941618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
3951618Srie 	/*
3961618Srie 	 * 0x06	addl x(%eax), %eax
3971618Srie 	 * 0x0c ...
3981618Srie 	 */
3991618Srie 	0x03, 0x80, 0x00, 0x00, 0x00, 0x00
4001618Srie };
4011618Srie 
4021618Srie static uchar_t tlsinstr_gd_le[] = {
4031618Srie 	/*
4041618Srie 	 * 0x00 movl %gs:0x0, %eax
4051618Srie 	 */
4061618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
4071618Srie 	/*
4081618Srie 	 * 0x06 addl $0x0, %eax
4091618Srie 	 */
4101618Srie 	0x05, 0x00, 0x00, 0x00, 0x00,
4111618Srie 	/*
4121618Srie 	 * 0x0b nop
4131618Srie 	 * 0x0c
4141618Srie 	 */
4151618Srie 	0x90
4161618Srie };
4171618Srie 
4181618Srie static uchar_t tlsinstr_gd_ie_movgs[] = {
4191618Srie 	/*
4201618Srie 	 *	movl %gs:0x0,%eax
4211618Srie 	 */
4221618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 00
4231618Srie };
4241618Srie 
4251618Srie #define	TLS_GD_IE_MOV	0x8b	/* movl opcode */
4261618Srie #define	TLS_GD_IE_POP	0x58	/* popl + reg */
4271618Srie 
4281618Srie #define	TLS_GD_LE_MOVL	0xb8	/* movl + reg */
4291618Srie 
4301618Srie #define	TLS_NOP		0x90	/* NOP instruction */
4311618Srie 
4321618Srie #define	MODRM_MSK_MOD	0xc0
4331618Srie #define	MODRM_MSK_RO	0x38
4341618Srie #define	MODRM_MSK_RM	0x07
4351618Srie 
4361618Srie #define	SIB_MSK_SS	0xc0
4371618Srie #define	SIB_MSK_IND	0x38
4381618Srie #define	SIB_MSK_BS	0x07
4391618Srie 
4401618Srie static Fixupret
tls_fixups(Ofl_desc * ofl,Rel_desc * arsp)4411618Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
4421618Srie {
4431618Srie 	Sym_desc	*sdp = arsp->rel_sym;
4441618Srie 	Word		rtype = arsp->rel_rtype;
4451618Srie 	uchar_t		*offset, r1, r2;
4461618Srie 
4471618Srie 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
4481618Srie 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
44912155SAli.Bahrami@Sun.COM 	    (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
4501618Srie 
4511618Srie 	if (sdp->sd_ref == REF_DYN_NEED) {
4521618Srie 		/*
4531618Srie 		 * IE reference model
4541618Srie 		 */
4551618Srie 		switch (rtype) {
4561618Srie 		case R_386_TLS_GD:
4571618Srie 			/*
4581618Srie 			 * Transition:
4591618Srie 			 *	0x0 leal x@tlsgd(,r1,1), %eax
4601618Srie 			 *	0x7 call ___tls_get_addr
4611618Srie 			 *	0xc
4621618Srie 			 * To:
4631618Srie 			 *	0x0 movl %gs:0, %eax
4641618Srie 			 *	0x6 addl x@gotntpoff(r1), %eax
4651618Srie 			 */
4661618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
46712155SAli.Bahrami@Sun.COM 			    R_386_TLS_GOTIE, arsp, ld_reloc_sym_name));
4681618Srie 			arsp->rel_rtype = R_386_TLS_GOTIE;
4691618Srie 			arsp->rel_roffset += 5;
4701618Srie 
4711618Srie 			/*
4723304Srie 			 * Adjust 'offset' to beginning of instruction
4731618Srie 			 * sequence.
4741618Srie 			 */
4751618Srie 			offset -= 3;
4761618Srie 			r1 = (offset[2] & SIB_MSK_IND) >> 3;
4771618Srie 			(void) memcpy(offset, tlsinstr_gd_ie,
4781618Srie 			    sizeof (tlsinstr_gd_ie));
4791618Srie 
4801618Srie 			/*
4811618Srie 			 * set register %r1 into the addl
4821618Srie 			 * instruction.
4831618Srie 			 */
4841618Srie 			offset[0x7] |= r1;
4851618Srie 			return (FIX_RELOC);
4861618Srie 
4871618Srie 		case R_386_TLS_GD_PLT:
4881618Srie 			/*
4891618Srie 			 * Fixup done via the TLS_GD relocation
4901618Srie 			 */
4911618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
49212155SAli.Bahrami@Sun.COM 			    R_386_NONE, arsp, ld_reloc_sym_name));
4931618Srie 			return (FIX_DONE);
4941618Srie 		}
4951618Srie 	}
4961618Srie 
4971618Srie 	/*
4981618Srie 	 * LE reference model
4991618Srie 	 */
5001618Srie 	switch (rtype) {
5011618Srie 	case R_386_TLS_GD:
5021618Srie 		/*
5031618Srie 		 * Transition:
5041618Srie 		 *	0x0 leal x@tlsgd(,r1,1), %eax
5051618Srie 		 *	0x7 call ___tls_get_addr
5061618Srie 		 *	0xc
5071618Srie 		 * To:
5081618Srie 		 *	0x0 movl %gs:0, %eax
5091618Srie 		 *	0x6 addl $x@ntpoff, %eax
5101618Srie 		 *	0xb nop
5111618Srie 		 *	0xc
5121618Srie 		 */
5131618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
51412155SAli.Bahrami@Sun.COM 		    R_386_TLS_LE, arsp, ld_reloc_sym_name));
5151618Srie 
5161618Srie 		arsp->rel_rtype = R_386_TLS_LE;
5171618Srie 		arsp->rel_roffset += 4;
5181618Srie 
5191618Srie 		/*
5203304Srie 		 * Adjust 'offset' to beginning of instruction
5211618Srie 		 * sequence.
5221618Srie 		 */
5231618Srie 		offset -= 3;
5241618Srie 		(void) memcpy(offset, tlsinstr_gd_le,
5251618Srie 		    sizeof (tlsinstr_gd_le));
5261618Srie 		return (FIX_RELOC);
5271618Srie 
5281618Srie 	case R_386_TLS_GD_PLT:
5291618Srie 	case R_386_PLT32:
5301618Srie 		/*
5311618Srie 		 * Fixup done via the TLS_GD relocation
5321618Srie 		 */
5331618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
53412155SAli.Bahrami@Sun.COM 		    R_386_NONE, arsp, ld_reloc_sym_name));
5351618Srie 		return (FIX_DONE);
5361618Srie 
5371618Srie 	case R_386_TLS_LDM_PLT:
5381618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
53912155SAli.Bahrami@Sun.COM 		    R_386_NONE, arsp, ld_reloc_sym_name));
5401618Srie 
5411618Srie 		/*
5421618Srie 		 * Transition:
5431618Srie 		 *	call __tls_get_addr()
5441618Srie 		 * to:
5451618Srie 		 *	nop
5461618Srie 		 *	nop
5471618Srie 		 *	nop
5481618Srie 		 *	nop
5491618Srie 		 *	nop
5501618Srie 		 */
5511618Srie 		*(offset - 1) = TLS_NOP;
5521618Srie 		*(offset) = TLS_NOP;
5531618Srie 		*(offset + 1) = TLS_NOP;
5541618Srie 		*(offset + 2) = TLS_NOP;
5551618Srie 		*(offset + 3) = TLS_NOP;
5561618Srie 		return (FIX_DONE);
5571618Srie 
5581618Srie 	case R_386_TLS_LDM:
5591618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
56012155SAli.Bahrami@Sun.COM 		    R_386_NONE, arsp, ld_reloc_sym_name));
5611618Srie 
5621618Srie 		/*
5631618Srie 		 * Transition:
5641618Srie 		 *
5651618Srie 		 *  0x00 leal x1@tlsldm(%ebx), %eax
5661618Srie 		 *  0x06 call ___tls_get_addr
5671618Srie 		 *
5681618Srie 		 * to:
5691618Srie 		 *
5701618Srie 		 *  0x00 movl %gs:0, %eax
5711618Srie 		 */
5721618Srie 		(void) memcpy(offset - 2, tlsinstr_gd_ie_movgs,
5731618Srie 		    sizeof (tlsinstr_gd_ie_movgs));
5741618Srie 		return (FIX_DONE);
5751618Srie 
5761618Srie 	case R_386_TLS_LDO_32:
5771618Srie 		/*
5781618Srie 		 *  Instructions:
5791618Srie 		 *
5801618Srie 		 *  0x10 leal x1@dtpoff(%eax), %edx	R_386_TLS_LDO_32
5811618Srie 		 *		to
5821618Srie 		 *  0x10 leal x1@ntpoff(%eax), %edx	R_386_TLS_LE
5831618Srie 		 *
5841618Srie 		 */
5851618Srie 		offset -= 2;
5861618Srie 
5871618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
58812155SAli.Bahrami@Sun.COM 		    R_386_TLS_LE, arsp, ld_reloc_sym_name));
5891618Srie 		arsp->rel_rtype = R_386_TLS_LE;
5901618Srie 		return (FIX_RELOC);
5911618Srie 
5921618Srie 	case R_386_TLS_GOTIE:
5931618Srie 		/*
5941618Srie 		 * These transitions are a little different than the
5951618Srie 		 * others, in that we could have multiple instructions
5961618Srie 		 * pointed to by a single relocation.  Depending upon the
5971618Srie 		 * instruction, we perform a different code transition.
5981618Srie 		 *
5991618Srie 		 * Here's the known transitions:
6001618Srie 		 *
6011618Srie 		 *  1) movl foo@gotntpoff(%reg1), %reg2
6021618Srie 		 *	0x8b, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
6031618Srie 		 *
6041618Srie 		 *  2) addl foo@gotntpoff(%reg1), %reg2
6051618Srie 		 *	0x03, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
6061618Srie 		 *
6071618Srie 		 *  Transitions IE -> LE
6081618Srie 		 *
6091618Srie 		 *  1) movl $foo@ntpoff, %reg2
6101618Srie 		 *	0xc7, 0xc0 | reg2, foo@ntpoff
6111618Srie 		 *
6121618Srie 		 *  2) addl $foo@ntpoff, %reg2
6131618Srie 		 *	0x81, 0xc0 | reg2, foo@ntpoff
6141618Srie 		 *
6151618Srie 		 * Note: reg1 != 4 (%esp)
6161618Srie 		 */
6171618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
61812155SAli.Bahrami@Sun.COM 		    R_386_TLS_LE, arsp, ld_reloc_sym_name));
6191618Srie 		arsp->rel_rtype = R_386_TLS_LE;
6201618Srie 
6211618Srie 		offset -= 2;
6221618Srie 		r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6231618Srie 		if (offset[0] == 0x8b) {
6241618Srie 			/* case 1 above */
6251618Srie 			offset[0] = 0xc7;	/* movl */
6261618Srie 			offset[1] = 0xc0 | r2;
6271618Srie 			return (FIX_RELOC);
6281618Srie 		}
6291618Srie 
6301618Srie 		if (offset[0] == 0x03) {
6311618Srie 			/* case 2 above */
6321618Srie 			assert(offset[0] == 0x03);
6331618Srie 			offset[0] = 0x81;	/* addl */
6341618Srie 			offset[1] = 0xc0 | r2;
6351618Srie 			return (FIX_RELOC);
6361618Srie 		}
6371618Srie 
6381618Srie 		/*
6391618Srie 		 * Unexpected instruction sequence - fatal error.
6401618Srie 		 */
6414734Sab196087 		{
6424734Sab196087 			Conv_inv_buf_t	inv_buf;
6434734Sab196087 
644*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
6454734Sab196087 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
6464734Sab196087 			    arsp->rel_isdesc->is_file->ifl_name,
64712155SAli.Bahrami@Sun.COM 			    ld_reloc_sym_name(arsp),
6484734Sab196087 			    arsp->rel_isdesc->is_name,
6494734Sab196087 			    EC_OFF(arsp->rel_roffset));
6504734Sab196087 		}
6511618Srie 		return (FIX_ERROR);
6521618Srie 
6531618Srie 	case R_386_TLS_IE:
6541618Srie 		/*
6551618Srie 		 * These transitions are a little different than the
6561618Srie 		 * others, in that we could have multiple instructions
6571618Srie 		 * pointed to by a single relocation.  Depending upon the
6581618Srie 		 * instruction, we perform a different code transition.
6591618Srie 		 *
6601618Srie 		 * Here's the known transitions:
6611618Srie 		 *  1) movl foo@indntpoff, %eax
6621618Srie 		 *	0xa1, foo@indntpoff
6631618Srie 		 *
6641618Srie 		 *  2) movl foo@indntpoff, %eax
6651618Srie 		 *	0x8b, 0x05 | (reg << 3), foo@gotntpoff
6661618Srie 		 *
6671618Srie 		 *  3) addl foo@indntpoff, %eax
6681618Srie 		 *	0x03, 0x05 | (reg << 3), foo@gotntpoff
6691618Srie 		 *
6701618Srie 		 *  Transitions IE -> LE
6711618Srie 		 *
6721618Srie 		 *  1) movl $foo@ntpoff, %eax
6731618Srie 		 *	0xb8, foo@ntpoff
6741618Srie 		 *
6751618Srie 		 *  2) movl $foo@ntpoff, %reg
6761618Srie 		 *	0xc7, 0xc0 | reg, foo@ntpoff
6771618Srie 		 *
6781618Srie 		 *  3) addl $foo@ntpoff, %reg
6791618Srie 		 *	0x81, 0xc0 | reg, foo@ntpoff
6801618Srie 		 */
6811618Srie 		arsp->rel_rtype = R_386_TLS_LE;
6821618Srie 		offset--;
6831618Srie 		if (offset[0] == 0xa1) {
6841618Srie 			/* case 1 above */
6851618Srie 			offset[0] = 0xb8;	/*  movl */
6861618Srie 			return (FIX_RELOC);
6871618Srie 		}
6881618Srie 
6891618Srie 		offset--;
6901618Srie 		if (offset[0] == 0x8b) {
6911618Srie 			/* case 2 above */
6921618Srie 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6931618Srie 			offset[0] = 0xc7;	/* movl */
6941618Srie 			offset[1] = 0xc0 | r2;
6951618Srie 			return (FIX_RELOC);
6961618Srie 		}
6971618Srie 		if (offset[0] == 0x03) {
6981618Srie 			/* case 3 above */
6991618Srie 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
7001618Srie 			offset[0] = 0x81;	/* addl */
7011618Srie 			offset[1] = 0xc0 | r2;
7021618Srie 			return (FIX_RELOC);
7031618Srie 		}
7041618Srie 		/*
7051618Srie 		 * Unexpected instruction sequence - fatal error.
7061618Srie 		 */
7074734Sab196087 		{
7084734Sab196087 			Conv_inv_buf_t	inv_buf;
7094734Sab196087 
710*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
7114734Sab196087 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
7124734Sab196087 			    arsp->rel_isdesc->is_file->ifl_name,
71312155SAli.Bahrami@Sun.COM 			    ld_reloc_sym_name(arsp),
7144734Sab196087 			    arsp->rel_isdesc->is_name,
7154734Sab196087 			    EC_OFF(arsp->rel_roffset));
7164734Sab196087 		}
7171618Srie 		return (FIX_ERROR);
7181618Srie 	}
7191618Srie 	return (FIX_RELOC);
7201618Srie }
7211618Srie 
7226206Sab196087 static uintptr_t
ld_do_activerelocs(Ofl_desc * ofl)7231618Srie ld_do_activerelocs(Ofl_desc *ofl)
7241618Srie {
7251618Srie 	Rel_desc	*arsp;
72612155SAli.Bahrami@Sun.COM 	Rel_cachebuf	*rcbp;
7279131SRod.Evans@Sun.COM 	Aliste		idx;
7281618Srie 	uintptr_t	return_code = 1;
7296299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
7301618Srie 
73112155SAli.Bahrami@Sun.COM 	if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
7322647Srie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
7332647Srie 
7341618Srie 	/*
7351618Srie 	 * Process active relocations.
7361618Srie 	 */
73712155SAli.Bahrami@Sun.COM 	REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
73812155SAli.Bahrami@Sun.COM 		uchar_t		*addr;
73912155SAli.Bahrami@Sun.COM 		Xword 		value;
74012155SAli.Bahrami@Sun.COM 		Sym_desc	*sdp;
74112155SAli.Bahrami@Sun.COM 		const char	*ifl_name;
74212155SAli.Bahrami@Sun.COM 		Xword		refaddr;
74312155SAli.Bahrami@Sun.COM 		int		moved = 0;
74412155SAli.Bahrami@Sun.COM 		Gotref		gref;
74512155SAli.Bahrami@Sun.COM 		Os_desc		*osp;
74612155SAli.Bahrami@Sun.COM 
74712155SAli.Bahrami@Sun.COM 		/*
74812155SAli.Bahrami@Sun.COM 		 * If the section this relocation is against has been discarded
74912155SAli.Bahrami@Sun.COM 		 * (-zignore), then discard (skip) the relocation itself.
75012155SAli.Bahrami@Sun.COM 		 */
75112155SAli.Bahrami@Sun.COM 		if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
75212155SAli.Bahrami@Sun.COM 		    ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
75312155SAli.Bahrami@Sun.COM 		    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
75412155SAli.Bahrami@Sun.COM 			DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
75512155SAli.Bahrami@Sun.COM 			continue;
75612155SAli.Bahrami@Sun.COM 		}
7571618Srie 
75812155SAli.Bahrami@Sun.COM 		/*
75912155SAli.Bahrami@Sun.COM 		 * We determine what the 'got reference' model (if required)
76012155SAli.Bahrami@Sun.COM 		 * is at this point.  This needs to be done before tls_fixup()
76112155SAli.Bahrami@Sun.COM 		 * since it may 'transition' our instructions.
76212155SAli.Bahrami@Sun.COM 		 *
76312155SAli.Bahrami@Sun.COM 		 * The got table entries have already been assigned,
76412155SAli.Bahrami@Sun.COM 		 * and we bind to those initial entries.
76512155SAli.Bahrami@Sun.COM 		 */
76612155SAli.Bahrami@Sun.COM 		if (arsp->rel_flags & FLG_REL_DTLS)
76712155SAli.Bahrami@Sun.COM 			gref = GOT_REF_TLSGD;
76812155SAli.Bahrami@Sun.COM 		else if (arsp->rel_flags & FLG_REL_MTLS)
76912155SAli.Bahrami@Sun.COM 			gref = GOT_REF_TLSLD;
77012155SAli.Bahrami@Sun.COM 		else if (arsp->rel_flags & FLG_REL_STLS)
77112155SAli.Bahrami@Sun.COM 			gref = GOT_REF_TLSIE;
77212155SAli.Bahrami@Sun.COM 		else
77312155SAli.Bahrami@Sun.COM 			gref = GOT_REF_GENERIC;
77412155SAli.Bahrami@Sun.COM 
77512155SAli.Bahrami@Sun.COM 		/*
77612155SAli.Bahrami@Sun.COM 		 * Perform any required TLS fixups.
77712155SAli.Bahrami@Sun.COM 		 */
77812155SAli.Bahrami@Sun.COM 		if (arsp->rel_flags & FLG_REL_TLSFIX) {
77912155SAli.Bahrami@Sun.COM 			Fixupret	ret;
78012155SAli.Bahrami@Sun.COM 
78112155SAli.Bahrami@Sun.COM 			if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
78212155SAli.Bahrami@Sun.COM 				return (S_ERROR);
78312155SAli.Bahrami@Sun.COM 			if (ret == FIX_DONE)
78412155SAli.Bahrami@Sun.COM 				continue;
78512155SAli.Bahrami@Sun.COM 		}
78612155SAli.Bahrami@Sun.COM 
78712155SAli.Bahrami@Sun.COM 		/*
78812155SAli.Bahrami@Sun.COM 		 * If this is a relocation against a move table, or
78912155SAli.Bahrami@Sun.COM 		 * expanded move table, adjust the relocation entries.
79012155SAli.Bahrami@Sun.COM 		 */
79112155SAli.Bahrami@Sun.COM 		if (RELAUX_GET_MOVE(arsp))
79212155SAli.Bahrami@Sun.COM 			ld_adj_movereloc(ofl, arsp);
79312155SAli.Bahrami@Sun.COM 
79412155SAli.Bahrami@Sun.COM 		sdp = arsp->rel_sym;
79512155SAli.Bahrami@Sun.COM 		refaddr = arsp->rel_roffset +
79612155SAli.Bahrami@Sun.COM 		    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
79712155SAli.Bahrami@Sun.COM 
79812155SAli.Bahrami@Sun.COM 		if (arsp->rel_flags & FLG_REL_CLVAL)
79912155SAli.Bahrami@Sun.COM 			value = 0;
80012155SAli.Bahrami@Sun.COM 		else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
8011618Srie 			/*
80212155SAli.Bahrami@Sun.COM 			 * The value for a symbol pointing to a SECTION
80312155SAli.Bahrami@Sun.COM 			 * is based off of that sections position.
8041618Srie 			 */
80512155SAli.Bahrami@Sun.COM 			if (sdp->sd_isc->is_flags & FLG_IS_RELUPD) {
80612155SAli.Bahrami@Sun.COM 				Sym_desc	*sym;
80712155SAli.Bahrami@Sun.COM 				Xword		radd;
80812155SAli.Bahrami@Sun.COM 				uchar_t		*raddr = (uchar_t *)
80912155SAli.Bahrami@Sun.COM 				    arsp->rel_isdesc->is_indata->d_buf +
81012155SAli.Bahrami@Sun.COM 				    arsp->rel_roffset;
8111618Srie 
8121618Srie 				/*
81312155SAli.Bahrami@Sun.COM 				 * This is a REL platform. Hence, the second
81412155SAli.Bahrami@Sun.COM 				 * argument of ld_am_I_partial() is the value
81512155SAli.Bahrami@Sun.COM 				 * stored at the target address where the
81612155SAli.Bahrami@Sun.COM 				 * relocation is going to be applied.
8171618Srie 				 */
81812155SAli.Bahrami@Sun.COM 				if (ld_reloc_targval_get(ofl, arsp, raddr,
81912155SAli.Bahrami@Sun.COM 				    &radd) == 0)
82012155SAli.Bahrami@Sun.COM 					return (S_ERROR);
82112155SAli.Bahrami@Sun.COM 				sym = ld_am_I_partial(arsp, radd);
82212155SAli.Bahrami@Sun.COM 				if (sym) {
82312155SAli.Bahrami@Sun.COM 					Sym	*osym = sym->sd_osym;
8248369SAli.Bahrami@Sun.COM 
8251618Srie 					/*
82612155SAli.Bahrami@Sun.COM 					 * The symbol was moved, so adjust the
82712155SAli.Bahrami@Sun.COM 					 * value relative to the new section.
8281618Srie 					 */
82912155SAli.Bahrami@Sun.COM 					value = sym->sd_sym->st_value;
83012155SAli.Bahrami@Sun.COM 					moved = 1;
8318369SAli.Bahrami@Sun.COM 
83212155SAli.Bahrami@Sun.COM 					/*
83312155SAli.Bahrami@Sun.COM 					 * The original raddend covers the
83412155SAli.Bahrami@Sun.COM 					 * displacement from the section start
83512155SAli.Bahrami@Sun.COM 					 * to the desired address. The value
83612155SAli.Bahrami@Sun.COM 					 * computed above gets us from the
83712155SAli.Bahrami@Sun.COM 					 * section start to the start of the
83812155SAli.Bahrami@Sun.COM 					 * symbol range. Adjust the old raddend
83912155SAli.Bahrami@Sun.COM 					 * to remove the offset from section
84012155SAli.Bahrami@Sun.COM 					 * start to symbol start, leaving the
84112155SAli.Bahrami@Sun.COM 					 * displacement within the range of
84212155SAli.Bahrami@Sun.COM 					 * the symbol.
84312155SAli.Bahrami@Sun.COM 					 */
84412155SAli.Bahrami@Sun.COM 					if (osym->st_value != 0) {
84512155SAli.Bahrami@Sun.COM 						radd -= osym->st_value;
84612155SAli.Bahrami@Sun.COM 						if (ld_reloc_targval_set(ofl,
84712155SAli.Bahrami@Sun.COM 						    arsp, raddr, radd) == 0)
84812155SAli.Bahrami@Sun.COM 							return (S_ERROR);
8498369SAli.Bahrami@Sun.COM 					}
8508369SAli.Bahrami@Sun.COM 				}
85112155SAli.Bahrami@Sun.COM 			}
85212155SAli.Bahrami@Sun.COM 			if (!moved) {
85312155SAli.Bahrami@Sun.COM 				value = _elf_getxoff(sdp->sd_isc->is_indata);
85412155SAli.Bahrami@Sun.COM 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
85512155SAli.Bahrami@Sun.COM 					value += sdp->sd_isc->
85612155SAli.Bahrami@Sun.COM 					    is_osdesc->os_shdr->sh_addr;
85712155SAli.Bahrami@Sun.COM 			}
85812155SAli.Bahrami@Sun.COM 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
85912155SAli.Bahrami@Sun.COM 				value -= ofl->ofl_tlsphdr->p_vaddr;
8602850Srie 
86112155SAli.Bahrami@Sun.COM 		} else if (IS_SIZE(arsp->rel_rtype)) {
86212155SAli.Bahrami@Sun.COM 			/*
86312155SAli.Bahrami@Sun.COM 			 * Size relocations require the symbols size.
86412155SAli.Bahrami@Sun.COM 			 */
86512155SAli.Bahrami@Sun.COM 			value = sdp->sd_sym->st_size;
86611827SRod.Evans@Sun.COM 
86712155SAli.Bahrami@Sun.COM 		} else if ((sdp->sd_flags & FLG_SY_CAP) &&
86812155SAli.Bahrami@Sun.COM 		    sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
86912155SAli.Bahrami@Sun.COM 			/*
87012155SAli.Bahrami@Sun.COM 			 * If relocation is against a capabilities symbol, we
87112155SAli.Bahrami@Sun.COM 			 * need to jump to an associated PLT, so that at runtime
87212155SAli.Bahrami@Sun.COM 			 * ld.so.1 is involved to determine the best binding
87312155SAli.Bahrami@Sun.COM 			 * choice. Otherwise, the value is the symbols value.
87412155SAli.Bahrami@Sun.COM 			 */
87512155SAli.Bahrami@Sun.COM 			value = ld_calc_plt_addr(sdp, ofl);
87612155SAli.Bahrami@Sun.COM 
87712155SAli.Bahrami@Sun.COM 		} else
87812155SAli.Bahrami@Sun.COM 			value = sdp->sd_sym->st_value;
87911827SRod.Evans@Sun.COM 
88012155SAli.Bahrami@Sun.COM 		/*
88112155SAli.Bahrami@Sun.COM 		 * Relocation against the GLOBAL_OFFSET_TABLE.
88212155SAli.Bahrami@Sun.COM 		 */
88312155SAli.Bahrami@Sun.COM 		if ((arsp->rel_flags & FLG_REL_GOT) &&
88412155SAli.Bahrami@Sun.COM 		    !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
88512155SAli.Bahrami@Sun.COM 			return (S_ERROR);
88612155SAli.Bahrami@Sun.COM 		osp = RELAUX_GET_OSDESC(arsp);
8871618Srie 
88812155SAli.Bahrami@Sun.COM 		/*
88912155SAli.Bahrami@Sun.COM 		 * If loadable and not producing a relocatable object add the
89012155SAli.Bahrami@Sun.COM 		 * sections virtual address to the reference address.
89112155SAli.Bahrami@Sun.COM 		 */
89212155SAli.Bahrami@Sun.COM 		if ((arsp->rel_flags & FLG_REL_LOAD) &&
89312155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0))
89412155SAli.Bahrami@Sun.COM 			refaddr +=
89512155SAli.Bahrami@Sun.COM 			    arsp->rel_isdesc->is_osdesc->os_shdr->sh_addr;
8961618Srie 
89712155SAli.Bahrami@Sun.COM 		/*
89812155SAli.Bahrami@Sun.COM 		 * If this entry has a PLT assigned to it, its value is actually
89912155SAli.Bahrami@Sun.COM 		 * the address of the PLT (and not the address of the function).
90012155SAli.Bahrami@Sun.COM 		 */
90112155SAli.Bahrami@Sun.COM 		if (IS_PLT(arsp->rel_rtype)) {
90212155SAli.Bahrami@Sun.COM 			if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
90312155SAli.Bahrami@Sun.COM 				value = ld_calc_plt_addr(sdp, ofl);
90412155SAli.Bahrami@Sun.COM 		}
9051618Srie 
90612155SAli.Bahrami@Sun.COM 		/*
90712155SAli.Bahrami@Sun.COM 		 * Determine whether the value needs further adjustment. Filter
90812155SAli.Bahrami@Sun.COM 		 * through the attributes of the relocation to determine what
90912155SAli.Bahrami@Sun.COM 		 * adjustment is required.  Note, many of the following cases
91012155SAli.Bahrami@Sun.COM 		 * are only applicable when a .got is present.  As a .got is
91112155SAli.Bahrami@Sun.COM 		 * not generated when a relocatable object is being built,
91212155SAli.Bahrami@Sun.COM 		 * any adjustments that require a .got need to be skipped.
91312155SAli.Bahrami@Sun.COM 		 */
91412155SAli.Bahrami@Sun.COM 		if ((arsp->rel_flags & FLG_REL_GOT) &&
91512155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
91612155SAli.Bahrami@Sun.COM 			Xword		R1addr;
91712155SAli.Bahrami@Sun.COM 			uintptr_t	R2addr;
91812155SAli.Bahrami@Sun.COM 			Word		gotndx;
91912155SAli.Bahrami@Sun.COM 			Gotndx		*gnp;
9201618Srie 
9211618Srie 			/*
92212155SAli.Bahrami@Sun.COM 			 * Perform relocation against GOT table.  Since this
92312155SAli.Bahrami@Sun.COM 			 * doesn't fit exactly into a relocation we place the
92412155SAli.Bahrami@Sun.COM 			 * appropriate byte in the GOT directly
92512155SAli.Bahrami@Sun.COM 			 *
92612155SAli.Bahrami@Sun.COM 			 * Calculate offset into GOT at which to apply
92712155SAli.Bahrami@Sun.COM 			 * the relocation.
9281618Srie 			 */
92912155SAli.Bahrami@Sun.COM 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
93012155SAli.Bahrami@Sun.COM 			assert(gnp);
9311618Srie 
93212155SAli.Bahrami@Sun.COM 			if (arsp->rel_rtype == R_386_TLS_DTPOFF32)
93312155SAli.Bahrami@Sun.COM 				gotndx = gnp->gn_gotndx + 1;
93412155SAli.Bahrami@Sun.COM 			else
93512155SAli.Bahrami@Sun.COM 				gotndx = gnp->gn_gotndx;
9361618Srie 
93712155SAli.Bahrami@Sun.COM 			R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
9381618Srie 
93912155SAli.Bahrami@Sun.COM 			/*
94012155SAli.Bahrami@Sun.COM 			 * Add the GOTs data's offset.
94112155SAli.Bahrami@Sun.COM 			 */
94212155SAli.Bahrami@Sun.COM 			R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
9431618Srie 
94412155SAli.Bahrami@Sun.COM 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
94512155SAli.Bahrami@Sun.COM 			    M_MACH, SHT_REL, arsp, R1addr, value,
94612155SAli.Bahrami@Sun.COM 			    ld_reloc_sym_name));
9471618Srie 
94812155SAli.Bahrami@Sun.COM 			/*
94912155SAli.Bahrami@Sun.COM 			 * And do it.
95012155SAli.Bahrami@Sun.COM 			 */
95112155SAli.Bahrami@Sun.COM 			if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
95212155SAli.Bahrami@Sun.COM 				*(Xword *)R2addr = ld_bswap_Xword(value);
95312155SAli.Bahrami@Sun.COM 			else
95412155SAli.Bahrami@Sun.COM 				*(Xword *)R2addr = value;
95512155SAli.Bahrami@Sun.COM 			continue;
9561618Srie 
95712155SAli.Bahrami@Sun.COM 		} else if (IS_GOT_BASED(arsp->rel_rtype) &&
95812155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
95912155SAli.Bahrami@Sun.COM 			value -= ofl->ofl_osgot->os_shdr->sh_addr;
9601618Srie 
96112155SAli.Bahrami@Sun.COM 		} else if (IS_GOT_PC(arsp->rel_rtype) &&
96212155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
96312155SAli.Bahrami@Sun.COM 			value = (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) -
96412155SAli.Bahrami@Sun.COM 			    refaddr;
9651618Srie 
96612155SAli.Bahrami@Sun.COM 		} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
96712155SAli.Bahrami@Sun.COM 		    (((flags & FLG_OF_RELOBJ) == 0) ||
96812155SAli.Bahrami@Sun.COM 		    (osp == sdp->sd_isc->is_osdesc))) {
96912155SAli.Bahrami@Sun.COM 			value -= refaddr;
9701618Srie 
97112155SAli.Bahrami@Sun.COM 		} else if (IS_TLS_INS(arsp->rel_rtype) &&
97212155SAli.Bahrami@Sun.COM 		    IS_GOT_RELATIVE(arsp->rel_rtype) &&
97312155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
97412155SAli.Bahrami@Sun.COM 			Gotndx	*gnp;
9751618Srie 
97612155SAli.Bahrami@Sun.COM 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
97712155SAli.Bahrami@Sun.COM 			assert(gnp);
97812155SAli.Bahrami@Sun.COM 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
97912155SAli.Bahrami@Sun.COM 			if (arsp->rel_rtype == R_386_TLS_IE) {
98012155SAli.Bahrami@Sun.COM 				value += ofl->ofl_osgot->os_shdr->sh_addr;
9811618Srie 			}
9821618Srie 
98312155SAli.Bahrami@Sun.COM 		} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
98412155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
98512155SAli.Bahrami@Sun.COM 			Gotndx *gnp;
98612155SAli.Bahrami@Sun.COM 
98712155SAli.Bahrami@Sun.COM 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
98812155SAli.Bahrami@Sun.COM 			    GOT_REF_GENERIC, ofl, NULL);
98912155SAli.Bahrami@Sun.COM 			assert(gnp);
99012155SAli.Bahrami@Sun.COM 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
99112155SAli.Bahrami@Sun.COM 
99212155SAli.Bahrami@Sun.COM 		} else if ((arsp->rel_flags & FLG_REL_STLS) &&
99312155SAli.Bahrami@Sun.COM 		    ((flags & FLG_OF_RELOBJ) == 0)) {
99412155SAli.Bahrami@Sun.COM 			Xword	tlsstatsize;
9951618Srie 
9961618Srie 			/*
99712155SAli.Bahrami@Sun.COM 			 * This is the LE TLS reference model.  Static
99812155SAli.Bahrami@Sun.COM 			 * offset is hard-coded.
9991618Srie 			 */
100012155SAli.Bahrami@Sun.COM 			tlsstatsize = S_ROUND(ofl->ofl_tlsphdr->p_memsz,
100112155SAli.Bahrami@Sun.COM 			    M_TLSSTATALIGN);
100212155SAli.Bahrami@Sun.COM 			value = tlsstatsize - value;
10031618Srie 
10041618Srie 			/*
100512155SAli.Bahrami@Sun.COM 			 * Since this code is fixed up, it assumes a
100612155SAli.Bahrami@Sun.COM 			 * negative offset that can be added to the
100712155SAli.Bahrami@Sun.COM 			 * thread pointer.
10081618Srie 			 */
100912155SAli.Bahrami@Sun.COM 			if ((arsp->rel_rtype == R_386_TLS_LDO_32) ||
101012155SAli.Bahrami@Sun.COM 			    (arsp->rel_rtype == R_386_TLS_LE))
101112155SAli.Bahrami@Sun.COM 				value = -value;
101212155SAli.Bahrami@Sun.COM 		}
10131618Srie 
101412155SAli.Bahrami@Sun.COM 		if (arsp->rel_isdesc->is_file)
101512155SAli.Bahrami@Sun.COM 			ifl_name = arsp->rel_isdesc->is_file->ifl_name;
101612155SAli.Bahrami@Sun.COM 		else
101712155SAli.Bahrami@Sun.COM 			ifl_name = MSG_INTL(MSG_STR_NULL);
10181618Srie 
101912155SAli.Bahrami@Sun.COM 		/*
102012155SAli.Bahrami@Sun.COM 		 * Make sure we have data to relocate.  Compiler and assembler
102112155SAli.Bahrami@Sun.COM 		 * developers have been known to generate relocations against
102212155SAli.Bahrami@Sun.COM 		 * invalid sections (normally .bss), so for their benefit give
102312155SAli.Bahrami@Sun.COM 		 * them sufficient information to help analyze the problem.
102412155SAli.Bahrami@Sun.COM 		 * End users should never see this.
102512155SAli.Bahrami@Sun.COM 		 */
102612155SAli.Bahrami@Sun.COM 		if (arsp->rel_isdesc->is_indata->d_buf == 0) {
102712155SAli.Bahrami@Sun.COM 			Conv_inv_buf_t	inv_buf;
10281618Srie 
1029*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
103012155SAli.Bahrami@Sun.COM 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
103112155SAli.Bahrami@Sun.COM 			    ifl_name, ld_reloc_sym_name(arsp),
103212155SAli.Bahrami@Sun.COM 			    EC_WORD(arsp->rel_isdesc->is_scnndx),
103312155SAli.Bahrami@Sun.COM 			    arsp->rel_isdesc->is_name);
103412155SAli.Bahrami@Sun.COM 			return (S_ERROR);
103512155SAli.Bahrami@Sun.COM 		}
10361618Srie 
103712155SAli.Bahrami@Sun.COM 		/*
103812155SAli.Bahrami@Sun.COM 		 * Get the address of the data item we need to modify.
103912155SAli.Bahrami@Sun.COM 		 */
104012155SAli.Bahrami@Sun.COM 		addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
104112155SAli.Bahrami@Sun.COM 		    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
104212155SAli.Bahrami@Sun.COM 
104312155SAli.Bahrami@Sun.COM 		DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
104412155SAli.Bahrami@Sun.COM 		    M_MACH, SHT_REL, arsp, EC_NATPTR(addr), value,
104512155SAli.Bahrami@Sun.COM 		    ld_reloc_sym_name));
104612155SAli.Bahrami@Sun.COM 		addr += (uintptr_t)osp->os_outdata->d_buf;
10471618Srie 
104812155SAli.Bahrami@Sun.COM 		if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
104912155SAli.Bahrami@Sun.COM 		    ofl->ofl_size) || (arsp->rel_roffset >
105012155SAli.Bahrami@Sun.COM 		    osp->os_shdr->sh_size)) {
105112155SAli.Bahrami@Sun.COM 			Conv_inv_buf_t	inv_buf;
105212155SAli.Bahrami@Sun.COM 			int		class;
105312155SAli.Bahrami@Sun.COM 
105412155SAli.Bahrami@Sun.COM 			if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
105512155SAli.Bahrami@Sun.COM 			    ofl->ofl_size)
105612155SAli.Bahrami@Sun.COM 				class = ERR_FATAL;
105712155SAli.Bahrami@Sun.COM 			else
105812155SAli.Bahrami@Sun.COM 				class = ERR_WARNING;
10591618Srie 
1060*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
106112155SAli.Bahrami@Sun.COM 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
106212155SAli.Bahrami@Sun.COM 			    ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
106312155SAli.Bahrami@Sun.COM 			    arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
106412155SAli.Bahrami@Sun.COM 			    EC_ADDR((uintptr_t)addr -
106512155SAli.Bahrami@Sun.COM 			    (uintptr_t)ofl->ofl_nehdr));
106612155SAli.Bahrami@Sun.COM 
106712155SAli.Bahrami@Sun.COM 			if (class == ERR_FATAL) {
106812155SAli.Bahrami@Sun.COM 				return_code = S_ERROR;
106912155SAli.Bahrami@Sun.COM 				continue;
107012155SAli.Bahrami@Sun.COM 			}
107112155SAli.Bahrami@Sun.COM 		}
10721618Srie 
107312155SAli.Bahrami@Sun.COM 		/*
107412155SAli.Bahrami@Sun.COM 		 * The relocation is additive.  Ignore the previous symbol
107512155SAli.Bahrami@Sun.COM 		 * value if this local partial symbol is expanded.
107612155SAli.Bahrami@Sun.COM 		 */
107712155SAli.Bahrami@Sun.COM 		if (moved)
107812155SAli.Bahrami@Sun.COM 			value -= *addr;
10795892Sab196087 
108012155SAli.Bahrami@Sun.COM 		/*
108112155SAli.Bahrami@Sun.COM 		 * If we have a replacement value for the relocation
108212155SAli.Bahrami@Sun.COM 		 * target, put it in place now.
108312155SAli.Bahrami@Sun.COM 		 */
108412155SAli.Bahrami@Sun.COM 		if (arsp->rel_flags & FLG_REL_NADDEND) {
108512155SAli.Bahrami@Sun.COM 			Xword addend = arsp->rel_raddend;
108612155SAli.Bahrami@Sun.COM 
108712155SAli.Bahrami@Sun.COM 			if (ld_reloc_targval_set(ofl, arsp, addr, addend) == 0)
108812155SAli.Bahrami@Sun.COM 				return (S_ERROR);
108912155SAli.Bahrami@Sun.COM 		}
109012155SAli.Bahrami@Sun.COM 
109112155SAli.Bahrami@Sun.COM 		/*
109212155SAli.Bahrami@Sun.COM 		 * If '-z noreloc' is specified - skip the do_reloc_ld stage.
109312155SAli.Bahrami@Sun.COM 		 */
109412155SAli.Bahrami@Sun.COM 		if (OFL_DO_RELOC(ofl)) {
109512155SAli.Bahrami@Sun.COM 			if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
109612155SAli.Bahrami@Sun.COM 			    ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
1097*13074SAli.Bahrami@Oracle.COM 			    ofl->ofl_lml) == 0) {
1098*13074SAli.Bahrami@Oracle.COM 				ofl->ofl_flags |= FLG_OF_FATAL;
109912155SAli.Bahrami@Sun.COM 				return_code = S_ERROR;
1100*13074SAli.Bahrami@Oracle.COM 			}
11011618Srie 		}
11021618Srie 	}
11031618Srie 	return (return_code);
11041618Srie }
11051618Srie 
11061618Srie /*
11071618Srie  * Add an output relocation record.
11081618Srie  */
11096206Sab196087 static uintptr_t
ld_add_outrel(Word flags,Rel_desc * rsp,Ofl_desc * ofl)11101618Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
11111618Srie {
11121618Srie 	Rel_desc	*orsp;
11131618Srie 	Sym_desc	*sdp = rsp->rel_sym;
11141618Srie 
11151618Srie 	/*
11161618Srie 	 * Static executables *do not* want any relocations against them.
11171618Srie 	 * Since our engine still creates relocations against a WEAK UNDEFINED
11181618Srie 	 * symbol in a static executable, it's best to disable them here
11191618Srie 	 * instead of through out the relocation code.
11201618Srie 	 */
112110792SRod.Evans@Sun.COM 	if (OFL_IS_STATIC_EXEC(ofl))
11221618Srie 		return (1);
11231618Srie 
11241618Srie 	/*
11251618Srie 	 * If we are adding a output relocation against a section
11261618Srie 	 * symbol (non-RELATIVE) then mark that section.  These sections
11271618Srie 	 * will be added to the .dynsym symbol table.
11281618Srie 	 */
11291618Srie 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
11301618Srie 	    ((flags & FLG_REL_SCNNDX) ||
11311618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
11321618Srie 
11331618Srie 		/*
11341618Srie 		 * If this is a COMMON symbol - no output section
11351618Srie 		 * exists yet - (it's created as part of sym_validate()).
11361618Srie 		 * So - we mark here that when it's created it should
11371618Srie 		 * be tagged with the FLG_OS_OUTREL flag.
11381618Srie 		 */
11391618Srie 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
11401682Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
11411618Srie 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
11421618Srie 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
11431618Srie 			else
11441618Srie 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
11451618Srie 		} else {
114611827SRod.Evans@Sun.COM 			Os_desc *osp;
114711827SRod.Evans@Sun.COM 			Is_desc *isp = sdp->sd_isc;
11481618Srie 
114911827SRod.Evans@Sun.COM 			if (isp && ((osp = isp->is_osdesc) != NULL) &&
115011827SRod.Evans@Sun.COM 			    ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11511618Srie 				ofl->ofl_dynshdrcnt++;
11521618Srie 				osp->os_flags |= FLG_OS_OUTREL;
11531618Srie 			}
11541618Srie 		}
11551618Srie 	}
11561618Srie 
115712155SAli.Bahrami@Sun.COM 	/* Enter it into the output relocation cache */
115812155SAli.Bahrami@Sun.COM 	if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
115912155SAli.Bahrami@Sun.COM 		return (S_ERROR);
11601618Srie 
11611618Srie 	if (flags & FLG_REL_GOT)
11621618Srie 		ofl->ofl_relocgotsz += (Xword)sizeof (Rel);
11631618Srie 	else if (flags & FLG_REL_PLT)
11641618Srie 		ofl->ofl_relocpltsz += (Xword)sizeof (Rel);
11651618Srie 	else if (flags & FLG_REL_BSS)
11661618Srie 		ofl->ofl_relocbsssz += (Xword)sizeof (Rel);
11671618Srie 	else if (flags & FLG_REL_NOINFO)
11681618Srie 		ofl->ofl_relocrelsz += (Xword)sizeof (Rel);
11691618Srie 	else
117012155SAli.Bahrami@Sun.COM 		RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rel);
11711618Srie 
11721618Srie 	if (orsp->rel_rtype == M_R_RELATIVE)
11731618Srie 		ofl->ofl_relocrelcnt++;
11741618Srie 
11751618Srie 	/*
11761618Srie 	 * We don't perform sorting on PLT relocations because
11771618Srie 	 * they have already been assigned a PLT index and if we
11781618Srie 	 * were to sort them we would have to re-assign the plt indexes.
11791618Srie 	 */
11801618Srie 	if (!(flags & FLG_REL_PLT))
11811618Srie 		ofl->ofl_reloccnt++;
11821618Srie 
11831618Srie 	/*
11841618Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
11851618Srie 	 */
11861618Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
11871618Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
11881618Srie 
11891618Srie 	/*
11901618Srie 	 * Identify and possibly warn of a displacement relocation.
11911618Srie 	 */
11921618Srie 	if (orsp->rel_flags & FLG_REL_DISP) {
11931618Srie 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
11941618Srie 
11951618Srie 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
11961618Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
11971618Srie 	}
11981618Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL,
11991618Srie 	    M_MACH, orsp));
12001618Srie 	return (1);
12011618Srie }
12021618Srie 
12031618Srie /*
12041618Srie  * process relocation for a LOCAL symbol
12051618Srie  */
12066206Sab196087 static uintptr_t
ld_reloc_local(Rel_desc * rsp,Ofl_desc * ofl)12071618Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
12081618Srie {
12096299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
12101618Srie 	Sym_desc	*sdp = rsp->rel_sym;
12111682Srie 	Word		shndx = sdp->sd_sym->st_shndx;
12121618Srie 
12131618Srie 	/*
12141618Srie 	 * if ((shared object) and (not pc relative relocation) and
12151618Srie 	 *    (not against ABS symbol))
12161618Srie 	 * then
12171618Srie 	 *	build R_386_RELATIVE
12181618Srie 	 * fi
12191618Srie 	 */
12201618Srie 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
12212850Srie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
12221618Srie 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
12231618Srie 	    !(rsp->rel_isdesc != NULL &&
12241618Srie 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
12251618Srie 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
12261618Srie 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
12271618Srie 		Word	ortype = rsp->rel_rtype;
12281618Srie 
12291618Srie 		rsp->rel_rtype = R_386_RELATIVE;
12301618Srie 		if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR)
12311618Srie 			return (S_ERROR);
12321618Srie 		rsp->rel_rtype = ortype;
12331618Srie 	}
12341618Srie 
12351618Srie 	/*
12361618Srie 	 * If the relocation is against a 'non-allocatable' section
12371618Srie 	 * and we can not resolve it now - then give a warning
12381618Srie 	 * message.
12391618Srie 	 *
12401618Srie 	 * We can not resolve the symbol if either:
12411618Srie 	 *	a) it's undefined
12421618Srie 	 *	b) it's defined in a shared library and a
12431618Srie 	 *	   COPY relocation hasn't moved it to the executable
12441618Srie 	 *
12451618Srie 	 * Note: because we process all of the relocations against the
12461618Srie 	 *	text segment before any others - we know whether
12471618Srie 	 *	or not a copy relocation will be generated before
12481618Srie 	 *	we get here (see reloc_init()->reloc_segments()).
12491618Srie 	 */
12501618Srie 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
12511618Srie 	    ((shndx == SHN_UNDEF) ||
12521618Srie 	    ((sdp->sd_ref == REF_DYN_NEED) &&
12531618Srie 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
125412155SAli.Bahrami@Sun.COM 		Conv_inv_buf_t	inv_buf;
125512155SAli.Bahrami@Sun.COM 		Os_desc		*osp = RELAUX_GET_OSDESC(rsp);
12564734Sab196087 
12571618Srie 		/*
12581618Srie 		 * If the relocation is against a SHT_SUNW_ANNOTATE
12591618Srie 		 * section - then silently ignore that the relocation
12601618Srie 		 * can not be resolved.
12611618Srie 		 */
126212155SAli.Bahrami@Sun.COM 		if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
12631618Srie 			return (0);
1264*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
12654734Sab196087 		    conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf),
12661618Srie 		    rsp->rel_isdesc->is_file->ifl_name,
126712155SAli.Bahrami@Sun.COM 		    ld_reloc_sym_name(rsp), osp->os_name);
12681618Srie 		return (1);
12691618Srie 	}
12701618Srie 
12711618Srie 	/*
12721618Srie 	 * Perform relocation.
12731618Srie 	 */
12741618Srie 	return (ld_add_actrel(NULL, rsp, ofl));
12751618Srie }
12761618Srie 
12776206Sab196087 static uintptr_t
ld_reloc_TLS(Boolean local,Rel_desc * rsp,Ofl_desc * ofl)12781618Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12791618Srie {
12801618Srie 	Word		rtype = rsp->rel_rtype;
12811618Srie 	Sym_desc	*sdp = rsp->rel_sym;
12826299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
12831618Srie 	Gotndx		*gnp;
12841618Srie 
12851618Srie 	/*
12862145Srie 	 * If we're building an executable - use either the IE or LE access
12872145Srie 	 * model.  If we're building a shared object process any IE model.
12881618Srie 	 */
12892145Srie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
12901618Srie 		/*
12912145Srie 		 * Set the DF_STATIC_TLS flag.
12921618Srie 		 */
12931618Srie 		ofl->ofl_dtflags |= DF_STATIC_TLS;
12941618Srie 
12952145Srie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
12961618Srie 			/*
12972145Srie 			 * Assign a GOT entry for static TLS references.
12981618Srie 			 */
12999131SRod.Evans@Sun.COM 			if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
13009131SRod.Evans@Sun.COM 			    GOT_REF_TLSIE, ofl, NULL)) == NULL) {
13012145Srie 
13022145Srie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
13032145Srie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
13049131SRod.Evans@Sun.COM 				    rtype, R_386_TLS_TPOFF, NULL) == S_ERROR)
13052145Srie 					return (S_ERROR);
13061618Srie 			}
13071618Srie 
13082145Srie 			/*
13092145Srie 			 * IE access model.
13102145Srie 			 */
13112145Srie 			if (IS_TLS_IE(rtype)) {
13122145Srie 				if (ld_add_actrel(FLG_REL_STLS,
13132145Srie 				    rsp, ofl) == S_ERROR)
13142145Srie 					return (S_ERROR);
13152145Srie 
13162145Srie 				/*
13172145Srie 				 * A non-pic shared object needs to adjust the
13182145Srie 				 * active relocation (indntpoff).
13192145Srie 				 */
13202145Srie 				if (((flags & FLG_OF_EXEC) == 0) &&
13212145Srie 				    (rtype == R_386_TLS_IE)) {
13222145Srie 					rsp->rel_rtype = R_386_RELATIVE;
13232145Srie 					return (ld_add_outrel(NULL, rsp, ofl));
13242145Srie 				}
13252145Srie 				return (1);
13262145Srie 			}
13271618Srie 
13281618Srie 			/*
13292145Srie 			 * Fixups are required for other executable models.
13301618Srie 			 */
13311618Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13321618Srie 			    rsp, ofl));
13331618Srie 		}
13342145Srie 
13351618Srie 		/*
13362145Srie 		 * LE access model.
13371618Srie 		 */
13381618Srie 		if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32))
13391618Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
13401618Srie 
13411618Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13421618Srie 		    rsp, ofl));
13431618Srie 	}
13441618Srie 
13451618Srie 	/*
13462145Srie 	 * Building a shared object.
13472145Srie 	 *
13482145Srie 	 * Assign a GOT entry for a dynamic TLS reference.
13491618Srie 	 */
13509131SRod.Evans@Sun.COM 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
13519131SRod.Evans@Sun.COM 	    GOT_REF_TLSLD, ofl, NULL)) == NULL)) {
13522145Srie 
13532145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
13549131SRod.Evans@Sun.COM 		    FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, NULL) == S_ERROR)
13552145Srie 			return (S_ERROR);
13562145Srie 
13579131SRod.Evans@Sun.COM 	} else if (IS_TLS_GD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
13589131SRod.Evans@Sun.COM 	    GOT_REF_TLSGD, ofl, NULL)) == NULL)) {
13592145Srie 
13602145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
13612145Srie 		    FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32,
13622145Srie 		    R_386_TLS_DTPOFF32) == S_ERROR)
13632145Srie 			return (S_ERROR);
13641618Srie 	}
13651618Srie 
13661618Srie 	/*
13671618Srie 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
13682145Srie 	 * cause a call to __tls_get_addr().  Convert this relocation to that
13692145Srie 	 * symbol now, and prepare for the PLT magic.
13701618Srie 	 */
13711618Srie 	if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) {
13723862Srie 		Sym_desc	*tlsgetsym;
13731618Srie 
13741618Srie 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU),
13753862Srie 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
13761618Srie 			return (S_ERROR);
13772145Srie 
13781618Srie 		rsp->rel_sym = tlsgetsym;
13791618Srie 		rsp->rel_rtype = R_386_PLT32;
13802145Srie 
13811618Srie 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
13821618Srie 			return (S_ERROR);
13832145Srie 
13841618Srie 		rsp->rel_sym = sdp;
13851618Srie 		rsp->rel_rtype = rtype;
13861618Srie 		return (1);
13871618Srie 	}
13881618Srie 
13891618Srie 	if (IS_TLS_LD(rtype))
13901618Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
13911618Srie 
13921618Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
13931618Srie }
13941618Srie 
13951618Srie /* ARGSUSED4 */
13966206Sab196087 static uintptr_t
ld_assign_got_ndx(Alist ** alpp,Gotndx * pgnp,Gotref gref,Ofl_desc * ofl,Rel_desc * rsp,Sym_desc * sdp)13979131SRod.Evans@Sun.COM ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
13989131SRod.Evans@Sun.COM     Rel_desc *rsp, Sym_desc *sdp)
13991618Srie {
14009131SRod.Evans@Sun.COM 	Gotndx	gn, *gnp;
14011618Srie 	uint_t	gotents;
14021618Srie 
14031618Srie 	if (pgnp)
14041618Srie 		return (1);
14051618Srie 
14061618Srie 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
14071618Srie 		gotents = 2;
14081618Srie 	else
14091618Srie 		gotents = 1;
14101618Srie 
14119131SRod.Evans@Sun.COM 	gn.gn_addend = 0;
14129131SRod.Evans@Sun.COM 	gn.gn_gotndx = ofl->ofl_gotcnt;
14139131SRod.Evans@Sun.COM 	gn.gn_gotref = gref;
14141618Srie 
14151618Srie 	ofl->ofl_gotcnt += gotents;
14161618Srie 
14171618Srie 	if (gref == GOT_REF_TLSLD) {
14189131SRod.Evans@Sun.COM 		if (ofl->ofl_tlsldgotndx == NULL) {
14199131SRod.Evans@Sun.COM 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
14209131SRod.Evans@Sun.COM 				return (S_ERROR);
14219131SRod.Evans@Sun.COM 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
14229131SRod.Evans@Sun.COM 			ofl->ofl_tlsldgotndx = gnp;
14239131SRod.Evans@Sun.COM 		}
14241618Srie 		return (1);
14251618Srie 	}
14261618Srie 
14279131SRod.Evans@Sun.COM 	/*
14289131SRod.Evans@Sun.COM 	 * GOT indexes are maintained on an Alist, where there is typically
14299131SRod.Evans@Sun.COM 	 * only one index.  The usage of this list is to scan the list to find
14309131SRod.Evans@Sun.COM 	 * an index, and then apply that index immediately to a relocation.
14319131SRod.Evans@Sun.COM 	 * Thus there are no external references to these GOT index structures
14329131SRod.Evans@Sun.COM 	 * that can be compromised by the Alist being reallocated.
14339131SRod.Evans@Sun.COM 	 */
14349131SRod.Evans@Sun.COM 	if (alist_append(alpp, &gn, sizeof (Gotndx), AL_CNT_SDP_GOT) == NULL)
14351618Srie 		return (S_ERROR);
14361618Srie 
14371618Srie 	return (1);
14381618Srie }
14391618Srie 
14406206Sab196087 static void
ld_assign_plt_ndx(Sym_desc * sdp,Ofl_desc * ofl)14411618Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14421618Srie {
14431618Srie 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14441618Srie 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
14451618Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14461618Srie }
14471618Srie 
14481618Srie /*
14491618Srie  * Initializes .got[0] with the _DYNAMIC symbol value.
14501618Srie  */
14516206Sab196087 static uintptr_t
ld_fillin_gotplt(Ofl_desc * ofl)14522145Srie ld_fillin_gotplt(Ofl_desc *ofl)
14531618Srie {
14546299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
14556299Sab196087 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
14562145Srie 
14571618Srie 	if (ofl->ofl_osgot) {
14582145Srie 		Sym_desc	*sdp;
14591618Srie 
14601618Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
146110792SRod.Evans@Sun.COM 		    SYM_NOHASH, NULL, ofl)) != NULL) {
14622145Srie 			uchar_t	*genptr;
14632145Srie 
14642145Srie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
14651618Srie 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
14661618Srie 			/* LINTED */
14671618Srie 			*(Word *)genptr = (Word)sdp->sd_sym->st_value;
14686206Sab196087 			if (bswap)
14696206Sab196087 				/* LINTED */
14706206Sab196087 				*(Word *)genptr =
14716206Sab196087 				    /* LINTED */
14726206Sab196087 				    ld_bswap_Word(*(Word *)genptr);
14731618Srie 		}
14741618Srie 	}
14751618Srie 
14761618Srie 	/*
14771618Srie 	 * Fill in the reserved slot in the procedure linkage table the first
14781618Srie 	 * entry is:
14791618Srie 	 *  if (building a.out) {
14801618Srie 	 *	PUSHL	got[1]		    # the address of the link map entry
14811618Srie 	 *	JMP *	got[2]		    # the address of rtbinder
14821618Srie 	 *  } else {
14831618Srie 	 *	PUSHL	got[1]@GOT(%ebx)    # the address of the link map entry
14841618Srie 	 *	JMP *	got[2]@GOT(%ebx)    # the address of rtbinder
14851618Srie 	 *  }
14861618Srie 	 */
14872145Srie 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
14881618Srie 		uchar_t *pltent;
14891618Srie 
14901618Srie 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
14912145Srie 		if (!(flags & FLG_OF_SHAROBJ)) {
14921618Srie 			pltent[0] = M_SPECIAL_INST;
14931618Srie 			pltent[1] = M_PUSHL_DISP;
14941618Srie 			pltent += 2;
14951618Srie 			/* LINTED */
14961618Srie 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
14974734Sab196087 			    sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE);
14986206Sab196087 			if (bswap)
14996206Sab196087 				/* LINTED */
15006206Sab196087 				*(Word *)pltent =
15016206Sab196087 				    /* LINTED */
15026206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15031618Srie 			pltent += 4;
15041618Srie 			pltent[0] = M_SPECIAL_INST;
15051618Srie 			pltent[1] = M_JMP_DISP_IND;
15061618Srie 			pltent += 2;
15071618Srie 			/* LINTED */
15081618Srie 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
15094734Sab196087 			    sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE);
15106206Sab196087 			if (bswap)
15116206Sab196087 				/* LINTED */
15126206Sab196087 				*(Word *)pltent =
15136206Sab196087 				    /* LINTED */
15146206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15151618Srie 		} else {
15161618Srie 			pltent[0] = M_SPECIAL_INST;
15171618Srie 			pltent[1] = M_PUSHL_REG_DISP;
15181618Srie 			pltent += 2;
15191618Srie 			/* LINTED */
15201618Srie 			*(Word *)pltent = (Word)(M_GOT_XLINKMAP *
15214734Sab196087 			    M_GOT_ENTSIZE);
15226206Sab196087 			if (bswap)
15236206Sab196087 				/* LINTED */
15246206Sab196087 				*(Word *)pltent =
15256206Sab196087 				    /* LINTED */
15266206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15271618Srie 			pltent += 4;
15281618Srie 			pltent[0] = M_SPECIAL_INST;
15291618Srie 			pltent[1] = M_JMP_REG_DISP_IND;
15301618Srie 			pltent += 2;
15311618Srie 			/* LINTED */
15321618Srie 			*(Word *)pltent = (Word)(M_GOT_XRTLD *
15334734Sab196087 			    M_GOT_ENTSIZE);
15346206Sab196087 			if (bswap)
15356206Sab196087 				/* LINTED */
15366206Sab196087 				*(Word *)pltent =
15376206Sab196087 				    /* LINTED */
15386206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15391618Srie 		}
15401618Srie 	}
15411618Srie 	return (1);
15421618Srie }
15436206Sab196087 
15446206Sab196087 
15456206Sab196087 
15466206Sab196087 /*
15476206Sab196087  * Template for generating "void (*)(void)" function
15486206Sab196087  */
15496206Sab196087 static const uchar_t nullfunc_tmpl[] = {	/* IA32 */
15506206Sab196087 /* 0x00 */	0xc3				/* ret */
15516206Sab196087 };
15526206Sab196087 
15536206Sab196087 
15546206Sab196087 
15556206Sab196087 /*
155610809SAli.Bahrami@Sun.COM  * Function used to provide fill padding in SHF_EXECINSTR sections
155710809SAli.Bahrami@Sun.COM  *
155810809SAli.Bahrami@Sun.COM  * entry:
155910809SAli.Bahrami@Sun.COM  *
156010809SAli.Bahrami@Sun.COM  *	base - base address of section being filled
156110809SAli.Bahrami@Sun.COM  *	offset - starting offset for fill within memory referenced by base
156210809SAli.Bahrami@Sun.COM  *	cnt - # bytes to be filled
156310809SAli.Bahrami@Sun.COM  *
156410809SAli.Bahrami@Sun.COM  * exit:
156510809SAli.Bahrami@Sun.COM  *	The fill has been completed.
156610809SAli.Bahrami@Sun.COM  */
156710809SAli.Bahrami@Sun.COM static void
execfill(void * base,off_t off,size_t cnt)156810809SAli.Bahrami@Sun.COM execfill(void *base, off_t off, size_t cnt)
156910809SAli.Bahrami@Sun.COM {
157010809SAli.Bahrami@Sun.COM 	/*
157110809SAli.Bahrami@Sun.COM 	 * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds.
157210809SAli.Bahrami@Sun.COM 	 * There are no alignment constraints.
157310809SAli.Bahrami@Sun.COM 	 */
157410809SAli.Bahrami@Sun.COM 	(void) memset(off + (char *)base, 0x90, cnt);
157510809SAli.Bahrami@Sun.COM }
157610809SAli.Bahrami@Sun.COM 
157710809SAli.Bahrami@Sun.COM 
157810809SAli.Bahrami@Sun.COM /*
15796206Sab196087  * Return the ld_targ definition for this target.
15806206Sab196087  */
15816206Sab196087 const Target *
ld_targ_init_x86(void)15826206Sab196087 ld_targ_init_x86(void)
15836206Sab196087 {
15846206Sab196087 	static const Target _ld_targ = {
15856206Sab196087 		{			/* Target_mach */
15866206Sab196087 			M_MACH,			/* m_mach */
15876206Sab196087 			M_MACHPLUS,		/* m_machplus */
15886206Sab196087 			M_FLAGSPLUS,		/* m_flagsplus */
15896206Sab196087 			M_CLASS,		/* m_class */
15906206Sab196087 			M_DATA,			/* m_data */
15916206Sab196087 
15926206Sab196087 			M_SEGM_ALIGN,		/* m_segm_align */
15936206Sab196087 			M_SEGM_ORIGIN,		/* m_segm_origin */
15948501SRod.Evans@Sun.COM 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
15956206Sab196087 			M_DATASEG_PERM,		/* m_dataseg_perm */
159611734SAli.Bahrami@Sun.COM 			M_STACK_PERM,		/* m_stack_perm */
15976206Sab196087 			M_WORD_ALIGN,		/* m_word_align */
15986206Sab196087 			MSG_ORIG(MSG_PTH_RTLD),	/* m_def_interp */
15996206Sab196087 
16006206Sab196087 			/* Relocation type codes */
16016206Sab196087 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
16026206Sab196087 			M_R_COPY,		/* m_r_copy */
16036206Sab196087 			M_R_GLOB_DAT,		/* m_r_glob_dat */
16046206Sab196087 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
16056206Sab196087 			M_R_NUM,		/* m_r_num */
16066206Sab196087 			M_R_NONE,		/* m_r_none */
16076206Sab196087 			M_R_RELATIVE,		/* m_r_relative */
16086206Sab196087 			M_R_REGISTER,		/* m_r_register */
16096206Sab196087 
16106206Sab196087 			/* Relocation related constants */
16116206Sab196087 			M_REL_DT_COUNT,		/* m_rel_dt_count */
16126206Sab196087 			M_REL_DT_ENT,		/* m_rel_dt_ent */
16136206Sab196087 			M_REL_DT_SIZE,		/* m_rel_dt_size */
16146206Sab196087 			M_REL_DT_TYPE,		/* m_rel_dt_type */
16156206Sab196087 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
16166206Sab196087 
16176206Sab196087 			/* GOT related constants */
16186206Sab196087 			M_GOT_ENTSIZE,		/* m_got_entsize */
16196206Sab196087 			M_GOT_XNumber,		/* m_got_xnumber */
16206206Sab196087 
16216206Sab196087 			/* PLT related constants */
16226206Sab196087 			M_PLT_ALIGN,		/* m_plt_align */
16236206Sab196087 			M_PLT_ENTSIZE,		/* m_plt_entsize */
16246206Sab196087 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
16256206Sab196087 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
16266206Sab196087 
16279085SAli.Bahrami@Sun.COM 			/* Section type of .eh_frame/.eh_frame_hdr sections */
16289085SAli.Bahrami@Sun.COM 			SHT_PROGBITS,		/* m_sht_unwind */
16299085SAli.Bahrami@Sun.COM 
16306206Sab196087 			M_DT_REGISTER,		/* m_dt_register */
16316206Sab196087 		},
16326206Sab196087 		{			/* Target_machid */
16336206Sab196087 			M_ID_ARRAY,		/* id_array */
16346206Sab196087 			M_ID_BSS,		/* id_bss */
16356206Sab196087 			M_ID_CAP,		/* id_cap */
163611827SRod.Evans@Sun.COM 			M_ID_CAPINFO,		/* id_capinfo */
163711827SRod.Evans@Sun.COM 			M_ID_CAPCHAIN,		/* id_capchain */
16386206Sab196087 			M_ID_DATA,		/* id_data */
16396206Sab196087 			M_ID_DYNAMIC,		/* id_dynamic */
16406206Sab196087 			M_ID_DYNSORT,		/* id_dynsort */
16416206Sab196087 			M_ID_DYNSTR,		/* id_dynstr */
16426206Sab196087 			M_ID_DYNSYM,		/* id_dynsym */
16436206Sab196087 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
16446206Sab196087 			M_ID_GOT,		/* id_got */
16456206Sab196087 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
16466206Sab196087 			M_ID_HASH,		/* id_hash */
16476206Sab196087 			M_ID_INTERP,		/* id_interp */
16486206Sab196087 			M_ID_LBSS,		/* id_lbss */
16496206Sab196087 			M_ID_LDYNSYM,		/* id_ldynsym */
16506206Sab196087 			M_ID_NOTE,		/* id_note */
16516206Sab196087 			M_ID_NULL,		/* id_null */
16526206Sab196087 			M_ID_PLT,		/* id_plt */
16536206Sab196087 			M_ID_REL,		/* id_rel */
16546206Sab196087 			M_ID_STRTAB,		/* id_strtab */
16556206Sab196087 			M_ID_SYMINFO,		/* id_syminfo */
16566206Sab196087 			M_ID_SYMTAB,		/* id_symtab */
16576206Sab196087 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
16586206Sab196087 			M_ID_TEXT,		/* id_text */
16596206Sab196087 			M_ID_TLS,		/* id_tls */
16606206Sab196087 			M_ID_TLSBSS,		/* id_tlsbss */
16616206Sab196087 			M_ID_UNKNOWN,		/* id_unknown */
16626206Sab196087 			M_ID_UNWIND,		/* id_unwind */
16639085SAli.Bahrami@Sun.COM 			M_ID_UNWINDHDR,		/* id_unwindhdr */
16646206Sab196087 			M_ID_USER,		/* id_user */
16656206Sab196087 			M_ID_VERSION,		/* id_version */
16666206Sab196087 		},
16676206Sab196087 		{			/* Target_nullfunc */
16686206Sab196087 			nullfunc_tmpl,		/* nf_template */
16696206Sab196087 			sizeof (nullfunc_tmpl),	/* nf_size */
16706206Sab196087 		},
167110809SAli.Bahrami@Sun.COM 		{			/* Target_fillfunc */
167210809SAli.Bahrami@Sun.COM 			execfill		/* ff_execfill */
167310809SAli.Bahrami@Sun.COM 		},
16746206Sab196087 		{			/* Target_machrel */
16756206Sab196087 			reloc_table,
16766206Sab196087 
16776206Sab196087 			ld_init_rel,		/* mr_init_rel */
16786206Sab196087 			ld_mach_eflags,		/* mr_mach_eflags */
16796206Sab196087 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
16806206Sab196087 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
16816206Sab196087 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
16826206Sab196087 			ld_perform_outreloc,	/* mr_perform_outreloc */
16836206Sab196087 			ld_do_activerelocs,	/* mr_do_activerelocs */
16846206Sab196087 			ld_add_outrel,		/* mr_add_outrel */
16856206Sab196087 			NULL,			/* mr_reloc_register */
16866206Sab196087 			ld_reloc_local,		/* mr_reloc_local */
16876206Sab196087 			NULL,			/* mr_reloc_GOTOP */
16886206Sab196087 			ld_reloc_TLS,		/* mr_reloc_TLS */
16896206Sab196087 			NULL,			/* mr_assign_got */
16909131SRod.Evans@Sun.COM 			ld_find_got_ndx,	/* mr_find_got_ndx */
16916206Sab196087 			ld_calc_got_offset,	/* mr_calc_got_offset */
16926206Sab196087 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
16936206Sab196087 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
16946206Sab196087 			NULL,			/* mr_allocate_got */
16956206Sab196087 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
16966206Sab196087 		},
16976206Sab196087 		{			/* Target_machsym */
16986206Sab196087 			NULL,			/* ms_reg_check */
16996206Sab196087 			NULL,			/* ms_mach_sym_typecheck */
17006206Sab196087 			NULL,			/* ms_is_regsym */
17016206Sab196087 			NULL,			/* ms_reg_find */
17026206Sab196087 			NULL			/* ms_reg_enter */
17036206Sab196087 		}
17046206Sab196087 	};
17056206Sab196087 
17066206Sab196087 	return (&_ld_targ);
17076206Sab196087 }
1708