xref: /titanic_50/usr/src/cmd/sgs/libld/common/machrel.intel.c (revision 6a634c9dca3093f3922e4b7ab826d7bdf17bf78e)
15aefb655Srie /*
25aefb655Srie  * CDDL HEADER START
35aefb655Srie  *
45aefb655Srie  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
75aefb655Srie  *
85aefb655Srie  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95aefb655Srie  * or http://www.opensolaris.org/os/licensing.
105aefb655Srie  * See the License for the specific language governing permissions
115aefb655Srie  * and limitations under the License.
125aefb655Srie  *
135aefb655Srie  * When distributing Covered Code, include this CDDL HEADER in each
145aefb655Srie  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155aefb655Srie  * If applicable, add the following below this CDDL HEADER, with the
165aefb655Srie  * fields enclosed by brackets "[]" replaced with your own identifying
175aefb655Srie  * information: Portions Copyright [yyyy] [name of copyright owner]
185aefb655Srie  *
195aefb655Srie  * CDDL HEADER END
205aefb655Srie  */
215aefb655Srie 
225aefb655Srie /*
235aefb655Srie  *	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.
245aefb655Srie  *	Copyright (c) 1988 AT&T
255aefb655Srie  *	  All Rights Reserved
265aefb655Srie  *
27bf994817SAli Bahrami  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
285aefb655Srie  */
295aefb655Srie 
30ba2be530Sab196087 /* Get the x86 version of the relocation engine */
31ba2be530Sab196087 #define	DO_RELOC_LIBLD_X86
32ba2be530Sab196087 
335aefb655Srie #include	<string.h>
345aefb655Srie #include	<stdio.h>
355aefb655Srie #include	<sys/elf_386.h>
365aefb655Srie #include	<debug.h>
375aefb655Srie #include	<reloc.h>
38ba2be530Sab196087 #include	<i386/machdep_x86.h>
395aefb655Srie #include	"msg.h"
405aefb655Srie #include	"_libld.h"
415aefb655Srie 
4257ef7aa9SRod Evans /*
4357ef7aa9SRod Evans  * Search the GOT index list for a GOT entry with a matching reference.
4457ef7aa9SRod Evans  */
4557ef7aa9SRod Evans /* ARGSUSED3 */
4657ef7aa9SRod Evans static Gotndx *
ld_find_got_ndx(Alist * alp,Gotref gref,Ofl_desc * ofl,Rel_desc * rdesc)4757ef7aa9SRod Evans ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc)
4857ef7aa9SRod Evans {
4957ef7aa9SRod Evans 	Aliste	idx;
5057ef7aa9SRod Evans 	Gotndx	*gnp;
51ba2be530Sab196087 
5257ef7aa9SRod Evans 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
5357ef7aa9SRod Evans 		return (ofl->ofl_tlsldgotndx);
5457ef7aa9SRod Evans 
5557ef7aa9SRod Evans 	for (ALIST_TRAVERSE(alp, idx, gnp)) {
5657ef7aa9SRod Evans 		if (gnp->gn_gotref == gref)
5757ef7aa9SRod Evans 			return (gnp);
5857ef7aa9SRod Evans 	}
5957ef7aa9SRod Evans 	return (NULL);
6057ef7aa9SRod Evans }
6157ef7aa9SRod Evans 
6257ef7aa9SRod Evans static Xword
ld_calc_got_offset(Rel_desc * rdesc,Ofl_desc * ofl)6357ef7aa9SRod Evans ld_calc_got_offset(Rel_desc *rdesc, Ofl_desc *ofl)
6457ef7aa9SRod Evans {
6557ef7aa9SRod Evans 	Os_desc		*osp = ofl->ofl_osgot;
6657ef7aa9SRod Evans 	Sym_desc	*sdp = rdesc->rel_sym;
6757ef7aa9SRod Evans 	Xword		gotndx;
6857ef7aa9SRod Evans 	Gotref		gref;
6957ef7aa9SRod Evans 	Gotndx		*gnp;
7057ef7aa9SRod Evans 
7157ef7aa9SRod Evans 	if (rdesc->rel_flags & FLG_REL_DTLS)
7257ef7aa9SRod Evans 		gref = GOT_REF_TLSGD;
7357ef7aa9SRod Evans 	else if (rdesc->rel_flags & FLG_REL_MTLS)
7457ef7aa9SRod Evans 		gref = GOT_REF_TLSLD;
7557ef7aa9SRod Evans 	else if (rdesc->rel_flags & FLG_REL_STLS)
7657ef7aa9SRod Evans 		gref = GOT_REF_TLSIE;
7757ef7aa9SRod Evans 	else
7857ef7aa9SRod Evans 		gref = GOT_REF_GENERIC;
7957ef7aa9SRod Evans 
8057ef7aa9SRod Evans 	gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
8157ef7aa9SRod Evans 	assert(gnp);
8257ef7aa9SRod Evans 
8357ef7aa9SRod Evans 	gotndx = (Xword)gnp->gn_gotndx;
8457ef7aa9SRod Evans 
8557ef7aa9SRod Evans 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
8657ef7aa9SRod Evans 	    (rdesc->rel_rtype == R_386_TLS_DTPOFF32))
8757ef7aa9SRod Evans 		gotndx++;
8857ef7aa9SRod Evans 
8957ef7aa9SRod Evans 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
9057ef7aa9SRod Evans }
91ba2be530Sab196087 
92ba2be530Sab196087 static Word
ld_init_rel(Rel_desc * reld,Word * typedata,void * reloc)93bf994817SAli Bahrami ld_init_rel(Rel_desc *reld, Word *typedata, void *reloc)
945aefb655Srie {
955aefb655Srie 	Rel	*rel = (Rel *)reloc;
965aefb655Srie 
975aefb655Srie 	/* LINTED */
98ba2be530Sab196087 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
995aefb655Srie 	reld->rel_roffset = rel->r_offset;
1005aefb655Srie 	reld->rel_raddend = 0;
101bf994817SAli Bahrami 	*typedata = 0;
1025aefb655Srie 
1035aefb655Srie 	return ((Word)ELF_R_SYM(rel->r_info));
1045aefb655Srie }
1055aefb655Srie 
106ba2be530Sab196087 static void
ld_mach_eflags(Ehdr * ehdr,Ofl_desc * ofl)1075aefb655Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
1085aefb655Srie {
1095aefb655Srie 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
1105aefb655Srie }
1115aefb655Srie 
112ba2be530Sab196087 static void
ld_mach_make_dynamic(Ofl_desc * ofl,size_t * cnt)1135aefb655Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
1145aefb655Srie {
1155aefb655Srie 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
1165aefb655Srie 		/*
1175aefb655Srie 		 * Create this entry if we are going to create a PLT table.
1185aefb655Srie 		 */
1195aefb655Srie 		if (ofl->ofl_pltcnt)
1205aefb655Srie 			(*cnt)++;		/* DT_PLTGOT */
1215aefb655Srie 	}
1225aefb655Srie }
1235aefb655Srie 
124ba2be530Sab196087 static void
ld_mach_update_odynamic(Ofl_desc * ofl,Dyn ** dyn)1255aefb655Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
1265aefb655Srie {
1275aefb655Srie 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
1285aefb655Srie 		(*dyn)->d_tag = DT_PLTGOT;
1295aefb655Srie 		if (ofl->ofl_osgot)
1305aefb655Srie 			(*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
1315aefb655Srie 		else
1325aefb655Srie 			(*dyn)->d_un.d_ptr = 0;
1335aefb655Srie 		(*dyn)++;
1345aefb655Srie 	}
1355aefb655Srie }
1365aefb655Srie 
137ba2be530Sab196087 static Xword
ld_calc_plt_addr(Sym_desc * sdp,Ofl_desc * ofl)1385aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
1395aefb655Srie {
1405aefb655Srie 	Xword	value;
1415aefb655Srie 
1425aefb655Srie 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
1435aefb655Srie 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
1445aefb655Srie 	return (value);
1455aefb655Srie }
1465aefb655Srie 
1475aefb655Srie /*
1485aefb655Srie  *  Build a single plt entry - code is:
1495aefb655Srie  *	if (building a.out)
1505aefb655Srie  *		JMP	*got_off
1515aefb655Srie  *	else
1525aefb655Srie  *		JMP	*got_off@GOT(%ebx)
1535aefb655Srie  *	PUSHL	&rel_off
1545aefb655Srie  *	JMP	-n(%pc)		# -n is pcrel offset to first plt entry
1555aefb655Srie  *
1565aefb655Srie  *	The got_off@GOT entry gets filled with the address of the PUSHL,
1575aefb655Srie  *	so the first pass through the plt jumps back here, jumping
1585aefb655Srie  *	in turn to the first plt entry, which jumps to the dynamic
1595aefb655Srie  *	linker.	 The dynamic linker then patches the GOT, rerouting
1605aefb655Srie  *	future plt calls to the proper destination.
1615aefb655Srie  */
1625aefb655Srie static void
plt_entry(Ofl_desc * ofl,Word rel_off,Sym_desc * sdp)1635aefb655Srie plt_entry(Ofl_desc * ofl, Word rel_off, Sym_desc * sdp)
1645aefb655Srie {
1655aefb655Srie 	uchar_t		*pltent, *gotent;
1665aefb655Srie 	Sword		plt_off;
1675aefb655Srie 	Word		got_off;
168ba2be530Sab196087 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1695aefb655Srie 
1705aefb655Srie 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
1715aefb655Srie 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
1725aefb655Srie 	    M_PLT_ENTSIZE);
1735aefb655Srie 	pltent = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf) + plt_off;
1745aefb655Srie 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
1755aefb655Srie 
1765aefb655Srie 	/*
1775aefb655Srie 	 * Fill in the got entry with the address of the next instruction.
1785aefb655Srie 	 */
1795aefb655Srie 	/* LINTED */
1805aefb655Srie 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
1815aefb655Srie 	    M_PLT_INSSIZE;
182ba2be530Sab196087 	if (bswap)
183ba2be530Sab196087 		/* LINTED */
184ba2be530Sab196087 		*(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
1855aefb655Srie 
1865aefb655Srie 	if (!(ofl->ofl_flags & FLG_OF_SHAROBJ)) {
1875aefb655Srie 		pltent[0] = M_SPECIAL_INST;
1885aefb655Srie 		pltent[1] = M_JMP_DISP_IND;
1895aefb655Srie 		pltent += 2;
1905aefb655Srie 		/* LINTED */
1915aefb655Srie 		*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->sh_addr +
1925aefb655Srie 		    got_off);
1935aefb655Srie 	} else {
1945aefb655Srie 		pltent[0] = M_SPECIAL_INST;
1955aefb655Srie 		pltent[1] = M_JMP_REG_DISP_IND;
1965aefb655Srie 		pltent += 2;
1975aefb655Srie 		/* LINTED */
1985aefb655Srie 		*(Word *)pltent = (Word)got_off;
1995aefb655Srie 	}
200ba2be530Sab196087 	if (bswap)
201ba2be530Sab196087 		/* LINTED */
202ba2be530Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2035aefb655Srie 	pltent += 4;
2045aefb655Srie 
2055aefb655Srie 	pltent[0] = M_INST_PUSHL;
2065aefb655Srie 	pltent++;
2075aefb655Srie 	/* LINTED */
2085aefb655Srie 	*(Word *)pltent = (Word)rel_off;
209ba2be530Sab196087 	if (bswap)
210ba2be530Sab196087 		/* LINTED */
211ba2be530Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2125aefb655Srie 	pltent += 4;
2135aefb655Srie 
2145aefb655Srie 	plt_off = -(plt_off + 16);	/* JMP, PUSHL, JMP take 16 bytes */
2155aefb655Srie 	pltent[0] = M_INST_JMP;
2165aefb655Srie 	pltent++;
2175aefb655Srie 	/* LINTED */
2185aefb655Srie 	*(Word *)pltent = (Word)plt_off;
219ba2be530Sab196087 	if (bswap)
220ba2be530Sab196087 		/* LINTED */
221ba2be530Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2225aefb655Srie }
2235aefb655Srie 
224ba2be530Sab196087 static uintptr_t
ld_perform_outreloc(Rel_desc * orsp,Ofl_desc * ofl,Boolean * remain_seen)225*1007fd6fSAli Bahrami ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl, Boolean *remain_seen)
2265aefb655Srie {
2275aefb655Srie 	Os_desc *	relosp, * osp = 0;
2285aefb655Srie 	Word		ndx, roffset, value;
2295aefb655Srie 	Rel		rea;
2305aefb655Srie 	char		*relbits;
2315aefb655Srie 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
2325aefb655Srie 	int		sectmoved = 0;
2335aefb655Srie 
2345aefb655Srie 	sdp = orsp->rel_sym;
2355aefb655Srie 
2365aefb655Srie 	/*
2375aefb655Srie 	 * If the section this relocation is against has been discarded
2385aefb655Srie 	 * (-zignore), then also discard (skip) the relocation itself.
2395aefb655Srie 	 */
2405aefb655Srie 	if (orsp->rel_isdesc && ((orsp->rel_flags &
2415aefb655Srie 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
2425aefb655Srie 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
2435aefb655Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
2445aefb655Srie 		return (1);
2455aefb655Srie 	}
2465aefb655Srie 
2475aefb655Srie 	/*
2485aefb655Srie 	 * If this is a relocation against a move table, or expanded move
2495aefb655Srie 	 * table, adjust the relocation entries.
2505aefb655Srie 	 */
251bf994817SAli Bahrami 	if (RELAUX_GET_MOVE(orsp))
2525aefb655Srie 		ld_adj_movereloc(ofl, orsp);
2535aefb655Srie 
2545aefb655Srie 	/*
2555aefb655Srie 	 * If this is a relocation against a section using a partial initialized
2565aefb655Srie 	 * symbol, adjust the embedded symbol info.
2575aefb655Srie 	 *
2585aefb655Srie 	 * The second argument of the am_I_partial() is the value stored at the
2595aefb655Srie 	 * target address relocation is going to be applied.
2605aefb655Srie 	 */
2615aefb655Srie 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
26257ef7aa9SRod Evans 		if (ofl->ofl_parsyms &&
2635aefb655Srie 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
2645aefb655Srie 		    /* LINTED */
2655aefb655Srie 		    (psym = ld_am_I_partial(orsp, *(Xword *)
2665aefb655Srie 		    ((uchar_t *)(orsp->rel_isdesc->is_indata->d_buf) +
2675aefb655Srie 		    orsp->rel_roffset)))) {
2685aefb655Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
2695aefb655Srie 			sectmoved = 1;
2705aefb655Srie 		}
2715aefb655Srie 	}
2725aefb655Srie 
2735aefb655Srie 	value = sdp->sd_sym->st_value;
2745aefb655Srie 
2755aefb655Srie 	if (orsp->rel_flags & FLG_REL_GOT) {
2765aefb655Srie 		osp = ofl->ofl_osgot;
2775aefb655Srie 		roffset = (Word)ld_calc_got_offset(orsp, ofl);
2785aefb655Srie 
2795aefb655Srie 	} else if (orsp->rel_flags & FLG_REL_PLT) {
2805aefb655Srie 		/*
2815aefb655Srie 		 * Note that relocations for PLT's actually
2825aefb655Srie 		 * cause a relocation againt the GOT.
2835aefb655Srie 		 */
2845aefb655Srie 		osp = ofl->ofl_osplt;
2855aefb655Srie 		roffset = (Word) (ofl->ofl_osgot->os_shdr->sh_addr) +
2865aefb655Srie 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2875aefb655Srie 
2885aefb655Srie 		plt_entry(ofl, osp->os_relosdesc->os_szoutrels, sdp);
2895aefb655Srie 
2905aefb655Srie 	} else if (orsp->rel_flags & FLG_REL_BSS) {
2915aefb655Srie 		/*
2925aefb655Srie 		 * This must be a R_386_COPY.  For these set the roffset to
2935aefb655Srie 		 * point to the new symbols location.
2945aefb655Srie 		 */
2955aefb655Srie 		osp = ofl->ofl_isbss->is_osdesc;
2965aefb655Srie 		roffset = (Word)value;
2975aefb655Srie 	} else {
298bf994817SAli Bahrami 		osp = RELAUX_GET_OSDESC(orsp);
2995aefb655Srie 
3005aefb655Srie 		/*
3015aefb655Srie 		 * Calculate virtual offset of reference point; equals offset
3025aefb655Srie 		 * into section + vaddr of section for loadable sections, or
3035aefb655Srie 		 * offset plus section displacement for nonloadable sections.
3045aefb655Srie 		 */
3055aefb655Srie 		roffset = orsp->rel_roffset +
3065aefb655Srie 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
3075aefb655Srie 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
3085aefb655Srie 			roffset += orsp->rel_isdesc->is_osdesc->
3095aefb655Srie 			    os_shdr->sh_addr;
3105aefb655Srie 	}
3115aefb655Srie 
3125aefb655Srie 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
3135aefb655Srie 		relosp = ofl->ofl_osrel;
3145aefb655Srie 
3155aefb655Srie 	/*
3165aefb655Srie 	 * Assign the symbols index for the output relocation.  If the
3175aefb655Srie 	 * relocation refers to a SECTION symbol then it's index is based upon
3185aefb655Srie 	 * the output sections symbols index.  Otherwise the index can be
3195aefb655Srie 	 * derived from the symbols index itself.
3205aefb655Srie 	 */
3215aefb655Srie 	if (orsp->rel_rtype == R_386_RELATIVE)
3225aefb655Srie 		ndx = STN_UNDEF;
3235aefb655Srie 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
3245aefb655Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
3255aefb655Srie 		if (sectmoved == 0) {
3265aefb655Srie 			/*
3275aefb655Srie 			 * Check for a null input section. This can
3285aefb655Srie 			 * occur if this relocation references a symbol
3295aefb655Srie 			 * generated by sym_add_sym().
3305aefb655Srie 			 */
33157ef7aa9SRod Evans 			if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
33257ef7aa9SRod Evans 				ndx = sdp->sd_isc->is_osdesc->os_identndx;
3335aefb655Srie 			else
3345aefb655Srie 				ndx = sdp->sd_shndx;
3355aefb655Srie 		} else
33635450702SAli Bahrami 			ndx = ofl->ofl_parexpnndx;
3375aefb655Srie 	} else
3385aefb655Srie 		ndx = sdp->sd_symndx;
3395aefb655Srie 
340cce0e03bSab196087 	/*
341cce0e03bSab196087 	 * If we have a replacement value for the relocation
342cce0e03bSab196087 	 * target, put it in place now.
343cce0e03bSab196087 	 */
344cce0e03bSab196087 	if (orsp->rel_flags & FLG_REL_NADDEND) {
345cce0e03bSab196087 		Xword	addend = orsp->rel_raddend;
346cce0e03bSab196087 		uchar_t	*addr;
347cce0e03bSab196087 
348cce0e03bSab196087 		/*
349cce0e03bSab196087 		 * Get the address of the data item we need to modify.
350cce0e03bSab196087 		 */
351cce0e03bSab196087 		addr = (uchar_t *)((uintptr_t)orsp->rel_roffset +
352cce0e03bSab196087 		    (uintptr_t)_elf_getxoff(orsp->rel_isdesc->is_indata));
353bf994817SAli Bahrami 		addr += (uintptr_t)RELAUX_GET_OSDESC(orsp)->os_outdata->d_buf;
354cce0e03bSab196087 		if (ld_reloc_targval_set(ofl, orsp, addr, addend) == 0)
355cce0e03bSab196087 			return (S_ERROR);
356cce0e03bSab196087 	}
357cce0e03bSab196087 
3585aefb655Srie 	relbits = (char *)relosp->os_outdata->d_buf;
3595aefb655Srie 
3605aefb655Srie 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
3615aefb655Srie 	rea.r_offset = roffset;
3625aefb655Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_REL, &rea, relosp->os_name,
363bf994817SAli Bahrami 	    ld_reloc_sym_name(orsp)));
3645aefb655Srie 
3655aefb655Srie 	/*
3665aefb655Srie 	 * Assert we haven't walked off the end of our relocation table.
3675aefb655Srie 	 */
3685aefb655Srie 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
3695aefb655Srie 
3705aefb655Srie 	(void) memcpy((relbits + relosp->os_szoutrels),
3715aefb655Srie 	    (char *)&rea, sizeof (Rel));
3725aefb655Srie 	relosp->os_szoutrels += sizeof (Rel);
3735aefb655Srie 
3745aefb655Srie 	/*
3755aefb655Srie 	 * Determine if this relocation is against a non-writable, allocatable
3765aefb655Srie 	 * section.  If so we may need to provide a text relocation diagnostic.
3775aefb655Srie 	 * Note that relocations against the .plt (R_386_JMP_SLOT) actually
3785aefb655Srie 	 * result in modifications to the .got.
3795aefb655Srie 	 */
3805aefb655Srie 	if (orsp->rel_rtype == R_386_JMP_SLOT)
3815aefb655Srie 		osp = ofl->ofl_osgot;
3825aefb655Srie 
383*1007fd6fSAli Bahrami 	ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
3845aefb655Srie 	return (1);
3855aefb655Srie }
3865aefb655Srie 
3875aefb655Srie /*
3885aefb655Srie  * i386 Instructions for TLS processing
3895aefb655Srie  */
3905aefb655Srie static uchar_t tlsinstr_gd_ie[] = {
3915aefb655Srie 	/*
3925aefb655Srie 	 * 0x00	movl %gs:0x0, %eax
3935aefb655Srie 	 */
3945aefb655Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
3955aefb655Srie 	/*
3965aefb655Srie 	 * 0x06	addl x(%eax), %eax
3975aefb655Srie 	 * 0x0c ...
3985aefb655Srie 	 */
3995aefb655Srie 	0x03, 0x80, 0x00, 0x00, 0x00, 0x00
4005aefb655Srie };
4015aefb655Srie 
4025aefb655Srie static uchar_t tlsinstr_gd_le[] = {
4035aefb655Srie 	/*
4045aefb655Srie 	 * 0x00 movl %gs:0x0, %eax
4055aefb655Srie 	 */
4065aefb655Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
4075aefb655Srie 	/*
4085aefb655Srie 	 * 0x06 addl $0x0, %eax
4095aefb655Srie 	 */
4105aefb655Srie 	0x05, 0x00, 0x00, 0x00, 0x00,
4115aefb655Srie 	/*
4125aefb655Srie 	 * 0x0b nop
4135aefb655Srie 	 * 0x0c
4145aefb655Srie 	 */
4155aefb655Srie 	0x90
4165aefb655Srie };
4175aefb655Srie 
4185aefb655Srie static uchar_t tlsinstr_gd_ie_movgs[] = {
4195aefb655Srie 	/*
4205aefb655Srie 	 *	movl %gs:0x0,%eax
4215aefb655Srie 	 */
4225aefb655Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 00
4235aefb655Srie };
4245aefb655Srie 
4255aefb655Srie #define	TLS_GD_IE_MOV	0x8b	/* movl opcode */
4265aefb655Srie #define	TLS_GD_IE_POP	0x58	/* popl + reg */
4275aefb655Srie 
4285aefb655Srie #define	TLS_GD_LE_MOVL	0xb8	/* movl + reg */
4295aefb655Srie 
4305aefb655Srie #define	TLS_NOP		0x90	/* NOP instruction */
4315aefb655Srie 
4325aefb655Srie #define	MODRM_MSK_MOD	0xc0
4335aefb655Srie #define	MODRM_MSK_RO	0x38
4345aefb655Srie #define	MODRM_MSK_RM	0x07
4355aefb655Srie 
4365aefb655Srie #define	SIB_MSK_SS	0xc0
4375aefb655Srie #define	SIB_MSK_IND	0x38
4385aefb655Srie #define	SIB_MSK_BS	0x07
4395aefb655Srie 
4405aefb655Srie static Fixupret
tls_fixups(Ofl_desc * ofl,Rel_desc * arsp)4415aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
4425aefb655Srie {
4435aefb655Srie 	Sym_desc	*sdp = arsp->rel_sym;
4445aefb655Srie 	Word		rtype = arsp->rel_rtype;
4455aefb655Srie 	uchar_t		*offset, r1, r2;
4465aefb655Srie 
4475aefb655Srie 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
4485aefb655Srie 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
449bf994817SAli Bahrami 	    (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
4505aefb655Srie 
4515aefb655Srie 	if (sdp->sd_ref == REF_DYN_NEED) {
4525aefb655Srie 		/*
4535aefb655Srie 		 * IE reference model
4545aefb655Srie 		 */
4555aefb655Srie 		switch (rtype) {
4565aefb655Srie 		case R_386_TLS_GD:
4575aefb655Srie 			/*
4585aefb655Srie 			 * Transition:
4595aefb655Srie 			 *	0x0 leal x@tlsgd(,r1,1), %eax
4605aefb655Srie 			 *	0x7 call ___tls_get_addr
4615aefb655Srie 			 *	0xc
4625aefb655Srie 			 * To:
4635aefb655Srie 			 *	0x0 movl %gs:0, %eax
4645aefb655Srie 			 *	0x6 addl x@gotntpoff(r1), %eax
4655aefb655Srie 			 */
4665aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
467bf994817SAli Bahrami 			    R_386_TLS_GOTIE, arsp, ld_reloc_sym_name));
4685aefb655Srie 			arsp->rel_rtype = R_386_TLS_GOTIE;
4695aefb655Srie 			arsp->rel_roffset += 5;
4705aefb655Srie 
4715aefb655Srie 			/*
472051d39bbSrie 			 * Adjust 'offset' to beginning of instruction
4735aefb655Srie 			 * sequence.
4745aefb655Srie 			 */
4755aefb655Srie 			offset -= 3;
4765aefb655Srie 			r1 = (offset[2] & SIB_MSK_IND) >> 3;
4775aefb655Srie 			(void) memcpy(offset, tlsinstr_gd_ie,
4785aefb655Srie 			    sizeof (tlsinstr_gd_ie));
4795aefb655Srie 
4805aefb655Srie 			/*
4815aefb655Srie 			 * set register %r1 into the addl
4825aefb655Srie 			 * instruction.
4835aefb655Srie 			 */
4845aefb655Srie 			offset[0x7] |= r1;
4855aefb655Srie 			return (FIX_RELOC);
4865aefb655Srie 
4875aefb655Srie 		case R_386_TLS_GD_PLT:
4885aefb655Srie 			/*
4895aefb655Srie 			 * Fixup done via the TLS_GD relocation
4905aefb655Srie 			 */
4915aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
492bf994817SAli Bahrami 			    R_386_NONE, arsp, ld_reloc_sym_name));
4935aefb655Srie 			return (FIX_DONE);
4945aefb655Srie 		}
4955aefb655Srie 	}
4965aefb655Srie 
4975aefb655Srie 	/*
4985aefb655Srie 	 * LE reference model
4995aefb655Srie 	 */
5005aefb655Srie 	switch (rtype) {
5015aefb655Srie 	case R_386_TLS_GD:
5025aefb655Srie 		/*
5035aefb655Srie 		 * Transition:
5045aefb655Srie 		 *	0x0 leal x@tlsgd(,r1,1), %eax
5055aefb655Srie 		 *	0x7 call ___tls_get_addr
5065aefb655Srie 		 *	0xc
5075aefb655Srie 		 * To:
5085aefb655Srie 		 *	0x0 movl %gs:0, %eax
5095aefb655Srie 		 *	0x6 addl $x@ntpoff, %eax
5105aefb655Srie 		 *	0xb nop
5115aefb655Srie 		 *	0xc
5125aefb655Srie 		 */
5135aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
514bf994817SAli Bahrami 		    R_386_TLS_LE, arsp, ld_reloc_sym_name));
5155aefb655Srie 
5165aefb655Srie 		arsp->rel_rtype = R_386_TLS_LE;
5175aefb655Srie 		arsp->rel_roffset += 4;
5185aefb655Srie 
5195aefb655Srie 		/*
520051d39bbSrie 		 * Adjust 'offset' to beginning of instruction
5215aefb655Srie 		 * sequence.
5225aefb655Srie 		 */
5235aefb655Srie 		offset -= 3;
5245aefb655Srie 		(void) memcpy(offset, tlsinstr_gd_le,
5255aefb655Srie 		    sizeof (tlsinstr_gd_le));
5265aefb655Srie 		return (FIX_RELOC);
5275aefb655Srie 
5285aefb655Srie 	case R_386_TLS_GD_PLT:
5295aefb655Srie 	case R_386_PLT32:
5305aefb655Srie 		/*
5315aefb655Srie 		 * Fixup done via the TLS_GD relocation
5325aefb655Srie 		 */
5335aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
534bf994817SAli Bahrami 		    R_386_NONE, arsp, ld_reloc_sym_name));
5355aefb655Srie 		return (FIX_DONE);
5365aefb655Srie 
5375aefb655Srie 	case R_386_TLS_LDM_PLT:
5385aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
539bf994817SAli Bahrami 		    R_386_NONE, arsp, ld_reloc_sym_name));
5405aefb655Srie 
5415aefb655Srie 		/*
5425aefb655Srie 		 * Transition:
5435aefb655Srie 		 *	call __tls_get_addr()
5445aefb655Srie 		 * to:
5455aefb655Srie 		 *	nop
5465aefb655Srie 		 *	nop
5475aefb655Srie 		 *	nop
5485aefb655Srie 		 *	nop
5495aefb655Srie 		 *	nop
5505aefb655Srie 		 */
5515aefb655Srie 		*(offset - 1) = TLS_NOP;
5525aefb655Srie 		*(offset) = TLS_NOP;
5535aefb655Srie 		*(offset + 1) = TLS_NOP;
5545aefb655Srie 		*(offset + 2) = TLS_NOP;
5555aefb655Srie 		*(offset + 3) = TLS_NOP;
5565aefb655Srie 		return (FIX_DONE);
5575aefb655Srie 
5585aefb655Srie 	case R_386_TLS_LDM:
5595aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
560bf994817SAli Bahrami 		    R_386_NONE, arsp, ld_reloc_sym_name));
5615aefb655Srie 
5625aefb655Srie 		/*
5635aefb655Srie 		 * Transition:
5645aefb655Srie 		 *
5655aefb655Srie 		 *  0x00 leal x1@tlsldm(%ebx), %eax
5665aefb655Srie 		 *  0x06 call ___tls_get_addr
5675aefb655Srie 		 *
5685aefb655Srie 		 * to:
5695aefb655Srie 		 *
5705aefb655Srie 		 *  0x00 movl %gs:0, %eax
5715aefb655Srie 		 */
5725aefb655Srie 		(void) memcpy(offset - 2, tlsinstr_gd_ie_movgs,
5735aefb655Srie 		    sizeof (tlsinstr_gd_ie_movgs));
5745aefb655Srie 		return (FIX_DONE);
5755aefb655Srie 
5765aefb655Srie 	case R_386_TLS_LDO_32:
5775aefb655Srie 		/*
5785aefb655Srie 		 *  Instructions:
5795aefb655Srie 		 *
5805aefb655Srie 		 *  0x10 leal x1@dtpoff(%eax), %edx	R_386_TLS_LDO_32
5815aefb655Srie 		 *		to
5825aefb655Srie 		 *  0x10 leal x1@ntpoff(%eax), %edx	R_386_TLS_LE
5835aefb655Srie 		 *
5845aefb655Srie 		 */
5855aefb655Srie 		offset -= 2;
5865aefb655Srie 
5875aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
588bf994817SAli Bahrami 		    R_386_TLS_LE, arsp, ld_reloc_sym_name));
5895aefb655Srie 		arsp->rel_rtype = R_386_TLS_LE;
5905aefb655Srie 		return (FIX_RELOC);
5915aefb655Srie 
5925aefb655Srie 	case R_386_TLS_GOTIE:
5935aefb655Srie 		/*
5945aefb655Srie 		 * These transitions are a little different than the
5955aefb655Srie 		 * others, in that we could have multiple instructions
5965aefb655Srie 		 * pointed to by a single relocation.  Depending upon the
5975aefb655Srie 		 * instruction, we perform a different code transition.
5985aefb655Srie 		 *
5995aefb655Srie 		 * Here's the known transitions:
6005aefb655Srie 		 *
6015aefb655Srie 		 *  1) movl foo@gotntpoff(%reg1), %reg2
6025aefb655Srie 		 *	0x8b, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
6035aefb655Srie 		 *
6045aefb655Srie 		 *  2) addl foo@gotntpoff(%reg1), %reg2
6055aefb655Srie 		 *	0x03, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
6065aefb655Srie 		 *
6075aefb655Srie 		 *  Transitions IE -> LE
6085aefb655Srie 		 *
6095aefb655Srie 		 *  1) movl $foo@ntpoff, %reg2
6105aefb655Srie 		 *	0xc7, 0xc0 | reg2, foo@ntpoff
6115aefb655Srie 		 *
6125aefb655Srie 		 *  2) addl $foo@ntpoff, %reg2
6135aefb655Srie 		 *	0x81, 0xc0 | reg2, foo@ntpoff
6145aefb655Srie 		 *
6155aefb655Srie 		 * Note: reg1 != 4 (%esp)
6165aefb655Srie 		 */
6175aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
618bf994817SAli Bahrami 		    R_386_TLS_LE, arsp, ld_reloc_sym_name));
6195aefb655Srie 		arsp->rel_rtype = R_386_TLS_LE;
6205aefb655Srie 
6215aefb655Srie 		offset -= 2;
6225aefb655Srie 		r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6235aefb655Srie 		if (offset[0] == 0x8b) {
6245aefb655Srie 			/* case 1 above */
6255aefb655Srie 			offset[0] = 0xc7;	/* movl */
6265aefb655Srie 			offset[1] = 0xc0 | r2;
6275aefb655Srie 			return (FIX_RELOC);
6285aefb655Srie 		}
6295aefb655Srie 
6305aefb655Srie 		if (offset[0] == 0x03) {
6315aefb655Srie 			/* case 2 above */
6325aefb655Srie 			assert(offset[0] == 0x03);
6335aefb655Srie 			offset[0] = 0x81;	/* addl */
6345aefb655Srie 			offset[1] = 0xc0 | r2;
6355aefb655Srie 			return (FIX_RELOC);
6365aefb655Srie 		}
6375aefb655Srie 
6385aefb655Srie 		/*
6395aefb655Srie 		 * Unexpected instruction sequence - fatal error.
6405aefb655Srie 		 */
641de777a60Sab196087 		{
642de777a60Sab196087 			Conv_inv_buf_t	inv_buf;
643de777a60Sab196087 
644*1007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
645de777a60Sab196087 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
6465aefb655Srie 			    arsp->rel_isdesc->is_file->ifl_name,
647bf994817SAli Bahrami 			    ld_reloc_sym_name(arsp),
648de777a60Sab196087 			    arsp->rel_isdesc->is_name,
6495aefb655Srie 			    EC_OFF(arsp->rel_roffset));
650de777a60Sab196087 		}
6515aefb655Srie 		return (FIX_ERROR);
6525aefb655Srie 
6535aefb655Srie 	case R_386_TLS_IE:
6545aefb655Srie 		/*
6555aefb655Srie 		 * These transitions are a little different than the
6565aefb655Srie 		 * others, in that we could have multiple instructions
6575aefb655Srie 		 * pointed to by a single relocation.  Depending upon the
6585aefb655Srie 		 * instruction, we perform a different code transition.
6595aefb655Srie 		 *
6605aefb655Srie 		 * Here's the known transitions:
6615aefb655Srie 		 *  1) movl foo@indntpoff, %eax
6625aefb655Srie 		 *	0xa1, foo@indntpoff
6635aefb655Srie 		 *
6645aefb655Srie 		 *  2) movl foo@indntpoff, %eax
6655aefb655Srie 		 *	0x8b, 0x05 | (reg << 3), foo@gotntpoff
6665aefb655Srie 		 *
6675aefb655Srie 		 *  3) addl foo@indntpoff, %eax
6685aefb655Srie 		 *	0x03, 0x05 | (reg << 3), foo@gotntpoff
6695aefb655Srie 		 *
6705aefb655Srie 		 *  Transitions IE -> LE
6715aefb655Srie 		 *
6725aefb655Srie 		 *  1) movl $foo@ntpoff, %eax
6735aefb655Srie 		 *	0xb8, foo@ntpoff
6745aefb655Srie 		 *
6755aefb655Srie 		 *  2) movl $foo@ntpoff, %reg
6765aefb655Srie 		 *	0xc7, 0xc0 | reg, foo@ntpoff
6775aefb655Srie 		 *
6785aefb655Srie 		 *  3) addl $foo@ntpoff, %reg
6795aefb655Srie 		 *	0x81, 0xc0 | reg, foo@ntpoff
6805aefb655Srie 		 */
6815aefb655Srie 		arsp->rel_rtype = R_386_TLS_LE;
6825aefb655Srie 		offset--;
6835aefb655Srie 		if (offset[0] == 0xa1) {
6845aefb655Srie 			/* case 1 above */
6855aefb655Srie 			offset[0] = 0xb8;	/*  movl */
6865aefb655Srie 			return (FIX_RELOC);
6875aefb655Srie 		}
6885aefb655Srie 
6895aefb655Srie 		offset--;
6905aefb655Srie 		if (offset[0] == 0x8b) {
6915aefb655Srie 			/* case 2 above */
6925aefb655Srie 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6935aefb655Srie 			offset[0] = 0xc7;	/* movl */
6945aefb655Srie 			offset[1] = 0xc0 | r2;
6955aefb655Srie 			return (FIX_RELOC);
6965aefb655Srie 		}
6975aefb655Srie 		if (offset[0] == 0x03) {
6985aefb655Srie 			/* case 3 above */
6995aefb655Srie 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
7005aefb655Srie 			offset[0] = 0x81;	/* addl */
7015aefb655Srie 			offset[1] = 0xc0 | r2;
7025aefb655Srie 			return (FIX_RELOC);
7035aefb655Srie 		}
7045aefb655Srie 		/*
7055aefb655Srie 		 * Unexpected instruction sequence - fatal error.
7065aefb655Srie 		 */
707de777a60Sab196087 		{
708de777a60Sab196087 			Conv_inv_buf_t	inv_buf;
709de777a60Sab196087 
710*1007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
711de777a60Sab196087 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
7125aefb655Srie 			    arsp->rel_isdesc->is_file->ifl_name,
713bf994817SAli Bahrami 			    ld_reloc_sym_name(arsp),
714de777a60Sab196087 			    arsp->rel_isdesc->is_name,
7155aefb655Srie 			    EC_OFF(arsp->rel_roffset));
716de777a60Sab196087 		}
7175aefb655Srie 		return (FIX_ERROR);
7185aefb655Srie 	}
7195aefb655Srie 	return (FIX_RELOC);
7205aefb655Srie }
7215aefb655Srie 
722ba2be530Sab196087 static uintptr_t
ld_do_activerelocs(Ofl_desc * ofl)7235aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
7245aefb655Srie {
7255aefb655Srie 	Rel_desc	*arsp;
726bf994817SAli Bahrami 	Rel_cachebuf	*rcbp;
72757ef7aa9SRod Evans 	Aliste		idx;
7285aefb655Srie 	uintptr_t	return_code = 1;
7291d9df23bSab196087 	ofl_flag_t	flags = ofl->ofl_flags;
7305aefb655Srie 
731bf994817SAli Bahrami 	if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
7325aefb655Srie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
7337010c12aSrie 
7345aefb655Srie 	/*
7355aefb655Srie 	 * Process active relocations.
7365aefb655Srie 	 */
737bf994817SAli Bahrami 	REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
7385aefb655Srie 		uchar_t		*addr;
7395aefb655Srie 		Xword 		value;
7405aefb655Srie 		Sym_desc	*sdp;
7415aefb655Srie 		const char	*ifl_name;
7425aefb655Srie 		Xword		refaddr;
7435aefb655Srie 		int		moved = 0;
7445aefb655Srie 		Gotref		gref;
745bf994817SAli Bahrami 		Os_desc		*osp;
7465aefb655Srie 
7475aefb655Srie 		/*
748bf994817SAli Bahrami 		 * If the section this relocation is against has been discarded
749bf994817SAli Bahrami 		 * (-zignore), then discard (skip) the relocation itself.
7505aefb655Srie 		 */
7515aefb655Srie 		if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
752bf994817SAli Bahrami 		    ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
7535aefb655Srie 		    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
754bf994817SAli Bahrami 			DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
7555aefb655Srie 			continue;
7565aefb655Srie 		}
7575aefb655Srie 
7585aefb655Srie 		/*
759bf994817SAli Bahrami 		 * We determine what the 'got reference' model (if required)
760bf994817SAli Bahrami 		 * is at this point.  This needs to be done before tls_fixup()
761bf994817SAli Bahrami 		 * since it may 'transition' our instructions.
7625aefb655Srie 		 *
7635aefb655Srie 		 * The got table entries have already been assigned,
7645aefb655Srie 		 * and we bind to those initial entries.
7655aefb655Srie 		 */
7665aefb655Srie 		if (arsp->rel_flags & FLG_REL_DTLS)
7675aefb655Srie 			gref = GOT_REF_TLSGD;
7685aefb655Srie 		else if (arsp->rel_flags & FLG_REL_MTLS)
7695aefb655Srie 			gref = GOT_REF_TLSLD;
7705aefb655Srie 		else if (arsp->rel_flags & FLG_REL_STLS)
7715aefb655Srie 			gref = GOT_REF_TLSIE;
7725aefb655Srie 		else
7735aefb655Srie 			gref = GOT_REF_GENERIC;
7745aefb655Srie 
7755aefb655Srie 		/*
7765aefb655Srie 		 * Perform any required TLS fixups.
7775aefb655Srie 		 */
7785aefb655Srie 		if (arsp->rel_flags & FLG_REL_TLSFIX) {
7795aefb655Srie 			Fixupret	ret;
7805aefb655Srie 
7815aefb655Srie 			if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
7825aefb655Srie 				return (S_ERROR);
7835aefb655Srie 			if (ret == FIX_DONE)
7845aefb655Srie 				continue;
7855aefb655Srie 		}
7865aefb655Srie 
7875aefb655Srie 		/*
7885aefb655Srie 		 * If this is a relocation against a move table, or
7895aefb655Srie 		 * expanded move table, adjust the relocation entries.
7905aefb655Srie 		 */
791bf994817SAli Bahrami 		if (RELAUX_GET_MOVE(arsp))
7925aefb655Srie 			ld_adj_movereloc(ofl, arsp);
7935aefb655Srie 
7945aefb655Srie 		sdp = arsp->rel_sym;
7955aefb655Srie 		refaddr = arsp->rel_roffset +
7965aefb655Srie 		    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
7975aefb655Srie 
7985aefb655Srie 		if (arsp->rel_flags & FLG_REL_CLVAL)
7995aefb655Srie 			value = 0;
800bf994817SAli Bahrami 		else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
8015aefb655Srie 			/*
8025aefb655Srie 			 * The value for a symbol pointing to a SECTION
8035aefb655Srie 			 * is based off of that sections position.
8045aefb655Srie 			 */
805b26cc8daSAli Bahrami 			if (sdp->sd_isc->is_flags & FLG_IS_RELUPD) {
806b26cc8daSAli Bahrami 				Sym_desc	*sym;
807b26cc8daSAli Bahrami 				Xword		radd;
808b26cc8daSAli Bahrami 				uchar_t		*raddr = (uchar_t *)
8095aefb655Srie 				    arsp->rel_isdesc->is_indata->d_buf +
810b26cc8daSAli Bahrami 				    arsp->rel_roffset;
811b26cc8daSAli Bahrami 
8125aefb655Srie 				/*
813bf994817SAli Bahrami 				 * This is a REL platform. Hence, the second
814bf994817SAli Bahrami 				 * argument of ld_am_I_partial() is the value
815bf994817SAli Bahrami 				 * stored at the target address where the
816bf994817SAli Bahrami 				 * relocation is going to be applied.
817b26cc8daSAli Bahrami 				 */
818bf994817SAli Bahrami 				if (ld_reloc_targval_get(ofl, arsp, raddr,
819bf994817SAli Bahrami 				    &radd) == 0)
820b26cc8daSAli Bahrami 					return (S_ERROR);
821b26cc8daSAli Bahrami 				sym = ld_am_I_partial(arsp, radd);
822b26cc8daSAli Bahrami 				if (sym) {
823b26cc8daSAli Bahrami 					Sym	*osym = sym->sd_osym;
824b26cc8daSAli Bahrami 
825b26cc8daSAli Bahrami 					/*
826bf994817SAli Bahrami 					 * The symbol was moved, so adjust the
827bf994817SAli Bahrami 					 * value relative to the new section.
8285aefb655Srie 					 */
8295aefb655Srie 					value = sym->sd_sym->st_value;
8305aefb655Srie 					moved = 1;
831b26cc8daSAli Bahrami 
832b26cc8daSAli Bahrami 					/*
833bf994817SAli Bahrami 					 * The original raddend covers the
834bf994817SAli Bahrami 					 * displacement from the section start
835bf994817SAli Bahrami 					 * to the desired address. The value
836bf994817SAli Bahrami 					 * computed above gets us from the
837bf994817SAli Bahrami 					 * section start to the start of the
838bf994817SAli Bahrami 					 * symbol range. Adjust the old raddend
839bf994817SAli Bahrami 					 * to remove the offset from section
840bf994817SAli Bahrami 					 * start to symbol start, leaving the
841bf994817SAli Bahrami 					 * displacement within the range of
842bf994817SAli Bahrami 					 * the symbol.
843b26cc8daSAli Bahrami 					 */
844b26cc8daSAli Bahrami 					if (osym->st_value != 0) {
845b26cc8daSAli Bahrami 						radd -= osym->st_value;
846bf994817SAli Bahrami 						if (ld_reloc_targval_set(ofl,
847bf994817SAli Bahrami 						    arsp, raddr, radd) == 0)
848bf994817SAli Bahrami 							return (S_ERROR);
849b26cc8daSAli Bahrami 					}
850b26cc8daSAli Bahrami 				}
851b26cc8daSAli Bahrami 			}
852b26cc8daSAli Bahrami 			if (!moved) {
853bf994817SAli Bahrami 				value = _elf_getxoff(sdp->sd_isc->is_indata);
854bf994817SAli Bahrami 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
8555aefb655Srie 					value += sdp->sd_isc->
8565aefb655Srie 					    is_osdesc->os_shdr->sh_addr;
8575aefb655Srie 			}
8585aefb655Srie 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
8595aefb655Srie 				value -= ofl->ofl_tlsphdr->p_vaddr;
8602926dd2eSrie 
8612926dd2eSrie 		} else if (IS_SIZE(arsp->rel_rtype)) {
8622926dd2eSrie 			/*
8632926dd2eSrie 			 * Size relocations require the symbols size.
8642926dd2eSrie 			 */
8652926dd2eSrie 			value = sdp->sd_sym->st_size;
86608278a5eSRod Evans 
86708278a5eSRod Evans 		} else if ((sdp->sd_flags & FLG_SY_CAP) &&
86808278a5eSRod Evans 		    sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
8695aefb655Srie 			/*
870bf994817SAli Bahrami 			 * If relocation is against a capabilities symbol, we
871bf994817SAli Bahrami 			 * need to jump to an associated PLT, so that at runtime
872bf994817SAli Bahrami 			 * ld.so.1 is involved to determine the best binding
873bf994817SAli Bahrami 			 * choice. Otherwise, the value is the symbols value.
8745aefb655Srie 			 */
87508278a5eSRod Evans 			value = ld_calc_plt_addr(sdp, ofl);
87608278a5eSRod Evans 
87708278a5eSRod Evans 		} else
8785aefb655Srie 			value = sdp->sd_sym->st_value;
8795aefb655Srie 
8805aefb655Srie 		/*
8815aefb655Srie 		 * Relocation against the GLOBAL_OFFSET_TABLE.
8825aefb655Srie 		 */
883bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_GOT) &&
884bf994817SAli Bahrami 		    !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
885bf994817SAli Bahrami 			return (S_ERROR);
886bf994817SAli Bahrami 		osp = RELAUX_GET_OSDESC(arsp);
8875aefb655Srie 
8885aefb655Srie 		/*
889bf994817SAli Bahrami 		 * If loadable and not producing a relocatable object add the
890bf994817SAli Bahrami 		 * sections virtual address to the reference address.
8915aefb655Srie 		 */
8925aefb655Srie 		if ((arsp->rel_flags & FLG_REL_LOAD) &&
8935aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0))
894bf994817SAli Bahrami 			refaddr +=
895bf994817SAli Bahrami 			    arsp->rel_isdesc->is_osdesc->os_shdr->sh_addr;
8965aefb655Srie 
8975aefb655Srie 		/*
898bf994817SAli Bahrami 		 * If this entry has a PLT assigned to it, its value is actually
899bf994817SAli Bahrami 		 * the address of the PLT (and not the address of the function).
9005aefb655Srie 		 */
9015aefb655Srie 		if (IS_PLT(arsp->rel_rtype)) {
9025aefb655Srie 			if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
9035aefb655Srie 				value = ld_calc_plt_addr(sdp, ofl);
9045aefb655Srie 		}
9055aefb655Srie 
9065aefb655Srie 		/*
907bf994817SAli Bahrami 		 * Determine whether the value needs further adjustment. Filter
908bf994817SAli Bahrami 		 * through the attributes of the relocation to determine what
909bf994817SAli Bahrami 		 * adjustment is required.  Note, many of the following cases
910bf994817SAli Bahrami 		 * are only applicable when a .got is present.  As a .got is
911bf994817SAli Bahrami 		 * not generated when a relocatable object is being built,
912bf994817SAli Bahrami 		 * any adjustments that require a .got need to be skipped.
9135aefb655Srie 		 */
9145aefb655Srie 		if ((arsp->rel_flags & FLG_REL_GOT) &&
9155aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
9165aefb655Srie 			Xword		R1addr;
9175aefb655Srie 			uintptr_t	R2addr;
9185aefb655Srie 			Word		gotndx;
9195aefb655Srie 			Gotndx		*gnp;
9205aefb655Srie 
9215aefb655Srie 			/*
922bf994817SAli Bahrami 			 * Perform relocation against GOT table.  Since this
923bf994817SAli Bahrami 			 * doesn't fit exactly into a relocation we place the
924bf994817SAli Bahrami 			 * appropriate byte in the GOT directly
9255aefb655Srie 			 *
9265aefb655Srie 			 * Calculate offset into GOT at which to apply
9275aefb655Srie 			 * the relocation.
9285aefb655Srie 			 */
929bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
9305aefb655Srie 			assert(gnp);
9315aefb655Srie 
9325aefb655Srie 			if (arsp->rel_rtype == R_386_TLS_DTPOFF32)
9335aefb655Srie 				gotndx = gnp->gn_gotndx + 1;
9345aefb655Srie 			else
9355aefb655Srie 				gotndx = gnp->gn_gotndx;
9365aefb655Srie 
9375aefb655Srie 			R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
9385aefb655Srie 
9395aefb655Srie 			/*
9405aefb655Srie 			 * Add the GOTs data's offset.
9415aefb655Srie 			 */
942bf994817SAli Bahrami 			R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
9435aefb655Srie 
944bf994817SAli Bahrami 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
945bf994817SAli Bahrami 			    M_MACH, SHT_REL, arsp, R1addr, value,
946bf994817SAli Bahrami 			    ld_reloc_sym_name));
9475aefb655Srie 
9485aefb655Srie 			/*
9495aefb655Srie 			 * And do it.
9505aefb655Srie 			 */
951f3324781Sab196087 			if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
952bf994817SAli Bahrami 				*(Xword *)R2addr = ld_bswap_Xword(value);
953f3324781Sab196087 			else
9545aefb655Srie 				*(Xword *)R2addr = value;
9555aefb655Srie 			continue;
9565aefb655Srie 
9575aefb655Srie 		} else if (IS_GOT_BASED(arsp->rel_rtype) &&
9585aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
9595aefb655Srie 			value -= ofl->ofl_osgot->os_shdr->sh_addr;
9605aefb655Srie 
9615aefb655Srie 		} else if (IS_GOT_PC(arsp->rel_rtype) &&
9625aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
963bf994817SAli Bahrami 			value = (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) -
9645aefb655Srie 			    refaddr;
9655aefb655Srie 
9665aefb655Srie 		} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
9675aefb655Srie 		    (((flags & FLG_OF_RELOBJ) == 0) ||
968bf994817SAli Bahrami 		    (osp == sdp->sd_isc->is_osdesc))) {
9695aefb655Srie 			value -= refaddr;
9705aefb655Srie 
9715aefb655Srie 		} else if (IS_TLS_INS(arsp->rel_rtype) &&
9725aefb655Srie 		    IS_GOT_RELATIVE(arsp->rel_rtype) &&
9735aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
9745aefb655Srie 			Gotndx	*gnp;
9755aefb655Srie 
976bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
9775aefb655Srie 			assert(gnp);
9785aefb655Srie 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
9795aefb655Srie 			if (arsp->rel_rtype == R_386_TLS_IE) {
980bf994817SAli Bahrami 				value += ofl->ofl_osgot->os_shdr->sh_addr;
9815aefb655Srie 			}
9825aefb655Srie 
9835aefb655Srie 		} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
9845aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
9855aefb655Srie 			Gotndx *gnp;
9865aefb655Srie 
98757ef7aa9SRod Evans 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
98857ef7aa9SRod Evans 			    GOT_REF_GENERIC, ofl, NULL);
9895aefb655Srie 			assert(gnp);
9905aefb655Srie 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
9915aefb655Srie 
9925aefb655Srie 		} else if ((arsp->rel_flags & FLG_REL_STLS) &&
9935aefb655Srie 		    ((flags & FLG_OF_RELOBJ) == 0)) {
9945aefb655Srie 			Xword	tlsstatsize;
9955aefb655Srie 
9965aefb655Srie 			/*
9975aefb655Srie 			 * This is the LE TLS reference model.  Static
9985aefb655Srie 			 * offset is hard-coded.
9995aefb655Srie 			 */
1000bf994817SAli Bahrami 			tlsstatsize = S_ROUND(ofl->ofl_tlsphdr->p_memsz,
10015aefb655Srie 			    M_TLSSTATALIGN);
10025aefb655Srie 			value = tlsstatsize - value;
10035aefb655Srie 
10045aefb655Srie 			/*
10055aefb655Srie 			 * Since this code is fixed up, it assumes a
10065aefb655Srie 			 * negative offset that can be added to the
10075aefb655Srie 			 * thread pointer.
10085aefb655Srie 			 */
10095aefb655Srie 			if ((arsp->rel_rtype == R_386_TLS_LDO_32) ||
10105aefb655Srie 			    (arsp->rel_rtype == R_386_TLS_LE))
10115aefb655Srie 				value = -value;
10125aefb655Srie 		}
10135aefb655Srie 
10145aefb655Srie 		if (arsp->rel_isdesc->is_file)
10155aefb655Srie 			ifl_name = arsp->rel_isdesc->is_file->ifl_name;
10165aefb655Srie 		else
10175aefb655Srie 			ifl_name = MSG_INTL(MSG_STR_NULL);
10185aefb655Srie 
10195aefb655Srie 		/*
1020bf994817SAli Bahrami 		 * Make sure we have data to relocate.  Compiler and assembler
1021bf994817SAli Bahrami 		 * developers have been known to generate relocations against
1022bf994817SAli Bahrami 		 * invalid sections (normally .bss), so for their benefit give
1023bf994817SAli Bahrami 		 * them sufficient information to help analyze the problem.
1024bf994817SAli Bahrami 		 * End users should never see this.
10255aefb655Srie 		 */
10265aefb655Srie 		if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1027de777a60Sab196087 			Conv_inv_buf_t	inv_buf;
1028de777a60Sab196087 
1029*1007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
1030bf994817SAli Bahrami 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
1031bf994817SAli Bahrami 			    ifl_name, ld_reloc_sym_name(arsp),
10324a8d0ea7SAli Bahrami 			    EC_WORD(arsp->rel_isdesc->is_scnndx),
10335aefb655Srie 			    arsp->rel_isdesc->is_name);
10345aefb655Srie 			return (S_ERROR);
10355aefb655Srie 		}
10365aefb655Srie 
10375aefb655Srie 		/*
10385aefb655Srie 		 * Get the address of the data item we need to modify.
10395aefb655Srie 		 */
10405aefb655Srie 		addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1041bf994817SAli Bahrami 		    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
10425aefb655Srie 
1043635216b6SRod Evans 		DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
1044bf994817SAli Bahrami 		    M_MACH, SHT_REL, arsp, EC_NATPTR(addr), value,
1045bf994817SAli Bahrami 		    ld_reloc_sym_name));
1046bf994817SAli Bahrami 		addr += (uintptr_t)osp->os_outdata->d_buf;
10475aefb655Srie 
10485aefb655Srie 		if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
10495aefb655Srie 		    ofl->ofl_size) || (arsp->rel_roffset >
1050bf994817SAli Bahrami 		    osp->os_shdr->sh_size)) {
1051de777a60Sab196087 			Conv_inv_buf_t	inv_buf;
10525aefb655Srie 			int		class;
10535aefb655Srie 
1054bf994817SAli Bahrami 			if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1055bf994817SAli Bahrami 			    ofl->ofl_size)
10565aefb655Srie 				class = ERR_FATAL;
10575aefb655Srie 			else
10585aefb655Srie 				class = ERR_WARNING;
10595aefb655Srie 
1060*1007fd6fSAli Bahrami 			ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
1061bf994817SAli Bahrami 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
1062bf994817SAli Bahrami 			    ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
1063bf994817SAli Bahrami 			    arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
10645aefb655Srie 			    EC_ADDR((uintptr_t)addr -
10655aefb655Srie 			    (uintptr_t)ofl->ofl_nehdr));
10665aefb655Srie 
10675aefb655Srie 			if (class == ERR_FATAL) {
10685aefb655Srie 				return_code = S_ERROR;
10695aefb655Srie 				continue;
10705aefb655Srie 			}
10715aefb655Srie 		}
10725aefb655Srie 
10735aefb655Srie 		/*
1074bf994817SAli Bahrami 		 * The relocation is additive.  Ignore the previous symbol
1075bf994817SAli Bahrami 		 * value if this local partial symbol is expanded.
10765aefb655Srie 		 */
10775aefb655Srie 		if (moved)
10785aefb655Srie 			value -= *addr;
10795aefb655Srie 
10805aefb655Srie 		/*
1081cce0e03bSab196087 		 * If we have a replacement value for the relocation
1082cce0e03bSab196087 		 * target, put it in place now.
1083cce0e03bSab196087 		 */
1084cce0e03bSab196087 		if (arsp->rel_flags & FLG_REL_NADDEND) {
1085cce0e03bSab196087 			Xword addend = arsp->rel_raddend;
1086cce0e03bSab196087 
1087bf994817SAli Bahrami 			if (ld_reloc_targval_set(ofl, arsp, addr, addend) == 0)
1088cce0e03bSab196087 				return (S_ERROR);
1089cce0e03bSab196087 		}
1090cce0e03bSab196087 
1091cce0e03bSab196087 		/*
1092bf994817SAli Bahrami 		 * If '-z noreloc' is specified - skip the do_reloc_ld stage.
10935aefb655Srie 		 */
1094f3324781Sab196087 		if (OFL_DO_RELOC(ofl)) {
1095bf994817SAli Bahrami 			if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
1096bf994817SAli Bahrami 			    ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
1097*1007fd6fSAli Bahrami 			    ofl->ofl_lml) == 0) {
1098*1007fd6fSAli Bahrami 				ofl->ofl_flags |= FLG_OF_FATAL;
10995aefb655Srie 				return_code = S_ERROR;
11005aefb655Srie 			}
11015aefb655Srie 		}
1102*1007fd6fSAli Bahrami 	}
11035aefb655Srie 	return (return_code);
11045aefb655Srie }
11055aefb655Srie 
11065aefb655Srie /*
11075aefb655Srie  * Add an output relocation record.
11085aefb655Srie  */
1109ba2be530Sab196087 static uintptr_t
ld_add_outrel(Word flags,Rel_desc * rsp,Ofl_desc * ofl)11105aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
11115aefb655Srie {
11125aefb655Srie 	Rel_desc	*orsp;
11135aefb655Srie 	Sym_desc	*sdp = rsp->rel_sym;
11145aefb655Srie 
11155aefb655Srie 	/*
11165aefb655Srie 	 * Static executables *do not* want any relocations against them.
11175aefb655Srie 	 * Since our engine still creates relocations against a WEAK UNDEFINED
11185aefb655Srie 	 * symbol in a static executable, it's best to disable them here
11195aefb655Srie 	 * instead of through out the relocation code.
11205aefb655Srie 	 */
1121635216b6SRod Evans 	if (OFL_IS_STATIC_EXEC(ofl))
11225aefb655Srie 		return (1);
11235aefb655Srie 
11245aefb655Srie 	/*
11255aefb655Srie 	 * If we are adding a output relocation against a section
11265aefb655Srie 	 * symbol (non-RELATIVE) then mark that section.  These sections
11275aefb655Srie 	 * will be added to the .dynsym symbol table.
11285aefb655Srie 	 */
11295aefb655Srie 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
11305aefb655Srie 	    ((flags & FLG_REL_SCNNDX) ||
11315aefb655Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
11325aefb655Srie 
11335aefb655Srie 		/*
11345aefb655Srie 		 * If this is a COMMON symbol - no output section
11355aefb655Srie 		 * exists yet - (it's created as part of sym_validate()).
11365aefb655Srie 		 * So - we mark here that when it's created it should
11375aefb655Srie 		 * be tagged with the FLG_OS_OUTREL flag.
11385aefb655Srie 		 */
11395aefb655Srie 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
11400bc07c75Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
11415aefb655Srie 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
11425aefb655Srie 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
11435aefb655Srie 			else
11445aefb655Srie 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
11455aefb655Srie 		} else {
114608278a5eSRod Evans 			Os_desc *osp;
114708278a5eSRod Evans 			Is_desc *isp = sdp->sd_isc;
11485aefb655Srie 
114908278a5eSRod Evans 			if (isp && ((osp = isp->is_osdesc) != NULL) &&
115008278a5eSRod Evans 			    ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11515aefb655Srie 				ofl->ofl_dynshdrcnt++;
11525aefb655Srie 				osp->os_flags |= FLG_OS_OUTREL;
11535aefb655Srie 			}
11545aefb655Srie 		}
11555aefb655Srie 	}
11565aefb655Srie 
1157bf994817SAli Bahrami 	/* Enter it into the output relocation cache */
1158bf994817SAli Bahrami 	if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
1159bf994817SAli Bahrami 		return (S_ERROR);
11605aefb655Srie 
11615aefb655Srie 	if (flags & FLG_REL_GOT)
11625aefb655Srie 		ofl->ofl_relocgotsz += (Xword)sizeof (Rel);
11635aefb655Srie 	else if (flags & FLG_REL_PLT)
11645aefb655Srie 		ofl->ofl_relocpltsz += (Xword)sizeof (Rel);
11655aefb655Srie 	else if (flags & FLG_REL_BSS)
11665aefb655Srie 		ofl->ofl_relocbsssz += (Xword)sizeof (Rel);
11675aefb655Srie 	else if (flags & FLG_REL_NOINFO)
11685aefb655Srie 		ofl->ofl_relocrelsz += (Xword)sizeof (Rel);
11695aefb655Srie 	else
1170bf994817SAli Bahrami 		RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rel);
11715aefb655Srie 
11725aefb655Srie 	if (orsp->rel_rtype == M_R_RELATIVE)
11735aefb655Srie 		ofl->ofl_relocrelcnt++;
11745aefb655Srie 
11755aefb655Srie 	/*
11765aefb655Srie 	 * We don't perform sorting on PLT relocations because
11775aefb655Srie 	 * they have already been assigned a PLT index and if we
11785aefb655Srie 	 * were to sort them we would have to re-assign the plt indexes.
11795aefb655Srie 	 */
11805aefb655Srie 	if (!(flags & FLG_REL_PLT))
11815aefb655Srie 		ofl->ofl_reloccnt++;
11825aefb655Srie 
11835aefb655Srie 	/*
11845aefb655Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
11855aefb655Srie 	 */
11865aefb655Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
11875aefb655Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
11885aefb655Srie 
11895aefb655Srie 	/*
11905aefb655Srie 	 * Identify and possibly warn of a displacement relocation.
11915aefb655Srie 	 */
11925aefb655Srie 	if (orsp->rel_flags & FLG_REL_DISP) {
11935aefb655Srie 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
11945aefb655Srie 
11955aefb655Srie 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
11965aefb655Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
11975aefb655Srie 	}
11985aefb655Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL,
11995aefb655Srie 	    M_MACH, orsp));
12005aefb655Srie 	return (1);
12015aefb655Srie }
12025aefb655Srie 
12035aefb655Srie /*
12045aefb655Srie  * process relocation for a LOCAL symbol
12055aefb655Srie  */
1206ba2be530Sab196087 static uintptr_t
ld_reloc_local(Rel_desc * rsp,Ofl_desc * ofl)12075aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
12085aefb655Srie {
12091d9df23bSab196087 	ofl_flag_t	flags = ofl->ofl_flags;
12105aefb655Srie 	Sym_desc	*sdp = rsp->rel_sym;
12110bc07c75Srie 	Word		shndx = sdp->sd_sym->st_shndx;
12125aefb655Srie 
12135aefb655Srie 	/*
12145aefb655Srie 	 * if ((shared object) and (not pc relative relocation) and
12155aefb655Srie 	 *    (not against ABS symbol))
12165aefb655Srie 	 * then
12175aefb655Srie 	 *	build R_386_RELATIVE
12185aefb655Srie 	 * fi
12195aefb655Srie 	 */
12205aefb655Srie 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
12212926dd2eSrie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
12225aefb655Srie 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
12235aefb655Srie 	    !(rsp->rel_isdesc != NULL &&
12245aefb655Srie 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
12255aefb655Srie 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
12265aefb655Srie 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
12275aefb655Srie 		Word	ortype = rsp->rel_rtype;
12285aefb655Srie 
12295aefb655Srie 		rsp->rel_rtype = R_386_RELATIVE;
12305aefb655Srie 		if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR)
12315aefb655Srie 			return (S_ERROR);
12325aefb655Srie 		rsp->rel_rtype = ortype;
12335aefb655Srie 	}
12345aefb655Srie 
12355aefb655Srie 	/*
12365aefb655Srie 	 * If the relocation is against a 'non-allocatable' section
12375aefb655Srie 	 * and we can not resolve it now - then give a warning
12385aefb655Srie 	 * message.
12395aefb655Srie 	 *
12405aefb655Srie 	 * We can not resolve the symbol if either:
12415aefb655Srie 	 *	a) it's undefined
12425aefb655Srie 	 *	b) it's defined in a shared library and a
12435aefb655Srie 	 *	   COPY relocation hasn't moved it to the executable
12445aefb655Srie 	 *
12455aefb655Srie 	 * Note: because we process all of the relocations against the
12465aefb655Srie 	 *	text segment before any others - we know whether
12475aefb655Srie 	 *	or not a copy relocation will be generated before
12485aefb655Srie 	 *	we get here (see reloc_init()->reloc_segments()).
12495aefb655Srie 	 */
12505aefb655Srie 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
12515aefb655Srie 	    ((shndx == SHN_UNDEF) ||
12525aefb655Srie 	    ((sdp->sd_ref == REF_DYN_NEED) &&
12535aefb655Srie 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1254de777a60Sab196087 		Conv_inv_buf_t	inv_buf;
1255bf994817SAli Bahrami 		Os_desc		*osp = RELAUX_GET_OSDESC(rsp);
1256de777a60Sab196087 
12575aefb655Srie 		/*
12585aefb655Srie 		 * If the relocation is against a SHT_SUNW_ANNOTATE
12595aefb655Srie 		 * section - then silently ignore that the relocation
12605aefb655Srie 		 * can not be resolved.
12615aefb655Srie 		 */
1262bf994817SAli Bahrami 		if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
12635aefb655Srie 			return (0);
1264*1007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1265de777a60Sab196087 		    conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf),
12665aefb655Srie 		    rsp->rel_isdesc->is_file->ifl_name,
1267bf994817SAli Bahrami 		    ld_reloc_sym_name(rsp), osp->os_name);
12685aefb655Srie 		return (1);
12695aefb655Srie 	}
12705aefb655Srie 
12715aefb655Srie 	/*
12725aefb655Srie 	 * Perform relocation.
12735aefb655Srie 	 */
12745aefb655Srie 	return (ld_add_actrel(NULL, rsp, ofl));
12755aefb655Srie }
12765aefb655Srie 
1277ba2be530Sab196087 static uintptr_t
ld_reloc_TLS(Boolean local,Rel_desc * rsp,Ofl_desc * ofl)12785aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
12795aefb655Srie {
12805aefb655Srie 	Word		rtype = rsp->rel_rtype;
12815aefb655Srie 	Sym_desc	*sdp = rsp->rel_sym;
12821d9df23bSab196087 	ofl_flag_t	flags = ofl->ofl_flags;
12835aefb655Srie 	Gotndx		*gnp;
12845aefb655Srie 
12855aefb655Srie 	/*
1286d326b23bSrie 	 * If we're building an executable - use either the IE or LE access
1287d326b23bSrie 	 * model.  If we're building a shared object process any IE model.
12885aefb655Srie 	 */
1289d326b23bSrie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
12905aefb655Srie 		/*
1291d326b23bSrie 		 * Set the DF_STATIC_TLS flag.
12925aefb655Srie 		 */
12935aefb655Srie 		ofl->ofl_dtflags |= DF_STATIC_TLS;
12945aefb655Srie 
1295d326b23bSrie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
12965aefb655Srie 			/*
1297d326b23bSrie 			 * Assign a GOT entry for static TLS references.
12985aefb655Srie 			 */
129957ef7aa9SRod Evans 			if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
130057ef7aa9SRod Evans 			    GOT_REF_TLSIE, ofl, NULL)) == NULL) {
1301d326b23bSrie 
1302d326b23bSrie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1303d326b23bSrie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
130457ef7aa9SRod Evans 				    rtype, R_386_TLS_TPOFF, NULL) == S_ERROR)
13055aefb655Srie 					return (S_ERROR);
13065aefb655Srie 			}
13075aefb655Srie 
13085aefb655Srie 			/*
1309d326b23bSrie 			 * IE access model.
1310d326b23bSrie 			 */
1311d326b23bSrie 			if (IS_TLS_IE(rtype)) {
1312d326b23bSrie 				if (ld_add_actrel(FLG_REL_STLS,
1313d326b23bSrie 				    rsp, ofl) == S_ERROR)
1314d326b23bSrie 					return (S_ERROR);
1315d326b23bSrie 
1316d326b23bSrie 				/*
1317d326b23bSrie 				 * A non-pic shared object needs to adjust the
1318d326b23bSrie 				 * active relocation (indntpoff).
1319d326b23bSrie 				 */
1320d326b23bSrie 				if (((flags & FLG_OF_EXEC) == 0) &&
1321d326b23bSrie 				    (rtype == R_386_TLS_IE)) {
1322d326b23bSrie 					rsp->rel_rtype = R_386_RELATIVE;
1323d326b23bSrie 					return (ld_add_outrel(NULL, rsp, ofl));
1324d326b23bSrie 				}
1325d326b23bSrie 				return (1);
1326d326b23bSrie 			}
1327d326b23bSrie 
1328d326b23bSrie 			/*
1329d326b23bSrie 			 * Fixups are required for other executable models.
13305aefb655Srie 			 */
13315aefb655Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13325aefb655Srie 			    rsp, ofl));
13335aefb655Srie 		}
1334d326b23bSrie 
13355aefb655Srie 		/*
1336d326b23bSrie 		 * LE access model.
13375aefb655Srie 		 */
13385aefb655Srie 		if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32))
13395aefb655Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
13405aefb655Srie 
13415aefb655Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13425aefb655Srie 		    rsp, ofl));
13435aefb655Srie 	}
13445aefb655Srie 
13455aefb655Srie 	/*
1346d326b23bSrie 	 * Building a shared object.
1347d326b23bSrie 	 *
1348d326b23bSrie 	 * Assign a GOT entry for a dynamic TLS reference.
13495aefb655Srie 	 */
135057ef7aa9SRod Evans 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
135157ef7aa9SRod Evans 	    GOT_REF_TLSLD, ofl, NULL)) == NULL)) {
1352d326b23bSrie 
1353d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
135457ef7aa9SRod Evans 		    FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, NULL) == S_ERROR)
13555aefb655Srie 			return (S_ERROR);
1356d326b23bSrie 
135757ef7aa9SRod Evans 	} else if (IS_TLS_GD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
135857ef7aa9SRod Evans 	    GOT_REF_TLSGD, ofl, NULL)) == NULL)) {
1359d326b23bSrie 
1360d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1361d326b23bSrie 		    FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32,
1362d326b23bSrie 		    R_386_TLS_DTPOFF32) == S_ERROR)
13635aefb655Srie 			return (S_ERROR);
13645aefb655Srie 	}
1365d326b23bSrie 
13665aefb655Srie 	/*
13675aefb655Srie 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1368d326b23bSrie 	 * cause a call to __tls_get_addr().  Convert this relocation to that
1369d326b23bSrie 	 * symbol now, and prepare for the PLT magic.
13705aefb655Srie 	 */
13715aefb655Srie 	if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) {
13725aefb655Srie 		Sym_desc	*tlsgetsym;
13735aefb655Srie 
13745aefb655Srie 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU),
1375f5a18a30Srie 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
13765aefb655Srie 			return (S_ERROR);
1377d326b23bSrie 
13785aefb655Srie 		rsp->rel_sym = tlsgetsym;
13795aefb655Srie 		rsp->rel_rtype = R_386_PLT32;
1380d326b23bSrie 
13815aefb655Srie 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
13825aefb655Srie 			return (S_ERROR);
1383d326b23bSrie 
13845aefb655Srie 		rsp->rel_sym = sdp;
13855aefb655Srie 		rsp->rel_rtype = rtype;
13865aefb655Srie 		return (1);
13875aefb655Srie 	}
13885aefb655Srie 
13895aefb655Srie 	if (IS_TLS_LD(rtype))
13905aefb655Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
13915aefb655Srie 
13925aefb655Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
13935aefb655Srie }
13945aefb655Srie 
13955aefb655Srie /* ARGSUSED4 */
1396ba2be530Sab196087 static uintptr_t
ld_assign_got_ndx(Alist ** alpp,Gotndx * pgnp,Gotref gref,Ofl_desc * ofl,Rel_desc * rsp,Sym_desc * sdp)139757ef7aa9SRod Evans ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
13985aefb655Srie     Rel_desc *rsp, Sym_desc *sdp)
13995aefb655Srie {
140057ef7aa9SRod Evans 	Gotndx	gn, *gnp;
14015aefb655Srie 	uint_t	gotents;
14025aefb655Srie 
14035aefb655Srie 	if (pgnp)
14045aefb655Srie 		return (1);
14055aefb655Srie 
14065aefb655Srie 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
14075aefb655Srie 		gotents = 2;
14085aefb655Srie 	else
14095aefb655Srie 		gotents = 1;
14105aefb655Srie 
141157ef7aa9SRod Evans 	gn.gn_addend = 0;
141257ef7aa9SRod Evans 	gn.gn_gotndx = ofl->ofl_gotcnt;
141357ef7aa9SRod Evans 	gn.gn_gotref = gref;
14145aefb655Srie 
14155aefb655Srie 	ofl->ofl_gotcnt += gotents;
14165aefb655Srie 
14175aefb655Srie 	if (gref == GOT_REF_TLSLD) {
141857ef7aa9SRod Evans 		if (ofl->ofl_tlsldgotndx == NULL) {
141957ef7aa9SRod Evans 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
142057ef7aa9SRod Evans 				return (S_ERROR);
142157ef7aa9SRod Evans 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
14225aefb655Srie 			ofl->ofl_tlsldgotndx = gnp;
142357ef7aa9SRod Evans 		}
14245aefb655Srie 		return (1);
14255aefb655Srie 	}
14265aefb655Srie 
142757ef7aa9SRod Evans 	/*
142857ef7aa9SRod Evans 	 * GOT indexes are maintained on an Alist, where there is typically
142957ef7aa9SRod Evans 	 * only one index.  The usage of this list is to scan the list to find
143057ef7aa9SRod Evans 	 * an index, and then apply that index immediately to a relocation.
143157ef7aa9SRod Evans 	 * Thus there are no external references to these GOT index structures
143257ef7aa9SRod Evans 	 * that can be compromised by the Alist being reallocated.
143357ef7aa9SRod Evans 	 */
143457ef7aa9SRod Evans 	if (alist_append(alpp, &gn, sizeof (Gotndx), AL_CNT_SDP_GOT) == NULL)
14355aefb655Srie 		return (S_ERROR);
14365aefb655Srie 
14375aefb655Srie 	return (1);
14385aefb655Srie }
14395aefb655Srie 
1440ba2be530Sab196087 static void
ld_assign_plt_ndx(Sym_desc * sdp,Ofl_desc * ofl)14415aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14425aefb655Srie {
14435aefb655Srie 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14445aefb655Srie 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
14455aefb655Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14465aefb655Srie }
14475aefb655Srie 
14485aefb655Srie /*
14495aefb655Srie  * Initializes .got[0] with the _DYNAMIC symbol value.
14505aefb655Srie  */
1451ba2be530Sab196087 static uintptr_t
ld_fillin_gotplt(Ofl_desc * ofl)14525aefb655Srie ld_fillin_gotplt(Ofl_desc *ofl)
14535aefb655Srie {
14541d9df23bSab196087 	ofl_flag_t	flags = ofl->ofl_flags;
1455ba2be530Sab196087 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1456d326b23bSrie 
14575aefb655Srie 	if (ofl->ofl_osgot) {
14585aefb655Srie 		Sym_desc	*sdp;
14595aefb655Srie 
14605aefb655Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
1461635216b6SRod Evans 		    SYM_NOHASH, NULL, ofl)) != NULL) {
1462d326b23bSrie 			uchar_t	*genptr;
1463d326b23bSrie 
1464d326b23bSrie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
14655aefb655Srie 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
14665aefb655Srie 			/* LINTED */
14675aefb655Srie 			*(Word *)genptr = (Word)sdp->sd_sym->st_value;
1468ba2be530Sab196087 			if (bswap)
1469ba2be530Sab196087 				/* LINTED */
1470ba2be530Sab196087 				*(Word *)genptr =
1471ba2be530Sab196087 				    /* LINTED */
1472ba2be530Sab196087 				    ld_bswap_Word(*(Word *)genptr);
14735aefb655Srie 		}
14745aefb655Srie 	}
14755aefb655Srie 
14765aefb655Srie 	/*
14775aefb655Srie 	 * Fill in the reserved slot in the procedure linkage table the first
14785aefb655Srie 	 * entry is:
14795aefb655Srie 	 *  if (building a.out) {
14805aefb655Srie 	 *	PUSHL	got[1]		    # the address of the link map entry
14815aefb655Srie 	 *	JMP *	got[2]		    # the address of rtbinder
14825aefb655Srie 	 *  } else {
14835aefb655Srie 	 *	PUSHL	got[1]@GOT(%ebx)    # the address of the link map entry
14845aefb655Srie 	 *	JMP *	got[2]@GOT(%ebx)    # the address of rtbinder
14855aefb655Srie 	 *  }
14865aefb655Srie 	 */
1487d326b23bSrie 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
14885aefb655Srie 		uchar_t *pltent;
14895aefb655Srie 
14905aefb655Srie 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
1491d326b23bSrie 		if (!(flags & FLG_OF_SHAROBJ)) {
14925aefb655Srie 			pltent[0] = M_SPECIAL_INST;
14935aefb655Srie 			pltent[1] = M_PUSHL_DISP;
14945aefb655Srie 			pltent += 2;
14955aefb655Srie 			/* LINTED */
14965aefb655Srie 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
14975aefb655Srie 			    sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE);
1498ba2be530Sab196087 			if (bswap)
1499ba2be530Sab196087 				/* LINTED */
1500ba2be530Sab196087 				*(Word *)pltent =
1501ba2be530Sab196087 				    /* LINTED */
1502ba2be530Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15035aefb655Srie 			pltent += 4;
15045aefb655Srie 			pltent[0] = M_SPECIAL_INST;
15055aefb655Srie 			pltent[1] = M_JMP_DISP_IND;
15065aefb655Srie 			pltent += 2;
15075aefb655Srie 			/* LINTED */
15085aefb655Srie 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
15095aefb655Srie 			    sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE);
1510ba2be530Sab196087 			if (bswap)
1511ba2be530Sab196087 				/* LINTED */
1512ba2be530Sab196087 				*(Word *)pltent =
1513ba2be530Sab196087 				    /* LINTED */
1514ba2be530Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15155aefb655Srie 		} else {
15165aefb655Srie 			pltent[0] = M_SPECIAL_INST;
15175aefb655Srie 			pltent[1] = M_PUSHL_REG_DISP;
15185aefb655Srie 			pltent += 2;
15195aefb655Srie 			/* LINTED */
15205aefb655Srie 			*(Word *)pltent = (Word)(M_GOT_XLINKMAP *
15215aefb655Srie 			    M_GOT_ENTSIZE);
1522ba2be530Sab196087 			if (bswap)
1523ba2be530Sab196087 				/* LINTED */
1524ba2be530Sab196087 				*(Word *)pltent =
1525ba2be530Sab196087 				    /* LINTED */
1526ba2be530Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15275aefb655Srie 			pltent += 4;
15285aefb655Srie 			pltent[0] = M_SPECIAL_INST;
15295aefb655Srie 			pltent[1] = M_JMP_REG_DISP_IND;
15305aefb655Srie 			pltent += 2;
15315aefb655Srie 			/* LINTED */
15325aefb655Srie 			*(Word *)pltent = (Word)(M_GOT_XRTLD *
15335aefb655Srie 			    M_GOT_ENTSIZE);
1534ba2be530Sab196087 			if (bswap)
1535ba2be530Sab196087 				/* LINTED */
1536ba2be530Sab196087 				*(Word *)pltent =
1537ba2be530Sab196087 				    /* LINTED */
1538ba2be530Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15395aefb655Srie 		}
15405aefb655Srie 	}
15415aefb655Srie 	return (1);
15425aefb655Srie }
1543ba2be530Sab196087 
1544ba2be530Sab196087 
1545ba2be530Sab196087 
1546ba2be530Sab196087 /*
1547ba2be530Sab196087  * Template for generating "void (*)(void)" function
1548ba2be530Sab196087  */
1549ba2be530Sab196087 static const uchar_t nullfunc_tmpl[] = {	/* IA32 */
1550ba2be530Sab196087 /* 0x00 */	0xc3				/* ret */
1551ba2be530Sab196087 };
1552ba2be530Sab196087 
1553ba2be530Sab196087 
1554ba2be530Sab196087 
1555ba2be530Sab196087 /*
15563c573fccSAli Bahrami  * Function used to provide fill padding in SHF_EXECINSTR sections
15573c573fccSAli Bahrami  *
15583c573fccSAli Bahrami  * entry:
15593c573fccSAli Bahrami  *
15603c573fccSAli Bahrami  *	base - base address of section being filled
15613c573fccSAli Bahrami  *	offset - starting offset for fill within memory referenced by base
15623c573fccSAli Bahrami  *	cnt - # bytes to be filled
15633c573fccSAli Bahrami  *
15643c573fccSAli Bahrami  * exit:
15653c573fccSAli Bahrami  *	The fill has been completed.
15663c573fccSAli Bahrami  */
15673c573fccSAli Bahrami static void
execfill(void * base,off_t off,size_t cnt)15683c573fccSAli Bahrami execfill(void *base, off_t off, size_t cnt)
15693c573fccSAli Bahrami {
15703c573fccSAli Bahrami 	/*
15713c573fccSAli Bahrami 	 * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds.
15723c573fccSAli Bahrami 	 * There are no alignment constraints.
15733c573fccSAli Bahrami 	 */
15743c573fccSAli Bahrami 	(void) memset(off + (char *)base, 0x90, cnt);
15753c573fccSAli Bahrami }
15763c573fccSAli Bahrami 
15773c573fccSAli Bahrami 
15783c573fccSAli Bahrami /*
1579ba2be530Sab196087  * Return the ld_targ definition for this target.
1580ba2be530Sab196087  */
1581ba2be530Sab196087 const Target *
ld_targ_init_x86(void)1582ba2be530Sab196087 ld_targ_init_x86(void)
1583ba2be530Sab196087 {
1584ba2be530Sab196087 	static const Target _ld_targ = {
1585ba2be530Sab196087 		{			/* Target_mach */
1586ba2be530Sab196087 			M_MACH,			/* m_mach */
1587ba2be530Sab196087 			M_MACHPLUS,		/* m_machplus */
1588ba2be530Sab196087 			M_FLAGSPLUS,		/* m_flagsplus */
1589ba2be530Sab196087 			M_CLASS,		/* m_class */
1590ba2be530Sab196087 			M_DATA,			/* m_data */
1591ba2be530Sab196087 
1592ba2be530Sab196087 			M_SEGM_ALIGN,		/* m_segm_align */
1593ba2be530Sab196087 			M_SEGM_ORIGIN,		/* m_segm_origin */
1594bb3b4f6cSRod Evans 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
1595ba2be530Sab196087 			M_DATASEG_PERM,		/* m_dataseg_perm */
159669112eddSAli Bahrami 			M_STACK_PERM,		/* m_stack_perm */
1597ba2be530Sab196087 			M_WORD_ALIGN,		/* m_word_align */
1598ba2be530Sab196087 			MSG_ORIG(MSG_PTH_RTLD),	/* m_def_interp */
1599ba2be530Sab196087 
1600ba2be530Sab196087 			/* Relocation type codes */
1601ba2be530Sab196087 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
1602ba2be530Sab196087 			M_R_COPY,		/* m_r_copy */
1603ba2be530Sab196087 			M_R_GLOB_DAT,		/* m_r_glob_dat */
1604ba2be530Sab196087 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
1605ba2be530Sab196087 			M_R_NUM,		/* m_r_num */
1606ba2be530Sab196087 			M_R_NONE,		/* m_r_none */
1607ba2be530Sab196087 			M_R_RELATIVE,		/* m_r_relative */
1608ba2be530Sab196087 			M_R_REGISTER,		/* m_r_register */
1609ba2be530Sab196087 
1610ba2be530Sab196087 			/* Relocation related constants */
1611ba2be530Sab196087 			M_REL_DT_COUNT,		/* m_rel_dt_count */
1612ba2be530Sab196087 			M_REL_DT_ENT,		/* m_rel_dt_ent */
1613ba2be530Sab196087 			M_REL_DT_SIZE,		/* m_rel_dt_size */
1614ba2be530Sab196087 			M_REL_DT_TYPE,		/* m_rel_dt_type */
1615ba2be530Sab196087 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
1616ba2be530Sab196087 
1617ba2be530Sab196087 			/* GOT related constants */
1618ba2be530Sab196087 			M_GOT_ENTSIZE,		/* m_got_entsize */
1619ba2be530Sab196087 			M_GOT_XNumber,		/* m_got_xnumber */
1620ba2be530Sab196087 
1621ba2be530Sab196087 			/* PLT related constants */
1622ba2be530Sab196087 			M_PLT_ALIGN,		/* m_plt_align */
1623ba2be530Sab196087 			M_PLT_ENTSIZE,		/* m_plt_entsize */
1624ba2be530Sab196087 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
1625ba2be530Sab196087 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
1626ba2be530Sab196087 
16277e16fca0SAli Bahrami 			/* Section type of .eh_frame/.eh_frame_hdr sections */
16287e16fca0SAli Bahrami 			SHT_PROGBITS,		/* m_sht_unwind */
16297e16fca0SAli Bahrami 
1630ba2be530Sab196087 			M_DT_REGISTER,		/* m_dt_register */
1631ba2be530Sab196087 		},
1632ba2be530Sab196087 		{			/* Target_machid */
1633ba2be530Sab196087 			M_ID_ARRAY,		/* id_array */
1634ba2be530Sab196087 			M_ID_BSS,		/* id_bss */
1635ba2be530Sab196087 			M_ID_CAP,		/* id_cap */
163608278a5eSRod Evans 			M_ID_CAPINFO,		/* id_capinfo */
163708278a5eSRod Evans 			M_ID_CAPCHAIN,		/* id_capchain */
1638ba2be530Sab196087 			M_ID_DATA,		/* id_data */
1639ba2be530Sab196087 			M_ID_DYNAMIC,		/* id_dynamic */
1640ba2be530Sab196087 			M_ID_DYNSORT,		/* id_dynsort */
1641ba2be530Sab196087 			M_ID_DYNSTR,		/* id_dynstr */
1642ba2be530Sab196087 			M_ID_DYNSYM,		/* id_dynsym */
1643ba2be530Sab196087 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
1644ba2be530Sab196087 			M_ID_GOT,		/* id_got */
1645ba2be530Sab196087 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
1646ba2be530Sab196087 			M_ID_HASH,		/* id_hash */
1647ba2be530Sab196087 			M_ID_INTERP,		/* id_interp */
1648ba2be530Sab196087 			M_ID_LBSS,		/* id_lbss */
1649ba2be530Sab196087 			M_ID_LDYNSYM,		/* id_ldynsym */
1650ba2be530Sab196087 			M_ID_NOTE,		/* id_note */
1651ba2be530Sab196087 			M_ID_NULL,		/* id_null */
1652ba2be530Sab196087 			M_ID_PLT,		/* id_plt */
1653ba2be530Sab196087 			M_ID_REL,		/* id_rel */
1654ba2be530Sab196087 			M_ID_STRTAB,		/* id_strtab */
1655ba2be530Sab196087 			M_ID_SYMINFO,		/* id_syminfo */
1656ba2be530Sab196087 			M_ID_SYMTAB,		/* id_symtab */
1657ba2be530Sab196087 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
1658ba2be530Sab196087 			M_ID_TEXT,		/* id_text */
1659ba2be530Sab196087 			M_ID_TLS,		/* id_tls */
1660ba2be530Sab196087 			M_ID_TLSBSS,		/* id_tlsbss */
1661ba2be530Sab196087 			M_ID_UNKNOWN,		/* id_unknown */
1662ba2be530Sab196087 			M_ID_UNWIND,		/* id_unwind */
16637e16fca0SAli Bahrami 			M_ID_UNWINDHDR,		/* id_unwindhdr */
1664ba2be530Sab196087 			M_ID_USER,		/* id_user */
1665ba2be530Sab196087 			M_ID_VERSION,		/* id_version */
1666ba2be530Sab196087 		},
1667ba2be530Sab196087 		{			/* Target_nullfunc */
1668ba2be530Sab196087 			nullfunc_tmpl,		/* nf_template */
1669ba2be530Sab196087 			sizeof (nullfunc_tmpl),	/* nf_size */
1670ba2be530Sab196087 		},
16713c573fccSAli Bahrami 		{			/* Target_fillfunc */
16723c573fccSAli Bahrami 			execfill		/* ff_execfill */
16733c573fccSAli Bahrami 		},
1674ba2be530Sab196087 		{			/* Target_machrel */
1675ba2be530Sab196087 			reloc_table,
1676ba2be530Sab196087 
1677ba2be530Sab196087 			ld_init_rel,		/* mr_init_rel */
1678ba2be530Sab196087 			ld_mach_eflags,		/* mr_mach_eflags */
1679ba2be530Sab196087 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
1680ba2be530Sab196087 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1681ba2be530Sab196087 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
1682ba2be530Sab196087 			ld_perform_outreloc,	/* mr_perform_outreloc */
1683ba2be530Sab196087 			ld_do_activerelocs,	/* mr_do_activerelocs */
1684ba2be530Sab196087 			ld_add_outrel,		/* mr_add_outrel */
1685ba2be530Sab196087 			NULL,			/* mr_reloc_register */
1686ba2be530Sab196087 			ld_reloc_local,		/* mr_reloc_local */
1687ba2be530Sab196087 			NULL,			/* mr_reloc_GOTOP */
1688ba2be530Sab196087 			ld_reloc_TLS,		/* mr_reloc_TLS */
1689ba2be530Sab196087 			NULL,			/* mr_assign_got */
169057ef7aa9SRod Evans 			ld_find_got_ndx,	/* mr_find_got_ndx */
1691ba2be530Sab196087 			ld_calc_got_offset,	/* mr_calc_got_offset */
1692ba2be530Sab196087 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
1693ba2be530Sab196087 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
1694ba2be530Sab196087 			NULL,			/* mr_allocate_got */
1695ba2be530Sab196087 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
1696ba2be530Sab196087 		},
1697ba2be530Sab196087 		{			/* Target_machsym */
1698ba2be530Sab196087 			NULL,			/* ms_reg_check */
1699ba2be530Sab196087 			NULL,			/* ms_mach_sym_typecheck */
1700ba2be530Sab196087 			NULL,			/* ms_is_regsym */
1701ba2be530Sab196087 			NULL,			/* ms_reg_find */
1702ba2be530Sab196087 			NULL			/* ms_reg_enter */
1703ba2be530Sab196087 		}
1704ba2be530Sab196087 	};
1705ba2be530Sab196087 
1706ba2be530Sab196087 	return (&_ld_targ);
1707ba2be530Sab196087 }
1708