17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * 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.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21141040e8Srie 
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.
247c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
257c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
267c478bd9Sstevel@tonic-gate  *
27bf994817SAli Bahrami  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
30ba2be530Sab /* Get the x86 version of the relocation engine */
31ba2be530Sab #define	DO_RELOC_LIBLD_X86
32ba2be530Sab 
337c478bd9Sstevel@tonic-gate #include	<string.h>
347c478bd9Sstevel@tonic-gate #include	<stdio.h>
357c478bd9Sstevel@tonic-gate #include	<sys/elf_386.h>
365aefb655Srie #include	<debug.h>
375aefb655Srie #include	<reloc.h>
38ba2be530Sab #include	<i386/machdep_x86.h>
397c478bd9Sstevel@tonic-gate #include	"msg.h"
407c478bd9Sstevel@tonic-gate #include	"_libld.h"
417c478bd9Sstevel@tonic-gate 
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;
5157ef7aa9SRod Evans 
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;
79ba2be530Sab 
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 }
91ba2be530Sab 
92ba2be530Sab static Word
ld_init_rel(Rel_desc * reld,Word * typedata,void * reloc)93bf994817SAli Bahrami ld_init_rel(Rel_desc *reld, Word *typedata, void *reloc)
947c478bd9Sstevel@tonic-gate {
9557ef7aa9SRod Evans 	Rel	*rel = (Rel *)reloc;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/* LINTED */
98ba2be530Sab 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
997c478bd9Sstevel@tonic-gate 	reld->rel_roffset = rel->r_offset;
1007c478bd9Sstevel@tonic-gate 	reld->rel_raddend = 0;
101bf994817SAli Bahrami 	*typedata = 0;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	return ((Word)ELF_R_SYM(rel->r_info));
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
106ba2be530Sab static void
ld_mach_eflags(Ehdr * ehdr,Ofl_desc * ofl)1075aefb655Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
1087c478bd9Sstevel@tonic-gate {
1095aefb655Srie 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
112ba2be530Sab static void
ld_mach_make_dynamic(Ofl_desc * ofl,size_t * cnt)1135aefb655Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
1167c478bd9Sstevel@tonic-gate 		/*
1177c478bd9Sstevel@tonic-gate 		 * Create this entry if we are going to create a PLT table.
1187c478bd9Sstevel@tonic-gate 		 */
1197c478bd9Sstevel@tonic-gate 		if (ofl->ofl_pltcnt)
1207c478bd9Sstevel@tonic-gate 			(*cnt)++;		/* DT_PLTGOT */
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
124ba2be530Sab static void
ld_mach_update_odynamic(Ofl_desc * ofl,Dyn ** dyn)125d326b23bSrie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
1267c478bd9Sstevel@tonic-gate {
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)++;
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
137ba2be530Sab static Xword
ld_calc_plt_addr(Sym_desc * sdp,Ofl_desc * ofl)1385aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	Xword	value;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
1437c478bd9Sstevel@tonic-gate 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
1447c478bd9Sstevel@tonic-gate 	return (value);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  *  Build a single plt entry - code is:
149*fec04708SRichard Lowe  *	if (building dynamic executable)
1507c478bd9Sstevel@tonic-gate  *		JMP	*got_off
1517c478bd9Sstevel@tonic-gate  *	else
1527c478bd9Sstevel@tonic-gate  *		JMP	*got_off@GOT(%ebx)
1537c478bd9Sstevel@tonic-gate  *	PUSHL	&rel_off
1547c478bd9Sstevel@tonic-gate  *	JMP	-n(%pc)		# -n is pcrel offset to first plt entry
1557c478bd9Sstevel@tonic-gate  *
1567c478bd9Sstevel@tonic-gate  *	The got_off@GOT entry gets filled with the address of the PUSHL,
1577c478bd9Sstevel@tonic-gate  *	so the first pass through the plt jumps back here, jumping
1587c478bd9Sstevel@tonic-gate  *	in turn to the first plt entry, which jumps to the dynamic
1597c478bd9Sstevel@tonic-gate  *	linker.	 The dynamic linker then patches the GOT, rerouting
1607c478bd9Sstevel@tonic-gate  *	future plt calls to the proper destination.
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate static void
plt_entry(Ofl_desc * ofl,Word rel_off,Sym_desc * sdp)1637c478bd9Sstevel@tonic-gate plt_entry(Ofl_desc * ofl, Word rel_off, Sym_desc * sdp)
1647c478bd9Sstevel@tonic-gate {
165b3fbe5e6Sseizo 	uchar_t		*pltent, *gotent;
1667c478bd9Sstevel@tonic-gate 	Sword		plt_off;
1677c478bd9Sstevel@tonic-gate 	Word		got_off;
168ba2be530Sab 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
1717c478bd9Sstevel@tonic-gate 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
1727c478bd9Sstevel@tonic-gate 	    M_PLT_ENTSIZE);
173b3fbe5e6Sseizo 	pltent = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf) + plt_off;
174b3fbe5e6Sseizo 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * Fill in the got entry with the address of the next instruction.
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	/* LINTED */
1807c478bd9Sstevel@tonic-gate 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
181b3fbe5e6Sseizo 	    M_PLT_INSSIZE;
182ba2be530Sab 	if (bswap)
183ba2be530Sab 		/* LINTED */
184ba2be530Sab 		*(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (!(ofl->ofl_flags & FLG_OF_SHAROBJ)) {
1877c478bd9Sstevel@tonic-gate 		pltent[0] = M_SPECIAL_INST;
1887c478bd9Sstevel@tonic-gate 		pltent[1] = M_JMP_DISP_IND;
1897c478bd9Sstevel@tonic-gate 		pltent += 2;
1907c478bd9Sstevel@tonic-gate 		/* LINTED */
1917c478bd9Sstevel@tonic-gate 		*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->sh_addr +
192de777a60Sab 		    got_off);
1937c478bd9Sstevel@tonic-gate 	} else {
1947c478bd9Sstevel@tonic-gate 		pltent[0] = M_SPECIAL_INST;
1957c478bd9Sstevel@tonic-gate 		pltent[1] = M_JMP_REG_DISP_IND;
1967c478bd9Sstevel@tonic-gate 		pltent += 2;
1977c478bd9Sstevel@tonic-gate 		/* LINTED */
1987c478bd9Sstevel@tonic-gate 		*(Word *)pltent = (Word)got_off;
1997c478bd9Sstevel@tonic-gate 	}
200ba2be530Sab 	if (bswap)
201ba2be530Sab 		/* LINTED */
202ba2be530Sab 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2037c478bd9Sstevel@tonic-gate 	pltent += 4;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	pltent[0] = M_INST_PUSHL;
2067c478bd9Sstevel@tonic-gate 	pltent++;
2077c478bd9Sstevel@tonic-gate 	/* LINTED */
2087c478bd9Sstevel@tonic-gate 	*(Word *)pltent = (Word)rel_off;
209ba2be530Sab 	if (bswap)
210ba2be530Sab 		/* LINTED */
211ba2be530Sab 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2127c478bd9Sstevel@tonic-gate 	pltent += 4;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	plt_off = -(plt_off + 16);	/* JMP, PUSHL, JMP take 16 bytes */
2157c478bd9Sstevel@tonic-gate 	pltent[0] = M_INST_JMP;
2167c478bd9Sstevel@tonic-gate 	pltent++;
2177c478bd9Sstevel@tonic-gate 	/* LINTED */
2187c478bd9Sstevel@tonic-gate 	*(Word *)pltent = (Word)plt_off;
219ba2be530Sab 	if (bswap)
220ba2be530Sab 		/* LINTED */
221ba2be530Sab 		*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
224ba2be530Sab static uintptr_t
ld_perform_outreloc(Rel_desc * orsp,Ofl_desc * ofl,Boolean * remain_seen)2251007fd6fSAli Bahrami ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl, Boolean *remain_seen)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	Os_desc *	relosp, * osp = 0;
2287c478bd9Sstevel@tonic-gate 	Word		ndx, roffset, value;
2297c478bd9Sstevel@tonic-gate 	Rel		rea;
2307c478bd9Sstevel@tonic-gate 	char		*relbits;
2317c478bd9Sstevel@tonic-gate 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
2327c478bd9Sstevel@tonic-gate 	int		sectmoved = 0;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	sdp = orsp->rel_sym;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/*
2377c478bd9Sstevel@tonic-gate 	 * If the section this relocation is against has been discarded
2387c478bd9Sstevel@tonic-gate 	 * (-zignore), then also discard (skip) the relocation itself.
2397c478bd9Sstevel@tonic-gate 	 */
2407c478bd9Sstevel@tonic-gate 	if (orsp->rel_isdesc && ((orsp->rel_flags &
2417c478bd9Sstevel@tonic-gate 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
2427c478bd9Sstevel@tonic-gate 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
2435aefb655Srie 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
2447c478bd9Sstevel@tonic-gate 		return (1);
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * If this is a relocation against a move table, or expanded move
2497c478bd9Sstevel@tonic-gate 	 * table, adjust the relocation entries.
2507c478bd9Sstevel@tonic-gate 	 */
251bf994817SAli Bahrami 	if (RELAUX_GET_MOVE(orsp))
2525aefb655Srie 		ld_adj_movereloc(ofl, orsp);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * If this is a relocation against a section using a partial initialized
2567c478bd9Sstevel@tonic-gate 	 * symbol, adjust the embedded symbol info.
2577c478bd9Sstevel@tonic-gate 	 *
2587c478bd9Sstevel@tonic-gate 	 * The second argument of the am_I_partial() is the value stored at the
2597c478bd9Sstevel@tonic-gate 	 * target address relocation is going to be applied.
2607c478bd9Sstevel@tonic-gate 	 */
2617c478bd9Sstevel@tonic-gate 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
26257ef7aa9SRod Evans 		if (ofl->ofl_parsyms &&
2637c478bd9Sstevel@tonic-gate 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
2647c478bd9Sstevel@tonic-gate 		    /* LINTED */
2655aefb655Srie 		    (psym = ld_am_I_partial(orsp, *(Xword *)
266b3fbe5e6Sseizo 		    ((uchar_t *)(orsp->rel_isdesc->is_indata->d_buf) +
2677c478bd9Sstevel@tonic-gate 		    orsp->rel_roffset)))) {
2685aefb655Srie 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
2697c478bd9Sstevel@tonic-gate 			sectmoved = 1;
270de777a60Sab 		}
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	value = sdp->sd_sym->st_value;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_GOT) {
2767c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
2775aefb655Srie 		roffset = (Word)ld_calc_got_offset(orsp, ofl);
2785aefb655Srie 
2797c478bd9Sstevel@tonic-gate 	} else if (orsp->rel_flags & FLG_REL_PLT) {
2807c478bd9Sstevel@tonic-gate 		/*
2817c478bd9Sstevel@tonic-gate 		 * Note that relocations for PLT's actually
2827c478bd9Sstevel@tonic-gate 		 * cause a relocation againt the GOT.
2837c478bd9Sstevel@tonic-gate 		 */
2847c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osplt;
2857c478bd9Sstevel@tonic-gate 		roffset = (Word) (ofl->ofl_osgot->os_shdr->sh_addr) +
2867c478bd9Sstevel@tonic-gate 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		plt_entry(ofl, osp->os_relosdesc->os_szoutrels, sdp);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	} else if (orsp->rel_flags & FLG_REL_BSS) {
2917c478bd9Sstevel@tonic-gate 		/*
2927c478bd9Sstevel@tonic-gate 		 * This must be a R_386_COPY.  For these set the roffset to
2937c478bd9Sstevel@tonic-gate 		 * point to the new symbols location.
2947c478bd9Sstevel@tonic-gate 		 */
2957c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_isbss->is_osdesc;
2967c478bd9Sstevel@tonic-gate 		roffset = (Word)value;
2977c478bd9Sstevel@tonic-gate 	} else {
298bf994817SAli Bahrami 		osp = RELAUX_GET_OSDESC(orsp);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 		/*
3017c478bd9Sstevel@tonic-gate 		 * Calculate virtual offset of reference point; equals offset
3027c478bd9Sstevel@tonic-gate 		 * into section + vaddr of section for loadable sections, or
3037c478bd9Sstevel@tonic-gate 		 * offset plus section displacement for nonloadable sections.
3047c478bd9Sstevel@tonic-gate 		 */
3057c478bd9Sstevel@tonic-gate 		roffset = orsp->rel_roffset +
3067c478bd9Sstevel@tonic-gate 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
3077c478bd9Sstevel@tonic-gate 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
3087c478bd9Sstevel@tonic-gate 			roffset += orsp->rel_isdesc->is_osdesc->
3097c478bd9Sstevel@tonic-gate 			    os_shdr->sh_addr;
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
3137c478bd9Sstevel@tonic-gate 		relosp = ofl->ofl_osrel;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/*
3167c478bd9Sstevel@tonic-gate 	 * Assign the symbols index for the output relocation.  If the
3177c478bd9Sstevel@tonic-gate 	 * relocation refers to a SECTION symbol then it's index is based upon
3187c478bd9Sstevel@tonic-gate 	 * the output sections symbols index.  Otherwise the index can be
3197c478bd9Sstevel@tonic-gate 	 * derived from the symbols index itself.
3207c478bd9Sstevel@tonic-gate 	 */
3217c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_386_RELATIVE)
3227c478bd9Sstevel@tonic-gate 		ndx = STN_UNDEF;
3237c478bd9Sstevel@tonic-gate 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
3247c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
3257c478bd9Sstevel@tonic-gate 		if (sectmoved == 0) {
3267c478bd9Sstevel@tonic-gate 			/*
3277c478bd9Sstevel@tonic-gate 			 * Check for a null input section. This can
3287c478bd9Sstevel@tonic-gate 			 * occur if this relocation references a symbol
3297c478bd9Sstevel@tonic-gate 			 * generated by sym_add_sym().
3307c478bd9Sstevel@tonic-gate 			 */
33157ef7aa9SRod Evans 			if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
33257ef7aa9SRod Evans 				ndx = sdp->sd_isc->is_osdesc->os_identndx;
3337c478bd9Sstevel@tonic-gate 			else
3347c478bd9Sstevel@tonic-gate 				ndx = sdp->sd_shndx;
3357c478bd9Sstevel@tonic-gate 		} else
33635450702SAli Bahrami 			ndx = ofl->ofl_parexpnndx;
3377c478bd9Sstevel@tonic-gate 	} else
3387c478bd9Sstevel@tonic-gate 		ndx = sdp->sd_symndx;
3397c478bd9Sstevel@tonic-gate 
340cce0e03bSab 	/*
341cce0e03bSab 	 * If we have a replacement value for the relocation
342cce0e03bSab 	 * target, put it in place now.
343cce0e03bSab 	 */
344cce0e03bSab 	if (orsp->rel_flags & FLG_REL_NADDEND) {
345cce0e03bSab 		Xword	addend = orsp->rel_raddend;
346cce0e03bSab 		uchar_t	*addr;
347cce0e03bSab 
348cce0e03bSab 		/*
349cce0e03bSab 		 * Get the address of the data item we need to modify.
350cce0e03bSab 		 */
351cce0e03bSab 		addr = (uchar_t *)((uintptr_t)orsp->rel_roffset +
352cce0e03bSab 		    (uintptr_t)_elf_getxoff(orsp->rel_isdesc->is_indata));
353bf994817SAli Bahrami 		addr += (uintptr_t)RELAUX_GET_OSDESC(orsp)->os_outdata->d_buf;
354cce0e03bSab 		if (ld_reloc_targval_set(ofl, orsp, addr, addend) == 0)
355cce0e03bSab 			return (S_ERROR);
356cce0e03bSab 	}
357cce0e03bSab 
3580bc0887eSRichard Lowe 	if ((orsp->rel_rtype != M_R_NONE) &&
3590bc0887eSRichard Lowe 	    (orsp->rel_rtype != M_R_RELATIVE)) {
3600bc0887eSRichard Lowe 		if (ndx == 0) {
3610bc0887eSRichard Lowe 			Conv_inv_buf_t	inv_buf;
3620bc0887eSRichard Lowe 			Is_desc *isp = orsp->rel_isdesc;
3630bc0887eSRichard Lowe 
3640bc0887eSRichard Lowe 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NOSYMBOL),
3650bc0887eSRichard Lowe 			    conv_reloc_type(ofl->ofl_nehdr->e_machine,
3660bc0887eSRichard Lowe 			    orsp->rel_rtype, 0, &inv_buf),
3670bc0887eSRichard Lowe 			    isp->is_file->ifl_name, EC_WORD(isp->is_scnndx),
3680bc0887eSRichard Lowe 			    isp->is_name, EC_XWORD(roffset));
3690bc0887eSRichard Lowe 			return (S_ERROR);
3700bc0887eSRichard Lowe 		}
3710bc0887eSRichard Lowe 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
3747c478bd9Sstevel@tonic-gate 	rea.r_offset = roffset;
3755aefb655Srie 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_REL, &rea, relosp->os_name,
376bf994817SAli Bahrami 	    ld_reloc_sym_name(orsp)));
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/*
3797c478bd9Sstevel@tonic-gate 	 * Assert we haven't walked off the end of our relocation table.
3807c478bd9Sstevel@tonic-gate 	 */
3817c478bd9Sstevel@tonic-gate 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
3827c478bd9Sstevel@tonic-gate 
3830bc0887eSRichard Lowe 	relbits = (char *)relosp->os_outdata->d_buf;
3840bc0887eSRichard Lowe 
3857c478bd9Sstevel@tonic-gate 	(void) memcpy((relbits + relosp->os_szoutrels),
3867c478bd9Sstevel@tonic-gate 	    (char *)&rea, sizeof (Rel));
3877c478bd9Sstevel@tonic-gate 	relosp->os_szoutrels += sizeof (Rel);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	/*
3907c478bd9Sstevel@tonic-gate 	 * Determine if this relocation is against a non-writable, allocatable
3917c478bd9Sstevel@tonic-gate 	 * section.  If so we may need to provide a text relocation diagnostic.
3927c478bd9Sstevel@tonic-gate 	 * Note that relocations against the .plt (R_386_JMP_SLOT) actually
3937c478bd9Sstevel@tonic-gate 	 * result in modifications to the .got.
3947c478bd9Sstevel@tonic-gate 	 */
3957c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == R_386_JMP_SLOT)
3967c478bd9Sstevel@tonic-gate 		osp = ofl->ofl_osgot;
3977c478bd9Sstevel@tonic-gate 
3981007fd6fSAli Bahrami 	ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
3997c478bd9Sstevel@tonic-gate 	return (1);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * i386 Instructions for TLS processing
4047c478bd9Sstevel@tonic-gate  */
405b3fbe5e6Sseizo static uchar_t tlsinstr_gd_ie[] = {
4067c478bd9Sstevel@tonic-gate 	/*
4077c478bd9Sstevel@tonic-gate 	 * 0x00	movl %gs:0x0, %eax
4087c478bd9Sstevel@tonic-gate 	 */
4097c478bd9Sstevel@tonic-gate 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
4107c478bd9Sstevel@tonic-gate 	/*
4117c478bd9Sstevel@tonic-gate 	 * 0x06	addl x(%eax), %eax
4127c478bd9Sstevel@tonic-gate 	 * 0x0c ...
4137c478bd9Sstevel@tonic-gate 	 */
4147c478bd9Sstevel@tonic-gate 	0x03, 0x80, 0x00, 0x00, 0x00, 0x00
4157c478bd9Sstevel@tonic-gate };
4167c478bd9Sstevel@tonic-gate 
417b3fbe5e6Sseizo static uchar_t tlsinstr_gd_le[] = {
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * 0x00 movl %gs:0x0, %eax
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
4227c478bd9Sstevel@tonic-gate 	/*
4237c478bd9Sstevel@tonic-gate 	 * 0x06 addl $0x0, %eax
4247c478bd9Sstevel@tonic-gate 	 */
4257c478bd9Sstevel@tonic-gate 	0x05, 0x00, 0x00, 0x00, 0x00,
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * 0x0b nop
4287c478bd9Sstevel@tonic-gate 	 * 0x0c
4297c478bd9Sstevel@tonic-gate 	 */
4307c478bd9Sstevel@tonic-gate 	0x90
4317c478bd9Sstevel@tonic-gate };
4327c478bd9Sstevel@tonic-gate 
433096c97d6SRichard Lowe static uchar_t tlsinstr_ld_le_movgs[] = {
4347c478bd9Sstevel@tonic-gate 	/*
435096c97d6SRichard Lowe 	 * 0x00 movl %gs:0x0,%eax
4367c478bd9Sstevel@tonic-gate 	 */
437096c97d6SRichard Lowe 	0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
438096c97d6SRichard Lowe };
439096c97d6SRichard Lowe 
440096c97d6SRichard Lowe /*
441096c97d6SRichard Lowe  * 0x00 nopl 0(%eax,%eax) -- the intel recommended 5-byte nop
442096c97d6SRichard Lowe  * See Intel® 64 and IA-32 Architectures Software Developer’s Manual
443096c97d6SRichard Lowe  *    Volume 2B: Instruction Set Reference, M-U
444096c97d6SRichard Lowe  *    Table 4-12, Recommended Multi-Byte Sequence of NOP Instruction
445096c97d6SRichard Lowe  */
446096c97d6SRichard Lowe static uchar_t tlsinstr_nop5[] = {
447096c97d6SRichard Lowe 
448096c97d6SRichard Lowe 	0x0f, 0x1f, 0x44, 0x00, 0x00
4497c478bd9Sstevel@tonic-gate };
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate #define	TLS_GD_IE_MOV	0x8b	/* movl opcode */
4527c478bd9Sstevel@tonic-gate #define	TLS_GD_IE_POP	0x58	/* popl + reg */
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate #define	TLS_GD_LE_MOVL	0xb8	/* movl + reg */
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate #define	TLS_NOP		0x90	/* NOP instruction */
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate #define	MODRM_MSK_MOD	0xc0
4597c478bd9Sstevel@tonic-gate #define	MODRM_MSK_RO	0x38
4607c478bd9Sstevel@tonic-gate #define	MODRM_MSK_RM	0x07
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate #define	SIB_MSK_SS	0xc0
4637c478bd9Sstevel@tonic-gate #define	SIB_MSK_IND	0x38
4647c478bd9Sstevel@tonic-gate #define	SIB_MSK_BS	0x07
4657c478bd9Sstevel@tonic-gate 
4665aefb655Srie static Fixupret
tls_fixups(Ofl_desc * ofl,Rel_desc * arsp)4675aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = arsp->rel_sym;
4707c478bd9Sstevel@tonic-gate 	Word		rtype = arsp->rel_rtype;
471b3fbe5e6Sseizo 	uchar_t		*offset, r1, r2;
4727c478bd9Sstevel@tonic-gate 
473b3fbe5e6Sseizo 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
474b3fbe5e6Sseizo 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
475bf994817SAli Bahrami 	    (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	if (sdp->sd_ref == REF_DYN_NEED) {
4787c478bd9Sstevel@tonic-gate 		/*
4797c478bd9Sstevel@tonic-gate 		 * IE reference model
4807c478bd9Sstevel@tonic-gate 		 */
4817c478bd9Sstevel@tonic-gate 		switch (rtype) {
4827c478bd9Sstevel@tonic-gate 		case R_386_TLS_GD:
4837c478bd9Sstevel@tonic-gate 			/*
4847c478bd9Sstevel@tonic-gate 			 * Transition:
4857c478bd9Sstevel@tonic-gate 			 *	0x0 leal x@tlsgd(,r1,1), %eax
4867c478bd9Sstevel@tonic-gate 			 *	0x7 call ___tls_get_addr
4877c478bd9Sstevel@tonic-gate 			 *	0xc
4887c478bd9Sstevel@tonic-gate 			 * To:
4897c478bd9Sstevel@tonic-gate 			 *	0x0 movl %gs:0, %eax
4907c478bd9Sstevel@tonic-gate 			 *	0x6 addl x@gotntpoff(r1), %eax
4917c478bd9Sstevel@tonic-gate 			 */
4925aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
493bf994817SAli Bahrami 			    R_386_TLS_GOTIE, arsp, ld_reloc_sym_name));
4947c478bd9Sstevel@tonic-gate 			arsp->rel_rtype = R_386_TLS_GOTIE;
4957c478bd9Sstevel@tonic-gate 			arsp->rel_roffset += 5;
4965aefb655Srie 
4977c478bd9Sstevel@tonic-gate 			/*
498051d39bbSrie 			 * Adjust 'offset' to beginning of instruction
4997c478bd9Sstevel@tonic-gate 			 * sequence.
5007c478bd9Sstevel@tonic-gate 			 */
5017c478bd9Sstevel@tonic-gate 			offset -= 3;
5027c478bd9Sstevel@tonic-gate 			r1 = (offset[2] & SIB_MSK_IND) >> 3;
5037c478bd9Sstevel@tonic-gate 			(void) memcpy(offset, tlsinstr_gd_ie,
5045aefb655Srie 			    sizeof (tlsinstr_gd_ie));
5055aefb655Srie 
5067c478bd9Sstevel@tonic-gate 			/*
5077c478bd9Sstevel@tonic-gate 			 * set register %r1 into the addl
5087c478bd9Sstevel@tonic-gate 			 * instruction.
5097c478bd9Sstevel@tonic-gate 			 */
5107c478bd9Sstevel@tonic-gate 			offset[0x7] |= r1;
5117c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
5125aefb655Srie 
5137c478bd9Sstevel@tonic-gate 		case R_386_TLS_GD_PLT:
5147c478bd9Sstevel@tonic-gate 			/*
5157c478bd9Sstevel@tonic-gate 			 * Fixup done via the TLS_GD relocation
5167c478bd9Sstevel@tonic-gate 			 */
5175aefb655Srie 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
518bf994817SAli Bahrami 			    R_386_NONE, arsp, ld_reloc_sym_name));
5197c478bd9Sstevel@tonic-gate 			return (FIX_DONE);
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/*
5247c478bd9Sstevel@tonic-gate 	 * LE reference model
5257c478bd9Sstevel@tonic-gate 	 */
5267c478bd9Sstevel@tonic-gate 	switch (rtype) {
5277c478bd9Sstevel@tonic-gate 	case R_386_TLS_GD:
5287c478bd9Sstevel@tonic-gate 		/*
5297c478bd9Sstevel@tonic-gate 		 * Transition:
5307c478bd9Sstevel@tonic-gate 		 *	0x0 leal x@tlsgd(,r1,1), %eax
5317c478bd9Sstevel@tonic-gate 		 *	0x7 call ___tls_get_addr
5327c478bd9Sstevel@tonic-gate 		 *	0xc
5337c478bd9Sstevel@tonic-gate 		 * To:
5347c478bd9Sstevel@tonic-gate 		 *	0x0 movl %gs:0, %eax
5357c478bd9Sstevel@tonic-gate 		 *	0x6 addl $x@ntpoff, %eax
5367c478bd9Sstevel@tonic-gate 		 *	0xb nop
5377c478bd9Sstevel@tonic-gate 		 *	0xc
5387c478bd9Sstevel@tonic-gate 		 */
5395aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
540bf994817SAli Bahrami 		    R_386_TLS_LE, arsp, ld_reloc_sym_name));
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
5437c478bd9Sstevel@tonic-gate 		arsp->rel_roffset += 4;
5445aefb655Srie 
5457c478bd9Sstevel@tonic-gate 		/*
546051d39bbSrie 		 * Adjust 'offset' to beginning of instruction
5477c478bd9Sstevel@tonic-gate 		 * sequence.
5487c478bd9Sstevel@tonic-gate 		 */
5497c478bd9Sstevel@tonic-gate 		offset -= 3;
5507c478bd9Sstevel@tonic-gate 		(void) memcpy(offset, tlsinstr_gd_le,
5515aefb655Srie 		    sizeof (tlsinstr_gd_le));
5527c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
5535aefb655Srie 
5547c478bd9Sstevel@tonic-gate 	case R_386_TLS_GD_PLT:
5557c478bd9Sstevel@tonic-gate 	case R_386_PLT32:
5567c478bd9Sstevel@tonic-gate 		/*
557096c97d6SRichard Lowe 		 * Fixup done via the TLS_GD/TLS_LDM relocation processing
558096c97d6SRichard Lowe 		 * and ld_reloc_plt() handling __tls_get_addr().
5597c478bd9Sstevel@tonic-gate 		 */
5605aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
561bf994817SAli Bahrami 		    R_386_NONE, arsp, ld_reloc_sym_name));
5627c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
5635aefb655Srie 
5647c478bd9Sstevel@tonic-gate 	case R_386_TLS_LDM_PLT:
5655aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
566bf994817SAli Bahrami 		    R_386_NONE, arsp, ld_reloc_sym_name));
5675aefb655Srie 
5687c478bd9Sstevel@tonic-gate 		/*
5697c478bd9Sstevel@tonic-gate 		 * Transition:
5707c478bd9Sstevel@tonic-gate 		 *	call __tls_get_addr()
5717c478bd9Sstevel@tonic-gate 		 * to:
572096c97d6SRichard Lowe 		 *	nopl 0x0(%eax,%eax)
5737c478bd9Sstevel@tonic-gate 		 */
574096c97d6SRichard Lowe 		(void) memcpy(offset - 1, tlsinstr_nop5,
575096c97d6SRichard Lowe 		    sizeof (tlsinstr_nop5));
5767c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
5775aefb655Srie 
5787c478bd9Sstevel@tonic-gate 	case R_386_TLS_LDM:
5795aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
580bf994817SAli Bahrami 		    R_386_NONE, arsp, ld_reloc_sym_name));
5815aefb655Srie 
5827c478bd9Sstevel@tonic-gate 		/*
5837c478bd9Sstevel@tonic-gate 		 * Transition:
5847c478bd9Sstevel@tonic-gate 		 *
5857c478bd9Sstevel@tonic-gate 		 *  0x00 leal x1@tlsldm(%ebx), %eax
5867c478bd9Sstevel@tonic-gate 		 *  0x06 call ___tls_get_addr
5877c478bd9Sstevel@tonic-gate 		 *
5887c478bd9Sstevel@tonic-gate 		 * to:
5897c478bd9Sstevel@tonic-gate 		 *
5907c478bd9Sstevel@tonic-gate 		 *  0x00 movl %gs:0, %eax
5917c478bd9Sstevel@tonic-gate 		 */
592096c97d6SRichard Lowe 		(void) memcpy(offset - 2, tlsinstr_ld_le_movgs,
593096c97d6SRichard Lowe 		    sizeof (tlsinstr_ld_le_movgs));
594096c97d6SRichard Lowe 
595096c97d6SRichard Lowe 		/*
596096c97d6SRichard Lowe 		 *  We implicitly treat this as if a R_386_TLS_LDM_PLT for the
597096c97d6SRichard Lowe 		 *  __tls_get_addr call followed it as the GNU compiler
598096c97d6SRichard Lowe 		 *  doesn't generate one.  This is safe, because if one _does_
599096c97d6SRichard Lowe 		 *  exist we'll just write the nop again.
600096c97d6SRichard Lowe 		 */
601096c97d6SRichard Lowe 		(void) memcpy(offset + 4, tlsinstr_nop5,
602096c97d6SRichard Lowe 		    sizeof (tlsinstr_nop5));
6037c478bd9Sstevel@tonic-gate 		return (FIX_DONE);
6045aefb655Srie 
6057c478bd9Sstevel@tonic-gate 	case R_386_TLS_LDO_32:
6067c478bd9Sstevel@tonic-gate 		/*
6077c478bd9Sstevel@tonic-gate 		 *  Instructions:
6087c478bd9Sstevel@tonic-gate 		 *
6097c478bd9Sstevel@tonic-gate 		 *  0x10 leal x1@dtpoff(%eax), %edx	R_386_TLS_LDO_32
6107c478bd9Sstevel@tonic-gate 		 *		to
6117c478bd9Sstevel@tonic-gate 		 *  0x10 leal x1@ntpoff(%eax), %edx	R_386_TLS_LE
6127c478bd9Sstevel@tonic-gate 		 *
6137c478bd9Sstevel@tonic-gate 		 */
6147c478bd9Sstevel@tonic-gate 		offset -= 2;
6157c478bd9Sstevel@tonic-gate 
6165aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
617bf994817SAli Bahrami 		    R_386_TLS_LE, arsp, ld_reloc_sym_name));
6187c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
6197c478bd9Sstevel@tonic-gate 		return (FIX_RELOC);
6205aefb655Srie 
6217c478bd9Sstevel@tonic-gate 	case R_386_TLS_GOTIE:
6227c478bd9Sstevel@tonic-gate 		/*
6237c478bd9Sstevel@tonic-gate 		 * These transitions are a little different than the
6247c478bd9Sstevel@tonic-gate 		 * others, in that we could have multiple instructions
6257c478bd9Sstevel@tonic-gate 		 * pointed to by a single relocation.  Depending upon the
6267c478bd9Sstevel@tonic-gate 		 * instruction, we perform a different code transition.
6277c478bd9Sstevel@tonic-gate 		 *
6287c478bd9Sstevel@tonic-gate 		 * Here's the known transitions:
6297c478bd9Sstevel@tonic-gate 		 *
6307c478bd9Sstevel@tonic-gate 		 *  1) movl foo@gotntpoff(%reg1), %reg2
6317c478bd9Sstevel@tonic-gate 		 *	0x8b, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
6327c478bd9Sstevel@tonic-gate 		 *
6337c478bd9Sstevel@tonic-gate 		 *  2) addl foo@gotntpoff(%reg1), %reg2
6347c478bd9Sstevel@tonic-gate 		 *	0x03, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
6357c478bd9Sstevel@tonic-gate 		 *
6367c478bd9Sstevel@tonic-gate 		 *  Transitions IE -> LE
6377c478bd9Sstevel@tonic-gate 		 *
6387c478bd9Sstevel@tonic-gate 		 *  1) movl $foo@ntpoff, %reg2
6397c478bd9Sstevel@tonic-gate 		 *	0xc7, 0xc0 | reg2, foo@ntpoff
6407c478bd9Sstevel@tonic-gate 		 *
6417c478bd9Sstevel@tonic-gate 		 *  2) addl $foo@ntpoff, %reg2
6427c478bd9Sstevel@tonic-gate 		 *	0x81, 0xc0 | reg2, foo@ntpoff
6437c478bd9Sstevel@tonic-gate 		 *
6447c478bd9Sstevel@tonic-gate 		 * Note: reg1 != 4 (%esp)
6457c478bd9Sstevel@tonic-gate 		 */
6465aefb655Srie 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
647bf994817SAli Bahrami 		    R_386_TLS_LE, arsp, ld_reloc_sym_name));
6487c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
6495aefb655Srie 
6507c478bd9Sstevel@tonic-gate 		offset -= 2;
6517c478bd9Sstevel@tonic-gate 		r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6527c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x8b) {
6537c478bd9Sstevel@tonic-gate 			/* case 1 above */
6547c478bd9Sstevel@tonic-gate 			offset[0] = 0xc7;	/* movl */
6557c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
6567c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x03) {
6607c478bd9Sstevel@tonic-gate 			/* case 2 above */
6617c478bd9Sstevel@tonic-gate 			assert(offset[0] == 0x03);
6627c478bd9Sstevel@tonic-gate 			offset[0] = 0x81;	/* addl */
6637c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
6647c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
6657c478bd9Sstevel@tonic-gate 		}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		/*
6687c478bd9Sstevel@tonic-gate 		 * Unexpected instruction sequence - fatal error.
6697c478bd9Sstevel@tonic-gate 		 */
670de777a60Sab 		{
671de777a60Sab 			Conv_inv_buf_t	inv_buf;
672de777a60Sab 
6731007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
674de777a60Sab 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
675de777a60Sab 			    arsp->rel_isdesc->is_file->ifl_name,
676bf994817SAli Bahrami 			    ld_reloc_sym_name(arsp),
677de777a60Sab 			    arsp->rel_isdesc->is_name,
678de777a60Sab 			    EC_OFF(arsp->rel_roffset));
679de777a60Sab 		}
6807c478bd9Sstevel@tonic-gate 		return (FIX_ERROR);
6815aefb655Srie 
6827c478bd9Sstevel@tonic-gate 	case R_386_TLS_IE:
6837c478bd9Sstevel@tonic-gate 		/*
6847c478bd9Sstevel@tonic-gate 		 * These transitions are a little different than the
6857c478bd9Sstevel@tonic-gate 		 * others, in that we could have multiple instructions
6867c478bd9Sstevel@tonic-gate 		 * pointed to by a single relocation.  Depending upon the
6877c478bd9Sstevel@tonic-gate 		 * instruction, we perform a different code transition.
6887c478bd9Sstevel@tonic-gate 		 *
6897c478bd9Sstevel@tonic-gate 		 * Here's the known transitions:
6907c478bd9Sstevel@tonic-gate 		 *  1) movl foo@indntpoff, %eax
6917c478bd9Sstevel@tonic-gate 		 *	0xa1, foo@indntpoff
6927c478bd9Sstevel@tonic-gate 		 *
6937c478bd9Sstevel@tonic-gate 		 *  2) movl foo@indntpoff, %eax
6947c478bd9Sstevel@tonic-gate 		 *	0x8b, 0x05 | (reg << 3), foo@gotntpoff
6957c478bd9Sstevel@tonic-gate 		 *
6967c478bd9Sstevel@tonic-gate 		 *  3) addl foo@indntpoff, %eax
6977c478bd9Sstevel@tonic-gate 		 *	0x03, 0x05 | (reg << 3), foo@gotntpoff
6987c478bd9Sstevel@tonic-gate 		 *
6997c478bd9Sstevel@tonic-gate 		 *  Transitions IE -> LE
7007c478bd9Sstevel@tonic-gate 		 *
7017c478bd9Sstevel@tonic-gate 		 *  1) movl $foo@ntpoff, %eax
7027c478bd9Sstevel@tonic-gate 		 *	0xb8, foo@ntpoff
7037c478bd9Sstevel@tonic-gate 		 *
7047c478bd9Sstevel@tonic-gate 		 *  2) movl $foo@ntpoff, %reg
7057c478bd9Sstevel@tonic-gate 		 *	0xc7, 0xc0 | reg, foo@ntpoff
7067c478bd9Sstevel@tonic-gate 		 *
7077c478bd9Sstevel@tonic-gate 		 *  3) addl $foo@ntpoff, %reg
7087c478bd9Sstevel@tonic-gate 		 *	0x81, 0xc0 | reg, foo@ntpoff
7097c478bd9Sstevel@tonic-gate 		 */
7107c478bd9Sstevel@tonic-gate 		arsp->rel_rtype = R_386_TLS_LE;
7117c478bd9Sstevel@tonic-gate 		offset--;
7127c478bd9Sstevel@tonic-gate 		if (offset[0] == 0xa1) {
7137c478bd9Sstevel@tonic-gate 			/* case 1 above */
7147c478bd9Sstevel@tonic-gate 			offset[0] = 0xb8;	/*  movl */
7157c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 		offset--;
7197c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x8b) {
7207c478bd9Sstevel@tonic-gate 			/* case 2 above */
7217c478bd9Sstevel@tonic-gate 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
7227c478bd9Sstevel@tonic-gate 			offset[0] = 0xc7;	/* movl */
7237c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
7247c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
7257c478bd9Sstevel@tonic-gate 		}
7267c478bd9Sstevel@tonic-gate 		if (offset[0] == 0x03) {
7277c478bd9Sstevel@tonic-gate 			/* case 3 above */
7287c478bd9Sstevel@tonic-gate 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
7297c478bd9Sstevel@tonic-gate 			offset[0] = 0x81;	/* addl */
7307c478bd9Sstevel@tonic-gate 			offset[1] = 0xc0 | r2;
7317c478bd9Sstevel@tonic-gate 			return (FIX_RELOC);
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 		/*
7347c478bd9Sstevel@tonic-gate 		 * Unexpected instruction sequence - fatal error.
7357c478bd9Sstevel@tonic-gate 		 */
736de777a60Sab 		{
737de777a60Sab 			Conv_inv_buf_t	inv_buf;
738de777a60Sab 
7391007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
740de777a60Sab 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
741de777a60Sab 			    arsp->rel_isdesc->is_file->ifl_name,
742bf994817SAli Bahrami 			    ld_reloc_sym_name(arsp),
743de777a60Sab 			    arsp->rel_isdesc->is_name,
744de777a60Sab 			    EC_OFF(arsp->rel_roffset));
745de777a60Sab 		}
7467c478bd9Sstevel@tonic-gate 		return (FIX_ERROR);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	return (FIX_RELOC);
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate 
751ba2be530Sab static uintptr_t
ld_do_activerelocs(Ofl_desc * ofl)7525aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
7537c478bd9Sstevel@tonic-gate {
754141040e8Srie 	Rel_desc	*arsp;
755bf994817SAli Bahrami 	Rel_cachebuf	*rcbp;
75657ef7aa9SRod Evans 	Aliste		idx;
7577c478bd9Sstevel@tonic-gate 	uintptr_t	return_code = 1;
7581d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
7597c478bd9Sstevel@tonic-gate 
760bf994817SAli Bahrami 	if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
7617010c12aSrie 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
7627010c12aSrie 
7637c478bd9Sstevel@tonic-gate 	/*
764141040e8Srie 	 * Process active relocations.
7657c478bd9Sstevel@tonic-gate 	 */
766bf994817SAli Bahrami 	REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
767bf994817SAli Bahrami 		uchar_t		*addr;
768096c97d6SRichard Lowe 		Xword		value;
769bf994817SAli Bahrami 		Sym_desc	*sdp;
770bf994817SAli Bahrami 		const char	*ifl_name;
771bf994817SAli Bahrami 		Xword		refaddr;
772bf994817SAli Bahrami 		int		moved = 0;
773bf994817SAli Bahrami 		Gotref		gref;
774bf994817SAli Bahrami 		Os_desc		*osp;
7757c478bd9Sstevel@tonic-gate 
776bf994817SAli Bahrami 		/*
777bf994817SAli Bahrami 		 * If the section this relocation is against has been discarded
778bf994817SAli Bahrami 		 * (-zignore), then discard (skip) the relocation itself.
779bf994817SAli Bahrami 		 */
780bf994817SAli Bahrami 		if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
781bf994817SAli Bahrami 		    ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
782bf994817SAli Bahrami 		    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
783bf994817SAli Bahrami 			DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
784bf994817SAli Bahrami 			continue;
785bf994817SAli Bahrami 		}
7867c478bd9Sstevel@tonic-gate 
787bf994817SAli Bahrami 		/*
788bf994817SAli Bahrami 		 * We determine what the 'got reference' model (if required)
789bf994817SAli Bahrami 		 * is at this point.  This needs to be done before tls_fixup()
790bf994817SAli Bahrami 		 * since it may 'transition' our instructions.
791bf994817SAli Bahrami 		 *
792bf994817SAli Bahrami 		 * The got table entries have already been assigned,
793bf994817SAli Bahrami 		 * and we bind to those initial entries.
794bf994817SAli Bahrami 		 */
795bf994817SAli Bahrami 		if (arsp->rel_flags & FLG_REL_DTLS)
796bf994817SAli Bahrami 			gref = GOT_REF_TLSGD;
797bf994817SAli Bahrami 		else if (arsp->rel_flags & FLG_REL_MTLS)
798bf994817SAli Bahrami 			gref = GOT_REF_TLSLD;
799bf994817SAli Bahrami 		else if (arsp->rel_flags & FLG_REL_STLS)
800bf994817SAli Bahrami 			gref = GOT_REF_TLSIE;
801bf994817SAli Bahrami 		else
802bf994817SAli Bahrami 			gref = GOT_REF_GENERIC;
8037c478bd9Sstevel@tonic-gate 
804bf994817SAli Bahrami 		/*
805bf994817SAli Bahrami 		 * Perform any required TLS fixups.
806bf994817SAli Bahrami 		 */
807bf994817SAli Bahrami 		if (arsp->rel_flags & FLG_REL_TLSFIX) {
808bf994817SAli Bahrami 			Fixupret	ret;
8097c478bd9Sstevel@tonic-gate 
810bf994817SAli Bahrami 			if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
811bf994817SAli Bahrami 				return (S_ERROR);
812bf994817SAli Bahrami 			if (ret == FIX_DONE)
813bf994817SAli Bahrami 				continue;
814bf994817SAli Bahrami 		}
8157c478bd9Sstevel@tonic-gate 
816bf994817SAli Bahrami 		/*
817bf994817SAli Bahrami 		 * If this is a relocation against a move table, or
818bf994817SAli Bahrami 		 * expanded move table, adjust the relocation entries.
819bf994817SAli Bahrami 		 */
820bf994817SAli Bahrami 		if (RELAUX_GET_MOVE(arsp))
821bf994817SAli Bahrami 			ld_adj_movereloc(ofl, arsp);
822bf994817SAli Bahrami 
823bf994817SAli Bahrami 		sdp = arsp->rel_sym;
824bf994817SAli Bahrami 		refaddr = arsp->rel_roffset +
825bf994817SAli Bahrami 		    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
826bf994817SAli Bahrami 
827bf994817SAli Bahrami 		if (arsp->rel_flags & FLG_REL_CLVAL)
828bf994817SAli Bahrami 			value = 0;
829bf994817SAli Bahrami 		else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
8307c478bd9Sstevel@tonic-gate 			/*
831bf994817SAli Bahrami 			 * The value for a symbol pointing to a SECTION
832bf994817SAli Bahrami 			 * is based off of that sections position.
8337c478bd9Sstevel@tonic-gate 			 */
834bf994817SAli Bahrami 			if (sdp->sd_isc->is_flags & FLG_IS_RELUPD) {
835bf994817SAli Bahrami 				Sym_desc	*sym;
836bf994817SAli Bahrami 				Xword		radd;
837bf994817SAli Bahrami 				uchar_t		*raddr = (uchar_t *)
838bf994817SAli Bahrami 				    arsp->rel_isdesc->is_indata->d_buf +
839bf994817SAli Bahrami 				    arsp->rel_roffset;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 				/*
842bf994817SAli Bahrami 				 * This is a REL platform. Hence, the second
843bf994817SAli Bahrami 				 * argument of ld_am_I_partial() is the value
844bf994817SAli Bahrami 				 * stored at the target address where the
845bf994817SAli Bahrami 				 * relocation is going to be applied.
8467c478bd9Sstevel@tonic-gate 				 */
847bf994817SAli Bahrami 				if (ld_reloc_targval_get(ofl, arsp, raddr,
848bf994817SAli Bahrami 				    &radd) == 0)
849bf994817SAli Bahrami 					return (S_ERROR);
850bf994817SAli Bahrami 				sym = ld_am_I_partial(arsp, radd);
851bf994817SAli Bahrami 				if (sym) {
852bf994817SAli Bahrami 					Sym	*osym = sym->sd_osym;
853b26cc8daSAli Bahrami 
8547c478bd9Sstevel@tonic-gate 					/*
855bf994817SAli Bahrami 					 * The symbol was moved, so adjust the
856bf994817SAli Bahrami 					 * value relative to the new section.
8577c478bd9Sstevel@tonic-gate 					 */
858bf994817SAli Bahrami 					value = sym->sd_sym->st_value;
859bf994817SAli Bahrami 					moved = 1;
860bf994817SAli Bahrami 
861bf994817SAli Bahrami 					/*
862bf994817SAli Bahrami 					 * The original raddend covers the
863bf994817SAli Bahrami 					 * displacement from the section start
864bf994817SAli Bahrami 					 * to the desired address. The value
865bf994817SAli Bahrami 					 * computed above gets us from the
866bf994817SAli Bahrami 					 * section start to the start of the
867bf994817SAli Bahrami 					 * symbol range. Adjust the old raddend
868bf994817SAli Bahrami 					 * to remove the offset from section
869bf994817SAli Bahrami 					 * start to symbol start, leaving the
870bf994817SAli Bahrami 					 * displacement within the range of
871bf994817SAli Bahrami 					 * the symbol.
872bf994817SAli Bahrami 					 */
873bf994817SAli Bahrami 					if (osym->st_value != 0) {
874bf994817SAli Bahrami 						radd -= osym->st_value;
875bf994817SAli Bahrami 						if (ld_reloc_targval_set(ofl,
876bf994817SAli Bahrami 						    arsp, raddr, radd) == 0)
877bf994817SAli Bahrami 							return (S_ERROR);
878b26cc8daSAli Bahrami 					}
879b26cc8daSAli Bahrami 				}
880bf994817SAli Bahrami 			}
881bf994817SAli Bahrami 			if (!moved) {
882bf994817SAli Bahrami 				value = _elf_getxoff(sdp->sd_isc->is_indata);
883bf994817SAli Bahrami 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
884bf994817SAli Bahrami 					value += sdp->sd_isc->
885bf994817SAli Bahrami 					    is_osdesc->os_shdr->sh_addr;
886bf994817SAli Bahrami 			}
887bf994817SAli Bahrami 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
888bf994817SAli Bahrami 				value -= ofl->ofl_tlsphdr->p_vaddr;
8892926dd2eSrie 
890bf994817SAli Bahrami 		} else if (IS_SIZE(arsp->rel_rtype)) {
891bf994817SAli Bahrami 			/*
892bf994817SAli Bahrami 			 * Size relocations require the symbols size.
893bf994817SAli Bahrami 			 */
894bf994817SAli Bahrami 			value = sdp->sd_sym->st_size;
89508278a5eSRod Evans 
896bf994817SAli Bahrami 		} else if ((sdp->sd_flags & FLG_SY_CAP) &&
897bf994817SAli Bahrami 		    sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
898bf994817SAli Bahrami 			/*
899bf994817SAli Bahrami 			 * If relocation is against a capabilities symbol, we
900bf994817SAli Bahrami 			 * need to jump to an associated PLT, so that at runtime
901bf994817SAli Bahrami 			 * ld.so.1 is involved to determine the best binding
902bf994817SAli Bahrami 			 * choice. Otherwise, the value is the symbols value.
903bf994817SAli Bahrami 			 */
904bf994817SAli Bahrami 			value = ld_calc_plt_addr(sdp, ofl);
905bf994817SAli Bahrami 
906bf994817SAli Bahrami 		} else
907bf994817SAli Bahrami 			value = sdp->sd_sym->st_value;
908bf994817SAli Bahrami 
909bf994817SAli Bahrami 		/*
910bf994817SAli Bahrami 		 * Relocation against the GLOBAL_OFFSET_TABLE.
911bf994817SAli Bahrami 		 */
912bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_GOT) &&
913bf994817SAli Bahrami 		    !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
914bf994817SAli Bahrami 			return (S_ERROR);
915bf994817SAli Bahrami 		osp = RELAUX_GET_OSDESC(arsp);
916bf994817SAli Bahrami 
917bf994817SAli Bahrami 		/*
918bf994817SAli Bahrami 		 * If loadable and not producing a relocatable object add the
919bf994817SAli Bahrami 		 * sections virtual address to the reference address.
920bf994817SAli Bahrami 		 */
921bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_LOAD) &&
922bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0))
923bf994817SAli Bahrami 			refaddr +=
924bf994817SAli Bahrami 			    arsp->rel_isdesc->is_osdesc->os_shdr->sh_addr;
925bf994817SAli Bahrami 
926bf994817SAli Bahrami 		/*
927bf994817SAli Bahrami 		 * If this entry has a PLT assigned to it, its value is actually
928bf994817SAli Bahrami 		 * the address of the PLT (and not the address of the function).
929bf994817SAli Bahrami 		 */
930bf994817SAli Bahrami 		if (IS_PLT(arsp->rel_rtype)) {
931bf994817SAli Bahrami 			if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
93208278a5eSRod Evans 				value = ld_calc_plt_addr(sdp, ofl);
933bf994817SAli Bahrami 		}
93408278a5eSRod Evans 
935bf994817SAli Bahrami 		/*
936bf994817SAli Bahrami 		 * Determine whether the value needs further adjustment. Filter
937bf994817SAli Bahrami 		 * through the attributes of the relocation to determine what
938bf994817SAli Bahrami 		 * adjustment is required.  Note, many of the following cases
939bf994817SAli Bahrami 		 * are only applicable when a .got is present.  As a .got is
940bf994817SAli Bahrami 		 * not generated when a relocatable object is being built,
941bf994817SAli Bahrami 		 * any adjustments that require a .got need to be skipped.
942bf994817SAli Bahrami 		 */
943bf994817SAli Bahrami 		if ((arsp->rel_flags & FLG_REL_GOT) &&
944bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
945bf994817SAli Bahrami 			Xword		R1addr;
946bf994817SAli Bahrami 			uintptr_t	R2addr;
947bf994817SAli Bahrami 			Word		gotndx;
948bf994817SAli Bahrami 			Gotndx		*gnp;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 			/*
951bf994817SAli Bahrami 			 * Perform relocation against GOT table.  Since this
952bf994817SAli Bahrami 			 * doesn't fit exactly into a relocation we place the
953bf994817SAli Bahrami 			 * appropriate byte in the GOT directly
954bf994817SAli Bahrami 			 *
955bf994817SAli Bahrami 			 * Calculate offset into GOT at which to apply
956bf994817SAli Bahrami 			 * the relocation.
9577c478bd9Sstevel@tonic-gate 			 */
958bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
959bf994817SAli Bahrami 			assert(gnp);
9607c478bd9Sstevel@tonic-gate 
961bf994817SAli Bahrami 			if (arsp->rel_rtype == R_386_TLS_DTPOFF32)
962bf994817SAli Bahrami 				gotndx = gnp->gn_gotndx + 1;
963bf994817SAli Bahrami 			else
964bf994817SAli Bahrami 				gotndx = gnp->gn_gotndx;
965bf994817SAli Bahrami 
966bf994817SAli Bahrami 			R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 			/*
969bf994817SAli Bahrami 			 * Add the GOTs data's offset.
9707c478bd9Sstevel@tonic-gate 			 */
971bf994817SAli Bahrami 			R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
972bf994817SAli Bahrami 
973bf994817SAli Bahrami 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
974bf994817SAli Bahrami 			    M_MACH, SHT_REL, arsp, R1addr, value,
975bf994817SAli Bahrami 			    ld_reloc_sym_name));
9767c478bd9Sstevel@tonic-gate 
977141040e8Srie 			/*
978bf994817SAli Bahrami 			 * And do it.
979141040e8Srie 			 */
980bf994817SAli Bahrami 			if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
981bf994817SAli Bahrami 				*(Xword *)R2addr = ld_bswap_Xword(value);
982bf994817SAli Bahrami 			else
983bf994817SAli Bahrami 				*(Xword *)R2addr = value;
984bf994817SAli Bahrami 			continue;
985bf994817SAli Bahrami 
986bf994817SAli Bahrami 		} else if (IS_GOT_BASED(arsp->rel_rtype) &&
987bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
988bf994817SAli Bahrami 			value -= ofl->ofl_osgot->os_shdr->sh_addr;
989bf994817SAli Bahrami 
990bf994817SAli Bahrami 		} else if (IS_GOT_PC(arsp->rel_rtype) &&
991bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
992bf994817SAli Bahrami 			value = (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) -
993bf994817SAli Bahrami 			    refaddr;
994bf994817SAli Bahrami 
995bf994817SAli Bahrami 		} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
996bf994817SAli Bahrami 		    (((flags & FLG_OF_RELOBJ) == 0) ||
997bf994817SAli Bahrami 		    (osp == sdp->sd_isc->is_osdesc))) {
998bf994817SAli Bahrami 			value -= refaddr;
999bf994817SAli Bahrami 
1000bf994817SAli Bahrami 		} else if (IS_TLS_INS(arsp->rel_rtype) &&
1001bf994817SAli Bahrami 		    IS_GOT_RELATIVE(arsp->rel_rtype) &&
1002bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
1003bf994817SAli Bahrami 			Gotndx	*gnp;
1004bf994817SAli Bahrami 
1005bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
1006bf994817SAli Bahrami 			assert(gnp);
1007bf994817SAli Bahrami 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
1008bf994817SAli Bahrami 			if (arsp->rel_rtype == R_386_TLS_IE) {
1009bf994817SAli Bahrami 				value += ofl->ofl_osgot->os_shdr->sh_addr;
1010bf994817SAli Bahrami 			}
10117c478bd9Sstevel@tonic-gate 
1012bf994817SAli Bahrami 		} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
1013bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
1014bf994817SAli Bahrami 			Gotndx *gnp;
10157c478bd9Sstevel@tonic-gate 
1016bf994817SAli Bahrami 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1017bf994817SAli Bahrami 			    GOT_REF_GENERIC, ofl, NULL);
1018bf994817SAli Bahrami 			assert(gnp);
1019bf994817SAli Bahrami 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
10207c478bd9Sstevel@tonic-gate 
1021bf994817SAli Bahrami 		} else if ((arsp->rel_flags & FLG_REL_STLS) &&
1022bf994817SAli Bahrami 		    ((flags & FLG_OF_RELOBJ) == 0)) {
1023bf994817SAli Bahrami 			Xword	tlsstatsize;
10247c478bd9Sstevel@tonic-gate 
1025bf994817SAli Bahrami 			/*
1026bf994817SAli Bahrami 			 * This is the LE TLS reference model.  Static
1027bf994817SAli Bahrami 			 * offset is hard-coded.
1028bf994817SAli Bahrami 			 */
1029bf994817SAli Bahrami 			tlsstatsize = S_ROUND(ofl->ofl_tlsphdr->p_memsz,
1030bf994817SAli Bahrami 			    M_TLSSTATALIGN);
1031bf994817SAli Bahrami 			value = tlsstatsize - value;
10327c478bd9Sstevel@tonic-gate 
1033bf994817SAli Bahrami 			/*
1034bf994817SAli Bahrami 			 * Since this code is fixed up, it assumes a
1035bf994817SAli Bahrami 			 * negative offset that can be added to the
1036bf994817SAli Bahrami 			 * thread pointer.
1037bf994817SAli Bahrami 			 */
1038bf994817SAli Bahrami 			if ((arsp->rel_rtype == R_386_TLS_LDO_32) ||
1039bf994817SAli Bahrami 			    (arsp->rel_rtype == R_386_TLS_LE))
1040bf994817SAli Bahrami 				value = -value;
1041bf994817SAli Bahrami 		}
10427c478bd9Sstevel@tonic-gate 
1043bf994817SAli Bahrami 		if (arsp->rel_isdesc->is_file)
1044bf994817SAli Bahrami 			ifl_name = arsp->rel_isdesc->is_file->ifl_name;
1045bf994817SAli Bahrami 		else
1046bf994817SAli Bahrami 			ifl_name = MSG_INTL(MSG_STR_NULL);
1047141040e8Srie 
1048bf994817SAli Bahrami 		/*
1049bf994817SAli Bahrami 		 * Make sure we have data to relocate.  Compiler and assembler
1050bf994817SAli Bahrami 		 * developers have been known to generate relocations against
1051bf994817SAli Bahrami 		 * invalid sections (normally .bss), so for their benefit give
1052bf994817SAli Bahrami 		 * them sufficient information to help analyze the problem.
1053bf994817SAli Bahrami 		 * End users should never see this.
1054bf994817SAli Bahrami 		 */
1055bf994817SAli Bahrami 		if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1056bf994817SAli Bahrami 			Conv_inv_buf_t	inv_buf;
10577c478bd9Sstevel@tonic-gate 
10581007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
1059bf994817SAli Bahrami 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
1060bf994817SAli Bahrami 			    ifl_name, ld_reloc_sym_name(arsp),
1061bf994817SAli Bahrami 			    EC_WORD(arsp->rel_isdesc->is_scnndx),
1062bf994817SAli Bahrami 			    arsp->rel_isdesc->is_name);
1063bf994817SAli Bahrami 			return (S_ERROR);
1064bf994817SAli Bahrami 		}
1065141040e8Srie 
1066bf994817SAli Bahrami 		/*
1067bf994817SAli Bahrami 		 * Get the address of the data item we need to modify.
1068bf994817SAli Bahrami 		 */
1069bf994817SAli Bahrami 		addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1070bf994817SAli Bahrami 		    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
1071141040e8Srie 
1072bf994817SAli Bahrami 		DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
1073bf994817SAli Bahrami 		    M_MACH, SHT_REL, arsp, EC_NATPTR(addr), value,
1074bf994817SAli Bahrami 		    ld_reloc_sym_name));
1075bf994817SAli Bahrami 		addr += (uintptr_t)osp->os_outdata->d_buf;
1076141040e8Srie 
1077bf994817SAli Bahrami 		if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1078bf994817SAli Bahrami 		    ofl->ofl_size) || (arsp->rel_roffset >
1079bf994817SAli Bahrami 		    osp->os_shdr->sh_size)) {
1080bf994817SAli Bahrami 			Conv_inv_buf_t	inv_buf;
1081bf994817SAli Bahrami 			int		class;
10827c478bd9Sstevel@tonic-gate 
1083bf994817SAli Bahrami 			if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1084bf994817SAli Bahrami 			    ofl->ofl_size)
1085bf994817SAli Bahrami 				class = ERR_FATAL;
10867c478bd9Sstevel@tonic-gate 			else
1087bf994817SAli Bahrami 				class = ERR_WARNING;
10887c478bd9Sstevel@tonic-gate 
10891007fd6fSAli Bahrami 			ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
1090bf994817SAli Bahrami 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
1091bf994817SAli Bahrami 			    ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
1092bf994817SAli Bahrami 			    arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
1093bf994817SAli Bahrami 			    EC_ADDR((uintptr_t)addr -
1094bf994817SAli Bahrami 			    (uintptr_t)ofl->ofl_nehdr));
10957c478bd9Sstevel@tonic-gate 
1096bf994817SAli Bahrami 			if (class == ERR_FATAL) {
1097bf994817SAli Bahrami 				return_code = S_ERROR;
1098bf994817SAli Bahrami 				continue;
10997c478bd9Sstevel@tonic-gate 			}
1100bf994817SAli Bahrami 		}
11017c478bd9Sstevel@tonic-gate 
1102bf994817SAli Bahrami 		/*
1103bf994817SAli Bahrami 		 * The relocation is additive.  Ignore the previous symbol
1104bf994817SAli Bahrami 		 * value if this local partial symbol is expanded.
1105bf994817SAli Bahrami 		 */
1106bf994817SAli Bahrami 		if (moved)
1107bf994817SAli Bahrami 			value -= *addr;
11087c478bd9Sstevel@tonic-gate 
1109bf994817SAli Bahrami 		/*
1110bf994817SAli Bahrami 		 * If we have a replacement value for the relocation
1111bf994817SAli Bahrami 		 * target, put it in place now.
1112bf994817SAli Bahrami 		 */
1113bf994817SAli Bahrami 		if (arsp->rel_flags & FLG_REL_NADDEND) {
1114bf994817SAli Bahrami 			Xword addend = arsp->rel_raddend;
1115cce0e03bSab 
1116bf994817SAli Bahrami 			if (ld_reloc_targval_set(ofl, arsp, addr, addend) == 0)
1117bf994817SAli Bahrami 				return (S_ERROR);
1118bf994817SAli Bahrami 		}
1119cce0e03bSab 
1120bf994817SAli Bahrami 		/*
1121bf994817SAli Bahrami 		 * If '-z noreloc' is specified - skip the do_reloc_ld stage.
1122bf994817SAli Bahrami 		 */
1123bf994817SAli Bahrami 		if (OFL_DO_RELOC(ofl)) {
1124bf994817SAli Bahrami 			if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
1125bf994817SAli Bahrami 			    ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
11261007fd6fSAli Bahrami 			    ofl->ofl_lml) == 0) {
11271007fd6fSAli Bahrami 				ofl->ofl_flags |= FLG_OF_FATAL;
1128bf994817SAli Bahrami 				return_code = S_ERROR;
11291007fd6fSAli Bahrami 			}
11307c478bd9Sstevel@tonic-gate 		}
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 	return (return_code);
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate 
1135141040e8Srie /*
1136141040e8Srie  * Add an output relocation record.
1137141040e8Srie  */
1138ba2be530Sab static uintptr_t
ld_add_outrel(Word flags,Rel_desc * rsp,Ofl_desc * ofl)11395aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
11407c478bd9Sstevel@tonic-gate {
1141141040e8Srie 	Rel_desc	*orsp;
1142141040e8Srie 	Sym_desc	*sdp = rsp->rel_sym;
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	/*
11457c478bd9Sstevel@tonic-gate 	 * Static executables *do not* want any relocations against them.
11467c478bd9Sstevel@tonic-gate 	 * Since our engine still creates relocations against a WEAK UNDEFINED
11477c478bd9Sstevel@tonic-gate 	 * symbol in a static executable, it's best to disable them here
11487c478bd9Sstevel@tonic-gate 	 * instead of through out the relocation code.
11497c478bd9Sstevel@tonic-gate 	 */
1150635216b6SRod Evans 	if (OFL_IS_STATIC_EXEC(ofl))
11517c478bd9Sstevel@tonic-gate 		return (1);
11527c478bd9Sstevel@tonic-gate 
11530bc0887eSRichard Lowe 	/*
11540bc0887eSRichard Lowe 	 * If the symbol will be reduced, we can't leave outstanding
11550bc0887eSRichard Lowe 	 * relocations against it, as nothing will ever be able to satisfy them
11560bc0887eSRichard Lowe 	 * (and the symbol won't be in .dynsym
11570bc0887eSRichard Lowe 	 */
11580bc0887eSRichard Lowe 	if ((sdp != NULL) &&
11590bc0887eSRichard Lowe 	    (sdp->sd_sym->st_shndx == SHN_UNDEF) &&
11600bc0887eSRichard Lowe 	    (rsp->rel_rtype != M_R_NONE) &&
11610bc0887eSRichard Lowe 	    (rsp->rel_rtype != M_R_RELATIVE)) {
11620bc0887eSRichard Lowe 		if (ld_sym_reducable(ofl, sdp))
11630bc0887eSRichard Lowe 			return (1);
11640bc0887eSRichard Lowe 	}
11650bc0887eSRichard Lowe 
11667c478bd9Sstevel@tonic-gate 	/*
11677c478bd9Sstevel@tonic-gate 	 * If we are adding a output relocation against a section
11687c478bd9Sstevel@tonic-gate 	 * symbol (non-RELATIVE) then mark that section.  These sections
11697c478bd9Sstevel@tonic-gate 	 * will be added to the .dynsym symbol table.
11707c478bd9Sstevel@tonic-gate 	 */
11717c478bd9Sstevel@tonic-gate 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
11727c478bd9Sstevel@tonic-gate 	    ((flags & FLG_REL_SCNNDX) ||
11737c478bd9Sstevel@tonic-gate 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 		/*
11767c478bd9Sstevel@tonic-gate 		 * If this is a COMMON symbol - no output section
11777c478bd9Sstevel@tonic-gate 		 * exists yet - (it's created as part of sym_validate()).
11787c478bd9Sstevel@tonic-gate 		 * So - we mark here that when it's created it should
11797c478bd9Sstevel@tonic-gate 		 * be tagged with the FLG_OS_OUTREL flag.
11807c478bd9Sstevel@tonic-gate 		 */
11817c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
11820bc07c75Srie 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
11837c478bd9Sstevel@tonic-gate 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
11847c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
11857c478bd9Sstevel@tonic-gate 			else
11867c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1187141040e8Srie 		} else {
118808278a5eSRod Evans 			Os_desc *osp;
118908278a5eSRod Evans 			Is_desc *isp = sdp->sd_isc;
11907c478bd9Sstevel@tonic-gate 
119108278a5eSRod Evans 			if (isp && ((osp = isp->is_osdesc) != NULL) &&
119208278a5eSRod Evans 			    ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
11937c478bd9Sstevel@tonic-gate 				ofl->ofl_dynshdrcnt++;
11947c478bd9Sstevel@tonic-gate 				osp->os_flags |= FLG_OS_OUTREL;
11957c478bd9Sstevel@tonic-gate 			}
11967c478bd9Sstevel@tonic-gate 		}
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate 
1199bf994817SAli Bahrami 	/* Enter it into the output relocation cache */
1200bf994817SAli Bahrami 	if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
1201bf994817SAli Bahrami 		return (S_ERROR);
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	if (flags & FLG_REL_GOT)
12047c478bd9Sstevel@tonic-gate 		ofl->ofl_relocgotsz += (Xword)sizeof (Rel);
12057c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_PLT)
12067c478bd9Sstevel@tonic-gate 		ofl->ofl_relocpltsz += (Xword)sizeof (Rel);
12077c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_BSS)
12087c478bd9Sstevel@tonic-gate 		ofl->ofl_relocbsssz += (Xword)sizeof (Rel);
12097c478bd9Sstevel@tonic-gate 	else if (flags & FLG_REL_NOINFO)
12107c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelsz += (Xword)sizeof (Rel);
12117c478bd9Sstevel@tonic-gate 	else
1212bf994817SAli Bahrami 		RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rel);
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	if (orsp->rel_rtype == M_R_RELATIVE)
12157c478bd9Sstevel@tonic-gate 		ofl->ofl_relocrelcnt++;
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	/*
12187c478bd9Sstevel@tonic-gate 	 * We don't perform sorting on PLT relocations because
12197c478bd9Sstevel@tonic-gate 	 * they have already been assigned a PLT index and if we
12207c478bd9Sstevel@tonic-gate 	 * were to sort them we would have to re-assign the plt indexes.
12217c478bd9Sstevel@tonic-gate 	 */
12227c478bd9Sstevel@tonic-gate 	if (!(flags & FLG_REL_PLT))
12237c478bd9Sstevel@tonic-gate 		ofl->ofl_reloccnt++;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	/*
1226141040e8Srie 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
12277c478bd9Sstevel@tonic-gate 	 */
1228141040e8Srie 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
12297c478bd9Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_BLDGOT;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	/*
12327c478bd9Sstevel@tonic-gate 	 * Identify and possibly warn of a displacement relocation.
12337c478bd9Sstevel@tonic-gate 	 */
12347c478bd9Sstevel@tonic-gate 	if (orsp->rel_flags & FLG_REL_DISP) {
12357c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
12385aefb655Srie 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
12397c478bd9Sstevel@tonic-gate 	}
12405aefb655Srie 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL,
12415aefb655Srie 	    M_MACH, orsp));
12427c478bd9Sstevel@tonic-gate 	return (1);
12437c478bd9Sstevel@tonic-gate }
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate /*
12467c478bd9Sstevel@tonic-gate  * process relocation for a LOCAL symbol
12477c478bd9Sstevel@tonic-gate  */
1248ba2be530Sab static uintptr_t
ld_reloc_local(Rel_desc * rsp,Ofl_desc * ofl)12495aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
12507c478bd9Sstevel@tonic-gate {
12511d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
12527c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
12530bc07c75Srie 	Word		shndx = sdp->sd_sym->st_shndx;
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	/*
12567c478bd9Sstevel@tonic-gate 	 * if ((shared object) and (not pc relative relocation) and
12577c478bd9Sstevel@tonic-gate 	 *    (not against ABS symbol))
12587c478bd9Sstevel@tonic-gate 	 * then
12597c478bd9Sstevel@tonic-gate 	 *	build R_386_RELATIVE
12607c478bd9Sstevel@tonic-gate 	 * fi
12617c478bd9Sstevel@tonic-gate 	 */
12627c478bd9Sstevel@tonic-gate 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
12632926dd2eSrie 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
12647c478bd9Sstevel@tonic-gate 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
12657c478bd9Sstevel@tonic-gate 	    !(rsp->rel_isdesc != NULL &&
12667c478bd9Sstevel@tonic-gate 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
12677c478bd9Sstevel@tonic-gate 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
12687c478bd9Sstevel@tonic-gate 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
12697c478bd9Sstevel@tonic-gate 		Word	ortype = rsp->rel_rtype;
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = R_386_RELATIVE;
1272fb8f92baSToomas Soome 		if (ld_add_outrel(0, rsp, ofl) == S_ERROR)
12737c478bd9Sstevel@tonic-gate 			return (S_ERROR);
12747c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = ortype;
12757c478bd9Sstevel@tonic-gate 	}
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	/*
12787c478bd9Sstevel@tonic-gate 	 * If the relocation is against a 'non-allocatable' section
12797c478bd9Sstevel@tonic-gate 	 * and we can not resolve it now - then give a warning
12807c478bd9Sstevel@tonic-gate 	 * message.
12817c478bd9Sstevel@tonic-gate 	 *
12827c478bd9Sstevel@tonic-gate 	 * We can not resolve the symbol if either:
12837c478bd9Sstevel@tonic-gate 	 *	a) it's undefined
12847c478bd9Sstevel@tonic-gate 	 *	b) it's defined in a shared library and a
12857c478bd9Sstevel@tonic-gate 	 *	   COPY relocation hasn't moved it to the executable
12867c478bd9Sstevel@tonic-gate 	 *
12877c478bd9Sstevel@tonic-gate 	 * Note: because we process all of the relocations against the
12887c478bd9Sstevel@tonic-gate 	 *	text segment before any others - we know whether
12897c478bd9Sstevel@tonic-gate 	 *	or not a copy relocation will be generated before
12907c478bd9Sstevel@tonic-gate 	 *	we get here (see reloc_init()->reloc_segments()).
12917c478bd9Sstevel@tonic-gate 	 */
12927c478bd9Sstevel@tonic-gate 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
12937c478bd9Sstevel@tonic-gate 	    ((shndx == SHN_UNDEF) ||
12947c478bd9Sstevel@tonic-gate 	    ((sdp->sd_ref == REF_DYN_NEED) &&
12957c478bd9Sstevel@tonic-gate 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1296bf994817SAli Bahrami 		Conv_inv_buf_t	inv_buf;
1297bf994817SAli Bahrami 		Os_desc		*osp = RELAUX_GET_OSDESC(rsp);
1298de777a60Sab 
12997c478bd9Sstevel@tonic-gate 		/*
13007c478bd9Sstevel@tonic-gate 		 * If the relocation is against a SHT_SUNW_ANNOTATE
13017c478bd9Sstevel@tonic-gate 		 * section - then silently ignore that the relocation
13027c478bd9Sstevel@tonic-gate 		 * can not be resolved.
13037c478bd9Sstevel@tonic-gate 		 */
1304bf994817SAli Bahrami 		if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
13057c478bd9Sstevel@tonic-gate 			return (0);
13061007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1307de777a60Sab 		    conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf),
13087c478bd9Sstevel@tonic-gate 		    rsp->rel_isdesc->is_file->ifl_name,
1309bf994817SAli Bahrami 		    ld_reloc_sym_name(rsp), osp->os_name);
13107c478bd9Sstevel@tonic-gate 		return (1);
13117c478bd9Sstevel@tonic-gate 	}
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	/*
13147c478bd9Sstevel@tonic-gate 	 * Perform relocation.
13157c478bd9Sstevel@tonic-gate 	 */
1316fb8f92baSToomas Soome 	return (ld_add_actrel(0, rsp, ofl));
13177c478bd9Sstevel@tonic-gate }
13187c478bd9Sstevel@tonic-gate 
1319ba2be530Sab static uintptr_t
ld_reloc_TLS(Boolean local,Rel_desc * rsp,Ofl_desc * ofl)13205aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
13217c478bd9Sstevel@tonic-gate {
13227c478bd9Sstevel@tonic-gate 	Word		rtype = rsp->rel_rtype;
13237c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp = rsp->rel_sym;
13241d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
13257c478bd9Sstevel@tonic-gate 	Gotndx		*gnp;
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	/*
1328d326b23bSrie 	 * If we're building an executable - use either the IE or LE access
1329d326b23bSrie 	 * model.  If we're building a shared object process any IE model.
13307c478bd9Sstevel@tonic-gate 	 */
1331d326b23bSrie 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
13327c478bd9Sstevel@tonic-gate 		/*
1333d326b23bSrie 		 * Set the DF_STATIC_TLS flag.
13347c478bd9Sstevel@tonic-gate 		 */
13357c478bd9Sstevel@tonic-gate 		ofl->ofl_dtflags |= DF_STATIC_TLS;
13367c478bd9Sstevel@tonic-gate 
1337d326b23bSrie 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
13387c478bd9Sstevel@tonic-gate 			/*
1339d326b23bSrie 			 * Assign a GOT entry for static TLS references.
13407c478bd9Sstevel@tonic-gate 			 */
134157ef7aa9SRod Evans 			if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
134257ef7aa9SRod Evans 			    GOT_REF_TLSIE, ofl, NULL)) == NULL) {
13437c478bd9Sstevel@tonic-gate 
1344d326b23bSrie 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1345d326b23bSrie 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1346fb8f92baSToomas Soome 				    rtype, R_386_TLS_TPOFF, 0) == S_ERROR)
1347d326b23bSrie 					return (S_ERROR);
1348d326b23bSrie 			}
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 			/*
1351d326b23bSrie 			 * IE access model.
13527c478bd9Sstevel@tonic-gate 			 */
1353d326b23bSrie 			if (IS_TLS_IE(rtype)) {
1354d326b23bSrie 				if (ld_add_actrel(FLG_REL_STLS,
13557c478bd9Sstevel@tonic-gate 				    rsp, ofl) == S_ERROR)
13567c478bd9Sstevel@tonic-gate 					return (S_ERROR);
1357d326b23bSrie 
1358d326b23bSrie 				/*
1359d326b23bSrie 				 * A non-pic shared object needs to adjust the
1360d326b23bSrie 				 * active relocation (indntpoff).
1361d326b23bSrie 				 */
1362d326b23bSrie 				if (((flags & FLG_OF_EXEC) == 0) &&
1363d326b23bSrie 				    (rtype == R_386_TLS_IE)) {
1364d326b23bSrie 					rsp->rel_rtype = R_386_RELATIVE;
1365fb8f92baSToomas Soome 					return (ld_add_outrel(0, rsp, ofl));
1366d326b23bSrie 				}
1367d326b23bSrie 				return (1);
13687c478bd9Sstevel@tonic-gate 			}
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 			/*
1371d326b23bSrie 			 * Fixups are required for other executable models.
13727c478bd9Sstevel@tonic-gate 			 */
13735aefb655Srie 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13747c478bd9Sstevel@tonic-gate 			    rsp, ofl));
13757c478bd9Sstevel@tonic-gate 		}
1376d326b23bSrie 
13777c478bd9Sstevel@tonic-gate 		/*
1378d326b23bSrie 		 * LE access model.
13797c478bd9Sstevel@tonic-gate 		 */
13807c478bd9Sstevel@tonic-gate 		if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32))
13815aefb655Srie 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
13827c478bd9Sstevel@tonic-gate 
13835aefb655Srie 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
13845aefb655Srie 		    rsp, ofl));
13857c478bd9Sstevel@tonic-gate 	}
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	/*
1388d326b23bSrie 	 * Building a shared object.
1389d326b23bSrie 	 *
1390d326b23bSrie 	 * Assign a GOT entry for a dynamic TLS reference.
13917c478bd9Sstevel@tonic-gate 	 */
139257ef7aa9SRod Evans 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
139357ef7aa9SRod Evans 	    GOT_REF_TLSLD, ofl, NULL)) == NULL)) {
1394d326b23bSrie 
1395d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1396fb8f92baSToomas Soome 		    FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, 0) == S_ERROR)
13977c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1398d326b23bSrie 
139957ef7aa9SRod Evans 	} else if (IS_TLS_GD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
140057ef7aa9SRod Evans 	    GOT_REF_TLSGD, ofl, NULL)) == NULL)) {
1401d326b23bSrie 
1402d326b23bSrie 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1403d326b23bSrie 		    FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32,
1404d326b23bSrie 		    R_386_TLS_DTPOFF32) == S_ERROR)
14057c478bd9Sstevel@tonic-gate 			return (S_ERROR);
14067c478bd9Sstevel@tonic-gate 	}
1407d326b23bSrie 
14087c478bd9Sstevel@tonic-gate 	/*
14097c478bd9Sstevel@tonic-gate 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1410d326b23bSrie 	 * cause a call to __tls_get_addr().  Convert this relocation to that
1411d326b23bSrie 	 * symbol now, and prepare for the PLT magic.
14127c478bd9Sstevel@tonic-gate 	 */
14137c478bd9Sstevel@tonic-gate 	if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) {
1414f5a18a30Srie 		Sym_desc	*tlsgetsym;
14157c478bd9Sstevel@tonic-gate 
14165aefb655Srie 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU),
1417f5a18a30Srie 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
14187c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1419d326b23bSrie 
14207c478bd9Sstevel@tonic-gate 		rsp->rel_sym = tlsgetsym;
14217c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = R_386_PLT32;
1422d326b23bSrie 
14235aefb655Srie 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
14247c478bd9Sstevel@tonic-gate 			return (S_ERROR);
1425d326b23bSrie 
14267c478bd9Sstevel@tonic-gate 		rsp->rel_sym = sdp;
14277c478bd9Sstevel@tonic-gate 		rsp->rel_rtype = rtype;
14287c478bd9Sstevel@tonic-gate 		return (1);
14297c478bd9Sstevel@tonic-gate 	}
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	if (IS_TLS_LD(rtype))
14325aefb655Srie 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
14337c478bd9Sstevel@tonic-gate 
14345aefb655Srie 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
14357c478bd9Sstevel@tonic-gate }
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate /* ARGSUSED4 */
1438ba2be530Sab static uintptr_t
ld_assign_got_ndx(Alist ** alpp,Gotndx * pgnp,Gotref gref,Ofl_desc * ofl,Rel_desc * rsp,Sym_desc * sdp)143957ef7aa9SRod Evans ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
144057ef7aa9SRod Evans     Rel_desc *rsp, Sym_desc *sdp)
14417c478bd9Sstevel@tonic-gate {
144257ef7aa9SRod Evans 	Gotndx	gn, *gnp;
14437c478bd9Sstevel@tonic-gate 	uint_t	gotents;
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	if (pgnp)
14467c478bd9Sstevel@tonic-gate 		return (1);
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
14497c478bd9Sstevel@tonic-gate 		gotents = 2;
14507c478bd9Sstevel@tonic-gate 	else
14517c478bd9Sstevel@tonic-gate 		gotents = 1;
14527c478bd9Sstevel@tonic-gate 
145357ef7aa9SRod Evans 	gn.gn_addend = 0;
145457ef7aa9SRod Evans 	gn.gn_gotndx = ofl->ofl_gotcnt;
145557ef7aa9SRod Evans 	gn.gn_gotref = gref;
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	ofl->ofl_gotcnt += gotents;
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	if (gref == GOT_REF_TLSLD) {
146057ef7aa9SRod Evans 		if (ofl->ofl_tlsldgotndx == NULL) {
146157ef7aa9SRod Evans 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
146257ef7aa9SRod Evans 				return (S_ERROR);
146357ef7aa9SRod Evans 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
146457ef7aa9SRod Evans 			ofl->ofl_tlsldgotndx = gnp;
146557ef7aa9SRod Evans 		}
14667c478bd9Sstevel@tonic-gate 		return (1);
14677c478bd9Sstevel@tonic-gate 	}
14687c478bd9Sstevel@tonic-gate 
146957ef7aa9SRod Evans 	/*
147057ef7aa9SRod Evans 	 * GOT indexes are maintained on an Alist, where there is typically
147157ef7aa9SRod Evans 	 * only one index.  The usage of this list is to scan the list to find
147257ef7aa9SRod Evans 	 * an index, and then apply that index immediately to a relocation.
147357ef7aa9SRod Evans 	 * Thus there are no external references to these GOT index structures
147457ef7aa9SRod Evans 	 * that can be compromised by the Alist being reallocated.
147557ef7aa9SRod Evans 	 */
147657ef7aa9SRod Evans 	if (alist_append(alpp, &gn, sizeof (Gotndx), AL_CNT_SDP_GOT) == NULL)
14777c478bd9Sstevel@tonic-gate 		return (S_ERROR);
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	return (1);
14807c478bd9Sstevel@tonic-gate }
14817c478bd9Sstevel@tonic-gate 
1482ba2be530Sab static void
ld_assign_plt_ndx(Sym_desc * sdp,Ofl_desc * ofl)14835aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
14847c478bd9Sstevel@tonic-gate {
14857c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
14867c478bd9Sstevel@tonic-gate 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1487141040e8Srie 	ofl->ofl_flags |= FLG_OF_BLDGOT;
14887c478bd9Sstevel@tonic-gate }
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate /*
14917c478bd9Sstevel@tonic-gate  * Initializes .got[0] with the _DYNAMIC symbol value.
14927c478bd9Sstevel@tonic-gate  */
1493ba2be530Sab static uintptr_t
ld_fillin_gotplt(Ofl_desc * ofl)1494d326b23bSrie ld_fillin_gotplt(Ofl_desc *ofl)
14957c478bd9Sstevel@tonic-gate {
14961d9df23bSab 	ofl_flag_t	flags = ofl->ofl_flags;
14971d9df23bSab 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1498d326b23bSrie 
14997c478bd9Sstevel@tonic-gate 	if (ofl->ofl_osgot) {
1500d326b23bSrie 		Sym_desc	*sdp;
15017c478bd9Sstevel@tonic-gate 
15025aefb655Srie 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
1503635216b6SRod Evans 		    SYM_NOHASH, NULL, ofl)) != NULL) {
1504d326b23bSrie 			uchar_t	*genptr;
1505d326b23bSrie 
1506d326b23bSrie 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
15077c478bd9Sstevel@tonic-gate 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
15087c478bd9Sstevel@tonic-gate 			/* LINTED */
15097c478bd9Sstevel@tonic-gate 			*(Word *)genptr = (Word)sdp->sd_sym->st_value;
1510ba2be530Sab 			if (bswap)
1511ba2be530Sab 				/* LINTED */
1512ba2be530Sab 				*(Word *)genptr =
1513ba2be530Sab 				    /* LINTED */
1514ba2be530Sab 				    ld_bswap_Word(*(Word *)genptr);
15157c478bd9Sstevel@tonic-gate 		}
15167c478bd9Sstevel@tonic-gate 	}
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	/*
15197c478bd9Sstevel@tonic-gate 	 * Fill in the reserved slot in the procedure linkage table the first
15207c478bd9Sstevel@tonic-gate 	 * entry is:
1521*fec04708SRichard Lowe 	 *  if (building executable) {
15227c478bd9Sstevel@tonic-gate 	 *	PUSHL	got[1]		    # the address of the link map entry
15237c478bd9Sstevel@tonic-gate 	 *	JMP *	got[2]		    # the address of rtbinder
15247c478bd9Sstevel@tonic-gate 	 *  } else {
15257c478bd9Sstevel@tonic-gate 	 *	PUSHL	got[1]@GOT(%ebx)    # the address of the link map entry
15267c478bd9Sstevel@tonic-gate 	 *	JMP *	got[2]@GOT(%ebx)    # the address of rtbinder
15277c478bd9Sstevel@tonic-gate 	 *  }
15287c478bd9Sstevel@tonic-gate 	 */
1529d326b23bSrie 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1530b3fbe5e6Sseizo 		uchar_t *pltent;
15317c478bd9Sstevel@tonic-gate 
1532b3fbe5e6Sseizo 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
1533d326b23bSrie 		if (!(flags & FLG_OF_SHAROBJ)) {
15347c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15357c478bd9Sstevel@tonic-gate 			pltent[1] = M_PUSHL_DISP;
15367c478bd9Sstevel@tonic-gate 			pltent += 2;
15377c478bd9Sstevel@tonic-gate 			/* LINTED */
15387c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
1539de777a60Sab 			    sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE);
1540ba2be530Sab 			if (bswap)
1541ba2be530Sab 				/* LINTED */
1542ba2be530Sab 				*(Word *)pltent =
1543ba2be530Sab 				    /* LINTED */
1544ba2be530Sab 				    ld_bswap_Word(*(Word *)pltent);
15457c478bd9Sstevel@tonic-gate 			pltent += 4;
15467c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15477c478bd9Sstevel@tonic-gate 			pltent[1] = M_JMP_DISP_IND;
15487c478bd9Sstevel@tonic-gate 			pltent += 2;
15497c478bd9Sstevel@tonic-gate 			/* LINTED */
15507c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
1551de777a60Sab 			    sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE);
1552ba2be530Sab 			if (bswap)
1553ba2be530Sab 				/* LINTED */
1554ba2be530Sab 				*(Word *)pltent =
1555ba2be530Sab 				    /* LINTED */
1556ba2be530Sab 				    ld_bswap_Word(*(Word *)pltent);
15577c478bd9Sstevel@tonic-gate 		} else {
15587c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15597c478bd9Sstevel@tonic-gate 			pltent[1] = M_PUSHL_REG_DISP;
15607c478bd9Sstevel@tonic-gate 			pltent += 2;
15617c478bd9Sstevel@tonic-gate 			/* LINTED */
15627c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(M_GOT_XLINKMAP *
1563de777a60Sab 			    M_GOT_ENTSIZE);
1564ba2be530Sab 			if (bswap)
1565ba2be530Sab 				/* LINTED */
1566ba2be530Sab 				*(Word *)pltent =
1567ba2be530Sab 				    /* LINTED */
1568ba2be530Sab 				    ld_bswap_Word(*(Word *)pltent);
15697c478bd9Sstevel@tonic-gate 			pltent += 4;
15707c478bd9Sstevel@tonic-gate 			pltent[0] = M_SPECIAL_INST;
15717c478bd9Sstevel@tonic-gate 			pltent[1] = M_JMP_REG_DISP_IND;
15727c478bd9Sstevel@tonic-gate 			pltent += 2;
15737c478bd9Sstevel@tonic-gate 			/* LINTED */
15747c478bd9Sstevel@tonic-gate 			*(Word *)pltent = (Word)(M_GOT_XRTLD *
1575de777a60Sab 			    M_GOT_ENTSIZE);
1576ba2be530Sab 			if (bswap)
1577ba2be530Sab 				/* LINTED */
1578ba2be530Sab 				*(Word *)pltent =
1579ba2be530Sab 				    /* LINTED */
1580ba2be530Sab 				    ld_bswap_Word(*(Word *)pltent);
15817c478bd9Sstevel@tonic-gate 		}
15827c478bd9Sstevel@tonic-gate 	}
15837c478bd9Sstevel@tonic-gate 	return (1);
15847c478bd9Sstevel@tonic-gate }
1585ba2be530Sab 
1586ba2be530Sab 
1587ba2be530Sab 
1588ba2be530Sab /*
1589ba2be530Sab  * Template for generating "void (*)(void)" function
1590ba2be530Sab  */
1591ba2be530Sab static const uchar_t nullfunc_tmpl[] = {	/* IA32 */
1592ba2be530Sab /* 0x00 */	0xc3				/* ret */
1593ba2be530Sab };
1594ba2be530Sab 
1595ba2be530Sab 
1596ba2be530Sab 
15973c573fccSAli Bahrami /*
15983c573fccSAli Bahrami  * Function used to provide fill padding in SHF_EXECINSTR sections
15993c573fccSAli Bahrami  *
16003c573fccSAli Bahrami  * entry:
16013c573fccSAli Bahrami  *
16023c573fccSAli Bahrami  *	base - base address of section being filled
16033c573fccSAli Bahrami  *	offset - starting offset for fill within memory referenced by base
16043c573fccSAli Bahrami  *	cnt - # bytes to be filled
16053c573fccSAli Bahrami  *
16063c573fccSAli Bahrami  * exit:
16073c573fccSAli Bahrami  *	The fill has been completed.
16083c573fccSAli Bahrami  */
16093c573fccSAli Bahrami static void
execfill(void * base,off_t off,size_t cnt)16103c573fccSAli Bahrami execfill(void *base, off_t off, size_t cnt)
16113c573fccSAli Bahrami {
16123c573fccSAli Bahrami 	/*
16133c573fccSAli Bahrami 	 * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds.
16143c573fccSAli Bahrami 	 * There are no alignment constraints.
16153c573fccSAli Bahrami 	 */
16163c573fccSAli Bahrami 	(void) memset(off + (char *)base, 0x90, cnt);
16173c573fccSAli Bahrami }
16183c573fccSAli Bahrami 
16193c573fccSAli Bahrami 
1620ba2be530Sab /*
1621ba2be530Sab  * Return the ld_targ definition for this target.
1622ba2be530Sab  */
1623ba2be530Sab const Target *
ld_targ_init_x86(void)1624ba2be530Sab ld_targ_init_x86(void)
1625ba2be530Sab {
1626ba2be530Sab 	static const Target _ld_targ = {
1627ba2be530Sab 		{			/* Target_mach */
1628ba2be530Sab 			M_MACH,			/* m_mach */
1629ba2be530Sab 			M_MACHPLUS,		/* m_machplus */
1630ba2be530Sab 			M_FLAGSPLUS,		/* m_flagsplus */
1631ba2be530Sab 			M_CLASS,		/* m_class */
1632ba2be530Sab 			M_DATA,			/* m_data */
1633ba2be530Sab 
1634ba2be530Sab 			M_SEGM_ALIGN,		/* m_segm_align */
1635ba2be530Sab 			M_SEGM_ORIGIN,		/* m_segm_origin */
1636bb3b4f6cSRod Evans 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
1637ba2be530Sab 			M_DATASEG_PERM,		/* m_dataseg_perm */
163869112eddSAli Bahrami 			M_STACK_PERM,		/* m_stack_perm */
1639ba2be530Sab 			M_WORD_ALIGN,		/* m_word_align */
1640ba2be530Sab 			MSG_ORIG(MSG_PTH_RTLD),	/* m_def_interp */
1641ba2be530Sab 
1642ba2be530Sab 			/* Relocation type codes */
1643ba2be530Sab 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
1644ba2be530Sab 			M_R_COPY,		/* m_r_copy */
1645ba2be530Sab 			M_R_GLOB_DAT,		/* m_r_glob_dat */
1646ba2be530Sab 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
1647ba2be530Sab 			M_R_NUM,		/* m_r_num */
1648ba2be530Sab 			M_R_NONE,		/* m_r_none */
1649ba2be530Sab 			M_R_RELATIVE,		/* m_r_relative */
1650ba2be530Sab 			M_R_REGISTER,		/* m_r_register */
1651ba2be530Sab 
1652ba2be530Sab 			/* Relocation related constants */
1653ba2be530Sab 			M_REL_DT_COUNT,		/* m_rel_dt_count */
1654ba2be530Sab 			M_REL_DT_ENT,		/* m_rel_dt_ent */
1655ba2be530Sab 			M_REL_DT_SIZE,		/* m_rel_dt_size */
1656ba2be530Sab 			M_REL_DT_TYPE,		/* m_rel_dt_type */
1657ba2be530Sab 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
1658ba2be530Sab 
1659ba2be530Sab 			/* GOT related constants */
1660ba2be530Sab 			M_GOT_ENTSIZE,		/* m_got_entsize */
1661ba2be530Sab 			M_GOT_XNumber,		/* m_got_xnumber */
1662ba2be530Sab 
1663ba2be530Sab 			/* PLT related constants */
1664ba2be530Sab 			M_PLT_ALIGN,		/* m_plt_align */
1665ba2be530Sab 			M_PLT_ENTSIZE,		/* m_plt_entsize */
1666ba2be530Sab 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
1667ba2be530Sab 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
1668ba2be530Sab 
16697e16fca0SAli Bahrami 			/* Section type of .eh_frame/.eh_frame_hdr sections */
16707e16fca0SAli Bahrami 			SHT_PROGBITS,		/* m_sht_unwind */
16717e16fca0SAli Bahrami 
1672ba2be530Sab 			M_DT_REGISTER,		/* m_dt_register */
1673ba2be530Sab 		},
1674ba2be530Sab 		{			/* Target_machid */
1675ba2be530Sab 			M_ID_ARRAY,		/* id_array */
1676ba2be530Sab 			M_ID_BSS,		/* id_bss */
1677ba2be530Sab 			M_ID_CAP,		/* id_cap */
167808278a5eSRod Evans 			M_ID_CAPINFO,		/* id_capinfo */
167908278a5eSRod Evans 			M_ID_CAPCHAIN,		/* id_capchain */
1680ba2be530Sab 			M_ID_DATA,		/* id_data */
1681ba2be530Sab 			M_ID_DYNAMIC,		/* id_dynamic */
1682ba2be530Sab 			M_ID_DYNSORT,		/* id_dynsort */
1683ba2be530Sab 			M_ID_DYNSTR,		/* id_dynstr */
1684ba2be530Sab 			M_ID_DYNSYM,		/* id_dynsym */
1685ba2be530Sab 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
1686ba2be530Sab 			M_ID_GOT,		/* id_got */
1687ba2be530Sab 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
1688ba2be530Sab 			M_ID_HASH,		/* id_hash */
1689ba2be530Sab 			M_ID_INTERP,		/* id_interp */
1690ba2be530Sab 			M_ID_LBSS,		/* id_lbss */
1691ba2be530Sab 			M_ID_LDYNSYM,		/* id_ldynsym */
1692ba2be530Sab 			M_ID_NOTE,		/* id_note */
1693ba2be530Sab 			M_ID_NULL,		/* id_null */
1694ba2be530Sab 			M_ID_PLT,		/* id_plt */
1695ba2be530Sab 			M_ID_REL,		/* id_rel */
1696ba2be530Sab 			M_ID_STRTAB,		/* id_strtab */
1697ba2be530Sab 			M_ID_SYMINFO,		/* id_syminfo */
1698ba2be530Sab 			M_ID_SYMTAB,		/* id_symtab */
1699ba2be530Sab 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
1700ba2be530Sab 			M_ID_TEXT,		/* id_text */
1701ba2be530Sab 			M_ID_TLS,		/* id_tls */
1702ba2be530Sab 			M_ID_TLSBSS,		/* id_tlsbss */
1703ba2be530Sab 			M_ID_UNKNOWN,		/* id_unknown */
1704ba2be530Sab 			M_ID_UNWIND,		/* id_unwind */
17057e16fca0SAli Bahrami 			M_ID_UNWINDHDR,		/* id_unwindhdr */
1706ba2be530Sab 			M_ID_USER,		/* id_user */
1707ba2be530Sab 			M_ID_VERSION,		/* id_version */
1708ba2be530Sab 		},
1709ba2be530Sab 		{			/* Target_nullfunc */
1710ba2be530Sab 			nullfunc_tmpl,		/* nf_template */
1711ba2be530Sab 			sizeof (nullfunc_tmpl),	/* nf_size */
1712ba2be530Sab 		},
17133c573fccSAli Bahrami 		{			/* Target_fillfunc */
17143c573fccSAli Bahrami 			execfill		/* ff_execfill */
17153c573fccSAli Bahrami 		},
1716ba2be530Sab 		{			/* Target_machrel */
1717ba2be530Sab 			reloc_table,
1718ba2be530Sab 
1719ba2be530Sab 			ld_init_rel,		/* mr_init_rel */
1720ba2be530Sab 			ld_mach_eflags,		/* mr_mach_eflags */
1721ba2be530Sab 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
1722ba2be530Sab 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1723ba2be530Sab 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
1724ba2be530Sab 			ld_perform_outreloc,	/* mr_perform_outreloc */
1725ba2be530Sab 			ld_do_activerelocs,	/* mr_do_activerelocs */
1726ba2be530Sab 			ld_add_outrel,		/* mr_add_outrel */
1727ba2be530Sab 			NULL,			/* mr_reloc_register */
1728ba2be530Sab 			ld_reloc_local,		/* mr_reloc_local */
1729ba2be530Sab 			NULL,			/* mr_reloc_GOTOP */
1730ba2be530Sab 			ld_reloc_TLS,		/* mr_reloc_TLS */
1731ba2be530Sab 			NULL,			/* mr_assign_got */
173257ef7aa9SRod Evans 			ld_find_got_ndx,	/* mr_find_got_ndx */
1733ba2be530Sab 			ld_calc_got_offset,	/* mr_calc_got_offset */
1734ba2be530Sab 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
1735ba2be530Sab 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
1736ba2be530Sab 			NULL,			/* mr_allocate_got */
1737ba2be530Sab 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
1738ba2be530Sab 		},
1739ba2be530Sab 		{			/* Target_machsym */
1740ba2be530Sab 			NULL,			/* ms_reg_check */
1741ba2be530Sab 			NULL,			/* ms_mach_sym_typecheck */
1742ba2be530Sab 			NULL,			/* ms_is_regsym */
1743ba2be530Sab 			NULL,			/* ms_reg_find */
1744ba2be530Sab 			NULL			/* ms_reg_enter */
1745ba2be530Sab 		}
1746ba2be530Sab 	};
1747ba2be530Sab 
1748ba2be530Sab 	return (&_ld_targ);
1749ba2be530Sab }
1750