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  *
278501SRod.Evans@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
281618Srie  * Use is subject to license terms.
291618Srie  */
301618Srie 
316206Sab196087 /* Get the x86 version of the relocation engine */
326206Sab196087 #define	DO_RELOC_LIBLD_X86
336206Sab196087 
341618Srie #include	<string.h>
351618Srie #include	<stdio.h>
361618Srie #include	<sys/elf_386.h>
371618Srie #include	<debug.h>
381618Srie #include	<reloc.h>
396206Sab196087 #include	<i386/machdep_x86.h>
401618Srie #include	"msg.h"
411618Srie #include	"_libld.h"
421618Srie 
43*9131SRod.Evans@Sun.COM /*
44*9131SRod.Evans@Sun.COM  * Search the GOT index list for a GOT entry with a matching reference.
45*9131SRod.Evans@Sun.COM  */
46*9131SRod.Evans@Sun.COM /* ARGSUSED3 */
47*9131SRod.Evans@Sun.COM static Gotndx *
48*9131SRod.Evans@Sun.COM ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc)
49*9131SRod.Evans@Sun.COM {
50*9131SRod.Evans@Sun.COM 	Aliste	idx;
51*9131SRod.Evans@Sun.COM 	Gotndx	*gnp;
52*9131SRod.Evans@Sun.COM 
53*9131SRod.Evans@Sun.COM 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
54*9131SRod.Evans@Sun.COM 		return (ofl->ofl_tlsldgotndx);
55*9131SRod.Evans@Sun.COM 
56*9131SRod.Evans@Sun.COM 	for (ALIST_TRAVERSE(alp, idx, gnp)) {
57*9131SRod.Evans@Sun.COM 		if (gnp->gn_gotref == gref)
58*9131SRod.Evans@Sun.COM 			return (gnp);
59*9131SRod.Evans@Sun.COM 	}
60*9131SRod.Evans@Sun.COM 	return (NULL);
61*9131SRod.Evans@Sun.COM }
626206Sab196087 
63*9131SRod.Evans@Sun.COM static Xword
64*9131SRod.Evans@Sun.COM ld_calc_got_offset(Rel_desc *rdesc, Ofl_desc *ofl)
65*9131SRod.Evans@Sun.COM {
66*9131SRod.Evans@Sun.COM 	Os_desc		*osp = ofl->ofl_osgot;
67*9131SRod.Evans@Sun.COM 	Sym_desc	*sdp = rdesc->rel_sym;
68*9131SRod.Evans@Sun.COM 	Xword		gotndx;
69*9131SRod.Evans@Sun.COM 	Gotref		gref;
70*9131SRod.Evans@Sun.COM 	Gotndx		*gnp;
71*9131SRod.Evans@Sun.COM 
72*9131SRod.Evans@Sun.COM 	if (rdesc->rel_flags & FLG_REL_DTLS)
73*9131SRod.Evans@Sun.COM 		gref = GOT_REF_TLSGD;
74*9131SRod.Evans@Sun.COM 	else if (rdesc->rel_flags & FLG_REL_MTLS)
75*9131SRod.Evans@Sun.COM 		gref = GOT_REF_TLSLD;
76*9131SRod.Evans@Sun.COM 	else if (rdesc->rel_flags & FLG_REL_STLS)
77*9131SRod.Evans@Sun.COM 		gref = GOT_REF_TLSIE;
78*9131SRod.Evans@Sun.COM 	else
79*9131SRod.Evans@Sun.COM 		gref = GOT_REF_GENERIC;
80*9131SRod.Evans@Sun.COM 
81*9131SRod.Evans@Sun.COM 	gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
82*9131SRod.Evans@Sun.COM 	assert(gnp);
83*9131SRod.Evans@Sun.COM 
84*9131SRod.Evans@Sun.COM 	gotndx = (Xword)gnp->gn_gotndx;
85*9131SRod.Evans@Sun.COM 
86*9131SRod.Evans@Sun.COM 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
87*9131SRod.Evans@Sun.COM 	    (rdesc->rel_rtype == R_386_TLS_DTPOFF32))
88*9131SRod.Evans@Sun.COM 		gotndx++;
89*9131SRod.Evans@Sun.COM 
90*9131SRod.Evans@Sun.COM 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
91*9131SRod.Evans@Sun.COM }
926206Sab196087 
936206Sab196087 static Word
941618Srie ld_init_rel(Rel_desc *reld, void *reloc)
951618Srie {
96*9131SRod.Evans@Sun.COM 	Rel	*rel = (Rel *)reloc;
971618Srie 
981618Srie 	/* LINTED */
996206Sab196087 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
1001618Srie 	reld->rel_roffset = rel->r_offset;
1011618Srie 	reld->rel_raddend = 0;
1021618Srie 	reld->rel_typedata = 0;
1031618Srie 
1041618Srie 	return ((Word)ELF_R_SYM(rel->r_info));
1051618Srie }
1061618Srie 
1076206Sab196087 static void
1081618Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
1091618Srie {
1101618Srie 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
1111618Srie }
1121618Srie 
1136206Sab196087 static void
1141618Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
1151618Srie {
1161618Srie 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
1171618Srie 		/*
1181618Srie 		 * Create this entry if we are going to create a PLT table.
1191618Srie 		 */
1201618Srie 		if (ofl->ofl_pltcnt)
1211618Srie 			(*cnt)++;		/* DT_PLTGOT */
1221618Srie 	}
1231618Srie }
1241618Srie 
1256206Sab196087 static void
1262145Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
1271618Srie {
1281618Srie 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
1291618Srie 		(*dyn)->d_tag = DT_PLTGOT;
1301618Srie 		if (ofl->ofl_osgot)
1311618Srie 			(*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
1321618Srie 		else
1331618Srie 			(*dyn)->d_un.d_ptr = 0;
1341618Srie 		(*dyn)++;
1351618Srie 	}
1361618Srie }
1371618Srie 
1386206Sab196087 static Xword
1391618Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
1401618Srie {
1411618Srie 	Xword	value;
1421618Srie 
1431618Srie 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
1441618Srie 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
1451618Srie 	return (value);
1461618Srie }
1471618Srie 
1481618Srie /*
1491618Srie  *  Build a single plt entry - code is:
1501618Srie  *	if (building a.out)
1511618Srie  *		JMP	*got_off
1521618Srie  *	else
1531618Srie  *		JMP	*got_off@GOT(%ebx)
1541618Srie  *	PUSHL	&rel_off
1551618Srie  *	JMP	-n(%pc)		# -n is pcrel offset to first plt entry
1561618Srie  *
1571618Srie  *	The got_off@GOT entry gets filled with the address of the PUSHL,
1581618Srie  *	so the first pass through the plt jumps back here, jumping
1591618Srie  *	in turn to the first plt entry, which jumps to the dynamic
1601618Srie  *	linker.	 The dynamic linker then patches the GOT, rerouting
1611618Srie  *	future plt calls to the proper destination.
1621618Srie  */
1631618Srie static void
1641618Srie plt_entry(Ofl_desc * ofl, Word rel_off, Sym_desc * sdp)
1651618Srie {
1661618Srie 	uchar_t		*pltent, *gotent;
1671618Srie 	Sword		plt_off;
1681618Srie 	Word		got_off;
1696206Sab196087 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1701618Srie 
1711618Srie 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
1721618Srie 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
1731618Srie 	    M_PLT_ENTSIZE);
1741618Srie 	pltent = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf) + plt_off;
1751618Srie 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
1761618Srie 
1771618Srie 	/*
1781618Srie 	 * Fill in the got entry with the address of the next instruction.
1791618Srie 	 */
1801618Srie 	/* LINTED */
1811618Srie 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
1821618Srie 	    M_PLT_INSSIZE;
1836206Sab196087 	if (bswap)
1846206Sab196087 		/* LINTED */
1856206Sab196087 		*(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
1861618Srie 
1871618Srie 	if (!(ofl->ofl_flags & FLG_OF_SHAROBJ)) {
1881618Srie 		pltent[0] = M_SPECIAL_INST;
1891618Srie 		pltent[1] = M_JMP_DISP_IND;
1901618Srie 		pltent += 2;
1911618Srie 		/* LINTED */
1921618Srie 		*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->sh_addr +
1934734Sab196087 		    got_off);
1941618Srie 	} else {
1951618Srie 		pltent[0] = M_SPECIAL_INST;
1961618Srie 		pltent[1] = M_JMP_REG_DISP_IND;
1971618Srie 		pltent += 2;
1981618Srie 		/* LINTED */
1991618Srie 		*(Word *)pltent = (Word)got_off;
2001618Srie 	}
2016206Sab196087 	if (bswap)
2026206Sab196087 		/* LINTED */
2036206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2041618Srie 	pltent += 4;
2051618Srie 
2061618Srie 	pltent[0] = M_INST_PUSHL;
2071618Srie 	pltent++;
2081618Srie 	/* LINTED */
2091618Srie 	*(Word *)pltent = (Word)rel_off;
2106206Sab196087 	if (bswap)
2116206Sab196087 		/* LINTED */
2126206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2131618Srie 	pltent += 4;
2141618Srie 
2151618Srie 	plt_off = -(plt_off + 16);	/* JMP, PUSHL, JMP take 16 bytes */
2161618Srie 	pltent[0] = M_INST_JMP;
2171618Srie 	pltent++;
2181618Srie 	/* LINTED */
2191618Srie 	*(Word *)pltent = (Word)plt_off;
2206206Sab196087 	if (bswap)
2216206Sab196087 		/* LINTED */
2226206Sab196087 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2231618Srie }
2241618Srie 
2256206Sab196087 static uintptr_t
2261618Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl)
2271618Srie {
2281618Srie 	Os_desc *	relosp, * osp = 0;
2291618Srie 	Word		ndx, roffset, value;
2301618Srie 	Rel		rea;
2311618Srie 	char		*relbits;
2321618Srie 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
2331618Srie 	int		sectmoved = 0;
2341618Srie 
2351618Srie 	sdp = orsp->rel_sym;
2361618Srie 
2371618Srie 	/*
2381618Srie 	 * If the section this relocation is against has been discarded
2391618Srie 	 * (-zignore), then also discard (skip) the relocation itself.
2401618Srie 	 */
2411618Srie 	if (orsp->rel_isdesc && ((orsp->rel_flags &
2421618Srie 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
2431618Srie 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
2441618Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
2451618Srie 		return (1);
2461618Srie 	}
2471618Srie 
2481618Srie 	/*
2491618Srie 	 * If this is a relocation against a move table, or expanded move
2501618Srie 	 * table, adjust the relocation entries.
2511618Srie 	 */
2521618Srie 	if (orsp->rel_move)
2531618Srie 		ld_adj_movereloc(ofl, orsp);
2541618Srie 
2551618Srie 	/*
2561618Srie 	 * If this is a relocation against a section using a partial initialized
2571618Srie 	 * symbol, adjust the embedded symbol info.
2581618Srie 	 *
2591618Srie 	 * The second argument of the am_I_partial() is the value stored at the
2601618Srie 	 * target address relocation is going to be applied.
2611618Srie 	 */
2621618Srie 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
263*9131SRod.Evans@Sun.COM 		if (ofl->ofl_parsyms &&
2641618Srie 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
2651618Srie 		    /* LINTED */
2661618Srie 		    (psym = ld_am_I_partial(orsp, *(Xword *)
2671618Srie 		    ((uchar_t *)(orsp->rel_isdesc->is_indata->d_buf) +
2681618Srie 		    orsp->rel_roffset)))) {
2691618Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
2701618Srie 			sectmoved = 1;
2714734Sab196087 		}
2721618Srie 	}
2731618Srie 
2741618Srie 	value = sdp->sd_sym->st_value;
2751618Srie 
2761618Srie 	if (orsp->rel_flags & FLG_REL_GOT) {
2771618Srie 		osp = ofl->ofl_osgot;
2781618Srie 		roffset = (Word)ld_calc_got_offset(orsp, ofl);
2791618Srie 
2801618Srie 	} else if (orsp->rel_flags & FLG_REL_PLT) {
2811618Srie 		/*
2821618Srie 		 * Note that relocations for PLT's actually
2831618Srie 		 * cause a relocation againt the GOT.
2841618Srie 		 */
2851618Srie 		osp = ofl->ofl_osplt;
2861618Srie 		roffset = (Word) (ofl->ofl_osgot->os_shdr->sh_addr) +
2871618Srie 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2881618Srie 
2891618Srie 		plt_entry(ofl, osp->os_relosdesc->os_szoutrels, sdp);
2901618Srie 
2911618Srie 	} else if (orsp->rel_flags & FLG_REL_BSS) {
2921618Srie 		/*
2931618Srie 		 * This must be a R_386_COPY.  For these set the roffset to
2941618Srie 		 * point to the new symbols location.
2951618Srie 		 */
2961618Srie 		osp = ofl->ofl_isbss->is_osdesc;
2971618Srie 		roffset = (Word)value;
2981618Srie 	} else {
2991618Srie 		osp = orsp->rel_osdesc;
3001618Srie 
3011618Srie 		/*
3021618Srie 		 * Calculate virtual offset of reference point; equals offset
3031618Srie 		 * into section + vaddr of section for loadable sections, or
3041618Srie 		 * offset plus section displacement for nonloadable sections.
3051618Srie 		 */
3061618Srie 		roffset = orsp->rel_roffset +
3071618Srie 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
3081618Srie 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
3091618Srie 			roffset += orsp->rel_isdesc->is_osdesc->
3101618Srie 			    os_shdr->sh_addr;
3111618Srie 	}
3121618Srie 
3131618Srie 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
3141618Srie 		relosp = ofl->ofl_osrel;
3151618Srie 
3161618Srie 	/*
3171618Srie 	 * Assign the symbols index for the output relocation.  If the
3181618Srie 	 * relocation refers to a SECTION symbol then it's index is based upon
3191618Srie 	 * the output sections symbols index.  Otherwise the index can be
3201618Srie 	 * derived from the symbols index itself.
3211618Srie 	 */
3221618Srie 	if (orsp->rel_rtype == R_386_RELATIVE)
3231618Srie 		ndx = STN_UNDEF;
3241618Srie 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
3251618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
3261618Srie 		if (sectmoved == 0) {
3271618Srie 			/*
3281618Srie 			 * Check for a null input section. This can
3291618Srie 			 * occur if this relocation references a symbol
3301618Srie 			 * generated by sym_add_sym().
3311618Srie 			 */
332*9131SRod.Evans@Sun.COM 			if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
333*9131SRod.Evans@Sun.COM 				ndx = sdp->sd_isc->is_osdesc->os_identndx;
3341618Srie 			else
3351618Srie 				ndx = sdp->sd_shndx;
3361618Srie 		} else
3378159SAli.Bahrami@Sun.COM 			ndx = ofl->ofl_parexpnndx;
3381618Srie 	} else
3391618Srie 		ndx = sdp->sd_symndx;
3401618Srie 
3415892Sab196087 	/*
3425892Sab196087 	 * If we have a replacement value for the relocation
3435892Sab196087 	 * target, put it in place now.
3445892Sab196087 	 */
3455892Sab196087 	if (orsp->rel_flags & FLG_REL_NADDEND) {
3465892Sab196087 		Xword	addend = orsp->rel_raddend;
3475892Sab196087 		uchar_t	*addr;
3485892Sab196087 
3495892Sab196087 		/*
3505892Sab196087 		 * Get the address of the data item we need to modify.
3515892Sab196087 		 */
3525892Sab196087 		addr = (uchar_t *)((uintptr_t)orsp->rel_roffset +
3535892Sab196087 		    (uintptr_t)_elf_getxoff(orsp->rel_isdesc->is_indata));
3545892Sab196087 		addr += (uintptr_t)orsp->rel_osdesc->os_outdata->d_buf;
3555892Sab196087 		if (ld_reloc_targval_set(ofl, orsp, addr, addend) == 0)
3565892Sab196087 			return (S_ERROR);
3575892Sab196087 	}
3585892Sab196087 
3591618Srie 	relbits = (char *)relosp->os_outdata->d_buf;
3601618Srie 
3611618Srie 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
3621618Srie 	rea.r_offset = roffset;
3631618Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_REL, &rea, relosp->os_name,
3641618Srie 	    orsp->rel_sname));
3651618Srie 
3661618Srie 	/*
3671618Srie 	 * Assert we haven't walked off the end of our relocation table.
3681618Srie 	 */
3691618Srie 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
3701618Srie 
3711618Srie 	(void) memcpy((relbits + relosp->os_szoutrels),
3721618Srie 	    (char *)&rea, sizeof (Rel));
3731618Srie 	relosp->os_szoutrels += sizeof (Rel);
3741618Srie 
3751618Srie 	/*
3761618Srie 	 * Determine if this relocation is against a non-writable, allocatable
3771618Srie 	 * section.  If so we may need to provide a text relocation diagnostic.
3781618Srie 	 * Note that relocations against the .plt (R_386_JMP_SLOT) actually
3791618Srie 	 * result in modifications to the .got.
3801618Srie 	 */
3811618Srie 	if (orsp->rel_rtype == R_386_JMP_SLOT)
3821618Srie 		osp = ofl->ofl_osgot;
3831618Srie 
3841618Srie 	ld_reloc_remain_entry(orsp, osp, ofl);
3851618Srie 	return (1);
3861618Srie }
3871618Srie 
3881618Srie /*
3891618Srie  * i386 Instructions for TLS processing
3901618Srie  */
3911618Srie static uchar_t tlsinstr_gd_ie[] = {
3921618Srie 	/*
3931618Srie 	 * 0x00	movl %gs:0x0, %eax
3941618Srie 	 */
3951618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
3961618Srie 	/*
3971618Srie 	 * 0x06	addl x(%eax), %eax
3981618Srie 	 * 0x0c ...
3991618Srie 	 */
4001618Srie 	0x03, 0x80, 0x00, 0x00, 0x00, 0x00
4011618Srie };
4021618Srie 
4031618Srie static uchar_t tlsinstr_gd_le[] = {
4041618Srie 	/*
4051618Srie 	 * 0x00 movl %gs:0x0, %eax
4061618Srie 	 */
4071618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
4081618Srie 	/*
4091618Srie 	 * 0x06 addl $0x0, %eax
4101618Srie 	 */
4111618Srie 	0x05, 0x00, 0x00, 0x00, 0x00,
4121618Srie 	/*
4131618Srie 	 * 0x0b nop
4141618Srie 	 * 0x0c
4151618Srie 	 */
4161618Srie 	0x90
4171618Srie };
4181618Srie 
4191618Srie static uchar_t tlsinstr_gd_ie_movgs[] = {
4201618Srie 	/*
4211618Srie 	 *	movl %gs:0x0,%eax
4221618Srie 	 */
4231618Srie 	0x65, 0xa1, 0x00, 0x00, 0x00, 00
4241618Srie };
4251618Srie 
4261618Srie #define	TLS_GD_IE_MOV	0x8b	/* movl opcode */
4271618Srie #define	TLS_GD_IE_POP	0x58	/* popl + reg */
4281618Srie 
4291618Srie #define	TLS_GD_LE_MOVL	0xb8	/* movl + reg */
4301618Srie 
4311618Srie #define	TLS_NOP		0x90	/* NOP instruction */
4321618Srie 
4331618Srie #define	MODRM_MSK_MOD	0xc0
4341618Srie #define	MODRM_MSK_RO	0x38
4351618Srie #define	MODRM_MSK_RM	0x07
4361618Srie 
4371618Srie #define	SIB_MSK_SS	0xc0
4381618Srie #define	SIB_MSK_IND	0x38
4391618Srie #define	SIB_MSK_BS	0x07
4401618Srie 
4411618Srie static Fixupret
4421618Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
4431618Srie {
4441618Srie 	Sym_desc	*sdp = arsp->rel_sym;
4451618Srie 	Word		rtype = arsp->rel_rtype;
4461618Srie 	uchar_t		*offset, r1, r2;
4471618Srie 
4481618Srie 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
4491618Srie 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
4501618Srie 	    (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf);
4511618Srie 
4521618Srie 	if (sdp->sd_ref == REF_DYN_NEED) {
4531618Srie 		/*
4541618Srie 		 * IE reference model
4551618Srie 		 */
4561618Srie 		switch (rtype) {
4571618Srie 		case R_386_TLS_GD:
4581618Srie 			/*
4591618Srie 			 * Transition:
4601618Srie 			 *	0x0 leal x@tlsgd(,r1,1), %eax
4611618Srie 			 *	0x7 call ___tls_get_addr
4621618Srie 			 *	0xc
4631618Srie 			 * To:
4641618Srie 			 *	0x0 movl %gs:0, %eax
4651618Srie 			 *	0x6 addl x@gotntpoff(r1), %eax
4661618Srie 			 */
4671618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4683304Srie 			    R_386_TLS_GOTIE, arsp));
4691618Srie 			arsp->rel_rtype = R_386_TLS_GOTIE;
4701618Srie 			arsp->rel_roffset += 5;
4711618Srie 
4721618Srie 			/*
4733304Srie 			 * Adjust 'offset' to beginning of instruction
4741618Srie 			 * sequence.
4751618Srie 			 */
4761618Srie 			offset -= 3;
4771618Srie 			r1 = (offset[2] & SIB_MSK_IND) >> 3;
4781618Srie 			(void) memcpy(offset, tlsinstr_gd_ie,
4791618Srie 			    sizeof (tlsinstr_gd_ie));
4801618Srie 
4811618Srie 			/*
4821618Srie 			 * set register %r1 into the addl
4831618Srie 			 * instruction.
4841618Srie 			 */
4851618Srie 			offset[0x7] |= r1;
4861618Srie 			return (FIX_RELOC);
4871618Srie 
4881618Srie 		case R_386_TLS_GD_PLT:
4891618Srie 			/*
4901618Srie 			 * Fixup done via the TLS_GD relocation
4911618Srie 			 */
4921618Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
4933304Srie 			    R_386_NONE, arsp));
4941618Srie 			return (FIX_DONE);
4951618Srie 		}
4961618Srie 	}
4971618Srie 
4981618Srie 	/*
4991618Srie 	 * LE reference model
5001618Srie 	 */
5011618Srie 	switch (rtype) {
5021618Srie 	case R_386_TLS_GD:
5031618Srie 		/*
5041618Srie 		 * Transition:
5051618Srie 		 *	0x0 leal x@tlsgd(,r1,1), %eax
5061618Srie 		 *	0x7 call ___tls_get_addr
5071618Srie 		 *	0xc
5081618Srie 		 * To:
5091618Srie 		 *	0x0 movl %gs:0, %eax
5101618Srie 		 *	0x6 addl $x@ntpoff, %eax
5111618Srie 		 *	0xb nop
5121618Srie 		 *	0xc
5131618Srie 		 */
5141618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
5153304Srie 		    R_386_TLS_LE, arsp));
5161618Srie 
5171618Srie 		arsp->rel_rtype = R_386_TLS_LE;
5181618Srie 		arsp->rel_roffset += 4;
5191618Srie 
5201618Srie 		/*
5213304Srie 		 * Adjust 'offset' to beginning of instruction
5221618Srie 		 * sequence.
5231618Srie 		 */
5241618Srie 		offset -= 3;
5251618Srie 		(void) memcpy(offset, tlsinstr_gd_le,
5261618Srie 		    sizeof (tlsinstr_gd_le));
5271618Srie 		return (FIX_RELOC);
5281618Srie 
5291618Srie 	case R_386_TLS_GD_PLT:
5301618Srie 	case R_386_PLT32:
5311618Srie 		/*
5321618Srie 		 * Fixup done via the TLS_GD relocation
5331618Srie 		 */
5341618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
5353304Srie 		    R_386_NONE, arsp));
5361618Srie 		return (FIX_DONE);
5371618Srie 
5381618Srie 	case R_386_TLS_LDM_PLT:
5391618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
5403304Srie 		    R_386_NONE, arsp));
5411618Srie 
5421618Srie 		/*
5431618Srie 		 * Transition:
5441618Srie 		 *	call __tls_get_addr()
5451618Srie 		 * to:
5461618Srie 		 *	nop
5471618Srie 		 *	nop
5481618Srie 		 *	nop
5491618Srie 		 *	nop
5501618Srie 		 *	nop
5511618Srie 		 */
5521618Srie 		*(offset - 1) = TLS_NOP;
5531618Srie 		*(offset) = TLS_NOP;
5541618Srie 		*(offset + 1) = TLS_NOP;
5551618Srie 		*(offset + 2) = TLS_NOP;
5561618Srie 		*(offset + 3) = TLS_NOP;
5571618Srie 		return (FIX_DONE);
5581618Srie 
5591618Srie 	case R_386_TLS_LDM:
5601618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
5613304Srie 		    R_386_NONE, arsp));
5621618Srie 
5631618Srie 		/*
5641618Srie 		 * Transition:
5651618Srie 		 *
5661618Srie 		 *  0x00 leal x1@tlsldm(%ebx), %eax
5671618Srie 		 *  0x06 call ___tls_get_addr
5681618Srie 		 *
5691618Srie 		 * to:
5701618Srie 		 *
5711618Srie 		 *  0x00 movl %gs:0, %eax
5721618Srie 		 */
5731618Srie 		(void) memcpy(offset - 2, tlsinstr_gd_ie_movgs,
5741618Srie 		    sizeof (tlsinstr_gd_ie_movgs));
5751618Srie 		return (FIX_DONE);
5761618Srie 
5771618Srie 	case R_386_TLS_LDO_32:
5781618Srie 		/*
5791618Srie 		 *  Instructions:
5801618Srie 		 *
5811618Srie 		 *  0x10 leal x1@dtpoff(%eax), %edx	R_386_TLS_LDO_32
5821618Srie 		 *		to
5831618Srie 		 *  0x10 leal x1@ntpoff(%eax), %edx	R_386_TLS_LE
5841618Srie 		 *
5851618Srie 		 */
5861618Srie 		offset -= 2;
5871618Srie 
5881618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
5893304Srie 		    R_386_TLS_LE, arsp));
5901618Srie 		arsp->rel_rtype = R_386_TLS_LE;
5911618Srie 		return (FIX_RELOC);
5921618Srie 
5931618Srie 	case R_386_TLS_GOTIE:
5941618Srie 		/*
5951618Srie 		 * These transitions are a little different than the
5961618Srie 		 * others, in that we could have multiple instructions
5971618Srie 		 * pointed to by a single relocation.  Depending upon the
5981618Srie 		 * instruction, we perform a different code transition.
5991618Srie 		 *
6001618Srie 		 * Here's the known transitions:
6011618Srie 		 *
6021618Srie 		 *  1) movl foo@gotntpoff(%reg1), %reg2
6031618Srie 		 *	0x8b, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
6041618Srie 		 *
6051618Srie 		 *  2) addl foo@gotntpoff(%reg1), %reg2
6061618Srie 		 *	0x03, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
6071618Srie 		 *
6081618Srie 		 *  Transitions IE -> LE
6091618Srie 		 *
6101618Srie 		 *  1) movl $foo@ntpoff, %reg2
6111618Srie 		 *	0xc7, 0xc0 | reg2, foo@ntpoff
6121618Srie 		 *
6131618Srie 		 *  2) addl $foo@ntpoff, %reg2
6141618Srie 		 *	0x81, 0xc0 | reg2, foo@ntpoff
6151618Srie 		 *
6161618Srie 		 * Note: reg1 != 4 (%esp)
6171618Srie 		 */
6181618Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
6193304Srie 		    R_386_TLS_LE, arsp));
6201618Srie 		arsp->rel_rtype = R_386_TLS_LE;
6211618Srie 
6221618Srie 		offset -= 2;
6231618Srie 		r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6241618Srie 		if (offset[0] == 0x8b) {
6251618Srie 			/* case 1 above */
6261618Srie 			offset[0] = 0xc7;	/* movl */
6271618Srie 			offset[1] = 0xc0 | r2;
6281618Srie 			return (FIX_RELOC);
6291618Srie 		}
6301618Srie 
6311618Srie 		if (offset[0] == 0x03) {
6321618Srie 			/* case 2 above */
6331618Srie 			assert(offset[0] == 0x03);
6341618Srie 			offset[0] = 0x81;	/* addl */
6351618Srie 			offset[1] = 0xc0 | r2;
6361618Srie 			return (FIX_RELOC);
6371618Srie 		}
6381618Srie 
6391618Srie 		/*
6401618Srie 		 * Unexpected instruction sequence - fatal error.
6411618Srie 		 */
6424734Sab196087 		{
6434734Sab196087 			Conv_inv_buf_t	inv_buf;
6444734Sab196087 
6454734Sab196087 			eprintf(ofl->ofl_lml, ERR_FATAL,
6464734Sab196087 			    MSG_INTL(MSG_REL_BADTLSINS),
6474734Sab196087 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
6484734Sab196087 			    arsp->rel_isdesc->is_file->ifl_name,
6494734Sab196087 			    demangle(arsp->rel_sname),
6504734Sab196087 			    arsp->rel_isdesc->is_name,
6514734Sab196087 			    EC_OFF(arsp->rel_roffset));
6524734Sab196087 		}
6531618Srie 		return (FIX_ERROR);
6541618Srie 
6551618Srie 	case R_386_TLS_IE:
6561618Srie 		/*
6571618Srie 		 * These transitions are a little different than the
6581618Srie 		 * others, in that we could have multiple instructions
6591618Srie 		 * pointed to by a single relocation.  Depending upon the
6601618Srie 		 * instruction, we perform a different code transition.
6611618Srie 		 *
6621618Srie 		 * Here's the known transitions:
6631618Srie 		 *  1) movl foo@indntpoff, %eax
6641618Srie 		 *	0xa1, foo@indntpoff
6651618Srie 		 *
6661618Srie 		 *  2) movl foo@indntpoff, %eax
6671618Srie 		 *	0x8b, 0x05 | (reg << 3), foo@gotntpoff
6681618Srie 		 *
6691618Srie 		 *  3) addl foo@indntpoff, %eax
6701618Srie 		 *	0x03, 0x05 | (reg << 3), foo@gotntpoff
6711618Srie 		 *
6721618Srie 		 *  Transitions IE -> LE
6731618Srie 		 *
6741618Srie 		 *  1) movl $foo@ntpoff, %eax
6751618Srie 		 *	0xb8, foo@ntpoff
6761618Srie 		 *
6771618Srie 		 *  2) movl $foo@ntpoff, %reg
6781618Srie 		 *	0xc7, 0xc0 | reg, foo@ntpoff
6791618Srie 		 *
6801618Srie 		 *  3) addl $foo@ntpoff, %reg
6811618Srie 		 *	0x81, 0xc0 | reg, foo@ntpoff
6821618Srie 		 */
6831618Srie 		arsp->rel_rtype = R_386_TLS_LE;
6841618Srie 		offset--;
6851618Srie 		if (offset[0] == 0xa1) {
6861618Srie 			/* case 1 above */
6871618Srie 			offset[0] = 0xb8;	/*  movl */
6881618Srie 			return (FIX_RELOC);
6891618Srie 		}
6901618Srie 
6911618Srie 		offset--;
6921618Srie 		if (offset[0] == 0x8b) {
6931618Srie 			/* case 2 above */
6941618Srie 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6951618Srie 			offset[0] = 0xc7;	/* movl */
6961618Srie 			offset[1] = 0xc0 | r2;
6971618Srie 			return (FIX_RELOC);
6981618Srie 		}
6991618Srie 		if (offset[0] == 0x03) {
7001618Srie 			/* case 3 above */
7011618Srie 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
7021618Srie 			offset[0] = 0x81;	/* addl */
7031618Srie 			offset[1] = 0xc0 | r2;
7041618Srie 			return (FIX_RELOC);
7051618Srie 		}
7061618Srie 		/*
7071618Srie 		 * Unexpected instruction sequence - fatal error.
7081618Srie 		 */
7094734Sab196087 		{
7104734Sab196087 			Conv_inv_buf_t	inv_buf;
7114734Sab196087 
7124734Sab196087 			eprintf(ofl->ofl_lml, ERR_FATAL,
7134734Sab196087 			    MSG_INTL(MSG_REL_BADTLSINS),
7144734Sab196087 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
7154734Sab196087 			    arsp->rel_isdesc->is_file->ifl_name,
7164734Sab196087 			    demangle(arsp->rel_sname),
7174734Sab196087 			    arsp->rel_isdesc->is_name,
7184734Sab196087 			    EC_OFF(arsp->rel_roffset));
7194734Sab196087 		}
7201618Srie 		return (FIX_ERROR);
7211618Srie 	}
7221618Srie 	return (FIX_RELOC);
7231618Srie }
7241618Srie 
7256206Sab196087 static uintptr_t
7261618Srie ld_do_activerelocs(Ofl_desc *ofl)
7271618Srie {
7281618Srie 	Rel_desc	*arsp;
7291618Srie 	Rel_cache	*rcp;
730*9131SRod.Evans@Sun.COM 	Aliste		idx;
7311618Srie 	uintptr_t	return_code = 1;
7326299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
7331618Srie 
734*9131SRod.Evans@Sun.COM 	if (ofl->ofl_actrels)
7352647Srie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
7362647Srie 
7371618Srie 	/*
7381618Srie 	 * Process active relocations.
7391618Srie 	 */
740*9131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_actrels, idx, rcp)) {
7411618Srie 		/* LINTED */
7421618Srie 		for (arsp = (Rel_desc *)(rcp + 1);
7431618Srie 		    arsp < rcp->rc_free; arsp++) {
7441618Srie 			uchar_t		*addr;
7451618Srie 			Xword 		value;
7461618Srie 			Sym_desc	*sdp;
7471618Srie 			const char	*ifl_name;
7481618Srie 			Xword		refaddr;
7491618Srie 			int		moved = 0;
7501618Srie 			Gotref		gref;
7511618Srie 
7521618Srie 			/*
7531618Srie 			 * If the section this relocation is against has been
7541618Srie 			 * discarded (-zignore), then discard (skip) the
7551618Srie 			 * relocation itself.
7561618Srie 			 */
7571618Srie 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
7581618Srie 			    ((arsp->rel_flags &
7591618Srie 			    (FLG_REL_GOT | FLG_REL_BSS |
7601618Srie 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
7611618Srie 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
7621618Srie 				    M_MACH, arsp));
7631618Srie 				continue;
7641618Srie 			}
7651618Srie 
7661618Srie 			/*
7678324SAli.Bahrami@Sun.COM 			 * We determine what the 'got reference'
7681618Srie 			 * model (if required) is at this point.  This
7691618Srie 			 * needs to be done before tls_fixup() since
7701618Srie 			 * it may 'transition' our instructions.
7711618Srie 			 *
7721618Srie 			 * The got table entries have already been assigned,
7731618Srie 			 * and we bind to those initial entries.
7741618Srie 			 */
7751618Srie 			if (arsp->rel_flags & FLG_REL_DTLS)
7761618Srie 				gref = GOT_REF_TLSGD;
7771618Srie 			else if (arsp->rel_flags & FLG_REL_MTLS)
7781618Srie 				gref = GOT_REF_TLSLD;
7791618Srie 			else if (arsp->rel_flags & FLG_REL_STLS)
7801618Srie 				gref = GOT_REF_TLSIE;
7811618Srie 			else
7821618Srie 				gref = GOT_REF_GENERIC;
7831618Srie 
7841618Srie 			/*
7851618Srie 			 * Perform any required TLS fixups.
7861618Srie 			 */
7871618Srie 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
7881618Srie 				Fixupret	ret;
7891618Srie 
7901618Srie 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
7911618Srie 					return (S_ERROR);
7921618Srie 				if (ret == FIX_DONE)
7931618Srie 					continue;
7941618Srie 			}
7951618Srie 
7961618Srie 			/*
7971618Srie 			 * If this is a relocation against a move table, or
7981618Srie 			 * expanded move table, adjust the relocation entries.
7991618Srie 			 */
8001618Srie 			if (arsp->rel_move)
8011618Srie 				ld_adj_movereloc(ofl, arsp);
8021618Srie 
8031618Srie 			sdp = arsp->rel_sym;
8041618Srie 			refaddr = arsp->rel_roffset +
8051618Srie 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
8061618Srie 
8071618Srie 			if (arsp->rel_flags & FLG_REL_CLVAL)
8081618Srie 				value = 0;
8091618Srie 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
8101618Srie 			    STT_SECTION) {
8111618Srie 				/*
8121618Srie 				 * The value for a symbol pointing to a SECTION
8131618Srie 				 * is based off of that sections position.
8141618Srie 				 */
8158369SAli.Bahrami@Sun.COM 				if (sdp->sd_isc->is_flags & FLG_IS_RELUPD) {
8168369SAli.Bahrami@Sun.COM 					Sym_desc	*sym;
8178369SAli.Bahrami@Sun.COM 					Xword		radd;
8188369SAli.Bahrami@Sun.COM 					uchar_t		*raddr = (uchar_t *)
8198369SAli.Bahrami@Sun.COM 					    arsp->rel_isdesc->is_indata->d_buf +
8208369SAli.Bahrami@Sun.COM 					    arsp->rel_roffset;
8218369SAli.Bahrami@Sun.COM 
8221618Srie 					/*
8238369SAli.Bahrami@Sun.COM 					 * This is a REL platform. Hence, the
8248369SAli.Bahrami@Sun.COM 					 * second argument of ld_am_I_partial()
8258369SAli.Bahrami@Sun.COM 					 * is the value stored at the target
8268369SAli.Bahrami@Sun.COM 					 * address where the relocation is
8278369SAli.Bahrami@Sun.COM 					 * going to be applied.
8281618Srie 					 */
8298369SAli.Bahrami@Sun.COM 					if (ld_reloc_targval_get(ofl, arsp,
8308369SAli.Bahrami@Sun.COM 					    raddr, &radd) == 0)
8318369SAli.Bahrami@Sun.COM 						return (S_ERROR);
8328369SAli.Bahrami@Sun.COM 					sym = ld_am_I_partial(arsp, radd);
8338369SAli.Bahrami@Sun.COM 					if (sym) {
8348369SAli.Bahrami@Sun.COM 						Sym	*osym = sym->sd_osym;
8358369SAli.Bahrami@Sun.COM 
8368369SAli.Bahrami@Sun.COM 						/*
8378369SAli.Bahrami@Sun.COM 						 * The symbol was moved, so
8388369SAli.Bahrami@Sun.COM 						 * adjust the value relative
8398369SAli.Bahrami@Sun.COM 						 * to the new section.
8408369SAli.Bahrami@Sun.COM 						 */
8418369SAli.Bahrami@Sun.COM 						value = sym->sd_sym->st_value;
8428369SAli.Bahrami@Sun.COM 						moved = 1;
8438369SAli.Bahrami@Sun.COM 
8448369SAli.Bahrami@Sun.COM 						/*
8458369SAli.Bahrami@Sun.COM 						 * The original raddend covers
8468369SAli.Bahrami@Sun.COM 						 * the displacement from the
8478369SAli.Bahrami@Sun.COM 						 * section start to the desired
8488369SAli.Bahrami@Sun.COM 						 * address. The value computed
8498369SAli.Bahrami@Sun.COM 						 * above gets us from the
8508369SAli.Bahrami@Sun.COM 						 * section start to the start
8518369SAli.Bahrami@Sun.COM 						 * of the symbol range. Adjust
8528369SAli.Bahrami@Sun.COM 						 * the old raddend to remove the
8538369SAli.Bahrami@Sun.COM 						 * offset from section start to
8548369SAli.Bahrami@Sun.COM 						 * symbol start, leaving the
8558369SAli.Bahrami@Sun.COM 						 * displacement within the
8568369SAli.Bahrami@Sun.COM 						 * range of the symbol.
8578369SAli.Bahrami@Sun.COM 						 */
8588369SAli.Bahrami@Sun.COM 						if (osym->st_value != 0) {
8598369SAli.Bahrami@Sun.COM 							radd -= osym->st_value;
8608369SAli.Bahrami@Sun.COM 							if (ld_reloc_targval_set
8618369SAli.Bahrami@Sun.COM 							    (ofl, arsp, raddr,
8628369SAli.Bahrami@Sun.COM 							    radd) == 0)
8638369SAli.Bahrami@Sun.COM 								return (
8648369SAli.Bahrami@Sun.COM 								    S_ERROR);
8658369SAli.Bahrami@Sun.COM 						}
8668369SAli.Bahrami@Sun.COM 					}
8678369SAli.Bahrami@Sun.COM 				}
8688369SAli.Bahrami@Sun.COM 				if (!moved) {
8691618Srie 					value = _elf_getxoff(
8701618Srie 					    sdp->sd_isc->is_indata);
8711618Srie 					if (sdp->sd_isc->is_shdr->sh_flags &
8721618Srie 					    SHF_ALLOC)
8731618Srie 						value += sdp->sd_isc->
8741618Srie 						    is_osdesc->os_shdr->sh_addr;
8751618Srie 				}
8761618Srie 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
8771618Srie 					value -= ofl->ofl_tlsphdr->p_vaddr;
8782850Srie 
8792850Srie 			} else if (IS_SIZE(arsp->rel_rtype)) {
8802850Srie 				/*
8812850Srie 				 * Size relocations require the symbols size.
8822850Srie 				 */
8832850Srie 				value = sdp->sd_sym->st_size;
8841618Srie 			} else {
8851618Srie 				/*
8862647Srie 				 * Else the value is the symbols value.
8871618Srie 				 */
8881618Srie 				value = sdp->sd_sym->st_value;
8891618Srie 			}
8901618Srie 
8911618Srie 			/*
8921618Srie 			 * Relocation against the GLOBAL_OFFSET_TABLE.
8931618Srie 			 */
8941618Srie 			if (arsp->rel_flags & FLG_REL_GOT)
8951618Srie 				arsp->rel_osdesc = ofl->ofl_osgot;
8961618Srie 
8971618Srie 			/*
8981618Srie 			 * If loadable and not producing a relocatable object
8991618Srie 			 * add the sections virtual address to the reference
9001618Srie 			 * address.
9011618Srie 			 */
9021618Srie 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
9031618Srie 			    ((flags & FLG_OF_RELOBJ) == 0))
9041618Srie 				refaddr += arsp->rel_isdesc->is_osdesc->
9051618Srie 				    os_shdr->sh_addr;
9061618Srie 
9071618Srie 			/*
9081618Srie 			 * If this entry has a PLT assigned to it, it's
9091618Srie 			 * value is actually the address of the PLT (and
9101618Srie 			 * not the address of the function).
9111618Srie 			 */
9121618Srie 			if (IS_PLT(arsp->rel_rtype)) {
9131618Srie 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
9141618Srie 					value = ld_calc_plt_addr(sdp, ofl);
9151618Srie 			}
9161618Srie 
9171618Srie 			/*
9181618Srie 			 * Determine whether the value needs further adjustment.
9191618Srie 			 * Filter through the attributes of the relocation to
9201618Srie 			 * determine what adjustment is required.  Note, many
9211618Srie 			 * of the following cases are only applicable when a
9221618Srie 			 * .got is present.  As a .got is not generated when a
9231618Srie 			 * relocatable object is being built, any adjustments
9241618Srie 			 * that require a .got need to be skipped.
9251618Srie 			 */
9261618Srie 			if ((arsp->rel_flags & FLG_REL_GOT) &&
9271618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9281618Srie 				Xword		R1addr;
9291618Srie 				uintptr_t	R2addr;
9301618Srie 				Word		gotndx;
9311618Srie 				Gotndx		*gnp;
9321618Srie 
9331618Srie 				/*
9341618Srie 				 * Perform relocation against GOT table.  Since
9351618Srie 				 * this doesn't fit exactly into a relocation
9361618Srie 				 * we place the appropriate byte in the GOT
9371618Srie 				 * directly
9381618Srie 				 *
9391618Srie 				 * Calculate offset into GOT at which to apply
9401618Srie 				 * the relocation.
9411618Srie 				 */
942*9131SRod.Evans@Sun.COM 				gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref,
943*9131SRod.Evans@Sun.COM 				    ofl, NULL);
9441618Srie 				assert(gnp);
9451618Srie 
9461618Srie 				if (arsp->rel_rtype == R_386_TLS_DTPOFF32)
9471618Srie 					gotndx = gnp->gn_gotndx + 1;
9481618Srie 				else
9491618Srie 					gotndx = gnp->gn_gotndx;
9501618Srie 
9511618Srie 				R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
9521618Srie 
9531618Srie 				/*
9541618Srie 				 * Add the GOTs data's offset.
9551618Srie 				 */
9561618Srie 				R2addr = R1addr + (uintptr_t)
9571618Srie 				    arsp->rel_osdesc->os_outdata->d_buf;
9581618Srie 
9591618Srie 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
9601618Srie 				    ELF_DBG_LD, M_MACH, SHT_REL,
9611618Srie 				    arsp->rel_rtype, R1addr, value,
9621618Srie 				    arsp->rel_sname, arsp->rel_osdesc));
9631618Srie 
9641618Srie 				/*
9651618Srie 				 * And do it.
9661618Srie 				 */
9675189Sab196087 				if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
9685189Sab196087 					*(Xword *)R2addr =
9696206Sab196087 					    ld_bswap_Xword(value);
9705189Sab196087 				else
9715189Sab196087 					*(Xword *)R2addr = value;
9721618Srie 				continue;
9731618Srie 
9741618Srie 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
9751618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9761618Srie 				value -= ofl->ofl_osgot->os_shdr->sh_addr;
9771618Srie 
9781618Srie 			} else if (IS_GOT_PC(arsp->rel_rtype) &&
9791618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9801618Srie 				value =
9811618Srie 				    (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) -
9821618Srie 				    refaddr;
9831618Srie 
9841618Srie 			} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
9851618Srie 			    (((flags & FLG_OF_RELOBJ) == 0) ||
9861618Srie 			    (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) {
9871618Srie 				value -= refaddr;
9881618Srie 
9891618Srie 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
9901618Srie 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
9911618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
9921618Srie 				Gotndx	*gnp;
9931618Srie 
994*9131SRod.Evans@Sun.COM 				gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref,
995*9131SRod.Evans@Sun.COM 				    ofl, NULL);
9961618Srie 				assert(gnp);
9971618Srie 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
9981618Srie 				if (arsp->rel_rtype == R_386_TLS_IE) {
9991618Srie 					value +=
10001618Srie 					    ofl->ofl_osgot->os_shdr->sh_addr;
10011618Srie 				}
10021618Srie 
10031618Srie 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
10041618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
10051618Srie 				Gotndx *gnp;
10061618Srie 
1007*9131SRod.Evans@Sun.COM 				gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1008*9131SRod.Evans@Sun.COM 				    GOT_REF_GENERIC, ofl, NULL);
10091618Srie 				assert(gnp);
10101618Srie 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
10111618Srie 
10121618Srie 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
10131618Srie 			    ((flags & FLG_OF_RELOBJ) == 0)) {
10141618Srie 				Xword	tlsstatsize;
10151618Srie 
10161618Srie 				/*
10171618Srie 				 * This is the LE TLS reference model.  Static
10181618Srie 				 * offset is hard-coded.
10191618Srie 				 */
10201618Srie 				tlsstatsize =
10211618Srie 				    S_ROUND(ofl->ofl_tlsphdr->p_memsz,
10221618Srie 				    M_TLSSTATALIGN);
10231618Srie 				value = tlsstatsize - value;
10241618Srie 
10251618Srie 				/*
10261618Srie 				 * Since this code is fixed up, it assumes a
10271618Srie 				 * negative offset that can be added to the
10281618Srie 				 * thread pointer.
10291618Srie 				 */
10301618Srie 				if ((arsp->rel_rtype == R_386_TLS_LDO_32) ||
10311618Srie 				    (arsp->rel_rtype == R_386_TLS_LE))
10321618Srie 					value = -value;
10331618Srie 			}
10341618Srie 
10351618Srie 			if (arsp->rel_isdesc->is_file)
10361618Srie 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
10371618Srie 			else
10381618Srie 				ifl_name = MSG_INTL(MSG_STR_NULL);
10391618Srie 
10401618Srie 			/*
10411618Srie 			 * Make sure we have data to relocate.  Compiler and
10421618Srie 			 * assembler developers have been known to generate
10431618Srie 			 * relocations against invalid sections (normally .bss),
10441618Srie 			 * so for their benefit give them sufficient information
10451618Srie 			 * to help analyze the problem.  End users should never
10461618Srie 			 * see this.
10471618Srie 			 */
10481618Srie 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
10494734Sab196087 				Conv_inv_buf_t	inv_buf;
10504734Sab196087 
10511618Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
10521618Srie 				    MSG_INTL(MSG_REL_EMPTYSEC),
10534734Sab196087 				    conv_reloc_386_type(arsp->rel_rtype,
10544734Sab196087 				    0, &inv_buf),
10551618Srie 				    ifl_name, demangle(arsp->rel_sname),
10561618Srie 				    arsp->rel_isdesc->is_name);
10571618Srie 				return (S_ERROR);
10581618Srie 			}
10591618Srie 
10601618Srie 			/*
10611618Srie 			 * Get the address of the data item we need to modify.
10621618Srie 			 */
10631618Srie 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
10641618Srie 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
10651618Srie 			    is_indata));
10661618Srie 
10671618Srie 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
10681618Srie 			    M_MACH, SHT_REL, arsp->rel_rtype, EC_NATPTR(addr),
10691618Srie 			    value, arsp->rel_sname, arsp->rel_osdesc));
10701618Srie 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
10711618Srie 
10721618Srie 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
10731618Srie 			    ofl->ofl_size) || (arsp->rel_roffset >
10741618Srie 			    arsp->rel_osdesc->os_shdr->sh_size)) {
10754734Sab196087 				Conv_inv_buf_t	inv_buf;
10764734Sab196087 				int		class;
10771618Srie 
10781618Srie 				if (((uintptr_t)addr -
10791618Srie 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
10801618Srie 					class = ERR_FATAL;
10811618Srie 				else
10821618Srie 					class = ERR_WARNING;
10831618Srie 
10841618Srie 				eprintf(ofl->ofl_lml, class,
10851618Srie 				    MSG_INTL(MSG_REL_INVALOFFSET),
10864734Sab196087 				    conv_reloc_386_type(arsp->rel_rtype,
10874734Sab196087 				    0, &inv_buf),
10881618Srie 				    ifl_name, arsp->rel_isdesc->is_name,
10891618Srie 				    demangle(arsp->rel_sname),
10901618Srie 				    EC_ADDR((uintptr_t)addr -
10911618Srie 				    (uintptr_t)ofl->ofl_nehdr));
10921618Srie 
10931618Srie 				if (class == ERR_FATAL) {
10941618Srie 					return_code = S_ERROR;
10951618Srie 					continue;
10961618Srie 				}
10971618Srie 			}
10981618Srie 
10991618Srie 			/*
11001618Srie 			 * The relocation is additive.  Ignore the previous
11011618Srie 			 * symbol value if this local partial symbol is
11021618Srie 			 * expanded.
11031618Srie 			 */
11041618Srie 			if (moved)
11051618Srie 				value -= *addr;
11061618Srie 
11071618Srie 			/*
11085892Sab196087 			 * If we have a replacement value for the relocation
11095892Sab196087 			 * target, put it in place now.
11105892Sab196087 			 */
11115892Sab196087 			if (arsp->rel_flags & FLG_REL_NADDEND) {
11125892Sab196087 				Xword addend = arsp->rel_raddend;
11135892Sab196087 
11145892Sab196087 				if (ld_reloc_targval_set(ofl, arsp,
11155892Sab196087 				    addr, addend) == 0)
11165892Sab196087 					return (S_ERROR);
11175892Sab196087 			}
11185892Sab196087 
11195892Sab196087 			/*
11205189Sab196087 			 * If '-z noreloc' is specified - skip the do_reloc_ld
11211618Srie 			 * stage.
11221618Srie 			 */
11235189Sab196087 			if (OFL_DO_RELOC(ofl)) {
11245189Sab196087 				if (do_reloc_ld((uchar_t)arsp->rel_rtype, addr,
11251618Srie 				    &value, arsp->rel_sname, ifl_name,
11265189Sab196087 				    OFL_SWAP_RELOC_DATA(ofl, arsp),
11271618Srie 				    ofl->ofl_lml) == 0)
11281618Srie 					return_code = S_ERROR;
11291618Srie 			}
11301618Srie 		}
11311618Srie 	}
11321618Srie 	return (return_code);
11331618Srie }
11341618Srie 
11351618Srie /*
11361618Srie  * Add an output relocation record.
11371618Srie  */
11386206Sab196087 static uintptr_t
11391618Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
11401618Srie {
11411618Srie 	Rel_desc	*orsp;
11421618Srie 	Rel_cache	*rcp;
11431618Srie 	Sym_desc	*sdp = rsp->rel_sym;
1144*9131SRod.Evans@Sun.COM 	static size_t	nextsize = 0;
11451618Srie 
11461618Srie 	/*
11471618Srie 	 * Static executables *do not* want any relocations against them.
11481618Srie 	 * Since our engine still creates relocations against a WEAK UNDEFINED
11491618Srie 	 * symbol in a static executable, it's best to disable them here
11501618Srie 	 * instead of through out the relocation code.
11511618Srie 	 */
11521618Srie 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
11531618Srie 	    (FLG_OF_STATIC | FLG_OF_EXEC))
11541618Srie 		return (1);
11551618Srie 
11561618Srie 	/*
1157*9131SRod.Evans@Sun.COM 	 * Obtain the new available relocation cache entry.
11581618Srie 	 */
1159*9131SRod.Evans@Sun.COM 	if ((rcp = ld_add_rel_cache(ofl, &ofl->ofl_outrels, &nextsize,
1160*9131SRod.Evans@Sun.COM 	    REL_LOIDESCNO, REL_HOIDESCNO)) == (Rel_cache *)S_ERROR)
1161*9131SRod.Evans@Sun.COM 		return (S_ERROR);
11621618Srie 
1163*9131SRod.Evans@Sun.COM 	orsp = rcp->rc_free;
11641618Srie 
11651618Srie 	/*
11661618Srie 	 * If we are adding a output relocation against a section
11671618Srie 	 * symbol (non-RELATIVE) then mark that section.  These sections
11681618Srie 	 * will be added to the .dynsym symbol table.
11691618Srie 	 */
11701618Srie 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
11711618Srie 	    ((flags & FLG_REL_SCNNDX) ||
11721618Srie 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
11731618Srie 
11741618Srie 		/*
11751618Srie 		 * If this is a COMMON symbol - no output section
11761618Srie 		 * exists yet - (it's created as part of sym_validate()).
11771618Srie 		 * So - we mark here that when it's created it should
11781618Srie 		 * be tagged with the FLG_OS_OUTREL flag.
11791618Srie 		 */
11801618Srie 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
11811682Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
11821618Srie 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
11831618Srie 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
11841618Srie 			else
11851618Srie 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
11861618Srie 		} else {
11871618Srie 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
11881618Srie 
11892347Srie 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11901618Srie 				ofl->ofl_dynshdrcnt++;
11911618Srie 				osp->os_flags |= FLG_OS_OUTREL;
11921618Srie 			}
11931618Srie 		}
11941618Srie 	}
11951618Srie 
11961618Srie 	*orsp = *rsp;
11971618Srie 	orsp->rel_flags |= flags;
11981618Srie 
11991618Srie 	rcp->rc_free++;
12001618Srie 	ofl->ofl_outrelscnt++;
12011618Srie 
12021618Srie 	if (flags & FLG_REL_GOT)
12031618Srie 		ofl->ofl_relocgotsz += (Xword)sizeof (Rel);
12041618Srie 	else if (flags & FLG_REL_PLT)
12051618Srie 		ofl->ofl_relocpltsz += (Xword)sizeof (Rel);
12061618Srie 	else if (flags & FLG_REL_BSS)
12071618Srie 		ofl->ofl_relocbsssz += (Xword)sizeof (Rel);
12081618Srie 	else if (flags & FLG_REL_NOINFO)
12091618Srie 		ofl->ofl_relocrelsz += (Xword)sizeof (Rel);
12101618Srie 	else
12111618Srie 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rel);
12121618Srie 
12131618Srie 	if (orsp->rel_rtype == M_R_RELATIVE)
12141618Srie 		ofl->ofl_relocrelcnt++;
12151618Srie 
12161618Srie 	/*
12171618Srie 	 * We don't perform sorting on PLT relocations because
12181618Srie 	 * they have already been assigned a PLT index and if we
12191618Srie 	 * were to sort them we would have to re-assign the plt indexes.
12201618Srie 	 */
12211618Srie 	if (!(flags & FLG_REL_PLT))
12221618Srie 		ofl->ofl_reloccnt++;
12231618Srie 
12241618Srie 	/*
12251618Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
12261618Srie 	 */
12271618Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
12281618Srie 		ofl->ofl_flags |= FLG_OF_BLDGOT;
12291618Srie 
12301618Srie 	/*
12311618Srie 	 * Identify and possibly warn of a displacement relocation.
12321618Srie 	 */
12331618Srie 	if (orsp->rel_flags & FLG_REL_DISP) {
12341618Srie 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
12351618Srie 
12361618Srie 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
12371618Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
12381618Srie 	}
12391618Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL,
12401618Srie 	    M_MACH, orsp));
12411618Srie 	return (1);
12421618Srie }
12431618Srie 
12441618Srie /*
12451618Srie  * process relocation for a LOCAL symbol
12461618Srie  */
12476206Sab196087 static uintptr_t
12481618Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
12491618Srie {
12506299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
12511618Srie 	Sym_desc	*sdp = rsp->rel_sym;
12521682Srie 	Word		shndx = sdp->sd_sym->st_shndx;
12531618Srie 
12541618Srie 	/*
12551618Srie 	 * if ((shared object) and (not pc relative relocation) and
12561618Srie 	 *    (not against ABS symbol))
12571618Srie 	 * then
12581618Srie 	 *	build R_386_RELATIVE
12591618Srie 	 * fi
12601618Srie 	 */
12611618Srie 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
12622850Srie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
12631618Srie 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
12641618Srie 	    !(rsp->rel_isdesc != NULL &&
12651618Srie 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
12661618Srie 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
12671618Srie 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
12681618Srie 		Word	ortype = rsp->rel_rtype;
12691618Srie 
12701618Srie 		rsp->rel_rtype = R_386_RELATIVE;
12711618Srie 		if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR)
12721618Srie 			return (S_ERROR);
12731618Srie 		rsp->rel_rtype = ortype;
12741618Srie 	}
12751618Srie 
12761618Srie 	/*
12771618Srie 	 * If the relocation is against a 'non-allocatable' section
12781618Srie 	 * and we can not resolve it now - then give a warning
12791618Srie 	 * message.
12801618Srie 	 *
12811618Srie 	 * We can not resolve the symbol if either:
12821618Srie 	 *	a) it's undefined
12831618Srie 	 *	b) it's defined in a shared library and a
12841618Srie 	 *	   COPY relocation hasn't moved it to the executable
12851618Srie 	 *
12861618Srie 	 * Note: because we process all of the relocations against the
12871618Srie 	 *	text segment before any others - we know whether
12881618Srie 	 *	or not a copy relocation will be generated before
12891618Srie 	 *	we get here (see reloc_init()->reloc_segments()).
12901618Srie 	 */
12911618Srie 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
12921618Srie 	    ((shndx == SHN_UNDEF) ||
12931618Srie 	    ((sdp->sd_ref == REF_DYN_NEED) &&
12941618Srie 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
12954734Sab196087 		Conv_inv_buf_t inv_buf;
12964734Sab196087 
12971618Srie 		/*
12981618Srie 		 * If the relocation is against a SHT_SUNW_ANNOTATE
12991618Srie 		 * section - then silently ignore that the relocation
13001618Srie 		 * can not be resolved.
13011618Srie 		 */
13021618Srie 		if (rsp->rel_osdesc &&
13031618Srie 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
13041618Srie 			return (0);
13051618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
13064734Sab196087 		    conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf),
13071618Srie 		    rsp->rel_isdesc->is_file->ifl_name,
13081618Srie 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
13091618Srie 		return (1);
13101618Srie 	}
13111618Srie 
13121618Srie 	/*
13131618Srie 	 * Perform relocation.
13141618Srie 	 */
13151618Srie 	return (ld_add_actrel(NULL, rsp, ofl));
13161618Srie }
13171618Srie 
13186206Sab196087 static uintptr_t
13191618Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
13201618Srie {
13211618Srie 	Word		rtype = rsp->rel_rtype;
13221618Srie 	Sym_desc	*sdp = rsp->rel_sym;
13236299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
13241618Srie 	Gotndx		*gnp;
13251618Srie 
13261618Srie 	/*
13272145Srie 	 * If we're building an executable - use either the IE or LE access
13282145Srie 	 * model.  If we're building a shared object process any IE model.
13291618Srie 	 */
13302145Srie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
13311618Srie 		/*
13322145Srie 		 * Set the DF_STATIC_TLS flag.
13331618Srie 		 */
13341618Srie 		ofl->ofl_dtflags |= DF_STATIC_TLS;
13351618Srie 
13362145Srie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
13371618Srie 			/*
13382145Srie 			 * Assign a GOT entry for static TLS references.
13391618Srie 			 */
1340*9131SRod.Evans@Sun.COM 			if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1341*9131SRod.Evans@Sun.COM 			    GOT_REF_TLSIE, ofl, NULL)) == NULL) {
13422145Srie 
13432145Srie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
13442145Srie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1345*9131SRod.Evans@Sun.COM 				    rtype, R_386_TLS_TPOFF, NULL) == S_ERROR)
13462145Srie 					return (S_ERROR);
13471618Srie 			}
13481618Srie 
13492145Srie 			/*
13502145Srie 			 * IE access model.
13512145Srie 			 */
13522145Srie 			if (IS_TLS_IE(rtype)) {
13532145Srie 				if (ld_add_actrel(FLG_REL_STLS,
13542145Srie 				    rsp, ofl) == S_ERROR)
13552145Srie 					return (S_ERROR);
13562145Srie 
13572145Srie 				/*
13582145Srie 				 * A non-pic shared object needs to adjust the
13592145Srie 				 * active relocation (indntpoff).
13602145Srie 				 */
13612145Srie 				if (((flags & FLG_OF_EXEC) == 0) &&
13622145Srie 				    (rtype == R_386_TLS_IE)) {
13632145Srie 					rsp->rel_rtype = R_386_RELATIVE;
13642145Srie 					return (ld_add_outrel(NULL, rsp, ofl));
13652145Srie 				}
13662145Srie 				return (1);
13672145Srie 			}
13681618Srie 
13691618Srie 			/*
13702145Srie 			 * Fixups are required for other executable models.
13711618Srie 			 */
13721618Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13731618Srie 			    rsp, ofl));
13741618Srie 		}
13752145Srie 
13761618Srie 		/*
13772145Srie 		 * LE access model.
13781618Srie 		 */
13791618Srie 		if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32))
13801618Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
13811618Srie 
13821618Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13831618Srie 		    rsp, ofl));
13841618Srie 	}
13851618Srie 
13861618Srie 	/*
13872145Srie 	 * Building a shared object.
13882145Srie 	 *
13892145Srie 	 * Assign a GOT entry for a dynamic TLS reference.
13901618Srie 	 */
1391*9131SRod.Evans@Sun.COM 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1392*9131SRod.Evans@Sun.COM 	    GOT_REF_TLSLD, ofl, NULL)) == NULL)) {
13932145Srie 
13942145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1395*9131SRod.Evans@Sun.COM 		    FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, NULL) == S_ERROR)
13962145Srie 			return (S_ERROR);
13972145Srie 
1398*9131SRod.Evans@Sun.COM 	} else if (IS_TLS_GD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1399*9131SRod.Evans@Sun.COM 	    GOT_REF_TLSGD, ofl, NULL)) == NULL)) {
14002145Srie 
14012145Srie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
14022145Srie 		    FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32,
14032145Srie 		    R_386_TLS_DTPOFF32) == S_ERROR)
14042145Srie 			return (S_ERROR);
14051618Srie 	}
14061618Srie 
14071618Srie 	/*
14081618Srie 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
14092145Srie 	 * cause a call to __tls_get_addr().  Convert this relocation to that
14102145Srie 	 * symbol now, and prepare for the PLT magic.
14111618Srie 	 */
14121618Srie 	if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) {
14133862Srie 		Sym_desc	*tlsgetsym;
14141618Srie 
14151618Srie 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU),
14163862Srie 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
14171618Srie 			return (S_ERROR);
14182145Srie 
14191618Srie 		rsp->rel_sym = tlsgetsym;
14201618Srie 		rsp->rel_sname = tlsgetsym->sd_name;
14211618Srie 		rsp->rel_rtype = R_386_PLT32;
14222145Srie 
14231618Srie 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
14241618Srie 			return (S_ERROR);
14252145Srie 
14261618Srie 		rsp->rel_sym = sdp;
14271618Srie 		rsp->rel_sname = sdp->sd_name;
14281618Srie 		rsp->rel_rtype = rtype;
14291618Srie 		return (1);
14301618Srie 	}
14311618Srie 
14321618Srie 	if (IS_TLS_LD(rtype))
14331618Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
14341618Srie 
14351618Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
14361618Srie }
14371618Srie 
14381618Srie /* ARGSUSED4 */
14396206Sab196087 static uintptr_t
1440*9131SRod.Evans@Sun.COM ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
1441*9131SRod.Evans@Sun.COM     Rel_desc *rsp, Sym_desc *sdp)
14421618Srie {
1443*9131SRod.Evans@Sun.COM 	Gotndx	gn, *gnp;
14441618Srie 	uint_t	gotents;
14451618Srie 
14461618Srie 	if (pgnp)
14471618Srie 		return (1);
14481618Srie 
14491618Srie 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
14501618Srie 		gotents = 2;
14511618Srie 	else
14521618Srie 		gotents = 1;
14531618Srie 
1454*9131SRod.Evans@Sun.COM 	gn.gn_addend = 0;
1455*9131SRod.Evans@Sun.COM 	gn.gn_gotndx = ofl->ofl_gotcnt;
1456*9131SRod.Evans@Sun.COM 	gn.gn_gotref = gref;
14571618Srie 
14581618Srie 	ofl->ofl_gotcnt += gotents;
14591618Srie 
14601618Srie 	if (gref == GOT_REF_TLSLD) {
1461*9131SRod.Evans@Sun.COM 		if (ofl->ofl_tlsldgotndx == NULL) {
1462*9131SRod.Evans@Sun.COM 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
1463*9131SRod.Evans@Sun.COM 				return (S_ERROR);
1464*9131SRod.Evans@Sun.COM 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
1465*9131SRod.Evans@Sun.COM 			ofl->ofl_tlsldgotndx = gnp;
1466*9131SRod.Evans@Sun.COM 		}
14671618Srie 		return (1);
14681618Srie 	}
14691618Srie 
1470*9131SRod.Evans@Sun.COM 	/*
1471*9131SRod.Evans@Sun.COM 	 * GOT indexes are maintained on an Alist, where there is typically
1472*9131SRod.Evans@Sun.COM 	 * only one index.  The usage of this list is to scan the list to find
1473*9131SRod.Evans@Sun.COM 	 * an index, and then apply that index immediately to a relocation.
1474*9131SRod.Evans@Sun.COM 	 * Thus there are no external references to these GOT index structures
1475*9131SRod.Evans@Sun.COM 	 * that can be compromised by the Alist being reallocated.
1476*9131SRod.Evans@Sun.COM 	 */
1477*9131SRod.Evans@Sun.COM 	if (alist_append(alpp, &gn, sizeof (Gotndx), AL_CNT_SDP_GOT) == NULL)
14781618Srie 		return (S_ERROR);
14791618Srie 
14801618Srie 	return (1);
14811618Srie }
14821618Srie 
14836206Sab196087 static void
14841618Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14851618Srie {
14861618Srie 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14871618Srie 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
14881618Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14891618Srie }
14901618Srie 
14911618Srie /*
14921618Srie  * Initializes .got[0] with the _DYNAMIC symbol value.
14931618Srie  */
14946206Sab196087 static uintptr_t
14952145Srie ld_fillin_gotplt(Ofl_desc *ofl)
14961618Srie {
14976299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
14986299Sab196087 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
14992145Srie 
15001618Srie 	if (ofl->ofl_osgot) {
15012145Srie 		Sym_desc	*sdp;
15021618Srie 
15031618Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
15041618Srie 		    SYM_NOHASH, 0, ofl)) != NULL) {
15052145Srie 			uchar_t	*genptr;
15062145Srie 
15072145Srie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
15081618Srie 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
15091618Srie 			/* LINTED */
15101618Srie 			*(Word *)genptr = (Word)sdp->sd_sym->st_value;
15116206Sab196087 			if (bswap)
15126206Sab196087 				/* LINTED */
15136206Sab196087 				*(Word *)genptr =
15146206Sab196087 				    /* LINTED */
15156206Sab196087 				    ld_bswap_Word(*(Word *)genptr);
15161618Srie 		}
15171618Srie 	}
15181618Srie 
15191618Srie 	/*
15201618Srie 	 * Fill in the reserved slot in the procedure linkage table the first
15211618Srie 	 * entry is:
15221618Srie 	 *  if (building a.out) {
15231618Srie 	 *	PUSHL	got[1]		    # the address of the link map entry
15241618Srie 	 *	JMP *	got[2]		    # the address of rtbinder
15251618Srie 	 *  } else {
15261618Srie 	 *	PUSHL	got[1]@GOT(%ebx)    # the address of the link map entry
15271618Srie 	 *	JMP *	got[2]@GOT(%ebx)    # the address of rtbinder
15281618Srie 	 *  }
15291618Srie 	 */
15302145Srie 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
15311618Srie 		uchar_t *pltent;
15321618Srie 
15331618Srie 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
15342145Srie 		if (!(flags & FLG_OF_SHAROBJ)) {
15351618Srie 			pltent[0] = M_SPECIAL_INST;
15361618Srie 			pltent[1] = M_PUSHL_DISP;
15371618Srie 			pltent += 2;
15381618Srie 			/* LINTED */
15391618Srie 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
15404734Sab196087 			    sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE);
15416206Sab196087 			if (bswap)
15426206Sab196087 				/* LINTED */
15436206Sab196087 				*(Word *)pltent =
15446206Sab196087 				    /* LINTED */
15456206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15461618Srie 			pltent += 4;
15471618Srie 			pltent[0] = M_SPECIAL_INST;
15481618Srie 			pltent[1] = M_JMP_DISP_IND;
15491618Srie 			pltent += 2;
15501618Srie 			/* LINTED */
15511618Srie 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
15524734Sab196087 			    sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE);
15536206Sab196087 			if (bswap)
15546206Sab196087 				/* LINTED */
15556206Sab196087 				*(Word *)pltent =
15566206Sab196087 				    /* LINTED */
15576206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15581618Srie 		} else {
15591618Srie 			pltent[0] = M_SPECIAL_INST;
15601618Srie 			pltent[1] = M_PUSHL_REG_DISP;
15611618Srie 			pltent += 2;
15621618Srie 			/* LINTED */
15631618Srie 			*(Word *)pltent = (Word)(M_GOT_XLINKMAP *
15644734Sab196087 			    M_GOT_ENTSIZE);
15656206Sab196087 			if (bswap)
15666206Sab196087 				/* LINTED */
15676206Sab196087 				*(Word *)pltent =
15686206Sab196087 				    /* LINTED */
15696206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15701618Srie 			pltent += 4;
15711618Srie 			pltent[0] = M_SPECIAL_INST;
15721618Srie 			pltent[1] = M_JMP_REG_DISP_IND;
15731618Srie 			pltent += 2;
15741618Srie 			/* LINTED */
15751618Srie 			*(Word *)pltent = (Word)(M_GOT_XRTLD *
15764734Sab196087 			    M_GOT_ENTSIZE);
15776206Sab196087 			if (bswap)
15786206Sab196087 				/* LINTED */
15796206Sab196087 				*(Word *)pltent =
15806206Sab196087 				    /* LINTED */
15816206Sab196087 				    ld_bswap_Word(*(Word *)pltent);
15821618Srie 		}
15831618Srie 	}
15841618Srie 	return (1);
15851618Srie }
15866206Sab196087 
15876206Sab196087 
15886206Sab196087 
15896206Sab196087 /*
15906206Sab196087  * Template for generating "void (*)(void)" function
15916206Sab196087  */
15926206Sab196087 static const uchar_t nullfunc_tmpl[] = {	/* IA32 */
15936206Sab196087 /* 0x00 */	0xc3				/* ret */
15946206Sab196087 };
15956206Sab196087 
15966206Sab196087 
15976206Sab196087 
15986206Sab196087 /*
15996206Sab196087  * Return the ld_targ definition for this target.
16006206Sab196087  */
16016206Sab196087 const Target *
16026206Sab196087 ld_targ_init_x86(void)
16036206Sab196087 {
16046206Sab196087 	static const Target _ld_targ = {
16056206Sab196087 		{			/* Target_mach */
16066206Sab196087 			M_MACH,			/* m_mach */
16076206Sab196087 			M_MACHPLUS,		/* m_machplus */
16086206Sab196087 			M_FLAGSPLUS,		/* m_flagsplus */
16096206Sab196087 			M_CLASS,		/* m_class */
16106206Sab196087 			M_DATA,			/* m_data */
16116206Sab196087 
16126206Sab196087 			M_SEGM_ALIGN,		/* m_segm_align */
16136206Sab196087 			M_SEGM_ORIGIN,		/* m_segm_origin */
16148501SRod.Evans@Sun.COM 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
16156206Sab196087 			M_DATASEG_PERM,		/* m_dataseg_perm */
16166206Sab196087 			M_WORD_ALIGN,		/* m_word_align */
16176206Sab196087 			MSG_ORIG(MSG_PTH_RTLD),	/* m_def_interp */
16186206Sab196087 
16196206Sab196087 			/* Relocation type codes */
16206206Sab196087 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
16216206Sab196087 			M_R_COPY,		/* m_r_copy */
16226206Sab196087 			M_R_GLOB_DAT,		/* m_r_glob_dat */
16236206Sab196087 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
16246206Sab196087 			M_R_NUM,		/* m_r_num */
16256206Sab196087 			M_R_NONE,		/* m_r_none */
16266206Sab196087 			M_R_RELATIVE,		/* m_r_relative */
16276206Sab196087 			M_R_REGISTER,		/* m_r_register */
16286206Sab196087 
16296206Sab196087 			/* Relocation related constants */
16306206Sab196087 			M_REL_DT_COUNT,		/* m_rel_dt_count */
16316206Sab196087 			M_REL_DT_ENT,		/* m_rel_dt_ent */
16326206Sab196087 			M_REL_DT_SIZE,		/* m_rel_dt_size */
16336206Sab196087 			M_REL_DT_TYPE,		/* m_rel_dt_type */
16346206Sab196087 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
16356206Sab196087 
16366206Sab196087 			/* GOT related constants */
16376206Sab196087 			M_GOT_ENTSIZE,		/* m_got_entsize */
16386206Sab196087 			M_GOT_XNumber,		/* m_got_xnumber */
16396206Sab196087 
16406206Sab196087 			/* PLT related constants */
16416206Sab196087 			M_PLT_ALIGN,		/* m_plt_align */
16426206Sab196087 			M_PLT_ENTSIZE,		/* m_plt_entsize */
16436206Sab196087 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
16446206Sab196087 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
16456206Sab196087 
16469085SAli.Bahrami@Sun.COM 			/* Section type of .eh_frame/.eh_frame_hdr sections */
16479085SAli.Bahrami@Sun.COM 			SHT_PROGBITS,		/* m_sht_unwind */
16489085SAli.Bahrami@Sun.COM 
16496206Sab196087 			M_DT_REGISTER,		/* m_dt_register */
16506206Sab196087 		},
16516206Sab196087 		{			/* Target_machid */
16526206Sab196087 			M_ID_ARRAY,		/* id_array */
16536206Sab196087 			M_ID_BSS,		/* id_bss */
16546206Sab196087 			M_ID_CAP,		/* id_cap */
16556206Sab196087 			M_ID_DATA,		/* id_data */
16566206Sab196087 			M_ID_DYNAMIC,		/* id_dynamic */
16576206Sab196087 			M_ID_DYNSORT,		/* id_dynsort */
16586206Sab196087 			M_ID_DYNSTR,		/* id_dynstr */
16596206Sab196087 			M_ID_DYNSYM,		/* id_dynsym */
16606206Sab196087 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
16616206Sab196087 			M_ID_GOT,		/* id_got */
16626206Sab196087 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
16636206Sab196087 			M_ID_HASH,		/* id_hash */
16646206Sab196087 			M_ID_INTERP,		/* id_interp */
16656206Sab196087 			M_ID_LBSS,		/* id_lbss */
16666206Sab196087 			M_ID_LDYNSYM,		/* id_ldynsym */
16676206Sab196087 			M_ID_NOTE,		/* id_note */
16686206Sab196087 			M_ID_NULL,		/* id_null */
16696206Sab196087 			M_ID_PLT,		/* id_plt */
16706206Sab196087 			M_ID_REL,		/* id_rel */
16716206Sab196087 			M_ID_STRTAB,		/* id_strtab */
16726206Sab196087 			M_ID_SYMINFO,		/* id_syminfo */
16736206Sab196087 			M_ID_SYMTAB,		/* id_symtab */
16746206Sab196087 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
16756206Sab196087 			M_ID_TEXT,		/* id_text */
16766206Sab196087 			M_ID_TLS,		/* id_tls */
16776206Sab196087 			M_ID_TLSBSS,		/* id_tlsbss */
16786206Sab196087 			M_ID_UNKNOWN,		/* id_unknown */
16796206Sab196087 			M_ID_UNWIND,		/* id_unwind */
16809085SAli.Bahrami@Sun.COM 			M_ID_UNWINDHDR,		/* id_unwindhdr */
16816206Sab196087 			M_ID_USER,		/* id_user */
16826206Sab196087 			M_ID_VERSION,		/* id_version */
16836206Sab196087 		},
16846206Sab196087 		{			/* Target_nullfunc */
16856206Sab196087 			nullfunc_tmpl,		/* nf_template */
16866206Sab196087 			sizeof (nullfunc_tmpl),	/* nf_size */
16876206Sab196087 		},
16886206Sab196087 		{			/* Target_machrel */
16896206Sab196087 			reloc_table,
16906206Sab196087 
16916206Sab196087 			ld_init_rel,		/* mr_init_rel */
16926206Sab196087 			ld_mach_eflags,		/* mr_mach_eflags */
16936206Sab196087 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
16946206Sab196087 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
16956206Sab196087 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
16966206Sab196087 			ld_perform_outreloc,	/* mr_perform_outreloc */
16976206Sab196087 			ld_do_activerelocs,	/* mr_do_activerelocs */
16986206Sab196087 			ld_add_outrel,		/* mr_add_outrel */
16996206Sab196087 			NULL,			/* mr_reloc_register */
17006206Sab196087 			ld_reloc_local,		/* mr_reloc_local */
17016206Sab196087 			NULL,			/* mr_reloc_GOTOP */
17026206Sab196087 			ld_reloc_TLS,		/* mr_reloc_TLS */
17036206Sab196087 			NULL,			/* mr_assign_got */
1704*9131SRod.Evans@Sun.COM 			ld_find_got_ndx,	/* mr_find_got_ndx */
17056206Sab196087 			ld_calc_got_offset,	/* mr_calc_got_offset */
17066206Sab196087 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
17076206Sab196087 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
17086206Sab196087 			NULL,			/* mr_allocate_got */
17096206Sab196087 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
17106206Sab196087 		},
17116206Sab196087 		{			/* Target_machsym */
17126206Sab196087 			NULL,			/* ms_reg_check */
17136206Sab196087 			NULL,			/* ms_mach_sym_typecheck */
17146206Sab196087 			NULL,			/* ms_is_regsym */
17156206Sab196087 			NULL,			/* ms_reg_find */
17166206Sab196087 			NULL			/* ms_reg_enter */
17176206Sab196087 		}
17186206Sab196087 	};
17196206Sab196087 
17206206Sab196087 	return (&_ld_targ);
17216206Sab196087 }
1722